YUV420P to YUV422 conversion routine by (Danny Laarmans <dlaarmans at androme dot...
[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
49e5dcbc
MD
39#define MBA_STUFFING 33
40#define MBA_STARTCODE 34
c6148de2
MN
41#define IS_FIL(a) ((a)&MB_TYPE_H261_FIL)
42
43/**
44 * H261Context
45 */
46typedef 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;
c6148de2
MN
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
49e5dcbc 57 int gob_start_code_skipped; // 1 if gob start code is already read before gob header is read
c6148de2
MN
58}H261Context;
59
5f6c92d4
MN
60void ff_h261_loop_filter(MpegEncContext *s){
61 H261Context * h= (H261Context*)s;
c6148de2
MN
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];
5f6c92d4
MN
67
68 if(!(IS_FIL (h->mtype)))
69 return;
70
fdbbf2e0
MN
71 s->dsp.h261_loop_filter(dest_y , linesize);
72 s->dsp.h261_loop_filter(dest_y + 8, linesize);
73 s->dsp.h261_loop_filter(dest_y + 8 * linesize , linesize);
74 s->dsp.h261_loop_filter(dest_y + 8 * linesize + 8, linesize);
75 s->dsp.h261_loop_filter(dest_cb, uvlinesize);
76 s->dsp.h261_loop_filter(dest_cr, uvlinesize);
c6148de2
MN
77}
78
79static int h261_decode_block(H261Context *h, DCTELEM *block,
80 int n, int coded);
49e5dcbc 81static int h261_decode_mb(H261Context *h);
c6148de2
MN
82void ff_set_qscale(MpegEncContext * s, int qscale);
83
84/***********************************************/
85/* decoding */
86
87static VLC h261_mba_vlc;
88static VLC h261_mtype_vlc;
89static VLC h261_mv_vlc;
90static VLC h261_cbp_vlc;
91
92void init_vlc_rl(RLTable *rl);
93
94static void h261_decode_init_vlc(H261Context *h){
95 static int done = 0;
96
97 if(!done){
98 done = 1;
49e5dcbc 99 init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
c6148de2
MN
100 h261_mba_bits, 1, 1,
101 h261_mba_code, 1, 1);
102 init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
103 h261_mtype_bits, 1, 1,
104 h261_mtype_code, 1, 1);
105 init_vlc(&h261_mv_vlc, H261_MV_VLC_BITS, 17,
106 &h261_mv_tab[0][1], 2, 1,
107 &h261_mv_tab[0][0], 2, 1);
108 init_vlc(&h261_cbp_vlc, H261_CBP_VLC_BITS, 63,
109 &h261_cbp_tab[0][1], 2, 1,
110 &h261_cbp_tab[0][0], 2, 1);
111 init_rl(&h261_rl_tcoeff);
112 init_vlc_rl(&h261_rl_tcoeff);
113 }
114}
115
116static int h261_decode_init(AVCodecContext *avctx){
117 H261Context *h= avctx->priv_data;
118 MpegEncContext * const s = &h->s;
119
120 // set defaults
121 MPV_decode_defaults(s);
122 s->avctx = avctx;
123
21adafec
MN
124 s->width = s->avctx->coded_width;
125 s->height = s->avctx->coded_height;
c6148de2
MN
126 s->codec_id = s->avctx->codec->id;
127
128 s->out_format = FMT_H261;
129 s->low_delay= 1;
130 avctx->pix_fmt= PIX_FMT_YUV420P;
131
132 s->codec_id= avctx->codec->id;
133
134 h261_decode_init_vlc(h);
135
49e5dcbc 136 h->gob_start_code_skipped = 0;
c6148de2
MN
137
138 return 0;
139}
140
141/**
142 * decodes the group of blocks header or slice header.
143 * @return <0 if an error occured
144 */
145static int h261_decode_gob_header(H261Context *h){
146 unsigned int val;
147 MpegEncContext * const s = &h->s;
148
49e5dcbc
MD
149 if ( !h->gob_start_code_skipped ){
150 /* Check for GOB Start Code */
151 val = show_bits(&s->gb, 15);
152 if(val)
153 return -1;
154
155 /* We have a GBSC */
156 skip_bits(&s->gb, 16);
157 }
c6148de2 158
49e5dcbc 159 h->gob_start_code_skipped = 0;
c6148de2
MN
160
161 h->gob_number = get_bits(&s->gb, 4); /* GN */
162 s->qscale = get_bits(&s->gb, 5); /* GQUANT */
163
49e5dcbc
MD
164 /* Check if gob_number is valid */
165 if (s->mb_height==18){ //cif
166 if ((h->gob_number<=0) || (h->gob_number>12))
167 return -1;
168 }
169 else{ //qcif
170 if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
171 return -1;
172 }
173
c6148de2
MN
174 /* GEI */
175 while (get_bits1(&s->gb) != 0) {
176 skip_bits(&s->gb, 8);
177 }
178
179 if(s->qscale==0)
180 return -1;
181
182 // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
183 // subsequent macroblocks, MBA is the difference between the absolute addresses of
184 // the macroblock and the last transmitted macroblock.
185 h->current_mba = 0;
186 h->mba_diff = 0;
187
188 return 0;
189}
190
191/**
192 * decodes the group of blocks / video packet header.
193 * @return <0 if no resync found
194 */
195static int ff_h261_resync(H261Context *h){
196 MpegEncContext * const s = &h->s;
197 int left, ret;
198
49e5dcbc 199 if ( h->gob_start_code_skipped ){
c6148de2
MN
200 ret= h261_decode_gob_header(h);
201 if(ret>=0)
202 return 0;
203 }
49e5dcbc 204 else{
c6148de2 205 if(show_bits(&s->gb, 15)==0){
c6148de2
MN
206 ret= h261_decode_gob_header(h);
207 if(ret>=0)
208 return 0;
49e5dcbc
MD
209 }
210 //ok, its not where its supposed to be ...
211 s->gb= s->last_resync_gb;
212 align_get_bits(&s->gb);
213 left= s->gb.size_in_bits - get_bits_count(&s->gb);
214
215 for(;left>15+1+4+5; left-=8){
216 if(show_bits(&s->gb, 15)==0){
217 GetBitContext bak= s->gb;
c6148de2 218
49e5dcbc
MD
219 ret= h261_decode_gob_header(h);
220 if(ret>=0)
221 return 0;
222
223 s->gb= bak;
224 }
225 skip_bits(&s->gb, 8);
c6148de2 226 }
c6148de2
MN
227 }
228
229 return -1;
230}
231
232/**
7344c87a 233 * decodes skipped macroblocks
c6148de2
MN
234 * @return 0
235 */
7344c87a 236static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
c6148de2
MN
237{
238 MpegEncContext * const s = &h->s;
239 int i;
7344c87a 240
c6148de2 241 s->mb_intra = 0;
7344c87a
CF
242
243 for(i=mba1; i<mba2; i++){
244 int j, xy;
245
246 s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
247 s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
248 xy = s->mb_x + s->mb_y * s->mb_stride;
249 ff_init_block_index(s);
250 ff_update_block_index(s);
251 s->dsp.clear_blocks(s->block[0]);
252
253 for(j=0;j<6;j++)
254 s->block_last_index[j] = -1;
255
256 s->mv_dir = MV_DIR_FORWARD;
257 s->mv_type = MV_TYPE_16X16;
258 s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
259 s->mv[0][0][0] = 0;
260 s->mv[0][0][1] = 0;
261 s->mb_skiped = 1;
5f6c92d4 262 h->mtype &= ~MB_TYPE_H261_FIL;
7344c87a
CF
263
264 MPV_decode_mb(s, s->block);
265 }
266
c6148de2
MN
267 return 0;
268}
269
b37e98c8
MN
270static int decode_mv_component(GetBitContext *gb, int v){
271 int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
49e5dcbc
MD
272
273 /* check if mv_diff is valid */
274 if ( mv_diff < 0 )
275 return v;
276
b37e98c8
MN
277 mv_diff = mvmap[mv_diff];
278
279 if(mv_diff && !get_bits1(gb))
280 mv_diff= -mv_diff;
281
282 v += mv_diff;
283 if (v <=-16) v+= 32;
284 else if(v >= 16) v-= 32;
285
286 return v;
287}
288
49e5dcbc 289static int h261_decode_mb(H261Context *h){
c6148de2 290 MpegEncContext * const s = &h->s;
6bc79819 291 int i, cbp, xy, old_mtype;
c6148de2
MN
292
293 cbp = 63;
294 // Read mba
295 do{
49e5dcbc
MD
296 h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
297
298 /* Check for slice end */
299 /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
300 if (h->mba_diff == MBA_STARTCODE){ // start code
301 h->gob_start_code_skipped = 1;
302 return SLICE_END;
303 }
c6148de2 304 }
49e5dcbc 305 while( h->mba_diff == MBA_STUFFING ); // stuffing
c6148de2 306
49e5dcbc
MD
307 if ( h->mba_diff < 0 ){
308 if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
309 return SLICE_END;
310
311 av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
312 return SLICE_ERROR;
313 }
c6148de2 314
49e5dcbc 315 h->mba_diff += 1;
c6148de2
MN
316 h->current_mba += h->mba_diff;
317
49e5dcbc
MD
318 if ( h->current_mba > MBA_STUFFING )
319 return SLICE_ERROR;
c6148de2
MN
320
321 s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
322 s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
c6148de2 323 xy = s->mb_x + s->mb_y * s->mb_stride;
c6148de2
MN
324 ff_init_block_index(s);
325 ff_update_block_index(s);
7344c87a 326 s->dsp.clear_blocks(s->block[0]);
c6148de2
MN
327
328 // Read mtype
6bc79819 329 old_mtype = h->mtype;
c6148de2
MN
330 h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
331 h->mtype = h261_mtype_map[h->mtype];
332
c6148de2
MN
333 // Read mquant
334 if ( IS_QUANT ( h->mtype ) ){
7344c87a 335 ff_set_qscale(s, get_bits(&s->gb, 5));
c6148de2
MN
336 }
337
338 s->mb_intra = IS_INTRA4x4(h->mtype);
339
340 // Read mv
341 if ( IS_16X16 ( h->mtype ) ){
342 // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
343 // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
344 // following three situations:
345 // 1) evaluating MVD for macroblocks 1, 12 and 23;
346 // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
347 // 3) MTYPE of the previous macroblock was not MC.
348 if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
349 ( h->mba_diff != 1) || ( !IS_16X16 ( old_mtype ) ))
350 {
351 h->current_mv_x = 0;
352 h->current_mv_y = 0;
353 }
354
b37e98c8
MN
355 h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
356 h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
c6148de2
MN
357 }
358
359 // Read cbp
360 if ( HAS_CBP( h->mtype ) ){
361 cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
362 }
363
364 if(s->mb_intra){
365 s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
366 goto intra;
367 }
368
369 //set motion vectors
370 s->mv_dir = MV_DIR_FORWARD;
371 s->mv_type = MV_TYPE_16X16;
372 s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
373 if(IS_16X16 ( h->mtype )){
374 s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
375 s->mv[0][0][1] = h->current_mv_y * 2;
376 }
377 else{
378 h->current_mv_x = s->mv[0][0][0] = 0;
379 h->current_mv_x = s->mv[0][0][1] = 0;
380 }
381
382intra:
383 /* decode each block */
384 if(s->mb_intra || HAS_CBP(h->mtype)){
385 for (i = 0; i < 6; i++) {
49e5dcbc
MD
386 if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
387 return SLICE_ERROR;
c6148de2
MN
388 }
389 cbp+=cbp;
390 }
391 }
392
49e5dcbc 393 MPV_decode_mb(s, s->block);
c6148de2 394
c6148de2
MN
395 return SLICE_OK;
396}
397
398/**
399 * decodes a macroblock
400 * @return <0 if an error occured
401 */
402static 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 }
7344c87a 476 s->block_last_index[n] = i-1;
c6148de2
MN
477 return 0;
478}
479
480/**
481 * decodes the H261 picture header.
482 * @return <0 if no startcode found
483 */
484int h261_decode_picture_header(H261Context *h){
485 MpegEncContext * const s = &h->s;
486 int format, i;
8c7aaf5b 487 uint32_t startcode= 0;
c6148de2
MN
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
49e5dcbc
MD
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;
c6148de2
MN
537
538 h->gob_number = 0;
539 return 0;
540}
541
542static int h261_decode_gob(H261Context *h){
543 MpegEncContext * const s = &h->s;
c6148de2
MN
544
545 ff_set_qscale(s, s->qscale);
7344c87a 546
7344c87a 547 /* decode mb's */
49e5dcbc 548 while(h->current_mba <= MBA_STUFFING)
c6148de2
MN
549 {
550 int ret;
551 /* DCT & quantize */
49e5dcbc 552 ret= h261_decode_mb(h);
c6148de2 553 if(ret<0){
c6148de2 554 if(ret==SLICE_END){
7344c87a 555 h261_decode_mb_skipped(h, h->current_mba, 33);
c6148de2 556 return 0;
c6148de2 557 }
49e5dcbc 558 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
c6148de2
MN
559 return -1;
560 }
49e5dcbc 561
7344c87a 562 h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
c6148de2
MN
563 }
564
565 return -1;
566}
567
568static 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
621static 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 */
643static int get_consumed_bytes(MpegEncContext *s, int buf_size){
c6148de2 644 if(s->flags&CODEC_FLAG_TRUNCATED){
8c7aaf5b 645 int pos= (get_bits_count(&s->gb)+7)>>3;
c6148de2
MN
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{
8c7aaf5b 650 int pos= get_bits_count(&s->gb)>>3;
c6148de2
MN
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
658static 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
c6148de2
MN
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) {
c6148de2
MN
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
689retry:
690
b658ecfe 691 init_get_bits(&s->gb, buf, buf_size*8);
c6148de2
MN
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
c6148de2
MN
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
21adafec 712 if (s->width != avctx->coded_width || s->height != avctx->coded_height){
c6148de2
MN
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) {
21adafec 719 avcodec_set_dimensions(avctx, s->width, s->height);
c6148de2
MN
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
c6148de2
MN
747assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
748assert(s->current_picture.pict_type == s->pict_type);
b658ecfe
MN
749 *pict= *(AVFrame*)&s->current_picture;
750 ff_print_debug_info(s, pict);
c6148de2
MN
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
b658ecfe 756 *data_size = sizeof(AVFrame);
c6148de2 757
b658ecfe 758 return get_consumed_bytes(s, buf_size);
c6148de2
MN
759}
760
761static 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
770AVCodec 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
782AVCodecParser h261_parser = {
783 { CODEC_ID_H261 },
784 sizeof(ParseContext),
785 NULL,
786 h261_parse,
787 ff_parse_close,
788};