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.

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