Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263dec.c @ d36a2466

History | View | Annotate | Download (16.6 KB)

1
/*
2
 * H263 decoder
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include "avcodec.h"
20
#include "dsputil.h"
21
#include "mpegvideo.h"
22

    
23
//#define DEBUG
24
//#define PRINT_FRAME_TIME
25
#ifdef PRINT_FRAME_TIME
26
static inline long long rdtsc()
27
{
28
        long long l;
29
        asm volatile(        "rdtsc\n\t"
30
                : "=A" (l)
31
        );
32
//        printf("%d\n", int(l/1000));
33
        return l;
34
}
35
#endif
36

    
37
static int h263_decode_init(AVCodecContext *avctx)
38
{
39
    MpegEncContext *s = avctx->priv_data;
40

    
41
    s->avctx = avctx;
42
    s->out_format = FMT_H263;
43

    
44
    s->width = avctx->width;
45
    s->height = avctx->height;
46
    s->workaround_bugs= avctx->workaround_bugs;
47

    
48
    /* select sub codec */
49
    switch(avctx->codec->id) {
50
    case CODEC_ID_H263:
51
        s->gob_number = 0;
52
        s->first_slice_line = 0;
53
        break;
54
    case CODEC_ID_MPEG4:
55
        s->time_increment_bits = 4; /* default value for broken headers */
56
        s->h263_pred = 1;
57
        s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
58
        break;
59
    case CODEC_ID_MSMPEG4V1:
60
        s->h263_msmpeg4 = 1;
61
        s->h263_pred = 1;
62
        s->msmpeg4_version=1;
63
        break;
64
    case CODEC_ID_MSMPEG4V2:
65
        s->h263_msmpeg4 = 1;
66
        s->h263_pred = 1;
67
        s->msmpeg4_version=2;
68
        break;
69
    case CODEC_ID_MSMPEG4V3:
70
        s->h263_msmpeg4 = 1;
71
        s->h263_pred = 1;
72
        s->msmpeg4_version=3;
73
        break;
74
    case CODEC_ID_WMV1:
75
        s->h263_msmpeg4 = 1;
76
        s->h263_pred = 1;
77
        s->msmpeg4_version=4;
78
        break;
79
    case CODEC_ID_H263I:
80
        s->h263_intel = 1;
81
        break;
82
    default:
83
        return -1;
84
    }
85
    s->codec_id= avctx->codec->id;
86
    avctx->mbskip_table= s->mbskip_table;
87
    
88
    /* for h263, we allocate the images after having read the header */
89
    if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
90
        if (MPV_common_init(s) < 0)
91
            return -1;
92

    
93
    if (s->h263_msmpeg4)
94
        msmpeg4_decode_init_vlc(s);
95
    else
96
        h263_decode_init_vlc(s);
97
    
98
    return 0;
99
}
100

    
101
static int h263_decode_end(AVCodecContext *avctx)
102
{
103
    MpegEncContext *s = avctx->priv_data;
104

    
105
    MPV_common_end(s);
106
    return 0;
107
}
108

    
109
static int h263_decode_frame(AVCodecContext *avctx, 
110
                             void *data, int *data_size,
111
                             UINT8 *buf, int buf_size)
