]> git.sesse.net Git - ffmpeg/blob - libavcodec/h261.c
7ee5bff043d40b2ac412a9d48c3b05e5f0e7993a
[ffmpeg] / libavcodec / h261.c
1 /*
2  * H261 decoder
3  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
4  * Copyright (c) 2004 Maarten Daniels
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file h261.c
23  * h261codec.
24  */
25
26 #include "common.h"
27 #include "dsputil.h"
28 #include "avcodec.h"
29 #include "mpegvideo.h"
30 #include "h261data.h"
31
32
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
38
39 #define MAX_MBA 33
40 #define IS_FIL(a)    ((a)&MB_TYPE_H261_FIL)
41
42 /**
43  * H261Context
44  */
45 typedef struct H261Context{
46     MpegEncContext s;
47
48     int current_mba;
49     int mba_diff;
50     int mtype;
51     int current_mv_x;
52     int current_mv_y;
53     int gob_number;
54     int loop_filter;
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 }H261Context;
58
59 void ff_h261_loop_filter(H261Context * h){
60     MpegEncContext * const s = &h->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];
66     
67     s->dsp.h261_loop_filter(dest_y                   , linesize);
68     s->dsp.h261_loop_filter(dest_y                + 8, linesize);
69     s->dsp.h261_loop_filter(dest_y + 8 * linesize    , linesize);
70     s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
71     s->dsp.h261_loop_filter(dest_cb, uvlinesize);
72     s->dsp.h261_loop_filter(dest_cr, uvlinesize);
73 }
74
75 static int h261_decode_block(H261Context *h, DCTELEM *block,
76                              int n, int coded);
77 static int h261_decode_mb(H261Context *h,
78                       DCTELEM block[6][64]);
79 void ff_set_qscale(MpegEncContext * s, int qscale);
80
81 /***********************************************/
82 /* decoding */
83
84 static VLC h261_mba_vlc;
85 static VLC h261_mtype_vlc;
86 static VLC h261_mv_vlc;
87 static VLC h261_cbp_vlc;
88
89 void init_vlc_rl(RLTable *rl);
90
91 static void h261_decode_init_vlc(H261Context *h){
92     static int done = 0;
93
94     if(!done){
95         done = 1;
96         init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 34,
97                  h261_mba_bits, 1, 1,
98                  h261_mba_code, 1, 1);
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);
110     }
111 }
112
113 static int h261_decode_init(AVCodecContext *avctx){
114     H261Context *h= avctx->priv_data;
115     MpegEncContext * const s = &h->s;
116
117     // set defaults
118     MPV_decode_defaults(s);
119     s->avctx = avctx;
120
121     s->width = s->avctx->width;
122     s->height = s->avctx->height;
123     s->codec_id = s->avctx->codec->id;
124
125     s->out_format = FMT_H261;
126     s->low_delay= 1;
127     avctx->pix_fmt= PIX_FMT_YUV420P;
128
129     s->codec_id= avctx->codec->id;
130
131     h261_decode_init_vlc(h);
132
133     h->bits_left = 0;
134     h->last_bits = 0;
135     
136     return 0;
137 }
138
139 /**
140  * decodes the group of blocks header or slice header.
141  * @return <0 if an error occured
142  */
143 static int h261_decode_gob_header(H261Context *h){
144     unsigned int val;
145     MpegEncContext * const s = &h->s;
146     
147     /* Check for GOB Start Code */
148     val = show_bits(&s->gb, 15);
149     if(val)
150         return -1;
151
152     /* We have a GBSC */
153     skip_bits(&s->gb, 16);
154
155     h->gob_number = get_bits(&s->gb, 4); /* GN */
156     s->qscale = get_bits(&s->gb, 5); /* GQUANT */
157
158     /* GEI */
159     while (get_bits1(&s->gb) != 0) {
160         skip_bits(&s->gb, 8);
161     }
162
163     if(s->qscale==0)
164         return -1;
165
166     // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
167     // subsequent macroblocks, MBA is the difference between the absolute addresses of
168     // the macroblock and the last transmitted macroblock.
169     h->current_mba = 0;
170     h->mba_diff = 0;
171
172     return 0;
173 }
174
175 /**
176  * decodes the group of blocks / video packet header.
177  * @return <0 if no resync found
178  */
179 static int ff_h261_resync(H261Context *h){
180     MpegEncContext * const s = &h->s;
181     int left, ret;
182
183     if(show_bits(&s->gb, 15)==0){
184         ret= h261_decode_gob_header(h);
185         if(ret>=0)
186             return 0;
187     }
188     //ok, its not where its supposed to be ...
189     s->gb= s->last_resync_gb;
190     align_get_bits(&s->gb);
191     left= s->gb.size_in_bits - get_bits_count(&s->gb);
192
193     for(;left>15+1+4+5; left-=8){
194         if(show_bits(&s->gb, 15)==0){
195             GetBitContext bak= s->gb;
196
197             ret= h261_decode_gob_header(h);
198             if(ret>=0)
199                 return 0;
200
201             s->gb= bak;
202         }
203         skip_bits(&s->gb, 8);
204     }
205
206     return -1;
207 }
208
209 /**
210  * decodes a skipped macroblock, called when when mba_diff > 1.
211  * @return 0
212  */
213 static int h261_decode_mb_skipped(H261Context *h,
214                                   DCTELEM block[6][64])
215 {
216     MpegEncContext * const s = &h->s;
217     int i;
218     const int xy = s->mb_x + s->mb_y * s->mb_stride;
219     s->mb_intra = 0;
220     for(i=0;i<6;i++)
221         s->block_last_index[i] = -1;
222     s->mv_dir = MV_DIR_FORWARD;
223     s->mv_type = MV_TYPE_16X16;
224     s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
225     s->mv[0][0][0] = 0;
226     s->mv[0][0][1] = 0;
227     s->mb_skiped = 1;
228     return 0;
229 }
230
231 static int decode_mv_component(GetBitContext *gb, int v){
232     int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
233     mv_diff = mvmap[mv_diff];
234
235     if(mv_diff && !get_bits1(gb))
236         mv_diff= -mv_diff;
237     
238     v += mv_diff;
239     if     (v <=-16) v+= 32;
240     else if(v >= 16) v-= 32;
241
242     return v;
243 }
244
245 static int h261_decode_mb(H261Context *h,
246                           DCTELEM block[6][64])
247 {
248     MpegEncContext * const s = &h->s;
249     int i, cbp, xy;
250
251     cbp = 63;
252     // Read mba
253     do{
254         h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2)+1;
255     }
256     while( h->mba_diff == MAX_MBA + 1 ); // stuffing
257
258     if ( h->mba_diff < 0 )
259         return -1;
260
261     h->current_mba += h->mba_diff;
262
263     if ( h->current_mba > MAX_MBA )
264         return -1;
265     
266     s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
267     s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
268
269     xy = s->mb_x + s->mb_y * s->mb_stride;
270
271     ff_init_block_index(s);
272     ff_update_block_index(s);
273
274     // Read mtype
275     int old_mtype = h->mtype;
276     h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
277     h->mtype = h261_mtype_map[h->mtype];
278
279     if (IS_FIL (h->mtype))
280         h->loop_filter = 1;
281
282     // Read mquant
283     if ( IS_QUANT ( h->mtype ) ){
284         s->qscale = get_bits(&s->gb, 5);
285     }
286
287     s->mb_intra = IS_INTRA4x4(h->mtype);
288
289     // Read mv
290     if ( IS_16X16 ( h->mtype ) ){
291         // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
292         // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
293         // following three situations:
294         // 1) evaluating MVD for macroblocks 1, 12 and 23;
295         // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
296         // 3) MTYPE of the previous macroblock was not MC.
297         if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
298              ( h->mba_diff != 1) || ( !IS_16X16 ( old_mtype ) ))
299         {
300             h->current_mv_x = 0;
301             h->current_mv_y = 0;
302         }
303
304         h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
305         h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
306     }
307
308     // Read cbp
309     if ( HAS_CBP( h->mtype ) ){
310         cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
311     }
312
313     if(s->mb_intra){
314         s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
315         goto intra;
316     }
317
318     //set motion vectors
319     s->mv_dir = MV_DIR_FORWARD;
320     s->mv_type = MV_TYPE_16X16;
321     s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
322     if(IS_16X16 ( h->mtype )){
323         s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
324         s->mv[0][0][1] = h->current_mv_y * 2;
325     }
326     else{
327         h->current_mv_x = s->mv[0][0][0] = 0;
328         h->current_mv_x = s->mv[0][0][1] = 0;
329     }
330
331 intra:
332     /* decode each block */
333     if(s->mb_intra || HAS_CBP(h->mtype)){
334         for (i = 0; i < 6; i++) {
335             if (h261_decode_block(h, block[i], i, cbp&32) < 0){
336                 return -1;
337             }
338             cbp+=cbp;
339         }
340     }
341
342     /* per-MB end of slice check */
343     {
344         int v= show_bits(&s->gb, 15);
345
346         if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){
347             v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits;
348         }
349
350         if(v==0){
351             return SLICE_END;
352         }
353     }
354     return SLICE_OK;
355 }
356
357 /**
358  * decodes a macroblock
359  * @return <0 if an error occured
360  */
361 static int h261_decode_block(H261Context * h, DCTELEM * block,
362                              int n, int coded)
363 {
364     MpegEncContext * const s = &h->s;
365     int code, level, i, j, run;
366     RLTable *rl = &h261_rl_tcoeff;
367     const uint8_t *scan_table;
368     
369     // For the variable length encoding there are two code tables, one being used for
370     // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
371     // for all other LEVELs except the first one in INTRA blocks which is fixed length
372     // coded with 8 bits.
373     // NOTE: the two code tables only differ in one VLC so we handle that manually.
374     scan_table = s->intra_scantable.permutated;
375     if (s->mb_intra){
376         /* DC coef */
377         level = get_bits(&s->gb, 8);
378         // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
379         if((level&0x7F) == 0){
380             av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
381             return -1;
382         }
383         // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
384         if (level == 255)
385             level = 128;
386         block[0] = level;
387         i = 1;
388     }else if(coded){
389         // Run  Level   Code
390         // EOB                  Not possible for first level when cbp is available (that's why the table is different)
391         // 0    1               1s
392         // *    *               0*
393         int check = show_bits(&s->gb, 2);
394         i = 0;
395         if ( check & 0x2 ){
396             skip_bits(&s->gb, 2);
397             block[0] = ( check & 0x1 ) ? -1 : 1;
398             i = 1;
399         }
400     }else{
401         i = 0;
402     }
403     if(!coded){
404         s->block_last_index[n] = i - 1;
405         return 0;
406     }
407     for(;;){
408         code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
409         if (code < 0){
410             av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
411             return -1;
412         }
413         if (code == rl->n) {
414             /* escape */
415             // 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.
416             run = get_bits(&s->gb, 6);
417             level = (int8_t)get_bits(&s->gb, 8);
418         }else if(code == 0){
419             break;
420         }else{
421             run = rl->table_run[code];
422             level = rl->table_level[code];
423             if (get_bits1(&s->gb))
424                 level = -level;
425         }
426         i += run;
427         if (i >= 64){
428             av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
429             return -1;
430         }
431         j = scan_table[i];
432         block[j] = level;
433         i++;
434     }
435     s->block_last_index[n] = i;
436     return 0;
437 }
438
439 /**
440  * decodes the H261 picture header.
441  * @return <0 if no startcode found
442  */
443 int h261_decode_picture_header(H261Context *h){
444     MpegEncContext * const s = &h->s;
445     int format, i;
446     static int h261_framecounter = 0;
447     uint32_t startcode;
448     align_get_bits(&s->gb);
449
450     startcode = (h->last_bits << (12 - (8-h->bits_left))) | get_bits(&s->gb, 20-8 - (8- h->bits_left));
451
452     for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
453         startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
454
455         if(startcode == 0x10)
456             break;
457     }
458
459     if (startcode != 0x10){
460         av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
461         return -1;
462     }
463
464     /* temporal reference */
465     s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
466
467     /* PTYPE starts here */
468     skip_bits1(&s->gb); /* split screen off */
469     skip_bits1(&s->gb); /* camera  off */
470     skip_bits1(&s->gb); /* freeze picture release off */
471
472     format = get_bits1(&s->gb);
473
474     //only 2 formats possible
475     if (format == 0){//QCIF
476         s->width = 176;
477         s->height = 144;
478         s->mb_width = 11;
479         s->mb_height = 9;
480     }else{//CIF
481         s->width = 352;
482         s->height = 288;
483         s->mb_width = 22;
484         s->mb_height = 18;
485     }
486
487     s->mb_num = s->mb_width * s->mb_height;
488
489     skip_bits1(&s->gb); /* still image mode off */
490     skip_bits1(&s->gb); /* Reserved */
491
492     /* PEI */
493     while (get_bits1(&s->gb) != 0){
494         skip_bits(&s->gb, 8);
495     }
496
497     //h261 has no I-FRAMES, pass the test in MPV_frame_start in mpegvideo.c
498     if(h261_framecounter > 1)
499         s->pict_type = P_TYPE;
500     else
501         s->pict_type = I_TYPE;
502
503     h261_framecounter++;
504
505     h->gob_number = 0;
506     return 0;
507 }
508
509 static int h261_decode_gob(H261Context *h){
510     MpegEncContext * const s = &h->s;
511     int i;
512     
513     ff_set_qscale(s, s->qscale);
514     while(h->current_mba <= MAX_MBA)
515     {
516         int ret;
517         /* DCT & quantize */
518         s->dsp.clear_blocks(s->block[0]);
519         ret= h261_decode_mb(h, s->block);
520         if(ret<0){
521             const int xy= s->mb_x + s->mb_y*s->mb_stride;
522             if(ret==SLICE_END){
523                 MPV_decode_mb(s, s->block);
524                 if(h->loop_filter){
525                     ff_h261_loop_filter(h);
526                 }
527                 h->loop_filter = 0;
528                 for(i=1; i<h->mba_diff; i++){
529                     s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1-i) % 11);
530                     s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1-i) / 11);
531                     ff_init_block_index(s);
532                     ff_update_block_index(s);
533                     s->dsp.clear_blocks(s->block[0]);
534                     ret= h261_decode_mb_skipped(h, s->block);
535                     MPV_decode_mb(s, s->block);
536                 }
537                 
538                 return 0;
539             }else if(ret==SLICE_NOEND){
540                 av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
541                 return -1;
542             }
543             av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
544             return -1;
545         }
546         MPV_decode_mb(s, s->block);
547         if(h->loop_filter){
548             ff_h261_loop_filter(h);
549         }
550
551         h->loop_filter = 0;
552         for(i=1; i<h->mba_diff; i++){
553             s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1-i) % 11);
554             s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1-i) / 11);
555             ff_init_block_index(s);
556             ff_update_block_index(s);
557             s->dsp.clear_blocks(s->block[0]);
558             ret= h261_decode_mb_skipped(h, s->block);
559             MPV_decode_mb(s, s->block);
560         }
561     }
562     
563     return -1;
564 }
565
566 static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
567     int vop_found, i, j, bits_left, last_bits;
568     uint32_t state;
569
570     H261Context *h = avctx->priv_data;
571
572     if(h){
573         bits_left = h->bits_left;
574         last_bits = h->last_bits;
575     }
576     else{
577         bits_left = 0;
578         last_bits = 0;
579     }
580
581     vop_found= pc->frame_start_found;
582     state= pc->state;
583     if(bits_left!=0 && !vop_found)
584         state = state << (8-bits_left) | last_bits;
585     i=0;
586     if(!vop_found){
587         for(i=0; i<buf_size; i++){
588             state= (state<<8) | buf[i];
589             for(j=0; j<8; j++){
590                 if(( (  (state<<j)  |  (buf[i]>>(8-j))  )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
591                     i++;
592                     vop_found=1;
593                     break;
594                 }
595             }
596             if(vop_found)
597                     break;    
598         }
599     }
600     if(vop_found){
601         for(; i<buf_size; i++){
602             if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
603                 state= (state<<8) | buf[i];
604             for(j=0; j<8; j++){
605                 if(( (  (state<<j)  |  (buf[i]>>(8-j))  )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
606                     pc->frame_start_found=0;
607                     pc->state=-1;
608                     return i-3;
609                 }
610             }
611         }
612     }
613
614     pc->frame_start_found= vop_found;
615     pc->state= state;
616     return END_NOT_FOUND;
617 }
618
619 static int h261_parse(AVCodecParserContext *s,
620                       AVCodecContext *avctx,
621                       uint8_t **poutbuf, int *poutbuf_size, 
622                       const uint8_t *buf, int buf_size)
623 {
624     ParseContext *pc = s->priv_data;
625     int next;
626     
627     next= h261_find_frame_end(pc,avctx, buf, buf_size);
628     if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
629         *poutbuf = NULL;
630         *poutbuf_size = 0;
631         return buf_size;
632     }
633     *poutbuf = (uint8_t *)buf;
634     *poutbuf_size = buf_size;
635     return next;
636 }
637
638 /**
639  * returns the number of bytes consumed for building the current frame
640  */
641 static int get_consumed_bytes(MpegEncContext *s, int buf_size){
642     int pos= (get_bits_count(&s->gb)+7)>>3;
643
644     if(s->flags&CODEC_FLAG_TRUNCATED){
645         pos -= s->parse_context.last_index;
646         if(pos<0) pos=0;// padding is not really read so this might be -1
647         return pos;
648     }else{
649         if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
650         if(pos+10>buf_size) pos=buf_size; // oops ;)
651
652         return pos;
653     }
654 }
655
656 static int h261_decode_frame(AVCodecContext *avctx,
657                              void *data, int *data_size,
658                              uint8_t *buf, int buf_size)
659 {
660     H261Context *h= avctx->priv_data;
661     MpegEncContext *s = &h->s;
662     int ret;
663     AVFrame *pict = data;
664
665 #ifdef DEBUG
666     printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
667     printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
668 #endif
669     s->flags= avctx->flags;
670     s->flags2= avctx->flags2;
671
672     /* no supplementary picture */
673     if (buf_size == 0) {
674
675         return 0;
676     }
677
678     if(s->flags&CODEC_FLAG_TRUNCATED){
679         int next;
680
681         next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
682
683         if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
684             return buf_size;
685     }
686
687
688 retry:
689
690     init_get_bits(&s->gb, buf, buf_size*8);
691
692     if(!s->context_initialized){
693         if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
694             return -1;
695     }
696
697     //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
698     if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
699         int i= ff_find_unused_picture(s, 0);
700         s->current_picture_ptr= &s->picture[i];
701     }
702
703     ret = h261_decode_picture_header(h);
704
705     /* skip if the header was thrashed */
706     if (ret < 0){
707         av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
708         return -1;
709     }
710
711     if (s->width != avctx->width || s->height != avctx->height){
712         ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
713         s->parse_context.buffer=0;
714         MPV_common_end(s);
715         s->parse_context= pc;
716     }
717     if (!s->context_initialized) {
718         avctx->width = s->width;
719         avctx->height = s->height;
720
721         goto retry;
722     }
723
724     // for hurry_up==5
725     s->current_picture.pict_type= s->pict_type;
726     s->current_picture.key_frame= s->pict_type == I_TYPE;
727
728     /* skip everything if we are in a hurry>=5 */
729     if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
730
731     if(MPV_frame_start(s, avctx) < 0)
732         return -1;
733
734     ff_er_frame_start(s);
735
736     /* decode each macroblock */
737     s->mb_x=0;
738     s->mb_y=0;
739
740     while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
741         if(ff_h261_resync(h)<0)
742             break;
743         h261_decode_gob(h);
744     }
745     MPV_frame_end(s);
746
747     // h261 doesn't have byte aligned codes
748     // store the bits of the next frame that are left in the last byte
749     // in the H261Context and remember the number of stored bits
750     {
751         int bitsleft;
752         int current_pos= get_bits_count(&s->gb)>>3;
753         bitsleft =  (current_pos<<3) - get_bits_count(&s->gb);
754         h->bits_left = - bitsleft;
755         if(bitsleft > 0)
756             h->last_bits= get_bits(&s->gb, 8 - h->bits_left);
757         else
758             h->last_bits = 0;
759     }
760
761 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
762 assert(s->current_picture.pict_type == s->pict_type);
763     *pict= *(AVFrame*)&s->current_picture;
764     ff_print_debug_info(s, pict);
765
766     /* Return the Picture timestamp as the frame number */
767     /* we substract 1 because it is added on utils.c    */
768     avctx->frame_number = s->picture_number - 1;
769
770     *data_size = sizeof(AVFrame);
771
772     return get_consumed_bytes(s, buf_size);
773 }
774
775 static int h261_decode_end(AVCodecContext *avctx)
776 {
777     H261Context *h= avctx->priv_data;
778     MpegEncContext *s = &h->s;
779
780     MPV_common_end(s);
781     return 0;
782 }
783
784 AVCodec h261_decoder = {
785     "h261",
786     CODEC_TYPE_VIDEO,
787     CODEC_ID_H261,
788     sizeof(H261Context),
789     h261_decode_init,
790     NULL,
791     h261_decode_end,
792     h261_decode_frame,
793     CODEC_CAP_TRUNCATED,
794 };
795
796 AVCodecParser h261_parser = {
797     { CODEC_ID_H261 },
798     sizeof(ParseContext),
799     NULL,
800     h261_parse,
801     ff_parse_close,
802 };