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_MSMPEG4:
61 /* for h263, we allocate the images after having read the header */
62 if (avctx->codec->id != CODEC_ID_H263)
63 if (MPV_common_init(s) < 0)
66 /* XXX: suppress this matrix init, only needed because using mpeg1
67 dequantize in mmx case */
69 s->non_intra_matrix[i] = default_non_intra_matrix[i];
72 msmpeg4_decode_init_vlc(s);
74 h263_decode_init_vlc(s);
79 static int h263_decode_end(AVCodecContext *avctx)
81 MpegEncContext *s = avctx->priv_data;
87 static int h263_decode_frame(AVCodecContext *avctx,
88 void *data, int *data_size,
89 UINT8 *buf, int buf_size)
91 MpegEncContext *s = avctx->priv_data;
93 AVPicture *pict = data;
96 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
97 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
100 /* no supplementary picture */
106 init_get_bits(&s->gb, buf, buf_size);
109 if (s->h263_msmpeg4) {
110 ret = msmpeg4_decode_picture_header(s);
111 } else if (s->h263_pred) {
112 ret = mpeg4_decode_picture_header(s);
113 } else if (s->h263_intel) {
114 ret = intel_h263_decode_picture_header(s);
116 ret = h263_decode_picture_header(s);
117 /* After H263 header decode we have the height, width, */
118 /* and other parameters. So then we could init the picture */
119 /* FIXME: By the way H263 decoder is evolving it should have */
120 /* an H263EncContext */
121 if (!s->context_initialized) {
122 avctx->width = s->width;
123 avctx->height = s->height;
124 if (MPV_common_init(s) < 0)
126 } else if (s->width != avctx->width || s->height != avctx->height) {
127 /* H.263 could change picture size any time */
129 if (MPV_common_init(s) < 0)
139 printf("qscale=%d\n", s->qscale);
142 /* decode each macroblock */
143 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
144 /* Check for GOB headers on H.263 */
145 /* FIXME: In the future H.263+ will have intra prediction */
146 /* and we are gonna need another way to detect MPEG4 */
147 if (s->mb_y && !s->h263_pred) {
148 s->first_gob_line = h263_decode_gob_header(s);
150 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
152 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
154 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
156 if (s->h263_msmpeg4) {
158 } else if (s->h263_pred) {
161 /* default quantization values */
167 if (mm_flags & MM_MMX) {
169 "pxor %%mm7, %%mm7 \n\t"
170 "movl $-128*6, %%eax \n\t"
172 "movq %%mm7, (%0, %%eax) \n\t"
173 "movq %%mm7, 8(%0, %%eax) \n\t"
174 "movq %%mm7, 16(%0, %%eax) \n\t"
175 "movq %%mm7, 24(%0, %%eax) \n\t"
176 "addl $32, %%eax \n\t"
178 : : "r" (((int)s->block)+128*6)
182 memset(s->block, 0, sizeof(s->block));
185 memset(s->block, 0, sizeof(s->block));
187 s->mv_dir = MV_DIR_FORWARD;
188 s->mv_type = MV_TYPE_16X16;
189 if (s->h263_msmpeg4) {
190 if (msmpeg4_decode_mb(s, s->block) < 0) {
191 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
195 if (h263_decode_mb(s, s->block) < 0) {
196 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
200 MPV_decode_mb(s, s->block);
202 if (avctx->draw_horiz_band) {
209 offset = y * s->linesize;
210 src_ptr[0] = s->current_picture[0] + offset;
211 src_ptr[1] = s->current_picture[1] + (offset >> 2);
212 src_ptr[2] = s->current_picture[2] + (offset >> 2);
213 avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
218 if (s->h263_msmpeg4 && s->pict_type==I_TYPE)
219 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
223 if(s->pict_type==B_TYPE){
224 pict->data[0] = s->current_picture[0];
225 pict->data[1] = s->current_picture[1];
226 pict->data[2] = s->current_picture[2];
228 pict->data[0] = s->last_picture[0];
229 pict->data[1] = s->last_picture[1];
230 pict->data[2] = s->last_picture[2];
232 pict->linesize[0] = s->linesize;
233 pict->linesize[1] = s->linesize / 2;
234 pict->linesize[2] = s->linesize / 2;
236 avctx->quality = s->qscale;
238 /* Return the Picture timestamp as the frame number */
239 /* we substract 1 because it is added on utils.c */
240 avctx->frame_number = s->picture_number - 1;
242 *data_size = sizeof(AVPicture);
246 AVCodec mpeg4_decoder = {
250 sizeof(MpegEncContext),
255 CODEC_CAP_DRAW_HORIZ_BAND,
258 AVCodec h263_decoder = {
262 sizeof(MpegEncContext),
267 CODEC_CAP_DRAW_HORIZ_BAND,
270 AVCodec msmpeg4_decoder = {
274 sizeof(MpegEncContext),
279 CODEC_CAP_DRAW_HORIZ_BAND,
282 AVCodec h263i_decoder = {
286 sizeof(MpegEncContext),
291 CODEC_CAP_DRAW_HORIZ_BAND,