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.

942 lines
31KB

  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/intreadwrite.h"
  26. #include "avcodec.h"
  27. #include "bytestream.h"
  28. #include "dsputil.h"
  29. #include "get_bits.h"
  30. //#undef NDEBUG
  31. //#include <assert.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 current_picture, 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, DCTELEM, 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(DCTELEM 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)
  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] * f->current_picture.linesize[0] / 2;
  228. else
  229. f->mv[i] = (i & 15) - 8 + ((i >> 4) - 8) * f->current_picture.linesize[0] / 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. assert(0);
  292. }
  293. }
  294. static void 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. assert(code >= 0 && code <= 6);
  305. if (code == 0) {
  306. src += f->mv[bytestream2_get_byte(&f->g)];
  307. if (start > src || src > end) {
  308. av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
  309. return;
  310. }
  311. mcdc(dst, src, log2w, h, stride, 1, 0);
  312. } else if (code == 1) {
  313. log2h--;
  314. decode_p_block(f, dst, src, log2w, log2h, stride);
  315. decode_p_block(f, dst + (stride << log2h),
  316. src + (stride << log2h), log2w, log2h, stride);
  317. } else if (code == 2) {
  318. log2w--;
  319. decode_p_block(f, dst , src, log2w, log2h, stride);
  320. decode_p_block(f, dst + (1 << log2w),
  321. src + (1 << log2w), log2w, log2h, stride);
  322. } else if (code == 3 && f->version < 2) {
  323. mcdc(dst, src, log2w, h, stride, 1, 0);
  324. } else if (code == 4) {
  325. src += f->mv[bytestream2_get_byte(&f->g)];
  326. if (start > src || src > end) {
  327. av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
  328. return;
  329. }
  330. mcdc(dst, src, log2w, h, stride, 1, bytestream2_get_le16(&f->g2));
  331. } else if (code == 5) {
  332. mcdc(dst, src, log2w, h, stride, 0, bytestream2_get_le16(&f->g2));
  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. }
  342. }
  343. static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length)
  344. {
  345. int x, y;
  346. const int width = f->avctx->width;
  347. const int height = f->avctx->height;
  348. uint16_t *src = (uint16_t *)f->last_picture.data[0];
  349. uint16_t *dst = (uint16_t *)f->current_picture.data[0];
  350. const int stride = f->current_picture.linesize[0] >> 1;
  351. unsigned int bitstream_size, bytestream_size, wordstream_size, extra,
  352. bytestream_offset, wordstream_offset;
  353. if (f->version > 1) {
  354. extra = 20;
  355. bitstream_size = AV_RL32(buf + 8);
  356. wordstream_size = AV_RL32(buf + 12);
  357. bytestream_size = AV_RL32(buf + 16);
  358. } else {
  359. extra = 0;
  360. bitstream_size = AV_RL16(buf - 4);
  361. wordstream_size = AV_RL16(buf - 2);
  362. bytestream_size = FFMAX(length - bitstream_size - wordstream_size, 0);
  363. }
  364. if (bitstream_size + bytestream_size + wordstream_size + extra != length
  365. || bitstream_size > (1 << 26)
  366. || bytestream_size > (1 << 26)
  367. || wordstream_size > (1 << 26)) {
  368. av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n",
  369. bitstream_size, bytestream_size, wordstream_size,
  370. bitstream_size + bytestream_size + wordstream_size - length);
  371. return -1;
  372. }
  373. av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size,
  374. bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  375. if (!f->bitstream_buffer)
  376. return AVERROR(ENOMEM);
  377. f->dsp.bswap_buf(f->bitstream_buffer, (const uint32_t*)(buf + extra),
  378. bitstream_size / 4);
  379. memset((uint8_t*)f->bitstream_buffer + bitstream_size,
  380. 0, FF_INPUT_BUFFER_PADDING_SIZE);
  381. init_get_bits(&f->gb, f->bitstream_buffer, 8 * bitstream_size);
  382. wordstream_offset = extra + bitstream_size;
  383. bytestream_offset = extra + bitstream_size + wordstream_size;
  384. bytestream2_init(&f->g2, buf + wordstream_offset,
  385. length - wordstream_offset);
  386. bytestream2_init(&f->g, buf + bytestream_offset,
  387. length - bytestream_offset);
  388. init_mv(f);
  389. for (y = 0; y < height; y += 8) {
  390. for (x = 0; x < width; x += 8)
  391. decode_p_block(f, dst + x, src + x, 3, 3, stride);
  392. src += 8 * stride;
  393. dst += 8 * stride;
  394. }
  395. return 0;
  396. }
  397. /**
  398. * decode block and dequantize.
  399. * Note this is almost identical to MJPEG.
  400. */
  401. static int decode_i_block(FourXContext *f, DCTELEM *block)
  402. {
  403. int code, i, j, level, val;
  404. /* DC coef */
  405. val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  406. if (val >> 4)
  407. av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
  408. if (val)
  409. val = get_xbits(&f->gb, val);
  410. val = val * dequant_table[0] + f->last_dc;
  411. f->last_dc = block[0] = val;
  412. /* AC coefs */
  413. i = 1;
  414. for (;;) {
  415. code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
  416. /* EOB */
  417. if (code == 0)
  418. break;
  419. if (code == 0xf0) {
  420. i += 16;
  421. } else {
  422. level = get_xbits(&f->gb, code & 0xf);
  423. i += code >> 4;
  424. if (i >= 64) {
  425. av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
  426. return 0;
  427. }
  428. j = ff_zigzag_direct[i];
  429. block[j] = level * dequant_table[j];
  430. i++;
  431. if (i >= 64)
  432. break;
  433. }
  434. }
  435. return 0;
  436. }
  437. static inline void idct_put(FourXContext *f, int x, int y)
  438. {
  439. DCTELEM (*block)[64] = f->block;
  440. int stride = f->current_picture.linesize[0] >> 1;
  441. int i;
  442. uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
  443. for (i = 0; i < 4; i++) {
  444. block[i][0] += 0x80 * 8 * 8;
  445. idct(block[i]);
  446. }
  447. if (!(f->avctx->flags & CODEC_FLAG_GRAY)) {
  448. for (i = 4; i < 6; i++)
  449. idct(block[i]);
  450. }
  451. /* Note transform is:
  452. * y = ( 1b + 4g + 2r) / 14
  453. * cb = ( 3b - 2g - 1r) / 14
  454. * cr = (-1b - 4g + 5r) / 14 */
  455. for (y = 0; y < 8; y++) {
  456. for (x = 0; x < 8; x++) {
  457. DCTELEM *temp = block[(x >> 2) + 2 * (y >> 2)] +
  458. 2 * (x & 3) + 2 * 8 * (y & 3); // FIXME optimize
  459. int cb = block[4][x + 8 * y];
  460. int cr = block[5][x + 8 * y];
  461. int cg = (cb + cr) >> 1;
  462. int y;
  463. cb += cb;
  464. y = temp[0];
  465. dst[0] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
  466. y = temp[1];
  467. dst[1] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
  468. y = temp[8];
  469. dst[stride] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
  470. y = temp[9];
  471. dst[1 + stride] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
  472. dst += 2;
  473. }
  474. dst += 2 * stride - 2 * 8;
  475. }
  476. }
  477. static int decode_i_mb(FourXContext *f)
  478. {
  479. int i;
  480. f->dsp.clear_blocks(f->block[0]);
  481. for (i = 0; i < 6; i++)
  482. if (decode_i_block(f, f->block[i]) < 0)
  483. return -1;
  484. return 0;
  485. }
  486. static const uint8_t *read_huffman_tables(FourXContext *f,
  487. const uint8_t * const buf)
  488. {
  489. int frequency[512] = { 0 };
  490. uint8_t flag[512];
  491. int up[512];
  492. uint8_t len_tab[257];
  493. int bits_tab[257];
  494. int start, end;
  495. const uint8_t *ptr = buf;
  496. int j;
  497. memset(up, -1, sizeof(up));
  498. start = *ptr++;
  499. end = *ptr++;
  500. for (;;) {
  501. int i;
  502. for (i = start; i <= end; i++)
  503. frequency[i] = *ptr++;
  504. start = *ptr++;
  505. if (start == 0)
  506. break;
  507. end = *ptr++;
  508. }
  509. frequency[256] = 1;
  510. while ((ptr - buf) & 3)
  511. ptr++; // 4byte align
  512. for (j = 257; j < 512; j++) {
  513. int min_freq[2] = { 256 * 256, 256 * 256 };
  514. int smallest[2] = { 0, 0 };
  515. int i;
  516. for (i = 0; i < j; i++) {
  517. if (frequency[i] == 0)
  518. continue;
  519. if (frequency[i] < min_freq[1]) {
  520. if (frequency[i] < min_freq[0]) {
  521. min_freq[1] = min_freq[0];
  522. smallest[1] = smallest[0];
  523. min_freq[0] = frequency[i];
  524. smallest[0] = i;
  525. } else {
  526. min_freq[1] = frequency[i];
  527. smallest[1] = i;
  528. }
  529. }
  530. }
  531. if (min_freq[1] == 256 * 256)
  532. break;
  533. frequency[j] = min_freq[0] + min_freq[1];
  534. flag[smallest[0]] = 0;
  535. flag[smallest[1]] = 1;
  536. up[smallest[0]] =
  537. up[smallest[1]] = j;
  538. frequency[smallest[0]] = frequency[smallest[1]] = 0;
  539. }
  540. for (j = 0; j < 257; j++) {
  541. int node, len = 0, bits = 0;
  542. for (node = j; up[node] != -1; node = up[node]) {
  543. bits += flag[node] << len;
  544. len++;
  545. if (len > 31)
  546. // can this happen at all ?
  547. av_log(f->avctx, AV_LOG_ERROR,
  548. "vlc length overflow\n");
  549. }
  550. bits_tab[j] = bits;
  551. len_tab[j] = len;
  552. }
  553. if (init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257, len_tab, 1, 1,
  554. bits_tab, 4, 4, 0))
  555. return NULL;
  556. return ptr;
  557. }
  558. static int mix(int c0, int c1)
  559. {
  560. int blue = 2 * (c0 & 0x001F) + (c1 & 0x001F);
  561. int green = (2 * (c0 & 0x03E0) + (c1 & 0x03E0)) >> 5;
  562. int red = 2 * (c0 >> 10) + (c1 >> 10);
  563. return red / 3 * 1024 + green / 3 * 32 + blue / 3;
  564. }
  565. static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length)
  566. {
  567. int x, y, x2, y2;
  568. const int width = f->avctx->width;
  569. const int height = f->avctx->height;
  570. const int mbs = (FFALIGN(width, 16) >> 4) * (FFALIGN(height, 16) >> 4);
  571. uint16_t *dst = (uint16_t*)f->current_picture.data[0];
  572. const int stride = f->current_picture.linesize[0]>>1;
  573. GetByteContext g3;
  574. if (length < mbs * 8) {
  575. av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n");
  576. return AVERROR_INVALIDDATA;
  577. }
  578. bytestream2_init(&g3, buf, length);
  579. for (y = 0; y < height; y += 16) {
  580. for (x = 0; x < width; x += 16) {
  581. unsigned int color[4] = { 0 }, bits;
  582. // warning following is purely guessed ...
  583. color[0] = bytestream2_get_le16u(&g3);
  584. color[1] = bytestream2_get_le16u(&g3);
  585. if (color[0] & 0x8000)
  586. av_log(NULL, AV_LOG_ERROR, "unk bit 1\n");
  587. if (color[1] & 0x8000)
  588. av_log(NULL, AV_LOG_ERROR, "unk bit 2\n");
  589. color[2] = mix(color[0], color[1]);
  590. color[3] = mix(color[1], color[0]);
  591. bits = bytestream2_get_le32u(&g3);
  592. for (y2 = 0; y2 < 16; y2++) {
  593. for (x2 = 0; x2 < 16; x2++) {
  594. int index = 2 * (x2 >> 2) + 8 * (y2 >> 2);
  595. dst[y2 * stride + x2] = color[(bits >> index) & 3];
  596. }
  597. }
  598. dst += 16;
  599. }
  600. dst += 16 * stride - x;
  601. }
  602. return 0;
  603. }
  604. static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length)
  605. {
  606. int x, y;
  607. const int width = f->avctx->width;
  608. const int height = f->avctx->height;
  609. const unsigned int bitstream_size = AV_RL32(buf);
  610. int token_count av_unused;
  611. unsigned int prestream_size;
  612. const uint8_t *prestream;
  613. if (length < bitstream_size + 12) {
  614. av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n");
  615. return AVERROR_INVALIDDATA;
  616. }
  617. token_count = AV_RL32(buf + bitstream_size + 8);
  618. prestream_size = 4 * AV_RL32(buf + bitstream_size + 4);
  619. prestream = buf + bitstream_size + 12;
  620. if (prestream_size + bitstream_size + 12 != length
  621. || bitstream_size > (1 << 26)
  622. || prestream_size > (1 << 26)) {
  623. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n",
  624. prestream_size, bitstream_size, length);
  625. return -1;
  626. }
  627. prestream = read_huffman_tables(f, prestream);
  628. init_get_bits(&f->gb, buf + 4, 8 * bitstream_size);
  629. prestream_size = length + buf - prestream;
  630. av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size,
  631. prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
  632. if (!f->bitstream_buffer)
  633. return AVERROR(ENOMEM);
  634. f->dsp.bswap_buf(f->bitstream_buffer, (const uint32_t*)prestream,
  635. prestream_size / 4);
  636. memset((uint8_t*)f->bitstream_buffer + prestream_size,
  637. 0, FF_INPUT_BUFFER_PADDING_SIZE);
  638. init_get_bits(&f->pre_gb, f->bitstream_buffer, 8 * prestream_size);
  639. f->last_dc = 0 * 128 * 8 * 8;
  640. for (y = 0; y < height; y += 16) {
  641. for (x = 0; x < width; x += 16) {
  642. if (decode_i_mb(f) < 0)
  643. return -1;
  644. idct_put(f, x, y);
  645. }
  646. }
  647. if (get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
  648. av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
  649. return 0;
  650. }
  651. static int decode_frame(AVCodecContext *avctx, void *data,
  652. int *data_size, AVPacket *avpkt)
  653. {
  654. const uint8_t *buf = avpkt->data;
  655. int buf_size = avpkt->size;
  656. FourXContext *const f = avctx->priv_data;
  657. AVFrame *picture = data;
  658. AVFrame *p, temp;
  659. int i, frame_4cc, frame_size;
  660. frame_4cc = AV_RL32(buf);
  661. if (buf_size != AV_RL32(buf + 4) + 8 || buf_size < 20)
  662. av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n",
  663. buf_size, AV_RL32(buf + 4));
  664. if (frame_4cc == AV_RL32("cfrm")) {
  665. int free_index = -1;
  666. const int data_size = buf_size - 20;
  667. const int id = AV_RL32(buf + 12);
  668. const int whole_size = AV_RL32(buf + 16);
  669. CFrameBuffer *cfrm;
  670. for (i = 0; i < CFRAME_BUFFER_COUNT; i++)
  671. if (f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
  672. av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n",
  673. f->cfrm[i].id);
  674. for (i = 0; i < CFRAME_BUFFER_COUNT; i++) {
  675. if (f->cfrm[i].id == id)
  676. break;
  677. if (f->cfrm[i].size == 0)
  678. free_index = i;
  679. }
  680. if (i >= CFRAME_BUFFER_COUNT) {
  681. i = free_index;
  682. f->cfrm[i].id = id;
  683. }
  684. cfrm = &f->cfrm[i];
  685. cfrm->data = av_fast_realloc(cfrm->data, &cfrm->allocated_size,
  686. cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
  687. // explicit check needed as memcpy below might not catch a NULL
  688. if (!cfrm->data) {
  689. av_log(f->avctx, AV_LOG_ERROR, "realloc falure");
  690. return -1;
  691. }
  692. memcpy(cfrm->data + cfrm->size, buf + 20, data_size);
  693. cfrm->size += data_size;
  694. if (cfrm->size >= whole_size) {
  695. buf = cfrm->data;
  696. frame_size = cfrm->size;
  697. if (id != avctx->frame_number)
  698. av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n",
  699. id, avctx->frame_number);
  700. cfrm->size = cfrm->id = 0;
  701. frame_4cc = AV_RL32("pfrm");
  702. } else
  703. return buf_size;
  704. } else {
  705. buf = buf + 12;
  706. frame_size = buf_size - 12;
  707. }
  708. temp = f->current_picture;
  709. f->current_picture = f->last_picture;
  710. f->last_picture = temp;
  711. p = &f->current_picture;
  712. avctx->coded_frame = p;
  713. // alternatively we would have to use our own buffer management
  714. avctx->flags |= CODEC_FLAG_EMU_EDGE;
  715. if (p->data[0])
  716. avctx->release_buffer(avctx, p);
  717. p->reference = 1;
  718. if (avctx->get_buffer(avctx, p) < 0) {
  719. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  720. return -1;
  721. }
  722. if (frame_4cc == AV_RL32("ifr2")) {
  723. p->pict_type = AV_PICTURE_TYPE_I;
  724. if (decode_i2_frame(f, buf - 4, frame_size + 4) < 0)
  725. return -1;
  726. } else if (frame_4cc == AV_RL32("ifrm")) {
  727. p->pict_type = AV_PICTURE_TYPE_I;
  728. if (decode_i_frame(f, buf, frame_size) < 0)
  729. return -1;
  730. } else if (frame_4cc == AV_RL32("pfrm") || frame_4cc == AV_RL32("pfr2")) {
  731. if (!f->last_picture.data[0]) {
  732. f->last_picture.reference = 1;
  733. if (avctx->get_buffer(avctx, &f->last_picture) < 0) {
  734. av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
  735. return -1;
  736. }
  737. }
  738. p->pict_type = AV_PICTURE_TYPE_P;
  739. if (decode_p_frame(f, buf, frame_size) < 0)
  740. return -1;
  741. } else if (frame_4cc == AV_RL32("snd_")) {
  742. av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n",
  743. buf_size);
  744. } else {
  745. av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n",
  746. buf_size);
  747. }
  748. p->key_frame = p->pict_type == AV_PICTURE_TYPE_I;
  749. *picture = *p;
  750. *data_size = sizeof(AVPicture);
  751. emms_c();
  752. return buf_size;
  753. }
  754. static av_cold void common_init(AVCodecContext *avctx)
  755. {
  756. FourXContext * const f = avctx->priv_data;
  757. ff_dsputil_init(&f->dsp, avctx);
  758. f->avctx = avctx;
  759. }
  760. static av_cold int decode_init(AVCodecContext *avctx)
  761. {
  762. FourXContext * const f = avctx->priv_data;
  763. if (avctx->extradata_size != 4 || !avctx->extradata) {
  764. av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n");
  765. return 1;
  766. }
  767. f->version = AV_RL32(avctx->extradata) >> 16;
  768. common_init(avctx);
  769. init_vlcs(f);
  770. if (f->version > 2)
  771. avctx->pix_fmt = PIX_FMT_RGB565;
  772. else
  773. avctx->pix_fmt = PIX_FMT_BGR555;
  774. return 0;
  775. }
  776. static av_cold int decode_end(AVCodecContext *avctx)
  777. {
  778. FourXContext * const f = avctx->priv_data;
  779. int i;
  780. av_freep(&f->bitstream_buffer);
  781. f->bitstream_buffer_size = 0;
  782. for (i = 0; i < CFRAME_BUFFER_COUNT; i++) {
  783. av_freep(&f->cfrm[i].data);
  784. f->cfrm[i].allocated_size = 0;
  785. }
  786. ff_free_vlc(&f->pre_vlc);
  787. if (f->current_picture.data[0])
  788. avctx->release_buffer(avctx, &f->current_picture);
  789. if (f->last_picture.data[0])
  790. avctx->release_buffer(avctx, &f->last_picture);
  791. return 0;
  792. }
  793. AVCodec ff_fourxm_decoder = {
  794. .name = "4xm",
  795. .type = AVMEDIA_TYPE_VIDEO,
  796. .id = CODEC_ID_4XM,
  797. .priv_data_size = sizeof(FourXContext),
  798. .init = decode_init,
  799. .close = decode_end,
  800. .decode = decode_frame,
  801. .capabilities = CODEC_CAP_DR1,
  802. .long_name = NULL_IF_CONFIG_SMALL("4X Movie"),
  803. };