FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rv10.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 264 367 71.9%
Branches: 152 242 62.8%

Line Branch Exec Source
1 /*
2 * RV10/RV20 decoder
3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * RV10/RV20 decoder
26 */
27
28 #include <inttypes.h>
29
30 #include "libavutil/imgutils.h"
31 #include "libavutil/thread.h"
32
33 #include "avcodec.h"
34 #include "error_resilience.h"
35 #include "h263.h"
36 #include "h263data.h"
37 #include "internal.h"
38 #include "mpeg_er.h"
39 #include "mpegutils.h"
40 #include "mpegvideo.h"
41 #include "mpeg4video.h"
42 #include "mpegvideodata.h"
43 #include "rv10.h"
44
45 #define RV_GET_MAJOR_VER(x) ((x) >> 28)
46 #define RV_GET_MINOR_VER(x) (((x) >> 20) & 0xFF)
47 #define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF)
48
49 #define MAX_VLC_ENTRIES 1023 // Note: Does not include the skip entries.
50 #define DC_VLC_BITS 9
51
52 typedef struct RVDecContext {
53 MpegEncContext m;
54 int sub_id;
55 int orig_width, orig_height;
56 } RVDecContext;
57
58 /* (run, length) encoded value for the symbols table. The actual symbols
59 * are run..run - length (mod 256).
60 * The last two entries in the following table apply to luma only.
61 * The skip values are not included in this list. */
62 static const uint8_t rv_sym_run_len[][2] = {
63 { 0, 0 }, { 1, 0 }, { 255, 0 }, { 3, 1 }, { 254, 1 },
64 { 7, 3 }, { 252, 3 }, { 15, 7 }, { 248, 7 }, { 31, 15 },
65 { 240, 15 }, { 63, 31 }, { 224, 31 }, { 127, 63 }, { 192, 63 },
66 { 255, 127 }, { 128, 127 }, { 127, 255 }, { 128, 255 },
67 };
68
69 /* entry[i] of the following tables gives
70 * the number of VLC codes of length i + 2. */
71 static const uint16_t rv_lum_len_count[15] = {
72 1, 0, 2, 4, 8, 16, 32, 0, 64, 0, 128, 0, 256, 0, 512,
73 };
74
75 static const uint16_t rv_chrom_len_count[15] = {
76 1, 2, 4, 0, 8, 0, 16, 0, 32, 0, 64, 0, 128, 0, 256,
77 };
78
79 static VLC rv_dc_lum, rv_dc_chrom;
80
81 int ff_rv_decode_dc(MpegEncContext *s, int n)
82 {
83 int code;
84
85 if (n < 4) {
86 code = get_vlc2(&s->gb, rv_dc_lum.table, DC_VLC_BITS, 2);
87 } else {
88 code = get_vlc2(&s->gb, rv_dc_chrom.table, DC_VLC_BITS, 2);
89 if (code < 0) {
90 av_log(s->avctx, AV_LOG_ERROR, "chroma dc error\n");
91 return -1;
92 }
93 }
94 return code;
95 }
96
97 /* read RV 1.0 compatible frame header */
98 150 static int rv10_decode_picture_header(MpegEncContext *s)
99 {
100 int mb_count, pb_frame, marker, mb_xy;
101
102 150 marker = get_bits1(&s->gb);
103
104
2/2
✓ Branch 1 taken 135 times.
✓ Branch 2 taken 15 times.
150 if (get_bits1(&s->gb))
105 135 s->pict_type = AV_PICTURE_TYPE_P;
106 else
107 15 s->pict_type = AV_PICTURE_TYPE_I;
108
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (!marker)
110 av_log(s->avctx, AV_LOG_ERROR, "marker missing\n");
111
112 150 pb_frame = get_bits1(&s->gb);
113
114 ff_dlog(s->avctx, "pict_type=%d pb_frame=%d\n", s->pict_type, pb_frame);
115
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (pb_frame) {
117 avpriv_request_sample(s->avctx, "PB-frame");
118 return AVERROR_PATCHWELCOME;
119 }
120
121 150 s->qscale = get_bits(&s->gb, 5);
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150 times.
150 if (s->qscale == 0) {
123 av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
124 return AVERROR_INVALIDDATA;
125 }
126
127
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 135 times.
150 if (s->pict_type == AV_PICTURE_TYPE_I) {
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (s->rv10_version == 3) {
129 /* specific MPEG like DC coding not used */
130 s->last_dc[0] = get_bits(&s->gb, 8);
131 s->last_dc[1] = get_bits(&s->gb, 8);
132 s->last_dc[2] = get_bits(&s->gb, 8);
133 ff_dlog(s->avctx, "DC:%d %d %d\n", s->last_dc[0],
134 s->last_dc[1], s->last_dc[2]);
135 }
136 }
137 /* if multiple packets per frame are sent, the position at which
138 * to display the macroblocks is coded here */
139
140 150 mb_xy = s->mb_x + s->mb_y * s->mb_width;
141
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
150 if (show_bits(&s->gb, 12) == 0 || (mb_xy && mb_xy < s->mb_num)) {
142 150 s->mb_x = get_bits(&s->gb, 6); /* mb_x */
143 150 s->mb_y = get_bits(&s->gb, 6); /* mb_y */
144 150 mb_count = get_bits(&s->gb, 12);
145 } else {
146 s->mb_x = 0;
147 s->mb_y = 0;
148 mb_count = s->mb_width * s->mb_height;
149 }
150 150 skip_bits(&s->gb, 3); /* ignored */
151 150 s->f_code = 1;
152
153 150 return mb_count;
154 }
155
156 742 static int rv20_decode_picture_header(RVDecContext *rv, int whole_size)
157 {
158 742 MpegEncContext *s = &rv->m;
159 int seq, mb_pos, i, ret;
160 int rpr_max;
161
162 742 i = get_bits(&s->gb, 2);
163
4/5
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 494 times.
✓ Branch 3 taken 181 times.
✗ Branch 4 not taken.
742 switch (i) {
164 52 case 0:
165 52 s->pict_type = AV_PICTURE_TYPE_I;
166 52 break;
167 15 case 1:
168 15 s->pict_type = AV_PICTURE_TYPE_I;
169 15 break; // hmm ...
170 494 case 2:
171 494 s->pict_type = AV_PICTURE_TYPE_P;
172 494 break;
173 181 case 3:
174 181 s->pict_type = AV_PICTURE_TYPE_B;
175 181 break;
176 default:
177 av_log(s->avctx, AV_LOG_ERROR, "unknown frame type\n");
178 return AVERROR_INVALIDDATA;
179 }
180
181
3/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 592 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 150 times.
742 if (s->low_delay && s->pict_type == AV_PICTURE_TYPE_B) {
182 av_log(s->avctx, AV_LOG_ERROR, "low delay B\n");
183 return -1;
184 }
185
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 731 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
742 if (!s->last_picture_ptr && s->pict_type == AV_PICTURE_TYPE_B) {
186 av_log(s->avctx, AV_LOG_ERROR, "early B-frame\n");
187 return AVERROR_INVALIDDATA;
188 }
189
190
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 742 times.
742 if (get_bits1(&s->gb)) {
191 av_log(s->avctx, AV_LOG_ERROR, "reserved bit set\n");
192 return AVERROR_INVALIDDATA;
193 }
194
195 742 s->qscale = get_bits(&s->gb, 5);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 742 times.
742 if (s->qscale == 0) {
197 av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
198 return AVERROR_INVALIDDATA;
199 }
200
201
2/2
✓ Branch 0 taken 592 times.
✓ Branch 1 taken 150 times.
742 if (RV_GET_MINOR_VER(rv->sub_id) >= 2)
202
3/4
✓ Branch 1 taken 413 times.
✓ Branch 2 taken 179 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 413 times.
592 s->loop_filter = get_bits1(&s->gb) && !s->avctx->lowres;
203
204
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 592 times.
742 if (RV_GET_MINOR_VER(rv->sub_id) <= 1)
205 150 seq = get_bits(&s->gb, 8) << 7;
206 else
207 592 seq = get_bits(&s->gb, 13) << 2;
208
209 742 rpr_max = s->avctx->extradata[1] & 7;
210
2/2
✓ Branch 0 taken 592 times.
✓ Branch 1 taken 150 times.
742 if (rpr_max) {
211 int f, new_w, new_h;
212 592 int rpr_bits = av_log2(rpr_max) + 1;
213
214 592 f = get_bits(&s->gb, rpr_bits);
215
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
592 if (f) {
217 if (s->avctx->extradata_size < 8 + 2 * f) {
218 av_log(s->avctx, AV_LOG_ERROR, "Extradata too small.\n");
219 return AVERROR_INVALIDDATA;
220 }
221
222 new_w = 4 * ((uint8_t *) s->avctx->extradata)[6 + 2 * f];
223 new_h = 4 * ((uint8_t *) s->avctx->extradata)[7 + 2 * f];
224 } else {
225 592 new_w = rv->orig_width;
226 592 new_h = rv->orig_height;
227 }
228
3/6
✓ Branch 0 taken 592 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 592 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 592 times.
592 if (new_w != s->width || new_h != s->height || !s->context_initialized) {
229 AVRational old_aspect = s->avctx->sample_aspect_ratio;
230 av_log(s->avctx, AV_LOG_DEBUG,
231 "attempting to change resolution to %dx%d\n", new_w, new_h);
232 if (av_image_check_size(new_w, new_h, 0, s->avctx) < 0)
233 return AVERROR_INVALIDDATA;
234
235 if (whole_size < (new_w + 15)/16 * ((new_h + 15)/16) / 8)
236 return AVERROR_INVALIDDATA;
237
238 ff_mpv_common_end(s);
239
240 // attempt to keep aspect during typical resolution switches
241 if (!old_aspect.num)
242 old_aspect = (AVRational){1, 1};
243 if (2 * (int64_t)new_w * s->height == (int64_t)new_h * s->width)
244 s->avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){2, 1});
245 if ((int64_t)new_w * s->height == 2 * (int64_t)new_h * s->width)
246 s->avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){1, 2});
247
248 ret = ff_set_dimensions(s->avctx, new_w, new_h);
249 if (ret < 0)
250 return ret;
251
252 s->width = new_w;
253 s->height = new_h;
254 if ((ret = ff_mpv_common_init(s)) < 0)
255 return ret;
256 }
257
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 592 times.
592 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
259 av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d/%d\n", f, rpr_bits, rpr_max);
260 }
261 }
262
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 742 times.
742 if (av_image_check_size(s->width, s->height, 0, s->avctx) < 0)
263 return AVERROR_INVALIDDATA;
264
265 742 mb_pos = ff_h263_decode_mba(s);
266
267 742 seq |= s->time & ~0x7FFF;
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 742 times.
742 if (seq - s->time > 0x4000)
269 seq -= 0x8000;
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 742 times.
742 if (seq - s->time < -0x4000)
271 seq += 0x8000;
272
273
2/2
✓ Branch 0 taken 292 times.
✓ Branch 1 taken 450 times.
742 if (seq != s->time) {
274
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 72 times.
292 if (s->pict_type != AV_PICTURE_TYPE_B) {
275 220 s->time = seq;
276 220 s->pp_time = s->time - s->last_non_b_time;
277 220 s->last_non_b_time = s->time;
278 } else {
279 72 s->time = seq;
280 72 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
281 }
282 }
283
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 561 times.
742 if (s->pict_type == AV_PICTURE_TYPE_B) {
284
3/6
✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 181 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 181 times.
181 if (s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0) {
285 av_log(s->avctx, AV_LOG_DEBUG,
286 "messed up order, possible from seeking? skipping current B-frame\n");
287 #define ERROR_SKIP_FRAME -123
288 return ERROR_SKIP_FRAME;
289 }
290 181 ff_mpeg4_init_direct_mv(s);
291 }
292
293 742 s->no_rounding = get_bits1(&s->gb);
294
295
3/4
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 592 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 150 times.
742 if (RV_GET_MINOR_VER(rv->sub_id) <= 1 && s->pict_type == AV_PICTURE_TYPE_B)
296 // binary decoder reads 3+2 bits here but they don't seem to be used
297 skip_bits(&s->gb, 5);
298
299 742 s->f_code = 1;
300 742 s->h263_aic = s->pict_type == AV_PICTURE_TYPE_I;
301 742 s->modified_quant = 1;
302
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 592 times.
742 if (!s->avctx->lowres)
303 150 s->loop_filter = 1;
304
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 742 times.
742 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
306 av_log(s->avctx, AV_LOG_INFO,
307 "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
308 seq, s->mb_x, s->mb_y, s->pict_type, s->qscale,
309 s->no_rounding);
310 }
311
312
3/4
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 561 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 181 times.
742 av_assert0(s->pict_type != AV_PICTURE_TYPE_B || !s->low_delay);
313
314 742 return s->mb_width * s->mb_height - mb_pos;
315 }
316
317 26 static av_cold void rv10_build_vlc(VLC *vlc, const uint16_t len_count[15],
318 const uint8_t sym_rl[][2], int sym_rl_elems)
319 {
320 uint16_t syms[MAX_VLC_ENTRIES];
321 uint8_t lens[MAX_VLC_ENTRIES];
322 26 unsigned nb_syms = 0, nb_lens = 0;
323
324
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 26 times.
494 for (unsigned i = 0; i < sym_rl_elems; i++) {
325 468 unsigned cur_sym = sym_rl[i][0];
326
2/2
✓ Branch 0 taken 19942 times.
✓ Branch 1 taken 468 times.
20410 for (unsigned tmp = nb_syms + sym_rl[i][1]; nb_syms <= tmp; nb_syms++)
327 19942 syms[nb_syms] = 0xFF & cur_sym--;
328 }
329
330
2/2
✓ Branch 0 taken 390 times.
✓ Branch 1 taken 26 times.
416 for (unsigned i = 0; i < 15; i++)
331
2/2
✓ Branch 0 taken 19942 times.
✓ Branch 1 taken 390 times.
20332 for (unsigned tmp = nb_lens + len_count[i]; nb_lens < tmp; nb_lens++)
332 19942 lens[nb_lens] = i + 2;
333 av_assert1(nb_lens == nb_syms);
334 26 ff_init_vlc_from_lengths(vlc, DC_VLC_BITS, nb_lens, lens, 1,
335 syms, 2, 2, 0, INIT_VLC_STATIC_OVERLONG, NULL);
336 26 }
337
338 13 static av_cold void rv10_init_static(void)
339 {
340 static VLC_TYPE table[1472 + 992][2];
341
342 13 rv_dc_lum.table = table;
343 13 rv_dc_lum.table_allocated = 1472;
344 13 rv10_build_vlc(&rv_dc_lum, rv_lum_len_count,
345 rv_sym_run_len, FF_ARRAY_ELEMS(rv_sym_run_len));
346
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 13 times.
65 for (int i = 0; i < 1 << (DC_VLC_BITS - 7 /* Length of skip prefix */); i++) {
347 /* All codes beginning with 0x7F have the same length and value.
348 * Modifying the table directly saves us the useless subtables. */
349 52 rv_dc_lum.table[(0x7F << (DC_VLC_BITS - 7)) + i][0] = 255;
350 52 rv_dc_lum.table[(0x7F << (DC_VLC_BITS - 7)) + i][1] = 18;
351 }
352 13 rv_dc_chrom.table = &table[1472];
353 13 rv_dc_chrom.table_allocated = 992;
354 13 rv10_build_vlc(&rv_dc_chrom, rv_chrom_len_count,
355 rv_sym_run_len, FF_ARRAY_ELEMS(rv_sym_run_len) - 2);
356
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 13 times.
26 for (int i = 0; i < 1 << (DC_VLC_BITS - 9 /* Length of skip prefix */); i++) {
357 /* Same as above. */
358 13 rv_dc_chrom.table[(0x1FE << (DC_VLC_BITS - 9)) + i][0] = 255;
359 13 rv_dc_chrom.table[(0x1FE << (DC_VLC_BITS - 9)) + i][1] = 18;
360 }
361 13 ff_h263_decode_init_vlc();
362 13 }
363
364 20 static av_cold int rv10_decode_init(AVCodecContext *avctx)
365 {
366 static AVOnce init_static_once = AV_ONCE_INIT;
367 20 RVDecContext *rv = avctx->priv_data;
368 20 MpegEncContext *s = &rv->m;
369 int major_ver, minor_ver, micro_ver, ret;
370
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (avctx->extradata_size < 8) {
372 av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n");
373 return AVERROR_INVALIDDATA;
374 }
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if ((ret = av_image_check_size(avctx->coded_width,
376 20 avctx->coded_height, 0, avctx)) < 0)
377 return ret;
378
379 20 ff_mpv_decode_init(s, avctx);
380
381 20 s->out_format = FMT_H263;
382
383 20 rv->orig_width =
384 20 s->width = avctx->coded_width;
385 20 rv->orig_height =
386 20 s->height = avctx->coded_height;
387
388 20 s->h263_long_vectors = ((uint8_t *) avctx->extradata)[3] & 1;
389 20 rv->sub_id = AV_RB32((uint8_t *) avctx->extradata + 4);
390
391 20 major_ver = RV_GET_MAJOR_VER(rv->sub_id);
392 20 minor_ver = RV_GET_MINOR_VER(rv->sub_id);
393 20 micro_ver = RV_GET_MICRO_VER(rv->sub_id);
394
395 20 s->low_delay = 1;
396
2/3
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
20 switch (major_ver) {
397 9 case 1:
398
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 s->rv10_version = micro_ver ? 3 : 1;
399 9 s->obmc = micro_ver == 2;
400 9 break;
401 11 case 2:
402
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 8 times.
11 if (minor_ver >= 2) {
403 3 s->low_delay = 0;
404 3 s->avctx->has_b_frames = 1;
405 }
406 11 break;
407 default:
408 av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", rv->sub_id);
409 avpriv_request_sample(avctx, "RV1/2 version");
410 return AVERROR_PATCHWELCOME;
411 }
412
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (avctx->debug & FF_DEBUG_PICT_INFO) {
414 av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%"PRIX32"\n", rv->sub_id,
415 ((uint32_t *) avctx->extradata)[0]);
416 }
417
418 20 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
419
420 20 ff_mpv_idct_init(s);
421
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
20 if ((ret = ff_mpv_common_init(s)) < 0)
422 return ret;
423
424 20 ff_h263dsp_init(&s->h263dsp);
425
426 /* init static VLCs */
427 20 ff_thread_once(&init_static_once, rv10_init_static);
428
429 20 return 0;
430 }
431
432 20 static av_cold int rv10_decode_end(AVCodecContext *avctx)
433 {
434 20 MpegEncContext *s = avctx->priv_data;
435
436 20 ff_mpv_common_end(s);
437 20 return 0;
438 }
439
440 892 static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf,
441 int buf_size, int buf_size2, int whole_size)
442 {
443 892 RVDecContext *rv = avctx->priv_data;
444 892 MpegEncContext *s = &rv->m;
445 int mb_count, mb_pos, left, start_mb_x, active_bits_size, ret;
446
447 892 active_bits_size = buf_size * 8;
448 892 init_get_bits(&s->gb, buf, FFMAX(buf_size, buf_size2) * 8);
449
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 742 times.
892 if (s->codec_id == AV_CODEC_ID_RV10)
450 150 mb_count = rv10_decode_picture_header(s);
451 else
452 742 mb_count = rv20_decode_picture_header(rv, whole_size);
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 892 times.
892 if (mb_count < 0) {
454 if (mb_count != ERROR_SKIP_FRAME)
455 av_log(s->avctx, AV_LOG_ERROR, "HEADER ERROR\n");
456 return AVERROR_INVALIDDATA;
457 }
458
459
1/2
✓ Branch 0 taken 892 times.
✗ Branch 1 not taken.
892 if (s->mb_x >= s->mb_width ||
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 892 times.
892 s->mb_y >= s->mb_height) {
461 av_log(s->avctx, AV_LOG_ERROR, "POS ERROR %d %d\n", s->mb_x, s->mb_y);
462 return AVERROR_INVALIDDATA;
463 }
464 892 mb_pos = s->mb_y * s->mb_width + s->mb_x;
465 892 left = s->mb_width * s->mb_height - mb_pos;
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 892 times.
892 if (mb_count > left) {
467 av_log(s->avctx, AV_LOG_ERROR, "COUNT ERROR\n");
468 return AVERROR_INVALIDDATA;
469 }
470
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 892 times.
892 if (whole_size < s->mb_width * s->mb_height / 8)
472 return AVERROR_INVALIDDATA;
473
474
4/6
✓ Branch 0 taken 445 times.
✓ Branch 1 taken 447 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 445 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 447 times.
892 if ((s->mb_x == 0 && s->mb_y == 0) || !s->current_picture_ptr) {
475 // FIXME write parser so we always have complete frames?
476
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 445 times.
445 if (s->current_picture_ptr) {
477 ff_er_frame_end(&s->er);
478 ff_mpv_frame_end(s);
479 s->mb_x = s->mb_y = s->resync_mb_x = s->resync_mb_y = 0;
480 }
481
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 445 times.
445 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
482 return ret;
483 445 ff_mpeg_er_frame_start(s);
484 } else {
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 447 times.
447 if (s->current_picture_ptr->f->pict_type != s->pict_type) {
486 av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
487 return AVERROR_INVALIDDATA;
488 }
489 }
490
491
492 ff_dlog(avctx, "qscale=%d\n", s->qscale);
493
494 /* default quantization values */
495
2/2
✓ Branch 0 taken 150 times.
✓ Branch 1 taken 742 times.
892 if (s->codec_id == AV_CODEC_ID_RV10) {
496
1/2
✓ Branch 0 taken 150 times.
✗ Branch 1 not taken.
150 if (s->mb_y == 0)
497 150 s->first_slice_line = 1;
498 } else {
499 742 s->first_slice_line = 1;
500 742 s->resync_mb_x = s->mb_x;
501 }
502 892 start_mb_x = s->mb_x;
503 892 s->resync_mb_y = s->mb_y;
504
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 825 times.
892 if (s->h263_aic) {
505 67 s->y_dc_scale_table =
506 67 s->c_dc_scale_table = ff_aic_dc_scale_table;
507 } else {
508 825 s->y_dc_scale_table =
509 825 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
510 }
511
512
2/2
✓ Branch 0 taken 742 times.
✓ Branch 1 taken 150 times.
892 if (s->modified_quant)
513 742 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
514
515 892 ff_set_qscale(s, s->qscale);
516
517 892 s->rv10_first_dc_coded[0] = 0;
518 892 s->rv10_first_dc_coded[1] = 0;
519 892 s->rv10_first_dc_coded[2] = 0;
520 892 s->block_wrap[0] =
521 892 s->block_wrap[1] =
522 892 s->block_wrap[2] =
523 892 s->block_wrap[3] = s->b8_stride;
524 892 s->block_wrap[4] =
525 892 s->block_wrap[5] = s->mb_stride;
526 892 ff_init_block_index(s);
527
528 /* decode each macroblock */
529
1/2
✓ Branch 0 taken 162300 times.
✗ Branch 1 not taken.
162300 for (s->mb_num_left = mb_count; s->mb_num_left > 0; s->mb_num_left--) {
530 int ret;
531 162300 ff_update_block_index(s);
532 ff_tlog(avctx, "**mb x=%d y=%d\n", s->mb_x, s->mb_y);
533
534 162300 s->mv_dir = MV_DIR_FORWARD;
535 162300 s->mv_type = MV_TYPE_16X16;
536 162300 ret = ff_h263_decode_mb(s, s->block);
537
538 // Repeat the slice end check from ff_h263_decode_mb with our active
539 // bitstream size
540
2/4
✓ Branch 0 taken 162300 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 162300 times.
✗ Branch 4 not taken.
162300 if (ret != SLICE_ERROR && active_bits_size >= get_bits_count(&s->gb)) {
541 162300 int v = show_bits(&s->gb, 16);
542
543
2/2
✓ Branch 1 taken 1341 times.
✓ Branch 2 taken 160959 times.
162300 if (get_bits_count(&s->gb) + 16 > active_bits_size)
544 1341 v >>= get_bits_count(&s->gb) + 16 - active_bits_size;
545
546
2/2
✓ Branch 0 taken 892 times.
✓ Branch 1 taken 161408 times.
162300 if (!v)
547 892 ret = SLICE_END;
548 }
549
2/6
✓ Branch 0 taken 162300 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 162300 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
162300 if (ret != SLICE_ERROR && active_bits_size < get_bits_count(&s->gb) &&
550 8 * buf_size2 >= get_bits_count(&s->gb)) {
551 active_bits_size = buf_size2 * 8;
552 av_log(avctx, AV_LOG_DEBUG, "update size from %d to %d\n",
553 8 * buf_size, active_bits_size);
554 ret = SLICE_OK;
555 }
556
557
2/4
✓ Branch 0 taken 162300 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 162300 times.
162300 if (ret == SLICE_ERROR || active_bits_size < get_bits_count(&s->gb)) {
558 av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x,
559 s->mb_y);
560 return AVERROR_INVALIDDATA;
561 }
562
2/2
✓ Branch 0 taken 140700 times.
✓ Branch 1 taken 21600 times.
162300 if (s->pict_type != AV_PICTURE_TYPE_B)
563 140700 ff_h263_update_motion_val(s);
564 162300 ff_mpv_reconstruct_mb(s, s->block);
565
2/2
✓ Branch 0 taken 59400 times.
✓ Branch 1 taken 102900 times.
162300 if (s->loop_filter)
566 59400 ff_h263_loop_filter(s);
567
568
2/2
✓ Branch 0 taken 7575 times.
✓ Branch 1 taken 154725 times.
162300 if (++s->mb_x == s->mb_width) {
569 7575 s->mb_x = 0;
570 7575 s->mb_y++;
571 7575 ff_init_block_index(s);
572 }
573
2/2
✓ Branch 0 taken 7297 times.
✓ Branch 1 taken 155003 times.
162300 if (s->mb_x == s->resync_mb_x)
574 7297 s->first_slice_line = 0;
575
2/2
✓ Branch 0 taken 892 times.
✓ Branch 1 taken 161408 times.
162300 if (ret == SLICE_END)
576 892 break;
577 }
578
579 892 ff_er_add_slice(&s->er, start_mb_x, s->resync_mb_y, s->mb_x - 1, s->mb_y,
580 ER_MB_END);
581
582 892 return active_bits_size;
583 }
584
585 1667 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
586 {
587
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1667 times.
1667 if (avctx->slice_count)
588 return avctx->slice_offset[n];
589 else
590 1667 return AV_RL32(buf + n * 8);
591 }
592
593 449 static int rv10_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
594 AVPacket *avpkt)
595 {
596 449 const uint8_t *buf = avpkt->data;
597 449 int buf_size = avpkt->size;
598 449 MpegEncContext *s = avctx->priv_data;
599 449 AVFrame *pict = data;
600 int i, ret;
601 int slice_count;
602 449 const uint8_t *slices_hdr = NULL;
603
604 ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
605
606 /* no supplementary picture */
607
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 445 times.
449 if (buf_size == 0) {
608 4 return 0;
609 }
610
611
1/2
✓ Branch 0 taken 445 times.
✗ Branch 1 not taken.
445 if (!avctx->slice_count) {
612 445 slice_count = (*buf++) + 1;
613 445 buf_size--;
614
615
2/4
✓ Branch 0 taken 445 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 445 times.
445 if (!slice_count || buf_size <= 8 * slice_count) {
616 av_log(avctx, AV_LOG_ERROR, "Invalid slice count: %d.\n",
617 slice_count);
618 return AVERROR_INVALIDDATA;
619 }
620
621 445 slices_hdr = buf + 4;
622 445 buf += 8 * slice_count;
623 445 buf_size -= 8 * slice_count;
624 } else
625 slice_count = avctx->slice_count;
626
627
2/2
✓ Branch 0 taken 892 times.
✓ Branch 1 taken 445 times.
1337 for (i = 0; i < slice_count; i++) {
628 892 unsigned offset = get_slice_offset(avctx, slices_hdr, i);
629 int size, size2;
630
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 892 times.
892 if (offset >= buf_size)
632 return AVERROR_INVALIDDATA;
633
634
2/2
✓ Branch 0 taken 445 times.
✓ Branch 1 taken 447 times.
892 if (i + 1 == slice_count)
635 445 size = buf_size - offset;
636 else
637 447 size = get_slice_offset(avctx, slices_hdr, i + 1) - offset;
638
639
2/2
✓ Branch 0 taken 564 times.
✓ Branch 1 taken 328 times.
892 if (i + 2 >= slice_count)
640 564 size2 = buf_size - offset;
641 else
642 328 size2 = get_slice_offset(avctx, slices_hdr, i + 2) - offset;
643
644
2/4
✓ Branch 0 taken 892 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 892 times.
✗ Branch 3 not taken.
892 if (size <= 0 || size2 <= 0 ||
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 892 times.
892 offset + FFMAX(size, size2) > buf_size)
646 return AVERROR_INVALIDDATA;
647
648
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 892 times.
892 if ((ret = rv10_decode_packet(avctx, buf + offset, size, size2, buf_size)) < 0)
649 return ret;
650
651
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 892 times.
892 if (ret > 8 * size)
652 i++;
653 }
654
655
2/4
✓ Branch 0 taken 445 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 445 times.
✗ Branch 3 not taken.
445 if (s->current_picture_ptr && s->mb_y >= s->mb_height) {
656 445 ff_er_frame_end(&s->er);
657 445 ff_mpv_frame_end(s);
658
659
4/4
✓ Branch 0 taken 373 times.
✓ Branch 1 taken 72 times.
✓ Branch 2 taken 300 times.
✓ Branch 3 taken 73 times.
445 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
660
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 372 times.
372 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
661 return ret;
662 372 ff_print_debug_info(s, s->current_picture_ptr, pict);
663 372 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
664
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 1 times.
73 } else if (s->last_picture_ptr) {
665
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
666 return ret;
667 72 ff_print_debug_info(s, s->last_picture_ptr, pict);
668 72 ff_mpv_export_qp_table(s, pict,s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
669 }
670
671
4/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 438 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
445 if (s->last_picture_ptr || s->low_delay) {
672 444 *got_frame = 1;
673 }
674
675 // so we can detect if frame_end was not called (find some nicer solution...)
676 445 s->current_picture_ptr = NULL;
677 }
678
679 445 return avpkt->size;
680 }
681
682 const AVCodec ff_rv10_decoder = {
683 .name = "rv10",
684 .long_name = NULL_IF_CONFIG_SMALL("RealVideo 1.0"),
685 .type = AVMEDIA_TYPE_VIDEO,
686 .id = AV_CODEC_ID_RV10,
687 .priv_data_size = sizeof(RVDecContext),
688 .init = rv10_decode_init,
689 .close = rv10_decode_end,
690 .decode = rv10_decode_frame,
691 .capabilities = AV_CODEC_CAP_DR1,
692 .max_lowres = 3,
693 .pix_fmts = (const enum AVPixelFormat[]) {
694 AV_PIX_FMT_YUV420P,
695 AV_PIX_FMT_NONE
696 },
697 };
698
699 const AVCodec ff_rv20_decoder = {
700 .name = "rv20",
701 .long_name = NULL_IF_CONFIG_SMALL("RealVideo 2.0"),
702 .type = AVMEDIA_TYPE_VIDEO,
703 .id = AV_CODEC_ID_RV20,
704 .priv_data_size = sizeof(RVDecContext),
705 .init = rv10_decode_init,
706 .close = rv10_decode_end,
707 .decode = rv10_decode_frame,
708 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
709 .flush = ff_mpeg_flush,
710 .max_lowres = 3,
711 .pix_fmts = (const enum AVPixelFormat[]) {
712 AV_PIX_FMT_YUV420P,
713 AV_PIX_FMT_NONE
714 },
715 };
716