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