FFmpeg
h261dec.c
Go to the documentation of this file.
1 /*
2  * H.261 decoder
3  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4  * Copyright (c) 2004 Maarten Daniels
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  * H.261 decoder.
26  */
27 
28 #include "libavutil/avassert.h"
29 #include "libavutil/thread.h"
30 #include "avcodec.h"
31 #include "mpeg_er.h"
32 #include "mpegutils.h"
33 #include "mpegvideo.h"
34 #include "h263.h"
35 #include "h261.h"
36 #include "internal.h"
37 
38 #define H261_MBA_VLC_BITS 8
39 #define H261_MTYPE_VLC_BITS 6
40 #define H261_MV_VLC_BITS 7
41 #define H261_CBP_VLC_BITS 9
42 #define TCOEFF_VLC_BITS 9
43 #define MBA_STUFFING 33
44 #define MBA_STARTCODE 34
45 
50 
52 {
53  INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
54  ff_h261_mba_bits, 1, 1,
55  ff_h261_mba_code, 1, 1, 540);
56  INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
57  ff_h261_mtype_bits, 1, 1,
58  ff_h261_mtype_code, 1, 1, 80);
59  INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
60  &ff_h261_mv_tab[0][1], 2, 1,
61  &ff_h261_mv_tab[0][0], 2, 1, 144);
62  INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
63  &ff_h261_cbp_tab[0][1], 2, 1,
64  &ff_h261_cbp_tab[0][0], 2, 1, 512);
66 }
67 
69 {
70  static AVOnce init_static_once = AV_ONCE_INIT;
71  H261Context *h = avctx->priv_data;
72  MpegEncContext *const s = &h->s;
73 
74  // set defaults
75  ff_mpv_decode_init(s, avctx);
76 
77  s->out_format = FMT_H261;
78  s->low_delay = 1;
79  avctx->pix_fmt = AV_PIX_FMT_YUV420P;
80 
82 
83  ff_thread_once(&init_static_once, h261_decode_init_static);
84 
85  return 0;
86 }
87 
88 /**
89  * Decode the group of blocks header or slice header.
90  * @return <0 if an error occurred
91  */
93 {
94  unsigned int val;
95  MpegEncContext *const s = &h->s;
96 
97  if (!h->gob_start_code_skipped) {
98  /* Check for GOB Start Code */
99  val = show_bits(&s->gb, 15);
100  if (val)
101  return -1;
102 
103  /* We have a GBSC */
104  skip_bits(&s->gb, 16);
105  }
106 
107  h->gob_start_code_skipped = 0;
108 
109  h->gob_number = get_bits(&s->gb, 4); /* GN */
110  s->qscale = get_bits(&s->gb, 5); /* GQUANT */
111 
112  /* Check if gob_number is valid */
113  if (s->mb_height == 18) { // CIF
114  if ((h->gob_number <= 0) || (h->gob_number > 12))
115  return -1;
116  } else { // QCIF
117  if ((h->gob_number != 1) && (h->gob_number != 3) &&
118  (h->gob_number != 5))
119  return -1;
120  }
121 
122  /* GEI */
123  if (skip_1stop_8data_bits(&s->gb) < 0)
124  return AVERROR_INVALIDDATA;
125 
126  if (s->qscale == 0) {
127  av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
129  return -1;
130  }
131 
132  /* For the first transmitted macroblock in a GOB, MBA is the absolute
133  * address. For subsequent macroblocks, MBA is the difference between
134  * the absolute addresses of the macroblock and the last transmitted
135  * macroblock. */
136  h->current_mba = 0;
137  h->mba_diff = 0;
138 
139  return 0;
140 }
141 
142 /**
143  * Decode the group of blocks / video packet header.
144  * @return <0 if no resync found
145  */
147 {
148  MpegEncContext *const s = &h->s;
149  int left, ret;
150 
151  if (h->gob_start_code_skipped) {
152  ret = h261_decode_gob_header(h);
153  if (ret >= 0)
154  return 0;
155  } else {
156  if (show_bits(&s->gb, 15) == 0) {
157  ret = h261_decode_gob_header(h);
158  if (ret >= 0)
159  return 0;
160  }
161  // OK, it is not where it is supposed to be ...
162  s->gb = s->last_resync_gb;
163  align_get_bits(&s->gb);
164  left = get_bits_left(&s->gb);
165 
166  for (; left > 15 + 1 + 4 + 5; left -= 8) {
167  if (show_bits(&s->gb, 15) == 0) {
168  GetBitContext bak = s->gb;
169 
170  ret = h261_decode_gob_header(h);
171  if (ret >= 0)
172  return 0;
173 
174  s->gb = bak;
175  }
176  skip_bits(&s->gb, 8);
177  }
178  }
179 
180  return -1;
181 }
182 
183 /**
184  * Decode skipped macroblocks.
185  * @return 0
186  */
187 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
188 {
189  MpegEncContext *const s = &h->s;
190  int i;
191 
192  s->mb_intra = 0;
193 
194  for (i = mba1; i < mba2; i++) {
195  int j, xy;
196 
197  s->mb_x = ((h->gob_number - 1) % 2) * 11 + i % 11;
198  s->mb_y = ((h->gob_number - 1) / 2) * 3 + i / 11;
199  xy = s->mb_x + s->mb_y * s->mb_stride;
202 
203  for (j = 0; j < 6; j++)
204  s->block_last_index[j] = -1;
205 
206  s->mv_dir = MV_DIR_FORWARD;
207  s->mv_type = MV_TYPE_16X16;
209  s->mv[0][0][0] = 0;
210  s->mv[0][0][1] = 0;
211  s->mb_skipped = 1;
212  h->mtype &= ~MB_TYPE_H261_FIL;
213 
214  if (s->current_picture.motion_val[0]) {
215  int b_stride = 2*s->mb_width + 1;
216  int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride;
217  s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0];
218  s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1];
219  }
220 
222  }
223 
224  return 0;
225 }
226 
227 static const int mvmap[17] = {
228  0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
229 };
230 
231 static int decode_mv_component(GetBitContext *gb, int v)
232 {
233  int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
234 
235  /* check if mv_diff is valid */
236  if (mv_diff < 0)
237  return v;
238 
239  mv_diff = mvmap[mv_diff];
240 
241  if (mv_diff && !get_bits1(gb))
242  mv_diff = -mv_diff;
243 
244  v += mv_diff;
245  if (v <= -16)
246  v += 32;
247  else if (v >= 16)
248  v -= 32;
249 
250  return v;
251 }
252 
253 /**
254  * Decode a macroblock.
255  * @return <0 if an error occurred
256  */
257 static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
258 {
259  MpegEncContext *const s = &h->s;
260  int level, i, j, run;
261  RLTable *rl = &ff_h261_rl_tcoeff;
262  const uint8_t *scan_table;
263 
264  /* For the variable length encoding there are two code tables, one being
265  * used for the first transmitted LEVEL in INTER, INTER + MC and
266  * INTER + MC + FIL blocks, the second for all other LEVELs except the
267  * first one in INTRA blocks which is fixed length coded with 8 bits.
268  * NOTE: The two code tables only differ in one VLC so we handle that
269  * manually. */
270  scan_table = s->intra_scantable.permutated;
271  if (s->mb_intra) {
272  /* DC coef */
273  level = get_bits(&s->gb, 8);
274  // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
275  if ((level & 0x7F) == 0) {
276  av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n",
277  level, s->mb_x, s->mb_y);
278  return -1;
279  }
280  /* The code 1000 0000 is not used, the reconstruction level of 1024
281  * being coded as 1111 1111. */
282  if (level == 255)
283  level = 128;
284  block[0] = level;
285  i = 1;
286  } else if (coded) {
287  // Run Level Code
288  // EOB Not possible for first level when cbp is available (that's why the table is different)
289  // 0 1 1s
290  // * * 0*
291  int check = show_bits(&s->gb, 2);
292  i = 0;
293  if (check & 0x2) {
294  skip_bits(&s->gb, 2);
295  block[0] = (check & 0x1) ? -1 : 1;
296  i = 1;
297  }
298  } else {
299  i = 0;
300  }
301  if (!coded) {
302  s->block_last_index[n] = i - 1;
303  return 0;
304  }
305  {
306  OPEN_READER(re, &s->gb);
307  i--; // offset by -1 to allow direct indexing of scan_table
308  for (;;) {
309  UPDATE_CACHE(re, &s->gb);
310  GET_RL_VLC(level, run, re, &s->gb, rl->rl_vlc[0], TCOEFF_VLC_BITS, 2, 0);
311  if (run == 66) {
312  if (level) {
313  CLOSE_READER(re, &s->gb);
314  av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n",
315  s->mb_x, s->mb_y);
316  return -1;
317  }
318  /* escape */
319  /* The remaining combinations of (run, level) are encoded with a
320  * 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits
321  * level. */
322  run = SHOW_UBITS(re, &s->gb, 6) + 1;
323  SKIP_CACHE(re, &s->gb, 6);
324  level = SHOW_SBITS(re, &s->gb, 8);
325  SKIP_COUNTER(re, &s->gb, 6 + 8);
326  } else if (level == 0) {
327  break;
328  } else {
329  if (SHOW_UBITS(re, &s->gb, 1))
330  level = -level;
331  SKIP_COUNTER(re, &s->gb, 1);
332  }
333  i += run;
334  if (i >= 64) {
335  CLOSE_READER(re, &s->gb);
336  av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n",
337  s->mb_x, s->mb_y);
338  return -1;
339  }
340  j = scan_table[i];
341  block[j] = level;
342  }
343  CLOSE_READER(re, &s->gb);
344  }
345  s->block_last_index[n] = i;
346  return 0;
347 }
348 
350 {
351  MpegEncContext *const s = &h->s;
352  int i, cbp, xy;
353 
354  cbp = 63;
355  // Read mba
356  do {
357  h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table,
358  H261_MBA_VLC_BITS, 2);
359 
360  /* Check for slice end */
361  /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
362  if (h->mba_diff == MBA_STARTCODE) { // start code
363  h->gob_start_code_skipped = 1;
364  return SLICE_END;
365  }
366  } while (h->mba_diff == MBA_STUFFING); // stuffing
367 
368  if (h->mba_diff < 0) {
369  if (get_bits_left(&s->gb) <= 7)
370  return SLICE_END;
371 
372  av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
373  return SLICE_ERROR;
374  }
375 
376  h->mba_diff += 1;
377  h->current_mba += h->mba_diff;
378 
379  if (h->current_mba > MBA_STUFFING)
380  return SLICE_ERROR;
381 
382  s->mb_x = ((h->gob_number - 1) % 2) * 11 + ((h->current_mba - 1) % 11);
383  s->mb_y = ((h->gob_number - 1) / 2) * 3 + ((h->current_mba - 1) / 11);
384  xy = s->mb_x + s->mb_y * s->mb_stride;
387 
388  // Read mtype
389  h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
390  if (h->mtype < 0) {
391  av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
392  h->mtype);
393  return SLICE_ERROR;
394  }
396  h->mtype = ff_h261_mtype_map[h->mtype];
397 
398  // Read mquant
399  if (IS_QUANT(h->mtype))
400  ff_set_qscale(s, get_bits(&s->gb, 5));
401 
402  s->mb_intra = IS_INTRA4x4(h->mtype);
403 
404  // Read mv
405  if (IS_16X16(h->mtype)) {
406  /* Motion vector data is included for all MC macroblocks. MVD is
407  * obtained from the macroblock vector by subtracting the vector
408  * of the preceding macroblock. For this calculation the vector
409  * of the preceding macroblock is regarded as zero in the
410  * following three situations:
411  * 1) evaluating MVD for macroblocks 1, 12 and 23;
412  * 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
413  * 3) MTYPE of the previous macroblock was not MC. */
414  if ((h->current_mba == 1) || (h->current_mba == 12) ||
415  (h->current_mba == 23) || (h->mba_diff != 1)) {
416  h->current_mv_x = 0;
417  h->current_mv_y = 0;
418  }
419 
422  } else {
423  h->current_mv_x = 0;
424  h->current_mv_y = 0;
425  }
426 
427  // Read cbp
428  if (HAS_CBP(h->mtype))
429  cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 1) + 1;
430 
431  if (s->mb_intra) {
433  goto intra;
434  }
435 
436  //set motion vectors
437  s->mv_dir = MV_DIR_FORWARD;
438  s->mv_type = MV_TYPE_16X16;
440  s->mv[0][0][0] = h->current_mv_x * 2; // gets divided by 2 in motion compensation
441  s->mv[0][0][1] = h->current_mv_y * 2;
442 
443  if (s->current_picture.motion_val[0]) {
444  int b_stride = 2*s->mb_width + 1;
445  int b_xy = 2 * s->mb_x + (2 * s->mb_y) * b_stride;
446  s->current_picture.motion_val[0][b_xy][0] = s->mv[0][0][0];
447  s->current_picture.motion_val[0][b_xy][1] = s->mv[0][0][1];
448  }
449 
450 intra:
451  /* decode each block */
452  if (s->mb_intra || HAS_CBP(h->mtype)) {
453  s->bdsp.clear_blocks(s->block[0]);
454  for (i = 0; i < 6; i++) {
455  if (h261_decode_block(h, s->block[i], i, cbp & 32) < 0)
456  return SLICE_ERROR;
457  cbp += cbp;
458  }
459  } else {
460  for (i = 0; i < 6; i++)
461  s->block_last_index[i] = -1;
462  }
463 
465 
466  return SLICE_OK;
467 }
468 
469 /**
470  * Decode the H.261 picture header.
471  * @return <0 if no startcode found
472  */
474 {
475  MpegEncContext *const s = &h->s;
476  int format, i;
477  uint32_t startcode = 0;
478 
479  for (i = get_bits_left(&s->gb); i > 24; i -= 1) {
480  startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
481 
482  if (startcode == 0x10)
483  break;
484  }
485 
486  if (startcode != 0x10) {
487  av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
488  return -1;
489  }
490 
491  /* temporal reference */
492  i = get_bits(&s->gb, 5); /* picture timestamp */
493  if (i < (s->picture_number & 31))
494  i += 32;
495  s->picture_number = (s->picture_number & ~31) + i;
496 
497  s->avctx->framerate = (AVRational) { 30000, 1001 };
498 
499  /* PTYPE starts here */
500  skip_bits1(&s->gb); /* split screen off */
501  skip_bits1(&s->gb); /* camera off */
502  skip_bits1(&s->gb); /* freeze picture release off */
503 
504  format = get_bits1(&s->gb);
505 
506  // only 2 formats possible
507  if (format == 0) { // QCIF
508  s->width = 176;
509  s->height = 144;
510  s->mb_width = 11;
511  s->mb_height = 9;
512  } else { // CIF
513  s->width = 352;
514  s->height = 288;
515  s->mb_width = 22;
516  s->mb_height = 18;
517  }
518 
519  s->mb_num = s->mb_width * s->mb_height;
520 
521  skip_bits1(&s->gb); /* still image mode off */
522  skip_bits1(&s->gb); /* Reserved */
523 
524  /* PEI */
525  if (skip_1stop_8data_bits(&s->gb) < 0)
526  return AVERROR_INVALIDDATA;
527 
528  /* H.261 has no I-frames, but if we pass AV_PICTURE_TYPE_I for the first
529  * frame, the codec crashes if it does not contain all I-blocks
530  * (e.g. when a packet is lost). */
532 
533  h->gob_number = 0;
534  return 0;
535 }
536 
538 {
539  MpegEncContext *const s = &h->s;
540 
541  ff_set_qscale(s, s->qscale);
542 
543  /* decode mb's */
544  while (h->current_mba <= MBA_STUFFING) {
545  int ret;
546  /* DCT & quantize */
547  ret = h261_decode_mb(h);
548  if (ret < 0) {
549  if (ret == SLICE_END) {
551  return 0;
552  }
553  av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n",
554  s->mb_x + s->mb_y * s->mb_stride);
555  return -1;
556  }
557 
559  h->current_mba - h->mba_diff,
560  h->current_mba - 1);
561  }
562 
563  return -1;
564 }
565 
566 /**
567  * returns the number of bytes consumed for building the current frame
568  */
569 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
570 {
571  int pos = get_bits_count(&s->gb) >> 3;
572  if (pos == 0)
573  pos = 1; // avoid infinite loops (i doubt that is needed but ...)
574  if (pos + 10 > buf_size)
575  pos = buf_size; // oops ;)
576 
577  return pos;
578 }
579 
580 static int h261_decode_frame(AVCodecContext *avctx, void *data,
581  int *got_frame, AVPacket *avpkt)
582 {
583  const uint8_t *buf = avpkt->data;
584  int buf_size = avpkt->size;
585  H261Context *h = avctx->priv_data;
586  MpegEncContext *s = &h->s;
587  int ret;
588  AVFrame *pict = data;
589 
590  ff_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
591  ff_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
592 
593  h->gob_start_code_skipped = 0;
594 
595 retry:
596  init_get_bits(&s->gb, buf, buf_size * 8);
597 
598  if (!s->context_initialized)
599  // we need the IDCT permutation for reading a custom matrix
600  ff_mpv_idct_init(s);
601 
603 
604  /* skip if the header was thrashed */
605  if (ret < 0) {
606  av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
607  return -1;
608  }
609 
610  if (s->width != avctx->coded_width || s->height != avctx->coded_height) {
611  ParseContext pc = s->parse_context; // FIXME move this demuxing hack to libavformat
612  s->parse_context.buffer = 0;
614  s->parse_context = pc;
615  }
616 
617  if (!s->context_initialized) {
618  if ((ret = ff_mpv_common_init(s)) < 0)
619  return ret;
620 
621  ret = ff_set_dimensions(avctx, s->width, s->height);
622  if (ret < 0)
623  return ret;
624 
625  goto retry;
626  }
627 
628  // for skipping the frame
631 
632  if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
634  avctx->skip_frame >= AVDISCARD_ALL)
635  return get_consumed_bytes(s, buf_size);
636 
637  if (ff_mpv_frame_start(s, avctx) < 0)
638  return -1;
639 
641 
642  /* decode each macroblock */
643  s->mb_x = 0;
644  s->mb_y = 0;
645 
646  while (h->gob_number < (s->mb_height == 18 ? 12 : 5)) {
647  if (h261_resync(h) < 0)
648  break;
649  h261_decode_gob(h);
650  }
651  ff_mpv_frame_end(s);
652 
655 
656  if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
657  return ret;
659 
660  *got_frame = 1;
661 
662  return get_consumed_bytes(s, buf_size);
663 }
664 
666 {
667  H261Context *h = avctx->priv_data;
668  MpegEncContext *s = &h->s;
669 
671  return 0;
672 }
673 
675  .name = "h261",
676  .long_name = NULL_IF_CONFIG_SMALL("H.261"),
677  .type = AVMEDIA_TYPE_VIDEO,
678  .id = AV_CODEC_ID_H261,
679  .priv_data_size = sizeof(H261Context),
681  .close = h261_decode_end,
683  .capabilities = AV_CODEC_CAP_DR1,
685  .max_lowres = 3,
686 };
#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
#define SLICE_ERROR
Definition: mpegvideo.h:520
AVRational framerate
Definition: avcodec.h:2062
discard all frames except keyframes
Definition: avcodec.h:235
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:2272
int picture_number
Definition: mpegvideo.h:127
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define H261_CBP_VLC_BITS
Definition: h261dec.c:41
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
static int get_consumed_bytes(MpegEncContext *s, int buf_size)
returns the number of bytes consumed for building the current frame
Definition: h261dec.c:569
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:719
#define H261_MBA_VLC_BITS
Definition: h261dec.c:38
float re
Definition: fft.c:82
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
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
MpegEncContext s
Definition: h261.h:38
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define H261_MV_VLC_BITS
Definition: h261dec.c:40
#define INIT_VLC_STATIC(vlc, bits, a, b, c, d, e, f, g, static_size)
Definition: vlc.h:120
#define SKIP_COUNTER(name, gb, num)
Definition: get_bits.h:185
const uint8_t ff_h261_mba_bits[35]
Definition: h261data.c:48
int size
Definition: packet.h:364
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
Definition: avcodec.h:1655
int current_mv_x
Definition: h261.h:43
H261Context.
Definition: h261.h:37
#define MB_TYPE_INTRA
Definition: mpegutils.h:73
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:741
int gob_number
Definition: h261.h:45
#define TCOEFF_VLC_BITS
Definition: h261dec.c:42
#define AV_EF_BITSTREAM
detect bitstream specification deviations
Definition: avcodec.h:1649
mpegvideo header.
#define HAS_CBP(a)
Definition: mpegutils.h:101
#define FF_ARRAY_ELEMS(a)
discard all
Definition: avcodec.h:236
uint8_t permutated[64]
Definition: idctdsp.h:33
uint8_t run
Definition: svq3.c:205
#define SLICE_OK
Definition: mpegvideo.h:519
int mb_num
number of MBs of a picture
Definition: mpegvideo.h:133
#define MBA_STUFFING
Definition: h261dec.c:43
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample format(the sample packing is implied by the sample format) and sample rate.The lists are not just lists
AVCodec.
Definition: codec.h:190
const uint8_t ff_h261_mba_code[35]
Definition: h261data.c:34
static av_cold void h261_decode_init_static(void)
Definition: h261dec.c:51
RLTable.
Definition: rl.h:39
int qscale
QP.
Definition: mpegvideo.h:204
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static VLC h261_mtype_vlc
Definition: h261dec.c:47
const uint8_t ff_h261_cbp_tab[63][2]
Definition: h261data.c:95
static int h261_decode_picture_header(H261Context *h)
Decode the H.261 picture header.
Definition: h261dec.c:473
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1997
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
Definition: internal.h:40
The exact code depends on how similar the blocks are and how related they are to the block
uint8_t
#define av_cold
Definition: attributes.h:88
enum OutputFormat out_format
output format
Definition: mpegvideo.h:104
static int h261_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *avpkt)
Definition: h261dec.c:580
int current_mv_y
Definition: h261.h:44
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
#define MB_TYPE_16x16
Definition: mpegutils.h:54
Picture current_picture
copy of the current picture structure.
Definition: mpegvideo.h:180
GetBitContext last_resync_gb
used to search for the next resync marker
Definition: mpegvideo.h:358
uint8_t * data
Definition: packet.h:363
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:219
static int h261_decode_block(H261Context *h, int16_t *block, int n, int coded)
Decode a macroblock.
Definition: h261dec.c:257
#define ff_dlog(a,...)
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
#define AVOnce
Definition: thread.h:172
#define av_log(a,...)
static void ff_update_block_index(MpegEncContext *s)
Definition: mpegvideo.h:742
void ff_set_qscale(MpegEncContext *s, int qscale)
set qscale and update qscale dependent variables.
Definition: mpegvideo.c:2336
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:849
#define UPDATE_CACHE(name, gb)
Definition: get_bits.h:178
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
AVCodec ff_h261_decoder
Definition: h261dec.c:674
static av_cold int h261_decode_init(AVCodecContext *avctx)
Definition: h261dec.c:68
int mb_skipped
MUST BE SET only during DECODING.
Definition: mpegvideo.h:195
void(* clear_blocks)(int16_t *blocks)
Definition: blockdsp.h:37
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
unsigned int pos
Definition: spdifenc.c:412
simple assert() macros that are a bit more flexible than ISO C assert().
int mtype
Definition: h261.h:42
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:406
GetBitContext gb
Definition: mpegvideo.h:451
#define CLOSE_READER(name, gb)
Definition: get_bits.h:149
void ff_mpv_common_end(MpegEncContext *s)
Definition: mpegvideo.c:1109
#define GET_RL_VLC(level, run, name, gb, table, bits,max_depth, need_update)
Definition: get_bits.h:738
Definition: vlc.h:26
#define INIT_FIRST_VLC_RL(rl, static_size)
Definition: rl.h:71
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:397
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1640
static const int mvmap[17]
Definition: h261dec.c:227
int16_t(*[2] motion_val)[2]
Definition: mpegpicture.h:53
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 unsigned int show_bits(GetBitContext *s, int n)
Show 1-25 bits.
Definition: get_bits.h:446
int current_mba
Definition: h261.h:40
#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
int block_last_index[12]
last non zero coefficient in block
Definition: mpegvideo.h:86
const uint8_t ff_h261_mtype_code[10]
Definition: h261data.c:63
static av_cold int h261_decode_end(AVCodecContext *avctx)
Definition: h261dec.c:665
const uint8_t ff_h261_mtype_bits[10]
Definition: h261data.c:69
static int h261_decode_gob_header(H261Context *h)
Decode the group of blocks header or slice header.
Definition: h261dec.c:92
RL_VLC_ELEM * rl_vlc[32]
decoding only
Definition: rl.h:48
#define SHOW_UBITS(name, gb, num)
Definition: get_bits.h:211
const uint8_t ff_h261_mv_tab[17][2]
Definition: h261data.c:89
if(ret)
#define MV_TYPE_16X16
1 vector for the whole mb
Definition: mpegvideo.h:266
const int ff_h261_mtype_map[10]
Definition: h261data.c:75
#define AV_ONCE_INIT
Definition: thread.h:173
uint8_t * buffer
Definition: parser.h:29
Libavcodec external API header.
BlockDSPContext bdsp
Definition: mpegvideo.h:226
int mba_diff
Definition: h261.h:41
main external API structure.
Definition: avcodec.h:531
ScanTable intra_scantable
Definition: mpegvideo.h:91
int height
picture size. must be a multiple of 16
Definition: mpegvideo.h:100
#define IS_QUANT(a)
Definition: mpegutils.h:95
#define OPEN_READER(name, gb)
Definition: get_bits.h:138
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:521
static int h261_decode_gob(H261Context *h)
Definition: h261dec.c:537
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits1(GetBitContext *s)
Definition: get_bits.h:538
H.261 codec.
int coded_height
Definition: avcodec.h:719
#define IS_16X16(a)
Definition: mpegutils.h:86
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
static int h261_resync(H261Context *h)
Decode the group of blocks / video packet header.
Definition: h261dec.c:146
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:659
int context_initialized
Definition: mpegvideo.h:124
#define H261_MTYPE_VLC_BITS
Definition: h261dec.c:39
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
#define SKIP_CACHE(name, gb, num)
Definition: get_bits.h:180
RLTable ff_h261_rl_tcoeff
Definition: h261data.c:150
#define MB_TYPE_SKIP
Definition: mpegutils.h:62
static VLC h261_cbp_vlc
Definition: h261dec.c:49
#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 VLC h261_mv_vlc
Definition: h261dec.c:48
int gob_start_code_skipped
Definition: h261.h:46
uint8_t level
Definition: svq3.c:206
int mv[2][4][2]
motion vectors for a macroblock first coordinate : 0 = forward 1 = backward second " : depend...
Definition: mpegvideo.h:276
MpegEncContext.
Definition: mpegvideo.h:81
struct AVCodecContext * avctx
Definition: mpegvideo.h:98
#define SHOW_SBITS(name, gb, num)
Definition: get_bits.h:212
discard all non reference
Definition: avcodec.h:232
#define MB_TYPE_H261_FIL
Definition: h261.h:49
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
static VLC h261_mba_vlc
Definition: h261dec.c:46
Bi-dir predicted.
Definition: avutil.h:276
static int h261_decode_mb(H261Context *h)
Definition: h261dec.c:349
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
#define IS_INTRA4x4(a)
Definition: mpegutils.h:75
static int decode_mv_component(GetBitContext *gb, int v)
Definition: h261dec.c:231
#define MBA_STARTCODE
Definition: h261dec.c:44
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
int16_t(* block)[64]
points to one of the following blocks
Definition: mpegvideo.h:511
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:175
ParseContext parse_context
Definition: mpegvideo.h:362
VLC_TYPE(* table)[2]
code, bits
Definition: vlc.h:28
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2)
Decode skipped macroblocks.
Definition: h261dec.c:187
int key_frame
1 -> keyframe, 0-> not
Definition: frame.h:392
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:693
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1222
uint32_t * mb_type
types and macros are defined in mpegutils.h
Definition: mpegpicture.h:56
static int skip_1stop_8data_bits(GetBitContext *gb)
Definition: get_bits.h:854
static double val(void *priv, double ch)
Definition: aeval.c:76
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
#define MB_TYPE_L0
Definition: mpegutils.h:67
int i
Definition: input.c:407
Predicted.
Definition: avutil.h:275
#define check(x, y, S, v)