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 */
48 s->has_b_frames = 1; //default, might be overriden in the vol header during header parsing
50 case CODEC_ID_MSMPEG4V1:
55 case CODEC_ID_MSMPEG4V2:
60 case CODEC_ID_MSMPEG4V3:
76 s->codec_id= avctx->codec->id;
77 avctx->mbskip_table= s->mbskip_table;
79 /* for h263, we allocate the images after having read the header */
80 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
81 if (MPV_common_init(s) < 0)
85 msmpeg4_decode_init_vlc(s);
87 h263_decode_init_vlc(s);
92 static int h263_decode_end(AVCodecContext *avctx)
94 MpegEncContext *s = avctx->priv_data;
100 static int h263_decode_frame(AVCodecContext *avctx,
101 void *data, int *data_size,
102 UINT8 *buf, int buf_size)
104 MpegEncContext *s = avctx->priv_data;
106 AVPicture *pict = data;
109 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
110 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
113 s->hurry_up= avctx->hurry_up;
115 /* no supplementary picture */
121 if(s->bitstream_buffer_size) //divx 5.01+ frame reorder
122 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size);
124 init_get_bits(&s->gb, buf, buf_size);
127 if (s->h263_msmpeg4) {
128 ret = msmpeg4_decode_picture_header(s);
129 } else if (s->h263_pred) {
130 ret = mpeg4_decode_picture_header(s);
131 s->has_b_frames= !s->low_delay;
132 } else if (s->h263_intel) {
133 ret = intel_h263_decode_picture_header(s);
135 ret = h263_decode_picture_header(s);
138 /* After H263 & mpeg4 header decode we have the height, width,*/
139 /* and other parameters. So then we could init the picture */
140 /* FIXME: By the way H263 decoder is evolving it should have */
141 /* an H263EncContext */
142 if (!s->context_initialized) {
143 avctx->width = s->width;
144 avctx->height = s->height;
145 avctx->aspect_ratio_info= s->aspect_ratio_info;
146 if (MPV_common_init(s) < 0)
148 } else if (s->width != avctx->width || s->height != avctx->height) {
149 /* H.263 could change picture size any time */
151 if (MPV_common_init(s) < 0)
155 if(ret==FRAME_SKIPED) return 0;
158 /* skip b frames if we dont have reference frames */
159 if(s->num_available_buffers<2 && s->pict_type==B_TYPE) return 0;
160 /* skip b frames if we are in a hurry */
161 if(s->hurry_up && s->pict_type==B_TYPE) return 0;
166 printf("qscale=%d\n", s->qscale);
169 /* decode each macroblock */
173 s->block_wrap[3]= s->mb_width*2 + 2;
175 s->block_wrap[5]= s->mb_width + 2;
176 for(s->mb_y=0; s->mb_y < s->mb_height; s->mb_y++) {
177 /* Check for GOB headers on H.263 */
178 /* FIXME: In the future H.263+ will have intra prediction */
179 /* and we are gonna need another way to detect MPEG4 */
180 if (s->mb_y && !s->h263_pred) {
181 s->first_gob_line = h263_decode_gob_header(s);
184 s->block_index[0]= s->block_wrap[0]*(s->mb_y*2 + 1) - 1;
185 s->block_index[1]= s->block_wrap[0]*(s->mb_y*2 + 1);
186 s->block_index[2]= s->block_wrap[0]*(s->mb_y*2 + 2) - 1;
187 s->block_index[3]= s->block_wrap[0]*(s->mb_y*2 + 2);
188 s->block_index[4]= s->block_wrap[4]*(s->mb_y + 1) + s->block_wrap[0]*(s->mb_height*2 + 2);
189 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);
190 for(s->mb_x=0; s->mb_x < s->mb_width; s->mb_x++) {
191 s->block_index[0]+=2;
192 s->block_index[1]+=2;
193 s->block_index[2]+=2;
194 s->block_index[3]+=2;
198 printf("**mb x=%d y=%d\n", s->mb_x, s->mb_y);
200 //fprintf(stderr,"\nFrame: %d\tMB: %d",avctx->frame_number, (s->mb_y * s->mb_width) + s->mb_x);
202 if (s->h263_pred && s->msmpeg4_version!=2) {
205 /* default quantization values */
209 clear_blocks(s->block[0]);
211 s->mv_dir = MV_DIR_FORWARD;
212 s->mv_type = MV_TYPE_16X16;
213 if (s->h263_msmpeg4) {
214 if (msmpeg4_decode_mb(s, s->block) < 0) {
215 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
219 if (h263_decode_mb(s, s->block) < 0) {
220 fprintf(stderr,"\nError at MB: %d\n", (s->mb_y * s->mb_width) + s->mb_x);
224 MPV_decode_mb(s, s->block);
226 if ( avctx->draw_horiz_band
227 && (s->num_available_buffers>=1 || (!s->has_b_frames)) ) {
234 offset = y * s->linesize;
235 if(s->pict_type==B_TYPE || (!s->has_b_frames)){
236 src_ptr[0] = s->current_picture[0] + offset;
237 src_ptr[1] = s->current_picture[1] + (offset >> 2);
238 src_ptr[2] = s->current_picture[2] + (offset >> 2);
240 src_ptr[0] = s->last_picture[0] + offset;
241 src_ptr[1] = s->last_picture[1] + (offset >> 2);
242 src_ptr[2] = s->last_picture[2] + (offset >> 2);
244 avctx->draw_horiz_band(avctx, src_ptr, s->linesize,
249 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
250 if(msmpeg4_decode_ext_header(s, buf_size) < 0) return -1;
252 /* divx 5.01+ bistream reorder stuff */
253 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0){
254 int current_pos= get_bits_count(&s->gb)/8;
255 if( buf_size - current_pos > 5
256 && buf_size - current_pos < BITSTREAM_BUFFER_SIZE){
257 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
258 s->bitstream_buffer_size= buf_size - current_pos;
261 s->bitstream_buffer_size=0;
265 if(s->pict_type==B_TYPE || (!s->has_b_frames)){
266 pict->data[0] = s->current_picture[0];
267 pict->data[1] = s->current_picture[1];
268 pict->data[2] = s->current_picture[2];
270 pict->data[0] = s->last_picture[0];
271 pict->data[1] = s->last_picture[1];
272 pict->data[2] = s->last_picture[2];
274 pict->linesize[0] = s->linesize;
275 pict->linesize[1] = s->linesize / 2;
276 pict->linesize[2] = s->linesize / 2;
278 avctx->quality = s->qscale;
280 /* Return the Picture timestamp as the frame number */
281 /* we substract 1 because it is added on utils.c */
282 avctx->frame_number = s->picture_number - 1;
284 /* dont output the last pic after seeking
285 note we allready added +1 for the current pix in MPV_frame_end(s) */
286 if(s->num_available_buffers>=2 || (!s->has_b_frames))
287 *data_size = sizeof(AVPicture);
292 AVCodec mpeg4_decoder = {
296 sizeof(MpegEncContext),
301 CODEC_CAP_DRAW_HORIZ_BAND,
304 AVCodec h263_decoder = {
308 sizeof(MpegEncContext),
313 CODEC_CAP_DRAW_HORIZ_BAND,
316 AVCodec msmpeg4v1_decoder = {
320 sizeof(MpegEncContext),
325 CODEC_CAP_DRAW_HORIZ_BAND,
328 AVCodec msmpeg4v2_decoder = {
332 sizeof(MpegEncContext),
337 CODEC_CAP_DRAW_HORIZ_BAND,
340 AVCodec msmpeg4v3_decoder = {
344 sizeof(MpegEncContext),
349 CODEC_CAP_DRAW_HORIZ_BAND,
352 AVCodec wmv1_decoder = {
356 sizeof(MpegEncContext),
361 CODEC_CAP_DRAW_HORIZ_BAND,
364 AVCodec h263i_decoder = {
368 sizeof(MpegEncContext),
373 CODEC_CAP_DRAW_HORIZ_BAND,