Revision 49e5dcbc libavcodec/h261.c

View differences:

libavcodec/h261.c
36 36
#define H261_CBP_VLC_BITS 9
37 37
#define TCOEFF_VLC_BITS 9
38 38

  
39
#define MAX_MBA 33
39
#define MBA_STUFFING 33
40
#define MBA_STARTCODE 34
40 41
#define IS_FIL(a)    ((a)&MB_TYPE_H261_FIL)
41 42

  
42 43
/**
......
51 52
    int current_mv_x;
52 53
    int current_mv_y;
53 54
    int gob_number;
54
    int loop_filter;
55 55
    int bits_left; //8 - nr of bits left of the following frame in the last byte in this frame
56 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
57 58
}H261Context;
58 59

  
59 60
void ff_h261_loop_filter(H261Context * h){
......
74 75

  
75 76
static int h261_decode_block(H261Context *h, DCTELEM *block,
76 77
                             int n, int coded);
77
static int h261_decode_mb(H261Context *h,
78
                      DCTELEM block[6][64]);
78
static int h261_decode_mb(H261Context *h);
79 79
void ff_set_qscale(MpegEncContext * s, int qscale);
80 80

  
81 81
/***********************************************/
......
93 93

  
94 94
    if(!done){
95 95
        done = 1;
96
        init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 34,
96
        init_vlc(&h261_mba_vlc, H261_MBA_VLC_BITS, 35,
97 97
                 h261_mba_bits, 1, 1,
98 98
                 h261_mba_code, 1, 1);
99 99
        init_vlc(&h261_mtype_vlc, H261_MTYPE_VLC_BITS, 10,
......
132 132

  
133 133
    h->bits_left = 0;
134 134
    h->last_bits = 0;
135
    h->gob_start_code_skipped = 0;
135 136
    
136 137
    return 0;
137 138
}
......
144 145
    unsigned int val;
145 146
    MpegEncContext * const s = &h->s;
146 147
    
147
    /* Check for GOB Start Code */
148
    val = show_bits(&s->gb, 15);
149
    if(val)
150
        return -1;
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
    }
151 157

  
152
    /* We have a GBSC */
153
    skip_bits(&s->gb, 16);
158
    h->gob_start_code_skipped = 0;
154 159

  
155 160
    h->gob_number = get_bits(&s->gb, 4); /* GN */
156 161
    s->qscale = get_bits(&s->gb, 5); /* GQUANT */
157 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

  
158 173
    /* GEI */
