aom_codec.c 3.84 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
John Koleszar's avatar
John Koleszar committed
3
 *
4 5 6 7 8 9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
John Koleszar's avatar
John Koleszar committed
10 11
 */

James Zern's avatar
James Zern committed
12
/*!\file
John Koleszar's avatar
John Koleszar committed
13 14 15
 * \brief Provides the high level interface to wrap decoder algorithms.
 *
 */
16
#include <stdarg.h>
17
#include <stdlib.h>
Adrian Grange's avatar
Adrian Grange committed
18 19 20
#include "aom/aom_integer.h"
#include "aom/internal/aom_codec_internal.h"
#include "aom_version.h"
John Koleszar's avatar
John Koleszar committed
21

clang-format's avatar
clang-format committed
22
#define SAVE_STATUS(ctx, var) (ctx ? (ctx->err = var) : var)
John Koleszar's avatar
John Koleszar committed
23

Adrian Grange's avatar
Adrian Grange committed
24
int aom_codec_version(void) { return VERSION_PACKED; }
John Koleszar's avatar
John Koleszar committed
25

Adrian Grange's avatar
Adrian Grange committed
26
const char *aom_codec_version_str(void) { return VERSION_STRING_NOSP; }
John Koleszar's avatar
John Koleszar committed
27

Adrian Grange's avatar
Adrian Grange committed
28
const char *aom_codec_version_extra_str(void) { return VERSION_EXTRA; }
John Koleszar's avatar
John Koleszar committed
29

Adrian Grange's avatar
Adrian Grange committed
30
const char *aom_codec_iface_name(aom_codec_iface_t *iface) {
John Koleszar's avatar
John Koleszar committed
31
  return iface ? iface->name : "<invalid interface>";
John Koleszar's avatar
John Koleszar committed
32 33
}

Adrian Grange's avatar
Adrian Grange committed
34
const char *aom_codec_err_to_string(aom_codec_err_t err) {
John Koleszar's avatar
John Koleszar committed
35
  switch (err) {
Adrian Grange's avatar
Adrian Grange committed
36 37 38 39 40
    case AOM_CODEC_OK: return "Success";
    case AOM_CODEC_ERROR: return "Unspecified internal error";
    case AOM_CODEC_MEM_ERROR: return "Memory allocation error";
    case AOM_CODEC_ABI_MISMATCH: return "ABI version mismatch";
    case AOM_CODEC_INCAPABLE:
John Koleszar's avatar
John Koleszar committed
41
      return "Codec does not implement requested capability";
Adrian Grange's avatar
Adrian Grange committed
42
    case AOM_CODEC_UNSUP_BITSTREAM:
John Koleszar's avatar
John Koleszar committed
43
      return "Bitstream not supported by this decoder";
Adrian Grange's avatar
Adrian Grange committed
44
    case AOM_CODEC_UNSUP_FEATURE:
John Koleszar's avatar
John Koleszar committed
45
      return "Bitstream required feature not supported by this decoder";
Adrian Grange's avatar
Adrian Grange committed
46 47 48
    case AOM_CODEC_CORRUPT_FRAME: return "Corrupt frame detected";
    case AOM_CODEC_INVALID_PARAM: return "Invalid parameter";
    case AOM_CODEC_LIST_END: return "End of iterated list";
John Koleszar's avatar
John Koleszar committed
49
  }
John Koleszar's avatar
John Koleszar committed
50

John Koleszar's avatar
John Koleszar committed
51
  return "Unrecognized error code";
John Koleszar's avatar
John Koleszar committed
52 53
}

Adrian Grange's avatar
Adrian Grange committed
54 55
const char *aom_codec_error(aom_codec_ctx_t *ctx) {
  return (ctx) ? aom_codec_err_to_string(ctx->err)
Adrian Grange's avatar
Adrian Grange committed
56
               : aom_codec_err_to_string(AOM_CODEC_INVALID_PARAM);
John Koleszar's avatar
John Koleszar committed
57 58
}

Adrian Grange's avatar
Adrian Grange committed
59
const char *aom_codec_error_detail(aom_codec_ctx_t *ctx) {
John Koleszar's avatar
John Koleszar committed
60 61
  if (ctx && ctx->err)
    return ctx->priv ? ctx->priv->err_detail : ctx->err_detail;
John Koleszar's avatar
John Koleszar committed
62

John Koleszar's avatar
John Koleszar committed
63
  return NULL;
John Koleszar's avatar
John Koleszar committed
64 65
}

