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.

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