FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rv34.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 932 1047 89.0%
Functions: 43 46 93.5%
Branches: 541 663 81.6%

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 456145 static inline void ZERO8x2(void* dst, int stride)
53 {
54 456145 fill_rectangle(dst, 1, 2, stride, 0, 4);
55 456145 fill_rectangle(((uint8_t*)(dst))+4, 1, 2, stride, 0, 4);
56 456145 }
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 129797 static int rv34_decode_cbp(GetBitContext *gb, const RV34VLC *vlc, int table)
193 {
194 129797 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 129797 const int *curshift = shifts;
199 int i, t, mask;
200
201 129797 code = get_vlc2(gb, vlc->cbppattern[table], 9, 2);
202 129797 pattern = code & 0xF;
203 129797 code >>= 4;
204
205 129797 ones = rv34_count_ones[pattern];
206
207
2/2
✓ Branch 0 taken 519188 times.
✓ Branch 1 taken 129797 times.
648985 for(mask = 8; mask; mask >>= 1, curshift++){
208
2/2
✓ Branch 0 taken 182266 times.
✓ Branch 1 taken 336922 times.
519188 if(pattern & mask)
209 182266 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 519188 times.
✓ Branch 1 taken 129797 times.
648985 for(i = 0; i < 4; i++){
213 519188 t = (modulo_three_table[code] >> (6 - 2*i)) & 3;
214
2/2
✓ Branch 0 taken 121686 times.
✓ Branch 1 taken 397502 times.
519188 if(t == 1)
215 121686 cbp |= cbp_masks[get_bits1(gb)] << i;
216
2/2
✓ Branch 0 taken 59528 times.
✓ Branch 1 taken 459660 times.
519188 if(t == 2)
217 59528 cbp |= cbp_masks[2] << i;
218 }
219 129797 return cbp;
220 }
221
222 /**
223 * Get one coefficient value from the bitstream and store it.
224 */
225 2281776 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 1164928 times.
✓ Branch 1 taken 1116848 times.
2281776 if(coef){
229
2/2
✓ Branch 0 taken 159313 times.
✓ Branch 1 taken 1005615 times.
1164928 if(coef == esc){
230 159313 coef = get_vlc2(gb, vlc, 9, 2);
231
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 159174 times.
159313 if(coef > 23){
232 139 coef -= 23;
233 139 coef = 22 + ((1 << coef) | get_bits(gb, coef));
234 }
235 159313 coef += esc;
236 }
237
2/2
✓ Branch 1 taken 575045 times.
✓ Branch 2 taken 589883 times.
1164928 if(get_bits1(gb))
238 575045 coef = -coef;
239 1164928 *dst = (coef*q + 8) >> 4;
240 }
241 2281776 }
242
243 /**
244 * Decode 2x2 subblock of coefficients.
245 */
246 135049 static inline void decode_subblock(int16_t *dst, int code, const int is_block2,
247 GetBitContext *gb, const VLCElem *vlc, int q)
248 {
249 135049 int flags = modulo_three_table[code];
250
251 135049 decode_coeff( dst+0*4+0, (flags >> 6) , 3, gb, vlc, q);
252
2/2
✓ Branch 0 taken 83836 times.
✓ Branch 1 taken 51213 times.
135049 if(is_block2){
253 83836 decode_coeff(dst+1*4+0, (flags >> 4) & 3, 2, gb, vlc, q);
254 83836 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 135049 decode_coeff( dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q);
260 135049 }
261
262 /**
263 * Decode a single coefficient.
264 */
265 406616 static inline void decode_subblock1(int16_t *dst, int code, GetBitContext *gb,
266 const VLCElem *vlc, int q)
267 {
268 406616 int coeff = modulo_three_table[code] >> 6;
269 406616 decode_coeff(dst, coeff, 3, gb, vlc, q);
270 406616 }
271
272 333741 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 333741 int flags = modulo_three_table[code];
277
278 333741 decode_coeff(dst+0*4+0, (flags >> 6) , 3, gb, vlc, q_dc);
279 333741 decode_coeff(dst+0*4+1, (flags >> 4) & 3, 2, gb, vlc, q_ac1);
280 333741 decode_coeff(dst+1*4+0, (flags >> 2) & 3, 2, gb, vlc, q_ac1);
281 333741 decode_coeff(dst+1*4+1, (flags >> 0) & 3, 2, gb, vlc, q_ac2);
282 333741 }
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 740357 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 740357 int code, pattern, has_ac = 1;
299
300 740357 code = get_vlc2(gb, rvlc->first_pattern[fc], 9, 2);
301
302 740357 pattern = code & 0x7;
303
304 740357 code >>= 3;
305
306
2/2
✓ Branch 0 taken 333741 times.
✓ Branch 1 taken 406616 times.
740357 if (modulo_three_table[code] & 0x3F) {
307 333741 decode_subblock3(dst, code, gb, rvlc->coefficient, q_dc, q_ac1, q_ac2);
308 } else {
309 406616 decode_subblock1(dst, code, gb, rvlc->coefficient, q_dc);
310
2/2
✓ Branch 0 taken 365422 times.
✓ Branch 1 taken 41194 times.
406616 if (!pattern)
311 365422 return 0;
312 41194 has_ac = 0;
313 }
314
315
2/2
✓ Branch 0 taken 44969 times.
✓ Branch 1 taken 329966 times.
374935 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 83836 times.
✓ Branch 1 taken 291099 times.
374935 if(pattern & 2){ // Looks like coefficients 1 and 2 are swapped for this block
320 83836 code = get_vlc2(gb, rvlc->second_pattern[sc], 9, 2);
321 83836 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 368691 times.
374935 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 374935 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 2782 int ff_rv34_get_start_offset(GetBitContext *gb, int mb_size)
340 {
341 int i;
342
1/2
✓ Branch 0 taken 10416 times.
✗ Branch 1 not taken.
10416 for(i = 0; i < 5; i++)
343
2/2
✓ Branch 0 taken 2782 times.
✓ Branch 1 taken 7634 times.
10416 if(rv34_mb_max_sizes[i] >= mb_size - 1)
344 2782 break;
345 2782 return rv34_mb_bits_sizes[i];
346 }
347
348 /**
349 * Select VLC set for decoding from current quantizer, modifier and frame type.
350 */
351 130895 static inline RV34VLC* choose_vlc_set(int quant, int mod, int type)
352 {
353
4/4
✓ Branch 0 taken 60858 times.
✓ Branch 1 taken 70037 times.
✓ Branch 2 taken 60381 times.
✓ Branch 3 taken 477 times.
130895 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 98339 return type ? &inter_vlcs[rv34_quant_to_vlc_set[1][quant]]
357
2/2
✓ Branch 0 taken 98339 times.
✓ Branch 1 taken 32556 times.
130895 : &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 *gb = &s->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 286080 static int rv34_decode_inter_mb_header(RV34DecContext *r, int8_t *intra_types)
399 {
400 286080 MpegEncContext *s = &r->s;
401 286080 GetBitContext *gb = &s->gb;
402 286080 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
403 int i, t;
404
405 286080 r->block_type = r->decode_mb_info(r);
406
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286080 times.
286080 if(r->block_type == -1)
407 return -1;
408 286080 s->cur_pic.mb_type[mb_pos] = rv34_mb_type_to_lavc[r->block_type];
409 286080 r->mb_type[mb_pos] = r->block_type;
410
2/2
✓ Branch 0 taken 163923 times.
✓ Branch 1 taken 122157 times.
286080 if(r->block_type == RV34_MB_SKIP){
411
2/2
✓ Branch 0 taken 31715 times.
✓ Branch 1 taken 132208 times.
163923 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 132208 times.
✓ Branch 1 taken 31715 times.
163923 if(s->pict_type == AV_PICTURE_TYPE_B)
414 132208 r->mb_type[mb_pos] = RV34_MB_B_DIRECT;
415 }
416 286080 r->is16 = !!IS_INTRA16x16(s->cur_pic.mb_type[mb_pos]);
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 286080 times.
286080 if (rv34_decode_mv(r, r->block_type) < 0)
418 return -1;
419
2/2
✓ Branch 0 taken 163923 times.
✓ Branch 1 taken 122157 times.
286080 if(r->block_type == RV34_MB_SKIP){
420 163923 fill_rectangle(intra_types, 4, 4, r->intra_types_stride, 0, sizeof(intra_types[0]));
421 163923 return 0;
422 }
423 122157 r->chroma_vlc = 1;
424 122157 r->luma_vlc = 0;
425
426
2/2
✓ Branch 0 taken 24367 times.
✓ Branch 1 taken 97790 times.
122157 if (IS_INTRA(s->cur_pic.mb_type[mb_pos])) {
427
2/2
✓ Branch 0 taken 11048 times.
✓ Branch 1 taken 13319 times.
24367 if(r->is16){
428 11048 t = get_bits(gb, 2);
429 11048 fill_rectangle(intra_types, 4, 4, r->intra_types_stride, t, sizeof(intra_types[0]));
430 11048 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 24367 r->chroma_vlc = 0;
437 24367 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 0);
438 }else{
439
2/2
✓ Branch 0 taken 1564640 times.
✓ Branch 1 taken 97790 times.
1662430 for(i = 0; i < 16; i++)
440 1564640 intra_types[(i & 3) + (i>>2) * r->intra_types_stride] = 0;
441 97790 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 97241 times.
97790 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 122157 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 806848 static int calc_add_mv(RV34DecContext *r, int dir, int val)
532 {
533
2/2
✓ Branch 0 taken 403424 times.
✓ Branch 1 taken 403424 times.
806848 int mul = dir ? -r->mv_weight2 : r->mv_weight1;
534
535 806848 return (int)(val * (SUINT)mul + 0x2000) >> 14;
536 }
537
538 /**
539 * Predict motion vector for B-frame macroblock.
540 */
541 47292 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 34544 times.
✓ Branch 1 taken 12748 times.
47292 if(A_avail + B_avail + C_avail != 3){
546 34544 *mx = A[0] + B[0] + C[0];
547 34544 *my = A[1] + B[1] + C[1];
548
2/2
✓ Branch 0 taken 11663 times.
✓ Branch 1 taken 22881 times.
34544 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 47292 }
557
558 /**
559 * motion vector prediction for B-frames
560 */
561 47292 static void rv34_pred_mv_b(RV34DecContext *r, int block_type, int dir)
562 {
563 47292 MpegEncContext *s = &r->s;
564 47292 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
565 47292 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
566 47292 int A[2] = { 0 }, B[2] = { 0 }, C[2] = { 0 };
567 47292 int has_A = 0, has_B = 0, has_C = 0;
568 int mx, my;
569 int i, j;
570 47292 MPVWorkPicture *cur_pic = &s->cur_pic;
571
2/2
✓ Branch 0 taken 24483 times.
✓ Branch 1 taken 22809 times.
47292 const int mask = dir ? MB_TYPE_BACKWARD_MV : MB_TYPE_FORWARD_MV;
572 47292 int type = cur_pic->mb_type[mb_pos];
573
574
2/2
✓ Branch 0 taken 29730 times.
✓ Branch 1 taken 17562 times.
47292 if((r->avail_cache[6-1] & type) & mask){
575 29730 A[0] = cur_pic->motion_val[dir][mv_pos - 1][0];
576 29730 A[1] = cur_pic->motion_val[dir][mv_pos - 1][1];
577 29730 has_A = 1;
578 }
579
2/2
✓ Branch 0 taken 24178 times.
✓ Branch 1 taken 23114 times.
47292 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 40737 times.
✓ Branch 1 taken 6555 times.
✓ Branch 2 taken 22080 times.
✓ Branch 3 taken 18657 times.
47292 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 24294 times.
✓ Branch 2 taken 391 times.
✓ Branch 3 taken 527 times.
25212 }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 47292 rv34_pred_b_vector(A, B, C, has_A, has_B, has_C, &mx, &my);
595
596 47292 mx += r->dmv[dir][0];
597 47292 my += r->dmv[dir][1];
598
599
2/2
✓ Branch 0 taken 94584 times.
✓ Branch 1 taken 47292 times.
141876 for(j = 0; j < 2; j++){
600
2/2
✓ Branch 0 taken 189168 times.
✓ Branch 1 taken 94584 times.
283752 for(i = 0; i < 2; i++){
601 189168 cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][0] = mx;
602 189168 cur_pic->motion_val[dir][mv_pos + i + j*s->b8_stride][1] = my;
603 }
604 }
605
4/4
✓ Branch 0 taken 32118 times.
✓ Branch 1 taken 15174 times.
✓ Branch 2 taken 13500 times.
✓ Branch 3 taken 18618 times.
47292 if(block_type == RV34_MB_B_BACKWARD || block_type == RV34_MB_B_FORWARD){
606 28674 ZERO8x2(cur_pic->motion_val[!dir][mv_pos], s->b8_stride);
607 }
608 47292 }
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 514595 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 514595 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 514595 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride + mv_off;
688 514595 int is16x16 = 1;
689 514595 int emu = 0;
690 514595 int16_t *motion_val = s->cur_pic.motion_val[dir][mv_pos];
691
692
2/2
✓ Branch 0 taken 127708 times.
✓ Branch 1 taken 386887 times.
514595 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 386887 mx = motion_val[0] >> 2;
707 386887 my = motion_val[1] >> 2;
708 386887 lx = motion_val[0] & 3;
709 386887 ly = motion_val[1] & 3;
710 386887 cx = motion_val[0] / 2;
711 386887 cy = motion_val[1] / 2;
712 386887 umx = cx >> 2;
713 386887 umy = cy >> 2;
714 386887 uvmx = (cx & 3) << 1;
715 386887 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 34910 times.
✓ Branch 1 taken 351977 times.
✓ Branch 2 taken 7463 times.
✓ Branch 3 taken 27447 times.
386887 if(uvmx == 6 && uvmy == 6)
718 7463 uvmx = uvmy = 4;
719 }
720
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 514595 times.
514595 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 514595 dxy = ly*4 + lx;
729
2/2
✓ Branch 0 taken 221659 times.
✓ Branch 1 taken 292936 times.
514595 srcY = dir ? s->next_pic.data[0] : s->last_pic.data[0];
730
2/2
✓ Branch 0 taken 221659 times.
✓ Branch 1 taken 292936 times.
514595 srcU = dir ? s->next_pic.data[1] : s->last_pic.data[1];
731
2/2
✓ Branch 0 taken 221659 times.
✓ Branch 1 taken 292936 times.
514595 srcV = dir ? s->next_pic.data[2] : s->last_pic.data[2];
732 514595 src_x = s->mb_x * 16 + xoff + mx;
733 514595 src_y = s->mb_y * 16 + yoff + my;
734 514595 uvsrc_x = s->mb_x * 8 + (xoff >> 1) + umx;
735 514595 uvsrc_y = s->mb_y * 8 + (yoff >> 1) + umy;
736 514595 srcY += src_y * s->linesize + src_x;
737 514595 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
738 514595 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
739
2/4
✓ Branch 0 taken 514595 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 514595 times.
✗ Branch 3 not taken.
514595 if(s->h_edge_pos - (width << 3) < 6 || s->v_edge_pos - (height << 3) < 6 ||
740
6/6
✓ Branch 0 taken 166878 times.
✓ Branch 1 taken 347717 times.
✓ Branch 2 taken 166878 times.
✓ Branch 3 taken 347717 times.
✓ Branch 4 taken 498480 times.
✓ Branch 5 taken 16115 times.
514595 (unsigned)(src_x - !!lx*2) > s->h_edge_pos - !!lx*2 - (width <<3) - 4 ||
741
6/6
✓ Branch 0 taken 144037 times.
✓ Branch 1 taken 354443 times.
✓ Branch 2 taken 144037 times.
✓ Branch 3 taken 354443 times.
✓ Branch 4 taken 27692 times.
✓ Branch 5 taken 470788 times.
498480 (unsigned)(src_y - !!ly*2) > s->v_edge_pos - !!ly*2 - (height<<3) - 4) {
742 43807 srcY -= 2 + 2*s->linesize;
743 43807 s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, srcY,
744 s->linesize, s->linesize,
745 43807 (width << 3) + 6, (height << 3) + 6,
746 src_x - 2, src_y - 2,
747 s->h_edge_pos, s->v_edge_pos);
748 43807 srcY = s->sc.edge_emu_buffer + 2 + 2*s->linesize;
749 43807 emu = 1;
750 }
751
2/2
✓ Branch 0 taken 231619 times.
✓ Branch 1 taken 282976 times.
514595 if(!weighted){
752 231619 Y = s->dest[0] + xoff + yoff *s->linesize;
753 231619 U = s->dest[1] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
754 231619 V = s->dest[2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
755 }else{
756 282976 Y = r->tmp_b_block_y [dir] + xoff + yoff *s->linesize;
757 282976 U = r->tmp_b_block_uv[dir*2] + (xoff>>1) + (yoff>>1)*s->uvlinesize;
758 282976 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 511875 times.
514595 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 506907 times.
511875 }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 404843 times.
✓ Branch 1 taken 109752 times.
✓ Branch 2 taken 402123 times.
✓ Branch 3 taken 2720 times.
✓ Branch 4 taken 397155 times.
✓ Branch 5 taken 4968 times.
514595 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 117440 times.
✓ Branch 1 taken 397155 times.
514595 qpel_mc[!is16x16][dxy](Y, srcY, s->linesize);
772
2/2
✓ Branch 0 taken 43807 times.
✓ Branch 1 taken 470788 times.
514595 if (emu) {
773 43807 uint8_t *uvbuf = s->sc.edge_emu_buffer;
774
775 43807 s->vdsp.emulated_edge_mc(uvbuf, srcU,
776 s->uvlinesize, s->uvlinesize,
777 43807 (width << 2) + 1, (height << 2) + 1,
778 uvsrc_x, uvsrc_y,
779 43807 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
780 43807 srcU = uvbuf;
781 43807 uvbuf += 9*s->uvlinesize;
782
783 43807 s->vdsp.emulated_edge_mc(uvbuf, srcV,
784 s->uvlinesize, s->uvlinesize,
785 43807 (width << 2) + 1, (height << 2) + 1,
786 uvsrc_x, uvsrc_y,
787 43807 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
788 43807 srcV = uvbuf;
789 }
790 514595 chroma_mc[2-width] (U, srcU, s->uvlinesize, height*4, uvmx, uvmy);
791 514595 chroma_mc[2-width] (V, srcV, s->uvlinesize, height*4, uvmx, uvmy);
792 514595 }
793
794 112465 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 112465 rv34_mc(r, block_type, xoff, yoff, mv_off, width, height, dir, r->rv30, 0,
799 112465 r->rdsp.put_pixels_tab,
800 112465 r->rdsp.put_chroma_pixels_tab);
801 112465 }
802
803 116417 static void rv4_weight(RV34DecContext *r)
804 {
805 116417 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 116417 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 116417 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 116417 }
824
825 155277 static void rv34_mc_2mv(RV34DecContext *r, const int block_type)
826 {
827
5/6
✓ Branch 0 taken 117369 times.
✓ Branch 1 taken 37908 times.
✓ Branch 2 taken 108060 times.
✓ Branch 3 taken 9309 times.
✓ Branch 4 taken 108060 times.
✗ Branch 5 not taken.
155277 int weighted = !r->rv30 && block_type != RV34_MB_B_BIDIR && r->weight1 != 8192;
828
829 155277 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 0, r->rv30, weighted,
830 155277 r->rdsp.put_pixels_tab,
831 155277 r->rdsp.put_chroma_pixels_tab);
832
2/2
✓ Branch 0 taken 47217 times.
✓ Branch 1 taken 108060 times.
155277 if(!weighted){
833 47217 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 0,
834 47217 r->rdsp.avg_pixels_tab,
835 47217 r->rdsp.avg_chroma_pixels_tab);
836 }else{
837 108060 rv34_mc(r, block_type, 0, 0, 0, 2, 2, 1, r->rv30, 1,
838 108060 r->rdsp.put_pixels_tab,
839 108060 r->rdsp.put_chroma_pixels_tab);
840 108060 rv4_weight(r);
841 }
842 155277 }
843
844 11447 static void rv34_mc_2mv_skip(RV34DecContext *r)
845 {
846 int i, j;
847
3/4
✓ Branch 0 taken 8357 times.
✓ Branch 1 taken 3090 times.
✓ Branch 2 taken 8357 times.
✗ Branch 3 not taken.
11447 int weighted = !r->rv30 && r->weight1 != 8192;
848
849
2/2
✓ Branch 0 taken 22894 times.
✓ Branch 1 taken 11447 times.
34341 for(j = 0; j < 2; j++)
850
2/2
✓ Branch 0 taken 45788 times.
✓ Branch 1 taken 22894 times.
68682 for(i = 0; i < 2; i++){
851 45788 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 45788 r->rdsp.put_pixels_tab,
854 45788 r->rdsp.put_chroma_pixels_tab);
855
4/4
✓ Branch 0 taken 33428 times.
✓ Branch 1 taken 12360 times.
✓ Branch 2 taken 33428 times.
✓ Branch 3 taken 12360 times.
45788 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 8357 times.
✓ Branch 1 taken 3090 times.
11447 if(weighted)
861 8357 rv4_weight(r);
862 11447 }
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 286080 static int rv34_decode_mv(RV34DecContext *r, int block_type)
872 {
873 286080 MpegEncContext *s = &r->s;
874 286080 GetBitContext *gb = &s->gb;
875 int i, j, k, l;
876 286080 int mv_pos = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
877 int next_bt;
878
879 286080 memset(r->dmv, 0, sizeof(r->dmv));
880
2/2
✓ Branch 0 taken 99368 times.
✓ Branch 1 taken 286080 times.
385448 for(i = 0; i < num_mvs[block_type]; i++){
881 99368 r->dmv[i][0] = get_interleaved_se_golomb(gb);
882 99368 r->dmv[i][1] = get_interleaved_se_golomb(gb);
883
1/2
✓ Branch 0 taken 99368 times.
✗ Branch 1 not taken.
99368 if (r->dmv[i][0] == INVALID_VLC ||
884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99368 times.
99368 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 24367 times.
✓ Branch 1 taken 163923 times.
✓ Branch 2 taken 25207 times.
✓ Branch 3 taken 17078 times.
✓ Branch 4 taken 37808 times.
✓ Branch 5 taken 3844 times.
✓ Branch 6 taken 9309 times.
✓ Branch 7 taken 4544 times.
✗ Branch 8 not taken.
286080 switch(block_type){
890 24367 case RV34_MB_TYPE_INTRA:
891 case RV34_MB_TYPE_INTRA16x16:
892 24367 ZERO8x2(s->cur_pic.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
893 24367 return 0;
894 163923 case RV34_MB_SKIP:
895
2/2
✓ Branch 0 taken 31715 times.
✓ Branch 1 taken 132208 times.
163923 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 157415 times.
157415 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 157415 next_bt = s->next_pic.mb_type[s->mb_x + s->mb_y * s->mb_stride];
907
4/4
✓ Branch 0 taken 143748 times.
✓ Branch 1 taken 13667 times.
✓ Branch 2 taken 93320 times.
✓ Branch 3 taken 50428 times.
157415 if(IS_INTRA(next_bt) || IS_SKIP(next_bt)){
908 106987 ZERO8x2(s->cur_pic.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
909 106987 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 100856 times.
✓ Branch 1 taken 50428 times.
151284 for(j = 0; j < 2; j++)
912
2/2
✓ Branch 0 taken 201712 times.
✓ Branch 1 taken 100856 times.
302568 for(i = 0; i < 2; i++)
913
2/2
✓ Branch 0 taken 403424 times.
✓ Branch 1 taken 201712 times.
605136 for(k = 0; k < 2; k++)
914
2/2
✓ Branch 0 taken 806848 times.
✓ Branch 1 taken 403424 times.
1210272 for(l = 0; l < 2; l++)
915 806848 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 155273 times.
✓ Branch 1 taken 2142 times.
✓ Branch 2 taken 152111 times.
✓ Branch 3 taken 3162 times.
✓ Branch 4 taken 145968 times.
✓ Branch 5 taken 6143 times.
157415 if(!(IS_16X8(next_bt) || IS_8X16(next_bt) || IS_8X8(next_bt))) //we can use whole macroblock MC
917 145968 rv34_mc_2mv(r, block_type);
918 else
919 11447 rv34_mc_2mv_skip(r);
920 157415 ZERO8x2(s->cur_pic.motion_val[0][s->mb_x * 2 + s->mb_y * 2 * s->b8_stride], s->b8_stride);
921 157415 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 37808 case RV34_MB_B_FORWARD:
928 case RV34_MB_B_BACKWARD:
929 37808 r->dmv[1][0] = r->dmv[0][0];
930 37808 r->dmv[1][1] = r->dmv[0][1];
931
2/2
✓ Branch 0 taken 9134 times.
✓ Branch 1 taken 28674 times.
37808 if(r->rv30)
932 9134 rv34_pred_mv_rv3(r, block_type, block_type == RV34_MB_B_BACKWARD);
933 else
934 28674 rv34_pred_mv_b (r, block_type, block_type == RV34_MB_B_BACKWARD);
935 37808 rv34_mc_1mv (r, block_type, 0, 0, 0, 2, 2, block_type == RV34_MB_B_BACKWARD);
936 37808 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 9309 case RV34_MB_B_BIDIR:
951 9309 rv34_pred_mv_b (r, block_type, 0);
952 9309 rv34_pred_mv_b (r, block_type, 1);
953 9309 rv34_mc_2mv (r, block_type);
954 9309 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 261713 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 33756 static inline int adjust_pred16(int itype, int up, int left)
1013 {
1014
4/4
✓ Branch 0 taken 5834 times.
✓ Branch 1 taken 27922 times.
✓ Branch 2 taken 54 times.
✓ Branch 3 taken 5780 times.
33756 if(!up && !left)
1015 54 itype = DC_128_PRED8x8;
1016
2/2
✓ Branch 0 taken 5780 times.
✓ Branch 1 taken 27922 times.
33702 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 27082 times.
27922 }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 33756 return itype;
1026 }
1027
1028 708606 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 708606 MpegEncContext *s = &r->s;
1033 708606 int16_t *ptr = s->block[0];
1034 708606 int has_ac = rv34_decode_block(ptr, &s->gb, r->cur_vlcs,
1035 fc, sc, q_dc, q_ac, q_ac);
1036
2/2
✓ Branch 0 taken 352387 times.
✓ Branch 1 taken 356219 times.
708606 if(has_ac){
1037 352387 r->rdsp.rv34_idct_add(pdst, stride, ptr);
1038 }else{
1039 356219 r->rdsp.rv34_idct_dc_add(pdst, stride, ptr[0]);
1040 356219 ptr[0] = 0;
1041 }
1042 708606 }
1043
1044 16878 static void rv34_output_i16x16(RV34DecContext *r, int8_t *intra_types, int cbp)
1045 {
1046 16878 LOCAL_ALIGNED_16(int16_t, block16, [16]);
1047 16878 MpegEncContext *s = &r->s;
1048 16878 GetBitContext *gb = &s->gb;
1049 16878 int q_dc = rv34_qscale_tab[ r->luma_dc_quant_i[s->qscale] ],
1050 16878 q_ac = rv34_qscale_tab[s->qscale];
1051 16878 uint8_t *dst = s->dest[0];
1052 16878 int16_t *ptr = s->block[0];
1053 int i, j, itype, has_ac;
1054
1055 16878 memset(block16, 0, 16 * sizeof(*block16));
1056
1057 16878 has_ac = rv34_decode_block(block16, gb, r->cur_vlcs, 3, 0, q_dc, q_dc, q_ac);
1058
2/2
✓ Branch 0 taken 7956 times.
✓ Branch 1 taken 8922 times.
16878 if(has_ac)
1059 7956 r->rdsp.rv34_inv_transform(block16);
1060 else
1061 8922 r->rdsp.rv34_inv_transform_dc(block16);
1062
1063 16878 itype = ittrans16[intra_types[0]];
1064 16878 itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
1065 16878 r->h.pred16x16[itype](dst, s->linesize);
1066
1067
2/2
✓ Branch 0 taken 67512 times.
✓ Branch 1 taken 16878 times.
84390 for(j = 0; j < 4; j++){
1068
2/2
✓ Branch 0 taken 270048 times.
✓ Branch 1 taken 67512 times.
337560 for(i = 0; i < 4; i++, cbp >>= 1){
1069 270048 int dc = block16[i + j*4];
1070
1071
2/2
✓ Branch 0 taken 14318 times.
✓ Branch 1 taken 255730 times.
270048 if(cbp & 1){
1072 14318 has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
1073 }else
1074 255730 has_ac = 0;
1075
1076
2/2
✓ Branch 0 taken 14318 times.
✓ Branch 1 taken 255730 times.
270048 if(has_ac){
1077 14318 ptr[0] = dc;
1078 14318 r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
1079 }else
1080 255730 r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
1081 }
1082
1083 67512 dst += 4*s->linesize;
1084 }
1085
1086 16878 itype = ittrans16[intra_types[0]];
1087
2/2
✓ Branch 0 taken 1214 times.
✓ Branch 1 taken 15664 times.
16878 if(itype == PLANE_PRED8x8) itype = DC_PRED8x8;
1088 16878 itype = adjust_pred16(itype, r->avail_cache[6-4], r->avail_cache[6-1]);
1089
1090 16878 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1091 16878 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1092
1093
2/2
✓ Branch 0 taken 33756 times.
✓ Branch 1 taken 16878 times.
50634 for(j = 1; j < 3; j++){
1094 33756 dst = s->dest[j];
1095 33756 r->h.pred8x8[itype](dst, s->uvlinesize);
1096
2/2
✓ Branch 0 taken 135024 times.
✓ Branch 1 taken 33756 times.
168780 for(i = 0; i < 4; i++, cbp >>= 1){
1097 uint8_t *pdst;
1098
2/2
✓ Branch 0 taken 110244 times.
✓ Branch 1 taken 24780 times.
135024 if(!(cbp & 1)) continue;
1099 24780 pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
1100
1101 24780 rv34_process_block(r, pdst, s->uvlinesize,
1102 r->chroma_vlc, 1, q_dc, q_ac);
1103 }
1104 }
1105 16878 }
1106
1107 15129 static void rv34_output_intra(RV34DecContext *r, int8_t *intra_types, int cbp)
1108 {
1109 15129 MpegEncContext *s = &r->s;
1110 15129 uint8_t *dst = s->dest[0];
1111 15129 int avail[6*8] = {0};
1112 int i, j, k;
1113 int idx, q_ac, q_dc;
1114
1115 // Set neighbour information.
1116
2/2
✓ Branch 0 taken 8929 times.
✓ Branch 1 taken 6200 times.
15129 if(r->avail_cache[1])
1117 8929 avail[0] = 1;
1118
2/2
✓ Branch 0 taken 9550 times.
✓ Branch 1 taken 5579 times.
15129 if(r->avail_cache[2])
1119 9550 avail[1] = avail[2] = 1;
1120
2/2
✓ Branch 0 taken 9550 times.
✓ Branch 1 taken 5579 times.
15129 if(r->avail_cache[3])
1121 9550 avail[3] = avail[4] = 1;
1122
2/2
✓ Branch 0 taken 9514 times.
✓ Branch 1 taken 5615 times.
15129 if(r->avail_cache[4])
1123 9514 avail[5] = 1;
1124
2/2
✓ Branch 0 taken 14351 times.
✓ Branch 1 taken 778 times.
15129 if(r->avail_cache[5])
1125 14351 avail[8] = avail[16] = 1;
1126
2/2
✓ Branch 0 taken 14351 times.
✓ Branch 1 taken 778 times.
15129 if(r->avail_cache[9])
1127 14351 avail[24] = avail[32] = 1;
1128
1129 15129 q_ac = rv34_qscale_tab[s->qscale];
1130
2/2
✓ Branch 0 taken 60516 times.
✓ Branch 1 taken 15129 times.
75645 for(j = 0; j < 4; j++){
1131 60516 idx = 9 + j*8;
1132
2/2
✓ Branch 0 taken 242064 times.
✓ Branch 1 taken 60516 times.
302580 for(i = 0; i < 4; i++, cbp >>= 1, dst += 4, idx++){
1133 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]);
1134 242064 avail[idx] = 1;
1135
2/2
✓ Branch 0 taken 93479 times.
✓ Branch 1 taken 148585 times.
242064 if(!(cbp & 1)) continue;
1136
1137 148585 rv34_process_block(r, dst, s->linesize,
1138 r->luma_vlc, 0, q_ac, q_ac);
1139 }
1140 60516 dst += s->linesize * 4 - 4*4;
1141 60516 intra_types += r->intra_types_stride;
1142 }
1143
1144 15129 intra_types -= r->intra_types_stride * 4;
1145
1146 15129 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1147 15129 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1148
1149
2/2
✓ Branch 0 taken 30258 times.
✓ Branch 1 taken 15129 times.
45387 for(k = 0; k < 2; k++){
1150 30258 dst = s->dest[1+k];
1151 30258 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 0, 4);
1152
1153
2/2
✓ Branch 0 taken 60516 times.
✓ Branch 1 taken 30258 times.
90774 for(j = 0; j < 2; j++){
1154 60516 int* acache = r->avail_cache + 6 + j*4;
1155
2/2
✓ Branch 0 taken 121032 times.
✓ Branch 1 taken 60516 times.
181548 for(i = 0; i < 2; i++, cbp >>= 1, acache++){
1156 121032 int itype = ittrans[intra_types[i*2+j*2*r->intra_types_stride]];
1157
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]);
1158 121032 acache[0] = 1;
1159
1160
2/2
✓ Branch 0 taken 49869 times.
✓ Branch 1 taken 71163 times.
121032 if(!(cbp&1)) continue;
1161
1162 71163 rv34_process_block(r, dst + 4*i, s->uvlinesize,
1163 r->chroma_vlc, 1, q_dc, q_ac);
1164 }
1165
1166 60516 dst += 4*s->uvlinesize;
1167 }
1168 }
1169 15129 }
1170
1171 2257744 static int is_mv_diff_gt_3(int16_t (*motion_val)[2], int step)
1172 {
1173 int d;
1174 2257744 d = motion_val[0][0] - motion_val[-step][0];
1175
4/4
✓ Branch 0 taken 2164788 times.
✓ Branch 1 taken 92956 times.
✓ Branch 2 taken 96398 times.
✓ Branch 3 taken 2068390 times.
2257744 if(d < -3 || d > 3)
1176 189354 return 1;
1177 2068390 d = motion_val[0][1] - motion_val[-step][1];
1178
4/4
✓ Branch 0 taken 2053961 times.
✓ Branch 1 taken 14429 times.
✓ Branch 2 taken 17585 times.
✓ Branch 3 taken 2036376 times.
2068390 if(d < -3 || d > 3)
1179 32014 return 1;
1180 2036376 return 0;
1181 }
1182
1183 286080 static int rv34_set_deblock_coef(RV34DecContext *r)
1184 {
1185 286080 MpegEncContext *s = &r->s;
1186 286080 int hmvmask = 0, vmvmask = 0, i, j;
1187 286080 int midx = s->mb_x * 2 + s->mb_y * 2 * s->b8_stride;
1188 286080 int16_t (*motion_val)[2] = &s->cur_pic.motion_val[0][midx];
1189
2/2
✓ Branch 0 taken 572160 times.
✓ Branch 1 taken 286080 times.
858240 for(j = 0; j < 16; j += 8){
1190
2/2
✓ Branch 0 taken 1144320 times.
✓ Branch 1 taken 572160 times.
1716480 for(i = 0; i < 2; i++){
1191
2/2
✓ Branch 1 taken 107013 times.
✓ Branch 2 taken 1037307 times.
1144320 if(is_mv_diff_gt_3(motion_val + i, 1))
1192 107013 vmvmask |= 0x11 << (j + i*2);
1193
6/6
✓ Branch 0 taken 572160 times.
✓ Branch 1 taken 572160 times.
✓ Branch 2 taken 541264 times.
✓ Branch 3 taken 30896 times.
✓ Branch 5 taken 114355 times.
✓ Branch 6 taken 999069 times.
1144320 if((j || s->mb_y) && is_mv_diff_gt_3(motion_val + i, s->b8_stride))
1194 114355 hmvmask |= 0x03 << (j + i*2);
1195 }
1196 572160 motion_val += s->b8_stride;
1197 }
1198
2/2
✓ Branch 0 taken 42563 times.
✓ Branch 1 taken 243517 times.
286080 if(s->first_slice_line)
1199 42563 hmvmask &= ~0x000F;
1200
2/2
✓ Branch 0 taken 9160 times.
✓ Branch 1 taken 276920 times.
286080 if(!s->mb_x)
1201 9160 vmvmask &= ~0x1111;
1202
2/2
✓ Branch 0 taken 68640 times.
✓ Branch 1 taken 217440 times.
286080 if(r->rv30){ //RV30 marks both subblocks on the edge for filtering
1203 68640 vmvmask |= (vmvmask & 0x4444) >> 1;
1204 68640 hmvmask |= (hmvmask & 0x0F00) >> 4;
1205
2/2
✓ Branch 0 taken 65520 times.
✓ Branch 1 taken 3120 times.
68640 if(s->mb_x)
1206 65520 r->deblock_coefs[s->mb_x - 1 + s->mb_y*s->mb_stride] |= (vmvmask & 0x1111) << 3;
1207
2/2
✓ Branch 0 taken 61878 times.
✓ Branch 1 taken 6762 times.
68640 if(!s->first_slice_line)
1208 61878 r->deblock_coefs[s->mb_x + (s->mb_y - 1)*s->mb_stride] |= (hmvmask & 0xF) << 12;
1209 }
1210 286080 return hmvmask | vmvmask;
1211 }
1212
1213 286080 static int rv34_decode_inter_macroblock(RV34DecContext *r, int8_t *intra_types)
1214 {
1215 286080 MpegEncContext *s = &r->s;
1216 286080 GetBitContext *gb = &s->gb;
1217 286080 uint8_t *dst = s->dest[0];
1218 286080 int16_t *ptr = s->block[0];
1219 286080 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1220 int cbp, cbp2;
1221 int q_dc, q_ac, has_ac;
1222 int i, j;
1223 int dist;
1224
1225 // Calculate which neighbours are available. Maybe it's worth optimizing too.
1226 286080 memset(r->avail_cache, 0, sizeof(r->avail_cache));
1227 286080 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
1228 286080 dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
1229
4/4
✓ Branch 0 taken 276920 times.
✓ Branch 1 taken 9160 times.
✓ Branch 2 taken 276415 times.
✓ Branch 3 taken 505 times.
286080 if(s->mb_x && dist)
1230 276415 r->avail_cache[5] =
1231 276415 r->avail_cache[9] = s->cur_pic.mb_type[mb_pos - 1];
1232
2/2
✓ Branch 0 taken 243517 times.
✓ Branch 1 taken 42563 times.
286080 if(dist >= s->mb_width)
1233 243517 r->avail_cache[2] =
1234 243517 r->avail_cache[3] = s->cur_pic.mb_type[mb_pos - s->mb_stride];
1235
4/4
✓ Branch 0 taken 276920 times.
✓ Branch 1 taken 9160 times.
✓ Branch 2 taken 236121 times.
✓ Branch 3 taken 40799 times.
286080 if(((s->mb_x+1) < s->mb_width) && dist >= s->mb_width - 1)
1236 236121 r->avail_cache[4] = s->cur_pic.mb_type[mb_pos - s->mb_stride + 1];
1237
4/4
✓ Branch 0 taken 276920 times.
✓ Branch 1 taken 9160 times.
✓ Branch 2 taken 235192 times.
✓ Branch 3 taken 41728 times.
286080 if(s->mb_x && dist > s->mb_width)
1238 235192 r->avail_cache[1] = s->cur_pic.mb_type[mb_pos - s->mb_stride - 1];
1239
1240 286080 s->qscale = r->si.quant;
1241 286080 cbp = cbp2 = rv34_decode_inter_mb_header(r, intra_types);
1242 286080 r->cbp_luma [mb_pos] = cbp;
1243 286080 r->cbp_chroma[mb_pos] = cbp >> 16;
1244 286080 r->deblock_coefs[mb_pos] = rv34_set_deblock_coef(r) | r->cbp_luma[mb_pos];
1245 286080 s->cur_pic.qscale_table[mb_pos] = s->qscale;
1246
1247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 286080 times.
286080 if(cbp == -1)
1248 return -1;
1249
1250
2/2
✓ Branch 0 taken 24367 times.
✓ Branch 1 taken 261713 times.
286080 if (IS_INTRA(s->cur_pic.mb_type[mb_pos])) {
1251
2/2
✓ Branch 0 taken 11048 times.
✓ Branch 1 taken 13319 times.
24367 if(r->is16) rv34_output_i16x16(r, intra_types, cbp);
1252 13319 else rv34_output_intra(r, intra_types, cbp);
1253 24367 return 0;
1254 }
1255
1256
2/2
✓ Branch 0 taken 549 times.
✓ Branch 1 taken 261164 times.
261713 if(r->is16){
1257 // Only for RV34_MB_P_MIX16x16
1258 549 LOCAL_ALIGNED_16(int16_t, block16, [16]);
1259 549 memset(block16, 0, 16 * sizeof(*block16));
1260 549 q_dc = rv34_qscale_tab[ r->luma_dc_quant_p[s->qscale] ];
1261 549 q_ac = rv34_qscale_tab[s->qscale];
1262
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))
1263 268 r->rdsp.rv34_inv_transform(block16);
1264 else
1265 281 r->rdsp.rv34_inv_transform_dc(block16);
1266
1267 549 q_ac = rv34_qscale_tab[s->qscale];
1268
1269
2/2
✓ Branch 0 taken 2196 times.
✓ Branch 1 taken 549 times.
2745 for(j = 0; j < 4; j++){
1270
2/2
✓ Branch 0 taken 8784 times.
✓ Branch 1 taken 2196 times.
10980 for(i = 0; i < 4; i++, cbp >>= 1){
1271 8784 int dc = block16[i + j*4];
1272
1273
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8778 times.
8784 if(cbp & 1){
1274 6 has_ac = rv34_decode_block(ptr, gb, r->cur_vlcs, r->luma_vlc, 0, q_ac, q_ac, q_ac);
1275 }else
1276 8778 has_ac = 0;
1277
1278
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 8778 times.
8784 if(has_ac){
1279 6 ptr[0] = dc;
1280 6 r->rdsp.rv34_idct_add(dst+4*i, s->linesize, ptr);
1281 }else
1282 8778 r->rdsp.rv34_idct_dc_add(dst+4*i, s->linesize, dc);
1283 }
1284
1285 2196 dst += 4*s->linesize;
1286 }
1287
1288 549 r->cur_vlcs = choose_vlc_set(r->si.quant, r->si.vlc_set, 1);
1289 }else{
1290 261164 q_ac = rv34_qscale_tab[s->qscale];
1291
1292
2/2
✓ Branch 0 taken 1044656 times.
✓ Branch 1 taken 261164 times.
1305820 for(j = 0; j < 4; j++){
1293
2/2
✓ Branch 0 taken 4178624 times.
✓ Branch 1 taken 1044656 times.
5223280 for(i = 0; i < 4; i++, cbp >>= 1){
1294
2/2
✓ Branch 0 taken 3859345 times.
✓ Branch 1 taken 319279 times.
4178624 if(!(cbp & 1)) continue;
1295
1296 319279 rv34_process_block(r, dst + 4*i, s->linesize,
1297 r->luma_vlc, 0, q_ac, q_ac);
1298 }
1299 1044656 dst += 4*s->linesize;
1300 }
1301 }
1302
1303 261713 q_dc = rv34_qscale_tab[rv34_chroma_quant[1][s->qscale]];
1304 261713 q_ac = rv34_qscale_tab[rv34_chroma_quant[0][s->qscale]];
1305
1306
2/2
✓ Branch 0 taken 523426 times.
✓ Branch 1 taken 261713 times.
785139 for(j = 1; j < 3; j++){
1307 523426 dst = s->dest[j];
1308
2/2
✓ Branch 0 taken 2093704 times.
✓ Branch 1 taken 523426 times.
2617130 for(i = 0; i < 4; i++, cbp >>= 1){
1309 uint8_t *pdst;
1310
2/2
✓ Branch 0 taken 1948905 times.
✓ Branch 1 taken 144799 times.
2093704 if(!(cbp & 1)) continue;
1311 144799 pdst = dst + (i&1)*4 + (i&2)*2*s->uvlinesize;
1312
1313 144799 rv34_process_block(r, pdst, s->uvlinesize,
1314 r->chroma_vlc, 1, q_dc, q_ac);
1315 }
1316 }
1317
1318 261713 return 0;
1319 }
1320
1321 7640 static int rv34_decode_intra_macroblock(RV34DecContext *r, int8_t *intra_types)
1322 {
1323 7640 MpegEncContext *s = &r->s;
1324 int cbp, dist;
1325 7640 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1326
1327 // Calculate which neighbours are available. Maybe it's worth optimizing too.
1328 7640 memset(r->avail_cache, 0, sizeof(r->avail_cache));
1329 7640 fill_rectangle(r->avail_cache + 6, 2, 2, 4, 1, 4);
1330 7640 dist = (s->mb_x - s->resync_mb_x) + (s->mb_y - s->resync_mb_y) * s->mb_width;
1331
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)
1332 7325 r->avail_cache[5] =
1333 7325 r->avail_cache[9] = s->cur_pic.mb_type[mb_pos - 1];
1334
2/2
✓ Branch 0 taken 6133 times.
✓ Branch 1 taken 1507 times.
7640 if(dist >= s->mb_width)
1335 6133 r->avail_cache[2] =
1336 6133 r->avail_cache[3] = s->cur_pic.mb_type[mb_pos - s->mb_stride];
1337
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)
1338 5928 r->avail_cache[4] = s->cur_pic.mb_type[mb_pos - s->mb_stride + 1];
1339
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)
1340 5878 r->avail_cache[1] = s->cur_pic.mb_type[mb_pos - s->mb_stride - 1];
1341
1342 7640 s->qscale = r->si.quant;
1343 7640 cbp = rv34_decode_intra_mb_header(r, intra_types);
1344 7640 r->cbp_luma [mb_pos] = cbp;
1345 7640 r->cbp_chroma[mb_pos] = cbp >> 16;
1346 7640 r->deblock_coefs[mb_pos] = 0xFFFF;
1347 7640 s->cur_pic.qscale_table[mb_pos] = s->qscale;
1348
1349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7640 times.
7640 if(cbp == -1)
1350 return -1;
1351
1352
2/2
✓ Branch 0 taken 5830 times.
✓ Branch 1 taken 1810 times.
7640 if(r->is16){
1353 5830 rv34_output_i16x16(r, intra_types, cbp);
1354 5830 return 0;
1355 }
1356
1357 1810 rv34_output_intra(r, intra_types, cbp);
1358 1810 return 0;
1359 }
1360
1361 295111 static int check_slice_end(RV34DecContext *r, MpegEncContext *s)
1362 {
1363 int bits;
1364
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 294583 times.
295111 if(s->mb_y >= s->mb_height)
1365 528 return 1;
1366
2/2
✓ Branch 0 taken 863 times.
✓ Branch 1 taken 293720 times.
294583 if(!s->mb_num_left)
1367 863 return 1;
1368
2/2
✓ Branch 0 taken 129066 times.
✓ Branch 1 taken 164654 times.
293720 if(r->s.mb_skip_run > 1)
1369 129066 return 0;
1370 164654 bits = get_bits_left(&s->gb);
1371
4/6
✓ Branch 0 taken 164654 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 169 times.
✓ Branch 3 taken 164485 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 169 times.
164654 if(bits <= 0 || (bits < 8 && !show_bits(&s->gb, bits)))
1372 return 1;
1373 164654 return 0;
1374 }
1375
1376
1377 9 static void rv34_decoder_free(RV34DecContext *r)
1378 {
1379 9 av_freep(&r->intra_types_hist);
1380 9 r->intra_types = NULL;
1381 9 av_freep(&r->tmp_b_block_base);
1382 9 av_freep(&r->mb_type);
1383 9 av_freep(&r->cbp_luma);
1384 9 av_freep(&r->cbp_chroma);
1385 9 av_freep(&r->deblock_coefs);
1386 9 }
1387
1388
1389 9 static int rv34_decoder_alloc(RV34DecContext *r)
1390 {
1391 9 r->intra_types_stride = r->s.mb_width * 4 + 4;
1392
1393 9 r->cbp_chroma = av_mallocz(r->s.mb_stride * r->s.mb_height *
1394 sizeof(*r->cbp_chroma));
1395 9 r->cbp_luma = av_mallocz(r->s.mb_stride * r->s.mb_height *
1396 sizeof(*r->cbp_luma));
1397 9 r->deblock_coefs = av_mallocz(r->s.mb_stride * r->s.mb_height *
1398 sizeof(*r->deblock_coefs));
1399 9 r->intra_types_hist = av_malloc(r->intra_types_stride * 4 * 2 *
1400 sizeof(*r->intra_types_hist));
1401 9 r->mb_type = av_mallocz(r->s.mb_stride * r->s.mb_height *
1402 sizeof(*r->mb_type));
1403
1404
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 &&
1405
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)) {
1406 r->s.context_reinit = 1;
1407 rv34_decoder_free(r);
1408 return AVERROR(ENOMEM);
1409 }
1410
1411 9 r->intra_types = r->intra_types_hist + r->intra_types_stride * 4;
1412
1413 9 return 0;
1414 }
1415
1416
1417 static int rv34_decoder_realloc(RV34DecContext *r)
1418 {
1419 rv34_decoder_free(r);
1420 return rv34_decoder_alloc(r);
1421 }
1422
1423
1424 1391 static int rv34_decode_slice(RV34DecContext *r, int end, const uint8_t* buf, int buf_size)
1425 {
1426 1391 MpegEncContext *s = &r->s;
1427 1391 GetBitContext *gb = &s->gb;
1428 int mb_pos, slice_type;
1429 int res;
1430
1431 1391 init_get_bits(&r->s.gb, buf, buf_size*8);
1432 1391 res = r->parse_slice_header(r, gb, &r->si);
1433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1391 times.
1391 if(res < 0){
1434 av_log(s->avctx, AV_LOG_ERROR, "Incorrect or unknown slice header\n");
1435 return -1;
1436 }
1437
1438
2/2
✓ Branch 0 taken 1332 times.
✓ Branch 1 taken 59 times.
1391 slice_type = r->si.type ? r->si.type : AV_PICTURE_TYPE_I;
1439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1391 times.
1391 if (slice_type != s->pict_type) {
1440 av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
1441 return AVERROR_INVALIDDATA;
1442 }
1443
2/4
✓ Branch 0 taken 1391 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1391 times.
1391 if (s->width != r->si.width || s->height != r->si.height) {
1444 av_log(s->avctx, AV_LOG_ERROR, "Size mismatch\n");
1445 return AVERROR_INVALIDDATA;
1446 }
1447
1448 1391 r->si.end = end;
1449 1391 s->qscale = r->si.quant;
1450 1391 s->mb_num_left = r->si.end - r->si.start;
1451 1391 r->s.mb_skip_run = 0;
1452
1453 1391 mb_pos = s->mb_x + s->mb_y * s->mb_width;
1454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1391 times.
1391 if(r->si.start != mb_pos){
1455 av_log(s->avctx, AV_LOG_ERROR, "Slice indicates MB offset %d, got %d\n", r->si.start, mb_pos);
1456 s->mb_x = r->si.start % s->mb_width;
1457 s->mb_y = r->si.start / s->mb_width;
1458 }
1459 1391 memset(r->intra_types_hist, -1, r->intra_types_stride * 4 * 2 * sizeof(*r->intra_types_hist));
1460 1391 s->first_slice_line = 1;
1461 1391 s->resync_mb_x = s->mb_x;
1462 1391 s->resync_mb_y = s->mb_y;
1463
1464 1391 ff_init_block_index(s);
1465
2/2
✓ Branch 1 taken 293720 times.
✓ Branch 2 taken 1391 times.
295111 while(!check_slice_end(r, s)) {
1466 293720 ff_update_block_index(s, 8, 0, 1);
1467
1468
2/2
✓ Branch 0 taken 286080 times.
✓ Branch 1 taken 7640 times.
293720 if(r->si.type)
1469 286080 res = rv34_decode_inter_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
1470 else
1471 7640 res = rv34_decode_intra_macroblock(r, r->intra_types + s->mb_x * 4 + 4);
1472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 293720 times.
293720 if(res < 0){
1473 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_ERROR);
1474 return -1;
1475 }
1476
2/2
✓ Branch 0 taken 9448 times.
✓ Branch 1 taken 284272 times.
293720 if (++s->mb_x == s->mb_width) {
1477 9448 s->mb_x = 0;
1478 9448 s->mb_y++;
1479 9448 ff_init_block_index(s);
1480
1481 9448 memmove(r->intra_types_hist, r->intra_types, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
1482 9448 memset(r->intra_types, -1, r->intra_types_stride * 4 * sizeof(*r->intra_types_hist));
1483
1484
3/4
✓ Branch 0 taken 9448 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8920 times.
✓ Branch 3 taken 528 times.
9448 if(r->loop_filter && s->mb_y >= 2)
1485 8920 r->loop_filter(r, s->mb_y - 2);
1486
1487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9448 times.
9448 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
1488 ff_thread_progress_report(&s->cur_pic.ptr->progress,
1489 s->mb_y - 2);
1490
1491 }
1492
2/2
✓ Branch 0 taken 9069 times.
✓ Branch 1 taken 284651 times.
293720 if(s->mb_x == s->resync_mb_x)
1493 9069 s->first_slice_line=0;
1494 293720 s->mb_num_left--;
1495 }
1496 1391 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
1497
1498 1391 return s->mb_y == s->mb_height;
1499 }
1500
1501 /** @} */ // reconstruction group end
1502
1503 /**
1504 * Initialize decoder.
1505 */
1506 9 av_cold int ff_rv34_decode_init(AVCodecContext *avctx)
1507 {
1508 static AVOnce init_static_once = AV_ONCE_INIT;
1509 9 RV34DecContext *r = avctx->priv_data;
1510 9 MpegEncContext *s = &r->s;
1511 int ret;
1512
1513 9 ret = ff_mpv_decode_init(s, avctx);
1514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1515 return ret;
1516 9 s->out_format = FMT_H263;
1517
1518 9 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
1519 9 avctx->has_b_frames = 1;
1520 9 s->low_delay = 0;
1521
1522
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if ((ret = ff_mpv_common_init(s)) < 0)
1523 return ret;
1524
1525 9 ff_h264_pred_init(&r->h, AV_CODEC_ID_RV40, 8, 1);
1526
1527 9 ret = rv34_decoder_alloc(r);
1528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (ret < 0)
1529 return ret;
1530
1531 9 ff_thread_once(&init_static_once, rv34_init_tables);
1532
1533 9 return 0;
1534 }
1535
1536 int ff_rv34_decode_update_thread_context(AVCodecContext *dst, const AVCodecContext *src)
1537 {
1538 RV34DecContext *r = dst->priv_data, *r1 = src->priv_data;
1539 MpegEncContext * const s = &r->s, * const s1 = &r1->s;
1540 int err;
1541
1542 if (dst == src || !s1->context_initialized)
1543 return 0;
1544
1545 if (s->height != s1->height || s->width != s1->width || s->context_reinit) {
1546 s->height = s1->height;
1547 s->width = s1->width;
1548 if ((err = ff_mpv_common_frame_size_change(s)) < 0)
1549 return err;
1550 if ((err = rv34_decoder_realloc(r)) < 0)
1551 return err;
1552 }
1553
1554 r->cur_pts = r1->cur_pts;
1555 r->last_pts = r1->last_pts;
1556 r->next_pts = r1->next_pts;
1557
1558 memset(&r->si, 0, sizeof(r->si));
1559
1560 // Do no call ff_mpeg_update_thread_context on a partially initialized
1561 // decoder context.
1562 if (!s1->context_initialized)
1563 return 0;
1564
1565 return ff_mpeg_update_thread_context(dst, src);
1566 }
1567
1568 4174 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n, int slice_count, int buf_size)
1569 {
1570
2/2
✓ Branch 0 taken 3287 times.
✓ Branch 1 taken 887 times.
4174 if (n < slice_count) {
1571
1/2
✓ Branch 0 taken 3287 times.
✗ Branch 1 not taken.
3287 return AV_RL32(buf + n*8 - 4) == 1 ? AV_RL32(buf + n*8) : AV_RB32(buf + n*8);
1572 } else
1573 887 return buf_size;
1574 }
1575
1576 528 static int finish_frame(AVCodecContext *avctx, AVFrame *pict)
1577 {
1578 528 RV34DecContext *r = avctx->priv_data;
1579 528 MpegEncContext *s = &r->s;
1580 528 int got_picture = 0, ret;
1581
1582 528 ff_er_frame_end(&s->er, NULL);
1583 528 ff_mpv_frame_end(s);
1584 528 s->mb_num_left = 0;
1585
1586
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
528 if (HAVE_THREADS && (s->avctx->active_thread_type & FF_THREAD_FRAME))
1587 ff_thread_progress_report(&s->cur_pic.ptr->progress, INT_MAX);
1588
1589
2/2
✓ Branch 0 taken 388 times.
✓ Branch 1 taken 140 times.
528 if (s->pict_type == AV_PICTURE_TYPE_B) {
1590
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 388 times.
388 if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0)
1591 return ret;
1592 388 ff_print_debug_info(s, s->cur_pic.ptr, pict);
1593 388 ff_mpv_export_qp_table(s, pict, s->cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
1594 388 got_picture = 1;
1595
2/2
✓ Branch 0 taken 131 times.
✓ Branch 1 taken 9 times.
140 } else if (s->last_pic.ptr) {
1596
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 131 times.
131 if ((ret = av_frame_ref(pict, s->last_pic.ptr->f)) < 0)
1597 return ret;
1598 131 ff_print_debug_info(s, s->last_pic.ptr, pict);
1599 131 ff_mpv_export_qp_table(s, pict, s->last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
1600 131 got_picture = 1;
1601 }
1602
1603 528 return got_picture;
1604 }
1605
1606 static AVRational update_sar(int old_w, int old_h, AVRational sar, int new_w, int new_h)
1607 {
1608 // attempt to keep aspect during typical resolution switches
1609 if (!sar.num)
1610 sar = (AVRational){1, 1};
1611
1612 sar = av_mul_q(sar, av_mul_q((AVRational){new_h, new_w}, (AVRational){old_w, old_h}));
1613 return sar;
1614 }
1615
1616 532 int ff_rv34_decode_frame(AVCodecContext *avctx, AVFrame *pict,
1617 int *got_picture_ptr, AVPacket *avpkt)
1618 {
1619 532 const uint8_t *buf = avpkt->data;
1620 532 int buf_size = avpkt->size;
1621 532 RV34DecContext *r = avctx->priv_data;
1622 532 MpegEncContext *s = &r->s;
1623 SliceInfo si;
1624 int i, ret;
1625 int slice_count;
1626 532 const uint8_t *slices_hdr = NULL;
1627 532 int last = 0;
1628 532 int faulty_b = 0;
1629 int offset;
1630
1631 /* no supplementary picture */
1632
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 528 times.
532 if (buf_size == 0) {
1633 /* special case for last picture */
1634
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (s->next_pic.ptr) {
1635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = av_frame_ref(pict, s->next_pic.ptr->f)) < 0)
1636 return ret;
1637 2 ff_mpv_unref_picture(&s->next_pic);
1638
1639 2 *got_picture_ptr = 1;
1640 }
1641 4 return 0;
1642 }
1643
1644 528 slice_count = (*buf++) + 1;
1645 528 slices_hdr = buf + 4;
1646 528 buf += 8 * slice_count;
1647 528 buf_size -= 1 + 8 * slice_count;
1648
1649 528 offset = get_slice_offset(avctx, slices_hdr, 0, slice_count, buf_size);
1650 //parse first slice header to check whether this frame can be decoded
1651
2/4
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 528 times.
528 if(offset < 0 || offset > buf_size){
1652 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1653 return AVERROR_INVALIDDATA;
1654 }
1655 528 init_get_bits(&s->gb, buf+offset, (buf_size-offset)*8);
1656
2/4
✓ Branch 1 taken 528 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 528 times.
528 if(r->parse_slice_header(r, &r->s.gb, &si) < 0 || si.start){
1657 av_log(avctx, AV_LOG_ERROR, "First slice header is incorrect\n");
1658 return AVERROR_INVALIDDATA;
1659 }
1660
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 515 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
528 if (!s->last_pic.ptr && si.type == AV_PICTURE_TYPE_B) {
1661 av_log(avctx, AV_LOG_ERROR, "Invalid decoder state: B-frame without "
1662 "reference data.\n");
1663 faulty_b = 1;
1664 }
1665
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
528 if( (avctx->skip_frame >= AVDISCARD_NONREF && si.type==AV_PICTURE_TYPE_B)
1666
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
528 || (avctx->skip_frame >= AVDISCARD_NONKEY && si.type!=AV_PICTURE_TYPE_I)
1667
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
528 || avctx->skip_frame >= AVDISCARD_ALL)
1668 return avpkt->size;
1669
1670 /* first slice */
1671
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 if (si.start == 0) {
1672
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
528 if (s->mb_num_left > 0 && s->cur_pic.ptr) {
1673 av_log(avctx, AV_LOG_ERROR, "New frame but still %d MB left.\n",
1674 s->mb_num_left);
1675 if (!s->context_reinit)
1676 ff_er_frame_end(&s->er, NULL);
1677 ff_mpv_frame_end(s);
1678 }
1679
1680
3/6
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 528 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 528 times.
528 if (s->width != si.width || s->height != si.height || s->context_reinit) {
1681 int err;
1682
1683 av_log(s->avctx, AV_LOG_WARNING, "Changing dimensions to %dx%d\n",
1684 si.width, si.height);
1685
1686 if (av_image_check_size(si.width, si.height, 0, s->avctx))
1687 return AVERROR_INVALIDDATA;
1688
1689 s->avctx->sample_aspect_ratio = update_sar(
1690 s->width, s->height, s->avctx->sample_aspect_ratio,
1691 si.width, si.height);
1692 s->width = si.width;
1693 s->height = si.height;
1694
1695 err = ff_set_dimensions(s->avctx, s->width, s->height);
1696 if (err < 0)
1697 return err;
1698 if ((err = ff_mpv_common_frame_size_change(s)) < 0)
1699 return err;
1700 if ((err = rv34_decoder_realloc(r)) < 0)
1701 return err;
1702 }
1703
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
528 if (faulty_b)
1704 return AVERROR_INVALIDDATA;
1705
2/2
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 18 times.
528 s->pict_type = si.type ? si.type : AV_PICTURE_TYPE_I;
1706
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 528 times.
528 if (ff_mpv_frame_start(s, s->avctx) < 0)
1707 return -1;
1708 528 ff_mpeg_er_frame_start(s);
1709
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 519 times.
528 if (!r->tmp_b_block_base) {
1710 int i;
1711
1712 9 r->tmp_b_block_base = av_malloc(s->linesize * 48);
1713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!r->tmp_b_block_base)
1714 return AVERROR(ENOMEM);
1715
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 9 times.
27 for (i = 0; i < 2; i++)
1716 18 r->tmp_b_block_y[i] = r->tmp_b_block_base
1717 18 + i * 16 * s->linesize;
1718
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 9 times.
45 for (i = 0; i < 4; i++)
1719 36 r->tmp_b_block_uv[i] = r->tmp_b_block_base + 32 * s->linesize
1720 36 + (i >> 1) * 8 * s->uvlinesize
1721 36 + (i & 1) * 16;
1722 }
1723 528 r->cur_pts = si.pts;
1724
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 388 times.
528 if (s->pict_type != AV_PICTURE_TYPE_B) {
1725 140 r->last_pts = r->next_pts;
1726 140 r->next_pts = r->cur_pts;
1727 } else {
1728 388 int refdist = GET_PTS_DIFF(r->next_pts, r->last_pts);
1729 388 int dist0 = GET_PTS_DIFF(r->cur_pts, r->last_pts);
1730 388 int dist1 = GET_PTS_DIFF(r->next_pts, r->cur_pts);
1731
1732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if(!refdist){
1733 r->mv_weight1 = r->mv_weight2 = r->weight1 = r->weight2 = 8192;
1734 r->scaled_weight = 0;
1735 }else{
1736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (FFMAX(dist0, dist1) > refdist)
1737 av_log(avctx, AV_LOG_TRACE, "distance overflow\n");
1738
1739 388 r->mv_weight1 = (dist0 << 14) / refdist;
1740 388 r->mv_weight2 = (dist1 << 14) / refdist;
1741
2/2
✓ Branch 0 taken 350 times.
✓ Branch 1 taken 38 times.
388 if((r->mv_weight1|r->mv_weight2) & 511){
1742 350 r->weight1 = r->mv_weight1;
1743 350 r->weight2 = r->mv_weight2;
1744 350 r->scaled_weight = 0;
1745 }else{
1746 38 r->weight1 = r->mv_weight1 >> 9;
1747 38 r->weight2 = r->mv_weight2 >> 9;
1748 38 r->scaled_weight = 1;
1749 }
1750 }
1751 }
1752 528 s->mb_x = s->mb_y = 0;
1753 528 ff_thread_finish_setup(s->avctx);
1754 } else if (s->context_reinit) {
1755 av_log(s->avctx, AV_LOG_ERROR, "Decoder needs full frames to "
1756 "reinitialize (start MB is %d).\n", si.start);
1757 return AVERROR_INVALIDDATA;
1758 } else if (HAVE_THREADS &&
1759 (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
1760 av_log(s->avctx, AV_LOG_ERROR, "Decoder needs full frames in frame "
1761 "multithreading mode (start MB is %d).\n", si.start);
1762 return AVERROR_INVALIDDATA;
1763 }
1764
1765
1/2
✓ Branch 0 taken 1391 times.
✗ Branch 1 not taken.
1391 for(i = 0; i < slice_count; i++){
1766 1391 int offset = get_slice_offset(avctx, slices_hdr, i , slice_count, buf_size);
1767 1391 int offset1 = get_slice_offset(avctx, slices_hdr, i+1, slice_count, buf_size);
1768 int size;
1769
1770
3/6
✓ Branch 0 taken 1391 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1391 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1391 times.
1391 if(offset < 0 || offset > offset1 || offset1 > buf_size){
1771 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1772 break;
1773 }
1774 1391 size = offset1 - offset;
1775
1776 1391 r->si.end = s->mb_width * s->mb_height;
1777 1391 s->mb_num_left = r->s.mb_x + r->s.mb_y*r->s.mb_width - r->si.start;
1778
1779
2/2
✓ Branch 0 taken 864 times.
✓ Branch 1 taken 527 times.
1391 if(i+1 < slice_count){
1780 864 int offset2 = get_slice_offset(avctx, slices_hdr, i+2, slice_count, buf_size);
1781
2/4
✓ Branch 0 taken 864 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 864 times.
864 if (offset2 < offset1 || offset2 > buf_size) {
1782 av_log(avctx, AV_LOG_ERROR, "Slice offset is invalid\n");
1783 break;
1784 }
1785 864 init_get_bits(&s->gb, buf+offset1, (buf_size-offset1)*8);
1786
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 863 times.
864 if(r->parse_slice_header(r, &r->s.gb, &si) < 0){
1787 1 size = offset2 - offset;
1788 }else
1789 863 r->si.end = si.start;
1790 }
1791
2/4
✓ Branch 0 taken 1391 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1391 times.
1391 av_assert0 (size >= 0 && size <= buf_size - offset);
1792 1391 last = rv34_decode_slice(r, r->si.end, buf + offset, size);
1793
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 863 times.
1391 if(last)
1794 528 break;
1795 }
1796
1797
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 if (s->cur_pic.ptr) {
1798
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 if (last) {
1799
1/2
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
528 if(r->loop_filter)
1800 528 r->loop_filter(r, s->mb_height - 1);
1801
1802 528 ret = finish_frame(avctx, pict);
1803
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 528 times.
528 if (ret < 0)
1804 return ret;
1805 528 *got_picture_ptr = ret;
1806 } else if (HAVE_THREADS &&
1807 (s->avctx->active_thread_type & FF_THREAD_FRAME)) {
1808 av_log(avctx, AV_LOG_INFO, "marking unfished frame as finished\n");
1809 /* always mark the current frame as finished, frame-mt supports
1810 * only complete frames */
1811 ff_er_frame_end(&s->er, NULL);
1812 ff_mpv_frame_end(s);
1813 s->mb_num_left = 0;
1814 ff_thread_progress_report(&s->cur_pic.ptr->progress, INT_MAX);
1815 return AVERROR_INVALIDDATA;
1816 }
1817 }
1818
1819 528 return avpkt->size;
1820 }
1821
1822 9 av_cold int ff_rv34_decode_end(AVCodecContext *avctx)
1823 {
1824 9 RV34DecContext *r = avctx->priv_data;
1825
1826 9 rv34_decoder_free(r);
1827
1828 9 return ff_mpv_decode_close(avctx);
1829 }
1830