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.

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