Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263dec.c @ 1c2a8c7f

History | View | Annotate | Download (11.1 KB)

1
/*
2
 * H263 decoder
3
 * Copyright (c) 2001 Gerard Lantau.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program 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
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 */
19
#include <stdlib.h>
20
#include <stdio.h>
21
#include <string.h>
22
#include "dsputil.h"
23
#include "avcodec.h"
24
#include "mpegvideo.h"
25

    
26
//#define DEBUG
27

    
28
static int h263_decode_init(AVCodecContext *avctx)
29
{
30
    MpegEncContext *s = avctx->priv_data;
31
    int i;
32

    
33
    s->avctx = avctx;
34
    s->out_format = FMT_H263;
35

    
36
    s->width = avctx->width;
37
    s->height = avctx->height;
38

    
39
    /* select sub codec */
40
    switch(avctx->codec->id) {
41
    case CODEC_ID_H263:
42
        s->gob_number = 0;
43
        s->first_gob_line = 0;
44
        break;
45
    case CODEC_ID_MPEG4:
46
        s->time_increment_bits = 4; /* default value for broken headers */
47
        s->h263_pred = 1;
48
        s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
49
        break;
50
    case CODEC_ID_MSMPEG4V1:
51
        s->h263_msmpeg4 = 1;
52
        s->h263_pred = 1;
53
        s->msmpeg4_version=1;
54
        break;
55
    case CODEC_ID_MSMPEG4V2:
56
        s->h263_msmpeg4 = 1;
57
        s->h263_pred = 1;
58
        s->msmpeg4_version=2;
59
        break;
60
    case CODEC_ID_MSMPEG4V3:
61
        s->h263_msmpeg4 = 1;
62
        s->h263_pred = 1;
63
        s->msmpeg4_version=3;
64
        break;
65
    case CODEC_ID_WMV1:
66
        s->h263_msmpeg4 = 1;
67
        s->h263_pred = 1;
68
        s->msmpeg4_version=4;
69
        break;
70
    case CODEC_ID_H263I:
71
        s->h263_intel = 1;
72
        break;
73
    default:
74
        return -1;
75
    }
76

    
77
    /* for h263, we allocate the images after having read the header */
78
    if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
79
        if (MPV_common_init(s) < 0)
80
            return -1;
81

    
82
    /* XXX: suppress this matrix init, only needed because using mpeg1
83
       dequantize in mmx case */
84
    for(i=0;i<64;i++)
85
        s->non_intra_matrix[i] = default_non_intra_matrix[i];
86

    
87
    if (s->h263_msmpeg4)
88
        msmpeg4_decode_init_vlc(s);
89
    else
90
        h263_decode_init_vlc(s);
91
    
92
    return 0;
93
}
94

    
95
static int h263_decode_end(AVCodecContext *avctx)
96
{
97
    MpegEncContext *s = avctx->priv_data;
98

    
99
    MPV_common_end(s);
100
    return 0;
101
}
102

    
103
static int h263_decode_frame(AVCodecContext *avctx, 
104
                             void *data, int *data_size,
105
                             UINT8 *buf, int buf_size)
