comparison flac.c @ 1816:fa7d0134f9f0 libavcodec

simplify only change context if frame header is error-free, as some fields may not be stored in the frame header and so a single damaged frame-header may trash them
author michael
date Wed, 18 Feb 2004 14:05:49 +0000
parents 4804dddf2d0e
children 93029a89c173
comparison
equal deleted inserted replaced
1815:2152760d08ad 1816:fa7d0134f9f0
66 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000, 66 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
67 0, 0, 0, 0 }; 67 0, 0, 0, 0 };
68 68
69 static int sample_size_table[] = 69 static int sample_size_table[] =
70 { 0, 8, 12, 0, 16, 20, 24, 0 }; 70 { 0, 8, 12, 0, 16, 20, 24, 0 };
71
72 static int blocksize_table[] = {
73 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
74 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
75 };
71 76
72 static const uint8_t table_crc8[256] = { 77 static const uint8_t table_crc8[256] = {
73 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 78 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
74 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d, 79 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
75 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 80 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
166 { 171 {
167 s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize); 172 s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
168 } 173 }
169 174
170 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize); 175 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
171 // s->bitstream= av_realloc(s->bitstream, s->max_framesize);
172 } 176 }
173 177
174 static void metadata_streaminfo(FLACContext *s) 178 static void metadata_streaminfo(FLACContext *s)
175 { 179 {
176 /* mandatory streaminfo */ 180 /* mandatory streaminfo */
225 else 229 else
226 { 230 {
227 // av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp); 231 // av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
228 for (; i < samples; i++, sample++){ 232 for (; i < samples; i++, sample++){
229 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0); 233 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
230 if(get_bits_count(&s->gb) > s->gb.size_in_bits){
231 av_log(s->avctx, AV_LOG_ERROR, "fucking FLAC\n");
232 return -1;
233 }
234 } 234 }
235 } 235 }
236 i= 0; 236 i= 0;
237 } 237 }
238 238
431 } 431 }
432 432
433 static int decode_frame(FLACContext *s) 433 static int decode_frame(FLACContext *s)
434 { 434 {
435 int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8; 435 int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
436 int decorrelation, bps, blocksize, samplerate;
436 437
437 blocksize_code = get_bits(&s->gb, 4); 438 blocksize_code = get_bits(&s->gb, 4);
438 439
439 sample_rate_code = get_bits(&s->gb, 4); 440 sample_rate_code = get_bits(&s->gb, 4);
440 441
441 assignment = get_bits(&s->gb, 4); /* channel assignment */ 442 assignment = get_bits(&s->gb, 4); /* channel assignment */
442 if (assignment < 8) 443 if (assignment < 8 && s->channels == assignment+1)
443 { 444 decorrelation = INDEPENDENT;
444 s->decorrelation = INDEPENDENT; 445 else if (assignment >=8 && assignment < 11 && s->channels == 2)
445 if (s->channels != assignment+1) 446 decorrelation = LEFT_SIDE + assignment - 8;
446 av_log(s->avctx, AV_LOG_DEBUG, "channel number and number of assigned channels differ!\n");
447 // av_log(s->avctx, AV_LOG_DEBUG, "channels: %d\n", assignment+1);
448 }
449 else if (assignment < 11)
450 {
451 s->decorrelation= LEFT_SIDE + assignment - 8;
452 }
453 else 447 else
454 { 448 {
455 av_log(s->avctx, AV_LOG_DEBUG, "unsupported channel assignment\n"); 449 av_log(s->avctx, AV_LOG_DEBUG, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
456 return -1;
457 }
458
459 if ((assignment >= 8) && (s->channels != 2))
460 {
461 return -1; 450 return -1;
462 } 451 }
463 452
464 sample_size_code = get_bits(&s->gb, 3); 453 sample_size_code = get_bits(&s->gb, 3);
465 if (sample_size_code != 0) 454 if(sample_size_code == 0)
466 s->bps = sample_size_table[sample_size_code]; 455 bps= s->bps;
467 456 else if((sample_size_code != 3) && (sample_size_code != 7))
468 if ((sample_size_code == 3) || (sample_size_code == 7)) 457 bps = sample_size_table[sample_size_code];
458 else
469 { 459 {
470 av_log(s->avctx, AV_LOG_DEBUG, "invalid sample size code (%d)\n", sample_size_code); 460 av_log(s->avctx, AV_LOG_DEBUG, "invalid sample size code (%d)\n", sample_size_code);
471 return -1; 461 return -1;
472 } 462 }
473 463
474 if (get_bits1(&s->gb)) 464 if (get_bits1(&s->gb))
475 { 465 {
476 av_log(s->avctx, AV_LOG_DEBUG, "broken stream, invalid padding\n"); 466 av_log(s->avctx, AV_LOG_DEBUG, "broken stream, invalid padding\n");
477 // return -1; 467 return -1;
478 } 468 }
479 469
470 if(get_utf8(&s->gb) < 0){
471 av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
472 return -1;
473 }
474 #if 0
480 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/ 475 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
481 (s->min_blocksize != s->max_blocksize)){ 476 (s->min_blocksize != s->max_blocksize)){
482 if(get_utf8(&s->gb) < 0){
483 av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
484 return -1;
485 }
486 }else{ 477 }else{
487 if(get_utf8(&s->gb) < 0){ 478 }
488 av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n"); 479 #endif
489 return -1; 480
490 }
491 }
492
493 if (blocksize_code == 0) 481 if (blocksize_code == 0)
494 s->blocksize = s->min_blocksize; 482 blocksize = s->min_blocksize;
495 else if (blocksize_code == 1)
496 s->blocksize = 192;
497 else if (blocksize_code <= 5)
498 s->blocksize = 576 << (blocksize_code - 2);
499 else if (blocksize_code == 6) 483 else if (blocksize_code == 6)
500 s->blocksize = get_bits(&s->gb, 8)+1; 484 blocksize = get_bits(&s->gb, 8)+1;
501 else if (blocksize_code == 7) 485 else if (blocksize_code == 7)
502 s->blocksize = get_bits(&s->gb, 16)+1; 486 blocksize = get_bits(&s->gb, 16)+1;
503 else if (blocksize_code >= 8) 487 else
504 s->blocksize = 256 << (blocksize_code - 8); 488 blocksize = blocksize_table[blocksize_code];
505 489
506 if(s->blocksize > s->max_blocksize){ 490 if(blocksize > s->max_blocksize){
507 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", s->blocksize, s->max_blocksize); 491 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
508 return -1; 492 return -1;
509 } 493 }
510 494
511 if (sample_rate_code == 0){ 495 if (sample_rate_code == 0){
512 //Streaminfo 496 samplerate= s->samplerate;
513 }else if ((sample_rate_code > 3) && (sample_rate_code < 12)) 497 }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
514 s->samplerate = sample_rate_table[sample_rate_code]; 498 samplerate = sample_rate_table[sample_rate_code];
515 else if (sample_rate_code == 12) 499 else if (sample_rate_code == 12)
516 s->samplerate = get_bits(&s->gb, 8) * 1000; 500 samplerate = get_bits(&s->gb, 8) * 1000;
517 else if (sample_rate_code == 13) 501 else if (sample_rate_code == 13)
518 s->samplerate = get_bits(&s->gb, 16); 502 samplerate = get_bits(&s->gb, 16);
519 else if (sample_rate_code == 14) 503 else if (sample_rate_code == 14)
520 s->samplerate = get_bits(&s->gb, 16) * 10; 504 samplerate = get_bits(&s->gb, 16) * 10;
521 else{ 505 else{
522 av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code); 506 av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
523 return -1; 507 return -1;
524 } 508 }
525 509
527 crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8); 511 crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8);
528 if(crc8){ 512 if(crc8){
529 av_log(s->avctx, AV_LOG_ERROR, "header crc missmatch crc=%2X\n", crc8); 513 av_log(s->avctx, AV_LOG_ERROR, "header crc missmatch crc=%2X\n", crc8);
530 return -1; 514 return -1;
531 } 515 }
516
517 s->blocksize = blocksize;
518 s->samplerate = samplerate;
519 s->bps = bps;
520 s->decorrelation= decorrelation;
532 521
533 // dump_headers(s); 522 // dump_headers(s);
534 523
535 /* subframes */ 524 /* subframes */
536 for (i = 0; i < s->channels; i++) 525 for (i = 0; i < s->channels; i++)