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.

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