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"
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
){
56 init_vlc(&h261_mba_vlc
, H261_MBA_VLC_BITS
, 35,
58 h261_mba_code
, 1, 1, 1);
59 init_vlc(&h261_mtype_vlc
, H261_MTYPE_VLC_BITS
, 10,
60 h261_mtype_bits
, 1, 1,
61 h261_mtype_code
, 1, 1, 1);
62 init_vlc(&h261_mv_vlc
, H261_MV_VLC_BITS
, 17,
63 &h261_mv_tab
[0][1], 2, 1,
64 &h261_mv_tab
[0][0], 2, 1, 1);
65 init_vlc(&h261_cbp_vlc
, H261_CBP_VLC_BITS
, 63,
66 &h261_cbp_tab
[0][1], 2, 1,
67 &h261_cbp_tab
[0][0], 2, 1, 1);
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
;
78 MPV_decode_defaults(s
);
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
;
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;
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
){
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);
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))
127 if ((h
->gob_number
!=1) && (h
->gob_number
!=3) && (h
->gob_number
!=5))
132 while (get_bits1(&s
->gb
) != 0) {
133 skip_bits(&s
->gb
, 8);
137 av_log(s
->avctx
, AV_LOG_ERROR
, "qscale has forbidden 0 value\n");
138 if (s
->avctx
->error_recognition
>= FF_ER_COMPLIANT
)
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.
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
;
159 if ( h
->gob_start_code_skipped
){
160 ret
= h261_decode_gob_header(h
);
165 if(show_bits(&s
->gb
, 15)==0){
166 ret
= h261_decode_gob_header(h
);
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
);
185 skip_bits(&s
->gb
, 8);
193 * decodes skipped macroblocks
196 static int h261_decode_mb_skipped(H261Context
*h
, int mba1
, int mba2
)
198 MpegEncContext
* const s
= &h
->s
;
203 for(i
=mba1
; i
<mba2
; i
++){
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
);
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
;
221 h
->mtype
&= ~MB_TYPE_H261_FIL
;
223 MPV_decode_mb(s
, s
->block
);
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 */
236 mv_diff
= mvmap
[mv_diff
];
238 if(mv_diff
&& !get_bits1(gb
))
243 else if(v
>= 16) v
-= 32;
248 static int h261_decode_mb(H261Context
*h
){
249 MpegEncContext
* const s
= &h
->s
;
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;
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
)
270 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal mba at %d %d\n", s
->mb_x
, s
->mb_y
);
275 h
->current_mba
+= h
->mba_diff
;
277 if ( h
->current_mba
> MBA_STUFFING
)
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
);
287 h
->mtype
= get_vlc2(&s
->gb
, h261_mtype_vlc
.table
, H261_MTYPE_VLC_BITS
, 2);
288 h
->mtype
= h261_mtype_map
[h
->mtype
];
291 if ( IS_QUANT ( h
->mtype
) ){
292 ff_set_qscale(s
, get_bits(&s
->gb
, 5));
295 s
->mb_intra
= IS_INTRA4x4(h
->mtype
);
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 ) ||
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
);
320 if ( HAS_CBP( h
->mtype
) ){
321 cbp
= get_vlc2(&s
->gb
, h261_cbp_vlc
.table
, H261_CBP_VLC_BITS
, 2) + 1;
325 s
->current_picture
.mb_type
[xy
]= MB_TYPE_INTRA
;
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;
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){
347 for (i
= 0; i
< 6; i
++)
348 s
->block_last_index
[i
]= -1;
351 MPV_decode_mb(s
, s
->block
);
357 * decodes a macroblock
358 * @return <0 if an error occurred
360 static int h261_decode_block(H261Context
* h
, DCTELEM
* block
,
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
;
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
);
382 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
389 // EOB Not possible for first level when cbp is available (that's why the table is different)
392 int check
= show_bits(&s
->gb
, 2);
395 skip_bits(&s
->gb
, 2);
396 block
[0] = ( check
& 0x1 ) ? -1 : 1;
403 s
->block_last_index
[n
] = i
- 1;
407 code
= get_vlc2(&s
->gb
, rl
->vlc
.table
, TCOEFF_VLC_BITS
, 2);
409 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal ac vlc code at %dx%d\n", s
->mb_x
, s
->mb_y
);
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);
420 run
= rl
->table_run
[code
];
421 level
= rl
->table_level
[code
];
422 if (get_bits1(&s
->gb
))
427 av_log(s
->avctx
, AV_LOG_ERROR
, "run overflow at %dx%d\n", s
->mb_x
, s
->mb_y
);
434 s
->block_last_index
[n
] = i
-1;
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
;
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)
454 if (startcode
!= 0x10){
455 av_log(s
->avctx
, AV_LOG_ERROR
, "Bad picture start code\n");
459 /* temporal reference */
460 i
= get_bits(&s
->gb
, 5); /* picture timestamp */
461 if(i
< (s
->picture_number
&31))
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
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 */
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
;
507 static int h261_decode_gob(H261Context
*h
){
508 MpegEncContext
* const s
= &h
->s
;
510 ff_set_qscale(s
, s
->qscale
);
513 while(h
->current_mba
<= MBA_STUFFING
)
517 ret
= h261_decode_mb(h
);
520 h261_decode_mb_skipped(h
, h
->current_mba
, 33);
523 av_log(s
->avctx
, AV_LOG_ERROR
, "Error at MB: %d\n", s
->mb_x
+ s
->mb_y
*s
->mb_stride
);
527 h261_decode_mb_skipped(h
, h
->current_mba
-h
->mba_diff
, h
->current_mba
-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 ;)
544 static int h261_decode_frame(AVCodecContext
*avctx
,
545 void *data
, int *data_size
,
546 const uint8_t *buf
, int buf_size
)
548 H261Context
*h
= avctx
->priv_data
;
549 MpegEncContext
*s
= &h
->s
;
551 AVFrame
*pict
= data
;
554 av_log(avctx
, AV_LOG_DEBUG
, "*****frame %d size=%d\n", avctx
->frame_number
, buf_size
);
555 av_log(avctx
, AV_LOG_DEBUG
, "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;
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
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 */
581 av_log(s
->avctx
, AV_LOG_ERROR
, "header damaged\n");
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;
589 s
->parse_context
= pc
;
591 if (!s
->context_initialized
) {
592 avcodec_set_dimensions(avctx
, s
->width
, s
->height
);
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)
611 ff_er_frame_start(s
);
613 /* decode each macroblock */
617 while(h
->gob_number
< (s
->mb_height
==18 ? 12 : 5)){
618 if(ff_h261_resync(h
)<0)
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
;
643 AVCodec h261_decoder
= {
653 .long_name
= NULL_IF_CONFIG_SMALL("H.261"),