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.

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