Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263dec.c @ 4278e7a6

History | View | Annotate | Download (9.02 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;
49
        break;
50
    case CODEC_ID_MSMPEG4:
51
        s->h263_msmpeg4 = 1;
52
        s->h263_pred = 1;
53
        break;
54
    case CODEC_ID_H263I:
55
        s->h263_intel = 1;
56
        break;
57
    default:
58
        return -1;
59
    }
60

    
61
    /* for h263, we allocate the images after having read the header */
62
    if (avctx->codec->id != CODEC_ID_H263)
63
        if (MPV_common_init(s) < 0)
64
            return -1;
65

    
66
    /* XXX: suppress this matrix init, only needed because using mpeg1
67
       dequantize in mmx case */
68
    for(i=0;i<64;i++)
69
        s->non_intra_matrix[i] = default_non_intra_matrix[i];
70

    
71
    if (s->h263_msmpeg4)
72
        msmpeg4_decode_init_vlc(s);
73
    else
74
        h263_decode_init_vlc(s);
75
    
76
    return 0;
77
}
78

    
79
static int h263_decode_end(AVCodecContext *avctx)
80
{
81
    MpegEncContext *s = avctx->priv_data;
82

    
83
    MPV_common_end(s);
84
    return 0;
85
}
86

    
87
static int h263_decode_frame(AVCodecContext *avctx, 
88
                             void *data, int *data_size,
89
                             UINT8 *buf, int buf_size)
90
{
91
    MpegEncContext *s = avctx->priv_data;
92
    int ret;
93
    AVPicture *pict = data; 
94

    
95
#ifdef DEBUG
96
    printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
97
    printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
98
#endif
99
    
100
    /* no supplementary picture */
101
    if (buf_size == 0) {
102
        *data_size = 0;
103
        return 0;
104
    }
105

    
106
    init_get_bits(&s->gb, buf, buf_size);
107

    
108
    /* let's go :-) */
109
    if (s->h263_msmpeg4) {
110
        ret = msmpeg4_decode_picture_header(s);
111
    } else if (s->h263_pred) {
112
        ret = mpeg4_decode_picture_header(s);
113
    } else if (s->h263_intel) {
114
        ret = intel_h263_decode_picture_header(s);
115
    } else {
116
        ret = h263_decode_picture_header(s);
117
        /* After H263 header decode we have the height, width,       */
118
        /* and other parameters. So then we could init the picture   */
119
        /* FIXME: By the way H263 decoder is evolving it should have */
120
        /* an H263EncContext                                         */
121
        if (!s->context_initialized) {
122
            avctx->width = s->width;
123
            avctx->height = s->height;
124
            if (MPV_common_init(s) < 0)
125
                return -1;
126
        } else if (s->width != avctx->width || s->height != avctx->height) {
127
            /* H.263 could change picture size any time */
128
            MPV_common_end(s);
129
            if (MPV_common_init(s) < 0)
130
                return -1;
131
        }
132
    }
133
    if (ret < 0)
134
        return -1;
135

    
136
    MPV_frame_start(s);
137

    
138
#ifdef DEBUG
139
    printf("qscale=%d\n", s->qscale);
140
#endif
141

    
142
    /* decode each macroblock */
143
    s->block_wrap[0]=
144
    s->block_wrap[1]=
145
    s->block_wrap[2]=
146
    s->block_wrap[3]= s->mb_width*2 + 2;
147
    s->block_wrap[4]=
148
    s->block_wrap[5]= s->mb_width + 2;
149
    for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
150
        /* Check for GOB headers on H.263 */
151
        /* FIXME: In the future H.263+ will have intra prediction */
152
        /* and we are gonna need another way to detect MPEG4      */
153
        if (s->mb_y && !s->h263_pred) {
154
            s->first_gob_line = h263_decode_gob_header(s);
155
        }
156
        s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
157
        s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
158
        s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
159
        s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
160
        s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
161
        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);
162
        for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
163
            s->block_index[0]+=2;
164
            s->block_index[1]+=2;
165
            s->block_index[2]+=2;
166
            s->block_index[3]+=2;
167
            s->block_index[4]++;
168
            s->block_index[5]++;
169
#ifdef DEBUG
170
            printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
171
#endif
172
            //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
173
            /* DCT & quantize */
174
            if (s->h263_msmpeg4) {
175
                msmpeg4_dc_scale(s);
176
            } else if (s->h263_pred) {
177
                h263_dc_scale(s);
178
            } else {
179
                /* default quantization values */
180
                s->y_dc_scale = 8;
181
                s->c_dc_scale = 8;
182
            }
