- Support empty GOB's (no mb's) and skip all mb's
[libav.git] / libavcodec / h261.c
CommitLineData
c6148de2
MN
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 */
45typedef 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
59void ff_h261_loop_filter(H261Context * h){
60 MpegEncContext * const s = &h->s;
c6148de2
MN
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];
fdbbf2e0
MN
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);
c6148de2
MN
73}
74
75static int h261_decode_block(H261Context *h, DCTELEM *block,
76 int n, int coded);
77static int h261_decode_mb(H261Context *h,
78 DCTELEM block[6][64]);
79void ff_set_qscale(MpegEncContext * s, int qscale);
80
81/***********************************************/
82/* decoding */
83
84static VLC h261_mba_vlc;
85static VLC h261_mtype_vlc;
86static VLC h261_mv_vlc;
87static VLC h261_cbp_vlc;
88
89void init_vlc_rl(RLTable *rl);
90
91static 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
113static 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 */
143static 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 */
179static 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/**
7344c87a 210 * decodes skipped macroblocks
c6148de2
MN
211 * @return 0
212 */
7344c87a 213static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
c6148de2
MN
214{
215 MpegEncContext * const s = &h->s;
216 int i;
7344c87a 217
c6148de2 218 s->mb_intra = 0;
7344c87a
CF
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
c6148de2
MN
243 return 0;
244}
245
b37e98c8
MN
246static 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
c6148de2
MN
260static int h261_decode_mb(H261Context *h,
261 DCTELEM block[6][64])
262{
263 MpegEncContext * const s = &h->s;
6bc79819 264 int i, cbp, xy, old_mtype;
c6148de2
MN
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);
7344c87a 288 s->dsp.clear_blocks(s->block[0]);
c6148de2
MN
289
290 // Read mtype
6bc79819 291 old_mtype = h->mtype;
c6148de2
MN
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 ) ){
7344c87a 300 ff_set_qscale(s, get_bits(&s->gb, 5));
c6148de2
MN
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
b37e98c8
MN
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);
c6148de2
MN
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
347intra:
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 */
377static 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 }
7344c87a 451 s->block_last_index[n] = i-1;
c6148de2
MN
452 return 0;
453}
454
455/**
456 * decodes the H261 picture header.
457 * @return <0 if no startcode found
458 */
459int 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
525static int h261_decode_gob(H261Context *h){
526 MpegEncContext * const s = &h->s;
c6148de2
MN
527
528 ff_set_qscale(s, s->qscale);
7344c87a
CF
529
530 /* check for empty gob */
531 int v= show_bits(&s->gb, 15);
532
533 if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){
534 v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits;
535 }
536
537 if(v==0){
538 h261_decode_mb_skipped(h, 0, 33);
539 return 0;
540 }
541
542 /* decode mb's */
c6148de2
MN
543 while(h->current_mba <= MAX_MBA)
544 {
545 int ret;
546 /* DCT & quantize */
c6148de2
MN
547 ret= h261_decode_mb(h, s->block);
548 if(ret<0){
549 const int xy= s->mb_x + s->mb_y*s->mb_stride;
550 if(ret==SLICE_END){
551 MPV_decode_mb(s, s->block);
552 if(h->loop_filter){
553 ff_h261_loop_filter(h);
554 }
555 h->loop_filter = 0;
7344c87a
CF
556 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
557 h261_decode_mb_skipped(h, h->current_mba, 33);
c6148de2
MN
558 return 0;
559 }else if(ret==SLICE_NOEND){
560 av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
561 return -1;
562 }
563 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
564 return -1;
565 }
566 MPV_decode_mb(s, s->block);
567 if(h->loop_filter){
568 ff_h261_loop_filter(h);
569 }
570
571 h->loop_filter = 0;
7344c87a 572 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
c6148de2
MN
573 }
574
575 return -1;
576}
577
578static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
579 int vop_found, i, j, bits_left, last_bits;
580 uint32_t state;
581
582 H261Context *h = avctx->priv_data;
583
584 if(h){
585 bits_left = h->bits_left;
586 last_bits = h->last_bits;
587 }
588 else{
589 bits_left = 0;
590 last_bits = 0;
591 }
592
593 vop_found= pc->frame_start_found;
594 state= pc->state;
595 if(bits_left!=0 && !vop_found)
596 state = state << (8-bits_left) | last_bits;
597 i=0;
598 if(!vop_found){
599 for(i=0; i<buf_size; i++){
600 state= (state<<8) | buf[i];
601 for(j=0; j<8; j++){
602 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
603 i++;
604 vop_found=1;
605 break;
606 }
607 }
608 if(vop_found)
609 break;
610 }
611 }
612 if(vop_found){
613 for(; i<buf_size; i++){
614 if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
615 state= (state<<8) | buf[i];
616 for(j=0; j<8; j++){
617 if(( ( (state<<j) | (buf[i]>>(8-j)) )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
618 pc->frame_start_found=0;
619 pc->state=-1;
620 return i-3;
621 }
622 }
623 }
624 }
625
626 pc->frame_start_found= vop_found;
627 pc->state= state;
628 return END_NOT_FOUND;
629}
630
631static int h261_parse(AVCodecParserContext *s,
632 AVCodecContext *avctx,
633 uint8_t **poutbuf, int *poutbuf_size,
634 const uint8_t *buf, int buf_size)
635{
636 ParseContext *pc = s->priv_data;
637 int next;
638
639 next= h261_find_frame_end(pc,avctx, buf, buf_size);
640 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
641 *poutbuf = NULL;
642 *poutbuf_size = 0;
643 return buf_size;
644 }
645 *poutbuf = (uint8_t *)buf;
646 *poutbuf_size = buf_size;
647 return next;
648}
649
650/**
651 * returns the number of bytes consumed for building the current frame
652 */
653static int get_consumed_bytes(MpegEncContext *s, int buf_size){
654 int pos= (get_bits_count(&s->gb)+7)>>3;
655
656 if(s->flags&CODEC_FLAG_TRUNCATED){
657 pos -= s->parse_context.last_index;
658 if(pos<0) pos=0;// padding is not really read so this might be -1
659 return pos;
660 }else{
661 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
662 if(pos+10>buf_size) pos=buf_size; // oops ;)
663
664 return pos;
665 }
666}
667
668static int h261_decode_frame(AVCodecContext *avctx,
669 void *data, int *data_size,
670 uint8_t *buf, int buf_size)
671{
672 H261Context *h= avctx->priv_data;
673 MpegEncContext *s = &h->s;
674 int ret;
675 AVFrame *pict = data;
676
c6148de2
MN
677#ifdef DEBUG
678 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
679 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
680#endif
681 s->flags= avctx->flags;
682 s->flags2= avctx->flags2;
683
684 /* no supplementary picture */
685 if (buf_size == 0) {
c6148de2
MN
686
687 return 0;
688 }
689
690 if(s->flags&CODEC_FLAG_TRUNCATED){
691 int next;
692
693 next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
694
695 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
696 return buf_size;
697 }
698
699
700retry:
701
b658ecfe 702 init_get_bits(&s->gb, buf, buf_size*8);
c6148de2
MN
703
704 if(!s->context_initialized){
705 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
706 return -1;
707 }
708
709 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
710 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
711 int i= ff_find_unused_picture(s, 0);
712 s->current_picture_ptr= &s->picture[i];
713 }
714
715 ret = h261_decode_picture_header(h);
716
c6148de2
MN
717 /* skip if the header was thrashed */
718 if (ret < 0){
719 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
720 return -1;
721 }
722
723 if (s->width != avctx->width || s->height != avctx->height){
724 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
725 s->parse_context.buffer=0;
726 MPV_common_end(s);
727 s->parse_context= pc;
728 }
729 if (!s->context_initialized) {
730 avctx->width = s->width;
731 avctx->height = s->height;
732
733 goto retry;
734 }
735
736 // for hurry_up==5
737 s->current_picture.pict_type= s->pict_type;
738 s->current_picture.key_frame= s->pict_type == I_TYPE;
739
740 /* skip everything if we are in a hurry>=5 */
741 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
742
743 if(MPV_frame_start(s, avctx) < 0)
744 return -1;
745
746 ff_er_frame_start(s);
747
748 /* decode each macroblock */
749 s->mb_x=0;
750 s->mb_y=0;
751
752 while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
753 if(ff_h261_resync(h)<0)
754 break;
755 h261_decode_gob(h);
756 }
757 MPV_frame_end(s);
758
759 // h261 doesn't have byte aligned codes
760 // store the bits of the next frame that are left in the last byte
761 // in the H261Context and remember the number of stored bits
762 {
763 int bitsleft;
764 int current_pos= get_bits_count(&s->gb)>>3;
765 bitsleft = (current_pos<<3) - get_bits_count(&s->gb);
766 h->bits_left = - bitsleft;
767 if(bitsleft > 0)
768 h->last_bits= get_bits(&s->gb, 8 - h->bits_left);
769 else
770 h->last_bits = 0;
771 }
772
773assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
774assert(s->current_picture.pict_type == s->pict_type);
b658ecfe
MN
775 *pict= *(AVFrame*)&s->current_picture;
776 ff_print_debug_info(s, pict);
c6148de2
MN
777
778 /* Return the Picture timestamp as the frame number */
779 /* we substract 1 because it is added on utils.c */
780 avctx->frame_number = s->picture_number - 1;
781
b658ecfe 782 *data_size = sizeof(AVFrame);
c6148de2 783
b658ecfe 784 return get_consumed_bytes(s, buf_size);
c6148de2
MN
785}
786
787static int h261_decode_end(AVCodecContext *avctx)
788{
789 H261Context *h= avctx->priv_data;
790 MpegEncContext *s = &h->s;
791
792 MPV_common_end(s);
793 return 0;
794}
795
796AVCodec h261_decoder = {
797 "h261",
798 CODEC_TYPE_VIDEO,
799 CODEC_ID_H261,
800 sizeof(H261Context),
801 h261_decode_init,
802 NULL,
803 h261_decode_end,
804 h261_decode_frame,
805 CODEC_CAP_TRUNCATED,
806};
807
808AVCodecParser h261_parser = {
809 { CODEC_ID_H261 },
810 sizeof(ParseContext),
811 NULL,
812 h261_parse,
813 ff_parse_close,
814};