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.
59 // Frame parallel related.
60 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;
71 int need_resync; // wait for key/intra-only frame
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 #if CONFIG_VP9_POSTPROC
120 vp9_free_postproc_buffers(&frame_worker_data->pbi->common);
122 vp9_decoder_remove(frame_worker_data->pbi);
123 vpx_free(frame_worker_data->scratch_buffer);
124 #if CONFIG_MULTITHREAD
125 pthread_mutex_destroy(&frame_worker_data->stats_mutex);
126 pthread_cond_destroy(&frame_worker_data->stats_cond);
128 vpx_free(frame_worker_data);
130 #if CONFIG_MULTITHREAD
131 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
135 if (ctx->buffer_pool) {
136 vp9_free_ref_frame_buffers(ctx->buffer_pool);
137 vp9_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
140 vpx_free(ctx->frame_workers);
141 vpx_free(ctx->buffer_pool);
146 static int parse_bitdepth_colorspace_sampling(
147 BITSTREAM_PROFILE profile, struct vp9_read_bit_buffer *rb) {
148 vpx_color_space_t color_space;
149 if (profile >= PROFILE_2)
150 rb->bit_offset += 1; // Bit-depth 10 or 12.
151 color_space = (vpx_color_space_t)vp9_rb_read_literal(rb, 3);
152 if (color_space != VPX_CS_SRGB) {
153 rb->bit_offset += 1; // [16,235] (including xvycc) vs [0,255] range.
154 if (profile == PROFILE_1 || profile == PROFILE_3) {
155 rb->bit_offset += 2; // subsampling x/y.
156 rb->bit_offset += 1; // unused.
159 if (profile == PROFILE_1 || profile == PROFILE_3) {
160 rb->bit_offset += 1; // unused
162 // RGB is only available in version 1.
169 static vpx_codec_err_t decoder_peek_si_internal(const uint8_t *data,
170 unsigned int data_sz,
171 vpx_codec_stream_info_t *si,
173 vpx_decrypt_cb decrypt_cb,
174 void *decrypt_state) {
175 int intra_only_flag = 0;
176 uint8_t clear_buffer[9];
178 if (data + data_sz <= data)
179 return VPX_CODEC_INVALID_PARAM;
185 data_sz = MIN(sizeof(clear_buffer), data_sz);
186 decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
193 struct vp9_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
194 const int frame_marker = vp9_rb_read_literal(&rb, 2);
195 const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
197 if (frame_marker != VP9_FRAME_MARKER)
198 return VPX_CODEC_UNSUP_BITSTREAM;
200 if (profile >= MAX_PROFILES)
201 return VPX_CODEC_UNSUP_BITSTREAM;
203 if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
204 return VPX_CODEC_UNSUP_BITSTREAM;
206 if (vp9_rb_read_bit(&rb)) { // show an existing frame
207 vp9_rb_read_literal(&rb, 3); // Frame buffer to show.
212 return VPX_CODEC_UNSUP_BITSTREAM;
214 si->is_kf = !vp9_rb_read_bit(&rb);
215 show_frame = vp9_rb_read_bit(&rb);
216 error_resilient = vp9_rb_read_bit(&rb);
219 if (!vp9_read_sync_code(&rb))
220 return VPX_CODEC_UNSUP_BITSTREAM;
222 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
223 return VPX_CODEC_UNSUP_BITSTREAM;
224 vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
226 intra_only_flag = show_frame ? 0 : vp9_rb_read_bit(&rb);
228 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
230 if (intra_only_flag) {
231 if (!vp9_read_sync_code(&rb))
232 return VPX_CODEC_UNSUP_BITSTREAM;
233 if (profile > PROFILE_0) {
234 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
235 return VPX_CODEC_UNSUP_BITSTREAM;
237 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
238 vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
242 if (is_intra_only != NULL)
243 *is_intra_only = intra_only_flag;
247 static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
248 unsigned int data_sz,
249 vpx_codec_stream_info_t *si) {
250 return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
253 static vpx_codec_err_t decoder_get_si(vpx_codec_alg_priv_t *ctx,
254 vpx_codec_stream_info_t *si) {
255 const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
256 ? sizeof(vp9_stream_info_t)
257 : sizeof(vpx_codec_stream_info_t);
258 memcpy(si, &ctx->si, sz);
259 si->sz = (unsigned int)sz;
264 static void set_error_detail(vpx_codec_alg_priv_t *ctx,
265 const char *const error) {
266 ctx->base.err_detail = error;
269 static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
270 const struct vpx_internal_error_info *error) {
271 if (error->error_code)
272 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
274 return error->error_code;
277 static void init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
280 for (i = 0; i < ctx->num_frame_workers; ++i) {
281 VP9Worker *const worker = &ctx->frame_workers[i];
282 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
283 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
284 BufferPool *const pool = cm->buffer_pool;
286 cm->new_fb_idx = INVALID_IDX;
287 cm->byte_alignment = ctx->byte_alignment;
289 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
290 pool->get_fb_cb = ctx->get_ext_fb_cb;
291 pool->release_fb_cb = ctx->release_ext_fb_cb;
292 pool->cb_priv = ctx->ext_priv;
294 pool->get_fb_cb = vp9_get_frame_buffer;
295 pool->release_fb_cb = vp9_release_frame_buffer;
297 if (vp9_alloc_internal_frame_buffers(&pool->int_frame_buffers))
298 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
299 "Failed to initialize internal frame buffers");
301 pool->cb_priv = &pool->int_frame_buffers;
306 static void set_default_ppflags(vp8_postproc_cfg_t *cfg) {
307 cfg->post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
308 cfg->deblocking_level = 4;
309 cfg->noise_level = 0;
312 static void set_ppflags(const vpx_codec_alg_priv_t *ctx,
313 vp9_ppflags_t *flags) {
314 flags->post_proc_flag =
315 ctx->postproc_cfg.post_proc_flag;
317 flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
318 flags->noise_level = ctx->postproc_cfg.noise_level;
321 static int frame_worker_hook(void *arg1, void *arg2) {
322 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
323 const uint8_t *data = frame_worker_data->data;
326 frame_worker_data->result =
327 vp9_receive_compressed_data(frame_worker_data->pbi,
328 frame_worker_data->data_size,
330 frame_worker_data->data_end = data;
332 if (frame_worker_data->pbi->frame_parallel_decode) {
333 // In frame parallel decoding, a worker thread must successfully decode all
334 // the compressed data.
335 if (frame_worker_data->result != 0 ||
336 frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
337 VP9Worker *const worker = frame_worker_data->pbi->frame_worker_owner;
338 BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
339 // Signal all the other threads that are waiting for this frame.
340 vp9_frameworker_lock_stats(worker);
341 frame_worker_data->frame_context_ready = 1;
342 lock_buffer_pool(pool);
343 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
344 unlock_buffer_pool(pool);
345 frame_worker_data->pbi->need_resync = 1;
346 vp9_frameworker_signal_stats(worker);
347 vp9_frameworker_unlock_stats(worker);
350 } else if (frame_worker_data->result != 0) {
351 // Check decode result in serial decode.
352 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
353 frame_worker_data->pbi->need_resync = 1;
355 return !frame_worker_data->result;
358 static vpx_codec_err_t init_decoder(vpx_codec_alg_priv_t *ctx) {
360 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
362 ctx->last_show_frame = -1;
363 ctx->next_submit_worker_id = 0;
364 ctx->last_submit_worker_id = 0;
365 ctx->next_output_worker_id = 0;
366 ctx->frame_cache_read = 0;
367 ctx->frame_cache_write = 0;
368 ctx->num_cache_frames = 0;
369 ctx->need_resync = 1;
370 ctx->num_frame_workers =
371 (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads: 1;
372 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
373 ctx->num_frame_workers = MAX_DECODE_THREADS;
374 ctx->available_threads = ctx->num_frame_workers;
377 ctx->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
378 if (ctx->buffer_pool == NULL)
379 return VPX_CODEC_MEM_ERROR;
381 #if CONFIG_MULTITHREAD
382 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
383 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
384 return VPX_CODEC_MEM_ERROR;
388 ctx->frame_workers = (VP9Worker *)
389 vpx_malloc(ctx->num_frame_workers * sizeof(*ctx->frame_workers));
390 if (ctx->frame_workers == NULL) {
391 set_error_detail(ctx, "Failed to allocate frame_workers");
392 return VPX_CODEC_MEM_ERROR;
395 for (i = 0; i < ctx->num_frame_workers; ++i) {
396 VP9Worker *const worker = &ctx->frame_workers[i];
397 FrameWorkerData *frame_worker_data = NULL;
398 winterface->init(worker);
399 worker->data1 = vpx_memalign(32, sizeof(FrameWorkerData));
400 if (worker->data1 == NULL) {
401 set_error_detail(ctx, "Failed to allocate frame_worker_data");
402 return VPX_CODEC_MEM_ERROR;
404 frame_worker_data = (FrameWorkerData *)worker->data1;
405 frame_worker_data->pbi = vp9_decoder_create(ctx->buffer_pool);
406 if (frame_worker_data->pbi == NULL) {
407 set_error_detail(ctx, "Failed to allocate frame_worker_data");
408 return VPX_CODEC_MEM_ERROR;
410 frame_worker_data->pbi->frame_worker_owner = worker;
411 frame_worker_data->worker_id = i;
412 frame_worker_data->scratch_buffer = NULL;
413 frame_worker_data->scratch_buffer_size = 0;
414 frame_worker_data->frame_context_ready = 0;
415 frame_worker_data->received_frame = 0;
416 #if CONFIG_MULTITHREAD
417 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
418 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
419 return VPX_CODEC_MEM_ERROR;
422 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
423 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
424 return VPX_CODEC_MEM_ERROR;
427 // If decoding in serial mode, FrameWorker thread could create tile worker
428 // thread or loopfilter thread.
429 frame_worker_data->pbi->max_threads =
430 (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
432 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
433 frame_worker_data->pbi->frame_parallel_decode = ctx->frame_parallel_decode;
434 frame_worker_data->pbi->common.frame_parallel_decode =
435 ctx->frame_parallel_decode;
436 worker->hook = (VP9WorkerHook)frame_worker_hook;
437 if (!winterface->reset(worker)) {
438 set_error_detail(ctx, "Frame Worker thread creation failed");
439 return VPX_CODEC_MEM_ERROR;
443 // If postprocessing was enabled by the application and a
444 // configuration has not been provided, default it.
445 if (!ctx->postproc_cfg_set &&
446 (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
447 set_default_ppflags(&ctx->postproc_cfg);
449 init_buffer_callbacks(ctx);
454 static INLINE void check_resync(vpx_codec_alg_priv_t *const ctx,
455 const VP9Decoder *const pbi) {
456 // Clear resync flag if worker got a key frame or intra only frame.
457 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
458 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
459 ctx->need_resync = 0;
462 static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
463 const uint8_t **data, unsigned int data_sz,
464 void *user_priv, int64_t deadline) {
465 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
468 // Determine the stream parameters. Note that we rely on peek_si to
469 // validate that we have a buffer that does not wrap around the top
472 int is_intra_only = 0;
473 const vpx_codec_err_t res =
474 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
475 ctx->decrypt_cb, ctx->decrypt_state);
476 if (res != VPX_CODEC_OK)
479 if (!ctx->si.is_kf && !is_intra_only)
480 return VPX_CODEC_ERROR;
483 if (!ctx->frame_parallel_decode) {
484 VP9Worker *const worker = ctx->frame_workers;
485 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
486 frame_worker_data->data = *data;
487 frame_worker_data->data_size = data_sz;
488 frame_worker_data->user_priv = user_priv;
489 frame_worker_data->received_frame = 1;
491 // Set these even if already initialized. The caller may have changed the
492 // decrypt config between frames.
493 frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
494 frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;
496 worker->had_error = 0;
497 winterface->execute(worker);
499 // Update data pointer after decode.
500 *data = frame_worker_data->data_end;
502 if (worker->had_error)
503 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
505 check_resync(ctx, frame_worker_data->pbi);
507 VP9Worker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
508 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
509 // Copy context from last worker thread to next worker thread.
510 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
511 vp9_frameworker_copy_context(
512 &ctx->frame_workers[ctx->next_submit_worker_id],
513 &ctx->frame_workers[ctx->last_submit_worker_id]);
515 frame_worker_data->pbi->ready_for_new_data = 0;
516 // Copy the compressed data into worker's internal buffer.
517 // TODO(hkuang): Will all the workers allocate the same size
518 // as the size of the first intra frame be better? This will
519 // avoid too many deallocate and allocate.
520 if (frame_worker_data->scratch_buffer_size < data_sz) {
521 frame_worker_data->scratch_buffer =
522 (uint8_t *)vpx_realloc(frame_worker_data->scratch_buffer, data_sz);
523 if (frame_worker_data->scratch_buffer == NULL) {
524 set_error_detail(ctx, "Failed to reallocate scratch buffer");
525 return VPX_CODEC_MEM_ERROR;
527 frame_worker_data->scratch_buffer_size = data_sz;
529 frame_worker_data->data_size = data_sz;
530 memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
532 frame_worker_data->frame_decoded = 0;
533 frame_worker_data->frame_context_ready = 0;
534 frame_worker_data->received_frame = 1;
535 frame_worker_data->data = frame_worker_data->scratch_buffer;
536 frame_worker_data->user_priv = user_priv;
538 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
539 ctx->last_submit_worker_id =
540 (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
542 ctx->next_submit_worker_id =
543 (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
544 --ctx->available_threads;
545 worker->had_error = 0;
546 winterface->launch(worker);
552 static void wait_worker_and_cache_frame(vpx_codec_alg_priv_t *ctx) {
553 YV12_BUFFER_CONFIG sd;
554 vp9_ppflags_t flags = {0, 0, 0};
555 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
556 VP9Worker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
557 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
558 ctx->next_output_worker_id =
559 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
560 // TODO(hkuang): Add worker error handling here.
561 winterface->sync(worker);
562 frame_worker_data->received_frame = 0;
563 ++ctx->available_threads;
565 check_resync(ctx, frame_worker_data->pbi);
567 if (vp9_get_raw_frame(frame_worker_data->pbi, &sd, &flags) == 0) {
568 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
569 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
570 ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
571 yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
572 frame_worker_data->user_priv);
573 ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
574 frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
575 ctx->frame_cache_write =
576 (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
577 ++ctx->num_cache_frames;
581 static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
582 const uint8_t *data, unsigned int data_sz,
583 void *user_priv, long deadline) {
584 const uint8_t *data_start = data;
585 const uint8_t * const data_end = data + data_sz;
587 uint32_t frame_sizes[8];
590 if (data == NULL && data_sz == 0) {
595 // Reset flushed when receiving a valid frame.
598 // Initialize the decoder workers on the first frame.
599 if (ctx->frame_workers == NULL) {
600 const vpx_codec_err_t res = init_decoder(ctx);
601 if (res != VPX_CODEC_OK)
605 res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
606 ctx->decrypt_cb, ctx->decrypt_state);
607 if (res != VPX_CODEC_OK)
610 if (ctx->frame_parallel_decode) {
611 // Decode in frame parallel mode. When decoding in this mode, the frame
612 // passed to the decoder must be either a normal frame or a superframe with
613 // superframe index so the decoder could get each frame's start position
614 // in the superframe.
615 if (frame_count > 0) {
618 for (i = 0; i < frame_count; ++i) {
619 const uint8_t *data_start_copy = data_start;
620 const uint32_t frame_size = frame_sizes[i];
621 if (data_start < data
622 || frame_size > (uint32_t) (data_end - data_start)) {
623 set_error_detail(ctx, "Invalid frame size in index");
624 return VPX_CODEC_CORRUPT_FRAME;
627 if (ctx->available_threads == 0) {
628 // No more threads for decoding. Wait until the next output worker
629 // finishes decoding. Then copy the decoded frame into cache.
630 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
631 wait_worker_and_cache_frame(ctx);
633 // TODO(hkuang): Add unit test to test this path.
634 set_error_detail(ctx, "Frame output cache is full.");
635 return VPX_CODEC_ERROR;
639 res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
641 if (res != VPX_CODEC_OK)
643 data_start += frame_size;
646 if (ctx->available_threads == 0) {
647 // No more threads for decoding. Wait until the next output worker
648 // finishes decoding. Then copy the decoded frame into cache.
649 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
650 wait_worker_and_cache_frame(ctx);
652 // TODO(hkuang): Add unit test to test this path.
653 set_error_detail(ctx, "Frame output cache is full.");
654 return VPX_CODEC_ERROR;
658 res = decode_one(ctx, &data, data_sz, user_priv, deadline);
659 if (res != VPX_CODEC_OK)
663 // Decode in serial mode.
664 if (frame_count > 0) {
667 for (i = 0; i < frame_count; ++i) {
668 const uint8_t *data_start_copy = data_start;
669 const uint32_t frame_size = frame_sizes[i];
671 if (data_start < data
672 || frame_size > (uint32_t) (data_end - data_start)) {
673 set_error_detail(ctx, "Invalid frame size in index");
674 return VPX_CODEC_CORRUPT_FRAME;
677 res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
679 if (res != VPX_CODEC_OK)
682 data_start += frame_size;
685 while (data_start < data_end) {
686 const uint32_t frame_size = (uint32_t) (data_end - data_start);
687 const vpx_codec_err_t res = decode_one(ctx, &data_start, frame_size,
688 user_priv, deadline);
689 if (res != VPX_CODEC_OK)
692 // Account for suboptimal termination by the encoder.
693 while (data_start < data_end) {
694 const uint8_t marker = read_marker(ctx->decrypt_cb,
695 ctx->decrypt_state, data_start);
707 static void release_last_output_frame(vpx_codec_alg_priv_t *ctx) {
708 RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
709 // Decrease reference count of last output frame in frame parallel mode.
710 if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
711 BufferPool *const pool = ctx->buffer_pool;
712 lock_buffer_pool(pool);
713 decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
714 unlock_buffer_pool(pool);
718 static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
719 vpx_codec_iter_t *iter) {
720 vpx_image_t *img = NULL;
722 // Only return frame when all the cpu are busy or
723 // application fluhsed the decoder in frame parallel decode.
724 if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
729 // Output the frames in the cache first.
730 if (ctx->num_cache_frames > 0) {
731 release_last_output_frame(ctx);
732 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
733 if (ctx->need_resync)
735 img = &ctx->frame_cache[ctx->frame_cache_read].img;
736 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
737 --ctx->num_cache_frames;
741 // iter acts as a flip flop, so an image is only returned on the first
742 // call to get_frame.
743 if (*iter == NULL && ctx->frame_workers != NULL) {
745 YV12_BUFFER_CONFIG sd;
746 vp9_ppflags_t flags = {0, 0, 0};
747 const VP9WorkerInterface *const winterface = vp9_get_worker_interface();
748 VP9Worker *const worker =
749 &ctx->frame_workers[ctx->next_output_worker_id];
750 FrameWorkerData *const frame_worker_data =
751 (FrameWorkerData *)worker->data1;
752 ctx->next_output_worker_id =
753 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
754 if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
755 set_ppflags(ctx, &flags);
756 // Wait for the frame from worker thread.
757 if (winterface->sync(worker)) {
758 // Check if worker has received any frames.
759 if (frame_worker_data->received_frame == 1) {
760 ++ctx->available_threads;
761 frame_worker_data->received_frame = 0;
762 check_resync(ctx, frame_worker_data->pbi);
764 if (vp9_get_raw_frame(frame_worker_data->pbi, &sd, &flags) == 0) {
765 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
766 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
767 release_last_output_frame(ctx);
768 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
769 if (ctx->need_resync)
771 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
772 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
777 // Decoding failed. Release the worker thread.
778 frame_worker_data->received_frame = 0;
779 ++ctx->available_threads;
780 ctx->need_resync = 1;
781 if (ctx->flushed != 1)
784 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
789 static vpx_codec_err_t decoder_set_fb_fn(
790 vpx_codec_alg_priv_t *ctx,
791 vpx_get_frame_buffer_cb_fn_t cb_get,
792 vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
793 if (cb_get == NULL || cb_release == NULL) {
794 return VPX_CODEC_INVALID_PARAM;
795 } else if (ctx->frame_workers == NULL) {
796 // If the decoder has already been initialized, do not accept changes to
797 // the frame buffer functions.
798 ctx->get_ext_fb_cb = cb_get;
799 ctx->release_ext_fb_cb = cb_release;
800 ctx->ext_priv = cb_priv;
804 return VPX_CODEC_ERROR;
807 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
809 vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
811 // Only support this function in serial decode.
812 if (ctx->frame_parallel_decode) {
813 set_error_detail(ctx, "Not supported in frame parallel decode");
814 return VPX_CODEC_INCAPABLE;
818 vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
819 YV12_BUFFER_CONFIG sd;
820 VP9Worker *const worker = ctx->frame_workers;
821 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
822 image2yuvconfig(&frame->img, &sd);
823 return vp9_set_reference_dec(&frame_worker_data->pbi->common,
824 (VP9_REFFRAME)frame->frame_type, &sd);
826 return VPX_CODEC_INVALID_PARAM;
830 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
832 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
834 // Only support this function in serial decode.
835 if (ctx->frame_parallel_decode) {
836 set_error_detail(ctx, "Not supported in frame parallel decode");
837 return VPX_CODEC_INCAPABLE;
841 vpx_ref_frame_t *frame = (vpx_ref_frame_t *) data;
842 YV12_BUFFER_CONFIG sd;
843 VP9Worker *const worker = ctx->frame_workers;
844 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
845 image2yuvconfig(&frame->img, &sd);
846 return vp9_copy_reference_dec(frame_worker_data->pbi,
847 (VP9_REFFRAME)frame->frame_type, &sd);
849 return VPX_CODEC_INVALID_PARAM;
853 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
855 vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
857 // Only support this function in serial decode.
858 if (ctx->frame_parallel_decode) {
859 set_error_detail(ctx, "Not supported in frame parallel decode");
860 return VPX_CODEC_INCAPABLE;
864 YV12_BUFFER_CONFIG* fb;
865 VP9Worker *const worker = ctx->frame_workers;
866 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
867 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
868 if (fb == NULL) return VPX_CODEC_ERROR;
869 yuvconfig2image(&data->img, fb, NULL);
872 return VPX_CODEC_INVALID_PARAM;
876 static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
878 #if CONFIG_VP9_POSTPROC
879 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
882 ctx->postproc_cfg_set = 1;
883 ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
886 return VPX_CODEC_INVALID_PARAM;
891 return VPX_CODEC_INCAPABLE;
895 static vpx_codec_err_t ctrl_set_dbg_options(vpx_codec_alg_priv_t *ctx,
899 return VPX_CODEC_INCAPABLE;
902 static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
904 int *const update_info = va_arg(args, int *);
906 // Only support this function in serial decode.
907 if (ctx->frame_parallel_decode) {
908 set_error_detail(ctx, "Not supported in frame parallel decode");
909 return VPX_CODEC_INCAPABLE;
913 if (ctx->frame_workers) {
914 VP9Worker *const worker = ctx->frame_workers;
915 FrameWorkerData *const frame_worker_data =
916 (FrameWorkerData *)worker->data1;
917 *update_info = frame_worker_data->pbi->refresh_frame_flags;
920 return VPX_CODEC_ERROR;
924 return VPX_CODEC_INVALID_PARAM;
927 static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
929 int *corrupted = va_arg(args, int *);
932 if (ctx->frame_workers) {
933 VP9Worker *const worker = ctx->frame_workers;
934 FrameWorkerData *const frame_worker_data =
935 (FrameWorkerData *)worker->data1;
936 RefCntBuffer *const frame_bufs =
937 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
938 if (frame_worker_data->pbi->common.frame_to_show == NULL)
939 return VPX_CODEC_ERROR;
940 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
943 return VPX_CODEC_ERROR;
947 return VPX_CODEC_INVALID_PARAM;
950 static vpx_codec_err_t ctrl_get_frame_size(vpx_codec_alg_priv_t *ctx,
952 int *const frame_size = va_arg(args, int *);
954 // Only support this function in serial decode.
955 if (ctx->frame_parallel_decode) {
956 set_error_detail(ctx, "Not supported in frame parallel decode");
957 return VPX_CODEC_INCAPABLE;
961 if (ctx->frame_workers) {
962 VP9Worker *const worker = ctx->frame_workers;
963 FrameWorkerData *const frame_worker_data =
964 (FrameWorkerData *)worker->data1;
965 const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
966 frame_size[0] = cm->width;
967 frame_size[1] = cm->height;
970 return VPX_CODEC_ERROR;
974 return VPX_CODEC_INVALID_PARAM;
977 static vpx_codec_err_t ctrl_get_display_size(vpx_codec_alg_priv_t *ctx,
979 int *const display_size = va_arg(args, int *);
981 // Only support this function in serial decode.
982 if (ctx->frame_parallel_decode) {
983 set_error_detail(ctx, "Not supported in frame parallel decode");
984 return VPX_CODEC_INCAPABLE;
988 if (ctx->frame_workers) {
989 VP9Worker *const worker = ctx->frame_workers;
990 FrameWorkerData *const frame_worker_data =
991 (FrameWorkerData *)worker->data1;
992 const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
993 display_size[0] = cm->display_width;
994 display_size[1] = cm->display_height;
997 return VPX_CODEC_ERROR;
1001 return VPX_CODEC_INVALID_PARAM;
1004 static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
1006 unsigned int *const bit_depth = va_arg(args, unsigned int *);
1007 VP9Worker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
1011 FrameWorkerData *const frame_worker_data =
1012 (FrameWorkerData *)worker->data1;
1013 const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
1014 *bit_depth = cm->bit_depth;
1015 return VPX_CODEC_OK;
1017 return VPX_CODEC_ERROR;
1021 return VPX_CODEC_INVALID_PARAM;
1024 static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
1026 ctx->invert_tile_order = va_arg(args, int);
1027 return VPX_CODEC_OK;
1030 static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
1032 vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
1033 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
1034 ctx->decrypt_state = init ? init->decrypt_state : NULL;
1035 return VPX_CODEC_OK;
1038 static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
1040 const int legacy_byte_alignment = 0;
1041 const int min_byte_alignment = 32;
1042 const int max_byte_alignment = 1024;
1043 const int byte_alignment = va_arg(args, int);
1045 if (byte_alignment != legacy_byte_alignment &&
1046 (byte_alignment < min_byte_alignment ||
1047 byte_alignment > max_byte_alignment ||
1048 (byte_alignment & (byte_alignment - 1)) != 0))
1049 return VPX_CODEC_INVALID_PARAM;
1051 ctx->byte_alignment = byte_alignment;
1052 if (ctx->frame_workers) {
1053 VP9Worker *const worker = ctx->frame_workers;
1054 FrameWorkerData *const frame_worker_data =
1055 (FrameWorkerData *)worker->data1;
1056 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1058 return VPX_CODEC_OK;
1061 static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
1062 {VP8_COPY_REFERENCE, ctrl_copy_reference},
1065 {VP8_SET_REFERENCE, ctrl_set_reference},
1066 {VP8_SET_POSTPROC, ctrl_set_postproc},
1067 {VP8_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options},
1068 {VP8_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options},
1069 {VP8_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options},
1070 {VP8_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options},
1071 {VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order},
1072 {VPXD_SET_DECRYPTOR, ctrl_set_decryptor},
1073 {VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment},
1076 {VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates},
1077 {VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted},
1078 {VP9_GET_REFERENCE, ctrl_get_reference},
1079 {VP9D_GET_DISPLAY_SIZE, ctrl_get_display_size},
1080 {VP9D_GET_BIT_DEPTH, ctrl_get_bit_depth},
1081 {VP9D_GET_FRAME_SIZE, ctrl_get_frame_size},
1086 #ifndef VERSION_STRING
1087 #define VERSION_STRING
1089 CODEC_INTERFACE(vpx_codec_vp9_dx) = {
1090 "WebM Project VP9 Decoder" VERSION_STRING,
1091 VPX_CODEC_INTERNAL_ABI_VERSION,
1092 VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
1093 VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // vpx_codec_caps_t
1094 decoder_init, // vpx_codec_init_fn_t
1095 decoder_destroy, // vpx_codec_destroy_fn_t
1096 decoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
1098 decoder_peek_si, // vpx_codec_peek_si_fn_t
1099 decoder_get_si, // vpx_codec_get_si_fn_t
1100 decoder_decode, // vpx_codec_decode_fn_t
1101 decoder_get_frame, // vpx_codec_frame_get_fn_t
1102 decoder_set_fb_fn, // vpx_codec_set_fb_fn_t
1106 NULL, // vpx_codec_enc_cfg_map_t
1107 NULL, // vpx_codec_encode_fn_t
1108 NULL, // vpx_codec_get_cx_data_fn_t
1109 NULL, // vpx_codec_enc_config_set_fn_t
1110 NULL, // vpx_codec_get_global_headers_fn_t
1111 NULL, // vpx_codec_get_preview_frame_fn_t
1112 NULL // vpx_codec_enc_mr_get_mem_loc_fn_t