112
{
113
    MpegEncContext *s = avctx->priv_data;
114
    int ret;
115
    AVPicture *pict = data; 
116
#ifdef PRINT_FRAME_TIME
117
uint64_t time= rdtsc();
118
#endif
119
#ifdef DEBUG
120
    printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
121
    printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
122
#endif
123

    
124
    s->hurry_up= avctx->hurry_up;
125
    s->error_resilience= avctx->error_resilience;
126
    s->workaround_bugs= avctx->workaround_bugs;
127

    
128
    /* no supplementary picture */
129
    if (buf_size == 0) {
130
        *data_size = 0;
131
        return 0;
132
    }
133

    
134
    if(s->bitstream_buffer_size && buf_size<20){ //divx 5.01+ frame reorder
135
        init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
136
    }else
137
        init_get_bits(&s->gb, buf, buf_size);
138
    s->bitstream_buffer_size=0;
139

    
140
    /* let's go :-) */
141
    if (s->h263_msmpeg4) {
142
        ret = msmpeg4_decode_picture_header(s);
143
    } else if (s->h263_pred) {
144
        ret = mpeg4_decode_picture_header(s);
145
        s->has_b_frames= !s->low_delay;
146
    } else if (s->h263_intel) {
147
        ret = intel_h263_decode_picture_header(s);
148
    } else {
149
        ret = h263_decode_picture_header(s);
150
    }
151

    
152
        /* After H263 & mpeg4 header decode we have the height, width,*/
153
        /* and other parameters. So then we could init the picture   */
154
        /* FIXME: By the way H263 decoder is evolving it should have */
155
        /* an H263EncContext                                         */
156
    if (!s->context_initialized) {
157
        avctx->width = s->width;
158
        avctx->height = s->height;
159
        avctx->aspect_ratio_info= s->aspect_ratio_info;
160
        if (MPV_common_init(s) < 0)
161
            return -1;
162
    } else if (s->width != avctx->width || s->height != avctx->height) {
163
        /* H.263 could change picture size any time */
164
        MPV_common_end(s);
165
        if (MPV_common_init(s) < 0)
166
            return -1;
167
    }
168

    
169
    if(ret==FRAME_SKIPED) return 0;
170
    /* skip if the header was thrashed */
171
    if (ret < 0)
172
        return -1;
173
    /* skip b frames if we dont have reference frames */
174
    if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
175
    /* skip b frames if we are in a hurry */
176
    if(s->hurry_up && s->pict_type==B_TYPE) return 0;
177
    
178
    if(s->next_p_frame_damaged){
179
        if(s->pict_type==B_TYPE)
180
            return 0;
181
        else
182
            s->next_p_frame_damaged=0;
183
    }
184

    
185
    MPV_frame_start(s);
186

    
187
#ifdef DEBUG
188
    printf("qscale=%d\n", s->qscale);
189
#endif
190

    
191
    /* init resync/ error resilience specific variables */
192
    s->next_resync_qscale= s->qscale;
193
    s->next_resync_gb= s->gb;
194
    if(s->resync_marker) s->mb_num_left= 0;
195
    else                 s->mb_num_left= s->mb_num;
196

    
197
    /* decode each macroblock */
198
    s->block_wrap[0]=
199
    s->block_wrap[1]=
200
    s->block_wrap[2]=
201
    s->block_wrap[3]= s->mb_width*2 + 2;
202
    s->block_wrap[4]=
203
    s->block_wrap[5]= s->mb_width + 2;
204
    for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
205
        /* Check for GOB headers on H.263 */
206
        /* FIXME: In the future H.263+ will have intra prediction */
207
        /* and we are gonna need another way to detect MPEG4      */
208
        if (s->mb_y && !s->h263_pred) {
209
            s->first_slice_line = h263_decode_gob_header(s);
210
        }
211
        
212
        if(s->msmpeg4_version==1){
213
            s->last_dc[0]=
214
            s->last_dc[1]=
215
            s->last_dc[2]= 128;
216
        }
217

    
218
        s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
219
        s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
220
        s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
221
        s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
222
        s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
223
        s->block_index[5]= s->block_wrap[4]*(s->mb_y + 1 + s->mb_height + 2) + s->block_wrap[0]*(s->mb_height*2 + 2);
224
        for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
225
            s->block_index[0]+=2;
226
            s->block_index[1]+=2;
227
            s->block_index[2]+=2;
228
            s->block_index[3]+=2;
229
            s->block_index[4]++;
230
            s->block_index[5]++;
231
#ifdef DEBUG
232
            printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
233
#endif
234

    
235
            if(s->resync_marker){
236
                if(s->mb_num_left<=0){
237
                    /* except the first block */
238
                    if(s->mb_x!=0 || s->mb_y!=0){
239
                        /* did we miss the next resync marker without noticing an error yet */
240
                        if(((get_bits_count(&s->gb)+8)&(~7)) != s->next_resync_pos && s->decoding_error==0){
241
                            fprintf(stderr, "slice end missmatch x:%d y:%d %d %d\n", 
242
                                    s->mb_x, s->mb_y, get_bits_count(&s->gb), s->next_resync_pos);
243
                            ff_conceal_past_errors(s, 1);
244
                        }
245
                    }
246
                    s->qscale= s->next_resync_qscale;
247
                    s->gb= s->next_resync_gb;
248
                    s->resync_mb_x= s->mb_x; //we know that the marker is here cuz mb_num_left was the distance to it
249
                    s->resync_mb_y= s->mb_y;
250
                    s->first_slice_line=1;
251

    
252
                    if(s->codec_id==CODEC_ID_MPEG4){
253
                        ff_mpeg4_clean_buffers(s);
254
                        ff_mpeg4_resync(s);
255
                    }
256
                }
257

    
258
                if(   s->resync_mb_x==s->mb_x 
259
                   && s->resync_mb_y==s->mb_y && s->decoding_error!=0){
260
                    fprintf(stderr, "resynced at %d %d\n", s->mb_x, s->mb_y);
261
                    s->decoding_error= 0;
262
                }
263
            }
264

    
265
            //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
266
            /* DCT & quantize */
267
            if (s->h263_pred && !(s->msmpeg4_version==1 || s->msmpeg4_version==2)) {
268
                /* old ffmpeg encoded msmpeg4v3 workaround */
269
                if(s->workaround_bugs==1 && s->msmpeg4_version==3) 
270
                    ff_old_msmpeg4_dc_scale(s);
271
                else
272
                    h263_dc_scale(s);                
273
            } else {
274
                /* default quantization values */
275
                s->y_dc_scale = 8;
276
                s->c_dc_scale = 8;
277
            }
278

    
279
            if(s->decoding_error!=DECODING_DESYNC){
280
                int last_error= s->decoding_error;
281
                clear_blocks(s->block[0]);
282
            
283
                s->mv_dir = MV_DIR_FORWARD;
284
                s->mv_type = MV_TYPE_16X16;
285
                if (s->h263_msmpeg4) {
286
                    if (msmpeg4_decode_mb(s, s->block) < 0) {
287
                        fprintf(stderr,"Error at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
288
                        s->decoding_error=DECODING_DESYNC;
289
                    }
290
                } else {
291
                    if (h263_decode_mb(s, s->block) < 0) {
292
                        fprintf(stderr,"Error at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
293
                        s->decoding_error=DECODING_DESYNC;
294
                    }
295
                }
296

    
297
                if(s->decoding_error!=last_error){
298
                    ff_conceal_past_errors(s, 0);
299
                }
300
            }
301

    
302
            /* conceal errors */
303
            if(    s->decoding_error==DECODING_DESYNC
304
               || (s->decoding_error==DECODING_ACDC_LOST && s->mb_intra)){
305
                s->mv_dir = MV_DIR_FORWARD;
306
                s->mv_type = MV_TYPE_16X16;
307
                s->mb_skiped=0;
308
                s->mb_intra=0;
309
                s->mv[0][0][0]=0; //FIXME this is not optimal 
310
                s->mv[0][0][1]=0;
311
                clear_blocks(s->block[0]);
312
            }else if(s->decoding_error && !s->mb_intra){
313
                clear_blocks(s->block[0]);
314
            }
315
            //FIXME remove AC for intra
316
                        
317
            MPV_decode_mb(s, s->block);
318

    
319
            s->mb_num_left--;            
320
        }
321
        if (    avctx->draw_horiz_band 
322
            && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
323
            UINT8 *src_ptr[3];
324
            int y, h, offset;
325
            y = s->mb_y * 16;
326
            h = s->height - y;
327
            if (h > 16)
328
                h = 16;
329
            offset = y * s->linesize;
330
            if(s->pict_type==B_TYPE || (!s->has_b_frames)){
331
                src_ptr[0] = s->current_picture[0] + offset;
332
                src_ptr[1] = s->current_picture[1] + (offset >> 2);
333
                src_ptr[2] = s->current_picture[2] + (offset >> 2);
334
            } else {
335
                src_ptr[0] = s->last_picture[0] + offset;
336
                src_ptr[1] = s->last_picture[1] + (offset >> 2);
337
                src_ptr[2] = s->last_picture[2] + (offset >> 2);
338
            }
339
            avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
340
                                   y, s->width, h);
341
        }
342
    }
