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.

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