Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263dec.c @ 8bdbae01

History | View | Annotate | Download (7.61 KB)

1 de6d9b64 Fabrice Bellard
/*
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 519c2b6d Fabrice Bellard
    int i;
32
33 bf89e6b1 Fabrice Bellard
    s->avctx = avctx;
34 de6d9b64 Fabrice Bellard
    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 102d3908 Juanjo
        s->gob_number = 0;
43
        s->first_gob_line = 0;
44 de6d9b64 Fabrice Bellard
        break;
45 bf89e6b1 Fabrice Bellard
    case CODEC_ID_MPEG4:
46 de6d9b64 Fabrice Bellard
        s->time_increment_bits = 4; /* default value for broken headers */
47
        s->h263_pred = 1;
48
        break;
49
    case CODEC_ID_MSMPEG4:
50
        s->h263_msmpeg4 = 1;
51
        s->h263_pred = 1;
52
        break;
53
    case CODEC_ID_H263I:
54
        s->h263_intel = 1;
55
        break;
56
    default:
57
        return -1;
58
    }
59
60
    /* for h263, we allocate the images after having read the header */
61 4949028f Juanjo
    if (avctx->codec->id != CODEC_ID_H263)
62
        if (MPV_common_init(s) < 0)
63
            return -1;
64 de6d9b64 Fabrice Bellard
65 519c2b6d Fabrice Bellard
    /* XXX: suppress this matrix init, only needed because using mpeg1
66
       dequantize in mmx case */
67
    for(i=0;i<64;i++)
68
        s->non_intra_matrix[i] = default_non_intra_matrix[i];
69
70 de6d9b64 Fabrice Bellard
    if (s->h263_msmpeg4)
71
        msmpeg4_decode_init_vlc(s);
72
    else
73
        h263_decode_init_vlc(s);
74
    
75
    return 0;
76
}
77
78
static int h263_decode_end(AVCodecContext *avctx)
79
{
80
    MpegEncContext *s = avctx->priv_data;
81
82
    MPV_common_end(s);
83
    return 0;
84
}
85
86
static int h263_decode_frame(AVCodecContext *avctx, 
87
                             void *data, int *data_size,
88
                             UINT8 *buf, int buf_size)
89
{
90
    MpegEncContext *s = avctx->priv_data;
91
    int ret;
92
    AVPicture *pict = data; 
93
94
#ifdef DEBUG
95
    printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
96
    printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
97
#endif
98 4949028f Juanjo
    
99 de6d9b64 Fabrice Bellard
    /* no supplementary picture */
100
    if (buf_size == 0) {
101
        *data_size = 0;
102
        return 0;
103
    }
104
105
    init_get_bits(&s->gb, buf, buf_size);
106
107
    /* let's go :-) */
108
    if (s->h263_msmpeg4) {
109
        ret = msmpeg4_decode_picture_header(s);
110
    } else if (s->h263_pred) {
111
        ret = mpeg4_decode_picture_header(s);
112
    } else if (s->h263_intel) {
113
        ret = intel_h263_decode_picture_header(s);
114
    } else {
115
        ret = h263_decode_picture_header(s);
116 9eaa4690 Juanjo
        /* After H263 header decode we have the height, width,       */
117
        /* and other parameters. So then we could init the picture   */
118
        /* FIXME: By the way H263 decoder is evolving it should have */
119
        /* an H263EncContext                                         */
120
        if (!s->context_initialized) {
121 4949028f Juanjo
            avctx->width = s->width;
122
            avctx->height = s->height;
123 9eaa4690 Juanjo
            if (MPV_common_init(s) < 0)
124
                return -1;
125
        } else if (s->width != avctx->width || s->height != avctx->height) {
126
            /* H.263 could change picture size any time */
127
            MPV_common_end(s);
128 4949028f Juanjo
            if (MPV_common_init(s) < 0)
129
                return -1;
130
        }
131 de6d9b64 Fabrice Bellard
    }
132
    if (ret < 0)
133
        return -1;
134
135
    MPV_frame_start(s);
136
137
#ifdef DEBUG
138
    printf("qscale=%d\n", s->qscale);
139
#endif
140
141
    /* decode each macroblock */
142
    for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
143 644d98a4 Juanjo
        /* Check for GOB headers on H.263 */
144
        /* FIXME: In the future H.263+ will have intra prediction */
145
        /* and we are gonna need another way to detect MPEG4      */
146
        if (s->mb_y && !s->h263_pred) {
147
            s->first_gob_line = h263_decode_gob_header(s);
148
        }
149 de6d9b64 Fabrice Bellard
        for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
150
#ifdef DEBUG
151
            printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
152
#endif
153 4949028f Juanjo
            //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
154 de6d9b64 Fabrice Bellard
            /* DCT & quantize */
155
            if (s->h263_msmpeg4) {
156
                msmpeg4_dc_scale(s);
157
            } else if (s->h263_pred) {
158
                h263_dc_scale(s);
159
            } else {
160
                /* default quantization values */
161
                s->y_dc_scale = 8;
162
                s->c_dc_scale = 8;
163
            }
164
165 8bdbae01 Michael Niedermayer
#ifdef HAVE_MMX
166
            if (mm_flags & MM_MMX) {
167
                asm volatile(
168
                        "pxor %%mm7, %%mm7                \n\t"
169
                        "movl $-128*6, %%eax                \n\t"
170
                        "1:                                \n\t"
171
                        "movq %%mm7, (%0, %%eax)        \n\t"
172
                        "movq %%mm7, 8(%0, %%eax)        \n\t"
173
                        "movq %%mm7, 16(%0, %%eax)        \n\t"
174
                        "movq %%mm7, 24(%0, %%eax)        \n\t"
175
                        "addl $32, %%eax                \n\t"
176
                        " js 1b                                \n\t"
177
                        : : "r" (((int)s->block)+128*6)
178
                        : "%eax"
179
                );
180
            }else{
181
                memset(s->block, 0, sizeof(s->block));
182
            }
183
#else
184 b7ec19d3 Fabrice Bellard
            memset(s->block, 0, sizeof(s->block));
185 8bdbae01 Michael Niedermayer
#endif
186 de6d9b64 Fabrice Bellard
            s->mv_dir = MV_DIR_FORWARD;
187
            s->mv_type = MV_TYPE_16X16; 
188
            if (s->h263_msmpeg4) {
189 b7ec19d3 Fabrice Bellard
                if (msmpeg4_decode_mb(s, s->block) < 0)
190 de6d9b64 Fabrice Bellard
                    return -1;
191
            } else {
192 96815ce9 Juanjo
                if (h263_decode_mb(s, s->block) < 0) {
193
                    fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
194 de6d9b64 Fabrice Bellard
                    return -1;
195 96815ce9 Juanjo
                }
196 de6d9b64 Fabrice Bellard
            }
197 b7ec19d3 Fabrice Bellard
            MPV_decode_mb(s, s->block);
198 de6d9b64 Fabrice Bellard
        }
