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.

784 lines
26KB

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