malloc padding to avoid reading past the malloc()ed area.
[libav.git] / libavcodec / h263dec.c
CommitLineData
de6d9b64 1/*
983e3246 2 * H.263 decoder
ff4ec49e 3 * Copyright (c) 2001 Fabrice Bellard.
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 5 *
ff4ec49e
FB
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
de6d9b64 10 *
ff4ec49e 11 * This library is distributed in the hope that it will be useful,
de6d9b64 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
de6d9b64 15 *
ff4ec49e
FB
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
de6d9b64 19 */
983e3246
MN
20
21/**
22 * @file h263dec.c
23 * H.263 decoder.
24 */
25
de6d9b64 26#include "avcodec.h"
6000abfa 27#include "dsputil.h"
de6d9b64
FB
28#include "mpegvideo.h"
29
30//#define DEBUG
02734c6a 31//#define PRINT_FRAME_TIME
de6d9b64 32
1457ab52 33int ff_h263_decode_init(AVCodecContext *avctx)
de6d9b64
FB
34{
35 MpegEncContext *s = avctx->priv_data;
519c2b6d 36
bf89e6b1 37 s->avctx = avctx;
de6d9b64
FB
38 s->out_format = FMT_H263;
39
21adafec
MN
40 s->width = avctx->coded_width;
41 s->height = avctx->coded_height;
92ba5ffb 42 s->workaround_bugs= avctx->workaround_bugs;
de6d9b64 43
4d2858de 44 // set defaults
3edcacde 45 MPV_decode_defaults(s);
4d2858de 46 s->quant_precision=5;
4d2858de 47 s->decode_mb= ff_h263_decode_mb;
4e00e76b 48 s->low_delay= 1;
5fe061a0 49 avctx->pix_fmt= PIX_FMT_YUV420P;
a573cc27 50 s->unrestricted_mv= 1;
4d2858de 51
de6d9b64
FB
52 /* select sub codec */
53 switch(avctx->codec->id) {
54 case CODEC_ID_H263:
68b94c35 55 s->unrestricted_mv= 0;
de6d9b64 56 break;
bf89e6b1 57 case CODEC_ID_MPEG4:
fcfee63b 58 s->decode_mb= ff_mpeg4_decode_mb;
de6d9b64
FB
59 s->time_increment_bits = 4; /* default value for broken headers */
60 s->h263_pred = 1;
4e00e76b 61 s->low_delay = 0; //default, might be overriden in the vol header during header parsing
de6d9b64 62 break;
84afee34 63 case CODEC_ID_MSMPEG4V1:
de6d9b64
FB
64 s->h263_msmpeg4 = 1;
65 s->h263_pred = 1;
84afee34
MN
66 s->msmpeg4_version=1;
67 break;
68 case CODEC_ID_MSMPEG4V2:
69 s->h263_msmpeg4 = 1;
70 s->h263_pred = 1;
71 s->msmpeg4_version=2;
72 break;
73 case CODEC_ID_MSMPEG4V3:
74 s->h263_msmpeg4 = 1;
75 s->h263_pred = 1;
76 s->msmpeg4_version=3;
de6d9b64 77 break;
e1a9dbff
MN
78 case CODEC_ID_WMV1:
79 s->h263_msmpeg4 = 1;
80 s->h263_pred = 1;
81 s->msmpeg4_version=4;
82 break;
8f8402e4
MN
83 case CODEC_ID_WMV2:
84 s->h263_msmpeg4 = 1;
85 s->h263_pred = 1;
86 s->msmpeg4_version=5;
87 break;
0d33db8a 88 case CODEC_ID_WMV3:
89 s->h263_msmpeg4 = 1;
90 s->h263_pred = 1;
91 s->msmpeg4_version=6;
92 break;
de6d9b64 93 case CODEC_ID_H263I:
de6d9b64 94 break;
d4f5d74a
GM
95 case CODEC_ID_FLV1:
96 s->h263_flv = 1;
97 break;
de6d9b64
FB
98 default:
99 return -1;
100 }
d7e9533a 101 s->codec_id= avctx->codec->id;
93a21abd 102
de6d9b64 103 /* for h263, we allocate the images after having read the header */
488fa19a 104 if (avctx->codec->id != CODEC_ID_H263 && avctx->codec->id != CODEC_ID_MPEG4)
4949028f
J
105 if (MPV_common_init(s) < 0)
106 return -1;
de6d9b64
FB
107
108 if (s->h263_msmpeg4)
8f8402e4 109 ff_msmpeg4_decode_init(s);
de6d9b64
FB
110 else
111 h263_decode_init_vlc(s);
112
113 return 0;
114}
115
1457ab52 116int ff_h263_decode_end(AVCodecContext *avctx)
de6d9b64
FB
117{
118 MpegEncContext *s = avctx->priv_data;
119
120 MPV_common_end(s);
121 return 0;
122}
123
7866eeff 124/**
bb628dae 125 * returns the number of bytes consumed for building the current frame
7866eeff
MN
126 */
127static int get_consumed_bytes(MpegEncContext *s, int buf_size){
128 int pos= (get_bits_count(&s->gb)+7)>>3;
d7425f59 129
d5a21172 130 if(s->divx_packed){
7866eeff
MN
131 //we would have to scan through the whole buf to handle the weird reordering ...
132 return buf_size;
d7425f59
MN
133 }else if(s->flags&CODEC_FLAG_TRUNCATED){
134 pos -= s->parse_context.last_index;
135 if(pos<0) pos=0; // padding is not really read so this might be -1
136 return pos;
7866eeff
MN
137 }else{
138 if(pos==0) pos=1; //avoid infinite loops (i doubt thats needed but ...)
77277835 139 if(pos+10>buf_size) pos=buf_size; // oops ;)
7866eeff
MN
140
141 return pos;
142 }
143}
144
4d2858de 145static int decode_slice(MpegEncContext *s){
46b4feec 146 const int part_mask= s->partitioned_frame ? (AC_END|AC_ERROR) : 0x7F;
af2a9e8f 147 const int mb_size= 16>>s->avctx->lowres;
4d2858de
MN
148 s->last_resync_gb= s->gb;
149 s->first_slice_line= 1;
150
151 s->resync_mb_x= s->mb_x;
152 s->resync_mb_y= s->mb_y;
153
554daa24 154 ff_set_qscale(s, s->qscale);
4d2858de
MN
155
156 if(s->partitioned_frame){
157 const int qscale= s->qscale;
158
159 if(s->codec_id==CODEC_ID_MPEG4){
160 if(ff_mpeg4_decode_partitions(s) < 0)
161 return -1;
162 }
163
d99ce8d7 164 /* restore variables which were modified */
4d2858de
MN
165 s->first_slice_line=1;
166 s->mb_x= s->resync_mb_x;
167 s->mb_y= s->resync_mb_y;
554daa24 168 ff_set_qscale(s, qscale);
4d2858de
MN
169 }
170
171 for(; s->mb_y < s->mb_height; s->mb_y++) {
172 /* per-row end of slice checks */
173 if(s->msmpeg4_version){
174 if(s->resync_mb_y + s->slice_height == s->mb_y){
46b4feec
MN
175 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
176
4d2858de
MN
177 return 0;
178 }
179 }
180
181 if(s->msmpeg4_version==1){
182 s->last_dc[0]=
183 s->last_dc[1]=
184 s->last_dc[2]= 128;
185 }
186
187 ff_init_block_index(s);
188 for(; s->mb_x < s->mb_width; s->mb_x++) {
189 int ret;
190
191 ff_update_block_index(s);
192
193 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
194 s->first_slice_line=0;
195 }
196
197 /* DCT & quantize */
dee6dde6 198
4d2858de
MN
199 s->mv_dir = MV_DIR_FORWARD;
200 s->mv_type = MV_TYPE_16X16;
160d679c 201// s->mb_skipped = 0;
4d2858de
MN
202//printf("%d %d %06X\n", ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
203 ret= s->decode_mb(s, s->block);
4d2858de 204
7d1c3fc1
MN
205 if (s->pict_type!=B_TYPE)
206 ff_h263_update_motion_val(s);
207
4d2858de 208 if(ret<0){
7bc9090a 209 const int xy= s->mb_x + s->mb_y*s->mb_stride;
4d2858de 210 if(ret==SLICE_END){
aef95c8c 211 MPV_decode_mb(s, s->block);
332f9ac4
MN
212 if(s->loop_filter)
213 ff_h263_loop_filter(s);
aef95c8c 214
35aa136a 215//printf("%d %d %d %06X\n", s->mb_x, s->mb_y, s->gb.size*8 - get_bits_count(&s->gb), show_bits(&s->gb, 24));
46b4feec 216 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
a61f8265
MN
217
218 s->padding_bug_score--;
219
4d2858de
MN
220 if(++s->mb_x >= s->mb_width){
221 s->mb_x=0;
af2a9e8f 222 ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
4d2858de
MN
223 s->mb_y++;
224 }
225 return 0;
226 }else if(ret==SLICE_NOEND){
9b879566 227 av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
46b4feec 228 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x+1, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
4d2858de
MN
229 return -1;
230 }
9b879566 231 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
46b4feec 232 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_ERROR|DC_ERROR|MV_ERROR)&part_mask);
4d2858de
MN
233
234 return -1;
235 }
aef95c8c
MN
236
237 MPV_decode_mb(s, s->block);
332f9ac4
MN
238 if(s->loop_filter)
239 ff_h263_loop_filter(s);
4d2858de
MN
240 }
241
af2a9e8f 242 ff_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
4d2858de 243
4d2858de
MN
244 s->mb_x= 0;
245 }
246
247 assert(s->mb_x==0 && s->mb_y==s->mb_height);
248
a61f8265
MN
249 /* try to detect the padding bug */
250 if( s->codec_id==CODEC_ID_MPEG4
251 && (s->workaround_bugs&FF_BUG_AUTODETECT)
68f593b4
MN
252 && s->gb.size_in_bits - get_bits_count(&s->gb) >=0
253 && s->gb.size_in_bits - get_bits_count(&s->gb) < 48
96d9cb22 254// && !s->resync_marker
a61f8265
MN
255 && !s->data_partitioning){
256
257 const int bits_count= get_bits_count(&s->gb);
68f593b4 258 const int bits_left = s->gb.size_in_bits - bits_count;
a61f8265 259
650faac6
MN
260 if(bits_left==0){
261 s->padding_bug_score+=16;
8671359b 262 } else if(bits_left != 1){
a61f8265
MN
263 int v= show_bits(&s->gb, 8);
264 v|= 0x7F >> (7-(bits_count&7));
8671359b 265
4940ed6a 266 if(v==0x7F && bits_left<=8)
a61f8265 267 s->padding_bug_score--;
4940ed6a
MN
268 else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16)
269 s->padding_bug_score+= 4;
a61f8265
MN
270 else
271 s->padding_bug_score++;
650faac6 272 }
a61f8265 273 }
4940ed6a
MN
274
275 if(s->workaround_bugs&FF_BUG_AUTODETECT){
503a4725 276 if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version || !s->resync_marker)*/)
4940ed6a
MN
277 s->workaround_bugs |= FF_BUG_NO_PADDING;
278 else
279 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
280 }
a61f8265 281
bb628dae 282 // handle formats which don't have unique end markers
cb7c98d6 283 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
68f593b4 284 int left= s->gb.size_in_bits - get_bits_count(&s->gb);
4d2858de
MN
285 int max_extra=7;
286
287 /* no markers in M$ crap */
288 if(s->msmpeg4_version && s->pict_type==I_TYPE)
289 max_extra+= 17;
290
291 /* buggy padding but the frame should still end approximately at the bitstream end */
cb7c98d6 292 if((s->workaround_bugs&FF_BUG_NO_PADDING) && s->error_resilience>=3)
4d2858de 293 max_extra+= 48;
cb7c98d6 294 else if((s->workaround_bugs&FF_BUG_NO_PADDING))
4d2858de
MN
295 max_extra+= 256*256*256*64;
296
297 if(left>max_extra){
9b879566 298 av_log(s->avctx, AV_LOG_ERROR, "discarding %d junk bits at end, next would be %X\n", left, show_bits(&s->gb, 24));
4d2858de
MN
299 }
300 else if(left<0){
9b879566 301 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
4d2858de 302 }else
46b4feec 303 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, AC_END|DC_END|MV_END);
4d2858de
MN
304
305 return 0;
306 }
a61f8265 307
9b879566 308 av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
68f593b4 309 s->gb.size_in_bits - get_bits_count(&s->gb),
9b6a5b87 310 show_bits(&s->gb, 24), s->padding_bug_score);
46b4feec
MN
311
312 ff_er_add_slice(s, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END)&part_mask);
313
4d2858de
MN
314 return -1;
315}
316
d7425f59
MN
317/**
318 * finds the end of the current frame in the bitstream.
319 * @return the position of the first byte of the next frame, or -1
320 */
e4cb187d 321int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
d7425f59
MN
322 int vop_found, i;
323 uint32_t state;
324
325 vop_found= pc->frame_start_found;
326 state= pc->state;
327
328 i=0;
329 if(!vop_found){
330 for(i=0; i<buf_size; i++){
331 state= (state<<8) | buf[i];
332 if(state == 0x1B6){
333 i++;
334 vop_found=1;
335 break;
336 }
337 }
338 }
80097bbf 339
e4cb187d
MN
340 if(vop_found){
341 /* EOF considered as end of frame */
342 if (buf_size == 0)
343 return 0;
344 for(; i<buf_size; i++){
345 state= (state<<8) | buf[i];
346 if((state&0xFFFFFF00) == 0x100){
347 pc->frame_start_found=0;
348 pc->state=-1;
349 return i-3;
350 }
d7425f59
MN
351 }
352 }
353 pc->frame_start_found= vop_found;
354 pc->state= state;
067ff8b1 355 return END_NOT_FOUND;
d7425f59
MN
356}
357
e4cb187d 358static int h263_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
d07f9043
MN
359 int vop_found, i;
360 uint32_t state;
361
362 vop_found= pc->frame_start_found;
363 state= pc->state;
364
365 i=0;
366 if(!vop_found){
367 for(i=0; i<buf_size; i++){
368 state= (state<<8) | buf[i];
369 if(state>>(32-22) == 0x20){
370 i++;
371 vop_found=1;
372 break;
373 }
374 }
375 }
376
377 if(vop_found){
378 for(; i<buf_size; i++){
379 state= (state<<8) | buf[i];
380 if(state>>(32-22) == 0x20){
381 pc->frame_start_found=0;
382 pc->state=-1;
383 return i-3;
384 }
385 }
386 }
387 pc->frame_start_found= vop_found;
388 pc->state= state;
389
390 return END_NOT_FOUND;
391}
392
e4cb187d
MN
393static int h263_parse(AVCodecParserContext *s,
394 AVCodecContext *avctx,
395 uint8_t **poutbuf, int *poutbuf_size,
396 const uint8_t *buf, int buf_size)
397{
398 ParseContext *pc = s->priv_data;
399 int next;
400
401 next= h263_find_frame_end(pc, buf, buf_size);
402
403 if (ff_combine_frame(pc, next, (uint8_t **)&buf, &buf_size) < 0) {
404 *poutbuf = NULL;
405 *poutbuf_size = 0;
406 return buf_size;
407 }
408
409 *poutbuf = (uint8_t *)buf;
410 *poutbuf_size = buf_size;
411 return next;
412}
413
1457ab52 414int ff_h263_decode_frame(AVCodecContext *avctx,
de6d9b64 415 void *data, int *data_size,
0c1a9eda 416 uint8_t *buf, int buf_size)
de6d9b64
FB
417{
418 MpegEncContext *s = avctx->priv_data;
1df1df0b 419 int ret;
492cd3a9 420 AVFrame *pict = data;
5d3cea3a 421
02734c6a
MN
422#ifdef PRINT_FRAME_TIME
423uint64_t time= rdtsc();
424#endif
de6d9b64
FB
425#ifdef DEBUG
426 printf("*****frame %d size=%d\n", avctx->frame_number, buf_size);
427 printf("bytes=%x %x %x %x\n", buf[0], buf[1], buf[2], buf[3]);
428#endif
b50eef3a 429 s->flags= avctx->flags;
303e50e6 430 s->flags2= avctx->flags2;
6733417a 431
e9174ba4 432 /* no supplementary picture */
de6d9b64 433 if (buf_size == 0) {
e9174ba4
MN
434 /* special case for last picture */
435 if (s->low_delay==0 && s->next_picture_ptr) {
436 *pict= *(AVFrame*)s->next_picture_ptr;
437 s->next_picture_ptr= NULL;
438
439 *data_size = sizeof(AVFrame);
440 }
441
de6d9b64
FB
442 return 0;
443 }
b7a22d84 444
d7425f59
MN
445 if(s->flags&CODEC_FLAG_TRUNCATED){
446 int next;
d7425f59 447
d7425f59 448 if(s->codec_id==CODEC_ID_MPEG4){
e4cb187d 449 next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
d07f9043 450 }else if(s->codec_id==CODEC_ID_H263){
e4cb187d 451 next= h263_find_frame_end(&s->parse_context, buf, buf_size);
d7425f59 452 }else{
160d679c 453 av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
d7425f59
MN
454 return -1;
455 }
b7a22d84 456
e4cb187d 457 if( ff_combine_frame(&s->parse_context, next, &buf, &buf_size) < 0 )
d7425f59 458 return buf_size;
d7425f59 459 }
de6d9b64 460
fb204d4b 461
4a3d7fbc
MN
462retry:
463
fb204d4b 464 if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
68f593b4 465 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
51b8fd19 466 }else
68f593b4 467 init_get_bits(&s->gb, buf, buf_size*8);
4136dd24 468 s->bitstream_buffer_size=0;
de6d9b64 469
3a3d3a42 470 if (!s->context_initialized) {
46fd0de8 471 if (MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
3a3d3a42
MN
472 return -1;
473 }
5f194811
MN
474
475 //we need to set current_picture_ptr before reading the header, otherwise we cant store anyting im there
476 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
477 int i= ff_find_unused_picture(s, 0);
478 s->current_picture_ptr= &s->picture[i];
479 }
1457ab52 480
de6d9b64 481 /* let's go :-) */
1457ab52
MN
482 if (s->msmpeg4_version==5) {
483 ret= ff_wmv2_decode_picture_header(s);
484 } else if (s->msmpeg4_version) {
de6d9b64
FB
485 ret = msmpeg4_decode_picture_header(s);
486 } else if (s->h263_pred) {
b704e742
MN
487 if(s->avctx->extradata_size && s->picture_number==0){
488 GetBitContext gb;
489
68f593b4 490 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
b704e742
MN
491 ret = ff_mpeg4_decode_picture_header(s, &gb);
492 }
493 ret = ff_mpeg4_decode_picture_header(s, &s->gb);
494
9f9c3229
MN
495 if(s->flags& CODEC_FLAG_LOW_DELAY)
496 s->low_delay=1;
d55f7b65 497 } else if (s->codec_id == CODEC_ID_H263I) {
de6d9b64 498 ret = intel_h263_decode_picture_header(s);
d4f5d74a
GM
499 } else if (s->h263_flv) {
500 ret = flv_h263_decode_picture_header(s);
de6d9b64
FB
501 } else {
502 ret = h263_decode_picture_header(s);
488fa19a 503 }
ffba1dc0 504
160d679c 505 if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
ffba1dc0
MN
506
507 /* skip if the header was thrashed */
508 if (ret < 0){
9b879566 509 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
ffba1dc0
MN
510 return -1;
511 }
512
4e00e76b 513 avctx->has_b_frames= !s->low_delay;
9b6a5b87
MN
514
515 if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
541ae140
MN
516 if(s->avctx->stream_codec_tag == ff_get_fourcc("XVID") ||
517 s->avctx->codec_tag == ff_get_fourcc("XVID") || s->avctx->codec_tag == ff_get_fourcc("XVIX"))
9b6a5b87 518 s->xvid_build= -1;
541ae140 519#if 0
8fc6c2b5
MN
520 if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
521 && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
522 s->xvid_build= -1;
541ae140
MN
523#endif
524 }
9b6a5b87 525
541ae140 526 if(s->xvid_build==0 && s->divx_version==0 && s->lavc_build==0){
9b6a5b87
MN
527 if(s->avctx->codec_tag == ff_get_fourcc("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
528 s->divx_version= 400; //divx 4
529 }
45cb4767
MN
530
531 if(s->xvid_build && s->divx_version){
532 s->divx_version=
533 s->divx_build= 0;
534 }
4d2858de
MN
535
536 if(s->workaround_bugs&FF_BUG_AUTODETECT){
7004ffb3 537 if(s->avctx->codec_tag == ff_get_fourcc("XVIX"))
4d2858de 538 s->workaround_bugs|= FF_BUG_XVID_ILACE;
9b6a5b87 539
7004ffb3 540 if(s->avctx->codec_tag == ff_get_fourcc("UMP4")){
4d2858de 541 s->workaround_bugs|= FF_BUG_UMP4;
4d2858de 542 }
03e93d35 543
9b6a5b87 544 if(s->divx_version>=500){
03e93d35
MN
545 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
546 }
7da71a5c 547
36df8805
MN
548 if(s->divx_version>502){
549 s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
550 }
551
a6a494fb
MN
552 if(s->xvid_build && s->xvid_build<=3)
553 s->padding_bug_score= 256*256*256*64;
554
7da71a5c
MN
555 if(s->xvid_build && s->xvid_build<=1)
556 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
557
9b6a5b87
MN
558 if(s->xvid_build && s->xvid_build<=12)
559 s->workaround_bugs|= FF_BUG_EDGE;
560
4ccde216
MN
561 if(s->xvid_build && s->xvid_build<=32)
562 s->workaround_bugs|= FF_BUG_DC_CLIP;
563
db794953
MN
564#define SET_QPEL_FUNC(postfix1, postfix2) \
565 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
566 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
567 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
568
569 if(s->lavc_build && s->lavc_build<4653)
570 s->workaround_bugs|= FF_BUG_STD_QPEL;
571
c40c3482
MN
572 if(s->lavc_build && s->lavc_build<4655)
573 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
574
8025405f 575 if(s->lavc_build && s->lavc_build<4670){
6674a126
MN
576 s->workaround_bugs|= FF_BUG_EDGE;
577 }
4ccde216
MN
578
579 if(s->lavc_build && s->lavc_build<=4712)
580 s->workaround_bugs|= FF_BUG_DC_CLIP;
6674a126 581
c40c3482
MN
582 if(s->divx_version)
583 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
a61f8265 584//printf("padding_bug_score: %d\n", s->padding_bug_score);
d671e2fe
MN
585 if(s->divx_version==501 && s->divx_build==20020416)
586 s->padding_bug_score= 256*256*256*64;
1c6dcb0f 587
6674a126 588 if(s->divx_version && s->divx_version<500){
1c6dcb0f
MN
589 s->workaround_bugs|= FF_BUG_EDGE;
590 }
7d1c3fc1 591
b44bdf7e
MN
592 if(s->divx_version)
593 s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
a61f8265 594#if 0
4d2858de 595 if(s->divx_version==500)
d671e2fe 596 s->padding_bug_score= 256*256*256*64;
4d2858de
MN
597
598 /* very ugly XVID padding bug detection FIXME/XXX solve this differently
599 * lets hope this at least works
600 */
601 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==0
602 && s->codec_id==CODEC_ID_MPEG4 && s->vo_type==0)
603 s->workaround_bugs|= FF_BUG_NO_PADDING;
604
605 if(s->lavc_build && s->lavc_build<4609) //FIXME not sure about the version num but a 4609 file seems ok
606 s->workaround_bugs|= FF_BUG_NO_PADDING;
a61f8265 607#endif
4d2858de
MN
608 }
609
db794953
MN
610 if(s->workaround_bugs& FF_BUG_STD_QPEL){
611 SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
612 SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
613 SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
614 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
615 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
616 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
617
618 SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
619 SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
620 SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
621 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
622 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
623 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
624 }
4d2858de 625
8fc6c2b5 626 if(avctx->debug & FF_DEBUG_BUGS)
9b879566 627 av_log(s->avctx, AV_LOG_DEBUG, "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
8fc6c2b5
MN
628 s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
629 s->divx_packed ? "p" : "");
630
38274ea2
MN
631#if 0 // dump bits per frame / qp / complexity
632{
633 static FILE *f=NULL;
634 if(!f) f=fopen("rate_qp_cplx.txt", "w");
32b90921 635 fprintf(f, "%d %d %f\n", buf_size, s->qscale, buf_size*(double)s->qscale);
38274ea2
MN
636}
637#endif
b442a92f 638
ddabdfef 639#if defined(HAVE_MMX) && defined(CONFIG_GPL)
ee0c00d3 640 if(s->codec_id == CODEC_ID_MPEG4 && s->xvid_build && avctx->idct_algo == FF_IDCT_AUTO && (mm_flags & MM_MMX) && !(s->flags&CODEC_FLAG_BITEXACT)){
ddabdfef 641 avctx->idct_algo= FF_IDCT_XVIDMMX;
cfeae977 642 avctx->coded_width= 0; // force reinit
ee0c00d3 643 }
b442a92f
AB
644#endif
645
488fa19a 646 /* After H263 & mpeg4 header decode we have the height, width,*/
9eaa4690
J
647 /* and other parameters. So then we could init the picture */
648 /* FIXME: By the way H263 decoder is evolving it should have */
649 /* an H263EncContext */
58b0b0dd 650
21adafec
MN
651 if ( s->width != avctx->coded_width
652 || s->height != avctx->coded_height) {
93a21abd 653 /* H.263 could change picture size any time */
bcc7125e
MN
654 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
655 s->parse_context.buffer=0;
93a21abd 656 MPV_common_end(s);
bcc7125e 657 s->parse_context= pc;
93a21abd 658 }
488fa19a 659 if (!s->context_initialized) {
21adafec 660 avcodec_set_dimensions(avctx, s->width, s->height);
4d2858de 661
4a3d7fbc 662 goto retry;
de6d9b64 663 }
35aa136a
MN
664
665 if((s->codec_id==CODEC_ID_H263 || s->codec_id==CODEC_ID_H263P))
666 s->gob_index = ff_h263_get_gob_height(s);
347ae22e 667
1e491e29
MN
668 // for hurry_up==5
669 s->current_picture.pict_type= s->pict_type;
670 s->current_picture.key_frame= s->pict_type == I_TYPE;
347ae22e 671
bb628dae 672 /* skip B-frames if we don't have reference frames */
676e61c7 673 if(s->last_picture_ptr==NULL && (s->pict_type==B_TYPE || s->dropable)) return get_consumed_bytes(s, buf_size);
2417652e 674 /* skip b frames if we are in a hurry */
f8fd15e0 675 if(avctx->hurry_up && s->pict_type==B_TYPE) return get_consumed_bytes(s, buf_size);
8c3eba7c
MN
676 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==B_TYPE)
677 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=I_TYPE)
678 || avctx->skip_frame >= AVDISCARD_ALL)
679 return get_consumed_bytes(s, buf_size);
347ae22e 680 /* skip everything if we are in a hurry>=5 */
f8fd15e0 681 if(avctx->hurry_up>=5) return get_consumed_bytes(s, buf_size);
6733417a
MN
682
683 if(s->next_p_frame_damaged){
684 if(s->pict_type==B_TYPE)
7866eeff 685 return get_consumed_bytes(s, buf_size);
6733417a
MN
686 else
687 s->next_p_frame_damaged=0;
688 }
689
d6db1c9c
MN
690 if(MPV_frame_start(s, avctx) < 0)
691 return -1;
de6d9b64
FB
692
693#ifdef DEBUG
694 printf("qscale=%d\n", s->qscale);
695#endif
696
46b4feec 697 ff_er_frame_start(s);
4d2858de 698
487bce53
MN
699 //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
700 //which isnt available before MPV_frame_start()
701 if (s->msmpeg4_version==5){
702 if(ff_wmv2_decode_secondary_picture_header(s) < 0)
703 return -1;
704 }
705
de6d9b64 706 /* decode each macroblock */
4d2858de
MN
707 s->mb_x=0;
708 s->mb_y=0;
709
710 decode_slice(s);
487bce53 711 while(s->mb_y<s->mb_height){
4d2858de 712 if(s->msmpeg4_version){
487bce53 713 if(s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_count(&s->gb) > s->gb.size_in_bits)
4d2858de
MN
714 break;
715 }else{
716 if(ff_h263_resync(s)<0)
717 break;
6733417a
MN
718 }
719
28269849 720 if(s->msmpeg4_version<4 && s->h263_pred)
4d2858de 721 ff_mpeg4_clean_buffers(s);
649c00c9 722
4d2858de 723 decode_slice(s);
de6d9b64 724 }
8b32880c 725
e1a9dbff 726 if (s->h263_msmpeg4 && s->msmpeg4_version<4 && s->pict_type==I_TYPE)
1e491e29
MN
727 if(msmpeg4_decode_ext_header(s, buf_size) < 0){
728 s->error_status_table[s->mb_num-1]= AC_ERROR|DC_ERROR|MV_ERROR;
729 }
eec1c6b9
MN
730
731 /* divx 5.01+ bistream reorder stuff */
d5a21172 732 if(s->codec_id==CODEC_ID_MPEG4 && s->bitstream_buffer_size==0 && s->divx_packed){
6733417a 733 int current_pos= get_bits_count(&s->gb)>>3;
fb204d4b 734 int startcode_found=0;
0ecca7a4
MN
735
736 if(buf_size - current_pos > 5){
6733417a 737 int i;
7866eeff 738 for(i=current_pos; i<buf_size-3; i++){
6733417a
MN
739 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
740 startcode_found=1;
741 break;
742 }
743 }
fb204d4b
MN
744 }
745 if(s->gb.buffer == s->bitstream_buffer && buf_size>20){ //xvid style
746 startcode_found=1;
747 current_pos=0;
748 }
749
750 if(startcode_found){
0ecca7a4
MN
751 s->bitstream_buffer= av_fast_realloc(
752 s->bitstream_buffer,
753 &s->allocated_bitstream_buffer_size,
754 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
fb204d4b
MN
755 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
756 s->bitstream_buffer_size= buf_size - current_pos;
6733417a
MN
757 }
758 }
759
46b4feec 760 ff_er_frame_end(s);
4d2858de 761
de6d9b64 762 MPV_frame_end(s);
db794953 763
db6e7795
MN
764assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
765assert(s->current_picture.pict_type == s->pict_type);
4e00e76b 766 if(s->pict_type==B_TYPE || s->low_delay){
492cd3a9 767 *pict= *(AVFrame*)&s->current_picture;
0c9bbaec 768 ff_print_debug_info(s, pict);
6f91bcd1 769 } else {
492cd3a9 770 *pict= *(AVFrame*)&s->last_picture;
e4a1fdfd
MN
771 if(pict)
772 ff_print_debug_info(s, pict);
6f91bcd1 773 }
8fc69895 774
81401c1f
J
775 /* Return the Picture timestamp as the frame number */
776 /* we substract 1 because it is added on utils.c */
777 avctx->frame_number = s->picture_number - 1;
778
bb628dae 779 /* don't output the last pic after seeking */
b536d0aa 780 if(s->last_picture_ptr || s->low_delay)
492cd3a9 781 *data_size = sizeof(AVFrame);
02734c6a
MN
782#ifdef PRINT_FRAME_TIME
783printf("%Ld\n", rdtsc()-time);
784#endif
d07f9043 785
7866eeff 786 return get_consumed_bytes(s, buf_size);
de6d9b64
FB
787}
788
bf89e6b1
FB
789AVCodec mpeg4_decoder = {
790 "mpeg4",
de6d9b64 791 CODEC_TYPE_VIDEO,
bf89e6b1 792 CODEC_ID_MPEG4,
de6d9b64 793 sizeof(MpegEncContext),
1457ab52 794 ff_h263_decode_init,
de6d9b64 795 NULL,
1457ab52
MN
796 ff_h263_decode_end,
797 ff_h263_decode_frame,
934982c4 798 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
7a06ff14 799 .flush= ff_mpeg_flush,
de6d9b64
FB
800};
801
802AVCodec h263_decoder = {
803 "h263",
804 CODEC_TYPE_VIDEO,
805 CODEC_ID_H263,
806 sizeof(MpegEncContext),
1457ab52 807 ff_h263_decode_init,
de6d9b64 808 NULL,
1457ab52
MN
809 ff_h263_decode_end,
810 ff_h263_decode_frame,
934982c4 811 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 | CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
7a06ff14 812 .flush= ff_mpeg_flush,
de6d9b64
FB
813};
814
84afee34
MN
815AVCodec msmpeg4v1_decoder = {
816 "msmpeg4v1",
817 CODEC_TYPE_VIDEO,
818 CODEC_ID_MSMPEG4V1,
819 sizeof(MpegEncContext),
1457ab52 820 ff_h263_decode_init,
84afee34 821 NULL,
1457ab52
MN
822 ff_h263_decode_end,
823 ff_h263_decode_frame,
93a21abd 824 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
84afee34
MN
825};
826
827AVCodec msmpeg4v2_decoder = {
828 "msmpeg4v2",
829 CODEC_TYPE_VIDEO,
830 CODEC_ID_MSMPEG4V2,
831 sizeof(MpegEncContext),
1457ab52 832 ff_h263_decode_init,
84afee34 833 NULL,
1457ab52
MN
834 ff_h263_decode_end,
835 ff_h263_decode_frame,
93a21abd 836 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
84afee34
MN
837};
838
839AVCodec msmpeg4v3_decoder = {
de6d9b64
FB
840 "msmpeg4",
841 CODEC_TYPE_VIDEO,
84afee34 842 CODEC_ID_MSMPEG4V3,
de6d9b64 843 sizeof(MpegEncContext),
1457ab52 844 ff_h263_decode_init,
de6d9b64 845 NULL,
1457ab52
MN
846 ff_h263_decode_end,
847 ff_h263_decode_frame,
93a21abd 848 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
de6d9b64
FB
849};
850
e1a9dbff
MN
851AVCodec wmv1_decoder = {
852 "wmv1",
853 CODEC_TYPE_VIDEO,
854 CODEC_ID_WMV1,
855 sizeof(MpegEncContext),
1457ab52 856 ff_h263_decode_init,
8f8402e4 857 NULL,
1457ab52
MN
858 ff_h263_decode_end,
859 ff_h263_decode_frame,
93a21abd 860 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
8f8402e4
MN
861};
862
de6d9b64
FB
863AVCodec h263i_decoder = {
864 "h263i",
865 CODEC_TYPE_VIDEO,
866 CODEC_ID_H263I,
867 sizeof(MpegEncContext),
1457ab52 868 ff_h263_decode_init,
de6d9b64 869 NULL,
1457ab52
MN
870 ff_h263_decode_end,
871 ff_h263_decode_frame,
93a21abd 872 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1,
de6d9b64
FB
873};
874
d4f5d74a
GM
875AVCodec flv_decoder = {
876 "flv",
877 CODEC_TYPE_VIDEO,
878 CODEC_ID_FLV1,
879 sizeof(MpegEncContext),
880 ff_h263_decode_init,
881 NULL,
882 ff_h263_decode_end,
883 ff_h263_decode_frame,
884 CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1
885};
e4cb187d
MN
886
887AVCodecParser h263_parser = {
888 { CODEC_ID_H263 },
889 sizeof(ParseContext),
890 NULL,
891 h263_parse,
892 ff_parse_close,
893};