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
12 /*!\file
13 * \brief Provides the high level interface to wrap decoder algorithms.
14 *
15 */
16 #include <stdarg.h>
17 #include <stdlib.h>
18 #include "vpx/vpx_integer.h"
19 #include "vpx/internal/vpx_codec_internal.h"
20 #include "vpx_version.h"
21
22 #define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var)
23
vpx_codec_version(void)24 int vpx_codec_version(void) {
25 return VERSION_PACKED;
26 }
27
28
vpx_codec_version_str(void)29 const char *vpx_codec_version_str(void) {
30 return VERSION_STRING_NOSP;
31 }
32
33
vpx_codec_version_extra_str(void)34 const char *vpx_codec_version_extra_str(void) {
35 return VERSION_EXTRA;
36 }
37
38
vpx_codec_iface_name(vpx_codec_iface_t * iface)39 const char *vpx_codec_iface_name(vpx_codec_iface_t *iface) {
40 return iface ? iface->name : "<invalid interface>";
41 }
42
vpx_codec_err_to_string(vpx_codec_err_t err)43 const char *vpx_codec_err_to_string(vpx_codec_err_t err) {
44 switch (err) {
45 case VPX_CODEC_OK:
46 return "Success";
47 case VPX_CODEC_ERROR:
48 return "Unspecified internal error";
49 case VPX_CODEC_MEM_ERROR:
50 return "Memory allocation error";
51 case VPX_CODEC_ABI_MISMATCH:
52 return "ABI version mismatch";
53 case VPX_CODEC_INCAPABLE:
54 return "Codec does not implement requested capability";
55 case VPX_CODEC_UNSUP_BITSTREAM:
56 return "Bitstream not supported by this decoder";
57 case VPX_CODEC_UNSUP_FEATURE:
58 return "Bitstream required feature not supported by this decoder";
59 case VPX_CODEC_CORRUPT_FRAME:
60 return "Corrupt frame detected";
61 case VPX_CODEC_INVALID_PARAM:
62 return "Invalid parameter";
63 case VPX_CODEC_LIST_END:
64 return "End of iterated list";
65 }
66
67 return "Unrecognized error code";
68 }
69
vpx_codec_error(vpx_codec_ctx_t * ctx)70 const char *vpx_codec_error(vpx_codec_ctx_t *ctx) {
71 return (ctx) ? vpx_codec_err_to_string(ctx->err)
72 : vpx_codec_err_to_string(VPX_CODEC_INVALID_PARAM);
73 }
74
vpx_codec_error_detail(vpx_codec_ctx_t * ctx)75 const char *vpx_codec_error_detail(vpx_codec_ctx_t *ctx) {
76 if (ctx && ctx->err)
77 return ctx->priv ? ctx->priv->err_detail : ctx->err_detail;
78
79 return NULL;
80 }
81
82
vpx_codec_destroy(vpx_codec_ctx_t * ctx)83 vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx) {
84 vpx_codec_err_t res;
85
86 if (!ctx)
87 res = VPX_CODEC_INVALID_PARAM;
88 else if (!ctx->iface || !ctx->priv)
89 res = VPX_CODEC_ERROR;
90 else {
91 if (ctx->priv->alg_priv)
92 ctx->iface->destroy(ctx->priv->alg_priv);
93
94 ctx->iface = NULL;
95 ctx->name = NULL;
96 ctx->priv = NULL;
97 res = VPX_CODEC_OK;
98 }
99
100 return SAVE_STATUS(ctx, res);
101 }
102
103
vpx_codec_get_caps(vpx_codec_iface_t * iface)104 vpx_codec_caps_t vpx_codec_get_caps(vpx_codec_iface_t *iface) {
105 return (iface) ? iface->caps : 0;
106 }
107
108
vpx_codec_control_(vpx_codec_ctx_t * ctx,int ctrl_id,...)109 vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx,
110 int ctrl_id,
111 ...) {
112 vpx_codec_err_t res;
113
114 if (!ctx || !ctrl_id)
115 res = VPX_CODEC_INVALID_PARAM;
116 else if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps)
117 res = VPX_CODEC_ERROR;
118 else {
119 vpx_codec_ctrl_fn_map_t *entry;
120
121 res = VPX_CODEC_ERROR;
122
123 for (entry = ctx->iface->ctrl_maps; entry && entry->fn; entry++) {
124 if (!entry->ctrl_id || entry->ctrl_id == ctrl_id) {
125 va_list ap;
126
127 va_start(ap, ctrl_id);
128 res = entry->fn(ctx->priv->alg_priv, ap);
129 va_end(ap);
130 break;
131 }
132 }
133 }
134
135 return SAVE_STATUS(ctx, res);
136 }
137
vpx_internal_error(struct vpx_internal_error_info * info,vpx_codec_err_t error,const char * fmt,...)138 void vpx_internal_error(struct vpx_internal_error_info *info,
139 vpx_codec_err_t error,
140 const char *fmt,
141 ...) {
142 va_list ap;
143
144 info->error_code = error;
145 info->has_detail = 0;
146
147 if (fmt) {
148 size_t sz = sizeof(info->detail);
149
150 info->has_detail = 1;
151 va_start(ap, fmt);
152 vsnprintf(info->detail, sz - 1, fmt, ap);
153 va_end(ap);
154 info->detail[sz - 1] = '\0';
155 }
156
157 if (info->setjmp)
158 longjmp(info->jmp, info->error_code);
159 }
160