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.

1228 lines
46KB

  1. /*
  2. * VC-1 and WMV3 decoder
  3. * Copyright (c) 2011 Mashiat Sarker Shakkhar
  4. * Copyright (c) 2006-2007 Konstantin Shishkov
  5. * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * VC-1 and WMV3 decoder
  26. */
  27. #include "avcodec.h"
  28. #include "blockdsp.h"
  29. #include "get_bits.h"
  30. #include "internal.h"
  31. #include "mpeg_er.h"
  32. #include "mpegvideo.h"
  33. #include "msmpeg4.h"
  34. #include "msmpeg4data.h"
  35. #include "vc1.h"
  36. #include "vc1data.h"
  37. #include "vdpau_compat.h"
  38. #include "libavutil/avassert.h"
  39. #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
  40. typedef struct SpriteData {
  41. /**
  42. * Transform coefficients for both sprites in 16.16 fixed point format,
  43. * in the order they appear in the bitstream:
  44. * x scale
  45. * rotation 1 (unused)
  46. * x offset
  47. * rotation 2 (unused)
  48. * y scale
  49. * y offset
  50. * alpha
  51. */
  52. int coefs[2][7];
  53. int effect_type, effect_flag;
  54. int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
  55. int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
  56. } SpriteData;
  57. static inline int get_fp_val(GetBitContext* gb)
  58. {
  59. return (get_bits_long(gb, 30) - (1 << 29)) << 1;
  60. }
  61. static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
  62. {
  63. c[1] = c[3] = 0;
  64. switch (get_bits(gb, 2)) {
  65. case 0:
  66. c[0] = 1 << 16;
  67. c[2] = get_fp_val(gb);
  68. c[4] = 1 << 16;
  69. break;
  70. case 1:
  71. c[0] = c[4] = get_fp_val(gb);
  72. c[2] = get_fp_val(gb);
  73. break;
  74. case 2:
  75. c[0] = get_fp_val(gb);
  76. c[2] = get_fp_val(gb);
  77. c[4] = get_fp_val(gb);
  78. break;
  79. case 3:
  80. c[0] = get_fp_val(gb);
  81. c[1] = get_fp_val(gb);
  82. c[2] = get_fp_val(gb);
  83. c[3] = get_fp_val(gb);
  84. c[4] = get_fp_val(gb);
  85. break;
  86. }
  87. c[5] = get_fp_val(gb);
  88. if (get_bits1(gb))
  89. c[6] = get_fp_val(gb);
  90. else
  91. c[6] = 1 << 16;
  92. }
  93. static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
  94. {
  95. AVCodecContext *avctx = v->s.avctx;
  96. int sprite, i;
  97. for (sprite = 0; sprite <= v->two_sprites; sprite++) {
  98. vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
  99. if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
  100. avpriv_request_sample(avctx, "Non-zero rotation coefficients");
  101. av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
  102. for (i = 0; i < 7; i++)
  103. av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
  104. sd->coefs[sprite][i] / (1<<16),
  105. (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
  106. av_log(avctx, AV_LOG_DEBUG, "\n");
  107. }
  108. skip_bits(gb, 2);
  109. if (sd->effect_type = get_bits_long(gb, 30)) {
  110. switch (sd->effect_pcount1 = get_bits(gb, 4)) {
  111. case 7:
  112. vc1_sprite_parse_transform(gb, sd->effect_params1);
  113. break;
  114. case 14:
  115. vc1_sprite_parse_transform(gb, sd->effect_params1);
  116. vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
  117. break;
  118. default:
  119. for (i = 0; i < sd->effect_pcount1; i++)
  120. sd->effect_params1[i] = get_fp_val(gb);
  121. }
  122. if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
  123. // effect 13 is simple alpha blending and matches the opacity above
  124. av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
  125. for (i = 0; i < sd->effect_pcount1; i++)
  126. av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
  127. sd->effect_params1[i] / (1 << 16),
  128. (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
  129. av_log(avctx, AV_LOG_DEBUG, "\n");
  130. }
  131. sd->effect_pcount2 = get_bits(gb, 16);
  132. if (sd->effect_pcount2 > 10) {
  133. av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
  134. return AVERROR_INVALIDDATA;
  135. } else if (sd->effect_pcount2) {
  136. i = -1;
  137. av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
  138. while (++i < sd->effect_pcount2) {
  139. sd->effect_params2[i] = get_fp_val(gb);
  140. av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
  141. sd->effect_params2[i] / (1 << 16),
  142. (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
  143. }
  144. av_log(avctx, AV_LOG_DEBUG, "\n");
  145. }
  146. }
  147. if (sd->effect_flag = get_bits1(gb))
  148. av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
  149. if (get_bits_count(gb) >= gb->size_in_bits +
  150. (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) {
  151. av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
  152. return AVERROR_INVALIDDATA;
  153. }
  154. if (get_bits_count(gb) < gb->size_in_bits - 8)
  155. av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
  156. return 0;
  157. }
  158. static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
  159. {
  160. int i, plane, row, sprite;
  161. int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
  162. uint8_t* src_h[2][2];
  163. int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
  164. int ysub[2];
  165. MpegEncContext *s = &v->s;
  166. for (i = 0; i <= v->two_sprites; i++) {
  167. xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
  168. xadv[i] = sd->coefs[i][0];
  169. if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
  170. xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
  171. yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
  172. yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
  173. }
  174. alpha = av_clip_uint16(sd->coefs[1][6]);
  175. for (plane = 0; plane < (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++) {
  176. int width = v->output_width>>!!plane;
  177. for (row = 0; row < v->output_height>>!!plane; row++) {
  178. uint8_t *dst = v->sprite_output_frame->data[plane] +
  179. v->sprite_output_frame->linesize[plane] * row;
  180. for (sprite = 0; sprite <= v->two_sprites; sprite++) {
  181. uint8_t *iplane = s->current_picture.f->data[plane];
  182. int iline = s->current_picture.f->linesize[plane];
  183. int ycoord = yoff[sprite] + yadv[sprite] * row;
  184. int yline = ycoord >> 16;
  185. int next_line;
  186. ysub[sprite] = ycoord & 0xFFFF;
  187. if (sprite) {
  188. iplane = s->last_picture.f->data[plane];
  189. iline = s->last_picture.f->linesize[plane];
  190. }
  191. next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
  192. if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
  193. src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
  194. if (ysub[sprite])
  195. src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
  196. } else {
  197. if (sr_cache[sprite][0] != yline) {
  198. if (sr_cache[sprite][1] == yline) {
  199. FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
  200. FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
  201. } else {
  202. v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
  203. sr_cache[sprite][0] = yline;
  204. }
  205. }
  206. if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
  207. v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
  208. iplane + next_line, xoff[sprite],
  209. xadv[sprite], width);
  210. sr_cache[sprite][1] = yline + 1;
  211. }
  212. src_h[sprite][0] = v->sr_rows[sprite][0];
  213. src_h[sprite][1] = v->sr_rows[sprite][1];
  214. }
  215. }
  216. if (!v->two_sprites) {
  217. if (ysub[0]) {
  218. v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
  219. } else {
  220. memcpy(dst, src_h[0][0], width);
  221. }
  222. } else {
  223. if (ysub[0] && ysub[1]) {
  224. v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
  225. src_h[1][0], src_h[1][1], ysub[1], alpha, width);
  226. } else if (ysub[0]) {
  227. v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
  228. src_h[1][0], alpha, width);
  229. } else if (ysub[1]) {
  230. v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
  231. src_h[0][0], (1<<16)-1-alpha, width);
  232. } else {
  233. v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
  234. }
  235. }
  236. }
  237. if (!plane) {
  238. for (i = 0; i <= v->two_sprites; i++) {
  239. xoff[i] >>= 1;
  240. yoff[i] >>= 1;
  241. }
  242. }
  243. }
  244. }
  245. static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
  246. {
  247. int ret;
  248. MpegEncContext *s = &v->s;
  249. AVCodecContext *avctx = s->avctx;
  250. SpriteData sd;
  251. memset(&sd, 0, sizeof(sd));
  252. ret = vc1_parse_sprites(v, gb, &sd);
  253. if (ret < 0)
  254. return ret;
  255. if (!s->current_picture.f || !s->current_picture.f->data[0]) {
  256. av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
  257. return AVERROR_UNKNOWN;
  258. }
  259. if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
  260. av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
  261. v->two_sprites = 0;
  262. }
  263. av_frame_unref(v->sprite_output_frame);
  264. if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0)
  265. return ret;
  266. vc1_draw_sprites(v, &sd);
  267. return 0;
  268. }
  269. static void vc1_sprite_flush(AVCodecContext *avctx)
  270. {
  271. VC1Context *v = avctx->priv_data;
  272. MpegEncContext *s = &v->s;
  273. AVFrame *f = s->current_picture.f;
  274. int plane, i;
  275. /* Windows Media Image codecs have a convergence interval of two keyframes.
  276. Since we can't enforce it, clear to black the missing sprite. This is
  277. wrong but it looks better than doing nothing. */
  278. if (f && f->data[0])
  279. for (plane = 0; plane < (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY ? 1 : 3); plane++)
  280. for (i = 0; i < v->sprite_height>>!!plane; i++)
  281. memset(f->data[plane] + i * f->linesize[plane],
  282. plane ? 128 : 0, f->linesize[plane]);
  283. }
  284. #endif
  285. av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
  286. {
  287. MpegEncContext *s = &v->s;
  288. int i;
  289. int mb_height = FFALIGN(s->mb_height, 2);
  290. /* Allocate mb bitplanes */
  291. v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
  292. v->direct_mb_plane = av_malloc (s->mb_stride * mb_height);
  293. v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
  294. v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height);
  295. v->acpred_plane = av_malloc (s->mb_stride * mb_height);
  296. v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
  297. v->n_allocated_blks = s->mb_width + 2;
  298. v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
  299. v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
  300. v->cbp = v->cbp_base + s->mb_stride;
  301. v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
  302. v->ttblk = v->ttblk_base + s->mb_stride;
  303. v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
  304. v->is_intra = v->is_intra_base + s->mb_stride;
  305. v->luma_mv_base = av_mallocz(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
  306. v->luma_mv = v->luma_mv_base + s->mb_stride;
  307. /* allocate block type info in that way so it could be used with s->block_index[] */
  308. v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  309. v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
  310. v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
  311. v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
  312. /* allocate memory to store block level MV info */
  313. v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  314. v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
  315. v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
  316. v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
  317. v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  318. v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
  319. v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
  320. v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  321. /* Init coded blocks info */
  322. if (v->profile == PROFILE_ADVANCED) {
  323. // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
  324. // return -1;
  325. // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
  326. // return -1;
  327. }
  328. ff_intrax8_common_init(&v->x8,s);
  329. if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  330. for (i = 0; i < 4; i++)
  331. if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
  332. return AVERROR(ENOMEM);
  333. }
  334. if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
  335. !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
  336. !v->mb_type_base) {
  337. av_freep(&v->mv_type_mb_plane);
  338. av_freep(&v->direct_mb_plane);
  339. av_freep(&v->acpred_plane);
  340. av_freep(&v->over_flags_plane);
  341. av_freep(&v->block);
  342. av_freep(&v->cbp_base);
  343. av_freep(&v->ttblk_base);
  344. av_freep(&v->is_intra_base);
  345. av_freep(&v->luma_mv_base);
  346. av_freep(&v->mb_type_base);
  347. return AVERROR(ENOMEM);
  348. }
  349. return 0;
  350. }
  351. av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
  352. {
  353. int i;
  354. for (i = 0; i < 64; i++) {
  355. #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
  356. v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
  357. v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
  358. v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
  359. v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
  360. v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
  361. }
  362. v->left_blk_sh = 0;
  363. v->top_blk_sh = 3;
  364. }
  365. /** Initialize a VC1/WMV3 decoder
  366. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  367. * @todo TODO: Decypher remaining bits in extra_data
  368. */
  369. static av_cold int vc1_decode_init(AVCodecContext *avctx)
  370. {
  371. VC1Context *v = avctx->priv_data;
  372. MpegEncContext *s = &v->s;
  373. GetBitContext gb;
  374. int ret;
  375. /* save the container output size for WMImage */
  376. v->output_width = avctx->width;
  377. v->output_height = avctx->height;
  378. if (!avctx->extradata_size || !avctx->extradata)
  379. return -1;
  380. if (!CONFIG_GRAY || !(avctx->flags & AV_CODEC_FLAG_GRAY))
  381. avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
  382. else {
  383. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  384. if (avctx->color_range == AVCOL_RANGE_UNSPECIFIED)
  385. avctx->color_range = AVCOL_RANGE_MPEG;
  386. }
  387. v->s.avctx = avctx;
  388. if ((ret = ff_vc1_init_common(v)) < 0)
  389. return ret;
  390. // ensure static VLC tables are initialized
  391. if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
  392. return ret;
  393. if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
  394. return ret;
  395. // Hack to ensure the above functions will be called
  396. // again once we know all necessary settings.
  397. // That this is necessary might indicate a bug.
  398. ff_vc1_decode_end(avctx);
  399. ff_blockdsp_init(&s->bdsp, avctx);
  400. ff_h264chroma_init(&v->h264chroma, 8);
  401. ff_qpeldsp_init(&s->qdsp);
  402. if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
  403. int count = 0;
  404. // looks like WMV3 has a sequence header stored in the extradata
  405. // advanced sequence header may be before the first frame
  406. // the last byte of the extradata is a version number, 1 for the
  407. // samples we can decode
  408. init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
  409. if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
  410. return ret;
  411. count = avctx->extradata_size*8 - get_bits_count(&gb);
  412. if (count > 0) {
  413. av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
  414. count, get_bits_long(&gb, FFMIN(count, 32)));
  415. } else if (count < 0) {
  416. av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
  417. }
  418. } else { // VC1/WVC1/WVP2
  419. const uint8_t *start = avctx->extradata;
  420. uint8_t *end = avctx->extradata + avctx->extradata_size;
  421. const uint8_t *next;
  422. int size, buf2_size;
  423. uint8_t *buf2 = NULL;
  424. int seq_initialized = 0, ep_initialized = 0;
  425. if (avctx->extradata_size < 16) {
  426. av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
  427. return -1;
  428. }
  429. buf2 = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  430. if (!buf2)
  431. return AVERROR(ENOMEM);
  432. start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
  433. next = start;
  434. for (; next < end; start = next) {
  435. next = find_next_marker(start + 4, end);
  436. size = next - start - 4;
  437. if (size <= 0)
  438. continue;
  439. buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
  440. init_get_bits(&gb, buf2, buf2_size * 8);
  441. switch (AV_RB32(start)) {
  442. case VC1_CODE_SEQHDR:
  443. if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
  444. av_free(buf2);
  445. return ret;
  446. }
  447. seq_initialized = 1;
  448. break;
  449. case VC1_CODE_ENTRYPOINT:
  450. if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
  451. av_free(buf2);
  452. return ret;
  453. }
  454. ep_initialized = 1;
  455. break;
  456. }
  457. }
  458. av_free(buf2);
  459. if (!seq_initialized || !ep_initialized) {
  460. av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
  461. return -1;
  462. }
  463. v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
  464. }
  465. v->sprite_output_frame = av_frame_alloc();
  466. if (!v->sprite_output_frame)
  467. return AVERROR(ENOMEM);
  468. avctx->profile = v->profile;
  469. if (v->profile == PROFILE_ADVANCED)
  470. avctx->level = v->level;
  471. avctx->has_b_frames = !!avctx->max_b_frames;
  472. if (v->color_prim == 1 || v->color_prim == 5 || v->color_prim == 6)
  473. avctx->color_primaries = v->color_prim;
  474. if (v->transfer_char == 1 || v->transfer_char == 7)
  475. avctx->color_trc = v->transfer_char;
  476. if (v->matrix_coef == 1 || v->matrix_coef == 6 || v->matrix_coef == 7)
  477. avctx->colorspace = v->matrix_coef;
  478. s->mb_width = (avctx->coded_width + 15) >> 4;
  479. s->mb_height = (avctx->coded_height + 15) >> 4;
  480. if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
  481. ff_vc1_init_transposed_scantables(v);
  482. } else {
  483. memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
  484. v->left_blk_sh = 3;
  485. v->top_blk_sh = 0;
  486. }
  487. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  488. v->sprite_width = avctx->coded_width;
  489. v->sprite_height = avctx->coded_height;
  490. avctx->coded_width = avctx->width = v->output_width;
  491. avctx->coded_height = avctx->height = v->output_height;
  492. // prevent 16.16 overflows
  493. if (v->sprite_width > 1 << 14 ||
  494. v->sprite_height > 1 << 14 ||
  495. v->output_width > 1 << 14 ||
  496. v->output_height > 1 << 14) return -1;
  497. if ((v->sprite_width&1) || (v->sprite_height&1)) {
  498. avpriv_request_sample(avctx, "odd sprites support");
  499. return AVERROR_PATCHWELCOME;
  500. }
  501. }
  502. return 0;
  503. }
  504. /** Close a VC1/WMV3 decoder
  505. * @warning Initial try at using MpegEncContext stuff
  506. */
  507. av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
  508. {
  509. VC1Context *v = avctx->priv_data;
  510. int i;
  511. av_frame_free(&v->sprite_output_frame);
  512. for (i = 0; i < 4; i++)
  513. av_freep(&v->sr_rows[i >> 1][i & 1]);
  514. av_freep(&v->hrd_rate);
  515. av_freep(&v->hrd_buffer);
  516. ff_mpv_common_end(&v->s);
  517. av_freep(&v->mv_type_mb_plane);
  518. av_freep(&v->direct_mb_plane);
  519. av_freep(&v->forward_mb_plane);
  520. av_freep(&v->fieldtx_plane);
  521. av_freep(&v->acpred_plane);
  522. av_freep(&v->over_flags_plane);
  523. av_freep(&v->mb_type_base);
  524. av_freep(&v->blk_mv_type_base);
  525. av_freep(&v->mv_f_base);
  526. av_freep(&v->mv_f_next_base);
  527. av_freep(&v->block);
  528. av_freep(&v->cbp_base);
  529. av_freep(&v->ttblk_base);
  530. av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
  531. av_freep(&v->luma_mv_base);
  532. ff_intrax8_common_end(&v->x8);
  533. return 0;
  534. }
  535. /** Decode a VC1/WMV3 frame
  536. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  537. */
  538. static int vc1_decode_frame(AVCodecContext *avctx, void *data,
  539. int *got_frame, AVPacket *avpkt)
  540. {
  541. const uint8_t *buf = avpkt->data;
  542. int buf_size = avpkt->size, n_slices = 0, i, ret;
  543. VC1Context *v = avctx->priv_data;
  544. MpegEncContext *s = &v->s;
  545. AVFrame *pict = data;
  546. uint8_t *buf2 = NULL;
  547. const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
  548. int mb_height, n_slices1=-1;
  549. struct {
  550. uint8_t *buf;
  551. GetBitContext gb;
  552. int mby_start;
  553. } *slices = NULL, *tmp;
  554. v->second_field = 0;
  555. if(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  556. s->low_delay = 1;
  557. /* no supplementary picture */
  558. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
  559. /* special case for last picture */
  560. if (s->low_delay == 0 && s->next_picture_ptr) {
  561. if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
  562. return ret;
  563. s->next_picture_ptr = NULL;
  564. *got_frame = 1;
  565. }
  566. return buf_size;
  567. }
  568. #if FF_API_CAP_VDPAU
  569. if (s->avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU) {
  570. if (v->profile < PROFILE_ADVANCED)
  571. avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
  572. else
  573. avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
  574. }
  575. #endif
  576. //for advanced profile we may need to parse and unescape data
  577. if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  578. int buf_size2 = 0;
  579. buf2 = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  580. if (!buf2)
  581. return AVERROR(ENOMEM);
  582. if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
  583. const uint8_t *start, *end, *next;
  584. int size;
  585. next = buf;
  586. for (start = buf, end = buf + buf_size; next < end; start = next) {
  587. next = find_next_marker(start + 4, end);
  588. size = next - start - 4;
  589. if (size <= 0) continue;
  590. switch (AV_RB32(start)) {
  591. case VC1_CODE_FRAME:
  592. if (avctx->hwaccel
  593. #if FF_API_CAP_VDPAU
  594. || s->avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU
  595. #endif
  596. )
  597. buf_start = start;
  598. buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
  599. break;
  600. case VC1_CODE_FIELD: {
  601. int buf_size3;
  602. if (avctx->hwaccel
  603. #if FF_API_CAP_VDPAU
  604. || s->avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU
  605. #endif
  606. )
  607. buf_start_second_field = start;
  608. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  609. if (!tmp) {
  610. ret = AVERROR(ENOMEM);
  611. goto err;
  612. }
  613. slices = tmp;
  614. slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  615. if (!slices[n_slices].buf) {
  616. ret = AVERROR(ENOMEM);
  617. goto err;
  618. }
  619. buf_size3 = vc1_unescape_buffer(start + 4, size,
  620. slices[n_slices].buf);
  621. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  622. buf_size3 << 3);
  623. /* assuming that the field marker is at the exact middle,
  624. hope it's correct */
  625. slices[n_slices].mby_start = s->mb_height + 1 >> 1;
  626. n_slices1 = n_slices - 1; // index of the last slice of the first field
  627. n_slices++;
  628. break;
  629. }
  630. case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
  631. buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
  632. init_get_bits(&s->gb, buf2, buf_size2 * 8);
  633. ff_vc1_decode_entry_point(avctx, v, &s->gb);
  634. break;
  635. case VC1_CODE_SLICE: {
  636. int buf_size3;
  637. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  638. if (!tmp) {
  639. ret = AVERROR(ENOMEM);
  640. goto err;
  641. }
  642. slices = tmp;
  643. slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  644. if (!slices[n_slices].buf) {
  645. ret = AVERROR(ENOMEM);
  646. goto err;
  647. }
  648. buf_size3 = vc1_unescape_buffer(start + 4, size,
  649. slices[n_slices].buf);
  650. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  651. buf_size3 << 3);
  652. slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
  653. n_slices++;
  654. break;
  655. }
  656. }
  657. }
  658. } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
  659. const uint8_t *divider;
  660. int buf_size3;
  661. divider = find_next_marker(buf, buf + buf_size);
  662. if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
  663. av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
  664. ret = AVERROR_INVALIDDATA;
  665. goto err;
  666. } else { // found field marker, unescape second field
  667. if (avctx->hwaccel
  668. #if FF_API_CAP_VDPAU
  669. || s->avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU
  670. #endif
  671. )
  672. buf_start_second_field = divider;
  673. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  674. if (!tmp) {
  675. ret = AVERROR(ENOMEM);
  676. goto err;
  677. }
  678. slices = tmp;
  679. slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  680. if (!slices[n_slices].buf) {
  681. ret = AVERROR(ENOMEM);
  682. goto err;
  683. }
  684. buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
  685. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  686. buf_size3 << 3);
  687. slices[n_slices].mby_start = s->mb_height + 1 >> 1;
  688. n_slices1 = n_slices - 1;
  689. n_slices++;
  690. }
  691. buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
  692. } else {
  693. buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
  694. }
  695. init_get_bits(&s->gb, buf2, buf_size2*8);
  696. } else
  697. init_get_bits(&s->gb, buf, buf_size*8);
  698. if (v->res_sprite) {
  699. v->new_sprite = !get_bits1(&s->gb);
  700. v->two_sprites = get_bits1(&s->gb);
  701. /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
  702. we're using the sprite compositor. These are intentionally kept separate
  703. so you can get the raw sprites by using the wmv3 decoder for WMVP or
  704. the vc1 one for WVP2 */
  705. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  706. if (v->new_sprite) {
  707. // switch AVCodecContext parameters to those of the sprites
  708. avctx->width = avctx->coded_width = v->sprite_width;
  709. avctx->height = avctx->coded_height = v->sprite_height;
  710. } else {
  711. goto image;
  712. }
  713. }
  714. }
  715. if (s->context_initialized &&
  716. (s->width != avctx->coded_width ||
  717. s->height != avctx->coded_height)) {
  718. ff_vc1_decode_end(avctx);
  719. }
  720. if (!s->context_initialized) {
  721. if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
  722. goto err;
  723. if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0) {
  724. ff_mpv_common_end(s);
  725. goto err;
  726. }
  727. s->low_delay = !avctx->has_b_frames || v->res_sprite;
  728. if (v->profile == PROFILE_ADVANCED) {
  729. if(avctx->coded_width<=1 || avctx->coded_height<=1) {
  730. ret = AVERROR_INVALIDDATA;
  731. goto err;
  732. }
  733. s->h_edge_pos = avctx->coded_width;
  734. s->v_edge_pos = avctx->coded_height;
  735. }
  736. }
  737. // do parse frame header
  738. v->pic_header_flag = 0;
  739. v->first_pic_header_flag = 1;
  740. if (v->profile < PROFILE_ADVANCED) {
  741. if ((ret = ff_vc1_parse_frame_header(v, &s->gb)) < 0) {
  742. goto err;
  743. }
  744. } else {
  745. if ((ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
  746. goto err;
  747. }
  748. }
  749. v->first_pic_header_flag = 0;
  750. if (avctx->debug & FF_DEBUG_PICT_INFO)
  751. av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
  752. if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
  753. && s->pict_type != AV_PICTURE_TYPE_I) {
  754. av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
  755. ret = AVERROR_INVALIDDATA;
  756. goto err;
  757. }
  758. if ((s->mb_height >> v->field_mode) == 0) {
  759. av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
  760. ret = AVERROR_INVALIDDATA;
  761. goto err;
  762. }
  763. // for skipping the frame
  764. s->current_picture.f->pict_type = s->pict_type;
  765. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  766. /* skip B-frames if we don't have reference frames */
  767. if (!s->last_picture_ptr && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
  768. av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n");
  769. goto end;
  770. }
  771. if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
  772. (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
  773. avctx->skip_frame >= AVDISCARD_ALL) {
  774. goto end;
  775. }
  776. if (s->next_p_frame_damaged) {
  777. if (s->pict_type == AV_PICTURE_TYPE_B)
  778. goto end;
  779. else
  780. s->next_p_frame_damaged = 0;
  781. }
  782. if ((ret = ff_mpv_frame_start(s, avctx)) < 0) {
  783. goto err;
  784. }
  785. v->s.current_picture_ptr->field_picture = v->field_mode;
  786. v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
  787. v->s.current_picture_ptr->f->top_field_first = v->tff;
  788. // process pulldown flags
  789. s->current_picture_ptr->f->repeat_pict = 0;
  790. // Pulldown flags are only valid when 'broadcast' has been set.
  791. // So ticks_per_frame will be 2
  792. if (v->rff) {
  793. // repeat field
  794. s->current_picture_ptr->f->repeat_pict = 1;
  795. } else if (v->rptfrm) {
  796. // repeat frames
  797. s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
  798. }
  799. s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
  800. s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
  801. #if FF_API_CAP_VDPAU
  802. if ((CONFIG_VC1_VDPAU_DECODER)
  803. &&s->avctx->codec->capabilities&AV_CODEC_CAP_HWACCEL_VDPAU) {
  804. if (v->field_mode && buf_start_second_field) {
  805. ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
  806. ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
  807. } else {
  808. ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
  809. }
  810. } else
  811. #endif
  812. if (avctx->hwaccel) {
  813. if (v->field_mode && buf_start_second_field) {
  814. // decode first field
  815. s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
  816. if ((ret = avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start)) < 0)
  817. goto err;
  818. if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start)) < 0)
  819. goto err;
  820. if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
  821. goto err;
  822. // decode second field
  823. s->gb = slices[n_slices1 + 1].gb;
  824. s->picture_structure = PICT_TOP_FIELD + v->tff;
  825. v->second_field = 1;
  826. v->pic_header_flag = 0;
  827. if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
  828. av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
  829. ret = AVERROR_INVALIDDATA;
  830. goto err;
  831. }
  832. v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
  833. if ((ret = avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field)) < 0)
  834. goto err;
  835. if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field)) < 0)
  836. goto err;
  837. if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
  838. goto err;
  839. } else {
  840. s->picture_structure = PICT_FRAME;
  841. if ((ret = avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start)) < 0)
  842. goto err;
  843. if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start)) < 0)
  844. goto err;
  845. if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
  846. goto err;
  847. }
  848. } else {
  849. int header_ret = 0;
  850. ff_mpeg_er_frame_start(s);
  851. v->bits = buf_size * 8;
  852. v->end_mb_x = s->mb_width;
  853. if (v->field_mode) {
  854. s->current_picture.f->linesize[0] <<= 1;
  855. s->current_picture.f->linesize[1] <<= 1;
  856. s->current_picture.f->linesize[2] <<= 1;
  857. s->linesize <<= 1;
  858. s->uvlinesize <<= 1;
  859. }
  860. mb_height = s->mb_height >> v->field_mode;
  861. av_assert0 (mb_height > 0);
  862. for (i = 0; i <= n_slices; i++) {
  863. if (i > 0 && slices[i - 1].mby_start >= mb_height) {
  864. if (v->field_mode <= 0) {
  865. av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
  866. "picture boundary (%d >= %d)\n", i,
  867. slices[i - 1].mby_start, mb_height);
  868. continue;
  869. }
  870. v->second_field = 1;
  871. av_assert0((s->mb_height & 1) == 0);
  872. v->blocks_off = s->b8_stride * (s->mb_height&~1);
  873. v->mb_off = s->mb_stride * s->mb_height >> 1;
  874. } else {
  875. v->second_field = 0;
  876. v->blocks_off = 0;
  877. v->mb_off = 0;
  878. }
  879. if (i) {
  880. v->pic_header_flag = 0;
  881. if (v->field_mode && i == n_slices1 + 2) {
  882. if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
  883. av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
  884. ret = AVERROR_INVALIDDATA;
  885. if (avctx->err_recognition & AV_EF_EXPLODE)
  886. goto err;
  887. continue;
  888. }
  889. } else if (get_bits1(&s->gb)) {
  890. v->pic_header_flag = 1;
  891. if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
  892. av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
  893. ret = AVERROR_INVALIDDATA;
  894. if (avctx->err_recognition & AV_EF_EXPLODE)
  895. goto err;
  896. continue;
  897. }
  898. }
  899. }
  900. if (header_ret < 0)
  901. continue;
  902. s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
  903. if (!v->field_mode || v->second_field)
  904. s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
  905. else {
  906. if (i >= n_slices) {
  907. av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
  908. continue;
  909. }
  910. s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
  911. }
  912. if (s->end_mb_y <= s->start_mb_y) {
  913. av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
  914. continue;
  915. }
  916. if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
  917. av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
  918. continue;
  919. }
  920. ff_vc1_decode_blocks(v);
  921. if (i != n_slices)
  922. s->gb = slices[i].gb;
  923. }
  924. if (v->field_mode) {
  925. v->second_field = 0;
  926. s->current_picture.f->linesize[0] >>= 1;
  927. s->current_picture.f->linesize[1] >>= 1;
  928. s->current_picture.f->linesize[2] >>= 1;
  929. s->linesize >>= 1;
  930. s->uvlinesize >>= 1;
  931. if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
  932. FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
  933. FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
  934. }
  935. }
  936. ff_dlog(s->avctx, "Consumed %i/%i bits\n",
  937. get_bits_count(&s->gb), s->gb.size_in_bits);
  938. // if (get_bits_count(&s->gb) > buf_size * 8)
  939. // return -1;
  940. if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B) {
  941. ret = AVERROR_INVALIDDATA;
  942. goto err;
  943. }
  944. if (!v->field_mode)
  945. ff_er_frame_end(&s->er);
  946. }
  947. ff_mpv_frame_end(s);
  948. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  949. image:
  950. avctx->width = avctx->coded_width = v->output_width;
  951. avctx->height = avctx->coded_height = v->output_height;
  952. if (avctx->skip_frame >= AVDISCARD_NONREF)
  953. goto end;
  954. #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
  955. if ((ret = vc1_decode_sprites(v, &s->gb)) < 0)
  956. goto err;
  957. #endif
  958. if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
  959. goto err;
  960. *got_frame = 1;
  961. } else {
  962. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  963. if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
  964. goto err;
  965. ff_print_debug_info(s, s->current_picture_ptr, pict);
  966. *got_frame = 1;
  967. } else if (s->last_picture_ptr) {
  968. if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
  969. goto err;
  970. ff_print_debug_info(s, s->last_picture_ptr, pict);
  971. *got_frame = 1;
  972. }
  973. }
  974. end:
  975. av_free(buf2);
  976. for (i = 0; i < n_slices; i++)
  977. av_free(slices[i].buf);
  978. av_free(slices);
  979. return buf_size;
  980. err:
  981. av_free(buf2);
  982. for (i = 0; i < n_slices; i++)
  983. av_free(slices[i].buf);
  984. av_free(slices);
  985. return ret;
  986. }
  987. static const AVProfile profiles[] = {
  988. { FF_PROFILE_VC1_SIMPLE, "Simple" },
  989. { FF_PROFILE_VC1_MAIN, "Main" },
  990. { FF_PROFILE_VC1_COMPLEX, "Complex" },
  991. { FF_PROFILE_VC1_ADVANCED, "Advanced" },
  992. { FF_PROFILE_UNKNOWN },
  993. };
  994. static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
  995. #if CONFIG_VC1_DXVA2_HWACCEL
  996. AV_PIX_FMT_DXVA2_VLD,
  997. #endif
  998. #if CONFIG_VC1_D3D11VA_HWACCEL
  999. AV_PIX_FMT_D3D11VA_VLD,
  1000. #endif
  1001. #if CONFIG_VC1_VAAPI_HWACCEL
  1002. AV_PIX_FMT_VAAPI_VLD,
  1003. #endif
  1004. #if CONFIG_VC1_VDPAU_HWACCEL
  1005. AV_PIX_FMT_VDPAU,
  1006. #endif
  1007. AV_PIX_FMT_YUV420P,
  1008. AV_PIX_FMT_NONE
  1009. };
  1010. AVCodec ff_vc1_decoder = {
  1011. .name = "vc1",
  1012. .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
  1013. .type = AVMEDIA_TYPE_VIDEO,
  1014. .id = AV_CODEC_ID_VC1,
  1015. .priv_data_size = sizeof(VC1Context),
  1016. .init = vc1_decode_init,
  1017. .close = ff_vc1_decode_end,
  1018. .decode = vc1_decode_frame,
  1019. .flush = ff_mpeg_flush,
  1020. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  1021. .pix_fmts = vc1_hwaccel_pixfmt_list_420,
  1022. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1023. };
  1024. #if CONFIG_WMV3_DECODER
  1025. AVCodec ff_wmv3_decoder = {
  1026. .name = "wmv3",
  1027. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
  1028. .type = AVMEDIA_TYPE_VIDEO,
  1029. .id = AV_CODEC_ID_WMV3,
  1030. .priv_data_size = sizeof(VC1Context),
  1031. .init = vc1_decode_init,
  1032. .close = ff_vc1_decode_end,
  1033. .decode = vc1_decode_frame,
  1034. .flush = ff_mpeg_flush,
  1035. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  1036. .pix_fmts = vc1_hwaccel_pixfmt_list_420,
  1037. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1038. };
  1039. #endif
  1040. #if CONFIG_WMV3_VDPAU_DECODER && FF_API_VDPAU
  1041. AVCodec ff_wmv3_vdpau_decoder = {
  1042. .name = "wmv3_vdpau",
  1043. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
  1044. .type = AVMEDIA_TYPE_VIDEO,
  1045. .id = AV_CODEC_ID_WMV3,
  1046. .priv_data_size = sizeof(VC1Context),
  1047. .init = vc1_decode_init,
  1048. .close = ff_vc1_decode_end,
  1049. .decode = vc1_decode_frame,
  1050. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HWACCEL_VDPAU,
  1051. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
  1052. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1053. };
  1054. #endif
  1055. #if CONFIG_VC1_VDPAU_DECODER && FF_API_VDPAU
  1056. AVCodec ff_vc1_vdpau_decoder = {
  1057. .name = "vc1_vdpau",
  1058. .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
  1059. .type = AVMEDIA_TYPE_VIDEO,
  1060. .id = AV_CODEC_ID_VC1,
  1061. .priv_data_size = sizeof(VC1Context),
  1062. .init = vc1_decode_init,
  1063. .close = ff_vc1_decode_end,
  1064. .decode = vc1_decode_frame,
  1065. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | AV_CODEC_CAP_HWACCEL_VDPAU,
  1066. .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
  1067. .profiles = NULL_IF_CONFIG_SMALL(profiles)
  1068. };
  1069. #endif
  1070. #if CONFIG_WMV3IMAGE_DECODER
  1071. AVCodec ff_wmv3image_decoder = {
  1072. .name = "wmv3image",
  1073. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
  1074. .type = AVMEDIA_TYPE_VIDEO,
  1075. .id = AV_CODEC_ID_WMV3IMAGE,
  1076. .priv_data_size = sizeof(VC1Context),
  1077. .init = vc1_decode_init,
  1078. .close = ff_vc1_decode_end,
  1079. .decode = vc1_decode_frame,
  1080. .capabilities = AV_CODEC_CAP_DR1,
  1081. .flush = vc1_sprite_flush,
  1082. .pix_fmts = (const enum AVPixelFormat[]) {
  1083. AV_PIX_FMT_YUV420P,
  1084. AV_PIX_FMT_NONE
  1085. },
  1086. };
  1087. #endif
  1088. #if CONFIG_VC1IMAGE_DECODER
  1089. AVCodec ff_vc1image_decoder = {
  1090. .name = "vc1image",
  1091. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
  1092. .type = AVMEDIA_TYPE_VIDEO,
  1093. .id = AV_CODEC_ID_VC1IMAGE,
  1094. .priv_data_size = sizeof(VC1Context),
  1095. .init = vc1_decode_init,
  1096. .close = ff_vc1_decode_end,
  1097. .decode = vc1_decode_frame,
  1098. .capabilities = AV_CODEC_CAP_DR1,
  1099. .flush = vc1_sprite_flush,
  1100. .pix_fmts = (const enum AVPixelFormat[]) {
  1101. AV_PIX_FMT_YUV420P,
  1102. AV_PIX_FMT_NONE
  1103. },
  1104. };
  1105. #endif