FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rv10.c
Date: 2023-03-31 03:41:15
Exec Total Coverage
Lines: 261 362 72.1%
Functions: 9 10 90.0%
Branches: 150 238 63.0%

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