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.

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