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.

986 lines
32KB

  1. /*
  2. * 4XM codec
  3. * Copyright (c) 2003 Michael Niedermayer
  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. * 4XM codec.
  24. */
  25. #include <inttypes.h>
  26. #include "libavutil/frame.h"
  27. #include "libavutil/imgutils.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "avcodec.h"
  30. #include "blockdsp.h"
  31. #include "bswapdsp.h"
  32. #include "bytestream.h"
  33. #include "get_bits.h"
  34. #include "internal.h"
  35. #define BLOCK_TYPE_VLC_BITS 5
  36. #define ACDC_VLC_BITS 9
  37. #define CFRAME_BUFFER_COUNT 100
  38. static const uint8_t block_type_tab[2][4][8][2] = {
  39. {
  40. { // { 8, 4, 2 } x { 8, 4, 2}
  41. { 0, 1 }, { 2, 2 }, { 6, 3 }, { 14, 4 }, { 30, 5 }, { 31, 5 }, { 0, 0 }
  42. }, { // { 8, 4 } x 1
  43. { 0, 1 }, { 0, 0 }, { 2, 2 }, { 6, 3 }, { 14, 4 }, { 15, 4 }, { 0, 0 }
  44. }, { // 1 x { 8, 4 }
  45. { 0, 1 }, { 2, 2 }, { 0, 0 }, { 6, 3 }, { 14, 4 }, { 15, 4 }, { 0, 0 }
  46. }, { // 1 x 2, 2 x 1
  47. { 0, 1 }, { 0, 0 }, { 0, 0 }, { 2, 2 }, { 6, 3 }, { 14, 4 }, { 15, 4 }
  48. }
  49. }, {
  50. { // { 8, 4, 2 } x { 8, 4, 2}
  51. { 1, 2 }, { 4, 3 }, { 5, 3 }, { 0, 2 }, { 6, 3 }, { 7, 3 }, { 0, 0 }
  52. }, {// { 8, 4 } x 1
  53. { 1, 2 }, { 0, 0 }, { 2, 2 }, { 0, 2 }, { 6, 3 }, { 7, 3 }, { 0, 0 }
  54. }, {// 1 x { 8, 4 }
  55. { 1, 2 }, { 2, 2 }, { 0, 0 }, { 0, 2 }, { 6, 3 }, { 7, 3 }, { 0, 0 }
  56. }, {// 1 x 2, 2 x 1
  57. { 1, 2 }, { 0, 0 }, { 0, 0 }, { 0, 2 }, { 2, 2 }, { 6, 3 }, { 7, 3 }
  58. }
  59. }
  60. };
  61. static const uint8_t size2index[4][4] = {
  62. { -1, 3, 1, 1 },
  63. { 3, 0, 0, 0 },
  64. { 2, 0, 0, 0 },
  65. { 2, 0, 0, 0 },
  66. };
  67. static const int8_t mv[256][2] = {
  68. { 0, 0 }, { 0, -1 }, { -1, 0 }, { 1, 0 }, { 0, 1 }, { -1, -1 }, { 1, -1 }, { -1, 1 },
  69. { 1, 1 }, { 0, -2 }, { -2, 0 }, { 2, 0 }, { 0, 2 }, { -1, -2 }, { 1, -2 }, { -2, -1 },
  70. { 2, -1 }, { -2, 1 }, { 2, 1 }, { -1, 2 }, { 1, 2 }, { -2, -2 }, { 2, -2 }, { -2, 2 },
  71. { 2, 2 }, { 0, -3 }, { -3, 0 }, { 3, 0 }, { 0, 3 }, { -1, -3 }, { 1, -3 }, { -3, -1 },
  72. { 3, -1 }, { -3, 1 }, { 3, 1 }, { -1, 3 }, { 1, 3 }, { -2, -3 }, { 2, -3 }, { -3, -2 },
  73. { 3, -2 }, { -3, 2 }, { 3, 2 }, { -2, 3 }, { 2, 3 }, { 0, -4 }, { -4, 0 }, { 4, 0 },
  74. { 0, 4 }, { -1, -4 }, { 1, -4 }, { -4, -1 }, { 4, -1 }, { 4, 1 }, { -1, 4 }, { 1, 4 },
  75. { -3, -3 }, { -3, 3 }, { 3, 3 }, { -2, -4 }, { -4, -2 }, { 4, -2 }, { -4, 2 }, { -2, 4 },
  76. { 2, 4 }, { -3, -4 }, { 3, -4 }, { 4, -3 }, { -5, 0 }, { -4, 3 }, { -3, 4 }, { 3, 4 },
  77. { -1, -5 }, { -5, -1 }, { -5, 1 }, { -1, 5 }, { -2, -5 }, { 2, -5 }, { 5, -2 }, { 5, 2 },
  78. { -4, -4 }, { -4, 4 }, { -3, -5 }, { -5, -3 }, { -5, 3 }, { 3, 5 }, { -6, 0 }, { 0, 6 },
  79. { -6, -1 }, { -6, 1 }, { 1, 6 }, { 2, -6 }, { -6, 2 }, { 2, 6 }, { -5, -4 }, { 5, 4 },
  80. { 4, 5 }, { -6, -3 }, { 6, 3 }, { -7, 0 }, { -1, -7 }, { 5, -5 }, { -7, 1 }, { -1, 7 },
  81. { 4, -6 }, { 6, 4 }, { -2, -7 }, { -7, 2 }, { -3, -7 }, { 7, -3 }, { 3, 7 }, { 6, -5 },
  82. { 0, -8 }, { -1, -8 }, { -7, -4 }, { -8, 1 }, { 4, 7 }, { 2, -8 }, { -2, 8 }, { 6, 6 },
  83. { -8, 3 }, { 5, -7 }, { -5, 7 }, { 8, -4 }, { 0, -9 }, { -9, -1 }, { 1, 9 }, { 7, -6 },
  84. { -7, 6 }, { -5, -8 }, { -5, 8 }, { -9, 3 }, { 9, -4 }, { 7, -7 }, { 8, -6 }, { 6, 8 },
  85. { 10, 1 }, { -10, 2 }, { 9, -5 }, { 10, -3 }, { -8, -7 }, { -10, -4 }, { 6, -9 }, { -11, 0 },
  86. { 11, 1 }, { -11, -2 }, { -2, 11 }, { 7, -9 }, { -7, 9 }, { 10, 6 }, { -4, 11 }, { 8, -9 },
  87. { 8, 9 }, { 5, 11 }, { 7, -10 }, { 12, -3 }, { 11, 6 }, { -9, -9 }, { 8, 10 }, { 5, 12 },
  88. { -11, 7 }, { 13, 2 }, { 6, -12 }, { 10, 9 }, { -11, 8 }, { -7, 12 }, { 0, 14 }, { 14, -2 },
  89. { -9, 11 }, { -6, 13 }, { -14, -4 }, { -5, -14 }, { 5, 14 }, { -15, -1 }, { -14, -6 }, { 3, -15 },
  90. { 11, -11 }, { -7, 14 }, { -5, 15 }, { 8, -14 }, { 15, 6 }, { 3, 16 }, { 7, -15 }, { -16, 5 },
  91. { 0, 17 }, { -16, -6 }, { -10, 14 }, { -16, 7 }, { 12, 13 }, { -16, 8 }, { -17, 6 }, { -18, 3 },
  92. { -7, 17 }, { 15, 11 }, { 16, 10 }, { 2, -19 }, { 3, -19 }, { -11, -16 }, { -18, 8 }, { -19, -6 },
  93. { 2, -20 }, { -17, -11 }, { -10, -18 }, { 8, 19 }, { -21, -1 }, { -20, 7 }, { -4, 21 }, { 21, 5 },
  94. { 15, 16 }, { 2, -22 }, { -10, -20 }, { -22, 5 }, { 20, -11 }, { -7, -22 }, { -12, 20 }, { 23, -5 },
  95. { 13, -20 }, { 24, -2 }, { -15, 19 }, { -11, 22 }, { 16, 19 }, { 23, -10 }, { -18, -18 }, { -9, -24 },
  96. { 24, -10 }, { -3, 26 }, { -23, 13 }, { -18, -20 }, { 17, 21 }, { -4, 27 }, { 27, 6 }, { 1, -28 },
  97. { -11, 26 }, { -17, -23 }, { 7, 28 }, { 11, -27 }, { 29, 5 }, { -23, -19 }, { -28, -11 }, { -21, 22 },
  98. { -30, 7 }, { -17, 26 }, { -27, 16 }, { 13, 29 }, { 19, -26 }, { 10, -31 }, { -14, -30 }, { 20, -27 },
  99. { -29, 18 }, { -16, -31 }, { -28, -22 }, { 21, -30 }, { -25, 28 }, { 26, -29 }, { 25, -32 }, { -32, -32 }
  100. };
  101. /* This is simply the scaled down elementwise product of the standard JPEG
  102. * quantizer table and the AAN premul table. */
  103. static const uint8_t dequant_table[64] = {
  104. 16, 15, 13, 19, 24, 31, 28, 17,
  105. 17, 23, 25, 31, 36, 63, 45, 21,
  106. 18, 24, 27, 37, 52, 59, 49, 20,
  107. 16, 28, 34, 40, 60, 80, 51, 20,
  108. 18, 31, 48, 66, 68, 86, 56, 21,
  109. 19, 38, 56, 59, 64, 64, 48, 20,
  110. 27, 48, 55, 55, 56, 51, 35, 15,
  111. 20, 35, 34, 32, 31, 22, 15, 8,
  112. };
  113. static VLC block_type_vlc[2][4];
  114. typedef struct CFrameBuffer {
  115. unsigned int allocated_size;
  116. unsigned int size;
  117. int id;
  118. uint8_t *data;
  119. } CFrameBuffer;
  120. typedef struct FourXContext {
  121. AVCodecContext *avctx;
  122. BlockDSPContext bdsp;
  123. BswapDSPContext bbdsp;
  124. uint16_t *frame_buffer;
  125. uint16_t *last_frame_buffer;
  126. GetBitContext pre_gb; ///< ac/dc prefix
  127. GetBitContext gb;
  128. GetByteContext g;
  129. GetByteContext g2;
  130. int mv[256];
  131. VLC pre_vlc;
  132. int last_dc;
  133. DECLARE_ALIGNED(16, int16_t, block)[6][64];
  134. void *bitstream_buffer;
  135. unsigned int bitstream_buffer_size;
  136. int version;
  137. CFrameBuffer cfrm[CFRAME_BUFFER_COUNT];
  138. } FourXContext;
  139. #define FIX_1_082392200 70936
  140. #define FIX_1_414213562 92682
  141. #define FIX_1_847759065 121095
  142. #define FIX_2_613125930 171254
  143. #define MULTIPLY(var, const) (((var) * (const)) >> 16)
  144. static void idct(int16_t block[64])
  145. {
  146. int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  147. int tmp10, tmp11, tmp12, tmp13;
  148. int z5, z10, z11, z12, z13;
  149. int i;
  150. int temp[64];
  151. for (i = 0; i < 8; i++) {
  152. tmp10 = block[8 * 0 + i] + block[8 * 4 + i];
  153. tmp11 = block[8 * 0 + i] - block[8 * 4 + i];
  154. tmp13 = block[8 * 2 + i] + block[8 * 6 + i];
  155. tmp12 = MULTIPLY(block[8 * 2 + i] - block[8 * 6 + i], FIX_1_414213562) - tmp13;
  156. tmp0 = tmp10 + tmp13;
  157. tmp3 = tmp10 - tmp13;
  158. tmp1 = tmp11 + tmp12;
  159. tmp2 = tmp11 - tmp12;
  160. z13 = block[8 * 5 + i] + block[8 * 3 + i];
  161. z10 = block[8 * 5 + i] - block[8 * 3 + i];
  162. z11 = block[8 * 1 + i] + block[8 * 7 + i];
  163. z12 = block[8 * 1 + i] - block[8 * 7 + i];
  164. tmp7 = z11 + z13;
  165. tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
  166. z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
  167. tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
  168. tmp12 = MULTIPLY(z10, -FIX_2_613125930) + z5;
  169. tmp6 = tmp12 - tmp7;
  170. tmp5 = tmp11 - tmp6;
  171. tmp4 = tmp10 + tmp5;
  172. temp[8 * 0 + i] = tmp0 + tmp7;
  173. temp[8 * 7 + i] = tmp0 - tmp7;
  174. temp[8 * 1 + i] = tmp1 + tmp6;
  175. temp[8 * 6 + i] = tmp1 - tmp6;
  176. temp[8 * 2 + i] = tmp2 + tmp5;
  177. temp[8 * 5 + i] = tmp2 - tmp5;
  178. temp[8 * 4 + i] = tmp3 + tmp4;
  179. temp[8 * 3 + i] = tmp3 - tmp4;
  180. }
  181. for (i = 0; i < 8 * 8; i += 8) {
  182. tmp10 = temp[0 + i] + temp[4 + i];
  183. tmp11 = temp[0 + i] - temp[4 + i];
  184. tmp13 = temp[2 + i] + temp[6 + i];
  185. tmp12 = MULTIPLY(temp[2 + i] - temp[6 + i], FIX_1_414213562) - tmp13;
  186. tmp0 = tmp10 + tmp13;
  187. tmp3 = tmp10 - tmp13;
  188. tmp1 = tmp11 + tmp12;
  189. tmp2 = tmp11 - tmp12;
  190. z13 = temp[5 + i] + temp[3 + i];
  191. z10 = temp[5 + i] - temp[3 + i];
  192. z11 = temp[1 + i] + temp[7 + i];
  193. z12 = temp[1 + i] - temp[7 + i];
  194. tmp7 = z11 + z13;
  195. tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
  196. z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
  197. tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
  198. tmp12 = MULTIPLY(z10, -FIX_2_613125930) + z5;
  199. tmp6 = tmp12 - tmp7;
  200. tmp5 = tmp11 - tmp6;
  201. tmp4 = tmp10 + tmp5;
  202. block[0 + i] = (tmp0 + tmp7) >> 6;
  203. block[7 + i] = (tmp0 - tmp7) >> 6;
  204. block[1 + i] = (tmp1 + tmp6) >> 6;
  205. block[6 + i] = (tmp1 - tmp6) >> 6;
  206. block[2 + i] = (tmp2 + tmp5) >> 6;
  207. block[5 + i] = (tmp2 - tmp5) >> 6;
  208. block[4 + i] = (tmp3 + tmp4) >> 6;
  209. block[3 + i] = (tmp3 - tmp4) >> 6;
  210. }
  211. }
  212. static av_cold void init_vlcs(FourXContext *f)
  213. {
  214. static VLC_TYPE table[2][4][32][2];
  215. int i, j;
  216. for (i = 0; i < 2; i++) {
  217. for (j = 0; j < 4; j++) {
  218. block_type_vlc[i][j].table = table[i][j];
  219. block_type_vlc[i][j].table_allocated = 32;
  220. init_vlc(&block_type_vlc[i][j], BLOCK_TYPE_VLC_BITS, 7,
  221. &block_type_tab[i][j][0][1], 2, 1,
  222. &block_type_tab[i][j][0][0], 2, 1,
  223. INIT_VLC_USE_NEW_STATIC);
  224. }
  225. }
  226. }
  227. static void init_mv(FourXContext *f, int linesize)
  228. {
  229. int i;
  230. for (i = 0; i < 256; i++) {
  231. if (f->version > 1)
  232. f->mv[i] = mv[i][0] + mv[i][1] * linesize / 2;
  233. else
  234. f->mv[i] = (i & 15) - 8 + ((i >> 4) - 8) * linesize / 2;
  235. }
  236. }
  237. #if HAVE_BIGENDIAN
  238. #define LE_CENTRIC_MUL(dst, src, scale, dc) \
  239. { \
  240. unsigned tmpval = AV_RN32(src); \
  241. tmpval = (tmpval << 16) | (tmpval >> 16); \
  242. tmpval = tmpval * (scale) + (dc); \
  243. tmpval = (tmpval << 16) | (tmpval >> 16); \
  244. AV_WN32A(dst, tmpval); \
  245. }
  246. #else
  247. #define LE_CENTRIC_MUL(dst, src, scale, dc) \
  248. { \
  249. unsigned tmpval = AV_RN32(src) * (scale) + (dc); \
  250. AV_WN32A(dst, tmpval); \
  251. }
  252. #endif
  253. static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w,
  254. int h, int stride, int scale, unsigned dc)
  255. {
  256. int i;
  257. dc *= 0x10001;
  258. switch (log2w) {
  259. case 0:
  260. for (i = 0; i < h; i++) {
  261. dst[0] = scale * src[0] + dc;
  262. if (scale)
  263. src += stride;
  264. dst += stride;
  265. }
  266. break;
  267. case 1:
  268. for (i = 0; i < h; i++) {
  269. LE_CENTRIC_MUL(dst, src, scale, dc);
  270. if (scale)
  271. src += stride;
  272. dst += stride;
  273. }
  274. break;
  275. case 2:
  276. for (i = 0; i < h; i++) {
  277. LE_CENTRIC_MUL(dst, src, scale, dc);
  278. LE_CENTRIC_MUL(dst + 2, src + 2, scale, dc);
  279. if (scale)
  280. src += stride;
  281. dst += stride;
  282. }
  283. break;
  284. case 3:
  285. for (i = 0; i < h; i++) {
  286. LE_CENTRIC_MUL(dst, src, scale, dc);
  287. LE_CENTRIC_MUL(dst + 2, src + 2, scale, dc);
  288. LE_CENTRIC_MUL(dst + 4, src + 4, scale, dc);
  289. LE_CENTRIC_MUL(dst + 6, src + 6, scale, dc);
  290. if (scale)
  291. src += stride;
  292. dst += stride;
  293. }
  294. break;
  295. default:
  296. break;
  297. }
  298. }
  299. static int decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src,
  300. int log2w, int log2h, int stride)
  301. {
  302. int index, h, code, ret, scale = 1;
  303. uint16_t *start, *end;
  304. unsigned dc = 0;
  305. if (log2h < 0 || log2w < 0)
  306. return AVERROR_INVALIDDATA;
  307. index = size2index[log2h][log2w];
  308. if (index < 0)
  309. return AVERROR_INVALIDDATA;
  310. h = 1 << log2h;
  311. code = get_vlc2(&f->gb, block_type_vlc[1 - (f->version > 1)][index].table,
  312. BLOCK_TYPE_VLC_BITS, 1);
  313. if (code < 0 || code > 6)
  314. return AVERROR_INVALIDDATA;
  315. start = f->last_frame_buffer;
  316. end = start + stride * (f->avctx->height - h + 1) - (1 << log2w);
  317. if (code == 1) {
  318. if (--log2h < 0)
  319. return AVERROR_INVALIDDATA;
  320. if ((ret = decode_p_block(f, dst, src, log2w, log2h, stride)) < 0)
  321. return ret;
  322. return decode_p_block(f, dst + (stride << log2h),
  323. src + (stride << log2h),
  324. log2w, log2h, stride);
  325. } else if (code == 2) {
  326. log2w--;
  327. if ((ret = decode_p_block(f, dst , src, log2w, log2h, stride)) < 0)
  328. return ret;
  329. return decode_p_block(f, dst + (1 << log2w),
  330. src + (1 << log2w),
  331. log2w, log2h, stride);
  332. } else if (code == 6) {
  333. if (log2w) {
  334. dst[0] = bytestream2_get_le16(&f->g2);
  335. dst[1] = bytestream2_get_le16(&f->g2);
  336. } else {
  337. dst[0] = bytestream2_get_le16(&f->g2);
  338. dst[stride] = bytestream2_get_le16(&f->g2);
  339. }
  340. return 0;
  341. }
  342. if (code == 0) {
  343. src += f->mv[bytestream2_get_byte(&f->g)];
  344. } else if (code == 3 && f->version >= 2) {
  345. return 0;
  346. } else if (code == 4) {
  347. src += f->mv[bytestream2_get_byte(&f->g)];
  348. dc = bytestream2_get_le16(&f->g2);
  349. } else if (code == 5) {
  350. scale = 0;
  351. dc = bytestream2_get_le16(&f->g2);
  352. }
  353. if (start > src || src > end) {
  354. av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
  355. return AVERROR_INVALIDDATA;
  356. }
  357. mcdc(dst, src, log2w, h, stride, scale, dc);
  358. return 0;
  359. }
  360. static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length)
  361. {
  362. int x, y;
  363. const int width = f->avctx->width;
  364. const int height = f->avctx->height;
  365. uint16_t *dst = f->frame_buffer;
  366. uint16_t *src;
  367. unsigned int bitstream_size, bytestream_size, wordstream_size, extra,
  368. bytestream_offset, wordstream_offset;
  369. int ret;
  370. src = f->last_frame_buffer;
  371. if (f->version > 1) {
  372. if (length < 20)
  373. return AVERROR_INVALIDDATA;
  374. extra = 20;
  375. bitstream_size = AV_RL32(buf + 8);
  376. wordstream_size = AV_RL32(buf + 12);
  377. bytestream_size = AV_RL32(buf + 16);
  378. } else {
  379. extra = 0;
  380. bitstream_size = AV_RL16(buf - 4);
  381. wordstream_size = AV_RL16(buf - 2);
  382. bytestream_size = FFMAX(length - bitstream_size - wordstream_size, 0);
  383. }
  384. if (bitstream_size + bytestream_size + wordstream_size + extra != length
  385. || bitstream_size > (1 << 26)
  386. || bytestream_size > (1 << 26)
  387. || wordstream_size > (1 << 26)) {
  388. av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n",
  389. bitstream_size, bytestream_size, wordstream_size,
  390. bitstream_size + bytestream_size + wordstream_size - length);
  391. return AVERROR_INVALIDDATA;
  392. }
  393. av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size,
  394. bitstream_size + AV_INPUT_BUFFER_PADDING_SIZE);
  395. if (!f->bitstream_buffer)
  396. return AVERROR(ENOMEM);
  397. f->bbdsp.bswap_buf(f->bitstream_buffer, (const uint32_t *) (buf + extra),
  398. bitstream_size / 4);
  399. memset((uint8_t*)f->bitstream_buffer + bitstream_size,
  400. 0, AV_INPUT_BUFFER_PADDING_SIZE);
  401. init_get_bits(&f->gb, f->bitstream_buffer, 8 * bitstream_size);
  402. wordstream_offset = extra + bitstream_size;
  403. bytestream_offset = extra + bitstream_size + wordstream_size;
  404. bytestream2_init(&f->g2, buf + wordstream_offset,
  405. length - wordstream_offset);
  406. bytestream2_init(&f->g, buf + bytestream_offset,
  407. length - bytestream_offset);
  408. init_mv(f, width * 2);
  409. for (y = 0; y < height; y += 8) {
  410. for (x = 0; x < width; x += 8)
  411. if ((ret = decode_p_block(f, dst + x, src + x, 3, 3, width)) < 0)
  412. return ret;
  413. src += 8 * width;
  414. dst += 8 * width;
  415. }
  416. return 0;
  417. }
  418. /**
  419. * decode block and dequantize.
  420. * Note this is almost identical to MJPEG.
  421. */
  422. static int decode_i_block(FourXContext *f, int16_t *block)
  423. {
  424. int code, i, j, level, val;
  425. /* DC coef */
  426. val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  427. if (val >> 4)
  428. av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
  429. if (val)
  430. val = get_xbits(&f->gb, val);
  431. val = val * dequant_table[0] + f->last_dc;
  432. f->last_dc = block[0] = val;
  433. /* AC coefs */
  434. i = 1;
  435. for (;;) {
  436. code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  437. /* EOB */
  438. if (code == 0)
  439. break;
  440. if (code == 0xf0) {
  441. i += 16;
  442. } else {
  443. level = get_xbits(&f->gb, code & 0xf);
  444. i += code >> 4;
  445. if (i >= 64) {
  446. av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
  447. return 0;
  448. }
  449. j = ff_zigzag_direct[i];
  450. block[j] = level * dequant_table[j];
  451. i++;
  452. if (i >= 64)
  453. break;
  454. }
  455. }
  456. return 0;
  457. }
  458. static inline void idct_put(FourXContext *f, int x, int y)
  459. {
  460. int16_t (*block)[64] = f->block;
  461. int stride = f->avctx->width;
  462. int i;
  463. uint16_t *dst = f->frame_buffer + y * stride + x;
  464. for (i = 0; i < 4; i++) {
  465. block[i][0] += 0x80 * 8 * 8;
  466. idct(block[i]);
  467. }
  468. if (!(f->avctx->flags & AV_CODEC_FLAG_GRAY)) {
  469. for (i = 4; i < 6; i++)
  470. idct(block[i]);
  471. }
  472. /* Note transform is:
  473. * y = ( 1b + 4g + 2r) / 14
  474. * cb = ( 3b - 2g - 1r) / 14
  475. * cr = (-1b - 4g + 5r) / 14 */
  476. for (y = 0; y < 8; y++) {
  477. for (x = 0; x < 8; x++) {
  478. int16_t *temp = block[(x >> 2) + 2 * (y >> 2)] +
  479. 2 * (x & 3) + 2 * 8 * (y & 3); // FIXME optimize
  480. int cb = block[4][x + 8 * y];
  481. int cr = block[5][x + 8 * y];
  482. int cg = (cb + cr) >> 1;
  483. int y;
  484. cb += cb;
  485. y = temp[0];
  486. dst[0] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
  487. y = temp[1];
  488. dst[1] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
  489. y = temp[8];
  490. dst[stride] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
  491. y = temp[9];
  492. dst[1 + stride] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
  493. dst += 2;
  494. }
  495. dst += 2 * stride - 2 * 8;
  496. }
  497. }
  498. static int decode_i_mb(FourXContext *f)
  499. {
  500. int ret;
  501. int i;
  502. f->bdsp.clear_blocks(f->block[0]);
  503. for (i = 0; i < 6; i++)
  504. if ((ret = decode_i_block(f, f->block[i])) < 0)
  505. return ret;
  506. return 0;
  507. }
  508. static const uint8_t *read_huffman_tables(FourXContext *f,
  509. const uint8_t * const buf,
  510. int len)
  511. {
  512. int frequency[512] = { 0 };
  513. uint8_t flag[512];
  514. int up[512];
  515. uint8_t len_tab[257];
  516. int bits_tab[257];
  517. int start, end;
  518. const uint8_t *ptr = buf;
  519. int j;
  520. memset(up, -1, sizeof(up));
  521. start = *ptr++;
  522. end = *ptr++;
  523. for (;;) {
  524. int i;
  525. len -= end - start + 1;
  526. if (end < start || len < 0)
  527. return NULL;
  528. for (i = start; i <= end; i++)
  529. frequency[i] = *ptr++;
  530. start = *ptr++;
  531. if (start == 0)
  532. break;
  533. if (--len < 0)
  534. return NULL;
  535. end = *ptr++;
  536. }
  537. frequency[256] = 1;
  538. while ((ptr - buf) & 3)
  539. ptr++; // 4byte align
  540. for (j = 257; j < 512; j++) {
  541. int min_freq[2] = { 256 * 256, 256 * 256 };
  542. int smallest[2] = { 0, 0 };
  543. int i;
  544. for (i = 0; i < j; i++) {
  545. if (frequency[i] == 0)
  546. continue;
  547. if (frequency[i] < min_freq[1]) {
  548. if (frequency[i] < min_freq[0]) {
  549. min_freq[1] = min_freq[0];
  550. smallest[1] = smallest[0];
  551. min_freq[0] = frequency[i];
  552. smallest[0] = i;
  553. } else {
  554. min_freq[1] = frequency[i];
  555. smallest[1] = i;
  556. }
  557. }
  558. }
  559. if (min_freq[1] == 256 * 256)
  560. break;
  561. frequency[j] = min_freq[0] + min_freq[1];
  562. flag[smallest[0]] = 0;
  563. flag[smallest[1]] = 1;
  564. up[smallest[0]] =
  565. up[smallest[1]] = j;
  566. frequency[smallest[0]] = frequency[smallest[1]] = 0;
  567. }
  568. for (j = 0; j < 257; j++) {
  569. int node, len = 0, bits = 0;
  570. for (node = j; up[node] != -1; node = up[node]) {
  571. bits += flag[node] << len;
  572. len++;
  573. if (len > 31)
  574. // can this happen at all ?
  575. av_log(f->avctx, AV_LOG_ERROR,
  576. "vlc length overflow\n");
  577. }
  578. bits_tab[j] = bits;
  579. len_tab[j] = len;
  580. }
  581. if (init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257, len_tab, 1, 1,
  582. bits_tab, 4, 4, 0))
  583. return NULL;
  584. return ptr;
  585. }
  586. static int mix(int c0, int c1)
  587. {
  588. int blue = 2 * (c0 & 0x001F) + (c1 & 0x001F);
  589. int green = (2 * (c0 & 0x03E0) + (c1 & 0x03E0)) >> 5;
  590. int red = 2 * (c0 >> 10) + (c1 >> 10);
  591. return red / 3 * 1024 + green / 3 * 32 + blue / 3;
  592. }
  593. static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length)
  594. {
  595. int x, y, x2, y2;
  596. const int width = f->avctx->width;
  597. const int height = f->avctx->height;
  598. const int mbs = (FFALIGN(width, 16) >> 4) * (FFALIGN(height, 16) >> 4);
  599. uint16_t *dst = f->frame_buffer;
  600. GetByteContext g3;
  601. if (length < mbs * 8) {
  602. av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n");
  603. return AVERROR_INVALIDDATA;
  604. }
  605. bytestream2_init(&g3, buf, length);
  606. for (y = 0; y < height; y += 16) {
  607. for (x = 0; x < width; x += 16) {
  608. unsigned int color[4] = { 0 }, bits;
  609. // warning following is purely guessed ...
  610. color[0] = bytestream2_get_le16u(&g3);
  611. color[1] = bytestream2_get_le16u(&g3);
  612. if (color[0] & 0x8000)
  613. av_log(f->avctx, AV_LOG_ERROR, "unk bit 1\n");
  614. if (color[1] & 0x8000)
  615. av_log(f->avctx, AV_LOG_ERROR, "unk bit 2\n");
  616. color[2] = mix(color[0], color[1]);
  617. color[3] = mix(color[1], color[0]);
  618. bits = bytestream2_get_le32u(&g3);
  619. for (y2 = 0; y2 < 16; y2++) {
  620. for (x2 = 0; x2 < 16; x2++) {
  621. int index = 2 * (x2 >> 2) + 8 * (y2 >> 2);
  622. dst[y2 * width + x2] = color[(bits >> index) & 3];
  623. }
  624. }
  625. dst += 16;
  626. }
  627. dst += 16 * width - x;
  628. }
  629. return 0;
  630. }
  631. static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length)
  632. {
  633. int x, y, ret;
  634. const int width = f->avctx->width;
  635. const int height = f->avctx->height;
  636. const unsigned int bitstream_size = AV_RL32(buf);
  637. int token_count av_unused;
  638. unsigned int prestream_size;
  639. const uint8_t *prestream;
  640. if (bitstream_size > (1 << 26))
  641. return AVERROR_INVALIDDATA;
  642. if (length < bitstream_size + 12) {
  643. av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n");
  644. return AVERROR_INVALIDDATA;
  645. }
  646. token_count = AV_RL32(buf + bitstream_size + 8);
  647. prestream_size = 4 * AV_RL32(buf + bitstream_size + 4);
  648. prestream = buf + bitstream_size + 12;
  649. if (prestream_size + bitstream_size + 12 != length
  650. || prestream_size > (1 << 26)) {
  651. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n",
  652. prestream_size, bitstream_size, length);
  653. return AVERROR_INVALIDDATA;
  654. }
  655. prestream = read_huffman_tables(f, prestream, prestream_size);
  656. if (!prestream) {
  657. av_log(f->avctx, AV_LOG_ERROR, "Error reading Huffman tables.\n");
  658. return AVERROR_INVALIDDATA;
  659. }
  660. init_get_bits(&f->gb, buf + 4, 8 * bitstream_size);
  661. prestream_size = length + buf - prestream;
  662. av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size,
  663. prestream_size + AV_INPUT_BUFFER_PADDING_SIZE);
  664. if (!f->bitstream_buffer)
  665. return AVERROR(ENOMEM);
  666. f->bbdsp.bswap_buf(f->bitstream_buffer, (const uint32_t *) prestream,
  667. prestream_size / 4);
  668. memset((uint8_t*)f->bitstream_buffer + prestream_size,
  669. 0, AV_INPUT_BUFFER_PADDING_SIZE);
  670. init_get_bits(&f->pre_gb, f->bitstream_buffer, 8 * prestream_size);
  671. f->last_dc = 0 * 128 * 8 * 8;
  672. for (y = 0; y < height; y += 16) {
  673. for (x = 0; x < width; x += 16) {
  674. if ((ret = decode_i_mb(f)) < 0)
  675. return ret;
  676. idct_put(f, x, y);
  677. }
  678. }
  679. if (get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
  680. av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
  681. return 0;
  682. }
  683. static int decode_frame(AVCodecContext *avctx, void *data,
  684. int *got_frame, AVPacket *avpkt)
  685. {
  686. const uint8_t *buf = avpkt->data;
  687. int buf_size = avpkt->size;
  688. FourXContext *const f = avctx->priv_data;
  689. AVFrame *picture = data;
  690. int i, frame_4cc, frame_size, ret;
  691. if (buf_size < 20)
  692. return AVERROR_INVALIDDATA;
  693. if (avctx->width % 16 || avctx->height % 16) {
  694. av_log(avctx, AV_LOG_ERROR,
  695. "Dimensions non-multiple of 16 are invalid.\n");
  696. return AVERROR_INVALIDDATA;
  697. }
  698. if (buf_size < AV_RL32(buf + 4) + 8) {
  699. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %"PRIu32"\n",
  700. buf_size, AV_RL32(buf + 4));
  701. return AVERROR_INVALIDDATA;
  702. }
  703. frame_4cc = AV_RL32(buf);
  704. if (frame_4cc == AV_RL32("cfrm")) {
  705. int free_index = -1;
  706. int id, whole_size;
  707. const int data_size = buf_size - 20;
  708. CFrameBuffer *cfrm;
  709. id = AV_RL32(buf + 12);
  710. whole_size = AV_RL32(buf + 16);
  711. for (i = 0; i < CFRAME_BUFFER_COUNT; i++)
  712. if (f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
  713. av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n",
  714. f->cfrm[i].id);
  715. for (i = 0; i < CFRAME_BUFFER_COUNT; i++) {
  716. if (f->cfrm[i].id == id)
  717. break;
  718. if (f->cfrm[i].size == 0)
  719. free_index = i;
  720. }
  721. if (i >= CFRAME_BUFFER_COUNT) {
  722. i = free_index;
  723. f->cfrm[i].id = id;
  724. }
  725. cfrm = &f->cfrm[i];
  726. cfrm->data = av_fast_realloc(cfrm->data, &cfrm->allocated_size,
  727. cfrm->size + data_size + AV_INPUT_BUFFER_PADDING_SIZE);
  728. // explicit check needed as memcpy below might not catch a NULL
  729. if (!cfrm->data) {
  730. av_log(f->avctx, AV_LOG_ERROR, "realloc failure");
  731. return AVERROR(ENOMEM);
  732. }
  733. memcpy(cfrm->data + cfrm->size, buf + 20, data_size);
  734. cfrm->size += data_size;
  735. if (cfrm->size >= whole_size) {
  736. buf = cfrm->data;
  737. frame_size = cfrm->size;
  738. if (id != avctx->frame_number)
  739. av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n",
  740. id, avctx->frame_number);
  741. if (f->version <= 1)
  742. return AVERROR_INVALIDDATA;
  743. cfrm->size = cfrm->id = 0;
  744. frame_4cc = AV_RL32("pfrm");
  745. } else
  746. return buf_size;
  747. } else {
  748. buf = buf + 12;
  749. frame_size = buf_size - 12;
  750. }
  751. if ((ret = ff_get_buffer(avctx, picture, 0)) < 0) {
  752. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  753. return ret;
  754. }
  755. if (frame_4cc == AV_RL32("ifr2")) {
  756. picture->pict_type = AV_PICTURE_TYPE_I;
  757. if ((ret = decode_i2_frame(f, buf - 4, frame_size + 4)) < 0)
  758. return ret;
  759. } else if (frame_4cc == AV_RL32("ifrm")) {
  760. picture->pict_type = AV_PICTURE_TYPE_I;
  761. if ((ret = decode_i_frame(f, buf, frame_size)) < 0)
  762. return ret;
  763. } else if (frame_4cc == AV_RL32("pfrm") || frame_4cc == AV_RL32("pfr2")) {
  764. picture->pict_type = AV_PICTURE_TYPE_P;
  765. if ((ret = decode_p_frame(f, buf, frame_size)) < 0)
  766. return ret;
  767. } else if (frame_4cc == AV_RL32("snd_")) {
  768. av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n",
  769. buf_size);
  770. } else {
  771. av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n",
  772. buf_size);
  773. }
  774. picture->key_frame = picture->pict_type == AV_PICTURE_TYPE_I;
  775. av_image_copy_plane(picture->data[0], picture->linesize[0],
  776. (const uint8_t*)f->frame_buffer, avctx->width * 2,
  777. avctx->width * 2, avctx->height);
  778. FFSWAP(uint16_t *, f->frame_buffer, f->last_frame_buffer);
  779. *got_frame = 1;
  780. emms_c();
  781. return buf_size;
  782. }
  783. static av_cold int decode_end(AVCodecContext *avctx)
  784. {
  785. FourXContext * const f = avctx->priv_data;
  786. int i;
  787. av_freep(&f->frame_buffer);
  788. av_freep(&f->last_frame_buffer);
  789. av_freep(&f->bitstream_buffer);
  790. f->bitstream_buffer_size = 0;
  791. for (i = 0; i < CFRAME_BUFFER_COUNT; i++) {
  792. av_freep(&f->cfrm[i].data);
  793. f->cfrm[i].allocated_size = 0;
  794. }
  795. ff_free_vlc(&f->pre_vlc);
  796. return 0;
  797. }
  798. static av_cold int decode_init(AVCodecContext *avctx)
  799. {
  800. FourXContext * const f = avctx->priv_data;
  801. int ret;
  802. if (avctx->extradata_size != 4 || !avctx->extradata) {
  803. av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n");
  804. return AVERROR_INVALIDDATA;
  805. }
  806. ret = av_image_check_size(avctx->width, avctx->height, 0, avctx);
  807. if (ret < 0)
  808. return ret;
  809. f->frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
  810. f->last_frame_buffer = av_mallocz(avctx->width * avctx->height * 2);
  811. if (!f->frame_buffer || !f->last_frame_buffer) {
  812. decode_end(avctx);
  813. return AVERROR(ENOMEM);
  814. }
  815. f->version = AV_RL32(avctx->extradata) >> 16;
  816. ff_blockdsp_init(&f->bdsp, avctx);
  817. ff_bswapdsp_init(&f->bbdsp);
  818. f->avctx = avctx;
  819. init_vlcs(f);
  820. if (f->version > 2)
  821. avctx->pix_fmt = AV_PIX_FMT_RGB565;
  822. else
  823. avctx->pix_fmt = AV_PIX_FMT_BGR555;
  824. return 0;
  825. }
  826. AVCodec ff_fourxm_decoder = {
  827. .name = "4xm",
  828. .long_name = NULL_IF_CONFIG_SMALL("4X Movie"),
  829. .type = AVMEDIA_TYPE_VIDEO,
  830. .id = AV_CODEC_ID_4XM,
  831. .priv_data_size = sizeof(FourXContext),
  832. .init = decode_init,
  833. .close = decode_end,
  834. .decode = decode_frame,
  835. .capabilities = AV_CODEC_CAP_DR1,
  836. };