343
    
344
    if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
345
        if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
346
    
347
    /* divx 5.01+ bistream reorder stuff */
348
    if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){
349
        int current_pos= get_bits_count(&s->gb)>>3;
350

    
351
        if(   buf_size - current_pos > 5 
352
           && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
353
            int i;
354
            int startcode_found=0;
355
            for(i=current_pos; i<buf_size; i++){
356
                if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
357
                    startcode_found=1;
358
                    break;
359
                }
360
            }
361
            if(startcode_found){
362
                memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
363
                s->bitstream_buffer_size= buf_size - current_pos;
364
            }
365
        }
366
    }
367

    
368
    if(s->bitstream_buffer_size==0 && s->error_resilience>0){
369
        int left= s->gb.size*8 - get_bits_count(&s->gb);
370
        int max_extra=8;
371
        
372
        if(s->codec_id==CODEC_ID_MPEG4) max_extra+=32;
373

    
374
        if(left>max_extra){
375
            fprintf(stderr, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
376
            if(s->decoding_error==0)
377
                ff_conceal_past_errors(s, 1);
378
        }
379
        if(left<0){
380
            fprintf(stderr, "overreading %d bits\n", -left);
381
            if(s->decoding_error==0)
382
                ff_conceal_past_errors(s, 1);
383
        }
384
    }
385
  
386
    MPV_frame_end(s);
387
#if 0 //dirty show MVs, we should export the MV tables and write a filter to show them
388
{
389
  int mb_y;
390
  s->has_b_frames=1;
391
  for(mb_y=0; mb_y<s->mb_height; mb_y++){
392
    int mb_x;
393
    int y= mb_y*16 + 8;
394
    for(mb_x=0; mb_x<s->mb_width; mb_x++){
395
      int x= mb_x*16 + 8;
396
      uint8_t *ptr= s->last_picture[0];
397
      int xy= 1 + mb_x*2 + (mb_y*2 + 1)*(s->mb_width*2 + 2);
398
      int mx= (s->motion_val[xy][0]>>1) + x;
399
      int my= (s->motion_val[xy][1]>>1) + y;
400
      int i;
401
      int max;
402

403
      if(mx<0) mx=0;
404
      if(my<0) my=0;
405
      if(mx>=s->width)  mx= s->width -1;
406
      if(my>=s->height) my= s->height-1;
407
      max= ABS(mx-x);
408
      if(ABS(my-y) > max) max= ABS(my-y);
409
      /* the ugliest linedrawing routine ... */
410
      for(i=0; i<max; i++){
411
        int x1= x + (mx-x)*i/max;
412
        int y1= y + (my-y)*i/max;
413
        ptr[y1*s->linesize + x1]+=100;
414
      }
415
      ptr[y*s->linesize + x]+=100;
416
      s->mbskip_table[mb_x + mb_y*s->mb_width]=0;
417
    }
418
  }
419

420
}
421
#endif    
422
    if(s->pict_type==B_TYPE || (!s->has_b_frames)){
423
        pict->data[0] = s->current_picture[0];
424
        pict->data[1] = s->current_picture[1];
425
        pict->data[2] = s->current_picture[2];
426
    } else {
427
        pict->data[0] = s->last_picture[0];
428
        pict->data[1] = s->last_picture[1];
429
        pict->data[2] = s->last_picture[2];
430
    }
431
    pict->linesize[0] = s->linesize;
432
    pict->linesize[1] = s->linesize / 2;
433
    pict->linesize[2] = s->linesize / 2;
434

    
435
    avctx->quality = s->qscale;
436

    
437
    /* Return the Picture timestamp as the frame number */
438
    /* we substract 1 because it is added on utils.c    */
439
    avctx->frame_number = s->picture_number - 1;
440

    
441
    /* dont output the last pic after seeking 
442
       note we allready added +1 for the current pix in MPV_frame_end(s) */
443
    if(s->num_available_buffers>=2 || (!s->has_b_frames))
444
        *data_size = sizeof(AVPicture);
445
#ifdef PRINT_FRAME_TIME
446
printf("%Ld\n", rdtsc()-time);
447
#endif
448
    return buf_size;
449
}
450

    
451
AVCodec mpeg4_decoder = {
452
    "mpeg4",
453
    CODEC_TYPE_VIDEO,
454
    CODEC_ID_MPEG4,
455
    sizeof(MpegEncContext),
456
    h263_decode_init,
457
    NULL,
458
    h263_decode_end,
459
    h263_decode_frame,
460
    CODEC_CAP_DRAW_HORIZ_BAND,
461
};
462

    
463
AVCodec h263_decoder = {
464
    "h263",
465
    CODEC_TYPE_VIDEO,
466
    CODEC_ID_H263,
467
    sizeof(MpegEncContext),
468
    h263_decode_init,
469
    NULL,
470
    h263_decode_end,
471
    h263_decode_frame,
472
    CODEC_CAP_DRAW_HORIZ_BAND,
473
};
474

    
475
AVCodec msmpeg4v1_decoder = {
476
    "msmpeg4v1",
477
    CODEC_TYPE_VIDEO,
478
    CODEC_ID_MSMPEG4V1,
479
    sizeof(MpegEncContext),
480
    h263_decode_init,
481
    NULL,
482
    h263_decode_end,
483
    h263_decode_frame,
484
    CODEC_CAP_DRAW_HORIZ_BAND,
485
};
486

    
487
AVCodec msmpeg4v2_decoder = {
488
    "msmpeg4v2",
489
    CODEC_TYPE_VIDEO,
490
    CODEC_ID_MSMPEG4V2,
491
    sizeof(MpegEncContext),
492
    h263_decode_init,
493
    NULL,
494
    h263_decode_end,
495
    h263_decode_frame,
496
    CODEC_CAP_DRAW_HORIZ_BAND,
497
};
498

    
499
AVCodec msmpeg4v3_decoder = {
500
    "msmpeg4",
501
    CODEC_TYPE_VIDEO,
502
    CODEC_ID_MSMPEG4V3,
503
    sizeof(MpegEncContext),
504
    h263_decode_init,
505
    NULL,
506
    h263_decode_end,
507
    h263_decode_frame,
508
    CODEC_CAP_DRAW_HORIZ_BAND,
509
};
510

    
511
AVCodec wmv1_decoder = {
512
    "wmv1",
513
    CODEC_TYPE_VIDEO,
514
    CODEC_ID_WMV1,
515
    sizeof(MpegEncContext),
516
    h263_decode_init,
517
    NULL,
518
    h263_decode_end,
519
    h263_decode_frame,
520
    CODEC_CAP_DRAW_HORIZ_BAND,
521
};
522

    
523
AVCodec h263i_decoder = {
524
    "h263i",
525
    CODEC_TYPE_VIDEO,
526
    CODEC_ID_H263I,
527
    sizeof(MpegEncContext),
528
    h263_decode_init,
529
    NULL,
530
    h263_decode_end,
531
    h263_decode_frame,
532
    CODEC_CAP_DRAW_HORIZ_BAND,
533
};
534