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.

800 lines
27KB

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