h261dec.c
Go to the documentation of this file.
1 /*
2  * H261 decoder
3  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4  * Copyright (c) 2004 Maarten Daniels
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
28 #include "dsputil.h"
29 #include "avcodec.h"
30 #include "mpegvideo.h"
31 #include "h263.h"
32 #include "h261.h"
33 #include "h261data.h"
34 
35 #define H261_MBA_VLC_BITS 9
36 #define H261_MTYPE_VLC_BITS 6
37 #define H261_MV_VLC_BITS 7
38 #define H261_CBP_VLC_BITS 9
39 #define TCOEFF_VLC_BITS 9
40 #define MBA_STUFFING 33
41 #define MBA_STARTCODE 34
42 
43 extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
44 
49 
50 static int h261_decode_block(H261Context * h, DCTELEM * block, int n, int coded);
51 
53  static int done = 0;
54 
55  if(!done){
56  done = 1;
57  INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
58  h261_mba_bits, 1, 1,
59  h261_mba_code, 1, 1, 662);
60  INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
61  h261_mtype_bits, 1, 1,
62  h261_mtype_code, 1, 1, 80);
63  INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
64  &h261_mv_tab[0][1], 2, 1,
65  &h261_mv_tab[0][0], 2, 1, 144);
66  INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
67  &h261_cbp_tab[0][1], 2, 1,
68  &h261_cbp_tab[0][0], 2, 1, 512);
71  }
72 }
73 
75  H261Context *h= avctx->priv_data;
76  MpegEncContext * const s = &h->s;
77 
78  // set defaults
80  s->avctx = avctx;
81 
82  s->width = s->avctx->coded_width;
83  s->height = s->avctx->coded_height;
84  s->codec_id = s->avctx->codec->id;
85 
86  s->out_format = FMT_H261;
87  s->low_delay= 1;
88  avctx->pix_fmt= PIX_FMT_YUV420P;
89 
90  s->codec_id= avctx->codec->id;
91 
93 
95 
96  return 0;
97 }
98 
104  unsigned int val;
105  MpegEncContext * const s = &h->s;
106 
107  if ( !h->gob_start_code_skipped ){
108  /* Check for GOB Start Code */
109  val = show_bits(&s->gb, 15);
110  if(val)
111  return -1;
112 
113  /* We have a GBSC */
114  skip_bits(&s->gb, 16);
115  }
116 
117  h->gob_start_code_skipped = 0;
118 
119  h->gob_number = get_bits(&s->gb, 4); /* GN */
120  s->qscale = get_bits(&s->gb, 5); /* GQUANT */
121 
122  /* Check if gob_number is valid */
123  if (s->mb_height==18){ //cif
124  if ((h->gob_number<=0) || (h->gob_number>12))
125  return -1;
126  }
127  else{ //qcif
128  if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
129  return -1;
130  }
131 
132  /* GEI */
133  while (get_bits1(&s->gb) != 0) {
134  skip_bits(&s->gb, 8);
135  }
136 
137  if(s->qscale==0) {
138  av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
140  return -1;
141  }
142 
143  // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
144  // subsequent macroblocks, MBA is the difference between the absolute addresses of
145  // the macroblock and the last transmitted macroblock.
146  h->current_mba = 0;
147  h->mba_diff = 0;
148 
149  return 0;
150 }
151 
157  MpegEncContext * const s = &h->s;
158  int left, ret;
159 
160  if ( h->gob_start_code_skipped ){
161  ret= h261_decode_gob_header(h);
162  if(ret>=0)
163  return 0;
164  }
165  else{
166  if(show_bits(&s->gb, 15)==0){
167  ret= h261_decode_gob_header(h);
168  if(ret>=0)
169  return 0;
170  }
171  //OK, it is not where it is supposed to be ...
172  s->gb= s->last_resync_gb;
173  align_get_bits(&s->gb);
174  left= get_bits_left(&s->gb);
175 
176  for(;left>15+1+4+5; left-=8){
177  if(show_bits(&s->gb, 15)==0){
178  GetBitContext bak= s->gb;
179 
180  ret= h261_decode_gob_header(h);
181  if(ret>=0)
182  return 0;
183 
184  s->gb= bak;
185  }
186  skip_bits(&s->gb, 8);
187  }
188  }
189 
190  return -1;
191 }
192 
197 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
198 {
199  MpegEncContext * const s = &h->s;
200  int i;
201 
202  s->mb_intra = 0;
203 
204  for(i=mba1; i<mba2; i++){
205  int j, xy;
206 
207  s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
208  s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
209  xy = s->mb_x + s->mb_y * s->mb_stride;
212 
213  for(j=0;j<6;j++)
214  s->block_last_index[j] = -1;
215 
216  s->mv_dir = MV_DIR_FORWARD;
217  s->mv_type = MV_TYPE_16X16;
219  s->mv[0][0][0] = 0;
220  s->mv[0][0][1] = 0;
221  s->mb_skipped = 1;
222  h->mtype &= ~MB_TYPE_H261_FIL;
223 
224  MPV_decode_mb(s, s->block);
225  }
226 
227  return 0;
228 }
229 
230 static int decode_mv_component(GetBitContext *gb, int v){
231  int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
232 
233  /* check if mv_diff is valid */
234  if ( mv_diff < 0 )
235  return v;
236 
237  mv_diff = mvmap[mv_diff];
238 
239  if(mv_diff && !get_bits1(gb))
240  mv_diff= -mv_diff;
241 
242  v += mv_diff;
243  if (v <=-16) v+= 32;
244  else if(v >= 16) v-= 32;
245 
246  return v;
247 }
248 
250  MpegEncContext * const s = &h->s;
251  int i, cbp, xy;
252 
253  cbp = 63;
254  // Read mba
255  do{
256  h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
257 
258  /* Check for slice end */
259  /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
260  if (h->mba_diff == MBA_STARTCODE){ // start code
261  h->gob_start_code_skipped = 1;
262  return SLICE_END;
263  }
264  }
265  while( h->mba_diff == MBA_STUFFING ); // stuffing
266 
267  if ( h->mba_diff < 0 ){
268  if (get_bits_left(&s->gb) <= 7)
269  return SLICE_END;
270 
271  av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
272  return SLICE_ERROR;
273  }
274 
275  h->mba_diff += 1;
276  h->current_mba += h->mba_diff;
277 
278  if ( h->current_mba > MBA_STUFFING )
279  return SLICE_ERROR;
280 
281  s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
282  s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
283  xy = s->mb_x + s->mb_y * s->mb_stride;
286 
287  // Read mtype
288  h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
289  if (h->mtype < 0 || h->mtype >= FF_ARRAY_ELEMS(h261_mtype_map)) {
290  av_log(s->avctx, AV_LOG_ERROR, "Invalid mtype index %d\n",
291  h->mtype);
292  return SLICE_ERROR;
293  }
294  h->mtype = h261_mtype_map[h->mtype];
295 
296  // Read mquant
297  if ( IS_QUANT ( h->mtype ) ){
298  ff_set_qscale(s, get_bits(&s->gb, 5));
299  }
300 
301  s->mb_intra = IS_INTRA4x4(h->mtype);
302 
303  // Read mv
304  if ( IS_16X16 ( h->mtype ) ){
305  // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
306  // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
307  // following three situations:
308  // 1) evaluating MVD for macroblocks 1, 12 and 23;
309  // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
310  // 3) MTYPE of the previous macroblock was not MC.
311  if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
312  ( h->mba_diff != 1))
313  {
314  h->current_mv_x = 0;
315  h->current_mv_y = 0;
316  }
317 
320  }else{
321  h->current_mv_x = 0;
322  h->current_mv_y = 0;
323  }
324 
325  // Read cbp
326  if ( HAS_CBP( h->mtype ) ){
327  cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
328  }
329 
330  if(s->mb_intra){
332  goto intra;
333  }
334 
335  //set motion vectors
336  s->mv_dir = MV_DIR_FORWARD;
337  s->mv_type = MV_TYPE_16X16;
339  s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
340  s->mv[0][0][1] = h->current_mv_y * 2;
341 
342 intra:
343  /* decode each block */
344  if(s->mb_intra || HAS_CBP(h->mtype)){
345  s->dsp.clear_blocks(s->block[0]);
346  for (i = 0; i < 6; i++) {
347  if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
348  return SLICE_ERROR;
349  }
350  cbp+=cbp;
351  }
352  }else{
353  for (i = 0; i < 6; i++)
354  s->block_last_index[i]= -1;
355  }
356 
357  MPV_decode_mb(s, s->block);
358 
359  return SLICE_OK;
360 }
361 
367  int n, int coded)
368 {
369  MpegEncContext * const s = &h->s;
370  int code, level, i, j, run;
371  RLTable *rl = &h261_rl_tcoeff;
372  const uint8_t *scan_table;
373 
374  // For the variable length encoding there are two code tables, one being used for
375  // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
376  // for all other LEVELs except the first one in INTRA blocks which is fixed length
377  // coded with 8 bits.
378  // NOTE: the two code tables only differ in one VLC so we handle that manually.
379  scan_table = s->intra_scantable.permutated;
380  if (s->mb_intra){
381  /* DC coef */
382  level = get_bits(&s->gb, 8);
383  // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
384  if((level&0x7F) == 0){
385  av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
386  return -1;
387  }
388  // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
389  if (level == 255)
390  level = 128;
391  block[0] = level;
392  i = 1;
393  }else if(coded){
394  // Run Level Code
395  // EOB Not possible for first level when cbp is available (that's why the table is different)
396  // 0 1 1s
397  // * * 0*
398  int check = show_bits(&s->gb, 2);
399  i = 0;
400  if ( check & 0x2 ){
401  skip_bits(&s->gb, 2);
402  block[0] = ( check & 0x1 ) ? -1 : 1;
403  i = 1;
404  }
405  }else{
406  i = 0;
407  }
408  if(!coded){
409  s->block_last_index[n] = i - 1;
410  return 0;
411  }
412  for(;;){
413  code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
414  if (code < 0){
415  av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
416  return -1;
417  }
418  if (code == rl->n) {
419  /* escape */
420  // The remaining combinations of (run, level) are encoded with a 20-bit word consisting of 6 bits escape, 6 bits run and 8 bits level.
421  run = get_bits(&s->gb, 6);
422  level = get_sbits(&s->gb, 8);
423  }else if(code == 0){
424  break;
425  }else{
426  run = rl->table_run[code];
427  level = rl->table_level[code];
428  if (get_bits1(&s->gb))
429  level = -level;
430  }
431  i += run;
432  if (i >= 64){
433  av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
434  return -1;
435  }
436  j = scan_table[i];
437  block[j] = level;
438  i++;
439  }
440  s->block_last_index[n] = i-1;
441  return 0;
442 }
443 
449  MpegEncContext * const s = &h->s;
450  int format, i;
451  uint32_t startcode= 0;
452 
453  for(i= get_bits_left(&s->gb); i>24; i-=1){
454  startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
455 
456  if(startcode == 0x10)
457  break;
458  }
459 
460  if (startcode != 0x10){
461  av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
462  return -1;
463  }
464 
465  /* temporal reference */
466  i= get_bits(&s->gb, 5); /* picture timestamp */
467  if(i < (s->picture_number&31))
468  i += 32;
469  s->picture_number = (s->picture_number&~31) + i;
470 
471  s->avctx->time_base= (AVRational){1001, 30000};
473 
474 
475  /* PTYPE starts here */
476  skip_bits1(&s->gb); /* split screen off */
477  skip_bits1(&s->gb); /* camera off */
478  skip_bits1(&s->gb); /* freeze picture release off */
479 
480  format = get_bits1(&s->gb);
481 
482  //only 2 formats possible
483  if (format == 0){//QCIF
484  s->width = 176;
485  s->height = 144;
486  s->mb_width = 11;
487  s->mb_height = 9;
488  }else{//CIF
489  s->width = 352;
490  s->height = 288;
491  s->mb_width = 22;
492  s->mb_height = 18;
493  }
494 
495  s->mb_num = s->mb_width * s->mb_height;
496 
497  skip_bits1(&s->gb); /* still image mode off */
498  skip_bits1(&s->gb); /* Reserved */
499 
500  /* PEI */
501  while (get_bits1(&s->gb) != 0){
502  skip_bits(&s->gb, 8);
503  }
504 
505  // h261 has no I-FRAMES, but if we pass AV_PICTURE_TYPE_I for the first frame, the codec crashes if it does
506  // not contain all I-blocks (e.g. when a packet is lost)
508 
509  h->gob_number = 0;
510  return 0;
511 }
512 
514  MpegEncContext * const s = &h->s;
515 
516  ff_set_qscale(s, s->qscale);
517 
518  /* decode mb's */
519  while(h->current_mba <= MBA_STUFFING)
520  {
521  int ret;
522  /* DCT & quantize */
523  ret= h261_decode_mb(h);
524  if(ret<0){
525  if(ret==SLICE_END){
527  return 0;
528  }
529  av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
530  return -1;
531  }
532 
534  }
535 
536  return -1;
537 }
538 
542 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
543  int pos= get_bits_count(&s->gb)>>3;
544  if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
545  if(pos+10>buf_size) pos=buf_size; // oops ;)
546 
547  return pos;
548 }
549 
551  void *data, int *data_size,
552  AVPacket *avpkt)
553 {
554  const uint8_t *buf = avpkt->data;
555  int buf_size = avpkt->size;
556  H261Context *h= avctx->priv_data;
557  MpegEncContext *s = &h->s;
558  int ret;
559  AVFrame *pict = data;
560 
561  av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
562  av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
563  s->flags= avctx->flags;
564  s->flags2= avctx->flags2;
565 
567 
568 retry:
569 
570  init_get_bits(&s->gb, buf, buf_size*8);
571 
572  if(!s->context_initialized){
573  if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
574  return -1;
575  }
576 
577  //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
578  if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
579  int i= ff_find_unused_picture(s, 0);
580  if (i < 0)
581  return i;
582  s->current_picture_ptr= &s->picture[i];
583  }
584 
586 
587  /* skip if the header was thrashed */
588  if (ret < 0){
589  av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
590  return -1;
591  }
592 
593  if (s->width != avctx->coded_width || s->height != avctx->coded_height){
594  ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
595  s->parse_context.buffer=0;
596  MPV_common_end(s);
597  s->parse_context= pc;
598  }
599  if (!s->context_initialized) {
600  avcodec_set_dimensions(avctx, s->width, s->height);
601 
602  goto retry;
603  }
604 
605  // for skipping the frame
608 
611  || avctx->skip_frame >= AVDISCARD_ALL)
612  return get_consumed_bytes(s, buf_size);
613 
614  if(MPV_frame_start(s, avctx) < 0)
615  return -1;
616 
618 
619  /* decode each macroblock */
620  s->mb_x=0;
621  s->mb_y=0;
622 
623  while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
624  if(ff_h261_resync(h)<0)
625  break;
626  h261_decode_gob(h);
627  }
628  MPV_frame_end(s);
629 
631 assert(s->current_picture.f.pict_type == s->pict_type);
632  *pict= *(AVFrame*)s->current_picture_ptr;
633  ff_print_debug_info(s, pict);
634 
635  *data_size = sizeof(AVFrame);
636 
637  return get_consumed_bytes(s, buf_size);
638 }
639 
641 {
642  H261Context *h= avctx->priv_data;
643  MpegEncContext *s = &h->s;
644 
645  MPV_common_end(s);
646  return 0;
647 }
648 
650  .name = "h261",
651  .type = AVMEDIA_TYPE_VIDEO,
652  .id = CODEC_ID_H261,
653  .priv_data_size = sizeof(H261Context),
657  .capabilities = CODEC_CAP_DR1,
658  .max_lowres = 3,
659  .long_name = NULL_IF_CONFIG_SMALL("H.261"),
660 };