2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
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
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
14 #include "./vpx_version.h"
16 #include "vpx/internal/vpx_codec_internal.h"
17 #include "vpx/vp8dx.h"
18 #include "vpx/vpx_decoder.h"
20 #include "vp9/common/vp9_frame_buffers.h"
22 #include "vp9/decoder/vp9_decoder.h"
23 #include "vp9/decoder/vp9_decodeframe.h"
24 #include "vp9/decoder/vp9_read_bit_buffer.h"
26 #include "vp9/vp9_iface_common.h"
28 #define VP9_CAP_POSTPROC (CONFIG_VP9_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
30 typedef vpx_codec_stream_info_t vp9_stream_info_t;
32 struct vpx_codec_alg_priv {
33 vpx_codec_priv_t base;
34 vpx_codec_dec_cfg_t cfg;
36 struct VP9Decoder *pbi;
38 vp8_postproc_cfg_t postproc_cfg;
39 vpx_decrypt_cb decrypt_cb;
43 int invert_tile_order;
44 int frame_parallel_decode; // frame-based threading.
46 // External frame buffer info to save for VP9 common.
47 void *ext_priv; // Private data associated with the external frame buffers.
48 vpx_get_frame_buffer_cb_fn_t get_ext_fb_cb;
49 vpx_release_frame_buffer_cb_fn_t release_ext_fb_cb;
52 static vpx_codec_err_t decoder_init(vpx_codec_ctx_t *ctx,
53 vpx_codec_priv_enc_mr_cfg_t *data) {
54 // This function only allocates space for the vpx_codec_alg_priv_t
55 // structure. More memory may be required at the time the stream
56 // information becomes known.
60 vpx_codec_alg_priv_t *alg_priv = vpx_memalign(32, sizeof(*alg_priv));
62 return VPX_CODEC_MEM_ERROR;
66 ctx->priv = (vpx_codec_priv_t *)alg_priv;
67 ctx->priv->sz = sizeof(*ctx->priv);
68 ctx->priv->iface = ctx->iface;
69 ctx->priv->alg_priv = alg_priv;
70 ctx->priv->alg_priv->si.sz = sizeof(ctx->priv->alg_priv->si);
71 ctx->priv->init_flags = ctx->init_flags;
72 ctx->priv->alg_priv->frame_parallel_decode =
73 (ctx->init_flags & VPX_CODEC_USE_FRAME_THREADING);
75 // Disable frame parallel decoding for now.
76 ctx->priv->alg_priv->frame_parallel_decode = 0;
78 if (ctx->config.dec) {
79 // Update the reference to the config structure to an internal copy.
80 ctx->priv->alg_priv->cfg = *ctx->config.dec;
81 ctx->config.dec = &ctx->priv->alg_priv->cfg;
88 static vpx_codec_err_t decoder_destroy(vpx_codec_alg_priv_t *ctx) {
90 vp9_decoder_remove(ctx->pbi);
99 static vpx_codec_err_t decoder_peek_si_internal(const uint8_t *data,
100 unsigned int data_sz,
101 vpx_codec_stream_info_t *si,
103 vpx_decrypt_cb decrypt_cb,
104 void *decrypt_state) {
105 int intra_only_flag = 0;
106 uint8_t clear_buffer[9];
108 if (data + data_sz <= data)
109 return VPX_CODEC_INVALID_PARAM;
115 data_sz = MIN(sizeof(clear_buffer), data_sz);
116 decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
123 struct vp9_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
124 const int frame_marker = vp9_rb_read_literal(&rb, 2);
125 const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
127 if (frame_marker != VP9_FRAME_MARKER)
128 return VPX_CODEC_UNSUP_BITSTREAM;
130 if (profile >= MAX_PROFILES) return VPX_CODEC_UNSUP_BITSTREAM;
132 if (vp9_rb_read_bit(&rb)) { // show an existing frame
133 vp9_rb_read_literal(&rb, 3); // Frame buffer to show.
138 return VPX_CODEC_UNSUP_BITSTREAM;
140 si->is_kf = !vp9_rb_read_bit(&rb);
141 show_frame = vp9_rb_read_bit(&rb);
142 error_resilient = vp9_rb_read_bit(&rb);
148 if (!vp9_read_sync_code(&rb))
149 return VPX_CODEC_UNSUP_BITSTREAM;
151 if (profile > PROFILE_1)
152 rb.bit_offset += 1; // Bit-depth 10 or 12
153 colorspace = vp9_rb_read_literal(&rb, 3);
154 if (colorspace != sRGB) {
155 rb.bit_offset += 1; // [16,235] (including xvycc) vs [0,255] range
156 if (profile == PROFILE_1 || profile == PROFILE_3) {
157 rb.bit_offset += 2; // subsampling x/y
158 rb.bit_offset += 1; // unused
161 if (profile == PROFILE_1 || profile == PROFILE_3) {
162 rb.bit_offset += 1; // unused
164 // RGB is only available in version 1
165 return VPX_CODEC_UNSUP_BITSTREAM;
168 vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
170 intra_only_flag = show_frame ? 0 : vp9_rb_read_bit(&rb);
171 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
173 if (intra_only_flag) {
174 if (!vp9_read_sync_code(&rb))
175 return VPX_CODEC_UNSUP_BITSTREAM;
176 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
177 vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
181 if (is_intra_only != NULL)
182 *is_intra_only = intra_only_flag;
186 static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
187 unsigned int data_sz,
188 vpx_codec_stream_info_t *si) {
189 return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
192 static vpx_codec_err_t decoder_get_si(vpx_codec_alg_priv_t *ctx,
193 vpx_codec_stream_info_t *si) {
194 const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
195 ? sizeof(vp9_stream_info_t)
196 : sizeof(vpx_codec_stream_info_t);
197 memcpy(si, &ctx->si, sz);
198 si->sz = (unsigned int)sz;
203 static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
204 const struct vpx_internal_error_info *error) {
205 if (error->error_code)
206 ctx->base.err_detail = error->has_detail ? error->detail : NULL;
208 return error->error_code;
211 static void init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
212 VP9_COMMON *const cm = &ctx->pbi->common;
216 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
217 cm->get_fb_cb = ctx->get_ext_fb_cb;
218 cm->release_fb_cb = ctx->release_ext_fb_cb;
219 cm->cb_priv = ctx->ext_priv;
221 cm->get_fb_cb = vp9_get_frame_buffer;
222 cm->release_fb_cb = vp9_release_frame_buffer;
224 if (vp9_alloc_internal_frame_buffers(&cm->int_frame_buffers))
225 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
226 "Failed to initialize internal frame buffers");
228 cm->cb_priv = &cm->int_frame_buffers;
232 static void set_default_ppflags(vp8_postproc_cfg_t *cfg) {
233 cfg->post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
234 cfg->deblocking_level = 4;
235 cfg->noise_level = 0;
238 static void set_ppflags(const vpx_codec_alg_priv_t *ctx,
239 vp9_ppflags_t *flags) {
240 flags->post_proc_flag =
241 ctx->postproc_cfg.post_proc_flag;
243 flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
244 flags->noise_level = ctx->postproc_cfg.noise_level;
247 static void init_decoder(vpx_codec_alg_priv_t *ctx) {
248 ctx->pbi = vp9_decoder_create();
249 if (ctx->pbi == NULL)
252 ctx->pbi->max_threads = ctx->cfg.threads;
253 ctx->pbi->inv_tile_order = ctx->invert_tile_order;
254 ctx->pbi->frame_parallel_decode = ctx->frame_parallel_decode;
256 // If postprocessing was enabled by the application and a
257 // configuration has not been provided, default it.
258 if (!ctx->postproc_cfg_set &&
259 (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
260 set_default_ppflags(&ctx->postproc_cfg);
262 init_buffer_callbacks(ctx);
265 static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
266 const uint8_t **data, unsigned int data_sz,
267 void *user_priv, int64_t deadline) {
268 YV12_BUFFER_CONFIG sd;
269 vp9_ppflags_t flags = {0, 0, 0};
270 VP9_COMMON *cm = NULL;
277 // Determine the stream parameters. Note that we rely on peek_si to
278 // validate that we have a buffer that does not wrap around the top
281 int is_intra_only = 0;
282 const vpx_codec_err_t res =
283 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
284 ctx->decrypt_cb, ctx->decrypt_state);
285 if (res != VPX_CODEC_OK)
288 if (!ctx->si.is_kf && !is_intra_only)
289 return VPX_CODEC_ERROR;
292 // Initialize the decoder instance on the first frame
293 if (ctx->pbi == NULL) {
295 if (ctx->pbi == NULL)
296 return VPX_CODEC_ERROR;
299 // Set these even if already initialized. The caller may have changed the
300 // decrypt config between frames.
301 ctx->pbi->decrypt_cb = ctx->decrypt_cb;
302 ctx->pbi->decrypt_state = ctx->decrypt_state;
304 cm = &ctx->pbi->common;
306 if (vp9_receive_compressed_data(ctx->pbi, data_sz, data))
307 return update_error_state(ctx, &cm->error);
309 if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
310 set_ppflags(ctx, &flags);
312 if (vp9_get_raw_frame(ctx->pbi, &sd, &flags))
313 return update_error_state(ctx, &cm->error);
315 yuvconfig2image(&ctx->img, &sd, user_priv);
316 ctx->img.fb_priv = cm->frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
322 static INLINE uint8_t read_marker(vpx_decrypt_cb decrypt_cb,
324 const uint8_t *data) {
327 decrypt_cb(decrypt_state, data, &marker, 1);
333 static vpx_codec_err_t parse_superframe_index(const uint8_t *data,
335 uint32_t sizes[8], int *count,
336 vpx_decrypt_cb decrypt_cb,
337 void *decrypt_state) {
338 // A chunk ending with a byte matching 0xc0 is an invalid chunk unless
339 // it is a super frame index. If the last byte of real video compression
340 // data is 0xc0 the encoder must add a 0 byte. If we have the marker but
341 // not the associated matching marker byte at the front of the index we have
342 // an invalid bitstream and need to return an error.
347 marker = read_marker(decrypt_cb, decrypt_state, data + data_sz - 1);
350 if ((marker & 0xe0) == 0xc0) {
351 const uint32_t frames = (marker & 0x7) + 1;
352 const uint32_t mag = ((marker >> 3) & 0x3) + 1;
353 const size_t index_sz = 2 + mag * frames;
355 // This chunk is marked as having a superframe index but doesn't have
356 // enough data for it, thus it's an invalid superframe index.
357 if (data_sz < index_sz)
358 return VPX_CODEC_CORRUPT_FRAME;
361 const uint8_t marker2 = read_marker(decrypt_cb, decrypt_state,
362 data + data_sz - index_sz);
364 // This chunk is marked as having a superframe index but doesn't have
365 // the matching marker byte at the front of the index therefore it's an
367 if (marker != marker2)
368 return VPX_CODEC_CORRUPT_FRAME;
372 // Found a valid superframe index.
374 const uint8_t *x = &data[data_sz - index_sz + 1];
376 // Frames has a maximum of 8 and mag has a maximum of 4.
377 uint8_t clear_buffer[32];
378 assert(sizeof(clear_buffer) >= frames * mag);
380 decrypt_cb(decrypt_state, x, clear_buffer, frames * mag);
384 for (i = 0; i < frames; ++i) {
385 uint32_t this_sz = 0;
387 for (j = 0; j < mag; ++j)
388 this_sz |= (*x++) << (j * 8);
397 static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
398 const uint8_t *data, unsigned int data_sz,
399 void *user_priv, long deadline) {
400 const uint8_t *data_start = data;
401 const uint8_t * const data_end = data + data_sz;
403 uint32_t frame_sizes[8];
406 if (data == NULL || data_sz == 0)
407 return VPX_CODEC_INVALID_PARAM;
409 res = parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
410 ctx->decrypt_cb, ctx->decrypt_state);
411 if (res != VPX_CODEC_OK)
414 if (ctx->frame_parallel_decode) {
415 // Decode in frame parallel mode. When decoding in this mode, the frame
416 // passed to the decoder must be either a normal frame or a superframe with
417 // superframe index so the decoder could get each frame's start position
418 // in the superframe.
419 if (frame_count > 0) {
422 for (i = 0; i < frame_count; ++i) {
423 const uint8_t *data_start_copy = data_start;
424 const uint32_t frame_size = frame_sizes[i];
426 if (data_start < data
427 || frame_size > (uint32_t) (data_end - data_start)) {
428 ctx->base.err_detail = "Invalid frame size in index";
429 return VPX_CODEC_CORRUPT_FRAME;
432 res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
434 if (res != VPX_CODEC_OK)
437 data_start += frame_size;
440 res = decode_one(ctx, &data_start, data_sz, user_priv, deadline);
441 if (res != VPX_CODEC_OK)
444 // Extra data detected after the frame.
445 if (data_start < data_end - 1) {
446 ctx->base.err_detail = "Fail to decode frame in parallel mode";
447 return VPX_CODEC_INCAPABLE;
451 // Decode in serial mode.
452 if (frame_count > 0) {
455 for (i = 0; i < frame_count; ++i) {
456 const uint8_t *data_start_copy = data_start;
457 const uint32_t frame_size = frame_sizes[i];
459 if (data_start < data
460 || frame_size > (uint32_t) (data_end - data_start)) {
461 ctx->base.err_detail = "Invalid frame size in index";
462 return VPX_CODEC_CORRUPT_FRAME;
465 res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
467 if (res != VPX_CODEC_OK)
470 data_start += frame_size;
473 while (data_start < data_end) {
474 const uint32_t frame_size = (uint32_t) (data_end - data_start);
475 const vpx_codec_err_t res = decode_one(ctx, &data_start, frame_size,
476 user_priv, deadline);
477 if (res != VPX_CODEC_OK)
480 // Account for suboptimal termination by the encoder.
481 while (data_start < data_end) {
482 const uint8_t marker = read_marker(ctx->decrypt_cb,
483 ctx->decrypt_state, data_start);
495 static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
496 vpx_codec_iter_t *iter) {
497 vpx_image_t *img = NULL;
499 if (ctx->img_avail) {
500 // iter acts as a flip flop, so an image is only returned on the first
501 // call to get_frame.
512 static vpx_codec_err_t decoder_set_fb_fn(
513 vpx_codec_alg_priv_t *ctx,
514 vpx_get_frame_buffer_cb_fn_t cb_get,
515 vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
516 if (cb_get == NULL || cb_release == NULL) {
517 return VPX_CODEC_INVALID_PARAM;
518 } else if (ctx->pbi == NULL) {
519 // If the decoder has already been initialized, do not accept changes to
520 // the frame buffer functions.
521 ctx->get_ext_fb_cb = cb_get;
522 ctx->release_ext_fb_cb = cb_release;
523 ctx->ext_priv = cb_priv;
527 return VPX_CODEC_ERROR;
530 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
532 vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
535 vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
536 YV12_BUFFER_CONFIG sd;
538 image2yuvconfig(&frame->img, &sd);
539 return vp9_set_reference_dec(&ctx->pbi->common,
540 (VP9_REFFRAME)frame->frame_type, &sd);
542 return VPX_CODEC_INVALID_PARAM;
546 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
548 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
551 vpx_ref_frame_t *frame = (vpx_ref_frame_t *)data;
552 YV12_BUFFER_CONFIG sd;
554 image2yuvconfig(&frame->img, &sd);
556 return vp9_copy_reference_dec(ctx->pbi,
557 (VP9_REFFRAME)frame->frame_type, &sd);
559 return VPX_CODEC_INVALID_PARAM;
563 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
565 vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
568 YV12_BUFFER_CONFIG* fb;
570 vp9_get_reference_dec(ctx->pbi, data->idx, &fb);
571 yuvconfig2image(&data->img, fb, NULL);
574 return VPX_CODEC_INVALID_PARAM;
578 static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
580 #if CONFIG_VP9_POSTPROC
581 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
584 ctx->postproc_cfg_set = 1;
585 ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
588 return VPX_CODEC_INVALID_PARAM;
593 return VPX_CODEC_INCAPABLE;
597 static vpx_codec_err_t ctrl_set_dbg_options(vpx_codec_alg_priv_t *ctx,
601 return VPX_CODEC_INCAPABLE;
604 static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
606 int *const update_info = va_arg(args, int *);
610 *update_info = ctx->pbi->refresh_frame_flags;
612 return VPX_CODEC_ERROR;
615 return VPX_CODEC_INVALID_PARAM;
620 static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
622 int *corrupted = va_arg(args, int *);
626 *corrupted = ctx->pbi->common.frame_to_show->corrupted;
628 return VPX_CODEC_ERROR;
631 return VPX_CODEC_INVALID_PARAM;
635 static vpx_codec_err_t ctrl_get_display_size(vpx_codec_alg_priv_t *ctx,
637 int *const display_size = va_arg(args, int *);
641 const VP9_COMMON *const cm = &ctx->pbi->common;
642 display_size[0] = cm->display_width;
643 display_size[1] = cm->display_height;
645 return VPX_CODEC_ERROR;
649 return VPX_CODEC_INVALID_PARAM;
653 static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
655 ctx->invert_tile_order = va_arg(args, int);
659 static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
661 vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
662 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
663 ctx->decrypt_state = init ? init->decrypt_state : NULL;
667 static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
668 {VP8_COPY_REFERENCE, ctrl_copy_reference},
671 {VP8_SET_REFERENCE, ctrl_set_reference},
672 {VP8_SET_POSTPROC, ctrl_set_postproc},
673 {VP8_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options},
674 {VP8_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options},
675 {VP8_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options},
676 {VP8_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options},
677 {VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order},
678 {VPXD_SET_DECRYPTOR, ctrl_set_decryptor},
681 {VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates},
682 {VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted},
683 {VP9_GET_REFERENCE, ctrl_get_reference},
684 {VP9D_GET_DISPLAY_SIZE, ctrl_get_display_size},
689 #ifndef VERSION_STRING
690 #define VERSION_STRING
692 CODEC_INTERFACE(vpx_codec_vp9_dx) = {
693 "WebM Project VP9 Decoder" VERSION_STRING,
694 VPX_CODEC_INTERNAL_ABI_VERSION,
695 VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
696 VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // vpx_codec_caps_t
697 decoder_init, // vpx_codec_init_fn_t
698 decoder_destroy, // vpx_codec_destroy_fn_t
699 decoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
700 NOT_IMPLEMENTED, // vpx_codec_get_mmap_fn_t
701 NOT_IMPLEMENTED, // vpx_codec_set_mmap_fn_t
703 decoder_peek_si, // vpx_codec_peek_si_fn_t
704 decoder_get_si, // vpx_codec_get_si_fn_t
705 decoder_decode, // vpx_codec_decode_fn_t
706 decoder_get_frame, // vpx_codec_frame_get_fn_t
707 decoder_set_fb_fn, // vpx_codec_set_fb_fn_t
711 NOT_IMPLEMENTED, // vpx_codec_enc_cfg_map_t
712 NOT_IMPLEMENTED, // vpx_codec_encode_fn_t
713 NOT_IMPLEMENTED, // vpx_codec_get_cx_data_fn_t
714 NOT_IMPLEMENTED, // vpx_codec_enc_config_set_fn_t
715 NOT_IMPLEMENTED, // vpx_codec_get_global_headers_fn_t
716 NOT_IMPLEMENTED, // vpx_codec_get_preview_frame_fn_t
717 NOT_IMPLEMENTED // vpx_codec_enc_mr_get_mem_loc_fn_t