199 bf89e6b1 Fabrice Bellard
        if (avctx->draw_horiz_band) {
200
            UINT8 *src_ptr[3];
201
            int y, h, offset;
202
            y = s->mb_y * 16;
203
            h = s->height - y;
204
            if (h > 16)
205
                h = 16;
206
            offset = y * s->linesize;
207
            src_ptr[0] = s->current_picture[0] + offset;
208
            src_ptr[1] = s->current_picture[1] + (offset >> 2);
209
            src_ptr[2] = s->current_picture[2] + (offset >> 2);
210
            avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
211
                                   y, s->width, h);
212
        }
213 de6d9b64 Fabrice Bellard
    }
214
215
    MPV_frame_end(s);
216
    
217
    pict->data[0] = s->current_picture[0];
218
    pict->data[1] = s->current_picture[1];
219
    pict->data[2] = s->current_picture[2];
220
    pict->linesize[0] = s->linesize;
221
    pict->linesize[1] = s->linesize / 2;
222
    pict->linesize[2] = s->linesize / 2;
223
224
    avctx->quality = s->qscale;
225
    *data_size = sizeof(AVPicture);
226
    return buf_size;
227
}
228
229 bf89e6b1 Fabrice Bellard
AVCodec mpeg4_decoder = {
230
    "mpeg4",
231 de6d9b64 Fabrice Bellard
    CODEC_TYPE_VIDEO,
232 bf89e6b1 Fabrice Bellard
    CODEC_ID_MPEG4,
233 de6d9b64 Fabrice Bellard
    sizeof(MpegEncContext),
234
    h263_decode_init,
235
    NULL,
236
    h263_decode_end,
237
    h263_decode_frame,
238 bf89e6b1 Fabrice Bellard
    CODEC_CAP_DRAW_HORIZ_BAND,
239 de6d9b64 Fabrice Bellard
};
240
241
AVCodec h263_decoder = {
242
    "h263",
243
    CODEC_TYPE_VIDEO,
244
    CODEC_ID_H263,
245
    sizeof(MpegEncContext),
246
    h263_decode_init,
247
    NULL,
248
    h263_decode_end,
249
    h263_decode_frame,
250 bf89e6b1 Fabrice Bellard
    CODEC_CAP_DRAW_HORIZ_BAND,
251 de6d9b64 Fabrice Bellard
};
252
253
AVCodec msmpeg4_decoder = {
254
    "msmpeg4",
255
    CODEC_TYPE_VIDEO,
256
    CODEC_ID_MSMPEG4,
257
    sizeof(MpegEncContext),
258
    h263_decode_init,
259
    NULL,
260
    h263_decode_end,
261
    h263_decode_frame,
262 bf89e6b1 Fabrice Bellard
    CODEC_CAP_DRAW_HORIZ_BAND,
263 de6d9b64 Fabrice Bellard
};
264
265
AVCodec h263i_decoder = {
266
    "h263i",
267
    CODEC_TYPE_VIDEO,
268
    CODEC_ID_H263I,
269
    sizeof(MpegEncContext),
270
    h263_decode_init,
271
    NULL,
272
    h263_decode_end,
273
    h263_decode_frame,
274 bf89e6b1 Fabrice Bellard
    CODEC_CAP_DRAW_HORIZ_BAND,
275 de6d9b64 Fabrice Bellard
};