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
{
56 int gob_start_code_skipped
; // 1 if gob start code is already read before gob header is read
59 void ff_h261_loop_filter(MpegEncContext
*s
){
60 H261Context
* h
= (H261Context
*)s
;
61 const int linesize
= s
->linesize
;
62 const int uvlinesize
= s
->uvlinesize
;
63 uint8_t *dest_y
= s
->dest
[0];
64 uint8_t *dest_cb
= s
->dest
[1];
65 uint8_t *dest_cr
= s
->dest
[2];
67 if(!(IS_FIL (h
->mtype
)))
70 s
->dsp
.h261_loop_filter(dest_y
, linesize
);
71 s
->dsp
.h261_loop_filter(dest_y
+ 8, linesize
);
72 s
->dsp
.h261_loop_filter(dest_y
+ 8 * linesize
, linesize
);
73 s
->dsp
.h261_loop_filter(dest_y
+ 8 * linesize
+ 8, linesize
);
74 s
->dsp
.h261_loop_filter(dest_cb
, uvlinesize
);
75 s
->dsp
.h261_loop_filter(dest_cr
, uvlinesize
);
78 static int ff_h261_get_picture_format(int width
, int height
){
80 if (width
== 176 && height
== 144)
83 else if (width
== 352 && height
== 288)
90 static void h261_encode_block(H261Context
* h
, DCTELEM
* block
,
92 static int h261_decode_block(H261Context
*h
, DCTELEM
*block
,
95 void ff_h261_encode_picture_header(MpegEncContext
* s
, int picture_number
){
96 H261Context
* h
= (H261Context
*) s
;
99 align_put_bits(&s
->pb
);
101 /* Update the pointer to last GOB */
102 s
->ptr_lastgob
= pbBufPtr(&s
->pb
);
104 put_bits(&s
->pb
, 20, 0x10); /* PSC */
106 temp_ref
= s
->picture_number
* (int64_t)30000 * s
->avctx
->time_base
.num
/
107 (1001 * (int64_t)s
->avctx
->time_base
.den
); //FIXME maybe this should use a timestamp
108 put_bits(&s
->pb
, 5, temp_ref
& 0x1f); /* TemporalReference */
110 put_bits(&s
->pb
, 1, 0); /* split screen off */
111 put_bits(&s
->pb
, 1, 0); /* camera off */
112 put_bits(&s
->pb
, 1, 0); /* freeze picture release off */
114 format
= ff_h261_get_picture_format(s
->width
, s
->height
);
116 put_bits(&s
->pb
, 1, format
); /* 0 == QCIF, 1 == CIF */
118 put_bits(&s
->pb
, 1, 0); /* still image mode */
119 put_bits(&s
->pb
, 1, 0); /* reserved */
121 put_bits(&s
->pb
, 1, 0); /* no PEI */
130 * Encodes a group of blocks header.
132 static void h261_encode_gob_header(MpegEncContext
* s
, int mb_line
){
133 H261Context
* h
= (H261Context
*)s
;
134 if(ff_h261_get_picture_format(s
->width
, s
->height
) == 0){
135 h
->gob_number
+=2; // QCIF
138 h
->gob_number
++; // CIF
140 put_bits(&s
->pb
, 16, 1); /* GBSC */
141 put_bits(&s
->pb
, 4, h
->gob_number
); /* GN */
142 put_bits(&s
->pb
, 5, s
->qscale
); /* GQUANT */
143 put_bits(&s
->pb
, 1, 0); /* no GEI */
150 void ff_h261_reorder_mb_index(MpegEncContext
* s
){
151 int index
= s
->mb_x
+ s
->mb_y
*s
->mb_width
;
154 h261_encode_gob_header(s
,0);
156 /* for CIF the GOB's are fragmented in the middle of a scanline
157 that's why we need to adjust the x and y index of the macroblocks */
158 if(ff_h261_get_picture_format(s
->width
,s
->height
) == 1){ // CIF
159 s
->mb_x
= index
% 11 ; index
/= 11;
160 s
->mb_y
= index
% 3 ; index
/= 3;
161 s
->mb_x
+= 11*(index
% 2); index
/= 2;
164 ff_init_block_index(s
);
165 ff_update_block_index(s
);
169 static void h261_encode_motion(H261Context
* h
, int val
){
170 MpegEncContext
* const s
= &h
->s
;
174 put_bits(&s
->pb
,h261_mv_tab
[code
][1],h261_mv_tab
[code
][0]);
182 code
= sign
? -val
: val
;
183 put_bits(&s
->pb
,h261_mv_tab
[code
][1],h261_mv_tab
[code
][0]);
184 put_bits(&s
->pb
,1,sign
);
188 static inline int get_cbp(MpegEncContext
* s
,
189 DCTELEM block
[6][64])
193 for (i
= 0; i
< 6; i
++) {
194 if (s
->block_last_index
[i
] >= 0)
199 void ff_h261_encode_mb(MpegEncContext
* s
,
200 DCTELEM block
[6][64],
201 int motion_x
, int motion_y
)
203 H261Context
* h
= (H261Context
*)s
;
204 int mvd
, mv_diff_x
, mv_diff_y
, i
, cbp
;
205 cbp
= 63; // avoid warning
213 cbp
= get_cbp(s
, block
);
215 /* mvd indicates if this block is motion compensated */
216 mvd
= motion_x
| motion_y
;
218 if((cbp
| mvd
| s
->dquant
) == 0) {
219 /* skip macroblock */
227 /* MB is not skipped, encode MBA */
228 put_bits(&s
->pb
, h261_mba_bits
[(h
->current_mba
-h
->previous_mba
)-1], h261_mba_code
[(h
->current_mba
-h
->previous_mba
)-1]);
230 /* calculate MTYPE */
234 if(mvd
|| s
->loop_filter
)
240 assert(h
->mtype
> 1);
246 put_bits(&s
->pb
, h261_mtype_bits
[h
->mtype
], h261_mtype_code
[h
->mtype
]);
248 h
->mtype
= h261_mtype_map
[h
->mtype
];
250 if(IS_QUANT(h
->mtype
)){
251 ff_set_qscale(s
,s
->qscale
+s
->dquant
);
252 put_bits(&s
->pb
, 5, s
->qscale
);
255 if(IS_16X16(h
->mtype
)){
256 mv_diff_x
= (motion_x
>> 1) - h
->current_mv_x
;
257 mv_diff_y
= (motion_y
>> 1) - h
->current_mv_y
;
258 h
->current_mv_x
= (motion_x
>> 1);
259 h
->current_mv_y
= (motion_y
>> 1);
260 h261_encode_motion(h
,mv_diff_x
);
261 h261_encode_motion(h
,mv_diff_y
);
264 h
->previous_mba
= h
->current_mba
;
266 if(HAS_CBP(h
->mtype
)){
267 put_bits(&s
->pb
,h261_cbp_tab
[cbp
-1][1],h261_cbp_tab
[cbp
-1][0]);
270 /* encode each block */
271 h261_encode_block(h
, block
[i
], i
);
274 if ( ( h
->current_mba
== 11 ) || ( h
->current_mba
== 22 ) || ( h
->current_mba
== 33 ) || ( !IS_16X16 ( h
->mtype
) )){
280 void ff_h261_encode_init(MpegEncContext
*s
){
285 init_rl(&h261_rl_tcoeff
, 1);
291 s
->c_dc_scale_table
= ff_mpeg1_dc_scale_table
;
296 * encodes a 8x8 block.
297 * @param block the 8x8 block
298 * @param n block index (0-3 are luma, 4-5 are chroma)
300 static void h261_encode_block(H261Context
* h
, DCTELEM
* block
, int n
){
301 MpegEncContext
* const s
= &h
->s
;
302 int level
, run
, last
, i
, j
, last_index
, last_non_zero
, sign
, slevel
, code
;
305 rl
= &h261_rl_tcoeff
;
309 /* 255 cannot be represented, so we clamp */
314 /* 0 cannot be represented also */
315 else if (level
< 1) {
320 put_bits(&s
->pb
, 8, 0xff);
322 put_bits(&s
->pb
, 8, level
);
324 } else if((block
[0]==1 || block
[0] == -1) && (s
->block_last_index
[n
] > -1)){
326 put_bits(&s
->pb
,2,block
[0]>0 ? 2 : 3 );
333 last_index
= s
->block_last_index
[n
];
334 last_non_zero
= i
- 1;
335 for (; i
<= last_index
; i
++) {
336 j
= s
->intra_scantable
.permutated
[i
];
339 run
= i
- last_non_zero
- 1;
340 last
= (i
== last_index
);
347 code
= get_rl_index(rl
, 0 /*no last in H.261, EOB is used*/, run
, level
);
348 if(run
==0 && level
< 16)
350 put_bits(&s
->pb
, rl
->table_vlc
[code
][1], rl
->table_vlc
[code
][0]);
352 put_bits(&s
->pb
, 6, run
);
354 assert(level
<= 127);
355 put_bits(&s
->pb
, 8, slevel
& 0xff);
357 put_bits(&s
->pb
, 1, sign
);
363 put_bits(&s
->pb
, rl
->table_vlc
[0][1], rl
->table_vlc
[0][0]);// END OF BLOCK
367 /***********************************************/
370 static VLC h261_mba_vlc
;
371 static VLC h261_mtype_vlc
;
372 static VLC h261_mv_vlc
;
373 static VLC h261_cbp_vlc
;
375 void init_vlc_rl(RLTable
*rl
, int use_static
);
377 static void h261_decode_init_vlc(H261Context
*h
){
382 init_vlc(&h261_mba_vlc
, H261_MBA_VLC_BITS
, 35,
384 h261_mba_code
, 1, 1, 1);
385 init_vlc(&h261_mtype_vlc
, H261_MTYPE_VLC_BITS
, 10,
386 h261_mtype_bits
, 1, 1,
387 h261_mtype_code
, 1, 1, 1);
388 init_vlc(&h261_mv_vlc
, H261_MV_VLC_BITS
, 17,
389 &h261_mv_tab
[0][1], 2, 1,
390 &h261_mv_tab
[0][0], 2, 1, 1);
391 init_vlc(&h261_cbp_vlc
, H261_CBP_VLC_BITS
, 63,
392 &h261_cbp_tab
[0][1], 2, 1,
393 &h261_cbp_tab
[0][0], 2, 1, 1);
394 init_rl(&h261_rl_tcoeff
, 1);
395 init_vlc_rl(&h261_rl_tcoeff
, 1);
399 static int h261_decode_init(AVCodecContext
*avctx
){
400 H261Context
*h
= avctx
->priv_data
;
401 MpegEncContext
* const s
= &h
->s
;
404 MPV_decode_defaults(s
);
407 s
->width
= s
->avctx
->coded_width
;
408 s
->height
= s
->avctx
->coded_height
;
409 s
->codec_id
= s
->avctx
->codec
->id
;
411 s
->out_format
= FMT_H261
;
413 avctx
->pix_fmt
= PIX_FMT_YUV420P
;
415 s
->codec_id
= avctx
->codec
->id
;
417 h261_decode_init_vlc(h
);
419 h
->gob_start_code_skipped
= 0;
425 * decodes the group of blocks header or slice header.
426 * @return <0 if an error occured
428 static int h261_decode_gob_header(H261Context
*h
){
430 MpegEncContext
* const s
= &h
->s
;
432 if ( !h
->gob_start_code_skipped
){
433 /* Check for GOB Start Code */
434 val
= show_bits(&s
->gb
, 15);
439 skip_bits(&s
->gb
, 16);
442 h
->gob_start_code_skipped
= 0;
444 h
->gob_number
= get_bits(&s
->gb
, 4); /* GN */
445 s
->qscale
= get_bits(&s
->gb
, 5); /* GQUANT */
447 /* Check if gob_number is valid */
448 if (s
->mb_height
==18){ //cif
449 if ((h
->gob_number
<=0) || (h
->gob_number
>12))
453 if ((h
->gob_number
!=1) && (h
->gob_number
!=3) && (h
->gob_number
!=5))
458 while (get_bits1(&s
->gb
) != 0) {
459 skip_bits(&s
->gb
, 8);
465 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
466 // subsequent macroblocks, MBA is the difference between the absolute addresses of
467 // the macroblock and the last transmitted macroblock.
475 * decodes the group of blocks / video packet header.
476 * @return <0 if no resync found
478 static int ff_h261_resync(H261Context
*h
){
479 MpegEncContext
* const s
= &h
->s
;
482 if ( h
->gob_start_code_skipped
){
483 ret
= h261_decode_gob_header(h
);
488 if(show_bits(&s
->gb
, 15)==0){
489 ret
= h261_decode_gob_header(h
);
493 //ok, its not where its supposed to be ...
494 s
->gb
= s
->last_resync_gb
;
495 align_get_bits(&s
->gb
);
496 left
= s
->gb
.size_in_bits
- get_bits_count(&s
->gb
);
498 for(;left
>15+1+4+5; left
-=8){
499 if(show_bits(&s
->gb
, 15)==0){
500 GetBitContext bak
= s
->gb
;
502 ret
= h261_decode_gob_header(h
);
508 skip_bits(&s
->gb
, 8);
516 * decodes skipped macroblocks
519 static int h261_decode_mb_skipped(H261Context
*h
, int mba1
, int mba2
)
521 MpegEncContext
* const s
= &h
->s
;
526 for(i
=mba1
; i
<mba2
; i
++){
529 s
->mb_x
= ((h
->gob_number
-1) % 2) * 11 + i
% 11;
530 s
->mb_y
= ((h
->gob_number
-1) / 2) * 3 + i
/ 11;
531 xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
532 ff_init_block_index(s
);
533 ff_update_block_index(s
);
536 s
->block_last_index
[j
] = -1;
538 s
->mv_dir
= MV_DIR_FORWARD
;
539 s
->mv_type
= MV_TYPE_16X16
;
540 s
->current_picture
.mb_type
[xy
]= MB_TYPE_SKIP
| MB_TYPE_16x16
| MB_TYPE_L0
;
544 h
->mtype
&= ~MB_TYPE_H261_FIL
;
546 MPV_decode_mb(s
, s
->block
);
552 static int decode_mv_component(GetBitContext
*gb
, int v
){
553 int mv_diff
= get_vlc2(gb
, h261_mv_vlc
.table
, H261_MV_VLC_BITS
, 2);
555 /* check if mv_diff is valid */
559 mv_diff
= mvmap
[mv_diff
];
561 if(mv_diff
&& !get_bits1(gb
))
566 else if(v
>= 16) v
-= 32;
571 static int h261_decode_mb(H261Context
*h
){
572 MpegEncContext
* const s
= &h
->s
;
578 h
->mba_diff
= get_vlc2(&s
->gb
, h261_mba_vlc
.table
, H261_MBA_VLC_BITS
, 2);
580 /* Check for slice end */
581 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
582 if (h
->mba_diff
== MBA_STARTCODE
){ // start code
583 h
->gob_start_code_skipped
= 1;
587 while( h
->mba_diff
== MBA_STUFFING
); // stuffing
589 if ( h
->mba_diff
< 0 ){
590 if ( get_bits_count(&s
->gb
) + 7 >= s
->gb
.size_in_bits
)
593 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal mba at %d %d\n", s
->mb_x
, s
->mb_y
);
598 h
->current_mba
+= h
->mba_diff
;
600 if ( h
->current_mba
> MBA_STUFFING
)
603 s
->mb_x
= ((h
->gob_number
-1) % 2) * 11 + ((h
->current_mba
-1) % 11);
604 s
->mb_y
= ((h
->gob_number
-1) / 2) * 3 + ((h
->current_mba
-1) / 11);
605 xy
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
606 ff_init_block_index(s
);
607 ff_update_block_index(s
);
610 h
->mtype
= get_vlc2(&s
->gb
, h261_mtype_vlc
.table
, H261_MTYPE_VLC_BITS
, 2);
611 h
->mtype
= h261_mtype_map
[h
->mtype
];
614 if ( IS_QUANT ( h
->mtype
) ){
615 ff_set_qscale(s
, get_bits(&s
->gb
, 5));
618 s
->mb_intra
= IS_INTRA4x4(h
->mtype
);
621 if ( IS_16X16 ( h
->mtype
) ){
622 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
623 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
624 // following three situations:
625 // 1) evaluating MVD for macroblocks 1, 12 and 23;
626 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
627 // 3) MTYPE of the previous macroblock was not MC.
628 if ( ( h
->current_mba
== 1 ) || ( h
->current_mba
== 12 ) || ( h
->current_mba
== 23 ) ||
635 h
->current_mv_x
= decode_mv_component(&s
->gb
, h
->current_mv_x
);
636 h
->current_mv_y
= decode_mv_component(&s
->gb
, h
->current_mv_y
);
643 if ( HAS_CBP( h
->mtype
) ){
644 cbp
= get_vlc2(&s
->gb
, h261_cbp_vlc
.table
, H261_CBP_VLC_BITS
, 2) + 1;
648 s
->current_picture
.mb_type
[xy
]= MB_TYPE_INTRA
;
653 s
->mv_dir
= MV_DIR_FORWARD
;
654 s
->mv_type
= MV_TYPE_16X16
;
655 s
->current_picture
.mb_type
[xy
]= MB_TYPE_16x16
| MB_TYPE_L0
;
656 s
->mv
[0][0][0] = h
->current_mv_x
* 2;//gets divided by 2 in motion compensation
657 s
->mv
[0][0][1] = h
->current_mv_y
* 2;
660 /* decode each block */
661 if(s
->mb_intra
|| HAS_CBP(h
->mtype
)){
662 s
->dsp
.clear_blocks(s
->block
[0]);
663 for (i
= 0; i
< 6; i
++) {
664 if (h261_decode_block(h
, s
->block
[i
], i
, cbp
&32) < 0){
670 for (i
= 0; i
< 6; i
++)
671 s
->block_last_index
[i
]= -1;
674 MPV_decode_mb(s
, s
->block
);
680 * decodes a macroblock
681 * @return <0 if an error occured
683 static int h261_decode_block(H261Context
* h
, DCTELEM
* block
,
686 MpegEncContext
* const s
= &h
->s
;
687 int code
, level
, i
, j
, run
;
688 RLTable
*rl
= &h261_rl_tcoeff
;
689 const uint8_t *scan_table
;
691 // For the variable length encoding there are two code tables, one being used for
692 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
693 // for all other LEVELs except the first one in INTRA blocks which is fixed length
694 // coded with 8 bits.
695 // NOTE: the two code tables only differ in one VLC so we handle that manually.
696 scan_table
= s
->intra_scantable
.permutated
;
699 level
= get_bits(&s
->gb
, 8);
700 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
701 if((level
&0x7F) == 0){
702 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal dc %d at %d %d\n", level
, s
->mb_x
, s
->mb_y
);
705 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
712 // EOB Not possible for first level when cbp is available (that's why the table is different)
715 int check
= show_bits(&s
->gb
, 2);
718 skip_bits(&s
->gb
, 2);
719 block
[0] = ( check
& 0x1 ) ? -1 : 1;
726 s
->block_last_index
[n
] = i
- 1;
730 code
= get_vlc2(&s
->gb
, rl
->vlc
.table
, TCOEFF_VLC_BITS
, 2);
732 av_log(s
->avctx
, AV_LOG_ERROR
, "illegal ac vlc code at %dx%d\n", s
->mb_x
, s
->mb_y
);
737 // 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.
738 run
= get_bits(&s
->gb
, 6);
739 level
= get_sbits(&s
->gb
, 8);
743 run
= rl
->table_run
[code
];
744 level
= rl
->table_level
[code
];
745 if (get_bits1(&s
->gb
))
750 av_log(s
->avctx
, AV_LOG_ERROR
, "run overflow at %dx%d\n", s
->mb_x
, s
->mb_y
);
757 s
->block_last_index
[n
] = i
-1;
762 * decodes the H261 picture header.
763 * @return <0 if no startcode found
765 int h261_decode_picture_header(H261Context
*h
){
766 MpegEncContext
* const s
= &h
->s
;
768 uint32_t startcode
= 0;
770 for(i
= s
->gb
.size_in_bits
- get_bits_count(&s
->gb
); i
>24; i
-=1){
771 startcode
= ((startcode
<< 1) | get_bits(&s
->gb
, 1)) & 0x000FFFFF;
773 if(startcode
== 0x10)
777 if (startcode
!= 0x10){
778 av_log(s
->avctx
, AV_LOG_ERROR
, "Bad picture start code\n");
782 /* temporal reference */
783 s
->picture_number
= get_bits(&s
->gb
, 5); /* picture timestamp */
785 /* PTYPE starts here */
786 skip_bits1(&s
->gb
); /* split screen off */
787 skip_bits1(&s
->gb
); /* camera off */
788 skip_bits1(&s
->gb
); /* freeze picture release off */
790 format
= get_bits1(&s
->gb
);
792 //only 2 formats possible
793 if (format
== 0){//QCIF
805 s
->mb_num
= s
->mb_width
* s
->mb_height
;
807 skip_bits1(&s
->gb
); /* still image mode off */
808 skip_bits1(&s
->gb
); /* Reserved */
811 while (get_bits1(&s
->gb
) != 0){
812 skip_bits(&s
->gb
, 8);
815 // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
816 // not contain all I-blocks (e.g. when a packet is lost)
817 s
->pict_type
= P_TYPE
;
823 static int h261_decode_gob(H261Context
*h
){
824 MpegEncContext
* const s
= &h
->s
;
826 ff_set_qscale(s
, s
->qscale
);
829 while(h
->current_mba
<= MBA_STUFFING
)
833 ret
= h261_decode_mb(h
);
836 h261_decode_mb_skipped(h
, h
->current_mba
, 33);
839 av_log(s
->avctx
, AV_LOG_ERROR
, "Error at MB: %d\n", s
->mb_x
+ s
->mb_y
*s
->mb_stride
);
843 h261_decode_mb_skipped(h
, h
->current_mba
-h
->mba_diff
, h
->current_mba
-1);
849 static int h261_find_frame_end(ParseContext
*pc
, AVCodecContext
* avctx
, const uint8_t *buf
, int buf_size
){
853 vop_found
= pc
->frame_start_found
;
856 for(i
=0; i
<buf_size
&& !vop_found
; i
++){
857 state
= (state
<<8) | buf
[i
];
859 if(((state
>>j
)&0xFFFFF) == 0x00010){
867 for(; i
<buf_size
; i
++){
868 state
= (state
<<8) | buf
[i
];
870 if(((state
>>j
)&0xFFFFF) == 0x00010){
871 pc
->frame_start_found
=0;
872 pc
->state
= state
>>(2*8);
879 pc
->frame_start_found
= vop_found
;
881 return END_NOT_FOUND
;
884 static int h261_parse(AVCodecParserContext
*s
,
885 AVCodecContext
*avctx
,
886 uint8_t **poutbuf
, int *poutbuf_size
,
887 const uint8_t *buf
, int buf_size
)
889 ParseContext
*pc
= s
->priv_data
;
892 next
= h261_find_frame_end(pc
,avctx
, buf
, buf_size
);
893 if (ff_combine_frame(pc
, next
, (uint8_t **)&buf
, &buf_size
) < 0) {
898 *poutbuf
= (uint8_t *)buf
;
899 *poutbuf_size
= buf_size
;
904 * returns the number of bytes consumed for building the current frame
906 static int get_consumed_bytes(MpegEncContext
*s
, int buf_size
){
907 int pos
= get_bits_count(&s
->gb
)>>3;
908 if(pos
==0) pos
=1; //avoid infinite loops (i doubt thats needed but ...)
909 if(pos
+10>buf_size
) pos
=buf_size
; // oops ;)
914 static int h261_decode_frame(AVCodecContext
*avctx
,
915 void *data
, int *data_size
,
916 uint8_t *buf
, int buf_size
)
918 H261Context
*h
= avctx
->priv_data
;
919 MpegEncContext
*s
= &h
->s
;
921 AVFrame
*pict
= data
;
924 printf("*****frame %d size=%d\n", avctx
->frame_number
, buf_size
);
925 printf("bytes=%x %x %x %x\n", buf
[0], buf
[1], buf
[2], buf
[3]);
927 s
->flags
= avctx
->flags
;
928 s
->flags2
= avctx
->flags2
;
930 h
->gob_start_code_skipped
=0;
934 init_get_bits(&s
->gb
, buf
, buf_size
*8);
936 if(!s
->context_initialized
){
937 if (MPV_common_init(s
) < 0) //we need the idct permutaton for reading a custom matrix
941 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
942 if(s
->current_picture_ptr
==NULL
|| s
->current_picture_ptr
->data
[0]){
943 int i
= ff_find_unused_picture(s
, 0);
944 s
->current_picture_ptr
= &s
->picture
[i
];
947 ret
= h261_decode_picture_header(h
);
949 /* skip if the header was thrashed */
951 av_log(s
->avctx
, AV_LOG_ERROR
, "header damaged\n");
955 if (s
->width
!= avctx
->coded_width
|| s
->height
!= avctx
->coded_height
){
956 ParseContext pc
= s
->parse_context
; //FIXME move these demuxng hack to avformat
957 s
->parse_context
.buffer
=0;
959 s
->parse_context
= pc
;
961 if (!s
->context_initialized
) {
962 avcodec_set_dimensions(avctx
, s
->width
, s
->height
);
968 s
->current_picture
.pict_type
= s
->pict_type
;
969 s
->current_picture
.key_frame
= s
->pict_type
== I_TYPE
;
971 /* skip everything if we are in a hurry>=5 */
972 if(avctx
->hurry_up
>=5) return get_consumed_bytes(s
, buf_size
);
973 if( (avctx
->skip_frame
>= AVDISCARD_NONREF
&& s
->pict_type
==B_TYPE
)
974 ||(avctx
->skip_frame
>= AVDISCARD_NONKEY
&& s
->pict_type
!=I_TYPE
)
975 || avctx
->skip_frame
>= AVDISCARD_ALL
)
976 return get_consumed_bytes(s
, buf_size
);
978 if(MPV_frame_start(s
, avctx
) < 0)
981 ff_er_frame_start(s
);
983 /* decode each macroblock */
987 while(h
->gob_number
< (s
->mb_height
==18 ? 12 : 5)){
988 if(ff_h261_resync(h
)<0)
994 assert(s
->current_picture
.pict_type
== s
->current_picture_ptr
->pict_type
);
995 assert(s
->current_picture
.pict_type
== s
->pict_type
);
996 *pict
= *(AVFrame
*)s
->current_picture_ptr
;
997 ff_print_debug_info(s
, pict
);
999 /* Return the Picture timestamp as the frame number */
1000 /* we substract 1 because it is added on utils.c */
1001 avctx
->frame_number
= s
->picture_number
- 1;
1003 *data_size
= sizeof(AVFrame
);
1005 return get_consumed_bytes(s
, buf_size
);
1008 static int h261_decode_end(AVCodecContext
*avctx
)
1010 H261Context
*h
= avctx
->priv_data
;
1011 MpegEncContext
*s
= &h
->s
;
1017 #ifdef CONFIG_ENCODERS
1018 AVCodec h261_encoder
= {
1022 sizeof(H261Context
),
1029 AVCodec h261_decoder
= {
1033 sizeof(H261Context
),
1041 AVCodecParser h261_parser
= {
1043 sizeof(ParseContext
),