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 ctx->iface->destroy((vpx_codec_alg_priv_t *)ctx->priv);
92
93 ctx->iface = NULL;
94 ctx->name = NULL;
95 ctx->priv = NULL;
96 res = VPX_CODEC_OK;
97 }
98
99 return SAVE_STATUS(ctx, res);
100 }
101
102
vpx_codec_get_caps(vpx_codec_iface_t * iface)103 vpx_codec_caps_t vpx_codec_get_caps(vpx_codec_iface_t *iface) {
104 return (iface) ? iface->caps : 0;
105 }
106
107
vpx_codec_control_(vpx_codec_ctx_t * ctx,int ctrl_id,...)108 vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx,
109 int ctrl_id,
110 ...) {
111 vpx_codec_err_t res;
112
113 if (!ctx || !ctrl_id)
114 res = VPX_CODEC_INVALID_PARAM;
115 else if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps)
116 res = VPX_CODEC_ERROR;
117 else {
118 vpx_codec_ctrl_fn_map_t *entry;
119
120 res = VPX_CODEC_ERROR;
121
122 for (entry = ctx->iface->ctrl_maps; entry && entry->fn; entry++) {
123 if (!entry->ctrl_id || entry->ctrl_id == ctrl_id) {
124 va_list ap;
125
126 va_start(ap, ctrl_id);
127 res = entry->fn((vpx_codec_alg_priv_t *)ctx->priv, ap);
128 va_end(ap);
129 break;
130 }
131 }
132 }
133
134 return SAVE_STATUS(ctx, res);
135 }
136
vpx_internal_error(struct vpx_internal_error_info * info,vpx_codec_err_t error,const char * fmt,...)137 void vpx_internal_error(struct vpx_internal_error_info *info,
138 vpx_codec_err_t error,
139 const char *fmt,
140 ...) {
141 va_list ap;
142
143 info->error_code = error;
144 info->has_detail = 0;
145
146 if (fmt) {
147 size_t sz = sizeof(info->detail);
148
149 info->has_detail = 1;
150 va_start(ap, fmt);
151 vsnprintf(info->detail, sz - 1, fmt, ap);
152 va_end(ap);
153 info->detail[sz - 1] = '\0';
154 }
155
156 if (info->setjmp)
157 longjmp(info->jmp, info->error_code);
158 }
159