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