comparison vorbis_dec.c @ 10477:8cccee204e2e libavcodec

vorbis_dec: factor out duplicated code for index reading, bounds checking and error message into a shared macro.
author reimar
date Wed, 28 Oct 2009 22:41:45 +0000
parents 24a069f83049
children fe81255af588
comparison
equal deleted inserted replaced
10476:e37d143be97e 10477:8cccee204e2e
158 /* Helper functions */ 158 /* Helper functions */
159 159
160 #define BARK(x) \ 160 #define BARK(x) \
161 (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x)) 161 (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
162 162
163 static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n";
164 #define VALIDATE_INDEX(idx, limit) \
165 if (idx >= limit) {\
166 av_log(vc->avccontext, AV_LOG_ERROR,\
167 idx_err_str,\
168 (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
169 return -1;\
170 }
171 #define GET_VALIDATED_INDEX(idx, bits, limit) \
172 {\
173 idx = get_bits(gb, bits);\
174 VALIDATE_INDEX(idx, limit)\
175 }
176
163 static float vorbisfloat2float(uint_fast32_t val) 177 static float vorbisfloat2float(uint_fast32_t val)
164 { 178 {
165 double mant = val & 0x1fffff; 179 double mant = val & 0x1fffff;
166 long exp = (val & 0x7fe00000L) >> 21; 180 long exp = (val & 0x7fe00000L) >> 21;
167 if (val & 0x80000000) 181 if (val & 0x80000000)
488 floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2); 502 floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
489 503
490 AV_DEBUG(" %d floor %d class dim: %d subclasses %d \n", i, j, floor_setup->data.t1.class_dimensions[j], floor_setup->data.t1.class_subclasses[j]); 504 AV_DEBUG(" %d floor %d class dim: %d subclasses %d \n", i, j, floor_setup->data.t1.class_dimensions[j], floor_setup->data.t1.class_subclasses[j]);
491 505
492 if (floor_setup->data.t1.class_subclasses[j]) { 506 if (floor_setup->data.t1.class_subclasses[j]) {
493 int bits = get_bits(gb, 8); 507 GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
494 if (bits >= vc->codebook_count) {
495 av_log(vc->avccontext, AV_LOG_ERROR, "Masterbook index %d is out of range.\n", bits);
496 return -1;
497 }
498 floor_setup->data.t1.class_masterbook[j] = bits;
499 508
500 AV_DEBUG(" masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]); 509 AV_DEBUG(" masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
501 } 510 }
502 511
503 for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) { 512 for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
504 int16_t bits = get_bits(gb, 8) - 1; 513 int16_t bits = get_bits(gb, 8) - 1;
505 if (bits != -1 && bits >= vc->codebook_count) { 514 if (bits != -1)
506 av_log(vc->avccontext, AV_LOG_ERROR, "Subclass book index %d is out of range.\n", bits); 515 VALIDATE_INDEX(bits, vc->codebook_count)
507 return -1;
508 }
509 floor_setup->data.t1.subclass_books[j][k] = bits; 516 floor_setup->data.t1.subclass_books[j][k] = bits;
510 517
511 AV_DEBUG(" book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]); 518 AV_DEBUG(" book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
512 } 519 }
513 } 520 }
562 /* read book indexes */ 569 /* read book indexes */
563 { 570 {
564 int idx; 571 int idx;
565 uint_fast8_t book_idx; 572 uint_fast8_t book_idx;
566 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) { 573 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
567 book_idx = get_bits(gb, 8); 574 GET_VALIDATED_INDEX(floor_setup->data.t0.book_list[idx], 8, vc->codebook_count)
568 if (book_idx >= vc->codebook_count)
569 return -1;
570 floor_setup->data.t0.book_list[idx] = book_idx;
571 if (vc->codebooks[book_idx].dimensions > max_codebook_dim) 575 if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
572 max_codebook_dim = vc->codebooks[book_idx].dimensions; 576 max_codebook_dim = vc->codebooks[book_idx].dimensions;
573 } 577 }
574 } 578 }
575 579
648 av_log(vc->avccontext, AV_LOG_ERROR, "partition out of bounds: type, begin, end, size, blocksize: %"PRIdFAST16", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32"\n", res_setup->type, res_setup->begin, res_setup->end, res_setup->partition_size, vc->blocksize[1] / 2); 652 av_log(vc->avccontext, AV_LOG_ERROR, "partition out of bounds: type, begin, end, size, blocksize: %"PRIdFAST16", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32", %"PRIdFAST32"\n", res_setup->type, res_setup->begin, res_setup->end, res_setup->partition_size, vc->blocksize[1] / 2);
649 return -1; 653 return -1;
650 } 654 }
651 655
652 res_setup->classifications = get_bits(gb, 6) + 1; 656 res_setup->classifications = get_bits(gb, 6) + 1;
653 res_setup->classbook = get_bits(gb, 8); 657 GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
654 if (res_setup->classbook >= vc->codebook_count) {
655 av_log(vc->avccontext, AV_LOG_ERROR, "classbook value %d out of range. \n", res_setup->classbook);
656 return -1;
657 }
658 658
659 AV_DEBUG(" begin %d end %d part.size %d classif.s %d classbook %d \n", res_setup->begin, res_setup->end, res_setup->partition_size, 659 AV_DEBUG(" begin %d end %d part.size %d classif.s %d classbook %d \n", res_setup->begin, res_setup->end, res_setup->partition_size,
660 res_setup->classifications, res_setup->classbook); 660 res_setup->classifications, res_setup->classbook);
661 661
662 for (j = 0; j < res_setup->classifications; ++j) { 662 for (j = 0; j < res_setup->classifications; ++j) {
671 671
672 res_setup->maxpass = 0; 672 res_setup->maxpass = 0;
673 for (j = 0; j < res_setup->classifications; ++j) { 673 for (j = 0; j < res_setup->classifications; ++j) {
674 for (k = 0; k < 8; ++k) { 674 for (k = 0; k < 8; ++k) {
675 if (cascade[j]&(1 << k)) { 675 if (cascade[j]&(1 << k)) {
676 int bits = get_bits(gb, 8); 676 GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
677 if (bits >= vc->codebook_count) {
678 av_log(vc->avccontext, AV_LOG_ERROR, "book value %d out of range. \n", bits);
679 return -1;
680 }
681 res_setup->books[j][k] = bits;
682 677
683 AV_DEBUG(" %d class casscade depth %d book: %d \n", j, k, res_setup->books[j][k]); 678 AV_DEBUG(" %d class casscade depth %d book: %d \n", j, k, res_setup->books[j][k]);
684 679
685 if (k>res_setup->maxpass) 680 if (k>res_setup->maxpass)
686 res_setup->maxpass = k; 681 res_setup->maxpass = k;
721 if (get_bits1(gb)) { 716 if (get_bits1(gb)) {
722 mapping_setup->coupling_steps = get_bits(gb, 8) + 1; 717 mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
723 mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t)); 718 mapping_setup->magnitude = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
724 mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t)); 719 mapping_setup->angle = av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
725 for (j = 0; j < mapping_setup->coupling_steps; ++j) { 720 for (j = 0; j < mapping_setup->coupling_steps; ++j) {
726 mapping_setup->magnitude[j] = get_bits(gb, ilog(vc->audio_channels - 1)); 721 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
727 mapping_setup->angle[j] = get_bits(gb, ilog(vc->audio_channels - 1)); 722 GET_VALIDATED_INDEX(mapping_setup->angle[j], ilog(vc->audio_channels - 1), vc->audio_channels)
728 if (mapping_setup->magnitude[j] >= vc->audio_channels) {
729 av_log(vc->avccontext, AV_LOG_ERROR, "magnitude channel %d out of range. \n", mapping_setup->magnitude[j]);
730 return -1;
731 }
732 if (mapping_setup->angle[j] >= vc->audio_channels) {
733 av_log(vc->avccontext, AV_LOG_ERROR, "angle channel %d out of range. \n", mapping_setup->angle[j]);
734 return -1;
735 }
736 } 723 }
737 } else { 724 } else {
738 mapping_setup->coupling_steps = 0; 725 mapping_setup->coupling_steps = 0;
739 } 726 }
740 727
750 for (j = 0; j < vc->audio_channels; ++j) 737 for (j = 0; j < vc->audio_channels; ++j)
751 mapping_setup->mux[j] = get_bits(gb, 4); 738 mapping_setup->mux[j] = get_bits(gb, 4);
752 } 739 }
753 740
754 for (j = 0; j < mapping_setup->submaps; ++j) { 741 for (j = 0; j < mapping_setup->submaps; ++j) {
755 int bits;
756 skip_bits(gb, 8); // FIXME check? 742 skip_bits(gb, 8); // FIXME check?
757 bits = get_bits(gb, 8); 743 GET_VALIDATED_INDEX(mapping_setup->submap_floor[j], 8, vc->floor_count)
758 if (bits >= vc->floor_count) { 744 GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
759 av_log(vc->avccontext, AV_LOG_ERROR, "submap floor value %d out of range. \n", bits);
760 return -1;
761 }
762 mapping_setup->submap_floor[j] = bits;
763 bits = get_bits(gb, 8);
764 if (bits >= vc->residue_count) {
765 av_log(vc->avccontext, AV_LOG_ERROR, "submap residue value %d out of range. \n", bits);
766 return -1;
767 }
768 mapping_setup->submap_residue[j] = bits;
769 745
770 AV_DEBUG(" %d mapping %d submap : floor %d, residue %d \n", i, j, mapping_setup->submap_floor[j], mapping_setup->submap_residue[j]); 746 AV_DEBUG(" %d mapping %d submap : floor %d, residue %d \n", i, j, mapping_setup->submap_floor[j], mapping_setup->submap_residue[j]);
771 } 747 }
772 } 748 }
773 return 0; 749 return 0;
825 vorbis_mode *mode_setup = &vc->modes[i]; 801 vorbis_mode *mode_setup = &vc->modes[i];
826 802
827 mode_setup->blockflag = get_bits1(gb); 803 mode_setup->blockflag = get_bits1(gb);
828 mode_setup->windowtype = get_bits(gb, 16); //FIXME check 804 mode_setup->windowtype = get_bits(gb, 16); //FIXME check
829 mode_setup->transformtype = get_bits(gb, 16); //FIXME check 805 mode_setup->transformtype = get_bits(gb, 16); //FIXME check
830 mode_setup->mapping = get_bits(gb, 8); 806 GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
831 if (mode_setup->mapping >= vc->mapping_count) {
832 av_log(vc->avccontext, AV_LOG_ERROR, "mode mapping value %d out of range. \n", mode_setup->mapping);
833 return -1;
834 }
835 807
836 AV_DEBUG(" %d mode: blockflag %d, windowtype %d, transformtype %d, mapping %d \n", i, mode_setup->blockflag, mode_setup->windowtype, mode_setup->transformtype, mode_setup->mapping); 808 AV_DEBUG(" %d mode: blockflag %d, windowtype %d, transformtype %d, mapping %d \n", i, mode_setup->blockflag, mode_setup->windowtype, mode_setup->transformtype, mode_setup->mapping);
837 } 809 }
838 return 0; 810 return 0;
839 } 811 }
1496 } 1468 }
1497 1469
1498 if (vc->mode_count == 1) { 1470 if (vc->mode_count == 1) {
1499 mode_number = 0; 1471 mode_number = 0;
1500 } else { 1472 } else {
1501 mode_number = get_bits(gb, ilog(vc->mode_count-1)); 1473 GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
1502 }
1503 if (mode_number >= vc->mode_count) {
1504 av_log(vc->avccontext, AV_LOG_ERROR, "mode number %d out of range.\n", mode_number);
1505 return -1;
1506 } 1474 }
1507 vc->mode_number = mode_number; 1475 vc->mode_number = mode_number;
1508 mapping = &vc->mappings[vc->modes[mode_number].mapping]; 1476 mapping = &vc->mappings[vc->modes[mode_number].mapping];
1509 1477
1510 AV_DEBUG(" Mode number: %d , mapping: %d , blocktype %d \n", mode_number, vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag); 1478 AV_DEBUG(" Mode number: %d , mapping: %d , blocktype %d \n", mode_number, vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);