John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 1 | /* |
John Koleszar | c2140b8 | 2010-09-09 08:16:39 -0400 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebM project authors. All Rights Reserved. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 3 | * |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 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 |
John Koleszar | 94c52e4 | 2010-06-18 12:39:21 -0400 | [diff] [blame] | 7 | * in the file PATENTS. All contributing project authors may |
John Koleszar | 09202d8 | 2010-06-04 16:19:40 -0400 | [diff] [blame] | 8 | * be found in the AUTHORS file in the root of the source tree. |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 9 | */ |
| 10 | |
James Zern | f42d52e | 2011-02-16 17:54:49 -0800 | [diff] [blame] | 11 | /*!\file |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 12 | * \brief Provides the high level interface to wrap decoder algorithms. |
| 13 | * |
| 14 | */ |
James Zern | e80d569 | 2010-05-26 18:14:26 -0400 | [diff] [blame] | 15 | #include <string.h> |
John Koleszar | b749234 | 2010-05-24 11:39:59 -0400 | [diff] [blame] | 16 | #include "vpx/internal/vpx_codec_internal.h" |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 17 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 18 | #define SAVE_STATUS(ctx, var) (ctx ? (ctx->err = var) : var) |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 19 | |
Dmitry Kovalev | 73edeb0 | 2014-08-20 17:02:10 -0700 | [diff] [blame] | 20 | static vpx_codec_alg_priv_t *get_alg_priv(vpx_codec_ctx_t *ctx) { |
| 21 | return (vpx_codec_alg_priv_t *)ctx->priv; |
| 22 | } |
| 23 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 24 | vpx_codec_err_t vpx_codec_dec_init_ver(vpx_codec_ctx_t *ctx, |
| 25 | vpx_codec_iface_t *iface, |
Dmitry Kovalev | 4460965 | 2014-08-25 14:02:53 -0700 | [diff] [blame] | 26 | const vpx_codec_dec_cfg_t *cfg, |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 27 | vpx_codec_flags_t flags, int ver) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 28 | vpx_codec_err_t res; |
James Zern | e80d569 | 2010-05-26 18:14:26 -0400 | [diff] [blame] | 29 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 30 | if (ver != VPX_DECODER_ABI_VERSION) |
| 31 | res = VPX_CODEC_ABI_MISMATCH; |
| 32 | else if (!ctx || !iface) |
| 33 | res = VPX_CODEC_INVALID_PARAM; |
| 34 | else if (iface->abi_version != VPX_CODEC_INTERNAL_ABI_VERSION) |
| 35 | res = VPX_CODEC_ABI_MISMATCH; |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 36 | else if ((flags & VPX_CODEC_USE_POSTPROC) && |
| 37 | !(iface->caps & VPX_CODEC_CAP_POSTPROC)) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 38 | res = VPX_CODEC_INCAPABLE; |
John Koleszar | 83b1d90 | 2012-11-05 12:37:14 -0800 | [diff] [blame] | 39 | else if ((flags & VPX_CODEC_USE_ERROR_CONCEALMENT) && |
| 40 | !(iface->caps & VPX_CODEC_CAP_ERROR_CONCEALMENT)) |
| 41 | res = VPX_CODEC_INCAPABLE; |
| 42 | else if ((flags & VPX_CODEC_USE_INPUT_FRAGMENTS) && |
| 43 | !(iface->caps & VPX_CODEC_CAP_INPUT_FRAGMENTS)) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 44 | res = VPX_CODEC_INCAPABLE; |
| 45 | else if (!(iface->caps & VPX_CODEC_CAP_DECODER)) |
| 46 | res = VPX_CODEC_INCAPABLE; |
| 47 | else { |
| 48 | memset(ctx, 0, sizeof(*ctx)); |
| 49 | ctx->iface = iface; |
| 50 | ctx->name = iface->name; |
| 51 | ctx->priv = NULL; |
| 52 | ctx->init_flags = flags; |
| 53 | ctx->config.dec = cfg; |
James Zern | e80d569 | 2010-05-26 18:14:26 -0400 | [diff] [blame] | 54 | |
James Zern | febdebf | 2014-08-09 19:16:18 -0700 | [diff] [blame] | 55 | res = ctx->iface->init(ctx, NULL); |
| 56 | if (res) { |
| 57 | ctx->err_detail = ctx->priv ? ctx->priv->err_detail : NULL; |
| 58 | vpx_codec_destroy(ctx); |
James Zern | e80d569 | 2010-05-26 18:14:26 -0400 | [diff] [blame] | 59 | } |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 60 | } |
James Zern | e80d569 | 2010-05-26 18:14:26 -0400 | [diff] [blame] | 61 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 62 | return SAVE_STATUS(ctx, res); |
James Zern | e80d569 | 2010-05-26 18:14:26 -0400 | [diff] [blame] | 63 | } |
| 64 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 65 | vpx_codec_err_t vpx_codec_peek_stream_info(vpx_codec_iface_t *iface, |
| 66 | const uint8_t *data, |
| 67 | unsigned int data_sz, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 68 | vpx_codec_stream_info_t *si) { |
| 69 | vpx_codec_err_t res; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 70 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 71 | if (!iface || !data || !data_sz || !si || |
| 72 | si->sz < sizeof(vpx_codec_stream_info_t)) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 73 | res = VPX_CODEC_INVALID_PARAM; |
| 74 | else { |
| 75 | /* Set default/unknown values */ |
| 76 | si->w = 0; |
| 77 | si->h = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 78 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 79 | res = iface->dec.peek_si(data, data_sz, si); |
| 80 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 81 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 82 | return res; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 83 | } |
| 84 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 85 | vpx_codec_err_t vpx_codec_get_stream_info(vpx_codec_ctx_t *ctx, |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 86 | vpx_codec_stream_info_t *si) { |
| 87 | vpx_codec_err_t res; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 88 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 89 | if (!ctx || !si || si->sz < sizeof(vpx_codec_stream_info_t)) |
| 90 | res = VPX_CODEC_INVALID_PARAM; |
| 91 | else if (!ctx->iface || !ctx->priv) |
| 92 | res = VPX_CODEC_ERROR; |
| 93 | else { |
| 94 | /* Set default/unknown values */ |
| 95 | si->w = 0; |
| 96 | si->h = 0; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 97 | |
Dmitry Kovalev | 73edeb0 | 2014-08-20 17:02:10 -0700 | [diff] [blame] | 98 | res = ctx->iface->dec.get_si(get_alg_priv(ctx), si); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 99 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 100 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 101 | return SAVE_STATUS(ctx, res); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 102 | } |
| 103 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 104 | vpx_codec_err_t vpx_codec_decode(vpx_codec_ctx_t *ctx, const uint8_t *data, |
| 105 | unsigned int data_sz, void *user_priv, |
| 106 | long deadline) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 107 | vpx_codec_err_t res; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 108 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 109 | /* Sanity checks */ |
| 110 | /* NULL data ptr allowed if data_sz is 0 too */ |
James Zern | 6a2e9ef | 2014-08-09 18:35:40 -0700 | [diff] [blame] | 111 | if (!ctx || (!data && data_sz) || (data && !data_sz)) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 112 | res = VPX_CODEC_INVALID_PARAM; |
| 113 | else if (!ctx->iface || !ctx->priv) |
| 114 | res = VPX_CODEC_ERROR; |
| 115 | else { |
Dmitry Kovalev | 73edeb0 | 2014-08-20 17:02:10 -0700 | [diff] [blame] | 116 | res = ctx->iface->dec.decode(get_alg_priv(ctx), data, data_sz, user_priv, |
| 117 | deadline); |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 118 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 119 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 120 | return SAVE_STATUS(ctx, res); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 121 | } |
| 122 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 123 | vpx_image_t *vpx_codec_get_frame(vpx_codec_ctx_t *ctx, vpx_codec_iter_t *iter) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 124 | vpx_image_t *img; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 125 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 126 | if (!ctx || !iter || !ctx->iface || !ctx->priv) |
| 127 | img = NULL; |
| 128 | else |
Dmitry Kovalev | 73edeb0 | 2014-08-20 17:02:10 -0700 | [diff] [blame] | 129 | img = ctx->iface->dec.get_frame(get_alg_priv(ctx), iter); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 130 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 131 | return img; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 132 | } |
| 133 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 134 | vpx_codec_err_t vpx_codec_register_put_frame_cb(vpx_codec_ctx_t *ctx, |
| 135 | vpx_codec_put_frame_cb_fn_t cb, |
| 136 | void *user_priv) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 137 | vpx_codec_err_t res; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 138 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 139 | if (!ctx || !cb) |
| 140 | res = VPX_CODEC_INVALID_PARAM; |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 141 | else if (!ctx->iface || !ctx->priv || |
| 142 | !(ctx->iface->caps & VPX_CODEC_CAP_PUT_FRAME)) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 143 | res = VPX_CODEC_ERROR; |
| 144 | else { |
| 145 | ctx->priv->dec.put_frame_cb.u.put_frame = cb; |
| 146 | ctx->priv->dec.put_frame_cb.user_priv = user_priv; |
| 147 | res = VPX_CODEC_OK; |
| 148 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 149 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 150 | return SAVE_STATUS(ctx, res); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 151 | } |
| 152 | |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 153 | vpx_codec_err_t vpx_codec_register_put_slice_cb(vpx_codec_ctx_t *ctx, |
| 154 | vpx_codec_put_slice_cb_fn_t cb, |
| 155 | void *user_priv) { |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 156 | vpx_codec_err_t res; |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 157 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 158 | if (!ctx || !cb) |
| 159 | res = VPX_CODEC_INVALID_PARAM; |
clang-format | 83a5207 | 2016-08-08 20:22:13 -0700 | [diff] [blame] | 160 | else if (!ctx->iface || !ctx->priv || |
| 161 | !(ctx->iface->caps & VPX_CODEC_CAP_PUT_SLICE)) |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 162 | res = VPX_CODEC_ERROR; |
| 163 | else { |
| 164 | ctx->priv->dec.put_slice_cb.u.put_slice = cb; |
| 165 | ctx->priv->dec.put_slice_cb.user_priv = user_priv; |
| 166 | res = VPX_CODEC_OK; |
| 167 | } |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 168 | |
John Koleszar | c6b9039 | 2012-07-13 15:21:29 -0700 | [diff] [blame] | 169 | return SAVE_STATUS(ctx, res); |
John Koleszar | 0ea50ce | 2010-05-18 11:58:33 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Frank Galligan | a4f30a5 | 2014-02-06 17:13:08 -0800 | [diff] [blame] | 172 | vpx_codec_err_t vpx_codec_set_frame_buffer_functions( |
| 173 | vpx_codec_ctx_t *ctx, vpx_get_frame_buffer_cb_fn_t cb_get, |
| 174 | vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) { |
| 175 | vpx_codec_err_t res; |
| 176 | |
| 177 | if (!ctx || !cb_get || !cb_release) { |
| 178 | res = VPX_CODEC_INVALID_PARAM; |
| 179 | } else if (!ctx->iface || !ctx->priv || |
| 180 | !(ctx->iface->caps & VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER)) { |
| 181 | res = VPX_CODEC_ERROR; |
| 182 | } else { |
Dmitry Kovalev | 73edeb0 | 2014-08-20 17:02:10 -0700 | [diff] [blame] | 183 | res = ctx->iface->dec.set_fb_fn(get_alg_priv(ctx), cb_get, cb_release, |
Frank Galligan | a4f30a5 | 2014-02-06 17:13:08 -0800 | [diff] [blame] | 184 | cb_priv); |
| 185 | } |
| 186 | |
| 187 | return SAVE_STATUS(ctx, res); |
| 188 | } |