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
28 #include "libavutil/avassert.h"
31 #include "mpegvideo.h"
36 #define H261_MBA_VLC_BITS 9
37 #define H261_MTYPE_VLC_BITS 6
38 #define H261_MV_VLC_BITS 7
39 #define H261_CBP_VLC_BITS 9
40 #define TCOEFF_VLC_BITS 9
41 #define MBA_STUFFING 33
42 #define MBA_STARTCODE 34
44 extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
46 static VLC h261_mba_vlc;
47 static VLC h261_mtype_vlc;
48 static VLC h261_mv_vlc;
49 static VLC h261_cbp_vlc;
51 static int h261_decode_block(H261Context * h, DCTELEM * block, int n, int coded);
53 static av_cold void h261_decode_init_vlc(H261Context *h){
58 INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
59 ff_h261_mba_bits, 1, 1,
60 ff_h261_mba_code, 1, 1, 662);
61 INIT_VLC_STATIC(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
62 ff_h261_mtype_bits, 1, 1,
63 ff_h261_mtype_code, 1, 1, 80);
64 INIT_VLC_STATIC(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
65 &ff_h261_mv_tab[0][1], 2, 1,
66 &ff_h261_mv_tab[0][0], 2, 1, 144);
67 INIT_VLC_STATIC(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
68 &ff_h261_cbp_tab[0][1], 2, 1,
69 &ff_h261_cbp_tab[0][0], 2, 1, 512);
70 ff_init_rl(&ff_h261_rl_tcoeff, ff_h261_rl_table_store);
71 INIT_VLC_RL(ff_h261_rl_tcoeff, 552);
75 static av_cold int h261_decode_init(AVCodecContext *avctx){
76 H261Context *h= avctx->priv_data;
77 MpegEncContext * const s = &h->s;
80 ff_MPV_decode_defaults(s);
83 s->width = s->avctx->coded_width;
84 s->height = s->avctx->coded_height;
85 s->codec_id = s->avctx->codec->id;
87 s->out_format = FMT_H261;
89 avctx->pix_fmt= AV_PIX_FMT_YUV420P;
91 s->codec_id= avctx->codec->id;
93 h261_decode_init_vlc(h);
95 h->gob_start_code_skipped = 0;
101 * Decode the group of blocks header or slice header.
102 * @return <0 if an error occurred
104 static int h261_decode_gob_header(H261Context *h){
106 MpegEncContext * const s = &h->s;
108 if ( !h->gob_start_code_skipped ){
109 /* Check for GOB Start Code */
110 val = show_bits(&s->gb, 15);
115 skip_bits(&s->gb, 16);
118 h->gob_start_code_skipped = 0;
120 h->gob_number = get_bits(&s->gb, 4); /* GN */
121 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
123 /* Check if gob_number is valid */
124 if (s->mb_height==18){ //cif
125 if ((h->gob_number<=0) || (h->gob_number>12))
129 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
134 while (get_bits1(&s->gb) != 0) {
135 skip_bits(&s->gb, 8);
139 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
140 if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
144 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
145 // subsequent macroblocks, MBA is the difference between the absolute addresses of
146 // the macroblock and the last transmitted macroblock.
154 * Decode the group of blocks / video packet header.
155 * @return <0 if no resync found
157 static int ff_h261_resync(H261Context *h){
158 MpegEncContext * const s = &h->s;
161 if ( h->gob_start_code_skipped ){
162 ret= h261_decode_gob_header(h);
167 if(show_bits(&s->gb, 15)==0){
168 ret= h261_decode_gob_header(h);
172 //OK, it is not where it is supposed to be ...
173 s->gb= s->last_resync_gb;
174 align_get_bits(&s->gb);
175 left= get_bits_left(&s->gb);
177 for(;left>15+1+4+5; left-=8){
178 if(show_bits(&s->gb, 15)==0){
179 GetBitContext bak= s->gb;
181 ret= h261_decode_gob_header(h);
187 skip_bits(&s->gb, 8);
195 * Decode skipped macroblocks.
198 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
200 MpegEncContext * const s = &h->s;
205 for(i=mba1; i<mba2; i++){
208 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
209 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
210 xy = s->mb_x + s->mb_y * s->mb_stride;
211 ff_init_block_index(s);
212 ff_update_block_index(s);
215 s->block_last_index[j] = -1;
217 s->mv_dir = MV_DIR_FORWARD;
218 s->mv_type = MV_TYPE_16X16;
219 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
223 h->mtype &= ~MB_TYPE_H261_FIL;
225 ff_MPV_decode_mb(s, s->block);
231 static int decode_mv_component(GetBitContext *gb, int v){
232 static const int mvmap[17] = {
233 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12, -13, -14, -15, -16
235 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
237 /* check if mv_diff is valid */
241 mv_diff = mvmap[mv_diff];
243 if(mv_diff && !get_bits1(gb))
248 else if(v >= 16) v-= 32;
253 static int h261_decode_mb(H261Context *h){
254 MpegEncContext * const s = &h->s;
260 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
262 /* Check for slice end */
263 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
264 if (h->mba_diff == MBA_STARTCODE){ // start code
265 h->gob_start_code_skipped = 1;
269 while( h->mba_diff == MBA_STUFFING ); // stuffing
271 if ( h->mba_diff < 0 ){
272 if (get_bits_left(&s->gb) <= 7)
275 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
280 h->current_mba += h->mba_diff;
282 if ( h->current_mba > MBA_STUFFING )
285 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
286 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
287 xy = s->mb_x + s->mb_y * s->mb_stride;
288 ff_init_block_index(s);
289 ff_update_block_index(s);
292 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
294 av_log(s->avctx, AV_LOG_ERROR, "illegal mtype %d\n", h->mtype);
297 h->mtype = ff_h261_mtype_map[h->mtype];
300 if ( IS_QUANT ( h->mtype ) ){
301 ff_set_qscale(s, get_bits(&s->gb, 5));
304 s->mb_intra = IS_INTRA4x4(h->mtype);
307 if ( IS_16X16 ( h->mtype ) ){
308 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
309 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
310 // following three situations:
311 // 1) evaluating MVD for macroblocks 1, 12 and 23;
312 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
313 // 3) MTYPE of the previous macroblock was not MC.
314 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
321 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
322 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
329 if ( HAS_CBP( h->mtype ) ){
330 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
334 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
339 s->mv_dir = MV_DIR_FORWARD;
340 s->mv_type = MV_TYPE_16X16;
341 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
342 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
343 s->mv[0][0][1] = h->current_mv_y * 2;
346 /* decode each block */
347 if(s->mb_intra || HAS_CBP(h->mtype)){
348 s->dsp.clear_blocks(s->block[0]);
349 for (i = 0; i < 6; i++) {
350 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
356 for (i = 0; i < 6; i++)
357 s->block_last_index[i]= -1;
360 ff_MPV_decode_mb(s, s->block);
366 * Decode a macroblock.
367 * @return <0 if an error occurred
369 static int h261_decode_block(H261Context * h, DCTELEM * block,
372 MpegEncContext * const s = &h->s;
373 int code, level, i, j, run;
374 RLTable *rl = &ff_h261_rl_tcoeff;
375 const uint8_t *scan_table;
377 // For the variable length encoding there are two code tables, one being used for
378 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
379 // for all other LEVELs except the first one in INTRA blocks which is fixed length
380 // coded with 8 bits.
381 // NOTE: the two code tables only differ in one VLC so we handle that manually.
382 scan_table = s->intra_scantable.permutated;
385 level = get_bits(&s->gb, 8);
386 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
387 if((level&0x7F) == 0){
388 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
391 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
398 // EOB Not possible for first level when cbp is available (that's why the table is different)
401 int check = show_bits(&s->gb, 2);
404 skip_bits(&s->gb, 2);
405 block[0] = ( check & 0x1 ) ? -1 : 1;
412 s->block_last_index[n] = i - 1;
416 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
418 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
423 // 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.
424 run = get_bits(&s->gb, 6);
425 level = get_sbits(&s->gb, 8);
429 run = rl->table_run[code];
430 level = rl->table_level[code];
431 if (get_bits1(&s->gb))
436 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
443 s->block_last_index[n] = i-1;
448 * Decode the H.261 picture header.
449 * @return <0 if no startcode found
451 static int h261_decode_picture_header(H261Context *h){
452 MpegEncContext * const s = &h->s;
454 uint32_t startcode= 0;
456 for(i= get_bits_left(&s->gb); i>24; i-=1){
457 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
459 if(startcode == 0x10)
463 if (startcode != 0x10){
464 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
468 /* temporal reference */
469 i= get_bits(&s->gb, 5); /* picture timestamp */
470 if(i < (s->picture_number&31))
472 s->picture_number = (s->picture_number&~31) + i;
474 s->avctx->time_base= (AVRational){1001, 30000};
475 s->current_picture.f.pts = s->picture_number;
478 /* PTYPE starts here */
479 skip_bits1(&s->gb); /* split screen off */
480 skip_bits1(&s->gb); /* camera off */
481 skip_bits1(&s->gb); /* freeze picture release off */
483 format = get_bits1(&s->gb);
485 //only 2 formats possible
486 if (format == 0){//QCIF
498 s->mb_num = s->mb_width * s->mb_height;
500 skip_bits1(&s->gb); /* still image mode off */
501 skip_bits1(&s->gb); /* Reserved */
504 while (get_bits1(&s->gb) != 0){
505 skip_bits(&s->gb, 8);
508 // h261 has no I-FRAMES, but if we pass AV_PICTURE_TYPE_I for the first frame, the codec crashes if it does
509 // not contain all I-blocks (e.g. when a packet is lost)
510 s->pict_type = AV_PICTURE_TYPE_P;
516 static int h261_decode_gob(H261Context *h){
517 MpegEncContext * const s = &h->s;
519 ff_set_qscale(s, s->qscale);
522 while(h->current_mba <= MBA_STUFFING)
526 ret= h261_decode_mb(h);
529 h261_decode_mb_skipped(h, h->current_mba, 33);
532 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
536 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
543 * returns the number of bytes consumed for building the current frame
545 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
546 int pos= get_bits_count(&s->gb)>>3;
547 if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
548 if(pos+10>buf_size) pos=buf_size; // oops ;)
553 static int h261_decode_frame(AVCodecContext *avctx,
554 void *data, int *data_size,
557 const uint8_t *buf = avpkt->data;
558 int buf_size = avpkt->size;
559 H261Context *h= avctx->priv_data;
560 MpegEncContext *s = &h->s;
562 AVFrame *pict = data;
564 av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
565 av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
566 s->flags= avctx->flags;
567 s->flags2= avctx->flags2;
569 h->gob_start_code_skipped=0;
573 init_get_bits(&s->gb, buf, buf_size*8);
575 if(!s->context_initialized){
576 if (ff_MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
580 //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
581 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
582 int i= ff_find_unused_picture(s, 0);
585 s->current_picture_ptr= &s->picture[i];
588 ret = h261_decode_picture_header(h);
590 /* skip if the header was thrashed */
592 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
596 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
597 ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
598 s->parse_context.buffer=0;
599 ff_MPV_common_end(s);
600 s->parse_context= pc;
602 if (!s->context_initialized) {
603 avcodec_set_dimensions(avctx, s->width, s->height);
608 // for skipping the frame
609 s->current_picture.f.pict_type = s->pict_type;
610 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
612 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
613 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
614 || avctx->skip_frame >= AVDISCARD_ALL)
615 return get_consumed_bytes(s, buf_size);
617 if(ff_MPV_frame_start(s, avctx) < 0)
620 ff_er_frame_start(s);
622 /* decode each macroblock */
626 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
627 if(ff_h261_resync(h)<0)
633 av_assert0(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
634 av_assert0(s->current_picture.f.pict_type == s->pict_type);
636 *pict = s->current_picture_ptr->f;
637 ff_print_debug_info(s, pict);
639 *data_size = sizeof(AVFrame);
641 return get_consumed_bytes(s, buf_size);
644 static av_cold int h261_decode_end(AVCodecContext *avctx)
646 H261Context *h= avctx->priv_data;
647 MpegEncContext *s = &h->s;
649 ff_MPV_common_end(s);
653 AVCodec ff_h261_decoder = {
655 .type = AVMEDIA_TYPE_VIDEO,
656 .id = AV_CODEC_ID_H261,
657 .priv_data_size = sizeof(H261Context),
658 .init = h261_decode_init,
659 .close = h261_decode_end,
660 .decode = h261_decode_frame,
661 .capabilities = CODEC_CAP_DR1,
663 .long_name = NULL_IF_CONFIG_SMALL("H.261"),