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.

685 lines
21KB

  1. /*
  2. * Microsoft Screen 4 (aka Microsoft Expression Encoder 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 4 (aka Microsoft Titanium Screen 2,
  24. * aka Microsoft Expression Encoder Screen) decoder
  25. */
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "get_bits.h"
  29. #include "internal.h"
  30. #include "mss34dsp.h"
  31. #include "unary_legacy.h"
  32. #define HEADER_SIZE 8
  33. enum FrameType {
  34. INTRA_FRAME = 0,
  35. INTER_FRAME,
  36. SKIP_FRAME
  37. };
  38. enum BlockType {
  39. SKIP_BLOCK = 0,
  40. DCT_BLOCK,
  41. IMAGE_BLOCK,
  42. };
  43. enum CachePos {
  44. LEFT = 0,
  45. TOP_LEFT,
  46. TOP,
  47. };
  48. static const uint8_t mss4_dc_vlc_lens[2][16] = {
  49. { 0, 1, 5, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0 },
  50. { 0, 3, 1, 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0 }
  51. };
  52. static const uint8_t mss4_ac_vlc_lens[2][16] = {
  53. { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125 },
  54. { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 119 }
  55. };
  56. static const uint8_t mss4_ac_vlc_syms[2][162] = {
  57. { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  58. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  59. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08,
  60. 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0,
  61. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16,
  62. 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
  63. 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  64. 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  65. 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  66. 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  67. 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  68. 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  69. 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  70. 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
  71. 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
  72. 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5,
  73. 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4,
  74. 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
  75. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA,
  76. 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
  77. 0xF9, 0xFA },
  78. { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  79. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  80. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  81. 0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0,
  82. 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16, 0x24, 0x34,
  83. 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
  84. 0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38,
  85. 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  86. 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  87. 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  88. 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  89. 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  90. 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
  91. 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
  92. 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
  93. 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
  94. 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
  95. 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
  96. 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9,
  97. 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
  98. 0xF9, 0xFA }
  99. };
  100. static const uint8_t vec_len_syms[2][4] = {
  101. { 4, 2, 3, 1 },
  102. { 4, 1, 2, 3 }
  103. };
  104. static const uint8_t mss4_vec_entry_vlc_lens[2][16] = {
  105. { 0, 2, 2, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  106. { 0, 1, 5, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  107. };
  108. static const uint8_t mss4_vec_entry_vlc_syms[2][9] = {
  109. { 0, 7, 6, 5, 8, 4, 3, 1, 2 },
  110. { 0, 2, 3, 4, 5, 6, 7, 1, 8 }
  111. };
  112. #define MAX_ENTRIES 162
  113. typedef struct MSS4Context {
  114. AVFrame *pic;
  115. VLC dc_vlc[2], ac_vlc[2];
  116. VLC vec_entry_vlc[2];
  117. int block[64];
  118. uint8_t imgbuf[3][16 * 16];
  119. int quality;
  120. uint16_t quant_mat[2][64];
  121. int *prev_dc[3];
  122. ptrdiff_t dc_stride[3];
  123. int dc_cache[4][4];
  124. int prev_vec[3][4];
  125. } MSS4Context;
  126. static av_cold int mss4_init_vlc(VLC *vlc, const uint8_t *lens,
  127. const uint8_t *syms, int num_syms)
  128. {
  129. uint8_t bits[MAX_ENTRIES];
  130. uint16_t codes[MAX_ENTRIES];
  131. int i, j;
  132. int prefix = 0, max_bits = 0, idx = 0;
  133. for (i = 0; i < 16; i++) {
  134. for (j = 0; j < lens[i]; j++) {
  135. bits[idx] = i + 1;
  136. codes[idx] = prefix++;
  137. max_bits = i + 1;
  138. idx++;
  139. }
  140. prefix <<= 1;
  141. }
  142. return ff_init_vlc_sparse(vlc, FFMIN(max_bits, 9), num_syms, bits, 1, 1,
  143. codes, 2, 2, syms, 1, 1, 0);
  144. }
  145. static av_cold int mss4_init_vlcs(MSS4Context *ctx)
  146. {
  147. int ret, i;
  148. for (i = 0; i < 2; i++) {
  149. ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL, 12);
  150. if (ret)
  151. return ret;
  152. ret = mss4_init_vlc(&ctx->ac_vlc[i], mss4_ac_vlc_lens[i],
  153. mss4_ac_vlc_syms[i], 162);
  154. if (ret)
  155. return ret;
  156. ret = mss4_init_vlc(&ctx->vec_entry_vlc[i], mss4_vec_entry_vlc_lens[i],
  157. mss4_vec_entry_vlc_syms[i], 9);
  158. if (ret)
  159. return ret;
  160. }
  161. return 0;
  162. }
  163. static av_cold void mss4_free_vlcs(MSS4Context *ctx)
  164. {
  165. int i;
  166. for (i = 0; i < 2; i++) {
  167. ff_free_vlc(&ctx->dc_vlc[i]);
  168. ff_free_vlc(&ctx->ac_vlc[i]);
  169. ff_free_vlc(&ctx->vec_entry_vlc[i]);
  170. }
  171. }
  172. /* This function returns values in the range
  173. * (-range + 1; -range/2] U [range/2; range - 1)
  174. * i.e.
  175. * nbits = 0 -> 0
  176. * nbits = 1 -> -1, 1
  177. * nbits = 2 -> -3, -2, 2, 3
  178. */
  179. static av_always_inline int get_coeff_bits(GetBitContext *gb, int nbits)
  180. {
  181. int val;
  182. if (!nbits)
  183. return 0;
  184. val = get_bits(gb, nbits);
  185. if (val < (1 << (nbits - 1)))
  186. val -= (1 << nbits) - 1;
  187. return val;
  188. }
  189. static inline int get_coeff(GetBitContext *gb, VLC *vlc)
  190. {
  191. int val = get_vlc2(gb, vlc->table, vlc->bits, 2);
  192. return get_coeff_bits(gb, val);
  193. }
  194. static int mss4_decode_dct(GetBitContext *gb, VLC *dc_vlc, VLC *ac_vlc,
  195. int *block, int *dc_cache,
  196. int bx, int by, uint16_t *quant_mat)
  197. {
  198. int skip, val, pos = 1, zz_pos, dc;
  199. memset(block, 0, sizeof(*block) * 64);
  200. dc = get_coeff(gb, dc_vlc);
  201. // DC prediction is the same as in MSS3
  202. if (by) {
  203. if (bx) {
  204. int l, tl, t;
  205. l = dc_cache[LEFT];
  206. tl = dc_cache[TOP_LEFT];
  207. t = dc_cache[TOP];
  208. if (FFABS(t - tl) <= FFABS(l - tl))
  209. dc += l;
  210. else
  211. dc += t;
  212. } else {
  213. dc += dc_cache[TOP];
  214. }
  215. } else if (bx) {
  216. dc += dc_cache[LEFT];
  217. }
  218. dc_cache[LEFT] = dc;
  219. block[0] = dc * quant_mat[0];
  220. while (pos < 64) {
  221. val = get_vlc2(gb, ac_vlc->table, 9, 2);
  222. if (!val)
  223. return 0;
  224. if (val == -1)
  225. return -1;
  226. if (val == 0xF0) {
  227. pos += 16;
  228. continue;
  229. }
  230. skip = val >> 4;
  231. val = get_coeff_bits(gb, val & 0xF);
  232. pos += skip;
  233. if (pos >= 64)
  234. return -1;
  235. zz_pos = ff_zigzag_direct[pos];
  236. block[zz_pos] = val * quant_mat[zz_pos];
  237. pos++;
  238. }
  239. return pos == 64 ? 0 : -1;
  240. }
  241. static int mss4_decode_dct_block(MSS4Context *c, GetBitContext *gb,
  242. uint8_t *dst[3], int mb_x, int mb_y)
  243. {
  244. int i, j, k, ret;
  245. uint8_t *out = dst[0];
  246. for (j = 0; j < 2; j++) {
  247. for (i = 0; i < 2; i++) {
  248. int xpos = mb_x * 2 + i;
  249. c->dc_cache[j][TOP_LEFT] = c->dc_cache[j][TOP];
  250. c->dc_cache[j][TOP] = c->prev_dc[0][mb_x * 2 + i];
  251. ret = mss4_decode_dct(gb, c->dc_vlc, c->ac_vlc, c->block,
  252. c->dc_cache[j],
  253. xpos, mb_y * 2 + j, c->quant_mat[0]);
  254. if (ret)
  255. return ret;
  256. c->prev_dc[0][mb_x * 2 + i] = c->dc_cache[j][LEFT];
  257. ff_mss34_dct_put(out + xpos * 8, c->pic->linesize[0],
  258. c->block);
  259. }
  260. out += 8 * c->pic->linesize[0];
  261. }
  262. for (i = 1; i < 3; i++) {
  263. c->dc_cache[i + 1][TOP_LEFT] = c->dc_cache[i + 1][TOP];
  264. c->dc_cache[i + 1][TOP] = c->prev_dc[i][mb_x];
  265. ret = mss4_decode_dct(gb, c->dc_vlc + 1, c->ac_vlc + 1,
  266. c->block, c->dc_cache[i + 1], mb_x, mb_y,
  267. c->quant_mat[1]);
  268. if (ret)
  269. return ret;
  270. c->prev_dc[i][mb_x] = c->dc_cache[i + 1][LEFT];
  271. ff_mss34_dct_put(c->imgbuf[i], 8, c->block);
  272. out = dst[i] + mb_x * 16;
  273. // Since the DCT block is coded as YUV420 and the whole frame as YUV444,
  274. // we need to scale chroma.
  275. for (j = 0; j < 16; j++) {
  276. for (k = 0; k < 8; k++)
  277. AV_WN16A(out + k * 2, c->imgbuf[i][k + (j & ~1) * 4] * 0x101);
  278. out += c->pic->linesize[i];
  279. }
  280. }
  281. return 0;
  282. }
  283. static void read_vec_pos(GetBitContext *gb, int *vec_pos, int *sel_flag,
  284. int *sel_len, int *prev)
  285. {
  286. int i, y_flag = 0;
  287. for (i = 2; i >= 0; i--) {
  288. if (!sel_flag[i]) {
  289. vec_pos[i] = 0;
  290. continue;
  291. }
  292. if ((!i && !y_flag) || get_bits1(gb)) {
  293. if (sel_len[i] > 0) {
  294. int pval = prev[i];
  295. vec_pos[i] = get_bits(gb, sel_len[i]);
  296. if (vec_pos[i] >= pval)
  297. vec_pos[i]++;
  298. } else {
  299. vec_pos[i] = !prev[i];
  300. }
  301. y_flag = 1;
  302. } else {
  303. vec_pos[i] = prev[i];
  304. }
  305. }
  306. }
  307. static int get_value_cached(GetBitContext *gb, int vec_pos, uint8_t *vec,
  308. int vec_size, int component, int shift, int *prev)
  309. {
  310. if (vec_pos < vec_size)
  311. return vec[vec_pos];
  312. if (!get_bits1(gb))
  313. return prev[component];
  314. prev[component] = get_bits(gb, 8 - shift) << shift;
  315. return prev[component];
  316. }
  317. #define MKVAL(vals) (vals[0] | (vals[1] << 3) | (vals[2] << 6))
  318. /* Image mode - the hardest to comprehend MSS4 coding mode.
  319. *
  320. * In this mode all three 16x16 blocks are coded together with a method
  321. * remotely similar to the methods employed in MSS1-MSS3.
  322. * The idea is that every component has a vector of 1-4 most common symbols
  323. * and an escape mode for reading new value from the bitstream. Decoding
  324. * consists of retrieving pixel values from the vector or reading new ones
  325. * from the bitstream; depending on flags read from the bitstream, these vector
  326. * positions can be updated or reused from the state of the previous line
  327. * or previous pixel.
  328. */
  329. static int mss4_decode_image_block(MSS4Context *ctx, GetBitContext *gb,
  330. uint8_t *picdst[3], int mb_x, int mb_y)
  331. {
  332. uint8_t vec[3][4];
  333. int vec_len[3];
  334. int sel_len[3], sel_flag[3];
  335. int i, j, k, mode, split;
  336. int prev_vec1 = 0, prev_split = 0;
  337. int vals[3] = { 0 };
  338. int prev_pix[3] = { 0 };
  339. int prev_mode[16] = { 0 };
  340. uint8_t *dst[3];
  341. const int val_shift = ctx->quality == 100 ? 0 : 2;
  342. for (i = 0; i < 3; i++)
  343. dst[i] = ctx->imgbuf[i];
  344. for (i = 0; i < 3; i++) {
  345. vec_len[i] = vec_len_syms[!!i][get_unary(gb, 0, 3)];
  346. for (j = 0; j < vec_len[i]; j++) {
  347. vec[i][j] = get_coeff(gb, &ctx->vec_entry_vlc[!!i]);
  348. vec[i][j] += ctx->prev_vec[i][j];
  349. ctx->prev_vec[i][j] = vec[i][j];
  350. }
  351. sel_flag[i] = vec_len[i] > 1;
  352. sel_len[i] = vec_len[i] > 2 ? vec_len[i] - 2 : 0;
  353. }
  354. for (j = 0; j < 16; j++) {
  355. if (get_bits1(gb)) {
  356. split = 0;
  357. if (get_bits1(gb)) {
  358. prev_mode[0] = 0;
  359. vals[0] = vals[1] = vals[2] = 0;
  360. mode = 2;
  361. } else {
  362. mode = get_bits1(gb);
  363. if (mode)
  364. split = get_bits(gb, 4);
  365. }
  366. for (i = 0; i < 16; i++) {
  367. if (mode <= 1) {
  368. vals[0] = prev_mode[i] & 7;
  369. vals[1] = (prev_mode[i] >> 3) & 7;
  370. vals[2] = prev_mode[i] >> 6;
  371. if (mode == 1 && i == split) {
  372. read_vec_pos(gb, vals, sel_flag, sel_len, vals);
  373. }
  374. } else if (mode == 2) {
  375. if (get_bits1(gb))
  376. read_vec_pos(gb, vals, sel_flag, sel_len, vals);
  377. }
  378. for (k = 0; k < 3; k++)
  379. *dst[k]++ = get_value_cached(gb, vals[k], vec[k],
  380. vec_len[k], k,
  381. val_shift, prev_pix);
  382. prev_mode[i] = MKVAL(vals);
  383. }
  384. } else {
  385. if (get_bits1(gb)) {
  386. split = get_bits(gb, 4);
  387. if (split >= prev_split)
  388. split++;
  389. prev_split = split;
  390. } else {
  391. split = prev_split;
  392. }
  393. if (split) {
  394. vals[0] = prev_mode[0] & 7;
  395. vals[1] = (prev_mode[0] >> 3) & 7;
  396. vals[2] = prev_mode[0] >> 6;
  397. for (i = 0; i < 3; i++) {
  398. for (k = 0; k < split; k++) {
  399. *dst[i]++ = get_value_cached(gb, vals[i], vec[i],
  400. vec_len[i], i, val_shift,
  401. prev_pix);
  402. prev_mode[k] = MKVAL(vals);
  403. }
  404. }
  405. }
  406. if (split != 16) {
  407. vals[0] = prev_vec1 & 7;
  408. vals[1] = (prev_vec1 >> 3) & 7;
  409. vals[2] = prev_vec1 >> 6;
  410. if (get_bits1(gb)) {
  411. read_vec_pos(gb, vals, sel_flag, sel_len, vals);
  412. prev_vec1 = MKVAL(vals);
  413. }
  414. for (i = 0; i < 3; i++) {
  415. for (k = 0; k < 16 - split; k++) {
  416. *dst[i]++ = get_value_cached(gb, vals[i], vec[i],
  417. vec_len[i], i, val_shift,
  418. prev_pix);
  419. prev_mode[split + k] = MKVAL(vals);
  420. }
  421. }
  422. }
  423. }
  424. }
  425. for (i = 0; i < 3; i++)
  426. for (j = 0; j < 16; j++)
  427. memcpy(picdst[i] + mb_x * 16 + j * ctx->pic->linesize[i],
  428. ctx->imgbuf[i] + j * 16, 16);
  429. return 0;
  430. }
  431. static inline void mss4_update_dc_cache(MSS4Context *c, int mb_x)
  432. {
  433. int i;
  434. c->dc_cache[0][TOP] = c->prev_dc[0][mb_x * 2 + 1];
  435. c->dc_cache[0][LEFT] = 0;
  436. c->dc_cache[1][TOP] = 0;
  437. c->dc_cache[1][LEFT] = 0;
  438. for (i = 0; i < 2; i++)
  439. c->prev_dc[0][mb_x * 2 + i] = 0;
  440. for (i = 1; i < 3; i++) {
  441. c->dc_cache[i + 1][TOP] = c->prev_dc[i][mb_x];
  442. c->dc_cache[i + 1][LEFT] = 0;
  443. c->prev_dc[i][mb_x] = 0;
  444. }
  445. }
  446. static int mss4_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  447. AVPacket *avpkt)
  448. {
  449. const uint8_t *buf = avpkt->data;
  450. int buf_size = avpkt->size;
  451. MSS4Context *c = avctx->priv_data;
  452. GetBitContext gb;
  453. GetByteContext bc;
  454. uint8_t *dst[3];
  455. int width, height, quality, frame_type;
  456. int x, y, i, mb_width, mb_height, blk_type;
  457. int ret;
  458. if (buf_size < HEADER_SIZE) {
  459. av_log(avctx, AV_LOG_ERROR,
  460. "Frame should have at least %d bytes, got %d instead\n",
  461. HEADER_SIZE, buf_size);
  462. return AVERROR_INVALIDDATA;
  463. }
  464. bytestream2_init(&bc, buf, buf_size);
  465. width = bytestream2_get_be16(&bc);
  466. height = bytestream2_get_be16(&bc);
  467. bytestream2_skip(&bc, 2);
  468. quality = bytestream2_get_byte(&bc);
  469. frame_type = bytestream2_get_byte(&bc);
  470. if (width > avctx->width ||
  471. height != avctx->height) {
  472. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\n",
  473. width, height);
  474. return AVERROR_INVALIDDATA;
  475. }
  476. if (quality < 1 || quality > 100) {
  477. av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
  478. return AVERROR_INVALIDDATA;
  479. }
  480. if ((frame_type & ~3) || frame_type == 3) {
  481. av_log(avctx, AV_LOG_ERROR, "Invalid frame type %d\n", frame_type);
  482. return AVERROR_INVALIDDATA;
  483. }
  484. if (frame_type != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) {
  485. av_log(avctx, AV_LOG_ERROR,
  486. "Empty frame found but it is not a skip frame.\n");
  487. return AVERROR_INVALIDDATA;
  488. }
  489. if ((ret = ff_reget_buffer(avctx, c->pic)) < 0) {
  490. av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
  491. return ret;
  492. }
  493. c->pic->key_frame = (frame_type == INTRA_FRAME);
  494. c->pic->pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I
  495. : AV_PICTURE_TYPE_P;
  496. if (frame_type == SKIP_FRAME) {
  497. *got_frame = 1;
  498. if ((ret = av_frame_ref(data, c->pic)) < 0)
  499. return ret;
  500. return buf_size;
  501. }
  502. if (c->quality != quality) {
  503. c->quality = quality;
  504. for (i = 0; i < 2; i++)
  505. ff_mss34_gen_quant_mat(c->quant_mat[i], quality, !i);
  506. }
  507. init_get_bits(&gb, buf + HEADER_SIZE, (buf_size - HEADER_SIZE) * 8);
  508. mb_width = FFALIGN(width, 16) >> 4;
  509. mb_height = FFALIGN(height, 16) >> 4;
  510. dst[0] = c->pic->data[0];
  511. dst[1] = c->pic->data[1];
  512. dst[2] = c->pic->data[2];
  513. memset(c->prev_vec, 0, sizeof(c->prev_vec));
  514. for (y = 0; y < mb_height; y++) {
  515. memset(c->dc_cache, 0, sizeof(c->dc_cache));
  516. for (x = 0; x < mb_width; x++) {
  517. blk_type = decode012(&gb);
  518. switch (blk_type) {
  519. case DCT_BLOCK:
  520. if (mss4_decode_dct_block(c, &gb, dst, x, y) < 0) {
  521. av_log(avctx, AV_LOG_ERROR,
  522. "Error decoding DCT block %d,%d\n",
  523. x, y);
  524. return AVERROR_INVALIDDATA;
  525. }
  526. break;
  527. case IMAGE_BLOCK:
  528. if (mss4_decode_image_block(c, &gb, dst, x, y) < 0) {
  529. av_log(avctx, AV_LOG_ERROR,
  530. "Error decoding VQ block %d,%d\n",
  531. x, y);
  532. return AVERROR_INVALIDDATA;
  533. }
  534. break;
  535. case SKIP_BLOCK:
  536. if (frame_type == INTRA_FRAME) {
  537. av_log(avctx, AV_LOG_ERROR, "Skip block in intra frame\n");
  538. return AVERROR_INVALIDDATA;
  539. }
  540. break;
  541. }
  542. if (blk_type != DCT_BLOCK)
  543. mss4_update_dc_cache(c, x);
  544. }
  545. dst[0] += c->pic->linesize[0] * 16;
  546. dst[1] += c->pic->linesize[1] * 16;
  547. dst[2] += c->pic->linesize[2] * 16;
  548. }
  549. if ((ret = av_frame_ref(data, c->pic)) < 0)
  550. return ret;
  551. *got_frame = 1;
  552. return buf_size;
  553. }
  554. static av_cold int mss4_decode_end(AVCodecContext *avctx)
  555. {
  556. MSS4Context * const c = avctx->priv_data;
  557. int i;
  558. av_frame_free(&c->pic);
  559. for (i = 0; i < 3; i++)
  560. av_freep(&c->prev_dc[i]);
  561. mss4_free_vlcs(c);
  562. return 0;
  563. }
  564. static av_cold int mss4_decode_init(AVCodecContext *avctx)
  565. {
  566. MSS4Context * const c = avctx->priv_data;
  567. int i;
  568. if (mss4_init_vlcs(c)) {
  569. av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
  570. mss4_free_vlcs(c);
  571. return AVERROR(ENOMEM);
  572. }
  573. for (i = 0; i < 3; i++) {
  574. c->dc_stride[i] = FFALIGN(avctx->width, 16) >> (2 + !!i);
  575. c->prev_dc[i] = av_malloc(sizeof(**c->prev_dc) * c->dc_stride[i]);
  576. if (!c->prev_dc[i]) {
  577. av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
  578. mss4_free_vlcs(c);
  579. return AVERROR(ENOMEM);
  580. }
  581. }
  582. c->pic = av_frame_alloc();
  583. if (!c->pic) {
  584. mss4_decode_end(avctx);
  585. return AVERROR(ENOMEM);
  586. }
  587. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  588. return 0;
  589. }
  590. AVCodec ff_mts2_decoder = {
  591. .name = "mts2",
  592. .long_name = NULL_IF_CONFIG_SMALL("MS Expression Encoder Screen"),
  593. .type = AVMEDIA_TYPE_VIDEO,
  594. .id = AV_CODEC_ID_MTS2,
  595. .priv_data_size = sizeof(MSS4Context),
  596. .init = mss4_decode_init,
  597. .close = mss4_decode_end,
  598. .decode = mss4_decode_frame,
  599. .capabilities = AV_CODEC_CAP_DR1,
  600. };