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.

871 lines
27KB

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