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