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.

1034 lines
34KB

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