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.

867 lines
30KB

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