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.

862 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 "qpeldsp.h"
  30. #include "vc1.h"
  31. #include "wmv2data.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->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(AVCodecContext *avctx, GetByteContext *gB, uint16_t *dst, ptrdiff_t 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. if (repeat >= (INT_MAX >> 8) - 1) {
  175. av_log(avctx, AV_LOG_ERROR, "repeat overflow\n");
  176. return AVERROR_INVALIDDATA;
  177. }
  178. repeat = (repeat << 8) + bytestream2_get_byte(gB) + 1;
  179. }
  180. if (last_symbol == -2) {
  181. int skip = FFMIN((unsigned)repeat, dst + w - p);
  182. repeat -= skip;
  183. p += skip;
  184. }
  185. } else
  186. last_symbol = 127 - b;
  187. }
  188. if (last_symbol >= 0)
  189. *p = last_symbol;
  190. else if (last_symbol == -1 && prev_avail)
  191. *p = *(p - stride);
  192. } while (++p < dst + w);
  193. dst += stride;
  194. prev_avail = 1;
  195. } while (--h);
  196. return 0;
  197. }
  198. static int decode_rle(GetBitContext *gb, uint8_t *pal_dst, ptrdiff_t pal_stride,
  199. uint8_t *rgb_dst, ptrdiff_t rgb_stride, uint32_t *pal,
  200. int keyframe, int kf_slipt, int slice, int w, int h)
  201. {
  202. uint8_t bits[270] = { 0 };
  203. uint32_t codes[270];
  204. VLC vlc;
  205. int current_length = 0, read_codes = 0, next_code = 0, current_codes = 0;
  206. int remaining_codes, surplus_codes, i;
  207. const int alphabet_size = 270 - keyframe;
  208. int last_symbol = 0, repeat = 0, prev_avail = 0;
  209. if (!keyframe) {
  210. int x, y, clipw, cliph;
  211. x = get_bits(gb, 12);
  212. y = get_bits(gb, 12);
  213. clipw = get_bits(gb, 12) + 1;
  214. cliph = get_bits(gb, 12) + 1;
  215. if (x + clipw > w || y + cliph > h)
  216. return AVERROR_INVALIDDATA;
  217. pal_dst += pal_stride * y + x;
  218. rgb_dst += rgb_stride * y + x * 3;
  219. w = clipw;
  220. h = cliph;
  221. if (y)
  222. prev_avail = 1;
  223. } else {
  224. if (slice > 0) {
  225. pal_dst += pal_stride * kf_slipt;
  226. rgb_dst += rgb_stride * kf_slipt;
  227. prev_avail = 1;
  228. h -= kf_slipt;
  229. } else
  230. h = kf_slipt;
  231. }
  232. /* read explicit codes */
  233. do {
  234. while (current_codes--) {
  235. int symbol = get_bits(gb, 8);
  236. if (symbol >= 204 - keyframe)
  237. symbol += 14 - keyframe;
  238. else if (symbol > 189)
  239. symbol = get_bits1(gb) + (symbol << 1) - 190;
  240. if (bits[symbol])
  241. return AVERROR_INVALIDDATA;
  242. bits[symbol] = current_length;
  243. codes[symbol] = next_code++;
  244. read_codes++;
  245. }
  246. current_length++;
  247. next_code <<= 1;
  248. remaining_codes = (1 << current_length) - next_code;
  249. current_codes = get_bits(gb, av_ceil_log2(remaining_codes + 1));
  250. if (current_length > 22 || current_codes > remaining_codes)
  251. return AVERROR_INVALIDDATA;
  252. } while (current_codes != remaining_codes);
  253. remaining_codes = alphabet_size - read_codes;
  254. /* determine the minimum length to fit the rest of the alphabet */
  255. while ((surplus_codes = (2 << current_length) -
  256. (next_code << 1) - remaining_codes) < 0) {
  257. current_length++;
  258. next_code <<= 1;
  259. }
  260. /* add the rest of the symbols lexicographically */
  261. for (i = 0; i < alphabet_size; i++)
  262. if (!bits[i]) {
  263. if (surplus_codes-- == 0) {
  264. current_length++;
  265. next_code <<= 1;
  266. }
  267. bits[i] = current_length;
  268. codes[i] = next_code++;
  269. }
  270. if (next_code != 1 << current_length)
  271. return AVERROR_INVALIDDATA;
  272. if ((i = init_vlc(&vlc, 9, alphabet_size, bits, 1, 1, codes, 4, 4, 0)) < 0)
  273. return i;
  274. /* frame decode */
  275. do {
  276. uint8_t *pp = pal_dst;
  277. uint8_t *rp = rgb_dst;
  278. do {
  279. if (repeat-- < 1) {
  280. int b = get_vlc2(gb, vlc.table, 9, 3);
  281. if (b < 256)
  282. last_symbol = b;
  283. else if (b < 268) {
  284. b -= 256;
  285. if (b == 11)
  286. b = get_bits(gb, 4) + 10;
  287. if (!b)
  288. repeat = 0;
  289. else
  290. repeat = get_bits(gb, b);
  291. repeat += (1 << b) - 1;
  292. if (last_symbol == -2) {
  293. int skip = FFMIN(repeat, pal_dst + w - pp);
  294. repeat -= skip;
  295. pp += skip;
  296. rp += skip * 3;
  297. }
  298. } else
  299. last_symbol = 267 - b;
  300. }
  301. if (last_symbol >= 0) {
  302. *pp = last_symbol;
  303. AV_WB24(rp, pal[last_symbol]);
  304. } else if (last_symbol == -1 && prev_avail) {
  305. *pp = *(pp - pal_stride);
  306. memcpy(rp, rp - rgb_stride, 3);
  307. }
  308. rp += 3;
  309. } while (++pp < pal_dst + w);
  310. pal_dst += pal_stride;
  311. rgb_dst += rgb_stride;
  312. prev_avail = 1;
  313. } while (--h);
  314. ff_free_vlc(&vlc);
  315. return 0;
  316. }
  317. static int decode_wmv9(AVCodecContext *avctx, const uint8_t *buf, int buf_size,
  318. int x, int y, int w, int h, int wmv9_mask)
  319. {
  320. MSS2Context *ctx = avctx->priv_data;
  321. MSS12Context *c = &ctx->c;
  322. VC1Context *v = avctx->priv_data;
  323. MpegEncContext *s = &v->s;
  324. AVFrame *f;
  325. int ret;
  326. ff_mpeg_flush(avctx);
  327. if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
  328. return ret;
  329. s->loop_filter = avctx->skip_loop_filter < AVDISCARD_ALL;
  330. if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
  331. av_log(v->s.avctx, AV_LOG_ERROR, "header error\n");
  332. return AVERROR_INVALIDDATA;
  333. }
  334. if (s->pict_type != AV_PICTURE_TYPE_I) {
  335. av_log(v->s.avctx, AV_LOG_ERROR, "expected I-frame\n");
  336. return AVERROR_INVALIDDATA;
  337. }
  338. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  339. if ((ret = ff_mpv_frame_start(s, avctx)) < 0) {
  340. av_log(v->s.avctx, AV_LOG_ERROR, "ff_mpv_frame_start error\n");
  341. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  342. return ret;
  343. }
  344. ff_mpeg_er_frame_start(s);
  345. v->bits = buf_size * 8;
  346. v->end_mb_x = (w + 15) >> 4;
  347. s->end_mb_y = (h + 15) >> 4;
  348. if (v->respic & 1)
  349. v->end_mb_x = v->end_mb_x + 1 >> 1;
  350. if (v->respic & 2)
  351. s->end_mb_y = s->end_mb_y + 1 >> 1;
  352. ff_vc1_decode_blocks(v);
  353. if (v->end_mb_x == s->mb_width && s->end_mb_y == s->mb_height) {
  354. ff_er_frame_end(&s->er);
  355. } else {
  356. av_log(v->s.avctx, AV_LOG_WARNING,
  357. "disabling error correction due to block count mismatch %dx%d != %dx%d\n",
  358. v->end_mb_x, s->end_mb_y, s->mb_width, s->mb_height);
  359. }
  360. ff_mpv_frame_end(s);
  361. f = s->current_picture.f;
  362. if (v->respic == 3) {
  363. ctx->dsp.upsample_plane(f->data[0], f->linesize[0], w, h);
  364. ctx->dsp.upsample_plane(f->data[1], f->linesize[1], w+1 >> 1, h+1 >> 1);
  365. ctx->dsp.upsample_plane(f->data[2], f->linesize[2], w+1 >> 1, h+1 >> 1);
  366. } else if (v->respic)
  367. avpriv_request_sample(v->s.avctx,
  368. "Asymmetric WMV9 rectangle subsampling");
  369. av_assert0(f->linesize[1] == f->linesize[2]);
  370. if (wmv9_mask != -1)
  371. ctx->dsp.mss2_blit_wmv9_masked(c->rgb_pic + y * c->rgb_stride + x * 3,
  372. c->rgb_stride, wmv9_mask,
  373. c->pal_pic + y * c->pal_stride + x,
  374. c->pal_stride,
  375. f->data[0], f->linesize[0],
  376. f->data[1], f->data[2], f->linesize[1],
  377. w, h);
  378. else
  379. ctx->dsp.mss2_blit_wmv9(c->rgb_pic + y * c->rgb_stride + x * 3,
  380. c->rgb_stride,
  381. f->data[0], f->linesize[0],
  382. f->data[1], f->data[2], f->linesize[1],
  383. w, h);
  384. avctx->pix_fmt = AV_PIX_FMT_RGB24;
  385. return 0;
  386. }
  387. struct Rectangle {
  388. int coded, x, y, w, h;
  389. };
  390. #define MAX_WMV9_RECTANGLES 20
  391. #define ARITH2_PADDING 2
  392. static int mss2_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  393. AVPacket *avpkt)
  394. {
  395. const uint8_t *buf = avpkt->data;
  396. int buf_size = avpkt->size;
  397. MSS2Context *ctx = avctx->priv_data;
  398. MSS12Context *c = &ctx->c;
  399. AVFrame *frame = data;
  400. GetBitContext gb;
  401. GetByteContext gB;
  402. ArithCoder acoder;
  403. int keyframe, has_wmv9, has_mv, is_rle, is_555, ret;
  404. struct Rectangle wmv9rects[MAX_WMV9_RECTANGLES], *r;
  405. int used_rects = 0, i, implicit_rect = 0, av_uninit(wmv9_mask);
  406. if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
  407. return ret;
  408. if (keyframe = get_bits1(&gb))
  409. skip_bits(&gb, 7);
  410. has_wmv9 = get_bits1(&gb);
  411. has_mv = keyframe ? 0 : get_bits1(&gb);
  412. is_rle = get_bits1(&gb);
  413. is_555 = is_rle && get_bits1(&gb);
  414. if (c->slice_split > 0)
  415. ctx->split_position = c->slice_split;
  416. else if (c->slice_split < 0) {
  417. if (get_bits1(&gb)) {
  418. if (get_bits1(&gb)) {
  419. if (get_bits1(&gb))
  420. ctx->split_position = get_bits(&gb, 16);
  421. else
  422. ctx->split_position = get_bits(&gb, 12);
  423. } else
  424. ctx->split_position = get_bits(&gb, 8) << 4;
  425. } else {
  426. if (keyframe)
  427. ctx->split_position = avctx->height / 2;
  428. }
  429. } else
  430. ctx->split_position = avctx->height;
  431. if (c->slice_split && (ctx->split_position < 1 - is_555 ||
  432. ctx->split_position > avctx->height - 1))
  433. return AVERROR_INVALIDDATA;
  434. align_get_bits(&gb);
  435. buf += get_bits_count(&gb) >> 3;
  436. buf_size -= get_bits_count(&gb) >> 3;
  437. if (buf_size < 1)
  438. return AVERROR_INVALIDDATA;
  439. if (is_555 && (has_wmv9 || has_mv || c->slice_split && ctx->split_position))
  440. return AVERROR_INVALIDDATA;
  441. avctx->pix_fmt = is_555 ? AV_PIX_FMT_RGB555 : AV_PIX_FMT_RGB24;
  442. if (ctx->last_pic->format != avctx->pix_fmt)
  443. av_frame_unref(ctx->last_pic);
  444. if (has_wmv9) {
  445. bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
  446. arith2_init(&acoder, &gB);
  447. implicit_rect = !arith2_get_bit(&acoder);
  448. while (arith2_get_bit(&acoder)) {
  449. if (used_rects == MAX_WMV9_RECTANGLES)
  450. return AVERROR_INVALIDDATA;
  451. r = &wmv9rects[used_rects];
  452. if (!used_rects)
  453. r->x = arith2_get_number(&acoder, avctx->width);
  454. else
  455. r->x = arith2_get_number(&acoder, avctx->width -
  456. wmv9rects[used_rects - 1].x) +
  457. wmv9rects[used_rects - 1].x;
  458. r->y = arith2_get_number(&acoder, avctx->height);
  459. r->w = arith2_get_number(&acoder, avctx->width - r->x) + 1;
  460. r->h = arith2_get_number(&acoder, avctx->height - r->y) + 1;
  461. used_rects++;
  462. }
  463. if (implicit_rect && used_rects) {
  464. av_log(avctx, AV_LOG_ERROR, "implicit_rect && used_rects > 0\n");
  465. return AVERROR_INVALIDDATA;
  466. }
  467. if (implicit_rect) {
  468. wmv9rects[0].x = 0;
  469. wmv9rects[0].y = 0;
  470. wmv9rects[0].w = avctx->width;
  471. wmv9rects[0].h = avctx->height;
  472. used_rects = 1;
  473. }
  474. for (i = 0; i < used_rects; i++) {
  475. if (!implicit_rect && arith2_get_bit(&acoder)) {
  476. av_log(avctx, AV_LOG_ERROR, "Unexpected grandchildren\n");
  477. return AVERROR_INVALIDDATA;
  478. }
  479. if (!i) {
  480. wmv9_mask = arith2_get_bit(&acoder) - 1;
  481. if (!wmv9_mask)
  482. wmv9_mask = arith2_get_number(&acoder, 256);
  483. }
  484. wmv9rects[i].coded = arith2_get_number(&acoder, 2);
  485. }
  486. buf += arith2_get_consumed_bytes(&acoder);
  487. buf_size -= arith2_get_consumed_bytes(&acoder);
  488. if (buf_size < 1)
  489. return AVERROR_INVALIDDATA;
  490. }
  491. c->mvX = c->mvY = 0;
  492. if (keyframe && !is_555) {
  493. if ((i = decode_pal_v2(c, buf, buf_size)) < 0)
  494. return AVERROR_INVALIDDATA;
  495. buf += i;
  496. buf_size -= i;
  497. } else if (has_mv) {
  498. buf += 4;
  499. buf_size -= 4;
  500. if (buf_size < 1)
  501. return AVERROR_INVALIDDATA;
  502. c->mvX = AV_RB16(buf - 4) - avctx->width;
  503. c->mvY = AV_RB16(buf - 2) - avctx->height;
  504. }
  505. if (c->mvX < 0 || c->mvY < 0) {
  506. FFSWAP(uint8_t *, c->pal_pic, c->last_pal_pic);
  507. if ((ret = ff_get_buffer(avctx, frame, AV_GET_BUFFER_FLAG_REF)) < 0)
  508. return ret;
  509. if (ctx->last_pic->data[0]) {
  510. av_assert0(frame->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 ((ret = ff_reget_buffer(avctx, ctx->last_pic)) < 0)
  519. return ret;
  520. if ((ret = av_frame_ref(frame, ctx->last_pic)) < 0)
  521. return ret;
  522. c->last_rgb_pic = NULL;
  523. }
  524. c->rgb_pic = frame->data[0] +
  525. frame->linesize[0] * (avctx->height - 1);
  526. c->rgb_stride = -frame->linesize[0];
  527. frame->key_frame = keyframe;
  528. frame->pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  529. if (is_555) {
  530. bytestream2_init(&gB, buf, buf_size);
  531. if (decode_555(avctx, &gB, (uint16_t *)c->rgb_pic, c->rgb_stride >> 1,
  532. keyframe, avctx->width, avctx->height))
  533. return AVERROR_INVALIDDATA;
  534. buf_size -= bytestream2_tell(&gB);
  535. } else {
  536. if (keyframe) {
  537. c->corrupted = 0;
  538. ff_mss12_slicecontext_reset(&ctx->sc[0]);
  539. if (c->slice_split)
  540. ff_mss12_slicecontext_reset(&ctx->sc[1]);
  541. }
  542. if (is_rle) {
  543. if ((ret = init_get_bits8(&gb, buf, buf_size)) < 0)
  544. return ret;
  545. if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
  546. c->rgb_pic, c->rgb_stride, c->pal, keyframe,
  547. ctx->split_position, 0,
  548. avctx->width, avctx->height))
  549. return ret;
  550. align_get_bits(&gb);
  551. if (c->slice_split)
  552. if (ret = decode_rle(&gb, c->pal_pic, c->pal_stride,
  553. c->rgb_pic, c->rgb_stride, c->pal, keyframe,
  554. ctx->split_position, 1,
  555. avctx->width, avctx->height))
  556. return ret;
  557. align_get_bits(&gb);
  558. buf += get_bits_count(&gb) >> 3;
  559. buf_size -= get_bits_count(&gb) >> 3;
  560. } else if (!implicit_rect || wmv9_mask != -1) {
  561. if (c->corrupted)
  562. return AVERROR_INVALIDDATA;
  563. bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
  564. arith2_init(&acoder, &gB);
  565. c->keyframe = keyframe;
  566. if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[0], &acoder, 0, 0,
  567. avctx->width,
  568. ctx->split_position))
  569. return AVERROR_INVALIDDATA;
  570. buf += arith2_get_consumed_bytes(&acoder);
  571. buf_size -= arith2_get_consumed_bytes(&acoder);
  572. if (c->slice_split) {
  573. if (buf_size < 1)
  574. return AVERROR_INVALIDDATA;
  575. bytestream2_init(&gB, buf, buf_size + ARITH2_PADDING);
  576. arith2_init(&acoder, &gB);
  577. if (c->corrupted = ff_mss12_decode_rect(&ctx->sc[1], &acoder, 0,
  578. ctx->split_position,
  579. avctx->width,
  580. avctx->height - ctx->split_position))
  581. return AVERROR_INVALIDDATA;
  582. buf += arith2_get_consumed_bytes(&acoder);
  583. buf_size -= arith2_get_consumed_bytes(&acoder);
  584. }
  585. } else
  586. memset(c->pal_pic, 0, c->pal_stride * avctx->height);
  587. }
  588. if (has_wmv9) {
  589. for (i = 0; i < used_rects; i++) {
  590. int x = wmv9rects[i].x;
  591. int y = wmv9rects[i].y;
  592. int w = wmv9rects[i].w;
  593. int h = wmv9rects[i].h;
  594. if (wmv9rects[i].coded) {
  595. int WMV9codedFrameSize;
  596. if (buf_size < 4 || !(WMV9codedFrameSize = AV_RL24(buf)))
  597. return AVERROR_INVALIDDATA;
  598. if (ret = decode_wmv9(avctx, buf + 3, buf_size - 3,
  599. x, y, w, h, wmv9_mask))
  600. return ret;
  601. buf += WMV9codedFrameSize + 3;
  602. buf_size -= WMV9codedFrameSize + 3;
  603. } else {
  604. uint8_t *dst = c->rgb_pic + y * c->rgb_stride + x * 3;
  605. if (wmv9_mask != -1) {
  606. ctx->dsp.mss2_gray_fill_masked(dst, c->rgb_stride,
  607. wmv9_mask,
  608. c->pal_pic + y * c->pal_stride + x,
  609. c->pal_stride,
  610. w, h);
  611. } else {
  612. do {
  613. memset(dst, 0x80, w * 3);
  614. dst += c->rgb_stride;
  615. } while (--h);
  616. }
  617. }
  618. }
  619. }
  620. if (buf_size)
  621. av_log(avctx, AV_LOG_WARNING, "buffer not fully consumed\n");
  622. if (c->mvX < 0 || c->mvY < 0) {
  623. av_frame_unref(ctx->last_pic);
  624. ret = av_frame_ref(ctx->last_pic, frame);
  625. if (ret < 0)
  626. return ret;
  627. }
  628. *got_frame = 1;
  629. return avpkt->size;
  630. }
  631. static av_cold int wmv9_init(AVCodecContext *avctx)
  632. {
  633. VC1Context *v = avctx->priv_data;
  634. int ret;
  635. v->s.avctx = avctx;
  636. if ((ret = ff_vc1_init_common(v)) < 0)
  637. return ret;
  638. ff_vc1dsp_init(&v->vc1dsp);
  639. v->profile = PROFILE_MAIN;
  640. v->zz_8x4 = ff_wmv2_scantableA;
  641. v->zz_4x8 = ff_wmv2_scantableB;
  642. v->res_y411 = 0;
  643. v->res_sprite = 0;
  644. v->frmrtq_postproc = 7;
  645. v->bitrtq_postproc = 31;
  646. v->res_x8 = 0;
  647. v->multires = 0;
  648. v->res_fasttx = 1;
  649. v->fastuvmc = 0;
  650. v->extended_mv = 0;
  651. v->dquant = 1;
  652. v->vstransform = 1;
  653. v->res_transtab = 0;
  654. v->overlap = 0;
  655. v->resync_marker = 0;
  656. v->rangered = 0;
  657. v->s.max_b_frames = avctx->max_b_frames = 0;
  658. v->quantizer_mode = 0;
  659. v->finterpflag = 0;
  660. v->res_rtm_flag = 1;
  661. ff_vc1_init_transposed_scantables(v);
  662. if ((ret = ff_msmpeg4_decode_init(avctx)) < 0 ||
  663. (ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
  664. return ret;
  665. /* error concealment */
  666. v->s.me.qpel_put = v->s.qdsp.put_qpel_pixels_tab;
  667. v->s.me.qpel_avg = v->s.qdsp.avg_qpel_pixels_tab;
  668. return 0;
  669. }
  670. static av_cold int mss2_decode_end(AVCodecContext *avctx)
  671. {
  672. MSS2Context *const ctx = avctx->priv_data;
  673. av_frame_free(&ctx->last_pic);
  674. ff_mss12_decode_end(&ctx->c);
  675. av_freep(&ctx->c.pal_pic);
  676. av_freep(&ctx->c.last_pal_pic);
  677. ff_vc1_decode_end(avctx);
  678. return 0;
  679. }
  680. static av_cold int mss2_decode_init(AVCodecContext *avctx)
  681. {
  682. MSS2Context * const ctx = avctx->priv_data;
  683. MSS12Context *c = &ctx->c;
  684. int ret;
  685. c->avctx = avctx;
  686. if (ret = ff_mss12_decode_init(c, 1, &ctx->sc[0], &ctx->sc[1]))
  687. return ret;
  688. ctx->last_pic = av_frame_alloc();
  689. c->pal_stride = c->mask_stride;
  690. c->pal_pic = av_mallocz(c->pal_stride * avctx->height);
  691. c->last_pal_pic = av_mallocz(c->pal_stride * avctx->height);
  692. if (!c->pal_pic || !c->last_pal_pic || !ctx->last_pic) {
  693. mss2_decode_end(avctx);
  694. return AVERROR(ENOMEM);
  695. }
  696. if (ret = wmv9_init(avctx)) {
  697. mss2_decode_end(avctx);
  698. return ret;
  699. }
  700. ff_mss2dsp_init(&ctx->dsp);
  701. ff_qpeldsp_init(&ctx->qdsp);
  702. avctx->pix_fmt = c->free_colours == 127 ? AV_PIX_FMT_RGB555
  703. : AV_PIX_FMT_RGB24;
  704. return 0;
  705. }
  706. AVCodec ff_mss2_decoder = {
  707. .name = "mss2",
  708. .long_name = NULL_IF_CONFIG_SMALL("MS Windows Media Video V9 Screen"),
  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 = AV_CODEC_CAP_DR1,
  716. };