Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h261.c @ 49e5dcbc

History | View | Annotate | Download (22.4 KB)

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->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
    h->gob_start_code_skipped = 0;
136
    
137
    return 0;
138
}
139

    
140
/**
141
 * decodes the group of blocks header or slice header.
142
 * @return <0 if an error occured
143
 */
144
static int h261_decode_gob_header(H261Context *h){
145
    unsigned int val;
146
    MpegEncContext * const s = &h->s;
147
    
148
    if ( !h->gob_start_code_skipped ){
149
        /* Check for GOB Start Code */
150
        val = show_bits(&s->gb, 15);
151
        if(val)
152
            return -1;
153

    
154
        /* We have a GBSC */
155
        skip_bits(&s->gb, 16);
156
    }
157

    
158
    h->gob_start_code_skipped = 0;
159

    
160
    h->gob_number = get_bits(&s->gb, 4); /* GN */
161
    s->qscale = get_bits(&s->gb, 5); /* GQUANT */
162

    
163
    /* Check if gob_number is valid */
164
    if (s->mb_height==18){ //cif
165
        if ((h->gob_number<=0) || (h->gob_number>12))
166
            return -1;
167
    }
168
    else{ //qcif
169
        if ((h->gob_number!=1) && (h->gob_number!=3) && (h->gob_number!=5))
170
            return -1;
171
    }
172

    
173
    /* GEI */
174
    while (get_bits1(&s->gb) != 0) {
175
        skip_bits(&s->gb, 8);
176
    }
177

    
178
    if(s->qscale==0)
179
        return -1;
180

    
181
    // For the first transmitted macroblock in a GOB, MBA is the absolute address. For
182
    // subsequent macroblocks, MBA is the difference between the absolute addresses of
183
    // the macroblock and the last transmitted macroblock.
184
    h->current_mba = 0;
185
    h->mba_diff = 0;
186

    
187
    return 0;
188
}
189

    
190
/**
191
 * decodes the group of blocks / video packet header.
192
 * @return <0 if no resync found
193
 */
194
static int ff_h261_resync(H261Context *h){
195
    MpegEncContext * const s = &h->s;
196
    int left, ret;
197

    
198
    if ( h->gob_start_code_skipped ){
199
        ret= h261_decode_gob_header(h);
200
        if(ret>=0)
201
            return 0;
202
    }
203
    else{
204
        if(show_bits(&s->gb, 15)==0){
205
            ret= h261_decode_gob_header(h);
206
            if(ret>=0)
207
                return 0;
208
        }
209
        //ok, its not where its supposed to be ...
210
        s->gb= s->last_resync_gb;
211
        align_get_bits(&s->gb);
212
        left= s->gb.size_in_bits - get_bits_count(&s->gb);
213

    
214
        for(;left>15+1+4+5; left-=8){
215
            if(show_bits(&s->gb, 15)==0){
216
                GetBitContext bak= s->gb;
217

    
218
                ret= h261_decode_gob_header(h);
219
                if(ret>=0)
220
                    return 0;
221

    
222
                s->gb= bak;
223
            }
224
            skip_bits(&s->gb, 8);
225
        }
226
    }
227

    
228
    return -1;
229
}
230

    
231
/**
232
 * decodes skipped macroblocks
233
 * @return 0
234
 */
