FFmpeg
rv10.c
Go to the documentation of this file.
1 /*
2  * RV10/RV20 decoder
3  * Copyright (c) 2000,2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 /**
24  * @file
25  * RV10/RV20 decoder
26  */
27 
28 #include <inttypes.h>
29 
30 #include "libavutil/imgutils.h"
31 #include "libavutil/thread.h"
32 
33 #include "avcodec.h"
34 #include "error_resilience.h"
35 #include "h263.h"
36 #include "h263data.h"
37 #include "internal.h"
38 #include "mpeg_er.h"
39 #include "mpegutils.h"
40 #include "mpegvideo.h"
41 #include "mpeg4video.h"
42 #include "mpegvideodata.h"
43 #include "rv10.h"
44 
45 #define RV_GET_MAJOR_VER(x) ((x) >> 28)
46 #define RV_GET_MINOR_VER(x) (((x) >> 20) & 0xFF)
47 #define RV_GET_MICRO_VER(x) (((x) >> 12) & 0xFF)
48 
49 #define MAX_VLC_ENTRIES 1023 // Note: Does not include the skip entries.
50 #define DC_VLC_BITS 9
51 
52 typedef struct RVDecContext {
54  int sub_id;
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 
80 
82 {
83  int code;
84 
85  if (n < 4) {
86  code = get_vlc2(&s->gb, rv_dc_lum.table, DC_VLC_BITS, 2);
87  } else {
88  code = get_vlc2(&s->gb, rv_dc_chrom.table, DC_VLC_BITS, 2);
89  if (code < 0) {
90  av_log(s->avctx, AV_LOG_ERROR, "chroma dc error\n");
91  return -1;
92  }
93  }
94  return code;
95 }
96 
97 /* read RV 1.0 compatible frame header */
99 {
100  int mb_count, pb_frame, marker, mb_xy;
101 
102  marker = get_bits1(&s->gb);
103 
104  if (get_bits1(&s->gb))
106  else
108 
109  if (!marker)
110  av_log(s->avctx, AV_LOG_ERROR, "marker missing\n");
111 
112  pb_frame = get_bits1(&s->gb);
113 
114  ff_dlog(s->avctx, "pict_type=%d pb_frame=%d\n", s->pict_type, pb_frame);
115 
116  if (pb_frame) {
117  avpriv_request_sample(s->avctx, "PB-frame");
118  return AVERROR_PATCHWELCOME;
119  }
120 
121  s->qscale = get_bits(&s->gb, 5);
122  if (s->qscale == 0) {
123  av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
124  return AVERROR_INVALIDDATA;
125  }
126 
127  if (s->pict_type == AV_PICTURE_TYPE_I) {
128  if (s->rv10_version == 3) {
129  /* specific MPEG like DC coding not used */
130  s->last_dc[0] = get_bits(&s->gb, 8);
131  s->last_dc[1] = get_bits(&s->gb, 8);
132  s->last_dc[2] = get_bits(&s->gb, 8);
133  ff_dlog(s->avctx, "DC:%d %d %d\n", s->last_dc[0],
134  s->last_dc[1], s->last_dc[2]);
135  }
136  }
137  /* if multiple packets per frame are sent, the position at which
138  * to display the macroblocks is coded here */
139 
140  mb_xy = s->mb_x + s->mb_y * s->mb_width;
141  if (show_bits(&s->gb, 12) == 0 || (mb_xy && mb_xy < s->mb_num)) {
142  s->mb_x = get_bits(&s->gb, 6); /* mb_x */
143  s->mb_y = get_bits(&s->gb, 6); /* mb_y */
144  mb_count = get_bits(&s->gb, 12);
145  } else {
146  s->mb_x = 0;
147  s->mb_y = 0;
148  mb_count = s->mb_width * s->mb_height;
149  }
150  skip_bits(&s->gb, 3); /* ignored */
151  s->f_code = 1;
152  s->unrestricted_mv = 1;
153 
154  return mb_count;
155 }
156 
158 {
159  MpegEncContext *s = &rv->m;
160  int seq, mb_pos, i, ret;
161  int rpr_max;
162 
163  i = get_bits(&s->gb, 2);
164  switch (i) {
165  case 0:
167  break;
168  case 1:
170  break; // hmm ...
171  case 2:
173  break;
174  case 3:
176  break;
177  default:
178  av_log(s->avctx, AV_LOG_ERROR, "unknown frame type\n");
179  return AVERROR_INVALIDDATA;
180  }
181 
182  if (s->low_delay && s->pict_type == AV_PICTURE_TYPE_B) {
183  av_log(s->avctx, AV_LOG_ERROR, "low delay B\n");
184  return -1;
185  }
186  if (!s->last_picture_ptr && s->pict_type == AV_PICTURE_TYPE_B) {
187  av_log(s->avctx, AV_LOG_ERROR, "early B-frame\n");
188  return AVERROR_INVALIDDATA;
189  }
190 
191  if (get_bits1(&s->gb)) {
192  av_log(s->avctx, AV_LOG_ERROR, "reserved bit set\n");
193  return AVERROR_INVALIDDATA;
194  }
195 
196  s->qscale = get_bits(&s->gb, 5);
197  if (s->qscale == 0) {
198  av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
199  return AVERROR_INVALIDDATA;
200  }
201 
202  if (RV_GET_MINOR_VER(rv->sub_id) >= 2)
203  s->loop_filter = get_bits1(&s->gb) && !s->avctx->lowres;
204 
205  if (RV_GET_MINOR_VER(rv->sub_id) <= 1)
206  seq = get_bits(&s->gb, 8) << 7;
207  else
208  seq = get_bits(&s->gb, 13) << 2;
209 
210  rpr_max = s->avctx->extradata[1] & 7;
211  if (rpr_max) {
212  int f, new_w, new_h;
213  int rpr_bits = av_log2(rpr_max) + 1;
214 
215  f = get_bits(&s->gb, rpr_bits);
216 
217  if (f) {
218  if (s->avctx->extradata_size < 8 + 2 * f) {
219  av_log(s->avctx, AV_LOG_ERROR, "Extradata too small.\n");
220  return AVERROR_INVALIDDATA;
221  }
222 
223  new_w = 4 * ((uint8_t *) s->avctx->extradata)[6 + 2 * f];
224  new_h = 4 * ((uint8_t *) s->avctx->extradata)[7 + 2 * f];
225  } else {
226  new_w = rv->orig_width;
227  new_h = rv->orig_height;
228  }
229  if (new_w != s->width || new_h != s->height) {
230  AVRational old_aspect = s->avctx->sample_aspect_ratio;
232  "attempting to change resolution to %dx%d\n", new_w, new_h);
233  if (av_image_check_size(new_w, new_h, 0, s->avctx) < 0)
234  return AVERROR_INVALIDDATA;
236 
237  // attempt to keep aspect during typical resolution switches
238  if (!old_aspect.num)
239  old_aspect = (AVRational){1, 1};
240  if (2 * (int64_t)new_w * s->height == (int64_t)new_h * s->width)
241  s->avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){2, 1});
242  if ((int64_t)new_w * s->height == 2 * (int64_t)new_h * s->width)
243  s->avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){1, 2});
244 
245  ret = ff_set_dimensions(s->avctx, new_w, new_h);
246  if (ret < 0)
247  return ret;
248 
249  s->width = new_w;
250  s->height = new_h;
251  if ((ret = ff_mpv_common_init(s)) < 0)
252  return ret;
253  }
254 
255  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
256  av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d/%d\n", f, rpr_bits, rpr_max);
257  }
258  }
259  if (av_image_check_size(s->width, s->height, 0, s->avctx) < 0)
260  return AVERROR_INVALIDDATA;
261 
262  mb_pos = ff_h263_decode_mba(s);
263 
264  seq |= s->time & ~0x7FFF;
265  if (seq - s->time > 0x4000)
266  seq -= 0x8000;
267  if (seq - s->time < -0x4000)
268  seq += 0x8000;
269 
270  if (seq != s->time) {
271  if (s->pict_type != AV_PICTURE_TYPE_B) {
272  s->time = seq;
273  s->pp_time = s->time - s->last_non_b_time;
274  s->last_non_b_time = s->time;
275  } else {
276  s->time = seq;
277  s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
278  }
279  }
280  if (s->pict_type == AV_PICTURE_TYPE_B) {
281  if (s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0) {
283  "messed up order, possible from seeking? skipping current B-frame\n");
284 #define ERROR_SKIP_FRAME -123
285  return ERROR_SKIP_FRAME;
286  }
288  }
289 
290  s->no_rounding = get_bits1(&s->gb);
291 
292  if (RV_GET_MINOR_VER(rv->sub_id) <= 1 && s->pict_type == AV_PICTURE_TYPE_B)
293  // binary decoder reads 3+2 bits here but they don't seem to be used
294  skip_bits(&s->gb, 5);
295 
296  s->f_code = 1;
297  s->unrestricted_mv = 1;
299  s->modified_quant = 1;
300  if (!s->avctx->lowres)
301  s->loop_filter = 1;
302 
303  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
305  "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
306  seq, s->mb_x, s->mb_y, s->pict_type, s->qscale,
307  s->no_rounding);
308  }
309 
311 
312  return s->mb_width * s->mb_height - mb_pos;
313 }
314 
315 static av_cold void rv10_build_vlc(VLC *vlc, const uint16_t len_count[15],
316  const uint8_t sym_rl[][2], int sym_rl_elems)
317 {
318  uint16_t syms[MAX_VLC_ENTRIES];
319  uint8_t lens[MAX_VLC_ENTRIES];
320  unsigned nb_syms = 0, nb_lens = 0;
321 
322  for (unsigned i = 0; i < sym_rl_elems; i++) {
323  unsigned cur_sym = sym_rl[i][0];
324  for (unsigned tmp = nb_syms + sym_rl[i][1]; nb_syms <= tmp; nb_syms++)
325  syms[nb_syms] = 0xFF & cur_sym--;
326  }
327 
328  for (unsigned i = 0; i < 15; i++)
329  for (unsigned tmp = nb_lens + len_count[i]; nb_lens < tmp; nb_lens++)
330  lens[nb_lens] = i + 2;
331  av_assert1(nb_lens == nb_syms);
332  ff_init_vlc_from_lengths(vlc, DC_VLC_BITS, nb_lens, lens, 1,
333  syms, 2, 2, 0, INIT_VLC_STATIC_OVERLONG, NULL);
334 }
335 
336 static av_cold void rv10_init_static(void)
337 {
338  static VLC_TYPE table[1472 + 992][2];
339 
340  rv_dc_lum.table = table;
341  rv_dc_lum.table_allocated = 1472;
342  rv10_build_vlc(&rv_dc_lum, rv_lum_len_count,
344  for (int i = 0; i < 1 << (DC_VLC_BITS - 7 /* Length of skip prefix */); i++) {
345  /* All codes beginning with 0x7F have the same length and value.
346  * Modifying the table directly saves us the useless subtables. */
347  rv_dc_lum.table[(0x7F << (DC_VLC_BITS - 7)) + i][0] = 255;
348  rv_dc_lum.table[(0x7F << (DC_VLC_BITS - 7)) + i][1] = 18;
349  }
350  rv_dc_chrom.table = &table[1472];
351  rv_dc_chrom.table_allocated = 992;
352  rv10_build_vlc(&rv_dc_chrom, rv_chrom_len_count,
354  for (int i = 0; i < 1 << (DC_VLC_BITS - 9 /* Length of skip prefix */); i++) {
355  /* Same as above. */
356  rv_dc_chrom.table[(0x1FE << (DC_VLC_BITS - 9)) + i][0] = 255;
357  rv_dc_chrom.table[(0x1FE << (DC_VLC_BITS - 9)) + i][1] = 18;
358  }
360 }
361 
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_init(s, avctx);
378 
379  s->out_format = FMT_H263;
380 
381  rv->orig_width =
382  s->width = avctx->coded_width;
383  rv->orig_height =
384  s->height = avctx->coded_height;
385 
386  s->h263_long_vectors = ((uint8_t *) avctx->extradata)[3] & 1;
387  rv->sub_id = AV_RB32((uint8_t *) avctx->extradata + 4);
388 
389  major_ver = RV_GET_MAJOR_VER(rv->sub_id);
390  minor_ver = RV_GET_MINOR_VER(rv->sub_id);
391  micro_ver = RV_GET_MICRO_VER(rv->sub_id);
392 
393  s->low_delay = 1;
394  switch (major_ver) {
395  case 1:
396  s->rv10_version = micro_ver ? 3 : 1;
397  s->obmc = micro_ver == 2;
398  break;
399  case 2:
400  if (minor_ver >= 2) {
401  s->low_delay = 0;
402  s->avctx->has_b_frames = 1;
403  }
404  break;
405  default:
406  av_log(s->avctx, AV_LOG_ERROR, "unknown header %X\n", rv->sub_id);
407  avpriv_request_sample(avctx, "RV1/2 version");
408  return AVERROR_PATCHWELCOME;
409  }
410 
411  if (avctx->debug & FF_DEBUG_PICT_INFO) {
412  av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%"PRIX32"\n", rv->sub_id,
413  ((uint32_t *) avctx->extradata)[0]);
414  }
415 
416  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
417 
418  ff_mpv_idct_init(s);
419  if ((ret = ff_mpv_common_init(s)) < 0)
420  return ret;
421 
423 
424  /* init static VLCs */
425  ff_thread_once(&init_static_once, rv10_init_static);
426 
427  return 0;
428 }
429 
431 {
432  MpegEncContext *s = avctx->priv_data;
433 
435  return 0;
436 }
437 
438 static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf,
439  int buf_size, int buf_size2, int whole_size)
440 {
441  RVDecContext *rv = avctx->priv_data;
442  MpegEncContext *s = &rv->m;
443  int mb_count, mb_pos, left, start_mb_x, active_bits_size, ret;
444 
445  active_bits_size = buf_size * 8;
446  init_get_bits(&s->gb, buf, FFMAX(buf_size, buf_size2) * 8);
447  if (s->codec_id == AV_CODEC_ID_RV10)
448  mb_count = rv10_decode_picture_header(s);
449  else
450  mb_count = rv20_decode_picture_header(rv);
451  if (mb_count < 0) {
452  if (mb_count != ERROR_SKIP_FRAME)
453  av_log(s->avctx, AV_LOG_ERROR, "HEADER ERROR\n");
454  return AVERROR_INVALIDDATA;
455  }
456 
457  if (s->mb_x >= s->mb_width ||
458  s->mb_y >= s->mb_height) {
459  av_log(s->avctx, AV_LOG_ERROR, "POS ERROR %d %d\n", s->mb_x, s->mb_y);
460  return AVERROR_INVALIDDATA;
461  }
462  mb_pos = s->mb_y * s->mb_width + s->mb_x;
463  left = s->mb_width * s->mb_height - mb_pos;
464  if (mb_count > left) {
465  av_log(s->avctx, AV_LOG_ERROR, "COUNT ERROR\n");
466  return AVERROR_INVALIDDATA;
467  }
468 
469  if (whole_size < s->mb_width * s->mb_height / 8)
470  return AVERROR_INVALIDDATA;
471 
472  if ((s->mb_x == 0 && s->mb_y == 0) || !s->current_picture_ptr) {
473  // FIXME write parser so we always have complete frames?
474  if (s->current_picture_ptr) {
475  ff_er_frame_end(&s->er);
476  ff_mpv_frame_end(s);
477  s->mb_x = s->mb_y = s->resync_mb_x = s->resync_mb_y = 0;
478  }
479  if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
480  return ret;
482  } else {
483  if (s->current_picture_ptr->f->pict_type != s->pict_type) {
484  av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
485  return AVERROR_INVALIDDATA;
486  }
487  }
488 
489 
490  ff_dlog(avctx, "qscale=%d\n", s->qscale);
491 
492  /* default quantization values */
493  if (s->codec_id == AV_CODEC_ID_RV10) {
494  if (s->mb_y == 0)
495  s->first_slice_line = 1;
496  } else {
497  s->first_slice_line = 1;
498  s->resync_mb_x = s->mb_x;
499  }
500  start_mb_x = s->mb_x;
501  s->resync_mb_y = s->mb_y;
502  if (s->h263_aic) {
503  s->y_dc_scale_table =
505  } else {
506  s->y_dc_scale_table =
508  }
509 
510  if (s->modified_quant)
512 
513  ff_set_qscale(s, s->qscale);
514 
515  s->rv10_first_dc_coded[0] = 0;
516  s->rv10_first_dc_coded[1] = 0;
517  s->rv10_first_dc_coded[2] = 0;
518  s->block_wrap[0] =
519  s->block_wrap[1] =
520  s->block_wrap[2] =
521  s->block_wrap[3] = s->b8_stride;
522  s->block_wrap[4] =
523  s->block_wrap[5] = s->mb_stride;
525 
526  /* decode each macroblock */
527  for (s->mb_num_left = mb_count; s->mb_num_left > 0; s->mb_num_left--) {
528  int ret;
530  ff_tlog(avctx, "**mb x=%d y=%d\n", s->mb_x, s->mb_y);
531 
532  s->mv_dir = MV_DIR_FORWARD;
533  s->mv_type = MV_TYPE_16X16;
534  ret = ff_h263_decode_mb(s, s->block);
535 
536  // Repeat the slice end check from ff_h263_decode_mb with our active
537  // bitstream size
538  if (ret != SLICE_ERROR && active_bits_size >= get_bits_count(&s->gb)) {
539  int v = show_bits(&s->gb, 16);
540 
541  if (get_bits_count(&s->gb) + 16 > active_bits_size)
542  v >>= get_bits_count(&s->gb) + 16 - active_bits_size;
543 
544  if (!v)
545  ret = SLICE_END;
546  }
547  if (ret != SLICE_ERROR && active_bits_size < get_bits_count(&s->gb) &&
548  8 * buf_size2 >= get_bits_count(&s->gb)) {
549  active_bits_size = buf_size2 * 8;
550  av_log(avctx, AV_LOG_DEBUG, "update size from %d to %d\n",
551  8 * buf_size, active_bits_size);
552  ret = SLICE_OK;
553  }
554 
555  if (ret == SLICE_ERROR || active_bits_size < get_bits_count(&s->gb)) {
556  av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x,
557  s->mb_y);
558  return AVERROR_INVALIDDATA;
559  }
560  if (s->pict_type != AV_PICTURE_TYPE_B)
563  if (s->loop_filter)
565 
566  if (++s->mb_x == s->mb_width) {
567  s->mb_x = 0;
568  s->mb_y++;
570  }
571  if (s->mb_x == s->resync_mb_x)
572  s->first_slice_line = 0;
573  if (ret == SLICE_END)
574  break;
575  }
576 
577  ff_er_add_slice(&s->er, start_mb_x, s->resync_mb_y, s->mb_x - 1, s->mb_y,
578  ER_MB_END);
579 
580  return active_bits_size;
581 }
582 
583 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
584 {
585  if (avctx->slice_count)
586  return avctx->slice_offset[n];
587  else
588  return AV_RL32(buf + n * 8);
589 }
590 
591 static int rv10_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
592  AVPacket *avpkt)
593 {
594  const uint8_t *buf = avpkt->data;
595  int buf_size = avpkt->size;
596  MpegEncContext *s = avctx->priv_data;
597  AVFrame *pict = data;
598  int i, ret;
599  int slice_count;
600  const uint8_t *slices_hdr = NULL;
601 
602  ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
603 
604  /* no supplementary picture */
605  if (buf_size == 0) {
606  return 0;
607  }
608 
609  if (!avctx->slice_count) {
610  slice_count = (*buf++) + 1;
611  buf_size--;
612 
613  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  slices_hdr = buf + 4;
620  buf += 8 * slice_count;
621  buf_size -= 8 * slice_count;
622  } else
623  slice_count = avctx->slice_count;
624 
625  for (i = 0; i < slice_count; i++) {
626  unsigned offset = get_slice_offset(avctx, slices_hdr, i);
627  int size, size2;
628 
629  if (offset >= buf_size)
630  return AVERROR_INVALIDDATA;
631 
632  if (i + 1 == slice_count)
633  size = buf_size - offset;
634  else
635  size = get_slice_offset(avctx, slices_hdr, i + 1) - offset;
636 
637  if (i + 2 >= slice_count)
638  size2 = buf_size - offset;
639  else
640  size2 = get_slice_offset(avctx, slices_hdr, i + 2) - offset;
641 
642  if (size <= 0 || size2 <= 0 ||
643  offset + FFMAX(size, size2) > buf_size)
644  return AVERROR_INVALIDDATA;
645 
646  if ((ret = rv10_decode_packet(avctx, buf + offset, size, size2, buf_size)) < 0)
647  return ret;
648 
649  if (ret > 8 * size)
650  i++;
651  }
652 
653  if (s->current_picture_ptr && s->mb_y >= s->mb_height) {
654  ff_er_frame_end(&s->er);
655  ff_mpv_frame_end(s);
656 
657  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
658  if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
659  return ret;
662  } else if (s->last_picture_ptr) {
663  if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
664  return ret;
667  }
668 
669  if (s->last_picture_ptr || s->low_delay) {
670  *got_frame = 1;
671  }
672 
673  // so we can detect if frame_end was not called (find some nicer solution...)
675  }
676 
677  return avpkt->size;
678 }
679 
681  .name = "rv10",
682  .long_name = NULL_IF_CONFIG_SMALL("RealVideo 1.0"),
683  .type = AVMEDIA_TYPE_VIDEO,
684  .id = AV_CODEC_ID_RV10,
685  .priv_data_size = sizeof(RVDecContext),
687  .close = rv10_decode_end,
689  .capabilities = AV_CODEC_CAP_DR1,
690  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
691  .max_lowres = 3,
692  .pix_fmts = (const enum AVPixelFormat[]) {
695  },
696 };
697 
699  .name = "rv20",
700  .long_name = NULL_IF_CONFIG_SMALL("RealVideo 2.0"),
701  .type = AVMEDIA_TYPE_VIDEO,
702  .id = AV_CODEC_ID_RV20,
703  .priv_data_size = sizeof(RVDecContext),
705  .close = rv10_decode_end,
707  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
708  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
709  .flush = ff_mpeg_flush,
710  .max_lowres = 3,
711  .pix_fmts = (const enum AVPixelFormat[]) {
714  },
715 };
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:48
int rv10_first_dc_coded[3]
Definition: mpegvideo.h:423
#define ff_tlog(ctx,...)
Definition: internal.h:86
#define NULL
Definition: coverity.c:32
#define SLICE_ERROR
Definition: mpegvideo.h:522
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2272
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
Definition: rv10.c:583
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static void flush(AVCodecContext *avctx)
const uint8_t * y_dc_scale_table
qscale -> y_dc_scale table
Definition: mpegvideo.h:188
#define MAX_VLC_ENTRIES
Definition: rv10.c:49
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:719
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
#define ER_MB_END
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:106
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define avpriv_request_sample(...)
void ff_er_frame_end(ERContext *s)
int orig_height
Definition: rv10.c:55
int num
Numerator.
Definition: rational.h:59
av_cold void ff_h263dsp_init(H263DSPContext *ctx)
Definition: h263dsp.c:117
int size
Definition: packet.h:364
enum AVCodecID codec_id
Definition: mpegvideo.h:112
AVCodec ff_rv20_decoder
Definition: rv10.c:698
int sub_id
Definition: rv10.c:54
int av_log2(unsigned v)
Definition: intmath.c:26
int obmc
overlapped block motion compensation
Definition: mpegvideo.h:366
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:910
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
mpegvideo header.
#define FF_ARRAY_ELEMS(a)
#define SLICE_OK
Definition: mpegvideo.h:521
static int rv10_decode_picture_header(MpegEncContext *s)
Definition: rv10.c:98
AVCodec.
Definition: codec.h:190
int qscale
QP.
Definition: mpegvideo.h:204
int h263_aic
Advanced INTRA Coding (AIC)
Definition: mpegvideo.h:87
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
int block_wrap[6]
Definition: mpegvideo.h:294
int ff_h263_decode_mb(MpegEncContext *s, int16_t block[6][64])
Definition: ituh263dec.c:696
int modified_quant
Definition: mpegvideo.h:381
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:75
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int mb_num_left
number of MBs left in this video packet (for partitioned Slices only)
Definition: mpegvideo.h:359
int64_t time
time of current frame
Definition: mpegvideo.h:392
const uint8_t ff_mpeg1_dc_scale_table[128]
Definition: mpegvideodata.c:33
void ff_h263_decode_init_vlc(void)
Definition: ituh263dec.c:108
uint8_t
#define av_cold
Definition: attributes.h:88
enum OutputFormat out_format
output format
Definition: mpegvideo.h:104
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1619
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
#define f(width, name)
Definition: cbs_vp9.c:255
#define RV_GET_MICRO_VER(x)
Definition: rv10.c:47
int av_frame_ref(AVFrame *dst, const AVFrame *src)
Set up a new reference to the data described by the source frame.
Definition: frame.c:443
int ff_rv_decode_dc(MpegEncContext *s, int n)
Definition: rv10.c:81
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:632
int no_rounding
apply no rounding to motion compensation (MPEG-4, msmpeg4, ...) for B-frames rounding mode is always ...
Definition: mpegvideo.h:284
H.263 tables.
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:91
uint8_t * data
Definition: packet.h:363
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
MpegEncContext m
Definition: rv10.c:53
int ff_h263_decode_mba(MpegEncContext *s)
Definition: ituh263dec.c:138
#define ff_dlog(a,...)
#define ERROR_SKIP_FRAME
uint16_t pp_time
time distance between the last 2 p,s,i frames
Definition: mpegvideo.h:394
av_cold void ff_mpv_idct_init(MpegEncContext *s)
Definition: mpegvideo.c:331
int mb_height
number of MBs horizontally & vertically
Definition: mpegvideo.h:129
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
Definition: avcodec.h:1749
ptrdiff_t size
Definition: opengl_enc.c:100
#define AVOnce
Definition: thread.h:172
#define av_log(a,...)
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:750
static const uint16_t table[]
Definition: prosumer.c:206
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:2336
static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int buf_size2, int whole_size)
Definition: rv10.c:438
int slice_count
slice count
Definition: avcodec.h:885
static const uint8_t rv_sym_run_len[][2]
Definition: rv10.c:62
H263DSPContext h263dsp
Definition: mpegvideo.h:237
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:821
int last_dc[3]
last DC values for MPEG-1
Definition: mpegvideo.h:185
static const uint16_t rv_chrom_len_count[15]
Definition: rv10.c:75
AVCodec ff_rv10_decoder
Definition: rv10.c:680
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
int unrestricted_mv
mv can point outside of the coded picture
Definition: mpegvideo.h:223
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
ERContext er
Definition: mpegvideo.h:571
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define RV_GET_MINOR_VER(x)
Definition: rv10.c:46
void ff_mpeg4_init_direct_mv(MpegEncContext *s)
Definition: mpeg4video.c:71
const char * name
Name of the codec implementation.
Definition: codec.h:197
int low_delay
no reordering needed / has no B-frames
Definition: mpegvideo.h:408
GetBitContext gb
Definition: mpegvideo.h:453
void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1109
#define FFMAX(a, b)
Definition: common.h:103
Definition: vlc.h:26
void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo.c:2305
int resync_mb_x
x position of last resync marker
Definition: mpegvideo.h:356
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:317
void ff_h263_loop_filter(MpegEncContext *s)
Definition: h263.c:135
int ff_mpv_export_qp_table(MpegEncContext *s, AVFrame *f, Picture *p, int qp_type)
Definition: mpegvideo.c:1425
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:397
const uint8_t ff_h263_chroma_qscale_table[32]
Definition: h263data.c:262
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
static av_cold void rv10_init_static(void)
Definition: rv10.c:336
#define RV_GET_MAJOR_VER(x)
Definition: rv10.c:45
#define DC_VLC_BITS
Definition: rv10.c:50
Picture * current_picture_ptr
pointer to the current picture
Definition: mpegvideo.h:184
void ff_mpeg_er_frame_start(MpegEncContext *s)
Definition: mpeg_er.c:46
static const uint16_t rv_lum_len_count[15]
Definition: rv10.c:71
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
static int rv10_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: rv10.c:591
#define s(width, name)
Definition: cbs_vp9.c:257
static av_always_inline int get_vlc2(GetBitContext *s, VLC_TYPE(*table)[2], int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:797
static av_cold int rv10_decode_end(AVCodecContext *avctx)
Definition: rv10.c:430
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
int table_allocated
Definition: vlc.h:29
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:266
int first_slice_line
used in MPEG-4 too to handle resync markers
Definition: mpegvideo.h:438
const uint8_t ff_aic_dc_scale_table[32]
Definition: h263data.c:247
static av_cold int rv10_decode_init(AVCodecContext *avctx)
Definition: rv10.c:362
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
#define AV_ONCE_INIT
Definition: thread.h:173
int64_t last_non_b_time
Definition: mpegvideo.h:393
Libavcodec external API header.
void ff_h263_update_motion_val(MpegEncContext *s)
Definition: h263.c:42
int debug
debug
Definition: avcodec.h:1618
main external API structure.
Definition: avcodec.h:531
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:100
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2]...the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so...,+,-,+,-,+,+,-,+,-,+,...hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32-hcoeff[1]-hcoeff[2]-...a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2}an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||.........intra?||||:Block01:yes no||||:Block02:.................||||:Block03::y DC::ref index:||||:Block04::cb DC::motion x:||||.........:cr DC::motion y:||||.................|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------------------------------|||Y subbands||Cb subbands||Cr subbands||||------||------||------|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||------||------||------||||------||------||------|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||------||------||------||||------||------||------|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||------||------||------||||------||------||------|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------------------------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction------------|\Dequantization-------------------\||Reference frames|\IDWT|--------------|Motion\|||Frame 0||Frame 1||Compensation.OBMC v-------|--------------|--------------.\------> Frame n output Frame Frame<----------------------------------/|...|-------------------Range Coder:============Binary Range Coder:-------------------The implemented range coder is an adapted version based upon"Range encoding: an algorithm for removing redundancy from a digitised message."by G.N.N.Martin.The symbols encoded by the Snow range coder are bits(0|1).The associated probabilities are not fix but change depending on the symbol mix seen so far.bit seen|new state---------+-----------------------------------------------0|256-state_transition_table[256-old_state];1|state_transition_table[old_state];state_transition_table={0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:-------------------------FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1.the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:206
void ff_print_debug_info(MpegEncContext *s, Picture *p, AVFrame *pict)
Definition: mpegvideo.c:1418
#define SLICE_END
end marker found
Definition: mpegvideo.h:523
int extradata_size
Definition: avcodec.h:633
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
int coded_height
Definition: avcodec.h:719
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
Rational number (pair of numerator and denominator).
Definition: rational.h:58
struct AVFrame * f
Definition: mpegpicture.h:46
int ff_init_vlc_from_lengths(VLC *vlc_arg, int nb_bits, int nb_codes, const int8_t *lens, int lens_wrap, const void *symbols, int symbols_wrap, int symbols_size, int offset, int flags, void *logctx)
Build VLC decoding tables suitable for use with get_vlc2()
Definition: bitstream.c:381
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
generic function called after decoding the header and before a frame is decoded.
Definition: mpegvideo.c:1186
int f_code
forward MV resolution
Definition: mpegvideo.h:238
#define MV_DIR_FORWARD
Definition: mpegvideo.h:262
int pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:212
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
const uint8_t * c_dc_scale_table
qscale -> c_dc_scale table
Definition: mpegvideo.h:189
int b8_stride
2*mb_width+1 used for some 8x8 block arrays to allow simple addressing
Definition: mpegvideo.h:131
MpegEncContext.
Definition: mpegvideo.h:81
static av_cold void rv10_build_vlc(VLC *vlc, const uint16_t len_count[15], const uint8_t sym_rl[][2], int sym_rl_elems)
Definition: rv10.c:315
struct AVCodecContext * avctx
Definition: mpegvideo.h:98
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
common internal api header.
int mb_stride
mb_width+1 used for some arrays to allow simple addressing of left & top MBs without sig11 ...
Definition: mpegvideo.h:130
void ff_mpv_decode_init(MpegEncContext *s, AVCodecContext *avctx)
Initialize the given MpegEncContext for decoding.
Definition: mpegvideo.c:669
Picture * last_picture_ptr
pointer to the previous picture.
Definition: mpegvideo.h:182
Bi-dir predicted.
Definition: avutil.h:276
const uint8_t * chroma_qscale_table
qscale -> chroma_qscale (H.263)
Definition: mpegvideo.h:190
void * priv_data
Definition: avcodec.h:558
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition: mpegvideo.c:883
static int rv20_decode_picture_header(RVDecContext *rv)
Definition: rv10.c:157
void ff_mpv_frame_end(MpegEncContext *s)
Definition: mpegvideo.c:1410
void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mpegvideo.c:2253
int resync_mb_y
y position of last resync marker
Definition: mpegvideo.h:357
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:513
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:901
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1222
int rv10_version
RV10 version: 0 or 3.
Definition: mpegvideo.h:422
#define VLC_TYPE
Definition: vlc.h:24
int h263_long_vectors
use horrible H.263v1 long vector mode
Definition: mpegvideo.h:224
static VLC rv_dc_lum
Definition: rv10.c:79
int orig_width
Definition: rv10.c:55
#define FF_QSCALE_TYPE_MPEG1
Definition: internal.h:92
int32_t rv
Definition: input.c:405
#define INIT_VLC_STATIC_OVERLONG
Definition: vlc.h:96
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:91
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: packet.h:340
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
int i
Definition: input.c:407
Predicted.
Definition: avutil.h:275
uint16_t pb_time
time distance between the last b and p,s,i frame
Definition: mpegvideo.h:395
static VLC rv_dc_chrom
Definition: rv10.c:79
static uint8_t tmp[11]
Definition: aes_ctr.c:27