Statistics
| Branch: | Revision:

ffmpeg / libavcodec / h263dec.c @ 4af5e4ef

History | View | Annotate | Download (5.05 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
    
32
    s->out_format = FMT_H263;
33

    
34
    s->width = avctx->width;
35
    s->height = avctx->height;
36

    
37
    /* select sub codec */
38
    switch(avctx->codec->id) {
39
    case CODEC_ID_H263:
40
        break;
41
    case CODEC_ID_OPENDIVX:
42
        s->time_increment_bits = 4; /* default value for broken headers */
43
        s->h263_pred = 1;
44
        break;
45
    case CODEC_ID_MSMPEG4:
46
        s->h263_msmpeg4 = 1;
47
        s->h263_pred = 1;
48
        break;
49
    case CODEC_ID_H263I:
50
        s->h263_intel = 1;
51
        break;
52
    default:
53
        return -1;
54
    }
55

    
56
    /* for h263, we allocate the images after having read the header */
57
    if (MPV_common_init(s) < 0)
58
        return -1;
59

    
60
    if (s->h263_msmpeg4)
61
        msmpeg4_decode_init_vlc(s);
62
    else
63
        h263_decode_init_vlc(s);
64
    
65
    return 0;
66
}
67

    
68
static int h263_decode_end(AVCodecContext *avctx)
69
{
70
    MpegEncContext *s = avctx->priv_data;
71

    
72
    MPV_common_end(s);
73
    return 0;
74
}
75

    
76
static int h263_decode_frame(AVCodecContext *avctx, 
77
                             void *data, int *data_size,
78
                             UINT8 *buf, int buf_size)
79
{
80
    MpegEncContext *s = avctx->priv_data;
81
    int ret;
82
    AVPicture *pict = data; 
83

    
84
#ifdef DEBUG
85
    printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
86
    printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
87
#endif
88

    
89
    /* no supplementary picture */
90
    if (buf_size == 0) {
91
        *data_size = 0;
92
        return 0;
93
    }
94

    
95
    init_get_bits(&s->gb, buf, buf_size);
96

    
97
    /* let's go :-) */
98
    if (s->h263_msmpeg4) {
99
        ret = msmpeg4_decode_picture_header(s);
100
    } else if (s->h263_pred) {
101
        ret = mpeg4_decode_picture_header(s);
102
    } else if (s->h263_intel) {
103
        ret = intel_h263_decode_picture_header(s);
104
    } else {
105
        ret = h263_decode_picture_header(s);
106
    }
107
    if (ret < 0)
108
        return -1;
109

    
110
    /* make sure we start with an I-Frame */
111
    if (!s->slice_height && (s->pict_type != I_TYPE))
112
        return -1;
113

    
114
    MPV_frame_start(s);
115

    
116
#ifdef DEBUG
117
    printf("qscale=%d\n", s->qscale);
118
#endif
119

    
120
    /* decode each macroblock */
121
    for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
122
        for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
123
#ifdef DEBUG
124
            printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
125
#endif
126
            /* DCT & quantize */
127
            if (s->h263_msmpeg4) {
128
                msmpeg4_dc_scale(s);
129
            } else if (s->h263_pred) {
130
                h263_dc_scale(s);
131
            } else {
132
                /* default quantization values */
133
                s->y_dc_scale = 8;
134
                s->c_dc_scale = 8;
135
            }
136

    
137
            memset(s->block, 0, sizeof(s->block));
138
            s->mv_dir = MV_DIR_FORWARD;
139
            s->mv_type = MV_TYPE_16X16; 
140
            if (s->h263_msmpeg4) {
141
                if (msmpeg4_decode_mb(s, s->block) < 0)
142
                    return -1;
143
            } else {
144
                if (h263_decode_mb(s, s->block) < 0)
145
                    return -1;
146
            }
147
            MPV_decode_mb(s, s->block);
148
        }
149
    }
150

    
151
    MPV_frame_end(s);
152
    
153
    pict->data[0] = s->current_picture[0];
154
    pict->data[1] = s->current_picture[1];
155
    pict->data[2] = s->current_picture[2];
156
    pict->linesize[0] = s->linesize;
157
    pict->linesize[1] = s->linesize / 2;
158
    pict->linesize[2] = s->linesize / 2;
159

    
160
    avctx->quality = s->qscale;
161
    *data_size = sizeof(AVPicture);
162
    return buf_size;
163
}
164

    
165
AVCodec opendivx_decoder = {
166
    "opendivx",
167
    CODEC_TYPE_VIDEO,
168
    CODEC_ID_OPENDIVX,
169
    sizeof(MpegEncContext),
170
    h263_decode_init,
171
    NULL,
172
    h263_decode_end,
173
    h263_decode_frame,
174
};
175

    
176
AVCodec h263_decoder = {
177
    "h263",
178
    CODEC_TYPE_VIDEO,
179
    CODEC_ID_H263,
180
    sizeof(MpegEncContext),
181
    h263_decode_init,
182
    NULL,
183
    h263_decode_end,
184
    h263_decode_frame,
185
};
186

    
187
AVCodec msmpeg4_decoder = {
188
    "msmpeg4",
189
    CODEC_TYPE_VIDEO,
190
    CODEC_ID_MSMPEG4,
191
    sizeof(MpegEncContext),
192
    h263_decode_init,
193
    NULL,
194
    h263_decode_end,
195
    h263_decode_frame,
196
};
197

    
198
AVCodec h263i_decoder = {
199
    "h263i",
200
    CODEC_TYPE_VIDEO,
201
    CODEC_ID_H263I,
202
    sizeof(MpegEncContext),
203
    h263_decode_init,
204
    NULL,
205
    h263_decode_end,
206
    h263_decode_frame,
207
};
208