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.

688 lines
21KB

  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, ptrdiff_t 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, ptrdiff_t stride,
  256. ptrdiff_t rgb_stride, PixContext *pctx,
  257. const uint32_t *pal)
  258. {
  259. int i, j, p;
  260. uint8_t *rgb_dst = rgb_pic + x * 3 + y * rgb_stride;
  261. dst += x + y * stride;
  262. for (j = 0; j < height; j++) {
  263. for (i = 0; i < width; i++) {
  264. if (!i && !j)
  265. p = decode_pixel(acoder, pctx, NULL, 0, 0);
  266. else
  267. p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
  268. i, j, width - i - 1);
  269. dst[i] = p;
  270. if (rgb_pic)
  271. AV_WB24(rgb_dst + i * 3, pal[p]);
  272. }
  273. dst += stride;
  274. rgb_dst += rgb_stride;
  275. }
  276. return 0;
  277. }
  278. static void copy_rectangles(MSS12Context const *c,
  279. int x, int y, int width, int height)
  280. {
  281. int j;
  282. if (c->last_rgb_pic)
  283. for (j = y; j < y + height; j++) {
  284. memcpy(c->rgb_pic + j * c->rgb_stride + x * 3,
  285. c->last_rgb_pic + j * c->rgb_stride + x * 3,
  286. width * 3);
  287. memcpy(c->pal_pic + j * c->pal_stride + x,
  288. c->last_pal_pic + j * c->pal_stride + x,
  289. width);
  290. }
  291. }
  292. static int motion_compensation(MSS12Context const *c,
  293. int x, int y, int width, int height)
  294. {
  295. if (x + c->mvX < 0 || x + c->mvX + width > c->avctx->width ||
  296. y + c->mvY < 0 || y + c->mvY + height > c->avctx->height ||
  297. !c->rgb_pic)
  298. return -1;
  299. else {
  300. uint8_t *dst = c->pal_pic + x + y * c->pal_stride;
  301. uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
  302. uint8_t *src;
  303. uint8_t *rgb_src;
  304. int j;
  305. x += c->mvX;
  306. y += c->mvY;
  307. if (c->last_rgb_pic) {
  308. src = c->last_pal_pic + x + y * c->pal_stride;
  309. rgb_src = c->last_rgb_pic + x * 3 + y * c->rgb_stride;
  310. } else {
  311. src = c->pal_pic + x + y * c->pal_stride;
  312. rgb_src = c->rgb_pic + x * 3 + y * c->rgb_stride;
  313. }
  314. for (j = 0; j < height; j++) {
  315. memmove(dst, src, width);
  316. memmove(rgb_dst, rgb_src, width * 3);
  317. dst += c->pal_stride;
  318. src += c->pal_stride;
  319. rgb_dst += c->rgb_stride;
  320. rgb_src += c->rgb_stride;
  321. }
  322. }
  323. return 0;
  324. }
  325. static int decode_region_masked(MSS12Context const *c, ArithCoder *acoder,
  326. uint8_t *dst, ptrdiff_t stride, uint8_t *mask,
  327. ptrdiff_t mask_stride, int x, int y,
  328. int width, int height,
  329. PixContext *pctx)
  330. {
  331. int i, j, p;
  332. uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * c->rgb_stride;
  333. dst += x + y * stride;
  334. mask += x + y * mask_stride;
  335. for (j = 0; j < height; j++) {
  336. for (i = 0; i < width; i++) {
  337. if (c->avctx->err_recognition & AV_EF_EXPLODE &&
  338. ( c->rgb_pic && mask[i] != 0x01 && mask[i] != 0x02 && mask[i] != 0x04 ||
  339. !c->rgb_pic && mask[i] != 0x80 && mask[i] != 0xFF))
  340. return -1;
  341. if (mask[i] == 0x02) {
  342. copy_rectangles(c, x + i, y + j, 1, 1);
  343. } else if (mask[i] == 0x04) {
  344. if (motion_compensation(c, x + i, y + j, 1, 1))
  345. return -1;
  346. } else if (mask[i] != 0x80) {
  347. if (!i && !j)
  348. p = decode_pixel(acoder, pctx, NULL, 0, 0);
  349. else
  350. p = decode_pixel_in_context(acoder, pctx, dst + i, stride,
  351. i, j, width - i - 1);
  352. dst[i] = p;
  353. if (c->rgb_pic)
  354. AV_WB24(rgb_dst + i * 3, c->pal[p]);
  355. }
  356. }
  357. dst += stride;
  358. mask += mask_stride;
  359. rgb_dst += c->rgb_stride;
  360. }
  361. return 0;
  362. }
  363. static av_cold void slicecontext_init(SliceContext *sc,
  364. int version, int full_model_syms)
  365. {
  366. model_init(&sc->intra_region, 2, THRESH_ADAPTIVE);
  367. model_init(&sc->inter_region, 2, THRESH_ADAPTIVE);
  368. model_init(&sc->split_mode, 3, THRESH_HIGH);
  369. model_init(&sc->edge_mode, 2, THRESH_HIGH);
  370. model_init(&sc->pivot, 3, THRESH_LOW);
  371. pixctx_init(&sc->intra_pix_ctx, 8, full_model_syms, 0);
  372. pixctx_init(&sc->inter_pix_ctx, version ? 3 : 2,
  373. full_model_syms, version ? 1 : 0);
  374. }
  375. void ff_mss12_slicecontext_reset(SliceContext *sc)
  376. {
  377. model_reset(&sc->intra_region);
  378. model_reset(&sc->inter_region);
  379. model_reset(&sc->split_mode);
  380. model_reset(&sc->edge_mode);
  381. model_reset(&sc->pivot);
  382. pixctx_reset(&sc->intra_pix_ctx);
  383. pixctx_reset(&sc->inter_pix_ctx);
  384. }
  385. static int decode_pivot(SliceContext *sc, ArithCoder *acoder, int base)
  386. {
  387. int val, inv;
  388. inv = acoder->get_model_sym(acoder, &sc->edge_mode);
  389. val = acoder->get_model_sym(acoder, &sc->pivot) + 1;
  390. if (val > 2) {
  391. if ((base + 1) / 2 - 2 <= 0)
  392. return -1;
  393. val = acoder->get_number(acoder, (base + 1) / 2 - 2) + 3;
  394. }
  395. if (val >= base)
  396. return -1;
  397. return inv ? base - val : val;
  398. }
  399. static int decode_region_intra(SliceContext *sc, ArithCoder *acoder,
  400. int x, int y, int width, int height)
  401. {
  402. MSS12Context const *c = sc->c;
  403. int mode;
  404. mode = acoder->get_model_sym(acoder, &sc->intra_region);
  405. if (!mode) {
  406. int i, j, pix, rgb_pix;
  407. ptrdiff_t stride = c->pal_stride;
  408. ptrdiff_t rgb_stride = c->rgb_stride;
  409. uint8_t *dst = c->pal_pic + x + y * stride;
  410. uint8_t *rgb_dst = c->rgb_pic + x * 3 + y * rgb_stride;
  411. pix = decode_pixel(acoder, &sc->intra_pix_ctx, NULL, 0, 0);
  412. rgb_pix = c->pal[pix];
  413. for (i = 0; i < height; i++, dst += stride, rgb_dst += rgb_stride) {
  414. memset(dst, pix, width);
  415. if (c->rgb_pic)
  416. for (j = 0; j < width * 3; j += 3)
  417. AV_WB24(rgb_dst + j, rgb_pix);
  418. }
  419. } else {
  420. return decode_region(acoder, c->pal_pic, c->rgb_pic,
  421. x, y, width, height, c->pal_stride, c->rgb_stride,
  422. &sc->intra_pix_ctx, &c->pal[0]);
  423. }
  424. return 0;
  425. }
  426. static int decode_region_inter(SliceContext *sc, ArithCoder *acoder,
  427. int x, int y, int width, int height)
  428. {
  429. MSS12Context const *c = sc->c;
  430. int mode;
  431. mode = acoder->get_model_sym(acoder, &sc->inter_region);
  432. if (!mode) {
  433. mode = decode_pixel(acoder, &sc->inter_pix_ctx, NULL, 0, 0);
  434. if (c->avctx->err_recognition & AV_EF_EXPLODE &&
  435. ( c->rgb_pic && mode != 0x01 && mode != 0x02 && mode != 0x04 ||
  436. !c->rgb_pic && mode != 0x80 && mode != 0xFF))
  437. return -1;
  438. if (mode == 0x02)
  439. copy_rectangles(c, x, y, width, height);
  440. else if (mode == 0x04)
  441. return motion_compensation(c, x, y, width, height);
  442. else if (mode != 0x80)
  443. return decode_region_intra(sc, acoder, x, y, width, height);
  444. } else {
  445. if (decode_region(acoder, c->mask, NULL,
  446. x, y, width, height, c->mask_stride, 0,
  447. &sc->inter_pix_ctx, &c->pal[0]) < 0)
  448. return -1;
  449. return decode_region_masked(c, acoder, c->pal_pic,
  450. c->pal_stride, c->mask,
  451. c->mask_stride,
  452. x, y, width, height,
  453. &sc->intra_pix_ctx);
  454. }
  455. return 0;
  456. }
  457. int ff_mss12_decode_rect(SliceContext *sc, ArithCoder *acoder,
  458. int x, int y, int width, int height)
  459. {
  460. int mode, pivot;
  461. mode = acoder->get_model_sym(acoder, &sc->split_mode);
  462. switch (mode) {
  463. case SPLIT_VERT:
  464. if ((pivot = decode_pivot(sc, acoder, height)) < 1)
  465. return -1;
  466. if (ff_mss12_decode_rect(sc, acoder, x, y, width, pivot))
  467. return -1;
  468. if (ff_mss12_decode_rect(sc, acoder, x, y + pivot, width, height - pivot))
  469. return -1;
  470. break;
  471. case SPLIT_HOR:
  472. if ((pivot = decode_pivot(sc, acoder, width)) < 1)
  473. return -1;
  474. if (ff_mss12_decode_rect(sc, acoder, x, y, pivot, height))
  475. return -1;
  476. if (ff_mss12_decode_rect(sc, acoder, x + pivot, y, width - pivot, height))
  477. return -1;
  478. break;
  479. case SPLIT_NONE:
  480. if (sc->c->keyframe)
  481. return decode_region_intra(sc, acoder, x, y, width, height);
  482. else
  483. return decode_region_inter(sc, acoder, x, y, width, height);
  484. default:
  485. return -1;
  486. }
  487. return 0;
  488. }
  489. av_cold int ff_mss12_decode_init(MSS12Context *c, int version,
  490. SliceContext* sc1, SliceContext *sc2)
  491. {
  492. AVCodecContext *avctx = c->avctx;
  493. int i;
  494. if (avctx->extradata_size < 52 + 256 * 3) {
  495. av_log(avctx, AV_LOG_ERROR, "Insufficient extradata size %d\n",
  496. avctx->extradata_size);
  497. return AVERROR_INVALIDDATA;
  498. }
  499. if (AV_RB32(avctx->extradata) < avctx->extradata_size) {
  500. av_log(avctx, AV_LOG_ERROR,
  501. "Insufficient extradata size: expected %"PRIu32" got %d\n",
  502. AV_RB32(avctx->extradata),
  503. avctx->extradata_size);
  504. return AVERROR_INVALIDDATA;
  505. }
  506. avctx->coded_width = AV_RB32(avctx->extradata + 20);
  507. avctx->coded_height = AV_RB32(avctx->extradata + 24);
  508. if (avctx->coded_width > 4096 || avctx->coded_height > 4096) {
  509. av_log(avctx, AV_LOG_ERROR, "Frame dimensions %dx%d too large",
  510. avctx->coded_width, avctx->coded_height);
  511. return AVERROR_INVALIDDATA;
  512. }
  513. if (avctx->width || avctx->height) {
  514. if (avctx->width <= 0 || avctx->width > avctx->coded_width ||
  515. avctx->height <= 0 || avctx->height > avctx->coded_height) {
  516. av_log(avctx, AV_LOG_ERROR, "Invalid display dimensions\n");
  517. return AVERROR_INVALIDDATA;
  518. }
  519. } else {
  520. avctx->width = avctx->coded_width;
  521. avctx->height = avctx->coded_height;
  522. }
  523. av_log(avctx, AV_LOG_DEBUG, "Encoder version %"PRIu32".%"PRIu32"\n",
  524. AV_RB32(avctx->extradata + 4), AV_RB32(avctx->extradata + 8));
  525. if (version != AV_RB32(avctx->extradata + 4) > 1) {
  526. av_log(avctx, AV_LOG_ERROR,
  527. "Header version doesn't match codec tag\n");
  528. return -1;
  529. }
  530. c->free_colours = AV_RB32(avctx->extradata + 48);
  531. if ((unsigned)c->free_colours > 256) {
  532. av_log(avctx, AV_LOG_ERROR,
  533. "Incorrect number of changeable palette entries: %d\n",
  534. c->free_colours);
  535. return AVERROR_INVALIDDATA;
  536. }
  537. av_log(avctx, AV_LOG_DEBUG, "%d free colour(s)\n", c->free_colours);
  538. av_log(avctx, AV_LOG_DEBUG, "Display dimensions %"PRIu32"x%"PRIu32"\n",
  539. AV_RB32(avctx->extradata + 12), AV_RB32(avctx->extradata + 16));
  540. av_log(avctx, AV_LOG_DEBUG, "Coded dimensions %dx%d\n",
  541. avctx->coded_width, avctx->coded_height);
  542. av_log(avctx, AV_LOG_DEBUG, "%g frames per second\n",
  543. av_int2float(AV_RB32(avctx->extradata + 28)));
  544. av_log(avctx, AV_LOG_DEBUG, "Bitrate %"PRIu32" bps\n",
  545. AV_RB32(avctx->extradata + 32));
  546. av_log(avctx, AV_LOG_DEBUG, "Max. lead time %g ms\n",
  547. av_int2float(AV_RB32(avctx->extradata + 36)));
  548. av_log(avctx, AV_LOG_DEBUG, "Max. lag time %g ms\n",
  549. av_int2float(AV_RB32(avctx->extradata + 40)));
  550. av_log(avctx, AV_LOG_DEBUG, "Max. seek time %g ms\n",
  551. av_int2float(AV_RB32(avctx->extradata + 44)));
  552. if (version) {
  553. if (avctx->extradata_size < 60 + 256 * 3) {
  554. av_log(avctx, AV_LOG_ERROR,
  555. "Insufficient extradata size %d for v2\n",
  556. avctx->extradata_size);
  557. return AVERROR_INVALIDDATA;
  558. }
  559. c->slice_split = AV_RB32(avctx->extradata + 52);
  560. av_log(avctx, AV_LOG_DEBUG, "Slice split %d\n", c->slice_split);
  561. c->full_model_syms = AV_RB32(avctx->extradata + 56);
  562. if (c->full_model_syms < 2 || c->full_model_syms > 256) {
  563. av_log(avctx, AV_LOG_ERROR,
  564. "Incorrect number of used colours %d\n",
  565. c->full_model_syms);
  566. return AVERROR_INVALIDDATA;
  567. }
  568. av_log(avctx, AV_LOG_DEBUG, "Used colours %d\n",
  569. c->full_model_syms);
  570. } else {
  571. c->slice_split = 0;
  572. c->full_model_syms = 256;
  573. }
  574. for (i = 0; i < 256; i++)
  575. c->pal[i] = AV_RB24(avctx->extradata + 52 +
  576. (version ? 8 : 0) + i * 3);
  577. c->mask_stride = FFALIGN(avctx->width, 16);
  578. c->mask = av_malloc(c->mask_stride * avctx->height);
  579. if (!c->mask) {
  580. av_log(avctx, AV_LOG_ERROR, "Cannot allocate mask plane\n");
  581. return AVERROR(ENOMEM);
  582. }
  583. sc1->c = c;
  584. slicecontext_init(sc1, version, c->full_model_syms);
  585. if (c->slice_split) {
  586. sc2->c = c;
  587. slicecontext_init(sc2, version, c->full_model_syms);
  588. }
  589. c->corrupted = 1;
  590. return 0;
  591. }
  592. av_cold int ff_mss12_decode_end(MSS12Context *c)
  593. {
  594. av_freep(&c->mask);
  595. return 0;
  596. }