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.

677 lines
20KB

  1. /*
  2. * Copyright (c) 2012 Konstantin Shishkov
  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. * Common functions for Microsoft Screen 1 and 2
  23. */
  24. #include <inttypes.h>
  25. #include "libavutil/intfloat.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "avcodec.h"
  28. #include "mss12.h"
  29. enum SplitMode {
  30. SPLIT_VERT = 0,
  31. SPLIT_HOR,
  32. SPLIT_NONE
  33. };
  34. static const int sec_order_sizes[4] = { 1, 7, 6, 1 };
  35. enum ContextDirection {
  36. TOP_LEFT = 0,
  37. TOP,
  38. TOP_RIGHT,
  39. LEFT
  40. };
  41. static int model_calc_threshold(Model *m)
  42. {
  43. int thr;
  44. thr = 2 * m->weights[m->num_syms] - 1;
  45. thr = ((thr >> 1) + 4 * m->cum_prob[0]) / thr;
  46. return FFMIN(thr, 0x3FFF);
  47. }
  48. static void model_reset(Model *m)
  49. {
  50. int i;
  51. for (i = 0; i <= m->num_syms; i++) {
  52. m->weights[i] = 1;
  53. m->cum_prob[i] = m->num_syms - i;
  54. }
  55. m->weights[0] = 0;
  56. for (i = 0; i < m->num_syms; i++)
  57. m->idx2sym[i + 1] = i;
  58. }
  59. static av_cold void model_init(Model *m, int num_syms, int thr_weight)
  60. {
  61. m->num_syms = num_syms;
  62. m->thr_weight = thr_weight;
  63. m->threshold = num_syms * thr_weight;
  64. }
  65. static void model_rescale_weights(Model *m)
  66. {
  67. int i;
  68. int cum_prob;
  69. if (m->thr_weight == THRESH_ADAPTIVE)
  70. m->threshold = model_calc_threshold(m);
  71. while (m->cum_prob[0] > m->threshold) {
  72. cum_prob = 0;
  73. for (i = m->num_syms; i >= 0; i--) {
  74. m->cum_prob[i] = cum_prob;
  75. m->weights[i] = (m->weights[i] + 1) >> 1;
  76. cum_prob += m->weights[i];
  77. }
  78. }
  79. }
  80. void ff_mss12_model_update(Model *m, int val)
  81. {
  82. int i;
  83. if (m->weights[val] == m->weights[val - 1]) {
  84. for (i = val; m->weights[i - 1] == m->weights[val]; i--);
  85. if (i != val) {
  86. int sym1, sym2;
  87. sym1 = m->idx2sym[val];
  88. sym2 = m->idx2sym[i];
  89. m->idx2sym[val] = sym2;
  90. m->idx2sym[i] = sym1;
  91. val = i;
  92. }
  93. }
  94. m->weights[val]++;
  95. for (i = val - 1; i >= 0; i--)
  96. m->cum_prob[i]++;
  97. model_rescale_weights(m);
  98. }
  99. static void pixctx_reset(PixContext *ctx)
  100. {
  101. int i, j;
  102. if (!ctx->special_initial_cache)
  103. for (i = 0; i < ctx->cache_size; i++)
  104. ctx->cache[i] = i;
  105. else {
  106. ctx->cache[0] = 1;
  107. ctx->cache[1] = 2;
  108. ctx->cache[2] = 4;
  109. }
  110. model_reset(&ctx->cache_model);
  111. model_reset(&ctx->full_model);
  112. for (i = 0; i < 15; i++)
  113. for (j = 0; j < 4; j++)
  114. model_reset(&ctx->sec_models[i][j]);
  115. }
  116. static av_cold void pixctx_init(PixContext *ctx, int cache_size,
  117. int full_model_syms, int special_initial_cache)
  118. {
  119. int i, j, k, idx;
  120. ctx->cache_size = cache_size + 4;
  121. ctx->num_syms = cache_size;
  122. ctx->special_initial_cache = special_initial_cache;
  123. model_init(&ctx->cache_model, ctx->num_syms + 1, THRESH_LOW);
  124. model_init(&ctx->full_model, full_model_syms, THRESH_HIGH);
  125. for (i = 0, idx = 0; i < 4; i++)
  126. for (j = 0; j < sec_order_sizes[i]; j++, idx++)
  127. for (k = 0; k < 4; k++)
  128. model_init(&ctx->sec_models[idx][k], 2 + i,
  129. i ? THRESH_LOW : THRESH_ADAPTIVE);
  130. }
  131. static av_always_inline int decode_pixel(ArithCoder *acoder, PixContext *pctx,
  132. uint8_t *ngb, int num_ngb, int any_ngb)
  133. {
  134. int i, val, pix;
  135. val = acoder->get_model_sym(acoder, &pctx->cache_model);
  136. if (val < pctx->num_syms) {
  137. if (any_ngb) {
  138. int idx, j;
  139. idx = 0;
  140. for (i = 0; i < pctx->cache_size; i++) {
  141. for (j = 0; j < num_ngb; j++)
  142. if (pctx->cache[i] == ngb[j])
  143. break;
  144. if (j == num_ngb) {
  145. if (idx == val)
  146. break;
  147. idx++;
  148. }
  149. }
  150. val = FFMIN(i, pctx->cache_size - 1);
  151. }
  152. pix = pctx->cache[val];
  153. } else {
  154. pix = acoder->get_model_sym(acoder, &pctx->full_model);
  155. for (i = 0; i < pctx->cache_size - 1; i++)
  156. if (pctx->cache[i] == pix)
  157. break;
  158. val = i;
  159. }
  160. if (val) {
  161. for (i = val; i > 0; i--)
  162. pctx->cache[i] = pctx->cache[i - 1];
  163. pctx->cache[0] = pix;
  164. }
  165. return pix;
  166. }
  167. static int decode_pixel_in_context(ArithCoder *acoder, PixContext *pctx,
  168. uint8_t *src, int stride, int x, int y,
  169. int has_right)
  170. {
  171. uint8_t neighbours[4];
  172. uint8_t ref_pix[4];
  173. int nlen;
  174. int layer = 0, sub;
  175. int pix;
  176. int i, j;
  177. if (!y) {
  178. memset(neighbours, src[-1], 4);
  179. } else {
  180. neighbours[TOP] = src[-stride];
  181. if (!x) {
  182. neighbours[TOP_LEFT] = neighbours[LEFT] = neighbours[TOP];
  183. } else {
  184. neighbours[TOP_LEFT] = src[-stride - 1];
  185. neighbours[ LEFT] = src[-1];
  186. }
  187. if (has_right)
  188. neighbours[TOP_RIGHT] = src[-stride + 1];
  189. else
  190. neighbours[TOP_RIGHT] = neighbours[TOP];
  191. }
  192. sub = 0;
  193. if (x >= 2 && src[-2] == neighbours[LEFT])
  194. sub = 1;
  195. if (y >= 2 && src[-2 * stride] == neighbours[TOP])
  196. sub |= 2;
  197. nlen = 1;
  198. ref_pix[0] = neighbours[0];
  199. for (i = 1; i < 4; i++) {
  200. for (j = 0; j < nlen; j++)
  201. if (ref_pix[j] == neighbours[i])
  202. break;
  203. if (j == nlen)
  204. ref_pix[nlen++] = neighbours[i];
  205. }
  206. switch (nlen) {
  207. case 1:
  208. layer = 0;
  209. break;
  210. case 2:
  211. if (neighbours[TOP] == neighbours[TOP_LEFT]) {
  212. if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
  213. layer = 1;
  214. else if (neighbours[LEFT] == neighbours[TOP_LEFT])
  215. layer = 2;
  216. else
  217. layer = 3;
  218. } else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT]) {
  219. if (neighbours[LEFT] == neighbours[TOP_LEFT])
  220. layer = 4;
  221. else
  222. layer = 5;
  223. } else if (neighbours[LEFT] == neighbours[TOP_LEFT]) {
  224. layer = 6;
  225. } else {
  226. layer = 7;
  227. }
  228. break;
  229. case 3:
  230. if (neighbours[TOP] == neighbours[TOP_LEFT])
  231. layer = 8;
  232. else if (neighbours[TOP_RIGHT] == neighbours[TOP_LEFT])
  233. layer = 9;
  234. else if (neighbours[LEFT] == neighbours[TOP_LEFT])
  235. layer = 10;
  236. else if (neighbours[TOP_RIGHT] == neighbours[TOP])
  237. layer = 11;
  238. else if (neighbours[TOP] == neighbours[LEFT])
  239. layer = 12;
  240. else
  241. layer = 13;
  242. break;
  243. case 4:
  244. layer = 14;
  245. break;
  246. }
  247. pix = acoder->get_model_sym(acoder,
  248. &pctx->sec_models[layer][sub]);
  249. if (pix < nlen)
  250. return ref_pix[pix];
  251. else
  252. return decode_pixel(acoder, pctx, ref_pix, nlen, 1);
  253. }
  254. static int decode_region(ArithCoder *acoder, uint8_t *dst, uint8_t *rgb_pic,
  255. int x, int y, int width, int height, int stride,
  256. int rgb_stride, PixContext *pctx, const uint32_t *pal)
  257. {
  258. int i, j, p;
  259. uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
  260. dst += x + y * stride;
  261. for (j = 0; j < height; j++) {
  262. for (i = 0; i < width; i++) {
  263. if (!i && !j)
  264. p = decode_pixel(acoder, pctx, NULL, 0, 0);
  265. else
  266. p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
  267. i, j, width - i - 1);
  268. dst[i] = p;
  269. if (rgb_pic)
  270. AV_WB24(rgb_dst + i * 3, pal[p]);
  271. }
  272. dst += stride;
  273. rgb_dst += rgb_stride;
  274. }
  275. return 0;
  276. }
  277. static void copy_rectangles(MSS12Context const *c,
  278. int x, int y, int width, int height)
  279. {
  280. int j;
  281. if (c->last_rgb_pic)
  282. for (j = y; j < y + height; j++) {
  283. memcpy(c->rgb_pic + j * c->rgb_stride + x * 3,
  284. c->last_rgb_pic + j * c->rgb_stride + x * 3,
  285. width * 3);
  286. memcpy(c->pal_pic + j * c->pal_stride + x,
  287. c->last_pal_pic + j * c->pal_stride + x,
  288. width);
  289. }
  290. }
  291. static int motion_compensation(MSS12Context const *c,
  292. int x, int y, int width, int height)
  293. {
  294. if (x + c->mvX < 0 || x + c->mvX + width > c->avctx->width ||
  295. y + c->mvY < 0 || y + c->mvY + height > c->avctx->height ||
  296. !c->rgb_pic)
  297. return -1;
  298. else {
  299. uint8_t *dst = c->pal_pic + x + y * c->pal_stride;
  300. uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
  301. uint8_t *src;
  302. uint8_t *rgb_src;
  303. int j;
  304. x += c->mvX;
  305. y += c->mvY;
  306. if (c->last_rgb_pic) {
  307. src = c->last_pal_pic + x + y * c->pal_stride;
  308. rgb_src = c->last_rgb_pic + x * 3 + y * c->rgb_stride;
  309. } else {
  310. src = c->pal_pic + x + y * c->pal_stride;
  311. rgb_src = c->rgb_pic + x * 3 + y * c->rgb_stride;
  312. }
  313. for (j = 0; j < height; j++) {
  314. memmove(dst, src, width);
  315. memmove(rgb_dst, rgb_src, width * 3);
  316. dst += c->pal_stride;
  317. src += c->pal_stride;
  318. rgb_dst += c->rgb_stride;
  319. rgb_src += c->rgb_stride;
  320. }
  321. }
  322. return 0;
  323. }
  324. static int decode_region_masked(MSS12Context const *c, ArithCoder *acoder,
  325. uint8_t *dst, int stride, uint8_t *mask,
  326. int mask_stride, int x, int y,
  327. int width, int height,
  328. PixContext *pctx)
  329. {
  330. int i, j, p;
  331. uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
  332. dst += x + y * stride;
  333. mask += x + y * mask_stride;
  334. for (j = 0; j < height; j++) {
  335. for (i = 0; i < width; i++) {
  336. if (c->avctx->err_recognition & AV_EF_EXPLODE &&
  337. ( c->rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
  338. !c->rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
  339. return -1;
  340. if (mask[i] == 0x02) {
  341. copy_rectangles(c, x + i, y + j, 1, 1);
  342. } else if (mask[i] == 0x04) {
  343. if (motion_compensation(c, x + i, y + j, 1, 1))
  344. return -1;
  345. } else if (mask[i] != 0x80) {
  346. if (!i && !j)
  347. p = decode_pixel(acoder, pctx, NULL, 0, 0);
  348. else
  349. p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
  350. i, j, width - i - 1);
  351. dst[i] = p;
  352. if (c->rgb_pic)
  353. AV_WB24(rgb_dst + i * 3, c->pal[p]);
  354. }
  355. }
  356. dst += stride;
  357. mask += mask_stride;
  358. rgb_dst += c->rgb_stride;
  359. }
  360. return 0;
  361. }
  362. static av_cold void slicecontext_init(SliceContext *sc,
  363. int version, int full_model_syms)
  364. {
  365. model_init(&sc->intra_region, 2, THRESH_ADAPTIVE);
  366. model_init(&sc->inter_region, 2, THRESH_ADAPTIVE);
  367. model_init(&sc->split_mode, 3, THRESH_HIGH);
  368. model_init(&sc->edge_mode, 2, THRESH_HIGH);
  369. model_init(&sc->pivot, 3, THRESH_LOW);
  370. pixctx_init(&sc->intra_pix_ctx, 8, full_model_syms, 0);
  371. pixctx_init(&sc->inter_pix_ctx, version ? 3 : 2,
  372. full_model_syms, version ? 1 : 0);
  373. }
  374. void ff_mss12_slicecontext_reset(SliceContext *sc)
  375. {
  376. model_reset(&sc->intra_region);
  377. model_reset(&sc->inter_region);
  378. model_reset(&sc->split_mode);
  379. model_reset(&sc->edge_mode);
  380. model_reset(&sc->pivot);
  381. pixctx_reset(&sc->intra_pix_ctx);
  382. pixctx_reset(&sc->inter_pix_ctx);
  383. }
  384. static int decode_pivot(SliceContext *sc, ArithCoder *acoder, int base)
  385. {
  386. int val, inv;
  387. inv = acoder->get_model_sym(acoder, &sc->edge_mode);
  388. val = acoder->get_model_sym(acoder, &sc->pivot) + 1;
  389. if (val > 2) {
  390. if ((base + 1) / 2 - 2 <= 0)
  391. return -1;
  392. val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
  393. }
  394. if (val >= base)
  395. return -1;
  396. return inv ? base - val : val;
  397. }
  398. static int decode_region_intra(SliceContext *sc, ArithCoder *acoder,
  399. int x, int y, int width, int height)
  400. {
  401. MSS12Context const *c = sc->c;
  402. int mode;
  403. mode = acoder->get_model_sym(acoder, &sc->intra_region);
  404. if (!mode) {
  405. int i, j, pix, rgb_pix;
  406. int stride = c->pal_stride;
  407. int rgb_stride = c->rgb_stride;
  408. uint8_t *dst = c->pal_pic + x + y * stride;
  409. uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * rgb_stride;
  410. pix = decode_pixel(acoder, &sc->intra_pix_ctx, NULL, 0, 0);
  411. rgb_pix = c->pal[pix];
  412. for (i = 0; i < height; i++, dst += stride, rgb_dst += rgb_stride) {
  413. memset(dst, pix, width);
  414. if (c->rgb_pic)
  415. for (j = 0; j < width * 3; j += 3)
  416. AV_WB24(rgb_dst + j, rgb_pix);
  417. }
  418. } else {
  419. return decode_region(acoder, c->pal_pic, c->rgb_pic,
  420. x, y, width, height, c->pal_stride, c->rgb_stride,
  421. &sc->intra_pix_ctx, &c->pal[0]);
  422. }
  423. return 0;
  424. }
  425. static int decode_region_inter(SliceContext *sc, ArithCoder *acoder,
  426. int x, int y, int width, int height)
  427. {
  428. MSS12Context const *c = sc->c;
  429. int mode;
  430. mode = acoder->get_model_sym(acoder, &sc->inter_region);
  431. if (!mode) {
  432. mode = decode_pixel(acoder, &sc->inter_pix_ctx, NULL, 0, 0);
  433. if (c->avctx->err_recognition & AV_EF_EXPLODE &&
  434. ( c->rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
  435. !c->rgb_pic && mode != 0x80 && mode != 0xFF))
  436. return -1;
  437. if (mode == 0x02)
  438. copy_rectangles(c, x, y, width, height);
  439. else if (mode == 0x04)
  440. return motion_compensation(c, x, y, width, height);
  441. else if (mode != 0x80)
  442. return decode_region_intra(sc, acoder, x, y, width, height);
  443. } else {
  444. if (decode_region(acoder, c->mask, NULL,
  445. x, y, width, height, c->mask_stride, 0,
  446. &sc->inter_pix_ctx, &c->pal[0]) < 0)
  447. return -1;
  448. return decode_region_masked(c, acoder, c->pal_pic,
  449. c->pal_stride, c->mask,
  450. c->mask_stride,
  451. x, y, width, height,
  452. &sc->intra_pix_ctx);
  453. }
  454. return 0;
  455. }
  456. int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder,
  457. int x, int y, int width, int height)
  458. {
  459. int mode, pivot;
  460. mode = acoder->get_model_sym(acoder, &sc->split_mode);
  461. switch (mode) {
  462. case SPLIT_VERT:
  463. if ((pivot = decode_pivot(sc, acoder, height)) < 1)
  464. return -1;
  465. if (ff_mss12_decode_rect(sc, acoder, x, y, width, pivot))
  466. return -1;
  467. if (ff_mss12_decode_rect(sc, acoder, x, y + pivot, width, height - pivot))
  468. return -1;
  469. break;
  470. case SPLIT_HOR:
  471. if ((pivot = decode_pivot(sc, acoder, width)) < 1)
  472. return -1;
  473. if (ff_mss12_decode_rect(sc, acoder, x, y, pivot, height))
  474. return -1;
  475. if (ff_mss12_decode_rect(sc, acoder, x + pivot, y, width - pivot, height))
  476. return -1;
  477. break;
  478. case SPLIT_NONE:
  479. if (sc->c->keyframe)
  480. return decode_region_intra(sc, acoder, x, y, width, height);
  481. else
  482. return decode_region_inter(sc, acoder, x, y, width, height);
  483. default:
  484. return -1;
  485. }
  486. return 0;
  487. }
  488. av_cold int ff_mss12_decode_init(MSS12Context *c, int version,
  489. SliceContext* sc1, SliceContext *sc2)
  490. {
  491. AVCodecContext *avctx = c->avctx;
  492. int i;
  493. if (avctx->extradata_size < 52 + 256 * 3) {
  494. av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
  495. avctx->extradata_size);
  496. return AVERROR_INVALIDDATA;
  497. }
  498. if (AV_RB32(avctx->extradata) < avctx->extradata_size) {
  499. av_log(avctx, AV_LOG_ERROR,
  500. "Insufficient extradata size: expected %"PRIu32" got %d\n",
  501. AV_RB32(avctx->extradata),
  502. avctx->extradata_size);
  503. return AVERROR_INVALIDDATA;
  504. }
  505. avctx->coded_width = AV_RB32(avctx->extradata + 20);
  506. avctx->coded_height = AV_RB32(avctx->extradata + 24);
  507. if (avctx->coded_width > 4096 || avctx->coded_height > 4096) {
  508. av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too large",
  509. avctx->coded_width, avctx->coded_height);
  510. return AVERROR_INVALIDDATA;
  511. }
  512. av_log(avctx, AV_LOG_DEBUG, "Encoder version %"PRIu32".%"PRIu32"\n",
  513. AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
  514. if (version != AV_RB32(avctx->extradata + 4) > 1) {
  515. av_log(avctx, AV_LOG_ERROR,
  516. "Header version doesn't match codec tag\n");
  517. return -1;
  518. }
  519. c->free_colours = AV_RB32(avctx->extradata + 48);
  520. if ((unsigned)c->free_colours > 256) {
  521. av_log(avctx, AV_LOG_ERROR,
  522. "Incorrect number of changeable palette entries: %d\n",
  523. c->free_colours);
  524. return AVERROR_INVALIDDATA;
  525. }
  526. av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);
  527. av_log(avctx, AV_LOG_DEBUG, "Display dimensions %"PRIu32"x%"PRIu32"\n",
  528. AV_RB32(avctx->extradata + 12), AV_RB32(avctx->extradata + 16));
  529. av_log(avctx, AV_LOG_DEBUG, "Coded dimensions %dx%d\n",
  530. avctx->coded_width, avctx->coded_height);
  531. av_log(avctx, AV_LOG_DEBUG, "%g frames per second\n",
  532. av_int2float(AV_RB32(avctx->extradata + 28)));
  533. av_log(avctx, AV_LOG_DEBUG, "Bitrate %"PRIu32" bps\n",
  534. AV_RB32(avctx->extradata + 32));
  535. av_log(avctx, AV_LOG_DEBUG, "Max. lead time %g ms\n",
  536. av_int2float(AV_RB32(avctx->extradata + 36)));
  537. av_log(avctx, AV_LOG_DEBUG, "Max. lag time %g ms\n",
  538. av_int2float(AV_RB32(avctx->extradata + 40)));
  539. av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
  540. av_int2float(AV_RB32(avctx->extradata + 44)));
  541. if (version) {
  542. if (avctx->extradata_size < 60 + 256 * 3) {
  543. av_log(avctx, AV_LOG_ERROR,
  544. "Insufficient extradata size %d for v2\n",
  545. avctx->extradata_size);
  546. return AVERROR_INVALIDDATA;
  547. }
  548. c->slice_split = AV_RB32(avctx->extradata + 52);
  549. av_log(avctx, AV_LOG_DEBUG, "Slice split %d\n", c->slice_split);
  550. c->full_model_syms = AV_RB32(avctx->extradata + 56);
  551. if (c->full_model_syms < 2 || c->full_model_syms > 256) {
  552. av_log(avctx, AV_LOG_ERROR,
  553. "Incorrect number of used colours %d\n",
  554. c->full_model_syms);
  555. return AVERROR_INVALIDDATA;
  556. }
  557. av_log(avctx, AV_LOG_DEBUG, "Used colours %d\n",
  558. c->full_model_syms);
  559. } else {
  560. c->slice_split = 0;
  561. c->full_model_syms = 256;
  562. }
  563. for (i = 0; i < 256; i++)
  564. c->pal[i] = AV_RB24(avctx->extradata + 52 +
  565. (version ? 8 : 0) + i * 3);
  566. c->mask_stride = FFALIGN(avctx->width, 16);
  567. c->mask = av_malloc(c->mask_stride * avctx->height);
  568. if (!c->mask) {
  569. av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
  570. return AVERROR(ENOMEM);
  571. }
  572. sc1->c = c;
  573. slicecontext_init(sc1, version, c->full_model_syms);
  574. if (c->slice_split) {
  575. sc2->c = c;
  576. slicecontext_init(sc2, version, c->full_model_syms);
  577. }
  578. c->corrupted = 1;
  579. return 0;
  580. }
  581. av_cold int ff_mss12_decode_end(MSS12Context *c)
  582. {
  583. av_freep(&c->mask);
  584. return 0;
  585. }