lavc: Split out ff_hwaccel_pixfmt_list_420[] over individual codecs
[libav.git] / libavcodec / h263dec.c
CommitLineData
de6d9b64 1/*
983e3246 2 * H.263 decoder
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
ff4ec49e 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
115329f1 22
983e3246 23/**
ba87f080 24 * @file
983e3246
MN
25 * H.263 decoder.
26 */
115329f1 27
c6c98d08 28#include "libavutil/cpu.h"
40e5d31b 29#include "internal.h"
de6d9b64
FB
30#include "avcodec.h"
31#include "mpegvideo.h"
fc53b6af 32#include "h263.h"
c33d9814 33#include "h263_parser.h"
df495dbd 34#include "mpeg4video_parser.h"
15025553 35#include "msmpeg4.h"
70e0c871 36#include "vdpau_internal.h"
6a9c8594 37#include "thread.h"
a0b0d753 38#include "flv.h"
ca334dd1 39#include "mpeg4video.h"
de6d9b64
FB
40
41//#define DEBUG
02734c6a 42//#define PRINT_FRAME_TIME
de6d9b64 43
98a6fff9 44av_cold int ff_h263_decode_init(AVCodecContext *avctx)
de6d9b64
FB
45{
46 MpegEncContext *s = avctx->priv_data;
519c2b6d 47
bf89e6b1 48 s->avctx = avctx;
de6d9b64
FB
49 s->out_format = FMT_H263;
50
21adafec
MN
51 s->width = avctx->coded_width;
52 s->height = avctx->coded_height;
92ba5ffb 53 s->workaround_bugs= avctx->workaround_bugs;
de6d9b64 54
4d2858de 55 // set defaults
efd29844 56 ff_MPV_decode_defaults(s);
4d2858de 57 s->quant_precision=5;
4d2858de 58 s->decode_mb= ff_h263_decode_mb;
4e00e76b 59 s->low_delay= 1;
ee769c6a 60 if (avctx->codec->id == AV_CODEC_ID_MSS2)
716d413c 61 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
ee769c6a
AD
62 else
63 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
a573cc27 64 s->unrestricted_mv= 1;
4d2858de 65
de6d9b64
FB
66 /* select sub codec */
67 switch(avctx->codec->id) {
36ef5369 68 case AV_CODEC_ID_H263:
68b94c35 69 s->unrestricted_mv= 0;
580a7465 70 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
de6d9b64 71 break;
36ef5369 72 case AV_CODEC_ID_MPEG4:
de6d9b64 73 break;
36ef5369 74 case AV_CODEC_ID_MSMPEG4V1:
de6d9b64 75 s->h263_pred = 1;
84afee34
MN
76 s->msmpeg4_version=1;
77 break;
36ef5369 78 case AV_CODEC_ID_MSMPEG4V2:
84afee34
MN
79 s->h263_pred = 1;
80 s->msmpeg4_version=2;
81 break;
36ef5369 82 case AV_CODEC_ID_MSMPEG4V3:
84afee34
MN
83 s->h263_pred = 1;
84 s->msmpeg4_version=3;
de6d9b64 85 break;
36ef5369 86 case AV_CODEC_ID_WMV1:
e1a9dbff
MN
87 s->h263_pred = 1;
88 s->msmpeg4_version=4;
89 break;
36ef5369 90 case AV_CODEC_ID_WMV2:
8f8402e4
MN
91 s->h263_pred = 1;
92 s->msmpeg4_version=5;
93 break;
36ef5369
AK
94 case AV_CODEC_ID_VC1:
95 case AV_CODEC_ID_WMV3:
96 case AV_CODEC_ID_VC1IMAGE:
97 case AV_CODEC_ID_WMV3IMAGE:
ee769c6a 98 case AV_CODEC_ID_MSS2:
0d33db8a 99 s->h263_pred = 1;
100 s->msmpeg4_version=6;
580a7465 101 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
0d33db8a 102 break;
36ef5369 103 case AV_CODEC_ID_H263I:
de6d9b64 104 break;
36ef5369 105 case AV_CODEC_ID_FLV1:
d4f5d74a
GM
106 s->h263_flv = 1;
107 break;
de6d9b64
FB
108 default:
109 return -1;
110 }
d7e9533a 111 s->codec_id= avctx->codec->id;
a05aa821 112 avctx->hwaccel= ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
93a21abd 113
de6d9b64 114 /* for h263, we allocate the images after having read the header */
36ef5369 115 if (avctx->codec->id != AV_CODEC_ID_H263 && avctx->codec->id != AV_CODEC_ID_MPEG4)
efd29844 116 if (ff_MPV_common_init(s) < 0)
4949028f 117 return -1;
de6d9b64 118
ca1fe6c0 119 ff_h263_decode_init_vlc();
115329f1 120
de6d9b64
FB
121 return 0;
122}
123
98a6fff9 124av_cold int ff_h263_decode_end(AVCodecContext *avctx)
de6d9b64
FB
125{
126 MpegEncContext *s = avctx->priv_data;
127
efd29844 128 ff_MPV_common_end(s);
de6d9b64
FB
129 return 0;
130}
131
7866eeff 132/**
58c42af7 133 * Return the number of bytes consumed for building the current frame.
7866eeff
MN
134 */
135static int get_consumed_bytes(MpegEncContext *s, int buf_size){
136 int pos= (get_bits_count(&s->gb)+7)>>3;
115329f1 137
c0aec489 138 if(s->divx_packed || s->avctx->hwaccel){
7866eeff 139 //we would have to scan through the whole buf to handle the weird reordering ...
115329f1 140 return buf_size;
d7425f59
MN
141 }else if(s->flags&CODEC_FLAG_TRUNCATED){
142 pos -= s->parse_context.last_index;
143 if(pos<0) pos=0; // padding is not really read so this might be -1
144 return pos;
7866eeff 145 }else{
755bfeab 146 if(pos==0) pos=1; //avoid infinite loops (i doubt that is needed but ...)
77277835 147 if(pos+10>buf_size) pos=buf_size; // oops ;)
7866eeff
MN
148
149 return pos;
150 }
151}
152
4d2858de 153static int decode_slice(MpegEncContext *s){
5bf2ac2b 154 const int part_mask= s->partitioned_frame ? (ER_AC_END|ER_AC_ERROR) : 0x7F;
2bcbd984 155 const int mb_size = 16;
4d2858de
MN
156 s->last_resync_gb= s->gb;
157 s->first_slice_line= 1;
115329f1 158
4d2858de
MN
159 s->resync_mb_x= s->mb_x;
160 s->resync_mb_y= s->mb_y;
161
554daa24 162 ff_set_qscale(s, s->qscale);
115329f1 163
1468d503
GB
164 if (s->avctx->hwaccel) {
165 const uint8_t *start= s->gb.buffer + get_bits_count(&s->gb)/8;
166 const uint8_t *end = ff_h263_find_resync_marker(start + 1, s->gb.buffer_end);
167 skip_bits_long(&s->gb, 8*(end - start));
168 return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
169 }
40e5d31b 170
4d2858de
MN
171 if(s->partitioned_frame){
172 const int qscale= s->qscale;
173
36ef5369 174 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4){
4d2858de 175 if(ff_mpeg4_decode_partitions(s) < 0)
115329f1 176 return -1;
4d2858de 177 }
115329f1 178
d99ce8d7 179 /* restore variables which were modified */
4d2858de
MN
180 s->first_slice_line=1;
181 s->mb_x= s->resync_mb_x;
182 s->mb_y= s->resync_mb_y;
554daa24 183 ff_set_qscale(s, qscale);
4d2858de
MN
184 }
185
186 for(; s->mb_y < s->mb_height; s->mb_y++) {
187 /* per-row end of slice checks */
188 if(s->msmpeg4_version){
189 if(s->resync_mb_y + s->slice_height == s->mb_y){
54974c62 190 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
46b4feec 191
4d2858de
MN
192 return 0;
193 }
194 }
115329f1 195
4d2858de
MN
196 if(s->msmpeg4_version==1){
197 s->last_dc[0]=
198 s->last_dc[1]=
199 s->last_dc[2]= 128;
200 }
115329f1 201
4d2858de
MN
202 ff_init_block_index(s);
203 for(; s->mb_x < s->mb_width; s->mb_x++) {
204 int ret;
205
206 ff_update_block_index(s);
207
208 if(s->resync_mb_x == s->mb_x && s->resync_mb_y+1 == s->mb_y){
115329f1 209 s->first_slice_line=0;
4d2858de
MN
210 }
211
212 /* DCT & quantize */
115329f1 213
4d2858de
MN
214 s->mv_dir = MV_DIR_FORWARD;
215 s->mv_type = MV_TYPE_16X16;
160d679c 216// s->mb_skipped = 0;
1218777f
DB
217 av_dlog(s, "%d %d %06X\n",
218 ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
4d2858de 219 ret= s->decode_mb(s, s->block);
4d2858de 220
975a1447 221 if (s->pict_type!=AV_PICTURE_TYPE_B)
7d1c3fc1
MN
222 ff_h263_update_motion_val(s);
223
4d2858de 224 if(ret<0){
7bc9090a 225 const int xy= s->mb_x + s->mb_y*s->mb_stride;
4d2858de 226 if(ret==SLICE_END){
efd29844 227 ff_MPV_decode_mb(s, s->block);
332f9ac4
MN
228 if(s->loop_filter)
229 ff_h263_loop_filter(s);
aef95c8c 230
54974c62 231 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
a61f8265
MN
232
233 s->padding_bug_score--;
115329f1 234
4d2858de
MN
235 if(++s->mb_x >= s->mb_width){
236 s->mb_x=0;
1d0feb5d 237 ff_mpeg_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
efd29844 238 ff_MPV_report_decode_progress(s);
4d2858de
MN
239 s->mb_y++;
240 }
115329f1 241 return 0;
4d2858de 242 }else if(ret==SLICE_NOEND){
9b879566 243 av_log(s->avctx, AV_LOG_ERROR, "Slice mismatch at MB: %d\n", xy);
54974c62 244 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x+1, s->mb_y, ER_MB_END&part_mask);
4d2858de
MN
245 return -1;
246 }
9b879566 247 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
54974c62 248 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR&part_mask);
115329f1 249
4d2858de
MN
250 return -1;
251 }
aef95c8c 252
efd29844 253 ff_MPV_decode_mb(s, s->block);
332f9ac4
MN
254 if(s->loop_filter)
255 ff_h263_loop_filter(s);
4d2858de 256 }
115329f1 257
1d0feb5d 258 ff_mpeg_draw_horiz_band(s, s->mb_y*mb_size, mb_size);
efd29844 259 ff_MPV_report_decode_progress(s);
115329f1 260
4d2858de
MN
261 s->mb_x= 0;
262 }
115329f1 263
4d2858de
MN
264 assert(s->mb_x==0 && s->mb_y==s->mb_height);
265
36ef5369 266 if(s->codec_id==AV_CODEC_ID_MPEG4
df59c9c0
MN
267 && (s->workaround_bugs&FF_BUG_AUTODETECT)
268 && get_bits_left(&s->gb) >= 48
269 && show_bits(&s->gb, 24)==0x4010
270 && !s->data_partitioning)
271 s->padding_bug_score+=32;
272
a61f8265 273 /* try to detect the padding bug */
36ef5369 274 if( s->codec_id==AV_CODEC_ID_MPEG4
115329f1 275 && (s->workaround_bugs&FF_BUG_AUTODETECT)
6e44ba15
RB
276 && get_bits_left(&s->gb) >=0
277 && get_bits_left(&s->gb) < 48
96d9cb22 278// && !s->resync_marker
a61f8265 279 && !s->data_partitioning){
115329f1 280
a61f8265 281 const int bits_count= get_bits_count(&s->gb);
68f593b4 282 const int bits_left = s->gb.size_in_bits - bits_count;
115329f1 283
650faac6
MN
284 if(bits_left==0){
285 s->padding_bug_score+=16;
8671359b 286 } else if(bits_left != 1){
a61f8265
MN
287 int v= show_bits(&s->gb, 8);
288 v|= 0x7F >> (7-(bits_count&7));
8671359b 289
4940ed6a 290 if(v==0x7F && bits_left<=8)
a61f8265 291 s->padding_bug_score--;
4940ed6a
MN
292 else if(v==0x7F && ((get_bits_count(&s->gb)+8)&8) && bits_left<=16)
293 s->padding_bug_score+= 4;
a61f8265 294 else
115329f1
DB
295 s->padding_bug_score++;
296 }
a61f8265 297 }
115329f1 298
4940ed6a 299 if(s->workaround_bugs&FF_BUG_AUTODETECT){
b317567c 300 if(s->padding_bug_score > -2 && !s->data_partitioning /*&& (s->divx_version>=0 || !s->resync_marker)*/)
4940ed6a
MN
301 s->workaround_bugs |= FF_BUG_NO_PADDING;
302 else
303 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
304 }
a61f8265 305
bb628dae 306 // handle formats which don't have unique end markers
cb7c98d6 307 if(s->msmpeg4_version || (s->workaround_bugs&FF_BUG_NO_PADDING)){ //FIXME perhaps solve this more cleanly
6e44ba15 308 int left= get_bits_left(&s->gb);
4d2858de 309 int max_extra=7;
115329f1 310
4d2858de 311 /* no markers in M$ crap */
975a1447 312 if(s->msmpeg4_version && s->pict_type==AV_PICTURE_TYPE_I)
4d2858de 313 max_extra+= 17;
115329f1 314
4d2858de 315 /* buggy padding but the frame should still end approximately at the bitstream end */
5b22d6e1 316 if((s->workaround_bugs&FF_BUG_NO_PADDING) && (s->err_recognition&AV_EF_BUFFER))
4d2858de 317 max_extra+= 48;
cb7c98d6 318 else if((s->workaround_bugs&FF_BUG_NO_PADDING))
4d2858de 319 max_extra+= 256*256*256*64;
115329f1 320
4d2858de 321 if(left>max_extra){
9b879566 322 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
323 }
324 else if(left<0){
9b879566 325 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
4d2858de 326 }else
54974c62 327 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x-1, s->mb_y, ER_MB_END);
115329f1 328
4d2858de
MN
329 return 0;
330 }
a61f8265 331
115329f1 332 av_log(s->avctx, AV_LOG_ERROR, "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
6e44ba15 333 get_bits_left(&s->gb),
9b6a5b87 334 show_bits(&s->gb, 24), s->padding_bug_score);
115329f1 335
54974c62 336 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y, ER_MB_END&part_mask);
46b4feec 337
4d2858de
MN
338 return -1;
339}
340
115329f1 341int ff_h263_decode_frame(AVCodecContext *avctx,
df9b9567 342 void *data, int *got_frame,
7a00bbad 343 AVPacket *avpkt)
de6d9b64 344{
7a00bbad
TB
345 const uint8_t *buf = avpkt->data;
346 int buf_size = avpkt->size;
de6d9b64 347 MpegEncContext *s = avctx->priv_data;
1df1df0b 348 int ret;
115329f1
DB
349 AVFrame *pict = data;
350
02734c6a
MN
351#ifdef PRINT_FRAME_TIME
352uint64_t time= rdtsc();
353#endif
b50eef3a 354 s->flags= avctx->flags;
303e50e6 355 s->flags2= avctx->flags2;
6733417a 356
e9174ba4 357 /* no supplementary picture */
de6d9b64 358 if (buf_size == 0) {
e9174ba4
MN
359 /* special case for last picture */
360 if (s->low_delay==0 && s->next_picture_ptr) {
324deaa2 361 *pict = s->next_picture_ptr->f;
e9174ba4
MN
362 s->next_picture_ptr= NULL;
363
df9b9567 364 *got_frame = 1;
e9174ba4
MN
365 }
366
de6d9b64
FB
367 return 0;
368 }
b7a22d84 369
d7425f59
MN
370 if(s->flags&CODEC_FLAG_TRUNCATED){
371 int next;
115329f1 372
36ef5369 373 if(CONFIG_MPEG4_DECODER && s->codec_id==AV_CODEC_ID_MPEG4){
e4cb187d 374 next= ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
36ef5369 375 }else if(CONFIG_H263_DECODER && s->codec_id==AV_CODEC_ID_H263){
c33d9814 376 next= ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
d7425f59 377 }else{
160d679c 378 av_log(s->avctx, AV_LOG_ERROR, "this codec does not support truncated bitstreams\n");
d7425f59
MN
379 return -1;
380 }
115329f1 381
c53d2d90 382 if( ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf, &buf_size) < 0 )
d7425f59 383 return buf_size;
d7425f59 384 }
de6d9b64 385
115329f1 386
4a3d7fbc 387retry:
115329f1 388
fb204d4b 389 if(s->bitstream_buffer_size && (s->divx_packed || buf_size<20)){ //divx 5.01+/xvid frame reorder
68f593b4 390 init_get_bits(&s->gb, s->bitstream_buffer, s->bitstream_buffer_size*8);
51b8fd19 391 }else
68f593b4 392 init_get_bits(&s->gb, buf, buf_size*8);
4136dd24 393 s->bitstream_buffer_size=0;
de6d9b64 394
3a3d3a42 395 if (!s->context_initialized) {
efd29844 396 if (ff_MPV_common_init(s) < 0) //we need the idct permutaton for reading a custom matrix
3a3d3a42
MN
397 return -1;
398 }
115329f1 399
755bfeab
DB
400 /* We need to set current_picture_ptr before reading the header,
401 * otherwise we cannot store anyting in there */
657ccb5a 402 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5f194811 403 int i= ff_find_unused_picture(s, 0);
4f820131
AU
404 if (i < 0)
405 return i;
5f194811
MN
406 s->current_picture_ptr= &s->picture[i];
407 }
115329f1 408
de6d9b64 409 /* let's go :-) */
49fb20cb 410 if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5) {
1457ab52 411 ret= ff_wmv2_decode_picture_header(s);
49fb20cb 412 } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
04d38225 413 ret = ff_msmpeg4_decode_picture_header(s);
0b60f3bd 414 } else if (CONFIG_MPEG4_DECODER && s->h263_pred) {
b704e742
MN
415 if(s->avctx->extradata_size && s->picture_number==0){
416 GetBitContext gb;
115329f1 417
68f593b4 418 init_get_bits(&gb, s->avctx->extradata, s->avctx->extradata_size*8);
b704e742
MN
419 ret = ff_mpeg4_decode_picture_header(s, &gb);
420 }
421 ret = ff_mpeg4_decode_picture_header(s, &s->gb);
36ef5369 422 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
d7525cba 423 ret = ff_intel_h263_decode_picture_header(s);
a0b0d753
MN
424 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
425 ret = ff_flv_decode_picture_header(s);
de6d9b64 426 } else {
ddce8953 427 ret = ff_h263_decode_picture_header(s);
488fa19a 428 }
115329f1 429
160d679c 430 if(ret==FRAME_SKIPPED) return get_consumed_bytes(s, buf_size);
ffba1dc0
MN
431
432 /* skip if the header was thrashed */
433 if (ret < 0){
9b879566 434 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
ffba1dc0
MN
435 return -1;
436 }
115329f1 437
4e00e76b 438 avctx->has_b_frames= !s->low_delay;
115329f1 439
b317567c 440 if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
2bb6eba2
AJ
441 if(s->stream_codec_tag == AV_RL32("XVID") ||
442 s->codec_tag == AV_RL32("XVID") || s->codec_tag == AV_RL32("XVIX") ||
80ac87c1 443 s->codec_tag == AV_RL32("RMP4") || s->codec_tag == AV_RL32("ZMP4") ||
d3752b15
MN
444 s->codec_tag == AV_RL32("SIPP")
445 )
b317567c 446 s->xvid_build= 0;
541ae140 447#if 0
2bb6eba2 448 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==1
115329f1 449 && s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
b317567c 450 s->xvid_build= 0;
541ae140
MN
451#endif
452 }
9b6a5b87 453
b317567c 454 if(s->xvid_build==-1 && s->divx_version==-1 && s->lavc_build==-1){
2bb6eba2 455 if(s->codec_tag == AV_RL32("DIVX") && s->vo_type==0 && s->vol_control_parameters==0)
9b6a5b87
MN
456 s->divx_version= 400; //divx 4
457 }
115329f1 458
b317567c 459 if(s->xvid_build>=0 && s->divx_version>=0){
45cb4767 460 s->divx_version=
b317567c 461 s->divx_build= -1;
45cb4767 462 }
4d2858de
MN
463
464 if(s->workaround_bugs&FF_BUG_AUTODETECT){
2bb6eba2 465 if(s->codec_tag == AV_RL32("XVIX"))
4d2858de 466 s->workaround_bugs|= FF_BUG_XVID_ILACE;
9b6a5b87 467
2bb6eba2 468 if(s->codec_tag == AV_RL32("UMP4")){
4d2858de 469 s->workaround_bugs|= FF_BUG_UMP4;
4d2858de 470 }
03e93d35 471
58d1dc0e 472 if(s->divx_version>=500 && s->divx_build<1814){
03e93d35
MN
473 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
474 }
7da71a5c 475
58d1dc0e 476 if(s->divx_version>502 && s->divx_build<1814){
36df8805
MN
477 s->workaround_bugs|= FF_BUG_QPEL_CHROMA2;
478 }
479
b317567c 480 if(s->xvid_build<=3U)
a6a494fb 481 s->padding_bug_score= 256*256*256*64;
115329f1 482
b317567c 483 if(s->xvid_build<=1U)
7da71a5c
MN
484 s->workaround_bugs|= FF_BUG_QPEL_CHROMA;
485
b317567c 486 if(s->xvid_build<=12U)
9b6a5b87
MN
487 s->workaround_bugs|= FF_BUG_EDGE;
488
b317567c 489 if(s->xvid_build<=32U)
4ccde216
MN
490 s->workaround_bugs|= FF_BUG_DC_CLIP;
491
db794953
MN
492#define SET_QPEL_FUNC(postfix1, postfix2) \
493 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2;\
494 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;\
495 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
496
b317567c 497 if(s->lavc_build<4653U)
db794953 498 s->workaround_bugs|= FF_BUG_STD_QPEL;
115329f1 499
b317567c 500 if(s->lavc_build<4655U)
c40c3482
MN
501 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
502
b317567c 503 if(s->lavc_build<4670U){
6674a126
MN
504 s->workaround_bugs|= FF_BUG_EDGE;
505 }
115329f1 506
b317567c 507 if(s->lavc_build<=4712U)
4ccde216 508 s->workaround_bugs|= FF_BUG_DC_CLIP;
6674a126 509
b317567c 510 if(s->divx_version>=0)
c40c3482 511 s->workaround_bugs|= FF_BUG_DIRECT_BLOCKSIZE;
d671e2fe
MN
512 if(s->divx_version==501 && s->divx_build==20020416)
513 s->padding_bug_score= 256*256*256*64;
1c6dcb0f 514
b317567c 515 if(s->divx_version<500U){
1c6dcb0f
MN
516 s->workaround_bugs|= FF_BUG_EDGE;
517 }
115329f1 518
b317567c 519 if(s->divx_version>=0)
b44bdf7e 520 s->workaround_bugs|= FF_BUG_HPEL_CHROMA;
a61f8265 521#if 0
4d2858de 522 if(s->divx_version==500)
d671e2fe 523 s->padding_bug_score= 256*256*256*64;
4d2858de
MN
524
525 /* very ugly XVID padding bug detection FIXME/XXX solve this differently
ca74c0a1 526 * Let us hope this at least works.
4d2858de 527 */
b317567c 528 if( s->resync_marker==0 && s->data_partitioning==0 && s->divx_version==-1
36ef5369 529 && s->codec_id==AV_CODEC_ID_MPEG4 && s->vo_type==0)
4d2858de 530 s->workaround_bugs|= FF_BUG_NO_PADDING;
115329f1 531
b317567c 532 if(s->lavc_build<4609U) //FIXME not sure about the version num but a 4609 file seems ok
4d2858de 533 s->workaround_bugs|= FF_BUG_NO_PADDING;
a61f8265 534#endif
4d2858de 535 }
115329f1 536
db794953
MN
537 if(s->workaround_bugs& FF_BUG_STD_QPEL){
538 SET_QPEL_FUNC(qpel_pixels_tab[0][ 5], qpel16_mc11_old_c)
539 SET_QPEL_FUNC(qpel_pixels_tab[0][ 7], qpel16_mc31_old_c)
540 SET_QPEL_FUNC(qpel_pixels_tab[0][ 9], qpel16_mc12_old_c)
541 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
542 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
543 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
544
545 SET_QPEL_FUNC(qpel_pixels_tab[1][ 5], qpel8_mc11_old_c)
546 SET_QPEL_FUNC(qpel_pixels_tab[1][ 7], qpel8_mc31_old_c)
547 SET_QPEL_FUNC(qpel_pixels_tab[1][ 9], qpel8_mc12_old_c)
548 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
549 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
550 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
551 }
4d2858de 552
8fc6c2b5 553 if(avctx->debug & FF_DEBUG_BUGS)
115329f1 554 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
555 s->workaround_bugs, s->lavc_build, s->xvid_build, s->divx_version, s->divx_build,
556 s->divx_packed ? "p" : "");
115329f1 557
b250f9c6 558#if HAVE_MMX
36ef5369 559 if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build>=0 && avctx->idct_algo == FF_IDCT_AUTO && (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
ddabdfef 560 avctx->idct_algo= FF_IDCT_XVIDMMX;
71db86d5 561 ff_dct_common_init(s);
6baac1af 562 s->picture_number=0;
ee0c00d3 563 }
b442a92f
AB
564#endif
565
488fa19a 566 /* After H263 & mpeg4 header decode we have the height, width,*/
9eaa4690
J
567 /* and other parameters. So then we could init the picture */
568 /* FIXME: By the way H263 decoder is evolving it should have */
569 /* an H263EncContext */
115329f1 570
8701f4f8 571 if (!avctx->coded_width || !avctx->coded_height) {
bcc7125e 572 ParseContext pc= s->parse_context; //FIXME move these demuxng hack to avformat
71db86d5 573
bcc7125e 574 s->parse_context.buffer=0;
efd29844 575 ff_MPV_common_end(s);
bcc7125e 576 s->parse_context= pc;
21adafec 577 avcodec_set_dimensions(avctx, s->width, s->height);
4d2858de 578
4a3d7fbc 579 goto retry;
de6d9b64 580 }
35aa136a 581
8701f4f8
JG
582 if (s->width != avctx->coded_width ||
583 s->height != avctx->coded_height ||
584 s->context_reinit) {
585 /* H.263 could change picture size any time */
586 s->context_reinit = 0;
587
588 avcodec_set_dimensions(avctx, s->width, s->height);
589
590 if ((ret = ff_MPV_common_frame_size_change(s)))
591 return ret;
592 }
593
36ef5369 594 if((s->codec_id==AV_CODEC_ID_H263 || s->codec_id==AV_CODEC_ID_H263P || s->codec_id == AV_CODEC_ID_H263I))
35aa136a 595 s->gob_index = ff_h263_get_gob_height(s);
115329f1 596
8ed2ae09 597 // for skipping the frame
657ccb5a
DB
598 s->current_picture.f.pict_type = s->pict_type;
599 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
347ae22e 600
bb628dae 601 /* skip B-frames if we don't have reference frames */
ba0c8981
DB
602 if (s->last_picture_ptr == NULL &&
603 (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
604 return get_consumed_bytes(s, buf_size);
975a1447
SS
605 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==AV_PICTURE_TYPE_B)
606 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=AV_PICTURE_TYPE_I)
115329f1 607 || avctx->skip_frame >= AVDISCARD_ALL)
8c3eba7c 608 return get_consumed_bytes(s, buf_size);
115329f1 609
6733417a 610 if(s->next_p_frame_damaged){
975a1447 611 if(s->pict_type==AV_PICTURE_TYPE_B)
7866eeff 612 return get_consumed_bytes(s, buf_size);
6733417a
MN
613 else
614 s->next_p_frame_damaged=0;
615 }
616
0b711ca3 617 if((!s->no_rounding) || s->pict_type==AV_PICTURE_TYPE_B){
a1b13cf5 618 s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
2833fc46
LM
619 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
620 }else{
a1b13cf5 621 s->me.qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
2833fc46
LM
622 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
623 }
624
efd29844 625 if(ff_MPV_frame_start(s, avctx) < 0)
d6db1c9c 626 return -1;
de6d9b64 627
6a9c8594
AS
628 if (!s->divx_packed) ff_thread_finish_setup(avctx);
629
70e0c871 630 if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
bf9d70d5 631 ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
70e0c871
NC
632 goto frame_end;
633 }
634
1468d503 635 if (avctx->hwaccel) {
bf9d70d5 636 if (avctx->hwaccel->start_frame(avctx, s->gb.buffer, s->gb.buffer_end - s->gb.buffer) < 0)
1468d503
GB
637 return -1;
638 }
639
54974c62 640 ff_mpeg_er_frame_start(s);
115329f1 641
487bce53 642 //the second part of the wmv2 header contains the MB skip bits which are stored in current_picture->mb_type
efd29844 643 //which is not available before ff_MPV_frame_start()
49fb20cb 644 if (CONFIG_WMV2_DECODER && s->msmpeg4_version==5){
9abc7e0f
MN
645 ret = ff_wmv2_decode_secondary_picture_header(s);
646 if(ret<0) return ret;
647 if(ret==1) goto intrax8_decoded;
487bce53
MN
648 }
649
de6d9b64 650 /* decode each macroblock */
115329f1 651 s->mb_x=0;
4d2858de 652 s->mb_y=0;
115329f1 653
2a11952f 654 ret = decode_slice(s);
487bce53 655 while(s->mb_y<s->mb_height){
4d2858de 656 if(s->msmpeg4_version){
3574a85c 657 if(s->slice_height==0 || s->mb_x!=0 || (s->mb_y%s->slice_height)!=0 || get_bits_left(&s->gb)<0)
4d2858de
MN
658 break;
659 }else{
6a9c8594 660 int prev_x=s->mb_x, prev_y=s->mb_y;
4d2858de
MN
661 if(ff_h263_resync(s)<0)
662 break;
6a9c8594 663 if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
54974c62 664 s->er.error_occurred = 1;
6733417a 665 }
115329f1 666
28269849 667 if(s->msmpeg4_version<4 && s->h263_pred)
4d2858de 668 ff_mpeg4_clean_buffers(s);
649c00c9 669
2a11952f 670 if (decode_slice(s) < 0) ret = AVERROR_INVALIDDATA;
de6d9b64 671 }
8b32880c 672
8c51620f 673 if (s->msmpeg4_version && s->msmpeg4_version<4 && s->pict_type==AV_PICTURE_TYPE_I)
04d38225 674 if(!CONFIG_MSMPEG4_DECODER || ff_msmpeg4_decode_ext_header(s, buf_size) < 0){
54974c62 675 s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
1e491e29 676 }
115329f1 677
6c587587 678 assert(s->bitstream_buffer_size==0);
8ef4f92b 679frame_end:
eec1c6b9 680 /* divx 5.01+ bistream reorder stuff */
36ef5369 681 if(s->codec_id==AV_CODEC_ID_MPEG4 && s->divx_packed){
6733417a 682 int current_pos= get_bits_count(&s->gb)>>3;
fb204d4b 683 int startcode_found=0;
115329f1 684
0ecca7a4 685 if(buf_size - current_pos > 5){
6733417a 686 int i;
7866eeff 687 for(i=current_pos; i<buf_size-3; i++){
6733417a
MN
688 if(buf[i]==0 && buf[i+1]==0 && buf[i+2]==1 && buf[i+3]==0xB6){
689 startcode_found=1;
690 break;
691 }
692 }
fb204d4b 693 }
b317567c 694 if(s->gb.buffer == s->bitstream_buffer && buf_size>7 && s->xvid_build>=0){ //xvid style
fb204d4b
MN
695 startcode_found=1;
696 current_pos=0;
697 }
698
699 if(startcode_found){
238ef6da
RD
700 av_fast_malloc(
701 &s->bitstream_buffer,
115329f1 702 &s->allocated_bitstream_buffer_size,
0ecca7a4 703 buf_size - current_pos + FF_INPUT_BUFFER_PADDING_SIZE);
238ef6da
RD
704 if (!s->bitstream_buffer)
705 return AVERROR(ENOMEM);
fb204d4b
MN
706 memcpy(s->bitstream_buffer, buf + current_pos, buf_size - current_pos);
707 s->bitstream_buffer_size= buf_size - current_pos;
6733417a
MN
708 }
709 }
710
9abc7e0f 711intrax8_decoded:
54974c62 712 ff_er_frame_end(&s->er);
4d2858de 713
f986c8e2
MN
714 if (avctx->hwaccel) {
715 if (avctx->hwaccel->end_frame(avctx) < 0)
716 return -1;
717 }
718
efd29844 719 ff_MPV_frame_end(s);
db794953 720
ad62507f
AC
721 assert(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
722 assert(s->current_picture.f.pict_type == s->pict_type);
975a1447 723 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
324deaa2 724 *pict = s->current_picture_ptr->f;
5430b070 725 } else if (s->last_picture_ptr != NULL) {
324deaa2 726 *pict = s->last_picture_ptr->f;
5430b070
MN
727 }
728
729 if(s->last_picture_ptr || s->low_delay){
df9b9567 730 *got_frame = 1;
0c9bbaec 731 ff_print_debug_info(s, pict);
6f91bcd1 732 }
8fc69895 733
02734c6a 734#ifdef PRINT_FRAME_TIME
949b1a13 735av_log(avctx, AV_LOG_DEBUG, "%"PRId64"\n", rdtsc()-time);
02734c6a 736#endif
d07f9043 737
9abc9873 738 return (ret && (avctx->err_recognition & AV_EF_EXPLODE))?ret:get_consumed_bytes(s, buf_size);
de6d9b64
FB
739}
740
8d061989
RB
741const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
742#if CONFIG_VAAPI
743 AV_PIX_FMT_VAAPI_VLD,
744#endif
745#if CONFIG_VDPAU
746 AV_PIX_FMT_VDPAU,
747#endif
748 AV_PIX_FMT_YUV420P,
749 AV_PIX_FMT_NONE
750};
751
d36beb3f 752AVCodec ff_h263_decoder = {
ec6402b7
AK
753 .name = "h263",
754 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 755 .id = AV_CODEC_ID_H263,
ec6402b7
AK
756 .priv_data_size = sizeof(MpegEncContext),
757 .init = ff_h263_decode_init,
758 .close = ff_h263_decode_end,
759 .decode = ff_h263_decode_frame,
00c3b67b
MS
760 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
761 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
762 .flush = ff_mpeg_flush,
00c3b67b 763 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
8d061989 764 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
de6d9b64 765};