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