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.

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