106
{
107
    MpegEncContext *s = avctx->priv_data;
108
    int ret;
109
    AVPicture *pict = data; 
110

    
111
#ifdef DEBUG
112
    printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
113
    printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
114
#endif
115
    
116
    /* no supplementary picture */
117
    if (buf_size == 0) {
118
        *data_size = 0;
119
        return 0;
120
    }
121

    
122
    if(s->bitstream_buffer_size) //divx 5.01+ frame reorder
123
        init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
124
    else
125
        init_get_bits(&s->gb, buf, buf_size);
126

    
127
    /* let's go :-) */
128
    if (s->h263_msmpeg4) {
129
        ret = msmpeg4_decode_picture_header(s);
130
    } else if (s->h263_pred) {
131
        ret = mpeg4_decode_picture_header(s);
132
        s->has_b_frames= !s->low_delay;
133
    } else if (s->h263_intel) {
134
        ret = intel_h263_decode_picture_header(s);
135
    } else {
136
        ret = h263_decode_picture_header(s);
137
    }
138

    
139
        /* After H263 & mpeg4 header decode we have the height, width,*/
140
        /* and other parameters. So then we could init the picture   */
141
        /* FIXME: By the way H263 decoder is evolving it should have */
142
        /* an H263EncContext                                         */
143
    if (!s->context_initialized) {
144
        avctx->width = s->width;
145
        avctx->height = s->height;
146
        avctx->aspect_ratio_info= s->aspect_ratio_info;
147
        if (MPV_common_init(s) < 0)
148
            return -1;
149
    } else if (s->width != avctx->width || s->height != avctx->height) {
150
        /* H.263 could change picture size any time */
151
        MPV_common_end(s);
152
        if (MPV_common_init(s) < 0)
153
            return -1;
154
    }
155

    
156
    if(ret==FRAME_SKIPED) return 0;
157
    if (ret < 0)
158
        return -1;
159
    /* skip b frames if we dont have reference frames */
160
    if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
161
        
162
    MPV_frame_start(s);
163

    
164
#ifdef DEBUG
165
    printf("qscale=%d\n", s->qscale);
166
#endif
167

    
168
    /* decode each macroblock */
169
    s->block_wrap[0]=
170
    s->block_wrap[1]=
171
    s->block_wrap[2]=
172
    s->block_wrap[3]= s->mb_width*2 + 2;
173
    s->block_wrap[4]=
174
    s->block_wrap[5]= s->mb_width + 2;
175
    for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
176
        /* Check for GOB headers on H.263 */
177
        /* FIXME: In the future H.263+ will have intra prediction */
178
        /* and we are gonna need another way to detect MPEG4      */
179
        if (s->mb_y && !s->h263_pred) {
180
            s->first_gob_line = h263_decode_gob_header(s);
181
        }
182

    
183
        s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
184
        s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
185
        s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
186
        s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
187
        s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
188
        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);
189
        for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
190
            s->block_index[0]+=2;
191
            s->block_index[1]+=2;
192
            s->block_index[2]+=2;
193
            s->block_index[3]+=2;
194
            s->block_index[4]++;
195
            s->block_index[5]++;
196
#ifdef DEBUG
197
            printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
198
#endif
199
            //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
200
            /* DCT & quantize */
201
            if (s->h263_msmpeg4) {
202
                msmpeg4_dc_scale(s);
203
            } else if (s->h263_pred) {
204
                h263_dc_scale(s);
205
            } else {
206
                /* default quantization values */
207
                s->y_dc_scale = 8;
208
                s->c_dc_scale = 8;
209
            }
210
            clear_blocks(s->block[0]);
211
            
212
            s->mv_dir = MV_DIR_FORWARD;
213
            s->mv_type = MV_TYPE_16X16; 
214
            if (s->h263_msmpeg4) {
215
                if (msmpeg4_decode_mb(s, s->block) < 0) {
216
                    fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
217
                    return -1;
218
                }
219
            } else {
220
                if (h263_decode_mb(s, s->block) < 0) {
221
                    fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
222
                    return -1;
223
                }
224
            }
225
            MPV_decode_mb(s, s->block);
226
        }
227
        if (    avctx->draw_horiz_band 
228
            && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
229
            UINT8 *src_ptr[3];
230
            int y, h, offset;
231
            y = s->mb_y * 16;
232
            h = s->height - y;
233
            if (h > 16)
234
                h = 16;
235
            offset = y * s->linesize;
236
            if(s->pict_type==B_TYPE || (!s->has_b_frames)){
237
                src_ptr[0] = s->current_picture[0] + offset;
238
                src_ptr[1] = s->current_picture[1] + (offset >> 2);
239
                src_ptr[2] = s->current_picture[2] + (offset >> 2);
240
            } else {
241
                src_ptr[0] = s->last_picture[0] + offset;
242
                src_ptr[1] = s->last_picture[1] + (offset >> 2);
243
                src_ptr[2] = s->last_picture[2] + (offset >> 2);
244
            }
245
            avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
246
                                   y, s->width, h);
247
        }
248
    }
249
    
250
    if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
251
        if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
252
    
253
    /* divx 5.01+ bistream reorder stuff */
254
    if(s->h263_pred && s->bitstream_buffer_size==0){
255
        int current_pos= get_bits_count(&s->gb)/8;
256
        if(   buf_size - current_pos > 5 
257
           && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
258
            memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
259
            s->bitstream_buffer_size= buf_size - current_pos;
260
        }
261
    }else