235
static int h261_decode_mb_skipped(H261Context *h, int mba1, int mba2 )
236
{
237
    MpegEncContext * const s = &h->s;
238
    int i;
239
    
240
    s->mb_intra = 0;
241

    
242
    for(i=mba1; i<mba2; i++){
243
        int j, xy;
244

    
245
        s->mb_x= ((h->gob_number-1) % 2) * 11 + i % 11;
246
        s->mb_y= ((h->gob_number-1) / 2) * 3 + i / 11;
247
        xy = s->mb_x + s->mb_y * s->mb_stride;
248
        ff_init_block_index(s);
249
        ff_update_block_index(s);
250
        s->dsp.clear_blocks(s->block[0]);
251

    
252
        for(j=0;j<6;j++)
253
            s->block_last_index[j] = -1;
254

    
255
        s->mv_dir = MV_DIR_FORWARD;
256
        s->mv_type = MV_TYPE_16X16;
257
        s->current_picture.mb_type[xy]= MB_TYPE_SKIP | MB_TYPE_16x16 | MB_TYPE_L0;
258
        s->mv[0][0][0] = 0;
259
        s->mv[0][0][1] = 0;
260
        s->mb_skiped = 1;
261

    
262
        MPV_decode_mb(s, s->block);
263
    }
264

    
265
    return 0;
266
}
267

    
268
static int decode_mv_component(GetBitContext *gb, int v){
269
    int mv_diff = get_vlc2(gb, h261_mv_vlc.table, H261_MV_VLC_BITS, 2);
270

    
271
    /* check if mv_diff is valid */
272
    if ( mv_diff < 0 )
273
        return v;
274

    
275
    mv_diff = mvmap[mv_diff];
276

    
277
    if(mv_diff && !get_bits1(gb))
278
        mv_diff= -mv_diff;
279
    
280
    v += mv_diff;
281
    if     (v <=-16) v+= 32;
282
    else if(v >= 16) v-= 32;
283

    
284
    return v;
285
}
286

    
287
static int h261_decode_mb(H261Context *h){
288
    MpegEncContext * const s = &h->s;
289
    int i, cbp, xy, old_mtype;
290

    
291
    cbp = 63;
292
    // Read mba
293
    do{
294
        h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2);
295

    
296
        /* Check for slice end */
297
        /* NOTE: GOB can be empty (no MB data) or exist only of MBA_stuffing */
298
        if (h->mba_diff == MBA_STARTCODE){ // start code
299
            h->gob_start_code_skipped = 1;
300
            return SLICE_END;
301
        }
302
    }
303
    while( h->mba_diff == MBA_STUFFING ); // stuffing
304

    
305
    if ( h->mba_diff < 0 ){
306
        if ( get_bits_count(&s->gb) + 7 >= s->gb.size_in_bits )
307
            return SLICE_END;
308

    
309
        av_log(s->avctx, AV_LOG_ERROR, "illegal mba at %d %d\n", s->mb_x, s->mb_y);
310
        return SLICE_ERROR;
311
    }
312

    
313
    h->mba_diff += 1;
314
    h->current_mba += h->mba_diff;
315

    
316
    if ( h->current_mba > MBA_STUFFING )
317
        return SLICE_ERROR;
318
    
319
    s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
320
    s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
321
    xy = s->mb_x + s->mb_y * s->mb_stride;
322
    ff_init_block_index(s);
323
    ff_update_block_index(s);
324
    s->dsp.clear_blocks(s->block[0]);
325

    
326
    // Read mtype
327
    old_mtype = h->mtype;
328
    h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
329
    h->mtype = h261_mtype_map[h->mtype];
330

    
331
    // Read mquant
332
    if ( IS_QUANT ( h->mtype ) ){
333
        ff_set_qscale(s, get_bits(&s->gb, 5));
334
    }
335

    
336
    s->mb_intra = IS_INTRA4x4(h->mtype);
337

    
338
    // Read mv
339
    if ( IS_16X16 ( h->mtype ) ){
340
        // Motion vector data is included for all MC macroblocks. MVD is obtained from the macroblock vector by subtracting the
341
        // vector of the preceding macroblock. For this calculation the vector of the preceding macroblock is regarded as zero in the
342
        // following three situations:
343
        // 1) evaluating MVD for macroblocks 1, 12 and 23;
344
        // 2) evaluating MVD for macroblocks in which MBA does not represent a difference of 1;
345
        // 3) MTYPE of the previous macroblock was not MC.
346
        if ( ( h->current_mba == 1 ) || ( h->current_mba == 12 ) || ( h->current_mba == 23 ) ||
347
             ( h->mba_diff != 1) || ( !IS_16X16 ( old_mtype ) ))
348
        {
349
            h->current_mv_x = 0;
350
            h->current_mv_y = 0;
351
        }
352

    
353
        h->current_mv_x= decode_mv_component(&s->gb, h->current_mv_x);
354
        h->current_mv_y= decode_mv_component(&s->gb, h->current_mv_y);
355
    }
