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.

1185 lines
44KB

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