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