356

    
357
    // Read cbp
358
    if ( HAS_CBP( h->mtype ) ){
359
        cbp = get_vlc2(&s->gb, h261_cbp_vlc.table, H261_CBP_VLC_BITS, 2) + 1;
360
    }
361

    
362
    if(s->mb_intra){
363
        s->current_picture.mb_type[xy]= MB_TYPE_INTRA;
364
        goto intra;
365
    }
366

    
367
    //set motion vectors
368
    s->mv_dir = MV_DIR_FORWARD;
369
    s->mv_type = MV_TYPE_16X16;
370
    s->current_picture.mb_type[xy]= MB_TYPE_16x16 | MB_TYPE_L0;
371
    if(IS_16X16 ( h->mtype )){
372
        s->mv[0][0][0] = h->current_mv_x * 2;//gets divided by 2 in motion compensation
373
        s->mv[0][0][1] = h->current_mv_y * 2;
374
    }
375
    else{
376
        h->current_mv_x = s->mv[0][0][0] = 0;
377
        h->current_mv_x = s->mv[0][0][1] = 0;
378
    }
379

    
380
intra:
381
    /* decode each block */
382
    if(s->mb_intra || HAS_CBP(h->mtype)){
383
        for (i = 0; i < 6; i++) {
384
            if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
385
                return SLICE_ERROR;
386
            }
387
            cbp+=cbp;
388
        }
389
    }
390

    
391
    MPV_decode_mb(s, s->block);
392

    
393
    if(IS_FIL (h->mtype)){
394
        ff_h261_loop_filter(h);
395
    }
396

    
397
    return SLICE_OK;
398
}
399

    
400
/**
401
 * decodes a macroblock
402
 * @return <0 if an error occured
403
 */
404
static int h261_decode_block(H261Context * h, DCTELEM * block,
405
                             int n, int coded)
406
{
407
    MpegEncContext * const s = &h->s;
408
    int code, level, i, j, run;
409
    RLTable *rl = &h261_rl_tcoeff;
410
    const uint8_t *scan_table;
411
    
412
    // For the variable length encoding there are two code tables, one being used for
413
    // the first transmitted LEVEL in INTER, INTER+MC and INTER+MC+FIL blocks, the second
414
    // for all other LEVELs except the first one in INTRA blocks which is fixed length
415
    // coded with 8 bits.
416
    // NOTE: the two code tables only differ in one VLC so we handle that manually.
417
    scan_table = s->intra_scantable.permutated;
418
    if (s->mb_intra){
419
        /* DC coef */
420
        level = get_bits(&s->gb, 8);
421
        // 0 (00000000b) and -128 (10000000b) are FORBIDDEN
422
        if((level&0x7F) == 0){
423
            av_log(s->avctx, AV_LOG_ERROR, "illegal dc %d at %d %d\n", level, s->mb_x, s->mb_y);
424
            return -1;
425
        }
426
        // The code 1000 0000 is not used, the reconstruction level of 1024 being coded as 1111 1111.
427
        if (level == 255)
428
            level = 128;
429
        block[0] = level;
430
        i = 1;
431
    }else if(coded){
432
        // Run  Level   Code
433
        // EOB                  Not possible for first level when cbp is available (that's why the table is different)
434
        // 0    1               1s
435
        // *    *               0*
436
        int check = show_bits(&s->gb, 2);
437
        i = 0;
438
        if ( check & 0x2 ){
439
            skip_bits(&s->gb, 2);
440
            block[0] = ( check & 0x1 ) ? -1 : 1;
441
            i = 1;
442
        }
443
    }else{
444
        i = 0;
445
    }
446
    if(!coded){
447
        s->block_last_index[n] = i - 1;
448
        return 0;
449
    }
450
    for(;;){
451
        code = get_vlc2(&s->gb, rl->vlc.table, TCOEFF_VLC_BITS, 2);
452
        if (code < 0){
453
            av_log(s->avctx, AV_LOG_ERROR, "illegal ac vlc code at %dx%d\n", s->mb_x, s->mb_y);
454
            return -1;
455
        }
456
        if (code == rl->n) {
457
            /* escape */
458
            // 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.
459
            run = get_bits(&s->gb, 6);
460
            level = (int8_t)get_bits(&s->gb, 8);
461
        }else if(code == 0){
462
            break;
463
        }else{
464
            run = rl->table_run[code];
465
            level = rl->table_level[code];
466
            if (get_bits1(&s->gb))
467
                level = -level;
468
        }
469
        i += run;
470
        if (i >= 64){
471
            av_log(s->avctx, AV_LOG_ERROR, "run overflow at %dx%d\n", s->mb_x, s->mb_y);
472
            return -1;
473
        }
474
        j = scan_table[i];
475
        block[j] = level;
476
        i++;
477
    }
478
    s->block_last_index[n] = i-1;
479
    return 0;
480
}
481

    
482
/**
483
 * decodes the H261 picture header.
484
 * @return <0 if no startcode found
485
 */
