]> git.sesse.net Git - ffmpeg/blob - libavcodec/h263dec.c
Merge commit 'ee80cf741a44115758e62399b7bde08d33161151'
[ffmpeg] / libavcodec / h263dec.c
1 /*
2  * H.263 decoder
3  * Copyright (c) 2001 Fabrice Bellard
4  * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * H.263 decoder.
26  */
27
28 #define UNCHECKED_BITSTREAM_READER 1
29
30 #include "libavutil/cpu.h"
31 #include "avcodec.h"
32 #include "error_resilience.h"
33 #include "flv.h"
34 #include "h263.h"
35 #include "h263_parser.h"
36 #include "internal.h"
37 #include "mpeg4video.h"
38 #include "mpeg4video_parser.h"
39 #include "mpegvideo.h"
40 #include "msmpeg4.h"
41 #include "vdpau_internal.h"
42 #include "thread.h"
43
44 av_cold int ff_h263_decode_init(AVCodecContext *avctx)
45 {
46     MpegEncContext *s = avctx->priv_data;
47     int ret;
48
49     s->avctx           = avctx;
50     s->out_format      = FMT_H263;
51     s->width           = avctx->coded_width;
52     s->height          = avctx->coded_height;
53     s->workaround_bugs = avctx->workaround_bugs;
54
55     // set defaults
56     ff_MPV_decode_defaults(s);
57     s->quant_precision = 5;
58     s->decode_mb       = ff_h263_decode_mb;
59     s->low_delay       = 1;
60     if (avctx->codec->id == AV_CODEC_ID_MSS2)
61         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
62     else
63         avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
64     s->unrestricted_mv = 1;
65
66     /* select sub codec */
67     switch (avctx->codec->id) {
68     case AV_CODEC_ID_H263:
69     case AV_CODEC_ID_H263P:
70         s->unrestricted_mv = 0;
71         avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
72         break;
73     case AV_CODEC_ID_MPEG4:
74         break;
75     case AV_CODEC_ID_MSMPEG4V1:
76         s->h263_pred       = 1;
77         s->msmpeg4_version = 1;
78         break;
79     case AV_CODEC_ID_MSMPEG4V2:
80         s->h263_pred       = 1;
81         s->msmpeg4_version = 2;
82         break;
83     case AV_CODEC_ID_MSMPEG4V3:
84         s->h263_pred       = 1;
85         s->msmpeg4_version = 3;
86         break;
87     case AV_CODEC_ID_WMV1:
88         s->h263_pred       = 1;
89         s->msmpeg4_version = 4;
90         break;
91     case AV_CODEC_ID_WMV2:
92         s->h263_pred       = 1;
93         s->msmpeg4_version = 5;
94         break;
95     case AV_CODEC_ID_VC1:
96     case AV_CODEC_ID_WMV3:
97     case AV_CODEC_ID_VC1IMAGE:
98     case AV_CODEC_ID_WMV3IMAGE:
99     case AV_CODEC_ID_MSS2:
100         s->h263_pred       = 1;
101         s->msmpeg4_version = 6;
102         avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
103         break;
104     case AV_CODEC_ID_H263I:
105         break;
106     case AV_CODEC_ID_FLV1:
107         s->h263_flv = 1;
108         break;
109     default:
110         av_log(avctx, AV_LOG_ERROR, "Unsupported codec %d\n",
111                avctx->codec->id);
112         return AVERROR(ENOSYS);
113     }
114     s->codec_id    = avctx->codec->id;
115     avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
116
117     if (avctx->stream_codec_tag == AV_RL32("l263") && avctx->extradata_size == 56 && avctx->extradata[0] == 1)
118         s->ehc_mode = 1;
119
120     /* for h263, we allocate the images after having read the header */
121     if (avctx->codec->id != AV_CODEC_ID_H263 &&
122         avctx->codec->id != AV_CODEC_ID_H263P &&
123         avctx->codec->id != AV_CODEC_ID_MPEG4)
124         if ((ret = ff_MPV_common_init(s)) < 0)
125             return ret;
126
127     ff_h263_decode_init_vlc();
128
129     return 0;
130 }
131
132 av_cold int ff_h263_decode_end(AVCodecContext *avctx)
133 {
134     MpegEncContext *s = avctx->priv_data;
135
136     ff_MPV_common_end(s);
137     return 0;
138 }
139
140 /**
141  * Return the number of bytes consumed for building the current frame.
142  */
143 static int get_consumed_bytes(MpegEncContext *s, int buf_size)
144 {
145     int pos = (get_bits_count(&s->gb) + 7) >> 3;
146
147     if (s->divx_packed || s->avctx->hwaccel) {
148         /* We would have to scan through the whole buf to handle the weird
149          * reordering ... */
150         return buf_size;
151     } else if (s->flags & CODEC_FLAG_TRUNCATED) {
152         pos -= s->parse_context.last_index;
153         // padding is not really read so this might be -1
154         if (pos < 0)
155             pos = 0;
156         return pos;
157     } else {
158         // avoid infinite loops (maybe not needed...)
159         if (pos == 0)
160             pos = 1;
161         // oops ;)
162         if (pos + 10 > buf_size)
163             pos = buf_size;
164
165         return pos;
166     }
167 }
168
169 static int decode_slice(MpegEncContext *s)
170 {
171     const int part_mask = s->partitioned_frame
172                           ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
173     const int mb_size   = 16 >> s->avctx->lowres;
174     int ret;
175
176     s->last_resync_gb   = s->gb;
177     s->first_slice_line = 1;
178     s->resync_mb_x      = s->mb_x;
179     s->resync_mb_y      = s->mb_y;
180
181     ff_set_qscale(s, s->qscale);
182
183     if (s->avctx->hwaccel) {
184         const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
185         ret = s->avctx->hwaccel->decode_slice(s->avctx, start, s->gb.buffer_end - start);
186         // ensure we exit decode loop
187         s->mb_y = s->mb_height;
188         return ret;
189     }
190
191     if (s->partitioned_frame) {
192         const int qscale = s->qscale;
193
194         if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
195             if ((ret = ff_mpeg4_decode_partitions(s)) < 0)
196                 return ret;
197
198         /* restore variables which were modified */
199         s->first_slice_line = 1;
200         s->mb_x             = s->resync_mb_x;
201         s->mb_y             = s->resync_mb_y;
202         ff_set_qscale(s, qscale);
203     }
204
205     for (; s->mb_y < s->mb_height; s->mb_y++) {
206         /* per-row end of slice checks */
207         if (s->msmpeg4_version) {
208             if (s->resync_mb_y + s->slice_height == s->mb_y) {
209                 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
210                                 s->mb_x - 1, s->mb_y, ER_MB_END);
211
212                 return 0;
213             }
214         }
215
216         if (s->msmpeg4_version == 1) {
217             s->last_dc[0] =
218             s->last_dc[1] =
219             s->last_dc[2] = 128;
220         }
221
222         ff_init_block_index(s);
223         for (; s->mb_x < s->mb_width; s->mb_x++) {
224             int ret;
225
226             ff_update_block_index(s);
227
228             if (s->resync_mb_x == s->mb_x && s->resync_mb_y + 1 == s->mb_y)
229                 s->first_slice_line = 0;
230
231             /* DCT & quantize */
232
233             s->mv_dir  = MV_DIR_FORWARD;
234             s->mv_type = MV_TYPE_16X16;
235 //            s->mb_skipped = 0;
236             av_dlog(s, "%d %d %06X\n",
237                     ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
238             ret = s->decode_mb(s, s->block);
239
240             if (s->pict_type != AV_PICTURE_TYPE_B)
241                 ff_h263_update_motion_val(s);
242
243             if (ret < 0) {
244                 const int xy = s->mb_x + s->mb_y * s->mb_stride;
245                 if (ret == SLICE_END) {
246                     ff_MPV_decode_mb(s, s->block);
247                     if (s->loop_filter)
248                         ff_h263_loop_filter(s);
249
250                     ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
251                                     s->mb_x, s->mb_y, ER_MB_END & part_mask);
252
253                     s->padding_bug_score--;
254
255                     if (++s->mb_x >= s->mb_width) {
256                         s->mb_x = 0;
257                         ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
258                         ff_MPV_report_decode_progress(s);
259                         s->mb_y++;
260                     }
261                     return 0;
262                 } else if (ret == SLICE_NOEND) {
263                     av_log(s->avctx, AV_LOG_ERROR,
264                            "Slice mismatch at MB: %d\n", xy);
265                     ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
266                                     s->mb_x + 1, s->mb_y,
267                                     ER_MB_END & part_mask);
268                     return AVERROR_INVALIDDATA;
269                 }
270                 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
271                 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
272                                 s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
273
274                 return AVERROR_INVALIDDATA;
275             }
276
277             ff_MPV_decode_mb(s, s->block);
278             if (s->loop_filter)
279                 ff_h263_loop_filter(s);
280         }
281
282         ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
283         ff_MPV_report_decode_progress(s);
284
285         s->mb_x = 0;
286     }
287
288     av_assert1(s->mb_x == 0 && s->mb_y == s->mb_height);
289
290     if (s->codec_id == AV_CODEC_ID_MPEG4         &&
291         (s->workaround_bugs & FF_BUG_AUTODETECT) &&
292         get_bits_left(&s->gb) >= 48              &&
293         show_bits(&s->gb, 24) == 0x4010          &&
294         !s->data_partitioning)
295         s->padding_bug_score += 32;
296
297     /* try to detect the padding bug */
298     if (s->codec_id == AV_CODEC_ID_MPEG4         &&
299         (s->workaround_bugs & FF_BUG_AUTODETECT) &&
300         get_bits_left(&s->gb) >= 0               &&
301         get_bits_left(&s->gb) < 137              &&
302         // !s->resync_marker                     &&
303         !s->data_partitioning) {
304         const int bits_count = get_bits_count(&s->gb);
305         const int bits_left  = s->gb.size_in_bits - bits_count;
306
307         if (bits_left == 0) {
308             s->padding_bug_score += 16;
309         } else if (bits_left != 1) {
310             int v = show_bits(&s->gb, 8);
311             v |= 0x7F >> (7 - (bits_count & 7));
312
313             if (v == 0x7F && bits_left <= 8)
314                 s->padding_bug_score--;
315             else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
316                      bits_left <= 16)
317                 s->padding_bug_score += 4;
318             else
319                 s->padding_bug_score++;
320         }
321     }
322
323     if (s->workaround_bugs & FF_BUG_AUTODETECT) {
324         if (s->padding_bug_score > -2 && !s->data_partitioning
325             /* && (s->divx_version >= 0 || !s->resync_marker) */)
326             s->workaround_bugs |= FF_BUG_NO_PADDING;
327         else
328             s->workaround_bugs &= ~FF_BUG_NO_PADDING;
329     }
330
331     // handle formats which don't have unique end markers
332     if (s->msmpeg4_version || (s->workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
333         int left      = get_bits_left(&s->gb);
334         int max_extra = 7;
335
336         /* no markers in M$ crap */
337         if (s->msmpeg4_version && s->pict_type == AV_PICTURE_TYPE_I)
338             max_extra += 17;
339
340         /* buggy padding but the frame should still end approximately at
341          * the bitstream end */
342         if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
343             (s->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
344             max_extra += 48;
345         else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
346             max_extra += 256 * 256 * 256 * 64;
347
348         if (left > max_extra)
349             av_log(s->avctx, AV_LOG_ERROR,
350                    "discarding %d junk bits at end, next would be %X\n",
351                    left, show_bits(&s->gb, 24));
352         else if (left < 0)
353             av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
354         else
355             ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
356                             s->mb_x - 1, s->mb_y, ER_MB_END);
357
358         return 0;
359     }
360
361     av_log(s->avctx, AV_LOG_ERROR,
362            "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
363            get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
364
365     ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
366                     ER_MB_END & part_mask);
367
368     return AVERROR_INVALIDDATA;
369 }
370
371 int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
372                          AVPacket *avpkt)
373 {
374     const uint8_t *buf = avpkt->data;
375     int buf_size       = avpkt->size;
376     MpegEncContext *s  = avctx->priv_data;
377     int ret;
378     int slice_ret = 0;
379     AVFrame *pict = data;
380
381     s->flags  = avctx->flags;
382     s->flags2 = avctx->flags2;
383
384     /* no supplementary picture */
385     if (buf_size == 0) {
386         /* special case for last picture */
387         if (s->low_delay == 0 && s->next_picture_ptr) {
388             if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
389                 return ret;
390             s->next_picture_ptr = NULL;
391
392             *got_frame = 1;
393         }
394
395         return 0;
396     }
397
398     if (s->flags & CODEC_FLAG_TRUNCATED) {
399         int next;
400
401         if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) {
402             next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
403         } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) {
404             next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
405         } else if (CONFIG_H263P_DECODER && s->codec_id == AV_CODEC_ID_H263P) {
406             next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
407         } else {
408             av_log(s->avctx, AV_LOG_ERROR,
409                    "this codec does not support truncated bitstreams\n");
410             return AVERROR(ENOSYS);
411         }
412
413         if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf,
414                              &buf_size) < 0)
415             return buf_size;
416     }
417
418 retry:
419     if (s->divx_packed && s->bitstream_buffer_size) {
420         int i;
421         for(i=0; i < buf_size-3; i++) {
422             if (buf[i]==0 && buf[i+1]==0 && buf[i+2]==1) {
423                 if (buf[i+3]==0xB0) {
424                     av_log(s->avctx, AV_LOG_WARNING, "Discarding excessive bitstream in packed xvid\n");
425                     s->bitstream_buffer_size = 0;
426                 }
427                 break;
428             }
429         }
430     }
431
432     if (s->bitstream_buffer_size && (s->divx_packed || buf_size < 20)) // divx 5.01+/xvid frame reorder
433         ret = init_get_bits8(&s->gb, s->bitstream_buffer,
434                              s->bitstream_buffer_size);
435     else
436         ret = init_get_bits8(&s->gb, buf, buf_size);
437
438     s->bitstream_buffer_size = 0;
439     if (ret < 0)
440         return ret;
441
442     if (!s->context_initialized)
443         // we need the idct permutaton for reading a custom matrix
444         if ((ret = ff_MPV_common_init(s)) < 0)
445             return ret;
446
447     /* We need to set current_picture_ptr before reading the header,
448      * otherwise we cannot store anyting in there */
449     if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
450         int i = ff_find_unused_picture(s, 0);
451         if (i < 0)
452             return i;
453         s->current_picture_ptr = &s->picture[i];
454     }
455
456     /* let's go :-) */
457     if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
458         ret = ff_wmv2_decode_picture_header(s);
459     } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
460         ret = ff_msmpeg4_decode_picture_header(s);
461     } else if (CONFIG_MPEG4_DECODER && s->h263_pred) {
462         if (s->avctx->extradata_size && s->picture_number == 0) {
463             GetBitContext gb;
464
465             if (init_get_bits8(&gb, s->avctx->extradata, s->avctx->extradata_size) >= 0 )
466                 ff_mpeg4_decode_picture_header(s, &gb);
467         }
468         ret = ff_mpeg4_decode_picture_header(s, &s->gb);
469     } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
470         ret = ff_intel_h263_decode_picture_header(s);
471     } else if (CONFIG_FLV_DECODER && s->h263_flv) {
472         ret = ff_flv_decode_picture_header(s);
473     } else {
474         ret = ff_h263_decode_picture_header(s);
475     }
476
477     if (ret < 0 || ret == FRAME_SKIPPED) {
478         if (   s->width  != avctx->coded_width
479             || s->height != avctx->coded_height) {
480                 av_log(s->avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
481                 s->width = avctx->coded_width;
482                 s->height= avctx->coded_height;
483         }
484     }
485     if (ret == FRAME_SKIPPED)
486         return get_consumed_bytes(s, buf_size);
487
488     /* skip if the header was thrashed */
489     if (ret < 0) {
490         av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
491         return ret;
492     }
493
494     avctx->has_b_frames = !s->low_delay;
495
496     if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1) {
497         if (s->stream_codec_tag == AV_RL32("XVID") ||
498             s->codec_tag        == AV_RL32("XVID") ||
499             s->codec_tag        == AV_RL32("XVIX") ||
500             s->codec_tag        == AV_RL32("RMP4") ||
501             s->codec_tag        == AV_RL32("ZMP4") ||
502             s->codec_tag        == AV_RL32("SIPP"))
503             s->xvid_build = 0;
504 #if 0
505         if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
506             s->vol_control_parameters == 1 &&
507             s->padding_bug_score > 0 && s->low_delay) // XVID with modified fourcc
508             s->xvid_build = 0;
509 #endif
510     }
511
512     if (s->xvid_build == -1 && s->divx_version == -1 && s->lavc_build == -1)
513         if (s->codec_tag == AV_RL32("DIVX") && s->vo_type == 0 &&
514             s->vol_control_parameters == 0)
515             s->divx_version = 400;  // divx 4
516
517     if (s->xvid_build >= 0 && s->divx_version >= 0) {
518         s->divx_version =
519         s->divx_build   = -1;
520     }
521
522     if (s->workaround_bugs & FF_BUG_AUTODETECT) {
523         if (s->codec_tag == AV_RL32("XVIX"))
524             s->workaround_bugs |= FF_BUG_XVID_ILACE;
525
526         if (s->codec_tag == AV_RL32("UMP4"))
527             s->workaround_bugs |= FF_BUG_UMP4;
528
529         if (s->divx_version >= 500 && s->divx_build < 1814)
530             s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
531
532         if (s->divx_version > 502 && s->divx_build < 1814)
533             s->workaround_bugs |= FF_BUG_QPEL_CHROMA2;
534
535         if (s->xvid_build <= 3U)
536             s->padding_bug_score = 256 * 256 * 256 * 64;
537
538         if (s->xvid_build <= 1U)
539             s->workaround_bugs |= FF_BUG_QPEL_CHROMA;
540
541         if (s->xvid_build <= 12U)
542             s->workaround_bugs |= FF_BUG_EDGE;
543
544         if (s->xvid_build <= 32U)
545             s->workaround_bugs |= FF_BUG_DC_CLIP;
546
547 #define SET_QPEL_FUNC(postfix1, postfix2)                           \
548     s->dsp.put_        ## postfix1 = ff_put_        ## postfix2;    \
549     s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2;    \
550     s->dsp.avg_        ## postfix1 = ff_avg_        ## postfix2;
551
552         if (s->lavc_build < 4653U)
553             s->workaround_bugs |= FF_BUG_STD_QPEL;
554
555         if (s->lavc_build < 4655U)
556             s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
557
558         if (s->lavc_build < 4670U)
559             s->workaround_bugs |= FF_BUG_EDGE;
560
561         if (s->lavc_build <= 4712U)
562             s->workaround_bugs |= FF_BUG_DC_CLIP;
563
564         if (s->divx_version >= 0)
565             s->workaround_bugs |= FF_BUG_DIRECT_BLOCKSIZE;
566         if (s->divx_version == 501 && s->divx_build == 20020416)
567             s->padding_bug_score = 256 * 256 * 256 * 64;
568
569         if (s->divx_version < 500U)
570             s->workaround_bugs |= FF_BUG_EDGE;
571
572         if (s->divx_version >= 0)
573             s->workaround_bugs |= FF_BUG_HPEL_CHROMA;
574 #if 0
575         if (s->divx_version == 500)
576             s->padding_bug_score = 256 * 256 * 256 * 64;
577
578         /* very ugly XVID padding bug detection FIXME/XXX solve this differently
579          * Let us hope this at least works. */
580         if (s->resync_marker == 0 && s->data_partitioning == 0        &&
581             s->divx_version == -1 && s->codec_id == AV_CODEC_ID_MPEG4 &&
582             s->vo_type == 0)
583             s->workaround_bugs |= FF_BUG_NO_PADDING;
584
585         // FIXME not sure about the version num but a 4609 file seems ok
586         if (s->lavc_build < 4609U)
587             s->workaround_bugs |= FF_BUG_NO_PADDING;
588 #endif
589     }
590
591     if (s->workaround_bugs & FF_BUG_STD_QPEL) {
592         SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
593         SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
594         SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
595         SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
596         SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
597         SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
598
599         SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
600         SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
601         SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
602         SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
603         SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
604         SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
605     }
606
607     if (avctx->debug & FF_DEBUG_BUGS)
608         av_log(s->avctx, AV_LOG_DEBUG,
609                "bugs: %X lavc_build:%d xvid_build:%d divx_version:%d divx_build:%d %s\n",
610                s->workaround_bugs, s->lavc_build, s->xvid_build,
611                s->divx_version, s->divx_build, s->divx_packed ? "p" : "");
612
613 #if HAVE_MMX
614     if (s->codec_id == AV_CODEC_ID_MPEG4 && s->xvid_build >= 0 &&
615         avctx->idct_algo == FF_IDCT_AUTO &&
616         (av_get_cpu_flags() & AV_CPU_FLAG_MMX)) {
617         avctx->idct_algo = FF_IDCT_XVIDMMX;
618         ff_dct_common_init(s);
619         goto retry;
620     }
621 #endif
622
623     /* After H263 & mpeg4 header decode we have the height, width,
624      * and other parameters. So then we could init the picture.
625      * FIXME: By the way H263 decoder is evolving it should have
626      * an H263EncContext */
627     if (s->width  != avctx->coded_width  ||
628         s->height != avctx->coded_height ||
629         s->context_reinit) {
630         /* H.263 could change picture size any time */
631         s->context_reinit = 0;
632
633         avcodec_set_dimensions(avctx, s->width, s->height);
634
635         if ((ret = ff_MPV_common_frame_size_change(s)))
636             return ret;
637     }
638
639     if (s->codec_id == AV_CODEC_ID_H263  ||
640         s->codec_id == AV_CODEC_ID_H263P ||
641         s->codec_id == AV_CODEC_ID_H263I)
642         s->gob_index = ff_h263_get_gob_height(s);
643
644     // for skipping the frame
645     s->current_picture.f.pict_type = s->pict_type;
646     s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
647
648     /* skip B-frames if we don't have reference frames */
649     if (s->last_picture_ptr == NULL &&
650         (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
651         return get_consumed_bytes(s, buf_size);
652     if ((avctx->skip_frame >= AVDISCARD_NONREF &&
653          s->pict_type == AV_PICTURE_TYPE_B)    ||
654         (avctx->skip_frame >= AVDISCARD_NONKEY &&
655          s->pict_type != AV_PICTURE_TYPE_I)    ||
656         avctx->skip_frame >= AVDISCARD_ALL)
657         return get_consumed_bytes(s, buf_size);
658
659     if (s->next_p_frame_damaged) {
660         if (s->pict_type == AV_PICTURE_TYPE_B)
661             return get_consumed_bytes(s, buf_size);
662         else
663             s->next_p_frame_damaged = 0;
664     }
665
666     if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
667         s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
668         s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
669     } else {
670         s->me.qpel_put = s->dsp.put_no_rnd_qpel_pixels_tab;
671         s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
672     }
673
674     if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
675         return ret;
676
677     if (!s->divx_packed && !avctx->hwaccel)
678         ff_thread_finish_setup(avctx);
679
680     if (CONFIG_MPEG4_VDPAU_DECODER && (s->avctx->codec->capabilities & CODEC_CAP_HWACCEL_VDPAU)) {
681         ff_vdpau_mpeg4_decode_picture(s, s->gb.buffer, s->gb.buffer_end - s->gb.buffer);
682         goto frame_end;
683     }
684
685     if (avctx->hwaccel) {
686         ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer,
687                                           s->gb.buffer_end - s->gb.buffer);
688         if (ret < 0 )
689             return ret;
690     }
691
692     ff_mpeg_er_frame_start(s);
693
694     /* the second part of the wmv2 header contains the MB skip bits which
695      * are stored in current_picture->mb_type which is not available before
696      * ff_MPV_frame_start() */
697     if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
698         ret = ff_wmv2_decode_secondary_picture_header(s);
699         if (ret < 0)
700             return ret;
701         if (ret == 1)
702             goto frame_end;
703     }
704
705     /* decode each macroblock */
706     s->mb_x = 0;
707     s->mb_y = 0;
708
709     slice_ret = decode_slice(s);
710     while (s->mb_y < s->mb_height) {
711         if (s->msmpeg4_version) {
712             if (s->slice_height == 0 || s->mb_x != 0 ||
713                 (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
714                 break;
715         } else {
716             int prev_x = s->mb_x, prev_y = s->mb_y;
717             if (ff_h263_resync(s) < 0)
718                 break;
719             if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
720                 s->er.error_occurred = 1;
721         }
722
723         if (s->msmpeg4_version < 4 && s->h263_pred)
724             ff_mpeg4_clean_buffers(s);
725
726         if (decode_slice(s) < 0)
727             slice_ret = AVERROR_INVALIDDATA;
728     }
729
730     if (s->msmpeg4_version && s->msmpeg4_version < 4 &&
731         s->pict_type == AV_PICTURE_TYPE_I)
732         if (!CONFIG_MSMPEG4_DECODER ||
733             ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
734             s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
735
736     av_assert1(s->bitstream_buffer_size == 0);
737 frame_end:
738     ff_er_frame_end(&s->er);
739
740     if (avctx->hwaccel) {
741         ret = avctx->hwaccel->end_frame(avctx);
742         if (ret < 0)
743             return ret;
744     }
745
746     ff_MPV_frame_end(s);
747
748     /* divx 5.01+ bitstream reorder stuff */
749     /* Since this clobbers the input buffer and hwaccel codecs still need the
750      * data during hwaccel->end_frame we should not do this any earlier */
751     if (s->codec_id == AV_CODEC_ID_MPEG4 && s->divx_packed) {
752         int current_pos     = s->gb.buffer == s->bitstream_buffer ? 0 : (get_bits_count(&s->gb) >> 3);
753         int startcode_found = 0;
754
755         if (buf_size - current_pos > 7) {
756             int i;
757             for (i = current_pos; i < buf_size - 4; i++)
758                 if (buf[i]     == 0 &&
759                     buf[i + 1] == 0 &&
760                     buf[i + 2] == 1 &&
761                     buf[i + 3] == 0xB6) {
762                     startcode_found = !(buf[i + 4] & 0x40);
763                     break;
764                 }
765         }
766
767         if (startcode_found) {
768             av_fast_malloc(&s->bitstream_buffer,
769                            &s->allocated_bitstream_buffer_size,
770                            buf_size - current_pos +
771                            FF_INPUT_BUFFER_PADDING_SIZE);
772             if (!s->bitstream_buffer)
773                 return AVERROR(ENOMEM);
774             memcpy(s->bitstream_buffer, buf + current_pos,
775                    buf_size - current_pos);
776             s->bitstream_buffer_size = buf_size - current_pos;
777         }
778     }
779
780     if (!s->divx_packed && avctx->hwaccel)
781         ff_thread_finish_setup(avctx);
782
783     av_assert1(s->current_picture.f.pict_type == s->current_picture_ptr->f.pict_type);
784     av_assert1(s->current_picture.f.pict_type == s->pict_type);
785     if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
786         if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
787             return ret;
788         ff_print_debug_info(s, s->current_picture_ptr, pict);
789         ff_mpv_export_qp_table(s, pict, s->current_picture_ptr, FF_QSCALE_TYPE_MPEG1);
790     } else if (s->last_picture_ptr != NULL) {
791         if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
792             return ret;
793         ff_print_debug_info(s, s->last_picture_ptr, pict);
794         ff_mpv_export_qp_table(s, pict, s->last_picture_ptr, FF_QSCALE_TYPE_MPEG1);
795     }
796
797     if (s->last_picture_ptr || s->low_delay) {
798         if (   pict->format == AV_PIX_FMT_YUV420P
799             && (s->codec_tag == AV_RL32("GEOV") || s->codec_tag == AV_RL32("GEOX"))) {
800             int x, y, p;
801             av_frame_make_writable(pict);
802             for (p=0; p<3; p++) {
803                 int w = FF_CEIL_RSHIFT(pict-> width, !!p);
804                 int h = FF_CEIL_RSHIFT(pict->height, !!p);
805                 int linesize = pict->linesize[p];
806                 for (y=0; y<(h>>1); y++)
807                     for (x=0; x<w; x++)
808                         FFSWAP(int,
809                                pict->data[p][x + y*linesize],
810                                pict->data[p][x + (h-1-y)*linesize]);
811             }
812         }
813         *got_frame = 1;
814     }
815
816     if (slice_ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
817         return ret;
818     else
819         return get_consumed_bytes(s, buf_size);
820 }
821
822 const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
823 #if CONFIG_VAAPI
824     AV_PIX_FMT_VAAPI_VLD,
825 #endif
826 #if CONFIG_VDPAU
827     AV_PIX_FMT_VDPAU,
828 #endif
829     AV_PIX_FMT_YUV420P,
830     AV_PIX_FMT_NONE
831 };
832
833 AVCodec ff_h263_decoder = {
834     .name           = "h263",
835     .long_name      = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
836     .type           = AVMEDIA_TYPE_VIDEO,
837     .id             = AV_CODEC_ID_H263,
838     .priv_data_size = sizeof(MpegEncContext),
839     .init           = ff_h263_decode_init,
840     .close          = ff_h263_decode_end,
841     .decode         = ff_h263_decode_frame,
842     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
843                       CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
844     .flush          = ff_mpeg_flush,
845     .max_lowres     = 3,
846     .pix_fmts       = ff_h263_hwaccel_pixfmt_list_420,
847 };
848
849 AVCodec ff_h263p_decoder = {
850     .name           = "h263p",
851     .long_name      = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
852     .type           = AVMEDIA_TYPE_VIDEO,
853     .id             = AV_CODEC_ID_H263P,
854     .priv_data_size = sizeof(MpegEncContext),
855     .init           = ff_h263_decode_init,
856     .close          = ff_h263_decode_end,
857     .decode         = ff_h263_decode_frame,
858     .capabilities   = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
859                       CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
860     .flush          = ff_mpeg_flush,
861     .max_lowres     = 3,
862     .pix_fmts       = ff_h263_hwaccel_pixfmt_list_420,
863 };