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.

863 lines
29KB

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