486
int h261_decode_picture_header(H261Context *h){
487
    MpegEncContext * const s = &h->s;
488
    int format, i;
489
    uint32_t startcode;
490
    align_get_bits(&s->gb);
491

    
492
    startcode = (h->last_bits << (12 - (8-h->bits_left))) | get_bits(&s->gb, 20-8 - (8- h->bits_left));
493

    
494
    for(i= s->gb.size_in_bits - get_bits_count(&s->gb); i>24; i-=1){
495
        startcode = ((startcode << 1) | get_bits(&s->gb, 1)) & 0x000FFFFF;
496

    
497
        if(startcode == 0x10)
498
            break;
499
    }
500

    
501
    if (startcode != 0x10){
502
        av_log(s->avctx, AV_LOG_ERROR, "Bad picture start code\n");
503
        return -1;
504
    }
505

    
506
    /* temporal reference */
507
    s->picture_number = get_bits(&s->gb, 5); /* picture timestamp */
508

    
509
    /* PTYPE starts here */
510
    skip_bits1(&s->gb); /* split screen off */
511
    skip_bits1(&s->gb); /* camera  off */
512
    skip_bits1(&s->gb); /* freeze picture release off */
513

    
514
    format = get_bits1(&s->gb);
515

    
516
    //only 2 formats possible
517
    if (format == 0){//QCIF
518
        s->width = 176;
519
        s->height = 144;
520
        s->mb_width = 11;
521
        s->mb_height = 9;
522
    }else{//CIF
523
        s->width = 352;
524
        s->height = 288;
525
        s->mb_width = 22;
526
        s->mb_height = 18;
527
    }
528

    
529
    s->mb_num = s->mb_width * s->mb_height;
530

    
531
    skip_bits1(&s->gb); /* still image mode off */
532
    skip_bits1(&s->gb); /* Reserved */
533

    
534
    /* PEI */
535
    while (get_bits1(&s->gb) != 0){
536
        skip_bits(&s->gb, 8);
537
    }
538

    
539
    // h261 has no I-FRAMES, but if we pass I_TYPE for the first frame, the codec crashes if it does 
540
    // not contain all I-blocks (e.g. when a packet is lost)
541
    s->pict_type = P_TYPE;
542

    
543
    h->gob_number = 0;
544
    return 0;
545
}
546

    
547
static int h261_decode_gob(H261Context *h){
548
    MpegEncContext * const s = &h->s;
549
    
550
    ff_set_qscale(s, s->qscale);
551

    
552
    /* decode mb's */
553
    while(h->current_mba <= MBA_STUFFING)
554
    {
555
        int ret;
556
        /* DCT & quantize */
557
        ret= h261_decode_mb(h);
558
        if(ret<0){
559
            if(ret==SLICE_END){
560
                h261_decode_mb_skipped(h, h->current_mba, 33);                
561
                return 0;
562
            }
563
            av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
564
            return -1;
565
        }
566
        
567
        h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
568
    }
569
    
570
    return -1;
571
}
572

    
573
static int h261_find_frame_end(ParseContext *pc, AVCodecContext* avctx, const uint8_t *buf, int buf_size){
574
    int vop_found, i, j, bits_left, last_bits;
575
    uint32_t state;
576

    
577
    H261Context *h = avctx->priv_data;
578

    
579
    if(h){
580
        bits_left = h->bits_left;
581
        last_bits = h->last_bits;
582
    }
583
    else{
584
        bits_left = 0;
585
        last_bits = 0;
586
    }
587

    
588
    vop_found= pc->frame_start_found;
589
    state= pc->state;
590
    if(bits_left!=0 && !vop_found)
591
        state = state << (8-bits_left) | last_bits;
592
    i=0;
593
    if(!vop_found){
594
        for(i=0; i<buf_size; i++){
595
            state= (state<<8) | buf[i];
596
            for(j=0; j<8; j++){
597
                if(( (  (state<<j)  |  (buf[i]>>(8-j))  )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
598
                    i++;
599
                    vop_found=1;
600
                    break;
601
                }
602
            }
603
            if(vop_found)
604
                    break;    
605
        }
606
    }
607
    if(vop_found){
608
        for(; i<buf_size; i++){
609
            if(avctx->flags & CODEC_FLAG_TRUNCATED)//XXX ffplay workaround, someone a better solution?
610
                state= (state<<8) | buf[i];
611
            for(j=0; j<8; j++){
612
                if(( (  (state<<j)  |  (buf[i]>>(8-j))  )>>(32-20) == 0x10 )&&(((state >> (17-j)) & 0x4000) == 0x0)){
613
                    pc->frame_start_found=0;
614
                    pc->state=-1;
615
                    return i-3;
616
                }
617
            }
618
        }
619
    }
620

    
621
    pc->frame_start_found= vop_found;
622
    pc->state= state;
623
    return END_NOT_FOUND;
624
}
625

    
626
static int h261_parse(AVCodecParserContext *s,
627
                      AVCodecContext *avctx,
628
                      uint8_t **poutbuf, int *poutbuf_size, 
629
                      const uint8_t *buf, int buf_size)
630
{
631
    ParseContext *pc = s->priv_data;
632
    int next;
633
    
634
    next= h261_find_frame_end(pc,avctx, buf, buf_size);
635
    if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
636
        *poutbuf = NULL;
637
        *poutbuf_size = 0;
638
        return buf_size;
639
    }
640
    *poutbuf = (uint8_t *)buf;
641
    *poutbuf_size = buf_size;
642
    return next;
643
}
644

    
645
/**
646
 * returns the number of bytes consumed for building the current frame
647
 */