Adrian Grange's avatar
Adrian Grange committed
66 67
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx) {
  aom_codec_err_t res;
John Koleszar's avatar
John Koleszar committed
68

John Koleszar's avatar
John Koleszar committed
69
  if (!ctx)
Adrian Grange's avatar
Adrian Grange committed
70
    res = AOM_CODEC_INVALID_PARAM;
John Koleszar's avatar
John Koleszar committed
71
  else if (!ctx->iface || !ctx->priv)
Adrian Grange's avatar
Adrian Grange committed
72
    res = AOM_CODEC_ERROR;
John Koleszar's avatar
John Koleszar committed
73
  else {
Adrian Grange's avatar
Adrian Grange committed
74
    ctx->iface->destroy((aom_codec_alg_priv_t *)ctx->priv);
John Koleszar's avatar
John Koleszar committed
75

John Koleszar's avatar
John Koleszar committed
76 77 78
    ctx->iface = NULL;
    ctx->name = NULL;
    ctx->priv = NULL;
Adrian Grange's avatar
Adrian Grange committed
79
    res = AOM_CODEC_OK;
John Koleszar's avatar
John Koleszar committed
80
  }
John Koleszar's avatar
John Koleszar committed
81

John Koleszar's avatar
John Koleszar committed
82
  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
83 84
}

Adrian Grange's avatar
Adrian Grange committed
85
aom_codec_caps_t aom_codec_get_caps(aom_codec_iface_t *iface) {
John Koleszar's avatar
John Koleszar committed
86
  return (iface) ? iface->caps : 0;
John Koleszar's avatar
John Koleszar committed
87 88
}

Adrian Grange's avatar
Adrian Grange committed
89 90
aom_codec_err_t aom_codec_control_(aom_codec_ctx_t *ctx, int ctrl_id, ...) {
  aom_codec_err_t res;
John Koleszar's avatar
John Koleszar committed
91 92

  if (!ctx || !ctrl_id)
Adrian Grange's avatar
Adrian Grange committed
93
    res = AOM_CODEC_INVALID_PARAM;
John Koleszar's avatar
John Koleszar committed
94
  else if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps)
Adrian Grange's avatar
Adrian Grange committed
95
    res = AOM_CODEC_ERROR;
John Koleszar's avatar
John Koleszar committed
96
  else {
Adrian Grange's avatar
Adrian Grange committed
97
    aom_codec_ctrl_fn_map_t *entry;
John Koleszar's avatar
John Koleszar committed
98

Adrian Grange's avatar
Adrian Grange committed
99
    res = AOM_CODEC_ERROR;
John Koleszar's avatar
John Koleszar committed
100 101 102

    for (entry = ctx->iface->ctrl_maps; entry && entry->fn; entry++) {
      if (!entry->ctrl_id || entry->ctrl_id == ctrl_id) {
clang-format's avatar
clang-format committed
103
        va_list ap;
John Koleszar's avatar
John Koleszar committed
104 105

        va_start(ap, ctrl_id);
Adrian Grange's avatar
Adrian Grange committed
106
        res = entry->fn((aom_codec_alg_priv_t *)ctx->priv, ap);
John Koleszar's avatar
John Koleszar committed
107 108 109
        va_end(ap);
        break;
      }
John Koleszar's avatar
John Koleszar committed
110
    }
John Koleszar's avatar
John Koleszar committed
111
  }
John Koleszar's avatar
John Koleszar committed
112

John Koleszar's avatar
John Koleszar committed
113
  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
114
}
115

Adrian Grange's avatar
Adrian Grange committed
116 117
void aom_internal_error(struct aom_internal_error_info *info,
                        aom_codec_err_t error, const char *fmt, ...) {
118 119 120 121 122 123
  va_list ap;

  info->error_code = error;
  info->has_detail = 0;

  if (fmt) {
clang-format's avatar
clang-format committed
124
    size_t sz = sizeof(info->detail);
125 126 127 128 129 130 131 132

    info->has_detail = 1;
    va_start(ap, fmt);
    vsnprintf(info->detail, sz - 1, fmt, ap);
    va_end(ap);
    info->detail[sz - 1] = '\0';
  }

clang-format's avatar
clang-format committed
133
  if (info->setjmp) longjmp(info->jmp, info->error_code);
134
}