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