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.

1040 lines
38KB

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