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