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.

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