159 174
    while (get_bits1(&s->gb) != 0) {
160 175
        skip_bits(&s->gb, 8);
......
180 195
    MpegEncContext * const s = &h->s;
181 196
    int left, ret;
182 197

  
183
    if(show_bits(&s->gb, 15)==0){
198
    if ( h->gob_start_code_skipped ){
184 199
        ret= h261_decode_gob_header(h);
185 200
        if(ret>=0)
186 201
            return 0;
187 202
    }
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){
203
    else{
194 204
        if(show_bits(&s->gb, 15)==0){
195
            GetBitContext bak= s->gb;
196

  
197 205
            ret= h261_decode_gob_header(h);
198 206
            if(ret>=0)
199 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;
200 221

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

  
206 228
    return -1;
......
245 267

  
246 268
static int decode_mv_component(GetBitContext *gb, int v){
247 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

  
248 275
    mv_diff = mvmap[mv_diff];
249 276

  
250 277
    if(mv_diff && !get_bits1(gb))
......
257 284
    return v;
258 285
}
259 286

  
260
static int h261_decode_mb(H261Context *h,
261
                          DCTELEM block[6][64])
262
{
287
static int h261_decode_mb(H261Context *h){
263 288
    MpegEncContext * const s = &h->s;
264 289
    int i, cbp, xy, old_mtype;
265 290

  
266 291
    cbp = 63;
267 292
    // Read mba
268 293
    do{
269
        h->mba_diff = get_vlc2(&s->gb, h261_mba_vlc.table, H261_MBA_VLC_BITS, 2)+1;
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
        }
270 302
    }
271
    while( h->mba_diff == MAX_MBA + 1 ); // stuffing
303
    while( h->mba_diff == MBA_STUFFING ); // stuffing
272 304

  
273
    if ( h->mba_diff < 0 )
274
        return -1;
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
    }
275 312

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

  
278
    if ( h->current_mba > MAX_MBA )
279
        return -1;
316
    if ( h->current_mba > MBA_STUFFING )
317
        return SLICE_ERROR;
280 318
    
281 319
    s->mb_x= ((h->gob_number-1) % 2) * 11 + ((h->current_mba-1) % 11);
282 320
    s->mb_y= ((h->gob_number-1) / 2) * 3 + ((h->current_mba-1) / 11);
283

  
284 321
    xy = s->mb_x + s->mb_y * s->mb_stride;
285

  
286 322
    ff_init_block_index(s);
287 323
    ff_update_block_index(s);
288 324
    s->dsp.clear_blocks(s->block[0]);
......
292 328
    h->mtype = get_vlc2(&s->gb, h261_mtype_vlc.table, H261_MTYPE_VLC_BITS, 2);
293 329
    h->mtype = h261_mtype_map[h->mtype];
294 330

  
295
    if (IS_FIL (h->mtype))
296
        h->loop_filter = 1;
297

  
298 331
    // Read mquant
299 332
    if ( IS_QUANT ( h->mtype ) ){
300 333
        ff_set_qscale(s, get_bits(&s->gb, 5));
......
348 381
    /* decode each block */
349 382
    if(s->mb_intra || HAS_CBP(h->mtype)){
350 383
        for (i = 0; i < 6; i++) {
351
            if (h261_decode_block(h, block[i], i, cbp&32) < 0){
352
                return -1;
384
            if (h261_decode_block(h, s->block[i], i, cbp&32) < 0){
385
                return SLICE_ERROR;
353 386
            }
354 387
            cbp+=cbp;
355 388
        }
356 389
    }
357 390

  
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
        }
391
    MPV_decode_mb(s, s->block);
365 392

  
366
        if(v==0){
367
            return SLICE_END;
368
        }
393
    if(IS_FIL (h->mtype)){
394
        ff_h261_loop_filter(h);
369 395
    }
396

  
370 397
    return SLICE_OK;
371 398
}
372 399

  
......
459 486
int h261_decode_picture_header(H261Context *h){
460 487
    MpegEncContext * const s = &h->s;
461 488
    int format, i;
462
    static int h261_framecounter = 0;
463 489
    uint32_t startcode;
464 490
    align_get_bits(&s->gb);
465 491

  
......
510 536
        skip_bits(&s->gb, 8);
511 537
    }
512 538

  
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++;
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;
520 542

  
521 543
    h->gob_number = 0;
522 544
    return 0;
......
524 546

  
525 547
static int h261_decode_gob(H261Context *h){
526 548
    MpegEncContext * const s = &h->s;
527
    int v;
528 549
    
529 550
    ff_set_qscale(s, s->qscale);
530 551

  
531
    /* check for empty gob */
532
    v= show_bits(&s->gb, 15);
533

  
534
    if(get_bits_count(&s->gb) + 15 > s->gb.size_in_bits){
535
        v>>= get_bits_count(&s->gb) + 15 - s->gb.size_in_bits;
536
    }
537

  
538
    if(v==0){
539
        h261_decode_mb_skipped(h, 0, 33);
540
        return 0;
541
    }
542

  
543 552
    /* decode mb's */
544
    while(h->current_mba <= MAX_MBA)
553
    while(h->current_mba <= MBA_STUFFING)
545 554
    {
546 555
        int ret;
547 556
        /* DCT & quantize */
548
        ret= h261_decode_mb(h, s->block);
557
        ret= h261_decode_mb(h);
549 558
        if(ret<0){
550
            const int xy= s->mb_x + s->mb_y*s->mb_stride;
551 559
            if(ret==SLICE_END){
552
                MPV_decode_mb(s, s->block);
553
                if(h->loop_filter){
554
                    ff_h261_loop_filter(h);
555
                }
556
                h->loop_filter = 0;
557
                h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
558 560
                h261_decode_mb_skipped(h, h->current_mba, 33);                
559 561
                return 0;
560
            }else if(ret==SLICE_NOEND){
561
                av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
562
                return -1;
563 562
            }
564
            av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
563
            av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", s->mb_x + s->mb_y*s->mb_stride);
565 564
            return -1;
566 565
        }
567
        MPV_decode_mb(s, s->block);
568
        if(h->loop_filter){
569
            ff_h261_loop_filter(h);
570
        }
571

  
572
        h->loop_filter = 0;
566
        
573 567
        h261_decode_mb_skipped(h, h->current_mba-h->mba_diff, h->current_mba-1);
574 568
    }
575 569
    

Also available in: Unified diff