xref: /aosp_15_r20/external/libvpx/vpx/src/vpx_codec.c (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
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 <stdarg.h>
16 #include <stdio.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) { return VERSION_PACKED; }
25 
vpx_codec_version_str(void)26 const char *vpx_codec_version_str(void) { return VERSION_STRING_NOSP; }
27 
vpx_codec_version_extra_str(void)28 const char *vpx_codec_version_extra_str(void) { return VERSION_EXTRA; }
29 
vpx_codec_iface_name(vpx_codec_iface_t * iface)30 const char *vpx_codec_iface_name(vpx_codec_iface_t *iface) {
31   return iface ? iface->name : "<invalid interface>";
32 }
33 
vpx_codec_err_to_string(vpx_codec_err_t err)34 const char *vpx_codec_err_to_string(vpx_codec_err_t err) {
35   switch (err) {
36     case VPX_CODEC_OK: return "Success";
37     case VPX_CODEC_ERROR: return "Unspecified internal error";
38     case VPX_CODEC_MEM_ERROR: return "Memory allocation error";
39     case VPX_CODEC_ABI_MISMATCH: return "ABI version mismatch";
40     case VPX_CODEC_INCAPABLE:
41       return "Codec does not implement requested capability";
42     case VPX_CODEC_UNSUP_BITSTREAM:
43       return "Bitstream not supported by this decoder";
44     case VPX_CODEC_UNSUP_FEATURE:
45       return "Bitstream required feature not supported by this decoder";
46     case VPX_CODEC_CORRUPT_FRAME: return "Corrupt frame detected";
47     case VPX_CODEC_INVALID_PARAM: return "Invalid parameter";
48     case VPX_CODEC_LIST_END: return "End of iterated list";
49   }
50 
51   return "Unrecognized error code";
52 }
53 
vpx_codec_error(const vpx_codec_ctx_t * ctx)54 const char *vpx_codec_error(const vpx_codec_ctx_t *ctx) {
55   return (ctx) ? vpx_codec_err_to_string(ctx->err)
56                : vpx_codec_err_to_string(VPX_CODEC_INVALID_PARAM);
57 }
58 
vpx_codec_error_detail(const vpx_codec_ctx_t * ctx)59 const char *vpx_codec_error_detail(const vpx_codec_ctx_t *ctx) {
60   if (ctx && ctx->err)
61     return ctx->priv ? ctx->priv->err_detail : ctx->err_detail;
62 
63   return NULL;
64 }
65 
vpx_codec_destroy(vpx_codec_ctx_t * ctx)66 vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx) {
67   vpx_codec_err_t res;
68 
69   if (!ctx)
70     res = VPX_CODEC_INVALID_PARAM;
71   else if (!ctx->iface || !ctx->priv)
72     res = VPX_CODEC_ERROR;
73   else {
74     ctx->iface->destroy((vpx_codec_alg_priv_t *)ctx->priv);
75 
76     ctx->iface = NULL;
77     ctx->name = NULL;
78     ctx->priv = NULL;
79     res = VPX_CODEC_OK;
80   }
81 
82   return SAVE_STATUS(ctx, res);
83 }
84 
vpx_codec_get_caps(vpx_codec_iface_t * iface)85 vpx_codec_caps_t vpx_codec_get_caps(vpx_codec_iface_t *iface) {
86   return iface ? iface->caps : 0;
87 }
88 
vpx_codec_control_(vpx_codec_ctx_t * ctx,int ctrl_id,...)89 vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx, int ctrl_id, ...) {
90   vpx_codec_err_t res;
91 
92   if (!ctx || !ctrl_id)
93     res = VPX_CODEC_INVALID_PARAM;
94   else if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps)
95     res = VPX_CODEC_ERROR;
96   else {
97     vpx_codec_ctrl_fn_map_t *entry;
98 
99     res = VPX_CODEC_INCAPABLE;
100 
101     for (entry = ctx->iface->ctrl_maps; entry->fn; entry++) {
102       if (!entry->ctrl_id || entry->ctrl_id == ctrl_id) {
103         va_list ap;
104 
105         va_start(ap, ctrl_id);
106         res = entry->fn((vpx_codec_alg_priv_t *)ctx->priv, ap);
107         va_end(ap);
108         break;
109       }
110     }
111   }
112 
113   return SAVE_STATUS(ctx, res);
114 }
115 
vpx_internal_error(struct vpx_internal_error_info * info,vpx_codec_err_t error,const char * fmt,...)116 void vpx_internal_error(struct vpx_internal_error_info *info,
117                         vpx_codec_err_t error, const char *fmt, ...) {
118   va_list ap;
119 
120   info->error_code = error;
121   info->has_detail = 0;
122 
123   if (fmt) {
124     size_t sz = sizeof(info->detail);
125 
126     info->has_detail = 1;
127     va_start(ap, fmt);
128     vsnprintf(info->detail, sz - 1, fmt, ap);
129     va_end(ap);
130     info->detail[sz - 1] = '\0';
131   }
132 
133   if (info->setjmp) longjmp(info->jmp, info->error_code);
134 }
135