vpx_codec.c 3.94 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 11
 */


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>
18 19
#include "vpx/vpx_integer.h"
#include "vpx/internal/vpx_codec_internal.h"
John Koleszar's avatar
John Koleszar committed
20 21 22 23
#include "vpx_version.h"

#define SAVE_STATUS(ctx,var) (ctx?(ctx->err = var):var)

John Koleszar's avatar
John Koleszar committed
24 25
int vpx_codec_version(void) {
  return VERSION_PACKED;
John Koleszar's avatar
John Koleszar committed
26 27 28
}


John Koleszar's avatar
John Koleszar committed
29 30
const char *vpx_codec_version_str(void) {
  return VERSION_STRING_NOSP;
John Koleszar's avatar
John Koleszar committed
31 32 33
}


John Koleszar's avatar
John Koleszar committed
34 35
const char *vpx_codec_version_extra_str(void) {
  return VERSION_EXTRA;
John Koleszar's avatar
John Koleszar committed
36 37 38
}


John Koleszar's avatar
John Koleszar committed
39 40
const char *vpx_codec_iface_name(vpx_codec_iface_t *iface) {
  return iface ? iface->name : "<invalid interface>";
John Koleszar's avatar
John Koleszar committed
41 42
}

John Koleszar's avatar
John Koleszar committed
43 44
const char *vpx_codec_err_to_string(vpx_codec_err_t  err) {
  switch (err) {
John Koleszar's avatar
John Koleszar committed
45
    case VPX_CODEC_OK:
John Koleszar's avatar
John Koleszar committed
46
      return "Success";
John Koleszar's avatar
John Koleszar committed
47
    case VPX_CODEC_ERROR:
John Koleszar's avatar
John Koleszar committed
48
      return "Unspecified internal error";
John Koleszar's avatar
John Koleszar committed
49
    case VPX_CODEC_MEM_ERROR:
John Koleszar's avatar
John Koleszar committed
50
      return "Memory allocation error";
John Koleszar's avatar
John Koleszar committed
51
    case VPX_CODEC_ABI_MISMATCH:
John Koleszar's avatar
John Koleszar committed
52
      return "ABI version mismatch";
John Koleszar's avatar
John Koleszar committed
53
    case VPX_CODEC_INCAPABLE:
John Koleszar's avatar
John Koleszar committed
54
      return "Codec does not implement requested capability";
John Koleszar's avatar
John Koleszar committed
55
    case VPX_CODEC_UNSUP_BITSTREAM:
John Koleszar's avatar
John Koleszar committed
56
      return "Bitstream not supported by this decoder";
John Koleszar's avatar
John Koleszar committed
57
    case VPX_CODEC_UNSUP_FEATURE:
John Koleszar's avatar
John Koleszar committed
58
      return "Bitstream required feature not supported by this decoder";
John Koleszar's avatar
John Koleszar committed
59
    case VPX_CODEC_CORRUPT_FRAME:
John Koleszar's avatar
John Koleszar committed
60
      return "Corrupt frame detected";
John Koleszar's avatar
John Koleszar committed
61
    case  VPX_CODEC_INVALID_PARAM:
John Koleszar's avatar
John Koleszar committed
62
      return "Invalid parameter";
John Koleszar's avatar
John Koleszar committed
63
    case VPX_CODEC_LIST_END:
John Koleszar's avatar
John Koleszar committed
64 65
      return "End of iterated list";
  }
John Koleszar's avatar
John Koleszar committed
66

John Koleszar's avatar
John Koleszar committed
67
  return "Unrecognized error code";
John Koleszar's avatar
John Koleszar committed
68 69
}

John Koleszar's avatar
John Koleszar committed
70 71 72
const char *vpx_codec_error(vpx_codec_ctx_t  *ctx) {
  return (ctx) ? vpx_codec_err_to_string(ctx->err)
         : vpx_codec_err_to_string(VPX_CODEC_INVALID_PARAM);
John Koleszar's avatar
John Koleszar committed
73 74
}

John Koleszar's avatar
John Koleszar committed
75 76 77
const char *vpx_codec_error_detail(vpx_codec_ctx_t  *ctx) {
  if (ctx && ctx->err)
    return ctx->priv ? ctx->priv->err_detail : ctx->err_detail;
John Koleszar's avatar
John Koleszar committed
78

John Koleszar's avatar
John Koleszar committed
79
  return NULL;
John Koleszar's avatar
John Koleszar committed
80 81 82
}


John Koleszar's avatar
John Koleszar committed
83 84
vpx_codec_err_t vpx_codec_destroy(vpx_codec_ctx_t *ctx) {
  vpx_codec_err_t res;
John Koleszar's avatar
John Koleszar committed
85

John Koleszar's avatar
John Koleszar committed
86 87 88 89 90
  if (!ctx)
    res = VPX_CODEC_INVALID_PARAM;
  else if (!ctx->iface || !ctx->priv)
    res = VPX_CODEC_ERROR;
  else {
91
    ctx->iface->destroy((vpx_codec_alg_priv_t *)ctx->priv);
John Koleszar's avatar
John Koleszar committed
92

John Koleszar's avatar
John Koleszar committed
93 94 95 96 97
    ctx->iface = NULL;
    ctx->name = NULL;
    ctx->priv = NULL;
    res = VPX_CODEC_OK;
  }
John Koleszar's avatar
John Koleszar committed
98

John Koleszar's avatar
John Koleszar committed
99
  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
100 101 102
}


John Koleszar's avatar
John Koleszar committed
103 104
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
105 106 107 108 109
}


vpx_codec_err_t vpx_codec_control_(vpx_codec_ctx_t  *ctx,
                                   int               ctrl_id,
John Koleszar's avatar
John Koleszar committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
                                   ...) {
  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) {
        va_list  ap;

        va_start(ap, ctrl_id);
127
        res = entry->fn((vpx_codec_alg_priv_t *)ctx->priv, ap);
John Koleszar's avatar
John Koleszar committed
128 129 130
        va_end(ap);
        break;
      }
John Koleszar's avatar
John Koleszar committed
131
    }
John Koleszar's avatar
John Koleszar committed
132
  }
John Koleszar's avatar
John Koleszar committed
133

John Koleszar's avatar
John Koleszar committed
134
  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
135
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

void vpx_internal_error(struct vpx_internal_error_info *info,
                        vpx_codec_err_t                 error,
                        const char                     *fmt,
                        ...) {
  va_list ap;

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

  if (fmt) {
    size_t  sz = sizeof(info->detail);

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

  if (info->setjmp)
    longjmp(info->jmp, info->error_code);
}