183

    
184
#ifdef HAVE_MMX
185
            if (mm_flags & MM_MMX) {
186
                asm volatile(
187
                        "pxor %%mm7, %%mm7                \n\t"
188
                        "movl $-128*6, %%eax                \n\t"
189
                        "1:                                \n\t"
190
                        "movq %%mm7, (%0, %%eax)        \n\t"
191
                        "movq %%mm7, 8(%0, %%eax)        \n\t"
192
                        "movq %%mm7, 16(%0, %%eax)        \n\t"
193
                        "movq %%mm7, 24(%0, %%eax)        \n\t"
194
                        "addl $32, %%eax                \n\t"
195
                        " js 1b                                \n\t"
196
                        : : "r" (((int)s->block)+128*6)
197
                        : "%eax"
198
                );
199
            }else{
200
                memset(s->block, 0, sizeof(s->block));
201
            }
202
#else
203
            memset(s->block, 0, sizeof(s->block));
204
#endif
205
            s->mv_dir = MV_DIR_FORWARD;
206
            s->mv_type = MV_TYPE_16X16; 
207
            if (s->h263_msmpeg4) {
208
                if (msmpeg4_decode_mb(s, s->block) < 0) {
209
                    fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
210
                    return -1;
211
                }
212
            } else {
213
                if (h263_decode_mb(s, s->block) < 0) {
214
                    fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
215
                    return -1;
216
                }
217
            }
218
            MPV_decode_mb(s, s->block);
219
        }
220
        if (avctx->draw_horiz_band) {
221
            UINT8 *src_ptr[3];
222
            int y, h, offset;
223
            y = s->mb_y * 16;
224
            h = s->height - y;
225
            if (h > 16)
226
                h = 16;
227
            offset = y * s->linesize;
228
            src_ptr[0] = s->current_picture[0] + offset;
229
            src_ptr[1] = s->current_picture[1] + (offset >> 2);
230
            src_ptr[2] = s->current_picture[2] + (offset >> 2);
231
            avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
232
                                   y, s->width, h);
233
        }
234
    }
235
    
236
    if (s->h263_msmpeg4 && s->pict_type==I_TYPE)
237
        if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
238

    
239
    MPV_frame_end(s);
240
    
241
    if(s->pict_type==B_TYPE){
242
        pict->data[0] = s->current_picture[0];
243
        pict->data[1] = s->current_picture[1];
244
        pict->data[2] = s->current_picture[2];
245
    } else {
246
        pict->data[0] = s->last_picture[0];
247
        pict->data[1] = s->last_picture[1];
248
        pict->data[2] = s->last_picture[2];
249
    }
250
    pict->linesize[0] = s->linesize;
251
    pict->linesize[1] = s->linesize / 2;
252
    pict->linesize[2] = s->linesize / 2;
253

    
254
    avctx->quality = s->qscale;
255

    
256
    /* Return the Picture timestamp as the frame number */
257
    /* we substract 1 because it is added on utils.c    */
258
    avctx->frame_number = s->picture_number - 1;
259

    
260
    *data_size = sizeof(AVPicture);
261
    return buf_size;
262
}
263

    
264
AVCodec mpeg4_decoder = {
265
    "mpeg4",
266
    CODEC_TYPE_VIDEO,
267
    CODEC_ID_MPEG4,
268
    sizeof(MpegEncContext),
269
    h263_decode_init,
270
    NULL,
271
    h263_decode_end,
272
    h263_decode_frame,
273
    CODEC_CAP_DRAW_HORIZ_BAND,
274
};
275

    
276
AVCodec h263_decoder = {
277
    "h263",
278
    CODEC_TYPE_VIDEO,
279
    CODEC_ID_H263,
280
    sizeof(MpegEncContext),
281
    h263_decode_init,
282
    NULL,
283
    h263_decode_end,
284
    h263_decode_frame,
285
    CODEC_CAP_DRAW_HORIZ_BAND,
286
};
287

    
288
AVCodec msmpeg4_decoder = {
289
    "msmpeg4",
290
    CODEC_TYPE_VIDEO,
291
    CODEC_ID_MSMPEG4,
292
    sizeof(MpegEncContext),
293
    h263_decode_init,
294
    NULL,
295
    h263_decode_end,
296
    h263_decode_frame,
297
    CODEC_CAP_DRAW_HORIZ_BAND,
298
};
299

    
300
AVCodec h263i_decoder = {
301
    "h263i",
302
    CODEC_TYPE_VIDEO,
303
    CODEC_ID_H263I,
304
    sizeof(MpegEncContext),
305
    h263_decode_init,
306
    NULL,
307
    h263_decode_end,
308
    h263_decode_frame,
309
    CODEC_CAP_DRAW_HORIZ_BAND,
310
};
311