FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rv34.c
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 932 1043 89.4%
Functions: 43 46 93.5%
Branches: 540 655 82.4%

Line Branch Exec Source
1 /*
2 * RV30/40 decoder common data
3 * Copyright (c) 2007 Mike Melanson, Konstantin Shishkov
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 /**
23 * @file
24 * RV30/40 decoder common data
25 */
26
27 #include "libavutil/avassert.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/mem.h"
31 #include "libavutil/mem_internal.h"
32 #include "libavutil/thread.h"
33
34 #include "avcodec.h"
35 #include "decode.h"
36 #include "error_resilience.h"
37 #include "mpegutils.h"
38 #include "mpegvideo.h"
39 #include "mpegvideodec.h"
40 #include "golomb.h"
41 #include "mathops.h"
42 #include "mpeg_er.h"
43 #include "qpeldsp.h"
44 #include "rectangle.h"
45 #include "thread.h"
46 #include "threadprogress.h"
47
48 #include "rv34vlc.h"
49 #include "rv34data.h"
50 #include "rv34.h"
51
52 457801 static inline void ZERO8x2(void* dst, int stride)
53 {
54 457801 fill_rectangle(dst, 1, 2, stride, 0, 4);
55 457801 fill_rectangle(((uint8_t*)(dst))+4, 1, 2, stride, 0, 4);
56 457801 }
57
58 /** translation of RV30/40 macroblock types to lavc ones */
59 static const int rv34_mb_type_to_lavc[12] = {
60 MB_TYPE_INTRA,
61 MB_TYPE_INTRA16x16 | MB_TYPE_SEPARATE_DC,
62 MB_TYPE_16x16 | MB_TYPE_FORWARD_MV,
63 MB_TYPE_8x8 | MB_TYPE_FORWARD_MV,
64 MB_TYPE_16x16 | MB_TYPE_FORWARD_MV,
65 MB_TYPE_16x16 | MB_TYPE_BACKWARD_MV,
66 MB_TYPE_SKIP,
67 MB_TYPE_DIRECT2 | MB_TYPE_16x16,
68 MB_TYPE_16x8 | MB_TYPE_FORWARD_MV,
69 MB_TYPE_8x16 | MB_TYPE_FORWARD_MV,
70 MB_TYPE_16x16 | MB_TYPE_BIDIR_MV,
71 MB_TYPE_16x16 | MB_TYPE_FORWARD_MV | MB_TYPE_SEPARATE_DC
72 };
73
74
75 static RV34VLC intra_vlcs[NUM_INTRA_TABLES], inter_vlcs[NUM_INTER_TABLES];
76
77 static int rv34_decode_mv(RV34DecContext *r, int block_type);
78
79 /**
80 * @name RV30/40 VLC generating functions
81 * @{
82 */
83
84 static VLCElem table_data[117592];
85
86 /**
87 * Generate VLC from codeword lengths.
88 * @param bits codeword lengths (zeroes are accepted)
89 * @param size length of input data
90 * @param vlc output VLC
91 * @param insyms symbols for input codes (NULL for default ones)
92 * @param num VLC table number (for static initialization)
93 */
94 895 static av_cold void rv34_gen_vlc_ext(const uint8_t *bits, int size, VLC *vlc,
95 const uint8_t *syms, int *offset)
96 {
97 895 int counts[17] = {0}, codes[17];
98 uint16_t cw[MAX_VLC_SIZE];
99 int maxbits;
100
101 av_assert1(size > 0);
102
103
2/2
✓ Branch 0 taken 290320 times.
✓ Branch 1 taken 895 times.
291215 for (int i = 0; i < size; i++)
104 290320 counts[bits[i]]++;
105
106 /* bits[0] is zero for some tables, i.e. syms actually starts at 1.
107 * So we reset it here. The code assigned to this element is 0x00. */
108 895 codes[0] = counts[0] = 0;
109
2/2
✓ Branch 0 taken 14320 times.
✓ Branch 1 taken 895 times.
15215 for (int i = 0; i < 16; i++) {
110 14320 codes[i+1] = (codes[i] + counts[i]) << 1;
111
2/2
✓ Branch 0 taken 8095 times.
✓ Branch 1 taken 6225 times.
14320 if (counts[i])
112 8095 maxbits = i;
113 }
114
2/2
✓ Branch 0 taken 290320 times.
✓ Branch 1 taken 895 times.
291215 for (int i = 0; i < size; i++)
115 290320 cw[i] = codes[bits[i]]++;
116
117 895 vlc->table = &table_data[*offset];
118 895 vlc->table_allocated = FF_ARRAY_ELEMS(table_data) - *offset;
119 895 ff_vlc_init_sparse(vlc, FFMIN(maxbits, 9), size,
120 bits, 1, 1,
121 cw, 2, 2,
122 syms, !!syms, !!syms, VLC_INIT_STATIC_OVERLONG);
123 895 *offset += vlc->table_size;
124 895 }
125
126 555 static av_cold void rv34_gen_vlc(const uint8_t *bits, int size, const VLCElem **vlcp,
127 int *offset)
128 {
129 555 VLC vlc = { 0 };
130 555 rv34_gen_vlc_ext(bits, size, &vlc, NULL, offset);
131 555 *vlcp = vlc.table;
132 555 }
133
134 /**
135 * Initialize all tables.
136 */
137 5 static av_cold void rv34_init_tables(void)
138 {
139 5 int i, j, k, offset = 0;
140
141
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5 times.
30 for(i = 0; i < NUM_INTRA_TABLES; i++){
142
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 25 times.
75 for(j = 0; j < 2; j++){
143 50 rv34_gen_vlc(rv34_table_intra_cbppat [i][j], CBPPAT_VLC_SIZE,
144 &intra_vlcs[i].cbppattern[j], &offset);
145 50 rv34_gen_vlc(rv34_table_intra_secondpat[i][j], OTHERBLK_VLC_SIZE,
146 &intra_vlcs[i].second_pattern[j], &offset);
147 50 rv34_gen_vlc(rv34_table_intra_thirdpat [i][j], OTHERBLK_VLC_SIZE,
148 &intra_vlcs[i].third_pattern[j], &offset);
149
2/2
✓ Branch 0 taken 200 times.
✓ Branch 1 taken 50 times.
250 for(k = 0; k < 4; k++){
150 200 rv34_gen_vlc_ext(rv34_table_intra_cbp[i][j+k*2], CBP_VLC_SIZE,
151 &intra_vlcs[i].cbp[j][k], rv34_cbp_code, &offset);
152 }
153 }
154
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 25 times.
125 for(j = 0; j < 4; j++){
155 100 rv34_gen_vlc(rv34_table_intra_firstpat[i][j], FIRSTBLK_VLC_SIZE,
156 &intra_vlcs[i].first_pattern[j], &offset);
157 }
158 25 rv34_gen_vlc(rv34_intra_coeff[i], COEFF_VLC_SIZE,
159 &intra_vlcs[i].coefficient, &offset);
160 }
161
162
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 5 times.
40 for(i = 0; i < NUM_INTER_TABLES; i++){
163 35 rv34_gen_vlc(rv34_inter_cbppat[i], CBPPAT_VLC_SIZE,
164 &inter_vlcs[i].cbppattern[0], &offset);
165
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 35 times.
175 for(j = 0; j < 4; j++){
166 140 rv34_gen_vlc_ext(rv34_inter_cbp[i][j], CBP_VLC_SIZE,
167 &inter_vlcs[i].cbp[0][j], rv34_cbp_code, &offset);
168 }
169
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 35 times.
105 for(j = 0; j < 2; j++){
170 70 rv34_gen_vlc(rv34_table_inter_firstpat [i][j], FIRSTBLK_VLC_SIZE,
171 &inter_vlcs[i].first_pattern[j], &offset);
172 70 rv34_gen_vlc(rv34_table_inter_secondpat[i][j], OTHERBLK_VLC_SIZE,
173 &inter_vlcs[i].second_pattern[j], &offset);
174 70 rv34_gen_vlc(rv34_table_inter_thirdpat [i][j], OTHERBLK_VLC_SIZE,
175 &inter_vlcs[i].third_pattern[j], &offset);
176 }
177 35 rv34_gen_vlc(rv34_inter_coeff[i], COEFF_VLC_SIZE,
178 &inter_vlcs[i].coefficient, &offset);
179 }
180 5 }
181
182 /** @} */ // vlc group
183
184 /**
185 * @name RV30/40 4x4 block decoding functions
186 * @{
187 */
188
189 /**
190 * Decode coded block pattern.
191 */
192 129866 static int rv34_decode_cbp(GetBitContext *gb, const RV34VLC *vlc, int table)
193 {
194 129866 int pattern, code, cbp=0;
195 int ones;
196 static const int cbp_masks[3] = {0x100000, 0x010000, 0x110000};
197 static const int shifts[4] = { 0, 2, 8, 10 };
198 129866 const int *curshift = shifts;
199 int i, t, mask;
200
201 129866 code = get_vlc2(gb, vlc->cbppattern[table], 9, 2);
202 129866 pattern = code & 0xF;
203 129866 code >>= 4;
204
205 129866 ones = rv34_count_ones[pattern];
206
207
2/2
✓ Branch 0 taken 519464 times.
✓ Branch 1 taken 129866 times.
649330 for(mask = 8; mask; mask >>= 1, curshift++){
208
2/2
✓ Branch 0 taken 182302 times.
✓ Branch 1 taken 337162 times.
519464 if(pattern & mask)
209 182302 cbp |= get_vlc2(gb, vlc->cbp[table][ones].table, vlc->cbp[table][ones].bits, 1) << curshift[0];
210 }
211
212
2/2
✓ Branch 0 taken 519464 times.
✓ Branch 1 taken 129866 times.
649330 for(i = 0; i < 4; i++){
213 519464 t = (modulo_three_table[code] >> (6 - 2*i)) & 3;
214
2/2
✓ Branch 0 taken 121745 times.
✓ Branch 1 taken 397719 times.
519464 if(t == 1)
215 121745 cbp |= cbp_masks[get_bits1(gb)] << i;
216
2/2
✓ Branch 0 taken 59551 times.
✓ Branch 1 taken 459913 times.
519464 if(t == 2)
217 59551 cbp |= cbp_masks[2] << i;
218 }
219 129866 return cbp;
220 }
221
222 /**
223 * Get one coefficient value from the bitstream and store it.
224 */
225 2282071 static inline void decode_coeff(int16_t *dst, int coef, int esc, GetBitContext *gb,
226 const VLCElem *vlc, int q)
227 {
228
2/2
✓ Branch 0 taken 1165141 times.
✓ Branch 1 taken 1116930 times.
2282071 if(coef){
229
2/2
✓ Branch 0 taken 159314 times.
✓ Branch 1 taken 1005827 times.
1165141 if(coef == esc){
230 159314 coef = get_vlc2(gb, vlc, 9, 2);
231
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 159175 times.
159314 if(coef > 23){
232 139 coef -= 23;
233 139 coef = 22 + ((1 << coef) | get_bits(gb, coef));
234 }
235 159314 coef += esc;
236 }
237
2/2
✓ Branch 1 taken 575126 times.
✓ Branch 2 taken 590015 times.
1165141 if(get_bits1(gb))
238 575126 coef = -coef;
239 1165141 *dst = (coef*q + 8) >> 4;
240 }
241 2282071 }
242
243 /**
244 * Decode 2x2 subblock of coefficients.
245 */
246 135062 static inline void decode_subblock(int16_t *dst, int code, const int is_block2,
247 GetBitContext *gb, const VLCElem *vlc, int q)
248 {
249 135062 int flags = modulo_three_table[code];
250
251 135062 decode_coeff( dst+0*4+0, (flags >> 6) , 3, gb, vlc, q);
252
2/2
✓ Branch 0 taken 83849 times.
✓ Branch 1 taken 51213 times.
135062 if(is_block2){
253 83849 decode_coeff(dst+1*4+0, (flags >> 4) & 3, 2, gb, vlc, q);
254 83849 decode_coeff(dst+0*4+1, (flags >> 2) & 3, 2, gb, vlc, q);
255 }else{
256 51213 decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q);
257 51213 decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q);
258 }
259 135062 decode_coeff( dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q);
260 135062 }
261
262 /**
263 * Decode a single coefficient.
264 */
265 406803 static inline void decode_subblock1(int16_t *dst, int code, GetBitContext *gb,
266 const VLCElem *vlc, int q)
267 {
268 406803 int coeff = modulo_three_table[code] >> 6;
269 406803 decode_coeff(dst, coeff, 3, gb, vlc, q);
270 406803 }
271
272 333755 static inline void decode_subblock3(int16_t *dst, int code, GetBitContext *gb,
273 const VLCElem *vlc,
274 int q_dc, int q_ac1, int q_ac2)
275 {
276 333755 int flags = modulo_three_table[code];
277
278 333755 decode_coeff(dst+0*4+0, (flags >> 6) , 3, gb, vlc, q_dc);
279 333755 decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q_ac1);
280 333755 decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q_ac1);
281 333755 decode_coeff(dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q_ac2);
282 333755 }
283
284 /**
285 * Decode coefficients for 4x4 block.
286 *
287 * This is done by filling 2x2 subblocks with decoded coefficients
288 * in this order (the same for subblocks and subblock coefficients):
289 * o--o
290 * /
291 * /
292 * o--o
293 */
294
295 740558 static int rv34_decode_block(int16_t *dst, GetBitContext *gb, const RV34VLC *rvlc,
296 int fc, int sc, int q_dc, int q_ac1, int q_ac2)
297 {
298 740558 int code, pattern, has_ac = 1;
299
300 740558 code = get_vlc2(gb, rvlc->first_pattern[fc], 9, 2);
301
302 740558 pattern = code & 0x7;
303
304 740558 code >>= 3;
305
306
2/2
✓ Branch 0 taken 333755 times.
✓ Branch 1 taken 406803 times.
740558 if (modulo_three_table[code] & 0x3F) {
307 333755 decode_subblock3(dst, code, gb, rvlc->coefficient, q_dc, q_ac1, q_ac2);
308 } else {
309 406803 decode_subblock1(dst, code, gb, rvlc->coefficient, q_dc);
310
2/2
✓ Branch 0 taken 365599 times.
✓ Branch 1 taken 41204 times.
406803 if (!pattern)
311 365599 return 0;
312 41204 has_ac = 0;
313 }
314
315
2/2
✓ Branch 0 taken 44969 times.
✓ Branch 1 taken 329990 times.
374959 if(pattern & 4){
316 44969 code = get_vlc2(gb, rvlc->second_pattern[sc], 9, 2);
317 44969 decode_subblock(dst + 4*0+2, code, 0, gb, rvlc->coefficient, q_ac2);
318 }
319
2/2
✓ Branch 0 taken 83849 times.
✓ Branch 1 taken 291110 times.
374959 if(pattern & 2){ // Looks like coefficients 1 and 2 are swapped for this block
320 83849 code = get_vlc2(gb, rvlc->second_pattern[sc], 9, 2);
321 83849 decode_subblock(dst + 4*2+0, code, 1, gb, rvlc->coefficient, q_ac2);
322 }
323
2/2
✓ Branch 0 taken 6244 times.
✓ Branch 1 taken 368715 times.
374959 if(pattern & 1){
324 6244 code = get_vlc2(gb, rvlc->third_pattern[sc], 9, 2);
325 6244 decode_subblock(dst + 4*2+2, code, 0, gb, rvlc->coefficient, q_ac2);
326 }
327 374959 return has_ac | pattern;
328 }
329
330 /**
331 * @name RV30/40 bitstream parsing
332 * @{
333 */
334
335 /**
336 * Decode starting slice position.
337 * @todo Maybe replace with ff_h263_decode_mba() ?
338 */
339 2786 int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
340 {
341 int i;
342
1/2
✓ Branch 0 taken 10432 times.
✗ Branch 1 not taken.
10432 for(i = 0; i < 5; i++)
343
2/2
✓ Branch 0 taken 2786 times.
✓ Branch 1 taken 7646 times.
10432 if(rv34_mb_max_sizes[i] >= mb_size - 1)
344 2786 break;
345 2786 return get_bits(gb, rv34_mb_bits_sizes[i]);
346 }
347
348 /**
349 * Select VLC set for decoding from current quantizer, modifier and frame type.
350 */
351 130964 static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
352 {
353
4/4
✓ Branch 0 taken 60927 times.
✓ Branch 1 taken 70037 times.
✓ Branch 2 taken 60450 times.
✓ Branch 3 taken 477 times.
130964 if(mod == 2 && quant < 19) quant += 10;
354
3/4
✓ Branch 0 taken 24464 times.
✓ Branch 1 taken 46050 times.
✓ Branch 2 taken 24464 times.
✗ Branch 3 not taken.
70514 else if(mod && quant < 26) quant += 5;
355 av_assert2(quant >= 0 && quant < 32);
356 98400 return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][quant]]
357
2/2
✓ Branch 0 taken 98400 times.
✓ Branch 1 taken 32564 times.
130964 : &intra_vlcs[rv34_quant_to_vlc_set[0][quant]];
358 }
359
360 /**
361 * Decode intra macroblock header and return CBP in case of success, -1 otherwise.
362 */
363 7640 static int rv34_decode_intra_mb_header(RV34DecContext *r, int8_t *intra_types)
364 {
365 7640 MpegEncContext *s = &r->s;
366 7640 GetBitContext *const gb = &r->gb;
367 7640 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
368 int t;
369
370 7640 r->is16 = get_bits1(gb);
371
2/2
✓ Branch 0 taken 5830 times.
✓ Branch 1 taken 1810 times.
7640 if(r->is16){
372 5830 s->cur_pic.mb_type[mb_pos] = MB_TYPE_INTRA16x16;
373 5830 r->block_type = RV34_MB_TYPE_INTRA16x16;
374 5830 t = get_bits(gb, 2);
375 5830 fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
376 5830 r->luma_vlc = 2;
377 }else{
378
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 1343 times.
1810 if(!r->rv30){
379
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 467 times.
467 if(!get_bits1(gb))
380 av_log(s->avctx, AV_LOG_ERROR, "Need DQUANT\n");
381 }
382 1810 s->cur_pic.mb_type[mb_pos] = MB_TYPE_INTRA;
383 1810 r->block_type = RV34_MB_TYPE_INTRA;
384
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1810 times.
1810 if(r->decode_intra_types(r, gb, intra_types) < 0)
385 return -1;
386 1810 r->luma_vlc = 1;
387 }
388
389 7640 r->chroma_vlc = 0;
390 7640 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
391
392 7640 return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
393 }
394
395 /**
396 * Decode inter macroblock header and return CBP in case of success, -1 otherwise.
397 */
398 286800 static int rv34_decode_inter_mb_header(RV34DecContext *r, int8_t *intra_types)
399 {
400 286800 MpegEncContext *s = &r->s;
401 286800 GetBitContext *const gb = &r->gb;
402 286800 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
403 int i, t;
404
405 286800 r->block_type = r->decode_mb_info(r);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286800 times.
286800 if(r->block_type == -1)
407 return -1;
408 286800 s->cur_pic.mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
409 286800 r->mb_type[mb_pos] = r->block_type;
410
2/2
✓ Branch 0 taken 164574 times.
✓ Branch 1 taken 122226 times.
286800 if(r->block_type == RV34_MB_SKIP){
411
2/2
✓ Branch 0 taken 31715 times.
✓ Branch 1 taken 132859 times.
164574 if(s->pict_type == AV_PICTURE_TYPE_P)
412 31715 r->mb_type[mb_pos] = RV34_MB_P_16x16;
413
2/2
✓ Branch 0 taken 132859 times.
✓ Branch 1 taken 31715 times.
164574 if(s->pict_type == AV_PICTURE_TYPE_B)
414 132859 r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
415 }
416 286800 r->is16 = !!IS_INTRA16x16(s->cur_pic.mb_type[mb_pos]);
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 286800 times.
286800 if (rv34_decode_mv(r, r->block_type) < 0)
418 return -1;
419
2/2
✓ Branch 0 taken 164574 times.
✓ Branch 1 taken 122226 times.
286800 if(r->block_type == RV34_MB_SKIP){
420 164574 fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
421 164574 return 0;
422 }
423 122226 r->chroma_vlc = 1;
424 122226 r->luma_vlc = 0;
425
426
2/2
✓ Branch 0 taken 24375 times.
✓ Branch 1 taken 97851 times.
122226 if (IS_INTRA(s->cur_pic.mb_type[mb_pos])) {
427
2/2
✓ Branch 0 taken 11056 times.
✓ Branch 1 taken 13319 times.
24375 if(r->is16){
428 11056 t = get_bits(gb, 2);
429 11056 fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
430 11056 r->luma_vlc = 2;
431 }else{
432
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13319 times.
13319 if(r->decode_intra_types(r, gb, intra_types) < 0)
433 return -1;
434 13319 r->luma_vlc = 1;
435 }
436 24375 r->chroma_vlc = 0;
437 24375 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
438 }else{
439
2/2
✓ Branch 0 taken 1565616 times.
✓ Branch 1 taken 97851 times.
1663467 for(i = 0; i < 16; i++)
440 1565616 intra_types[(i & 3) + (i>>2) * r->intra_types_stride] = 0;
441 97851 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
442
2/2
✓ Branch 0 taken 549 times.
✓ Branch 1 taken 97302 times.
97851 if(r->mb_type[mb_pos] == RV34_MB_P_MIX16x16){
443 549 r->is16 = 1;
444 549 r->chroma_vlc = 1;
445 549 r->luma_vlc = 2;
446 549 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
447 }
448 }
449
450 122226 return rv34_decode_cbp(gb, r->cur_vlcs, r->is16);
451 }
452
453 /** @} */ //bitstream functions
454
455 /**
456 * @name motion vector related code (prediction, reconstruction, motion compensation)
457 * @{
458 */
459
460 /** macroblock partition width in 8x8 blocks */
461 static const uint8_t part_sizes_w[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2 };
462
463 /** macroblock partition height in 8x8 blocks */
464 static const uint8_t part_sizes_h[RV34_MB_TYPES] = { 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2 };
465
466 /** availability index for subblocks */
467 static const uint8_t avail_indexes[4] = { 6, 7, 10, 11 };
468
469 /**
470 * motion vector prediction
471 *
472 * Motion prediction performed for the block by using median prediction of
473 * motion vectors from the left, top and right top blocks but in corner cases
474 * some other vectors may be used instead.
475 */
476 42942 static void rv34_pred_mv(RV34DecContext *r, int block_type, int subblock_no, int dmv_no)
477 {
478 42942 MpegEncContext *s = &r->s;
479 42942 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
480 42942 int A[2] = {0}, B[2], C[2];
481 int i, j;
482 int mx, my;
483 42942 int* avail = r->avail_cache + avail_indexes[subblock_no];
484 42942 int c_off = part_sizes_w[block_type];
485 42942 int16_t (*motion_val)[2] = s->cur_pic.motion_val[0];
486
487 42942 mv_pos += (subblock_no & 1) + (subblock_no >> 1)*s->b8_stride;
488
2/2
✓ Branch 0 taken 4544 times.
✓ Branch 1 taken 38398 times.
42942 if(subblock_no == 3)
489 4544 c_off = -1;
490
491
2/2
✓ Branch 0 taken 41455 times.
✓ Branch 1 taken 1487 times.
42942 if(avail[-1]){
492 41455 A[0] = motion_val[mv_pos-1][0];
493 41455 A[1] = motion_val[mv_pos-1][1];
494 }
495
2/2
✓ Branch 0 taken 30578 times.
✓ Branch 1 taken 12364 times.
42942 if(avail[-4]){
496 30578 B[0] = motion_val[mv_pos-s->b8_stride][0];
497 30578 B[1] = motion_val[mv_pos-s->b8_stride][1];
498 }else{
499 12364 B[0] = A[0];
500 12364 B[1] = A[1];
501 }
502
2/2
✓ Branch 0 taken 14048 times.
✓ Branch 1 taken 28894 times.
42942 if(!avail[c_off-4]){
503
5/6
✓ Branch 0 taken 1883 times.
✓ Branch 1 taken 12165 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 1831 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 52 times.
14048 if(avail[-4] && (avail[-1] || r->rv30)){
504 1831 C[0] = motion_val[mv_pos-s->b8_stride-1][0];
505 1831 C[1] = motion_val[mv_pos-s->b8_stride-1][1];
506 }else{
507 12217 C[0] = A[0];
508 12217 C[1] = A[1];
509 }
510 }else{
511 28894 C[0] = motion_val[mv_pos-s->b8_stride+c_off][0];
512 28894 C[1] = motion_val[mv_pos-s->b8_stride+c_off][1];
513 }
514 42942 mx = mid_pred(A[0], B[0], C[0]);
515 42942 my = mid_pred(A[1], B[1], C[1]);
516 42942 mx += r->dmv[dmv_no][0];
517 42942 my += r->dmv[dmv_no][1];
518
2/2
✓ Branch 0 taken 64988 times.
✓ Branch 1 taken 42942 times.
107930 for(j = 0; j < part_sizes_h[block_type]; j++){
519
2/2
✓ Branch 0 taken 101864 times.
✓ Branch 1 taken 64988 times.
166852 for(i = 0; i < part_sizes_w[block_type]; i++){
520 101864 motion_val[mv_pos + i + j*s->b8_stride][0] = mx;
521 101864 motion_val[mv_pos + i + j*s->b8_stride][1] = my;
522 }
523 }
524 42942 }
525
526 #define GET_PTS_DIFF(a, b) (((a) - (b) + 8192) & 0x1FFF)
527
528 /**
529 * Calculate motion vector component that should be added for direct blocks.
530 */
531 810576 static int calc_add_mv(RV34DecContext *r, int dir, int val)
532 {
533
2/2
✓ Branch 0 taken 405288 times.
✓ Branch 1 taken 405288 times.
810576 int mul = dir ? -r->mv_weight2 : r->mv_weight1;
534
535 810576 return (int)(val * (SUINT)mul + 0x2000) >> 14;
536 }
537
538 /**
539 * Predict motion vector for B-frame macroblock.
540 */
541 47304 static inline void rv34_pred_b_vector(int A[2], int B[2], int C[2],
542 int A_avail, int B_avail, int C_avail,
543 int *mx, int *my)
544 {
545
2/2
✓ Branch 0 taken 34556 times.
✓ Branch 1 taken 12748 times.
47304 if(A_avail + B_avail + C_avail != 3){
546 34556 *mx = A[0] + B[0] + C[0];
547 34556 *my = A[1] + B[1] + C[1];
548
2/2
✓ Branch 0 taken 11663 times.
✓ Branch 1 taken 22893 times.
34556 if(A_avail + B_avail + C_avail == 2){
549 11663 *mx /= 2;
550 11663 *my /= 2;
551 }
552 }else{
553 12748 *mx = mid_pred(A[0], B[0], C[0]);
554 12748 *my = mid_pred(A[1], B[1], C[1]);
555 }
556 47304 }
557
558 /**
559 * motion vector prediction for B-frames
560 */
561 47304 static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
562 {
563 47304 MpegEncContext *s = &r->s;
564 47304 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
565 47304 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
566 47304 int A[2] = { 0 }, B[2] = { 0 }, C[2] = { 0 };
567 47304 int has_A = 0, has_B = 0, has_C = 0;
568 int mx, my;
569 int i, j;
570 47304 MPVWorkPicture *cur_pic = &s->cur_pic;
571
2/2
✓ Branch 0 taken 24486 times.
✓ Branch 1 taken 22818 times.
47304 const int mask = dir ? MB_TYPE_BACKWARD_MV : MB_TYPE_FORWARD_MV;
572 47304 int type = cur_pic->mb_type[mb_pos];
573
574
2/2
✓ Branch 0 taken 29735 times.
✓ Branch 1 taken 17569 times.
47304 if((r->avail_cache[6-1] & type) & mask){
575 29735 A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
576 29735 A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
577 29735 has_A = 1;
578 }
579
2/2
✓ Branch 0 taken 24178 times.
✓ Branch 1 taken 23126 times.
47304 if((r->avail_cache[6-4] & type) & mask){
580 24178 B[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][0];
581 24178 B[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride][1];
582 24178 has_B = 1;
583 }
584
4/4
✓ Branch 0 taken 40744 times.
✓ Branch 1 taken 6560 times.
✓ Branch 2 taken 22080 times.
✓ Branch 3 taken 18664 times.
47304 if(r->avail_cache[6-4] && (r->avail_cache[6-2] & type) & mask){
585 22080 C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][0];
586 22080 C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride + 2][1];
587 22080 has_C = 1;
588
4/4
✓ Branch 0 taken 918 times.
✓ Branch 1 taken 24306 times.
✓ Branch 2 taken 391 times.
✓ Branch 3 taken 527 times.
25224 }else if((s->mb_x+1) == s->mb_width && (r->avail_cache[6-5] & type) & mask){
589 391 C[0] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][0];
590 391 C[1] = cur_pic->motion_val[dir][mv_pos - s->b8_stride - 1][1];
591 391 has_C = 1;
592 }
593
594 47304 rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
595
596 47304 mx += r->dmv[dir][0];
597 47304 my += r->dmv[dir][1];
598
599
2/2
✓ Branch 0 taken 94608 times.
✓ Branch 1 taken 47304 times.
141912 for(j = 0; j < 2; j++){
600
2/2
✓ Branch 0 taken 189216 times.
✓ Branch 1 taken 94608 times.
283824 for(i = 0; i < 2; i++){
601 189216 cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
602 189216 cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
603 }
604 }
605
4/4
✓ Branch 0 taken 32129 times.
✓ Branch 1 taken 15175 times.
✓ Branch 2 taken 13507 times.
✓ Branch 3 taken 18622 times.
47304 if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD){
606 28682 ZERO8x2(cur_pic->motion_val[!dir][mv_pos], s->b8_stride);
607 }
608 47304 }
609
610 /**
611 * motion vector prediction - RV3 version
612 */
613 9134 static void rv34_pred_mv_rv3(RV34DecContext *r, int block_type, int dir)
614 {
615 9134 MpegEncContext *s = &r->s;
616 9134 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
617 9134 int A[2] = {0}, B[2], C[2];
618 int i, j, k;
619 int mx, my;
620 9134 int* avail = r->avail_cache + avail_indexes[0];
621
622
2/2
✓ Branch 0 taken 8666 times.
✓ Branch 1 taken 468 times.
9134 if(avail[-1]){
623 8666 A[0] = s->cur_pic.motion_val[0][mv_pos - 1][0];
624 8666 A[1] = s->cur_pic.motion_val[0][mv_pos - 1][1];
625 }
626
2/2
✓ Branch 0 taken 8618 times.
✓ Branch 1 taken 516 times.
9134 if(avail[-4]){
627 8618 B[0] = s->cur_pic.motion_val[0][mv_pos - s->b8_stride][0];
628 8618 B[1] = s->cur_pic.motion_val[0][mv_pos - s->b8_stride][1];
629 }else{
630 516 B[0] = A[0];
631 516 B[1] = A[1];
632 }
633
2/2
✓ Branch 0 taken 950 times.
✓ Branch 1 taken 8184 times.
9134 if(!avail[-4 + 2]){
634
3/4
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 516 times.
✓ Branch 2 taken 434 times.
✗ Branch 3 not taken.
950 if(avail[-4] && (avail[-1])){
635 434 C[0] = s->cur_pic.motion_val[0][mv_pos - s->b8_stride - 1][0];
636 434 C[1] = s->cur_pic.motion_val[0][mv_pos - s->b8_stride - 1][1];
637 }else{
638 516 C[0] = A[0];
639 516 C[1] = A[1];
640 }
641 }else{
642 8184 C[0] = s->cur_pic.motion_val[0][mv_pos - s->b8_stride + 2][0];
643 8184 C[1] = s->cur_pic.motion_val[0][mv_pos - s->b8_stride + 2][1];
644 }
645 9134 mx = mid_pred(A[0], B[0], C[0]);
646 9134 my = mid_pred(A[1], B[1], C[1]);
647 9134 mx += r->dmv[0][0];
648 9134 my += r->dmv[0][1];
649
2/2
✓ Branch 0 taken 18268 times.
✓ Branch 1 taken 9134 times.
27402 for(j = 0; j < 2; j++){
650
2/2
✓ Branch 0 taken 36536 times.
✓ Branch 1 taken 18268 times.
54804 for(i = 0; i < 2; i++){
651
2/2
✓ Branch 0 taken 73072 times.
✓ Branch 1 taken 36536 times.
109608 for(k = 0; k < 2; k++){
652 73072 s->cur_pic.motion_val[k][mv_pos + i + j*s->b8_stride][0] = mx;
653 73072 s->cur_pic.motion_val[k][mv_pos + i + j*s->b8_stride][1] = my;
654 }
655 }
656 }
657 9134 }
658
659 static const int chroma_coeffs[3] = { 0, 3, 5 };
660
661 /**
662 * generic motion compensation function
663 *
664 * @param r decoder context
665 * @param block_type type of the current block
666 * @param xoff horizontal offset from the start of the current block
667 * @param yoff vertical offset from the start of the current block
668 * @param mv_off offset to the motion vector information
669 * @param width width of the current partition in 8x8 blocks
670 * @param height height of the current partition in 8x8 blocks
671 * @param dir motion compensation direction (i.e. from the last or the next reference frame)
672 * @param thirdpel motion vectors are specified in 1/3 of pixel
673 * @param qpel_mc a set of functions used to perform luma motion compensation
674 * @param chroma_mc a set of functions used to perform chroma motion compensation
675 */
676 516215 static inline void rv34_mc(RV34DecContext *r, const int block_type,
677 const int xoff, const int yoff, int mv_off,
678 const int width, const int height, int dir,
679 const int thirdpel, int weighted,
680 qpel_mc_func (*qpel_mc)[16],
681 h264_chroma_mc_func (*chroma_mc))
682 {
683 516215 MpegEncContext *s = &r->s;
684 uint8_t *Y, *U, *V;
685 const uint8_t *srcY, *srcU, *srcV;
686 int dxy, mx, my, umx, umy, lx, ly, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
687 516215 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
688 516215 int is16x16 = 1;
689 516215 int emu = 0;
690 516215 int16_t *motion_val = s->cur_pic.motion_val[dir][mv_pos];
691
692
2/2
✓ Branch 0 taken 127708 times.
✓ Branch 1 taken 388507 times.
516215 if(thirdpel){
693 int chroma_mx, chroma_my;
694 127708 mx = (motion_val[0] + (3 << 24)) / 3 - (1 << 24);
695 127708 my = (motion_val[1] + (3 << 24)) / 3 - (1 << 24);
696 127708 lx = (motion_val[0] + (3 << 24)) % 3;
697 127708 ly = (motion_val[1] + (3 << 24)) % 3;
698 127708 chroma_mx = motion_val[0] / 2;
699 127708 chroma_my = motion_val[1] / 2;
700 127708 umx = (chroma_mx + (3 << 24)) / 3 - (1 << 24);
701 127708 umy = (chroma_my + (3 << 24)) / 3 - (1 << 24);
702 127708 uvmx = chroma_coeffs[(chroma_mx + (3 << 24)) % 3];
703 127708 uvmy = chroma_coeffs[(chroma_my + (3 << 24)) % 3];
704 }else{
705 int cx, cy;
706 388507 mx = motion_val[0] >> 2;
707 388507 my = motion_val[1] >> 2;
708 388507 lx = motion_val[0] & 3;
709 388507 ly = motion_val[1] & 3;
710 388507 cx = motion_val[0] / 2;
711 388507 cy = motion_val[1] / 2;
712 388507 umx = cx >> 2;
713 388507 umy = cy >> 2;
714 388507 uvmx = (cx & 3) << 1;
715 388507 uvmy = (cy & 3) << 1;
716 //due to some flaw RV40 uses the same MC compensation routine for H2V2 and H3V3
717
4/4
✓ Branch 0 taken 34993 times.
✓ Branch 1 taken 353514 times.
✓ Branch 2 taken 7463 times.
✓ Branch 3 taken 27530 times.
388507 if(uvmx == 6 && uvmy == 6)
718 7463 uvmx = uvmy = 4;
719 }
720
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 516215 times.
516215 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
722 /* wait for the referenced mb row to be finished */
723 int mb_row = s->mb_y + ((yoff + my + 5 + 8 * height) >> 4);
724 const ThreadProgress *p = dir ? &s->next_pic.ptr->progress : &s->last_pic.ptr->progress;
725 ff_thread_progress_await(p, mb_row);
726 }
727
728 516215 dxy = ly*4 + lx;
729
2/2
✓ Branch 0 taken 222466 times.
✓ Branch 1 taken 293749 times.
516215 srcY = dir ? s->next_pic.data[0] : s->last_pic.data[0];
730
2/2
✓ Branch 0 taken 222466 times.
✓ Branch 1 taken 293749 times.
516215 srcU = dir ? s->next_pic.data[1] : s->last_pic.data[1];
731
2/2
✓ Branch 0 taken 222466 times.
✓ Branch 1 taken 293749 times.
516215 srcV = dir ? s->next_pic.data[2] : s->last_pic.data[2];
732 516215 src_x = s->mb_x * 16 + xoff + mx;
733 516215 src_y = s->mb_y * 16 + yoff + my;
734 516215 uvsrc_x = s->mb_x * 8 + (xoff >> 1) + umx;
735 516215 uvsrc_y = s->mb_y * 8 + (yoff >> 1) + umy;
736 516215 srcY += src_y * s->linesize + src_x;
737 516215 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
738 516215 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
739
2/4
✓ Branch 0 taken 516215 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 516215 times.
✗ Branch 3 not taken.
516215 if(s->h_edge_pos - (width << 3) < 6 || s->v_edge_pos - (height << 3) < 6 ||
740
6/6
✓ Branch 0 taken 167109 times.
✓ Branch 1 taken 349106 times.
✓ Branch 2 taken 167109 times.
✓ Branch 3 taken 349106 times.
✓ Branch 4 taken 500060 times.
✓ Branch 5 taken 16155 times.
516215 (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4 ||
741
6/6
✓ Branch 0 taken 144092 times.
✓ Branch 1 taken 355968 times.
✓ Branch 2 taken 144092 times.
✓ Branch 3 taken 355968 times.
✓ Branch 4 taken 27762 times.
✓ Branch 5 taken 472298 times.
500060 (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4) {
742 43917 srcY -= 2 + 2*s->linesize;
743 43917 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
744 s->linesize, s->linesize,
745 43917 (width << 3) + 6, (height << 3) + 6,
746 src_x - 2, src_y - 2,
747 s->h_edge_pos, s->v_edge_pos);
748 43917 srcY = s->sc.edge_emu_buffer + 2 + 2*s->linesize;
749 43917 emu = 1;
750 }
751
2/2
✓ Branch 0 taken 231631 times.
✓ Branch 1 taken 284584 times.
516215 if(!weighted){
752 231631 Y = s->dest[0] + xoff + yoff *s->linesize;
753 231631 U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
754 231631 V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
755 }else{
756 284584 Y = r->tmp_b_block_y [dir] + xoff + yoff *s->linesize;
757 284584 U = r->tmp_b_block_uv[dir*2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
758 284584 V = r->tmp_b_block_uv[dir*2+1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
759 }
760
761
2/2
✓ Branch 0 taken 2720 times.
✓ Branch 1 taken 513495 times.
516215 if(block_type == RV34_MB_P_16x8){
762 2720 qpel_mc[1][dxy](Y, srcY, s->linesize);
763 2720 Y += 8;
764 2720 srcY += 8;
765
2/2
✓ Branch 0 taken 4968 times.
✓ Branch 1 taken 508527 times.
513495 }else if(block_type == RV34_MB_P_8x16){
766 4968 qpel_mc[1][dxy](Y, srcY, s->linesize);
767 4968 Y += 8 * s->linesize;
768 4968 srcY += 8 * s->linesize;
769 }
770
6/6
✓ Branch 0 taken 406191 times.
✓ Branch 1 taken 110024 times.
✓ Branch 2 taken 403471 times.
✓ Branch 3 taken 2720 times.
✓ Branch 4 taken 398503 times.
✓ Branch 5 taken 4968 times.
516215 is16x16 = (block_type != RV34_MB_P_8x8) && (block_type != RV34_MB_P_16x8) && (block_type != RV34_MB_P_8x16);
771
2/2
✓ Branch 0 taken 117712 times.
✓ Branch 1 taken 398503 times.
516215 qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
772
2/2
✓ Branch 0 taken 43917 times.
✓ Branch 1 taken 472298 times.
516215 if (emu) {
773 43917 uint8_t *uvbuf = s->sc.edge_emu_buffer;
774
775 43917 s->vdsp.emulated_edge_mc(uvbuf, srcU,
776 s->uvlinesize, s->uvlinesize,
777 43917 (width << 2) + 1, (height << 2) + 1,
778 uvsrc_x, uvsrc_y,
779 43917 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
780 43917 srcU = uvbuf;
781 43917 uvbuf += 9*s->uvlinesize;
782
783 43917 s->vdsp.emulated_edge_mc(uvbuf, srcV,
784 s->uvlinesize, s->uvlinesize,
785 43917 (width << 2) + 1, (height << 2) + 1,
786 uvsrc_x, uvsrc_y,
787 43917 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
788 43917 srcV = uvbuf;
789 }
790 516215 chroma_mc[2-width] (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
791 516215 chroma_mc[2-width] (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
792 516215 }
793
794 112473 static void rv34_mc_1mv(RV34DecContext *r, const int block_type,
795 const int xoff, const int yoff, int mv_off,
796 const int width, const int height, int dir)
797 {
798 112473 rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30, 0,
799 112473 r->rdsp.put_pixels_tab,
800 112473 r->rdsp.put_chroma_pixels_tab);
801 112473 }
802
803 117119 static void rv4_weight(RV34DecContext *r)
804 {
805 117119 r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][0](r->s.dest[0],
806 r->tmp_b_block_y[0],
807 r->tmp_b_block_y[1],
808 r->weight1,
809 r->weight2,
810 r->s.linesize);
811 117119 r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[1],
812 r->tmp_b_block_uv[0],
813 r->tmp_b_block_uv[2],
814 r->weight1,
815 r->weight2,
816 r->s.uvlinesize);
817 117119 r->rdsp.rv40_weight_pixels_tab[r->scaled_weight][1](r->s.dest[2],
818 r->tmp_b_block_uv[1],
819 r->tmp_b_block_uv[3],
820 r->weight1,
821 r->weight2,
822 r->s.uvlinesize);
823 117119 }
824
825 155947 static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
826 {
827
5/6
✓ Branch 0 taken 118039 times.
✓ Branch 1 taken 37908 times.
✓ Branch 2 taken 108728 times.
✓ Branch 3 taken 9311 times.
✓ Branch 4 taken 108728 times.
✗ Branch 5 not taken.
155947 int weighted = !r->rv30 && block_type != RV34_MB_B_BIDIR && r->weight1 != 8192;
828
829 155947 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30, weighted,
830 155947 r->rdsp.put_pixels_tab,
831 155947 r->rdsp.put_chroma_pixels_tab);
832
2/2
✓ Branch 0 taken 47219 times.
✓ Branch 1 taken 108728 times.
155947 if(!weighted){
833 47219 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 0,
834 47219 r->rdsp.avg_pixels_tab,
835 47219 r->rdsp.avg_chroma_pixels_tab);
836 }else{
837 108728 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 1,
838 108728 r->rdsp.put_pixels_tab,
839 108728 r->rdsp.put_chroma_pixels_tab);
840 108728 rv4_weight(r);
841 }
842 155947 }
843
844 11481 static void rv34_mc_2mv_skip(RV34DecContext *r)
845 {
846 int i, j;
847
3/4
✓ Branch 0 taken 8391 times.
✓ Branch 1 taken 3090 times.
✓ Branch 2 taken 8391 times.
✗ Branch 3 not taken.
11481 int weighted = !r->rv30 && r->weight1 != 8192;
848
849
2/2
✓ Branch 0 taken 22962 times.
✓ Branch 1 taken 11481 times.
34443 for(j = 0; j < 2; j++)
850
2/2
✓ Branch 0 taken 45924 times.
✓ Branch 1 taken 22962 times.
68886 for(i = 0; i < 2; i++){
851 45924 rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 0, r->rv30,
852 weighted,
853 45924 r->rdsp.put_pixels_tab,
854 45924 r->rdsp.put_chroma_pixels_tab);
855
4/4
✓ Branch 0 taken 33564 times.
✓ Branch 1 taken 12360 times.
✓ Branch 2 taken 33564 times.
✓ Branch 3 taken 12360 times.
45924 rv34_mc(r, RV34_MB_P_8x8, i*8, j*8, i+j*r->s.b8_stride, 1, 1, 1, r->rv30,
856 weighted,
857 weighted ? r->rdsp.put_pixels_tab : r->rdsp.avg_pixels_tab,
858 weighted ? r->rdsp.put_chroma_pixels_tab : r->rdsp.avg_chroma_pixels_tab);
859 }
860
2/2
✓ Branch 0 taken 8391 times.
✓ Branch 1 taken 3090 times.
11481 if(weighted)
861 8391 rv4_weight(r);
862 11481 }
863
864 /** number of motion vectors in each macroblock type */
865 static const int num_mvs[RV34_MB_TYPES] = { 0, 0, 1, 4, 1, 1, 0, 0, 2, 2, 2, 1 };
866
867 /**
868 * Decode motion vector differences
869 * and perform motion vector reconstruction and motion compensation.
870 */
871 286800 static int rv34_decode_mv(RV34DecContext *r, int block_type)
872 {
873 286800 MpegEncContext *s = &r->s;
874 286800 GetBitContext *const gb = &r->gb;
875 int i, j, k, l;
876 286800 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
877 int next_bt;
878
879 286800 memset(r->dmv, 0, sizeof(r->dmv));
880
2/2
✓ Branch 0 taken 99380 times.
✓ Branch 1 taken 286800 times.
386180 for(i = 0; i < num_mvs[block_type]; i++){
881 99380 r->dmv[i][0] = get_interleaved_se_golomb(gb);
882 99380 r->dmv[i][1] = get_interleaved_se_golomb(gb);
883
1/2
✓ Branch 0 taken 99380 times.
✗ Branch 1 not taken.
99380 if (r->dmv[i][0] == INVALID_VLC ||
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99380 times.
99380 r->dmv[i][1] == INVALID_VLC) {
885 r->dmv[i][0] = r->dmv[i][1] = 0;
886 return AVERROR_INVALIDDATA;
887 }
888 }
889
8/9
✓ Branch 0 taken 24375 times.
✓ Branch 1 taken 164574 times.
✓ Branch 2 taken 25258 times.
✓ Branch 3 taken 17078 times.
✓ Branch 4 taken 37816 times.
✓ Branch 5 taken 3844 times.
✓ Branch 6 taken 9311 times.
✓ Branch 7 taken 4544 times.
✗ Branch 8 not taken.
286800 switch(block_type){
890 24375 case RV34_MB_TYPE_INTRA:
891 case RV34_MB_TYPE_INTRA16x16:
892 24375 ZERO8x2(s->cur_pic.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
893 24375 return 0;
894 164574 case RV34_MB_SKIP:
895
2/2
✓ Branch 0 taken 31715 times.
✓ Branch 1 taken 132859 times.
164574 if(s->pict_type == AV_PICTURE_TYPE_P){
896 31715 ZERO8x2(s->cur_pic.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
897 31715 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
898 31715 break;
899 }
900 case RV34_MB_B_DIRECT:
901 //surprisingly, it uses motion scheme from next reference frame
902 /* wait for the current mb row to be finished */
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 158117 times.
158117 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
904 ff_thread_progress_await(&s->next_pic.ptr->progress, FFMAX(0, s->mb_y-1));
905
906 158117 next_bt = s->next_pic.mb_type[s->mb_x + s->mb_y * s->mb_stride];
907
4/4
✓ Branch 0 taken 144391 times.
✓ Branch 1 taken 13726 times.
✓ Branch 2 taken 93730 times.
✓ Branch 3 taken 50661 times.
158117 if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
908 107456 ZERO8x2(s->cur_pic.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
909 107456 ZERO8x2(s->cur_pic.motion_val[1][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
910 }else
911
2/2
✓ Branch 0 taken 101322 times.
✓ Branch 1 taken 50661 times.
151983 for(j = 0; j < 2; j++)
912
2/2
✓ Branch 0 taken 202644 times.
✓ Branch 1 taken 101322 times.
303966 for(i = 0; i < 2; i++)
913
2/2
✓ Branch 0 taken 405288 times.
✓ Branch 1 taken 202644 times.
607932 for(k = 0; k < 2; k++)
914
2/2
✓ Branch 0 taken 810576 times.
✓ Branch 1 taken 405288 times.
1215864 for(l = 0; l < 2; l++)
915 810576 s->cur_pic.motion_val[l][mv_pos + i + j*s->b8_stride][k] = calc_add_mv(r, l, s->next_pic.motion_val[0][mv_pos + i + j*s->b8_stride][k]);
916
6/6
✓ Branch 0 taken 155964 times.
✓ Branch 1 taken 2153 times.
✓ Branch 2 taken 152793 times.
✓ Branch 3 taken 3171 times.
✓ Branch 4 taken 146636 times.
✓ Branch 5 taken 6157 times.
158117 if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt))) //we can use whole macroblock MC
917 146636 rv34_mc_2mv(r, block_type);
918 else
919 11481 rv34_mc_2mv_skip(r);
920 158117 ZERO8x2(s->cur_pic.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
921 158117 break;
922 17078 case RV34_MB_P_16x16:
923 case RV34_MB_P_MIX16x16:
924 17078 rv34_pred_mv(r, block_type, 0, 0);
925 17078 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, 0);
926 17078 break;
927 37816 case RV34_MB_B_FORWARD:
928 case RV34_MB_B_BACKWARD:
929 37816 r->dmv[1][0] = r->dmv[0][0];
930 37816 r->dmv[1][1] = r->dmv[0][1];
931
2/2
✓ Branch 0 taken 9134 times.
✓ Branch 1 taken 28682 times.
37816 if(r->rv30)
932 9134 rv34_pred_mv_rv3(r, block_type, block_type == RV34_MB_B_BACKWARD);
933 else
934 28682 rv34_pred_mv_b (r, block_type, block_type == RV34_MB_B_BACKWARD);
935 37816 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
936 37816 break;
937 3844 case RV34_MB_P_16x8:
938 case RV34_MB_P_8x16:
939 3844 rv34_pred_mv(r, block_type, 0, 0);
940
2/2
✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 2484 times.
3844 rv34_pred_mv(r, block_type, 1 + (block_type == RV34_MB_P_16x8), 1);
941
2/2
✓ Branch 0 taken 1360 times.
✓ Branch 1 taken 2484 times.
3844 if(block_type == RV34_MB_P_16x8){
942 1360 rv34_mc_1mv(r, block_type, 0, 0, 0, 2, 1, 0);
943 1360 rv34_mc_1mv(r, block_type, 0, 8, s->b8_stride, 2, 1, 0);
944 }
945
2/2
✓ Branch 0 taken 2484 times.
✓ Branch 1 taken 1360 times.
3844 if(block_type == RV34_MB_P_8x16){
946 2484 rv34_mc_1mv(r, block_type, 0, 0, 0, 1, 2, 0);
947 2484 rv34_mc_1mv(r, block_type, 8, 0, 1, 1, 2, 0);
948 }
949 3844 break;
950 9311 case RV34_MB_B_BIDIR:
951 9311 rv34_pred_mv_b (r, block_type, 0);
952 9311 rv34_pred_mv_b (r, block_type, 1);
953 9311 rv34_mc_2mv (r, block_type);
954 9311 break;
955 4544 case RV34_MB_P_8x8:
956
2/2
✓ Branch 0 taken 18176 times.
✓ Branch 1 taken 4544 times.
22720 for(i=0;i< 4;i++){
957 18176 rv34_pred_mv(r, block_type, i, i);
958 18176 rv34_mc_1mv (r, block_type, (i&1)<<3, (i&2)<<2, (i&1)+(i>>1)*s->b8_stride, 1, 1, 0);
959 }
960 4544 break;
961 }
962
963 262425 return 0;
964 }
965 /** @} */ // mv group
966
967 /**
968 * @name Macroblock reconstruction functions
969 * @{
970 */
971 /** mapping of RV30/40 intra prediction types to standard H.264 types */
972 static const int ittrans[9] = {
973 DC_PRED, VERT_PRED, HOR_PRED, DIAG_DOWN_RIGHT_PRED, DIAG_DOWN_LEFT_PRED,
974 VERT_RIGHT_PRED, VERT_LEFT_PRED, HOR_UP_PRED, HOR_DOWN_PRED,
975 };
976
977 /** mapping of RV30/40 intra 16x16 prediction types to standard H.264 types */
978 static const int ittrans16[4] = {
979 DC_PRED8x8, VERT_PRED8x8, HOR_PRED8x8, PLANE_PRED8x8,
980 };
981
982 /**
983 * Perform 4x4 intra prediction.
984 */
985 363096 static void rv34_pred_4x4_block(RV34DecContext *r, uint8_t *dst, int stride, int itype, int up, int left, int down, int right)
986 {
987 363096 uint8_t *prev = dst - stride + 4;
988 uint32_t topleft;
989
990
4/4
✓ Branch 0 taken 44632 times.
✓ Branch 1 taken 318464 times.
✓ Branch 2 taken 906 times.
✓ Branch 3 taken 43726 times.
363096 if(!up && !left)
991 906 itype = DC_128_PRED;
992
2/2
✓ Branch 0 taken 43726 times.
✓ Branch 1 taken 318464 times.
362190 else if(!up){
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43726 times.
43726 if(itype == VERT_PRED) itype = HOR_PRED;
994
2/2
✓ Branch 0 taken 25536 times.
✓ Branch 1 taken 18190 times.
43726 if(itype == DC_PRED) itype = LEFT_DC_PRED;
995
2/2
✓ Branch 0 taken 5318 times.
✓ Branch 1 taken 313146 times.
318464 }else if(!left){
996
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5318 times.
5318 if(itype == HOR_PRED) itype = VERT_PRED;
997
2/2
✓ Branch 0 taken 3700 times.
✓ Branch 1 taken 1618 times.
5318 if(itype == DC_PRED) itype = TOP_DC_PRED;
998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5318 times.
5318 if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
999 }
1000
2/2
✓ Branch 0 taken 289785 times.
✓ Branch 1 taken 73311 times.
363096 if(!down){
1001
2/2
✓ Branch 0 taken 16270 times.
✓ Branch 1 taken 273515 times.
289785 if(itype == DIAG_DOWN_LEFT_PRED) itype = DIAG_DOWN_LEFT_PRED_RV40_NODOWN;
1002
2/2
✓ Branch 0 taken 9069 times.
✓ Branch 1 taken 280716 times.
289785 if(itype == HOR_UP_PRED) itype = HOR_UP_PRED_RV40_NODOWN;
1003
2/2
✓ Branch 0 taken 23196 times.
✓ Branch 1 taken 266589 times.
289785 if(itype == VERT_LEFT_PRED) itype = VERT_LEFT_PRED_RV40_NODOWN;
1004 }
1005
4/4
✓ Branch 0 taken 120385 times.
✓ Branch 1 taken 242711 times.
✓ Branch 2 taken 76137 times.
✓ Branch 3 taken 44248 times.
363096 if(!right && up){
1006 76137 topleft = dst[-stride + 3] * 0x01010101u;
1007 76137 prev = (uint8_t*)&topleft;
1008 }
1009 363096 r->h.pred4x4[itype](dst, prev, stride);
1010 363096 }
1011
1012 33772 static inline int adjust_pred16(int itype, int up, int left)
1013 {
1014
4/4
✓ Branch 0 taken 5834 times.
✓ Branch 1 taken 27938 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 5780 times.
33772 if(!up && !left)
1015 54 itype = DC_128_PRED8x8;
1016
2/2
✓ Branch 0 taken 5780 times.
✓ Branch 1 taken 27938 times.
33718 else if(!up){
1017
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5780 times.
5780 if(itype == PLANE_PRED8x8)itype = HOR_PRED8x8;
1018
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5780 times.
5780 if(itype == VERT_PRED8x8) itype = HOR_PRED8x8;
1019
2/2
✓ Branch 0 taken 3780 times.
✓ Branch 1 taken 2000 times.
5780 if(itype == DC_PRED8x8) itype = LEFT_DC_PRED8x8;
1020
2/2
✓ Branch 0 taken 840 times.
✓ Branch 1 taken 27098 times.
27938 }else if(!left){
1021
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 840 times.
840 if(itype == PLANE_PRED8x8)itype = VERT_PRED8x8;
1022
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 840 times.
840 if(itype == HOR_PRED8x8) itype = VERT_PRED8x8;
1023
2/2
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 156 times.
840 if(itype == DC_PRED8x8) itype = TOP_DC_PRED8x8;
1024 }
1025 33772 return itype;
1026 }
1027
1028 708799 static inline void rv34_process_block(RV34DecContext *r,
1029 uint8_t *pdst, int stride,
1030 int fc, int sc, int q_dc, int q_ac)
1031 {
1032 708799 int16_t *const ptr = r->block;
1033 708799 int has_ac = rv34_decode_block(ptr, &r->gb, r->cur_vlcs,
1034 fc, sc, q_dc, q_ac, q_ac);
1035
2/2
✓ Branch 0 taken 352408 times.
✓ Branch 1 taken 356391 times.
708799 if(has_ac){
1036 352408 r->rdsp.rv34_idct_add(pdst, stride, ptr);
1037 }else{
1038 356391 r->rdsp.rv34_idct_dc_add(pdst, stride, ptr[0]);
1039 356391 ptr[0] = 0;
1040 }
1041 708799 }
1042
1043 16886 static void rv34_output_i16x16(RV34DecContext *r, int8_t *intra_types, int cbp)
1044 {
1045 16886 LOCAL_ALIGNED_16(int16_t, block16, [16]);
1046 16886 MpegEncContext *s = &r->s;
1047 16886 GetBitContext *const gb = &r->gb;
1048 16886 int q_dc = rv34_qscale_tab[ r->luma_dc_quant_i[s->qscale] ],
1049 16886 q_ac = rv34_qscale_tab[s->qscale];
1050 16886 uint8_t *dst = s->dest[0];
1051 16886 int16_t *const ptr = r->block;
1052 int i, j, itype, has_ac;
1053
1054 16886 memset(block16, 0, 16 * sizeof(*block16));
1055
1056 16886 has_ac = rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
1057
2/2
✓ Branch 0 taken 7959 times.
✓ Branch 1 taken 8927 times.
16886 if(has_ac)
1058 7959 r->rdsp.rv34_inv_transform(block16);
1059 else
1060 8927 r->rdsp.rv34_inv_transform_dc(block16);
1061
1062 16886 itype = ittrans16[intra_types[0]];
1063 16886 itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
1064 16886 r->h.pred16x16[itype](dst, s->linesize);
1065
1066
2/2
✓ Branch 0 taken 67544 times.
✓ Branch 1 taken 16886 times.
84430 for(j = 0; j < 4; j++){
1067
2/2
✓ Branch 0 taken 270176 times.
✓ Branch 1 taken 67544 times.
337720 for(i = 0; i < 4; i++, cbp >>= 1){
1068 270176 int dc = block16[i + j*4];
1069
1070
2/2
✓ Branch 0 taken 14318 times.
✓ Branch 1 taken 255858 times.
270176 if(cbp & 1){
1071 14318 has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
1072 }else
1073 255858 has_ac = 0;
1074
1075
2/2
✓ Branch 0 taken 14318 times.
✓ Branch 1 taken 255858 times.
270176 if(has_ac){
1076 14318 ptr[0] = dc;
1077 14318 r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
1078 }else
1079 255858 r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
1080 }
1081
1082 67544 dst += 4*s->linesize;
1083 }
1084
1085 16886 itype = ittrans16[intra_types[0]];
1086
2/2
✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 15672 times.
16886 if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
1087 16886 itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
1088
1089 16886 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1090 16886 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1091
1092
2/2
✓ Branch 0 taken 33772 times.
✓ Branch 1 taken 16886 times.
50658 for(j = 1; j < 3; j++){
1093 33772 dst = s->dest[j];
1094 33772 r->h.pred8x8[itype](dst, s->uvlinesize);
1095
2/2
✓ Branch 0 taken 135088 times.
✓ Branch 1 taken 33772 times.
168860 for(i = 0; i < 4; i++, cbp >>= 1){
1096 uint8_t *pdst;
1097
2/2
✓ Branch 0 taken 110307 times.
✓ Branch 1 taken 24781 times.
135088 if(!(cbp & 1)) continue;
1098 24781 pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
1099
1100 24781 rv34_process_block(r, pdst, s->uvlinesize,
1101 r->chroma_vlc, 1, q_dc, q_ac);
1102 }
1103 }
1104 16886 }
1105
1106 15129 static void rv34_output_intra(RV34DecContext *r, int8_t *intra_types, int cbp)
1107 {
1108 15129 MpegEncContext *s = &r->s;
1109 15129 uint8_t *dst = s->dest[0];
1110 15129 int avail[6*8] = {0};
1111 int i, j, k;
1112 int idx, q_ac, q_dc;
1113
1114 // Set neighbour information.
1115
2/2
✓ Branch 0 taken 8929 times.
✓ Branch 1 taken 6200 times.
15129 if(r->avail_cache[1])
1116 8929 avail[0] = 1;
1117
2/2
✓ Branch 0 taken 9550 times.
✓ Branch 1 taken 5579 times.
15129 if(r->avail_cache[2])
1118 9550 avail[1] = avail[2] = 1;
1119
2/2
✓ Branch 0 taken 9550 times.
✓ Branch 1 taken 5579 times.
15129 if(r->avail_cache[3])
1120 9550 avail[3] = avail[4] = 1;
1121
2/2
✓ Branch 0 taken 9514 times.
✓ Branch 1 taken 5615 times.
15129 if(r->avail_cache[4])
1122 9514 avail[5] = 1;
1123
2/2
✓ Branch 0 taken 14351 times.
✓ Branch 1 taken 778 times.
15129 if(r->avail_cache[5])
1124 14351 avail[8] = avail[16] = 1;
1125
2/2
✓ Branch 0 taken 14351 times.
✓ Branch 1 taken 778 times.
15129 if(r->avail_cache[9])
1126 14351 avail[24] = avail[32] = 1;
1127
1128 15129 q_ac = rv34_qscale_tab[s->qscale];
1129
2/2
✓ Branch 0 taken 60516 times.
✓ Branch 1 taken 15129 times.
75645 for(j = 0; j < 4; j++){
1130 60516 idx = 9 + j*8;
1131
2/2
✓ Branch 0 taken 242064 times.
✓ Branch 1 taken 60516 times.
302580 for(i = 0; i < 4; i++, cbp >>= 1, dst += 4, idx++){
1132 242064 rv34_pred_4x4_block(r, dst, s->linesize, ittrans[intra_types[i]], avail[idx-8], avail[idx-1], avail[idx+7], avail[idx-7]);
1133 242064 avail[idx] = 1;
1134
2/2
✓ Branch 0 taken 93479 times.
✓ Branch 1 taken 148585 times.
242064 if(!(cbp & 1)) continue;
1135
1136 148585 rv34_process_block(r, dst, s->linesize,
1137 r->luma_vlc, 0, q_ac, q_ac);
1138 }
1139 60516 dst += s->linesize * 4 - 4*4;
1140 60516 intra_types += r->intra_types_stride;
1141 }
1142
1143 15129 intra_types -= r->intra_types_stride * 4;
1144
1145 15129 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1146 15129 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1147
1148
2/2
✓ Branch 0 taken 30258 times.
✓ Branch 1 taken 15129 times.
45387 for(k = 0; k < 2; k++){
1149 30258 dst = s->dest[1+k];
1150 30258 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 0, 4);
1151
1152
2/2
✓ Branch 0 taken 60516 times.
✓ Branch 1 taken 30258 times.
90774 for(j = 0; j < 2; j++){
1153 60516 int* acache = r->avail_cache + 6 + j*4;
1154
2/2
✓ Branch 0 taken 121032 times.
✓ Branch 1 taken 60516 times.
181548 for(i = 0; i < 2; i++, cbp >>= 1, acache++){
1155 121032 int itype = ittrans[intra_types[i*2+j*2*r->intra_types_stride]];
1156
4/4
✓ Branch 0 taken 60516 times.
✓ Branch 1 taken 60516 times.
✓ Branch 2 taken 30258 times.
✓ Branch 3 taken 30258 times.
121032 rv34_pred_4x4_block(r, dst+4*i, s->uvlinesize, itype, acache[-4], acache[-1], !i && !j, acache[-3]);
1157 121032 acache[0] = 1;
1158
1159
2/2
✓ Branch 0 taken 49869 times.
✓ Branch 1 taken 71163 times.
121032 if(!(cbp&1)) continue;
1160
1161 71163 rv34_process_block(r, dst + 4*i, s->uvlinesize,
1162 r->chroma_vlc, 1, q_dc, q_ac);
1163 }
1164
1165 60516 dst += 4*s->uvlinesize;
1166 }
1167 }
1168 15129 }
1169
1170 2263432 static int is_mv_diff_gt_3(int16_t (*motion_val)[2], int step)
1171 {
1172 int d;
1173 2263432 d = motion_val[0][0] - motion_val[-step][0];
1174
4/4
✓ Branch 0 taken 2170456 times.
✓ Branch 1 taken 92976 times.
✓ Branch 2 taken 96418 times.
✓ Branch 3 taken 2074038 times.
2263432 if(d < -3 || d > 3)
1175 189394 return 1;
1176 2074038 d = motion_val[0][1] - motion_val[-step][1];
1177
4/4
✓ Branch 0 taken 2059609 times.
✓ Branch 1 taken 14429 times.
✓ Branch 2 taken 17585 times.
✓ Branch 3 taken 2042024 times.
2074038 if(d < -3 || d > 3)
1178 32014 return 1;
1179 2042024 return 0;
1180 }
1181
1182 286800 static int rv34_set_deblock_coef(RV34DecContext *r)
1183 {
1184 286800 MpegEncContext *s = &r->s;
1185 286800 int hmvmask = 0, vmvmask = 0, i, j;
1186 286800 int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
1187 286800 int16_t (*motion_val)[2] = &s->cur_pic.motion_val[0][midx];
1188
2/2
✓ Branch 0 taken 573600 times.
✓ Branch 1 taken 286800 times.
860400 for(j = 0; j < 16; j += 8){
1189
2/2
✓ Branch 0 taken 1147200 times.
✓ Branch 1 taken 573600 times.
1720800 for(i = 0; i < 2; i++){
1190
2/2
✓ Branch 1 taken 107029 times.
✓ Branch 2 taken 1040171 times.
1147200 if(is_mv_diff_gt_3(motion_val + i, 1))
1191 107029 vmvmask |= 0x11 << (j + i*2);
1192
6/6
✓ Branch 0 taken 573600 times.
✓ Branch 1 taken 573600 times.
✓ Branch 2 taken 542632 times.
✓ Branch 3 taken 30968 times.
✓ Branch 5 taken 114379 times.
✓ Branch 6 taken 1001853 times.
1147200 if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
1193 114379 hmvmask |= 0x03 << (j + i*2);
1194 }
1195 573600 motion_val += s->b8_stride;
1196 }
1197
2/2
✓ Branch 0 taken 42635 times.
✓ Branch 1 taken 244165 times.
286800 if(s->first_slice_line)
1198 42635 hmvmask &= ~0x000F;
1199
2/2
✓ Branch 0 taken 9180 times.
✓ Branch 1 taken 277620 times.
286800 if(!s->mb_x)
1200 9180 vmvmask &= ~0x1111;
1201
2/2
✓ Branch 0 taken 68640 times.
✓ Branch 1 taken 218160 times.
286800 if(r->rv30){ //RV30 marks both subblocks on the edge for filtering
1202 68640 vmvmask |= (vmvmask & 0x4444) >> 1;
1203 68640 hmvmask |= (hmvmask & 0x0F00) >> 4;
1204
2/2
✓ Branch 0 taken 65520 times.
✓ Branch 1 taken 3120 times.
68640 if(s->mb_x)
1205 65520 r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
1206
2/2
✓ Branch 0 taken 61878 times.
✓ Branch 1 taken 6762 times.
68640 if(!s->first_slice_line)
1207 61878 r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
1208 }
1209 286800 return hmvmask | vmvmask;
1210 }
1211
1212 286800 static int rv34_decode_inter_macroblock(RV34DecContext *r, int8_t *intra_types)
1213 {
1214 286800 MpegEncContext *s = &r->s;
1215 286800 GetBitContext *const gb = &r->gb;
1216 286800 uint8_t *dst = s->dest[0];
1217 286800 int16_t *const ptr = r->block;
1218 286800 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1219 int cbp, cbp2;
1220 int q_dc, q_ac, has_ac;
1221 int i, j;
1222 int dist;
1223
1224 // Calculate which neighbours are available. Maybe it's worth optimizing too.
1225 286800 memset(r->avail_cache, 0, sizeof(r->avail_cache));
1226 286800 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
1227 286800 dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
1228
4/4
✓ Branch 0 taken 277620 times.
✓ Branch 1 taken 9180 times.
✓ Branch 2 taken 277115 times.
✓ Branch 3 taken 505 times.
286800 if(s->mb_x && dist)
1229 277115 r->avail_cache[5] =
1230 277115 r->avail_cache[9] = s->cur_pic.mb_type[mb_pos - 1];
1231
2/2
✓ Branch 0 taken 244165 times.
✓ Branch 1 taken 42635 times.
286800 if(dist >= s->mb_width)
1232 244165 r->avail_cache[2] =
1233 244165 r->avail_cache[3] = s->cur_pic.mb_type[mb_pos - s->mb_stride];
1234
4/4
✓ Branch 0 taken 277620 times.
✓ Branch 1 taken 9180 times.
✓ Branch 2 taken 236751 times.
✓ Branch 3 taken 40869 times.
286800 if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
1235 236751 r->avail_cache[4] = s->cur_pic.mb_type[mb_pos - s->mb_stride + 1];
1236
4/4
✓ Branch 0 taken 277620 times.
✓ Branch 1 taken 9180 times.
✓ Branch 2 taken 235822 times.
✓ Branch 3 taken 41798 times.
286800 if(s->mb_x && dist > s->mb_width)
1237 235822 r->avail_cache[1] = s->cur_pic.mb_type[mb_pos - s->mb_stride - 1];
1238
1239 286800 s->qscale = r->si.quant;
1240 286800 cbp = cbp2 = rv34_decode_inter_mb_header(r, intra_types);
1241 286800 r->cbp_luma [mb_pos] = cbp;
1242 286800 r->cbp_chroma[mb_pos] = cbp >> 16;
1243 286800 r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
1244 286800 s->cur_pic.qscale_table[mb_pos] = s->qscale;
1245
1246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286800 times.
286800 if(cbp == -1)
1247 return -1;
1248
1249
2/2
✓ Branch 0 taken 24375 times.
✓ Branch 1 taken 262425 times.
286800 if (IS_INTRA(s->cur_pic.mb_type[mb_pos])) {
1250
2/2
✓ Branch 0 taken 11056 times.
✓ Branch 1 taken 13319 times.
24375 if(r->is16) rv34_output_i16x16(r, intra_types, cbp);
1251 13319 else rv34_output_intra(r, intra_types, cbp);
1252 24375 return 0;
1253 }
1254
1255
2/2
✓ Branch 0 taken 549 times.
✓ Branch 1 taken 261876 times.
262425 if(r->is16){
1256 // Only for RV34_MB_P_MIX16x16
1257 549 LOCAL_ALIGNED_16(int16_t, block16, [16]);
1258 549 memset(block16, 0, 16 * sizeof(*block16));
1259 549 q_dc = rv34_qscale_tab[ r->luma_dc_quant_p[s->qscale] ];
1260 549 q_ac = rv34_qscale_tab[s->qscale];
1261
2/2
✓ Branch 1 taken 268 times.
✓ Branch 2 taken 281 times.
549 if (rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac))
1262 268 r->rdsp.rv34_inv_transform(block16);
1263 else
1264 281 r->rdsp.rv34_inv_transform_dc(block16);
1265
1266 549 q_ac = rv34_qscale_tab[s->qscale];
1267
1268
2/2
✓ Branch 0 taken 2196 times.
✓ Branch 1 taken 549 times.
2745 for(j = 0; j < 4; j++){
1269
2/2
✓ Branch 0 taken 8784 times.
✓ Branch 1 taken 2196 times.
10980 for(i = 0; i < 4; i++, cbp >>= 1){
1270 8784 int dc = block16[i + j*4];
1271
1272
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8778 times.
8784 if(cbp & 1){
1273 6 has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
1274 }else
1275 8778 has_ac = 0;
1276
1277
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8778 times.
8784 if(has_ac){
1278 6 ptr[0] = dc;
1279 6 r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
1280 }else
1281 8778 r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
1282 }
1283
1284 2196 dst += 4*s->linesize;
1285 }
1286
1287 549 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
1288 }else{
1289 261876 q_ac = rv34_qscale_tab[s->qscale];
1290
1291
2/2
✓ Branch 0 taken 1047504 times.
✓ Branch 1 taken 261876 times.
1309380 for(j = 0; j < 4; j++){
1292
2/2
✓ Branch 0 taken 4190016 times.
✓ Branch 1 taken 1047504 times.
5237520 for(i = 0; i < 4; i++, cbp >>= 1){
1293
2/2
✓ Branch 0 taken 3870649 times.
✓ Branch 1 taken 319367 times.
4190016 if(!(cbp & 1)) continue;
1294
1295 319367 rv34_process_block(r, dst + 4*i, s->linesize,
1296 r->luma_vlc, 0, q_ac, q_ac);
1297 }
1298 1047504 dst += 4*s->linesize;
1299 }
1300 }
1301
1302 262425 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1303 262425 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1304
1305
2/2
✓ Branch 0 taken 524850 times.
✓ Branch 1 taken 262425 times.
787275 for(j = 1; j < 3; j++){
1306 524850 dst = s->dest[j];
1307
2/2
✓ Branch 0 taken 2099400 times.
✓ Branch 1 taken 524850 times.
2624250 for(i = 0; i < 4; i++, cbp >>= 1){
1308 uint8_t *pdst;
1309
2/2
✓ Branch 0 taken 1954497 times.
✓ Branch 1 taken 144903 times.
2099400 if(!(cbp & 1)) continue;
1310 144903 pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
1311
1312 144903 rv34_process_block(r, pdst, s->uvlinesize,
1313 r->chroma_vlc, 1, q_dc, q_ac);
1314 }
1315 }
1316
1317 262425 return 0;
1318 }
1319
1320 7640 static int rv34_decode_intra_macroblock(RV34DecContext *r, int8_t *intra_types)
1321 {
1322 7640 MpegEncContext *s = &r->s;
1323 int cbp, dist;
1324 7640 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1325
1326 // Calculate which neighbours are available. Maybe it's worth optimizing too.
1327 7640 memset(r->avail_cache, 0, sizeof(r->avail_cache));
1328 7640 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
1329 7640 dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
1330
4/4
✓ Branch 0 taken 7352 times.
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 7325 times.
✓ Branch 3 taken 27 times.
7640 if(s->mb_x && dist)
1331 7325 r->avail_cache[5] =
1332 7325 r->avail_cache[9] = s->cur_pic.mb_type[mb_pos - 1];
1333
2/2
✓ Branch 0 taken 6133 times.
✓ Branch 1 taken 1507 times.
7640 if(dist >= s->mb_width)
1334 6133 r->avail_cache[2] =
1335 6133 r->avail_cache[3] = s->cur_pic.mb_type[mb_pos - s->mb_stride];
1336
4/4
✓ Branch 0 taken 7352 times.
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 5928 times.
✓ Branch 3 taken 1424 times.
7640 if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
1337 5928 r->avail_cache[4] = s->cur_pic.mb_type[mb_pos - s->mb_stride + 1];
1338
4/4
✓ Branch 0 taken 7352 times.
✓ Branch 1 taken 288 times.
✓ Branch 2 taken 5878 times.
✓ Branch 3 taken 1474 times.
7640 if(s->mb_x && dist > s->mb_width)
1339 5878 r->avail_cache[1] = s->cur_pic.mb_type[mb_pos - s->mb_stride - 1];
1340
1341 7640 s->qscale = r->si.quant;
1342 7640 cbp = rv34_decode_intra_mb_header(r, intra_types);
1343 7640 r->cbp_luma [mb_pos] = cbp;
1344 7640 r->cbp_chroma[mb_pos] = cbp >> 16;
1345 7640 r->deblock_coefs[mb_pos] = 0xFFFF;
1346 7640 s->cur_pic.qscale_table[mb_pos] = s->qscale;
1347
1348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7640 times.
7640 if(cbp == -1)
1349 return -1;
1350
1351
2/2
✓ Branch 0 taken 5830 times.
✓ Branch 1 taken 1810 times.
7640 if(r->is16){
1352 5830 rv34_output_i16x16(r, intra_types, cbp);
1353 5830 return 0;
1354 }
1355
1356 1810 rv34_output_intra(r, intra_types, cbp);
1357 1810 return 0;
1358 }
1359
1360 295833 static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
1361 {
1362 int bits;
1363
2/2
✓ Branch 0 taken 529 times.
✓ Branch 1 taken 295304 times.
295833 if(s->mb_y >= s->mb_height)
1364 529 return 1;
1365
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 294440 times.
295304 if (!r->mb_num_left)
1366 864 return 1;
1367
2/2
✓ Branch 0 taken 129696 times.
✓ Branch 1 taken 164744 times.
294440 if (r->mb_skip_run > 1)
1368 129696 return 0;
1369 164744 bits = get_bits_left(&r->gb);
1370
4/6
✓ Branch 0 taken 164744 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 169 times.
✓ Branch 3 taken 164575 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 169 times.
164744 if (bits <= 0 || (bits < 8 && !show_bits(&r->gb, bits)))
1371 return 1;
1372 164744 return 0;
1373 }
1374
1375
1376 9 static void rv34_decoder_free(RV34DecContext *r)
1377 {
1378 9 av_freep(&r->intra_types_hist);
1379 9 r->intra_types = NULL;
1380 9 av_freep(&r->tmp_b_block_base);
1381 9 av_freep(&r->mb_type);
1382 9 av_freep(&r->cbp_luma);
1383 9 av_freep(&r->cbp_chroma);
1384 9 av_freep(&r->deblock_coefs);
1385 9 }
1386
1387
1388 9 static int rv34_decoder_alloc(RV34DecContext *r)
1389 {
1390 9 r->intra_types_stride = r->s.mb_width * 4 + 4;
1391
1392 9 r->cbp_chroma = av_mallocz(r->s.mb_stride * r->s.mb_height *
1393 sizeof(*r->cbp_chroma));
1394 9 r->cbp_luma = av_mallocz(r->s.mb_stride * r->s.mb_height *
1395 sizeof(*r->cbp_luma));
1396 9 r->deblock_coefs = av_mallocz(r->s.mb_stride * r->s.mb_height *
1397 sizeof(*r->deblock_coefs));
1398 9 r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
1399 sizeof(*r->intra_types_hist));
1400 9 r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height *
1401 sizeof(*r->mb_type));
1402
1403
3/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
9 if (!(r->cbp_chroma && r->cbp_luma && r->deblock_coefs &&
1404
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 r->intra_types_hist && r->mb_type)) {
1405 r->s.context_reinit = 1;
1406 rv34_decoder_free(r);
1407 return AVERROR(ENOMEM);
1408 }
1409
1410 9 r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
1411
1412 9 return 0;
1413 }
1414
1415
1416 static int rv34_decoder_realloc(RV34DecContext *r)
1417 {
1418 rv34_decoder_free(r);
1419 return rv34_decoder_alloc(r);
1420 }
1421
1422
1423 1393 static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
1424 {
1425 1393 MpegEncContext *s = &r->s;
1426 1393 GetBitContext *const gb = &r->gb;
1427 int mb_pos, slice_type;
1428 int res;
1429
1430 1393 init_get_bits(gb, buf, buf_size*8);
1431 1393 res = r->parse_slice_header(r, gb, &r->si);
1432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1393 times.
1393 if(res < 0){
1433 av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
1434 return -1;
1435 }
1436
1437
2/2
✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 59 times.
1393 slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
1438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1393 times.
1393 if (slice_type != s->pict_type) {
1439 av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
1440 return AVERROR_INVALIDDATA;
1441 }
1442
2/4
✓ Branch 0 taken 1393 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1393 times.
1393 if (s->width != r->si.width || s->height != r->si.height) {
1443 av_log(s->avctx, AV_LOG_ERROR, "Size mismatch\n");
1444 return AVERROR_INVALIDDATA;
1445 }
1446
1447 1393 r->si.end = end;
1448 1393 s->qscale = r->si.quant;
1449 1393 r->mb_num_left = r->si.end - r->si.start;
1450 1393 r->mb_skip_run = 0;
1451
1452 1393 mb_pos = s->mb_x + s->mb_y * s->mb_width;
1453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1393 times.
1393 if(r->si.start != mb_pos){
1454 av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
1455 s->mb_x = r->si.start % s->mb_width;
1456 s->mb_y = r->si.start / s->mb_width;
1457 }
1458 1393 memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
1459 1393 s->first_slice_line = 1;
1460 1393 s->resync_mb_x = s->mb_x;
1461 1393 s->resync_mb_y = s->mb_y;
1462
1463 1393 ff_init_block_index(s);
1464
2/2
✓ Branch 1 taken 294440 times.
✓ Branch 2 taken 1393 times.
295833 while(!check_slice_end(r, s)) {
1465 294440 s->dest[0] += 16;
1466 294440 s->dest[1] += 8;
1467 294440 s->dest[2] += 8;
1468
1469
2/2
✓ Branch 0 taken 286800 times.
✓ Branch 1 taken 7640 times.
294440 if(r->si.type)
1470 286800 res = rv34_decode_inter_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
1471 else
1472 7640 res = rv34_decode_intra_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
1473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 294440 times.
294440 if(res < 0){
1474 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_ERROR);
1475 return -1;
1476 }
1477
2/2
✓ Branch 0 taken 9468 times.
✓ Branch 1 taken 284972 times.
294440 if (++s->mb_x == s->mb_width) {
1478 9468 s->mb_x = 0;
1479 9468 s->mb_y++;
1480 9468 ff_init_block_index(s);
1481
1482 9468 memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
1483 9468 memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
1484
1485
3/4
✓ Branch 0 taken 9468 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8939 times.
✓ Branch 3 taken 529 times.
9468 if(r->loop_filter && s->mb_y >= 2)
1486 8939 r->loop_filter(r, s->mb_y - 2);
1487
1488
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9468 times.
9468 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
1489 ff_thread_progress_report(&s->cur_pic.ptr->progress,
1490 s->mb_y - 2);
1491
1492 }
1493
2/2
✓ Branch 0 taken 9089 times.
✓ Branch 1 taken 285351 times.
294440 if(s->mb_x == s->resync_mb_x)
1494 9089 s->first_slice_line=0;
1495 294440 r->mb_num_left--;
1496 }
1497 1393 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
1498
1499 1393 return s->mb_y == s->mb_height;
1500 }
1501
1502 /** @} */ // reconstruction group end
1503
1504 /**
1505 * Initialize decoder.
1506 */
1507 9 av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
1508 {
1509 static AVOnce init_static_once = AV_ONCE_INIT;
1510 9 RV34DecContext *r = avctx->priv_data;
1511 9 MpegEncContext *s = &r->s;
1512 int ret;
1513
1514 9 ret = ff_mpv_decode_init(s, avctx);
1515
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1516 return ret;
1517 9 s->out_format = FMT_H263;
1518
1519 9 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1520 9 avctx->has_b_frames = 1;
1521 9 s->low_delay = 0;
1522
1523
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = ff_mpv_common_init(s)) < 0)
1524 return ret;
1525
1526 9 ff_h264_pred_init(&r->h, AV_CODEC_ID_RV40, 8, 1);
1527
1528 9 ret = rv34_decoder_alloc(r);
1529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1530 return ret;
1531
1532 9 ff_thread_once(&init_static_once, rv34_init_tables);
1533
1534 9 return 0;
1535 }
1536
1537 int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1538 {
1539 RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
1540 MpegEncContext *const s1 = &r1->s;
1541 int ret;
1542
1543 if (dst == src || !s1->context_initialized)
1544 return 0;
1545
1546 ret = ff_mpeg_update_thread_context(dst, src);
1547 if (ret < 0)
1548 return ret;
1549
1550 // Did ff_mpeg_update_thread_context reinit?
1551 if (ret > 0) {
1552 ret = rv34_decoder_realloc(r);
1553 if (ret < 0)
1554 return ret;
1555 }
1556
1557 r->cur_pts = r1->cur_pts;
1558 r->last_pts = r1->last_pts;
1559 r->next_pts = r1->next_pts;
1560
1561 memset(&r->si, 0, sizeof(r->si));
1562
1563 return 0;
1564 }
1565
1566 4180 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n, int slice_count, int buf_size)
1567 {
1568
2/2
✓ Branch 0 taken 3291 times.
✓ Branch 1 taken 889 times.
4180 if (n < slice_count) {
1569
1/2
✓ Branch 0 taken 3291 times.
✗ Branch 1 not taken.
3291 return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
1570 } else
1571 889 return buf_size;
1572 }
1573
1574 529 static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
1575 {
1576 529 RV34DecContext *r = avctx->priv_data;
1577 529 MpegEncContext *s = &r->s;
1578 529 int got_picture = 0, ret;
1579
1580 529 ff_er_frame_end(&s->er, NULL);
1581 529 ff_mpv_frame_end(s);
1582 529 r->mb_num_left = 0;
1583
1584
2/2
✓ Branch 0 taken 389 times.
✓ Branch 1 taken 140 times.
529 if (s->pict_type == AV_PICTURE_TYPE_B) {
1585
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 389 times.
389 if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0)
1586 return ret;
1587 389 ff_print_debug_info(s, s->cur_pic.ptr, pict);
1588 389 ff_mpv_export_qp_table(s, pict, s->cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
1589 389 got_picture = 1;
1590
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 9 times.
140 } else if (s->last_pic.ptr) {
1591
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 131 times.
131 if ((ret = av_frame_ref(pict, s->last_pic.ptr->f)) < 0)
1592 return ret;
1593 131 ff_print_debug_info(s, s->last_pic.ptr, pict);
1594 131 ff_mpv_export_qp_table(s, pict, s->last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
1595 131 got_picture = 1;
1596 }
1597
1598 529 return got_picture;
1599 }
1600
1601 static AVRational update_sar(int old_w, int old_h, AVRational sar, int new_w, int new_h)
1602 {
1603 // attempt to keep aspect during typical resolution switches
1604 if (!sar.num)
1605 sar = (AVRational){1, 1};
1606
1607 sar = av_mul_q(sar, av_mul_q((AVRational){new_h, new_w}, (AVRational){old_w, old_h}));
1608 return sar;
1609 }
1610
1611 533 int ff_rv34_decode_frame(AVCodecContext *avctx, AVFrame *pict,
1612 int *got_picture_ptr, AVPacket *avpkt)
1613 {
1614 533 const uint8_t *buf = avpkt->data;
1615 533 int buf_size = avpkt->size;
1616 533 RV34DecContext *r = avctx->priv_data;
1617 533 MpegEncContext *s = &r->s;
1618 SliceInfo si;
1619 int i, ret;
1620 int slice_count;
1621 533 const uint8_t *slices_hdr = NULL;
1622 533 int last = 0;
1623 533 int faulty_b = 0;
1624 int offset;
1625
1626 /* no supplementary picture */
1627
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 529 times.
533 if (buf_size == 0) {
1628 /* special case for last picture */
1629
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (s->next_pic.ptr) {
1630
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = av_frame_ref(pict, s->next_pic.ptr->f)) < 0)
1631 return ret;
1632 2 ff_mpv_unref_picture(&s->next_pic);
1633
1634 2 *got_picture_ptr = 1;
1635 }
1636 4 return 0;
1637 }
1638
1639 529 slice_count = (*buf++) + 1;
1640 529 slices_hdr = buf + 4;
1641 529 buf += 8 * slice_count;
1642 529 buf_size -= 1 + 8 * slice_count;
1643
1644 529 offset = get_slice_offset(avctx, slices_hdr, 0, slice_count, buf_size);
1645 //parse first slice header to check whether this frame can be decoded
1646
2/4
✓ Branch 0 taken 529 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 529 times.
529 if(offset < 0 || offset > buf_size){
1647 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1648 return AVERROR_INVALIDDATA;
1649 }
1650 529 init_get_bits(&r->gb, buf+offset, (buf_size-offset)*8);
1651
2/4
✓ Branch 1 taken 529 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 529 times.
529 if (r->parse_slice_header(r, &r->gb, &si) < 0 || si.start) {
1652 av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
1653 return AVERROR_INVALIDDATA;
1654 }
1655
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 516 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
529 if (!s->last_pic.ptr && si.type == AV_PICTURE_TYPE_B) {
1656 av_log(avctx, AV_LOG_ERROR, "Invalid decoder state: B-frame without "
1657 "reference data.\n");
1658 faulty_b = 1;
1659 }
1660
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 529 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
529 if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
1661
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 529 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
529 || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
1662
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 529 times.
529 || avctx->skip_frame >= AVDISCARD_ALL)
1663 return avpkt->size;
1664
1665 /* first slice */
1666
1/2
✓ Branch 0 taken 529 times.
✗ Branch 1 not taken.
529 if (si.start == 0) {
1667
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 529 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
529 if (r->mb_num_left > 0 && s->cur_pic.ptr) {
1668 av_log(avctx, AV_LOG_ERROR, "New frame but still %d MB left.\n",
1669 r->mb_num_left);
1670 if (!s->context_reinit)
1671 ff_er_frame_end(&s->er, NULL);
1672 ff_mpv_frame_end(s);
1673 }
1674
1675
3/6
✓ Branch 0 taken 529 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 529 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 529 times.
529 if (s->width != si.width || s->height != si.height || s->context_reinit) {
1676 int err;
1677
1678 av_log(s->avctx, AV_LOG_WARNING, "Changing dimensions to %dx%d\n",
1679 si.width, si.height);
1680
1681 if (av_image_check_size(si.width, si.height, 0, s->avctx))
1682 return AVERROR_INVALIDDATA;
1683
1684 s->avctx->sample_aspect_ratio = update_sar(
1685 s->width, s->height, s->avctx->sample_aspect_ratio,
1686 si.width, si.height);
1687 s->width = si.width;
1688 s->height = si.height;
1689
1690 err = ff_set_dimensions(s->avctx, s->width, s->height);
1691 if (err < 0)
1692 return err;
1693 if ((err = ff_mpv_common_frame_size_change(s)) < 0)
1694 return err;
1695 if ((err = rv34_decoder_realloc(r)) < 0)
1696 return err;
1697 }
1698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 529 times.
529 if (faulty_b)
1699 return AVERROR_INVALIDDATA;
1700
2/2
✓ Branch 0 taken 511 times.
✓ Branch 1 taken 18 times.
529 s->pict_type = si.type ? si.type : AV_PICTURE_TYPE_I;
1701
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 529 times.
529 if (ff_mpv_frame_start(s, s->avctx) < 0)
1702 return -1;
1703 529 ff_mpeg_er_frame_start(s);
1704
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 520 times.
529 if (!r->tmp_b_block_base) {
1705 int i;
1706
1707 9 r->tmp_b_block_base = av_malloc(s->linesize * 48);
1708
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!r->tmp_b_block_base)
1709 return AVERROR(ENOMEM);
1710
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 for (i = 0; i < 2; i++)
1711 18 r->tmp_b_block_y[i] = r->tmp_b_block_base
1712 18 + i * 16 * s->linesize;
1713
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 9 times.
45 for (i = 0; i < 4; i++)
1714 36 r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
1715 36 + (i >> 1) * 8 * s->uvlinesize
1716 36 + (i & 1) * 16;
1717 }
1718 529 r->cur_pts = si.pts;
1719
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 389 times.
529 if (s->pict_type != AV_PICTURE_TYPE_B) {
1720 140 r->last_pts = r->next_pts;
1721 140 r->next_pts = r->cur_pts;
1722 } else {
1723 389 int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
1724 389 int dist0 = GET_PTS_DIFF(r->cur_pts, r->last_pts);
1725 389 int dist1 = GET_PTS_DIFF(r->next_pts, r->cur_pts);
1726
1727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 389 times.
389 if(!refdist){
1728 r->mv_weight1 = r->mv_weight2 = r->weight1 = r->weight2 = 8192;
1729 r->scaled_weight = 0;
1730 }else{
1731
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 389 times.
389 if (FFMAX(dist0, dist1) > refdist)
1732 av_log(avctx, AV_LOG_TRACE, "distance overflow\n");
1733
1734 389 r->mv_weight1 = (dist0 << 14) / refdist;
1735 389 r->mv_weight2 = (dist1 << 14) / refdist;
1736
2/2
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 38 times.
389 if((r->mv_weight1|r->mv_weight2) & 511){
1737 351 r->weight1 = r->mv_weight1;
1738 351 r->weight2 = r->mv_weight2;
1739 351 r->scaled_weight = 0;
1740 }else{
1741 38 r->weight1 = r->mv_weight1 >> 9;
1742 38 r->weight2 = r->mv_weight2 >> 9;
1743 38 r->scaled_weight = 1;
1744 }
1745 }
1746 }
1747 529 s->mb_x = s->mb_y = 0;
1748 529 ff_thread_finish_setup(s->avctx);
1749 } else if (s->context_reinit) {
1750 av_log(s->avctx, AV_LOG_ERROR, "Decoder needs full frames to "
1751 "reinitialize (start MB is %d).\n", si.start);
1752 return AVERROR_INVALIDDATA;
1753 } else if (HAVE_THREADS &&
1754 (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
1755 av_log(s->avctx, AV_LOG_ERROR, "Decoder needs full frames in frame "
1756 "multithreading mode (start MB is %d).\n", si.start);
1757 return AVERROR_INVALIDDATA;
1758 }
1759
1760
1/2
✓ Branch 0 taken 1393 times.
✗ Branch 1 not taken.
1393 for(i = 0; i < slice_count; i++){
1761 1393 int offset = get_slice_offset(avctx, slices_hdr, i , slice_count, buf_size);
1762 1393 int offset1 = get_slice_offset(avctx, slices_hdr, i+1, slice_count, buf_size);
1763 int size;
1764
1765
3/6
✓ Branch 0 taken 1393 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1393 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1393 times.
1393 if(offset < 0 || offset > offset1 || offset1 > buf_size){
1766 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1767 break;
1768 }
1769 1393 size = offset1 - offset;
1770
1771 1393 r->si.end = s->mb_width * s->mb_height;
1772 1393 r->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
1773
1774
2/2
✓ Branch 0 taken 865 times.
✓ Branch 1 taken 528 times.
1393 if(i+1 < slice_count){
1775 865 int offset2 = get_slice_offset(avctx, slices_hdr, i+2, slice_count, buf_size);
1776
2/4
✓ Branch 0 taken 865 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 865 times.
865 if (offset2 < offset1 || offset2 > buf_size) {
1777 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1778 break;
1779 }
1780 865 init_get_bits(&r->gb, buf+offset1, (buf_size-offset1)*8);
1781
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 864 times.
865 if (r->parse_slice_header(r, &r->gb, &si) < 0) {
1782 1 size = offset2 - offset;
1783 }else
1784 864 r->si.end = si.start;
1785 }
1786
2/4
✓ Branch 0 taken 1393 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1393 times.
1393 av_assert0 (size >= 0 && size <= buf_size - offset);
1787 1393 last = rv34_decode_slice(r, r->si.end, buf + offset, size);
1788
2/2
✓ Branch 0 taken 529 times.
✓ Branch 1 taken 864 times.
1393 if(last)
1789 529 break;
1790 }
1791
1792
1/2
✓ Branch 0 taken 529 times.
✗ Branch 1 not taken.
529 if (s->cur_pic.ptr) {
1793
1/2
✓ Branch 0 taken 529 times.
✗ Branch 1 not taken.
529 if (last) {
1794
1/2
✓ Branch 0 taken 529 times.
✗ Branch 1 not taken.
529 if(r->loop_filter)
1795 529 r->loop_filter(r, s->mb_height - 1);
1796
1797 529 ret = finish_frame(avctx, pict);
1798
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 529 times.
529 if (ret < 0)
1799 return ret;
1800 529 *got_picture_ptr = ret;
1801 } else if (HAVE_THREADS &&
1802 (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
1803 av_log(avctx, AV_LOG_INFO, "marking unfinished frame as finished\n");
1804 /* always mark the current frame as finished, frame-mt supports
1805 * only complete frames */
1806 ff_er_frame_end(&s->er, NULL);
1807 ff_mpv_frame_end(s);
1808 r->mb_num_left = 0;
1809 return AVERROR_INVALIDDATA;
1810 }
1811 }
1812
1813 529 return avpkt->size;
1814 }
1815
1816 9 av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
1817 {
1818 9 RV34DecContext *r = avctx->priv_data;
1819
1820 9 rv34_decoder_free(r);
1821
1822 9 return ff_mpv_decode_close(avctx);
1823 }
1824