]> git.sesse.net Git - ffmpeg/blob - libavcodec/h263dec.c
msmpeg4v2 encoding
[ffmpeg] / libavcodec / h263dec.c
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_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_H263I:
66         s->h263_intel = 1;
67         break;
68     default:
69         return -1;
70     }
71
72     /* for h263, we allocate the images after having read the header */
73     if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
74         if (MPV_common_init(s) < 0)
75             return -1;
76
77     /* XXX: suppress this matrix init, only needed because using mpeg1
78        dequantize in mmx case */
79     for(i=0;i<64;i++)
80         s->non_intra_matrix[i] = default_non_intra_matrix[i];
81
82     if (s->h263_msmpeg4)
83         msmpeg4_decode_init_vlc(s);
84     else
85         h263_decode_init_vlc(s);
86     
87     return 0;
88 }
89
90 static int h263_decode_end(AVCodecContext *avctx)
91 {
92     MpegEncContext *s = avctx->priv_data;
93
94     MPV_common_end(s);
95     return 0;
96 }
97
98 static int h263_decode_frame(AVCodecContext *avctx, 
99                              void *data, int *data_size,
100                              UINT8 *buf, int buf_size)
101 {
102     MpegEncContext *s = avctx->priv_data;
103     int ret;
104     AVPicture *pict = data; 
105
106 #ifdef DEBUG
107     printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
108     printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
109 #endif
110     
111     /* no supplementary picture */
112     if (buf_size == 0) {
113         *data_size = 0;
114         return 0;
115     }
116
117     init_get_bits(&s->gb, buf, buf_size);
118
119     /* let's go :-) */
120     if (s->h263_msmpeg4) {
121         ret = msmpeg4_decode_picture_header(s);
122     } else if (s->h263_pred) {
123         ret = mpeg4_decode_picture_header(s);
124     } else if (s->h263_intel) {
125         ret = intel_h263_decode_picture_header(s);
126     } else {
127         ret = h263_decode_picture_header(s);
128     }
129
130         /* After H263 & mpeg4 header decode we have the height, width,*/
131         /* and other parameters. So then we could init the picture   */
132         /* FIXME: By the way H263 decoder is evolving it should have */
133         /* an H263EncContext                                         */
134     if (!s->context_initialized) {
135         avctx->width = s->width;
136         avctx->height = s->height;
137         avctx->aspect_ratio_info= s->aspect_ratio_info;
138         if (MPV_common_init(s) < 0)
139             return -1;
140     } else if (s->width != avctx->width || s->height != avctx->height) {
141         /* H.263 could change picture size any time */
142         MPV_common_end(s);
143         if (MPV_common_init(s) < 0)
144             return -1;
145     }
146
147     if (ret < 0)
148         return -1;
149
150     MPV_frame_start(s);
151
152 #ifdef DEBUG
153     printf("qscale=%d\n", s->qscale);
154 #endif
155
156     /* decode each macroblock */
157     s->block_wrap[0]=
158     s->block_wrap[1]=
159     s->block_wrap[2]=
160     s->block_wrap[3]= s->mb_width*2 + 2;
161     s->block_wrap[4]=
162     s->block_wrap[5]= s->mb_width + 2;
163     for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
164         /* Check for GOB headers on H.263 */
165         /* FIXME: In the future H.263+ will have intra prediction */
166         /* and we are gonna need another way to detect MPEG4      */
167         if (s->mb_y && !s->h263_pred) {
168             s->first_gob_line = h263_decode_gob_header(s);
169         }
170
171         s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
172         s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
173         s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
174         s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
175         s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1)                    + s->block_wrap[0]*(s->mb_height*2 + 2);
176         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);
177         for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
178             s->block_index[0]+=2;
179             s->block_index[1]+=2;
180             s->block_index[2]+=2;
181             s->block_index[3]+=2;
182             s->block_index[4]++;
183             s->block_index[5]++;
184 #ifdef DEBUG
185             printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
186 #endif
187             //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
188             /* DCT & quantize */
189             if (s->h263_msmpeg4) {
190                 msmpeg4_dc_scale(s);
191             } else if (s->h263_pred) {
192                 h263_dc_scale(s);
193             } else {
194                 /* default quantization values */
195                 s->y_dc_scale = 8;
196                 s->c_dc_scale = 8;
197             }
198             clear_blocks(s->block[0]);
199             
200             s->mv_dir = MV_DIR_FORWARD;
201             s->mv_type = MV_TYPE_16X16; 
202             if (s->h263_msmpeg4) {
203                 if (msmpeg4_decode_mb(s, s->block) < 0) {
204                     fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
205                     return -1;
206                 }
207             } else {
208                 if (h263_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             }
213             MPV_decode_mb(s, s->block);
214         }
215         if (avctx->draw_horiz_band) {
216             UINT8 *src_ptr[3];
217             int y, h, offset;
218             y = s->mb_y * 16;
219             h = s->height - y;
220             if (h > 16)
221                 h = 16;
222             offset = y * s->linesize;
223             if(s->pict_type==B_TYPE || (!s->has_b_frames)){
224                 src_ptr[0] = s->current_picture[0] + offset;
225                 src_ptr[1] = s->current_picture[1] + (offset >> 2);
226                 src_ptr[2] = s->current_picture[2] + (offset >> 2);
227             } else {
228                 src_ptr[0] = s->last_picture[0] + offset;
229                 src_ptr[1] = s->last_picture[1] + (offset >> 2);
230                 src_ptr[2] = s->last_picture[2] + (offset >> 2);
231             }
232             avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
233                                    y, s->width, h);
234         }
235     }
236     
237     if (s->h263_msmpeg4 && s->pict_type==I_TYPE)
238         if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
239
240     MPV_frame_end(s);
241     
242     if(s->pict_type==B_TYPE || (!s->has_b_frames)){
243         pict->data[0] = s->current_picture[0];
244         pict->data[1] = s->current_picture[1];
245         pict->data[2] = s->current_picture[2];
246     } else {
247         pict->data[0] = s->last_picture[0];
248         pict->data[1] = s->last_picture[1];
249         pict->data[2] = s->last_picture[2];
250     }
251     pict->linesize[0] = s->linesize;
252     pict->linesize[1] = s->linesize / 2;
253     pict->linesize[2] = s->linesize / 2;
254
255     avctx->quality = s->qscale;
256
257     /* Return the Picture timestamp as the frame number */
258     /* we substract 1 because it is added on utils.c    */
259     avctx->frame_number = s->picture_number - 1;
260
261     *data_size = sizeof(AVPicture);
262     return buf_size;
263 }
264
265 AVCodec mpeg4_decoder = {
266     "mpeg4",
267     CODEC_TYPE_VIDEO,
268     CODEC_ID_MPEG4,
269     sizeof(MpegEncContext),
270     h263_decode_init,
271     NULL,
272     h263_decode_end,
273     h263_decode_frame,
274     CODEC_CAP_DRAW_HORIZ_BAND,
275 };
276
277 AVCodec h263_decoder = {
278     "h263",
279     CODEC_TYPE_VIDEO,
280     CODEC_ID_H263,
281     sizeof(MpegEncContext),
282     h263_decode_init,
283     NULL,
284     h263_decode_end,
285     h263_decode_frame,
286     CODEC_CAP_DRAW_HORIZ_BAND,
287 };
288
289 AVCodec msmpeg4v1_decoder = {
290     "msmpeg4v1",
291     CODEC_TYPE_VIDEO,
292     CODEC_ID_MSMPEG4V1,
293     sizeof(MpegEncContext),
294     h263_decode_init,
295     NULL,
296     h263_decode_end,
297     h263_decode_frame,
298     CODEC_CAP_DRAW_HORIZ_BAND,
299 };
300
301 AVCodec msmpeg4v2_decoder = {
302     "msmpeg4v2",
303     CODEC_TYPE_VIDEO,
304     CODEC_ID_MSMPEG4V2,
305     sizeof(MpegEncContext),
306     h263_decode_init,
307     NULL,
308     h263_decode_end,
309     h263_decode_frame,
310     CODEC_CAP_DRAW_HORIZ_BAND,
311 };
312
313 AVCodec msmpeg4v3_decoder = {
314     "msmpeg4",
315     CODEC_TYPE_VIDEO,
316     CODEC_ID_MSMPEG4V3,
317     sizeof(MpegEncContext),
318     h263_decode_init,
319     NULL,
320     h263_decode_end,
321     h263_decode_frame,
322     CODEC_CAP_DRAW_HORIZ_BAND,
323 };
324
325 AVCodec h263i_decoder = {
326     "h263i",
327     CODEC_TYPE_VIDEO,
328     CODEC_ID_H263I,
329     sizeof(MpegEncContext),
330     h263_decode_init,
331     NULL,
332     h263_decode_end,
333     h263_decode_frame,
334     CODEC_CAP_DRAW_HORIZ_BAND,
335 };
336