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.

1310 lines
49KB

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