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
30 #include "mpegvideo.h"
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
43 extern uint8_t ff_h261_rl_table_store[2][2*MAX_RUN + MAX_LEVEL + 3];
45 static VLC h261_mba_vlc;
46 static VLC h261_mtype_vlc;
47 static VLC h261_mv_vlc;
48 static VLC h261_cbp_vlc;
50 static int h261_decode_block(H261Context * h, DCTELEM * block, int n, int coded);
52 static av_cold void h261_decode_init_vlc(H261Context *h){
57 INIT_VLC_STATIC(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
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);
69 ff_init_rl(&h261_rl_tcoeff, ff_h261_rl_table_store);
70 INIT_VLC_RL(h261_rl_tcoeff, 552);
74 static av_cold int h261_decode_init(AVCodecContext *avctx){
75 H261Context *h= avctx->priv_data;
76 MpegEncContext * const s = &h->s;
79 ff_MPV_decode_defaults(s);
82 s->width = s->avctx->coded_width;
83 s->height = s->avctx->coded_height;
84 s->codec_id = s->avctx->codec->id;
86 s->out_format = FMT_H261;
88 avctx->pix_fmt= PIX_FMT_YUV420P;
90 s->codec_id= avctx->codec->id;
92 h261_decode_init_vlc(h);
94 h->gob_start_code_skipped = 0;
100 * Decode the group of blocks header or slice header.
101 * @return <0 if an error occurred
103 static int h261_decode_gob_header(H261Context *h){
105 MpegEncContext * const s = &h->s;
107 if ( !h->gob_start_code_skipped ){
108 /* Check for GOB Start Code */
109 val = show_bits(&s->gb, 15);
114 skip_bits(&s->gb, 16);
117 h->gob_start_code_skipped = 0;
119 h->gob_number = get_bits(&s->gb, 4); /* GN */
120 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
122 /* Check if gob_number is valid */
123 if (s->mb_height==18){ //cif
124 if ((h->gob_number<=0) || (h->gob_number>12))
128 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
133 while (get_bits1(&s->gb) != 0) {
134 skip_bits(&s->gb, 8);
138 av_log(s->avctx, AV_LOG_ERROR, "qscale has forbidden 0 value\n");
139 if (s->avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT))
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.
153 * Decode the group of blocks / video packet header.
154 * @return <0 if no resync found
156 static int ff_h261_resync(H261Context *h){
157 MpegEncContext * const s = &h->s;
160 if ( h->gob_start_code_skipped ){
161 ret= h261_decode_gob_header(h);
166 if(show_bits(&s->gb, 15)==0){
167 ret= h261_decode_gob_header(h);
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);
176 for(;left>15+1+4+5; left-=8){
177 if(show_bits(&s->gb, 15)==0){
178 GetBitContext bak= s->gb;
180 ret= h261_decode_gob_header(h);
186 skip_bits(&s->gb, 8);
194 * Decode skipped macroblocks.
197 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
199 MpegEncContext * const s = &h->s;
204 for(i=mba1; i<mba2; i++){
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;
210 ff_init_block_index(s);
211 ff_update_block_index(s);
214 s->block_last_index[j] = -1;
216 s->mv_dir = MV_DIR_FORWARD;
217 s->mv_type = MV_TYPE_16X16;
218 s->current_picture.f.mb_type[xy] = MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
222 h->mtype &= ~MB_TYPE_H261_FIL;
224 ff_MPV_decode_mb(s, s->block);
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);
233 /* check if mv_diff is valid */
237 mv_diff = mvmap[mv_diff];
239 if(mv_diff && !get_bits1(gb))
244 else if(v >= 16) v-= 32;
249 static int h261_decode_mb(H261Context *h){
250 MpegEncContext * const s = &h->s;
256 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
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;
265 while( h->mba_diff == MBA_STUFFING ); // stuffing
267 if ( h->mba_diff < 0 ){
268 if (get_bits_left(&s->gb) <= 7)
271 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
276 h->current_mba += h->mba_diff;
278 if ( h->current_mba > MBA_STUFFING )
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;
284 ff_init_block_index(s);
285 ff_update_block_index(s);
288 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
290 av_log(s->avctx, AV_LOG_ERROR, "illegal mtype %d\n", h->mtype);
293 h->mtype = h261_mtype_map[h->mtype];
296 if ( IS_QUANT ( h->mtype ) ){
297 ff_set_qscale(s, get_bits(&s->gb, 5));
300 s->mb_intra = IS_INTRA4x4(h->mtype);
303 if ( IS_16X16 ( h->mtype ) ){
304 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
305 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
306 // following three situations:
307 // 1) evaluating MVD for macroblocks 1, 12 and 23;
308 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
309 // 3) MTYPE of the previous macroblock was not MC.
310 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
317 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
318 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
325 if ( HAS_CBP( h->mtype ) ){
326 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
330 s->current_picture.f.mb_type[xy] = MB_TYPE_INTRA;
335 s->mv_dir = MV_DIR_FORWARD;
336 s->mv_type = MV_TYPE_16X16;
337 s->current_picture.f.mb_type[xy] = MB_TYPE_16x16 | MB_TYPE_L0;
338 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
339 s->mv[0][0][1] = h->current_mv_y * 2;
342 /* decode each block */
343 if(s->mb_intra || HAS_CBP(h->mtype)){
344 s->dsp.clear_blocks(s->block[0]);
345 for (i = 0; i < 6; i++) {
346 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
352 for (i = 0; i < 6; i++)
353 s->block_last_index[i]= -1;
356 ff_MPV_decode_mb(s, s->block);
362 * Decode a macroblock.
363 * @return <0 if an error occurred
365 static int h261_decode_block(H261Context * h, DCTELEM * block,
368 MpegEncContext * const s = &h->s;
369 int code, level, i, j, run;
370 RLTable *rl = &h261_rl_tcoeff;
371 const uint8_t *scan_table;
373 // For the variable length encoding there are two code tables, one being used for
374 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
375 // for all other LEVELs except the first one in INTRA blocks which is fixed length
376 // coded with 8 bits.
377 // NOTE: the two code tables only differ in one VLC so we handle that manually.
378 scan_table = s->intra_scantable.permutated;
381 level = get_bits(&s->gb, 8);
382 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
383 if((level&0x7F) == 0){
384 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
387 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
394 // EOB Not possible for first level when cbp is available (that's why the table is different)
397 int check = show_bits(&s->gb, 2);
400 skip_bits(&s->gb, 2);
401 block[0] = ( check & 0x1 ) ? -1 : 1;
408 s->block_last_index[n] = i - 1;
412 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
414 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
419 // 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.
420 run = get_bits(&s->gb, 6);
421 level = get_sbits(&s->gb, 8);
425 run = rl->table_run[code];
426 level = rl->table_level[code];
427 if (get_bits1(&s->gb))
432 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
439 s->block_last_index[n] = i-1;
444 * Decode the H.261 picture header.
445 * @return <0 if no startcode found
447 static int h261_decode_picture_header(H261Context *h){
448 MpegEncContext * const s = &h->s;
450 uint32_t startcode= 0;
452 for(i= get_bits_left(&s->gb); i>24; i-=1){
453 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
455 if(startcode == 0x10)
459 if (startcode != 0x10){
460 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
464 /* temporal reference */
465 i= get_bits(&s->gb, 5); /* picture timestamp */
466 if(i < (s->picture_number&31))
468 s->picture_number = (s->picture_number&~31) + i;
470 s->avctx->time_base= (AVRational){1001, 30000};
471 s->current_picture.f.pts = s->picture_number;
474 /* PTYPE starts here */
475 skip_bits1(&s->gb); /* split screen off */
476 skip_bits1(&s->gb); /* camera off */
477 skip_bits1(&s->gb); /* freeze picture release off */
479 format = get_bits1(&s->gb);
481 //only 2 formats possible
482 if (format == 0){//QCIF
494 s->mb_num = s->mb_width * s->mb_height;
496 skip_bits1(&s->gb); /* still image mode off */
497 skip_bits1(&s->gb); /* Reserved */
500 while (get_bits1(&s->gb) != 0){
501 skip_bits(&s->gb, 8);
504 // h261 has no I-FRAMES, but if we pass AV_PICTURE_TYPE_I for the first frame, the codec crashes if it does
505 // not contain all I-blocks (e.g. when a packet is lost)
506 s->pict_type = AV_PICTURE_TYPE_P;
512 static int h261_decode_gob(H261Context *h){
513 MpegEncContext * const s = &h->s;
515 ff_set_qscale(s, s->qscale);
518 while(h->current_mba <= MBA_STUFFING)
522 ret= h261_decode_mb(h);
525 h261_decode_mb_skipped(h, h->current_mba, 33);
528 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
532 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
539 * returns the number of bytes consumed for building the current frame
541 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
542 int pos= get_bits_count(&s->gb)>>3;
543 if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
544 if(pos+10>buf_size) pos=buf_size; // oops ;)
549 static int h261_decode_frame(AVCodecContext *avctx,
550 void *data, int *data_size,
553 const uint8_t *buf = avpkt->data;
554 int buf_size = avpkt->size;
555 H261Context *h= avctx->priv_data;
556 MpegEncContext *s = &h->s;
558 AVFrame *pict = data;
560 av_dlog(avctx, "*****frame %d size=%d\n", avctx->frame_number, buf_size);
561 av_dlog(avctx, "bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
562 s->flags= avctx->flags;
563 s->flags2= avctx->flags2;
565 h->gob_start_code_skipped=0;
569 init_get_bits(&s->gb, buf, buf_size*8);
571 if(!s->context_initialized){
572 if (ff_MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
576 //we need to set current_picture_ptr before reading the header, otherwise we cannot store anyting im there
577 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
578 int i= ff_find_unused_picture(s, 0);
581 s->current_picture_ptr= &s->picture[i];
584 ret = h261_decode_picture_header(h);
586 /* skip if the header was thrashed */
588 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
592 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
593 ParseContext pc= s->parse_context; //FIXME move this demuxing hack to libavformat
594 s->parse_context.buffer=0;
595 ff_MPV_common_end(s);
596 s->parse_context= pc;
598 if (!s->context_initialized) {
599 avcodec_set_dimensions(avctx, s->width, s->height);
604 // for skipping the frame
605 s->current_picture.f.pict_type = s->pict_type;
606 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
608 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
609 ||(avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
610 || avctx->skip_frame >= AVDISCARD_ALL)
611 return get_consumed_bytes(s, buf_size);
613 if(ff_MPV_frame_start(s, avctx) < 0)
616 ff_er_frame_start(s);
618 /* decode each macroblock */
622 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
623 if(ff_h261_resync(h)<0)
629 assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
630 assert(s->current_picture.f.pict_type == s->pict_type);
632 *pict = s->current_picture_ptr->f;
633 ff_print_debug_info(s, pict);
635 *data_size = sizeof(AVFrame);
637 return get_consumed_bytes(s, buf_size);
640 static av_cold int h261_decode_end(AVCodecContext *avctx)
642 H261Context *h= avctx->priv_data;
643 MpegEncContext *s = &h->s;
645 ff_MPV_common_end(s);
649 AVCodec ff_h261_decoder = {
651 .type = AVMEDIA_TYPE_VIDEO,
653 .priv_data_size = sizeof(H261Context),
654 .init = h261_decode_init,
655 .close = h261_decode_end,
656 .decode = h261_decode_frame,
657 .capabilities = CODEC_CAP_DR1,
659 .long_name = NULL_IF_CONFIG_SMALL("H.261"),