Replace 5 with AOT_SBR when referring to the MPEG-4 audio object type.
[FFMpeg-mirror/lagarith.git] / libavcodec / h261dec.c
blobd5dcc0b2c4953ca6680a54b4091917ab764da44b
1 /*
2 * H261 decoder
3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Maarten Daniels
6 * This file is part of FFmpeg.
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.
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.
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
23 /**
24 * @file libavcodec/h261dec.c
25 * H.261 decoder.
28 #include "dsputil.h"
29 #include "avcodec.h"
30 #include "mpegvideo.h"
31 #include "h261.h"
32 #include "h261data.h"
34 #define H261_MBA_VLC_BITS 9
35 #define H261_MTYPE_VLC_BITS 6
36 #define H261_MV_VLC_BITS 7
37 #define H261_CBP_VLC_BITS 9
38 #define TCOEFF_VLC_BITS 9
39 #define MBA_STUFFING 33
40 #define MBA_STARTCODE 34
42 extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
44 static VLC h261_mba_vlc;
45 static VLC h261_mtype_vlc;
46 static VLC h261_mv_vlc;
47 static VLC h261_cbp_vlc;
49 static int h261_decode_block(H261Context * h, DCTELEM * block, int n, int coded);
51 static av_cold void h261_decode_init_vlc(H261Context *h){
52 static int done = 0;
54 if(!done){
55 done = 1;
56 INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
57 h261_mba_bits, 1, 1,
58 h261_mba_code, 1, 1, 662);
59 INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
60 h261_mtype_bits, 1, 1,
61 h261_mtype_code, 1, 1, 80);
62 INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
63 &h261_mv_tab[0][1], 2, 1,
64 &h261_mv_tab[0][0], 2, 1, 144);
65 INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
66 &h261_cbp_tab[0][1], 2, 1,
67 &h261_cbp_tab[0][0], 2, 1, 512);
68 init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
69 INIT_VLC_RL(h261_rl_tcoeff, 552);
73 static av_cold int h261_decode_init(AVCodecContext *avctx){
74 H261Context *h= avctx->priv_data;
75 MpegEncContext * const s = &h->s;
77 // set defaults
78 MPV_decode_defaults(s);
79 s->avctx = avctx;
81 s->width = s->avctx->coded_width;
82 s->height = s->avctx->coded_height;
83 s->codec_id = s->avctx->codec->id;
85 s->out_format = FMT_H261;
86 s->low_delay= 1;
87 avctx->pix_fmt= PIX_FMT_YUV420P;
89 s->codec_id= avctx->codec->id;
91 h261_decode_init_vlc(h);
93 h->gob_start_code_skipped = 0;
95 return 0;
98 /**
99 * decodes the group of blocks header or slice header.
100 * @return <0 if an error occurred
102 static int h261_decode_gob_header(H261Context *h){
103 unsigned int val;
104 MpegEncContext * const s = &h->s;
106 if ( !h->gob_start_code_skipped ){
107 /* Check for GOB Start Code */
108 val = show_bits(&s->gb, 15);
109 if(val)
110 return -1;
112 /* We have a GBSC */
113 skip_bits(&s->gb, 16);
116 h->gob_start_code_skipped = 0;
118 h->gob_number = get_bits(&s->gb, 4); /* GN */
119 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
121 /* Check if gob_number is valid */
122 if (s->mb_height==18){ //cif
123 if ((h->gob_number<=0) || (h->gob_number>12))
124 return -1;
126 else{ //qcif
127 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
128 return -1;
131 /* GEI */
132 while (get_bits1(&s->gb) != 0) {
133 skip_bits(&s->gb, 8);
136 if(s->qscale==0) {
137 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
138 if (s->avctx->error_recognition >= FF_ER_COMPLIANT)
139 return -1;
142 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
143 // subsequent macroblocks, MBA is the difference between the absolute addresses of
144 // the macroblock and the last transmitted macroblock.
145 h->current_mba = 0;
146 h->mba_diff = 0;
148 return 0;
152 * decodes the group of blocks / video packet header.
153 * @return <0 if no resync found
155 static int ff_h261_resync(H261Context *h){
156 MpegEncContext * const s = &h->s;
157 int left, ret;
159 if ( h->gob_start_code_skipped ){
160 ret= h261_decode_gob_header(h);
161 if(ret>=0)
162 return 0;
164 else{
165 if(show_bits(&s->gb, 15)==0){
166 ret= h261_decode_gob_header(h);
167 if(ret>=0)
168 return 0;
170 //OK, it is not where it is supposed to be ...
171 s->gb= s->last_resync_gb;
172 align_get_bits(&s->gb);
173 left= s->gb.size_in_bits - get_bits_count(&s->gb);
175 for(;left>15+1+4+5; left-=8){
176 if(show_bits(&s->gb, 15)==0){
177 GetBitContext bak= s->gb;
179 ret= h261_decode_gob_header(h);
180 if(ret>=0)
181 return 0;
183 s->gb= bak;
185 skip_bits(&s->gb, 8);
189 return -1;
193 * decodes skipped macroblocks
194 * @return 0
196 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
198 MpegEncContext * const s = &h->s;
199 int i;
201 s->mb_intra = 0;
203 for(i=mba1; i<mba2; i++){
204 int j, xy;
206 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
207 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
208 xy = s->mb_x + s->mb_y * s->mb_stride;
209 ff_init_block_index(s);
210 ff_update_block_index(s);
212 for(j=0;j<6;j++)
213 s->block_last_index[j] = -1;
215 s->mv_dir = MV_DIR_FORWARD;
216 s->mv_type = MV_TYPE_16X16;
217 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
218 s->mv[0][0][0] = 0;
219 s->mv[0][0][1] = 0;
220 s->mb_skipped = 1;
221 h->mtype &= ~MB_TYPE_H261_FIL;
223 MPV_decode_mb(s, s->block);
226 return 0;
229 static int decode_mv_component(GetBitContext *gb, int v){
230 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
232 /* check if mv_diff is valid */
233 if ( mv_diff < 0 )
234 return v;
236 mv_diff = mvmap[mv_diff];
238 if(mv_diff && !get_bits1(gb))
239 mv_diff= -mv_diff;
241 v += mv_diff;
242 if (v <=-16) v+= 32;
243 else if(v >= 16) v-= 32;
245 return v;
248 static int h261_decode_mb(H261Context *h){
249 MpegEncContext * const s = &h->s;
250 int i, cbp, xy;
252 cbp = 63;
253 // Read mba
255 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
257 /* Check for slice end */
258 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
259 if (h->mba_diff == MBA_STARTCODE){ // start code
260 h->gob_start_code_skipped = 1;
261 return SLICE_END;
264 while( h->mba_diff == MBA_STUFFING ); // stuffing
266 if ( h->mba_diff < 0 ){
267 if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
268 return SLICE_END;
270 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
271 return SLICE_ERROR;
274 h->mba_diff += 1;
275 h->current_mba += h->mba_diff;
277 if ( h->current_mba > MBA_STUFFING )
278 return SLICE_ERROR;
280 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
281 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
282 xy = s->mb_x + s->mb_y * s->mb_stride;
283 ff_init_block_index(s);
284 ff_update_block_index(s);
286 // Read mtype
287 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
288 h->mtype = h261_mtype_map[h->mtype];
290 // Read mquant
291 if ( IS_QUANT ( h->mtype ) ){
292 ff_set_qscale(s, get_bits(&s->gb, 5));
295 s->mb_intra = IS_INTRA4x4(h->mtype);
297 // Read mv
298 if ( IS_16X16 ( h->mtype ) ){
299 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
300 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
301 // following three situations:
302 // 1) evaluating MVD for macroblocks 1, 12 and 23;
303 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
304 // 3) MTYPE of the previous macroblock was not MC.
305 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
306 ( h->mba_diff != 1))
308 h->current_mv_x = 0;
309 h->current_mv_y = 0;
312 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
313 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
314 }else{
315 h->current_mv_x = 0;
316 h->current_mv_y = 0;
319 // Read cbp
320 if ( HAS_CBP( h->mtype ) ){
321 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
324 if(s->mb_intra){
325 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
326 goto intra;
329 //set motion vectors
330 s->mv_dir = MV_DIR_FORWARD;
331 s->mv_type = MV_TYPE_16X16;
332 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
333 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
334 s->mv[0][0][1] = h->current_mv_y * 2;
336 intra:
337 /* decode each block */
338 if(s->mb_intra || HAS_CBP(h->mtype)){
339 s->dsp.clear_blocks(s->block[0]);
340 for (i = 0; i < 6; i++) {
341 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
342 return SLICE_ERROR;
344 cbp+=cbp;
346 }else{
347 for (i = 0; i < 6; i++)
348 s->block_last_index[i]= -1;
351 MPV_decode_mb(s, s->block);
353 return SLICE_OK;
357 * decodes a macroblock
358 * @return <0 if an error occurred
360 static int h261_decode_block(H261Context * h, DCTELEM * block,
361 int n, int coded)
363 MpegEncContext * const s = &h->s;
364 int code, level, i, j, run;
365 RLTable *rl = &h261_rl_tcoeff;
366 const uint8_t *scan_table;
368 // For the variable length encoding there are two code tables, one being used for
369 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
370 // for all other LEVELs except the first one in INTRA blocks which is fixed length
371 // coded with 8 bits.
372 // NOTE: the two code tables only differ in one VLC so we handle that manually.
373 scan_table = s->intra_scantable.permutated;
374 if (s->mb_intra){
375 /* DC coef */
376 level = get_bits(&s->gb, 8);
377 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
378 if((level&0x7F) == 0){
379 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
380 return -1;
382 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
383 if (level == 255)
384 level = 128;
385 block[0] = level;
386 i = 1;
387 }else if(coded){
388 // Run Level Code
389 // EOB Not possible for first level when cbp is available (that's why the table is different)
390 // 0 1 1s
391 // * * 0*
392 int check = show_bits(&s->gb, 2);
393 i = 0;
394 if ( check & 0x2 ){
395 skip_bits(&s->gb, 2);
396 block[0] = ( check & 0x1 ) ? -1 : 1;
397 i = 1;
399 }else{
400 i = 0;
402 if(!coded){
403 s->block_last_index[n] = i - 1;
404 return 0;
406 for(;;){
407 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
408 if (code < 0){
409 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
410 return -1;
412 if (code == rl->n) {
413 /* escape */
414 // 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.
415 run = get_bits(&s->gb, 6);
416 level = get_sbits(&s->gb, 8);
417 }else if(code == 0){
418 break;
419 }else{
420 run = rl->table_run[code];
421 level = rl->table_level[code];
422 if (get_bits1(&s->gb))
423 level = -level;
425 i += run;
426 if (i >= 64){
427 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
428 return -1;
430 j = scan_table[i];
431 block[j] = level;
432 i++;
434 s->block_last_index[n] = i-1;
435 return 0;
439 * decodes the H261 picture header.
440 * @return <0 if no startcode found
442 static int h261_decode_picture_header(H261Context *h){
443 MpegEncContext * const s = &h->s;
444 int format, i;
445 uint32_t startcode= 0;
447 for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
448 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
450 if(startcode == 0x10)
451 break;
454 if (startcode != 0x10){
455 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
456 return -1;
459 /* temporal reference */
460 i= get_bits(&s->gb, 5); /* picture timestamp */
461 if(i < (s->picture_number&31))
462 i += 32;
463 s->picture_number = (s->picture_number&~31) + i;
465 s->avctx->time_base= (AVRational){1001, 30000};
466 s->current_picture.pts= s->picture_number;
469 /* PTYPE starts here */
470 skip_bits1(&s->gb); /* split screen off */
471 skip_bits1(&s->gb); /* camera off */
472 skip_bits1(&s->gb); /* freeze picture release off */
474 format = get_bits1(&s->gb);
476 //only 2 formats possible
477 if (format == 0){//QCIF
478 s->width = 176;
479 s->height = 144;
480 s->mb_width = 11;
481 s->mb_height = 9;
482 }else{//CIF
483 s->width = 352;
484 s->height = 288;
485 s->mb_width = 22;
486 s->mb_height = 18;
489 s->mb_num = s->mb_width * s->mb_height;
491 skip_bits1(&s->gb); /* still image mode off */
492 skip_bits1(&s->gb); /* Reserved */
494 /* PEI */
495 while (get_bits1(&s->gb) != 0){
496 skip_bits(&s->gb, 8);
499 // h261 has no I-FRAMES, but if we pass FF_I_TYPE for the first frame, the codec crashes if it does
500 // not contain all I-blocks (e.g. when a packet is lost)
501 s->pict_type = FF_P_TYPE;
503 h->gob_number = 0;
504 return 0;
507 static int h261_decode_gob(H261Context *h){
508 MpegEncContext * const s = &h->s;
510 ff_set_qscale(s, s->qscale);
512 /* decode mb's */
513 while(h->current_mba <= MBA_STUFFING)
515 int ret;
516 /* DCT & quantize */
517 ret= h261_decode_mb(h);
518 if(ret<0){
519 if(ret==SLICE_END){
520 h261_decode_mb_skipped(h, h->current_mba, 33);
521 return 0;
523 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
524 return -1;
527 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
530 return -1;
534 * returns the number of bytes consumed for building the current frame
536 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
537 int pos= get_bits_count(&s->gb)>>3;
538 if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
539 if(pos+10>buf_size) pos=buf_size; // oops ;)
541 return pos;
544 static int h261_decode_frame(AVCodecContext *avctx,
545 void *data, int *data_size,
546 AVPacket *avpkt)
548 const uint8_t *buf = avpkt->data;
549 int buf_size = avpkt->size;
550 H261Context *h= avctx->priv_data;
551 MpegEncContext *s = &h->s;
552 int ret;
553 AVFrame *pict = data;
555 dprintf(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
556 dprintf(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
557 s->flags= avctx->flags;
558 s->flags2= avctx->flags2;
560 h->gob_start_code_skipped=0;
562 retry:
564 init_get_bits(&s->gb, buf, buf_size*8);
566 if(!s->context_initialized){
567 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
568 return -1;
571 //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
572 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
573 int i= ff_find_unused_picture(s, 0);
574 s->current_picture_ptr= &s->picture[i];
577 ret = h261_decode_picture_header(h);
579 /* skip if the header was thrashed */
580 if (ret < 0){
581 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
582 return -1;
585 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
586 ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
587 s->parse_context.buffer=0;
588 MPV_common_end(s);
589 s->parse_context= pc;
591 if (!s->context_initialized) {
592 avcodec_set_dimensions(avctx, s->width, s->height);
594 goto retry;
597 // for hurry_up==5
598 s->current_picture.pict_type= s->pict_type;
599 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
601 /* skip everything if we are in a hurry>=5 */
602 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
603 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
604 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
605 || avctx->skip_frame >= AVDISCARD_ALL)
606 return get_consumed_bytes(s, buf_size);
608 if(MPV_frame_start(s, avctx) < 0)
609 return -1;
611 ff_er_frame_start(s);
613 /* decode each macroblock */
614 s->mb_x=0;
615 s->mb_y=0;
617 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
618 if(ff_h261_resync(h)<0)
619 break;
620 h261_decode_gob(h);
622 MPV_frame_end(s);
624 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
625 assert(s->current_picture.pict_type == s->pict_type);
626 *pict= *(AVFrame*)s->current_picture_ptr;
627 ff_print_debug_info(s, pict);
629 *data_size = sizeof(AVFrame);
631 return get_consumed_bytes(s, buf_size);
634 static av_cold int h261_decode_end(AVCodecContext *avctx)
636 H261Context *h= avctx->priv_data;
637 MpegEncContext *s = &h->s;
639 MPV_common_end(s);
640 return 0;
643 AVCodec h261_decoder = {
644 "h261",
645 CODEC_TYPE_VIDEO,
646 CODEC_ID_H261,
647 sizeof(H261Context),
648 h261_decode_init,
649 NULL,
650 h261_decode_end,
651 h261_decode_frame,
652 CODEC_CAP_DR1,
653 .long_name = NULL_IF_CONFIG_SMALL("H.261"),