648
static int get_consumed_bytes(MpegEncContext *s, int buf_size){
649
    int pos= (get_bits_count(&s->gb)+7)>>3;
650

    
651
    if(s->flags&CODEC_FLAG_TRUNCATED){
652
        pos -= s->parse_context.last_index;
653
        if(pos<0) pos=0;// padding is not really read so this might be -1
654
        return pos;
655
    }else{
656
        if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
657
        if(pos+10>buf_size) pos=buf_size; // oops ;)
658

    
659
        return pos;
660
    }
661
}
662

    
663
static int h261_decode_frame(AVCodecContext *avctx,
664
                             void *data, int *data_size,
665
                             uint8_t *buf, int buf_size)
666
{
667
    H261Context *h= avctx->priv_data;
668
    MpegEncContext *s = &h->s;
669
    int ret;
670
    AVFrame *pict = data;
671

    
672
#ifdef DEBUG
673
    printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
674
    printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
675
#endif
676
    s->flags= avctx->flags;
677
    s->flags2= avctx->flags2;
678

    
679
    /* no supplementary picture */
680
    if (buf_size == 0) {
681

    
682
        return 0;
683
    }
684

    
685
    if(s->flags&CODEC_FLAG_TRUNCATED){
686
        int next;
687

    
688
        next= h261_find_frame_end(&s->parse_context,avctx, buf, buf_size);
689

    
690
        if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
691
            return buf_size;
692
    }
693

    
694

    
695
retry:
696

    
697
    init_get_bits(&s->gb, buf, buf_size*8);
698

    
699
    if(!s->context_initialized){
700
        if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
701
            return -1;
702
    }
703

    
704
    //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
705
    if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
706
        int i= ff_find_unused_picture(s, 0);
707
        s->current_picture_ptr= &s->picture[i];
708
    }
