1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11#include <stdlib.h>
12#include <string.h>
13
14#include "./vpx_config.h"
15#include "./vpx_version.h"
16
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_dsp/vpx_dsp_common.h"
22#include "vpx_util/vpx_thread.h"
23
24#include "vp9/common/vp9_alloccommon.h"
25#include "vp9/common/vp9_frame_buffers.h"
26
27#include "vp9/decoder/vp9_decodeframe.h"
28
29#include "vp9/vp9_dx_iface.h"
30#include "vp9/vp9_iface_common.h"
31
32#define VP9_CAP_POSTPROC (CONFIG_VP9_POSTPROC ? VPX_CODEC_CAP_POSTPROC : 0)
33
34static vpx_codec_err_t decoder_init(vpx_codec_ctx_t *ctx,
35                                    vpx_codec_priv_enc_mr_cfg_t *data) {
36  // This function only allocates space for the vpx_codec_alg_priv_t
37  // structure. More memory may be required at the time the stream
38  // information becomes known.
39  (void)data;
40
41  if (!ctx->priv) {
42    vpx_codec_alg_priv_t *const priv =
43        (vpx_codec_alg_priv_t *)vpx_calloc(1, sizeof(*priv));
44    if (priv == NULL)
45      return VPX_CODEC_MEM_ERROR;
46
47    ctx->priv = (vpx_codec_priv_t *)priv;
48    ctx->priv->init_flags = ctx->init_flags;
49    priv->si.sz = sizeof(priv->si);
50    priv->flushed = 0;
51    // Only do frame parallel decode when threads > 1.
52    priv->frame_parallel_decode =
53        (ctx->config.dec && (ctx->config.dec->threads > 1) &&
54         (ctx->init_flags & VPX_CODEC_USE_FRAME_THREADING)) ? 1 : 0;
55    if (ctx->config.dec) {
56      priv->cfg = *ctx->config.dec;
57      ctx->config.dec = &priv->cfg;
58    }
59  }
60
61  return VPX_CODEC_OK;
62}
63
64static vpx_codec_err_t decoder_destroy(vpx_codec_alg_priv_t *ctx) {
65  if (ctx->frame_workers != NULL) {
66    int i;
67    for (i = 0; i < ctx->num_frame_workers; ++i) {
68      VPxWorker *const worker = &ctx->frame_workers[i];
69      FrameWorkerData *const frame_worker_data =
70          (FrameWorkerData *)worker->data1;
71      vpx_get_worker_interface()->end(worker);
72      vp9_remove_common(&frame_worker_data->pbi->common);
73#if CONFIG_VP9_POSTPROC
74      vp9_free_postproc_buffers(&frame_worker_data->pbi->common);
75#endif
76      vp9_decoder_remove(frame_worker_data->pbi);
77      vpx_free(frame_worker_data->scratch_buffer);
78#if CONFIG_MULTITHREAD
79      pthread_mutex_destroy(&frame_worker_data->stats_mutex);
80      pthread_cond_destroy(&frame_worker_data->stats_cond);
81#endif
82      vpx_free(frame_worker_data);
83    }
84#if CONFIG_MULTITHREAD
85    pthread_mutex_destroy(&ctx->buffer_pool->pool_mutex);
86#endif
87  }
88
89  if (ctx->buffer_pool) {
90    vp9_free_ref_frame_buffers(ctx->buffer_pool);
91    vp9_free_internal_frame_buffers(&ctx->buffer_pool->int_frame_buffers);
92  }
93
94  vpx_free(ctx->frame_workers);
95  vpx_free(ctx->buffer_pool);
96  vpx_free(ctx);
97  return VPX_CODEC_OK;
98}
99
100static int parse_bitdepth_colorspace_sampling(
101    BITSTREAM_PROFILE profile, struct vpx_read_bit_buffer *rb) {
102  vpx_color_space_t color_space;
103  if (profile >= PROFILE_2)
104    rb->bit_offset += 1;  // Bit-depth 10 or 12.
105  color_space = (vpx_color_space_t)vpx_rb_read_literal(rb, 3);
106  if (color_space != VPX_CS_SRGB) {
107    rb->bit_offset += 1;  // [16,235] (including xvycc) vs [0,255] range.
108    if (profile == PROFILE_1 || profile == PROFILE_3) {
109      rb->bit_offset += 2;  // subsampling x/y.
110      rb->bit_offset += 1;  // unused.
111    }
112  } else {
113    if (profile == PROFILE_1 || profile == PROFILE_3) {
114      rb->bit_offset += 1;  // unused
115    } else {
116      // RGB is only available in version 1.
117      return 0;
118    }
119  }
120  return 1;
121}
122
123static vpx_codec_err_t decoder_peek_si_internal(const uint8_t *data,
124                                                unsigned int data_sz,
125                                                vpx_codec_stream_info_t *si,
126                                                int *is_intra_only,
127                                                vpx_decrypt_cb decrypt_cb,
128                                                void *decrypt_state) {
129  int intra_only_flag = 0;
130  uint8_t clear_buffer[10];
131
132  if (data + data_sz <= data)
133    return VPX_CODEC_INVALID_PARAM;
134
135  si->is_kf = 0;
136  si->w = si->h = 0;
137
138  if (decrypt_cb) {
139    data_sz = VPXMIN(sizeof(clear_buffer), data_sz);
140    decrypt_cb(decrypt_state, data, clear_buffer, data_sz);
141    data = clear_buffer;
142  }
143
144  // A maximum of 6 bits are needed to read the frame marker, profile and
145  // show_existing_frame.
146  if (data_sz < 1)
147    return VPX_CODEC_UNSUP_BITSTREAM;
148
149  {
150    int show_frame;
151    int error_resilient;
152    struct vpx_read_bit_buffer rb = { data, data + data_sz, 0, NULL, NULL };
153    const int frame_marker = vpx_rb_read_literal(&rb, 2);
154    const BITSTREAM_PROFILE profile = vp9_read_profile(&rb);
155
156    if (frame_marker != VP9_FRAME_MARKER)
157      return VPX_CODEC_UNSUP_BITSTREAM;
158
159    if (profile >= MAX_PROFILES)
160      return VPX_CODEC_UNSUP_BITSTREAM;
161
162    if (vpx_rb_read_bit(&rb)) {  // show an existing frame
163      // If profile is > 2 and show_existing_frame is true, then at least 1 more
164      // byte (6+3=9 bits) is needed.
165      if (profile > 2 && data_sz < 2)
166        return VPX_CODEC_UNSUP_BITSTREAM;
167      vpx_rb_read_literal(&rb, 3);  // Frame buffer to show.
168      return VPX_CODEC_OK;
169    }
170
171    // For the rest of the function, a maximum of 9 more bytes are needed
172    // (computed by taking the maximum possible bits needed in each case). Note
173    // that this has to be updated if we read any more bits in this function.
174    if (data_sz < 10)
175      return VPX_CODEC_UNSUP_BITSTREAM;
176
177    si->is_kf = !vpx_rb_read_bit(&rb);
178    show_frame = vpx_rb_read_bit(&rb);
179    error_resilient = vpx_rb_read_bit(&rb);
180
181    if (si->is_kf) {
182      if (!vp9_read_sync_code(&rb))
183        return VPX_CODEC_UNSUP_BITSTREAM;
184
185      if (!parse_bitdepth_colorspace_sampling(profile, &rb))
186        return VPX_CODEC_UNSUP_BITSTREAM;
187      vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
188    } else {
189      intra_only_flag = show_frame ? 0 : vpx_rb_read_bit(&rb);
190
191      rb.bit_offset += error_resilient ? 0 : 2;  // reset_frame_context
192
193      if (intra_only_flag) {
194        if (!vp9_read_sync_code(&rb))
195          return VPX_CODEC_UNSUP_BITSTREAM;
196        if (profile > PROFILE_0) {
197          if (!parse_bitdepth_colorspace_sampling(profile, &rb))
198            return VPX_CODEC_UNSUP_BITSTREAM;
199        }
200        rb.bit_offset += REF_FRAMES;  // refresh_frame_flags
201        vp9_read_frame_size(&rb, (int *)&si->w, (int *)&si->h);
202      }
203    }
204  }
205  if (is_intra_only != NULL)
206    *is_intra_only = intra_only_flag;
207  return VPX_CODEC_OK;
208}
209
210static vpx_codec_err_t decoder_peek_si(const uint8_t *data,
211                                       unsigned int data_sz,
212                                       vpx_codec_stream_info_t *si) {
213  return decoder_peek_si_internal(data, data_sz, si, NULL, NULL, NULL);
214}
215
216static vpx_codec_err_t decoder_get_si(vpx_codec_alg_priv_t *ctx,
217                                      vpx_codec_stream_info_t *si) {
218  const size_t sz = (si->sz >= sizeof(vp9_stream_info_t))
219                       ? sizeof(vp9_stream_info_t)
220                       : sizeof(vpx_codec_stream_info_t);
221  memcpy(si, &ctx->si, sz);
222  si->sz = (unsigned int)sz;
223
224  return VPX_CODEC_OK;
225}
226
227static void set_error_detail(vpx_codec_alg_priv_t *ctx,
228                             const char *const error) {
229  ctx->base.err_detail = error;
230}
231
232static vpx_codec_err_t update_error_state(vpx_codec_alg_priv_t *ctx,
233                           const struct vpx_internal_error_info *error) {
234  if (error->error_code)
235    set_error_detail(ctx, error->has_detail ? error->detail : NULL);
236
237  return error->error_code;
238}
239
240static void init_buffer_callbacks(vpx_codec_alg_priv_t *ctx) {
241  int i;
242
243  for (i = 0; i < ctx->num_frame_workers; ++i) {
244    VPxWorker *const worker = &ctx->frame_workers[i];
245    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
246    VP9_COMMON *const cm = &frame_worker_data->pbi->common;
247    BufferPool *const pool = cm->buffer_pool;
248
249    cm->new_fb_idx = INVALID_IDX;
250    cm->byte_alignment = ctx->byte_alignment;
251    cm->skip_loop_filter = ctx->skip_loop_filter;
252
253    if (ctx->get_ext_fb_cb != NULL && ctx->release_ext_fb_cb != NULL) {
254      pool->get_fb_cb = ctx->get_ext_fb_cb;
255      pool->release_fb_cb = ctx->release_ext_fb_cb;
256      pool->cb_priv = ctx->ext_priv;
257    } else {
258      pool->get_fb_cb = vp9_get_frame_buffer;
259      pool->release_fb_cb = vp9_release_frame_buffer;
260
261      if (vp9_alloc_internal_frame_buffers(&pool->int_frame_buffers))
262        vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
263                           "Failed to initialize internal frame buffers");
264
265      pool->cb_priv = &pool->int_frame_buffers;
266    }
267  }
268}
269
270static void set_default_ppflags(vp8_postproc_cfg_t *cfg) {
271  cfg->post_proc_flag = VP8_DEBLOCK | VP8_DEMACROBLOCK;
272  cfg->deblocking_level = 4;
273  cfg->noise_level = 0;
274}
275
276static void set_ppflags(const vpx_codec_alg_priv_t *ctx,
277                        vp9_ppflags_t *flags) {
278  flags->post_proc_flag =
279      ctx->postproc_cfg.post_proc_flag;
280
281  flags->deblocking_level = ctx->postproc_cfg.deblocking_level;
282  flags->noise_level = ctx->postproc_cfg.noise_level;
283}
284
285static int frame_worker_hook(void *arg1, void *arg2) {
286  FrameWorkerData *const frame_worker_data = (FrameWorkerData *)arg1;
287  const uint8_t *data = frame_worker_data->data;
288  (void)arg2;
289
290  frame_worker_data->result =
291      vp9_receive_compressed_data(frame_worker_data->pbi,
292                                  frame_worker_data->data_size,
293                                  &data);
294  frame_worker_data->data_end = data;
295
296  if (frame_worker_data->pbi->frame_parallel_decode) {
297    // In frame parallel decoding, a worker thread must successfully decode all
298    // the compressed data.
299    if (frame_worker_data->result != 0 ||
300        frame_worker_data->data + frame_worker_data->data_size - 1 > data) {
301      VPxWorker *const worker = frame_worker_data->pbi->frame_worker_owner;
302      BufferPool *const pool = frame_worker_data->pbi->common.buffer_pool;
303      // Signal all the other threads that are waiting for this frame.
304      vp9_frameworker_lock_stats(worker);
305      frame_worker_data->frame_context_ready = 1;
306      lock_buffer_pool(pool);
307      frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
308      unlock_buffer_pool(pool);
309      frame_worker_data->pbi->need_resync = 1;
310      vp9_frameworker_signal_stats(worker);
311      vp9_frameworker_unlock_stats(worker);
312      return 0;
313    }
314  } else if (frame_worker_data->result != 0) {
315    // Check decode result in serial decode.
316    frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
317    frame_worker_data->pbi->need_resync = 1;
318  }
319  return !frame_worker_data->result;
320}
321
322static vpx_codec_err_t init_decoder(vpx_codec_alg_priv_t *ctx) {
323  int i;
324  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
325
326  ctx->last_show_frame = -1;
327  ctx->next_submit_worker_id = 0;
328  ctx->last_submit_worker_id = 0;
329  ctx->next_output_worker_id = 0;
330  ctx->frame_cache_read = 0;
331  ctx->frame_cache_write = 0;
332  ctx->num_cache_frames = 0;
333  ctx->need_resync = 1;
334  ctx->num_frame_workers =
335      (ctx->frame_parallel_decode == 1) ? ctx->cfg.threads: 1;
336  if (ctx->num_frame_workers > MAX_DECODE_THREADS)
337    ctx->num_frame_workers = MAX_DECODE_THREADS;
338  ctx->available_threads = ctx->num_frame_workers;
339  ctx->flushed = 0;
340
341  ctx->buffer_pool = (BufferPool *)vpx_calloc(1, sizeof(BufferPool));
342  if (ctx->buffer_pool == NULL)
343    return VPX_CODEC_MEM_ERROR;
344
345#if CONFIG_MULTITHREAD
346    if (pthread_mutex_init(&ctx->buffer_pool->pool_mutex, NULL)) {
347      set_error_detail(ctx, "Failed to allocate buffer pool mutex");
348      return VPX_CODEC_MEM_ERROR;
349    }
350#endif
351
352  ctx->frame_workers = (VPxWorker *)
353      vpx_malloc(ctx->num_frame_workers * sizeof(*ctx->frame_workers));
354  if (ctx->frame_workers == NULL) {
355    set_error_detail(ctx, "Failed to allocate frame_workers");
356    return VPX_CODEC_MEM_ERROR;
357  }
358
359  for (i = 0; i < ctx->num_frame_workers; ++i) {
360    VPxWorker *const worker = &ctx->frame_workers[i];
361    FrameWorkerData *frame_worker_data = NULL;
362    winterface->init(worker);
363    worker->data1 = vpx_memalign(32, sizeof(FrameWorkerData));
364    if (worker->data1 == NULL) {
365      set_error_detail(ctx, "Failed to allocate frame_worker_data");
366      return VPX_CODEC_MEM_ERROR;
367    }
368    frame_worker_data = (FrameWorkerData *)worker->data1;
369    frame_worker_data->pbi = vp9_decoder_create(ctx->buffer_pool);
370    if (frame_worker_data->pbi == NULL) {
371      set_error_detail(ctx, "Failed to allocate frame_worker_data");
372      return VPX_CODEC_MEM_ERROR;
373    }
374    frame_worker_data->pbi->frame_worker_owner = worker;
375    frame_worker_data->worker_id = i;
376    frame_worker_data->scratch_buffer = NULL;
377    frame_worker_data->scratch_buffer_size = 0;
378    frame_worker_data->frame_context_ready = 0;
379    frame_worker_data->received_frame = 0;
380#if CONFIG_MULTITHREAD
381    if (pthread_mutex_init(&frame_worker_data->stats_mutex, NULL)) {
382      set_error_detail(ctx, "Failed to allocate frame_worker_data mutex");
383      return VPX_CODEC_MEM_ERROR;
384    }
385
386    if (pthread_cond_init(&frame_worker_data->stats_cond, NULL)) {
387      set_error_detail(ctx, "Failed to allocate frame_worker_data cond");
388      return VPX_CODEC_MEM_ERROR;
389    }
390#endif
391    // If decoding in serial mode, FrameWorker thread could create tile worker
392    // thread or loopfilter thread.
393    frame_worker_data->pbi->max_threads =
394        (ctx->frame_parallel_decode == 0) ? ctx->cfg.threads : 0;
395
396    frame_worker_data->pbi->inv_tile_order = ctx->invert_tile_order;
397    frame_worker_data->pbi->frame_parallel_decode = ctx->frame_parallel_decode;
398    frame_worker_data->pbi->common.frame_parallel_decode =
399        ctx->frame_parallel_decode;
400    worker->hook = (VPxWorkerHook)frame_worker_hook;
401    if (!winterface->reset(worker)) {
402      set_error_detail(ctx, "Frame Worker thread creation failed");
403      return VPX_CODEC_MEM_ERROR;
404    }
405  }
406
407  // If postprocessing was enabled by the application and a
408  // configuration has not been provided, default it.
409  if (!ctx->postproc_cfg_set &&
410      (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC))
411    set_default_ppflags(&ctx->postproc_cfg);
412
413  init_buffer_callbacks(ctx);
414
415  return VPX_CODEC_OK;
416}
417
418static INLINE void check_resync(vpx_codec_alg_priv_t *const ctx,
419                                const VP9Decoder *const pbi) {
420  // Clear resync flag if worker got a key frame or intra only frame.
421  if (ctx->need_resync == 1 && pbi->need_resync == 0 &&
422      (pbi->common.intra_only || pbi->common.frame_type == KEY_FRAME))
423    ctx->need_resync = 0;
424}
425
426static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx,
427                                  const uint8_t **data, unsigned int data_sz,
428                                  void *user_priv, int64_t deadline) {
429  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
430  (void)deadline;
431
432  // Determine the stream parameters. Note that we rely on peek_si to
433  // validate that we have a buffer that does not wrap around the top
434  // of the heap.
435  if (!ctx->si.h) {
436    int is_intra_only = 0;
437    const vpx_codec_err_t res =
438        decoder_peek_si_internal(*data, data_sz, &ctx->si, &is_intra_only,
439                                 ctx->decrypt_cb, ctx->decrypt_state);
440    if (res != VPX_CODEC_OK)
441      return res;
442
443    if (!ctx->si.is_kf && !is_intra_only)
444      return VPX_CODEC_ERROR;
445  }
446
447  if (!ctx->frame_parallel_decode) {
448    VPxWorker *const worker = ctx->frame_workers;
449    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
450    frame_worker_data->data = *data;
451    frame_worker_data->data_size = data_sz;
452    frame_worker_data->user_priv = user_priv;
453    frame_worker_data->received_frame = 1;
454
455    // Set these even if already initialized.  The caller may have changed the
456    // decrypt config between frames.
457    frame_worker_data->pbi->decrypt_cb = ctx->decrypt_cb;
458    frame_worker_data->pbi->decrypt_state = ctx->decrypt_state;
459
460    worker->had_error = 0;
461    winterface->execute(worker);
462
463    // Update data pointer after decode.
464    *data = frame_worker_data->data_end;
465
466    if (worker->had_error)
467      return update_error_state(ctx, &frame_worker_data->pbi->common.error);
468
469    check_resync(ctx, frame_worker_data->pbi);
470  } else {
471    VPxWorker *const worker = &ctx->frame_workers[ctx->next_submit_worker_id];
472    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
473    // Copy context from last worker thread to next worker thread.
474    if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
475      vp9_frameworker_copy_context(
476          &ctx->frame_workers[ctx->next_submit_worker_id],
477          &ctx->frame_workers[ctx->last_submit_worker_id]);
478
479    frame_worker_data->pbi->ready_for_new_data = 0;
480    // Copy the compressed data into worker's internal buffer.
481    // TODO(hkuang): Will all the workers allocate the same size
482    // as the size of the first intra frame be better? This will
483    // avoid too many deallocate and allocate.
484    if (frame_worker_data->scratch_buffer_size < data_sz) {
485      frame_worker_data->scratch_buffer =
486          (uint8_t *)vpx_realloc(frame_worker_data->scratch_buffer, data_sz);
487      if (frame_worker_data->scratch_buffer == NULL) {
488        set_error_detail(ctx, "Failed to reallocate scratch buffer");
489        return VPX_CODEC_MEM_ERROR;
490      }
491      frame_worker_data->scratch_buffer_size = data_sz;
492    }
493    frame_worker_data->data_size = data_sz;
494    memcpy(frame_worker_data->scratch_buffer, *data, data_sz);
495
496    frame_worker_data->frame_decoded = 0;
497    frame_worker_data->frame_context_ready = 0;
498    frame_worker_data->received_frame = 1;
499    frame_worker_data->data = frame_worker_data->scratch_buffer;
500    frame_worker_data->user_priv = user_priv;
501
502    if (ctx->next_submit_worker_id != ctx->last_submit_worker_id)
503      ctx->last_submit_worker_id =
504          (ctx->last_submit_worker_id + 1) % ctx->num_frame_workers;
505
506    ctx->next_submit_worker_id =
507        (ctx->next_submit_worker_id + 1) % ctx->num_frame_workers;
508    --ctx->available_threads;
509    worker->had_error = 0;
510    winterface->launch(worker);
511  }
512
513  return VPX_CODEC_OK;
514}
515
516static void wait_worker_and_cache_frame(vpx_codec_alg_priv_t *ctx) {
517  YV12_BUFFER_CONFIG sd;
518  vp9_ppflags_t flags = {0, 0, 0};
519  const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
520  VPxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
521  FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
522  ctx->next_output_worker_id =
523      (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
524  // TODO(hkuang): Add worker error handling here.
525  winterface->sync(worker);
526  frame_worker_data->received_frame = 0;
527  ++ctx->available_threads;
528
529  check_resync(ctx, frame_worker_data->pbi);
530
531  if (vp9_get_raw_frame(frame_worker_data->pbi, &sd, &flags) == 0) {
532    VP9_COMMON *const cm = &frame_worker_data->pbi->common;
533    RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
534    ctx->frame_cache[ctx->frame_cache_write].fb_idx = cm->new_fb_idx;
535    yuvconfig2image(&ctx->frame_cache[ctx->frame_cache_write].img, &sd,
536                    frame_worker_data->user_priv);
537    ctx->frame_cache[ctx->frame_cache_write].img.fb_priv =
538        frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
539    ctx->frame_cache_write =
540        (ctx->frame_cache_write + 1) % FRAME_CACHE_SIZE;
541    ++ctx->num_cache_frames;
542  }
543}
544
545static vpx_codec_err_t decoder_decode(vpx_codec_alg_priv_t *ctx,
546                                      const uint8_t *data, unsigned int data_sz,
547                                      void *user_priv, long deadline) {
548  const uint8_t *data_start = data;
549  const uint8_t * const data_end = data + data_sz;
550  vpx_codec_err_t res;
551  uint32_t frame_sizes[8];
552  int frame_count;
553
554  if (data == NULL && data_sz == 0) {
555    ctx->flushed = 1;
556    return VPX_CODEC_OK;
557  }
558
559  // Reset flushed when receiving a valid frame.
560  ctx->flushed = 0;
561
562  // Initialize the decoder workers on the first frame.
563  if (ctx->frame_workers == NULL) {
564    const vpx_codec_err_t res = init_decoder(ctx);
565    if (res != VPX_CODEC_OK)
566      return res;
567  }
568
569  res = vp9_parse_superframe_index(data, data_sz, frame_sizes, &frame_count,
570                                   ctx->decrypt_cb, ctx->decrypt_state);
571  if (res != VPX_CODEC_OK)
572    return res;
573
574  if (ctx->frame_parallel_decode) {
575    // Decode in frame parallel mode. When decoding in this mode, the frame
576    // passed to the decoder must be either a normal frame or a superframe with
577    // superframe index so the decoder could get each frame's start position
578    // in the superframe.
579    if (frame_count > 0) {
580      int i;
581
582      for (i = 0; i < frame_count; ++i) {
583        const uint8_t *data_start_copy = data_start;
584        const uint32_t frame_size = frame_sizes[i];
585        if (data_start < data
586            || frame_size > (uint32_t) (data_end - data_start)) {
587          set_error_detail(ctx, "Invalid frame size in index");
588          return VPX_CODEC_CORRUPT_FRAME;
589        }
590
591        if (ctx->available_threads == 0) {
592          // No more threads for decoding. Wait until the next output worker
593          // finishes decoding. Then copy the decoded frame into cache.
594          if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
595            wait_worker_and_cache_frame(ctx);
596          } else {
597            // TODO(hkuang): Add unit test to test this path.
598            set_error_detail(ctx, "Frame output cache is full.");
599            return VPX_CODEC_ERROR;
600          }
601        }
602
603        res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
604                         deadline);
605        if (res != VPX_CODEC_OK)
606          return res;
607        data_start += frame_size;
608      }
609    } else {
610      if (ctx->available_threads == 0) {
611        // No more threads for decoding. Wait until the next output worker
612        // finishes decoding. Then copy the decoded frame into cache.
613        if (ctx->num_cache_frames < FRAME_CACHE_SIZE) {
614          wait_worker_and_cache_frame(ctx);
615        } else {
616          // TODO(hkuang): Add unit test to test this path.
617          set_error_detail(ctx, "Frame output cache is full.");
618          return VPX_CODEC_ERROR;
619        }
620      }
621
622      res = decode_one(ctx, &data, data_sz, user_priv, deadline);
623      if (res != VPX_CODEC_OK)
624        return res;
625    }
626  } else {
627    // Decode in serial mode.
628    if (frame_count > 0) {
629      int i;
630
631      for (i = 0; i < frame_count; ++i) {
632        const uint8_t *data_start_copy = data_start;
633        const uint32_t frame_size = frame_sizes[i];
634        vpx_codec_err_t res;
635        if (data_start < data
636            || frame_size > (uint32_t) (data_end - data_start)) {
637          set_error_detail(ctx, "Invalid frame size in index");
638          return VPX_CODEC_CORRUPT_FRAME;
639        }
640
641        res = decode_one(ctx, &data_start_copy, frame_size, user_priv,
642                         deadline);
643        if (res != VPX_CODEC_OK)
644          return res;
645
646        data_start += frame_size;
647      }
648    } else {
649      while (data_start < data_end) {
650        const uint32_t frame_size = (uint32_t) (data_end - data_start);
651        const vpx_codec_err_t res = decode_one(ctx, &data_start, frame_size,
652                                               user_priv, deadline);
653        if (res != VPX_CODEC_OK)
654          return res;
655
656        // Account for suboptimal termination by the encoder.
657        while (data_start < data_end) {
658          const uint8_t marker = read_marker(ctx->decrypt_cb,
659                                             ctx->decrypt_state, data_start);
660          if (marker)
661            break;
662          ++data_start;
663        }
664      }
665    }
666  }
667
668  return res;
669}
670
671static void release_last_output_frame(vpx_codec_alg_priv_t *ctx) {
672  RefCntBuffer *const frame_bufs = ctx->buffer_pool->frame_bufs;
673  // Decrease reference count of last output frame in frame parallel mode.
674  if (ctx->frame_parallel_decode && ctx->last_show_frame >= 0) {
675    BufferPool *const pool = ctx->buffer_pool;
676    lock_buffer_pool(pool);
677    decrease_ref_count(ctx->last_show_frame, frame_bufs, pool);
678    unlock_buffer_pool(pool);
679  }
680}
681
682static vpx_image_t *decoder_get_frame(vpx_codec_alg_priv_t *ctx,
683                                      vpx_codec_iter_t *iter) {
684  vpx_image_t *img = NULL;
685
686  // Only return frame when all the cpu are busy or
687  // application fluhsed the decoder in frame parallel decode.
688  if (ctx->frame_parallel_decode && ctx->available_threads > 0 &&
689      !ctx->flushed) {
690    return NULL;
691  }
692
693  // Output the frames in the cache first.
694  if (ctx->num_cache_frames > 0) {
695    release_last_output_frame(ctx);
696    ctx->last_show_frame  = ctx->frame_cache[ctx->frame_cache_read].fb_idx;
697    if (ctx->need_resync)
698      return NULL;
699    img = &ctx->frame_cache[ctx->frame_cache_read].img;
700    ctx->frame_cache_read = (ctx->frame_cache_read + 1) % FRAME_CACHE_SIZE;
701    --ctx->num_cache_frames;
702    return img;
703  }
704
705  // iter acts as a flip flop, so an image is only returned on the first
706  // call to get_frame.
707  if (*iter == NULL && ctx->frame_workers != NULL) {
708    do {
709      YV12_BUFFER_CONFIG sd;
710      vp9_ppflags_t flags = {0, 0, 0};
711      const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
712      VPxWorker *const worker =
713          &ctx->frame_workers[ctx->next_output_worker_id];
714      FrameWorkerData *const frame_worker_data =
715          (FrameWorkerData *)worker->data1;
716      ctx->next_output_worker_id =
717          (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
718      if (ctx->base.init_flags & VPX_CODEC_USE_POSTPROC)
719        set_ppflags(ctx, &flags);
720      // Wait for the frame from worker thread.
721      if (winterface->sync(worker)) {
722        // Check if worker has received any frames.
723        if (frame_worker_data->received_frame == 1) {
724          ++ctx->available_threads;
725          frame_worker_data->received_frame = 0;
726          check_resync(ctx, frame_worker_data->pbi);
727        }
728        if (vp9_get_raw_frame(frame_worker_data->pbi, &sd, &flags) == 0) {
729          VP9_COMMON *const cm = &frame_worker_data->pbi->common;
730          RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
731          release_last_output_frame(ctx);
732          ctx->last_show_frame = frame_worker_data->pbi->common.new_fb_idx;
733          if (ctx->need_resync)
734            return NULL;
735          yuvconfig2image(&ctx->img, &sd, frame_worker_data->user_priv);
736          ctx->img.fb_priv = frame_bufs[cm->new_fb_idx].raw_frame_buffer.priv;
737          img = &ctx->img;
738          return img;
739        }
740      } else {
741        // Decoding failed. Release the worker thread.
742        frame_worker_data->received_frame = 0;
743        ++ctx->available_threads;
744        ctx->need_resync = 1;
745        if (ctx->flushed != 1)
746          return NULL;
747      }
748    } while (ctx->next_output_worker_id != ctx->next_submit_worker_id);
749  }
750  return NULL;
751}
752
753static vpx_codec_err_t decoder_set_fb_fn(
754    vpx_codec_alg_priv_t *ctx,
755    vpx_get_frame_buffer_cb_fn_t cb_get,
756    vpx_release_frame_buffer_cb_fn_t cb_release, void *cb_priv) {
757  if (cb_get == NULL || cb_release == NULL) {
758    return VPX_CODEC_INVALID_PARAM;
759  } else if (ctx->frame_workers == NULL) {
760    // If the decoder has already been initialized, do not accept changes to
761    // the frame buffer functions.
762    ctx->get_ext_fb_cb = cb_get;
763    ctx->release_ext_fb_cb = cb_release;
764    ctx->ext_priv = cb_priv;
765    return VPX_CODEC_OK;
766  }
767
768  return VPX_CODEC_ERROR;
769}
770
771static vpx_codec_err_t ctrl_set_reference(vpx_codec_alg_priv_t *ctx,
772                                          va_list args) {
773  vpx_ref_frame_t *const data = va_arg(args, vpx_ref_frame_t *);
774
775  // Only support this function in serial decode.
776  if (ctx->frame_parallel_decode) {
777    set_error_detail(ctx, "Not supported in frame parallel decode");
778    return VPX_CODEC_INCAPABLE;
779  }
780
781  if (data) {
782    vpx_ref_frame_t *const frame = (vpx_ref_frame_t *)data;
783    YV12_BUFFER_CONFIG sd;
784    VPxWorker *const worker = ctx->frame_workers;
785    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
786    image2yuvconfig(&frame->img, &sd);
787    return vp9_set_reference_dec(&frame_worker_data->pbi->common,
788                                 (VP9_REFFRAME)frame->frame_type, &sd);
789  } else {
790    return VPX_CODEC_INVALID_PARAM;
791  }
792}
793
794static vpx_codec_err_t ctrl_copy_reference(vpx_codec_alg_priv_t *ctx,
795                                           va_list args) {
796  vpx_ref_frame_t *data = va_arg(args, vpx_ref_frame_t *);
797
798  // Only support this function in serial decode.
799  if (ctx->frame_parallel_decode) {
800    set_error_detail(ctx, "Not supported in frame parallel decode");
801    return VPX_CODEC_INCAPABLE;
802  }
803
804  if (data) {
805    vpx_ref_frame_t *frame = (vpx_ref_frame_t *) data;
806    YV12_BUFFER_CONFIG sd;
807    VPxWorker *const worker = ctx->frame_workers;
808    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
809    image2yuvconfig(&frame->img, &sd);
810    return vp9_copy_reference_dec(frame_worker_data->pbi,
811                                  (VP9_REFFRAME)frame->frame_type, &sd);
812  } else {
813    return VPX_CODEC_INVALID_PARAM;
814  }
815}
816
817static vpx_codec_err_t ctrl_get_reference(vpx_codec_alg_priv_t *ctx,
818                                          va_list args) {
819  vp9_ref_frame_t *data = va_arg(args, vp9_ref_frame_t *);
820
821  // Only support this function in serial decode.
822  if (ctx->frame_parallel_decode) {
823    set_error_detail(ctx, "Not supported in frame parallel decode");
824    return VPX_CODEC_INCAPABLE;
825  }
826
827  if (data) {
828    YV12_BUFFER_CONFIG* fb;
829    VPxWorker *const worker = ctx->frame_workers;
830    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
831    fb = get_ref_frame(&frame_worker_data->pbi->common, data->idx);
832    if (fb == NULL) return VPX_CODEC_ERROR;
833    yuvconfig2image(&data->img, fb, NULL);
834    return VPX_CODEC_OK;
835  } else {
836    return VPX_CODEC_INVALID_PARAM;
837  }
838}
839
840static vpx_codec_err_t ctrl_set_postproc(vpx_codec_alg_priv_t *ctx,
841                                         va_list args) {
842#if CONFIG_VP9_POSTPROC
843  vp8_postproc_cfg_t *data = va_arg(args, vp8_postproc_cfg_t *);
844
845  if (data) {
846    ctx->postproc_cfg_set = 1;
847    ctx->postproc_cfg = *((vp8_postproc_cfg_t *)data);
848    return VPX_CODEC_OK;
849  } else {
850    return VPX_CODEC_INVALID_PARAM;
851  }
852#else
853  (void)ctx;
854  (void)args;
855  return VPX_CODEC_INCAPABLE;
856#endif
857}
858
859static vpx_codec_err_t ctrl_set_dbg_options(vpx_codec_alg_priv_t *ctx,
860                                            va_list args) {
861  (void)ctx;
862  (void)args;
863  return VPX_CODEC_INCAPABLE;
864}
865
866static vpx_codec_err_t ctrl_get_last_ref_updates(vpx_codec_alg_priv_t *ctx,
867                                                 va_list args) {
868  int *const update_info = va_arg(args, int *);
869
870  // Only support this function in serial decode.
871  if (ctx->frame_parallel_decode) {
872    set_error_detail(ctx, "Not supported in frame parallel decode");
873    return VPX_CODEC_INCAPABLE;
874  }
875
876  if (update_info) {
877    if (ctx->frame_workers) {
878      VPxWorker *const worker = ctx->frame_workers;
879      FrameWorkerData *const frame_worker_data =
880          (FrameWorkerData *)worker->data1;
881      *update_info = frame_worker_data->pbi->refresh_frame_flags;
882      return VPX_CODEC_OK;
883    } else {
884      return VPX_CODEC_ERROR;
885    }
886  }
887
888  return VPX_CODEC_INVALID_PARAM;
889}
890
891static vpx_codec_err_t ctrl_get_frame_corrupted(vpx_codec_alg_priv_t *ctx,
892                                                va_list args) {
893  int *corrupted = va_arg(args, int *);
894
895  if (corrupted) {
896    if (ctx->frame_workers) {
897      VPxWorker *const worker = ctx->frame_workers;
898      FrameWorkerData *const frame_worker_data =
899          (FrameWorkerData *)worker->data1;
900      RefCntBuffer *const frame_bufs =
901          frame_worker_data->pbi->common.buffer_pool->frame_bufs;
902      if (frame_worker_data->pbi->common.frame_to_show == NULL)
903        return VPX_CODEC_ERROR;
904      if (ctx->last_show_frame >= 0)
905        *corrupted = frame_bufs[ctx->last_show_frame].buf.corrupted;
906      return VPX_CODEC_OK;
907    } else {
908      return VPX_CODEC_ERROR;
909    }
910  }
911
912  return VPX_CODEC_INVALID_PARAM;
913}
914
915static vpx_codec_err_t ctrl_get_frame_size(vpx_codec_alg_priv_t *ctx,
916                                           va_list args) {
917  int *const frame_size = va_arg(args, int *);
918
919  // Only support this function in serial decode.
920  if (ctx->frame_parallel_decode) {
921    set_error_detail(ctx, "Not supported in frame parallel decode");
922    return VPX_CODEC_INCAPABLE;
923  }
924
925  if (frame_size) {
926    if (ctx->frame_workers) {
927      VPxWorker *const worker = ctx->frame_workers;
928      FrameWorkerData *const frame_worker_data =
929          (FrameWorkerData *)worker->data1;
930      const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
931      frame_size[0] = cm->width;
932      frame_size[1] = cm->height;
933      return VPX_CODEC_OK;
934    } else {
935      return VPX_CODEC_ERROR;
936    }
937  }
938
939  return VPX_CODEC_INVALID_PARAM;
940}
941
942static vpx_codec_err_t ctrl_get_render_size(vpx_codec_alg_priv_t *ctx,
943                                            va_list args) {
944  int *const render_size = va_arg(args, int *);
945
946  // Only support this function in serial decode.
947  if (ctx->frame_parallel_decode) {
948    set_error_detail(ctx, "Not supported in frame parallel decode");
949    return VPX_CODEC_INCAPABLE;
950  }
951
952  if (render_size) {
953    if (ctx->frame_workers) {
954      VPxWorker *const worker = ctx->frame_workers;
955      FrameWorkerData *const frame_worker_data =
956          (FrameWorkerData *)worker->data1;
957      const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
958      render_size[0] = cm->render_width;
959      render_size[1] = cm->render_height;
960      return VPX_CODEC_OK;
961    } else {
962      return VPX_CODEC_ERROR;
963    }
964  }
965
966  return VPX_CODEC_INVALID_PARAM;
967}
968
969static vpx_codec_err_t ctrl_get_bit_depth(vpx_codec_alg_priv_t *ctx,
970                                          va_list args) {
971  unsigned int *const bit_depth = va_arg(args, unsigned int *);
972  VPxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
973
974  if (bit_depth) {
975    if (worker) {
976      FrameWorkerData *const frame_worker_data =
977          (FrameWorkerData *)worker->data1;
978      const VP9_COMMON *const cm = &frame_worker_data->pbi->common;
979      *bit_depth = cm->bit_depth;
980      return VPX_CODEC_OK;
981    } else {
982      return VPX_CODEC_ERROR;
983    }
984  }
985
986  return VPX_CODEC_INVALID_PARAM;
987}
988
989static vpx_codec_err_t ctrl_set_invert_tile_order(vpx_codec_alg_priv_t *ctx,
990                                                  va_list args) {
991  ctx->invert_tile_order = va_arg(args, int);
992  return VPX_CODEC_OK;
993}
994
995static vpx_codec_err_t ctrl_set_decryptor(vpx_codec_alg_priv_t *ctx,
996                                          va_list args) {
997  vpx_decrypt_init *init = va_arg(args, vpx_decrypt_init *);
998  ctx->decrypt_cb = init ? init->decrypt_cb : NULL;
999  ctx->decrypt_state = init ? init->decrypt_state : NULL;
1000  return VPX_CODEC_OK;
1001}
1002
1003static vpx_codec_err_t ctrl_set_byte_alignment(vpx_codec_alg_priv_t *ctx,
1004                                               va_list args) {
1005  const int legacy_byte_alignment = 0;
1006  const int min_byte_alignment = 32;
1007  const int max_byte_alignment = 1024;
1008  const int byte_alignment = va_arg(args, int);
1009
1010  if (byte_alignment != legacy_byte_alignment &&
1011      (byte_alignment < min_byte_alignment ||
1012       byte_alignment > max_byte_alignment ||
1013       (byte_alignment & (byte_alignment - 1)) != 0))
1014    return VPX_CODEC_INVALID_PARAM;
1015
1016  ctx->byte_alignment = byte_alignment;
1017  if (ctx->frame_workers) {
1018    VPxWorker *const worker = ctx->frame_workers;
1019    FrameWorkerData *const frame_worker_data =
1020        (FrameWorkerData *)worker->data1;
1021    frame_worker_data->pbi->common.byte_alignment = byte_alignment;
1022  }
1023  return VPX_CODEC_OK;
1024}
1025
1026static vpx_codec_err_t ctrl_set_skip_loop_filter(vpx_codec_alg_priv_t *ctx,
1027                                                 va_list args) {
1028  ctx->skip_loop_filter = va_arg(args, int);
1029
1030  if (ctx->frame_workers) {
1031    VPxWorker *const worker = ctx->frame_workers;
1032    FrameWorkerData *const frame_worker_data = (FrameWorkerData *)worker->data1;
1033    frame_worker_data->pbi->common.skip_loop_filter = ctx->skip_loop_filter;
1034  }
1035
1036  return VPX_CODEC_OK;
1037}
1038
1039static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = {
1040  {VP8_COPY_REFERENCE,            ctrl_copy_reference},
1041
1042  // Setters
1043  {VP8_SET_REFERENCE,             ctrl_set_reference},
1044  {VP8_SET_POSTPROC,              ctrl_set_postproc},
1045  {VP8_SET_DBG_COLOR_REF_FRAME,   ctrl_set_dbg_options},
1046  {VP8_SET_DBG_COLOR_MB_MODES,    ctrl_set_dbg_options},
1047  {VP8_SET_DBG_COLOR_B_MODES,     ctrl_set_dbg_options},
1048  {VP8_SET_DBG_DISPLAY_MV,        ctrl_set_dbg_options},
1049  {VP9_INVERT_TILE_DECODE_ORDER,  ctrl_set_invert_tile_order},
1050  {VPXD_SET_DECRYPTOR,            ctrl_set_decryptor},
1051  {VP9_SET_BYTE_ALIGNMENT,        ctrl_set_byte_alignment},
1052  {VP9_SET_SKIP_LOOP_FILTER,      ctrl_set_skip_loop_filter},
1053
1054  // Getters
1055  {VP8D_GET_LAST_REF_UPDATES,     ctrl_get_last_ref_updates},
1056  {VP8D_GET_FRAME_CORRUPTED,      ctrl_get_frame_corrupted},
1057  {VP9_GET_REFERENCE,             ctrl_get_reference},
1058  {VP9D_GET_DISPLAY_SIZE,         ctrl_get_render_size},
1059  {VP9D_GET_BIT_DEPTH,            ctrl_get_bit_depth},
1060  {VP9D_GET_FRAME_SIZE,           ctrl_get_frame_size},
1061
1062  { -1, NULL},
1063};
1064
1065#ifndef VERSION_STRING
1066#define VERSION_STRING
1067#endif
1068CODEC_INTERFACE(vpx_codec_vp9_dx) = {
1069  "WebM Project VP9 Decoder" VERSION_STRING,
1070  VPX_CODEC_INTERNAL_ABI_VERSION,
1071  VPX_CODEC_CAP_DECODER | VP9_CAP_POSTPROC |
1072      VPX_CODEC_CAP_EXTERNAL_FRAME_BUFFER,  // vpx_codec_caps_t
1073  decoder_init,       // vpx_codec_init_fn_t
1074  decoder_destroy,    // vpx_codec_destroy_fn_t
1075  decoder_ctrl_maps,  // vpx_codec_ctrl_fn_map_t
1076  { // NOLINT
1077    decoder_peek_si,    // vpx_codec_peek_si_fn_t
1078    decoder_get_si,     // vpx_codec_get_si_fn_t
1079    decoder_decode,     // vpx_codec_decode_fn_t
1080    decoder_get_frame,  // vpx_codec_frame_get_fn_t
1081    decoder_set_fb_fn,  // vpx_codec_set_fb_fn_t
1082  },
1083  { // NOLINT
1084    0,
1085    NULL,  // vpx_codec_enc_cfg_map_t
1086    NULL,  // vpx_codec_encode_fn_t
1087    NULL,  // vpx_codec_get_cx_data_fn_t
1088    NULL,  // vpx_codec_enc_config_set_fn_t
1089    NULL,  // vpx_codec_get_global_headers_fn_t
1090    NULL,  // vpx_codec_get_preview_frame_fn_t
1091    NULL   // vpx_codec_enc_mr_get_mem_loc_fn_t
1092  }
1093};
1094