You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

691 lines
23KB

  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. * @file
  24. * H.263 decoder.
  25. */
  26. #include "libavutil/cpu.h"
  27. #include "avcodec.h"
  28. #include "error_resilience.h"
  29. #include "flv.h"
  30. #include "h263.h"
  31. #include "h263_parser.h"
  32. #include "hwaccel.h"
  33. #include "internal.h"
  34. #include "mpeg_er.h"
  35. #include "mpeg4video.h"
  36. #include "mpeg4video_parser.h"
  37. #include "mpegutils.h"
  38. #include "mpegvideo.h"
  39. #include "msmpeg4.h"
  40. #include "qpeldsp.h"
  41. #include "thread.h"
  42. #include "wmv2.h"
  43. static enum AVPixelFormat h263_get_format(AVCodecContext *avctx)
  44. {
  45. if (avctx->codec->id == AV_CODEC_ID_MSS2)
  46. return AV_PIX_FMT_YUV420P;
  47. return avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
  48. }
  49. av_cold int ff_h263_decode_init(AVCodecContext *avctx)
  50. {
  51. MpegEncContext *s = avctx->priv_data;
  52. int ret;
  53. s->avctx = avctx;
  54. s->out_format = FMT_H263;
  55. s->width = avctx->coded_width;
  56. s->height = avctx->coded_height;
  57. s->workaround_bugs = avctx->workaround_bugs;
  58. // set defaults
  59. ff_mpv_decode_defaults(s);
  60. s->quant_precision = 5;
  61. s->decode_mb = ff_h263_decode_mb;
  62. s->low_delay = 1;
  63. s->unrestricted_mv = 1;
  64. /* select sub codec */
  65. switch (avctx->codec->id) {
  66. case AV_CODEC_ID_H263:
  67. s->unrestricted_mv = 0;
  68. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  69. break;
  70. case AV_CODEC_ID_MPEG4:
  71. break;
  72. case AV_CODEC_ID_MSMPEG4V1:
  73. s->h263_pred = 1;
  74. s->msmpeg4_version = 1;
  75. break;
  76. case AV_CODEC_ID_MSMPEG4V2:
  77. s->h263_pred = 1;
  78. s->msmpeg4_version = 2;
  79. break;
  80. case AV_CODEC_ID_MSMPEG4V3:
  81. s->h263_pred = 1;
  82. s->msmpeg4_version = 3;
  83. break;
  84. case AV_CODEC_ID_WMV1:
  85. s->h263_pred = 1;
  86. s->msmpeg4_version = 4;
  87. break;
  88. case AV_CODEC_ID_WMV2:
  89. s->h263_pred = 1;
  90. s->msmpeg4_version = 5;
  91. break;
  92. case AV_CODEC_ID_VC1:
  93. case AV_CODEC_ID_WMV3:
  94. case AV_CODEC_ID_VC1IMAGE:
  95. case AV_CODEC_ID_WMV3IMAGE:
  96. case AV_CODEC_ID_MSS2:
  97. s->h263_pred = 1;
  98. s->msmpeg4_version = 6;
  99. avctx->chroma_sample_location = AVCHROMA_LOC_LEFT;
  100. break;
  101. case AV_CODEC_ID_H263I:
  102. break;
  103. case AV_CODEC_ID_FLV1:
  104. s->h263_flv = 1;
  105. break;
  106. default:
  107. av_log(avctx, AV_LOG_ERROR, "Unsupported codec %d\n",
  108. avctx->codec->id);
  109. return AVERROR(ENOSYS);
  110. }
  111. s->codec_id = avctx->codec->id;
  112. /* for H.263, we allocate the images after having read the header */
  113. if (avctx->codec->id != AV_CODEC_ID_H263 &&
  114. avctx->codec->id != AV_CODEC_ID_MPEG4) {
  115. avctx->pix_fmt = h263_get_format(avctx);
  116. ff_mpv_idct_init(s);
  117. if ((ret = ff_mpv_common_init(s)) < 0)
  118. return ret;
  119. }
  120. ff_h263dsp_init(&s->h263dsp);
  121. ff_qpeldsp_init(&s->qdsp);
  122. ff_h263_decode_init_vlc();
  123. return 0;
  124. }
  125. av_cold int ff_h263_decode_end(AVCodecContext *avctx)
  126. {
  127. MpegEncContext *s = avctx->priv_data;
  128. ff_mpv_common_end(s);
  129. return 0;
  130. }
  131. /**
  132. * Return the number of bytes consumed for building the current frame.
  133. */
  134. static int get_consumed_bytes(MpegEncContext *s, int buf_size)
  135. {
  136. int pos = (get_bits_count(&s->gb) + 7) >> 3;
  137. if (s->divx_packed || s->avctx->hwaccel) {
  138. /* We would have to scan through the whole buf to handle the weird
  139. * reordering ... */
  140. return buf_size;
  141. } else if (s->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
  142. pos -= s->parse_context.last_index;
  143. // padding is not really read so this might be -1
  144. if (pos < 0)
  145. pos = 0;
  146. return pos;
  147. } else {
  148. // avoid infinite loops (maybe not needed...)
  149. if (pos == 0)
  150. pos = 1;
  151. // oops ;)
  152. if (pos + 10 > buf_size)
  153. pos = buf_size;
  154. return pos;
  155. }
  156. }
  157. static int decode_slice(MpegEncContext *s)
  158. {
  159. const int part_mask = s->partitioned_frame
  160. ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
  161. const int mb_size = 16;
  162. int ret;
  163. s->last_resync_gb = s->gb;
  164. s->first_slice_line = 1;
  165. s->resync_mb_x = s->mb_x;
  166. s->resync_mb_y = s->mb_y;
  167. ff_set_qscale(s, s->qscale);
  168. if (s->avctx->hwaccel) {
  169. const uint8_t *start = s->gb.buffer + get_bits_count(&s->gb) / 8;
  170. const uint8_t *end = ff_h263_find_resync_marker(start + 1,
  171. s->gb.buffer_end);
  172. skip_bits_long(&s->gb, 8 * (end - start));
  173. return s->avctx->hwaccel->decode_slice(s->avctx, start, end - start);
  174. }
  175. if (s->partitioned_frame) {
  176. const int qscale = s->qscale;
  177. if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4)
  178. if ((ret = ff_mpeg4_decode_partitions(s->avctx->priv_data)) < 0)
  179. return ret;
  180. /* restore variables which were modified */
  181. s->first_slice_line = 1;
  182. s->mb_x = s->resync_mb_x;
  183. s->mb_y = s->resync_mb_y;
  184. ff_set_qscale(s, qscale);
  185. }
  186. for (; s->mb_y < s->mb_height; s->mb_y++) {
  187. /* per-row end of slice checks */
  188. if (s->msmpeg4_version) {
  189. if (s->resync_mb_y + s->slice_height == s->mb_y) {
  190. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  191. s->mb_x - 1, s->mb_y, ER_MB_END);
  192. return 0;
  193. }
  194. }
  195. if (s->msmpeg4_version == 1) {
  196. s->last_dc[0] =
  197. s->last_dc[1] =
  198. s->last_dc[2] = 128;
  199. }
  200. ff_init_block_index(s);
  201. for (; s->mb_x < s->mb_width; s->mb_x++) {
  202. int ret;
  203. ff_update_block_index(s);
  204. if (s->resync_mb_x == s->mb_x && s->resync_mb_y + 1 == s->mb_y)
  205. s->first_slice_line = 0;
  206. /* DCT & quantize */
  207. s->mv_dir = MV_DIR_FORWARD;
  208. s->mv_type = MV_TYPE_16X16;
  209. ff_dlog(s, "%d %06X\n",
  210. get_bits_count(&s->gb), show_bits(&s->gb, 24));
  211. ret = s->decode_mb(s, s->block);
  212. if (s->pict_type != AV_PICTURE_TYPE_B)
  213. ff_h263_update_motion_val(s);
  214. if (ret < 0) {
  215. const int xy = s->mb_x + s->mb_y * s->mb_stride;
  216. if (ret == SLICE_END) {
  217. ff_mpv_decode_mb(s, s->block);
  218. if (s->loop_filter)
  219. ff_h263_loop_filter(s);
  220. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  221. s->mb_x, s->mb_y, ER_MB_END & part_mask);
  222. s->padding_bug_score--;
  223. if (++s->mb_x >= s->mb_width) {
  224. s->mb_x = 0;
  225. ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
  226. ff_mpv_report_decode_progress(s);
  227. s->mb_y++;
  228. }
  229. return 0;
  230. } else if (ret == SLICE_NOEND) {
  231. av_log(s->avctx, AV_LOG_ERROR,
  232. "Slice mismatch at MB: %d\n", xy);
  233. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  234. s->mb_x + 1, s->mb_y,
  235. ER_MB_END & part_mask);
  236. return AVERROR_INVALIDDATA;
  237. }
  238. av_log(s->avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
  239. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  240. s->mb_x, s->mb_y, ER_MB_ERROR & part_mask);
  241. return AVERROR_INVALIDDATA;
  242. }
  243. ff_mpv_decode_mb(s, s->block);
  244. if (s->loop_filter)
  245. ff_h263_loop_filter(s);
  246. }
  247. ff_mpeg_draw_horiz_band(s, s->mb_y * mb_size, mb_size);
  248. ff_mpv_report_decode_progress(s);
  249. s->mb_x = 0;
  250. }
  251. assert(s->mb_x == 0 && s->mb_y == s->mb_height);
  252. if (s->codec_id == AV_CODEC_ID_MPEG4 &&
  253. (s->workaround_bugs & FF_BUG_AUTODETECT) &&
  254. get_bits_left(&s->gb) >= 48 &&
  255. show_bits(&s->gb, 24) == 0x4010 &&
  256. !s->data_partitioning)
  257. s->padding_bug_score += 32;
  258. /* try to detect the padding bug */
  259. if (s->codec_id == AV_CODEC_ID_MPEG4 &&
  260. (s->workaround_bugs & FF_BUG_AUTODETECT) &&
  261. get_bits_left(&s->gb) >= 0 &&
  262. get_bits_left(&s->gb) < 48 &&
  263. !s->data_partitioning) {
  264. const int bits_count = get_bits_count(&s->gb);
  265. const int bits_left = s->gb.size_in_bits - bits_count;
  266. if (bits_left == 0) {
  267. s->padding_bug_score += 16;
  268. } else if (bits_left != 1) {
  269. int v = show_bits(&s->gb, 8);
  270. v |= 0x7F >> (7 - (bits_count & 7));
  271. if (v == 0x7F && bits_left <= 8)
  272. s->padding_bug_score--;
  273. else if (v == 0x7F && ((get_bits_count(&s->gb) + 8) & 8) &&
  274. bits_left <= 16)
  275. s->padding_bug_score += 4;
  276. else
  277. s->padding_bug_score++;
  278. }
  279. }
  280. if (s->workaround_bugs & FF_BUG_AUTODETECT) {
  281. if (s->codec_id == AV_CODEC_ID_H263 ||
  282. (s->padding_bug_score > -2 && !s->data_partitioning))
  283. s->workaround_bugs |= FF_BUG_NO_PADDING;
  284. else
  285. s->workaround_bugs &= ~FF_BUG_NO_PADDING;
  286. }
  287. // handle formats which don't have unique end markers
  288. if (s->msmpeg4_version || (s->workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
  289. int left = get_bits_left(&s->gb);
  290. int max_extra = 7;
  291. /* no markers in M$ crap */
  292. if (s->msmpeg4_version && s->pict_type == AV_PICTURE_TYPE_I)
  293. max_extra += 17;
  294. /* buggy padding but the frame should still end approximately at
  295. * the bitstream end */
  296. if ((s->workaround_bugs & FF_BUG_NO_PADDING) &&
  297. (s->avctx->err_recognition & AV_EF_BUFFER))
  298. max_extra += 48;
  299. else if ((s->workaround_bugs & FF_BUG_NO_PADDING))
  300. max_extra += 256 * 256 * 256 * 64;
  301. if (left > max_extra)
  302. av_log(s->avctx, AV_LOG_ERROR,
  303. "discarding %d junk bits at end, next would be %X\n",
  304. left, show_bits(&s->gb, 24));
  305. else if (left < 0)
  306. av_log(s->avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
  307. else
  308. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y,
  309. s->mb_x - 1, s->mb_y, ER_MB_END);
  310. return 0;
  311. }
  312. av_log(s->avctx, AV_LOG_ERROR,
  313. "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
  314. get_bits_left(&s->gb), show_bits(&s->gb, 24), s->padding_bug_score);
  315. ff_er_add_slice(&s->er, s->resync_mb_x, s->resync_mb_y, s->mb_x, s->mb_y,
  316. ER_MB_END & part_mask);
  317. return AVERROR_INVALIDDATA;
  318. }
  319. int ff_h263_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  320. AVPacket *avpkt)
  321. {
  322. const uint8_t *buf = avpkt->data;
  323. int buf_size = avpkt->size;
  324. MpegEncContext *s = avctx->priv_data;
  325. int ret;
  326. AVFrame *pict = data;
  327. /* no supplementary picture */
  328. if (buf_size == 0) {
  329. /* special case for last picture */
  330. if (s->low_delay == 0 && s->next_picture_ptr) {
  331. if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
  332. return ret;
  333. s->next_picture_ptr = NULL;
  334. *got_frame = 1;
  335. }
  336. return 0;
  337. }
  338. if (s->avctx->flags & AV_CODEC_FLAG_TRUNCATED) {
  339. int next;
  340. if (CONFIG_MPEG4_DECODER && s->codec_id == AV_CODEC_ID_MPEG4) {
  341. next = ff_mpeg4_find_frame_end(&s->parse_context, buf, buf_size);
  342. } else if (CONFIG_H263_DECODER && s->codec_id == AV_CODEC_ID_H263) {
  343. next = ff_h263_find_frame_end(&s->parse_context, buf, buf_size);
  344. } else {
  345. av_log(s->avctx, AV_LOG_ERROR,
  346. "this codec does not support truncated bitstreams\n");
  347. return AVERROR(ENOSYS);
  348. }
  349. if (ff_combine_frame(&s->parse_context, next, (const uint8_t **)&buf,
  350. &buf_size) < 0)
  351. return buf_size;
  352. }
  353. if (s->bitstream_buffer_size && (s->divx_packed || buf_size < 20)) // divx 5.01+/xvid frame reorder
  354. ret = init_get_bits8(&s->gb, s->bitstream_buffer,
  355. s->bitstream_buffer_size);
  356. else
  357. ret = init_get_bits8(&s->gb, buf, buf_size);
  358. s->bitstream_buffer_size = 0;
  359. if (ret < 0)
  360. return ret;
  361. if (!s->context_initialized)
  362. // we need the idct permutation for reading a custom matrix
  363. ff_mpv_idct_init(s);
  364. /* let's go :-) */
  365. if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
  366. ret = ff_wmv2_decode_picture_header(s);
  367. } else if (CONFIG_MSMPEG4_DECODER && s->msmpeg4_version) {
  368. ret = ff_msmpeg4_decode_picture_header(s);
  369. } else if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4) {
  370. if (s->avctx->extradata_size && s->picture_number == 0) {
  371. GetBitContext gb;
  372. ret = init_get_bits8(&gb, s->avctx->extradata,
  373. s->avctx->extradata_size);
  374. if (ret < 0)
  375. return ret;
  376. ff_mpeg4_decode_picture_header(avctx->priv_data, &gb);
  377. }
  378. ret = ff_mpeg4_decode_picture_header(avctx->priv_data, &s->gb);
  379. } else if (CONFIG_H263I_DECODER && s->codec_id == AV_CODEC_ID_H263I) {
  380. ret = ff_intel_h263_decode_picture_header(s);
  381. } else if (CONFIG_FLV_DECODER && s->h263_flv) {
  382. ret = ff_flv_decode_picture_header(s);
  383. } else {
  384. ret = ff_h263_decode_picture_header(s);
  385. }
  386. if (ret == FRAME_SKIPPED)
  387. return get_consumed_bytes(s, buf_size);
  388. /* skip if the header was thrashed */
  389. if (ret < 0) {
  390. av_log(s->avctx, AV_LOG_ERROR, "header damaged\n");
  391. return ret;
  392. }
  393. if (!s->context_initialized) {
  394. avctx->pix_fmt = h263_get_format(avctx);
  395. if ((ret = ff_mpv_common_init(s)) < 0)
  396. return ret;
  397. }
  398. if (!s->current_picture_ptr || s->current_picture_ptr->f->data[0]) {
  399. int i = ff_find_unused_picture(s->avctx, s->picture, 0);
  400. if (i < 0)
  401. return i;
  402. s->current_picture_ptr = &s->picture[i];
  403. }
  404. avctx->has_b_frames = !s->low_delay;
  405. #define SET_QPEL_FUNC(postfix1, postfix2) \
  406. s->qdsp.put_ ## postfix1 = ff_put_ ## postfix2; \
  407. s->qdsp.put_no_rnd_ ## postfix1 = ff_put_no_rnd_ ## postfix2; \
  408. s->qdsp.avg_ ## postfix1 = ff_avg_ ## postfix2;
  409. if (s->workaround_bugs & FF_BUG_STD_QPEL) {
  410. SET_QPEL_FUNC(qpel_pixels_tab[0][5], qpel16_mc11_old_c)
  411. SET_QPEL_FUNC(qpel_pixels_tab[0][7], qpel16_mc31_old_c)
  412. SET_QPEL_FUNC(qpel_pixels_tab[0][9], qpel16_mc12_old_c)
  413. SET_QPEL_FUNC(qpel_pixels_tab[0][11], qpel16_mc32_old_c)
  414. SET_QPEL_FUNC(qpel_pixels_tab[0][13], qpel16_mc13_old_c)
  415. SET_QPEL_FUNC(qpel_pixels_tab[0][15], qpel16_mc33_old_c)
  416. SET_QPEL_FUNC(qpel_pixels_tab[1][5], qpel8_mc11_old_c)
  417. SET_QPEL_FUNC(qpel_pixels_tab[1][7], qpel8_mc31_old_c)
  418. SET_QPEL_FUNC(qpel_pixels_tab[1][9], qpel8_mc12_old_c)
  419. SET_QPEL_FUNC(qpel_pixels_tab[1][11], qpel8_mc32_old_c)
  420. SET_QPEL_FUNC(qpel_pixels_tab[1][13], qpel8_mc13_old_c)
  421. SET_QPEL_FUNC(qpel_pixels_tab[1][15], qpel8_mc33_old_c)
  422. }
  423. /* After H.263 & MPEG-4 header decode we have the height, width,
  424. * and other parameters. So then we could init the picture.
  425. * FIXME: By the way H.263 decoder is evolving it should have
  426. * an H263EncContext */
  427. if (s->width != avctx->coded_width ||
  428. s->height != avctx->coded_height ||
  429. s->context_reinit) {
  430. /* H.263 could change picture size any time */
  431. s->context_reinit = 0;
  432. ret = ff_set_dimensions(avctx, s->width, s->height);
  433. if (ret < 0)
  434. return ret;
  435. ff_set_sar(avctx, avctx->sample_aspect_ratio);
  436. if ((ret = ff_mpv_common_frame_size_change(s)))
  437. return ret;
  438. if (avctx->pix_fmt != h263_get_format(avctx)) {
  439. av_log(avctx, AV_LOG_ERROR, "format change not supported\n");
  440. avctx->pix_fmt = AV_PIX_FMT_NONE;
  441. return AVERROR_UNKNOWN;
  442. }
  443. }
  444. if (s->codec_id == AV_CODEC_ID_H263 ||
  445. s->codec_id == AV_CODEC_ID_H263P ||
  446. s->codec_id == AV_CODEC_ID_H263I)
  447. s->gob_index = H263_GOB_HEIGHT(s->height);
  448. // for skipping the frame
  449. s->current_picture.f->pict_type = s->pict_type;
  450. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  451. /* skip B-frames if we don't have reference frames */
  452. if (!s->last_picture_ptr &&
  453. (s->pict_type == AV_PICTURE_TYPE_B || s->droppable))
  454. return get_consumed_bytes(s, buf_size);
  455. if ((avctx->skip_frame >= AVDISCARD_NONREF &&
  456. s->pict_type == AV_PICTURE_TYPE_B) ||
  457. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  458. s->pict_type != AV_PICTURE_TYPE_I) ||
  459. avctx->skip_frame >= AVDISCARD_ALL)
  460. return get_consumed_bytes(s, buf_size);
  461. if (s->next_p_frame_damaged) {
  462. if (s->pict_type == AV_PICTURE_TYPE_B)
  463. return get_consumed_bytes(s, buf_size);
  464. else
  465. s->next_p_frame_damaged = 0;
  466. }
  467. if ((!s->no_rounding) || s->pict_type == AV_PICTURE_TYPE_B) {
  468. s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
  469. s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
  470. } else {
  471. s->me.qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
  472. s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
  473. }
  474. if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
  475. return ret;
  476. if (!s->divx_packed)
  477. ff_thread_finish_setup(avctx);
  478. if (avctx->hwaccel) {
  479. ret = avctx->hwaccel->start_frame(avctx, s->gb.buffer,
  480. s->gb.buffer_end - s->gb.buffer);
  481. if (ret < 0 )
  482. return ret;
  483. }
  484. ff_mpeg_er_frame_start(s);
  485. /* the second part of the wmv2 header contains the MB skip bits which
  486. * are stored in current_picture->mb_type which is not available before
  487. * ff_mpv_frame_start() */
  488. if (CONFIG_WMV2_DECODER && s->msmpeg4_version == 5) {
  489. ret = ff_wmv2_decode_secondary_picture_header(s);
  490. if (ret < 0)
  491. return ret;
  492. if (ret == 1)
  493. goto intrax8_decoded;
  494. }
  495. /* decode each macroblock */
  496. s->mb_x = 0;
  497. s->mb_y = 0;
  498. ret = decode_slice(s);
  499. while (s->mb_y < s->mb_height) {
  500. if (s->msmpeg4_version) {
  501. if (s->slice_height == 0 || s->mb_x != 0 ||
  502. (s->mb_y % s->slice_height) != 0 || get_bits_left(&s->gb) < 0)
  503. break;
  504. } else {
  505. int prev_x = s->mb_x, prev_y = s->mb_y;
  506. if (ff_h263_resync(s) < 0)
  507. break;
  508. if (prev_y * s->mb_width + prev_x < s->mb_y * s->mb_width + s->mb_x)
  509. s->er.error_occurred = 1;
  510. }
  511. if (s->msmpeg4_version < 4 && s->h263_pred)
  512. ff_mpeg4_clean_buffers(s);
  513. if (decode_slice(s) < 0)
  514. ret = AVERROR_INVALIDDATA;
  515. }
  516. if (s->msmpeg4_version && s->msmpeg4_version < 4 &&
  517. s->pict_type == AV_PICTURE_TYPE_I)
  518. if (!CONFIG_MSMPEG4_DECODER ||
  519. ff_msmpeg4_decode_ext_header(s, buf_size) < 0)
  520. s->er.error_status_table[s->mb_num - 1] = ER_MB_ERROR;
  521. assert(s->bitstream_buffer_size == 0);
  522. if (CONFIG_MPEG4_DECODER && avctx->codec_id == AV_CODEC_ID_MPEG4)
  523. ff_mpeg4_frame_end(avctx, buf, buf_size);
  524. intrax8_decoded:
  525. ff_er_frame_end(&s->er);
  526. if (avctx->hwaccel) {
  527. ret = avctx->hwaccel->end_frame(avctx);
  528. if (ret < 0)
  529. return ret;
  530. }
  531. ff_mpv_frame_end(s);
  532. if (!s->divx_packed && avctx->hwaccel)
  533. ff_thread_finish_setup(avctx);
  534. assert(s->current_picture.f->pict_type ==
  535. s->current_picture_ptr->f->pict_type);
  536. assert(s->current_picture.f->pict_type == s->pict_type);
  537. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  538. if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
  539. return ret;
  540. ff_print_debug_info(s, s->current_picture_ptr);
  541. } else if (s->last_picture_ptr) {
  542. if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
  543. return ret;
  544. ff_print_debug_info(s, s->last_picture_ptr);
  545. }
  546. if (s->last_picture_ptr || s->low_delay)
  547. *got_frame = 1;
  548. if (ret && (avctx->err_recognition & AV_EF_EXPLODE))
  549. return ret;
  550. else
  551. return get_consumed_bytes(s, buf_size);
  552. }
  553. const enum AVPixelFormat ff_h263_hwaccel_pixfmt_list_420[] = {
  554. #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
  555. AV_PIX_FMT_VAAPI,
  556. #endif
  557. #if CONFIG_MPEG4_VDPAU_HWACCEL
  558. AV_PIX_FMT_VDPAU,
  559. #endif
  560. AV_PIX_FMT_YUV420P,
  561. AV_PIX_FMT_NONE
  562. };
  563. AVCodec ff_h263_decoder = {
  564. .name = "h263",
  565. .long_name = NULL_IF_CONFIG_SMALL("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
  566. .type = AVMEDIA_TYPE_VIDEO,
  567. .id = AV_CODEC_ID_H263,
  568. .priv_data_size = sizeof(MpegEncContext),
  569. .init = ff_h263_decode_init,
  570. .close = ff_h263_decode_end,
  571. .decode = ff_h263_decode_frame,
  572. .capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  573. AV_CODEC_CAP_TRUNCATED | AV_CODEC_CAP_DELAY,
  574. .flush = ff_mpeg_flush,
  575. .pix_fmts = ff_h263_hwaccel_pixfmt_list_420,
  576. .hw_configs = (const AVCodecHWConfigInternal*[]) {
  577. #if CONFIG_H263_VAAPI_HWACCEL
  578. HWACCEL_VAAPI(h263),
  579. #endif
  580. #if CONFIG_MPEG4_VDPAU_HWACCEL
  581. HWACCEL_VDPAU(mpeg4),
  582. #endif
  583. NULL
  584. },
  585. };