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