vpx_decoder.c 5.74 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 <string.h>
16
#include "aom/internal/vpx_codec_internal.h"
John Koleszar's avatar
John Koleszar committed
17

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

20
21
22
23
static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) {
  return (vpx_codec_alg_priv_t *)ctx->priv;
}

clang-format's avatar
clang-format committed
24
25
vpx_codec_err_t vpx_codec_dec_init_ver(vpx_codec_ctx_t *ctx,
                                       vpx_codec_iface_t *iface,
26
                                       const vpx_codec_dec_cfg_t *cfg,
clang-format's avatar
clang-format committed
27
                                       vpx_codec_flags_t flags, int ver) {
John Koleszar's avatar
John Koleszar committed
28
29
30
31
32
33
34
35
  vpx_codec_err_t res;

  if (ver != VPX_DECODER_ABI_VERSION)
    res = VPX_CODEC_ABI_MISMATCH;
  else if (!ctx || !iface)
    res = VPX_CODEC_INVALID_PARAM;
  else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION)
    res = VPX_CODEC_ABI_MISMATCH;
clang-format's avatar
clang-format committed
36
37
  else if ((flags & VPX_CODEC_USE_POSTPROC) &&
           !(iface->caps & VPX_CODEC_CAP_POSTPROC))
John Koleszar's avatar
John Koleszar committed
38
    res = VPX_CODEC_INCAPABLE;
John Koleszar's avatar
John Koleszar committed
39
40
41
42
43
  else if ((flags & VPX_CODEC_USE_ERROR_CONCEALMENT) &&
           !(iface->caps & VPX_CODEC_CAP_ERROR_CONCEALMENT))
    res = VPX_CODEC_INCAPABLE;
  else if ((flags & VPX_CODEC_USE_INPUT_FRAGMENTS) &&
           !(iface->caps & VPX_CODEC_CAP_INPUT_FRAGMENTS))
John Koleszar's avatar
John Koleszar committed
44
45
46
47
48
49
50
51
52
53
54
    res = VPX_CODEC_INCAPABLE;
  else if (!(iface->caps & VPX_CODEC_CAP_DECODER))
    res = VPX_CODEC_INCAPABLE;
  else {
    memset(ctx, 0, sizeof(*ctx));
    ctx->iface = iface;
    ctx->name = iface->name;
    ctx->priv = NULL;
    ctx->init_flags = flags;
    ctx->config.dec = cfg;

55
56
57
58
    res = ctx->iface->init(ctx, NULL);
    if (res) {
      ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL;
      vpx_codec_destroy(ctx);
59
    }
John Koleszar's avatar
John Koleszar committed
60
  }
61

John Koleszar's avatar
John Koleszar committed
62
  return SAVE_STATUS(ctx, res);
63
64
}

clang-format's avatar
clang-format committed
65
66
67
vpx_codec_err_t vpx_codec_peek_stream_info(vpx_codec_iface_t *iface,
                                           const uint8_t *data,
                                           unsigned int data_sz,
John Koleszar's avatar
John Koleszar committed
68
69
70
                                           vpx_codec_stream_info_t *si) {
  vpx_codec_err_t res;

clang-format's avatar
clang-format committed
71
72
  if (!iface || !data || !data_sz || !si ||
      si->sz < sizeof(vpx_codec_stream_info_t))
John Koleszar's avatar
John Koleszar committed
73
74
75
76
77
78
79
80
81
82
    res = VPX_CODEC_INVALID_PARAM;
  else {
    /* Set default/unknown values */
    si->w = 0;
    si->h = 0;

    res = iface->dec.peek_si(data, data_sz, si);
  }

  return res;
John Koleszar's avatar
John Koleszar committed
83
84
}

