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.

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