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.

866 lines
26KB

  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 "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 = (uint16_t)c->high << 8 | 0xFF;
  52. c->value = (uint16_t)c->value << 8 | bytestream2_get_byte(c->gbc.gB);
  53. c->low = (uint16_t)c->low << 8;
  54. }
  55. }
  56. ARITH_GET_BIT(arith2)
  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(arith2)
  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->overread = 0;
  126. c->gbc.gB = gB;
  127. c->get_model_sym = arith2_get_model_sym;
  128. c->get_number = arith2_get_number;
  129. }
  130. static int decode_pal_v2(MSS12Context *ctx, const uint8_t *buf, int buf_size)
  131. {
  132. int i, ncol;
  133. uint32_t *pal = ctx->pal + 256 - ctx->free_colours;
  134. if (!ctx->free_colours)
  135. return 0;
  136. ncol = *buf++;
  137. if (ncol > ctx->free_colours || buf_size < 2 + ncol * 3)
  138. return AVERROR_INVALIDDATA;
  139. for (i = 0; i < ncol; i++)
  140. *pal++ = AV_RB24(buf + 3 * i);
  141. return 1 + ncol * 3;
  142. }
  143. static int decode_555(GetByteContext *gB, uint16_t *dst, int stride,
  144. int keyframe, int w, int h)
  145. {
  146. int last_symbol = 0, repeat = 0, prev_avail = 0;
  147. if (!keyframe) {
  148. int x, y, endx, endy, t;
  149. #define READ_PAIR(a, b) \
  150. a = bytestream2_get_byte(gB) << 4; \
  151. t = bytestream2_get_byte(gB); \
  152. a |= t >> 4; \
  153. b = (t & 0xF) << 8; \
  154. b |= bytestream2_get_byte(gB); \
  155. READ_PAIR(x, endx)
  156. READ_PAIR(y, endy)
  157. if (endx >= w || endy >= h || x > endx || y > endy)
  158. return AVERROR_INVALIDDATA;
  159. dst += x + stride * y;
  160. w = endx - x + 1;
  161. h = endy - y + 1;
  162. if (y)
  163. prev_avail = 1;
  164. }
  165. do {
  166. uint16_t *p = dst;
  167. do {
  168. if (repeat-- < 1) {
  169. int b = bytestream2_get_byte(gB);
  170. if (b < 128)
  171. last_symbol = b << 8 | bytestream2_get_byte(gB);
  172. else if (b > 129) {
  173. repeat = 0;
  174. while (b-- > 130) {
  175. if (repeat >= (INT_MAX >> 8) - 1) {
  176. av_log(NULL, AV_LOG_ERROR, "repeat overflow\n");
  177. return AVERROR_INVALIDDATA;
  178. }
  179. repeat = (repeat << 8) + bytestream2_get_byte(gB) + 1;
  180. }
  181. if (last_symbol == -2) {
  182. int skip = FFMIN((unsigned)repeat, dst + w - p);
  183. repeat -= skip;
  184. p += skip;
  185. }
  186. } else
  187. last_symbol = 127 - b;
  188. }
  189. if (last_symbol >= 0)
  190. *p = last_symbol;
  191. else if (last_symbol == -1 && prev_avail)
  192. *p = *(p - stride);
  193. } while (++p < dst + w);
  194. dst += stride;
  195. prev_avail = 1;
  196. } while (--h);
  197. return 0;
  198. }
  199. static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, int pal_stride,
  200. uint8_t *rgb_dst, int rgb_stride, uint32_t *pal,
  201. int keyframe, int kf_slipt, int slice, int w, int h)
  202. {
  203. uint8_t bits[270] = { 0 };
  204. uint32_t codes[270];
  205. VLC vlc;
  206. int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;
  207. int remaining_codes, surplus_codes, i;
  208. const int alphabet_size = 270 - keyframe;
  209. int last_symbol = 0, repeat = 0, prev_avail = 0;
  210. if (!keyframe) {
  211. int x, y, clipw, cliph;
  212. x = get_bits(gb, 12);
  213. y = get_bits(gb, 12);
  214. clipw = get_bits(gb, 12) + 1;
  215. cliph = get_bits(gb, 12) + 1;
  216. if (x + clipw > w || y + cliph > h)
  217. return AVERROR_INVALIDDATA;
  218. pal_dst += pal_stride * y + x;
  219. rgb_dst += rgb_stride * y + x * 3;
  220. w = clipw;
  221. h = cliph;
  222. if (y)
  223. prev_avail = 1;
  224. } else {
  225. if (slice > 0) {
  226. pal_dst += pal_stride * kf_slipt;
  227. rgb_dst += rgb_stride * kf_slipt;
  228. prev_avail = 1;
  229. h -= kf_slipt;
  230. } else
  231. h = kf_slipt;
  232. }
  233. /* read explicit codes */
  234. do {
  235. while (current_codes--) {
  236. int symbol = get_bits(gb, 8);
  237. if (symbol >= 204 - keyframe)
  238. symbol += 14 - keyframe;
  239. else if (symbol > 189)
  240. symbol = get_bits1(gb) + (symbol << 1) - 190;
  241. if (bits[symbol])
  242. return AVERROR_INVALIDDATA;
  243. bits[symbol] = current_length;
  244. codes[symbol] = next_code++;
  245. read_codes++;
  246. }
  247. current_length++;
  248. next_code <<= 1;
  249. remaining_codes = (1 << current_length) - next_code;
  250. current_codes = get_bits(gb, av_ceil_log2(remaining_codes + 1));
  251. if (current_length > 22 || current_codes > remaining_codes)
  252. return AVERROR_INVALIDDATA;
  253. } while (current_codes != remaining_codes);
  254. remaining_codes = alphabet_size - read_codes;
  255. /* determine the minimum length to fit the rest of the alphabet */
  256. while ((surplus_codes = (2 << current_length) -
  257. (next_code << 1) - remaining_codes) < 0) {
  258. current_length++;
  259. next_code <<= 1;
  260. }
  261. /* add the rest of the symbols lexicographically */
  262. for (i = 0; i < alphabet_size; i++)
  263. if (!bits[i]) {
  264. if (surplus_codes-- == 0) {
  265. current_length++;
  266. next_code <<= 1;
  267. }
  268. bits[i] = current_length;
  269. codes[i] = next_code++;
  270. }
  271. if (next_code != 1 << current_length)
  272. return AVERROR_INVALIDDATA;
  273. if ((i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0)) < 0)
  274. return i;
  275. /* frame decode */
  276. do {
  277. uint8_t *pp = pal_dst;
  278. uint8_t *rp = rgb_dst;
  279. do {
  280. if (repeat-- < 1) {
  281. int b = get_vlc2(gb, vlc.table, 9, 3);
  282. if (b < 256)
  283. last_symbol = b;
  284. else if (b < 268) {
  285. b -= 256;
  286. if (b == 11)
  287. b = get_bits(gb, 4) + 10;
  288. if (!b)
  289. repeat = 0;
  290. else
  291. repeat = get_bits(gb, b);
  292. repeat += (1 << b) - 1;
  293. if (last_symbol == -2) {
  294. int skip = FFMIN(repeat, pal_dst + w - pp);
  295. repeat -= skip;
  296. pp += skip;
  297. rp += skip * 3;
  298. }
  299. } else
  300. last_symbol = 267 - b;
  301. }
  302. if (last_symbol >= 0) {
  303. *pp = last_symbol;
  304. AV_WB24(rp, pal[last_symbol]);
  305. } else if (last_symbol == -1 && prev_avail) {
  306. *pp = *(pp - pal_stride);
  307. memcpy(rp, rp - rgb_stride, 3);
  308. }
  309. rp += 3;
  310. } while (++pp < pal_dst + w);
  311. pal_dst += pal_stride;
  312. rgb_dst += rgb_stride;
  313. prev_avail = 1;
  314. } while (--h);
  315. ff_free_vlc(&vlc);
  316. return 0;
  317. }
  318. static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size,
  319. int x, int y, int w, int h, int wmv9_mask)
  320. {
  321. MSS2Context *ctx = avctx->priv_data;
  322. MSS12Context *c = &ctx->c;
  323. VC1Context *v = avctx->priv_data;
  324. MpegEncContext *s = &v->s;
  325. AVFrame *f;
  326. int ret;
  327. ff_mpeg_flush(avctx);
  328. if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
  329. return ret;
  330. s->loop_filter = avctx->skip_loop_filter < AVDISCARD_ALL;
  331. if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
  332. av_log(v->s.avctx, AV_LOG_ERROR, "header error\n");
  333. return AVERROR_INVALIDDATA;
  334. }
  335. if (s->pict_type != AV_PICTURE_TYPE_I) {
  336. av_log(v->s.avctx, AV_LOG_ERROR, "expected I-frame\n");
  337. return AVERROR_INVALIDDATA;
  338. }
  339. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  340. if ((ret = ff_mpv_frame_start(s, avctx)) < 0) {
  341. av_log(v->s.avctx, AV_LOG_ERROR, "ff_mpv_frame_start error\n");
  342. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  343. return ret;
  344. }
  345. ff_mpeg_er_frame_start(s);
  346. v->bits = buf_size * 8;
  347. v->end_mb_x = (w + 15) >> 4;
  348. s->end_mb_y = (h + 15) >> 4;
  349. if (v->respic & 1)
  350. v->end_mb_x = v->end_mb_x + 1 >> 1;
  351. if (v->respic & 2)
  352. s->end_mb_y = s->end_mb_y + 1 >> 1;
  353. ff_vc1_decode_blocks(v);
  354. if (v->end_mb_x == s->mb_width && s->end_mb_y == s->mb_height) {
  355. ff_er_frame_end(&s->er);
  356. } else {
  357. av_log(v->s.avctx, AV_LOG_WARNING,
  358. "disabling error correction due to block count mismatch %dx%d != %dx%d\n",
  359. v->end_mb_x, s->end_mb_y, s->mb_width, s->mb_height);
  360. }
  361. ff_mpv_frame_end(s);
  362. f = s->current_picture.f;
  363. if (v->respic == 3) {
  364. ctx->dsp.upsample_plane(f->data[0], f->linesize[0], w, h);
  365. ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w+1 >> 1, h+1 >> 1);
  366. ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w+1 >> 1, h+1 >> 1);
  367. } else if (v->respic)
  368. avpriv_request_sample(v->s.avctx,
  369. "Asymmetric WMV9 rectangle subsampling");
  370. av_assert0(f->linesize[1] == f->linesize[2]);
  371. if (wmv9_mask != -1)
  372. ctx->dsp.mss2_blit_wmv9_masked(c->rgb_pic + y * c->rgb_stride + x * 3,
  373. c->rgb_stride, wmv9_mask,
  374. c->pal_pic + y * c->pal_stride + x,
  375. c->pal_stride,
  376. f->data[0], f->linesize[0],
  377. f->data[1], f->data[2], f->linesize[1],
  378. w, h);
  379. else
  380. ctx->dsp.mss2_blit_wmv9(c->rgb_pic + y * c->rgb_stride + x * 3,
  381. c->rgb_stride,
  382. f->data[0], f->linesize[0],
  383. f->data[1], f->data[2], f->linesize[1],
  384. w, h);
  385. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  386. return 0;
  387. }
  388. typedef struct Rectangle {
  389. int coded, x, y, w, h;
  390. } Rectangle;
  391. #define MAX_WMV9_RECTANGLES 20
  392. #define ARITH2_PADDING 2
  393. static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  394. AVPacket *avpkt)
  395. {
  396. const uint8_t *buf = avpkt->data;
  397. int buf_size = avpkt->size;
  398. MSS2Context *ctx = avctx->priv_data;
  399. MSS12Context *c = &ctx->c;
  400. AVFrame *frame = data;
  401. GetBitContext gb;
  402. GetByteContext gB;
  403. ArithCoder acoder;
  404. int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
  405. Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
  406. int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);
  407. av_assert0(AV_INPUT_BUFFER_PADDING_SIZE >=
  408. ARITH2_PADDING + (MIN_CACHE_BITS + 7) / 8);
  409. if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
  410. return ret;
  411. if (keyframe = get_bits1(&gb))
  412. skip_bits(&gb, 7);
  413. has_wmv9 = get_bits1(&gb);
  414. has_mv = keyframe ? 0 : get_bits1(&gb);
  415. is_rle = get_bits1(&gb);
  416. is_555 = is_rle && get_bits1(&gb);
  417. if (c->slice_split > 0)
  418. ctx->split_position = c->slice_split;
  419. else if (c->slice_split < 0) {
  420. if (get_bits1(&gb)) {
  421. if (get_bits1(&gb)) {
  422. if (get_bits1(&gb))
  423. ctx->split_position = get_bits(&gb, 16);
  424. else
  425. ctx->split_position = get_bits(&gb, 12);
  426. } else
  427. ctx->split_position = get_bits(&gb, 8) << 4;
  428. } else {
  429. if (keyframe)
  430. ctx->split_position = avctx->height / 2;
  431. }
  432. } else
  433. ctx->split_position = avctx->height;
  434. if (c->slice_split && (ctx->split_position < 1 - is_555 ||
  435. ctx->split_position > avctx->height - 1))
  436. return AVERROR_INVALIDDATA;
  437. align_get_bits(&gb);
  438. buf += get_bits_count(&gb) >> 3;
  439. buf_size -= get_bits_count(&gb) >> 3;
  440. if (buf_size < 1)
  441. return AVERROR_INVALIDDATA;
  442. if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
  443. return AVERROR_INVALIDDATA;
  444. avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;
  445. if (ctx->last_pic->format != avctx->pix_fmt)
  446. av_frame_unref(ctx->last_pic);
  447. if (has_wmv9) {
  448. bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
  449. arith2_init(&acoder, &gB);
  450. implicit_rect = !arith2_get_bit(&acoder);
  451. while (arith2_get_bit(&acoder)) {
  452. if (used_rects == MAX_WMV9_RECTANGLES)
  453. return AVERROR_INVALIDDATA;
  454. r = &wmv9rects[used_rects];
  455. if (!used_rects)
  456. r->x = arith2_get_number(&acoder, avctx->width);
  457. else
  458. r->x = arith2_get_number(&acoder, avctx->width -
  459. wmv9rects[used_rects - 1].x) +
  460. wmv9rects[used_rects - 1].x;
  461. r->y = arith2_get_number(&acoder, avctx->height);
  462. r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;
  463. r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
  464. used_rects++;
  465. }
  466. if (implicit_rect && used_rects) {
  467. av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
  468. return AVERROR_INVALIDDATA;
  469. }
  470. if (implicit_rect) {
  471. wmv9rects[0].x = 0;
  472. wmv9rects[0].y = 0;
  473. wmv9rects[0].w = avctx->width;
  474. wmv9rects[0].h = avctx->height;
  475. used_rects = 1;
  476. }
  477. for (i = 0; i < used_rects; i++) {
  478. if (!implicit_rect && arith2_get_bit(&acoder)) {
  479. av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
  480. return AVERROR_INVALIDDATA;
  481. }
  482. if (!i) {
  483. wmv9_mask = arith2_get_bit(&acoder) - 1;
  484. if (!wmv9_mask)
  485. wmv9_mask = arith2_get_number(&acoder, 256);
  486. }
  487. wmv9rects[i].coded = arith2_get_number(&acoder, 2);
  488. }
  489. buf += arith2_get_consumed_bytes(&acoder);
  490. buf_size -= arith2_get_consumed_bytes(&acoder);
  491. if (buf_size < 1)
  492. return AVERROR_INVALIDDATA;
  493. }
  494. c->mvX = c->mvY = 0;
  495. if (keyframe && !is_555) {
  496. if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
  497. return AVERROR_INVALIDDATA;
  498. buf += i;
  499. buf_size -= i;
  500. } else if (has_mv) {
  501. buf += 4;
  502. buf_size -= 4;
  503. if (buf_size < 1)
  504. return AVERROR_INVALIDDATA;
  505. c->mvX = AV_RB16(buf - 4) - avctx->width;
  506. c->mvY = AV_RB16(buf - 2) - avctx->height;
  507. }
  508. if (c->mvX < 0 || c->mvY < 0) {
  509. FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
  510. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  511. return ret;
  512. if (ctx->last_pic->data[0]) {
  513. av_assert0(frame->linesize[0] == ctx->last_pic->linesize[0]);
  514. c->last_rgb_pic = ctx->last_pic->data[0] +
  515. ctx->last_pic->linesize[0] * (avctx->height - 1);
  516. } else {
  517. av_log(avctx, AV_LOG_ERROR, "Missing keyframe\n");
  518. return AVERROR_INVALIDDATA;
  519. }
  520. } else {
  521. if ((ret = ff_reget_buffer(avctx, ctx->last_pic)) < 0)
  522. return ret;
  523. if ((ret = av_frame_ref(frame, ctx->last_pic)) < 0)
  524. return ret;
  525. c->last_rgb_pic = NULL;
  526. }
  527. c->rgb_pic = frame->data[0] +
  528. frame->linesize[0] * (avctx->height - 1);
  529. c->rgb_stride = -frame->linesize[0];
  530. frame->key_frame = keyframe;
  531. frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  532. if (is_555) {
  533. bytestream2_init(&gB, buf, buf_size);
  534. if (decode_555(&gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
  535. keyframe, avctx->width, avctx->height))
  536. return AVERROR_INVALIDDATA;
  537. buf_size -= bytestream2_tell(&gB);
  538. } else {
  539. if (keyframe) {
  540. c->corrupted = 0;
  541. ff_mss12_slicecontext_reset(&ctx->sc[0]);
  542. if (c->slice_split)
  543. ff_mss12_slicecontext_reset(&ctx->sc[1]);
  544. }
  545. if (is_rle) {
  546. if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
  547. return ret;
  548. if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
  549. c->rgb_pic, c->rgb_stride, c->pal, keyframe,
  550. ctx->split_position, 0,
  551. avctx->width, avctx->height))
  552. return ret;
  553. align_get_bits(&gb);
  554. if (c->slice_split)
  555. if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
  556. c->rgb_pic, c->rgb_stride, c->pal, keyframe,
  557. ctx->split_position, 1,
  558. avctx->width, avctx->height))
  559. return ret;
  560. align_get_bits(&gb);
  561. buf += get_bits_count(&gb) >> 3;
  562. buf_size -= get_bits_count(&gb) >> 3;
  563. } else if (!implicit_rect || wmv9_mask != -1) {
  564. 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. } else
  589. memset(c->pal_pic, 0, c->pal_stride * avctx->height);
  590. }
  591. if (has_wmv9) {
  592. for (i = 0; i < used_rects; i++) {
  593. int x = wmv9rects[i].x;
  594. int y = wmv9rects[i].y;
  595. int w = wmv9rects[i].w;
  596. int h = wmv9rects[i].h;
  597. if (wmv9rects[i].coded) {
  598. int WMV9codedFrameSize;
  599. if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
  600. return AVERROR_INVALIDDATA;
  601. if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
  602. x, y, w, h, wmv9_mask))
  603. return ret;
  604. buf += WMV9codedFrameSize + 3;
  605. buf_size -= WMV9codedFrameSize + 3;
  606. } else {
  607. uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
  608. if (wmv9_mask != -1) {
  609. ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
  610. wmv9_mask,
  611. c->pal_pic + y * c->pal_stride + x,
  612. c->pal_stride,
  613. w, h);
  614. } else {
  615. do {
  616. memset(dst, 0x80, w * 3);
  617. dst += c->rgb_stride;
  618. } while (--h);
  619. }
  620. }
  621. }
  622. }
  623. if (buf_size)
  624. av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
  625. if (c->mvX < 0 || c->mvY < 0) {
  626. av_frame_unref(ctx->last_pic);
  627. ret = av_frame_ref(ctx->last_pic, frame);
  628. if (ret < 0)
  629. return ret;
  630. }
  631. *got_frame = 1;
  632. return avpkt->size;
  633. }
  634. static av_cold int wmv9_init(AVCodecContext *avctx)
  635. {
  636. VC1Context *v = avctx->priv_data;
  637. int ret;
  638. v->s.avctx = avctx;
  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->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.qdsp.put_qpel_pixels_tab;
  670. v->s.me.qpel_avg = v->s.qdsp.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. av_frame_free(&ctx->last_pic);
  677. ff_mss12_decode_end(&ctx->c);
  678. av_freep(&ctx->c.pal_pic);
  679. av_freep(&ctx->c.last_pal_pic);
  680. ff_vc1_decode_end(avctx);
  681. return 0;
  682. }
  683. static av_cold int mss2_decode_init(AVCodecContext *avctx)
  684. {
  685. MSS2Context * const ctx = avctx->priv_data;
  686. MSS12Context *c = &ctx->c;
  687. int ret;
  688. c->avctx = avctx;
  689. if (ret = ff_mss12_decode_init(c, 1, &ctx->sc[0], &ctx->sc[1]))
  690. return ret;
  691. ctx->last_pic = av_frame_alloc();
  692. c->pal_stride = c->mask_stride;
  693. c->pal_pic = av_mallocz(c->pal_stride * avctx->height);
  694. c->last_pal_pic = av_mallocz(c->pal_stride * avctx->height);
  695. if (!c->pal_pic || !c->last_pal_pic || !ctx->last_pic) {
  696. mss2_decode_end(avctx);
  697. return AVERROR(ENOMEM);
  698. }
  699. if (ret = wmv9_init(avctx)) {
  700. mss2_decode_end(avctx);
  701. return ret;
  702. }
  703. ff_mss2dsp_init(&ctx->dsp);
  704. ff_qpeldsp_init(&ctx->qdsp);
  705. avctx->pix_fmt = c->free_colours == 127 ? AV_PIX_FMT_RGB555
  706. : AV_PIX_FMT_RGB24;
  707. return 0;
  708. }
  709. AVCodec ff_mss2_decoder = {
  710. .name = "mss2",
  711. .long_name = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
  712. .type = AVMEDIA_TYPE_VIDEO,
  713. .id = AV_CODEC_ID_MSS2,
  714. .priv_data_size = sizeof(MSS2Context),
  715. .init = mss2_decode_init,
  716. .close = mss2_decode_end,
  717. .decode = mss2_decode_frame,
  718. .capabilities = AV_CODEC_CAP_DR1,
  719. };