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"
20 #include "vpx_dsp/bitreader_buffer.h"
21 #include "vpx_util/vpx_thread.h"
23 #include "vp9/common/vp9_alloccommon.h"
24 #include "vp9/common/vp9_frame_buffers.h"
26 #include "vp9/decoder/vp9_decoder.h"
27 #include "vp9/decoder/vp9_decodeframe.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.
60 // Frame parallel related.
61 int frame_parallel_decode; // frame-based threading.
62 VPxWorker *frame_workers;
63 int num_frame_workers;
64 int next_submit_worker_id;
65 int last_submit_worker_id;
66 int next_output_worker_id;
67 int available_threads;
68 cache_frame frame_cache[FRAME_CACHE_SIZE];
69 int frame_cache_write;
72 int need_resync; // wait for key/intra-only frame
73 // BufferPool that holds all reference frames. Shared by all the FrameWorkers.
74 BufferPool *buffer_pool;
76 // External frame buffer info to save for VP9 common.
77 void *ext_priv; // Private data associated with the external frame buffers.
78 vpx_get_frame_buffer_cb_fn_t get_ext_fb_cb;
79 vpx_release_frame_buffer_cb_fn_t release_ext_fb_cb;
82 static vpx_codec_err_t decoder_init(vpx_codec_ctx_t *ctx,
83 vpx_codec_priv_enc_mr_cfg_t *data) {
84 // This function only allocates space for the vpx_codec_alg_priv_t
85 // structure. More memory may be required at the time the stream
86 // information becomes known.
90 vpx_codec_alg_priv_t *const priv = vpx_calloc(1, sizeof(*priv));
92 return VPX_CODEC_MEM_ERROR;
94 ctx->priv = (vpx_codec_priv_t *)priv;
95 ctx->priv->init_flags = ctx->init_flags;
96 priv->si.sz = sizeof(priv->si);
98 // Only do frame parallel decode when threads > 1.
99 priv->frame_parallel_decode =
100 (ctx->config.dec && (ctx->config.dec->threads > 1) &&
101 (ctx->init_flags & VPX_CODEC_USE_FRAME_THREADING)) ? 1 : 0;
102 if (ctx->config.dec) {
103 priv->cfg = *ctx->config.dec;
104 ctx->config.dec = &priv->cfg;
111 static vpx_codec_err_t decoder_destroy(vpx_codec_alg_priv_t *ctx) {
112 if (ctx->frame_workers != NULL) {
114 for (i = 0; i < ctx->num_frame_workers; ++i) {
115 VPxWorker *const worker = &ctx->frame_workers[i];
116 FrameWorkerData *const frame_worker_data =
117 (FrameWorkerData *)worker->data1;
118 vpx_get_worker_interface()->end(worker);
119 vp9_remove_common(&frame_worker_data->pbi->common);
120 #if CONFIG_VP9_POSTPROC
121 vp9_free_postproc_buffers(&frame_worker_data->pbi->common);
123 vp9_decoder_remove(frame_worker_data->pbi);
124 vpx_free(frame_worker_data->scratch_buffer);
125 #if CONFIG_MULTITHREAD
126 pthread_mutex_destroy(&frame_worker_data->stats_mutex);
127 pthread_cond_destroy(&frame_worker_data->stats_cond);
129 vpx_free(frame_worker_data);
131 #if CONFIG_MULTITHREAD
132 pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
136 if (ctx->buffer_pool) {
137 vp9_free_ref_frame_buffers(ctx->buffer_pool);
138 vp9_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
141 vpx_free(ctx->frame_workers);
142 vpx_free(ctx->buffer_pool);
147 static int parse_bitdepth_colorspace_sampling(
148 BITSTREAM_PROFILE profile, struct vpx_read_bit_buffer *rb) {
149 vpx_color_space_t color_space;
150 if (profile >= PROFILE_2)
151 rb->bit_offset += 1; // Bit-depth 10 or 12.
152 color_space = (vpx_color_space_t)vp9_rb_read_literal(rb, 3);
153 if (color_space != VPX_CS_SRGB) {
154 rb->bit_offset += 1; // [16,235] (including xvycc) vs [0,255] range.
155 if (profile == PROFILE_1 || profile == PROFILE_3) {
156 rb->bit_offset += 2; // subsampling x/y.
157 rb->bit_offset += 1; // unused.
160 if (profile == PROFILE_1 || profile == PROFILE_3) {
161 rb->bit_offset += 1; // unused
163 // RGB is only available in version 1.
170 static vpx_codec_err_t decoder_peek_si_internal(const uint8_t *data,
171 unsigned int data_sz,
172 vpx_codec_stream_info_t *si,
174 vpx_decrypt_cb decrypt_cb,
175 void *decrypt_state) {
176 int intra_only_flag = 0;
177 uint8_t clear_buffer[9];
179 if (data + data_sz <= data)
180 return VPX_CODEC_INVALID_PARAM;
186 data_sz = MIN(sizeof(clear_buffer), data_sz);
187 decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
194 struct vpx_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
195 const int frame_marker = vp9_rb_read_literal(&rb, 2);
196 const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
198 if (frame_marker != VP9_FRAME_MARKER)
199 return VPX_CODEC_UNSUP_BITSTREAM;
201 if (profile >= MAX_PROFILES)
202 return VPX_CODEC_UNSUP_BITSTREAM;
204 if ((profile >= 2 && data_sz <= 1) || data_sz < 1)
205 return VPX_CODEC_UNSUP_BITSTREAM;
207 if (vp9_rb_read_bit(&rb)) { // show an existing frame
208 vp9_rb_read_literal(&rb, 3); // Frame buffer to show.
213 return VPX_CODEC_UNSUP_BITSTREAM;
215 si->is_kf = !vp9_rb_read_bit(&rb);
216 show_frame = vp9_rb_read_bit(&rb);
217 error_resilient = vp9_rb_read_bit(&rb);
220 if (!vp9_read_sync_code(&rb))
221 return VPX_CODEC_UNSUP_BITSTREAM;
223 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
224 return VPX_CODEC_UNSUP_BITSTREAM;
225 vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
227 intra_only_flag = show_frame ? 0 : vp9_rb_read_bit(&rb);
229 rb.bit_offset += error_resilient ? 0 : 2; // reset_frame_context
231 if (intra_only_flag) {
232 if (!vp9_read_sync_code(&rb))
233 return VPX_CODEC_UNSUP_BITSTREAM;
234 if (profile > PROFILE_0) {
235 if (!parse_bitdepth_colorspace_sampling(profile, &rb))
236 return VPX_CODEC_UNSUP_BITSTREAM;
238 rb.bit_offset += REF_FRAMES; // refresh_frame_flags
239 vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
243 if (is_intra_only != NULL)
244 *is_intra_only = intra_only_flag;
248 static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
249 unsigned int data_sz,
250 vpx_codec_stream_info_t *si) {
251 return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
254 static vpx_codec_err_t decoder_get_si(vpx_codec_alg_priv_t *ctx,
255 vpx_codec_stream_info_t *si) {
256 const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
257 ? sizeof(vp9_stream_info_t)
258 : sizeof(vpx_codec_stream_info_t);
259 memcpy(si, &ctx->si, sz);
260 si->sz = (unsigned int)sz;
265 static void set_error_detail(vpx_codec_alg_priv_t *ctx,
266 const char *const error) {
267 ctx->base.err_detail = error;
270 static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
271 const struct vpx_internal_error_info *error) {
272 if (error->error_code)
273 set_error_detail(ctx, error->has_detail ? error->detail : NULL);
275 return error->error_code;
278 static void init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
281 for (i = 0; i < ctx->num_frame_workers; ++i) {
282 VPxWorker *const worker = &ctx->frame_workers[i];
283 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
284 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
285 BufferPool *const pool = cm->buffer_pool;
287 cm->new_fb_idx = INVALID_IDX;
288 cm->byte_alignment = ctx->byte_alignment;
289 cm->skip_loop_filter = ctx->skip_loop_filter;
291 if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
292 pool->get_fb_cb = ctx->get_ext_fb_cb;
293 pool->release_fb_cb = ctx->release_ext_fb_cb;
294 pool->cb_priv = ctx->ext_priv;
296 pool->get_fb_cb = vp9_get_frame_buffer;
297 pool->release_fb_cb = vp9_release_frame_buffer;
299 if (vp9_alloc_internal_frame_buffers(&pool->int_frame_buffers))
300 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
301 "Failed to initialize internal frame buffers");
303 pool->cb_priv = &pool->int_frame_buffers;
308 static void set_default_ppflags(vp8_postproc_cfg_t *cfg) {
309 cfg->post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
310 cfg->deblocking_level = 4;
311 cfg->noise_level = 0;
314 static void set_ppflags(const vpx_codec_alg_priv_t *ctx,
315 vp9_ppflags_t *flags) {
316 flags->post_proc_flag =
317 ctx->postproc_cfg.post_proc_flag;
319 flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
320 flags->noise_level = ctx->postproc_cfg.noise_level;
323 static int frame_worker_hook(void *arg1, void *arg2) {
324 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
325 const uint8_t *data = frame_worker_data->data;
328 frame_worker_data->result =
329 vp9_receive_compressed_data(frame_worker_data->pbi,
330 frame_worker_data->data_size,
332 frame_worker_data->data_end = data;
334 if (frame_worker_data->pbi->frame_parallel_decode) {
335 // In frame parallel decoding, a worker thread must successfully decode all
336 // the compressed data.
337 if (frame_worker_data->result != 0 ||
338 frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
339 VPxWorker *const worker = frame_worker_data->pbi->frame_worker_owner;
340 BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
341 // Signal all the other threads that are waiting for this frame.
342 vp9_frameworker_lock_stats(worker);
343 frame_worker_data->frame_context_ready = 1;
344 lock_buffer_pool(pool);
345 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
346 unlock_buffer_pool(pool);
347 frame_worker_data->pbi->need_resync = 1;
348 vp9_frameworker_signal_stats(worker);
349 vp9_frameworker_unlock_stats(worker);
352 } else if (frame_worker_data->result != 0) {
353 // Check decode result in serial decode.
354 frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
355 frame_worker_data->pbi->need_resync = 1;
357 return !frame_worker_data->result;
360 static vpx_codec_err_t init_decoder(vpx_codec_alg_priv_t *ctx) {
362 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
364 ctx->last_show_frame = -1;
365 ctx->next_submit_worker_id = 0;
366 ctx->last_submit_worker_id = 0;
367 ctx->next_output_worker_id = 0;
368 ctx->frame_cache_read = 0;
369 ctx->frame_cache_write = 0;
370 ctx->num_cache_frames = 0;
371 ctx->need_resync = 1;
372 ctx->num_frame_workers =
373 (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads: 1;
374 if (ctx->num_frame_workers > MAX_DECODE_THREADS)
375 ctx->num_frame_workers = MAX_DECODE_THREADS;
376 ctx->available_threads = ctx->num_frame_workers;
379 ctx->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
380 if (ctx->buffer_pool == NULL)
381 return VPX_CODEC_MEM_ERROR;
383 #if CONFIG_MULTITHREAD
384 if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
385 set_error_detail(ctx, "Failed to allocate buffer pool mutex");
386 return VPX_CODEC_MEM_ERROR;
390 ctx->frame_workers = (VPxWorker *)
391 vpx_malloc(ctx->num_frame_workers * sizeof(*ctx->frame_workers));
392 if (ctx->frame_workers == NULL) {
393 set_error_detail(ctx, "Failed to allocate frame_workers");
394 return VPX_CODEC_MEM_ERROR;
397 for (i = 0; i < ctx->num_frame_workers; ++i) {
398 VPxWorker *const worker = &ctx->frame_workers[i];
399 FrameWorkerData *frame_worker_data = NULL;
400 winterface->init(worker);
401 worker->data1 = vpx_memalign(32, sizeof(FrameWorkerData));
402 if (worker->data1 == NULL) {
403 set_error_detail(ctx, "Failed to allocate frame_worker_data");
404 return VPX_CODEC_MEM_ERROR;
406 frame_worker_data = (FrameWorkerData *)worker->data1;
407 frame_worker_data->pbi = vp9_decoder_create(ctx->buffer_pool);
408 if (frame_worker_data->pbi == NULL) {
409 set_error_detail(ctx, "Failed to allocate frame_worker_data");
410 return VPX_CODEC_MEM_ERROR;
412 frame_worker_data->pbi->frame_worker_owner = worker;
413 frame_worker_data->worker_id = i;
414 frame_worker_data->scratch_buffer = NULL;
415 frame_worker_data->scratch_buffer_size = 0;
416 frame_worker_data->frame_context_ready = 0;
417 frame_worker_data->received_frame = 0;
418 #if CONFIG_MULTITHREAD
419 if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
420 set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
421 return VPX_CODEC_MEM_ERROR;
424 if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
425 set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
426 return VPX_CODEC_MEM_ERROR;
429 // If decoding in serial mode, FrameWorker thread could create tile worker
430 // thread or loopfilter thread.
431 frame_worker_data->pbi->max_threads =
432 (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
434 frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
435 frame_worker_data->pbi->frame_parallel_decode = ctx->frame_parallel_decode;
436 frame_worker_data->pbi->common.frame_parallel_decode =
437 ctx->frame_parallel_decode;
438 worker->hook = (VPxWorkerHook)frame_worker_hook;
439 if (!winterface->reset(worker)) {
440 set_error_detail(ctx, "Frame Worker thread creation failed");
441 return VPX_CODEC_MEM_ERROR;
445 // If postprocessing was enabled by the application and a
446 // configuration has not been provided, default it.
447 if (!ctx->postproc_cfg_set &&
448 (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
449 set_default_ppflags(&ctx->postproc_cfg);
451 init_buffer_callbacks(ctx);
456 static INLINE void check_resync(vpx_codec_alg_priv_t *const ctx,
457 const VP9Decoder *const pbi) {
458 // Clear resync flag if worker got a key frame or intra only frame.
459 if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
460 (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
461 ctx->need_resync = 0;
464 static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
465 const uint8_t **data, unsigned int data_sz,
466 void *user_priv, int64_t deadline) {
467 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
470 // Determine the stream parameters. Note that we rely on peek_si to
471 // validate that we have a buffer that does not wrap around the top
474 int is_intra_only = 0;
475 const vpx_codec_err_t res =
476 decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
477 ctx->decrypt_cb, ctx->decrypt_state);
478 if (res != VPX_CODEC_OK)
481 if (!ctx->si.is_kf && !is_intra_only)
482 return VPX_CODEC_ERROR;
485 if (!ctx->frame_parallel_decode) {
486 VPxWorker *const worker = ctx->frame_workers;
487 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
488 frame_worker_data->data = *data;
489 frame_worker_data->data_size = data_sz;
490 frame_worker_data->user_priv = user_priv;
491 frame_worker_data->received_frame = 1;
493 // Set these even if already initialized. The caller may have changed the
494 // decrypt config between frames.
495 frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
496 frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;
498 worker->had_error = 0;
499 winterface->execute(worker);
501 // Update data pointer after decode.
502 *data = frame_worker_data->data_end;
504 if (worker->had_error)
505 return update_error_state(ctx, &frame_worker_data->pbi->common.error);
507 check_resync(ctx, frame_worker_data->pbi);
509 VPxWorker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
510 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
511 // Copy context from last worker thread to next worker thread.
512 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
513 vp9_frameworker_copy_context(
514 &ctx->frame_workers[ctx->next_submit_worker_id],
515 &ctx->frame_workers[ctx->last_submit_worker_id]);
517 frame_worker_data->pbi->ready_for_new_data = 0;
518 // Copy the compressed data into worker's internal buffer.
519 // TODO(hkuang): Will all the workers allocate the same size
520 // as the size of the first intra frame be better? This will
521 // avoid too many deallocate and allocate.
522 if (frame_worker_data->scratch_buffer_size < data_sz) {
523 frame_worker_data->scratch_buffer =
524 (uint8_t *)vpx_realloc(frame_worker_data->scratch_buffer, data_sz);
525 if (frame_worker_data->scratch_buffer == NULL) {
526 set_error_detail(ctx, "Failed to reallocate scratch buffer");
527 return VPX_CODEC_MEM_ERROR;
529 frame_worker_data->scratch_buffer_size = data_sz;
531 frame_worker_data->data_size = data_sz;
532 memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
534 frame_worker_data->frame_decoded = 0;
535 frame_worker_data->frame_context_ready = 0;
536 frame_worker_data->received_frame = 1;
537 frame_worker_data->data = frame_worker_data->scratch_buffer;
538 frame_worker_data->user_priv = user_priv;
540 if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
541 ctx->last_submit_worker_id =
542 (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
544 ctx->next_submit_worker_id =
545 (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
546 --ctx->available_threads;
547 worker->had_error = 0;
548 winterface->launch(worker);
554 static void wait_worker_and_cache_frame(vpx_codec_alg_priv_t *ctx) {
555 YV12_BUFFER_CONFIG sd;
556 vp9_ppflags_t flags = {0, 0, 0};
557 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
558 VPxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
559 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
560 ctx->next_output_worker_id =
561 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
562 // TODO(hkuang): Add worker error handling here.
563 winterface->sync(worker);
564 frame_worker_data->received_frame = 0;
565 ++ctx->available_threads;
567 check_resync(ctx, frame_worker_data->pbi);
569 if (vp9_get_raw_frame(frame_worker_data->pbi, &sd, &flags) == 0) {
570 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
571 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
572 ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
573 yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
574 frame_worker_data->user_priv);
575 ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
576 frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
577 ctx->frame_cache_write =
578 (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
579 ++ctx->num_cache_frames;
583 static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
584 const uint8_t *data, unsigned int data_sz,
585 void *user_priv, long deadline) {
586 const uint8_t *data_start = data;
587 const uint8_t * const data_end = data + data_sz;
589 uint32_t frame_sizes[8];
592 if (data == NULL && data_sz == 0) {
597 // Reset flushed when receiving a valid frame.
600 // Initialize the decoder workers on the first frame.
601 if (ctx->frame_workers == NULL) {
602 const vpx_codec_err_t res = init_decoder(ctx);
603 if (res != VPX_CODEC_OK)
607 res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
608 ctx->decrypt_cb, ctx->decrypt_state);
609 if (res != VPX_CODEC_OK)
612 if (ctx->frame_parallel_decode) {
613 // Decode in frame parallel mode. When decoding in this mode, the frame
614 // passed to the decoder must be either a normal frame or a superframe with
615 // superframe index so the decoder could get each frame's start position
616 // in the superframe.
617 if (frame_count > 0) {
620 for (i = 0; i < frame_count; ++i) {
621 const uint8_t *data_start_copy = data_start;
622 const uint32_t frame_size = frame_sizes[i];
623 if (data_start < data
624 || frame_size > (uint32_t) (data_end - data_start)) {
625 set_error_detail(ctx, "Invalid frame size in index");
626 return VPX_CODEC_CORRUPT_FRAME;
629 if (ctx->available_threads == 0) {
630 // No more threads for decoding. Wait until the next output worker
631 // finishes decoding. Then copy the decoded frame into cache.
632 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
633 wait_worker_and_cache_frame(ctx);
635 // TODO(hkuang): Add unit test to test this path.
636 set_error_detail(ctx, "Frame output cache is full.");
637 return VPX_CODEC_ERROR;
641 res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
643 if (res != VPX_CODEC_OK)
645 data_start += frame_size;
648 if (ctx->available_threads == 0) {
649 // No more threads for decoding. Wait until the next output worker
650 // finishes decoding. Then copy the decoded frame into cache.
651 if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
652 wait_worker_and_cache_frame(ctx);
654 // TODO(hkuang): Add unit test to test this path.
655 set_error_detail(ctx, "Frame output cache is full.");
656 return VPX_CODEC_ERROR;
660 res = decode_one(ctx, &data, data_sz, user_priv, deadline);
661 if (res != VPX_CODEC_OK)
665 // Decode in serial mode.
666 if (frame_count > 0) {
669 for (i = 0; i < frame_count; ++i) {
670 const uint8_t *data_start_copy = data_start;
671 const uint32_t frame_size = frame_sizes[i];
673 if (data_start < data
674 || frame_size > (uint32_t) (data_end - data_start)) {
675 set_error_detail(ctx, "Invalid frame size in index");
676 return VPX_CODEC_CORRUPT_FRAME;
679 res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
681 if (res != VPX_CODEC_OK)
684 data_start += frame_size;
687 while (data_start < data_end) {
688 const uint32_t frame_size = (uint32_t) (data_end - data_start);
689 const vpx_codec_err_t res = decode_one(ctx, &data_start, frame_size,
690 user_priv, deadline);
691 if (res != VPX_CODEC_OK)
694 // Account for suboptimal termination by the encoder.
695 while (data_start < data_end) {
696 const uint8_t marker = read_marker(ctx->decrypt_cb,
697 ctx->decrypt_state, data_start);
709 static void release_last_output_frame(vpx_codec_alg_priv_t *ctx) {
710 RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
711 // Decrease reference count of last output frame in frame parallel mode.
712 if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
713 BufferPool *const pool = ctx->buffer_pool;
714 lock_buffer_pool(pool);
715 decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
716 unlock_buffer_pool(pool);
720 static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
721 vpx_codec_iter_t *iter) {
722 vpx_image_t *img = NULL;
724 // Only return frame when all the cpu are busy or
725 // application fluhsed the decoder in frame parallel decode.
726 if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
731 // Output the frames in the cache first.
732 if (ctx->num_cache_frames > 0) {
733 release_last_output_frame(ctx);
734 ctx->last_show_frame = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
735 if (ctx->need_resync)
737 img = &ctx->frame_cache[ctx->frame_cache_read].img;
738 ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
739 --ctx->num_cache_frames;
743 // iter acts as a flip flop, so an image is only returned on the first
744 // call to get_frame.
745 if (*iter == NULL && ctx->frame_workers != NULL) {
747 YV12_BUFFER_CONFIG sd;
748 vp9_ppflags_t flags = {0, 0, 0};
749 const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
750 VPxWorker *const worker =
751 &ctx->frame_workers[ctx->next_output_worker_id];
752 FrameWorkerData *const frame_worker_data =
753 (FrameWorkerData *)worker->data1;
754 ctx->next_output_worker_id =
755 (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
756 if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
757 set_ppflags(ctx, &flags);
758 // Wait for the frame from worker thread.
759 if (winterface->sync(worker)) {
760 // Check if worker has received any frames.
761 if (frame_worker_data->received_frame == 1) {
762 ++ctx->available_threads;
763 frame_worker_data->received_frame = 0;
764 check_resync(ctx, frame_worker_data->pbi);
766 if (vp9_get_raw_frame(frame_worker_data->pbi, &sd, &flags) == 0) {
767 VP9_COMMON *const cm = &frame_worker_data->pbi->common;
768 RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
769 release_last_output_frame(ctx);
770 ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
771 if (ctx->need_resync)
773 yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
774 ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
779 // Decoding failed. Release the worker thread.
780 frame_worker_data->received_frame = 0;
781 ++ctx->available_threads;
782 ctx->need_resync = 1;
783 if (ctx->flushed != 1)
786 } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
791 static vpx_codec_err_t decoder_set_fb_fn(
792 vpx_codec_alg_priv_t *ctx,
793 vpx_get_frame_buffer_cb_fn_t cb_get,
794 vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
795 if (cb_get == NULL || cb_release == NULL) {
796 return VPX_CODEC_INVALID_PARAM;
797 } else if (ctx->frame_workers == NULL) {
798 // If the decoder has already been initialized, do not accept changes to
799 // the frame buffer functions.
800 ctx->get_ext_fb_cb = cb_get;
801 ctx->release_ext_fb_cb = cb_release;
802 ctx->ext_priv = cb_priv;
806 return VPX_CODEC_ERROR;
809 static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
811 vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
813 // Only support this function in serial decode.
814 if (ctx->frame_parallel_decode) {
815 set_error_detail(ctx, "Not supported in frame parallel decode");
816 return VPX_CODEC_INCAPABLE;
820 vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
821 YV12_BUFFER_CONFIG sd;
822 VPxWorker *const worker = ctx->frame_workers;
823 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
824 image2yuvconfig(&frame->img, &sd);
825 return vp9_set_reference_dec(&frame_worker_data->pbi->common,
826 (VP9_REFFRAME)frame->frame_type, &sd);
828 return VPX_CODEC_INVALID_PARAM;
832 static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
834 vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
836 // Only support this function in serial decode.
837 if (ctx->frame_parallel_decode) {
838 set_error_detail(ctx, "Not supported in frame parallel decode");
839 return VPX_CODEC_INCAPABLE;
843 vpx_ref_frame_t *frame = (vpx_ref_frame_t *) data;
844 YV12_BUFFER_CONFIG sd;
845 VPxWorker *const worker = ctx->frame_workers;
846 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
847 image2yuvconfig(&frame->img, &sd);
848 return vp9_copy_reference_dec(frame_worker_data->pbi,
849 (VP9_REFFRAME)frame->frame_type, &sd);
851 return VPX_CODEC_INVALID_PARAM;
855 static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
857 vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
859 // Only support this function in serial decode.
860 if (ctx->frame_parallel_decode) {
861 set_error_detail(ctx, "Not supported in frame parallel decode");
862 return VPX_CODEC_INCAPABLE;
866 YV12_BUFFER_CONFIG* fb;
867 VPxWorker *const worker = ctx->frame_workers;
868 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
869 fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
870 if (fb == NULL) return VPX_CODEC_ERROR;
871 yuvconfig2image(&data->img, fb, NULL);
874 return VPX_CODEC_INVALID_PARAM;
878 static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
880 #if CONFIG_VP9_POSTPROC
881 vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
884 ctx->postproc_cfg_set = 1;
885 ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
888 return VPX_CODEC_INVALID_PARAM;
893 return VPX_CODEC_INCAPABLE;
897 static vpx_codec_err_t ctrl_set_dbg_options(vpx_codec_alg_priv_t *ctx,
901 return VPX_CODEC_INCAPABLE;
904 static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
906 int *const update_info = va_arg(args, int *);
908 // Only support this function in serial decode.
909 if (ctx->frame_parallel_decode) {
910 set_error_detail(ctx, "Not supported in frame parallel decode");
911 return VPX_CODEC_INCAPABLE;
915 if (ctx->frame_workers) {
916 VPxWorker *const worker = ctx->frame_workers;
917 FrameWorkerData *const frame_worker_data =
918 (FrameWorkerData *)worker->data1;
919 *update_info = frame_worker_data->pbi->refresh_frame_flags;
922 return VPX_CODEC_ERROR;
926 return VPX_CODEC_INVALID_PARAM;
929 static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
931 int *corrupted = va_arg(args, int *);
934 if (ctx->frame_workers) {
935 VPxWorker *const worker = ctx->frame_workers;
936 FrameWorkerData *const frame_worker_data =
937 (FrameWorkerData *)worker->data1;
938 RefCntBuffer *const frame_bufs =
939 frame_worker_data->pbi->common.buffer_pool->frame_bufs;
940 if (frame_worker_data->pbi->common.frame_to_show == NULL)
941 return VPX_CODEC_ERROR;
942 if (ctx->last_show_frame >= 0)
943 *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
946 return VPX_CODEC_ERROR;
950 return VPX_CODEC_INVALID_PARAM;
953 static vpx_codec_err_t ctrl_get_frame_size(vpx_codec_alg_priv_t *ctx,
955 int *const frame_size = va_arg(args, int *);
957 // Only support this function in serial decode.
958 if (ctx->frame_parallel_decode) {
959 set_error_detail(ctx, "Not supported in frame parallel decode");
960 return VPX_CODEC_INCAPABLE;
964 if (ctx->frame_workers) {
965 VPxWorker *const worker = ctx->frame_workers;
966 FrameWorkerData *const frame_worker_data =
967 (FrameWorkerData *)worker->data1;
968 const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
969 frame_size[0] = cm->width;
970 frame_size[1] = cm->height;
973 return VPX_CODEC_ERROR;
977 return VPX_CODEC_INVALID_PARAM;
980 static vpx_codec_err_t ctrl_get_display_size(vpx_codec_alg_priv_t *ctx,
982 int *const display_size = va_arg(args, int *);
984 // Only support this function in serial decode.
985 if (ctx->frame_parallel_decode) {
986 set_error_detail(ctx, "Not supported in frame parallel decode");
987 return VPX_CODEC_INCAPABLE;
991 if (ctx->frame_workers) {
992 VPxWorker *const worker = ctx->frame_workers;
993 FrameWorkerData *const frame_worker_data =
994 (FrameWorkerData *)worker->data1;
995 const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
996 display_size[0] = cm->display_width;
997 display_size[1] = cm->display_height;
1000 return VPX_CODEC_ERROR;
1004 return VPX_CODEC_INVALID_PARAM;
1007 static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
1009 unsigned int *const bit_depth = va_arg(args, unsigned int *);
1010 VPxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
1014 FrameWorkerData *const frame_worker_data =
1015 (FrameWorkerData *)worker->data1;
1016 const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
1017 *bit_depth = cm->bit_depth;
1018 return VPX_CODEC_OK;
1020 return VPX_CODEC_ERROR;
1024 return VPX_CODEC_INVALID_PARAM;
1027 static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
1029 ctx->invert_tile_order = va_arg(args, int);
1030 return VPX_CODEC_OK;
1033 static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
1035 vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
1036 ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
1037 ctx->decrypt_state = init ? init->decrypt_state : NULL;
1038 return VPX_CODEC_OK;
1041 static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
1043 const int legacy_byte_alignment = 0;
1044 const int min_byte_alignment = 32;
1045 const int max_byte_alignment = 1024;
1046 const int byte_alignment = va_arg(args, int);
1048 if (byte_alignment != legacy_byte_alignment &&
1049 (byte_alignment < min_byte_alignment ||
1050 byte_alignment > max_byte_alignment ||
1051 (byte_alignment & (byte_alignment - 1)) != 0))
1052 return VPX_CODEC_INVALID_PARAM;
1054 ctx->byte_alignment = byte_alignment;
1055 if (ctx->frame_workers) {
1056 VPxWorker *const worker = ctx->frame_workers;
1057 FrameWorkerData *const frame_worker_data =
1058 (FrameWorkerData *)worker->data1;
1059 frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1061 return VPX_CODEC_OK;
1064 static vpx_codec_err_t ctrl_set_skip_loop_filter(vpx_codec_alg_priv_t *ctx,
1066 ctx->skip_loop_filter = va_arg(args, int);
1068 if (ctx->frame_workers) {
1069 VPxWorker *const worker = ctx->frame_workers;
1070 FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1071 frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
1074 return VPX_CODEC_OK;
1077 static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
1078 {VP8_COPY_REFERENCE, ctrl_copy_reference},
1081 {VP8_SET_REFERENCE, ctrl_set_reference},
1082 {VP8_SET_POSTPROC, ctrl_set_postproc},
1083 {VP8_SET_DBG_COLOR_REF_FRAME, ctrl_set_dbg_options},
1084 {VP8_SET_DBG_COLOR_MB_MODES, ctrl_set_dbg_options},
1085 {VP8_SET_DBG_COLOR_B_MODES, ctrl_set_dbg_options},
1086 {VP8_SET_DBG_DISPLAY_MV, ctrl_set_dbg_options},
1087 {VP9_INVERT_TILE_DECODE_ORDER, ctrl_set_invert_tile_order},
1088 {VPXD_SET_DECRYPTOR, ctrl_set_decryptor},
1089 {VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment},
1090 {VP9_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter},
1093 {VP8D_GET_LAST_REF_UPDATES, ctrl_get_last_ref_updates},
1094 {VP8D_GET_FRAME_CORRUPTED, ctrl_get_frame_corrupted},
1095 {VP9_GET_REFERENCE, ctrl_get_reference},
1096 {VP9D_GET_DISPLAY_SIZE, ctrl_get_display_size},
1097 {VP9D_GET_BIT_DEPTH, ctrl_get_bit_depth},
1098 {VP9D_GET_FRAME_SIZE, ctrl_get_frame_size},
1103 #ifndef VERSION_STRING
1104 #define VERSION_STRING
1106 CODEC_INTERFACE(vpx_codec_vp9_dx) = {
1107 "WebM Project VP9 Decoder" VERSION_STRING,
1108 VPX_CODEC_INTERNAL_ABI_VERSION,
1109 VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
1110 VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER, // vpx_codec_caps_t
1111 decoder_init, // vpx_codec_init_fn_t
1112 decoder_destroy, // vpx_codec_destroy_fn_t
1113 decoder_ctrl_maps, // vpx_codec_ctrl_fn_map_t
1115 decoder_peek_si, // vpx_codec_peek_si_fn_t
1116 decoder_get_si, // vpx_codec_get_si_fn_t
1117 decoder_decode, // vpx_codec_decode_fn_t
1118 decoder_get_frame, // vpx_codec_frame_get_fn_t
1119 decoder_set_fb_fn, // vpx_codec_set_fb_fn_t
1123 NULL, // vpx_codec_enc_cfg_map_t
1124 NULL, // vpx_codec_encode_fn_t
1125 NULL, // vpx_codec_get_cx_data_fn_t
1126 NULL, // vpx_codec_enc_config_set_fn_t
1127 NULL, // vpx_codec_get_global_headers_fn_t
1128 NULL, // vpx_codec_get_preview_frame_fn_t
1129 NULL // vpx_codec_enc_mr_get_mem_loc_fn_t