comparison 4xm.c @ 5529:af68496af656 libavcodec

add second vlc table pfr* of Toy-Story2_better-image-quality.4xa are decoded now without bitstream errors
author michael
date Sun, 12 Aug 2007 12:22:19 +0000
parents ce36118abbbb
children 80ee0d3dd53e
comparison
equal deleted inserted replaced
5528:4c3b1222ff57 5529:af68496af656
34 #define BLOCK_TYPE_VLC_BITS 5 34 #define BLOCK_TYPE_VLC_BITS 5
35 #define ACDC_VLC_BITS 9 35 #define ACDC_VLC_BITS 9
36 36
37 #define CFRAME_BUFFER_COUNT 100 37 #define CFRAME_BUFFER_COUNT 100
38 38
39 static const uint8_t block_type_tab[4][8][2]={ 39 static const uint8_t block_type_tab[2][4][8][2]={
40 {
40 { //{8,4,2}x{8,4,2} 41 { //{8,4,2}x{8,4,2}
41 { 0,1}, { 2,2}, { 6,3}, {14,4}, {30,5}, {31,5}, { 0,0} 42 { 0,1}, { 2,2}, { 6,3}, {14,4}, {30,5}, {31,5}, { 0,0}
42 },{ //{8,4}x1 43 },{ //{8,4}x1
43 { 0,1}, { 0,0}, { 2,2}, { 6,3}, {14,4}, {15,4}, { 0,0} 44 { 0,1}, { 0,0}, { 2,2}, { 6,3}, {14,4}, {15,4}, { 0,0}
44 },{ //1x{8,4} 45 },{ //1x{8,4}
45 { 0,1}, { 2,2}, { 0,0}, { 6,3}, {14,4}, {15,4}, { 0,0} 46 { 0,1}, { 2,2}, { 0,0}, { 6,3}, {14,4}, {15,4}, { 0,0}
46 },{ //1x2, 2x1 47 },{ //1x2, 2x1
47 { 0,1}, { 0,0}, { 0,0}, { 2,2}, { 6,3}, {14,4}, {15,4} 48 { 0,1}, { 0,0}, { 0,0}, { 2,2}, { 6,3}, {14,4}, {15,4}
48 } 49 }
50 },{
51 { //{8,4,2}x{8,4,2}
52 { 1,2}, { 4,3}, { 5,3}, {0,2}, {6,3}, {7,3}, {0,0}
53 },{//{8,4}x1
54 { 1,2}, { 0,0}, { 2,2}, {0,2}, {6,3}, {7,3}, {0,0}
55 },{//1x{8,4}
56 { 1,2}, { 2,2}, { 0,0}, {0,2}, {6,3}, {7,3}, {0,0}
57 },{//1x2, 2x1
58 { 1,2}, { 0,0}, { 0,0}, {0,2}, {2,2}, {6,3}, {7,3}
59 }
60 }
49 }; 61 };
50 62
51 static const uint8_t size2index[4][4]={ 63 static const uint8_t size2index[4][4]={
52 {-1, 3, 1, 1}, 64 {-1, 3, 1, 1},
53 { 3, 0, 0, 0}, 65 { 3, 0, 0, 0},
100 19, 38, 56, 59, 64, 64, 48, 20, 112 19, 38, 56, 59, 64, 64, 48, 20,
101 27, 48, 55, 55, 56, 51, 35, 15, 113 27, 48, 55, 55, 56, 51, 35, 15,
102 20, 35, 34, 32, 31, 22, 15, 8, 114 20, 35, 34, 32, 31, 22, 15, 8,
103 }; 115 };
104 116
105 static VLC block_type_vlc[4]; 117 static VLC block_type_vlc[2][4];
106 118
107 119
108 typedef struct CFrameBuffer{ 120 typedef struct CFrameBuffer{
109 unsigned int allocated_size; 121 unsigned int allocated_size;
110 unsigned int size; 122 unsigned int size;
124 VLC pre_vlc; 136 VLC pre_vlc;
125 int last_dc; 137 int last_dc;
126 DECLARE_ALIGNED_8(DCTELEM, block[6][64]); 138 DECLARE_ALIGNED_8(DCTELEM, block[6][64]);
127 uint8_t *bitstream_buffer; 139 uint8_t *bitstream_buffer;
128 unsigned int bitstream_buffer_size; 140 unsigned int bitstream_buffer_size;
141 int vlc_type;
129 CFrameBuffer cfrm[CFRAME_BUFFER_COUNT]; 142 CFrameBuffer cfrm[CFRAME_BUFFER_COUNT];
130 } FourXContext; 143 } FourXContext;
131 144
132 145
133 #define FIX_1_082392200 70936 146 #define FIX_1_082392200 70936
222 } 235 }
223 236
224 static void init_vlcs(FourXContext *f){ 237 static void init_vlcs(FourXContext *f){
225 int i; 238 int i;
226 239
227 for(i=0; i<4; i++){ 240 for(i=0; i<8; i++){
228 init_vlc(&block_type_vlc[i], BLOCK_TYPE_VLC_BITS, 7, 241 init_vlc(&block_type_vlc[0][i], BLOCK_TYPE_VLC_BITS, 7,
229 &block_type_tab[i][0][1], 2, 1, 242 &block_type_tab[0][i][0][1], 2, 1,
230 &block_type_tab[i][0][0], 2, 1, 1); 243 &block_type_tab[0][i][0][0], 2, 1, 1);
231 } 244 }
232 } 245 }
233 246
234 static void init_mv(FourXContext *f){ 247 static void init_mv(FourXContext *f){
235 int i; 248 int i;
281 } 294 }
282 295
283 static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, int log2w, int log2h, int stride){ 296 static void decode_p_block(FourXContext *f, uint16_t *dst, uint16_t *src, int log2w, int log2h, int stride){
284 const int index= size2index[log2h][log2w]; 297 const int index= size2index[log2h][log2w];
285 const int h= 1<<log2h; 298 const int h= 1<<log2h;
286 int code= get_vlc2(&f->gb, block_type_vlc[index].table, BLOCK_TYPE_VLC_BITS, 1); 299 int code= get_vlc2(&f->gb, block_type_vlc[f->vlc_type][index].table, BLOCK_TYPE_VLC_BITS, 1);
287 300
288 assert(code>=0 && code<=6); 301 assert(code>=0 && code<=6);
289 302
290 if(code == 0){ 303 if(code == 0){
291 src += f->mv[ *f->bytestream++ ]; 304 src += f->mv[ *f->bytestream++ ];
323 const int width= f->avctx->width; 336 const int width= f->avctx->width;
324 const int height= f->avctx->height; 337 const int height= f->avctx->height;
325 uint16_t *src= (uint16_t*)f->last_picture.data[0]; 338 uint16_t *src= (uint16_t*)f->last_picture.data[0];
326 uint16_t *dst= (uint16_t*)f->current_picture.data[0]; 339 uint16_t *dst= (uint16_t*)f->current_picture.data[0];
327 const int stride= f->current_picture.linesize[0]>>1; 340 const int stride= f->current_picture.linesize[0]>>1;
328 const unsigned int bitstream_size= get32(buf+8); 341 unsigned int bitstream_size, bytestream_size, wordstream_size, extra;
329 const unsigned int bytestream_size= get32(buf+16); 342
330 const unsigned int wordstream_size= get32(buf+12); 343 if(!get32(buf-4)){
331 344 extra=20;
332 if(bitstream_size+ bytestream_size+ wordstream_size + 20 != length 345 bitstream_size= get32(buf+8);
346 wordstream_size= get32(buf+12);
347 bytestream_size= get32(buf+16);
348 f->vlc_type= 0;
349 }else{
350 extra=0;
351 bitstream_size = AV_RL16(buf-4);
352 wordstream_size= AV_RL16(buf-2);
353 bytestream_size= FFMAX(length - bitstream_size - wordstream_size, 0);
354 f->vlc_type= 1;
355 }
356
357 if(bitstream_size+ bytestream_size+ wordstream_size + extra != length
333 || bitstream_size > (1<<26) 358 || bitstream_size > (1<<26)
334 || bytestream_size > (1<<26) 359 || bytestream_size > (1<<26)
335 || wordstream_size > (1<<26) 360 || wordstream_size > (1<<26)
336 ){ 361 ){
337 av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size, 362 av_log(f->avctx, AV_LOG_ERROR, "lengths %d %d %d %d\n", bitstream_size, bytestream_size, wordstream_size,
338 bitstream_size+ bytestream_size+ wordstream_size - length); 363 bitstream_size+ bytestream_size+ wordstream_size - length);
339 return -1; 364 return -1;
340 } 365 }
341 366
342 f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE); 367 f->bitstream_buffer= av_fast_realloc(f->bitstream_buffer, &f->bitstream_buffer_size, bitstream_size + FF_INPUT_BUFFER_PADDING_SIZE);
343 f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)(buf + 20), bitstream_size/4); 368 f->dsp.bswap_buf((uint32_t*)f->bitstream_buffer, (uint32_t*)(buf + extra), bitstream_size/4);
344 init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size); 369 init_get_bits(&f->gb, f->bitstream_buffer, 8*bitstream_size);
345 370
346 f->wordstream= (uint16_t*)(buf + 20 + bitstream_size); 371 f->wordstream= (uint16_t*)(buf + extra + bitstream_size);
347 f->bytestream= buf + 20 + bitstream_size + wordstream_size; 372 f->bytestream= buf + extra + bitstream_size + wordstream_size;
348 373
349 init_mv(f); 374 init_mv(f);
350 375
351 for(y=0; y<height; y+=8){ 376 for(y=0; y<height; y+=8){
352 for(x=0; x<width; x+=8){ 377 for(x=0; x<width; x+=8){
354 } 379 }
355 src += 8*stride; 380 src += 8*stride;
356 dst += 8*stride; 381 dst += 8*stride;
357 } 382 }
358 383
359 if(bitstream_size != (get_bits_count(&f->gb)+31)/32*4) 384 if( bitstream_size != (get_bits_count(&f->gb)+31)/32*4
385 || (((char*)f->wordstream - (char*)buf + 2)&~2) != extra + bitstream_size + wordstream_size
386 || (((char*)f->bytestream - (char*)buf + 3)&~3) != extra + bitstream_size + wordstream_size + bytestream_size)
360 av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n", 387 av_log(f->avctx, AV_LOG_ERROR, " %d %td %td bytes left\n",
361 bitstream_size - (get_bits_count(&f->gb)+31)/32*4, 388 bitstream_size - (get_bits_count(&f->gb)+31)/32*4,
362 bytestream_size - (f->bytestream - (buf + 20 + bitstream_size + wordstream_size)), 389 -(((char*)f->bytestream - (char*)buf + 3)&~3) + (extra + bitstream_size + wordstream_size + bytestream_size),
363 wordstream_size - (((uint8_t*)f->wordstream) - (buf + 20 + bitstream_size)) 390 -(((char*)f->wordstream - (char*)buf + 2)&~2) + (extra + bitstream_size + wordstream_size)
364 ); 391 );
365 392
366 return 0; 393 return 0;
367 } 394 }
368 395
673 if(avctx->get_buffer(avctx, p) < 0){ 700 if(avctx->get_buffer(avctx, p) < 0){
674 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n"); 701 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
675 return -1; 702 return -1;
676 } 703 }
677 704
678 if(frame_4cc == ff_get_fourcc("ifrm")){ 705 if(frame_4cc == ff_get_fourcc("ifrm") || frame_4cc == ff_get_fourcc("ifr2")){
679 p->pict_type= I_TYPE; 706 p->pict_type= I_TYPE;
680 if(decode_i_frame(f, buf, frame_size) < 0) 707 if(decode_i_frame(f, buf, frame_size) < 0)
681 return -1; 708 return -1;
682 }else if(frame_4cc == ff_get_fourcc("pfrm")){ 709 }else if(frame_4cc == ff_get_fourcc("pfrm") || frame_4cc == ff_get_fourcc("pfr2")){
683 p->pict_type= P_TYPE; 710 p->pict_type= P_TYPE;
684 if(decode_p_frame(f, buf, frame_size) < 0) 711 if(decode_p_frame(f, buf, frame_size) < 0)
685 return -1; 712 return -1;
686 }else if(frame_4cc == ff_get_fourcc("snd_")){ 713 }else if(frame_4cc == ff_get_fourcc("snd_")){
687 av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size); 714 av_log(avctx, AV_LOG_ERROR, "ignoring snd_ chunk length:%d\n", buf_size);