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.

1204 lines
45KB

  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 "profiles.h"
  36. #include "vc1.h"
  37. #include "vc1data.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, ret = AVERROR(ENOMEM);
  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. if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->forward_mb_plane ||
  298. !v->fieldtx_plane || !v->acpred_plane || !v->over_flags_plane)
  299. goto error;
  300. v->n_allocated_blks = s->mb_width + 2;
  301. v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
  302. v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
  303. if (!v->block || !v->cbp_base)
  304. goto error;
  305. v->cbp = v->cbp_base + s->mb_stride;
  306. v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
  307. if (!v->ttblk_base)
  308. goto error;
  309. v->ttblk = v->ttblk_base + s->mb_stride;
  310. v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
  311. if (!v->is_intra_base)
  312. goto error;
  313. v->is_intra = v->is_intra_base + s->mb_stride;
  314. v->luma_mv_base = av_mallocz(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
  315. if (!v->luma_mv_base)
  316. goto error;
  317. v->luma_mv = v->luma_mv_base + s->mb_stride;
  318. /* allocate block type info in that way so it could be used with s->block_index[] */
  319. v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  320. if (!v->mb_type_base)
  321. goto error;
  322. v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
  323. v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
  324. v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
  325. /* allocate memory to store block level MV info */
  326. v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  327. if (!v->blk_mv_type_base)
  328. goto error;
  329. v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
  330. v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
  331. if (!v->mv_f_base)
  332. goto error;
  333. v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
  334. v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  335. v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
  336. if (!v->mv_f_next_base)
  337. goto error;
  338. v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
  339. v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
  340. if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  341. for (i = 0; i < 4; i++)
  342. if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
  343. return AVERROR(ENOMEM);
  344. }
  345. ret = ff_intrax8_common_init(s->avctx, &v->x8, &s->idsp,
  346. s->block, s->block_last_index,
  347. s->mb_width, s->mb_height);
  348. if (ret < 0)
  349. goto error;
  350. return 0;
  351. error:
  352. ff_vc1_decode_end(s->avctx);
  353. return ret;
  354. }
  355. av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
  356. {
  357. int i;
  358. for (i = 0; i < 64; i++) {
  359. #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
  360. v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
  361. v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
  362. v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
  363. v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
  364. v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
  365. }
  366. v->left_blk_sh = 0;
  367. v->top_blk_sh = 3;
  368. }
  369. /** Initialize a VC1/WMV3 decoder
  370. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  371. * @todo TODO: Decipher remaining bits in extra_data
  372. */
  373. static av_cold int vc1_decode_init(AVCodecContext *avctx)
  374. {
  375. VC1Context *v = avctx->priv_data;
  376. MpegEncContext *s = &v->s;
  377. GetBitContext gb;
  378. int ret;
  379. /* save the container output size for WMImage */
  380. v->output_width = avctx->width;
  381. v->output_height = avctx->height;
  382. if (!avctx->extradata_size || !avctx->extradata)
  383. return -1;
  384. v->s.avctx = avctx;
  385. if ((ret = ff_vc1_init_common(v)) < 0)
  386. return ret;
  387. if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
  388. int count = 0;
  389. // looks like WMV3 has a sequence header stored in the extradata
  390. // advanced sequence header may be before the first frame
  391. // the last byte of the extradata is a version number, 1 for the
  392. // samples we can decode
  393. init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
  394. if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
  395. return ret;
  396. count = avctx->extradata_size*8 - get_bits_count(&gb);
  397. if (count > 0) {
  398. av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
  399. count, get_bits_long(&gb, FFMIN(count, 32)));
  400. } else if (count < 0) {
  401. av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
  402. }
  403. } else { // VC1/WVC1/WVP2
  404. const uint8_t *start = avctx->extradata;
  405. uint8_t *end = avctx->extradata + avctx->extradata_size;
  406. const uint8_t *next;
  407. int size, buf2_size;
  408. uint8_t *buf2 = NULL;
  409. int seq_initialized = 0, ep_initialized = 0;
  410. if (avctx->extradata_size < 16) {
  411. av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
  412. return -1;
  413. }
  414. buf2 = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  415. if (!buf2)
  416. return AVERROR(ENOMEM);
  417. start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
  418. next = start;
  419. for (; next < end; start = next) {
  420. next = find_next_marker(start + 4, end);
  421. size = next - start - 4;
  422. if (size <= 0)
  423. continue;
  424. buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
  425. init_get_bits(&gb, buf2, buf2_size * 8);
  426. switch (AV_RB32(start)) {
  427. case VC1_CODE_SEQHDR:
  428. if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
  429. av_free(buf2);
  430. return ret;
  431. }
  432. seq_initialized = 1;
  433. break;
  434. case VC1_CODE_ENTRYPOINT:
  435. if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
  436. av_free(buf2);
  437. return ret;
  438. }
  439. ep_initialized = 1;
  440. break;
  441. }
  442. }
  443. av_free(buf2);
  444. if (!seq_initialized || !ep_initialized) {
  445. av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
  446. return -1;
  447. }
  448. v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
  449. }
  450. avctx->profile = v->profile;
  451. if (v->profile == PROFILE_ADVANCED)
  452. avctx->level = v->level;
  453. if (!CONFIG_GRAY || !(avctx->flags & AV_CODEC_FLAG_GRAY))
  454. avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
  455. else {
  456. avctx->pix_fmt = AV_PIX_FMT_GRAY8;
  457. if (avctx->color_range == AVCOL_RANGE_UNSPECIFIED)
  458. avctx->color_range = AVCOL_RANGE_MPEG;
  459. }
  460. // ensure static VLC tables are initialized
  461. if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
  462. return ret;
  463. if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
  464. return ret;
  465. // Hack to ensure the above functions will be called
  466. // again once we know all necessary settings.
  467. // That this is necessary might indicate a bug.
  468. ff_vc1_decode_end(avctx);
  469. ff_blockdsp_init(&s->bdsp, avctx);
  470. ff_h264chroma_init(&v->h264chroma, 8);
  471. ff_qpeldsp_init(&s->qdsp);
  472. // Must happen after calling ff_vc1_decode_end
  473. // to avoid de-allocating the sprite_output_frame
  474. v->sprite_output_frame = av_frame_alloc();
  475. if (!v->sprite_output_frame)
  476. return AVERROR(ENOMEM);
  477. avctx->has_b_frames = !!avctx->max_b_frames;
  478. if (v->color_prim == 1 || v->color_prim == 5 || v->color_prim == 6)
  479. avctx->color_primaries = v->color_prim;
  480. if (v->transfer_char == 1 || v->transfer_char == 7)
  481. avctx->color_trc = v->transfer_char;
  482. if (v->matrix_coef == 1 || v->matrix_coef == 6 || v->matrix_coef == 7)
  483. avctx->colorspace = v->matrix_coef;
  484. s->mb_width = (avctx->coded_width + 15) >> 4;
  485. s->mb_height = (avctx->coded_height + 15) >> 4;
  486. if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
  487. ff_vc1_init_transposed_scantables(v);
  488. } else {
  489. memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
  490. v->left_blk_sh = 3;
  491. v->top_blk_sh = 0;
  492. }
  493. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  494. v->sprite_width = avctx->coded_width;
  495. v->sprite_height = avctx->coded_height;
  496. avctx->coded_width = avctx->width = v->output_width;
  497. avctx->coded_height = avctx->height = v->output_height;
  498. // prevent 16.16 overflows
  499. if (v->sprite_width > 1 << 14 ||
  500. v->sprite_height > 1 << 14 ||
  501. v->output_width > 1 << 14 ||
  502. v->output_height > 1 << 14) return -1;
  503. if ((v->sprite_width&1) || (v->sprite_height&1)) {
  504. avpriv_request_sample(avctx, "odd sprites support");
  505. return AVERROR_PATCHWELCOME;
  506. }
  507. }
  508. return 0;
  509. }
  510. /** Close a VC1/WMV3 decoder
  511. * @warning Initial try at using MpegEncContext stuff
  512. */
  513. av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
  514. {
  515. VC1Context *v = avctx->priv_data;
  516. int i;
  517. av_frame_free(&v->sprite_output_frame);
  518. for (i = 0; i < 4; i++)
  519. av_freep(&v->sr_rows[i >> 1][i & 1]);
  520. av_freep(&v->hrd_rate);
  521. av_freep(&v->hrd_buffer);
  522. ff_mpv_common_end(&v->s);
  523. av_freep(&v->mv_type_mb_plane);
  524. av_freep(&v->direct_mb_plane);
  525. av_freep(&v->forward_mb_plane);
  526. av_freep(&v->fieldtx_plane);
  527. av_freep(&v->acpred_plane);
  528. av_freep(&v->over_flags_plane);
  529. av_freep(&v->mb_type_base);
  530. av_freep(&v->blk_mv_type_base);
  531. av_freep(&v->mv_f_base);
  532. av_freep(&v->mv_f_next_base);
  533. av_freep(&v->block);
  534. av_freep(&v->cbp_base);
  535. av_freep(&v->ttblk_base);
  536. av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
  537. av_freep(&v->luma_mv_base);
  538. ff_intrax8_common_end(&v->x8);
  539. return 0;
  540. }
  541. /** Decode a VC1/WMV3 frame
  542. * @todo TODO: Handle VC-1 IDUs (Transport level?)
  543. */
  544. static int vc1_decode_frame(AVCodecContext *avctx, void *data,
  545. int *got_frame, AVPacket *avpkt)
  546. {
  547. const uint8_t *buf = avpkt->data;
  548. int buf_size = avpkt->size, n_slices = 0, i, ret;
  549. VC1Context *v = avctx->priv_data;
  550. MpegEncContext *s = &v->s;
  551. AVFrame *pict = data;
  552. uint8_t *buf2 = NULL;
  553. const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
  554. int mb_height, n_slices1=-1;
  555. struct {
  556. uint8_t *buf;
  557. GetBitContext gb;
  558. int mby_start;
  559. const uint8_t *rawbuf;
  560. int raw_size;
  561. } *slices = NULL, *tmp;
  562. v->second_field = 0;
  563. if(s->avctx->flags & AV_CODEC_FLAG_LOW_DELAY)
  564. s->low_delay = 1;
  565. /* no supplementary picture */
  566. if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
  567. /* special case for last picture */
  568. if (s->low_delay == 0 && s->next_picture_ptr) {
  569. if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
  570. return ret;
  571. s->next_picture_ptr = NULL;
  572. *got_frame = 1;
  573. }
  574. return buf_size;
  575. }
  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. buf_start = start;
  594. buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
  595. break;
  596. case VC1_CODE_FIELD: {
  597. int buf_size3;
  598. if (avctx->hwaccel)
  599. buf_start_second_field = start;
  600. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  601. if (!tmp) {
  602. ret = AVERROR(ENOMEM);
  603. goto err;
  604. }
  605. slices = tmp;
  606. slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  607. if (!slices[n_slices].buf) {
  608. ret = AVERROR(ENOMEM);
  609. goto err;
  610. }
  611. buf_size3 = vc1_unescape_buffer(start + 4, size,
  612. slices[n_slices].buf);
  613. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  614. buf_size3 << 3);
  615. /* assuming that the field marker is at the exact middle,
  616. hope it's correct */
  617. slices[n_slices].mby_start = s->mb_height + 1 >> 1;
  618. slices[n_slices].rawbuf = start;
  619. slices[n_slices].raw_size = size + 4;
  620. n_slices1 = n_slices - 1; // index of the last slice of the first field
  621. n_slices++;
  622. break;
  623. }
  624. case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
  625. buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
  626. init_get_bits(&s->gb, buf2, buf_size2 * 8);
  627. ff_vc1_decode_entry_point(avctx, v, &s->gb);
  628. break;
  629. case VC1_CODE_SLICE: {
  630. int buf_size3;
  631. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  632. if (!tmp) {
  633. ret = AVERROR(ENOMEM);
  634. goto err;
  635. }
  636. slices = tmp;
  637. slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  638. if (!slices[n_slices].buf) {
  639. ret = AVERROR(ENOMEM);
  640. goto err;
  641. }
  642. buf_size3 = vc1_unescape_buffer(start + 4, size,
  643. slices[n_slices].buf);
  644. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  645. buf_size3 << 3);
  646. slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
  647. slices[n_slices].rawbuf = start;
  648. slices[n_slices].raw_size = size + 4;
  649. n_slices++;
  650. break;
  651. }
  652. }
  653. }
  654. } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
  655. const uint8_t *divider;
  656. int buf_size3;
  657. divider = find_next_marker(buf, buf + buf_size);
  658. if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
  659. av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
  660. ret = AVERROR_INVALIDDATA;
  661. goto err;
  662. } else { // found field marker, unescape second field
  663. if (avctx->hwaccel)
  664. buf_start_second_field = divider;
  665. tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
  666. if (!tmp) {
  667. ret = AVERROR(ENOMEM);
  668. goto err;
  669. }
  670. slices = tmp;
  671. slices[n_slices].buf = av_mallocz(buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
  672. if (!slices[n_slices].buf) {
  673. ret = AVERROR(ENOMEM);
  674. goto err;
  675. }
  676. buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
  677. init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
  678. buf_size3 << 3);
  679. slices[n_slices].mby_start = s->mb_height + 1 >> 1;
  680. slices[n_slices].rawbuf = divider;
  681. slices[n_slices].raw_size = buf + buf_size - divider;
  682. n_slices1 = n_slices - 1;
  683. n_slices++;
  684. }
  685. buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
  686. } else {
  687. buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
  688. }
  689. init_get_bits(&s->gb, buf2, buf_size2*8);
  690. } else
  691. init_get_bits(&s->gb, buf, buf_size*8);
  692. if (v->res_sprite) {
  693. v->new_sprite = !get_bits1(&s->gb);
  694. v->two_sprites = get_bits1(&s->gb);
  695. /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
  696. we're using the sprite compositor. These are intentionally kept separate
  697. so you can get the raw sprites by using the wmv3 decoder for WMVP or
  698. the vc1 one for WVP2 */
  699. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  700. if (v->new_sprite) {
  701. // switch AVCodecContext parameters to those of the sprites
  702. avctx->width = avctx->coded_width = v->sprite_width;
  703. avctx->height = avctx->coded_height = v->sprite_height;
  704. } else {
  705. goto image;
  706. }
  707. }
  708. }
  709. if (s->context_initialized &&
  710. (s->width != avctx->coded_width ||
  711. s->height != avctx->coded_height)) {
  712. ff_vc1_decode_end(avctx);
  713. }
  714. if (!s->context_initialized) {
  715. if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
  716. goto err;
  717. if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0) {
  718. ff_mpv_common_end(s);
  719. goto err;
  720. }
  721. s->low_delay = !avctx->has_b_frames || v->res_sprite;
  722. if (v->profile == PROFILE_ADVANCED) {
  723. if(avctx->coded_width<=1 || avctx->coded_height<=1) {
  724. ret = AVERROR_INVALIDDATA;
  725. goto err;
  726. }
  727. s->h_edge_pos = avctx->coded_width;
  728. s->v_edge_pos = avctx->coded_height;
  729. }
  730. }
  731. // do parse frame header
  732. v->pic_header_flag = 0;
  733. v->first_pic_header_flag = 1;
  734. if (v->profile < PROFILE_ADVANCED) {
  735. if ((ret = ff_vc1_parse_frame_header(v, &s->gb)) < 0) {
  736. goto err;
  737. }
  738. } else {
  739. if ((ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
  740. goto err;
  741. }
  742. }
  743. v->first_pic_header_flag = 0;
  744. if (avctx->debug & FF_DEBUG_PICT_INFO)
  745. av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
  746. if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
  747. && s->pict_type != AV_PICTURE_TYPE_I) {
  748. av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
  749. ret = AVERROR_INVALIDDATA;
  750. goto err;
  751. }
  752. if ((s->mb_height >> v->field_mode) == 0) {
  753. av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
  754. ret = AVERROR_INVALIDDATA;
  755. goto err;
  756. }
  757. // for skipping the frame
  758. s->current_picture.f->pict_type = s->pict_type;
  759. s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
  760. /* skip B-frames if we don't have reference frames */
  761. if (!s->last_picture_ptr && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
  762. av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n");
  763. goto end;
  764. }
  765. if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
  766. (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
  767. avctx->skip_frame >= AVDISCARD_ALL) {
  768. goto end;
  769. }
  770. if (s->next_p_frame_damaged) {
  771. if (s->pict_type == AV_PICTURE_TYPE_B)
  772. goto end;
  773. else
  774. s->next_p_frame_damaged = 0;
  775. }
  776. if ((ret = ff_mpv_frame_start(s, avctx)) < 0) {
  777. goto err;
  778. }
  779. v->s.current_picture_ptr->field_picture = v->field_mode;
  780. v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
  781. v->s.current_picture_ptr->f->top_field_first = v->tff;
  782. // process pulldown flags
  783. s->current_picture_ptr->f->repeat_pict = 0;
  784. // Pulldown flags are only valid when 'broadcast' has been set.
  785. // So ticks_per_frame will be 2
  786. if (v->rff) {
  787. // repeat field
  788. s->current_picture_ptr->f->repeat_pict = 1;
  789. } else if (v->rptfrm) {
  790. // repeat frames
  791. s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
  792. }
  793. s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
  794. s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
  795. if (avctx->hwaccel) {
  796. s->mb_y = 0;
  797. if (v->field_mode && buf_start_second_field) {
  798. // decode first field
  799. s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
  800. if ((ret = avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start)) < 0)
  801. goto err;
  802. if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start)) < 0)
  803. goto err;
  804. if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
  805. goto err;
  806. // decode second field
  807. s->gb = slices[n_slices1 + 1].gb;
  808. s->picture_structure = PICT_TOP_FIELD + v->tff;
  809. v->second_field = 1;
  810. v->pic_header_flag = 0;
  811. if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
  812. av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
  813. ret = AVERROR_INVALIDDATA;
  814. goto err;
  815. }
  816. v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
  817. if ((ret = avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field)) < 0)
  818. goto err;
  819. if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field)) < 0)
  820. goto err;
  821. if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
  822. goto err;
  823. } else {
  824. s->picture_structure = PICT_FRAME;
  825. if ((ret = avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start)) < 0)
  826. goto err;
  827. if (n_slices == 0) {
  828. // no slices, decode the frame as-is
  829. if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start)) < 0)
  830. goto err;
  831. } else {
  832. // decode the frame part as the first slice
  833. if ((ret = avctx->hwaccel->decode_slice(avctx, buf_start, slices[0].rawbuf - buf_start)) < 0)
  834. goto err;
  835. // and process the slices as additional slices afterwards
  836. for (i = 0 ; i < n_slices; i++) {
  837. s->gb = slices[i].gb;
  838. s->mb_y = slices[i].mby_start;
  839. v->pic_header_flag = get_bits1(&s->gb);
  840. if (v->pic_header_flag) {
  841. if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
  842. av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
  843. ret = AVERROR_INVALIDDATA;
  844. if (avctx->err_recognition & AV_EF_EXPLODE)
  845. goto err;
  846. continue;
  847. }
  848. }
  849. if ((ret = avctx->hwaccel->decode_slice(avctx, slices[i].rawbuf, slices[i].raw_size)) < 0)
  850. goto err;
  851. }
  852. }
  853. if ((ret = avctx->hwaccel->end_frame(avctx)) < 0)
  854. goto err;
  855. }
  856. } else {
  857. int header_ret = 0;
  858. ff_mpeg_er_frame_start(s);
  859. v->bits = buf_size * 8;
  860. v->end_mb_x = s->mb_width;
  861. if (v->field_mode) {
  862. s->current_picture.f->linesize[0] <<= 1;
  863. s->current_picture.f->linesize[1] <<= 1;
  864. s->current_picture.f->linesize[2] <<= 1;
  865. s->linesize <<= 1;
  866. s->uvlinesize <<= 1;
  867. }
  868. mb_height = s->mb_height >> v->field_mode;
  869. av_assert0 (mb_height > 0);
  870. for (i = 0; i <= n_slices; i++) {
  871. if (i > 0 && slices[i - 1].mby_start >= mb_height) {
  872. if (v->field_mode <= 0) {
  873. av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
  874. "picture boundary (%d >= %d)\n", i,
  875. slices[i - 1].mby_start, mb_height);
  876. continue;
  877. }
  878. v->second_field = 1;
  879. av_assert0((s->mb_height & 1) == 0);
  880. v->blocks_off = s->b8_stride * (s->mb_height&~1);
  881. v->mb_off = s->mb_stride * s->mb_height >> 1;
  882. } else {
  883. v->second_field = 0;
  884. v->blocks_off = 0;
  885. v->mb_off = 0;
  886. }
  887. if (i) {
  888. v->pic_header_flag = 0;
  889. if (v->field_mode && i == n_slices1 + 2) {
  890. if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
  891. av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
  892. ret = AVERROR_INVALIDDATA;
  893. if (avctx->err_recognition & AV_EF_EXPLODE)
  894. goto err;
  895. continue;
  896. }
  897. } else if (get_bits1(&s->gb)) {
  898. v->pic_header_flag = 1;
  899. if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
  900. av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
  901. ret = AVERROR_INVALIDDATA;
  902. if (avctx->err_recognition & AV_EF_EXPLODE)
  903. goto err;
  904. continue;
  905. }
  906. }
  907. }
  908. if (header_ret < 0)
  909. continue;
  910. s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
  911. if (!v->field_mode || v->second_field)
  912. s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
  913. else {
  914. if (i >= n_slices) {
  915. av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
  916. continue;
  917. }
  918. s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
  919. }
  920. if (s->end_mb_y <= s->start_mb_y) {
  921. av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
  922. continue;
  923. }
  924. if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
  925. av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
  926. continue;
  927. }
  928. ff_vc1_decode_blocks(v);
  929. if (i != n_slices)
  930. s->gb = slices[i].gb;
  931. }
  932. if (v->field_mode) {
  933. v->second_field = 0;
  934. s->current_picture.f->linesize[0] >>= 1;
  935. s->current_picture.f->linesize[1] >>= 1;
  936. s->current_picture.f->linesize[2] >>= 1;
  937. s->linesize >>= 1;
  938. s->uvlinesize >>= 1;
  939. if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
  940. FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
  941. FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
  942. }
  943. }
  944. ff_dlog(s->avctx, "Consumed %i/%i bits\n",
  945. get_bits_count(&s->gb), s->gb.size_in_bits);
  946. // if (get_bits_count(&s->gb) > buf_size * 8)
  947. // return -1;
  948. if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B) {
  949. ret = AVERROR_INVALIDDATA;
  950. goto err;
  951. }
  952. if (!v->field_mode)
  953. ff_er_frame_end(&s->er);
  954. }
  955. ff_mpv_frame_end(s);
  956. if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
  957. image:
  958. avctx->width = avctx->coded_width = v->output_width;
  959. avctx->height = avctx->coded_height = v->output_height;
  960. if (avctx->skip_frame >= AVDISCARD_NONREF)
  961. goto end;
  962. #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
  963. if ((ret = vc1_decode_sprites(v, &s->gb)) < 0)
  964. goto err;
  965. #endif
  966. if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
  967. goto err;
  968. *got_frame = 1;
  969. } else {
  970. if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
  971. if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
  972. goto err;
  973. ff_print_debug_info(s, s->current_picture_ptr, pict);
  974. *got_frame = 1;
  975. } else if (s->last_picture_ptr) {
  976. if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
  977. goto err;
  978. ff_print_debug_info(s, s->last_picture_ptr, pict);
  979. *got_frame = 1;
  980. }
  981. }
  982. end:
  983. av_free(buf2);
  984. for (i = 0; i < n_slices; i++)
  985. av_free(slices[i].buf);
  986. av_free(slices);
  987. return buf_size;
  988. err:
  989. av_free(buf2);
  990. for (i = 0; i < n_slices; i++)
  991. av_free(slices[i].buf);
  992. av_free(slices);
  993. return ret;
  994. }
  995. static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
  996. #if CONFIG_VC1_DXVA2_HWACCEL
  997. AV_PIX_FMT_DXVA2_VLD,
  998. #endif
  999. #if CONFIG_VC1_D3D11VA_HWACCEL
  1000. AV_PIX_FMT_D3D11VA_VLD,
  1001. AV_PIX_FMT_D3D11,
  1002. #endif
  1003. #if CONFIG_VC1_NVDEC_HWACCEL
  1004. AV_PIX_FMT_CUDA,
  1005. #endif
  1006. #if CONFIG_VC1_VAAPI_HWACCEL
  1007. AV_PIX_FMT_VAAPI,
  1008. #endif
  1009. #if CONFIG_VC1_VDPAU_HWACCEL
  1010. AV_PIX_FMT_VDPAU,
  1011. #endif
  1012. AV_PIX_FMT_YUV420P,
  1013. AV_PIX_FMT_NONE
  1014. };
  1015. AVCodec ff_vc1_decoder = {
  1016. .name = "vc1",
  1017. .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
  1018. .type = AVMEDIA_TYPE_VIDEO,
  1019. .id = AV_CODEC_ID_VC1,
  1020. .priv_data_size = sizeof(VC1Context),
  1021. .init = vc1_decode_init,
  1022. .close = ff_vc1_decode_end,
  1023. .decode = vc1_decode_frame,
  1024. .flush = ff_mpeg_flush,
  1025. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  1026. .pix_fmts = vc1_hwaccel_pixfmt_list_420,
  1027. .profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
  1028. };
  1029. #if CONFIG_WMV3_DECODER
  1030. AVCodec ff_wmv3_decoder = {
  1031. .name = "wmv3",
  1032. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
  1033. .type = AVMEDIA_TYPE_VIDEO,
  1034. .id = AV_CODEC_ID_WMV3,
  1035. .priv_data_size = sizeof(VC1Context),
  1036. .init = vc1_decode_init,
  1037. .close = ff_vc1_decode_end,
  1038. .decode = vc1_decode_frame,
  1039. .flush = ff_mpeg_flush,
  1040. .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY,
  1041. .pix_fmts = vc1_hwaccel_pixfmt_list_420,
  1042. .profiles = NULL_IF_CONFIG_SMALL(ff_vc1_profiles)
  1043. };
  1044. #endif
  1045. #if CONFIG_WMV3IMAGE_DECODER
  1046. AVCodec ff_wmv3image_decoder = {
  1047. .name = "wmv3image",
  1048. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
  1049. .type = AVMEDIA_TYPE_VIDEO,
  1050. .id = AV_CODEC_ID_WMV3IMAGE,
  1051. .priv_data_size = sizeof(VC1Context),
  1052. .init = vc1_decode_init,
  1053. .close = ff_vc1_decode_end,
  1054. .decode = vc1_decode_frame,
  1055. .capabilities = AV_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
  1063. #if CONFIG_VC1IMAGE_DECODER
  1064. AVCodec ff_vc1image_decoder = {
  1065. .name = "vc1image",
  1066. .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
  1067. .type = AVMEDIA_TYPE_VIDEO,
  1068. .id = AV_CODEC_ID_VC1IMAGE,
  1069. .priv_data_size = sizeof(VC1Context),
  1070. .init = vc1_decode_init,
  1071. .close = ff_vc1_decode_end,
  1072. .decode = vc1_decode_frame,
  1073. .capabilities = AV_CODEC_CAP_DR1,
  1074. .flush = vc1_sprite_flush,
  1075. .pix_fmts = (const enum AVPixelFormat[]) {
  1076. AV_PIX_FMT_YUV420P,
  1077. AV_PIX_FMT_NONE
  1078. },
  1079. };
  1080. #endif