00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "libavutil/intreadwrite.h"
00028 #include "avcodec.h"
00029 #include "bytestream.h"
00030 #include "dsputil.h"
00031 #include "get_bits.h"
00032
00033 #include "libavutil/avassert.h"
00034
00035 #define BLOCK_TYPE_VLC_BITS 5
00036 #define ACDC_VLC_BITS 9
00037
00038 #define CFRAME_BUFFER_COUNT 100
00039
00040 static const uint8_t block_type_tab[2][4][8][2] = {
00041 {
00042 {
00043 { 0, 1 }, { 2, 2 }, { 6, 3 }, { 14, 4 }, { 30, 5 }, { 31, 5 }, { 0, 0 }
00044 }, {
00045 { 0, 1 }, { 0, 0 }, { 2, 2 }, { 6, 3 }, { 14, 4 }, { 15, 4 }, { 0, 0 }
00046 }, {
00047 { 0, 1 }, { 2, 2 }, { 0, 0 }, { 6, 3 }, { 14, 4 }, { 15, 4 }, { 0, 0 }
00048 }, {
00049 { 0, 1 }, { 0, 0 }, { 0, 0 }, { 2, 2 }, { 6, 3 }, { 14, 4 }, { 15, 4 }
00050 }
00051 }, {
00052 {
00053 { 1, 2 }, { 4, 3 }, { 5, 3 }, { 0, 2 }, { 6, 3 }, { 7, 3 }, { 0, 0 }
00054 }, {
00055 { 1, 2 }, { 0, 0 }, { 2, 2 }, { 0, 2 }, { 6, 3 }, { 7, 3 }, { 0, 0 }
00056 }, {
00057 { 1, 2 }, { 2, 2 }, { 0, 0 }, { 0, 2 }, { 6, 3 }, { 7, 3 }, { 0, 0 }
00058 }, {
00059 { 1, 2 }, { 0, 0 }, { 0, 0 }, { 0, 2 }, { 2, 2 }, { 6, 3 }, { 7, 3 }
00060 }
00061 }
00062 };
00063
00064 static const uint8_t size2index[4][4] = {
00065 { -1, 3, 1, 1 },
00066 { 3, 0, 0, 0 },
00067 { 2, 0, 0, 0 },
00068 { 2, 0, 0, 0 },
00069 };
00070
00071 static const int8_t mv[256][2] = {
00072 { 0, 0 }, { 0, -1 }, { -1, 0 }, { 1, 0 }, { 0, 1 }, { -1, -1 }, { 1, -1 }, { -1, 1 },
00073 { 1, 1 }, { 0, -2 }, { -2, 0 }, { 2, 0 }, { 0, 2 }, { -1, -2 }, { 1, -2 }, { -2, -1 },
00074 { 2, -1 }, { -2, 1 }, { 2, 1 }, { -1, 2 }, { 1, 2 }, { -2, -2 }, { 2, -2 }, { -2, 2 },
00075 { 2, 2 }, { 0, -3 }, { -3, 0 }, { 3, 0 }, { 0, 3 }, { -1, -3 }, { 1, -3 }, { -3, -1 },
00076 { 3, -1 }, { -3, 1 }, { 3, 1 }, { -1, 3 }, { 1, 3 }, { -2, -3 }, { 2, -3 }, { -3, -2 },
00077 { 3, -2 }, { -3, 2 }, { 3, 2 }, { -2, 3 }, { 2, 3 }, { 0, -4 }, { -4, 0 }, { 4, 0 },
00078 { 0, 4 }, { -1, -4 }, { 1, -4 }, { -4, -1 }, { 4, -1 }, { 4, 1 }, { -1, 4 }, { 1, 4 },
00079 { -3, -3 }, { -3, 3 }, { 3, 3 }, { -2, -4 }, { -4, -2 }, { 4, -2 }, { -4, 2 }, { -2, 4 },
00080 { 2, 4 }, { -3, -4 }, { 3, -4 }, { 4, -3 }, { -5, 0 }, { -4, 3 }, { -3, 4 }, { 3, 4 },
00081 { -1, -5 }, { -5, -1 }, { -5, 1 }, { -1, 5 }, { -2, -5 }, { 2, -5 }, { 5, -2 }, { 5, 2 },
00082 { -4, -4 }, { -4, 4 }, { -3, -5 }, { -5, -3 }, { -5, 3 }, { 3, 5 }, { -6, 0 }, { 0, 6 },
00083 { -6, -1 }, { -6, 1 }, { 1, 6 }, { 2, -6 }, { -6, 2 }, { 2, 6 }, { -5, -4 }, { 5, 4 },
00084 { 4, 5 }, { -6, -3 }, { 6, 3 }, { -7, 0 }, { -1, -7 }, { 5, -5 }, { -7, 1 }, { -1, 7 },
00085 { 4, -6 }, { 6, 4 }, { -2, -7 }, { -7, 2 }, { -3, -7 }, { 7, -3 }, { 3, 7 }, { 6, -5 },
00086 { 0, -8 }, { -1, -8 }, { -7, -4 }, { -8, 1 }, { 4, 7 }, { 2, -8 }, { -2, 8 }, { 6, 6 },
00087 { -8, 3 }, { 5, -7 }, { -5, 7 }, { 8, -4 }, { 0, -9 }, { -9, -1 }, { 1, 9 }, { 7, -6 },
00088 { -7, 6 }, { -5, -8 }, { -5, 8 }, { -9, 3 }, { 9, -4 }, { 7, -7 }, { 8, -6 }, { 6, 8 },
00089 { 10, 1 }, { -10, 2 }, { 9, -5 }, { 10, -3 }, { -8, -7 }, { -10, -4 }, { 6, -9 }, { -11, 0 },
00090 { 11, 1 }, { -11, -2 }, { -2, 11 }, { 7, -9 }, { -7, 9 }, { 10, 6 }, { -4, 11 }, { 8, -9 },
00091 { 8, 9 }, { 5, 11 }, { 7, -10 }, { 12, -3 }, { 11, 6 }, { -9, -9 }, { 8, 10 }, { 5, 12 },
00092 { -11, 7 }, { 13, 2 }, { 6, -12 }, { 10, 9 }, { -11, 8 }, { -7, 12 }, { 0, 14 }, { 14, -2 },
00093 { -9, 11 }, { -6, 13 }, { -14, -4 }, { -5, -14 }, { 5, 14 }, { -15, -1 }, { -14, -6 }, { 3, -15 },
00094 { 11, -11 }, { -7, 14 }, { -5, 15 }, { 8, -14 }, { 15, 6 }, { 3, 16 }, { 7, -15 }, { -16, 5 },
00095 { 0, 17 }, { -16, -6 }, { -10, 14 }, { -16, 7 }, { 12, 13 }, { -16, 8 }, { -17, 6 }, { -18, 3 },
00096 { -7, 17 }, { 15, 11 }, { 16, 10 }, { 2, -19 }, { 3, -19 }, { -11, -16 }, { -18, 8 }, { -19, -6 },
00097 { 2, -20 }, { -17, -11 }, { -10, -18 }, { 8, 19 }, { -21, -1 }, { -20, 7 }, { -4, 21 }, { 21, 5 },
00098 { 15, 16 }, { 2, -22 }, { -10, -20 }, { -22, 5 }, { 20, -11 }, { -7, -22 }, { -12, 20 }, { 23, -5 },
00099 { 13, -20 }, { 24, -2 }, { -15, 19 }, { -11, 22 }, { 16, 19 }, { 23, -10 }, { -18, -18 }, { -9, -24 },
00100 { 24, -10 }, { -3, 26 }, { -23, 13 }, { -18, -20 }, { 17, 21 }, { -4, 27 }, { 27, 6 }, { 1, -28 },
00101 { -11, 26 }, { -17, -23 }, { 7, 28 }, { 11, -27 }, { 29, 5 }, { -23, -19 }, { -28, -11 }, { -21, 22 },
00102 { -30, 7 }, { -17, 26 }, { -27, 16 }, { 13, 29 }, { 19, -26 }, { 10, -31 }, { -14, -30 }, { 20, -27 },
00103 { -29, 18 }, { -16, -31 }, { -28, -22 }, { 21, -30 }, { -25, 28 }, { 26, -29 }, { 25, -32 }, { -32, -32 }
00104 };
00105
00106
00107
00108 static const uint8_t dequant_table[64] = {
00109 16, 15, 13, 19, 24, 31, 28, 17,
00110 17, 23, 25, 31, 36, 63, 45, 21,
00111 18, 24, 27, 37, 52, 59, 49, 20,
00112 16, 28, 34, 40, 60, 80, 51, 20,
00113 18, 31, 48, 66, 68, 86, 56, 21,
00114 19, 38, 56, 59, 64, 64, 48, 20,
00115 27, 48, 55, 55, 56, 51, 35, 15,
00116 20, 35, 34, 32, 31, 22, 15, 8,
00117 };
00118
00119 static VLC block_type_vlc[2][4];
00120
00121
00122 typedef struct CFrameBuffer {
00123 unsigned int allocated_size;
00124 unsigned int size;
00125 int id;
00126 uint8_t *data;
00127 } CFrameBuffer;
00128
00129 typedef struct FourXContext {
00130 AVCodecContext *avctx;
00131 DSPContext dsp;
00132 AVFrame current_picture, last_picture;
00133 GetBitContext pre_gb;
00134 GetBitContext gb;
00135 GetByteContext g;
00136 GetByteContext g2;
00137 int mv[256];
00138 VLC pre_vlc;
00139 int last_dc;
00140 DECLARE_ALIGNED(16, DCTELEM, block)[6][64];
00141 void *bitstream_buffer;
00142 unsigned int bitstream_buffer_size;
00143 int version;
00144 CFrameBuffer cfrm[CFRAME_BUFFER_COUNT];
00145 } FourXContext;
00146
00147
00148 #define FIX_1_082392200 70936
00149 #define FIX_1_414213562 92682
00150 #define FIX_1_847759065 121095
00151 #define FIX_2_613125930 171254
00152
00153 #define MULTIPLY(var, const) (((var) * (const)) >> 16)
00154
00155 static void idct(DCTELEM block[64])
00156 {
00157 int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
00158 int tmp10, tmp11, tmp12, tmp13;
00159 int z5, z10, z11, z12, z13;
00160 int i;
00161 int temp[64];
00162
00163 for (i = 0; i < 8; i++) {
00164 tmp10 = block[8 * 0 + i] + block[8 * 4 + i];
00165 tmp11 = block[8 * 0 + i] - block[8 * 4 + i];
00166
00167 tmp13 = block[8 * 2 + i] + block[8 * 6 + i];
00168 tmp12 = MULTIPLY(block[8 * 2 + i] - block[8 * 6 + i], FIX_1_414213562) - tmp13;
00169
00170 tmp0 = tmp10 + tmp13;
00171 tmp3 = tmp10 - tmp13;
00172 tmp1 = tmp11 + tmp12;
00173 tmp2 = tmp11 - tmp12;
00174
00175 z13 = block[8 * 5 + i] + block[8 * 3 + i];
00176 z10 = block[8 * 5 + i] - block[8 * 3 + i];
00177 z11 = block[8 * 1 + i] + block[8 * 7 + i];
00178 z12 = block[8 * 1 + i] - block[8 * 7 + i];
00179
00180 tmp7 = z11 + z13;
00181 tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
00182
00183 z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
00184 tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
00185 tmp12 = MULTIPLY(z10, -FIX_2_613125930) + z5;
00186
00187 tmp6 = tmp12 - tmp7;
00188 tmp5 = tmp11 - tmp6;
00189 tmp4 = tmp10 + tmp5;
00190
00191 temp[8 * 0 + i] = tmp0 + tmp7;
00192 temp[8 * 7 + i] = tmp0 - tmp7;
00193 temp[8 * 1 + i] = tmp1 + tmp6;
00194 temp[8 * 6 + i] = tmp1 - tmp6;
00195 temp[8 * 2 + i] = tmp2 + tmp5;
00196 temp[8 * 5 + i] = tmp2 - tmp5;
00197 temp[8 * 4 + i] = tmp3 + tmp4;
00198 temp[8 * 3 + i] = tmp3 - tmp4;
00199 }
00200
00201 for (i = 0; i < 8 * 8; i += 8) {
00202 tmp10 = temp[0 + i] + temp[4 + i];
00203 tmp11 = temp[0 + i] - temp[4 + i];
00204
00205 tmp13 = temp[2 + i] + temp[6 + i];
00206 tmp12 = MULTIPLY(temp[2 + i] - temp[6 + i], FIX_1_414213562) - tmp13;
00207
00208 tmp0 = tmp10 + tmp13;
00209 tmp3 = tmp10 - tmp13;
00210 tmp1 = tmp11 + tmp12;
00211 tmp2 = tmp11 - tmp12;
00212
00213 z13 = temp[5 + i] + temp[3 + i];
00214 z10 = temp[5 + i] - temp[3 + i];
00215 z11 = temp[1 + i] + temp[7 + i];
00216 z12 = temp[1 + i] - temp[7 + i];
00217
00218 tmp7 = z11 + z13;
00219 tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
00220
00221 z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
00222 tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
00223 tmp12 = MULTIPLY(z10, -FIX_2_613125930) + z5;
00224
00225 tmp6 = tmp12 - tmp7;
00226 tmp5 = tmp11 - tmp6;
00227 tmp4 = tmp10 + tmp5;
00228
00229 block[0 + i] = (tmp0 + tmp7) >> 6;
00230 block[7 + i] = (tmp0 - tmp7) >> 6;
00231 block[1 + i] = (tmp1 + tmp6) >> 6;
00232 block[6 + i] = (tmp1 - tmp6) >> 6;
00233 block[2 + i] = (tmp2 + tmp5) >> 6;
00234 block[5 + i] = (tmp2 - tmp5) >> 6;
00235 block[4 + i] = (tmp3 + tmp4) >> 6;
00236 block[3 + i] = (tmp3 - tmp4) >> 6;
00237 }
00238 }
00239
00240 static av_cold void init_vlcs(FourXContext *f)
00241 {
00242 static VLC_TYPE table[2][4][32][2];
00243 int i, j;
00244
00245 for (i = 0; i < 2; i++) {
00246 for (j = 0; j < 4; j++) {
00247 block_type_vlc[i][j].table = table[i][j];
00248 block_type_vlc[i][j].table_allocated = 32;
00249 init_vlc(&block_type_vlc[i][j], BLOCK_TYPE_VLC_BITS, 7,
00250 &block_type_tab[i][j][0][1], 2, 1,
00251 &block_type_tab[i][j][0][0], 2, 1,
00252 INIT_VLC_USE_NEW_STATIC);
00253 }
00254 }
00255 }
00256
00257 static void init_mv(FourXContext *f)
00258 {
00259 int i;
00260
00261 for (i = 0; i < 256; i++) {
00262 if (f->version > 1)
00263 f->mv[i] = mv[i][0] + mv[i][1] * f->current_picture.linesize[0] / 2;
00264 else
00265 f->mv[i] = (i & 15) - 8 + ((i >> 4) - 8) * f->current_picture.linesize[0] / 2;
00266 }
00267 }
00268
00269 #if HAVE_BIGENDIAN
00270 #define LE_CENTRIC_MUL(dst, src, scale, dc) \
00271 { \
00272 unsigned tmpval = AV_RN32(src); \
00273 tmpval = (tmpval << 16) | (tmpval >> 16); \
00274 tmpval = tmpval * (scale) + (dc); \
00275 tmpval = (tmpval << 16) | (tmpval >> 16); \
00276 AV_WN32A(dst, tmpval); \
00277 }
00278 #else
00279 #define LE_CENTRIC_MUL(dst, src, scale, dc) \
00280 { \
00281 unsigned tmpval = AV_RN32(src) * (scale) + (dc); \
00282 AV_WN32A(dst, tmpval); \
00283 }
00284 #endif
00285
00286 static inline void mcdc(uint16_t *dst, const uint16_t *src, int log2w,
00287 int h, int stride, int scale, unsigned dc)
00288 {
00289 int i;
00290 dc *= 0x10001;
00291
00292 switch (log2w) {
00293 case 0:
00294 for (i = 0; i < h; i++) {
00295 dst[0] = scale * src[0] + dc;
00296 if (scale)
00297 src += stride;
00298 dst += stride;
00299 }
00300 break;
00301 case 1:
00302 for (i = 0; i < h; i++) {
00303 LE_CENTRIC_MUL(dst, src, scale, dc);
00304 if (scale)
00305 src += stride;
00306 dst += stride;
00307 }
00308 break;
00309 case 2:
00310 for (i = 0; i < h; i++) {
00311 LE_CENTRIC_MUL(dst, src, scale, dc);
00312 LE_CENTRIC_MUL(dst + 2, src + 2, scale, dc);
00313 if (scale)
00314 src += stride;
00315 dst += stride;
00316 }
00317 break;
00318 case 3:
00319 for (i = 0; i < h; i++) {
00320 LE_CENTRIC_MUL(dst, src, scale, dc);
00321 LE_CENTRIC_MUL(dst + 2, src + 2, scale, dc);
00322 LE_CENTRIC_MUL(dst + 4, src + 4, scale, dc);
00323 LE_CENTRIC_MUL(dst + 6, src + 6, scale, dc);
00324 if (scale)
00325 src += stride;
00326 dst += stride;
00327 }
00328 break;
00329 default:
00330 av_assert2(0);
00331 }
00332 }
00333
00334 static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src,
00335 int log2w, int log2h, int stride)
00336 {
00337 const int index = size2index[log2h][log2w];
00338 const int h = 1 << log2h;
00339 int code = get_vlc2(&f->gb,
00340 block_type_vlc[1 - (f->version > 1)][index].table,
00341 BLOCK_TYPE_VLC_BITS, 1);
00342 uint16_t *start = (uint16_t *)f->last_picture.data[0];
00343 uint16_t *end = start + stride * (f->avctx->height - h + 1) - (1 << log2w);
00344
00345 av_assert2(code >= 0 && code <= 6);
00346
00347 if (code == 0) {
00348 if (f->g.buffer_end - f->g.buffer < 1) {
00349 av_log(f->avctx, AV_LOG_ERROR, "bytestream overread\n");
00350 return;
00351 }
00352 src += f->mv[bytestream2_get_byte(&f->g)];
00353 if (start > src || src > end) {
00354 av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
00355 return;
00356 }
00357 mcdc(dst, src, log2w, h, stride, 1, 0);
00358 } else if (code == 1) {
00359 log2h--;
00360 decode_p_block(f, dst, src, log2w, log2h, stride);
00361 decode_p_block(f, dst + (stride << log2h),
00362 src + (stride << log2h), log2w, log2h, stride);
00363 } else if (code == 2) {
00364 log2w--;
00365 decode_p_block(f, dst , src, log2w, log2h, stride);
00366 decode_p_block(f, dst + (1 << log2w),
00367 src + (1 << log2w), log2w, log2h, stride);
00368 } else if (code == 3 && f->version < 2) {
00369 mcdc(dst, src, log2w, h, stride, 1, 0);
00370 } else if (code == 4) {
00371 if (f->g.buffer_end - f->g.buffer < 1) {
00372 av_log(f->avctx, AV_LOG_ERROR, "bytestream overread\n");
00373 return;
00374 }
00375 src += f->mv[bytestream2_get_byte(&f->g)];
00376 if (start > src || src > end) {
00377 av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
00378 return;
00379 }
00380 if (f->g2.buffer_end - f->g2.buffer < 1){
00381 av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n");
00382 return;
00383 }
00384 mcdc(dst, src, log2w, h, stride, 1, bytestream2_get_le16(&f->g2));
00385 } else if (code == 5) {
00386 if (f->g2.buffer_end - f->g2.buffer < 1) {
00387 av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n");
00388 return;
00389 }
00390 mcdc(dst, src, log2w, h, stride, 0, bytestream2_get_le16(&f->g2));
00391 } else if (code == 6) {
00392 if (f->g2.buffer_end - f->g2.buffer < 2) {
00393 av_log(f->avctx, AV_LOG_ERROR, "wordstream overread\n");
00394 return;
00395 }
00396 if (log2w) {
00397 dst[0] = bytestream2_get_le16(&f->g2);
00398 dst[1] = bytestream2_get_le16(&f->g2);
00399 } else {
00400 dst[0] = bytestream2_get_le16(&f->g2);
00401 dst[stride] = bytestream2_get_le16(&f->g2);
00402 }
00403 }
00404 }
00405
00406 static int decode_p_frame(FourXContext *f, const uint8_t *buf, int length)
00407 {
00408 int x, y;
00409 const int width = f->avctx->width;
00410 const int height = f->avctx->height;
00411 uint16_t *src = (uint16_t *)f->last_picture.data[0];
00412 uint16_t *dst = (uint16_t *)f->current_picture.data[0];
00413 const int stride = f->current_picture.linesize[0] >> 1;
00414 unsigned int bitstream_size, bytestream_size, wordstream_size, extra,
00415 bytestream_offset, wordstream_offset;
00416
00417 if (f->version > 1) {
00418 extra = 20;
00419 if (length < extra)
00420 return -1;
00421 bitstream_size = AV_RL32(buf + 8);
00422 wordstream_size = AV_RL32(buf + 12);
00423 bytestream_size = AV_RL32(buf + 16);
00424 } else {
00425 extra = 0;
00426 bitstream_size = AV_RL16(buf - 4);
00427 wordstream_size = AV_RL16(buf - 2);
00428 bytestream_size = FFMAX(length - bitstream_size - wordstream_size, 0);
00429 }
00430
00431 if (bitstream_size > length ||
00432 bytestream_size > length - bitstream_size ||
00433 wordstream_size > length - bytestream_size - bitstream_size ||
00434 extra > length - bytestream_size - bitstream_size - wordstream_size) {
00435 av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
00436 bitstream_size+ bytestream_size+ wordstream_size - length);
00437 return -1;
00438 }
00439
00440 av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size,
00441 bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
00442 if (!f->bitstream_buffer)
00443 return AVERROR(ENOMEM);
00444 f->dsp.bswap_buf(f->bitstream_buffer, (const uint32_t*)(buf + extra),
00445 bitstream_size / 4);
00446 memset((uint8_t*)f->bitstream_buffer + bitstream_size,
00447 0, FF_INPUT_BUFFER_PADDING_SIZE);
00448 init_get_bits(&f->gb, f->bitstream_buffer, 8 * bitstream_size);
00449
00450 wordstream_offset = extra + bitstream_size;
00451 bytestream_offset = extra + bitstream_size + wordstream_size;
00452 bytestream2_init(&f->g2, buf + wordstream_offset,
00453 length - wordstream_offset);
00454 bytestream2_init(&f->g, buf + bytestream_offset,
00455 length - bytestream_offset);
00456
00457 init_mv(f);
00458
00459 for (y = 0; y < height; y += 8) {
00460 for (x = 0; x < width; x += 8)
00461 decode_p_block(f, dst + x, src + x, 3, 3, stride);
00462 src += 8 * stride;
00463 dst += 8 * stride;
00464 }
00465
00466 return 0;
00467 }
00468
00473 static int decode_i_block(FourXContext *f, DCTELEM *block)
00474 {
00475 int code, i, j, level, val;
00476
00477 if (get_bits_left(&f->gb) < 2){
00478 av_log(f->avctx, AV_LOG_ERROR, "%d bits left before decode_i_block()\n", get_bits_left(&f->gb));
00479 return -1;
00480 }
00481
00482
00483 val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
00484 if (val >> 4)
00485 av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
00486
00487 if (val)
00488 val = get_xbits(&f->gb, val);
00489
00490 val = val * dequant_table[0] + f->last_dc;
00491 f->last_dc = block[0] = val;
00492
00493 i = 1;
00494 for (;;) {
00495 code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
00496
00497
00498 if (code == 0)
00499 break;
00500 if (code == 0xf0) {
00501 i += 16;
00502 } else {
00503 level = get_xbits(&f->gb, code & 0xf);
00504 i += code >> 4;
00505 if (i >= 64) {
00506 av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
00507 return 0;
00508 }
00509
00510 j = ff_zigzag_direct[i];
00511 block[j] = level * dequant_table[j];
00512 i++;
00513 if (i >= 64)
00514 break;
00515 }
00516 }
00517
00518 return 0;
00519 }
00520
00521 static inline void idct_put(FourXContext *f, int x, int y)
00522 {
00523 DCTELEM (*block)[64] = f->block;
00524 int stride = f->current_picture.linesize[0] >> 1;
00525 int i;
00526 uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
00527
00528 for (i = 0; i < 4; i++) {
00529 block[i][0] += 0x80 * 8 * 8;
00530 idct(block[i]);
00531 }
00532
00533 if (!(f->avctx->flags & CODEC_FLAG_GRAY)) {
00534 for (i = 4; i < 6; i++)
00535 idct(block[i]);
00536 }
00537
00538
00539
00540
00541
00542 for (y = 0; y < 8; y++) {
00543 for (x = 0; x < 8; x++) {
00544 DCTELEM *temp = block[(x >> 2) + 2 * (y >> 2)] +
00545 2 * (x & 3) + 2 * 8 * (y & 3);
00546 int cb = block[4][x + 8 * y];
00547 int cr = block[5][x + 8 * y];
00548 int cg = (cb + cr) >> 1;
00549 int y;
00550
00551 cb += cb;
00552
00553 y = temp[0];
00554 dst[0] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
00555 y = temp[1];
00556 dst[1] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
00557 y = temp[8];
00558 dst[stride] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
00559 y = temp[9];
00560 dst[1 + stride] = ((y + cb) >> 3) + (((y - cg) & 0xFC) << 3) + (((y + cr) & 0xF8) << 8);
00561 dst += 2;
00562 }
00563 dst += 2 * stride - 2 * 8;
00564 }
00565 }
00566
00567 static int decode_i_mb(FourXContext *f)
00568 {
00569 int i;
00570
00571 f->dsp.clear_blocks(f->block[0]);
00572
00573 for (i = 0; i < 6; i++)
00574 if (decode_i_block(f, f->block[i]) < 0)
00575 return -1;
00576
00577 return 0;
00578 }
00579
00580 static const uint8_t *read_huffman_tables(FourXContext *f,
00581 const uint8_t * const buf, int buf_size)
00582 {
00583 int frequency[512] = { 0 };
00584 uint8_t flag[512];
00585 int up[512];
00586 uint8_t len_tab[257];
00587 int bits_tab[257];
00588 int start, end;
00589 const uint8_t *ptr = buf;
00590 const uint8_t *ptr_end = buf + buf_size;
00591 int j;
00592
00593 memset(up, -1, sizeof(up));
00594
00595 start = *ptr++;
00596 end = *ptr++;
00597 for (;;) {
00598 int i;
00599
00600 if (start <= end && ptr_end - ptr < end - start + 1 + 1)
00601 return NULL;
00602 for (i = start; i <= end; i++)
00603 frequency[i] = *ptr++;
00604 start = *ptr++;
00605 if (start == 0)
00606 break;
00607
00608 end = *ptr++;
00609 }
00610 frequency[256] = 1;
00611
00612 while ((ptr - buf) & 3)
00613 ptr++;
00614
00615 for (j = 257; j < 512; j++) {
00616 int min_freq[2] = { 256 * 256, 256 * 256 };
00617 int smallest[2] = { 0, 0 };
00618 int i;
00619 for (i = 0; i < j; i++) {
00620 if (frequency[i] == 0)
00621 continue;
00622 if (frequency[i] < min_freq[1]) {
00623 if (frequency[i] < min_freq[0]) {
00624 min_freq[1] = min_freq[0];
00625 smallest[1] = smallest[0];
00626 min_freq[0] = frequency[i];
00627 smallest[0] = i;
00628 } else {
00629 min_freq[1] = frequency[i];
00630 smallest[1] = i;
00631 }
00632 }
00633 }
00634 if (min_freq[1] == 256 * 256)
00635 break;
00636
00637 frequency[j] = min_freq[0] + min_freq[1];
00638 flag[smallest[0]] = 0;
00639 flag[smallest[1]] = 1;
00640 up[smallest[0]] =
00641 up[smallest[1]] = j;
00642 frequency[smallest[0]] = frequency[smallest[1]] = 0;
00643 }
00644
00645 for (j = 0; j < 257; j++) {
00646 int node, len = 0, bits = 0;
00647
00648 for (node = j; up[node] != -1; node = up[node]) {
00649 bits += flag[node] << len;
00650 len++;
00651 if (len > 31)
00652
00653 av_log(f->avctx, AV_LOG_ERROR,
00654 "vlc length overflow\n");
00655 }
00656
00657 bits_tab[j] = bits;
00658 len_tab[j] = len;
00659 }
00660
00661 if (init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257, len_tab, 1, 1,
00662 bits_tab, 4, 4, 0))
00663 return NULL;
00664
00665 return ptr;
00666 }
00667
00668 static int mix(int c0, int c1)
00669 {
00670 int blue = 2 * (c0 & 0x001F) + (c1 & 0x001F);
00671 int green = (2 * (c0 & 0x03E0) + (c1 & 0x03E0)) >> 5;
00672 int red = 2 * (c0 >> 10) + (c1 >> 10);
00673 return red / 3 * 1024 + green / 3 * 32 + blue / 3;
00674 }
00675
00676 static int decode_i2_frame(FourXContext *f, const uint8_t *buf, int length)
00677 {
00678 int x, y, x2, y2;
00679 const int width = f->avctx->width;
00680 const int height = f->avctx->height;
00681 const int mbs = (FFALIGN(width, 16) >> 4) * (FFALIGN(height, 16) >> 4);
00682 uint16_t *dst = (uint16_t*)f->current_picture.data[0];
00683 const int stride = f->current_picture.linesize[0]>>1;
00684 const uint8_t *buf_end = buf + length;
00685 GetByteContext g3;
00686
00687 if (length < mbs * 8) {
00688 av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n");
00689 return AVERROR_INVALIDDATA;
00690 }
00691 bytestream2_init(&g3, buf, length);
00692
00693 for (y = 0; y < height; y += 16) {
00694 for (x = 0; x < width; x += 16) {
00695 unsigned int color[4] = { 0 }, bits;
00696 if (buf_end - buf < 8)
00697 return -1;
00698
00699 color[0] = bytestream2_get_le16u(&g3);
00700 color[1] = bytestream2_get_le16u(&g3);
00701
00702 if (color[0] & 0x8000)
00703 av_log(NULL, AV_LOG_ERROR, "unk bit 1\n");
00704 if (color[1] & 0x8000)
00705 av_log(NULL, AV_LOG_ERROR, "unk bit 2\n");
00706
00707 color[2] = mix(color[0], color[1]);
00708 color[3] = mix(color[1], color[0]);
00709
00710 bits = bytestream2_get_le32u(&g3);
00711 for (y2 = 0; y2 < 16; y2++) {
00712 for (x2 = 0; x2 < 16; x2++) {
00713 int index = 2 * (x2 >> 2) + 8 * (y2 >> 2);
00714 dst[y2 * stride + x2] = color[(bits >> index) & 3];
00715 }
00716 }
00717 dst += 16;
00718 }
00719 dst += 16 * stride - x;
00720 }
00721
00722 return 0;
00723 }
00724
00725 static int decode_i_frame(FourXContext *f, const uint8_t *buf, int length)
00726 {
00727 int x, y;
00728 const int width = f->avctx->width;
00729 const int height = f->avctx->height;
00730 const unsigned int bitstream_size = AV_RL32(buf);
00731 unsigned int prestream_size;
00732 const uint8_t *prestream;
00733
00734 if (bitstream_size > (1<<26) || length < bitstream_size + 12) {
00735 av_log(f->avctx, AV_LOG_ERROR, "packet size too small\n");
00736 return AVERROR_INVALIDDATA;
00737 }
00738
00739 prestream_size = 4 * AV_RL32(buf + bitstream_size + 4);
00740 prestream = buf + bitstream_size + 12;
00741
00742 if (prestream_size + bitstream_size + 12 != length
00743 || bitstream_size > (1 << 26)
00744 || prestream_size > (1 << 26)) {
00745 av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n",
00746 prestream_size, bitstream_size, length);
00747 return -1;
00748 }
00749
00750 prestream = read_huffman_tables(f, prestream, buf + length - prestream);
00751 if (!prestream)
00752 return -1;
00753
00754 init_get_bits(&f->gb, buf + 4, 8 * bitstream_size);
00755
00756 prestream_size = length + buf - prestream;
00757
00758 av_fast_malloc(&f->bitstream_buffer, &f->bitstream_buffer_size,
00759 prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
00760 if (!f->bitstream_buffer)
00761 return AVERROR(ENOMEM);
00762 f->dsp.bswap_buf(f->bitstream_buffer, (const uint32_t*)prestream,
00763 prestream_size / 4);
00764 memset((uint8_t*)f->bitstream_buffer + prestream_size,
00765 0, FF_INPUT_BUFFER_PADDING_SIZE);
00766 init_get_bits(&f->pre_gb, f->bitstream_buffer, 8 * prestream_size);
00767
00768 f->last_dc = 0 * 128 * 8 * 8;
00769
00770 for (y = 0; y < height; y += 16) {
00771 for (x = 0; x < width; x += 16) {
00772 if (decode_i_mb(f) < 0)
00773 return -1;
00774
00775 idct_put(f, x, y);
00776 }
00777 }
00778
00779 if (get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
00780 av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
00781
00782 return 0;
00783 }
00784
00785 static int decode_frame(AVCodecContext *avctx, void *data,
00786 int *data_size, AVPacket *avpkt)
00787 {
00788 const uint8_t *buf = avpkt->data;
00789 int buf_size = avpkt->size;
00790 FourXContext *const f = avctx->priv_data;
00791 AVFrame *picture = data;
00792 AVFrame *p, temp;
00793 int i, frame_4cc, frame_size;
00794
00795 if (buf_size < 12)
00796 return AVERROR_INVALIDDATA;
00797 frame_4cc = AV_RL32(buf);
00798 if (buf_size != AV_RL32(buf + 4) + 8 || buf_size < 20)
00799 av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n",
00800 buf_size, AV_RL32(buf + 4));
00801
00802 if (frame_4cc == AV_RL32("cfrm")) {
00803 int free_index = -1;
00804 const int data_size = buf_size - 20;
00805 const int id = AV_RL32(buf + 12);
00806 const int whole_size = AV_RL32(buf + 16);
00807 CFrameBuffer *cfrm;
00808
00809 if (data_size < 0 || whole_size < 0) {
00810 av_log(f->avctx, AV_LOG_ERROR, "sizes invalid\n");
00811 return AVERROR_INVALIDDATA;
00812 }
00813
00814 for (i = 0; i < CFRAME_BUFFER_COUNT; i++)
00815 if (f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
00816 av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n",
00817 f->cfrm[i].id);
00818
00819 for (i = 0; i < CFRAME_BUFFER_COUNT; i++) {
00820 if (f->cfrm[i].id == id)
00821 break;
00822 if (f->cfrm[i].size == 0)
00823 free_index = i;
00824 }
00825
00826 if (i >= CFRAME_BUFFER_COUNT) {
00827 i = free_index;
00828 f->cfrm[i].id = id;
00829 }
00830 cfrm = &f->cfrm[i];
00831
00832 if (data_size > UINT_MAX - cfrm->size - FF_INPUT_BUFFER_PADDING_SIZE)
00833 return AVERROR_INVALIDDATA;
00834
00835 cfrm->data = av_fast_realloc(cfrm->data, &cfrm->allocated_size,
00836 cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
00837
00838 if (!cfrm->data) {
00839 av_log(f->avctx, AV_LOG_ERROR, "realloc falure\n");
00840 return -1;
00841 }
00842
00843 memcpy(cfrm->data + cfrm->size, buf + 20, data_size);
00844 cfrm->size += data_size;
00845
00846 if (cfrm->size >= whole_size) {
00847 buf = cfrm->data;
00848 frame_size = cfrm->size;
00849
00850 if (id != avctx->frame_number)
00851 av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n",
00852 id, avctx->frame_number);
00853
00854 cfrm->size = cfrm->id = 0;
00855 frame_4cc = AV_RL32("pfrm");
00856 } else
00857 return buf_size;
00858 } else {
00859 buf = buf + 12;
00860 frame_size = buf_size - 12;
00861 }
00862
00863 temp = f->current_picture;
00864 f->current_picture = f->last_picture;
00865 f->last_picture = temp;
00866
00867 p = &f->current_picture;
00868 avctx->coded_frame = p;
00869
00870
00871 avctx->flags |= CODEC_FLAG_EMU_EDGE;
00872
00873 p->reference= 3;
00874 if (avctx->reget_buffer(avctx, p) < 0) {
00875 av_log(avctx, AV_LOG_ERROR, "reget_buffer() failed\n");
00876 return -1;
00877 }
00878
00879 if (frame_4cc == AV_RL32("ifr2")) {
00880 p->pict_type= AV_PICTURE_TYPE_I;
00881 if (decode_i2_frame(f, buf - 4, frame_size + 4) < 0) {
00882 av_log(f->avctx, AV_LOG_ERROR, "decode i2 frame failed\n");
00883 return -1;
00884 }
00885 } else if (frame_4cc == AV_RL32("ifrm")) {
00886 p->pict_type= AV_PICTURE_TYPE_I;
00887 if (decode_i_frame(f, buf, frame_size) < 0) {
00888 av_log(f->avctx, AV_LOG_ERROR, "decode i frame failed\n");
00889 return -1;
00890 }
00891 } else if (frame_4cc == AV_RL32("pfrm") || frame_4cc == AV_RL32("pfr2")) {
00892 if (!f->last_picture.data[0]) {
00893 f->last_picture.reference = 3;
00894 if (avctx->get_buffer(avctx, &f->last_picture) < 0) {
00895 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00896 return -1;
00897 }
00898 }
00899
00900 p->pict_type = AV_PICTURE_TYPE_P;
00901 if (decode_p_frame(f, buf, frame_size) < 0) {
00902 av_log(f->avctx, AV_LOG_ERROR, "decode p frame failed\n");
00903 return -1;
00904 }
00905 } else if (frame_4cc == AV_RL32("snd_")) {
00906 av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n",
00907 buf_size);
00908 } else {
00909 av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n",
00910 buf_size);
00911 }
00912
00913 p->key_frame = p->pict_type == AV_PICTURE_TYPE_I;
00914
00915 *picture = *p;
00916 *data_size = sizeof(AVPicture);
00917
00918 emms_c();
00919
00920 return buf_size;
00921 }
00922
00923
00924 static av_cold void common_init(AVCodecContext *avctx)
00925 {
00926 FourXContext * const f = avctx->priv_data;
00927
00928 ff_dsputil_init(&f->dsp, avctx);
00929
00930 f->avctx = avctx;
00931 }
00932
00933 static av_cold int decode_init(AVCodecContext *avctx)
00934 {
00935 FourXContext * const f = avctx->priv_data;
00936
00937 if (avctx->extradata_size != 4 || !avctx->extradata) {
00938 av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n");
00939 return 1;
00940 }
00941 if((avctx->width % 16) || (avctx->height % 16)) {
00942 av_log(avctx, AV_LOG_ERROR, "unsupported width/height\n");
00943 return AVERROR_INVALIDDATA;
00944 }
00945
00946 avcodec_get_frame_defaults(&f->current_picture);
00947 avcodec_get_frame_defaults(&f->last_picture);
00948 f->version = AV_RL32(avctx->extradata) >> 16;
00949 common_init(avctx);
00950 init_vlcs(f);
00951
00952 if (f->version > 2)
00953 avctx->pix_fmt = PIX_FMT_RGB565;
00954 else
00955 avctx->pix_fmt = PIX_FMT_BGR555;
00956
00957 return 0;
00958 }
00959
00960
00961 static av_cold int decode_end(AVCodecContext *avctx)
00962 {
00963 FourXContext * const f = avctx->priv_data;
00964 int i;
00965
00966 av_freep(&f->bitstream_buffer);
00967 f->bitstream_buffer_size = 0;
00968 for (i = 0; i < CFRAME_BUFFER_COUNT; i++) {
00969 av_freep(&f->cfrm[i].data);
00970 f->cfrm[i].allocated_size = 0;
00971 }
00972 ff_free_vlc(&f->pre_vlc);
00973 if (f->current_picture.data[0])
00974 avctx->release_buffer(avctx, &f->current_picture);
00975 if (f->last_picture.data[0])
00976 avctx->release_buffer(avctx, &f->last_picture);
00977
00978 return 0;
00979 }
00980
00981 AVCodec ff_fourxm_decoder = {
00982 .name = "4xm",
00983 .type = AVMEDIA_TYPE_VIDEO,
00984 .id = AV_CODEC_ID_4XM,
00985 .priv_data_size = sizeof(FourXContext),
00986 .init = decode_init,
00987 .close = decode_end,
00988 .decode = decode_frame,
00989 .capabilities = CODEC_CAP_DR1,
00990 .long_name = NULL_IF_CONFIG_SMALL("4X Movie"),
00991 };