00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avcodec.h"
00028 #include "dsputil.h"
00029 #include "mpegvideo.h"
00030 #include "bytestream.h"
00031
00032
00033
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 static const uint8_t dequant_table[64]={
00108 16, 15, 13, 19, 24, 31, 28, 17,
00109 17, 23, 25, 31, 36, 63, 45, 21,
00110 18, 24, 27, 37, 52, 59, 49, 20,
00111 16, 28, 34, 40, 60, 80, 51, 20,
00112 18, 31, 48, 66, 68, 86, 56, 21,
00113 19, 38, 56, 59, 64, 64, 48, 20,
00114 27, 48, 55, 55, 56, 51, 35, 15,
00115 20, 35, 34, 32, 31, 22, 15, 8,
00116 };
00117
00118 static VLC block_type_vlc[2][4];
00119
00120
00121 typedef struct CFrameBuffer{
00122 unsigned int allocated_size;
00123 unsigned int size;
00124 int id;
00125 uint8_t *data;
00126 }CFrameBuffer;
00127
00128 typedef struct FourXContext{
00129 AVCodecContext *avctx;
00130 DSPContext dsp;
00131 AVFrame current_picture, last_picture;
00132 GetBitContext pre_gb;
00133 GetBitContext gb;
00134 uint8_t *bytestream;
00135 uint16_t *wordstream;
00136 int mv[256];
00137 VLC pre_vlc;
00138 int last_dc;
00139 DECLARE_ALIGNED_8(DCTELEM, block[6][64]);
00140 uint8_t *bitstream_buffer;
00141 unsigned int bitstream_buffer_size;
00142 int version;
00143 CFrameBuffer cfrm[CFRAME_BUFFER_COUNT];
00144 } FourXContext;
00145
00146
00147 #define FIX_1_082392200 70936
00148 #define FIX_1_414213562 92682
00149 #define FIX_1_847759065 121095
00150 #define FIX_2_613125930 171254
00151
00152 #define MULTIPLY(var,const) (((var)*(const)) >> 16)
00153
00154 static void idct(DCTELEM block[64]){
00155 int tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
00156 int tmp10, tmp11, tmp12, tmp13;
00157 int z5, z10, z11, z12, z13;
00158 int i;
00159 int temp[64];
00160
00161 for(i=0; i<8; i++){
00162 tmp10 = block[8*0 + i] + block[8*4 + i];
00163 tmp11 = block[8*0 + i] - block[8*4 + i];
00164
00165 tmp13 = block[8*2 + i] + block[8*6 + i];
00166 tmp12 = MULTIPLY(block[8*2 + i] - block[8*6 + i], FIX_1_414213562) - tmp13;
00167
00168 tmp0 = tmp10 + tmp13;
00169 tmp3 = tmp10 - tmp13;
00170 tmp1 = tmp11 + tmp12;
00171 tmp2 = tmp11 - tmp12;
00172
00173 z13 = block[8*5 + i] + block[8*3 + i];
00174 z10 = block[8*5 + i] - block[8*3 + i];
00175 z11 = block[8*1 + i] + block[8*7 + i];
00176 z12 = block[8*1 + i] - block[8*7 + i];
00177
00178 tmp7 = z11 + z13;
00179 tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
00180
00181 z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
00182 tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
00183 tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5;
00184
00185 tmp6 = tmp12 - tmp7;
00186 tmp5 = tmp11 - tmp6;
00187 tmp4 = tmp10 + tmp5;
00188
00189 temp[8*0 + i] = tmp0 + tmp7;
00190 temp[8*7 + i] = tmp0 - tmp7;
00191 temp[8*1 + i] = tmp1 + tmp6;
00192 temp[8*6 + i] = tmp1 - tmp6;
00193 temp[8*2 + i] = tmp2 + tmp5;
00194 temp[8*5 + i] = tmp2 - tmp5;
00195 temp[8*4 + i] = tmp3 + tmp4;
00196 temp[8*3 + i] = tmp3 - tmp4;
00197 }
00198
00199 for(i=0; i<8*8; i+=8){
00200 tmp10 = temp[0 + i] + temp[4 + i];
00201 tmp11 = temp[0 + i] - temp[4 + i];
00202
00203 tmp13 = temp[2 + i] + temp[6 + i];
00204 tmp12 = MULTIPLY(temp[2 + i] - temp[6 + i], FIX_1_414213562) - tmp13;
00205
00206 tmp0 = tmp10 + tmp13;
00207 tmp3 = tmp10 - tmp13;
00208 tmp1 = tmp11 + tmp12;
00209 tmp2 = tmp11 - tmp12;
00210
00211 z13 = temp[5 + i] + temp[3 + i];
00212 z10 = temp[5 + i] - temp[3 + i];
00213 z11 = temp[1 + i] + temp[7 + i];
00214 z12 = temp[1 + i] - temp[7 + i];
00215
00216 tmp7 = z11 + z13;
00217 tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562);
00218
00219 z5 = MULTIPLY(z10 + z12, FIX_1_847759065);
00220 tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5;
00221 tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5;
00222
00223 tmp6 = tmp12 - tmp7;
00224 tmp5 = tmp11 - tmp6;
00225 tmp4 = tmp10 + tmp5;
00226
00227 block[0 + i] = (tmp0 + tmp7)>>6;
00228 block[7 + i] = (tmp0 - tmp7)>>6;
00229 block[1 + i] = (tmp1 + tmp6)>>6;
00230 block[6 + i] = (tmp1 - tmp6)>>6;
00231 block[2 + i] = (tmp2 + tmp5)>>6;
00232 block[5 + i] = (tmp2 - tmp5)>>6;
00233 block[4 + i] = (tmp3 + tmp4)>>6;
00234 block[3 + i] = (tmp3 - tmp4)>>6;
00235 }
00236 }
00237
00238 static void init_vlcs(FourXContext *f){
00239 int i;
00240
00241 for(i=0; i<8; i++){
00242 init_vlc(&block_type_vlc[0][i], BLOCK_TYPE_VLC_BITS, 7,
00243 &block_type_tab[0][i][0][1], 2, 1,
00244 &block_type_tab[0][i][0][0], 2, 1, 1);
00245 }
00246 }
00247
00248 static void init_mv(FourXContext *f){
00249 int i;
00250
00251 for(i=0; i<256; i++){
00252 if(f->version>1)
00253 f->mv[i] = mv[i][0] + mv[i][1] *f->current_picture.linesize[0]/2;
00254 else
00255 f->mv[i] = (i&15) - 8 + ((i>>4)-8)*f->current_picture.linesize[0]/2;
00256 }
00257 }
00258
00259 static inline void mcdc(uint16_t *dst, uint16_t *src, int log2w, int h, int stride, int scale, int dc){
00260 int i;
00261 dc*= 0x10001;
00262
00263 switch(log2w){
00264 case 0:
00265 for(i=0; i<h; i++){
00266 dst[0] = scale*src[0] + dc;
00267 if(scale) src += stride;
00268 dst += stride;
00269 }
00270 break;
00271 case 1:
00272 for(i=0; i<h; i++){
00273 ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
00274 if(scale) src += stride;
00275 dst += stride;
00276 }
00277 break;
00278 case 2:
00279 for(i=0; i<h; i++){
00280 ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
00281 ((uint32_t*)dst)[1] = scale*((uint32_t*)src)[1] + dc;
00282 if(scale) src += stride;
00283 dst += stride;
00284 }
00285 break;
00286 case 3:
00287 for(i=0; i<h; i++){
00288 ((uint32_t*)dst)[0] = scale*((uint32_t*)src)[0] + dc;
00289 ((uint32_t*)dst)[1] = scale*((uint32_t*)src)[1] + dc;
00290 ((uint32_t*)dst)[2] = scale*((uint32_t*)src)[2] + dc;
00291 ((uint32_t*)dst)[3] = scale*((uint32_t*)src)[3] + dc;
00292 if(scale) src += stride;
00293 dst += stride;
00294 }
00295 break;
00296 default: assert(0);
00297 }
00298 }
00299
00300 static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, int log2w, int log2h, int stride){
00301 const int index= size2index[log2h][log2w];
00302 const int h= 1<<log2h;
00303 int code= get_vlc2(&f->gb, block_type_vlc[1-(f->version>1)][index].table, BLOCK_TYPE_VLC_BITS, 1);
00304 uint16_t *start= f->last_picture.data[0];
00305 uint16_t *end= start + stride*(f->avctx->height-h+1) - (1<<log2w);
00306
00307 assert(code>=0 && code<=6);
00308
00309 if(code == 0){
00310 src += f->mv[ *f->bytestream++ ];
00311 if(start > src || src > end){
00312 av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
00313 return;
00314 }
00315 mcdc(dst, src, log2w, h, stride, 1, 0);
00316 }else if(code == 1){
00317 log2h--;
00318 decode_p_block(f, dst , src , log2w, log2h, stride);
00319 decode_p_block(f, dst + (stride<<log2h), src + (stride<<log2h), log2w, log2h, stride);
00320 }else if(code == 2){
00321 log2w--;
00322 decode_p_block(f, dst , src , log2w, log2h, stride);
00323 decode_p_block(f, dst + (1<<log2w), src + (1<<log2w), log2w, log2h, stride);
00324 }else if(code == 3 && f->version<2){
00325 mcdc(dst, src, log2w, h, stride, 1, 0);
00326 }else if(code == 4){
00327 src += f->mv[ *f->bytestream++ ];
00328 if(start > src || src > end){
00329 av_log(f->avctx, AV_LOG_ERROR, "mv out of pic\n");
00330 return;
00331 }
00332 mcdc(dst, src, log2w, h, stride, 1, le2me_16(*f->wordstream++));
00333 }else if(code == 5){
00334 mcdc(dst, src, log2w, h, stride, 0, le2me_16(*f->wordstream++));
00335 }else if(code == 6){
00336 if(log2w){
00337 dst[0] = le2me_16(*f->wordstream++);
00338 dst[1] = le2me_16(*f->wordstream++);
00339 }else{
00340 dst[0 ] = le2me_16(*f->wordstream++);
00341 dst[stride] = le2me_16(*f->wordstream++);
00342 }
00343 }
00344 }
00345
00346 static int get32(void *p){
00347 return le2me_32(*(uint32_t*)p);
00348 }
00349
00350 static int decode_p_frame(FourXContext *f, uint8_t *buf, int length){
00351 int x, y;
00352 const int width= f->avctx->width;
00353 const int height= f->avctx->height;
00354 uint16_t *src= (uint16_t*)f->last_picture.data[0];
00355 uint16_t *dst= (uint16_t*)f->current_picture.data[0];
00356 const int stride= f->current_picture.linesize[0]>>1;
00357 unsigned int bitstream_size, bytestream_size, wordstream_size, extra;
00358
00359 if(f->version>1){
00360 extra=20;
00361 bitstream_size= get32(buf+8);
00362 wordstream_size= get32(buf+12);
00363 bytestream_size= get32(buf+16);
00364 }else{
00365 extra=0;
00366 bitstream_size = AV_RL16(buf-4);
00367 wordstream_size= AV_RL16(buf-2);
00368 bytestream_size= FFMAX(length - bitstream_size - wordstream_size, 0);
00369 }
00370
00371 if(bitstream_size+ bytestream_size+ wordstream_size + extra != length
00372 || bitstream_size > (1<<26)
00373 || bytestream_size > (1<<26)
00374 || wordstream_size > (1<<26)
00375 ){
00376 av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
00377 bitstream_size+ bytestream_size+ wordstream_size - length);
00378 return -1;
00379 }
00380
00381 f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
00382 f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)(buf + extra), bitstream_size/4);
00383 init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size);
00384
00385 f->wordstream= (uint16_t*)(buf + extra + bitstream_size);
00386 f->bytestream= buf + extra + bitstream_size + wordstream_size;
00387
00388 init_mv(f);
00389
00390 for(y=0; y<height; y+=8){
00391 for(x=0; x<width; x+=8){
00392 decode_p_block(f, dst + x, src + x, 3, 3, stride);
00393 }
00394 src += 8*stride;
00395 dst += 8*stride;
00396 }
00397
00398 if( bitstream_size != (get_bits_count(&f->gb)+31)/32*4
00399 || (((char*)f->wordstream - (char*)buf + 2)&~2) != extra + bitstream_size + wordstream_size
00400 || (((char*)f->bytestream - (char*)buf + 3)&~3) != extra + bitstream_size + wordstream_size + bytestream_size)
00401 av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n",
00402 bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
00403 -(((char*)f->bytestream - (char*)buf + 3)&~3) + (extra + bitstream_size + wordstream_size + bytestream_size),
00404 -(((char*)f->wordstream - (char*)buf + 2)&~2) + (extra + bitstream_size + wordstream_size)
00405 );
00406
00407 return 0;
00408 }
00409
00414 static int decode_i_block(FourXContext *f, DCTELEM *block){
00415 int code, i, j, level, val;
00416
00417
00418 val = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
00419 if (val>>4){
00420 av_log(f->avctx, AV_LOG_ERROR, "error dc run != 0\n");
00421 }
00422
00423 if(val)
00424 val = get_xbits(&f->gb, val);
00425
00426 val = val * dequant_table[0] + f->last_dc;
00427 f->last_dc =
00428 block[0] = val;
00429
00430 i = 1;
00431 for(;;) {
00432 code = get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3);
00433
00434
00435 if (code == 0)
00436 break;
00437 if (code == 0xf0) {
00438 i += 16;
00439 } else {
00440 level = get_xbits(&f->gb, code & 0xf);
00441 i += code >> 4;
00442 if (i >= 64) {
00443 av_log(f->avctx, AV_LOG_ERROR, "run %d oveflow\n", i);
00444 return 0;
00445 }
00446
00447 j= ff_zigzag_direct[i];
00448 block[j] = level * dequant_table[j];
00449 i++;
00450 if (i >= 64)
00451 break;
00452 }
00453 }
00454
00455 return 0;
00456 }
00457
00458 static inline void idct_put(FourXContext *f, int x, int y){
00459 DCTELEM (*block)[64]= f->block;
00460 int stride= f->current_picture.linesize[0]>>1;
00461 int i;
00462 uint16_t *dst = ((uint16_t*)f->current_picture.data[0]) + y * stride + x;
00463
00464 for(i=0; i<4; i++){
00465 block[i][0] += 0x80*8*8;
00466 idct(block[i]);
00467 }
00468
00469 if(!(f->avctx->flags&CODEC_FLAG_GRAY)){
00470 for(i=4; i<6; i++) idct(block[i]);
00471 }
00472
00473
00474
00475
00476
00477
00478 for(y=0; y<8; y++){
00479 for(x=0; x<8; x++){
00480 DCTELEM *temp= block[(x>>2) + 2*(y>>2)] + 2*(x&3) + 2*8*(y&3);
00481 int cb= block[4][x + 8*y];
00482 int cr= block[5][x + 8*y];
00483 int cg= (cb + cr)>>1;
00484 int y;
00485
00486 cb+=cb;
00487
00488 y = temp[0];
00489 dst[0 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
00490 y = temp[1];
00491 dst[1 ]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
00492 y = temp[8];
00493 dst[ stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
00494 y = temp[9];
00495 dst[1+stride]= ((y+cb)>>3) + (((y-cg)&0xFC)<<3) + (((y+cr)&0xF8)<<8);
00496 dst += 2;
00497 }
00498 dst += 2*stride - 2*8;
00499 }
00500 }
00501
00502 static int decode_i_mb(FourXContext *f){
00503 int i;
00504
00505 f->dsp.clear_blocks(f->block[0]);
00506
00507 for(i=0; i<6; i++){
00508 if(decode_i_block(f, f->block[i]) < 0)
00509 return -1;
00510 }
00511
00512 return 0;
00513 }
00514
00515 static uint8_t *read_huffman_tables(FourXContext *f, uint8_t * const buf){
00516 int frequency[512];
00517 uint8_t flag[512];
00518 int up[512];
00519 uint8_t len_tab[257];
00520 int bits_tab[257];
00521 int start, end;
00522 uint8_t *ptr= buf;
00523 int j;
00524
00525 memset(frequency, 0, sizeof(frequency));
00526 memset(up, -1, sizeof(up));
00527
00528 start= *ptr++;
00529 end= *ptr++;
00530 for(;;){
00531 int i;
00532
00533 for(i=start; i<=end; i++){
00534 frequency[i]= *ptr++;
00535 }
00536 start= *ptr++;
00537 if(start==0) break;
00538
00539 end= *ptr++;
00540 }
00541 frequency[256]=1;
00542
00543 while((ptr - buf)&3) ptr++;
00544
00545 for(j=257; j<512; j++){
00546 int min_freq[2]= {256*256, 256*256};
00547 int smallest[2]= {0, 0};
00548 int i;
00549 for(i=0; i<j; i++){
00550 if(frequency[i] == 0) continue;
00551 if(frequency[i] < min_freq[1]){
00552 if(frequency[i] < min_freq[0]){
00553 min_freq[1]= min_freq[0]; smallest[1]= smallest[0];
00554 min_freq[0]= frequency[i];smallest[0]= i;
00555 }else{
00556 min_freq[1]= frequency[i];smallest[1]= i;
00557 }
00558 }
00559 }
00560 if(min_freq[1] == 256*256) break;
00561
00562 frequency[j]= min_freq[0] + min_freq[1];
00563 flag[ smallest[0] ]= 0;
00564 flag[ smallest[1] ]= 1;
00565 up[ smallest[0] ]=
00566 up[ smallest[1] ]= j;
00567 frequency[ smallest[0] ]= frequency[ smallest[1] ]= 0;
00568 }
00569
00570 for(j=0; j<257; j++){
00571 int node;
00572 int len=0;
00573 int bits=0;
00574
00575 for(node= j; up[node] != -1; node= up[node]){
00576 bits += flag[node]<<len;
00577 len++;
00578 if(len > 31) av_log(f->avctx, AV_LOG_ERROR, "vlc length overflow\n");
00579 }
00580
00581 bits_tab[j]= bits;
00582 len_tab[j]= len;
00583 }
00584
00585 init_vlc(&f->pre_vlc, ACDC_VLC_BITS, 257,
00586 len_tab , 1, 1,
00587 bits_tab, 4, 4, 0);
00588
00589 return ptr;
00590 }
00591
00592 static int mix(int c0, int c1){
00593 int blue = 2*(c0&0x001F) + (c1&0x001F);
00594 int green= (2*(c0&0x03E0) + (c1&0x03E0))>>5;
00595 int red = 2*(c0>>10) + (c1>>10);
00596 return red/3*1024 + green/3*32 + blue/3;
00597 }
00598
00599 static int decode_i2_frame(FourXContext *f, uint8_t *buf, int length){
00600 int x, y, x2, y2;
00601 const int width= f->avctx->width;
00602 const int height= f->avctx->height;
00603 uint16_t *dst= (uint16_t*)f->current_picture.data[0];
00604 const int stride= f->current_picture.linesize[0]>>1;
00605
00606 for(y=0; y<height; y+=16){
00607 for(x=0; x<width; x+=16){
00608 unsigned int color[4], bits;
00609 memset(color, 0, sizeof(color));
00610
00611 color[0]= bytestream_get_le16(&buf);
00612 color[1]= bytestream_get_le16(&buf);
00613
00614 if(color[0]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 1\n");
00615 if(color[1]&0x8000) av_log(NULL, AV_LOG_ERROR, "unk bit 2\n");
00616
00617 color[2]= mix(color[0], color[1]);
00618 color[3]= mix(color[1], color[0]);
00619
00620 bits= bytestream_get_le32(&buf);
00621 for(y2=0; y2<16; y2++){
00622 for(x2=0; x2<16; x2++){
00623 int index= 2*(x2>>2) + 8*(y2>>2);
00624 dst[y2*stride+x2]= color[(bits>>index)&3];
00625 }
00626 }
00627 dst+=16;
00628 }
00629 dst += 16*stride - width;
00630 }
00631
00632 return 0;
00633 }
00634
00635 static int decode_i_frame(FourXContext *f, uint8_t *buf, int length){
00636 int x, y;
00637 const int width= f->avctx->width;
00638 const int height= f->avctx->height;
00639 uint16_t *dst= (uint16_t*)f->current_picture.data[0];
00640 const int stride= f->current_picture.linesize[0]>>1;
00641 const unsigned int bitstream_size= get32(buf);
00642 const int token_count av_unused = get32(buf + bitstream_size + 8);
00643 unsigned int prestream_size= 4*get32(buf + bitstream_size + 4);
00644 uint8_t *prestream= buf + bitstream_size + 12;
00645
00646 if(prestream_size + bitstream_size + 12 != length
00647 || bitstream_size > (1<<26)
00648 || prestream_size > (1<<26)){
00649 av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d %d\n", prestream_size, bitstream_size, length);
00650 return -1;
00651 }
00652
00653 prestream= read_huffman_tables(f, prestream);
00654
00655 init_get_bits(&f->gb, buf + 4, 8*bitstream_size);
00656
00657 prestream_size= length + buf - prestream;
00658
00659 f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, prestream_size + FF_INPUT_BUFFER_PADDING_SIZE);
00660 f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)prestream, prestream_size/4);
00661 init_get_bits(&f->pre_gb, f->bitstream_buffer, 8*prestream_size);
00662
00663 f->last_dc= 0*128*8*8;
00664
00665 for(y=0; y<height; y+=16){
00666 for(x=0; x<width; x+=16){
00667 if(decode_i_mb(f) < 0)
00668 return -1;
00669
00670 idct_put(f, x, y);
00671 }
00672 dst += 16*stride;
00673 }
00674
00675 if(get_vlc2(&f->pre_gb, f->pre_vlc.table, ACDC_VLC_BITS, 3) != 256)
00676 av_log(f->avctx, AV_LOG_ERROR, "end mismatch\n");
00677
00678 return 0;
00679 }
00680
00681 static int decode_frame(AVCodecContext *avctx,
00682 void *data, int *data_size,
00683 uint8_t *buf, int buf_size)
00684 {
00685 FourXContext * const f = avctx->priv_data;
00686 AVFrame *picture = data;
00687 AVFrame *p, temp;
00688 int i, frame_4cc, frame_size;
00689
00690 frame_4cc= get32(buf);
00691 if(buf_size != get32(buf+4)+8 || buf_size < 20){
00692 av_log(f->avctx, AV_LOG_ERROR, "size mismatch %d %d\n", buf_size, get32(buf+4));
00693 }
00694
00695 if(frame_4cc == ff_get_fourcc("cfrm")){
00696 int free_index=-1;
00697 const int data_size= buf_size - 20;
00698 const int id= get32(buf+12);
00699 const int whole_size= get32(buf+16);
00700 CFrameBuffer *cfrm;
00701
00702 for(i=0; i<CFRAME_BUFFER_COUNT; i++){
00703 if(f->cfrm[i].id && f->cfrm[i].id < avctx->frame_number)
00704 av_log(f->avctx, AV_LOG_ERROR, "lost c frame %d\n", f->cfrm[i].id);
00705 }
00706
00707 for(i=0; i<CFRAME_BUFFER_COUNT; i++){
00708 if(f->cfrm[i].id == id) break;
00709 if(f->cfrm[i].size == 0 ) free_index= i;
00710 }
00711
00712 if(i>=CFRAME_BUFFER_COUNT){
00713 i= free_index;
00714 f->cfrm[i].id= id;
00715 }
00716 cfrm= &f->cfrm[i];
00717
00718 cfrm->data= av_fast_realloc(cfrm->data, &cfrm->allocated_size, cfrm->size + data_size + FF_INPUT_BUFFER_PADDING_SIZE);
00719 if(!cfrm->data){
00720 av_log(f->avctx, AV_LOG_ERROR, "realloc falure");
00721 return -1;
00722 }
00723
00724 memcpy(cfrm->data + cfrm->size, buf+20, data_size);
00725 cfrm->size += data_size;
00726
00727 if(cfrm->size >= whole_size){
00728 buf= cfrm->data;
00729 frame_size= cfrm->size;
00730
00731 if(id != avctx->frame_number){
00732 av_log(f->avctx, AV_LOG_ERROR, "cframe id mismatch %d %d\n", id, avctx->frame_number);
00733 }
00734
00735 cfrm->size= cfrm->id= 0;
00736 frame_4cc= ff_get_fourcc("pfrm");
00737 }else
00738 return buf_size;
00739 }else{
00740 buf= buf + 12;
00741 frame_size= buf_size - 12;
00742 }
00743
00744 temp= f->current_picture;
00745 f->current_picture= f->last_picture;
00746 f->last_picture= temp;
00747
00748 p= &f->current_picture;
00749 avctx->coded_frame= p;
00750
00751 avctx->flags |= CODEC_FLAG_EMU_EDGE;
00752
00753 if(p->data[0])
00754 avctx->release_buffer(avctx, p);
00755
00756 p->reference= 1;
00757 if(avctx->get_buffer(avctx, p) < 0){
00758 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00759 return -1;
00760 }
00761
00762 if(frame_4cc == ff_get_fourcc("ifr2")){
00763 p->pict_type= I_TYPE;
00764 if(decode_i2_frame(f, buf-4, frame_size) < 0)
00765 return -1;
00766 }else if(frame_4cc == ff_get_fourcc("ifrm")){
00767 p->pict_type= I_TYPE;
00768 if(decode_i_frame(f, buf, frame_size) < 0)
00769 return -1;
00770 }else if(frame_4cc == ff_get_fourcc("pfrm") || frame_4cc == ff_get_fourcc("pfr2")){
00771 p->pict_type= P_TYPE;
00772 if(decode_p_frame(f, buf, frame_size) < 0)
00773 return -1;
00774 }else if(frame_4cc == ff_get_fourcc("snd_")){
00775 av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);
00776 }else{
00777 av_log(avctx, AV_LOG_ERROR, "ignoring unknown chunk length:%d\n", buf_size);
00778 }
00779
00780 p->key_frame= p->pict_type == I_TYPE;
00781
00782 *picture= *p;
00783 *data_size = sizeof(AVPicture);
00784
00785 emms_c();
00786
00787 return buf_size;
00788 }
00789
00790
00791 static void common_init(AVCodecContext *avctx){
00792 FourXContext * const f = avctx->priv_data;
00793
00794 dsputil_init(&f->dsp, avctx);
00795
00796 f->avctx= avctx;
00797 }
00798
00799 static int decode_init(AVCodecContext *avctx){
00800 FourXContext * const f = avctx->priv_data;
00801
00802 if(avctx->extradata_size != 4 || !avctx->extradata) {
00803 av_log(avctx, AV_LOG_ERROR, "extradata wrong or missing\n");
00804 return 1;
00805 }
00806
00807 f->version= AV_RL32(avctx->extradata)>>16;
00808 common_init(avctx);
00809 init_vlcs(f);
00810
00811 if(f->version>2) avctx->pix_fmt= PIX_FMT_RGB565;
00812 else avctx->pix_fmt= PIX_FMT_RGB555;
00813
00814 return 0;
00815 }
00816
00817
00818 static int decode_end(AVCodecContext *avctx){
00819 FourXContext * const f = avctx->priv_data;
00820 int i;
00821
00822 av_freep(&f->bitstream_buffer);
00823 f->bitstream_buffer_size=0;
00824 for(i=0; i<CFRAME_BUFFER_COUNT; i++){
00825 av_freep(&f->cfrm[i].data);
00826 f->cfrm[i].allocated_size= 0;
00827 }
00828 free_vlc(&f->pre_vlc);
00829
00830 return 0;
00831 }
00832
00833 AVCodec fourxm_decoder = {
00834 "4xm",
00835 CODEC_TYPE_VIDEO,
00836 CODEC_ID_4XM,
00837 sizeof(FourXContext),
00838 decode_init,
00839 NULL,
00840 decode_end,
00841 decode_frame,
00842
00843 };
00844