1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /*!\file
12  * \brief Provides the high level interface to wrap decoder algorithms.
13  *
14  */
15 #include <stdarg.h>
16 #include <stdlib.h>
17 #include "vpx/vpx_integer.h"
18 #include "vpx/internal/vpx_codec_internal.h"
19 #include "vpx_version.h"
20 
21 #define SAVE_STATUS(ctx, var) (ctx ? (ctx->err = var) : var)
22 
vpx_codec_version(void)23 int vpx_codec_version(void) { return VERSION_PACKED; }
24 
vpx_codec_version_str(void)25 const char *vpx_codec_version_str(void) { return VERSION_STRING_NOSP; }
26 
vpx_codec_version_extra_str(void)27 const char *vpx_codec_version_extra_str(void) { return VERSION_EXTRA; }
28 
vpx_codec_iface_name(vpx_codec_iface_t * iface)29 const char *vpx_codec_iface_name(vpx_codec_iface_t *iface) {
30   return iface ? iface->name : "<invalid interface>";
31 }
32 
vpx_codec_err_to_string(vpx_codec_err_t err)33 const char *vpx_codec_err_to_string(vpx_codec_err_t err) {
34   switch (err) {
35     case VPX_CODEC_OK: return "Success";
36     case VPX_CODEC_ERROR: return "Unspecified internal error";
37     case VPX_CODEC_MEM_ERROR: return "Memory allocation error";
38     case VPX_CODEC_ABI_MISMATCH: return "ABI version mismatch";
39     case VPX_CODEC_INCAPABLE:
40       return "Codec does not implement requested capability";
41     case VPX_CODEC_UNSUP_BITSTREAM:
42       return "Bitstream not supported by this decoder";
43     case VPX_CODEC_UNSUP_FEATURE:
44       return "Bitstream required feature not supported by this decoder";
45     case VPX_CODEC_CORRUPT_FRAME: return "Corrupt frame detected";
46     case VPX_CODEC_INVALID_PARAM: return "Invalid parameter";
47     case VPX_CODEC_LIST_END: return "End of iterated list";
48   }
49 
50   return "Unrecognized error code";
51 }
52 
vpx_codec_error(vpx_codec_ctx_t * ctx)53 const char *vpx_codec_error(vpx_codec_ctx_t *ctx) {
54   return (ctx) ? vpx_codec_err_to_string(ctx->err)
55                : vpx_codec_err_to_string(VPX_CODEC_INVALID_PARAM);
56 }
57 
vpx_codec_error_detail(vpx_codec_ctx_t * ctx)58 const char *vpx_codec_error_detail(vpx_codec_ctx_t *ctx) {
59   if (ctx && ctx->err)
60     return ctx->priv ? ctx->priv->err_detail : ctx->err_detail;
61 
62   return NULL;
63 }
64 
vpx_codec_destroy(vpx_codec_ctx_t * ctx)65 vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx) {
66   vpx_codec_err_t res;
67 
68   if (!ctx)
69     res = VPX_CODEC_INVALID_PARAM;
70   else if (!ctx->iface || !ctx->priv)
71     res = VPX_CODEC_ERROR;
72   else {
73     ctx->iface->destroy((vpx_codec_alg_priv_t *)ctx->priv);
74 
75     ctx->iface = NULL;
76     ctx->name = NULL;
77     ctx->priv = NULL;
78     res = VPX_CODEC_OK;
79   }
80 
81   return SAVE_STATUS(ctx, res);
82 }
83 
vpx_codec_get_caps(vpx_codec_iface_t * iface)84 vpx_codec_caps_t vpx_codec_get_caps(vpx_codec_iface_t *iface) {
85   return (iface) ? iface->caps : 0;
86 }
87 
vpx_codec_control_(vpx_codec_ctx_t * ctx,int ctrl_id,...)88 vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx, int ctrl_id, ...) {
89   vpx_codec_err_t res;
90 
91   if (!ctx || !ctrl_id)
92     res = VPX_CODEC_INVALID_PARAM;
93   else if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps)
94     res = VPX_CODEC_ERROR;
95   else {
96     vpx_codec_ctrl_fn_map_t *entry;
97 
98     res = VPX_CODEC_INCAPABLE;
99 
100     for (entry = ctx->iface->ctrl_maps; entry && entry->fn; entry++) {
101       if (!entry->ctrl_id || entry->ctrl_id == ctrl_id) {
102         va_list ap;
103 
104         va_start(ap, ctrl_id);
105         res = entry->fn((vpx_codec_alg_priv_t *)ctx->priv, ap);
106         va_end(ap);
107         break;
108       }
109     }
110   }
111 
112   return SAVE_STATUS(ctx, res);
113 }
114 
vpx_internal_error(struct vpx_internal_error_info * info,vpx_codec_err_t error,const char * fmt,...)115 void vpx_internal_error(struct vpx_internal_error_info *info,
116                         vpx_codec_err_t error, const char *fmt, ...) {
117   va_list ap;
118 
119   info->error_code = error;
120   info->has_detail = 0;
121 
122   if (fmt) {
123     size_t sz = sizeof(info->detail);
124 
125     info->has_detail = 1;
126     va_start(ap, fmt);
127     vsnprintf(info->detail, sz - 1, fmt, ap);
128     va_end(ap);
129     info->detail[sz - 1] = '\0';
130   }
131 
132   if (info->setjmp) longjmp(info->jmp, info->error_code);
133 }
134