lavc: Add an internal wrapper around get_format()
[libav.git] / libavcodec / h263dec.c
CommitLineData
de6d9b64 1/*
983e3246 2 * H.263 decoder
406792e7 3 * Copyright (c) 2001 Fabrice Bellard
8f2ab833 4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
de6d9b64 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
ff4ec49e
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
de6d9b64 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
de6d9b64 17 *
ff4ec49e 18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 21 */
115329f1 22
983e3246 23/**
ba87f080 24 * @file
983e3246
MN
25 * H.263 decoder.
26 */
115329f1 27
c6c98d08 28#include "libavutil/cpu.h"
de6d9b64 29#include "avcodec.h"
5f401b7b 30#include "error_resilience.h"
1ce3ec24 31#include "flv.h"
fc53b6af 32#include "h263.h"
c33d9814 33#include "h263_parser.h"
1ce3ec24
LB
34#include "internal.h"
35#include "mpeg4video.h"
df495dbd 36#include "mpeg4video_parser.h"
1ce3ec24 37#include "mpegvideo.h"
15025553 38#include "msmpeg4.h"
6a9c8594 39#include "thread.h"
de6d9b64 40
98a6fff9 41av_cold int ff_h263_decode_init(AVCodecContext *avctx)
de6d9b64
FB
42{
43 MpegEncContext *s = avctx->priv_data;
07493148 44 int ret;
519c2b6d 45
1ce3ec24
LB
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;
de6d9b64 51
4d2858de 52 // set defaults
efd29844 53 ff_MPV_decode_defaults(s);
1ce3ec24
LB
54 s->quant_precision = 5;
55 s->decode_mb = ff_h263_decode_mb;
56 s->low_delay = 1;
ee769c6a 57 if (avctx->codec->id == AV_CODEC_ID_MSS2)
716d413c 58 avctx->pix_fmt = AV_PIX_FMT_YUV420P;
ee769c6a 59 else
632ad224 60 avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
1ce3ec24 61 s->unrestricted_mv = 1;
4d2858de 62
de6d9b64 63 /* select sub codec */
1ce3ec24 64 switch (avctx->codec->id) {
36ef5369 65 case AV_CODEC_ID_H263:
1ce3ec24 66 s->unrestricted_mv = 0;
580a7465 67 avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
de6d9b64 68 break;
36ef5369 69 case AV_CODEC_ID_MPEG4:
de6d9b64 70 break;
36ef5369 71 case AV_CODEC_ID_MSMPEG4V1:
1ce3ec24
LB
72 s->h263_pred = 1;
73 s->msmpeg4_version = 1;
84afee34 74 break;
36ef5369 75 case AV_CODEC_ID_MSMPEG4V2:
1ce3ec24
LB
76 s->h263_pred = 1;
77 s->msmpeg4_version = 2;
84afee34 78 break;
36ef5369 79 case AV_CODEC_ID_MSMPEG4V3:
1ce3ec24
LB
80 s->h263_pred = 1;
81 s->msmpeg4_version = 3;
de6d9b64 82 break;
36ef5369 83 case AV_CODEC_ID_WMV1:
1ce3ec24
LB
84 s->h263_pred = 1;
85 s->msmpeg4_version = 4;
e1a9dbff 86 break;
36ef5369 87 case AV_CODEC_ID_WMV2:
1ce3ec24
LB
88 s->h263_pred = 1;
89 s->msmpeg4_version = 5;
8f8402e4 90 break;
36ef5369
AK
91 case AV_CODEC_ID_VC1:
92 case AV_CODEC_ID_WMV3:
93 case AV_CODEC_ID_VC1IMAGE:
94 case AV_CODEC_ID_WMV3IMAGE:
ee769c6a 95 case AV_CODEC_ID_MSS2:
1ce3ec24
LB
96 s->h263_pred = 1;
97 s->msmpeg4_version = 6;
580a7465 98 avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
0d33db8a 99 break;
36ef5369 100 case AV_CODEC_ID_H263I:
de6d9b64 101 break;
36ef5369 102 case AV_CODEC_ID_FLV1:
d4f5d74a
GM
103 s->h263_flv = 1;
104 break;
de6d9b64 105 default:
07493148
LB
106 av_log(avctx, AV_LOG_ERROR, "Unsupported codec %d\n",
107 avctx->codec->id);
108 return AVERROR(ENOSYS);
de6d9b64 109 }
1ce3ec24 110 s->codec_id = avctx->codec->id;
08303d77 111 avctx->hwaccel = ff_find_hwaccel(avctx);
93a21abd 112
de6d9b64 113 /* for h263, we allocate the images after having read the header */
1ce3ec24
LB
114 if (avctx->codec->id != AV_CODEC_ID_H263 &&
115 avctx->codec->id != AV_CODEC_ID_MPEG4)
07493148
LB
116 if ((ret = ff_MPV_common_init(s)) < 0)
117 return ret;
de6d9b64 118
0338c396 119 ff_h263dsp_init(&s->h263dsp);
1ce3ec24 120 ff_h263_decode_init_vlc();
115329f1 121
de6d9b64
FB
122 return 0;
123}
124
98a6fff9 125av_cold int ff_h263_decode_end(AVCodecContext *avctx)
de6d9b64
FB
126{
127 MpegEncContext *s = avctx->priv_data;
128
efd29844 129 ff_MPV_common_end(s);
de6d9b64
FB
130 return 0;
131}
132
7866eeff 133/**
58c42af7 134 * Return the number of bytes consumed for building the current frame.
7866eeff 135 */
1ce3ec24
LB
136static int get_consumed_bytes(MpegEncContext *s, int buf_size)
137{
138 int pos = (get_bits_count(&s->gb) + 7) >> 3;
115329f1 139
1ce3ec24
LB
140 if (s->divx_packed || s->avctx->hwaccel) {
141 /* We would have to scan through the whole buf to handle the weird
142 * reordering ... */
115329f1 143 return buf_size;
1ce3ec24 144 } else if (s->flags & CODEC_FLAG_TRUNCATED) {
d7425f59 145 pos -= s->parse_context.last_index;
1ce3ec24
LB
146 // padding is not really read so this might be -1
147 if (pos < 0)
148 pos = 0;
d7425f59 149 return pos;
1ce3ec24
LB
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;
7866eeff
MN
157
158 return pos;
159 }
160}
161
1ce3ec24
LB
162static int decode_slice(MpegEncContext *s)
163{
164 const int part_mask = s->partitioned_frame
165 ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
2bcbd984 166 const int mb_size = 16;
07493148 167 int ret;
115329f1 168
1ce3ec24
LB
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;
4d2858de 173
554daa24 174 ff_set_qscale(s, s->qscale);
115329f1 175
1468d503 176 if (s->avctx->hwaccel) {
1ce3ec24
LB
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));
1468d503
GB
181 return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
182 }
40e5d31b 183
1ce3ec24
LB
184 if (s->partitioned_frame) {
185 const int qscale = s->qscale;
4d2858de 186
1ce3ec24 187 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
1a890257 188 if ((ret = ff_mpeg4_decode_partitions(s->avctx->priv_data)) < 0)
07493148 189 return ret;
115329f1 190
d99ce8d7 191 /* restore variables which were modified */
1ce3ec24
LB
192 s->first_slice_line = 1;
193 s->mb_x = s->resync_mb_x;
194 s->mb_y = s->resync_mb_y;
554daa24 195 ff_set_qscale(s, qscale);
4d2858de
MN
196 }
197
1ce3ec24 198 for (; s->mb_y < s->mb_height; s->mb_y++) {
4d2858de 199 /* per-row end of slice checks */
1ce3ec24
LB
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);
46b4feec 204
4d2858de
MN
205 return 0;
206 }
207 }
115329f1 208
1ce3ec24
LB
209 if (s->msmpeg4_version == 1) {
210 s->last_dc[0] =
211 s->last_dc[1] =
212 s->last_dc[2] = 128;
4d2858de 213 }
115329f1 214
4d2858de 215 ff_init_block_index(s);
1ce3ec24 216 for (; s->mb_x < s->mb_width; s->mb_x++) {
4d2858de
MN
217 int ret;
218
219 ff_update_block_index(s);
220
1ce3ec24
LB
221 if (s->resync_mb_x == s->mb_x && s->resync_mb_y + 1 == s->mb_y)
222 s->first_slice_line = 0;
4d2858de
MN
223
224 /* DCT & quantize */
115329f1 225
1ce3ec24 226 s->mv_dir = MV_DIR_FORWARD;
4d2858de 227 s->mv_type = MV_TYPE_16X16;
1218777f
DB
228 av_dlog(s, "%d %d %06X\n",
229 ret, get_bits_count(&s->gb), show_bits(&s->gb, 24));
1ce3ec24 230 ret = s->decode_mb(s, s->block);
4d2858de 231
1ce3ec24 232 if (s->pict_type != AV_PICTURE_TYPE_B)
7d1c3fc1
MN
233 ff_h263_update_motion_val(s);
234
1ce3ec24
LB
235 if (ret < 0) {
236 const int xy = s->mb_x + s->mb_y * s->mb_stride;
237 if (ret == SLICE_END) {
efd29844 238 ff_MPV_decode_mb(s, s->block);
1ce3ec24 239 if (s->loop_filter)
332f9ac4 240 ff_h263_loop_filter(s);
aef95c8c 241
1ce3ec24
LB
242 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
243 s->mb_x, s->mb_y, ER_MB_END & part_mask);
a61f8265
MN
244
245 s->padding_bug_score--;
115329f1 246
1ce3ec24
LB
247 if (++s->mb_x >= s->mb_width) {
248 s->mb_x = 0;
249 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
efd29844 250 ff_MPV_report_decode_progress(s);
4d2858de
MN
251 s->mb_y++;
252 }
115329f1 253 return 0;
1ce3ec24
LB
254 } else if (ret == SLICE_NOEND) {
255 av_log(s->avctx, AV_LOG_ERROR,
256 "Slice mismatch at MB: %d\n", xy);
257 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
258 s->mb_x + 1, s->mb_y,
259 ER_MB_END & part_mask);
07493148 260 return AVERROR_INVALIDDATA;
4d2858de 261 }
9b879566 262 av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
1ce3ec24
LB
263 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
264 s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
115329f1 265
07493148 266 return AVERROR_INVALIDDATA;
4d2858de 267 }
aef95c8c 268
efd29844 269 ff_MPV_decode_mb(s, s->block);
1ce3ec24 270 if (s->loop_filter)
332f9ac4 271 ff_h263_loop_filter(s);
4d2858de 272 }
115329f1 273
1ce3ec24 274 ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
efd29844 275 ff_MPV_report_decode_progress(s);
115329f1 276
1ce3ec24 277 s->mb_x = 0;
4d2858de 278 }
115329f1 279
1ce3ec24 280 assert(s->mb_x == 0 && s->mb_y == s->mb_height);
4d2858de 281
1ce3ec24
LB
282 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
283 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
284 get_bits_left(&s->gb) >= 48 &&
285 show_bits(&s->gb, 24) == 0x4010 &&
286 !s->data_partitioning)
287 s->padding_bug_score += 32;
df59c9c0 288
a61f8265 289 /* try to detect the padding bug */
1ce3ec24
LB
290 if (s->codec_id == AV_CODEC_ID_MPEG4 &&
291 (s->workaround_bugs & FF_BUG_AUTODETECT) &&
292 get_bits_left(&s->gb) >= 0 &&
293 get_bits_left(&s->gb) < 48 &&
1ce3ec24
LB
294 !s->data_partitioning) {
295 const int bits_count = get_bits_count(&s->gb);
296 const int bits_left = s->gb.size_in_bits - bits_count;
297
298 if (bits_left == 0) {
299 s->padding_bug_score += 16;
300 } else if (bits_left != 1) {
301 int v = show_bits(&s->gb, 8);
302 v |= 0x7F >> (7 - (bits_count & 7));
303
304 if (v == 0x7F && bits_left <= 8)
a61f8265 305 s->padding_bug_score--;
1ce3ec24
LB
306 else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
307 bits_left <= 16)
308 s->padding_bug_score += 4;
a61f8265 309 else
115329f1
DB
310 s->padding_bug_score++;
311 }
a61f8265 312 }
115329f1 313
1ce3ec24 314 if (s->workaround_bugs & FF_BUG_AUTODETECT) {
ad09f525 315 if (s->padding_bug_score > -2 && !s->data_partitioning)
1ce3ec24 316 s->workaround_bugs |= FF_BUG_NO_PADDING;
4940ed6a
MN
317 else
318 s->workaround_bugs &= ~FF_BUG_NO_PADDING;
319 }
a61f8265 320
bb628dae 321 // handle formats which don't have unique end markers
1ce3ec24
LB
322 if (s->msmpeg4_version || (s->workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
323 int left = get_bits_left(&s->gb);
324 int max_extra = 7;
115329f1 325
4d2858de 326 /* no markers in M$ crap */
1ce3ec24
LB
327 if (s->msmpeg4_version && s->pict_type == AV_PICTURE_TYPE_I)
328 max_extra += 17;
329
330 /* buggy padding but the frame should still end approximately at
331 * the bitstream end */
332 if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
333 (s->err_recognition & AV_EF_BUFFER))
334 max_extra += 48;
335 else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
336 max_extra += 256 * 256 * 256 * 64;
337
338 if (left > max_extra)
339 av_log(s->avctx, AV_LOG_ERROR,
340 "discarding %d junk bits at end, next would be %X\n",
341 left, show_bits(&s->gb, 24));
342 else if (left < 0)
9b879566 343 av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
1ce3ec24
LB
344 else
345 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
346 s->mb_x - 1, s->mb_y, ER_MB_END);
115329f1 347
4d2858de
MN
348 return 0;
349 }
a61f8265 350
1ce3ec24
LB
351 av_log(s->avctx, AV_LOG_ERROR,
352 "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
353 get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
115329f1 354
1ce3ec24
LB
355 ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
356 ER_MB_END & part_mask);
46b4feec 357
07493148 358 return AVERROR_INVALIDDATA;
4d2858de
MN
359}
360
1ce3ec24
LB
361int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
362 AVPacket *avpkt)
de6d9b64 363{
7a00bbad 364 const uint8_t *buf = avpkt->data;
1ce3ec24
LB
365 int buf_size = avpkt->size;
366 MpegEncContext *s = avctx->priv_data;
1df1df0b 367 int ret;
115329f1
DB
368 AVFrame *pict = data;
369
1ce3ec24
LB
370 s->flags = avctx->flags;
371 s->flags2 = avctx->flags2;
6733417a 372
e9174ba4 373 /* no supplementary picture */
de6d9b64 374 if (buf_size == 0) {
e9174ba4 375 /* special case for last picture */
1ce3ec24 376 if (s->low_delay == 0 && s->next_picture_ptr) {
f6774f90 377 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
759001c5 378 return ret;
1ce3ec24 379 s->next_picture_ptr = NULL;
e9174ba4 380
df9b9567 381 *got_frame = 1;
e9174ba4
MN
382 }
383
de6d9b64
FB
384 return 0;
385 }
b7a22d84 386
1ce3ec24 387 if (s->flags & CODEC_FLAG_TRUNCATED) {
d7425f59 388 int next;
115329f1 389
1ce3ec24
LB
390 if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) {
391 next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
392 } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) {
393 next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
394 } else {
395 av_log(s->avctx, AV_LOG_ERROR,
396 "this codec does not support truncated bitstreams\n");
07493148 397 return AVERROR(ENOSYS);
d7425f59 398 }
115329f1 399
1ce3ec24
LB
400 if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf,
401 &buf_size) < 0)
d7425f59 402 return buf_size;
d7425f59 403 }
de6d9b64 404
1ce3ec24 405 if (s->bitstream_buffer_size && (s->divx_packed || buf_size < 20)) // divx 5.01+/xvid frame reorder
aaaf2dc0
MN
406 ret = init_get_bits8(&s->gb, s->bitstream_buffer,
407 s->bitstream_buffer_size);
1ce3ec24 408 else
aaaf2dc0 409 ret = init_get_bits8(&s->gb, buf, buf_size);
1ce3ec24 410 s->bitstream_buffer_size = 0;
115329f1 411
aaaf2dc0
MN
412 if (ret < 0)
413 return ret;
414
1ce3ec24 415 if (!s->context_initialized)
07493148
LB
416 // we need the idct permutaton for reading a custom matrix
417 if ((ret = ff_MPV_common_init(s)) < 0)
418 return ret;
115329f1 419
755bfeab
DB
420 /* We need to set current_picture_ptr before reading the header,
421 * otherwise we cannot store anyting in there */
f6774f90 422 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f->data[0]) {
1ce3ec24 423 int i = ff_find_unused_picture(s, 0);
4f820131
AU
424 if (i < 0)
425 return i;
1ce3ec24 426 s->current_picture_ptr = &s->picture[i];
5f194811 427 }
115329f1 428
de6d9b64 429 /* let's go :-) */
1ce3ec24
LB
430 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
431 ret = ff_wmv2_decode_picture_header(s);
49fb20cb 432 } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
04d38225 433 ret = ff_msmpeg4_decode_picture_header(s);
87337993 434 } else if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
1ce3ec24 435 if (s->avctx->extradata_size && s->picture_number == 0) {
b704e742 436 GetBitContext gb;
115329f1 437
aaaf2dc0
MN
438 ret = init_get_bits8(&gb, s->avctx->extradata,
439 s->avctx->extradata_size);
440 if (ret < 0)
441 return ret;
ee8af2dd 442 ff_mpeg4_decode_picture_header(avctx->priv_data, &gb);
b704e742 443 }
ee8af2dd 444 ret = ff_mpeg4_decode_picture_header(avctx->priv_data, &s->gb);
36ef5369 445 } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
d7525cba 446 ret = ff_intel_h263_decode_picture_header(s);
a0b0d753
MN
447 } else if (CONFIG_FLV_DECODER && s->h263_flv) {
448 ret = ff_flv_decode_picture_header(s);
de6d9b64 449 } else {
ddce8953 450 ret = ff_h263_decode_picture_header(s);
488fa19a 451 }
115329f1 452
1ce3ec24
LB
453 if (ret == FRAME_SKIPPED)
454 return get_consumed_bytes(s, buf_size);
ffba1dc0
MN
455
456 /* skip if the header was thrashed */
1ce3ec24 457 if (ret < 0) {
9b879566 458 av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
07493148 459 return ret;
ffba1dc0 460 }
115329f1 461
1ce3ec24 462 avctx->has_b_frames = !s->low_delay;
115329f1 463
1ce3ec24
LB
464#define SET_QPEL_FUNC(postfix1, postfix2) \
465 s->dsp.put_ ## postfix1 = ff_put_ ## postfix2; \
466 s->dsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
467 s->dsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
db794953 468
1ce3ec24
LB
469 if (s->workaround_bugs & FF_BUG_STD_QPEL) {
470 SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
471 SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
472 SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
db794953
MN
473 SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
474 SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
475 SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
476
1ce3ec24
LB
477 SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
478 SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
479 SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
db794953
MN
480 SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
481 SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
482 SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
483 }
4d2858de 484
1ce3ec24
LB
485 /* After H263 & mpeg4 header decode we have the height, width,
486 * and other parameters. So then we could init the picture.
487 * FIXME: By the way H263 decoder is evolving it should have
488 * an H263EncContext */
8701f4f8
JG
489 if (s->width != avctx->coded_width ||
490 s->height != avctx->coded_height ||
491 s->context_reinit) {
492 /* H.263 could change picture size any time */
493 s->context_reinit = 0;
494
3b086317
AK
495 ret = ff_set_dimensions(avctx, s->width, s->height);
496 if (ret < 0)
497 return ret;
8701f4f8
JG
498
499 if ((ret = ff_MPV_common_frame_size_change(s)))
500 return ret;
501 }
502
1ce3ec24
LB
503 if (s->codec_id == AV_CODEC_ID_H263 ||
504 s->codec_id == AV_CODEC_ID_H263P ||
505 s->codec_id == AV_CODEC_ID_H263I)
35aa136a 506 s->gob_index = ff_h263_get_gob_height(s);
115329f1 507
8ed2ae09 508 // for skipping the frame
f6774f90 509 s->current_picture.f->pict_type = s->pict_type;
510 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
347ae22e 511
bb628dae 512 /* skip B-frames if we don't have reference frames */
ba0c8981
DB
513 if (s->last_picture_ptr == NULL &&
514 (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
515 return get_consumed_bytes(s, buf_size);
1ce3ec24
LB
516 if ((avctx->skip_frame >= AVDISCARD_NONREF &&
517 s->pict_type == AV_PICTURE_TYPE_B) ||
518 (avctx->skip_frame >= AVDISCARD_NONKEY &&
519 s->pict_type != AV_PICTURE_TYPE_I) ||
520 avctx->skip_frame >= AVDISCARD_ALL)
8c3eba7c 521 return get_consumed_bytes(s, buf_size);
115329f1 522
1ce3ec24
LB
523 if (s->next_p_frame_damaged) {
524 if (s->pict_type == AV_PICTURE_TYPE_B)
7866eeff 525 return get_consumed_bytes(s, buf_size);
6733417a 526 else
1ce3ec24 527 s->next_p_frame_damaged = 0;
6733417a
MN
528 }
529
1ce3ec24
LB
530 if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
531 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
532 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
533 } else {
534 s->me.qpel_put = s->dsp.put_no_rnd_qpel_pixels_tab;
535 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
2833fc46
LM
536 }
537
07493148
LB
538 if ((ret = ff_MPV_frame_start(s, avctx)) < 0)
539 return ret;
de6d9b64 540
05fa79b8
JG
541 if (!s->divx_packed && !avctx->hwaccel)
542 ff_thread_finish_setup(avctx);
6a9c8594 543
07493148
LB
544 if (avctx->hwaccel) {
545 ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer,
546 s->gb.buffer_end - s->gb.buffer);
547 if (ret < 0 )
548 return ret;
549 }
1468d503 550
54974c62 551 ff_mpeg_er_frame_start(s);
115329f1 552
1ce3ec24
LB
553 /* the second part of the wmv2 header contains the MB skip bits which
554 * are stored in current_picture->mb_type which is not available before
555 * ff_MPV_frame_start() */
556 if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
9abc7e0f 557 ret = ff_wmv2_decode_secondary_picture_header(s);
1ce3ec24
LB
558 if (ret < 0)
559 return ret;
560 if (ret == 1)
561 goto intrax8_decoded;
487bce53
MN
562 }
563
de6d9b64 564 /* decode each macroblock */
1ce3ec24
LB
565 s->mb_x = 0;
566 s->mb_y = 0;
115329f1 567
2a11952f 568 ret = decode_slice(s);
1ce3ec24
LB
569 while (s->mb_y < s->mb_height) {
570 if (s->msmpeg4_version) {
571 if (s->slice_height == 0 || s->mb_x != 0 ||
572 (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
4d2858de 573 break;
1ce3ec24
LB
574 } else {
575 int prev_x = s->mb_x, prev_y = s->mb_y;
576 if (ff_h263_resync(s) < 0)
4d2858de 577 break;
6a9c8594 578 if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
54974c62 579 s->er.error_occurred = 1;
6733417a 580 }
115329f1 581
1ce3ec24 582 if (s->msmpeg4_version < 4 && s->h263_pred)
4d2858de 583 ff_mpeg4_clean_buffers(s);
649c00c9 584
1ce3ec24
LB
585 if (decode_slice(s) < 0)
586 ret = AVERROR_INVALIDDATA;
de6d9b64 587 }
8b32880c 588
1ce3ec24
LB
589 if (s->msmpeg4_version && s->msmpeg4_version < 4 &&
590 s->pict_type == AV_PICTURE_TYPE_I)
591 if (!CONFIG_MSMPEG4_DECODER ||
592 ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
54974c62 593 s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
115329f1 594
1ce3ec24 595 assert(s->bitstream_buffer_size == 0);
fb204d4b 596
e2ceb176
AK
597 if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4)
598 ff_mpeg4_frame_end(avctx, buf, buf_size);
6733417a 599
9abc7e0f 600intrax8_decoded:
54974c62 601 ff_er_frame_end(&s->er);
4d2858de 602
07493148
LB
603 if (avctx->hwaccel) {
604 ret = avctx->hwaccel->end_frame(avctx);
605 if (ret < 0)
606 return ret;
607 }
f986c8e2 608
efd29844 609 ff_MPV_frame_end(s);
db794953 610
05fa79b8
JG
611 if (!s->divx_packed && avctx->hwaccel)
612 ff_thread_finish_setup(avctx);
613
f6774f90 614 assert(s->current_picture.f->pict_type ==
615 s->current_picture_ptr->f->pict_type);
616 assert(s->current_picture.f->pict_type == s->pict_type);
975a1447 617 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
f6774f90 618 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
759001c5
AK
619 return ret;
620 ff_print_debug_info(s, s->current_picture_ptr);
5430b070 621 } else if (s->last_picture_ptr != NULL) {
f6774f90 622 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
759001c5
AK
623 return ret;
624 ff_print_debug_info(s, s->last_picture_ptr);
5430b070
MN
625 }
626
1ce3ec24 627 if (s->last_picture_ptr || s->low_delay)
df9b9567 628 *got_frame = 1;
8fc69895 629
1ce3ec24
LB
630 if (ret && (avctx->err_recognition & AV_EF_EXPLODE))
631 return ret;
632 else
633 return get_consumed_bytes(s, buf_size);
de6d9b64
FB
634}
635
8d061989 636const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
33259118 637#if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
8d061989
RB
638 AV_PIX_FMT_VAAPI_VLD,
639#endif
33259118 640#if CONFIG_H263_VDPAU_HWACCEL || CONFIG_MPEG4_VDPAU_HWACCEL
8d061989
RB
641 AV_PIX_FMT_VDPAU,
642#endif
643 AV_PIX_FMT_YUV420P,
644 AV_PIX_FMT_NONE
645};
646
d36beb3f 647AVCodec ff_h263_decoder = {
ec6402b7 648 .name = "h263",
b2bed932 649 .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
ec6402b7 650 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 651 .id = AV_CODEC_ID_H263,
ec6402b7
AK
652 .priv_data_size = sizeof(MpegEncContext),
653 .init = ff_h263_decode_init,
654 .close = ff_h263_decode_end,
655 .decode = ff_h263_decode_frame,
00c3b67b
MS
656 .capabilities = CODEC_CAP_DRAW_HORIZ_BAND | CODEC_CAP_DR1 |
657 CODEC_CAP_TRUNCATED | CODEC_CAP_DELAY,
658 .flush = ff_mpeg_flush,
8d061989 659 .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
de6d9b64 660};