3 * Copyright (c) 2001 Gerard Lantau.
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.
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.
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.
24 #include "mpegvideo.h"
28 static int h263_decode_init(AVCodecContext *avctx)
30 MpegEncContext *s = avctx->priv_data;
34 s->out_format = FMT_H263;
36 s->width = avctx->width;
37 s->height = avctx->height;
39 /* select sub codec */
40 switch(avctx->codec->id) {
43 s->first_gob_line = 0;
46 s->time_increment_bits = 4; /* default value for broken headers */
50 case CODEC_ID_MSMPEG4V1:
55 case CODEC_ID_MSMPEG4V2:
60 case CODEC_ID_MSMPEG4V3:
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)
77 /* XXX: suppress this matrix init, only needed because using mpeg1
78 dequantize in mmx case */
80 s->non_intra_matrix[i] = default_non_intra_matrix[i];
83 msmpeg4_decode_init_vlc(s);
85 h263_decode_init_vlc(s);
90 static int h263_decode_end(AVCodecContext *avctx)
92 MpegEncContext *s = avctx->priv_data;
98 static int h263_decode_frame(AVCodecContext *avctx,
99 void *data, int *data_size,
100 UINT8 *buf, int buf_size)
102 MpegEncContext *s = avctx->priv_data;
104 AVPicture *pict = data;
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]);
111 /* no supplementary picture */
117 init_get_bits(&s->gb, buf, buf_size);
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);
127 ret = h263_decode_picture_header(s);
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)
140 } else if (s->width != avctx->width || s->height != avctx->height) {
141 /* H.263 could change picture size any time */
143 if (MPV_common_init(s) < 0)
153 printf("qscale=%d\n", s->qscale);
156 /* decode each macroblock */
160 s->block_wrap[3]= s->mb_width*2 + 2;
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);
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;
185 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
187 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
189 if (s->h263_msmpeg4) {
191 } else if (s->h263_pred) {
194 /* default quantization values */
198 clear_blocks(s->block[0]);
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);
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);
213 MPV_decode_mb(s, s->block);
215 if (avctx->draw_horiz_band) {
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);
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);
232 avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
237 if (s->h263_msmpeg4 && s->pict_type==I_TYPE)
238 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
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];
247 pict->data[0] = s->last_picture[0];
248 pict->data[1] = s->last_picture[1];
249 pict->data[2] = s->last_picture[2];
251 pict->linesize[0] = s->linesize;
252 pict->linesize[1] = s->linesize / 2;
253 pict->linesize[2] = s->linesize / 2;
255 avctx->quality = s->qscale;
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;
261 *data_size = sizeof(AVPicture);
265 AVCodec mpeg4_decoder = {
269 sizeof(MpegEncContext),
274 CODEC_CAP_DRAW_HORIZ_BAND,
277 AVCodec h263_decoder = {
281 sizeof(MpegEncContext),
286 CODEC_CAP_DRAW_HORIZ_BAND,
289 AVCodec msmpeg4v1_decoder = {
293 sizeof(MpegEncContext),
298 CODEC_CAP_DRAW_HORIZ_BAND,
301 AVCodec msmpeg4v2_decoder = {
305 sizeof(MpegEncContext),
310 CODEC_CAP_DRAW_HORIZ_BAND,
313 AVCodec msmpeg4v3_decoder = {
317 sizeof(MpegEncContext),
322 CODEC_CAP_DRAW_HORIZ_BAND,
325 AVCodec h263i_decoder = {
329 sizeof(MpegEncContext),
334 CODEC_CAP_DRAW_HORIZ_BAND,