vpx_codec.c 3.72 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5 6
 *  that can be found in the LICENSE file in the root of the source
 *  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.
John Koleszar's avatar
John Koleszar committed
9 10
 */

James Zern's avatar
James Zern committed
11
/*!\file
John Koleszar's avatar
John Koleszar committed
12 13 14
 * \brief Provides the high level interface to wrap decoder algorithms.
 *
 */
15
#include <stdarg.h>
16
#include <stdlib.h>
Yaowu Xu's avatar
Yaowu Xu committed
17 18
#include "aom/vpx_integer.h"
#include "aom/internal/vpx_codec_internal.h"
John Koleszar's avatar
John Koleszar committed
19 20
#include "vpx_version.h"

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

clang-format's avatar
clang-format committed
23
int vpx_codec_version(void) { return VERSION_PACKED; }
John Koleszar's avatar
John Koleszar committed
24

clang-format's avatar
clang-format committed
25
const char *vpx_codec_version_str(void) { return VERSION_STRING_NOSP; }
John Koleszar's avatar
John Koleszar committed
26

clang-format's avatar
clang-format committed
27
const char *vpx_codec_version_extra_str(void) { return VERSION_EXTRA; }
John Koleszar's avatar
John Koleszar committed
28

John Koleszar's avatar
John Koleszar committed
29 30
const char *vpx_codec_iface_name(vpx_codec_iface_t *iface) {
  return iface ? iface->name : "<invalid interface>";
John Koleszar's avatar
John Koleszar committed
31 32
}

clang-format's avatar
clang-format committed
33
const char *vpx_codec_err_to_string(vpx_codec_err_t err) {
John Koleszar's avatar
John Koleszar committed
34
  switch (err) {
clang-format's avatar
clang-format committed
35 36 37 38
    case VPX_CODEC_OK: return "Success";
    case VPX_CODEC_ERROR: return "Unspecified internal error";
    case VPX_CODEC_MEM_ERROR: return "Memory allocation error";
    case VPX_CODEC_ABI_MISMATCH: return "ABI version mismatch";
John Koleszar's avatar
John Koleszar committed
39
    case VPX_CODEC_INCAPABLE:
John Koleszar's avatar
John Koleszar committed
40
      return "Codec does not implement requested capability";
John Koleszar's avatar
John Koleszar committed
41
    case VPX_CODEC_UNSUP_BITSTREAM:
John Koleszar's avatar
John Koleszar committed
42
      return "Bitstream not supported by this decoder";
John Koleszar's avatar
John Koleszar committed
43
    case VPX_CODEC_UNSUP_FEATURE:
John Koleszar's avatar
John Koleszar committed
44
      return "Bitstream required feature not supported by this decoder";
clang-format's avatar
clang-format committed
45 46 47
    case VPX_CODEC_CORRUPT_FRAME: return "Corrupt frame detected";
    case VPX_CODEC_INVALID_PARAM: return "Invalid parameter";
    case VPX_CODEC_LIST_END: return "End of iterated list";
John Koleszar's avatar
John Koleszar committed
48
  }
John Koleszar's avatar
John Koleszar committed
49

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

clang-format's avatar
clang-format committed
53
const char *vpx_codec_error(vpx_codec_ctx_t *ctx) {
John Koleszar's avatar
John Koleszar committed
54
  return (ctx) ? vpx_codec_err_to_string(ctx->err)
clang-format's avatar
clang-format committed
55
               : vpx_codec_err_to_string(VPX_CODEC_INVALID_PARAM);
John Koleszar's avatar
John Koleszar committed
56 57
}

clang-format's avatar
clang-format committed
58
const char *vpx_codec_error_detail(vpx_codec_ctx_t *ctx) {
John Koleszar's avatar
John Koleszar committed
59 60
  if (ctx && ctx->err)
    return ctx->priv ? ctx->priv->err_detail : ctx->err_detail;
John Koleszar's avatar
John Koleszar committed
61

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

John Koleszar's avatar
John Koleszar committed
65 66
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx) {
  vpx_codec_err_t res;
John Koleszar's avatar
John Koleszar committed
67

John Koleszar's avatar
John Koleszar committed
68 69 70 71 72
  if (!ctx)
    res = VPX_CODEC_INVALID_PARAM;
  else if (!ctx->iface || !ctx->priv)
    res = VPX_CODEC_ERROR;
  else {
73
    ctx->iface->destroy((vpx_codec_alg_priv_t *)ctx->priv);
John Koleszar's avatar
John Koleszar committed
74

John Koleszar's avatar
John Koleszar committed
75 76 77 78 79
    ctx->iface = NULL;
    ctx->name = NULL;
    ctx->priv = NULL;
    res = VPX_CODEC_OK;
  }
John Koleszar's avatar
John Koleszar committed
80

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

John Koleszar's avatar
John Koleszar committed
84 85
vpx_codec_caps_t vpx_codec_get_caps(vpx_codec_iface_t *iface) {
  return (iface) ? iface->caps : 0;
John Koleszar's avatar
John Koleszar committed
86 87
}

clang-format's avatar
clang-format committed
88
vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t *ctx, int ctrl_id, ...) {
John Koleszar's avatar
John Koleszar committed
89 90 91 92 93 94 95 96 97 98 99 100 101
  vpx_codec_err_t res;

  if (!ctx || !ctrl_id)
    res = VPX_CODEC_INVALID_PARAM;
  else if (!ctx->iface || !ctx->priv || !ctx->iface->ctrl_maps)
    res = VPX_CODEC_ERROR;
  else {
    vpx_codec_ctrl_fn_map_t *entry;

    res = VPX_CODEC_ERROR;

    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
102
        va_list ap;
John Koleszar's avatar
John Koleszar committed
103 104

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

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

void vpx_internal_error(struct vpx_internal_error_info *info,
clang-format's avatar
clang-format committed
116
                        vpx_codec_err_t error, const char *fmt, ...) {
117 118 119 120 121 122
  va_list ap;

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

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

    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
132
  if (info->setjmp) longjmp(info->jmp, info->error_code);
133
}