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