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_config.h"
15 #include "./vpx_version.h"
17 #include "vpx/internal/vpx_codec_internal.h"
18 #include "vpx/vp8dx.h"
19 #include "vpx/vpx_decoder.h"
21 #include "vp9/common/vp9_alloccommon.h"
22 #include "vp9/common/vp9_frame_buffers.h"
23 #include "vp9/common/vp9_thread.h"
25 #include "vp9/decoder/vp9_decoder.h"
26 #include "vp9/decoder/vp9_decodeframe.h"
27 #include "vp9/decoder/vp9_read_bit_buffer.h"
29 #include "vp9/vp9_iface_common.h"
31 #define VP9_CAP_POSTPROC (CONFIG_VP9_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
33 typedef vpx_codec_stream_info_t vp9_stream_info_t;
35 // This limit is due to framebuffer numbers.
36 // TODO(hkuang): Remove this limit after implementing ondemand framebuffers.
37 #define FRAME_CACHE_SIZE 6 // Cache maximum 6 decoded frames.
39 typedef struct cache_frame {
44 struct vpx_codec_alg_priv {
45 vpx_codec_priv_t base;
46 vpx_codec_dec_cfg_t cfg;
49 vp8_postproc_cfg_t postproc_cfg;
50 vpx_decrypt_cb decrypt_cb;
55 int invert_tile_order;
56 int last_show_frame; // Index of last output frame.
58 // Frame parallel related.
59 int frame_parallel_decode; // frame-based threading.
61 VP9Worker *frame_workers;
62 int num_frame_workers;
63 int next_submit_worker_id;
64 int last_submit_worker_id;
65 int next_output_worker_id;
66 int available_threads;
67 cache_frame frame_cache[FRAME_CACHE_SIZE];
68 int frame_cache_write;
72 // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
73 BufferPool *buffer_pool;
75 // External frame buffer info to save for VP9 common.
76 void *ext_priv; // Private data associated with the external frame buffers.
77 vpx_get_frame_buffer_cb_fn_t get_ext_fb_cb;
78 vpx_release_frame_buffer_cb_fn_t release_ext_fb_cb;
81 static vpx_codec_err_t decoder_init(vpx_codec_ctx_t *ctx,
82 vpx_codec_priv_enc_mr_cfg_t *data) {
83 // This function only allocates space for the vpx_codec_alg_priv_t
84 // structure. More memory may be required at the time the stream
85 // information becomes known.
89 vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
91 return VPX_CODEC_MEM_ERROR;
93 ctx->priv = (vpx_codec_priv_t *)priv;
94 ctx->priv->init_flags = ctx->init_flags;
95 priv->si.sz = sizeof(priv->si);
97 // Only do frame parallel decode when threads > 1.
98 priv->frame_parallel_decode =
99 (ctx->config.dec && (ctx->config.dec->threads > 1) &&
100 (ctx->init_flags & VPX_CODEC_USE_FRAME_THREADING)) ? 1 : 0;
101 if (ctx->config.dec) {
102 priv->cfg = *ctx->config.dec;
103 ctx->config.dec = &priv->cfg;
110 static vpx_codec_err_t decoder_destroy(vpx_codec_alg_priv_t *ctx) {
111 if (ctx->frame_workers != NULL) {
113 for (i = 0; i < ctx->num_frame_workers; ++i) {
114 VP9Worker *const worker = &ctx->frame_workers[i];
115 FrameWorkerData *const frame_worker_data =
116 (FrameWorkerData *)worker->data1;
117 vp9_get_worker_interface()->end(worker);
118 vp9_remove_common(&frame_worker_data->pbi->common);
119 vp9_decoder_remove(frame_worker_data->pbi);
120 vpx_free(frame_worker_data->scratch_buffer);
121 #if CONFIG_MULTITHREAD
122 pthread_mutex_destroy(&frame_worker_data->stats_mutex);
123 pthread_cond_destroy(&frame_worker_data->stats_cond);
125 vpx_free(frame_worker_data);
127 #if CONFIG_MULTITHREAD
128 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
132 if (ctx->buffer_pool)
133 vp9_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
135 vpx_free(ctx->frame_workers);
136 vpx_free(ctx->buffer_pool);
141 static int parse_bitdepth_colorspace_sampling(
142 BITSTREAM_PROFILE profile, struct vp9_read_bit_buffer *rb) {
143 vpx_color_space_t color_space;
144 if (profile >= PROFILE_2)
145 rb->bit_offset += 1; // Bit-depth 10 or 12.
146 color_space = (vpx_color_space_t)vp9_rb_read_literal(rb, 3);
147 if (color_space != VPX_CS_SRGB) {
148 rb->bit_offset += 1; // [16,235] (including xvycc) vs [0,255] range.
149 if (profile == PROFILE_1 || profile == PROFILE_3) {
150 rb->bit_offset += 2; // subsampling x/y.
151 rb->bit_offset += 1; // unused.
154 if (profile == PROFILE_1 || profile == PROFILE_3) {
155 rb->bit_offset += 1; // unused
157 // RGB is only available in version 1.
164 static vpx_codec_err_t decoder_peek_si_internal(const uint8_t *data,
165 unsigned int data_sz,
166 vpx_codec_stream_info_t *si,
168 vpx_decrypt_cb decrypt_cb,
169 void *decrypt_state) {
170 int intra_only_flag = 0;
171 uint8_t clear_buffer[9];
173 if (data + data_sz <= data)
174 return VPX_CODEC_INVALID_PARAM;
180 data_sz = MIN(sizeof(clear_buffer), data_sz);
181 decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
188 struct vp9_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
189 const int frame_marker = vp9_rb_read_literal(&rb, 2);
190 const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
192 if (frame_marker != VP9_FRAME_MARKER)
193 return VPX_CODEC_UNSUP_BITSTREAM;
195 if (profile >= MAX_PROFILES)
196 return VPX_CODEC_UNSUP_BITSTREAM;
198 if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
199 return VPX_CODEC_UNSUP_BITSTREAM;
201 if (vp9_rb_read_bit(&rb)) { // show an existing frame
202 vp9_rb_read_literal(&rb, 3); // Frame buffer to show.
207 return VPX_CODEC_UNSUP_BITSTREAM;
209 si->is_kf = !vp9_rb_read_bit(&rb);
210 show_frame = vp9_rb_read_bit(&rb);
211 error_resilient = vp9_rb_read_bit(&rb);
214 if (!vp9_read_sync_code(&rb))
215 return VPX_CODEC_UNSUP_BITSTREAM;
217 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
218 return VPX_CODEC_UNSUP_BITSTREAM;
219 vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
221 intra_only_flag = show_frame ? 0 : vp9_rb_read_bit(&rb);
223 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
225 if (intra_only_flag) {
226 if (!vp9_read_sync_code(&rb))
227 return VPX_CODEC_UNSUP_BITSTREAM;
228 if (profile > PROFILE_0) {
229 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
230 return VPX_CODEC_UNSUP_BITSTREAM;
232 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
233 vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
237 if (is_intra_only != NULL)
238 *is_intra_only = intra_only_flag;
242 static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
243 unsigned int data_sz,
244 vpx_codec_stream_info_t *si) {
245 return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
248 static vpx_codec_err_t decoder_get_si(vpx_codec_alg_priv_t *ctx,
249 vpx_codec_stream_info_t *si) {
250 const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
251 ? sizeof(vp9_stream_info_t)
252 : sizeof(vpx_codec_stream_info_t);
253 memcpy(si, &ctx->si, sz);
254 si->sz = (unsigned int)sz;
259 static void set_error_detail(vpx_codec_alg_priv_t *ctx,
260 const char *const error) {
261 ctx->base.err_detail = error;
264 static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
265 const struct vpx_internal_error_info *error) {
266 if (error->error_code)
267 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
269 return error->error_code;
272 static void init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
275 for (i = 0; i < ctx->num_frame_workers; ++i) {
276 VP9Worker *const worker = &ctx->frame_workers[i];
277 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
278 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
279 BufferPool *const pool = cm->buffer_pool;
282 cm->byte_alignment = ctx->byte_alignment;
284 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
285 pool->get_fb_cb = ctx->get_ext_fb_cb;
286 pool->release_fb_cb = ctx->release_ext_fb_cb;
287 pool->cb_priv = ctx->ext_priv;
289 pool->get_fb_cb = vp9_get_frame_buffer;
290 pool->release_fb_cb = vp9_release_frame_buffer;
292 if (vp9_alloc_internal_frame_buffers(&pool->int_frame_buffers))
293 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
294 "Failed to initialize internal frame buffers");
296 pool->cb_priv = &pool->int_frame_buffers;
301 static void set_default_ppflags(vp8_postproc_cfg_t *cfg) {
302 cfg->post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
303 cfg->deblocking_level = 4;
304 cfg->noise_level = 0;
307 static void set_ppflags(const vpx_codec_alg_priv_t *ctx,
308 vp9_ppflags_t *flags) {
309 flags->post_proc_flag =
310 ctx->postproc_cfg.post_proc_flag;
312 flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
313 flags->noise_level = ctx->postproc_cfg.noise_level;
316 static int frame_worker_hook(void *arg1, void *arg2) {
317 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
318 const uint8_t *data = frame_worker_data->data;
321 frame_worker_data->result =
322 vp9_receive_compressed_data(frame_worker_data->pbi,
323 frame_worker_data->data_size,
325 frame_worker_data->data_end = data;
327 if (frame_worker_data->pbi->frame_parallel_decode) {
328 // In frame parallel decoding, a worker thread must successfully decode all
329 // the compressed data.
330 if (frame_worker_data->result != 0 ||
331 frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
332 VP9Worker *const worker = frame_worker_data->pbi->frame_worker_owner;
333 BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
334 // Signal all the other threads that are waiting for this frame.
335 vp9_frameworker_lock_stats(worker);
336 frame_worker_data->frame_context_ready = 1;
337 lock_buffer_pool(pool);
338 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
339 unlock_buffer_pool(pool);
340 frame_worker_data->pbi->need_resync = 1;
341 vp9_frameworker_signal_stats(worker);
342 vp9_frameworker_unlock_stats(worker);
345 } else if (frame_worker_data->result != 0) {
346 // Check decode result in serial decode.
347 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
348 frame_worker_data->pbi->need_resync = 1;
350 return !frame_worker_data->result;
353 static vpx_codec_err_t init_decoder(vpx_codec_alg_priv_t *ctx) {
355 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
357 ctx->last_show_frame = -1;
358 ctx->next_submit_worker_id = 0;
359 ctx->last_submit_worker_id = 0;
360 ctx->next_output_worker_id = 0;
361 ctx->frame_cache_read = 0;
362 ctx->frame_cache_write = 0;
363 ctx->num_cache_frames = 0;
364 ctx->num_frame_workers =
365 (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads: 1;
366 ctx->available_threads = ctx->num_frame_workers;
369 ctx->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
370 if (ctx->buffer_pool == NULL)
371 return VPX_CODEC_MEM_ERROR;
373 #if CONFIG_MULTITHREAD
374 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
375 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
376 return VPX_CODEC_MEM_ERROR;
380 ctx->frame_workers = (VP9Worker *)
381 vpx_malloc(ctx->num_frame_workers * sizeof(*ctx->frame_workers));
382 if (ctx->frame_workers == NULL) {
383 set_error_detail(ctx, "Failed to allocate frame_workers");
384 return VPX_CODEC_MEM_ERROR;
387 for (i = 0; i < ctx->num_frame_workers; ++i) {
388 VP9Worker *const worker = &ctx->frame_workers[i];
389 FrameWorkerData *frame_worker_data = NULL;
390 winterface->init(worker);
391 worker->data1 = vpx_memalign(32, sizeof(FrameWorkerData));
392 if (worker->data1 == NULL) {
393 set_error_detail(ctx, "Failed to allocate frame_worker_data");
394 return VPX_CODEC_MEM_ERROR;
396 frame_worker_data = (FrameWorkerData *)worker->data1;
397 frame_worker_data->pbi = vp9_decoder_create(ctx->buffer_pool);
398 if (frame_worker_data->pbi == NULL) {
399 set_error_detail(ctx, "Failed to allocate frame_worker_data");
400 return VPX_CODEC_MEM_ERROR;
402 frame_worker_data->pbi->frame_worker_owner = worker;
403 frame_worker_data->worker_id = i;
404 frame_worker_data->scratch_buffer = NULL;
405 frame_worker_data->scratch_buffer_size = 0;
406 frame_worker_data->frame_context_ready = 0;
407 #if CONFIG_MULTITHREAD
408 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
409 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
410 return VPX_CODEC_MEM_ERROR;
413 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
414 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
415 return VPX_CODEC_MEM_ERROR;
418 // If decoding in serial mode, FrameWorker thread could create tile worker
419 // thread or loopfilter thread.
420 frame_worker_data->pbi->max_threads =
421 (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
423 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
424 frame_worker_data->pbi->frame_parallel_decode = ctx->frame_parallel_decode;
425 frame_worker_data->pbi->common.frame_parallel_decode =
426 ctx->frame_parallel_decode;
427 worker->hook = (VP9WorkerHook)frame_worker_hook;
428 if (!winterface->reset(worker)) {
429 set_error_detail(ctx, "Frame Worker thread creation failed");
430 return VPX_CODEC_MEM_ERROR;
434 // If postprocessing was enabled by the application and a
435 // configuration has not been provided, default it.
436 if (!ctx->postproc_cfg_set &&
437 (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
438 set_default_ppflags(&ctx->postproc_cfg);
440 init_buffer_callbacks(ctx);
445 static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
446 const uint8_t **data, unsigned int data_sz,
447 void *user_priv, int64_t deadline) {
448 vp9_ppflags_t flags = {0, 0, 0};
449 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
452 // Determine the stream parameters. Note that we rely on peek_si to
453 // validate that we have a buffer that does not wrap around the top
456 int is_intra_only = 0;
457 const vpx_codec_err_t res =
458 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
459 ctx->decrypt_cb, ctx->decrypt_state);
460 if (res != VPX_CODEC_OK)
463 if (!ctx->si.is_kf && !is_intra_only)
464 return VPX_CODEC_ERROR;
467 if (!ctx->frame_parallel_decode) {
468 VP9Worker *const worker = ctx->frame_workers;
469 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
470 frame_worker_data->data = *data;
471 frame_worker_data->data_size = data_sz;
472 frame_worker_data->user_priv = user_priv;
474 // Set these even if already initialized. The caller may have changed the
475 // decrypt config between frames.
476 frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
477 frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;
479 worker->had_error = 0;
480 winterface->execute(worker);
482 // Update data pointer after decode.
483 *data = frame_worker_data->data_end;
485 if (worker->had_error)
486 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
488 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
489 VP9Worker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
490 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
491 // Copy context from last worker thread to next worker thread.
492 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
493 vp9_frameworker_copy_context(
494 &ctx->frame_workers[ctx->next_submit_worker_id],
495 &ctx->frame_workers[ctx->last_submit_worker_id]);
497 frame_worker_data->pbi->ready_for_new_data = 0;
498 // Copy the compressed data into worker's internal buffer.
499 // TODO(hkuang): Will all the workers allocate the same size
500 // as the size of the first intra frame be better? This will
501 // avoid too many deallocate and allocate.
502 if (frame_worker_data->scratch_buffer_size < data_sz) {
503 frame_worker_data->scratch_buffer =
504 (uint8_t *)vpx_realloc(frame_worker_data->scratch_buffer, data_sz);
505 if (frame_worker_data->scratch_buffer == NULL) {
506 set_error_detail(ctx, "Failed to reallocate scratch buffer");
507 return VPX_CODEC_MEM_ERROR;
509 frame_worker_data->scratch_buffer_size = data_sz;
511 frame_worker_data->data_size = data_sz;
512 vpx_memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
514 frame_worker_data->frame_decoded = 0;
515 frame_worker_data->frame_context_ready = 0;
516 frame_worker_data->received_frame = 1;
517 frame_worker_data->data = frame_worker_data->scratch_buffer;
518 frame_worker_data->user_priv = user_priv;
520 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
521 ctx->last_submit_worker_id =
522 (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
524 ctx->next_submit_worker_id =
525 (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
526 --ctx->available_threads;
527 worker->had_error = 0;
528 winterface->launch(worker);
531 if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
532 set_ppflags(ctx, &flags);
537 static void wait_worker_and_cache_frame(vpx_codec_alg_priv_t *ctx) {
538 YV12_BUFFER_CONFIG sd;
539 vp9_ppflags_t flags = {0, 0, 0};
540 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
541 VP9Worker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
542 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
543 ctx->next_output_worker_id =
544 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
545 // TODO(hkuang): Add worker error handling here.
546 winterface->sync(worker);
547 frame_worker_data->received_frame = 0;
548 ++ctx->available_threads;
549 if (vp9_get_raw_frame(frame_worker_data->pbi, &sd, &flags) == 0) {
550 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
551 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
552 ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
553 yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
554 frame_worker_data->user_priv);
555 ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
556 frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
557 ctx->frame_cache_write =
558 (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
559 ++ctx->num_cache_frames;
563 static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
564 const uint8_t *data, unsigned int data_sz,
565 void *user_priv, long deadline) {
566 const uint8_t *data_start = data;
567 const uint8_t * const data_end = data + data_sz;
569 uint32_t frame_sizes[8];
572 if (data == NULL && data_sz == 0) {
577 // Reset flushed when receiving a valid frame.
580 // Initialize the decoder workers on the first frame.
581 if (ctx->frame_workers == NULL) {
582 const vpx_codec_err_t res = init_decoder(ctx);
583 if (res != VPX_CODEC_OK)
587 res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
588 ctx->decrypt_cb, ctx->decrypt_state);
589 if (res != VPX_CODEC_OK)
592 if (ctx->frame_parallel_decode) {
593 // Decode in frame parallel mode. When decoding in this mode, the frame
594 // passed to the decoder must be either a normal frame or a superframe with
595 // superframe index so the decoder could get each frame's start position
596 // in the superframe.
597 if (frame_count > 0) {
600 for (i = 0; i < frame_count; ++i) {
601 const uint8_t *data_start_copy = data_start;
602 const uint32_t frame_size = frame_sizes[i];
603 if (data_start < data
604 || frame_size > (uint32_t) (data_end - data_start)) {
605 set_error_detail(ctx, "Invalid frame size in index");
606 return VPX_CODEC_CORRUPT_FRAME;
609 if (ctx->available_threads == 0) {
610 // No more threads for decoding. Wait until the next output worker
611 // finishes decoding. Then copy the decoded frame into cache.
612 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
613 wait_worker_and_cache_frame(ctx);
615 // TODO(hkuang): Add unit test to test this path.
616 set_error_detail(ctx, "Frame output cache is full.");
617 return VPX_CODEC_ERROR;
621 res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
623 if (res != VPX_CODEC_OK)
625 data_start += frame_size;
628 if (ctx->available_threads == 0) {
629 // No more threads for decoding. Wait until the next output worker
630 // finishes decoding. Then copy the decoded frame into cache.
631 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
632 wait_worker_and_cache_frame(ctx);
634 // TODO(hkuang): Add unit test to test this path.
635 set_error_detail(ctx, "Frame output cache is full.");
636 return VPX_CODEC_ERROR;
640 res = decode_one(ctx, &data, data_sz, user_priv, deadline);
641 if (res != VPX_CODEC_OK)
645 // Decode in serial mode.
646 if (frame_count > 0) {
649 for (i = 0; i < frame_count; ++i) {
650 const uint8_t *data_start_copy = data_start;
651 const uint32_t frame_size = frame_sizes[i];
653 if (data_start < data
654 || frame_size > (uint32_t) (data_end - data_start)) {
655 set_error_detail(ctx, "Invalid frame size in index");
656 return VPX_CODEC_CORRUPT_FRAME;
659 res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
661 if (res != VPX_CODEC_OK)
664 data_start += frame_size;
667 while (data_start < data_end) {
668 const uint32_t frame_size = (uint32_t) (data_end - data_start);
669 const vpx_codec_err_t res = decode_one(ctx, &data_start, frame_size,
670 user_priv, deadline);
671 if (res != VPX_CODEC_OK)
674 // Account for suboptimal termination by the encoder.
675 while (data_start < data_end) {
676 const uint8_t marker = read_marker(ctx->decrypt_cb,
677 ctx->decrypt_state, data_start);
689 static void release_last_output_frame(vpx_codec_alg_priv_t *ctx) {
690 RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
691 // Decrease reference count of last output frame in frame parallel mode.
692 if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
693 BufferPool *const pool = ctx->buffer_pool;
694 lock_buffer_pool(pool);
695 decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
696 unlock_buffer_pool(pool);
700 static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
701 vpx_codec_iter_t *iter) {
702 vpx_image_t *img = NULL;
704 // Only return frame when all the cpu are busy or
705 // application fluhsed the decoder in frame parallel decode.
706 if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
711 // Output the frames in the cache first.
712 if (ctx->num_cache_frames > 0) {
713 release_last_output_frame(ctx);
714 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
715 img = &ctx->frame_cache[ctx->frame_cache_read].img;
716 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
717 --ctx->num_cache_frames;
721 // iter acts as a flip flop, so an image is only returned on the first
722 // call to get_frame.
723 if (*iter == NULL && ctx->frame_workers != NULL) {
725 YV12_BUFFER_CONFIG sd;
726 vp9_ppflags_t flags = {0, 0, 0};
727 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
728 VP9Worker *const worker =
729 &ctx->frame_workers[ctx->next_output_worker_id];
730 FrameWorkerData *const frame_worker_data =
731 (FrameWorkerData *)worker->data1;
732 ctx->next_output_worker_id =
733 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
734 // Wait for the frame from worker thread.
735 if (winterface->sync(worker)) {
736 // Check if worker has received any frames.
737 if (frame_worker_data->received_frame == 1)
738 ++ctx->available_threads;
739 frame_worker_data->received_frame = 0;
740 if (vp9_get_raw_frame(frame_worker_data->pbi, &sd, &flags) == 0) {
741 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
742 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
743 release_last_output_frame(ctx);
744 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
745 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
746 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
751 // Decoding failed. Release the worker thread.
752 frame_worker_data->received_frame = 0;
753 ++ctx->available_threads;
754 if (ctx->flushed != 1)
757 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
762 static vpx_codec_err_t decoder_set_fb_fn(
763 vpx_codec_alg_priv_t *ctx,
764 vpx_get_frame_buffer_cb_fn_t cb_get,
765 vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
766 if (cb_get == NULL || cb_release == NULL) {
767 return VPX_CODEC_INVALID_PARAM;
768 } else if (ctx->frame_workers == NULL) {
769 // If the decoder has already been initialized, do not accept changes to
770 // the frame buffer functions.
771 ctx->get_ext_fb_cb = cb_get;
772 ctx->release_ext_fb_cb = cb_release;
773 ctx->ext_priv = cb_priv;
777 return VPX_CODEC_ERROR;
780 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
782 vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
784 // Only support this function in serial decode.
785 if (ctx->frame_parallel_decode) {
786 set_error_detail(ctx, "Not supported in frame parallel decode");
787 return VPX_CODEC_INCAPABLE;
791 vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
792 YV12_BUFFER_CONFIG sd;
793 VP9Worker *const worker = ctx->frame_workers;
794 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
795 image2yuvconfig(&frame->img, &sd);
796 return vp9_set_reference_dec(&frame_worker_data->pbi->common,
797 (VP9_REFFRAME)frame->frame_type, &sd);
799 return VPX_CODEC_INVALID_PARAM;
803 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
805 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
807 // Only support this function in serial decode.
808 if (ctx->frame_parallel_decode) {
809 set_error_detail(ctx, "Not supported in frame parallel decode");
810 return VPX_CODEC_INCAPABLE;
814 vpx_ref_frame_t *frame = (vpx_ref_frame_t *) data;
815 YV12_BUFFER_CONFIG sd;
816 VP9Worker *const worker = ctx->frame_workers;
817 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
818 image2yuvconfig(&frame->img, &sd);
819 return vp9_copy_reference_dec(frame_worker_data->pbi,
820 (VP9_REFFRAME)frame->frame_type, &sd);
822 return VPX_CODEC_INVALID_PARAM;
826 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
828 vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
830 // Only support this function in serial decode.
831 if (ctx->frame_parallel_decode) {
832 set_error_detail(ctx, "Not supported in frame parallel decode");
833 return VPX_CODEC_INCAPABLE;
837 YV12_BUFFER_CONFIG* fb;
838 VP9Worker *const worker = ctx->frame_workers;
839 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
840 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
841 if (fb == NULL) return VPX_CODEC_ERROR;
842 yuvconfig2image(&data->img, fb, NULL);
845 return VPX_CODEC_INVALID_PARAM;
849 static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
851 #if CONFIG_VP9_POSTPROC
852 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
855 ctx->postproc_cfg_set = 1;
856 ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
859 return VPX_CODEC_INVALID_PARAM;
864 return VPX_CODEC_INCAPABLE;
868 static vpx_codec_err_t ctrl_set_dbg_options(vpx_codec_alg_priv_t *ctx,
872 return VPX_CODEC_INCAPABLE;
875 static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
877 int *const update_info = va_arg(args, int *);
879 // Only support this function in serial decode.
880 if (ctx->frame_parallel_decode) {
881 set_error_detail(ctx, "Not supported in frame parallel decode");
882 return VPX_CODEC_INCAPABLE;
886 if (ctx->frame_workers) {
887 VP9Worker *const worker = ctx->frame_workers;
888 FrameWorkerData *const frame_worker_data =
889 (FrameWorkerData *)worker->data1;
890 *update_info = frame_worker_data->pbi->refresh_frame_flags;
892 return VPX_CODEC_ERROR;
896 return VPX_CODEC_INVALID_PARAM;
900 static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
902 int *corrupted = va_arg(args, int *);
905 if (ctx->frame_workers) {
906 VP9Worker *const worker = ctx->frame_workers;
907 FrameWorkerData *const frame_worker_data =
908 (FrameWorkerData *)worker->data1;
909 RefCntBuffer *const frame_bufs =
910 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
911 if (frame_worker_data->pbi->common.frame_to_show == NULL)
912 return VPX_CODEC_ERROR;
913 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
915 return VPX_CODEC_ERROR;
919 return VPX_CODEC_INVALID_PARAM;
923 static vpx_codec_err_t ctrl_get_display_size(vpx_codec_alg_priv_t *ctx,
925 int *const display_size = va_arg(args, int *);
927 // Only support this function in serial decode.
928 if (ctx->frame_parallel_decode) {
929 set_error_detail(ctx, "Not supported in frame parallel decode");
930 return VPX_CODEC_INCAPABLE;
934 if (ctx->frame_workers) {
935 VP9Worker *const worker = ctx->frame_workers;
936 FrameWorkerData *const frame_worker_data =
937 (FrameWorkerData *)worker->data1;
938 const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
939 display_size[0] = cm->display_width;
940 display_size[1] = cm->display_height;
942 return VPX_CODEC_ERROR;
946 return VPX_CODEC_INVALID_PARAM;
950 static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
952 unsigned int *const bit_depth = va_arg(args, unsigned int *);
953 VP9Worker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
957 FrameWorkerData *const frame_worker_data =
958 (FrameWorkerData *)worker->data1;
959 const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
960 *bit_depth = cm->bit_depth;
963 return VPX_CODEC_ERROR;
966 return VPX_CODEC_INVALID_PARAM;
970 static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
972 ctx->invert_tile_order = va_arg(args, int);
976 static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
978 vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
979 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
980 ctx->decrypt_state = init ? init->decrypt_state : NULL;
984 static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
986 const int legacy_byte_alignment = 0;
987 const int min_byte_alignment = 32;
988 const int max_byte_alignment = 1024;
989 const int byte_alignment = va_arg(args, int);
991 if (byte_alignment != legacy_byte_alignment &&
992 (byte_alignment < min_byte_alignment ||
993 byte_alignment > max_byte_alignment ||
994 (byte_alignment & (byte_alignment - 1)) != 0))
995 return VPX_CODEC_INVALID_PARAM;
997 ctx->byte_alignment = byte_alignment;
998 if (ctx->frame_workers) {
999 VP9Worker *const worker = ctx->frame_workers;
1000 FrameWorkerData *const frame_worker_data =
1001 (FrameWorkerData *)worker->data1;
1002 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1004 return VPX_CODEC_OK;
1007 static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
1008 {VP8_COPY_REFERENCE, ctrl_copy_reference},
1011 {VP8_SET_REFERENCE, ctrl_set_reference},
1012 {VP8_SET_POSTPROC, ctrl_set_postproc},
1013 {VP8_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options},
1014 {VP8_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options},
1015 {VP8_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options},
1016 {VP8_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options},
1017 {VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order},
1018 {VPXD_SET_DECRYPTOR, ctrl_set_decryptor},
1019 {VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment},
1022 {VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates},
1023 {VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted},
1024 {VP9_GET_REFERENCE, ctrl_get_reference},
1025 {VP9D_GET_DISPLAY_SIZE, ctrl_get_display_size},
1026 {VP9D_GET_BIT_DEPTH, ctrl_get_bit_depth},
1031 #ifndef VERSION_STRING
1032 #define VERSION_STRING
1034 CODEC_INTERFACE(vpx_codec_vp9_dx) = {
1035 "WebM Project VP9 Decoder" VERSION_STRING,
1036 VPX_CODEC_INTERNAL_ABI_VERSION,
1037 VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
1038 VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // vpx_codec_caps_t
1039 decoder_init, // vpx_codec_init_fn_t
1040 decoder_destroy, // vpx_codec_destroy_fn_t
1041 decoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
1043 decoder_peek_si, // vpx_codec_peek_si_fn_t
1044 decoder_get_si, // vpx_codec_get_si_fn_t
1045 decoder_decode, // vpx_codec_decode_fn_t
1046 decoder_get_frame, // vpx_codec_frame_get_fn_t
1047 decoder_set_fb_fn, // vpx_codec_set_fb_fn_t
1051 NULL, // vpx_codec_enc_cfg_map_t
1052 NULL, // vpx_codec_encode_fn_t
1053 NULL, // vpx_codec_get_cx_data_fn_t
1054 NULL, // vpx_codec_enc_config_set_fn_t
1055 NULL, // vpx_codec_get_global_headers_fn_t
1056 NULL, // vpx_codec_get_preview_frame_fn_t
1057 NULL // vpx_codec_enc_mr_get_mem_loc_fn_t