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 <stdarg.h>
17 #include <stdlib.h>
18 
19 #include "config/aom_config.h"
20 #include "config/aom_version.h"
21 
22 #include "aom/aom_integer.h"
23 #include "aom/internal/aom_codec_internal.h"
24 
aom_codec_version(void)25 int aom_codec_version(void) { return VERSION_PACKED; }
26 
aom_codec_version_str(void)27 const char *aom_codec_version_str(void) { return VERSION_STRING_NOSP; }
28 
aom_codec_version_extra_str(void)29 const char *aom_codec_version_extra_str(void) { return VERSION_EXTRA; }
30 
aom_codec_iface_name(aom_codec_iface_t * iface)31 const char *aom_codec_iface_name(aom_codec_iface_t *iface) {
32   return iface ? iface->name : "<invalid interface>";
33 }
34 
aom_codec_err_to_string(aom_codec_err_t err)35 const char *aom_codec_err_to_string(aom_codec_err_t err) {
36   switch (err) {
37     case AOM_CODEC_OK: return "Success";
38     case AOM_CODEC_ERROR: return "Unspecified internal error";
39     case AOM_CODEC_MEM_ERROR: return "Memory allocation error";
40     case AOM_CODEC_ABI_MISMATCH: return "ABI version mismatch";
41     case AOM_CODEC_INCAPABLE:
42       return "Codec does not implement requested capability";
43     case AOM_CODEC_UNSUP_BITSTREAM:
44       return "Bitstream not supported by this decoder";
45     case AOM_CODEC_UNSUP_FEATURE:
46       return "Bitstream required feature not supported by this decoder";
47     case AOM_CODEC_CORRUPT_FRAME: return "Corrupt frame detected";
48     case AOM_CODEC_INVALID_PARAM: return "Invalid parameter";
49     case AOM_CODEC_LIST_END: return "End of iterated list";
50   }
51 
52   return "Unrecognized error code";
53 }
54 
aom_codec_error(aom_codec_ctx_t * ctx)55 const char *aom_codec_error(aom_codec_ctx_t *ctx) {
56   return (ctx) ? aom_codec_err_to_string(ctx->err)
57                : aom_codec_err_to_string(AOM_CODEC_INVALID_PARAM);
58 }
59 
aom_codec_error_detail(aom_codec_ctx_t * ctx)60 const char *aom_codec_error_detail(aom_codec_ctx_t *ctx) {
61   if (ctx && ctx->err)
62     return ctx->priv ? ctx->priv->err_detail : ctx->err_detail;
63 
64   return NULL;
65 }
66 
aom_codec_destroy(aom_codec_ctx_t * ctx)67 aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx) {
68   if (!ctx) {
69     return AOM_CODEC_INVALID_PARAM;
70   }
71   if (!ctx->iface || !ctx->priv) {
72     ctx->err = AOM_CODEC_ERROR;
73     return AOM_CODEC_ERROR;
74   }
75   ctx->iface->destroy((aom_codec_alg_priv_t *)ctx->priv);
76   ctx->iface = NULL;
77   ctx->name = NULL;
78   ctx->priv = NULL;
79   ctx->err = AOM_CODEC_OK;
80   return AOM_CODEC_OK;
81 }
82 
aom_codec_get_caps(aom_codec_iface_t * iface)83 aom_codec_caps_t aom_codec_get_caps(aom_codec_iface_t *iface) {
84   return (iface) ? iface->caps : 0;
85 }
86 
aom_codec_control(aom_codec_ctx_t * ctx,int ctrl_id,...)87 aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id, ...) {
88   if (!ctx) {
89     return AOM_CODEC_INVALID_PARAM;
90   }
91   // Control ID must be non-zero.
92   if (!ctrl_id) {
93     ctx->err = AOM_CODEC_INVALID_PARAM;
94     return AOM_CODEC_INVALID_PARAM;
95   }
96   if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps) {
97     ctx->err = AOM_CODEC_ERROR;
98     return AOM_CODEC_ERROR;
99   }
100 
101   // "ctrl_maps" is an array of (control ID, function pointer) elements,
102   // with CTRL_MAP_END as a sentinel.
103   for (aom_codec_ctrl_fn_map_t *entry = ctx->iface->ctrl_maps;
104        !at_ctrl_map_end(entry); ++entry) {
105     if (entry->ctrl_id == ctrl_id) {
106       va_list ap;
107       va_start(ap, ctrl_id);
108       ctx->err = entry->fn((aom_codec_alg_priv_t *)ctx->priv, ap);
109       va_end(ap);
110       return ctx->err;
111     }
112   }
113   ctx->err = AOM_CODEC_ERROR;
114   return AOM_CODEC_ERROR;
115 }
116 
aom_internal_error(struct aom_internal_error_info * info,aom_codec_err_t error,const char * fmt,...)117 void aom_internal_error(struct aom_internal_error_info *info,
118                         aom_codec_err_t error, const char *fmt, ...) {
119   va_list ap;
120 
121   info->error_code = error;
122   info->has_detail = 0;
123 
124   if (fmt) {
125     size_t sz = sizeof(info->detail);
126 
127     info->has_detail = 1;
128     va_start(ap, fmt);
129     vsnprintf(info->detail, sz - 1, fmt, ap);
130     va_end(ap);
131     info->detail[sz - 1] = '\0';
132   }
133 
134   if (info->setjmp) longjmp(info->jmp, info->error_code);
135 }
136 
aom_merge_corrupted_flag(int * corrupted,int value)137 void aom_merge_corrupted_flag(int *corrupted, int value) {
138   *corrupted |= value;
139 }
140 
aom_obu_type_to_string(OBU_TYPE type)141 const char *aom_obu_type_to_string(OBU_TYPE type) {
142   switch (type) {
143     case OBU_SEQUENCE_HEADER: return "OBU_SEQUENCE_HEADER";
144     case OBU_TEMPORAL_DELIMITER: return "OBU_TEMPORAL_DELIMITER";
145     case OBU_FRAME_HEADER: return "OBU_FRAME_HEADER";
146     case OBU_REDUNDANT_FRAME_HEADER: return "OBU_REDUNDANT_FRAME_HEADER";
147     case OBU_FRAME: return "OBU_FRAME";
148     case OBU_TILE_GROUP: return "OBU_TILE_GROUP";
149     case OBU_METADATA: return "OBU_METADATA";
150     case OBU_TILE_LIST: return "OBU_TILE_LIST";
151     case OBU_PADDING: return "OBU_PADDING";
152     default: break;
153   }
154   return "<Invalid OBU Type>";
155 }
156