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.

679 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 FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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.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)
  128. {
  129. uint8_t bits[MAX_ENTRIES];
  130. int i, j;
  131. int idx = 0;
  132. for (i = 0; i < 16; i++) {
  133. for (j = 0; j < lens[i]; j++) {
  134. bits[idx] = i + 1;
  135. idx++;
  136. }
  137. }
  138. return ff_init_vlc_from_lengths(vlc, FFMIN(bits[idx - 1], 9), idx,
  139. bits, 1, syms, 1, 1, 0, 0, NULL);
  140. }
  141. static av_cold int mss4_init_vlcs(MSS4Context *ctx)
  142. {
  143. int ret, i;
  144. for (i = 0; i < 2; i++) {
  145. ret = mss4_init_vlc(&ctx->dc_vlc[i], mss4_dc_vlc_lens[i], NULL);
  146. if (ret)
  147. return ret;
  148. ret = mss4_init_vlc(&ctx->ac_vlc[i], mss4_ac_vlc_lens[i],
  149. mss4_ac_vlc_syms[i]);
  150. if (ret)
  151. return ret;
  152. ret = mss4_init_vlc(&ctx->vec_entry_vlc[i], mss4_vec_entry_vlc_lens[i],
  153. mss4_vec_entry_vlc_syms[i]);
  154. if (ret)
  155. return ret;
  156. }
  157. return 0;
  158. }
  159. static av_cold void mss4_free_vlcs(MSS4Context *ctx)
  160. {
  161. int i;
  162. for (i = 0; i < 2; i++) {
  163. ff_free_vlc(&ctx->dc_vlc[i]);
  164. ff_free_vlc(&ctx->ac_vlc[i]);
  165. ff_free_vlc(&ctx->vec_entry_vlc[i]);
  166. }
  167. }
  168. /* This function returns values in the range
  169. * (-range + 1; -range/2] U [range/2; range - 1)
  170. * i.e.
  171. * nbits = 0 -> 0
  172. * nbits = 1 -> -1, 1
  173. * nbits = 2 -> -3, -2, 2, 3
  174. */
  175. static av_always_inline int get_coeff_bits(GetBitContext *gb, int nbits)
  176. {
  177. int val;
  178. if (!nbits)
  179. return 0;
  180. val = get_bits(gb, nbits);
  181. if (val < (1 << (nbits - 1)))
  182. val -= (1 << nbits) - 1;
  183. return val;
  184. }
  185. static inline int get_coeff(GetBitContext *gb, VLC *vlc)
  186. {
  187. int val = get_vlc2(gb, vlc->table, vlc->bits, 2);
  188. return get_coeff_bits(gb, val);
  189. }
  190. static int mss4_decode_dct(GetBitContext *gb, VLC *dc_vlc, VLC *ac_vlc,
  191. int *block, int *dc_cache,
  192. int bx, int by, uint16_t *quant_mat)
  193. {
  194. int skip, val, pos = 1, zz_pos, dc;
  195. memset(block, 0, sizeof(*block) * 64);
  196. dc = get_coeff(gb, dc_vlc);
  197. // DC prediction is the same as in MSS3
  198. if (by) {
  199. if (bx) {
  200. int l, tl, t;
  201. l = dc_cache[LEFT];
  202. tl = dc_cache[TOP_LEFT];
  203. t = dc_cache[TOP];
  204. if (FFABS(t - tl) <= FFABS(l - tl))
  205. dc += l;
  206. else
  207. dc += t;
  208. } else {
  209. dc += dc_cache[TOP];
  210. }
  211. } else if (bx) {
  212. dc += dc_cache[LEFT];
  213. }
  214. dc_cache[LEFT] = dc;
  215. block[0] = dc * quant_mat[0];
  216. while (pos < 64) {
  217. val = get_vlc2(gb, ac_vlc->table, 9, 2);
  218. if (!val)
  219. return 0;
  220. if (val == -1)
  221. return -1;
  222. if (val == 0xF0) {
  223. pos += 16;
  224. continue;
  225. }
  226. skip = val >> 4;
  227. val = get_coeff_bits(gb, val & 0xF);
  228. pos += skip;
  229. if (pos >= 64)
  230. return -1;
  231. zz_pos = ff_zigzag_direct[pos];
  232. block[zz_pos] = val * quant_mat[zz_pos];
  233. pos++;
  234. }
  235. return pos == 64 ? 0 : -1;
  236. }
  237. static int mss4_decode_dct_block(MSS4Context *c, GetBitContext *gb,
  238. uint8_t *dst[3], int mb_x, int mb_y)
  239. {
  240. int i, j, k, ret;
  241. uint8_t *out = dst[0];
  242. for (j = 0; j < 2; j++) {
  243. for (i = 0; i < 2; i++) {
  244. int xpos = mb_x * 2 + i;
  245. c->dc_cache[j][TOP_LEFT] = c->dc_cache[j][TOP];
  246. c->dc_cache[j][TOP] = c->prev_dc[0][mb_x * 2 + i];
  247. ret = mss4_decode_dct(gb, c->dc_vlc, c->ac_vlc, c->block,
  248. c->dc_cache[j],
  249. xpos, mb_y * 2 + j, c->quant_mat[0]);
  250. if (ret)
  251. return ret;
  252. c->prev_dc[0][mb_x * 2 + i] = c->dc_cache[j][LEFT];
  253. ff_mss34_dct_put(out + xpos * 8, c->pic->linesize[0],
  254. c->block);
  255. }
  256. out += 8 * c->pic->linesize[0];
  257. }
  258. for (i = 1; i < 3; i++) {
  259. c->dc_cache[i + 1][TOP_LEFT] = c->dc_cache[i + 1][TOP];
  260. c->dc_cache[i + 1][TOP] = c->prev_dc[i][mb_x];
  261. ret = mss4_decode_dct(gb, c->dc_vlc + 1, c->ac_vlc + 1,
  262. c->block, c->dc_cache[i + 1], mb_x, mb_y,
  263. c->quant_mat[1]);
  264. if (ret)
  265. return ret;
  266. c->prev_dc[i][mb_x] = c->dc_cache[i + 1][LEFT];
  267. ff_mss34_dct_put(c->imgbuf[i], 8, c->block);
  268. out = dst[i] + mb_x * 16;
  269. // Since the DCT block is coded as YUV420 and the whole frame as YUV444,
  270. // we need to scale chroma.
  271. for (j = 0; j < 16; j++) {
  272. for (k = 0; k < 8; k++)
  273. AV_WN16A(out + k * 2, c->imgbuf[i][k + (j & ~1) * 4] * 0x101);
  274. out += c->pic->linesize[i];
  275. }
  276. }
  277. return 0;
  278. }
  279. static void read_vec_pos(GetBitContext *gb, int *vec_pos, int *sel_flag,
  280. int *sel_len, int *prev)
  281. {
  282. int i, y_flag = 0;
  283. for (i = 2; i >= 0; i--) {
  284. if (!sel_flag[i]) {
  285. vec_pos[i] = 0;
  286. continue;
  287. }
  288. if ((!i && !y_flag) || get_bits1(gb)) {
  289. if (sel_len[i] > 0) {
  290. int pval = prev[i];
  291. vec_pos[i] = get_bits(gb, sel_len[i]);
  292. if (vec_pos[i] >= pval)
  293. vec_pos[i]++;
  294. } else {
  295. vec_pos[i] = !prev[i];
  296. }
  297. y_flag = 1;
  298. } else {
  299. vec_pos[i] = prev[i];
  300. }
  301. }
  302. }
  303. static int get_value_cached(GetBitContext *gb, int vec_pos, uint8_t *vec,
  304. int vec_size, int component, int shift, int *prev)
  305. {
  306. if (vec_pos < vec_size)
  307. return vec[vec_pos];
  308. if (!get_bits1(gb))
  309. return prev[component];
  310. prev[component] = get_bits(gb, 8 - shift) << shift;
  311. return prev[component];
  312. }
  313. #define MKVAL(vals) ((vals)[0] | ((vals)[1] << 3) | ((vals)[2] << 6))
  314. /* Image mode - the hardest to comprehend MSS4 coding mode.
  315. *
  316. * In this mode all three 16x16 blocks are coded together with a method
  317. * remotely similar to the methods employed in MSS1-MSS3.
  318. * The idea is that every component has a vector of 1-4 most common symbols
  319. * and an escape mode for reading new value from the bitstream. Decoding
  320. * consists of retrieving pixel values from the vector or reading new ones
  321. * from the bitstream; depending on flags read from the bitstream, these vector
  322. * positions can be updated or reused from the state of the previous line
  323. * or previous pixel.
  324. */
  325. static int mss4_decode_image_block(MSS4Context *ctx, GetBitContext *gb,
  326. uint8_t *picdst[3], int mb_x, int mb_y)
  327. {
  328. uint8_t vec[3][4];
  329. int vec_len[3];
  330. int sel_len[3], sel_flag[3];
  331. int i, j, k, mode, split;
  332. int prev_vec1 = 0, prev_split = 0;
  333. int vals[3] = { 0 };
  334. int prev_pix[3] = { 0 };
  335. int prev_mode[16] = { 0 };
  336. uint8_t *dst[3];
  337. const int val_shift = ctx->quality == 100 ? 0 : 2;
  338. for (i = 0; i < 3; i++)
  339. dst[i] = ctx->imgbuf[i];
  340. for (i = 0; i < 3; i++) {
  341. vec_len[i] = vec_len_syms[!!i][get_unary(gb, 0, 3)];
  342. for (j = 0; j < vec_len[i]; j++) {
  343. vec[i][j] = get_coeff(gb, &ctx->vec_entry_vlc[!!i]);
  344. vec[i][j] += ctx->prev_vec[i][j];
  345. ctx->prev_vec[i][j] = vec[i][j];
  346. }
  347. sel_flag[i] = vec_len[i] > 1;
  348. sel_len[i] = vec_len[i] > 2 ? vec_len[i] - 2 : 0;
  349. }
  350. for (j = 0; j < 16; j++) {
  351. if (get_bits1(gb)) {
  352. split = 0;
  353. if (get_bits1(gb)) {
  354. prev_mode[0] = 0;
  355. vals[0] = vals[1] = vals[2] = 0;
  356. mode = 2;
  357. } else {
  358. mode = get_bits1(gb);
  359. if (mode)
  360. split = get_bits(gb, 4);
  361. }
  362. for (i = 0; i < 16; i++) {
  363. if (mode <= 1) {
  364. vals[0] = prev_mode[i] & 7;
  365. vals[1] = (prev_mode[i] >> 3) & 7;
  366. vals[2] = prev_mode[i] >> 6;
  367. if (mode == 1 && i == split) {
  368. read_vec_pos(gb, vals, sel_flag, sel_len, vals);
  369. }
  370. } else if (mode == 2) {
  371. if (get_bits1(gb))
  372. read_vec_pos(gb, vals, sel_flag, sel_len, vals);
  373. }
  374. for (k = 0; k < 3; k++)
  375. *dst[k]++ = get_value_cached(gb, vals[k], vec[k],
  376. vec_len[k], k,
  377. val_shift, prev_pix);
  378. prev_mode[i] = MKVAL(vals);
  379. }
  380. } else {
  381. if (get_bits1(gb)) {
  382. split = get_bits(gb, 4);
  383. if (split >= prev_split)
  384. split++;
  385. prev_split = split;
  386. } else {
  387. split = prev_split;
  388. }
  389. if (split) {
  390. vals[0] = prev_mode[0] & 7;
  391. vals[1] = (prev_mode[0] >> 3) & 7;
  392. vals[2] = prev_mode[0] >> 6;
  393. for (i = 0; i < 3; i++) {
  394. for (k = 0; k < split; k++) {
  395. *dst[i]++ = get_value_cached(gb, vals[i], vec[i],
  396. vec_len[i], i, val_shift,
  397. prev_pix);
  398. prev_mode[k] = MKVAL(vals);
  399. }
  400. }
  401. }
  402. if (split != 16) {
  403. vals[0] = prev_vec1 & 7;
  404. vals[1] = (prev_vec1 >> 3) & 7;
  405. vals[2] = prev_vec1 >> 6;
  406. if (get_bits1(gb)) {
  407. read_vec_pos(gb, vals, sel_flag, sel_len, vals);
  408. prev_vec1 = MKVAL(vals);
  409. }
  410. for (i = 0; i < 3; i++) {
  411. for (k = 0; k < 16 - split; k++) {
  412. *dst[i]++ = get_value_cached(gb, vals[i], vec[i],
  413. vec_len[i], i, val_shift,
  414. prev_pix);
  415. prev_mode[split + k] = MKVAL(vals);
  416. }
  417. }
  418. }
  419. }
  420. }
  421. for (i = 0; i < 3; i++)
  422. for (j = 0; j < 16; j++)
  423. memcpy(picdst[i] + mb_x * 16 + j * ctx->pic->linesize[i],
  424. ctx->imgbuf[i] + j * 16, 16);
  425. return 0;
  426. }
  427. static inline void mss4_update_dc_cache(MSS4Context *c, int mb_x)
  428. {
  429. int i;
  430. c->dc_cache[0][TOP] = c->prev_dc[0][mb_x * 2 + 1];
  431. c->dc_cache[0][LEFT] = 0;
  432. c->dc_cache[1][TOP] = 0;
  433. c->dc_cache[1][LEFT] = 0;
  434. for (i = 0; i < 2; i++)
  435. c->prev_dc[0][mb_x * 2 + i] = 0;
  436. for (i = 1; i < 3; i++) {
  437. c->dc_cache[i + 1][TOP] = c->prev_dc[i][mb_x];
  438. c->dc_cache[i + 1][LEFT] = 0;
  439. c->prev_dc[i][mb_x] = 0;
  440. }
  441. }
  442. static int mss4_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  443. AVPacket *avpkt)
  444. {
  445. const uint8_t *buf = avpkt->data;
  446. int buf_size = avpkt->size;
  447. MSS4Context *c = avctx->priv_data;
  448. GetBitContext gb;
  449. GetByteContext bc;
  450. uint8_t *dst[3];
  451. int width, height, quality, frame_type;
  452. int x, y, i, mb_width, mb_height, blk_type;
  453. int ret;
  454. if (buf_size < HEADER_SIZE) {
  455. av_log(avctx, AV_LOG_ERROR,
  456. "Frame should have at least %d bytes, got %d instead\n",
  457. HEADER_SIZE, buf_size);
  458. return AVERROR_INVALIDDATA;
  459. }
  460. bytestream2_init(&bc, buf, buf_size);
  461. width = bytestream2_get_be16(&bc);
  462. height = bytestream2_get_be16(&bc);
  463. bytestream2_skip(&bc, 2);
  464. quality = bytestream2_get_byte(&bc);
  465. frame_type = bytestream2_get_byte(&bc);
  466. if (width > avctx->width ||
  467. height != avctx->height) {
  468. av_log(avctx, AV_LOG_ERROR, "Invalid frame dimensions %dx%d\n",
  469. width, height);
  470. return AVERROR_INVALIDDATA;
  471. }
  472. if (quality < 1 || quality > 100) {
  473. av_log(avctx, AV_LOG_ERROR, "Invalid quality setting %d\n", quality);
  474. return AVERROR_INVALIDDATA;
  475. }
  476. if ((frame_type & ~3) || frame_type == 3) {
  477. av_log(avctx, AV_LOG_ERROR, "Invalid frame type %d\n", frame_type);
  478. return AVERROR_INVALIDDATA;
  479. }
  480. if (frame_type != SKIP_FRAME && !bytestream2_get_bytes_left(&bc)) {
  481. av_log(avctx, AV_LOG_ERROR,
  482. "Empty frame found but it is not a skip frame.\n");
  483. return AVERROR_INVALIDDATA;
  484. }
  485. mb_width = FFALIGN(width, 16) >> 4;
  486. mb_height = FFALIGN(height, 16) >> 4;
  487. if (frame_type != SKIP_FRAME && 8*buf_size < 8*HEADER_SIZE + mb_width*mb_height)
  488. return AVERROR_INVALIDDATA;
  489. if ((ret = ff_reget_buffer(avctx, c->pic, 0)) < 0)
  490. return ret;
  491. c->pic->key_frame = (frame_type == INTRA_FRAME);
  492. c->pic->pict_type = (frame_type == INTRA_FRAME) ? AV_PICTURE_TYPE_I
  493. : AV_PICTURE_TYPE_P;
  494. if (frame_type == SKIP_FRAME) {
  495. *got_frame = 1;
  496. if ((ret = av_frame_ref(data, c->pic)) < 0)
  497. return ret;
  498. return buf_size;
  499. }
  500. if (c->quality != quality) {
  501. c->quality = quality;
  502. for (i = 0; i < 2; i++)
  503. ff_mss34_gen_quant_mat(c->quant_mat[i], quality, !i);
  504. }
  505. if ((ret = init_get_bits8(&gb, buf + HEADER_SIZE, buf_size - HEADER_SIZE)) < 0)
  506. return ret;
  507. dst[0] = c->pic->data[0];
  508. dst[1] = c->pic->data[1];
  509. dst[2] = c->pic->data[2];
  510. memset(c->prev_vec, 0, sizeof(c->prev_vec));
  511. for (y = 0; y < mb_height; y++) {
  512. memset(c->dc_cache, 0, sizeof(c->dc_cache));
  513. for (x = 0; x < mb_width; x++) {
  514. blk_type = decode012(&gb);
  515. switch (blk_type) {
  516. case DCT_BLOCK:
  517. if (mss4_decode_dct_block(c, &gb, dst, x, y) < 0) {
  518. av_log(avctx, AV_LOG_ERROR,
  519. "Error decoding DCT block %d,%d\n",
  520. x, y);
  521. return AVERROR_INVALIDDATA;
  522. }
  523. break;
  524. case IMAGE_BLOCK:
  525. if (mss4_decode_image_block(c, &gb, dst, x, y) < 0) {
  526. av_log(avctx, AV_LOG_ERROR,
  527. "Error decoding VQ block %d,%d\n",
  528. x, y);
  529. return AVERROR_INVALIDDATA;
  530. }
  531. break;
  532. case SKIP_BLOCK:
  533. if (frame_type == INTRA_FRAME) {
  534. av_log(avctx, AV_LOG_ERROR, "Skip block in intra frame\n");
  535. return AVERROR_INVALIDDATA;
  536. }
  537. break;
  538. }
  539. if (blk_type != DCT_BLOCK)
  540. mss4_update_dc_cache(c, x);
  541. }
  542. dst[0] += c->pic->linesize[0] * 16;
  543. dst[1] += c->pic->linesize[1] * 16;
  544. dst[2] += c->pic->linesize[2] * 16;
  545. }
  546. if ((ret = av_frame_ref(data, c->pic)) < 0)
  547. return ret;
  548. *got_frame = 1;
  549. return buf_size;
  550. }
  551. static av_cold int mss4_decode_end(AVCodecContext *avctx)
  552. {
  553. MSS4Context * const c = avctx->priv_data;
  554. int i;
  555. av_frame_free(&c->pic);
  556. for (i = 0; i < 3; i++)
  557. av_freep(&c->prev_dc[i]);
  558. mss4_free_vlcs(c);
  559. return 0;
  560. }
  561. static av_cold int mss4_decode_init(AVCodecContext *avctx)
  562. {
  563. MSS4Context * const c = avctx->priv_data;
  564. int i;
  565. if (mss4_init_vlcs(c)) {
  566. av_log(avctx, AV_LOG_ERROR, "Cannot initialise VLCs\n");
  567. return AVERROR(ENOMEM);
  568. }
  569. for (i = 0; i < 3; i++) {
  570. c->dc_stride[i] = FFALIGN(avctx->width, 16) >> (2 + !!i);
  571. c->prev_dc[i] = av_malloc_array(c->dc_stride[i], sizeof(**c->prev_dc));
  572. if (!c->prev_dc[i]) {
  573. av_log(avctx, AV_LOG_ERROR, "Cannot allocate buffer\n");
  574. return AVERROR(ENOMEM);
  575. }
  576. }
  577. c->pic = av_frame_alloc();
  578. if (!c->pic)
  579. return AVERROR(ENOMEM);
  580. avctx->pix_fmt = AV_PIX_FMT_YUV444P;
  581. return 0;
  582. }
  583. AVCodec ff_mts2_decoder = {
  584. .name = "mts2",
  585. .long_name = NULL_IF_CONFIG_SMALL("MS Expression Encoder Screen"),
  586. .type = AVMEDIA_TYPE_VIDEO,
  587. .id = AV_CODEC_ID_MTS2,
  588. .priv_data_size = sizeof(MSS4Context),
  589. .init = mss4_decode_init,
  590. .close = mss4_decode_end,
  591. .decode = mss4_decode_frame,
  592. .capabilities = AV_CODEC_CAP_DR1,
  593. .caps_internal = FF_CODEC_CAP_INIT_CLEANUP | FF_CODEC_CAP_INIT_THREADSAFE,
  594. };