xref: /aosp_15_r20/external/libaom/aom/src/aom_decoder.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker /*!\file
13*77c1e3ccSAndroid Build Coastguard Worker  * \brief Provides the high level interface to wrap decoder algorithms.
14*77c1e3ccSAndroid Build Coastguard Worker  *
15*77c1e3ccSAndroid Build Coastguard Worker  */
16*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
17*77c1e3ccSAndroid Build Coastguard Worker #include "aom/internal/aom_codec_internal.h"
18*77c1e3ccSAndroid Build Coastguard Worker 
19*77c1e3ccSAndroid Build Coastguard Worker #define SAVE_STATUS(ctx, var) (ctx ? (ctx->err = var) : var)
20*77c1e3ccSAndroid Build Coastguard Worker 
get_alg_priv(aom_codec_ctx_t * ctx)21*77c1e3ccSAndroid Build Coastguard Worker static aom_codec_alg_priv_t *get_alg_priv(aom_codec_ctx_t *ctx) {
22*77c1e3ccSAndroid Build Coastguard Worker   return (aom_codec_alg_priv_t *)ctx->priv;
23*77c1e3ccSAndroid Build Coastguard Worker }
24*77c1e3ccSAndroid Build Coastguard Worker 
aom_codec_dec_init_ver(aom_codec_ctx_t * ctx,aom_codec_iface_t * iface,const aom_codec_dec_cfg_t * cfg,aom_codec_flags_t flags,int ver)25*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_codec_dec_init_ver(aom_codec_ctx_t *ctx,
26*77c1e3ccSAndroid Build Coastguard Worker                                        aom_codec_iface_t *iface,
27*77c1e3ccSAndroid Build Coastguard Worker                                        const aom_codec_dec_cfg_t *cfg,
28*77c1e3ccSAndroid Build Coastguard Worker                                        aom_codec_flags_t flags, int ver) {
29*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res;
30*77c1e3ccSAndroid Build Coastguard Worker 
31*77c1e3ccSAndroid Build Coastguard Worker   if (ver != AOM_DECODER_ABI_VERSION)
32*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_ABI_MISMATCH;
33*77c1e3ccSAndroid Build Coastguard Worker   else if (!ctx || !iface)
34*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_INVALID_PARAM;
35*77c1e3ccSAndroid Build Coastguard Worker   else if (iface->abi_version != AOM_CODEC_INTERNAL_ABI_VERSION)
36*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_ABI_MISMATCH;
37*77c1e3ccSAndroid Build Coastguard Worker   else if (!(iface->caps & AOM_CODEC_CAP_DECODER))
38*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_INCAPABLE;
39*77c1e3ccSAndroid Build Coastguard Worker   else {
40*77c1e3ccSAndroid Build Coastguard Worker     memset(ctx, 0, sizeof(*ctx));
41*77c1e3ccSAndroid Build Coastguard Worker     ctx->iface = iface;
42*77c1e3ccSAndroid Build Coastguard Worker     ctx->name = iface->name;
43*77c1e3ccSAndroid Build Coastguard Worker     ctx->priv = NULL;
44*77c1e3ccSAndroid Build Coastguard Worker     ctx->init_flags = flags;
45*77c1e3ccSAndroid Build Coastguard Worker     ctx->config.dec = cfg;
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker     res = ctx->iface->init(ctx);
48*77c1e3ccSAndroid Build Coastguard Worker     if (res) {
49*77c1e3ccSAndroid Build Coastguard Worker       ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
50*77c1e3ccSAndroid Build Coastguard Worker       aom_codec_destroy(ctx);
51*77c1e3ccSAndroid Build Coastguard Worker     }
52*77c1e3ccSAndroid Build Coastguard Worker   }
53*77c1e3ccSAndroid Build Coastguard Worker 
54*77c1e3ccSAndroid Build Coastguard Worker   return SAVE_STATUS(ctx, res);
55*77c1e3ccSAndroid Build Coastguard Worker }
56*77c1e3ccSAndroid Build Coastguard Worker 
aom_codec_peek_stream_info(aom_codec_iface_t * iface,const uint8_t * data,size_t data_sz,aom_codec_stream_info_t * si)57*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_codec_peek_stream_info(aom_codec_iface_t *iface,
58*77c1e3ccSAndroid Build Coastguard Worker                                            const uint8_t *data, size_t data_sz,
59*77c1e3ccSAndroid Build Coastguard Worker                                            aom_codec_stream_info_t *si) {
60*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res;
61*77c1e3ccSAndroid Build Coastguard Worker 
62*77c1e3ccSAndroid Build Coastguard Worker   if (!iface || !data || !data_sz || !si) {
63*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_INVALID_PARAM;
64*77c1e3ccSAndroid Build Coastguard Worker   } else {
65*77c1e3ccSAndroid Build Coastguard Worker     /* Set default/unknown values */
66*77c1e3ccSAndroid Build Coastguard Worker     si->w = 0;
67*77c1e3ccSAndroid Build Coastguard Worker     si->h = 0;
68*77c1e3ccSAndroid Build Coastguard Worker 
69*77c1e3ccSAndroid Build Coastguard Worker     res = iface->dec.peek_si(data, data_sz, si);
70*77c1e3ccSAndroid Build Coastguard Worker   }
71*77c1e3ccSAndroid Build Coastguard Worker 
72*77c1e3ccSAndroid Build Coastguard Worker   return res;
73*77c1e3ccSAndroid Build Coastguard Worker }
74*77c1e3ccSAndroid Build Coastguard Worker 
aom_codec_get_stream_info(aom_codec_ctx_t * ctx,aom_codec_stream_info_t * si)75*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_codec_get_stream_info(aom_codec_ctx_t *ctx,
76*77c1e3ccSAndroid Build Coastguard Worker                                           aom_codec_stream_info_t *si) {
77*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res;
78*77c1e3ccSAndroid Build Coastguard Worker 
79*77c1e3ccSAndroid Build Coastguard Worker   if (!ctx || !si) {
80*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_INVALID_PARAM;
81*77c1e3ccSAndroid Build Coastguard Worker   } else if (!ctx->iface || !ctx->priv) {
82*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_ERROR;
83*77c1e3ccSAndroid Build Coastguard Worker   } else {
84*77c1e3ccSAndroid Build Coastguard Worker     /* Set default/unknown values */
85*77c1e3ccSAndroid Build Coastguard Worker     si->w = 0;
86*77c1e3ccSAndroid Build Coastguard Worker     si->h = 0;
87*77c1e3ccSAndroid Build Coastguard Worker 
88*77c1e3ccSAndroid Build Coastguard Worker     res = ctx->iface->dec.get_si(get_alg_priv(ctx), si);
89*77c1e3ccSAndroid Build Coastguard Worker   }
90*77c1e3ccSAndroid Build Coastguard Worker 
91*77c1e3ccSAndroid Build Coastguard Worker   return SAVE_STATUS(ctx, res);
92*77c1e3ccSAndroid Build Coastguard Worker }
93*77c1e3ccSAndroid Build Coastguard Worker 
aom_codec_decode(aom_codec_ctx_t * ctx,const uint8_t * data,size_t data_sz,void * user_priv)94*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_codec_decode(aom_codec_ctx_t *ctx, const uint8_t *data,
95*77c1e3ccSAndroid Build Coastguard Worker                                  size_t data_sz, void *user_priv) {
96*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res;
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker   if (!ctx)
99*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_INVALID_PARAM;
100*77c1e3ccSAndroid Build Coastguard Worker   else if (!ctx->iface || !ctx->priv)
101*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_ERROR;
102*77c1e3ccSAndroid Build Coastguard Worker   else {
103*77c1e3ccSAndroid Build Coastguard Worker     res = ctx->iface->dec.decode(get_alg_priv(ctx), data, data_sz, user_priv);
104*77c1e3ccSAndroid Build Coastguard Worker   }
105*77c1e3ccSAndroid Build Coastguard Worker 
106*77c1e3ccSAndroid Build Coastguard Worker   return SAVE_STATUS(ctx, res);
107*77c1e3ccSAndroid Build Coastguard Worker }
108*77c1e3ccSAndroid Build Coastguard Worker 
aom_codec_get_frame(aom_codec_ctx_t * ctx,aom_codec_iter_t * iter)109*77c1e3ccSAndroid Build Coastguard Worker aom_image_t *aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter) {
110*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t *img;
111*77c1e3ccSAndroid Build Coastguard Worker 
112*77c1e3ccSAndroid Build Coastguard Worker   if (!ctx || !iter || !ctx->iface || !ctx->priv)
113*77c1e3ccSAndroid Build Coastguard Worker     img = NULL;
114*77c1e3ccSAndroid Build Coastguard Worker   else
115*77c1e3ccSAndroid Build Coastguard Worker     img = ctx->iface->dec.get_frame(get_alg_priv(ctx), iter);
116*77c1e3ccSAndroid Build Coastguard Worker 
117*77c1e3ccSAndroid Build Coastguard Worker   return img;
118*77c1e3ccSAndroid Build Coastguard Worker }
119*77c1e3ccSAndroid Build Coastguard Worker 
aom_codec_set_frame_buffer_functions(aom_codec_ctx_t * ctx,aom_get_frame_buffer_cb_fn_t cb_get,aom_release_frame_buffer_cb_fn_t cb_release,void * cb_priv)120*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_codec_set_frame_buffer_functions(
121*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_ctx_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get,
122*77c1e3ccSAndroid Build Coastguard Worker     aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
123*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t res;
124*77c1e3ccSAndroid Build Coastguard Worker 
125*77c1e3ccSAndroid Build Coastguard Worker   if (!ctx || !cb_get || !cb_release) {
126*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_INVALID_PARAM;
127*77c1e3ccSAndroid Build Coastguard Worker   } else if (!ctx->iface || !ctx->priv) {
128*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_ERROR;
129*77c1e3ccSAndroid Build Coastguard Worker   } else if (!(ctx->iface->caps & AOM_CODEC_CAP_EXTERNAL_FRAME_BUFFER)) {
130*77c1e3ccSAndroid Build Coastguard Worker     res = AOM_CODEC_INCAPABLE;
131*77c1e3ccSAndroid Build Coastguard Worker   } else {
132*77c1e3ccSAndroid Build Coastguard Worker     res = ctx->iface->dec.set_fb_fn(get_alg_priv(ctx), cb_get, cb_release,
133*77c1e3ccSAndroid Build Coastguard Worker                                     cb_priv);
134*77c1e3ccSAndroid Build Coastguard Worker   }
135*77c1e3ccSAndroid Build Coastguard Worker 
136*77c1e3ccSAndroid Build Coastguard Worker   return SAVE_STATUS(ctx, res);
137*77c1e3ccSAndroid Build Coastguard Worker }
138