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.

766 lines
25KB

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