clang-format's avatar
clang-format committed
85
vpx_codec_err_t vpx_codec_get_stream_info(vpx_codec_ctx_t *ctx,
John Koleszar's avatar
John Koleszar committed
86
87
88
89
90
91
92
93
94
95
96
97
                                          vpx_codec_stream_info_t *si) {
  vpx_codec_err_t res;

  if (!ctx || !si || si->sz < sizeof(vpx_codec_stream_info_t))
    res = VPX_CODEC_INVALID_PARAM;
  else if (!ctx->iface || !ctx->priv)
    res = VPX_CODEC_ERROR;
  else {
    /* Set default/unknown values */
    si->w = 0;
    si->h = 0;

98
    res = ctx->iface->dec.get_si(get_alg_priv(ctx), si);
John Koleszar's avatar
John Koleszar committed
99
100
101
  }

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

clang-format's avatar
clang-format committed
104
105
106
vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, const uint8_t *data,
                                 unsigned int data_sz, void *user_priv,
                                 long deadline) {
John Koleszar's avatar
John Koleszar committed
107
108
109
110
  vpx_codec_err_t res;

  /* Sanity checks */
  /* NULL data ptr allowed if data_sz is 0 too */
James Zern's avatar
James Zern committed
111
  if (!ctx || (!data && data_sz) || (data && !data_sz))
John Koleszar's avatar
John Koleszar committed
112
113
114
115
    res = VPX_CODEC_INVALID_PARAM;
  else if (!ctx->iface || !ctx->priv)
    res = VPX_CODEC_ERROR;
  else {
116
117
    res = ctx->iface->dec.decode(get_alg_priv(ctx), data, data_sz, user_priv,
                                 deadline);
John Koleszar's avatar
John Koleszar committed
118
119
120
  }

  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
121
122
}

clang-format's avatar
clang-format committed
123
vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter) {
John Koleszar's avatar
John Koleszar committed
124
  vpx_image_t *img;
John Koleszar's avatar
John Koleszar committed
125

John Koleszar's avatar
John Koleszar committed
126
127
128
  if (!ctx || !iter || !ctx->iface || !ctx->priv)
    img = NULL;
  else
129
    img = ctx->iface->dec.get_frame(get_alg_priv(ctx), iter);
John Koleszar's avatar
John Koleszar committed
130

John Koleszar's avatar
John Koleszar committed
131
  return img;
John Koleszar's avatar
John Koleszar committed
132
133
}

clang-format's avatar
clang-format committed
134
135
136
vpx_codec_err_t vpx_codec_register_put_frame_cb(vpx_codec_ctx_t *ctx,
                                                vpx_codec_put_frame_cb_fn_t cb,
                                                void *user_priv) {
John Koleszar's avatar
John Koleszar committed
137
138
139
140
  vpx_codec_err_t res;

  if (!ctx || !cb)
    res = VPX_CODEC_INVALID_PARAM;
clang-format's avatar
clang-format committed
141
142
  else if (!ctx->iface || !ctx->priv ||
           !(ctx->iface->caps & VPX_CODEC_CAP_PUT_FRAME))
John Koleszar's avatar
John Koleszar committed
143
144
145
146
147
148
149
150
    res = VPX_CODEC_ERROR;
  else {
    ctx->priv->dec.put_frame_cb.u.put_frame = cb;
    ctx->priv->dec.put_frame_cb.user_priv = user_priv;
    res = VPX_CODEC_OK;
  }

  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
151
152
}

clang-format's avatar
clang-format committed
153
154
155
vpx_codec_err_t vpx_codec_register_put_slice_cb(vpx_codec_ctx_t *ctx,
                                                vpx_codec_put_slice_cb_fn_t cb,
                                                void *user_priv) {
John Koleszar's avatar
John Koleszar committed
156
157
158
159
  vpx_codec_err_t res;

  if (!ctx || !cb)
    res = VPX_CODEC_INVALID_PARAM;
clang-format's avatar
clang-format committed
160
161
  else if (!ctx->iface || !ctx->priv ||
           !(ctx->iface->caps & VPX_CODEC_CAP_PUT_SLICE))
John Koleszar's avatar
John Koleszar committed
162
163
164
165
166
167
168
169
    res = VPX_CODEC_ERROR;
  else {
    ctx->priv->dec.put_slice_cb.u.put_slice = cb;
    ctx->priv->dec.put_slice_cb.user_priv = user_priv;
    res = VPX_CODEC_OK;
  }

  return SAVE_STATUS(ctx, res);
John Koleszar's avatar
John Koleszar committed
170
171
}

172
173
174
175
176
177
178
179
180
181
182
vpx_codec_err_t vpx_codec_set_frame_buffer_functions(
    vpx_codec_ctx_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get,
    vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
  vpx_codec_err_t res;

  if (!ctx || !cb_get || !cb_release) {
    res = VPX_CODEC_INVALID_PARAM;
  } else if (!ctx->iface || !ctx->priv ||
             !(ctx->iface->caps & VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER)) {
    res = VPX_CODEC_ERROR;
  } else {
183
    res = ctx->iface->dec.set_fb_fn(get_alg_priv(ctx), cb_get, cb_release,
184
185
186
187
188
                                    cb_priv);
  }

  return SAVE_STATUS(ctx, res);
}