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.

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