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