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#ifndef VP9_DECODER_VP9_DECODER_H_
12#define VP9_DECODER_VP9_DECODER_H_
13
14#include "./vpx_config.h"
15
16#include "vpx/vpx_codec.h"
17#include "vpx_dsp/bitreader.h"
18#include "vpx_scale/yv12config.h"
19#include "vpx_util/vpx_thread.h"
20
21#include "vp9/common/vp9_thread_common.h"
22#include "vp9/common/vp9_onyxc_int.h"
23#include "vp9/common/vp9_ppflags.h"
24#include "vp9/decoder/vp9_dthread.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30// TODO(hkuang): combine this with TileWorkerData.
31typedef struct TileData {
32  VP9_COMMON *cm;
33  vpx_reader bit_reader;
34  DECLARE_ALIGNED(16, MACROBLOCKD, xd);
35  /* dqcoeff are shared by all the planes. So planes must be decoded serially */
36  DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
37} TileData;
38
39typedef struct TileBuffer {
40  const uint8_t *data;
41  size_t size;
42  int col;  // only used with multi-threaded decoding
43} TileBuffer;
44
45typedef struct TileWorkerData {
46  const uint8_t *data_end;
47  int buf_start, buf_end;  // pbi->tile_buffers to decode, inclusive
48  vpx_reader bit_reader;
49  FRAME_COUNTS counts;
50  DECLARE_ALIGNED(16, MACROBLOCKD, xd);
51  /* dqcoeff are shared by all the planes. So planes must be decoded serially */
52  DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
53  struct vpx_internal_error_info error_info;
54} TileWorkerData;
55
56typedef struct VP9Decoder {
57  DECLARE_ALIGNED(16, MACROBLOCKD, mb);
58
59  DECLARE_ALIGNED(16, VP9_COMMON, common);
60
61  int ready_for_new_data;
62
63  int refresh_frame_flags;
64
65  int frame_parallel_decode;  // frame-based threading.
66
67  // TODO(hkuang): Combine this with cur_buf in macroblockd as they are
68  // the same.
69  RefCntBuffer *cur_buf;   //  Current decoding frame buffer.
70
71  VPxWorker *frame_worker_owner;   // frame_worker that owns this pbi.
72  VPxWorker lf_worker;
73  VPxWorker *tile_workers;
74  TileWorkerData *tile_worker_data;
75  TileBuffer tile_buffers[64];
76  int num_tile_workers;
77
78  TileData *tile_data;
79  int total_tiles;
80
81  VP9LfSync lf_row_sync;
82
83  vpx_decrypt_cb decrypt_cb;
84  void *decrypt_state;
85
86  int max_threads;
87  int inv_tile_order;
88  int need_resync;  // wait for key/intra-only frame.
89  int hold_ref_buf;  // hold the reference buffer.
90} VP9Decoder;
91
92int vp9_receive_compressed_data(struct VP9Decoder *pbi,
93                                size_t size, const uint8_t **dest);
94
95int vp9_get_raw_frame(struct VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
96                      vp9_ppflags_t *flags);
97
98vpx_codec_err_t vp9_copy_reference_dec(struct VP9Decoder *pbi,
99                                       VP9_REFFRAME ref_frame_flag,
100                                       YV12_BUFFER_CONFIG *sd);
101
102vpx_codec_err_t vp9_set_reference_dec(VP9_COMMON *cm,
103                                      VP9_REFFRAME ref_frame_flag,
104                                      YV12_BUFFER_CONFIG *sd);
105
106static INLINE uint8_t read_marker(vpx_decrypt_cb decrypt_cb,
107                                  void *decrypt_state,
108                                  const uint8_t *data) {
109  if (decrypt_cb) {
110    uint8_t marker;
111    decrypt_cb(decrypt_state, data, &marker, 1);
112    return marker;
113  }
114  return *data;
115}
116
117// This function is exposed for use in tests, as well as the inlined function
118// "read_marker".
119vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data,
120                                           size_t data_sz,
121                                           uint32_t sizes[8], int *count,
122                                           vpx_decrypt_cb decrypt_cb,
123                                           void *decrypt_state);
124
125struct VP9Decoder *vp9_decoder_create(BufferPool *const pool);
126
127void vp9_decoder_remove(struct VP9Decoder *pbi);
128
129static INLINE void decrease_ref_count(int idx, RefCntBuffer *const frame_bufs,
130                                      BufferPool *const pool) {
131  if (idx >= 0) {
132    --frame_bufs[idx].ref_count;
133    // A worker may only get a free framebuffer index when calling get_free_fb.
134    // But the private buffer is not set up until finish decoding header.
135    // So any error happens during decoding header, the frame_bufs will not
136    // have valid priv buffer.
137    if (frame_bufs[idx].ref_count == 0 &&
138        frame_bufs[idx].raw_frame_buffer.priv) {
139      pool->release_fb_cb(pool->cb_priv, &frame_bufs[idx].raw_frame_buffer);
140    }
141  }
142}
143
144#ifdef __cplusplus
145}  // extern "C"
146#endif
147
148#endif  // VP9_DECODER_VP9_DECODER_H_
149