3 * Copyright (c) 2000,2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer
6 * This file is part of FFmpeg.
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.
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.
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
30 #include "libavutil/imgutils.h"
31 #include "libavutil/thread.h"
34 #include "error_resilience.h"
39 #include "mpegutils.h"
40 #include "mpegvideo.h"
41 #include "mpeg4video.h"
42 #include "mpegvideodata.h"
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)
49 #define DC_VLC_BITS 14 // FIXME find a better solution
51 typedef struct RVDecContext {
54 int orig_width, orig_height;
57 /* The elements with negative length in the bits table correspond to
58 * open ends in the respective Huffman tree. */
59 static const uint8_t rv_sym[] = {
60 128, 127, 129, 125, 126, 130, 131, 121, 122, 123, 124, 132, 133, 134, 135,
61 113, 114, 115, 116, 117, 118, 119, 120, 136, 137, 138, 139, 140, 141, 142,
62 143, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
63 111, 112, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
64 157, 158, 159, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
65 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
66 92, 93, 94, 95, 96, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
67 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184,
68 185, 186, 187, 188, 189, 190, 191, 1, 2, 3, 4, 5, 6, 7, 8,
69 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
70 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
71 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
72 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 192, 193, 194, 195,
73 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210,
74 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225,
75 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240,
76 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
77 0, 0, 0, 0, 0, 0, 0, 0,
80 static const uint8_t rv_lum_len[] = {
81 2, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7,
82 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
83 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
84 8, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10, 10, 10, 10, 10,
85 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
86 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
87 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
88 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
89 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
90 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
91 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
92 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
93 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
94 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
95 12, 12, 12, -8, -9,-10,-11,-12,-13,-14, 14,
98 static const uint8_t rv_chrom_len[] = {
99 2, 3, 3, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8,
100 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 10, 10, 10, 10, 10,
101 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
102 10, 10, 10, 10, 10, 10, 10, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12,
103 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
104 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
105 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
106 12, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
107 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
108 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
109 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
110 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
111 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
112 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
113 14, 14, 14,-10,-11,-12,-13,-14,-15,-16, 16,
116 static VLC rv_dc_lum, rv_dc_chrom;
118 int ff_rv_decode_dc(MpegEncContext *s, int n)
123 code = get_vlc2(&s->gb, rv_dc_lum.table, DC_VLC_BITS, 2);
125 /* XXX: I don't understand why they use LONGER codes than
126 * necessary. The following code would be completely useless
127 * if they had thought about it !!! */
128 code = get_bits(&s->gb, 7);
130 code = (int8_t) (get_bits(&s->gb, 7) + 1);
131 } else if (code == 0x7d) {
132 code = -128 + get_bits(&s->gb, 7);
133 } else if (code == 0x7e) {
134 if (get_bits1(&s->gb) == 0)
135 code = (int8_t) (get_bits(&s->gb, 8) + 1);
137 code = (int8_t) (get_bits(&s->gb, 8));
138 } else if (code == 0x7f) {
139 skip_bits(&s->gb, 11);
146 code = get_vlc2(&s->gb, rv_dc_chrom.table, DC_VLC_BITS, 2);
149 code = get_bits(&s->gb, 9);
151 code = (int8_t) (get_bits(&s->gb, 7) + 1);
152 } else if (code == 0x1fd) {
153 code = -128 + get_bits(&s->gb, 7);
154 } else if (code == 0x1fe) {
155 skip_bits(&s->gb, 9);
158 av_log(s->avctx, AV_LOG_ERROR, "chroma dc error\n");
168 /* read RV 1.0 compatible frame header */
169 static int rv10_decode_picture_header(MpegEncContext *s)
171 int mb_count, pb_frame, marker, mb_xy;
173 marker = get_bits1(&s->gb);
175 if (get_bits1(&s->gb))
176 s->pict_type = AV_PICTURE_TYPE_P;
178 s->pict_type = AV_PICTURE_TYPE_I;
181 av_log(s->avctx, AV_LOG_ERROR, "marker missing\n");
183 pb_frame = get_bits1(&s->gb);
185 ff_dlog(s->avctx, "pict_type=%d pb_frame=%d\n", s->pict_type, pb_frame);
188 avpriv_request_sample(s->avctx, "PB-frame");
189 return AVERROR_PATCHWELCOME;
192 s->qscale = get_bits(&s->gb, 5);
193 if (s->qscale == 0) {
194 av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
195 return AVERROR_INVALIDDATA;
198 if (s->pict_type == AV_PICTURE_TYPE_I) {
199 if (s->rv10_version == 3) {
200 /* specific MPEG like DC coding not used */
201 s->last_dc[0] = get_bits(&s->gb, 8);
202 s->last_dc[1] = get_bits(&s->gb, 8);
203 s->last_dc[2] = get_bits(&s->gb, 8);
204 ff_dlog(s->avctx, "DC:%d %d %d\n", s->last_dc[0],
205 s->last_dc[1], s->last_dc[2]);
208 /* if multiple packets per frame are sent, the position at which
209 * to display the macroblocks is coded here */
211 mb_xy = s->mb_x + s->mb_y * s->mb_width;
212 if (show_bits(&s->gb, 12) == 0 || (mb_xy && mb_xy < s->mb_num)) {
213 s->mb_x = get_bits(&s->gb, 6); /* mb_x */
214 s->mb_y = get_bits(&s->gb, 6); /* mb_y */
215 mb_count = get_bits(&s->gb, 12);
219 mb_count = s->mb_width * s->mb_height;
221 skip_bits(&s->gb, 3); /* ignored */
223 s->unrestricted_mv = 1;
228 static int rv20_decode_picture_header(RVDecContext *rv)
230 MpegEncContext *s = &rv->m;
231 int seq, mb_pos, i, ret;
234 i = get_bits(&s->gb, 2);
237 s->pict_type = AV_PICTURE_TYPE_I;
240 s->pict_type = AV_PICTURE_TYPE_I;
243 s->pict_type = AV_PICTURE_TYPE_P;
246 s->pict_type = AV_PICTURE_TYPE_B;
249 av_log(s->avctx, AV_LOG_ERROR, "unknown frame type\n");
250 return AVERROR_INVALIDDATA;
253 if (s->low_delay && s->pict_type == AV_PICTURE_TYPE_B) {
254 av_log(s->avctx, AV_LOG_ERROR, "low delay B\n");
257 if (!s->last_picture_ptr && s->pict_type == AV_PICTURE_TYPE_B) {
258 av_log(s->avctx, AV_LOG_ERROR, "early B-frame\n");
259 return AVERROR_INVALIDDATA;
262 if (get_bits1(&s->gb)) {
263 av_log(s->avctx, AV_LOG_ERROR, "reserved bit set\n");
264 return AVERROR_INVALIDDATA;
267 s->qscale = get_bits(&s->gb, 5);
268 if (s->qscale == 0) {
269 av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
270 return AVERROR_INVALIDDATA;
273 if (RV_GET_MINOR_VER(rv->sub_id) >= 2)
274 s->loop_filter = get_bits1(&s->gb) && !s->avctx->lowres;
276 if (RV_GET_MINOR_VER(rv->sub_id) <= 1)
277 seq = get_bits(&s->gb, 8) << 7;
279 seq = get_bits(&s->gb, 13) << 2;
281 rpr_max = s->avctx->extradata[1] & 7;
284 int rpr_bits = av_log2(rpr_max) + 1;
286 f = get_bits(&s->gb, rpr_bits);
289 if (s->avctx->extradata_size < 8 + 2 * f) {
290 av_log(s->avctx, AV_LOG_ERROR, "Extradata too small.\n");
291 return AVERROR_INVALIDDATA;
294 new_w = 4 * ((uint8_t *) s->avctx->extradata)[6 + 2 * f];
295 new_h = 4 * ((uint8_t *) s->avctx->extradata)[7 + 2 * f];
297 new_w = rv->orig_width;
298 new_h = rv->orig_height;
300 if (new_w != s->width || new_h != s->height) {
301 AVRational old_aspect = s->avctx->sample_aspect_ratio;
302 av_log(s->avctx, AV_LOG_DEBUG,
303 "attempting to change resolution to %dx%d\n", new_w, new_h);
304 if (av_image_check_size(new_w, new_h, 0, s->avctx) < 0)
305 return AVERROR_INVALIDDATA;
306 ff_mpv_common_end(s);
308 // attempt to keep aspect during typical resolution switches
310 old_aspect = (AVRational){1, 1};
311 if (2 * (int64_t)new_w * s->height == (int64_t)new_h * s->width)
312 s->avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){2, 1});
313 if ((int64_t)new_w * s->height == 2 * (int64_t)new_h * s->width)
314 s->avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){1, 2});
316 ret = ff_set_dimensions(s->avctx, new_w, new_h);
322 if ((ret = ff_mpv_common_init(s)) < 0)
326 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
327 av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d/%d\n", f, rpr_bits, rpr_max);
330 if (av_image_check_size(s->width, s->height, 0, s->avctx) < 0)
331 return AVERROR_INVALIDDATA;
333 mb_pos = ff_h263_decode_mba(s);
335 seq |= s->time & ~0x7FFF;
336 if (seq - s->time > 0x4000)
338 if (seq - s->time < -0x4000)
341 if (seq != s->time) {
342 if (s->pict_type != AV_PICTURE_TYPE_B) {
344 s->pp_time = s->time - s->last_non_b_time;
345 s->last_non_b_time = s->time;
348 s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
351 if (s->pict_type == AV_PICTURE_TYPE_B) {
352 if (s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0) {
353 av_log(s->avctx, AV_LOG_DEBUG,
354 "messed up order, possible from seeking? skipping current B-frame\n");
355 #define ERROR_SKIP_FRAME -123
356 return ERROR_SKIP_FRAME;
358 ff_mpeg4_init_direct_mv(s);
361 s->no_rounding = get_bits1(&s->gb);
363 if (RV_GET_MINOR_VER(rv->sub_id) <= 1 && s->pict_type == AV_PICTURE_TYPE_B)
364 // binary decoder reads 3+2 bits here but they don't seem to be used
365 skip_bits(&s->gb, 5);
368 s->unrestricted_mv = 1;
369 s->h263_aic = s->pict_type == AV_PICTURE_TYPE_I;
370 s->modified_quant = 1;
371 if (!s->avctx->lowres)
374 if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
375 av_log(s->avctx, AV_LOG_INFO,
376 "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
377 seq, s->mb_x, s->mb_y, s->pict_type, s->qscale,
381 av_assert0(s->pict_type != AV_PICTURE_TYPE_B || !s->low_delay);
383 return s->mb_width * s->mb_height - mb_pos;
386 static av_cold void rv10_init_static(void)
388 INIT_VLC_STATIC_FROM_LENGTHS(&rv_dc_lum, DC_VLC_BITS, 263,
390 rv_sym, 1, 1, 0, 0, 16384);
391 INIT_VLC_STATIC_FROM_LENGTHS(&rv_dc_chrom, DC_VLC_BITS, 263,
393 rv_sym, 1, 1, 0, 0, 16388);
394 ff_h263_decode_init_vlc();
397 static av_cold int rv10_decode_init(AVCodecContext *avctx)
399 static AVOnce init_static_once = AV_ONCE_INIT;
400 RVDecContext *rv = avctx->priv_data;
401 MpegEncContext *s = &rv->m;
402 int major_ver, minor_ver, micro_ver, ret;
404 if (avctx->extradata_size < 8) {
405 av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n");
406 return AVERROR_INVALIDDATA;
408 if ((ret = av_image_check_size(avctx->coded_width,
409 avctx->coded_height, 0, avctx)) < 0)
412 ff_mpv_decode_defaults(s);
413 ff_mpv_decode_init(s, avctx);
415 s->out_format = FMT_H263;
418 s->width = avctx->coded_width;
420 s->height = avctx->coded_height;
422 s->h263_long_vectors = ((uint8_t *) avctx->extradata)[3] & 1;
423 rv->sub_id = AV_RB32((uint8_t *) avctx->extradata + 4);
425 major_ver = RV_GET_MAJOR_VER(rv->sub_id);
426 minor_ver = RV_GET_MINOR_VER(rv->sub_id);
427 micro_ver = RV_GET_MICRO_VER(rv->sub_id);
432 s->rv10_version = micro_ver ? 3 : 1;
433 s->obmc = micro_ver == 2;
436 if (minor_ver >= 2) {
438 s->avctx->has_b_frames = 1;
442 av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", rv->sub_id);
443 avpriv_request_sample(avctx, "RV1/2 version");
444 return AVERROR_PATCHWELCOME;
447 if (avctx->debug & FF_DEBUG_PICT_INFO) {
448 av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%"PRIX32"\n", rv->sub_id,
449 ((uint32_t *) avctx->extradata)[0]);
452 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
455 if ((ret = ff_mpv_common_init(s)) < 0)
458 ff_h263dsp_init(&s->h263dsp);
460 /* init static VLCs */
461 ff_thread_once(&init_static_once, rv10_init_static);
466 static av_cold int rv10_decode_end(AVCodecContext *avctx)
468 MpegEncContext *s = avctx->priv_data;
470 ff_mpv_common_end(s);
474 static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf,
475 int buf_size, int buf_size2, int whole_size)
477 RVDecContext *rv = avctx->priv_data;
478 MpegEncContext *s = &rv->m;
479 int mb_count, mb_pos, left, start_mb_x, active_bits_size, ret;
481 active_bits_size = buf_size * 8;
482 init_get_bits(&s->gb, buf, FFMAX(buf_size, buf_size2) * 8);
483 if (s->codec_id == AV_CODEC_ID_RV10)
484 mb_count = rv10_decode_picture_header(s);
486 mb_count = rv20_decode_picture_header(rv);
488 if (mb_count != ERROR_SKIP_FRAME)
489 av_log(s->avctx, AV_LOG_ERROR, "HEADER ERROR\n");
490 return AVERROR_INVALIDDATA;
493 if (s->mb_x >= s->mb_width ||
494 s->mb_y >= s->mb_height) {
495 av_log(s->avctx, AV_LOG_ERROR, "POS ERROR %d %d\n", s->mb_x, s->mb_y);
496 return AVERROR_INVALIDDATA;
498 mb_pos = s->mb_y * s->mb_width + s->mb_x;
499 left = s->mb_width * s->mb_height - mb_pos;
500 if (mb_count > left) {
501 av_log(s->avctx, AV_LOG_ERROR, "COUNT ERROR\n");
502 return AVERROR_INVALIDDATA;
505 if (whole_size < s->mb_width * s->mb_height / 8)
506 return AVERROR_INVALIDDATA;
508 if ((s->mb_x == 0 && s->mb_y == 0) || !s->current_picture_ptr) {
509 // FIXME write parser so we always have complete frames?
510 if (s->current_picture_ptr) {
511 ff_er_frame_end(&s->er);
513 s->mb_x = s->mb_y = s->resync_mb_x = s->resync_mb_y = 0;
515 if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
517 ff_mpeg_er_frame_start(s);
519 if (s->current_picture_ptr->f->pict_type != s->pict_type) {
520 av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
521 return AVERROR_INVALIDDATA;
526 ff_dlog(avctx, "qscale=%d\n", s->qscale);
528 /* default quantization values */
529 if (s->codec_id == AV_CODEC_ID_RV10) {
531 s->first_slice_line = 1;
533 s->first_slice_line = 1;
534 s->resync_mb_x = s->mb_x;
536 start_mb_x = s->mb_x;
537 s->resync_mb_y = s->mb_y;
539 s->y_dc_scale_table =
540 s->c_dc_scale_table = ff_aic_dc_scale_table;
542 s->y_dc_scale_table =
543 s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
546 if (s->modified_quant)
547 s->chroma_qscale_table = ff_h263_chroma_qscale_table;
549 ff_set_qscale(s, s->qscale);
551 s->rv10_first_dc_coded[0] = 0;
552 s->rv10_first_dc_coded[1] = 0;
553 s->rv10_first_dc_coded[2] = 0;
557 s->block_wrap[3] = s->b8_stride;
559 s->block_wrap[5] = s->mb_stride;
560 ff_init_block_index(s);
562 /* decode each macroblock */
563 for (s->mb_num_left = mb_count; s->mb_num_left > 0; s->mb_num_left--) {
565 ff_update_block_index(s);
566 ff_tlog(avctx, "**mb x=%d y=%d\n", s->mb_x, s->mb_y);
568 s->mv_dir = MV_DIR_FORWARD;
569 s->mv_type = MV_TYPE_16X16;
570 ret = ff_h263_decode_mb(s, s->block);
572 // Repeat the slice end check from ff_h263_decode_mb with our active
574 if (ret != SLICE_ERROR && active_bits_size >= get_bits_count(&s->gb)) {
575 int v = show_bits(&s->gb, 16);
577 if (get_bits_count(&s->gb) + 16 > active_bits_size)
578 v >>= get_bits_count(&s->gb) + 16 - active_bits_size;
583 if (ret != SLICE_ERROR && active_bits_size < get_bits_count(&s->gb) &&
584 8 * buf_size2 >= get_bits_count(&s->gb)) {
585 active_bits_size = buf_size2 * 8;
586 av_log(avctx, AV_LOG_DEBUG, "update size from %d to %d\n",
587 8 * buf_size, active_bits_size);
591 if (ret == SLICE_ERROR || active_bits_size < get_bits_count(&s->gb)) {
592 av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x,
594 return AVERROR_INVALIDDATA;
596 if (s->pict_type != AV_PICTURE_TYPE_B)
597 ff_h263_update_motion_val(s);
598 ff_mpv_reconstruct_mb(s, s->block);
600 ff_h263_loop_filter(s);
602 if (++s->mb_x == s->mb_width) {
605 ff_init_block_index(s);
607 if (s->mb_x == s->resync_mb_x)
608 s->first_slice_line = 0;
609 if (ret == SLICE_END)
613 ff_er_add_slice(&s->er, start_mb_x, s->resync_mb_y, s->mb_x - 1, s->mb_y,
616 return active_bits_size;
619 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
621 if (avctx->slice_count)
622 return avctx->slice_offset[n];
624 return AV_RL32(buf + n * 8);
627 static int rv10_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
630 const uint8_t *buf = avpkt->data;
631 int buf_size = avpkt->size;
632 MpegEncContext *s = avctx->priv_data;
633 AVFrame *pict = data;
636 const uint8_t *slices_hdr = NULL;
638 ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
640 /* no supplementary picture */
645 if (!avctx->slice_count) {
646 slice_count = (*buf++) + 1;
649 if (!slice_count || buf_size <= 8 * slice_count) {
650 av_log(avctx, AV_LOG_ERROR, "Invalid slice count: %d.\n",
652 return AVERROR_INVALIDDATA;
655 slices_hdr = buf + 4;
656 buf += 8 * slice_count;
657 buf_size -= 8 * slice_count;
659 slice_count = avctx->slice_count;
661 for (i = 0; i < slice_count; i++) {
662 unsigned offset = get_slice_offset(avctx, slices_hdr, i);
665 if (offset >= buf_size)
666 return AVERROR_INVALIDDATA;
668 if (i + 1 == slice_count)
669 size = buf_size - offset;
671 size = get_slice_offset(avctx, slices_hdr, i + 1) - offset;
673 if (i + 2 >= slice_count)
674 size2 = buf_size - offset;
676 size2 = get_slice_offset(avctx, slices_hdr, i + 2) - offset;
678 if (size <= 0 || size2 <= 0 ||
679 offset + FFMAX(size, size2) > buf_size)
680 return AVERROR_INVALIDDATA;
682 if ((ret = rv10_decode_packet(avctx, buf + offset, size, size2, buf_size)) < 0)
689 if (s->current_picture_ptr && s->mb_y >= s->mb_height) {
690 ff_er_frame_end(&s->er);
693 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
694 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
696 ff_print_debug_info(s, s->current_picture_ptr, pict);
697 ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
698 } else if (s->last_picture_ptr) {
699 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
701 ff_print_debug_info(s, s->last_picture_ptr, pict);
702 ff_mpv_export_qp_table(s, pict,s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
705 if (s->last_picture_ptr || s->low_delay) {
709 // so we can detect if frame_end was not called (find some nicer solution...)
710 s->current_picture_ptr = NULL;
716 AVCodec ff_rv10_decoder = {
718 .long_name = NULL_IF_CONFIG_SMALL("RealVideo 1.0"),
719 .type = AVMEDIA_TYPE_VIDEO,
720 .id = AV_CODEC_ID_RV10,
721 .priv_data_size = sizeof(RVDecContext),
722 .init = rv10_decode_init,
723 .close = rv10_decode_end,
724 .decode = rv10_decode_frame,
725 .capabilities = AV_CODEC_CAP_DR1,
726 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
728 .pix_fmts = (const enum AVPixelFormat[]) {
734 AVCodec ff_rv20_decoder = {
736 .long_name = NULL_IF_CONFIG_SMALL("RealVideo 2.0"),
737 .type = AVMEDIA_TYPE_VIDEO,
738 .id = AV_CODEC_ID_RV20,
739 .priv_data_size = sizeof(RVDecContext),
740 .init = rv10_decode_init,
741 .close = rv10_decode_end,
742 .decode = rv10_decode_frame,
743 .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
744 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
745 .flush = ff_mpeg_flush,
747 .pix_fmts = (const enum AVPixelFormat[]) {