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.

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