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 "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 {
56  int sub_id;
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 
82 
84 {
85  int code;
86 
87  if (n < 4) {
89  } else {
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 */
101 {
102  int mb_count, pb_frame, marker, mb_xy;
103 
104  marker = get_bits1(&s->gb);
105 
106  if (get_bits1(&s->gb))
107  s->pict_type = AV_PICTURE_TYPE_P;
108  else
109  s->pict_type = AV_PICTURE_TYPE_I;
110 
111  if (!marker)
112  av_log(s->avctx, AV_LOG_ERROR, "marker missing\n");
113 
114  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  if (pb_frame) {
119  avpriv_request_sample(s->avctx, "PB-frame");
120  return AVERROR_PATCHWELCOME;
121  }
122 
123  s->qscale = get_bits(&s->gb, 5);
124  if (s->qscale == 0) {
125  av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
126  return AVERROR_INVALIDDATA;
127  }
128 
129  if (s->pict_type == AV_PICTURE_TYPE_I) {
130  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  mb_xy = s->mb_x + s->mb_y * s->mb_width;
143  if (show_bits(&s->gb, 12) == 0 || (mb_xy && mb_xy < s->mb_num)) {
144  s->mb_x = get_bits(&s->gb, 6); /* mb_x */
145  s->mb_y = get_bits(&s->gb, 6); /* mb_y */
146  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  skip_bits(&s->gb, 3); /* ignored */
153 
154  return mb_count;
155 }
156 
157 static int rv20_decode_picture_header(RVDecContext *rv, int whole_size)
158 {
159  static const enum AVPictureType pict_types[] =
160  { AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_I /* hmm ... */,
162  MpegEncContext *s = &rv->m;
163  int seq, mb_pos, ret;
164  int rpr_max;
165 
166  s->pict_type = pict_types[get_bits(&s->gb, 2)];
167 
168  if (s->low_delay && s->pict_type == AV_PICTURE_TYPE_B) {
169  av_log(s->avctx, AV_LOG_ERROR, "low delay B\n");
170  return -1;
171  }
172  if (!s->last_pic.ptr && s->pict_type == AV_PICTURE_TYPE_B) {
173  av_log(s->avctx, AV_LOG_ERROR, "early B-frame\n");
174  return AVERROR_INVALIDDATA;
175  }
176 
177  if (get_bits1(&s->gb)) {
178  av_log(s->avctx, AV_LOG_ERROR, "reserved bit set\n");
179  return AVERROR_INVALIDDATA;
180  }
181 
182  s->qscale = get_bits(&s->gb, 5);
183  if (s->qscale == 0) {
184  av_log(s->avctx, AV_LOG_ERROR, "Invalid qscale value: 0\n");
185  return AVERROR_INVALIDDATA;
186  }
187 
188  if (RV_GET_MINOR_VER(rv->sub_id) >= 2)
189  s->loop_filter = get_bits1(&s->gb) && !s->avctx->lowres;
190 
191  if (RV_GET_MINOR_VER(rv->sub_id) <= 1)
192  seq = get_bits(&s->gb, 8) << 7;
193  else
194  seq = get_bits(&s->gb, 13) << 2;
195 
196  rpr_max = s->avctx->extradata[1] & 7;
197  if (rpr_max) {
198  int f, new_w, new_h;
199  int rpr_bits = av_log2(rpr_max) + 1;
200 
201  f = get_bits(&s->gb, rpr_bits);
202 
203  if (f) {
204  if (s->avctx->extradata_size < 8 + 2 * f) {
205  av_log(s->avctx, AV_LOG_ERROR, "Extradata too small.\n");
206  return AVERROR_INVALIDDATA;
207  }
208 
209  new_w = 4 * s->avctx->extradata[6 + 2 * f];
210  new_h = 4 * s->avctx->extradata[7 + 2 * f];
211  } else {
212  new_w = rv->orig_width;
213  new_h = rv->orig_height;
214  }
215  if (new_w != s->width || new_h != s->height || !s->context_initialized) {
216  AVRational old_aspect = s->avctx->sample_aspect_ratio;
217  av_log(s->avctx, AV_LOG_DEBUG,
218  "attempting to change resolution to %dx%d\n", new_w, new_h);
219  if (av_image_check_size(new_w, new_h, 0, s->avctx) < 0)
220  return AVERROR_INVALIDDATA;
221 
222  if (whole_size < (new_w + 15)/16 * ((new_h + 15)/16) / 8)
223  return AVERROR_INVALIDDATA;
224 
226 
227  // attempt to keep aspect during typical resolution switches
228  if (!old_aspect.num)
229  old_aspect = (AVRational){1, 1};
230  if (2 * (int64_t)new_w * s->height == (int64_t)new_h * s->width)
231  s->avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){2, 1});
232  if ((int64_t)new_w * s->height == 2 * (int64_t)new_h * s->width)
233  s->avctx->sample_aspect_ratio = av_mul_q(old_aspect, (AVRational){1, 2});
234 
235  ret = ff_set_dimensions(s->avctx, new_w, new_h);
236  if (ret < 0)
237  return ret;
238 
239  s->width = new_w;
240  s->height = new_h;
241  if ((ret = ff_mpv_common_init(s)) < 0)
242  return ret;
243  }
244 
245  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
246  av_log(s->avctx, AV_LOG_DEBUG, "F %d/%d/%d\n", f, rpr_bits, rpr_max);
247  }
248  }
249  if (av_image_check_size(s->width, s->height, 0, s->avctx) < 0)
250  return AVERROR_INVALIDDATA;
251 
252  mb_pos = ff_h263_decode_mba(s);
253 
254  seq |= s->time & ~0x7FFF;
255  if (seq - s->time > 0x4000)
256  seq -= 0x8000;
257  if (seq - s->time < -0x4000)
258  seq += 0x8000;
259 
260  if (seq != s->time) {
261  if (s->pict_type != AV_PICTURE_TYPE_B) {
262  s->time = seq;
263  s->pp_time = s->time - s->last_non_b_time;
264  s->last_non_b_time = s->time;
265  } else {
266  s->time = seq;
267  s->pb_time = s->pp_time - (s->last_non_b_time - s->time);
268  }
269  }
270  if (s->pict_type == AV_PICTURE_TYPE_B) {
271  if (s->pp_time <=s->pb_time || s->pp_time <= s->pp_time - s->pb_time || s->pp_time<=0) {
272  av_log(s->avctx, AV_LOG_DEBUG,
273  "messed up order, possible from seeking? skipping current B-frame\n");
274 #define ERROR_SKIP_FRAME -123
275  return ERROR_SKIP_FRAME;
276  }
278  }
279 
280  s->no_rounding = get_bits1(&s->gb);
281 
282  if (RV_GET_MINOR_VER(rv->sub_id) <= 1 && s->pict_type == AV_PICTURE_TYPE_B)
283  // binary decoder reads 3+2 bits here but they don't seem to be used
284  skip_bits(&s->gb, 5);
285 
286  s->h263_aic = s->pict_type == AV_PICTURE_TYPE_I;
287  s->modified_quant = 1;
288  if (!s->avctx->lowres)
289  s->loop_filter = 1;
290 
291  if (s->avctx->debug & FF_DEBUG_PICT_INFO) {
292  av_log(s->avctx, AV_LOG_INFO,
293  "num:%5d x:%2d y:%2d type:%d qscale:%2d rnd:%d\n",
294  seq, s->mb_x, s->mb_y, s->pict_type, s->qscale,
295  s->no_rounding);
296  }
297 
298  av_assert0(s->pict_type != AV_PICTURE_TYPE_B || !s->low_delay);
299 
300  return s->mb_width * s->mb_height - mb_pos;
301 }
302 
303 static av_cold void rv10_build_vlc(VLC *vlc, const uint16_t len_count[15],
304  const uint8_t sym_rl[][2], int sym_rl_elems)
305 {
306  uint16_t syms[MAX_VLC_ENTRIES];
307  uint8_t lens[MAX_VLC_ENTRIES];
308  unsigned nb_syms = 0, nb_lens = 0;
309 
310  for (unsigned i = 0; i < sym_rl_elems; i++) {
311  unsigned cur_sym = sym_rl[i][0];
312  for (unsigned tmp = nb_syms + sym_rl[i][1]; nb_syms <= tmp; nb_syms++)
313  syms[nb_syms] = 0xFF & cur_sym--;
314  }
315 
316  for (unsigned i = 0; i < 15; i++)
317  for (unsigned tmp = nb_lens + len_count[i]; nb_lens < tmp; nb_lens++)
318  lens[nb_lens] = i + 2;
319  av_assert1(nb_lens == nb_syms);
320  ff_vlc_init_from_lengths(vlc, DC_VLC_BITS, nb_lens, lens, 1,
321  syms, 2, 2, 0, VLC_INIT_STATIC_OVERLONG, NULL);
322 }
323 
324 static av_cold void rv10_init_static(void)
325 {
326  static VLCElem table[1472 + 992];
327 
329  rv_dc_lum.table_allocated = 1472;
332  for (int i = 0; i < 1 << (DC_VLC_BITS - 7 /* Length of skip prefix */); i++) {
333  /* All codes beginning with 0x7F have the same length and value.
334  * Modifying the table directly saves us the useless subtables. */
335  rv_dc_lum.table[(0x7F << (DC_VLC_BITS - 7)) + i].sym = 255;
336  rv_dc_lum.table[(0x7F << (DC_VLC_BITS - 7)) + i].len = 18;
337  }
338  rv_dc_chrom.table = &table[1472];
342  for (int i = 0; i < 1 << (DC_VLC_BITS - 9 /* Length of skip prefix */); i++) {
343  /* Same as above. */
344  rv_dc_chrom.table[(0x1FE << (DC_VLC_BITS - 9)) + i].sym = 255;
345  rv_dc_chrom.table[(0x1FE << (DC_VLC_BITS - 9)) + i].len = 18;
346  }
347 }
348 
350 {
351  static AVOnce init_static_once = AV_ONCE_INIT;
352  RVDecContext *rv = avctx->priv_data;
353  MpegEncContext *s = &rv->m;
354  int major_ver, minor_ver, micro_ver, ret;
355 
356  if (avctx->extradata_size < 8) {
357  av_log(avctx, AV_LOG_ERROR, "Extradata is too small.\n");
358  return AVERROR_INVALIDDATA;
359  }
360  if ((ret = av_image_check_size(avctx->coded_width,
361  avctx->coded_height, 0, avctx)) < 0)
362  return ret;
363 
364  ret = ff_h263_decode_init(avctx);
365  if (ret < 0)
366  return ret;
367 
368  rv->orig_width = avctx->coded_width;
369  rv->orig_height = avctx->coded_height;
370 
371  s->h263_long_vectors = avctx->extradata[3] & 1;
372  rv->sub_id = AV_RB32A(avctx->extradata + 4);
373 
374  major_ver = RV_GET_MAJOR_VER(rv->sub_id);
375  minor_ver = RV_GET_MINOR_VER(rv->sub_id);
376  micro_ver = RV_GET_MICRO_VER(rv->sub_id);
377 
378  switch (major_ver) {
379  case 1:
380  s->rv10_version = micro_ver ? 3 : 1;
381  s->obmc = micro_ver == 2;
382  break;
383  case 2:
384  if (minor_ver >= 2) {
385  s->low_delay = 0;
386  avctx->has_b_frames = 1;
387  }
388  break;
389  default:
390  av_log(avctx, AV_LOG_ERROR, "unknown header %X\n", rv->sub_id);
391  avpriv_request_sample(avctx, "RV1/2 version");
392  return AVERROR_PATCHWELCOME;
393  }
394 
395  if (avctx->debug & FF_DEBUG_PICT_INFO) {
396  av_log(avctx, AV_LOG_DEBUG, "ver:%X ver0:%"PRIX32"\n", rv->sub_id,
397  AV_RL32A(avctx->extradata));
398  }
399 
400  /* init static VLCs */
401  ff_thread_once(&init_static_once, rv10_init_static);
402 
403  return 0;
404 }
405 
406 static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf,
407  int buf_size, int buf_size2, int whole_size)
408 {
409  RVDecContext *rv = avctx->priv_data;
410  MpegEncContext *s = &rv->m;
411  int mb_count, mb_pos, left, start_mb_x, active_bits_size, ret;
412 
413  active_bits_size = buf_size * 8;
414  init_get_bits(&s->gb, buf, FFMAX(buf_size, buf_size2) * 8);
415  if (s->codec_id == AV_CODEC_ID_RV10)
416  mb_count = rv10_decode_picture_header(s);
417  else
418  mb_count = rv20_decode_picture_header(rv, whole_size);
419  if (mb_count < 0) {
420  if (mb_count != ERROR_SKIP_FRAME)
421  av_log(s->avctx, AV_LOG_ERROR, "HEADER ERROR\n");
422  return AVERROR_INVALIDDATA;
423  }
424 
425  if (s->mb_x >= s->mb_width ||
426  s->mb_y >= s->mb_height) {
427  av_log(s->avctx, AV_LOG_ERROR, "POS ERROR %d %d\n", s->mb_x, s->mb_y);
428  return AVERROR_INVALIDDATA;
429  }
430  mb_pos = s->mb_y * s->mb_width + s->mb_x;
431  left = s->mb_width * s->mb_height - mb_pos;
432  if (mb_count > left) {
433  av_log(s->avctx, AV_LOG_ERROR, "COUNT ERROR\n");
434  return AVERROR_INVALIDDATA;
435  }
436 
437  if (whole_size < s->mb_width * s->mb_height / 8)
438  return AVERROR_INVALIDDATA;
439 
440  if ((s->mb_x == 0 && s->mb_y == 0) || !s->cur_pic.ptr) {
441  // FIXME write parser so we always have complete frames?
442  if (s->cur_pic.ptr) {
443  ff_er_frame_end(&s->er, NULL);
445  s->mb_x = s->mb_y = s->resync_mb_x = s->resync_mb_y = 0;
446  }
447  if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
448  return ret;
450  } else {
451  if (s->cur_pic.ptr->f->pict_type != s->pict_type) {
452  av_log(s->avctx, AV_LOG_ERROR, "Slice type mismatch\n");
453  return AVERROR_INVALIDDATA;
454  }
455  }
456 
457 
458  ff_dlog(avctx, "qscale=%d\n", s->qscale);
459 
460  /* default quantization values */
461  if (s->codec_id == AV_CODEC_ID_RV10) {
462  if (s->mb_y == 0)
463  s->first_slice_line = 1;
464  } else {
465  s->first_slice_line = 1;
466  s->resync_mb_x = s->mb_x;
467  }
468  start_mb_x = s->mb_x;
469  s->resync_mb_y = s->mb_y;
470  if (s->h263_aic) {
471  s->y_dc_scale_table =
472  s->c_dc_scale_table = ff_aic_dc_scale_table;
473  } else {
474  s->y_dc_scale_table =
475  s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
476  }
477 
478  if (s->modified_quant)
479  s->chroma_qscale_table = ff_h263_chroma_qscale_table;
480 
481  ff_set_qscale(s, s->qscale);
482 
483  s->rv10_first_dc_coded[0] = 0;
484  s->rv10_first_dc_coded[1] = 0;
485  s->rv10_first_dc_coded[2] = 0;
487 
488  /* decode each macroblock */
489  for (s->mb_num_left = mb_count; s->mb_num_left > 0; s->mb_num_left--) {
490  int ret;
491  ff_update_block_index(s, 8, s->avctx->lowres, 1);
492  ff_tlog(avctx, "**mb x=%d y=%d\n", s->mb_x, s->mb_y);
493 
494  s->mv_dir = MV_DIR_FORWARD;
495  s->mv_type = MV_TYPE_16X16;
496  ret = ff_h263_decode_mb(s, s->block);
497 
498  // Repeat the slice end check from ff_h263_decode_mb with our active
499  // bitstream size
500  if (ret != SLICE_ERROR && active_bits_size >= get_bits_count(&s->gb)) {
501  int v = show_bits(&s->gb, 16);
502 
503  if (get_bits_count(&s->gb) + 16 > active_bits_size)
504  v >>= get_bits_count(&s->gb) + 16 - active_bits_size;
505 
506  if (!v)
507  ret = SLICE_END;
508  }
509  if (ret != SLICE_ERROR && active_bits_size < get_bits_count(&s->gb) &&
510  8 * buf_size2 >= get_bits_count(&s->gb)) {
511  active_bits_size = buf_size2 * 8;
512  av_log(avctx, AV_LOG_DEBUG, "update size from %d to %d\n",
513  8 * buf_size, active_bits_size);
514  ret = SLICE_OK;
515  }
516 
517  if (ret == SLICE_ERROR || active_bits_size < get_bits_count(&s->gb)) {
518  av_log(s->avctx, AV_LOG_ERROR, "ERROR at MB %d %d\n", s->mb_x,
519  s->mb_y);
520  return AVERROR_INVALIDDATA;
521  }
522  if (s->pict_type != AV_PICTURE_TYPE_B)
524  ff_mpv_reconstruct_mb(s, s->block);
525  if (s->loop_filter)
527 
528  if (++s->mb_x == s->mb_width) {
529  s->mb_x = 0;
530  s->mb_y++;
532  }
533  if (s->mb_x == s->resync_mb_x)
534  s->first_slice_line = 0;
535  if (ret == SLICE_END)
536  break;
537  }
538 
539  ff_er_add_slice(&s->er, start_mb_x, s->resync_mb_y, s->mb_x - 1, s->mb_y,
540  ER_MB_END);
541 
542  return active_bits_size;
543 }
544 
545 static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
546 {
547  return AV_RL32(buf + n * 8);
548 }
549 
550 static int rv10_decode_frame(AVCodecContext *avctx, AVFrame *pict,
551  int *got_frame, AVPacket *avpkt)
552 {
553  const uint8_t *buf = avpkt->data;
554  int buf_size = avpkt->size;
555  MpegEncContext *s = avctx->priv_data;
556  int i, ret;
557  int slice_count;
558  const uint8_t *slices_hdr = NULL;
559 
560  ff_dlog(avctx, "*****frame %"PRId64" size=%d\n", avctx->frame_num, buf_size);
561 
562  /* no supplementary picture */
563  if (buf_size == 0) {
564  return 0;
565  }
566 
567  slice_count = (*buf++) + 1;
568  buf_size--;
569 
570  if (!slice_count || buf_size <= 8 * slice_count) {
571  av_log(avctx, AV_LOG_ERROR, "Invalid slice count: %d.\n",
572  slice_count);
573  return AVERROR_INVALIDDATA;
574  }
575 
576  slices_hdr = buf + 4;
577  buf += 8 * slice_count;
578  buf_size -= 8 * slice_count;
579 
580  for (i = 0; i < slice_count; i++) {
581  unsigned offset = get_slice_offset(avctx, slices_hdr, i);
582  int size, size2;
583 
584  if (offset >= buf_size)
585  return AVERROR_INVALIDDATA;
586 
587  if (i + 1 == slice_count)
588  size = buf_size - offset;
589  else
590  size = get_slice_offset(avctx, slices_hdr, i + 1) - offset;
591 
592  if (i + 2 >= slice_count)
593  size2 = buf_size - offset;
594  else
595  size2 = get_slice_offset(avctx, slices_hdr, i + 2) - offset;
596 
597  if (size <= 0 || size2 <= 0 ||
598  offset + FFMAX(size, size2) > buf_size)
599  return AVERROR_INVALIDDATA;
600 
601  if ((ret = rv10_decode_packet(avctx, buf + offset, size, size2, buf_size)) < 0)
602  return ret;
603 
604  if (ret > 8 * size)
605  i++;
606  }
607 
608  if (s->cur_pic.ptr && s->mb_y >= s->mb_height) {
609  ff_er_frame_end(&s->er, NULL);
611 
612  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
613  if ((ret = av_frame_ref(pict, s->cur_pic.ptr->f)) < 0)
614  return ret;
615  ff_print_debug_info(s, s->cur_pic.ptr, pict);
616  ff_mpv_export_qp_table(s, pict, s->cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
617  } else if (s->last_pic.ptr) {
618  if ((ret = av_frame_ref(pict, s->last_pic.ptr->f)) < 0)
619  return ret;
620  ff_print_debug_info(s, s->last_pic.ptr, pict);
621  ff_mpv_export_qp_table(s, pict,s->last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
622  }
623 
624  if (s->last_pic.ptr || s->low_delay) {
625  *got_frame = 1;
626  }
627 
628  // so we can detect if frame_end was not called (find some nicer solution...)
629  ff_mpv_unref_picture(&s->cur_pic);
630  }
631 
632  return avpkt->size;
633 }
634 
636  .p.name = "rv10",
637  CODEC_LONG_NAME("RealVideo 1.0"),
638  .p.type = AVMEDIA_TYPE_VIDEO,
639  .p.id = AV_CODEC_ID_RV10,
640  .priv_data_size = sizeof(RVDecContext),
643  .close = ff_mpv_decode_close,
644  .p.capabilities = AV_CODEC_CAP_DR1,
645  .p.max_lowres = 3,
646  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
647 };
648 
650  .p.name = "rv20",
651  CODEC_LONG_NAME("RealVideo 2.0"),
652  .p.type = AVMEDIA_TYPE_VIDEO,
653  .p.id = AV_CODEC_ID_RV20,
654  .priv_data_size = sizeof(RVDecContext),
657  .close = ff_mpv_decode_close,
658  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
659  .flush = ff_mpeg_flush,
660  .p.max_lowres = 3,
661  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
662 };
ff_mpv_common_init
av_cold int ff_mpv_common_init(MpegEncContext *s)
init common structure for both encoder and decoder.
Definition: mpegvideo.c:422
MV_TYPE_16X16
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:192
h263data.h
ff_vlc_init_from_lengths
int ff_vlc_init_from_lengths(VLC *vlc, 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: vlc.c:306
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
RV_GET_MINOR_VER
#define RV_GET_MINOR_VER(x)
Definition: rv10.c:48
ff_h263_decode_mb
int ff_h263_decode_mb(MpegEncContext *s, int16_t block[6][64])
Definition: ituh263dec.c:788
thread.h
AVPictureType
AVPictureType
Definition: avutil.h:277
int64_t
long long int64_t
Definition: coverity.c:34
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:249
AV_RB32A
#define AV_RB32A(p)
Definition: intreadwrite.h:575
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
AVPacket::data
uint8_t * data
Definition: packet.h:535
table
static const uint16_t table[]
Definition: prosumer.c:203
FFCodec
Definition: codec_internal.h:127
VLCElem::len
VLCBaseType len
Definition: vlc.h:37
ff_er_add_slice
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
Definition: error_resilience.c:826
ff_init_block_index
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:552
mpegvideo.h
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
ff_h263_decode_mba
int ff_h263_decode_mba(MpegEncContext *s)
Definition: ituh263dec.c:143
ff_set_dimensions
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:91
init_get_bits
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:497
RVDecContext
Definition: rv10.c:54
FF_DEBUG_PICT_INFO
#define FF_DEBUG_PICT_INFO
Definition: avcodec.h:1375
MAX_VLC_ENTRIES
#define MAX_VLC_ENTRIES
Definition: rv10.c:51
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:364
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:318
ff_mpv_reconstruct_mb
void ff_mpv_reconstruct_mb(MpegEncContext *s, int16_t block[12][64])
Definition: mpegvideo_dec.c:1087
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
VLCElem::sym
VLCBaseType sym
Definition: vlc.h:36
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:607
SLICE_END
#define SLICE_END
end marker found
Definition: mpegvideo.h:326
ff_h263_update_motion_val
void ff_h263_update_motion_val(MpegEncContext *s)
Definition: h263.c:53
AVRational::num
int num
Numerator.
Definition: rational.h:59
ff_thread_once
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:205
mpegvideodec.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
h263dec.h
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
rv10_decode_picture_header
static int rv10_decode_picture_header(MpegEncContext *s)
Definition: rv10.c:100
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:515
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:697
ff_er_frame_end
void ff_er_frame_end(ERContext *s, int *decode_error_flags)
Indicate that a frame has finished decoding and perform error concealment in case it has been enabled...
Definition: error_resilience.c:896
ff_mpeg_flush
av_cold void ff_mpeg_flush(AVCodecContext *avctx)
Definition: mpegvideo_dec.c:431
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:341
ff_h263_chroma_qscale_table
const uint8_t ff_h263_chroma_qscale_table[32]
Definition: h263data.c:260
s
#define s(width, name)
Definition: cbs_vp9.c:198
RV_GET_MAJOR_VER
#define RV_GET_MAJOR_VER(x)
Definition: rv10.c:47
ff_mpeg_er_frame_start
void ff_mpeg_er_frame_start(MpegEncContext *s)
Definition: mpeg_er.c:49
ff_mpeg1_dc_scale_table
static const uint8_t *const ff_mpeg1_dc_scale_table
Definition: mpegvideodata.h:32
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
rv_dc_chrom
static VLC rv_dc_chrom
Definition: rv10.c:81
decode.h
rv10_build_vlc
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:303
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
ff_mpv_common_end
av_cold void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:509
ff_mpv_unref_picture
void ff_mpv_unref_picture(MPVWorkPicture *pic)
Definition: mpegpicture.c:98
RVDecContext::orig_height
int orig_height
Definition: rv10.c:57
AV_ONCE_INIT
#define AV_ONCE_INIT
Definition: thread.h:203
ff_mpv_export_qp_table
int ff_mpv_export_qp_table(const MpegEncContext *s, AVFrame *f, const MPVPicture *p, int qp_type)
Definition: mpegvideo_dec.c:392
NULL
#define NULL
Definition: coverity.c:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
RVDecContext::orig_width
int orig_width
Definition: rv10.c:57
tmp
static uint8_t tmp[20]
Definition: aes_ctr.c:47
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ERROR_SKIP_FRAME
#define ERROR_SKIP_FRAME
ff_mpv_decode_close
av_cold int ff_mpv_decode_close(AVCodecContext *avctx)
Definition: mpegvideo_dec.c:145
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:371
ff_set_qscale
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:585
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:635
FF_MPV_QSCALE_TYPE_MPEG1
#define FF_MPV_QSCALE_TYPE_MPEG1
Definition: mpegvideodec.h:40
AVOnce
#define AVOnce
Definition: thread.h:202
rv10_decode_frame
static int rv10_decode_frame(AVCodecContext *avctx, AVFrame *pict, int *got_frame, AVPacket *avpkt)
Definition: rv10.c:550
rv_chrom_len_count
static const uint16_t rv_chrom_len_count[15]
Definition: rv10.c:77
ff_dlog
#define ff_dlog(a,...)
Definition: tableprint_vlc.h:28
VLC::table_allocated
int table_allocated
Definition: vlc.h:53
ff_rv20_decoder
const FFCodec ff_rv20_decoder
Definition: rv10.c:649
rv_dc_lum
static VLC rv_dc_lum
Definition: rv10.c:81
f
f
Definition: af_crystalizer.c:122
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:536
RV_GET_MICRO_VER
#define RV_GET_MICRO_VER(x)
Definition: rv10.c:49
av_frame_ref
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:276
codec_internal.h
ff_update_block_index
static void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, int lowres, int chroma_x_shift)
Definition: mpegvideo.h:390
size
int size
Definition: twinvq_data.h:10344
VLCElem
Definition: vlc.h:32
ff_mpv_frame_start
int ff_mpv_frame_start(MpegEncContext *s, AVCodecContext *avctx)
generic function called after decoding the header and before a frame is decoded.
Definition: mpegvideo_dec.c:326
RVDecContext::m
MpegEncContext m
Definition: rv10.c:55
mpegvideodata.h
offset
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
Definition: writing_filters.txt:86
rv10_decode_packet
static int rv10_decode_packet(AVCodecContext *avctx, const uint8_t *buf, int buf_size, int buf_size2, int whole_size)
Definition: rv10.c:406
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:220
rv_sym_run_len
static const uint8_t rv_sym_run_len[][2]
Definition: rv10.c:64
ff_rv_decode_dc
int ff_rv_decode_dc(MpegEncContext *s, int n)
Definition: rv10.c:83
rv10_init_static
static av_cold void rv10_init_static(void)
Definition: rv10.c:324
ff_print_debug_info
void ff_print_debug_info(const MpegEncContext *s, const MPVPicture *p, AVFrame *pict)
Definition: mpegvideo_dec.c:385
AV_CODEC_ID_RV10
@ AV_CODEC_ID_RV10
Definition: codec_id.h:57
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
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
Definition: filter_design.txt:178
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:514
show_bits
static unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:354
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
AV_CODEC_ID_RV20
@ AV_CODEC_ID_RV20
Definition: codec_id.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
ff_h263_decode_init
av_cold int ff_h263_decode_init(AVCodecContext *avctx)
Definition: h263dec.c:91
ff_rv10_decoder
const FFCodec ff_rv10_decoder
Definition: rv10.c:635
avcodec.h
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:1878
get_slice_offset
static int get_slice_offset(AVCodecContext *avctx, const uint8_t *buf, int n)
Definition: rv10.c:545
ret
ret
Definition: filter_design.txt:187
SLICE_OK
#define SLICE_OK
Definition: mpegvideo.h:324
ff_aic_dc_scale_table
const uint8_t ff_aic_dc_scale_table[32]
Definition: h263data.c:245
rv20_decode_picture_header
static int rv20_decode_picture_header(RVDecContext *rv, int whole_size)
Definition: rv10.c:157
ff_h263_loop_filter
void ff_h263_loop_filter(MpegEncContext *s)
Definition: h263.c:94
ff_mpeg4_init_direct_mv
void ff_mpeg4_init_direct_mv(MpegEncContext *s)
Definition: mpeg4video.c:83
left
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:386
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
rv_lum_len_count
static const uint16_t rv_lum_len_count[15]
Definition: rv10.c:73
AVCodecContext
main external API structure.
Definition: avcodec.h:431
VLC_INIT_STATIC_OVERLONG
#define VLC_INIT_STATIC_OVERLONG
Definition: vlc.h:191
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
mpeg4video.h
error_resilience.h
VLC
Definition: vlc.h:50
ff_mpv_frame_end
void ff_mpv_frame_end(MpegEncContext *s)
Definition: mpegvideo_dec.c:377
AV_CODEC_CAP_DELAY
#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:76
VLC::table
VLCElem * table
Definition: vlc.h:52
AV_RL32A
#define AV_RL32A(p)
Definition: intreadwrite.h:568
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1374
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:607
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
rv10_decode_init
static av_cold int rv10_decode_init(AVCodecContext *avctx)
Definition: rv10.c:349
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:37
ER_MB_END
#define ER_MB_END
Definition: error_resilience.h:37
SLICE_ERROR
#define SLICE_ERROR
Definition: mpegvideo.h:325
MV_DIR_FORWARD
#define MV_DIR_FORWARD
Definition: mpegvideo.h:188
ff_tlog
#define ff_tlog(a,...)
Definition: tableprint_vlc.h:29
AVPacket
This structure stores compressed data.
Definition: packet.h:512
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
mpeg_er.h
DC_VLC_BITS
#define DC_VLC_BITS
Definition: rv10.c:52
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
av_image_check_size
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:318
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:64
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
rv10dec.h
RVDecContext::sub_id
int sub_id
Definition: rv10.c:56
h263.h