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.

968 lines
27KB

  1. /*
  2. * Microsoft Screen 3 (aka Microsoft ATC Screen) decoder
  3. * Copyright (c) 2012 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Microsoft Screen 3 (aka Microsoft ATC Screen) decoder
  24. */
  25. #include "avcodec.h"
  26. #include "bytestream.h"
  27. #define HEADER_SIZE 27
  28. #define MODEL2_SCALE 13
  29. #define MODEL_SCALE 15
  30. #define MODEL256_SEC_SCALE 9
  31. typedef struct Model2 {
  32. int upd_val, till_rescale;
  33. unsigned zero_freq, zero_weight;
  34. unsigned total_freq, total_weight;
  35. } Model2;
  36. typedef struct Model {
  37. int weights[16], freqs[16];
  38. int num_syms;
  39. int tot_weight;
  40. int upd_val, max_upd_val, till_rescale;
  41. } Model;
  42. typedef struct Model256 {
  43. int weights[256], freqs[256];
  44. int tot_weight;
  45. int secondary[68];
  46. int sec_size;
  47. int upd_val, max_upd_val, till_rescale;
  48. } Model256;
  49. #define RAC_BOTTOM 0x01000000
  50. typedef struct RangeCoder {
  51. const uint8_t *src, *src_end;
  52. uint32_t range, low;
  53. int got_error;
  54. } RangeCoder;
  55. enum BlockType {
  56. FILL_BLOCK = 0,
  57. IMAGE_BLOCK,
  58. DCT_BLOCK,
  59. HAAR_BLOCK,
  60. SKIP_BLOCK
  61. };
  62. typedef struct BlockTypeContext {
  63. int last_type;
  64. Model bt_model[5];
  65. } BlockTypeContext;
  66. typedef struct FillBlockCoder {
  67. int fill_val;
  68. Model coef_model;
  69. } FillBlockCoder;
  70. typedef struct ImageBlockCoder {
  71. Model256 esc_model, vec_entry_model;
  72. Model vec_size_model;
  73. Model vq_model[125];
  74. } ImageBlockCoder;
  75. typedef struct DCTBlockCoder {
  76. int *prev_dc;
  77. int prev_dc_stride;
  78. int prev_dc_height;
  79. int quality;
  80. uint16_t qmat[64];
  81. Model dc_model;
  82. Model2 sign_model;
  83. Model256 ac_model;
  84. } DCTBlockCoder;
  85. typedef struct HaarBlockCoder {
  86. int quality, scale;
  87. Model256 coef_model;
  88. Model coef_hi_model;
  89. } HaarBlockCoder;
  90. typedef struct MSS3Context {
  91. AVCodecContext *avctx;
  92. AVFrame pic;
  93. int got_error;
  94. RangeCoder coder;
  95. BlockTypeContext btype[3];
  96. FillBlockCoder fill_coder[3];
  97. ImageBlockCoder image_coder[3];
  98. DCTBlockCoder dct_coder[3];
  99. HaarBlockCoder haar_coder[3];
  100. int dctblock[64];
  101. int hblock[16 * 16];
  102. } MSS3Context;
  103. static const uint8_t mss3_luma_quant[64] = {
  104. 16, 11, 10, 16, 24, 40, 51, 61,
  105. 12, 12, 14, 19, 26, 58, 60, 55,
  106. 14, 13, 16, 24, 40, 57, 69, 56,
  107. 14, 17, 22, 29, 51, 87, 80, 62,
  108. 18, 22, 37, 56, 68, 109, 103, 77,
  109. 24, 35, 55, 64, 81, 104, 113, 92,
  110. 49, 64, 78, 87, 103, 121, 120, 101,
  111. 72, 92, 95, 98, 112, 100, 103, 99
  112. };
  113. static const uint8_t mss3_chroma_quant[64] = {
  114. 17, 18, 24, 47, 99, 99, 99, 99,
  115. 18, 21, 26, 66, 99, 99, 99, 99,
  116. 24, 26, 56, 99, 99, 99, 99, 99,
  117. 47, 66, 99, 99, 99, 99, 99, 99,
  118. 99, 99, 99, 99, 99, 99, 99, 99,
  119. 99, 99, 99, 99, 99, 99, 99, 99,
  120. 99, 99, 99, 99, 99, 99, 99, 99,
  121. 99, 99, 99, 99, 99, 99, 99, 99
  122. };
  123. const uint8_t zigzag_scan[64] = {
  124. 0, 1, 8, 16, 9, 2, 3, 10,
  125. 17, 24, 32, 25, 18, 11, 4, 5,
  126. 12, 19, 26, 33, 40, 48, 41, 34,
  127. 27, 20, 13, 6, 7, 14, 21, 28,
  128. 35, 42, 49, 56, 57, 50, 43, 36,
  129. 29, 22, 15, 23, 30, 37, 44, 51,
  130. 58, 59, 52, 45, 38, 31, 39, 46,
  131. 53, 60, 61, 54, 47, 55, 62, 63
  132. };
  133. static void model2_reset(Model2 *m)
  134. {
  135. m->zero_weight = 1;
  136. m->total_weight = 2;
  137. m->zero_freq = 0x1000;
  138. m->total_freq = 0x2000;
  139. m->upd_val = 4;
  140. m->till_rescale = 4;
  141. }
  142. static void model2_update(Model2 *m, int bit)
  143. {
  144. unsigned scale;
  145. if (!bit)
  146. m->zero_weight++;
  147. m->till_rescale--;
  148. if (m->till_rescale)
  149. return;
  150. m->total_weight += m->upd_val;
  151. if (m->total_weight > 0x2000) {
  152. m->total_weight = (m->total_weight + 1) >> 1;
  153. m->zero_weight = (m->zero_weight + 1) >> 1;
  154. if (m->total_weight == m->zero_weight)
  155. m->total_weight = m->zero_weight + 1;
  156. }
  157. m->upd_val = m->upd_val * 5 >> 2;
  158. if (m->upd_val > 64)
  159. m->upd_val = 64;
  160. scale = 0x80000000u / m->total_weight;
  161. m->zero_freq = m->zero_weight * scale >> 18;
  162. m->total_freq = m->total_weight * scale >> 18;
  163. m->till_rescale = m->upd_val;
  164. }
  165. static void model_update(Model *m, int val)
  166. {
  167. int i, sum = 0;
  168. unsigned scale;
  169. m->weights[val]++;
  170. m->till_rescale--;
  171. if (m->till_rescale)
  172. return;
  173. m->tot_weight += m->upd_val;
  174. if (m->tot_weight > 0x8000) {
  175. m->tot_weight = 0;
  176. for (i = 0; i < m->num_syms; i++) {
  177. m->weights[i] = (m->weights[i] + 1) >> 1;
  178. m->tot_weight += m->weights[i];
  179. }
  180. }
  181. scale = 0x80000000u / m->tot_weight;
  182. for (i = 0; i < m->num_syms; i++) {
  183. m->freqs[i] = sum * scale >> 16;
  184. sum += m->weights[i];
  185. }
  186. m->upd_val = m->upd_val * 5 >> 2;
  187. if (m->upd_val > m->max_upd_val)
  188. m->upd_val = m->max_upd_val;
  189. m->till_rescale = m->upd_val;
  190. }
  191. static void model_reset(Model *m)
  192. {
  193. int i;
  194. m->tot_weight = 0;
  195. for (i = 0; i < m->num_syms - 1; i++)
  196. m->weights[i] = 1;
  197. m->weights[m->num_syms - 1] = 0;
  198. m->upd_val = m->num_syms;
  199. m->till_rescale = 1;
  200. model_update(m, m->num_syms - 1);
  201. m->till_rescale =
  202. m->upd_val = (m->num_syms + 6) >> 1;
  203. }
  204. static av_cold void model_init(Model *m, int num_syms)
  205. {
  206. m->num_syms = num_syms;
  207. m->max_upd_val = 8 * num_syms + 48;
  208. model_reset(m);
  209. }
  210. static void model256_update(Model256 *m, int val)
  211. {
  212. int i, sum = 0;
  213. unsigned scale;
  214. int send, sidx = 1;
  215. m->weights[val]++;
  216. m->till_rescale--;
  217. if (m->till_rescale)
  218. return;
  219. m->tot_weight += m->upd_val;
  220. if (m->tot_weight > 0x8000) {
  221. m->tot_weight = 0;
  222. for (i = 0; i < 256; i++) {
  223. m->weights[i] = (m->weights[i] + 1) >> 1;
  224. m->tot_weight += m->weights[i];
  225. }
  226. }
  227. scale = 0x80000000u / m->tot_weight;
  228. m->secondary[0] = 0;
  229. for (i = 0; i < 256; i++) {
  230. m->freqs[i] = sum * scale >> 16;
  231. sum += m->weights[i];
  232. send = m->freqs[i] >> MODEL256_SEC_SCALE;
  233. while (sidx <= send)
  234. m->secondary[sidx++] = i - 1;
  235. }
  236. while (sidx < m->sec_size)
  237. m->secondary[sidx++] = 255;
  238. m->upd_val = m->upd_val * 5 >> 2;
  239. if (m->upd_val > m->max_upd_val)
  240. m->upd_val = m->max_upd_val;
  241. m->till_rescale = m->upd_val;
  242. }
  243. static void model256_reset(Model256 *m)
  244. {
  245. int i;
  246. for (i = 0; i < 255; i++)
  247. m->weights[i] = 1;
  248. m->weights[255] = 0;
  249. m->tot_weight = 0;
  250. m->upd_val = 256;
  251. m->till_rescale = 1;
  252. model256_update(m, 255);
  253. m->till_rescale =
  254. m->upd_val = (256 + 6) >> 1;
  255. }
  256. static av_cold void model256_init(Model256 *m)
  257. {
  258. m->max_upd_val = 8 * 256 + 48;
  259. m->sec_size = (1 << 6) + 2;
  260. model256_reset(m);
  261. }
  262. static void rac_init(RangeCoder *c, const uint8_t *src, int size)
  263. {
  264. int i;
  265. c->src = src;
  266. c->src_end = src + size;
  267. c->low = 0;
  268. for (i = 0; i < FFMIN(size, 4); i++)
  269. c->low = (c->low << 8) | *c->src++;
  270. c->range = 0xFFFFFFFF;
  271. c->got_error = 0;
  272. }
  273. static void rac_normalise(RangeCoder *c)
  274. {
  275. for (;;) {
  276. c->range <<= 8;
  277. c->low <<= 8;
  278. if (c->src < c->src_end) {
  279. c->low |= *c->src++;
  280. } else if (!c->low) {
  281. c->got_error = 1;
  282. return;
  283. }
  284. if (c->range >= RAC_BOTTOM)
  285. return;
  286. }
  287. }
  288. static int rac_get_bit(RangeCoder *c)
  289. {
  290. int bit;
  291. c->range >>= 1;
  292. bit = (c->range <= c->low);
  293. if (bit)
  294. c->low -= c->range;
  295. if (c->range < RAC_BOTTOM)
  296. rac_normalise(c);
  297. return bit;
  298. }
  299. static int rac_get_bits(RangeCoder *c, int nbits)
  300. {
  301. int val;
  302. c->range >>= nbits;
  303. val = c->low / c->range;
  304. c->low -= c->range * val;
  305. if (c->range < RAC_BOTTOM)
  306. rac_normalise(c);
  307. return val;
  308. }
  309. static int rac_get_model2_sym(RangeCoder *c, Model2 *m)
  310. {
  311. int bit, helper;
  312. helper = m->zero_freq * (c->range >> MODEL2_SCALE);
  313. bit = (c->low >= helper);
  314. if (bit) {
  315. c->low -= helper;
  316. c->range -= helper;
  317. } else {
  318. c->range = helper;
  319. }
  320. if (c->range < RAC_BOTTOM)
  321. rac_normalise(c);
  322. model2_update(m, bit);
  323. return bit;
  324. }
  325. static int rac_get_model_sym(RangeCoder *c, Model *m)
  326. {
  327. int prob, prob2, helper, val;
  328. int end, end2;
  329. prob = 0;
  330. prob2 = c->range;
  331. c->range >>= MODEL_SCALE;
  332. val = 0;
  333. end = m->num_syms >> 1;
  334. end2 = m->num_syms;
  335. do {
  336. helper = m->freqs[end] * c->range;
  337. if (helper <= c->low) {
  338. val = end;
  339. prob = helper;
  340. } else {
  341. end2 = end;
  342. prob2 = helper;
  343. }
  344. end = (end2 + val) >> 1;
  345. } while (end != val);
  346. c->low -= prob;
  347. c->range = prob2 - prob;
  348. if (c->range < RAC_BOTTOM)
  349. rac_normalise(c);
  350. model_update(m, val);
  351. return val;
  352. }
  353. static int rac_get_model256_sym(RangeCoder *c, Model256 *m)
  354. {
  355. int prob, prob2, helper, val;
  356. int start, end;
  357. int ssym;
  358. prob2 = c->range;
  359. c->range >>= MODEL_SCALE;
  360. helper = c->low / c->range;
  361. ssym = helper >> MODEL256_SEC_SCALE;
  362. val = m->secondary[ssym];
  363. end = start = m->secondary[ssym + 1] + 1;
  364. while (end > val + 1) {
  365. ssym = (end + val) >> 1;
  366. if (m->freqs[ssym] <= helper) {
  367. end = start;
  368. val = ssym;
  369. } else {
  370. end = (end + val) >> 1;
  371. start = ssym;
  372. }
  373. }
  374. prob = m->freqs[val] * c->range;
  375. if (val != 255)
  376. prob2 = m->freqs[val + 1] * c->range;
  377. c->low -= prob;
  378. c->range = prob2 - prob;
  379. if (c->range < RAC_BOTTOM)
  380. rac_normalise(c);
  381. model256_update(m, val);
  382. return val;
  383. }
  384. static int decode_block_type(RangeCoder *c, BlockTypeContext *bt)
  385. {
  386. bt->last_type = rac_get_model_sym(c, &bt->bt_model[bt->last_type]);
  387. return bt->last_type;
  388. }
  389. static int decode_coeff(RangeCoder *c, Model *m)
  390. {
  391. int val, sign;
  392. val = rac_get_model_sym(c, m);
  393. if (val) {
  394. sign = rac_get_bit(c);
  395. if (val > 1) {
  396. val--;
  397. val = (1 << val) + rac_get_bits(c, val);
  398. }
  399. if (!sign)
  400. val = -val;
  401. }
  402. return val;
  403. }
  404. static void decode_fill_block(RangeCoder *c, FillBlockCoder *fc,
  405. uint8_t *dst, int stride, int block_size)
  406. {
  407. int i;
  408. fc->fill_val += decode_coeff(c, &fc->coef_model);
  409. for (i = 0; i < block_size; i++, dst += stride)
  410. memset(dst, fc->fill_val, block_size);
  411. }
  412. static void decode_image_block(RangeCoder *c, ImageBlockCoder *ic,
  413. uint8_t *dst, int stride, int block_size)
  414. {
  415. int i, j;
  416. int vec_size;
  417. int vec[4];
  418. int prev_line[16];
  419. int A, B, C;
  420. vec_size = rac_get_model_sym(c, &ic->vec_size_model) + 2;
  421. for (i = 0; i < vec_size; i++)
  422. vec[i] = rac_get_model256_sym(c, &ic->vec_entry_model);
  423. for (; i < 4; i++)
  424. vec[i] = 0;
  425. memset(prev_line, 0, sizeof(prev_line));
  426. for (j = 0; j < block_size; j++) {
  427. A = 0;
  428. B = 0;
  429. for (i = 0; i < block_size; i++) {
  430. C = B;
  431. B = prev_line[i];
  432. A = rac_get_model_sym(c, &ic->vq_model[A + B * 5 + C * 25]);
  433. prev_line[i] = A;
  434. if (A < 4)
  435. dst[i] = vec[A];
  436. else
  437. dst[i] = rac_get_model256_sym(c, &ic->esc_model);
  438. }
  439. dst += stride;
  440. }
  441. }
  442. static int decode_dct(RangeCoder *c, DCTBlockCoder *bc, int *block,
  443. int bx, int by)
  444. {
  445. int skip, val, sign, pos = 1, zz_pos, dc;
  446. int blk_pos = bx + by * bc->prev_dc_stride;
  447. memset(block, 0, sizeof(*block) * 64);
  448. dc = decode_coeff(c, &bc->dc_model);
  449. if (by) {
  450. if (bx) {
  451. int l, tl, t;
  452. l = bc->prev_dc[blk_pos - 1];
  453. tl = bc->prev_dc[blk_pos - 1 - bc->prev_dc_stride];
  454. t = bc->prev_dc[blk_pos - bc->prev_dc_stride];
  455. if (FFABS(t - tl) <= FFABS(l - tl))
  456. dc += l;
  457. else
  458. dc += t;
  459. } else {
  460. dc += bc->prev_dc[blk_pos - bc->prev_dc_stride];
  461. }
  462. } else if (bx) {
  463. dc += bc->prev_dc[bx - 1];
  464. }
  465. bc->prev_dc[blk_pos] = dc;
  466. block[0] = dc * bc->qmat[0];
  467. while (pos < 64) {
  468. val = rac_get_model256_sym(c, &bc->ac_model);
  469. if (!val)
  470. return 0;
  471. if (val == 0xF0) {
  472. pos += 16;
  473. continue;
  474. }
  475. skip = val >> 4;
  476. val = val & 0xF;
  477. if (!val)
  478. return -1;
  479. pos += skip;
  480. if (pos >= 64)
  481. return -1;
  482. sign = rac_get_model2_sym(c, &bc->sign_model);
  483. if (val > 1) {
  484. val--;
  485. val = (1 << val) + rac_get_bits(c, val);
  486. }
  487. if (!sign)
  488. val = -val;
  489. zz_pos = zigzag_scan[pos];
  490. block[zz_pos] = val * bc->qmat[zz_pos];
  491. pos++;
  492. }
  493. return pos == 64 ? 0 : -1;
  494. }
  495. #define DCT_TEMPLATE(blk, step, SOP, shift) \
  496. const int t0 = -39409 * blk[7 * step] - 58980 * blk[1 * step]; \
  497. const int t1 = 39410 * blk[1 * step] - 58980 * blk[7 * step]; \
  498. const int t2 = -33410 * blk[5 * step] - 167963 * blk[3 * step]; \
  499. const int t3 = 33410 * blk[3 * step] - 167963 * blk[5 * step]; \
  500. const int t4 = blk[3 * step] + blk[7 * step]; \
  501. const int t5 = blk[1 * step] + blk[5 * step]; \
  502. const int t6 = 77062 * t4 + 51491 * t5; \
  503. const int t7 = 77062 * t5 - 51491 * t4; \
  504. const int t8 = 35470 * blk[2 * step] - 85623 * blk[6 * step]; \
  505. const int t9 = 35470 * blk[6 * step] + 85623 * blk[2 * step]; \
  506. const int tA = SOP(blk[0 * step] - blk[4 * step]); \
  507. const int tB = SOP(blk[0 * step] + blk[4 * step]); \
  508. \
  509. blk[0 * step] = ( t1 + t6 + t9 + tB) >> shift; \
  510. blk[1 * step] = ( t3 + t7 + t8 + tA) >> shift; \
  511. blk[2 * step] = ( t2 + t6 - t8 + tA) >> shift; \
  512. blk[3 * step] = ( t0 + t7 - t9 + tB) >> shift; \
  513. blk[4 * step] = (-(t0 + t7) - t9 + tB) >> shift; \
  514. blk[5 * step] = (-(t2 + t6) - t8 + tA) >> shift; \
  515. blk[6 * step] = (-(t3 + t7) + t8 + tA) >> shift; \
  516. blk[7 * step] = (-(t1 + t6) + t9 + tB) >> shift; \
  517. #define SOP_ROW(a) ((a) << 16) + 0x2000
  518. #define SOP_COL(a) ((a + 32) << 16)
  519. static void dct_put(uint8_t *dst, int stride, int *block)
  520. {
  521. int i, j;
  522. int *ptr;
  523. ptr = block;
  524. for (i = 0; i < 8; i++) {
  525. DCT_TEMPLATE(ptr, 1, SOP_ROW, 13);
  526. ptr += 8;
  527. }
  528. ptr = block;
  529. for (i = 0; i < 8; i++) {
  530. DCT_TEMPLATE(ptr, 8, SOP_COL, 22);
  531. ptr++;
  532. }
  533. ptr = block;
  534. for (j = 0; j < 8; j++) {
  535. for (i = 0; i < 8; i++)
  536. dst[i] = av_clip_uint8(ptr[i] + 128);
  537. dst += stride;
  538. ptr += 8;
  539. }
  540. }
  541. static void decode_dct_block(RangeCoder *c, DCTBlockCoder *bc,
  542. uint8_t *dst, int stride, int block_size,
  543. int *block, int mb_x, int mb_y)
  544. {
  545. int i, j;
  546. int bx, by;
  547. int nblocks = block_size >> 3;
  548. bx = mb_x * nblocks;
  549. by = mb_y * nblocks;
  550. for (j = 0; j < nblocks; j++) {
  551. for (i = 0; i < nblocks; i++) {
  552. if (decode_dct(c, bc, block, bx + i, by + j)) {
  553. c->got_error = 1;
  554. return;
  555. }
  556. dct_put(dst + i * 8, stride, block);
  557. }
  558. dst += 8 * stride;
  559. }
  560. }
  561. static void decode_haar_block(RangeCoder *c, HaarBlockCoder *hc,
  562. uint8_t *dst, int stride, int block_size,
  563. int *block)
  564. {
  565. const int hsize = block_size >> 1;
  566. int A, B, C, D, t1, t2, t3, t4;
  567. int i, j;
  568. for (j = 0; j < block_size; j++) {
  569. for (i = 0; i < block_size; i++) {
  570. if (i < hsize && j < hsize)
  571. block[i] = rac_get_model256_sym(c, &hc->coef_model);
  572. else
  573. block[i] = decode_coeff(c, &hc->coef_hi_model);
  574. block[i] *= hc->scale;
  575. }
  576. block += block_size;
  577. }
  578. block -= block_size * block_size;
  579. for (j = 0; j < hsize; j++) {
  580. for (i = 0; i < hsize; i++) {
  581. A = block[i];
  582. B = block[i + hsize];
  583. C = block[i + hsize * block_size];
  584. D = block[i + hsize * block_size + hsize];
  585. t1 = A - B;
  586. t2 = C - D;
  587. t3 = A + B;
  588. t4 = C + D;
  589. dst[i * 2] = av_clip_uint8(t1 - t2);
  590. dst[i * 2 + stride] = av_clip_uint8(t1 + t2);
  591. dst[i * 2 + 1] = av_clip_uint8(t3 - t4);
  592. dst[i * 2 + 1 + stride] = av_clip_uint8(t3 + t4);
  593. }
  594. block += block_size;
  595. dst += stride * 2;
  596. }
  597. }
  598. static void gen_quant_mat(uint16_t *qmat, const uint8_t *ref, float scale)
  599. {
  600. int i;
  601. for (i = 0; i < 64; i++)
  602. qmat[i] = (uint16_t)(ref[i] * scale + 50.0) / 100;
  603. }
  604. static void reset_coders(MSS3Context *ctx, int quality)
  605. {
  606. int i, j;
  607. for (i = 0; i < 3; i++) {
  608. ctx->btype[i].last_type = SKIP_BLOCK;
  609. for (j = 0; j < 5; j++)
  610. model_reset(&ctx->btype[i].bt_model[j]);
  611. ctx->fill_coder[i].fill_val = 0;
  612. model_reset(&ctx->fill_coder[i].coef_model);
  613. model256_reset(&ctx->image_coder[i].esc_model);
  614. model256_reset(&ctx->image_coder[i].vec_entry_model);
  615. model_reset(&ctx->image_coder[i].vec_size_model);
  616. for (j = 0; j < 125; j++)
  617. model_reset(&ctx->image_coder[i].vq_model[j]);
  618. if (ctx->dct_coder[i].quality != quality) {
  619. float scale;
  620. ctx->dct_coder[i].quality = quality;
  621. if (quality > 50)
  622. scale = 200.0f - 2 * quality;
  623. else
  624. scale = 5000.0f / quality;
  625. gen_quant_mat(ctx->dct_coder[i].qmat,
  626. i ? mss3_chroma_quant : mss3_luma_quant,
  627. scale);
  628. }
  629. memset(ctx->dct_coder[i].prev_dc, 0,
  630. sizeof(*ctx->dct_coder[i].prev_dc) *
  631. ctx->dct_coder[i].prev_dc_stride *
  632. ctx->dct_coder[i].prev_dc_height);
  633. model_reset(&ctx->dct_coder[i].dc_model);
  634. model2_reset(&ctx->dct_coder[i].sign_model);
  635. model256_reset(&ctx->dct_coder[i].ac_model);
  636. if (ctx->haar_coder[i].quality != quality) {
  637. ctx->haar_coder[i].quality = quality;
  638. ctx->haar_coder[i].scale = 17 - 7 * quality / 50;
  639. }
  640. model_reset(&ctx->haar_coder[i].coef_hi_model);
  641. model256_reset(&ctx->haar_coder[i].coef_model);
  642. }
  643. }
  644. static av_cold void init_coders(MSS3Context *ctx)
  645. {
  646. int i, j;
  647. for (i = 0; i < 3; i++) {
  648. for (j = 0; j < 5; j++)
  649. model_init(&ctx->btype[i].bt_model[j], 5);
  650. model_init(&ctx->fill_coder[i].coef_model, 12);
  651. model256_init(&ctx->image_coder[i].esc_model);
  652. model256_init(&ctx->image_coder[i].vec_entry_model);
  653. model_init(&ctx->image_coder[i].vec_size_model, 3);
  654. for (j = 0; j < 125; j++)
  655. model_init(&ctx->image_coder[i].vq_model[j], 5);
  656. model_init(&ctx->dct_coder[i].dc_model, 12);
  657. model256_init(&ctx->dct_coder[i].ac_model);
  658. model_init(&ctx->haar_coder[i].coef_hi_model, 12);
  659. model256_init(&ctx->haar_coder[i].coef_model);
  660. }
  661. }
  662. static int mss3_decode_frame(AVCodecContext *avctx, void *data, int *data_size,
  663. AVPacket *avpkt)
  664. {
  665. const uint8_t *buf = avpkt->data;
  666. int buf_size = avpkt->size;
  667. MSS3Context *c = avctx->priv_data;
  668. RangeCoder *acoder = &c->coder;
  669. GetByteContext gb;
  670. uint8_t *dst[3];
  671. int dec_width, dec_height, dec_x, dec_y, quality, keyframe;
  672. int x, y, i, mb_width, mb_height, blk_size, btype;
  673. int ret;
  674. if (buf_size < HEADER_SIZE) {
  675. av_log(avctx, AV_LOG_ERROR,
  676. "Frame should have at least %d bytes, got %d instead\n",
  677. HEADER_SIZE, buf_size);
  678. return AVERROR_INVALIDDATA;
  679. }
  680. bytestream2_init(&gb, buf, buf_size);
  681. keyframe = bytestream2_get_be32(&gb);
  682. if (keyframe & ~0x301) {
  683. av_log(avctx, AV_LOG_ERROR, "Invalid frame type %X\n", keyframe);
  684. return AVERROR_INVALIDDATA;
  685. }
  686. keyframe = !(keyframe & 1);
  687. bytestream2_skip(&gb, 6);
  688. dec_x = bytestream2_get_be16(&gb);
  689. dec_y = bytestream2_get_be16(&gb);
  690. dec_width = bytestream2_get_be16(&gb);
  691. dec_height = bytestream2_get_be16(&gb);
  692. if (dec_x + dec_width > avctx->width ||
  693. dec_y + dec_height > avctx->height ||
  694. (dec_width | dec_height) & 0xF) {
  695. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d +%d,%d\n",
  696. dec_width, dec_height, dec_x, dec_y);
  697. return AVERROR_INVALIDDATA;
  698. }
  699. bytestream2_skip(&gb, 4);
  700. quality = bytestream2_get_byte(&gb);
  701. if (quality < 1 || quality > 100) {
  702. av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
  703. return AVERROR_INVALIDDATA;
  704. }
  705. bytestream2_skip(&gb, 4);
  706. if (keyframe && !bytestream2_get_bytes_left(&gb)) {
  707. av_log(avctx, AV_LOG_ERROR, "Keyframe without data found\n");
  708. return AVERROR_INVALIDDATA;
  709. }
  710. if (!keyframe && c->got_error)
  711. return buf_size;
  712. c->got_error = 0;
  713. c->pic.reference = 3;
  714. c->pic.buffer_hints = FF_BUFFER_HINTS_VALID | FF_BUFFER_HINTS_PRESERVE |
  715. FF_BUFFER_HINTS_REUSABLE;
  716. if ((ret = avctx->reget_buffer(avctx, &c->pic)) < 0) {
  717. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  718. return ret;
  719. }
  720. c->pic.key_frame = keyframe;
  721. c->pic.pict_type = keyframe ? AV_PICTURE_TYPE_I : AV_PICTURE_TYPE_P;
  722. if (!bytestream2_get_bytes_left(&gb)) {
  723. *data_size = sizeof(AVFrame);
  724. *(AVFrame*)data = c->pic;
  725. return buf_size;
  726. }
  727. reset_coders(c, quality);
  728. rac_init(acoder, buf + HEADER_SIZE, buf_size - HEADER_SIZE);
  729. mb_width = dec_width >> 4;
  730. mb_height = dec_height >> 4;
  731. dst[0] = c->pic.data[0] + dec_x + dec_y * c->pic.linesize[0];
  732. dst[1] = c->pic.data[1] + dec_x / 2 + (dec_y / 2) * c->pic.linesize[1];
  733. dst[2] = c->pic.data[2] + dec_x / 2 + (dec_y / 2) * c->pic.linesize[2];
  734. for (y = 0; y < mb_height; y++) {
  735. for (x = 0; x < mb_width; x++) {
  736. for (i = 0; i < 3; i++) {
  737. blk_size = 8 << !i;
  738. btype = decode_block_type(acoder, c->btype + i);
  739. switch (btype) {
  740. case FILL_BLOCK:
  741. decode_fill_block(acoder, c->fill_coder + i,
  742. dst[i] + x * blk_size,
  743. c->pic.linesize[i], blk_size);
  744. break;
  745. case IMAGE_BLOCK:
  746. decode_image_block(acoder, c->image_coder + i,
  747. dst[i] + x * blk_size,
  748. c->pic.linesize[i], blk_size);
  749. break;
  750. case DCT_BLOCK:
  751. decode_dct_block(acoder, c->dct_coder + i,
  752. dst[i] + x * blk_size,
  753. c->pic.linesize[i], blk_size,
  754. c->dctblock, x, y);
  755. break;
  756. case HAAR_BLOCK:
  757. decode_haar_block(acoder, c->haar_coder + i,
  758. dst[i] + x * blk_size,
  759. c->pic.linesize[i], blk_size,
  760. c->hblock);
  761. break;
  762. }
  763. if (c->got_error || acoder->got_error) {
  764. av_log(avctx, AV_LOG_ERROR, "Error decoding block %d,%d\n",
  765. x, y);
  766. c->got_error = 1;
  767. return AVERROR_INVALIDDATA;
  768. }
  769. }
  770. }
  771. dst[0] += c->pic.linesize[0] * 16;
  772. dst[1] += c->pic.linesize[1] * 8;
  773. dst[2] += c->pic.linesize[2] * 8;
  774. }
  775. *data_size = sizeof(AVFrame);
  776. *(AVFrame*)data = c->pic;
  777. return buf_size;
  778. }
  779. static av_cold int mss3_decode_init(AVCodecContext *avctx)
  780. {
  781. MSS3Context * const c = avctx->priv_data;
  782. int i;
  783. c->avctx = avctx;
  784. if ((avctx->width & 0xF) || (avctx->height & 0xF)) {
  785. av_log(avctx, AV_LOG_ERROR,
  786. "Image dimensions should be a multiple of 16.\n");
  787. return AVERROR_INVALIDDATA;
  788. }
  789. c->got_error = 0;
  790. for (i = 0; i < 3; i++) {
  791. int b_width = avctx->width >> (2 + !!i);
  792. int b_height = avctx->height >> (2 + !!i);
  793. c->dct_coder[i].prev_dc_stride = b_width;
  794. c->dct_coder[i].prev_dc_height = b_height;
  795. c->dct_coder[i].prev_dc = av_malloc(sizeof(*c->dct_coder[i].prev_dc) *
  796. b_width * b_height);
  797. if (!c->dct_coder[i].prev_dc) {
  798. av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
  799. while (i >= 0) {
  800. av_freep(&c->dct_coder[i].prev_dc);
  801. i--;
  802. }
  803. return AVERROR(ENOMEM);
  804. }
  805. }
  806. avctx->pix_fmt = PIX_FMT_YUV420P;
  807. avctx->coded_frame = &c->pic;
  808. init_coders(c);
  809. return 0;
  810. }
  811. static av_cold int mss3_decode_end(AVCodecContext *avctx)
  812. {
  813. MSS3Context * const c = avctx->priv_data;
  814. int i;
  815. if (c->pic.data[0])
  816. avctx->release_buffer(avctx, &c->pic);
  817. for (i = 0; i < 3; i++)
  818. av_freep(&c->dct_coder[i].prev_dc);
  819. return 0;
  820. }
  821. AVCodec ff_msa1_decoder = {
  822. .name = "msa1",
  823. .type = AVMEDIA_TYPE_VIDEO,
  824. .id = CODEC_ID_MSA1,
  825. .priv_data_size = sizeof(MSS3Context),
  826. .init = mss3_decode_init,
  827. .close = mss3_decode_end,
  828. .decode = mss3_decode_frame,
  829. .capabilities = CODEC_CAP_DR1,
  830. .long_name = NULL_IF_CONFIG_SMALL("MS ATC Screen"),
  831. };