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