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 */
49 case CODEC_ID_MSMPEG4:
60 /* for h263, we allocate the images after having read the header */
61 if (avctx->codec->id != CODEC_ID_H263)
62 if (MPV_common_init(s) < 0)
65 /* XXX: suppress this matrix init, only needed because using mpeg1
66 dequantize in mmx case */
68 s->non_intra_matrix[i] = default_non_intra_matrix[i];
71 msmpeg4_decode_init_vlc(s);
73 h263_decode_init_vlc(s);
78 static int h263_decode_end(AVCodecContext *avctx)
80 MpegEncContext *s = avctx->priv_data;
86 static int h263_decode_frame(AVCodecContext *avctx,
87 void *data, int *data_size,
88 UINT8 *buf, int buf_size)
90 MpegEncContext *s = avctx->priv_data;
92 AVPicture *pict = data;
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]);
99 /* no supplementary picture */
105 init_get_bits(&s->gb, buf, buf_size);
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);
115 ret = h263_decode_picture_header(s);
116 /* 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 avctx->width = s->width;
122 avctx->height = s->height;
123 if (MPV_common_init(s) < 0)
125 } else if (s->width != avctx->width || s->height != avctx->height) {
126 /* H.263 could change picture size any time */
128 if (MPV_common_init(s) < 0)
138 printf("qscale=%d\n", s->qscale);
141 /* decode each macroblock */
142 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
143 /* 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);
149 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
151 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
153 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
155 if (s->h263_msmpeg4) {
157 } else if (s->h263_pred) {
160 /* default quantization values */
165 memset(s->block, 0, sizeof(s->block));
166 s->mv_dir = MV_DIR_FORWARD;
167 s->mv_type = MV_TYPE_16X16;
168 if (s->h263_msmpeg4) {
169 if (msmpeg4_decode_mb(s, s->block) < 0)
172 if (h263_decode_mb(s, s->block) < 0) {
173 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
177 MPV_decode_mb(s, s->block);
179 if (avctx->draw_horiz_band) {
186 offset = y * s->linesize;
187 src_ptr[0] = s->current_picture[0] + offset;
188 src_ptr[1] = s->current_picture[1] + (offset >> 2);
189 src_ptr[2] = s->current_picture[2] + (offset >> 2);
190 avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
197 pict->data[0] = s->current_picture[0];
198 pict->data[1] = s->current_picture[1];
199 pict->data[2] = s->current_picture[2];
200 pict->linesize[0] = s->linesize;
201 pict->linesize[1] = s->linesize / 2;
202 pict->linesize[2] = s->linesize / 2;
204 avctx->quality = s->qscale;
205 *data_size = sizeof(AVPicture);
209 AVCodec mpeg4_decoder = {
213 sizeof(MpegEncContext),
218 CODEC_CAP_DRAW_HORIZ_BAND,
221 AVCodec h263_decoder = {
225 sizeof(MpegEncContext),
230 CODEC_CAP_DRAW_HORIZ_BAND,
233 AVCodec msmpeg4_decoder = {
237 sizeof(MpegEncContext),
242 CODEC_CAP_DRAW_HORIZ_BAND,
245 AVCodec h263i_decoder = {
249 sizeof(MpegEncContext),
254 CODEC_CAP_DRAW_HORIZ_BAND,