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 <string.h>
16 #include "vpx/internal/vpx_codec_internal.h"
17
18 #define SAVE_STATUS(ctx, var) (ctx ? (ctx->err = var) : var)
19
get_alg_priv(vpx_codec_ctx_t * ctx)20 static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) {
21 return (vpx_codec_alg_priv_t *)ctx->priv;
22 }
23
vpx_codec_dec_init_ver(vpx_codec_ctx_t * ctx,vpx_codec_iface_t * iface,const vpx_codec_dec_cfg_t * cfg,vpx_codec_flags_t flags,int ver)24 vpx_codec_err_t vpx_codec_dec_init_ver(vpx_codec_ctx_t *ctx,
25 vpx_codec_iface_t *iface,
26 const vpx_codec_dec_cfg_t *cfg,
27 vpx_codec_flags_t flags, int ver) {
28 vpx_codec_err_t res;
29
30 if (ver != VPX_DECODER_ABI_VERSION)
31 res = VPX_CODEC_ABI_MISMATCH;
32 else if (!ctx || !iface)
33 res = VPX_CODEC_INVALID_PARAM;
34 else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
35 res = VPX_CODEC_ABI_MISMATCH;
36 else if ((flags & VPX_CODEC_USE_POSTPROC) &&
37 !(iface->caps & VPX_CODEC_CAP_POSTPROC))
38 res = VPX_CODEC_INCAPABLE;
39 else if ((flags & VPX_CODEC_USE_ERROR_CONCEALMENT) &&
40 !(iface->caps & VPX_CODEC_CAP_ERROR_CONCEALMENT))
41 res = VPX_CODEC_INCAPABLE;
42 else if ((flags & VPX_CODEC_USE_INPUT_FRAGMENTS) &&
43 !(iface->caps & VPX_CODEC_CAP_INPUT_FRAGMENTS))
44 res = VPX_CODEC_INCAPABLE;
45 else if (!(iface->caps & VPX_CODEC_CAP_DECODER))
46 res = VPX_CODEC_INCAPABLE;
47 else {
48 memset(ctx, 0, sizeof(*ctx));
49 ctx->iface = iface;
50 ctx->name = iface->name;
51 ctx->priv = NULL;
52 ctx->init_flags = flags;
53 ctx->config.dec = cfg;
54
55 res = ctx->iface->init(ctx, NULL);
56 if (res) {
57 ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
58 vpx_codec_destroy(ctx);
59 }
60 }
61
62 return SAVE_STATUS(ctx, res);
63 }
64
vpx_codec_peek_stream_info(vpx_codec_iface_t * iface,const uint8_t * data,unsigned int data_sz,vpx_codec_stream_info_t * si)65 vpx_codec_err_t vpx_codec_peek_stream_info(vpx_codec_iface_t *iface,
66 const uint8_t *data,
67 unsigned int data_sz,
68 vpx_codec_stream_info_t *si) {
69 vpx_codec_err_t res;
70
71 if (!iface || !data || !data_sz || !si ||
72 si->sz < sizeof(vpx_codec_stream_info_t))
73 res = VPX_CODEC_INVALID_PARAM;
74 else {
75 /* Set default/unknown values */
76 si->w = 0;
77 si->h = 0;
78
79 res = iface->dec.peek_si(data, data_sz, si);
80 }
81
82 return res;
83 }
84
vpx_codec_get_stream_info(vpx_codec_ctx_t * ctx,vpx_codec_stream_info_t * si)85 vpx_codec_err_t vpx_codec_get_stream_info(vpx_codec_ctx_t *ctx,
86 vpx_codec_stream_info_t *si) {
87 vpx_codec_err_t res;
88
89 if (!ctx || !si || si->sz < sizeof(vpx_codec_stream_info_t))
90 res = VPX_CODEC_INVALID_PARAM;
91 else if (!ctx->iface || !ctx->priv)
92 res = VPX_CODEC_ERROR;
93 else {
94 /* Set default/unknown values */
95 si->w = 0;
96 si->h = 0;
97
98 res = ctx->iface->dec.get_si(get_alg_priv(ctx), si);
99 }
100
101 return SAVE_STATUS(ctx, res);
102 }
103
vpx_codec_decode(vpx_codec_ctx_t * ctx,const uint8_t * data,unsigned int data_sz,void * user_priv,long deadline)104 vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, const uint8_t *data,
105 unsigned int data_sz, void *user_priv,
106 long deadline) {
107 vpx_codec_err_t res;
108
109 /* Sanity checks */
110 /* NULL data ptr allowed if data_sz is 0 too */
111 if (!ctx || (!data && data_sz) || (data && !data_sz))
112 res = VPX_CODEC_INVALID_PARAM;
113 else if (!ctx->iface || !ctx->priv)
114 res = VPX_CODEC_ERROR;
115 else {
116 res = ctx->iface->dec.decode(get_alg_priv(ctx), data, data_sz, user_priv,
117 deadline);
118 }
119
120 return SAVE_STATUS(ctx, res);
121 }
122
vpx_codec_get_frame(vpx_codec_ctx_t * ctx,vpx_codec_iter_t * iter)123 vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter) {
124 vpx_image_t *img;
125
126 if (!ctx || !iter || !ctx->iface || !ctx->priv)
127 img = NULL;
128 else
129 img = ctx->iface->dec.get_frame(get_alg_priv(ctx), iter);
130
131 return img;
132 }
133
vpx_codec_register_put_frame_cb(vpx_codec_ctx_t * ctx,vpx_codec_put_frame_cb_fn_t cb,void * user_priv)134 vpx_codec_err_t vpx_codec_register_put_frame_cb(vpx_codec_ctx_t *ctx,
135 vpx_codec_put_frame_cb_fn_t cb,
136 void *user_priv) {
137 vpx_codec_err_t res;
138
139 if (!ctx || !cb)
140 res = VPX_CODEC_INVALID_PARAM;
141 else if (!ctx->iface || !ctx->priv)
142 res = VPX_CODEC_ERROR;
143 else if (!(ctx->iface->caps & VPX_CODEC_CAP_PUT_FRAME))
144 res = VPX_CODEC_INCAPABLE;
145 else {
146 ctx->priv->dec.put_frame_cb.u.put_frame = cb;
147 ctx->priv->dec.put_frame_cb.user_priv = user_priv;
148 res = VPX_CODEC_OK;
149 }
150
151 return SAVE_STATUS(ctx, res);
152 }
153
vpx_codec_register_put_slice_cb(vpx_codec_ctx_t * ctx,vpx_codec_put_slice_cb_fn_t cb,void * user_priv)154 vpx_codec_err_t vpx_codec_register_put_slice_cb(vpx_codec_ctx_t *ctx,
155 vpx_codec_put_slice_cb_fn_t cb,
156 void *user_priv) {
157 vpx_codec_err_t res;
158
159 if (!ctx || !cb)
160 res = VPX_CODEC_INVALID_PARAM;
161 else if (!ctx->iface || !ctx->priv)
162 res = VPX_CODEC_ERROR;
163 else if (!(ctx->iface->caps & VPX_CODEC_CAP_PUT_SLICE))
164 res = VPX_CODEC_INCAPABLE;
165 else {
166 ctx->priv->dec.put_slice_cb.u.put_slice = cb;
167 ctx->priv->dec.put_slice_cb.user_priv = user_priv;
168 res = VPX_CODEC_OK;
169 }
170
171 return SAVE_STATUS(ctx, res);
172 }
173
vpx_codec_set_frame_buffer_functions(vpx_codec_ctx_t * ctx,vpx_get_frame_buffer_cb_fn_t cb_get,vpx_release_frame_buffer_cb_fn_t cb_release,void * cb_priv)174 vpx_codec_err_t vpx_codec_set_frame_buffer_functions(
175 vpx_codec_ctx_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get,
176 vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
177 vpx_codec_err_t res;
178
179 if (!ctx || !cb_get || !cb_release) {
180 res = VPX_CODEC_INVALID_PARAM;
181 } else if (!ctx->iface || !ctx->priv) {
182 res = VPX_CODEC_ERROR;
183 } else if (!(ctx->iface->caps & VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER)) {
184 res = VPX_CODEC_INCAPABLE;
185 } else {
186 res = ctx->iface->dec.set_fb_fn(get_alg_priv(ctx), cb_get, cb_release,
187 cb_priv);
188 }
189
190 return SAVE_STATUS(ctx, res);
191 }
192