• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 VPX_VP9_DECODER_VP9_DECODER_H_
12 #define VPX_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_job_queue.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define EOBS_PER_SB_LOG2 8
31 #define DQCOEFFS_PER_SB_LOG2 12
32 #define PARTITIONS_PER_SB 85
33 
34 typedef enum JobType { PARSE_JOB, RECON_JOB, LPF_JOB } JobType;
35 
36 typedef struct ThreadData {
37   struct VP9Decoder *pbi;
38   LFWorkerData *lf_data;
39   VP9LfSync *lf_sync;
40 } ThreadData;
41 
42 typedef struct TileBuffer {
43   const uint8_t *data;
44   size_t size;
45   int col;  // only used with multi-threaded decoding
46 } TileBuffer;
47 
48 typedef struct TileWorkerData {
49   const uint8_t *data_end;
50   int buf_start, buf_end;  // pbi->tile_buffers to decode, inclusive
51   vpx_reader bit_reader;
52   FRAME_COUNTS counts;
53   LFWorkerData *lf_data;
54   VP9LfSync *lf_sync;
55   DECLARE_ALIGNED(16, MACROBLOCKD, xd);
56   /* dqcoeff are shared by all the planes. So planes must be decoded serially */
57   DECLARE_ALIGNED(16, tran_low_t, dqcoeff[32 * 32]);
58   DECLARE_ALIGNED(16, uint16_t, extend_and_predict_buf[80 * 2 * 80 * 2]);
59   struct vpx_internal_error_info error_info;
60 } TileWorkerData;
61 
62 typedef void (*process_block_fn_t)(TileWorkerData *twd,
63                                    struct VP9Decoder *const pbi, int mi_row,
64                                    int mi_col, BLOCK_SIZE bsize, int bwl,
65                                    int bhl);
66 
67 typedef struct RowMTWorkerData {
68   int num_sbs;
69   int *eob[MAX_MB_PLANE];
70   PARTITION_TYPE *partition;
71   tran_low_t *dqcoeff[MAX_MB_PLANE];
72   int8_t *recon_map;
73   const uint8_t *data_end;
74   uint8_t *jobq_buf;
75   JobQueueRowMt jobq;
76   size_t jobq_size;
77   int num_tiles_done;
78   int num_jobs;
79 #if CONFIG_MULTITHREAD
80   pthread_mutex_t recon_done_mutex;
81   pthread_mutex_t *recon_sync_mutex;
82   pthread_cond_t *recon_sync_cond;
83 #endif
84   ThreadData *thread_data;
85 } RowMTWorkerData;
86 
87 /* Structure to queue and dequeue row decode jobs */
88 typedef struct Job {
89   int row_num;
90   int tile_col;
91   JobType job_type;
92 } Job;
93 
94 typedef struct VP9Decoder {
95   DECLARE_ALIGNED(16, MACROBLOCKD, mb);
96 
97   DECLARE_ALIGNED(16, VP9_COMMON, common);
98 
99   int ready_for_new_data;
100 
101   int refresh_frame_flags;
102 
103   // TODO(hkuang): Combine this with cur_buf in macroblockd as they are
104   // the same.
105   RefCntBuffer *cur_buf;  //  Current decoding frame buffer.
106 
107   VPxWorker lf_worker;
108   VPxWorker *tile_workers;
109   TileWorkerData *tile_worker_data;
110   TileBuffer tile_buffers[64];
111   int num_tile_workers;
112   int total_tiles;
113 
114   VP9LfSync lf_row_sync;
115 
116   vpx_decrypt_cb decrypt_cb;
117   void *decrypt_state;
118 
119   int max_threads;
120   int inv_tile_order;
121   int need_resync;   // wait for key/intra-only frame.
122   int hold_ref_buf;  // hold the reference buffer.
123 
124   int row_mt;
125   int lpf_mt_opt;
126   RowMTWorkerData *row_mt_worker_data;
127 } VP9Decoder;
128 
129 int vp9_receive_compressed_data(struct VP9Decoder *pbi, size_t size,
130                                 const uint8_t **psource);
131 
132 int vp9_get_raw_frame(struct VP9Decoder *pbi, YV12_BUFFER_CONFIG *sd,
133                       vp9_ppflags_t *flags);
134 
135 vpx_codec_err_t vp9_copy_reference_dec(struct VP9Decoder *pbi,
136                                        VP9_REFFRAME ref_frame_flag,
137                                        YV12_BUFFER_CONFIG *sd);
138 
139 vpx_codec_err_t vp9_set_reference_dec(VP9_COMMON *cm,
140                                       VP9_REFFRAME ref_frame_flag,
141                                       YV12_BUFFER_CONFIG *sd);
142 
read_marker(vpx_decrypt_cb decrypt_cb,void * decrypt_state,const uint8_t * data)143 static INLINE uint8_t read_marker(vpx_decrypt_cb decrypt_cb,
144                                   void *decrypt_state, const uint8_t *data) {
145   if (decrypt_cb) {
146     uint8_t marker;
147     decrypt_cb(decrypt_state, data, &marker, 1);
148     return marker;
149   }
150   return *data;
151 }
152 
153 // This function is exposed for use in tests, as well as the inlined function
154 // "read_marker".
155 vpx_codec_err_t vp9_parse_superframe_index(const uint8_t *data, size_t data_sz,
156                                            uint32_t sizes[8], int *count,
157                                            vpx_decrypt_cb decrypt_cb,
158                                            void *decrypt_state);
159 
160 struct VP9Decoder *vp9_decoder_create(BufferPool *const pool);
161 
162 void vp9_decoder_remove(struct VP9Decoder *pbi);
163 
164 void vp9_dec_alloc_row_mt_mem(RowMTWorkerData *row_mt_worker_data,
165                               VP9_COMMON *cm, int num_sbs, int max_threads,
166                               int num_jobs);
167 void vp9_dec_free_row_mt_mem(RowMTWorkerData *row_mt_worker_data);
168 
decrease_ref_count(int idx,RefCntBuffer * const frame_bufs,BufferPool * const pool)169 static INLINE void decrease_ref_count(int idx, RefCntBuffer *const frame_bufs,
170                                       BufferPool *const pool) {
171   if (idx >= 0 && frame_bufs[idx].ref_count > 0) {
172     --frame_bufs[idx].ref_count;
173     // A worker may only get a free framebuffer index when calling get_free_fb.
174     // But the private buffer is not set up until finish decoding header.
175     // So any error happens during decoding header, the frame_bufs will not
176     // have valid priv buffer.
177     if (!frame_bufs[idx].released && frame_bufs[idx].ref_count == 0 &&
178         frame_bufs[idx].raw_frame_buffer.priv) {
179       pool->release_fb_cb(pool->cb_priv, &frame_bufs[idx].raw_frame_buffer);
180       frame_bufs[idx].released = 1;
181     }
182   }
183 }
184 
185 #ifdef __cplusplus
186 }  // extern "C"
187 #endif
188 
189 #endif  // VPX_VP9_DECODER_VP9_DECODER_H_
190