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.

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