../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
24 / 24
100.0%
Branches:
8 / 8
100.0%
Functions:
2 / 2
100.0%

libavcodec/cavs.h
Line Branch Exec Source
1 /*
2 * Chinese AVS video (AVS1-P2/P16, JiZhun and Broadcasting profiles) decoder.
3 * Copyright (c) 2006 Stefan Gehrer <stefan.gehrer@gmx.de>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef AVCODEC_CAVS_H
23 #define AVCODEC_CAVS_H
24
25 #include <stddef.h>
26 #include <stdint.h>
27 #include <string.h>
28
29 #include "libavutil/attributes.h"
30 #include "libavutil/frame.h"
31 #include "libavutil/mem_internal.h"
32
33 #include "avcodec.h"
34 #include "cavs_aec.h"
35 #include "cavsdsp.h"
36 #include "blockdsp.h"
37 #include "h264chroma.h"
38 #include "get_bits.h"
39 #include "videodsp.h"
40
41 #define SLICE_MAX_START_CODE 0x000001af
42 #define EXT_START_CODE 0x000001b5
43 #define USER_START_CODE 0x000001b2
44 #define CAVS_START_CODE 0x000001b0
45 #define PIC_I_START_CODE 0x000001b3
46 #define PIC_PB_START_CODE 0x000001b6
47
48 #define A_AVAIL 1
49 #define B_AVAIL 2
50 #define C_AVAIL 4
51 #define D_AVAIL 8
52 #define NOT_AVAIL -1
53 #define REF_INTRA -2
54 #define REF_DIR -3
55
56 #define ESCAPE_CODE 59
57
58 #define FWD0 0x01
59 #define FWD1 0x02
60 #define BWD0 0x04
61 #define BWD1 0x08
62 #define SYM0 0x10
63 #define SYM1 0x20
64 #define SPLITH 0x40
65 #define SPLITV 0x80
66
67 #define MV_BWD_OFFS 12
68 #define MV_STRIDE 4
69
70 enum cavs_mb {
71 I_8X8 = 0,
72 P_SKIP,
73 P_16X16,
74 P_16X8,
75 P_8X16,
76 P_8X8,
77 B_SKIP,
78 B_DIRECT,
79 B_FWD_16X16,
80 B_BWD_16X16,
81 B_SYM_16X16,
82 B_8X8 = 29
83 };
84
85 enum cavs_sub_mb {
86 B_SUB_DIRECT,
87 B_SUB_FWD,
88 B_SUB_BWD,
89 B_SUB_SYM
90 };
91
92 enum cavs_intra_luma {
93 INTRA_L_VERT,
94 INTRA_L_HORIZ,
95 INTRA_L_LP,
96 INTRA_L_DOWN_LEFT,
97 INTRA_L_DOWN_RIGHT,
98 INTRA_L_LP_LEFT,
99 INTRA_L_LP_TOP,
100 INTRA_L_DC_128
101 };
102
103 enum cavs_intra_chroma {
104 INTRA_C_LP,
105 INTRA_C_HORIZ,
106 INTRA_C_VERT,
107 INTRA_C_PLANE,
108 INTRA_C_LP_LEFT,
109 INTRA_C_LP_TOP,
110 INTRA_C_DC_128,
111 };
112
113 enum cavs_mv_pred {
114 MV_PRED_MEDIAN,
115 MV_PRED_LEFT,
116 MV_PRED_TOP,
117 MV_PRED_TOPRIGHT,
118 MV_PRED_PSKIP,
119 MV_PRED_BSKIP
120 };
121
122 enum cavs_block {
123 BLK_16X16,
124 BLK_16X8,
125 BLK_8X16,
126 BLK_8X8
127 };
128
129 enum cavs_mv_loc {
130 MV_FWD_D3 = 0,
131 MV_FWD_B2,
132 MV_FWD_B3,
133 MV_FWD_C2,
134 MV_FWD_A1,
135 MV_FWD_X0,
136 MV_FWD_X1,
137 MV_FWD_A3 = 8,
138 MV_FWD_X2,
139 MV_FWD_X3,
140 MV_BWD_D3 = MV_BWD_OFFS,
141 MV_BWD_B2,
142 MV_BWD_B3,
143 MV_BWD_C2,
144 MV_BWD_A1,
145 MV_BWD_X0,
146 MV_BWD_X1,
147 MV_BWD_A3 = MV_BWD_OFFS+8,
148 MV_BWD_X2,
149 MV_BWD_X3
150 };
151
152 typedef struct cavs_vector {
153 int16_t x;
154 int16_t y;
155 int16_t dist;
156 int16_t ref;
157 /**
158 * mb_reference_index of the block, 0 when it codes none (clause 9.4.5);
159 * ref above is where the reference list puts it
160 */
161 int16_t ref_idx;
162 } cavs_vector;
163
164 struct dec_2dvlc {
165 int8_t rltab[59][3];
166 int8_t level_add[27];
167 int8_t golomb_order;
168 int inc_limit;
169 int8_t max_run;
170 };
171
172 typedef struct AVSFrame {
173 AVFrame *f;
174 int poc;
175 } AVSFrame;
176
177 typedef struct AVSContext {
178 AVCodecContext *avctx;
179 BlockDSPContext bdsp;
180 H264ChromaContext h264chroma;
181 VideoDSPContext vdsp;
182 CAVSDSPContext cdsp;
183 GetBitContext gb;
184 AVSFrame cur; ///< currently decoded frame
185 AVSFrame DPB[2]; ///< reference frames
186 /**
187 * The reference of every reference index, clause 9.4.5: a frame, or the
188 * field of one starting at parity; both lists share the array.
189 */
190 struct {
191 AVFrame *f; ///< frame the reference belongs to
192 int parity; ///< first line of the reference field inside it
193 } ref[4];
194 int ref_base[2]; ///< first entry of the forward and the backward list
195 int dist[4]; ///< BlockDistance of every reference, clause 9.4.6.1
196 int low_delay;
197 int progressive_seq;
198 int profile, level;
199 int aspect_ratio;
200 int mb_width, mb_height;
201 int width, height;
202 int stream_revision; ///<0 for samples from 2006, 1 for rm52j encoder
203 int progressive;
204 int pic_structure;
205 int top_field_first; ///< first coded field is the top one, clause 7.2.3
206 int b_field_enhanced; ///< BFieldEnhanced of the picture, clause 9.9.1 b)
207 int field; ///< field of the current MB, 0 or 1, clause 3.24
208 int field_mby; ///< macroblock row of the current macroblock in it
209 int skip_mode_flag; ///< select between skip_count or one skip_flag per MB
210 int aec_enable; ///< the macroblock layer uses AEC (AVS1-P16)
211 int aec_start; ///< byte offset of the current AEC slice's payload
212 CAVSAECContext aec; ///< AEC decoder state and contexts, clause 8.4
213 int prev_delta_qp; ///< PreviousDeltaQP of clause 9.4.8, an AEC context
214 int left_cbp; ///< MbCBP of the macroblock to the left
215 uint8_t *top_cbp; ///< MbCBP of the macroblock above, one per column
216 int left_mb_type; ///< MbType to the left, clause 8.4.4.2 b)
217 uint8_t *top_mb_type; ///< MbType above, one per column
218 int left_c_pred_mode; ///< IntraChromaPredMode to the left, 8.4.4.2 e)
219 uint8_t *top_c_pred_mode; ///< IntraChromaPredMode above, per column
220 int loop_filter_disable;
221 int alpha_offset, beta_offset;
222 int ref_flag;
223 int mbx, mby, mbidx; ///< macroblock coordinates
224 int flags; ///< availability flags of neighbouring macroblocks
225 int stc; ///< last start code
226 int pic_data_end; ///< a unit that is not a slice ended the picture's data
227 uint8_t *cy, *cu, *cv; ///< current MB sample pointers
228 int left_qp;
229 uint8_t *top_qp;
230
231 /** mv motion vector cache
232 0: D3 B2 B3 C2
233 4: A1 X0 X1 -
234 8: A3 X2 X3 -
235
236 X are the vectors in the current macroblock (5,6,9,10)
237 A is the macroblock to the left (4,8)
238 B is the macroblock to the top (1,2)
239 C is the macroblock to the top-right (3)
240 D is the macroblock to the top-left (0)
241
242 the same is repeated for backward motion vectors */
243 DECLARE_ALIGNED(8, cavs_vector, mv)[2*4*3];
244 /** mv_diff of the same blocks, for the ctxIdxInc of clause 8.4.4.2 g) */
245 int16_t mvd[2*4*3][2];
246 cavs_vector *top_mv[2];
247 cavs_vector *col_mv;
248
249 /** luma pred mode cache
250 0: -- B2 B3
251 3: A1 X0 X1
252 6: A3 X2 X3 */
253 int pred_mode_Y[3*3];
254 int *top_pred_Y;
255 ptrdiff_t l_stride, c_stride;
256 int luma_scan[4];
257 int qp;
258 int qp_fixed;
259 int pic_qp_fixed;
260 int cbp;
261 DECLARE_ALIGNED(32, int16_t, block)[64];
262 uint8_t permutated_scantable[64];
263 /** inverse block scan method 2 of clause 9.5.3 c), for field pictures */
264 uint8_t permutated_scantable_field[64];
265 /** the one of the two the current picture uses, selected in decode_pic() */
266 const uint8_t *scantable;
267
268 /** intra prediction is done with un-deblocked samples
269 they are saved here before deblocking the MB */
270 uint8_t *top_border_y, *top_border_u, *top_border_v;
271 uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
272 uint8_t intern_border_y[26];
273 uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
274
275 void (*intra_pred_l[8])(uint8_t *d, uint8_t *top, uint8_t *left, ptrdiff_t stride);
276 void (*intra_pred_c[7])(uint8_t *d, uint8_t *top, uint8_t *left, ptrdiff_t stride);
277 uint8_t *col_type_base;
278
279 /** 512 / dist[], for scaling neighbouring MVs (clause 9.4.6.2 step 3) */
280 int scale_den[4];
281
282 /**
283 * 16384 / BlockDistanceRef of clause 9.9.1 b) 3), indexed by the
284 * co-located block's own distance, which is 9 bits (clause 9.4.6.1)
285 */
286 uint16_t direct_den[512];
287
288 uint8_t *edge_emu_buffer;
289
290 /**
291 * picture payload with the annex A pseudo start codes removed,
292 * allocated only for the pictures that carry one
293 */
294 uint8_t *deemulated_buf;
295 unsigned int deemulated_buf_size;
296 /**
297 * byte offsets in deemulated_buf at which a start code prefix of the
298 * original payload begins
299 */
300 uint32_t *stc_offset;
301 unsigned int stc_offset_size;
302 int nb_stc_offset;
303
304 int got_keyframe;
305 } AVSContext;
306
307 extern const uint8_t ff_cavs_chroma_qp[64];
308 extern const uint8_t ff_cavs_partition_flags[30];
309
310 /** mark block as using intra prediction */
311 #define CAVS_DIR_MV (cavs_vector){ 0, 0, 1, REF_DIR, 0 }
312 /** mark block as "no prediction from this direction"
313 e.g. forward motion vector in BWD partition */
314 #define CAVS_INTRA_MV (cavs_vector){ 0, 0, 1, REF_INTRA, 0 }
315
316 1171563 static inline void set_mvs(cavs_vector *mv, enum cavs_block size) {
317
4/4
✓ Branch 2 → 3 taken 1006355 times.
✓ Branch 2 → 4 taken 32696 times.
✓ Branch 2 → 5 taken 28916 times.
✓ Branch 2 → 6 taken 103596 times.
1171563 switch(size) {
318 1006355 case BLK_16X16:
319 1006355 mv[MV_STRIDE ] = mv[0];
320 1006355 mv[MV_STRIDE+1] = mv[0];
321 av_fallthrough;
322 1039051 case BLK_16X8:
323 1039051 mv[1] = mv[0];
324 1039051 break;
325 28916 case BLK_8X16:
326 28916 mv[MV_STRIDE] = mv[0];
327 28916 break;
328 }
329 1171563 }
330
331 /** replicate one partition's mv_diff over the 8x8 blocks it covers */
332 240950 static inline void set_mvds(int16_t (*mvd)[2], enum cavs_block size)
333 {
334
4/4
✓ Branch 2 → 3 taken 101086 times.
✓ Branch 2 → 4 taken 28018 times.
✓ Branch 2 → 5 taken 24906 times.
✓ Branch 2 → 6 taken 86940 times.
240950 switch (size) {
335 101086 case BLK_16X16:
336 101086 memcpy(mvd[MV_STRIDE ], mvd[0], sizeof(*mvd));
337 101086 memcpy(mvd[MV_STRIDE+1], mvd[0], sizeof(*mvd));
338 av_fallthrough;
339 129104 case BLK_16X8:
340 129104 memcpy(mvd[1], mvd[0], sizeof(*mvd));
341 129104 break;
342 24906 case BLK_8X16:
343 24906 memcpy(mvd[MV_STRIDE], mvd[0], sizeof(*mvd));
344 24906 break;
345 }
346 240950 }
347
348 void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type);
349 void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left,
350 int block);
351 void ff_cavs_load_intra_pred_chroma(AVSContext *h);
352 void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv);
353 void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type);
354 void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC,
355 enum cavs_mv_pred mode, enum cavs_block size, int ref);
356 void ff_cavs_init_mb(AVSContext *h);
357 void ff_cavs_set_mb_row(AVSContext *h);
358 int ff_cavs_next_mb(AVSContext *h);
359 int ff_cavs_init_pic(AVSContext *h);
360 int ff_cavs_init_top_lines(AVSContext *h);
361 int ff_cavs_init(AVCodecContext *avctx);
362 int ff_cavs_end (AVCodecContext *avctx);
363
364 #endif /* AVCODEC_CAVS_H */
365