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.

992 lines
33KB

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