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 bits_left; //8 - nr of bits left of the following frame in the last byte in this frame
57 int last_bits; //bits left of the following frame in the last byte in this frame
58 int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read
61 void ff_h261_loop_filter(MpegEncContext *s){
62 H261Context * h= (H261Context*)s;
63 const int linesize = s->linesize;
64 const int uvlinesize= s->uvlinesize;
65 uint8_t *dest_y = s->dest[0];
66 uint8_t *dest_cb= s->dest[1];
67 uint8_t *dest_cr= s->dest[2];
69 if(!(IS_FIL (h->mtype)))
72 s->dsp.h261_loop_filter(dest_y , linesize);
73 s->dsp.h261_loop_filter(dest_y + 8, linesize);
74 s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize);
75 s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
76 s->dsp.h261_loop_filter(dest_cb, uvlinesize);
77 s->dsp.h261_loop_filter(dest_cr, uvlinesize);
80 int ff_h261_get_picture_format(int width, int height){
82 if (width == 176 && height == 144)
85 else if (width == 352 && height == 288)
92 static void h261_encode_block(H261Context * h, DCTELEM * block,
94 static int h261_decode_block(H261Context *h, DCTELEM *block,
96 static int h261_decode_mb(H261Context *h);
97 void ff_set_qscale(MpegEncContext * s, int qscale);
99 void ff_h261_encode_picture_header(MpegEncContext * s, int picture_number){
100 H261Context * h = (H261Context *) s;
101 int format, coded_frame_rate, coded_frame_rate_base, temp_ref;
102 int best_clock_code=1;
104 coded_frame_rate= 1800000;
105 coded_frame_rate_base= (1000+best_clock_code)*best_divisor;
107 align_put_bits(&s->pb);
109 /* Update the pointer to last GOB */
110 s->ptr_lastgob = pbBufPtr(&s->pb);
112 put_bits(&s->pb, 20, 0x10); /* PSC */
114 temp_ref= s->picture_number * (int64_t)coded_frame_rate * s->avctx->frame_rate_base /
115 (coded_frame_rate_base * (int64_t)s->avctx->frame_rate);
116 put_bits(&s->pb, 5, temp_ref & 0x1f); /* TemporalReference */
118 put_bits(&s->pb, 1, 0); /* split screen off */
119 put_bits(&s->pb, 1, 0); /* camera off */
120 put_bits(&s->pb, 1, 0); /* freeze picture release off */
122 format = ff_h261_get_picture_format(s->width, s->height);
124 put_bits(&s->pb, 1, format); /* 0 == QCIF, 1 == CIF */
126 put_bits(&s->pb, 1, 0); /* still image mode */
127 put_bits(&s->pb, 1, 0); /* reserved */
129 put_bits(&s->pb, 1, 0); /* no PEI */
138 * Encodes a group of blocks header.
140 static void h261_encode_gob_header(MpegEncContext * s, int mb_line){
141 H261Context * h = (H261Context *)s;
142 if(ff_h261_get_picture_format(s->width, s->height) == 0){
143 h->gob_number+=2; // QCIF
146 h->gob_number++; // CIF
148 put_bits(&s->pb, 16, 1); /* GBSC */
149 put_bits(&s->pb, 4, h->gob_number); /* GN */
150 put_bits(&s->pb, 5, s->qscale); /* GQUANT */
151 put_bits(&s->pb, 1, 0); /* no GEI */
158 void ff_h261_reorder_mb_index(MpegEncContext* s){
159 /* for CIF the GOB's are fragmented in the middle of a scanline
160 that's why we need to adjust the x and y index of the macroblocks */
161 if(ff_h261_get_picture_format(s->width,s->height) == 1){ // CIF
162 if((s->mb_x == 0 && (s->mb_y % 3 == 0) ) || (s->mb_x == 11 && ((s->mb_y -1 )% 3 == 0) ))
163 h261_encode_gob_header(s,0);
165 if((s->mb_y % 3) == 1 ){
169 else if( (s->mb_y % 3) == 2 ){
175 if((s->mb_y % 3) == 1 ){
179 else if( (s->mb_y % 3) == 0 ){
184 ff_init_block_index(s);
185 ff_update_block_index(s);
186 /* for QCIF we don't need to reorder MB's
187 there the GOB's aren't fragmented in the middle of a scanline */
188 }else if(ff_h261_get_picture_format(s->width,s->height) == 0){ // QCIF
189 if(s->mb_y % 3 == 0 && s->mb_x == 0)
190 h261_encode_gob_header(s,0);
194 static void h261_encode_motion(H261Context * h, int val){
195 MpegEncContext * const s = &h->s;
199 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
207 code = sign ? -val : val;
208 put_bits(&s->pb,h261_mv_tab[code][1],h261_mv_tab[code][0]);
209 put_bits(&s->pb,1,sign);
213 static inline int get_cbp(MpegEncContext * s,
214 DCTELEM block[6][64])
218 for (i = 0; i < 6; i++) {
219 if (s->block_last_index[i] >= 0)
224 void ff_h261_encode_mb(MpegEncContext * s,
225 DCTELEM block[6][64],
226 int motion_x, int motion_y)
228 H261Context * h = (H261Context *)s;
229 int old_mtype, mvd, mv_diff_x, mv_diff_y, i, cbp;
230 cbp = 63; // avoid warning
234 old_mtype = h->mtype;
239 cbp= get_cbp(s, block);
241 /* mvd indicates if this block is motion compensated */
242 if(((motion_x >> 1) - h->current_mv_x != 0) || ((motion_y >> 1 ) - h->current_mv_y) != 0){
245 else if((motion_x >> 1 == 0) && (motion_y >> 1 == 0)){
250 if((cbp | mvd | s->dquant ) == 0) {
251 /* skip macroblock */
259 /* MB is not skipped, encode MBA */
260 put_bits(&s->pb, h261_mba_bits[(h->current_mba-h->previous_mba)-1], h261_mba_code[(h->current_mba-h->previous_mba)-1]);
262 /* calculate MTYPE */
277 put_bits(&s->pb, h261_mtype_bits[h->mtype], h261_mtype_code[h->mtype]);
279 h->mtype = h261_mtype_map[h->mtype];
281 if(IS_QUANT(h->mtype)){
282 ff_set_qscale(s,s->qscale+s->dquant);
283 put_bits(&s->pb, 5, s->qscale);
286 if(IS_16X16(h->mtype)){
287 mv_diff_x = (motion_x >> 1) - h->current_mv_x;
288 mv_diff_y = (motion_y >> 1) - h->current_mv_y;
289 h->current_mv_x = (motion_x >> 1);
290 h->current_mv_y = (motion_y >> 1);
291 h261_encode_motion(h,mv_diff_x);
292 h261_encode_motion(h,mv_diff_y);
295 h->previous_mba = h->current_mba;
297 if(HAS_CBP(h->mtype)){
298 put_bits(&s->pb,h261_cbp_tab[cbp-1][1],h261_cbp_tab[cbp-1][0]);
301 /* encode each block */
302 h261_encode_block(h, block[i], i);
305 if ( ( h->current_mba == 11 ) || ( h->current_mba == 22 ) || ( h->current_mba == 33 ) || ( !IS_16X16 ( h->mtype ) )){
311 void ff_h261_encode_init(MpegEncContext *s){
316 init_rl(&h261_rl_tcoeff);
322 s->c_dc_scale_table= ff_mpeg1_dc_scale_table;
327 * encodes a 8x8 block.
328 * @param block the 8x8 block
329 * @param n block index (0-3 are luma, 4-5 are chroma)
331 static void h261_encode_block(H261Context * h, DCTELEM * block, int n){
332 MpegEncContext * const s = &h->s;
333 int level, run, last, i, j, last_index, last_non_zero, sign, slevel, code;
336 rl = &h261_rl_tcoeff;
340 /* 255 cannot be represented, so we clamp */
345 /* 0 cannot be represented also */
346 else if (level < 1) {
351 put_bits(&s->pb, 8, 0xff);
353 put_bits(&s->pb, 8, level);
355 } else if((block[0]==1 || block[0] == -1) && (s->block_last_index[n] > -1)){
357 put_bits(&s->pb,1,1);
358 put_bits(&s->pb,1,block[0]>0 ? 0 : 1 );
365 last_index = s->block_last_index[n];
366 last_non_zero = i - 1;
367 for (; i <= last_index; i++) {
368 j = s->intra_scantable.permutated[i];
371 run = i - last_non_zero - 1;
372 last = (i == last_index);
379 code = get_rl_index(rl, 0 /*no last in H.261, EOB is used*/, run, level);
380 if(run==0 && level < 16)
382 put_bits(&s->pb, rl->table_vlc[code][1], rl->table_vlc[code][0]);
384 put_bits(&s->pb, 6, run);
389 else if(slevel > 127){
392 put_bits(&s->pb, 8, slevel & 0xff);
394 put_bits(&s->pb, 1, sign);
400 put_bits(&s->pb, rl->table_vlc[0][1], rl->table_vlc[0][0]);// END OF BLOCK
404 /***********************************************/
407 static VLC h261_mba_vlc;
408 static VLC h261_mtype_vlc;
409 static VLC h261_mv_vlc;
410 static VLC h261_cbp_vlc;
412 void init_vlc_rl(RLTable *rl);
414 static void h261_decode_init_vlc(H261Context *h){
419 init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
421 h261_mba_code, 1, 1);
422 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
423 h261_mtype_bits, 1, 1,
424 h261_mtype_code, 1, 1);
425 init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
426 &h261_mv_tab[0][1], 2, 1,
427 &h261_mv_tab[0][0], 2, 1);
428 init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
429 &h261_cbp_tab[0][1], 2, 1,
430 &h261_cbp_tab[0][0], 2, 1);
431 init_rl(&h261_rl_tcoeff);
432 init_vlc_rl(&h261_rl_tcoeff);
436 static int h261_decode_init(AVCodecContext *avctx){
437 H261Context *h= avctx->priv_data;
438 MpegEncContext * const s = &h->s;
441 MPV_decode_defaults(s);
444 s->width = s->avctx->coded_width;
445 s->height = s->avctx->coded_height;
446 s->codec_id = s->avctx->codec->id;
448 s->out_format = FMT_H261;
450 avctx->pix_fmt= PIX_FMT_YUV420P;
452 s->codec_id= avctx->codec->id;
454 h261_decode_init_vlc(h);
456 h->gob_start_code_skipped = 0;
462 * decodes the group of blocks header or slice header.
463 * @return <0 if an error occured
465 static int h261_decode_gob_header(H261Context *h){
467 MpegEncContext * const s = &h->s;
469 if ( !h->gob_start_code_skipped ){
470 /* Check for GOB Start Code */
471 val = show_bits(&s->gb, 15);
476 skip_bits(&s->gb, 16);
479 h->gob_start_code_skipped = 0;
481 h->gob_number = get_bits(&s->gb, 4); /* GN */
482 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
484 /* Check if gob_number is valid */
485 if (s->mb_height==18){ //cif
486 if ((h->gob_number<=0) || (h->gob_number>12))
490 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
495 while (get_bits1(&s->gb) != 0) {
496 skip_bits(&s->gb, 8);
502 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
503 // subsequent macroblocks, MBA is the difference between the absolute addresses of
504 // the macroblock and the last transmitted macroblock.
512 * decodes the group of blocks / video packet header.
513 * @return <0 if no resync found
515 static int ff_h261_resync(H261Context *h){
516 MpegEncContext * const s = &h->s;
519 if ( h->gob_start_code_skipped ){
520 ret= h261_decode_gob_header(h);
525 if(show_bits(&s->gb, 15)==0){
526 ret= h261_decode_gob_header(h);
530 //ok, its not where its supposed to be ...
531 s->gb= s->last_resync_gb;
532 align_get_bits(&s->gb);
533 left= s->gb.size_in_bits - get_bits_count(&s->gb);
535 for(;left>15+1+4+5; left-=8){
536 if(show_bits(&s->gb, 15)==0){
537 GetBitContext bak= s->gb;
539 ret= h261_decode_gob_header(h);
545 skip_bits(&s->gb, 8);
553 * decodes skipped macroblocks
556 static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
558 MpegEncContext * const s = &h->s;
563 for(i=mba1; i<mba2; i++){
566 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
567 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
568 xy = s->mb_x + s->mb_y * s->mb_stride;
569 ff_init_block_index(s);
570 ff_update_block_index(s);
571 s->dsp.clear_blocks(s->block[0]);
574 s->block_last_index[j] = -1;
576 s->mv_dir = MV_DIR_FORWARD;
577 s->mv_type = MV_TYPE_16X16;
578 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
582 h->mtype &= ~MB_TYPE_H261_FIL;
584 MPV_decode_mb(s, s->block);
590 static int decode_mv_component(GetBitContext *gb, int v){
591 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
593 /* check if mv_diff is valid */
597 mv_diff = mvmap[mv_diff];
599 if(mv_diff && !get_bits1(gb))
604 else if(v >= 16) v-= 32;
609 static int h261_decode_mb(H261Context *h){
610 MpegEncContext * const s = &h->s;
611 int i, cbp, xy, old_mtype;
616 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
618 /* Check for slice end */
619 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
620 if (h->mba_diff == MBA_STARTCODE){ // start code
621 h->gob_start_code_skipped = 1;
625 while( h->mba_diff == MBA_STUFFING ); // stuffing
627 if ( h->mba_diff < 0 ){
628 if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
631 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
636 h->current_mba += h->mba_diff;
638 if ( h->current_mba > MBA_STUFFING )
641 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
642 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
643 xy = s->mb_x + s->mb_y * s->mb_stride;
644 ff_init_block_index(s);
645 ff_update_block_index(s);
646 s->dsp.clear_blocks(s->block[0]);
649 old_mtype = h->mtype;
650 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
651 h->mtype = h261_mtype_map[h->mtype];
654 if ( IS_QUANT ( h->mtype ) ){
655 ff_set_qscale(s, get_bits(&s->gb, 5));
658 s->mb_intra = IS_INTRA4x4(h->mtype);
661 if ( IS_16X16 ( h->mtype ) ){
662 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
663 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
664 // following three situations:
665 // 1) evaluating MVD for macroblocks 1, 12 and 23;
666 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
667 // 3) MTYPE of the previous macroblock was not MC.
668 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
669 ( h->mba_diff != 1) || ( !IS_16X16 ( old_mtype ) ))
675 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
676 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
680 if ( HAS_CBP( h->mtype ) ){
681 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
685 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
690 s->mv_dir = MV_DIR_FORWARD;
691 s->mv_type = MV_TYPE_16X16;
692 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
693 if(IS_16X16 ( h->mtype )){
694 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
695 s->mv[0][0][1] = h->current_mv_y * 2;
698 h->current_mv_x = s->mv[0][0][0] = 0;
699 h->current_mv_x = s->mv[0][0][1] = 0;
703 /* decode each block */
704 if(s->mb_intra || HAS_CBP(h->mtype)){
705 for (i = 0; i < 6; i++) {
706 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
713 MPV_decode_mb(s, s->block);
719 * decodes a macroblock
720 * @return <0 if an error occured
722 static int h261_decode_block(H261Context * h, DCTELEM * block,
725 MpegEncContext * const s = &h->s;
726 int code, level, i, j, run;
727 RLTable *rl = &h261_rl_tcoeff;
728 const uint8_t *scan_table;
730 // For the variable length encoding there are two code tables, one being used for
731 // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
732 // for all other LEVELs except the first one in INTRA blocks which is fixed length
733 // coded with 8 bits.
734 // NOTE: the two code tables only differ in one VLC so we handle that manually.
735 scan_table = s->intra_scantable.permutated;
738 level = get_bits(&s->gb, 8);
739 // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
740 if((level&0x7F) == 0){
741 av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
744 // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
751 // EOB Not possible for first level when cbp is available (that's why the table is different)
754 int check = show_bits(&s->gb, 2);
757 skip_bits(&s->gb, 2);
758 block[0] = ( check & 0x1 ) ? -1 : 1;
765 s->block_last_index[n] = i - 1;
769 code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
771 av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
776 // 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.
777 run = get_bits(&s->gb, 6);
778 level = (int8_t)get_bits(&s->gb, 8);
782 run = rl->table_run[code];
783 level = rl->table_level[code];
784 if (get_bits1(&s->gb))
789 av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
796 s->block_last_index[n] = i-1;
801 * decodes the H261 picture header.
802 * @return <0 if no startcode found
804 int h261_decode_picture_header(H261Context *h){
805 MpegEncContext * const s = &h->s;
807 uint32_t startcode= 0;
809 for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
810 startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
812 if(startcode == 0x10)
816 if (startcode != 0x10){
817 av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
821 /* temporal reference */
822 s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
824 /* PTYPE starts here */
825 skip_bits1(&s->gb); /* split screen off */
826 skip_bits1(&s->gb); /* camera off */
827 skip_bits1(&s->gb); /* freeze picture release off */
829 format = get_bits1(&s->gb);
831 //only 2 formats possible
832 if (format == 0){//QCIF
844 s->mb_num = s->mb_width * s->mb_height;
846 skip_bits1(&s->gb); /* still image mode off */
847 skip_bits1(&s->gb); /* Reserved */
850 while (get_bits1(&s->gb) != 0){
851 skip_bits(&s->gb, 8);
854 // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does
855 // not contain all I-blocks (e.g. when a packet is lost)
856 s->pict_type = P_TYPE;
862 static int h261_decode_gob(H261Context *h){
863 MpegEncContext * const s = &h->s;
865 ff_set_qscale(s, s->qscale);
868 while(h->current_mba <= MBA_STUFFING)
872 ret= h261_decode_mb(h);
875 h261_decode_mb_skipped(h, h->current_mba, 33);
878 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
882 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
888 static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
889 int vop_found, i, j, bits_left, last_bits;
892 H261Context *h = avctx->priv_data;
895 bits_left = h->bits_left;
896 last_bits = h->last_bits;
903 vop_found= pc->frame_start_found;
905 if(bits_left!=0 && !vop_found)
906 state = state << (8-bits_left) | last_bits;
909 for(i=0; i<buf_size; i++){
910 state= (state<<8) | buf[i];
912 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
923 for(; i<buf_size; i++){
924 if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
925 state= (state<<8) | buf[i];
927 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
928 pc->frame_start_found=0;
936 pc->frame_start_found= vop_found;
938 return END_NOT_FOUND;
941 static int h261_parse(AVCodecParserContext *s,
942 AVCodecContext *avctx,
943 uint8_t **poutbuf, int *poutbuf_size,
944 const uint8_t *buf, int buf_size)
946 ParseContext *pc = s->priv_data;
949 next= h261_find_frame_end(pc,avctx, buf, buf_size);
950 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
955 *poutbuf = (uint8_t *)buf;
956 *poutbuf_size = buf_size;
961 * returns the number of bytes consumed for building the current frame
963 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
964 if(s->flags&CODEC_FLAG_TRUNCATED){
965 int pos= (get_bits_count(&s->gb)+7)>>3;
966 pos -= s->parse_context.last_index;
967 if(pos<0) pos=0;// padding is not really read so this might be -1
970 int pos= get_bits_count(&s->gb)>>3;
971 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
972 if(pos+10>buf_size) pos=buf_size; // oops ;)
978 static int h261_decode_frame(AVCodecContext *avctx,
979 void *data, int *data_size,
980 uint8_t *buf, int buf_size)
982 H261Context *h= avctx->priv_data;
983 MpegEncContext *s = &h->s;
985 AVFrame *pict = data;
988 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
989 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
991 s->flags= avctx->flags;
992 s->flags2= avctx->flags2;
994 /* no supplementary picture */
999 if(s->flags&CODEC_FLAG_TRUNCATED){
1002 next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
1004 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
1011 init_get_bits(&s->gb, buf, buf_size*8);
1013 if(!s->context_initialized){
1014 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
1018 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
1019 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
1020 int i= ff_find_unused_picture(s, 0);
1021 s->current_picture_ptr= &s->picture[i];
1024 ret = h261_decode_picture_header(h);
1026 /* skip if the header was thrashed */
1028 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
1032 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
1033 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
1034 s->parse_context.buffer=0;
1036 s->parse_context= pc;
1038 if (!s->context_initialized) {
1039 avcodec_set_dimensions(avctx, s->width, s->height);
1045 s->current_picture.pict_type= s->pict_type;
1046 s->current_picture.key_frame= s->pict_type == I_TYPE;
1048 /* skip everything if we are in a hurry>=5 */
1049 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
1051 if(MPV_frame_start(s, avctx) < 0)
1054 ff_er_frame_start(s);
1056 /* decode each macroblock */
1060 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
1061 if(ff_h261_resync(h)<0)
1067 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
1068 assert(s->current_picture.pict_type == s->pict_type);
1069 *pict= *(AVFrame*)&s->current_picture;
1070 ff_print_debug_info(s, pict);
1072 /* Return the Picture timestamp as the frame number */
1073 /* we substract 1 because it is added on utils.c */
1074 avctx->frame_number = s->picture_number - 1;
1076 *data_size = sizeof(AVFrame);
1078 return get_consumed_bytes(s, buf_size);
1081 static int h261_decode_end(AVCodecContext *avctx)
1083 H261Context *h= avctx->priv_data;
1084 MpegEncContext *s = &h->s;
1090 AVCodec h261_encoder = {
1094 sizeof(H261Context),
1100 AVCodec h261_decoder = {
1104 sizeof(H261Context),
1109 CODEC_CAP_TRUNCATED,
1112 AVCodecParser h261_parser = {
1114 sizeof(ParseContext),