262
        s->bitstream_buffer_size=0;
263
  
264
    MPV_frame_end(s);
265
    
266
    if(s->pict_type==B_TYPE || (!s->has_b_frames)){
267
        pict->data[0] = s->current_picture[0];
268
        pict->data[1] = s->current_picture[1];
269
        pict->data[2] = s->current_picture[2];
270
    } else {
271
        pict->data[0] = s->last_picture[0];
272
        pict->data[1] = s->last_picture[1];
273
        pict->data[2] = s->last_picture[2];
274
    }
275
    pict->linesize[0] = s->linesize;
276
    pict->linesize[1] = s->linesize / 2;
277
    pict->linesize[2] = s->linesize / 2;
278

    
279
    avctx->quality = s->qscale;
280

    
281
    /* Return the Picture timestamp as the frame number */
282
    /* we substract 1 because it is added on utils.c    */
283
    avctx->frame_number = s->picture_number - 1;
284

    
285
    /* dont output the last pic after seeking 
286
       note we allready added +1 for the current pix in MPV_frame_end(s) */
287
    if(s->num_available_buffers>=2 || (!s->has_b_frames))
288
        *data_size = sizeof(AVPicture);
289

    
290
    return buf_size;
291
}
292

    
293
AVCodec mpeg4_decoder = {
294
    "mpeg4",
295
    CODEC_TYPE_VIDEO,
296
    CODEC_ID_MPEG4,
297
    sizeof(MpegEncContext),
298
    h263_decode_init,
299
    NULL,
300
    h263_decode_end,
301
    h263_decode_frame,
302
    CODEC_CAP_DRAW_HORIZ_BAND,
303
};
304

    
305
AVCodec h263_decoder = {
306
    "h263",
307
    CODEC_TYPE_VIDEO,
308
    CODEC_ID_H263,
309
    sizeof(MpegEncContext),
310
    h263_decode_init,
311
    NULL,
312
    h263_decode_end,
313
    h263_decode_frame,
314
    CODEC_CAP_DRAW_HORIZ_BAND,
315
};
316

    
317
AVCodec msmpeg4v1_decoder = {
318
    "msmpeg4v1",
319
    CODEC_TYPE_VIDEO,
320
    CODEC_ID_MSMPEG4V1,
321
    sizeof(MpegEncContext),
322
    h263_decode_init,
323
    NULL,
324
    h263_decode_end,
325
    h263_decode_frame,
326
    CODEC_CAP_DRAW_HORIZ_BAND,
327
};
328

    
329
AVCodec msmpeg4v2_decoder = {
330
    "msmpeg4v2",
331
    CODEC_TYPE_VIDEO,
332
    CODEC_ID_MSMPEG4V2,
333
    sizeof(MpegEncContext),
334
    h263_decode_init,
335
    NULL,
336
    h263_decode_end,
337
    h263_decode_frame,
338
    CODEC_CAP_DRAW_HORIZ_BAND,
339
};
340

    
341
AVCodec msmpeg4v3_decoder = {
342
    "msmpeg4",
343
    CODEC_TYPE_VIDEO,
344
    CODEC_ID_MSMPEG4V3,
345
    sizeof(MpegEncContext),
346
    h263_decode_init,
347
    NULL,
348
    h263_decode_end,
349
    h263_decode_frame,
350
    CODEC_CAP_DRAW_HORIZ_BAND,
351
};
352

    
353
AVCodec wmv1_decoder = {
354
    "wmv1",
355
    CODEC_TYPE_VIDEO,
356
    CODEC_ID_WMV1,
357
    sizeof(MpegEncContext),
358
    h263_decode_init,
359
    NULL,
360
    h263_decode_end,
361
    h263_decode_frame,
362
    CODEC_CAP_DRAW_HORIZ_BAND,
363
};
364

    
365
AVCodec h263i_decoder = {
366
    "h263i",
367
    CODEC_TYPE_VIDEO,
368
    CODEC_ID_H263I,
369
    sizeof(MpegEncContext),
370
    h263_decode_init,
371
    NULL,
372
    h263_decode_end,
373
    h263_decode_frame,
374
    CODEC_CAP_DRAW_HORIZ_BAND,
375
};
376