xref: /aosp_15_r20/external/libaom/aom/src/aom_codec.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 /*!\file
13  * \brief Provides the high level interface to wrap decoder algorithms.
14  *
15  */
16 #include <assert.h>
17 #include <stdarg.h>
18 #include <stdlib.h>
19 
20 #include "config/aom_config.h"
21 #include "config/aom_version.h"
22 
23 #include "aom/aom_integer.h"
24 #include "aom/internal/aom_codec_internal.h"
25 
aom_codec_version(void)26 int aom_codec_version(void) { return VERSION_PACKED; }
27 
aom_codec_version_str(void)28 const char *aom_codec_version_str(void) { return VERSION_STRING_NOSP; }
29 
aom_codec_version_extra_str(void)30 const char *aom_codec_version_extra_str(void) { return VERSION_EXTRA; }
31 
aom_codec_iface_name(aom_codec_iface_t * iface)32 const char *aom_codec_iface_name(aom_codec_iface_t *iface) {
33   return iface ? iface->name : "<invalid interface>";
34 }
35 
aom_codec_err_to_string(aom_codec_err_t err)36 const char *aom_codec_err_to_string(aom_codec_err_t err) {
37   switch (err) {
38     case AOM_CODEC_OK: return "Success";
39     case AOM_CODEC_ERROR: return "Unspecified internal error";
40     case AOM_CODEC_MEM_ERROR: return "Memory allocation error";
41     case AOM_CODEC_ABI_MISMATCH: return "ABI version mismatch";
42     case AOM_CODEC_INCAPABLE:
43       return "Codec does not implement requested capability";
44     case AOM_CODEC_UNSUP_BITSTREAM:
45       return "Bitstream not supported by this decoder";
46     case AOM_CODEC_UNSUP_FEATURE:
47       return "Bitstream required feature not supported by this decoder";
48     case AOM_CODEC_CORRUPT_FRAME: return "Corrupt frame detected";
49     case AOM_CODEC_INVALID_PARAM: return "Invalid parameter";
50     case AOM_CODEC_LIST_END: return "End of iterated list";
51   }
52 
53   return "Unrecognized error code";
54 }
55 
aom_codec_error(const aom_codec_ctx_t * ctx)56 const char *aom_codec_error(const aom_codec_ctx_t *ctx) {
57   return (ctx) ? aom_codec_err_to_string(ctx->err)
58                : aom_codec_err_to_string(AOM_CODEC_INVALID_PARAM);
59 }
60 
aom_codec_error_detail(const aom_codec_ctx_t * ctx)61 const char *aom_codec_error_detail(const aom_codec_ctx_t *ctx) {
62   if (ctx && ctx->err)
63     return ctx->priv ? ctx->priv->err_detail : ctx->err_detail;
64 
65   return NULL;
66 }
67 
aom_codec_destroy(aom_codec_ctx_t * ctx)68 aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx) {
69   if (!ctx) {
70     return AOM_CODEC_INVALID_PARAM;
71   }
72   if (!ctx->iface || !ctx->priv) {
73     ctx->err = AOM_CODEC_ERROR;
74     return AOM_CODEC_ERROR;
75   }
76   ctx->iface->destroy((aom_codec_alg_priv_t *)ctx->priv);
77   ctx->iface = NULL;
78   ctx->name = NULL;
79   ctx->priv = NULL;
80   ctx->err = AOM_CODEC_OK;
81   return AOM_CODEC_OK;
82 }
83 
aom_codec_get_caps(aom_codec_iface_t * iface)84 aom_codec_caps_t aom_codec_get_caps(aom_codec_iface_t *iface) {
85   return iface ? iface->caps : 0;
86 }
87 
aom_codec_control(aom_codec_ctx_t * ctx,int ctrl_id,...)88 aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id, ...) {
89   if (!ctx) {
90     return AOM_CODEC_INVALID_PARAM;
91   }
92   // Control ID must be non-zero.
93   if (!ctrl_id) {
94     ctx->err = AOM_CODEC_INVALID_PARAM;
95     return AOM_CODEC_INVALID_PARAM;
96   }
97   if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps) {
98     ctx->err = AOM_CODEC_ERROR;
99     return AOM_CODEC_ERROR;
100   }
101 
102   // "ctrl_maps" is an array of (control ID, function pointer) elements,
103   // with CTRL_MAP_END as a sentinel.
104   for (aom_codec_ctrl_fn_map_t *entry = ctx->iface->ctrl_maps;
105        !at_ctrl_map_end(entry); ++entry) {
106     if (entry->ctrl_id == ctrl_id) {
107       va_list ap;
108       va_start(ap, ctrl_id);
109       ctx->err = entry->fn((aom_codec_alg_priv_t *)ctx->priv, ap);
110       va_end(ap);
111       return ctx->err;
112     }
113   }
114   ctx->err = AOM_CODEC_ERROR;
115   ctx->priv->err_detail = "Invalid control ID";
116   return AOM_CODEC_ERROR;
117 }
118 
aom_codec_set_option(aom_codec_ctx_t * ctx,const char * name,const char * value)119 aom_codec_err_t aom_codec_set_option(aom_codec_ctx_t *ctx, const char *name,
120                                      const char *value) {
121   if (!ctx) {
122     return AOM_CODEC_INVALID_PARAM;
123   }
124   if (!ctx->iface || !ctx->priv || !ctx->iface->set_option) {
125     ctx->err = AOM_CODEC_ERROR;
126     return AOM_CODEC_ERROR;
127   }
128   ctx->err =
129       ctx->iface->set_option((aom_codec_alg_priv_t *)ctx->priv, name, value);
130   return ctx->err;
131 }
132 
133 LIBAOM_FORMAT_PRINTF(3, 0)
set_error(struct aom_internal_error_info * info,aom_codec_err_t error,const char * fmt,va_list ap)134 static void set_error(struct aom_internal_error_info *info,
135                       aom_codec_err_t error, const char *fmt, va_list ap) {
136   info->error_code = error;
137   info->has_detail = 0;
138 
139   if (fmt) {
140     size_t sz = sizeof(info->detail);
141 
142     info->has_detail = 1;
143     vsnprintf(info->detail, sz - 1, fmt, ap);
144     info->detail[sz - 1] = '\0';
145   }
146 }
147 
aom_set_error(struct aom_internal_error_info * info,aom_codec_err_t error,const char * fmt,...)148 void aom_set_error(struct aom_internal_error_info *info, aom_codec_err_t error,
149                    const char *fmt, ...) {
150   va_list ap;
151 
152   va_start(ap, fmt);
153   set_error(info, error, fmt, ap);
154   va_end(ap);
155 
156   assert(!info->setjmp);
157 }
158 
aom_internal_error(struct aom_internal_error_info * info,aom_codec_err_t error,const char * fmt,...)159 void aom_internal_error(struct aom_internal_error_info *info,
160                         aom_codec_err_t error, const char *fmt, ...) {
161   va_list ap;
162 
163   va_start(ap, fmt);
164   set_error(info, error, fmt, ap);
165   va_end(ap);
166 
167   if (info->setjmp) longjmp(info->jmp, info->error_code);
168 }
169 
aom_internal_error_copy(struct aom_internal_error_info * info,const struct aom_internal_error_info * src)170 void aom_internal_error_copy(struct aom_internal_error_info *info,
171                              const struct aom_internal_error_info *src) {
172   assert(info != src);
173   assert(!src->setjmp);
174 
175   if (!src->has_detail) {
176     aom_internal_error(info, src->error_code, NULL);
177   } else {
178     aom_internal_error(info, src->error_code, "%s", src->detail);
179   }
180 }
181 
aom_merge_corrupted_flag(int * corrupted,int value)182 void aom_merge_corrupted_flag(int *corrupted, int value) {
183   *corrupted |= value;
184 }
185 
aom_obu_type_to_string(OBU_TYPE type)186 const char *aom_obu_type_to_string(OBU_TYPE type) {
187   switch (type) {
188     case OBU_SEQUENCE_HEADER: return "OBU_SEQUENCE_HEADER";
189     case OBU_TEMPORAL_DELIMITER: return "OBU_TEMPORAL_DELIMITER";
190     case OBU_FRAME_HEADER: return "OBU_FRAME_HEADER";
191     case OBU_REDUNDANT_FRAME_HEADER: return "OBU_REDUNDANT_FRAME_HEADER";
192     case OBU_FRAME: return "OBU_FRAME";
193     case OBU_TILE_GROUP: return "OBU_TILE_GROUP";
194     case OBU_METADATA: return "OBU_METADATA";
195     case OBU_TILE_LIST: return "OBU_TILE_LIST";
196     case OBU_PADDING: return "OBU_PADDING";
197     default: break;
198   }
199   return "<Invalid OBU Type>";
200 }
201