h263dec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 "config_components.h"
  28. #include "avcodec.h"
  29. #include "codec_internal.h"
  30. #include "decode.h"
  31. #include "error_resilience.h"
  32. #include "flvdec.h"
  33. #include "h263.h"
  34. #include "h263dec.h"
  35. #include "hwaccel_internal.h"
  36. #include "hwconfig.h"
  37. #include "mpeg_er.h"
  38. #include "mpeg4video.h"
  39. #include "mpeg4videodec.h"
  40. #include "mpegvideo.h"
  41. #include "mpegvideodata.h"
  42. #include "mpegvideodec.h"
  43. #include "mpegvideo_unquantize.h"
  44. #include "msmpeg4dec.h"
  45. #include "thread.h"
  46. #include "wmv2dec.h"
  47. static const enum AVPixelFormat h263_hwaccel_pixfmt_list_420[] = {
  48. #if CONFIG_H263_VAAPI_HWACCEL || CONFIG_MPEG4_VAAPI_HWACCEL
  49. AV_PIX_FMT_VAAPI,
  50. #endif
  51. #if CONFIG_MPEG4_NVDEC_HWACCEL
  52. AV_PIX_FMT_CUDA,
  53. #endif
  54. #if CONFIG_MPEG4_VDPAU_HWACCEL
  55. AV_PIX_FMT_VDPAU,
  56. #endif
  57. #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL || CONFIG_MPEG4_VIDEOTOOLBOX_HWACCEL
  58. AV_PIX_FMT_VIDEOTOOLBOX,
  59. #endif
  60. AV_PIX_FMT_YUV420P,
  61. AV_PIX_FMT_NONE
  62. };
  63. static enum AVPixelFormat h263_get_format(AVCodecContext *avctx)
  64. {
  65. /* MPEG-4 Studio Profile only, not supported by hardware */
  66. if (avctx->bits_per_raw_sample > 8) {
  67. av_assert1(((MpegEncContext *)avctx->priv_data)->studio_profile);
  68. return avctx->pix_fmt;
  69. }
  70. if (CONFIG_GRAY && (avctx->flags & AV_CODEC_FLAG_GRAY)) {
  71. if (avctx->color_range == AVCOL_RANGE_UNSPECIFIED)
  72. avctx->color_range = AVCOL_RANGE_MPEG;
  73. return AV_PIX_FMT_GRAY8;
  74. }
  75. if (avctx->codec_id == AV_CODEC_ID_H263 ||
  76. avctx->codec_id == AV_CODEC_ID_H263P ||
  77. avctx->codec_id == AV_CODEC_ID_MPEG4)
  78. return avctx->pix_fmt = ff_get_format(avctx, h263_hwaccel_pixfmt_list_420);
  79. return AV_PIX_FMT_YUV420P;
  80. }
  81. av_cold int ff_h263_decode_init(AVCodecContext *avctx)
  82. {
  83. H263DecContext *const h = avctx->priv_data;
  84. MPVContext *const s = &h->c;
  85. MPVUnquantDSPContext unquant_dsp_ctx;
  86. int ret;
  87. s->out_format = FMT_H263;
  88. // set defaults
  89. ret = ff_mpv_decode_init(s, avctx);
  90. if (ret < 0)
  91. return ret;
  92. h->decode_mb = ff_h263_decode_mb;
  93. s->low_delay = 1;
  94. s->y_dc_scale_table =
  95. s->c_dc_scale_table = ff_mpeg1_dc_scale_table;
  96. ff_mpv_unquantize_init(&unquant_dsp_ctx,
  97. avctx->flags & AV_CODEC_FLAG_BITEXACT, 0);
  98. // dct_unquantize defaults for H.263;
  99. // they might change on a per-frame basis for MPEG-4;
  100. // dct_unquantize_inter will be unset for MSMPEG4 codecs later.
  101. s->dct_unquantize_intra = unquant_dsp_ctx.dct_unquantize_h263_intra;
  102. s->dct_unquantize_inter = unquant_dsp_ctx.dct_unquantize_h263_inter;
  103. /* select sub codec */
  104. switch (avctx->codec->id) {
  105. case AV_CODEC_ID_H263:
  106. case AV_CODEC_ID_H263P:
  107. avctx->chroma_sample_location = AVCHROMA_LOC_CENTER;
  108. h->decode_header = ff_h263_decode_picture_header;
  109. break;
  110. case AV_CODEC_ID_MPEG4:
  111. break;
  112. case AV_CODEC_ID_MSMPEG4V1:
  113. s->h263_pred = 1;
  114. s->msmpeg4_version = MSMP4_V1;
  115. break;
  116. case AV_CODEC_ID_MSMPEG4V2:
  117. s->h263_pred = 1;
  118. s->msmpeg4_version = MSMP4_V2;
  119. break;
  120. case AV_CODEC_ID_MSMPEG4V3:
  121. s->h263_pred = 1;
  122. s->msmpeg4_version = MSMP4_V3;
  123. break;
  124. case AV_CODEC_ID_WMV1:
  125. s->h263_pred = 1;
  126. s->msmpeg4_version = MSMP4_WMV1;
  127. break;
  128. case AV_CODEC_ID_WMV2:
  129. s->h263_pred = 1;
  130. s->msmpeg4_version = MSMP4_WMV2;
  131. break;
  132. case AV_CODEC_ID_RV10:
  133. case AV_CODEC_ID_RV20:
  134. break;
  135. #if CONFIG_H263I_DECODER
  136. case AV_CODEC_ID_H263I:
  137. h->decode_header = ff_intel_h263_decode_picture_header;
  138. break;
  139. #endif
  140. #if CONFIG_FLV_DECODER
  141. case AV_CODEC_ID_FLV1:
  142. h->decode_header = ff_flv_decode_picture_header;
  143. break;
  144. #endif
  145. default:
  146. av_unreachable("Switch contains a case for every codec using ff_h263_decode_init()");
  147. }
  148. if (avctx->codec_tag == AV_RL32("L263") || avctx->codec_tag == AV_RL32("S263"))
  149. if (avctx->extradata_size == 56 && avctx->extradata[0] == 1)
  150. h->ehc_mode = 1;
  151. /* for H.263, we allocate the images after having read the header */
  152. if (avctx->codec->id != AV_CODEC_ID_H263 &&
  153. avctx->codec->id != AV_CODEC_ID_H263P &&
  154. avctx->codec->id != AV_CODEC_ID_MPEG4) {
  155. avctx->pix_fmt = h263_get_format(avctx);
  156. if ((ret = ff_mpv_common_init(s)) < 0)
  157. return ret;
  158. }
  159. ff_h263dsp_init(&s->h263dsp);
  160. ff_h263_decode_init_vlc();
  161. return 0;
  162. }
  163. static void report_decode_progress(H263DecContext *const h)
  164. {
  165. if (h->c.pict_type != AV_PICTURE_TYPE_B && !h->partitioned_frame && !h->c.er.error_occurred)
  166. ff_thread_progress_report(&h->c.cur_pic.ptr->progress, h->c.mb_y);
  167. }
  168. static int decode_slice(H263DecContext *const h)
  169. {
  170. const int part_mask = h->partitioned_frame
  171. ? (ER_AC_END | ER_AC_ERROR) : 0x7F;
  172. const int mb_size = 16 >> h->c.avctx->lowres;
  173. int ret;
  174. h->last_resync_gb = h->gb;
  175. h->c.first_slice_line = 1;
  176. h->c.resync_mb_x = h->c.mb_x;
  177. h->c.resync_mb_y = h->c.mb_y;
  178. ff_set_qscale(&h->c, h->c.qscale);
  179. #if CONFIG_MPEG4_DECODER
  180. if (h->c.studio_profile) {
  181. if ((ret = ff_mpeg4_decode_studio_slice_header(h)) < 0)
  182. return ret;
  183. }
  184. #endif
  185. if (h->c.avctx->hwaccel) {
  186. const uint8_t *start = h->gb.buffer + get_bits_count(&h->gb) / 8;
  187. ret = FF_HW_CALL(h->c.avctx, decode_slice, start,
  188. get_bits_bytesize(&h->gb, 0) - get_bits_count(&h->gb) / 8);
  189. // ensure we exit decode loop
  190. h->c.mb_y = h->c.mb_height;
  191. return ret;
  192. }
  193. #if CONFIG_MPEG4_DECODER
  194. if (h->partitioned_frame) {
  195. const int qscale = h->c.qscale;
  196. av_assert1(h->c.codec_id == AV_CODEC_ID_MPEG4);
  197. ret = ff_mpeg4_decode_partitions(h);
  198. if (ret < 0)
  199. return ret;
  200. /* restore variables which were modified */
  201. h->c.first_slice_line = 1;
  202. h->c.mb_x = h->c.resync_mb_x;
  203. h->c.mb_y = h->c.resync_mb_y;
  204. ff_set_qscale(&h->c, qscale);
  205. }
  206. #endif
  207. for (; h->c.mb_y < h->c.mb_height; h->c.mb_y++) {
  208. /* per-row end of slice checks */
  209. if (h->c.msmpeg4_version != MSMP4_UNUSED) {
  210. if (h->c.resync_mb_y + h->slice_height == h->c.mb_y) {
  211. ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
  212. h->c.mb_x - 1, h->c.mb_y, ER_MB_END);
  213. return 0;
  214. }
  215. }
  216. if (h->c.msmpeg4_version == MSMP4_V1) {
  217. h->last_dc[0] =
  218. h->last_dc[1] =
  219. h->last_dc[2] = 128;
  220. }
  221. ff_init_block_index(&h->c);
  222. for (; h->c.mb_x < h->c.mb_width; h->c.mb_x++) {
  223. int ret;
  224. ff_update_block_index(&h->c, h->c.avctx->bits_per_raw_sample,
  225. h->c.avctx->lowres, h->c.chroma_x_shift);
  226. if (h->c.resync_mb_x == h->c.mb_x && h->c.resync_mb_y + 1 == h->c.mb_y)
  227. h->c.first_slice_line = 0;
  228. /* DCT & quantize */
  229. h->c.mv_dir = MV_DIR_FORWARD;
  230. h->c.mv_type = MV_TYPE_16X16;
  231. ff_dlog(h->c.avctx, "%d %06X\n",
  232. get_bits_count(&h->gb), show_bits(&h->gb, 24));
  233. ff_tlog(NULL, "Decoding MB at %dx%d\n", h->c.mb_x, h->c.mb_y);
  234. ret = h->decode_mb(h);
  235. if (h->c.h263_pred || h->c.h263_aic) {
  236. int mb_xy = h->c.mb_y * h->c.mb_stride + h->c.mb_x;
  237. if (!h->c.mb_intra) {
  238. ff_h263_clean_intra_table_entries(&h->c, mb_xy);
  239. } else
  240. h->c.mbintra_table[mb_xy] = 1;
  241. }
  242. if (h->c.pict_type != AV_PICTURE_TYPE_B)
  243. ff_h263_update_motion_val(&h->c);
  244. if (ret < 0) {
  245. const int xy = h->c.mb_x + h->c.mb_y * h->c.mb_stride;
  246. if (ret == SLICE_END) {
  247. ff_mpv_reconstruct_mb(&h->c, h->block);
  248. if (h->loop_filter)
  249. ff_h263_loop_filter(&h->c);
  250. ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
  251. h->c.mb_x, h->c.mb_y, ER_MB_END & part_mask);
  252. h->padding_bug_score--;
  253. if (++h->c.mb_x >= h->c.mb_width) {
  254. h->c.mb_x = 0;
  255. report_decode_progress(h);
  256. ff_mpeg_draw_horiz_band(&h->c, h->c.mb_y * mb_size, mb_size);
  257. h->c.mb_y++;
  258. }
  259. return 0;
  260. } else if (ret == SLICE_NOEND) {
  261. av_log(h->c.avctx, AV_LOG_ERROR,
  262. "Slice mismatch at MB: %d\n", xy);
  263. ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
  264. h->c.mb_x + 1, h->c.mb_y,
  265. ER_MB_END & part_mask);
  266. return AVERROR_INVALIDDATA;
  267. }
  268. av_log(h->c.avctx, AV_LOG_ERROR, "Error at MB: %d\n", xy);
  269. ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
  270. h->c.mb_x, h->c.mb_y, ER_MB_ERROR & part_mask);
  271. if ((h->c.avctx->err_recognition & AV_EF_IGNORE_ERR) && get_bits_left(&h->gb) > 0)
  272. continue;
  273. return AVERROR_INVALIDDATA;
  274. }
  275. ff_mpv_reconstruct_mb(&h->c, h->block);
  276. if (h->loop_filter)
  277. ff_h263_loop_filter(&h->c);
  278. }
  279. report_decode_progress(h);
  280. ff_mpeg_draw_horiz_band(&h->c, h->c.mb_y * mb_size, mb_size);
  281. h->c.mb_x = 0;
  282. }
  283. av_assert1(h->c.mb_x == 0 && h->c.mb_y == h->c.mb_height);
  284. // Detect incorrect padding with wrong stuffing codes used by NEC N-02B
  285. if (h->c.codec_id == AV_CODEC_ID_MPEG4 &&
  286. (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
  287. get_bits_left(&h->gb) >= 48 &&
  288. show_bits(&h->gb, 24) == 0x4010 &&
  289. !h->data_partitioning)
  290. h->padding_bug_score += 32;
  291. /* try to detect the padding bug */
  292. if (h->c.codec_id == AV_CODEC_ID_MPEG4 &&
  293. (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
  294. get_bits_left(&h->gb) >= 0 &&
  295. get_bits_left(&h->gb) < 137 &&
  296. !h->data_partitioning) {
  297. const int bits_count = get_bits_count(&h->gb);
  298. const int bits_left = h->gb.size_in_bits - bits_count;
  299. if (bits_left == 0) {
  300. h->padding_bug_score += 16;
  301. } else if (bits_left != 1) {
  302. int v = show_bits(&h->gb, 8);
  303. v |= 0x7F >> (7 - (bits_count & 7));
  304. if (v == 0x7F && bits_left <= 8)
  305. h->padding_bug_score--;
  306. else if (v == 0x7F && ((get_bits_count(&h->gb) + 8) & 8) &&
  307. bits_left <= 16)
  308. h->padding_bug_score += 4;
  309. else
  310. h->padding_bug_score++;
  311. }
  312. }
  313. if (h->c.codec_id == AV_CODEC_ID_H263 &&
  314. (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
  315. get_bits_left(&h->gb) >= 8 &&
  316. get_bits_left(&h->gb) < 300 &&
  317. h->c.pict_type == AV_PICTURE_TYPE_I &&
  318. show_bits(&h->gb, 8) == 0 &&
  319. !h->data_partitioning) {
  320. h->padding_bug_score += 32;
  321. }
  322. if (h->c.codec_id == AV_CODEC_ID_H263 &&
  323. (h->c.workaround_bugs & FF_BUG_AUTODETECT) &&
  324. get_bits_left(&h->gb) >= 64 &&
  325. AV_RB64(h->gb.buffer + (get_bits_bytesize(&h->gb, 0) - 8)) == 0xCDCDCDCDFC7F0000) {
  326. h->padding_bug_score += 32;
  327. }
  328. if (h->c.workaround_bugs & FF_BUG_AUTODETECT) {
  329. if (
  330. (h->padding_bug_score > -2 && !h->data_partitioning))
  331. h->c.workaround_bugs |= FF_BUG_NO_PADDING;
  332. else
  333. h->c.workaround_bugs &= ~FF_BUG_NO_PADDING;
  334. }
  335. // handle formats which don't have unique end markers
  336. if (h->c.msmpeg4_version != MSMP4_UNUSED || (h->c.workaround_bugs & FF_BUG_NO_PADDING)) { // FIXME perhaps solve this more cleanly
  337. int left = get_bits_left(&h->gb);
  338. int max_extra = 7;
  339. /* no markers in M$ crap */
  340. if (h->c.msmpeg4_version != MSMP4_UNUSED && h->c.pict_type == AV_PICTURE_TYPE_I)
  341. max_extra += 17;
  342. /* buggy padding but the frame should still end approximately at
  343. * the bitstream end */
  344. if ((h->c.workaround_bugs & FF_BUG_NO_PADDING) &&
  345. (h->c.avctx->err_recognition & (AV_EF_BUFFER|AV_EF_AGGRESSIVE)))
  346. max_extra += 48;
  347. else if ((h->c.workaround_bugs & FF_BUG_NO_PADDING))
  348. max_extra += 256 * 256 * 256 * 64;
  349. if (left > max_extra)
  350. av_log(h->c.avctx, AV_LOG_ERROR,
  351. "discarding %d junk bits at end, next would be %X\n",
  352. left, show_bits(&h->gb, 24));
  353. else if (left < 0)
  354. av_log(h->c.avctx, AV_LOG_ERROR, "overreading %d bits\n", -left);
  355. else
  356. ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y,
  357. h->c.mb_x - 1, h->c.mb_y, ER_MB_END);
  358. return 0;
  359. }
  360. av_log(h->c.avctx, AV_LOG_ERROR,
  361. "slice end not reached but screenspace end (%d left %06X, score= %d)\n",
  362. get_bits_left(&h->gb), show_bits(&h->gb, 24), h->padding_bug_score);
  363. ff_er_add_slice(&h->c.er, h->c.resync_mb_x, h->c.resync_mb_y, h->c.mb_x, h->c.mb_y,
  364. ER_MB_END & part_mask);
  365. return AVERROR_INVALIDDATA;
  366. }
  367. int ff_h263_decode_frame(AVCodecContext *avctx, AVFrame *pict,
  368. int *got_frame, AVPacket *avpkt)
  369. {
  370. H263DecContext *const h = avctx->priv_data;
  371. MPVContext *const s = &h->c;
  372. const uint8_t *buf = avpkt->data;
  373. int buf_size = avpkt->size;
  374. int ret;
  375. int slice_ret = 0;
  376. int bak_width, bak_height;
  377. /* no supplementary picture */
  378. if (buf_size == 0) {
  379. /* special case for last picture */
  380. if ((!h->c.low_delay || h->skipped_last_frame) && h->c.next_pic.ptr) {
  381. if ((ret = av_frame_ref(pict, h->c.next_pic.ptr->f)) < 0)
  382. return ret;
  383. if (h->skipped_last_frame) {
  384. /* If the stream ended with an NVOP, we output the last frame
  385. * in display order, but with the props from the last input
  386. * packet so that the stream's end time is correct. */
  387. ret = ff_decode_frame_props(avctx, pict);
  388. if (ret < 0)
  389. return ret;
  390. }
  391. ff_mpv_unref_picture(&h->c.next_pic);
  392. *got_frame = 1;
  393. }
  394. return 0;
  395. }
  396. // h->gb might be overridden in ff_mpeg4_decode_picture_header() below.
  397. ret = init_get_bits8(&h->gb, buf, buf_size);
  398. if (ret < 0)
  399. return ret;
  400. bak_width = h->c.width;
  401. bak_height = h->c.height;
  402. /* let's go :-) */
  403. ret = h->decode_header(h);
  404. if (ret < 0 || ret == FRAME_SKIPPED) {
  405. if ( h->c.width != bak_width
  406. || h->c.height != bak_height) {
  407. av_log(h->c.avctx, AV_LOG_WARNING, "Reverting picture dimensions change due to header decoding failure\n");
  408. h->c.width = bak_width;
  409. h->c.height= bak_height;
  410. }
  411. }
  412. if (ret == FRAME_SKIPPED)
  413. return buf_size;
  414. /* skip if the header was thrashed */
  415. if (ret < 0) {
  416. av_log(h->c.avctx, AV_LOG_ERROR, "header damaged\n");
  417. return ret;
  418. }
  419. if (!h->c.context_initialized) {
  420. avctx->pix_fmt = h263_get_format(avctx);
  421. if ((ret = ff_mpv_common_init(s)) < 0)
  422. return ret;
  423. }
  424. avctx->has_b_frames = !h->c.low_delay;
  425. #if CONFIG_MPEG4_DECODER
  426. if (avctx->codec_id == AV_CODEC_ID_MPEG4) {
  427. if (h->c.pict_type != AV_PICTURE_TYPE_B && h->c.mb_num/2 > get_bits_left(&h->gb))
  428. return AVERROR_INVALIDDATA;
  429. ff_mpeg4_workaround_bugs(avctx);
  430. if (h->c.studio_profile != (h->c.idsp.idct == NULL))
  431. ff_mpv_idct_init(s);
  432. }
  433. #endif
  434. /* After H.263 & MPEG-4 header decode we have the height, width,
  435. * and other parameters. So then we could init the picture. */
  436. if (h->c.width != avctx->coded_width ||
  437. h->c.height != avctx->coded_height ||
  438. h->c.context_reinit) {
  439. /* H.263 could change picture size any time */
  440. h->c.context_reinit = 0;
  441. ret = ff_set_dimensions(avctx, h->c.width, h->c.height);
  442. if (ret < 0)
  443. return ret;
  444. ff_set_sar(avctx, avctx->sample_aspect_ratio);
  445. if ((ret = ff_mpv_common_frame_size_change(s)))
  446. return ret;
  447. if (avctx->pix_fmt != h263_get_format(avctx)) {
  448. av_log(avctx, AV_LOG_ERROR, "format change not supported\n");
  449. avctx->pix_fmt = AV_PIX_FMT_NONE;
  450. return AVERROR_UNKNOWN;
  451. }
  452. }
  453. /* skip B-frames if we don't have reference frames */
  454. if (!h->c.last_pic.ptr &&
  455. (h->c.pict_type == AV_PICTURE_TYPE_B || h->c.droppable))
  456. return buf_size;
  457. if ((avctx->skip_frame >= AVDISCARD_NONREF &&
  458. h->c.pict_type == AV_PICTURE_TYPE_B) ||
  459. (avctx->skip_frame >= AVDISCARD_NONKEY &&
  460. h->c.pict_type != AV_PICTURE_TYPE_I) ||
  461. avctx->skip_frame >= AVDISCARD_ALL)
  462. return buf_size;
  463. if ((ret = ff_mpv_frame_start(s, avctx)) < 0)
  464. return ret;
  465. if (!h->divx_packed)
  466. ff_thread_finish_setup(avctx);
  467. if (avctx->hwaccel) {
  468. ret = FF_HW_CALL(avctx, start_frame, NULL,
  469. h->gb.buffer, get_bits_bytesize(&h->gb, 0));
  470. if (ret < 0 )
  471. return ret;
  472. }
  473. ff_mpv_er_frame_start_ext(s, h->partitioned_frame,
  474. s->pp_time, s->pb_time);
  475. /* the second part of the wmv2 header contains the MB skip bits which
  476. * are stored in current_picture->mb_type which is not available before
  477. * ff_mpv_frame_start() */
  478. #if CONFIG_WMV2_DECODER
  479. if (h->c.msmpeg4_version == MSMP4_WMV2) {
  480. ret = ff_wmv2_decode_secondary_picture_header(h);
  481. if (ret < 0)
  482. return ret;
  483. if (ret == 1)
  484. goto frame_end;
  485. }
  486. #endif
  487. /* decode each macroblock */
  488. h->c.mb_x = 0;
  489. h->c.mb_y = 0;
  490. slice_ret = decode_slice(h);
  491. while (h->c.mb_y < h->c.mb_height) {
  492. if (h->c.msmpeg4_version != MSMP4_UNUSED) {
  493. if (h->slice_height == 0 || h->c.mb_x != 0 || slice_ret < 0 ||
  494. (h->c.mb_y % h->slice_height) != 0 || get_bits_left(&h->gb) < 0)
  495. break;
  496. } else {
  497. int prev_x = h->c.mb_x, prev_y = h->c.mb_y;
  498. if (ff_h263_resync(h) < 0)
  499. break;
  500. if (prev_y * h->c.mb_width + prev_x < h->c.mb_y * h->c.mb_width + h->c.mb_x)
  501. h->c.er.error_occurred = 1;
  502. }
  503. if (h->c.msmpeg4_version < MSMP4_WMV1 && h->c.h263_pred)
  504. ff_mpeg4_clean_buffers(s);
  505. if (decode_slice(h) < 0)
  506. slice_ret = AVERROR_INVALIDDATA;
  507. }
  508. if (h->c.msmpeg4_version != MSMP4_UNUSED && h->c.msmpeg4_version < MSMP4_WMV1 &&
  509. h->c.pict_type == AV_PICTURE_TYPE_I)
  510. if (!CONFIG_MSMPEG4DEC ||
  511. ff_msmpeg4_decode_ext_header(h, buf_size) < 0)
  512. h->c.er.error_status_table[h->c.mb_num - 1] = ER_MB_ERROR;
  513. frame_end:
  514. if (!h->c.studio_profile)
  515. ff_er_frame_end(&h->c.er, NULL);
  516. if (avctx->hwaccel) {
  517. ret = FF_HW_SIMPLE_CALL(avctx, end_frame);
  518. if (ret < 0)
  519. return ret;
  520. }
  521. ff_mpv_frame_end(s);
  522. #if CONFIG_MPEG4_DECODER
  523. if (avctx->codec_id == AV_CODEC_ID_MPEG4)
  524. ff_mpeg4_frame_end(avctx, avpkt);
  525. #endif
  526. av_assert1(h->c.pict_type == h->c.cur_pic.ptr->f->pict_type);
  527. if (h->c.pict_type == AV_PICTURE_TYPE_B || h->c.low_delay) {
  528. if ((ret = av_frame_ref(pict, h->c.cur_pic.ptr->f)) < 0)
  529. return ret;
  530. ff_print_debug_info(s, h->c.cur_pic.ptr, pict);
  531. ff_mpv_export_qp_table(s, pict, h->c.cur_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
  532. } else if (h->c.last_pic.ptr) {
  533. if ((ret = av_frame_ref(pict, h->c.last_pic.ptr->f)) < 0)
  534. return ret;
  535. ff_print_debug_info(s, h->c.last_pic.ptr, pict);
  536. ff_mpv_export_qp_table(s, pict, h->c.last_pic.ptr, FF_MPV_QSCALE_TYPE_MPEG1);
  537. }
  538. if (h->c.last_pic.ptr || h->c.low_delay) {
  539. if ( pict->format == AV_PIX_FMT_YUV420P
  540. && (h->c.codec_tag == AV_RL32("GEOV") || h->c.codec_tag == AV_RL32("GEOX"))) {
  541. for (int p = 0; p < 3; p++) {
  542. int h = AV_CEIL_RSHIFT(pict->height, !!p);
  543. pict->data[p] += (h - 1) * pict->linesize[p];
  544. pict->linesize[p] *= -1;
  545. }
  546. }
  547. *got_frame = 1;
  548. }
  549. if (slice_ret < 0 && (avctx->err_recognition & AV_EF_EXPLODE))
  550. return slice_ret;
  551. else
  552. return buf_size;
  553. }
  554. static const AVCodecHWConfigInternal *const h263_hw_config_list[] = {
  555. #if CONFIG_H263_VAAPI_HWACCEL
  556. HWACCEL_VAAPI(h263),
  557. #endif
  558. #if CONFIG_MPEG4_NVDEC_HWACCEL
  559. HWACCEL_NVDEC(mpeg4),
  560. #endif
  561. #if CONFIG_MPEG4_VDPAU_HWACCEL
  562. HWACCEL_VDPAU(mpeg4),
  563. #endif
  564. #if CONFIG_H263_VIDEOTOOLBOX_HWACCEL
  565. HWACCEL_VIDEOTOOLBOX(h263),
  566. #endif
  567. NULL
  568. };
  569. const FFCodec ff_h263_decoder = {
  570. .p.name = "h263",
  571. CODEC_LONG_NAME("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
  572. .p.type = AVMEDIA_TYPE_VIDEO,
  573. .p.id = AV_CODEC_ID_H263,
  574. .priv_data_size = sizeof(H263DecContext),
  575. .init = ff_h263_decode_init,
  576. FF_CODEC_DECODE_CB(ff_h263_decode_frame),
  577. .close = ff_mpv_decode_close,
  578. .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  579. AV_CODEC_CAP_DELAY,
  580. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
  581. FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
  582. .flush = ff_mpeg_flush,
  583. .p.max_lowres = 3,
  584. .hw_configs = h263_hw_config_list,
  585. };
  586. const FFCodec ff_h263p_decoder = {
  587. .p.name = "h263p",
  588. CODEC_LONG_NAME("H.263 / H.263-1996, H.263+ / H.263-1998 / H.263 version 2"),
  589. .p.type = AVMEDIA_TYPE_VIDEO,
  590. .p.id = AV_CODEC_ID_H263P,
  591. .priv_data_size = sizeof(H263DecContext),
  592. .init = ff_h263_decode_init,
  593. FF_CODEC_DECODE_CB(ff_h263_decode_frame),
  594. .close = ff_mpv_decode_close,
  595. .p.capabilities = AV_CODEC_CAP_DRAW_HORIZ_BAND | AV_CODEC_CAP_DR1 |
  596. AV_CODEC_CAP_DELAY,
  597. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP |
  598. FF_CODEC_CAP_SKIP_FRAME_FILL_PARAM,
  599. .flush = ff_mpeg_flush,
  600. .p.max_lowres = 3,
  601. .hw_configs = h263_hw_config_list,
  602. };