3 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4 * Copyright (c) 2004 Maarten Daniels
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "mpegvideo.h"
33 #define H261_MBA_VLC_BITS 9
34 #define H261_MTYPE_VLC_BITS 6
35 #define H261_MV_VLC_BITS 7
36 #define H261_CBP_VLC_BITS 9
37 #define TCOEFF_VLC_BITS 9
39 #define MBA_STUFFING 33
40 #define MBA_STARTCODE 34
41 #define IS_FIL(a) ((a)&MB_TYPE_H261_FIL)
46 typedef struct H261Context
{
55 int bits_left
; //8 - nr of bits left of the following frame in the last byte in this frame
56 int last_bits
; //bits left of the following frame in the last byte in this frame
57 int gob_start_code_skipped
; // 1 if gob start code is already read before gob header is read
60 void ff_h261_loop_filter(H261Context
* h
){
61 MpegEncContext
* const s
= &h
->s
;
62 const int linesize
= s
->linesize
;
63 const int uvlinesize
= s
->uvlinesize
;
64 uint8_t *dest_y
= s
->dest
[0];
65 uint8_t *dest_cb
= s
->dest
[1];
66 uint8_t *dest_cr
= s
->dest
[2];
68 s
->dsp
.h261_loop_filter(dest_y
, linesize
);
69 s
->dsp
.h261_loop_filter(dest_y
+ 8, linesize
);
70 s
->dsp
.h261_loop_filter(dest_y
+ 8 * linesize
, linesize
);
71 s
->dsp
.h261_loop_filter(dest_y
+ 8 * linesize
+ 8, linesize
);
72 s
->dsp
.h261_loop_filter(dest_cb
, uvlinesize
);
73 s
->dsp
.h261_loop_filter(dest_cr
, uvlinesize
);
76 static int h261_decode_block(H261Context
*h
, DCTELEM
*block
,
78 static int h261_decode_mb(H261Context
*h
);
79 void ff_set_qscale(MpegEncContext
* s
, int qscale
);
81 /***********************************************/
84 static VLC h261_mba_vlc
;
85 static VLC h261_mtype_vlc
;
86 static VLC h261_mv_vlc
;
87 static VLC h261_cbp_vlc
;
89 void init_vlc_rl(RLTable
*rl
);
91 static void h261_decode_init_vlc(H261Context
*h
){
96 init_vlc(&h261_mba_vlc
, H261_MBA_VLC_BITS
, 35,
99 init_vlc(&h261_mtype_vlc
, H261_MTYPE_VLC_BITS
, 10,
100 h261_mtype_bits
, 1, 1,
101 h261_mtype_code
, 1, 1);
102 init_vlc(&h261_mv_vlc
, H261_MV_VLC_BITS
, 17,
103 &h261_mv_tab
[0][1], 2, 1,
104 &h261_mv_tab
[0][0], 2, 1);
105 init_vlc(&h261_cbp_vlc
, H261_CBP_VLC_BITS
, 63,
106 &h261_cbp_tab
[0][1], 2, 1,
107 &h261_cbp_tab
[0][0], 2, 1);
108 init_rl(&h261_rl_tcoeff
);
109 init_vlc_rl(&h261_rl_tcoeff
);
113 static int h261_decode_init(AVCodecContext
*avctx
){
114 H261Context
*h
= avctx
->priv_data
;
115 MpegEncContext
* const s
= &h
->s
;
118 MPV_decode_defaults(s
);
121 s
->width
= s
->avctx
->coded_width
;
122 s
->height
= s
->avctx
->coded_height
;
123 s
->codec_id
= s
->avctx
->codec
->id
;
125 s
->out_format
= FMT_H261
;
127 avctx
->pix_fmt
= PIX_FMT_YUV420P
;
129 s
->codec_id
= avctx
->codec
->id
;
131 h261_decode_init_vlc(h
);
135 h
->gob_start_code_skipped
= 0;
141 * decodes the group of blocks header or slice header.
142 * @return <0 if an error occured
144 static int h261_decode_gob_header(H261Context
*h
){
146 MpegEncContext
* const s
= &h
->s
;
148 if ( !h
->gob_start_code_skipped
){
149 /* Check for GOB Start Code */
150 val
= show_bits(&s
->gb
, 15);
155 skip_bits(&s
->gb
, 16);
158 h
->gob_start_code_skipped
= 0;
160 h
->gob_number
= get_bits(&s
->gb
, 4); /* GN */
161 s
->qscale
= get_bits(&s
->gb
, 5); /* GQUANT */
163 /* Check if gob_number is valid */
164 if (s
->mb_height
==18){ //cif
165 if ((h
->gob_number
<=0) || (h
->gob_number
>12))
169 if ((h
->gob_number
!=1) && (h
->gob_number
!=3) && (h
->gob_number
!=5))
174 while (get_bits1(&s
->gb
) != 0) {
175 skip_bits(&s
->gb
, 8);
181 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
182 // subsequent macroblocks, MBA is the difference between the absolute addresses of
183 // the macroblock and the last transmitted macroblock.
191 * decodes the group of blocks / video packet header.
192 * @return <0 if no resync found
194 static int ff_h261_resync(H261Context
*h
){
195 MpegEncContext
* const s
= &h
->s
;
198 if ( h
->gob_start_code_skipped
){
199 ret
= h261_decode_gob_header(h
);
204 if(show_bits(&s
->gb
, 15)==0){
205 ret
= h261_decode_gob_header(h
);
209 //ok, its not where its supposed to be ...
210 s
->gb
= s
->last_resync_gb
;
211 align_get_bits(&s
->gb
);
212 left
= s
->gb
.size_in_bits
- get_bits_count(&s
->gb
);
214 for(;left
>15+1+4+5; left
-=8){
215 if(show_bits(&s
->gb
, 15)==0){
216 GetBitContext bak
= s
->gb
;
218 ret
= h261_decode_gob_header(h
);
224 skip_bits(&s
->gb
, 8);
232 * decodes skipped macroblocks
235 static int h261_decode_mb_skipped(H261Context
*h
, int mba1
, int mba2
)
237 MpegEncContext
* const s
= &h
->s
;
242 for(i
=mba1
; i
<mba2
; i
++){
245 s
->mb_x
= ((h
->gob_number
-1) % 2) * 11 + i
% 11;
246 s
->mb_y
= ((h
->gob_number
-1) / 2) * 3 + i
/ 11;
247 xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
248 ff_init_block_index(s
);
249 ff_update_block_index(s
);
250 s
->dsp
.clear_blocks(s
->block
[0]);
253 s
->block_last_index
[j
] = -1;
255 s
->mv_dir
= MV_DIR_FORWARD
;
256 s
->mv_type
= MV_TYPE_16X16
;
257 s
->current_picture
.mb_type
[xy
]= MB_TYPE_SKIP
| MB_TYPE_16x16
| MB_TYPE_L0
;
262 MPV_decode_mb(s
, s
->block
);
268 static int decode_mv_component(GetBitContext
*gb
, int v
){
269 int mv_diff
= get_vlc2(gb
, h261_mv_vlc
.table
, H261_MV_VLC_BITS
, 2);
271 /* check if mv_diff is valid */
275 mv_diff
= mvmap
[mv_diff
];
277 if(mv_diff
&& !get_bits1(gb
))
282 else if(v
>= 16) v
-= 32;
287 static int h261_decode_mb(H261Context
*h
){
288 MpegEncContext
* const s
= &h
->s
;
289 int i
, cbp
, xy
, old_mtype
;
294 h
->mba_diff
= get_vlc2(&s
->gb
, h261_mba_vlc
.table
, H261_MBA_VLC_BITS
, 2);
296 /* Check for slice end */
297 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
298 if (h
->mba_diff
== MBA_STARTCODE
){ // start code
299 h
->gob_start_code_skipped
= 1;
303 while( h
->mba_diff
== MBA_STUFFING
); // stuffing
305 if ( h
->mba_diff
< 0 ){
306 if ( get_bits_count(&s
->gb
) + 7 >= s
->gb
.size_in_bits
)
309 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal mba at %d %d\n", s
->mb_x
, s
->mb_y
);
314 h
->current_mba
+= h
->mba_diff
;
316 if ( h
->current_mba
> MBA_STUFFING
)
319 s
->mb_x
= ((h
->gob_number
-1) % 2) * 11 + ((h
->current_mba
-1) % 11);
320 s
->mb_y
= ((h
->gob_number
-1) / 2) * 3 + ((h
->current_mba
-1) / 11);
321 xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
322 ff_init_block_index(s
);
323 ff_update_block_index(s
);
324 s
->dsp
.clear_blocks(s
->block
[0]);
327 old_mtype
= h
->mtype
;
328 h
->mtype
= get_vlc2(&s
->gb
, h261_mtype_vlc
.table
, H261_MTYPE_VLC_BITS
, 2);
329 h
->mtype
= h261_mtype_map
[h
->mtype
];
332 if ( IS_QUANT ( h
->mtype
) ){
333 ff_set_qscale(s
, get_bits(&s
->gb
, 5));
336 s
->mb_intra
= IS_INTRA4x4(h
->mtype
);
339 if ( IS_16X16 ( h
->mtype
) ){
340 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
341 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
342 // following three situations:
343 // 1) evaluating MVD for macroblocks 1, 12 and 23;
344 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
345 // 3) MTYPE of the previous macroblock was not MC.
346 if ( ( h
->current_mba
== 1 ) || ( h
->current_mba
== 12 ) || ( h
->current_mba
== 23 ) ||
347 ( h
->mba_diff
!= 1) || ( !IS_16X16 ( old_mtype
) ))
353 h
->current_mv_x
= decode_mv_component(&s
->gb
, h
->current_mv_x
);
354 h
->current_mv_y
= decode_mv_component(&s
->gb
, h
->current_mv_y
);
358 if ( HAS_CBP( h
->mtype
) ){
359 cbp
= get_vlc2(&s
->gb
, h261_cbp_vlc
.table
, H261_CBP_VLC_BITS
, 2) + 1;
363 s
->current_picture
.mb_type
[xy
]= MB_TYPE_INTRA
;
368 s
->mv_dir
= MV_DIR_FORWARD
;
369 s
->mv_type
= MV_TYPE_16X16
;
370 s
->current_picture
.mb_type
[xy
]= MB_TYPE_16x16
| MB_TYPE_L0
;
371 if(IS_16X16 ( h
->mtype
)){
372 s
->mv
[0][0][0] = h
->current_mv_x
* 2;//gets divided by 2 in motion compensation
373 s
->mv
[0][0][1] = h
->current_mv_y
* 2;
376 h
->current_mv_x
= s
->mv
[0][0][0] = 0;
377 h
->current_mv_x
= s
->mv
[0][0][1] = 0;
381 /* decode each block */
382 if(s
->mb_intra
|| HAS_CBP(h
->mtype
)){
383 for (i
= 0; i
< 6; i
++) {
384 if (h261_decode_block(h
, s
->block
[i
], i
, cbp
&32) < 0){
391 MPV_decode_mb(s
, s
->block
);
393 if(IS_FIL (h
->mtype
)){
394 ff_h261_loop_filter(h
);
401 * decodes a macroblock
402 * @return <0 if an error occured
404 static int h261_decode_block(H261Context
* h
, DCTELEM
* block
,
407 MpegEncContext
* const s
= &h
->s
;
408 int code
, level
, i
, j
, run
;
409 RLTable
*rl
= &h261_rl_tcoeff
;
410 const uint8_t *scan_table
;
412 // For the variable length encoding there are two code tables, one being used for
413 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
414 // for all other LEVELs except the first one in INTRA blocks which is fixed length
415 // coded with 8 bits.
416 // NOTE: the two code tables only differ in one VLC so we handle that manually.
417 scan_table
= s
->intra_scantable
.permutated
;
420 level
= get_bits(&s
->gb
, 8);
421 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
422 if((level
&0x7F) == 0){
423 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal dc %d at %d %d\n", level
, s
->mb_x
, s
->mb_y
);
426 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
433 // EOB Not possible for first level when cbp is available (that's why the table is different)
436 int check
= show_bits(&s
->gb
, 2);
439 skip_bits(&s
->gb
, 2);
440 block
[0] = ( check
& 0x1 ) ?
-1 : 1;
447 s
->block_last_index
[n
] = i
- 1;
451 code
= get_vlc2(&s
->gb
, rl
->vlc
.table
, TCOEFF_VLC_BITS
, 2);
453 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal ac vlc code at %dx%d\n", s
->mb_x
, s
->mb_y
);
458 // 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.
459 run
= get_bits(&s
->gb
, 6);
460 level
= (int8_t)get_bits(&s
->gb
, 8);
464 run
= rl
->table_run
[code
];
465 level
= rl
->table_level
[code
];
466 if (get_bits1(&s
->gb
))
471 av_log(s
->avctx
, AV_LOG_ERROR
, "run overflow at %dx%d\n", s
->mb_x
, s
->mb_y
);
478 s
->block_last_index
[n
] = i
-1;
483 * decodes the H261 picture header.
484 * @return <0 if no startcode found
486 int h261_decode_picture_header(H261Context
*h
){
487 MpegEncContext
* const s
= &h
->s
;
490 align_get_bits(&s
->gb
);
492 startcode
= (h
->last_bits
<< (12 - (8-h
->bits_left
))) | get_bits(&s
->gb
, 20-8 - (8- h
->bits_left
));
494 for(i
= s
->gb
.size_in_bits
- get_bits_count(&s
->gb
); i
>24; i
-=1){
495 startcode
= ((startcode
<< 1) | get_bits(&s
->gb
, 1)) & 0x000FFFFF;
497 if(startcode
== 0x10)
501 if (startcode
!= 0x10){
502 av_log(s
->avctx
, AV_LOG_ERROR
, "Bad picture start code\n");
506 /* temporal reference */
507 s
->picture_number
= get_bits(&s
->gb
, 5); /* picture timestamp */
509 /* PTYPE starts here */
510 skip_bits1(&s
->gb
); /* split screen off */
511 skip_bits1(&s
->gb
); /* camera off */
512 skip_bits1(&s
->gb
); /* freeze picture release off */
514 format
= get_bits1(&s
->gb
);
516 //only 2 formats possible
517 if (format
== 0){//QCIF
529 s
->mb_num
= s
->mb_width
* s
->mb_height
;
531 skip_bits1(&s
->gb
); /* still image mode off */
532 skip_bits1(&s
->gb
); /* Reserved */
535 while (get_bits1(&s
->gb
) != 0){
536 skip_bits(&s
->gb
, 8);
539 // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
540 // not contain all I-blocks (e.g. when a packet is lost)
541 s
->pict_type
= P_TYPE
;
547 static int h261_decode_gob(H261Context
*h
){
548 MpegEncContext
* const s
= &h
->s
;
550 ff_set_qscale(s
, s
->qscale
);
553 while(h
->current_mba
<= MBA_STUFFING
)
557 ret
= h261_decode_mb(h
);
560 h261_decode_mb_skipped(h
, h
->current_mba
, 33);
563 av_log(s
->avctx
, AV_LOG_ERROR
, "Error at MB: %d\n", s
->mb_x
+ s
->mb_y
*s
->mb_stride
);
567 h261_decode_mb_skipped(h
, h
->current_mba
-h
->mba_diff
, h
->current_mba
-1);
573 static int h261_find_frame_end(ParseContext
*pc
, AVCodecContext
* avctx
, const uint8_t *buf
, int buf_size
){
574 int vop_found
, i
, j
, bits_left
, last_bits
;
577 H261Context
*h
= avctx
->priv_data
;
580 bits_left
= h
->bits_left
;
581 last_bits
= h
->last_bits
;
588 vop_found
= pc
->frame_start_found
;
590 if(bits_left
!=0 && !vop_found
)
591 state
= state
<< (8-bits_left
) | last_bits
;
594 for(i
=0; i
<buf_size
; i
++){
595 state
= (state
<<8) | buf
[i
];
597 if(( ( (state
<<j
) | (buf
[i
]>>(8-j
)) )>>(32-20) == 0x10 )&&(((state
>> (17-j
)) & 0x4000) == 0x0)){
608 for(; i
<buf_size
; i
++){
609 if(avctx
->flags
& CODEC_FLAG_TRUNCATED
)//XXX ffplay workaround, someone a better solution?
610 state
= (state
<<8) | buf
[i
];
612 if(( ( (state
<<j
) | (buf
[i
]>>(8-j
)) )>>(32-20) == 0x10 )&&(((state
>> (17-j
)) & 0x4000) == 0x0)){
613 pc
->frame_start_found
=0;
621 pc
->frame_start_found
= vop_found
;
623 return END_NOT_FOUND
;
626 static int h261_parse(AVCodecParserContext
*s
,
627 AVCodecContext
*avctx
,
628 uint8_t **poutbuf
, int *poutbuf_size
,
629 const uint8_t *buf
, int buf_size
)
631 ParseContext
*pc
= s
->priv_data
;
634 next
= h261_find_frame_end(pc
,avctx
, buf
, buf_size
);
635 if (ff_combine_frame(pc
, next
, (uint8_t **)&buf
, &buf_size
) < 0) {
640 *poutbuf
= (uint8_t *)buf
;
641 *poutbuf_size
= buf_size
;
646 * returns the number of bytes consumed for building the current frame
648 static int get_consumed_bytes(MpegEncContext
*s
, int buf_size
){
649 int pos
= (get_bits_count(&s
->gb
)+7)>>3;
651 if(s
->flags
&CODEC_FLAG_TRUNCATED
){
652 pos
-= s
->parse_context
.last_index
;
653 if(pos
<0) pos
=0;// padding is not really read so this might be -1
656 if(pos
==0) pos
=1; //avoid infinite loops (i doubt thats needed but ...)
657 if(pos
+10>buf_size
) pos
=buf_size
; // oops ;)
663 static int h261_decode_frame(AVCodecContext
*avctx
,
664 void *data
, int *data_size
,
665 uint8_t *buf
, int buf_size
)
667 H261Context
*h
= avctx
->priv_data
;
668 MpegEncContext
*s
= &h
->s
;
670 AVFrame
*pict
= data
;
673 printf("*****frame %d size=%d\n", avctx
->frame_number
, buf_size
);
674 printf("bytes=%x %x %x %x\n", buf
[0], buf
[1], buf
[2], buf
[3]);
676 s
->flags
= avctx
->flags
;
677 s
->flags2
= avctx
->flags2
;
679 /* no supplementary picture */
685 if(s
->flags
&CODEC_FLAG_TRUNCATED
){
688 next
= h261_find_frame_end(&s
->parse_context
,avctx
, buf
, buf_size
);
690 if( ff_combine_frame(&s
->parse_context
, next
, &buf
, &buf_size
) < 0 )
697 init_get_bits(&s
->gb
, buf
, buf_size
*8);
699 if(!s
->context_initialized
){
700 if (MPV_common_init(s
) < 0) //we need the idct permutaton for reading a custom matrix
704 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
705 if(s
->current_picture_ptr
==NULL
|| s
->current_picture_ptr
->data
[0]){
706 int i
= ff_find_unused_picture(s
, 0);
707 s
->current_picture_ptr
= &s
->picture
[i
];
710 ret
= h261_decode_picture_header(h
);
712 /* skip if the header was thrashed */
714 av_log(s
->avctx
, AV_LOG_ERROR
, "header damaged\n");
718 if (s
->width
!= avctx
->coded_width
|| s
->height
!= avctx
->coded_height
){
719 ParseContext pc
= s
->parse_context
; //FIXME move these demuxng hack to avformat
720 s
->parse_context
.buffer
=0;
722 s
->parse_context
= pc
;
724 if (!s
->context_initialized
) {
725 avcodec_set_dimensions(avctx
, s
->width
, s
->height
);
731 s
->current_picture
.pict_type
= s
->pict_type
;
732 s
->current_picture
.key_frame
= s
->pict_type
== I_TYPE
;
734 /* skip everything if we are in a hurry>=5 */
735 if(avctx
->hurry_up
>=5) return get_consumed_bytes(s
, buf_size
);
737 if(MPV_frame_start(s
, avctx
) < 0)
740 ff_er_frame_start(s
);
742 /* decode each macroblock */
746 while(h
->gob_number
< (s
->mb_height
==18 ?
12 : 5)){
747 if(ff_h261_resync(h
)<0)
753 // h261 doesn't have byte aligned codes
754 // store the bits of the next frame that are left in the last byte
755 // in the H261Context and remember the number of stored bits
758 int current_pos
= get_bits_count(&s
->gb
)>>3;
759 bitsleft
= (current_pos
<<3) - get_bits_count(&s
->gb
);
760 h
->bits_left
= - bitsleft
;
762 h
->last_bits
= get_bits(&s
->gb
, 8 - h
->bits_left
);
767 assert(s
->current_picture
.pict_type
== s
->current_picture_ptr
->pict_type
);
768 assert(s
->current_picture
.pict_type
== s
->pict_type
);
769 *pict
= *(AVFrame
*)&s
->current_picture
;
770 ff_print_debug_info(s
, pict
);
772 /* Return the Picture timestamp as the frame number */
773 /* we substract 1 because it is added on utils.c */
774 avctx
->frame_number
= s
->picture_number
- 1;
776 *data_size
= sizeof(AVFrame
);
778 return get_consumed_bytes(s
, buf_size
);
781 static int h261_decode_end(AVCodecContext
*avctx
)
783 H261Context
*h
= avctx
->priv_data
;
784 MpegEncContext
*s
= &h
->s
;
790 AVCodec h261_decoder
= {
802 AVCodecParser h261_parser
= {
804 sizeof(ParseContext
),