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.

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