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.

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