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.

857 lines
26KB

  1. /*
  2. * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Microsoft Screen 2 (aka Windows Media Video V9 Screen) decoder
  23. */
  24. #include "libavutil/avassert.h"
  25. #include "error_resilience.h"
  26. #include "internal.h"
  27. #include "msmpeg4data.h"
  28. #include "qpeldsp.h"
  29. #include "vc1.h"
  30. #include "mss12.h"
  31. #include "mss2dsp.h"
  32. typedef struct MSS2Context {
  33. VC1Context v;
  34. int split_position;
  35. AVFrame *last_pic;
  36. MSS12Context c;
  37. MSS2DSPContext dsp;
  38. QpelDSPContext qdsp;
  39. SliceContext sc[2];
  40. } MSS2Context;
  41. static void arith2_normalise(ArithCoder *c)
  42. {
  43. while ((c->high >> 15) - (c->low >> 15) < 2) {
  44. if ((c->low ^ c->high) & 0x10000) {
  45. c->high ^= 0x8000;
  46. c->value ^= 0x8000;
  47. c->low ^= 0x8000;
  48. }
  49. c->high = c->high << 8 & 0xFFFFFF | 0xFF;
  50. c->value = c->value << 8 & 0xFFFFFF | bytestream2_get_byte(c->gbc.gB);
  51. c->low = c->low << 8 & 0xFFFFFF;
  52. }
  53. }
  54. ARITH_GET_BIT(2)
  55. /* L. Stuiver and A. Moffat: "Piecewise Integer Mapping for Arithmetic Coding."
  56. * In Proc. 8th Data Compression Conference (DCC '98), pp. 3-12, Mar. 1998 */
  57. static int arith2_get_scaled_value(int value, int n, int range)
  58. {
  59. int split = (n << 1) - range;
  60. if (value > split)
  61. return split + (value - split >> 1);
  62. else
  63. return value;
  64. }
  65. static void arith2_rescale_interval(ArithCoder *c, int range,
  66. int low, int high, int n)
  67. {
  68. int split = (n << 1) - range;
  69. if (high > split)
  70. c->high = split + (high - split << 1);
  71. else
  72. c->high = high;
  73. c->high += c->low - 1;
  74. if (low > split)
  75. c->low += split + (low - split << 1);
  76. else
  77. c->low += low;
  78. }
  79. static int arith2_get_number(ArithCoder *c, int n)
  80. {
  81. int range = c->high - c->low + 1;
  82. int scale = av_log2(range) - av_log2(n);
  83. int val;
  84. if (n << scale > range)
  85. scale--;
  86. n <<= scale;
  87. val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
  88. arith2_rescale_interval(c, range, val << scale, (val + 1) << scale, n);
  89. arith2_normalise(c);
  90. return val;
  91. }
  92. static int arith2_get_prob(ArithCoder *c, int16_t *probs)
  93. {
  94. int range = c->high - c->low + 1, n = *probs;
  95. int scale = av_log2(range) - av_log2(n);
  96. int i = 0, val;
  97. if (n << scale > range)
  98. scale--;
  99. n <<= scale;
  100. val = arith2_get_scaled_value(c->value - c->low, n, range) >> scale;
  101. while (probs[++i] > val) ;
  102. arith2_rescale_interval(c, range,
  103. probs[i] << scale, probs[i - 1] << scale, n);
  104. return i;
  105. }
  106. ARITH_GET_MODEL_SYM(2)
  107. static int arith2_get_consumed_bytes(ArithCoder *c)
  108. {
  109. int diff = (c->high >> 16) - (c->low >> 16);
  110. int bp = bytestream2_tell(c->gbc.gB) - 3 << 3;
  111. int bits = 1;
  112. while (!(diff & 0x80)) {
  113. bits++;
  114. diff <<= 1;
  115. }
  116. return (bits + bp + 7 >> 3) + ((c->low >> 16) + 1 == c->high >> 16);
  117. }
  118. static void arith2_init(ArithCoder *c, GetByteContext *gB)
  119. {
  120. c->low = 0;
  121. c->high = 0xFFFFFF;
  122. c->value = bytestream2_get_be24(gB);
  123. c->gbc.gB = gB;
  124. c->get_model_sym = arith2_get_model_sym;
  125. c->get_number = arith2_get_number;
  126. }
  127. static int decode_pal_v2(MSS12Context *ctx, const uint8_t *buf, int buf_size)
  128. {
  129. int i, ncol;
  130. uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
  131. if (!ctx->free_colours)
  132. return 0;
  133. ncol = *buf++;
  134. if (ncol > ctx->free_colours || buf_size < 2 + ncol * 3)
  135. return AVERROR_INVALIDDATA;
  136. for (i = 0; i < ncol; i++)
  137. *pal++ = AV_RB24(buf + 3 * i);
  138. return 1 + ncol * 3;
  139. }
  140. static int decode_555(GetByteContext *gB, uint16_t *dst, int stride,
  141. int keyframe, int w, int h)
  142. {
  143. int last_symbol = 0, repeat = 0, prev_avail = 0;
  144. if (!keyframe) {
  145. int x, y, endx, endy, t;
  146. #define READ_PAIR(a, b) \
  147. a = bytestream2_get_byte(gB) << 4; \
  148. t = bytestream2_get_byte(gB); \
  149. a |= t >> 4; \
  150. b = (t & 0xF) << 8; \
  151. b |= bytestream2_get_byte(gB); \
  152. READ_PAIR(x, endx)
  153. READ_PAIR(y, endy)
  154. if (endx >= w || endy >= h || x > endx || y > endy)
  155. return AVERROR_INVALIDDATA;
  156. dst += x + stride * y;
  157. w = endx - x + 1;
  158. h = endy - y + 1;
  159. if (y)
  160. prev_avail = 1;
  161. }
  162. do {
  163. uint16_t *p = dst;
  164. do {
  165. if (repeat-- < 1) {
  166. int b = bytestream2_get_byte(gB);
  167. if (b < 128)
  168. last_symbol = b << 8 | bytestream2_get_byte(gB);
  169. else if (b > 129) {
  170. repeat = 0;
  171. while (b-- > 130)
  172. repeat = (repeat << 8) + bytestream2_get_byte(gB) + 1;
  173. if (last_symbol == -2) {
  174. int skip = FFMIN((unsigned)repeat, dst + w - p);
  175. repeat -= skip;
  176. p += skip;
  177. }
  178. } else
  179. last_symbol = 127 - b;
  180. }
  181. if (last_symbol >= 0)
  182. *p = last_symbol;
  183. else if (last_symbol == -1 && prev_avail)
  184. *p = *(p - stride);
  185. } while (++p < dst + w);
  186. dst += stride;
  187. prev_avail = 1;
  188. } while (--h);
  189. return 0;
  190. }
  191. static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,
  192. uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,
  193. int keyframe, int kf_slipt, int slice, int w, int h)
  194. {
  195. uint8_t bits[270] = { 0 };
  196. uint32_t codes[270];
  197. VLC vlc;
  198. int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;
  199. int remaining_codes, surplus_codes, i;
  200. const int alphabet_size = 270 - keyframe;
  201. int last_symbol = 0, repeat = 0, prev_avail = 0;
  202. if (!keyframe) {
  203. int x, y, clipw, cliph;
  204. x = get_bits(gb, 12);
  205. y = get_bits(gb, 12);
  206. clipw = get_bits(gb, 12) + 1;
  207. cliph = get_bits(gb, 12) + 1;
  208. if (x + clipw > w || y + cliph > h)
  209. return AVERROR_INVALIDDATA;
  210. pal_dst += pal_stride * y + x;
  211. rgb_dst += rgb_stride * y + x * 3;
  212. w = clipw;
  213. h = cliph;
  214. if (y)
  215. prev_avail = 1;
  216. } else {
  217. if (slice > 0) {
  218. pal_dst += pal_stride * kf_slipt;
  219. rgb_dst += rgb_stride * kf_slipt;
  220. prev_avail = 1;
  221. h -= kf_slipt;
  222. } else
  223. h = kf_slipt;
  224. }
  225. /* read explicit codes */
  226. do {
  227. while (current_codes--) {
  228. int symbol = get_bits(gb, 8);
  229. if (symbol >= 204 - keyframe)
  230. symbol += 14 - keyframe;
  231. else if (symbol > 189)
  232. symbol = get_bits1(gb) + (symbol << 1) - 190;
  233. if (bits[symbol])
  234. return AVERROR_INVALIDDATA;
  235. bits[symbol] = current_length;
  236. codes[symbol] = next_code++;
  237. read_codes++;
  238. }
  239. current_length++;
  240. next_code <<= 1;
  241. remaining_codes = (1 << current_length) - next_code;
  242. current_codes = get_bits(gb, av_ceil_log2(remaining_codes + 1));
  243. if (current_length > 22 || current_codes > remaining_codes)
  244. return AVERROR_INVALIDDATA;
  245. } while (current_codes != remaining_codes);
  246. remaining_codes = alphabet_size - read_codes;
  247. /* determine the minimum length to fit the rest of the alphabet */
  248. while ((surplus_codes = (2 << current_length) -
  249. (next_code << 1) - remaining_codes) < 0) {
  250. current_length++;
  251. next_code <<= 1;
  252. }
  253. /* add the rest of the symbols lexicographically */
  254. for (i = 0; i < alphabet_size; i++)
  255. if (!bits[i]) {
  256. if (surplus_codes-- == 0) {
  257. current_length++;
  258. next_code <<= 1;
  259. }
  260. bits[i] = current_length;
  261. codes[i] = next_code++;
  262. }
  263. if (next_code != 1 << current_length)
  264. return AVERROR_INVALIDDATA;
  265. if (i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0))
  266. return i;
  267. /* frame decode */
  268. do {
  269. uint8_t *pp = pal_dst;
  270. uint8_t *rp = rgb_dst;
  271. do {
  272. if (repeat-- < 1) {
  273. int b = get_vlc2(gb, vlc.table, 9, 3);
  274. if (b < 256)
  275. last_symbol = b;
  276. else if (b < 268) {
  277. b -= 256;
  278. if (b == 11)
  279. b = get_bits(gb, 4) + 10;
  280. if (!b)
  281. repeat = 0;
  282. else
  283. repeat = get_bits(gb, b);
  284. repeat += (1 << b) - 1;
  285. if (last_symbol == -2) {
  286. int skip = FFMIN(repeat, pal_dst + w - pp);
  287. repeat -= skip;
  288. pp += skip;
  289. rp += skip * 3;
  290. }
  291. } else
  292. last_symbol = 267 - b;
  293. }
  294. if (last_symbol >= 0) {
  295. *pp = last_symbol;
  296. AV_WB24(rp, pal[last_symbol]);
  297. } else if (last_symbol == -1 && prev_avail) {
  298. *pp = *(pp - pal_stride);
  299. memcpy(rp, rp - rgb_stride, 3);
  300. }
  301. rp += 3;
  302. } while (++pp < pal_dst + w);
  303. pal_dst += pal_stride;
  304. rgb_dst += rgb_stride;
  305. prev_avail = 1;
  306. } while (--h);
  307. ff_free_vlc(&vlc);
  308. return 0;
  309. }
  310. static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size,
  311. int x, int y, int w, int h, int wmv9_mask)
  312. {
  313. MSS2Context *ctx = avctx->priv_data;
  314. MSS12Context *c = &ctx->c;
  315. VC1Context *v = avctx->priv_data;
  316. MpegEncContext *s = &v->s;
  317. AVFrame *f;
  318. int ret;
  319. ff_mpeg_flush(avctx);
  320. init_get_bits(&s->gb, buf, buf_size * 8);
  321. s->loop_filter = avctx->skip_loop_filter < AVDISCARD_ALL;
  322. if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
  323. av_log(v->s.avctx, AV_LOG_ERROR, "header error\n");
  324. return AVERROR_INVALIDDATA;
  325. }
  326. if (s->pict_type != AV_PICTURE_TYPE_I) {
  327. av_log(v->s.avctx, AV_LOG_ERROR, "expected I-frame\n");
  328. return AVERROR_INVALIDDATA;
  329. }
  330. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  331. if ((ret = ff_MPV_frame_start(s, avctx)) < 0) {
  332. av_log(v->s.avctx, AV_LOG_ERROR, "ff_MPV_frame_start error\n");
  333. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  334. return ret;
  335. }
  336. ff_mpeg_er_frame_start(s);
  337. v->bits = buf_size * 8;
  338. v->end_mb_x = (w + 15) >> 4;
  339. s->end_mb_y = (h + 15) >> 4;
  340. if (v->respic & 1)
  341. v->end_mb_x = v->end_mb_x + 1 >> 1;
  342. if (v->respic & 2)
  343. s->end_mb_y = s->end_mb_y + 1 >> 1;
  344. ff_vc1_decode_blocks(v);
  345. ff_er_frame_end(&s->er);
  346. ff_MPV_frame_end(s);
  347. f = s->current_picture.f;
  348. if (v->respic == 3) {
  349. ctx->dsp.upsample_plane(f->data[0], f->linesize[0], w, h);
  350. ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w >> 1, h >> 1);
  351. ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w >> 1, h >> 1);
  352. } else if (v->respic)
  353. avpriv_request_sample(v->s.avctx,
  354. "Asymmetric WMV9 rectangle subsampling");
  355. av_assert0(f->linesize[1] == f->linesize[2]);
  356. if (wmv9_mask != -1)
  357. ctx->dsp.mss2_blit_wmv9_masked(c->rgb_pic + y * c->rgb_stride + x * 3,
  358. c->rgb_stride, wmv9_mask,
  359. c->pal_pic + y * c->pal_stride + x,
  360. c->pal_stride,
  361. f->data[0], f->linesize[0],
  362. f->data[1], f->data[2], f->linesize[1],
  363. w, h);
  364. else
  365. ctx->dsp.mss2_blit_wmv9(c->rgb_pic + y * c->rgb_stride + x * 3,
  366. c->rgb_stride,
  367. f->data[0], f->linesize[0],
  368. f->data[1], f->data[2], f->linesize[1],
  369. w, h);
  370. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  371. return 0;
  372. }
  373. typedef struct Rectangle {
  374. int coded, x, y, w, h;
  375. } Rectangle;
  376. #define MAX_WMV9_RECTANGLES 20
  377. #define ARITH2_PADDING 2
  378. static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  379. AVPacket *avpkt)
  380. {
  381. const uint8_t *buf = avpkt->data;
  382. int buf_size = avpkt->size;
  383. MSS2Context *ctx = avctx->priv_data;
  384. MSS12Context *c = &ctx->c;
  385. AVFrame *frame = data;
  386. GetBitContext gb;
  387. GetByteContext gB;
  388. ArithCoder acoder;
  389. int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
  390. Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
  391. int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);
  392. av_assert0(FF_INPUT_BUFFER_PADDING_SIZE >=
  393. ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);
  394. init_get_bits(&gb, buf, buf_size * 8);
  395. if (keyframe = get_bits1(&gb))
  396. skip_bits(&gb, 7);
  397. has_wmv9 = get_bits1(&gb);
  398. has_mv = keyframe ? 0 : get_bits1(&gb);
  399. is_rle = get_bits1(&gb);
  400. is_555 = is_rle && get_bits1(&gb);
  401. if (c->slice_split > 0)
  402. ctx->split_position = c->slice_split;
  403. else if (c->slice_split < 0) {
  404. if (get_bits1(&gb)) {
  405. if (get_bits1(&gb)) {
  406. if (get_bits1(&gb))
  407. ctx->split_position = get_bits(&gb, 16);
  408. else
  409. ctx->split_position = get_bits(&gb, 12);
  410. } else
  411. ctx->split_position = get_bits(&gb, 8) << 4;
  412. } else {
  413. if (keyframe)
  414. ctx->split_position = avctx->height / 2;
  415. }
  416. } else
  417. ctx->split_position = avctx->height;
  418. if (c->slice_split && (ctx->split_position < 1 - is_555 ||
  419. ctx->split_position > avctx->height - 1))
  420. return AVERROR_INVALIDDATA;
  421. align_get_bits(&gb);
  422. buf += get_bits_count(&gb) >> 3;
  423. buf_size -= get_bits_count(&gb) >> 3;
  424. if (buf_size < 1)
  425. return AVERROR_INVALIDDATA;
  426. if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
  427. return AVERROR_INVALIDDATA;
  428. avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;
  429. if (ctx->last_pic->format != avctx->pix_fmt)
  430. av_frame_unref(ctx->last_pic);
  431. if (has_wmv9) {
  432. bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
  433. arith2_init(&acoder, &gB);
  434. implicit_rect = !arith2_get_bit(&acoder);
  435. while (arith2_get_bit(&acoder)) {
  436. if (used_rects == MAX_WMV9_RECTANGLES)
  437. return AVERROR_INVALIDDATA;
  438. r = &wmv9rects[used_rects];
  439. if (!used_rects)
  440. r->x = arith2_get_number(&acoder, avctx->width);
  441. else
  442. r->x = arith2_get_number(&acoder, avctx->width -
  443. wmv9rects[used_rects - 1].x) +
  444. wmv9rects[used_rects - 1].x;
  445. r->y = arith2_get_number(&acoder, avctx->height);
  446. r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;
  447. r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
  448. used_rects++;
  449. }
  450. if (implicit_rect && used_rects) {
  451. av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
  452. return AVERROR_INVALIDDATA;
  453. }
  454. if (implicit_rect) {
  455. wmv9rects[0].x = 0;
  456. wmv9rects[0].y = 0;
  457. wmv9rects[0].w = avctx->width;
  458. wmv9rects[0].h = avctx->height;
  459. used_rects = 1;
  460. }
  461. for (i = 0; i < used_rects; i++) {
  462. if (!implicit_rect && arith2_get_bit(&acoder)) {
  463. av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
  464. return AVERROR_INVALIDDATA;
  465. }
  466. if (!i) {
  467. wmv9_mask = arith2_get_bit(&acoder) - 1;
  468. if (!wmv9_mask)
  469. wmv9_mask = arith2_get_number(&acoder, 256);
  470. }
  471. wmv9rects[i].coded = arith2_get_number(&acoder, 2);
  472. }
  473. buf += arith2_get_consumed_bytes(&acoder);
  474. buf_size -= arith2_get_consumed_bytes(&acoder);
  475. if (buf_size < 1)
  476. return AVERROR_INVALIDDATA;
  477. }
  478. c->mvX = c->mvY = 0;
  479. if (keyframe && !is_555) {
  480. if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
  481. return AVERROR_INVALIDDATA;
  482. buf += i;
  483. buf_size -= i;
  484. } else if (has_mv) {
  485. buf += 4;
  486. buf_size -= 4;
  487. if (buf_size < 1)
  488. return AVERROR_INVALIDDATA;
  489. c->mvX = AV_RB16(buf - 4) - avctx->width;
  490. c->mvY = AV_RB16(buf - 2) - avctx->height;
  491. }
  492. if (c->mvX < 0 || c->mvY < 0) {
  493. FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
  494. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0) {
  495. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  496. return ret;
  497. }
  498. if (ctx->last_pic->data[0]) {
  499. av_assert0(frame->linesize[0] == ctx->last_pic->linesize[0]);
  500. c->last_rgb_pic = ctx->last_pic->data[0] +
  501. ctx->last_pic->linesize[0] * (avctx->height - 1);
  502. } else {
  503. av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
  504. return AVERROR_INVALIDDATA;
  505. }
  506. } else {
  507. if ((ret = ff_reget_buffer(avctx, ctx->last_pic)) < 0) {
  508. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  509. return ret;
  510. }
  511. if ((ret = av_frame_ref(frame, ctx->last_pic)) < 0)
  512. return ret;
  513. c->last_rgb_pic = NULL;
  514. }
  515. c->rgb_pic = frame->data[0] +
  516. frame->linesize[0] * (avctx->height - 1);
  517. c->rgb_stride = -frame->linesize[0];
  518. frame->key_frame = keyframe;
  519. frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  520. if (is_555) {
  521. bytestream2_init(&gB, buf, buf_size);
  522. if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
  523. keyframe, avctx->width, avctx->height))
  524. return AVERROR_INVALIDDATA;
  525. buf_size -= bytestream2_tell(&gB);
  526. } else {
  527. if (keyframe) {
  528. c->corrupted = 0;
  529. ff_mss12_slicecontext_reset(&ctx->sc[0]);
  530. if (c->slice_split)
  531. ff_mss12_slicecontext_reset(&ctx->sc[1]);
  532. }
  533. if (is_rle) {
  534. init_get_bits(&gb, buf, buf_size * 8);
  535. if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
  536. c->rgb_pic, c->rgb_stride, c->pal, keyframe,
  537. ctx->split_position, 0,
  538. avctx->width, avctx->height))
  539. return ret;
  540. align_get_bits(&gb);
  541. if (c->slice_split)
  542. if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
  543. c->rgb_pic, c->rgb_stride, c->pal, keyframe,
  544. ctx->split_position, 1,
  545. avctx->width, avctx->height))
  546. return ret;
  547. align_get_bits(&gb);
  548. buf += get_bits_count(&gb) >> 3;
  549. buf_size -= get_bits_count(&gb) >> 3;
  550. } else if (!implicit_rect || wmv9_mask != -1) {
  551. if (c->corrupted)
  552. return AVERROR_INVALIDDATA;
  553. bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
  554. arith2_init(&acoder, &gB);
  555. c->keyframe = keyframe;
  556. if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,
  557. avctx->width,
  558. ctx->split_position))
  559. return AVERROR_INVALIDDATA;
  560. buf += arith2_get_consumed_bytes(&acoder);
  561. buf_size -= arith2_get_consumed_bytes(&acoder);
  562. if (c->slice_split) {
  563. if (buf_size < 1)
  564. return AVERROR_INVALIDDATA;
  565. bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
  566. arith2_init(&acoder, &gB);
  567. if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,
  568. ctx->split_position,
  569. avctx->width,
  570. avctx->height - ctx->split_position))
  571. return AVERROR_INVALIDDATA;
  572. buf += arith2_get_consumed_bytes(&acoder);
  573. buf_size -= arith2_get_consumed_bytes(&acoder);
  574. }
  575. } else
  576. memset(c->pal_pic, 0, c->pal_stride * avctx->height);
  577. }
  578. if (has_wmv9) {
  579. for (i = 0; i < used_rects; i++) {
  580. int x = wmv9rects[i].x;
  581. int y = wmv9rects[i].y;
  582. int w = wmv9rects[i].w;
  583. int h = wmv9rects[i].h;
  584. if (wmv9rects[i].coded) {
  585. int WMV9codedFrameSize;
  586. if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
  587. return AVERROR_INVALIDDATA;
  588. if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
  589. x, y, w, h, wmv9_mask))
  590. return ret;
  591. buf += WMV9codedFrameSize + 3;
  592. buf_size -= WMV9codedFrameSize + 3;
  593. } else {
  594. uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
  595. if (wmv9_mask != -1) {
  596. ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
  597. wmv9_mask,
  598. c->pal_pic + y * c->pal_stride + x,
  599. c->pal_stride,
  600. w, h);
  601. } else {
  602. do {
  603. memset(dst, 0x80, w * 3);
  604. dst += c->rgb_stride;
  605. } while (--h);
  606. }
  607. }
  608. }
  609. }
  610. if (buf_size)
  611. av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
  612. if (c->mvX < 0 || c->mvY < 0) {
  613. av_frame_unref(ctx->last_pic);
  614. ret = av_frame_ref(ctx->last_pic, frame);
  615. if (ret < 0)
  616. return ret;
  617. }
  618. *got_frame = 1;
  619. return avpkt->size;
  620. }
  621. static av_cold int wmv9_init(AVCodecContext *avctx)
  622. {
  623. VC1Context *v = avctx->priv_data;
  624. int ret;
  625. v->s.avctx = avctx;
  626. if ((ret = ff_vc1_init_common(v)) < 0)
  627. return ret;
  628. ff_vc1dsp_init(&v->vc1dsp);
  629. v->profile = PROFILE_MAIN;
  630. v->zz_8x4 = ff_wmv2_scantableA;
  631. v->zz_4x8 = ff_wmv2_scantableB;
  632. v->res_y411 = 0;
  633. v->res_sprite = 0;
  634. v->frmrtq_postproc = 7;
  635. v->bitrtq_postproc = 31;
  636. v->res_x8 = 0;
  637. v->multires = 0;
  638. v->res_fasttx = 1;
  639. v->fastuvmc = 0;
  640. v->extended_mv = 0;
  641. v->dquant = 1;
  642. v->vstransform = 1;
  643. v->res_transtab = 0;
  644. v->overlap = 0;
  645. v->resync_marker = 0;
  646. v->rangered = 0;
  647. v->s.max_b_frames = avctx->max_b_frames = 0;
  648. v->quantizer_mode = 0;
  649. v->finterpflag = 0;
  650. v->res_rtm_flag = 1;
  651. ff_vc1_init_transposed_scantables(v);
  652. if ((ret = ff_msmpeg4_decode_init(avctx)) < 0 ||
  653. (ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
  654. return ret;
  655. /* error concealment */
  656. v->s.me.qpel_put = v->s.qdsp.put_qpel_pixels_tab;
  657. v->s.me.qpel_avg = v->s.qdsp.avg_qpel_pixels_tab;
  658. return 0;
  659. }
  660. static av_cold int mss2_decode_end(AVCodecContext *avctx)
  661. {
  662. MSS2Context *const ctx = avctx->priv_data;
  663. av_frame_free(&ctx->last_pic);
  664. ff_mss12_decode_end(&ctx->c);
  665. av_freep(&ctx->c.pal_pic);
  666. av_freep(&ctx->c.last_pal_pic);
  667. ff_vc1_decode_end(avctx);
  668. return 0;
  669. }
  670. static av_cold int mss2_decode_init(AVCodecContext *avctx)
  671. {
  672. MSS2Context * const ctx = avctx->priv_data;
  673. MSS12Context *c = &ctx->c;
  674. int ret;
  675. c->avctx = avctx;
  676. if (ret = ff_mss12_decode_init(c, 1, &ctx->sc[0], &ctx->sc[1]))
  677. return ret;
  678. c->pal_stride = c->mask_stride;
  679. c->pal_pic = av_mallocz(c->pal_stride * avctx->height);
  680. c->last_pal_pic = av_mallocz(c->pal_stride * avctx->height);
  681. if (!c->pal_pic || !c->last_pal_pic) {
  682. mss2_decode_end(avctx);
  683. return AVERROR(ENOMEM);
  684. }
  685. if (ret = wmv9_init(avctx)) {
  686. mss2_decode_end(avctx);
  687. return ret;
  688. }
  689. ff_mss2dsp_init(&ctx->dsp);
  690. ff_qpeldsp_init(&ctx->qdsp);
  691. avctx->pix_fmt = c->free_colours == 127 ? AV_PIX_FMT_RGB555
  692. : AV_PIX_FMT_RGB24;
  693. ctx->last_pic = av_frame_alloc();
  694. if (!ctx->last_pic) {
  695. mss2_decode_end(avctx);
  696. return AVERROR(ENOMEM);
  697. }
  698. return 0;
  699. }
  700. AVCodec ff_mss2_decoder = {
  701. .name = "mss2",
  702. .long_name = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
  703. .type = AVMEDIA_TYPE_VIDEO,
  704. .id = AV_CODEC_ID_MSS2,
  705. .priv_data_size = sizeof(MSS2Context),
  706. .init = mss2_decode_init,
  707. .close = mss2_decode_end,
  708. .decode = mss2_decode_frame,
  709. .capabilities = CODEC_CAP_DR1,
  710. };