709

    
710
    ret = h261_decode_picture_header(h);
711

    
712
    /* skip if the header was thrashed */
713
    if (ret < 0){
714
        av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
715
        return -1;
716
    }
717

    
718
    if (s->width != avctx->width || s->height != avctx->height){
719
        ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
720
        s->parse_context.buffer=0;
721
        MPV_common_end(s);
722
        s->parse_context= pc;
723
    }
724
    if (!s->context_initialized) {
725
        avctx->width = s->width;
726
        avctx->height = s->height;
727

    
728
        goto retry;
729
    }
730

    
731
    // for hurry_up==5
732
    s->current_picture.pict_type= s->pict_type;
733
    s->current_picture.key_frame= s->pict_type == I_TYPE;
734

    
735
    /* skip everything if we are in a hurry>=5 */
736
    if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
737

    
738
    if(MPV_frame_start(s, avctx) < 0)
739
        return -1;
740

    
741
    ff_er_frame_start(s);
742

    
743
    /* decode each macroblock */
744
    s->mb_x=0;
745
    s->mb_y=0;
746

    
747
    while(h->gob_number < (s->mb_height==18 ? 12 : 5)){
748
        if(ff_h261_resync(h)<0)
749
            break;
750
        h261_decode_gob(h);
751
    }
752
    MPV_frame_end(s);
753

    
754
    // h261 doesn't have byte aligned codes
755
    // store the bits of the next frame that are left in the last byte
756
    // in the H261Context and remember the number of stored bits
757
    {
758
        int bitsleft;
759
        int current_pos= get_bits_count(&s->gb)>>3;
760
        bitsleft =  (current_pos<<3) - get_bits_count(&s->gb);
761
        h->bits_left = - bitsleft;
762
        if(bitsleft > 0)
763
            h->last_bits= get_bits(&s->gb, 8 - h->bits_left);
764
        else
765
            h->last_bits = 0;
766
    }
767

    
768
assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
769
assert(s->current_picture.pict_type == s->pict_type);
770
    *pict= *(AVFrame*)&s->current_picture;
771
    ff_print_debug_info(s, pict);
772

    
773
    /* Return the Picture timestamp as the frame number */
774
    /* we substract 1 because it is added on utils.c    */
775
    avctx->frame_number = s->picture_number - 1;
776

    
777
    *data_size = sizeof(AVFrame);
778

    
779
    return get_consumed_bytes(s, buf_size);
780
}
781

    
782
static int h261_decode_end(AVCodecContext *avctx)
783
{
784
    H261Context *h= avctx->priv_data;
785
    MpegEncContext *s = &h->s;
786

    
787
    MPV_common_end(s);
788
    return 0;
789
}
790

    
791
AVCodec h261_decoder = {
792
    "h261",
793
    CODEC_TYPE_VIDEO,
794
    CODEC_ID_H261,
795
    sizeof(H261Context),
796
    h261_decode_init,
797
    NULL,
798
    h261_decode_end,
799
    h261_decode_frame,
800
    CODEC_CAP_TRUNCATED,
801
};
802

    
803
AVCodecParser h261_parser = {
804
    { CODEC_ID_H261 },
805
    sizeof(ParseContext),
806
    NULL,
807
    h261_parse,
808
    ff_parse_close,
809
};