- //FIXME no exits should be left (underspecified tree - ie. unused valid vlcs - not allowed by SPEC)
-
- return 0;
-}
-
-// Free all allocated memory -----------------------------------------
-
-static void vorbis_free(vorbis_context *vc) {
- int_fast16_t i;
-
- av_freep(&vc->channel_residues);
- av_freep(&vc->channel_floors);
- av_freep(&vc->saved);
- av_freep(&vc->ret);
-
- av_freep(&vc->residues);
- av_freep(&vc->modes);
-
- ff_mdct_end(&vc->mdct0);
- ff_mdct_end(&vc->mdct1);
-
- for(i=0;i<vc->codebook_count;++i) {
- av_free(vc->codebooks[i].codevectors);
- free_vlc(&vc->codebooks[i].vlc);
- }
- av_freep(&vc->codebooks);
-
- for(i=0;i<vc->floor_count;++i) {
- av_free(vc->floors[i].x_list);
- av_free(vc->floors[i].x_list_order);
- av_free(vc->floors[i].low_neighbour);
- av_free(vc->floors[i].high_neighbour);
- }
- av_freep(&vc->floors);
-
- for(i=0;i<vc->mapping_count;++i) {
- av_free(vc->mappings[i].magnitude);
- av_free(vc->mappings[i].angle);
- av_free(vc->mappings[i].mux);
- }
- av_freep(&vc->mappings);
-}
-
-// Parse setup header -------------------------------------------------
-
-// Process codebooks part
-
-static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc) {
- uint_fast16_t cb;
- GetBitContext *gb=&vc->gb;
-
- vc->codebook_count=get_bits(gb,8)+1;
-
- AV_DEBUG(" Codebooks: %d \n", vc->codebook_count);
-
- vc->codebooks=(vorbis_codebook *)av_mallocz(vc->codebook_count * sizeof(vorbis_codebook));
-
- for(cb=0;cb<vc->codebook_count;++cb) {
- vorbis_codebook *codebook_setup=&vc->codebooks[cb];
- uint_fast8_t ordered;
- uint_fast32_t t, used_entries=0;
- uint_fast32_t entries;
- uint_fast8_t tmp_vlc_bits[V_MAX_VLCS];
- uint_fast32_t tmp_vlc_codes[V_MAX_VLCS];
-
-// memset(tmp_vlc_bits, 0, sizeof(tmp_vlc_bits));
-
- AV_DEBUG(" %d. Codebook \n", cb);
-
- if (get_bits(gb, 24)!=0x564342) {
- av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook setup data corrupt. \n", cb);
- return 1;
- }
-
- codebook_setup->dimensions=get_bits(gb, 16);
- if (codebook_setup->dimensions>16) {
- av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook's dimension is too large (%d). \n", cb, codebook_setup->dimensions);
- return 1;
- }
- entries=get_bits(gb, 24);
- if (entries>V_MAX_VLCS) {
- av_log(vc->avccontext, AV_LOG_ERROR, " %d. Codebook has too many entries (%d). \n", cb, entries);
- return 1;
- }
-
- ordered=get_bits1(gb);
-
- AV_DEBUG(" codebook_dimensions %d, codebook_entries %d \n", codebook_setup->dimensions, entries);
-
- if (!ordered) {
- uint_fast16_t ce;
- uint_fast8_t flag;
- uint_fast8_t sparse=get_bits1(gb);
-
- AV_DEBUG(" not ordered \n");
-
- if (sparse) {
- AV_DEBUG(" sparse \n");
-
- used_entries=0;
- for(ce=0;ce<entries;++ce) {
- flag=get_bits1(gb);
- if (flag) {
- tmp_vlc_bits[ce]=get_bits(gb, 5)+1;
- ++used_entries;
- }
- else tmp_vlc_bits[ce]=0;
- }
- } else {
- AV_DEBUG(" not sparse \n");
-
- used_entries=entries;
- for(ce=0;ce<entries;++ce) {
- tmp_vlc_bits[ce]=get_bits(gb, 5)+1;
- }
- }
- } else {
- uint_fast16_t current_entry=0;
- uint_fast8_t current_length=get_bits(gb, 5)+1;
-
- AV_DEBUG(" ordered, current length: %d \n", current_length); //FIXME
-
- used_entries=entries;
- for(;current_entry<used_entries;++current_length) {
- uint_fast16_t i, number;
-
- AV_DEBUG(" number bits: %d ", ilog(entries - current_entry));
-
- number=get_bits(gb, ilog(entries - current_entry));
-
- AV_DEBUG(" number: %d \n", number);
-
- for(i=current_entry;i<number+current_entry;++i) {
- if (i<used_entries) tmp_vlc_bits[i]=current_length;
- }
-
- current_entry+=number;
- }
- if (current_entry>used_entries) {
- av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
- return 1;
- }
- }
-
- codebook_setup->lookup_type=get_bits(gb, 4);
-
- AV_DEBUG(" lookup type: %d : %s \n", codebook_setup->lookup_type, codebook_setup->lookup_type ? "vq" : "no lookup" );
-
-// If the codebook is used for (inverse) VQ, calculate codevectors.
-
- if (codebook_setup->lookup_type==1) {
- uint_fast16_t i, j, k;
- uint_fast16_t codebook_lookup_values=nth_root(entries, codebook_setup->dimensions);
- uint_fast16_t codebook_multiplicands[codebook_lookup_values];
-
- float codebook_minimum_value=vorbisfloat2float(get_bits_long_le(gb, 32));
- float codebook_delta_value=vorbisfloat2float(get_bits_long_le(gb, 32));
- uint_fast8_t codebook_value_bits=get_bits(gb, 4)+1;
- uint_fast8_t codebook_sequence_p=get_bits1(gb);
-
- AV_DEBUG(" We expect %d numbers for building the codevectors. \n", codebook_lookup_values);
- AV_DEBUG(" delta %f minmum %f \n", codebook_delta_value, codebook_minimum_value);
-
- for(i=0;i<codebook_lookup_values;++i) {
- codebook_multiplicands[i]=get_bits(gb, codebook_value_bits);
-
- AV_DEBUG(" multiplicands*delta+minmum : %e \n", (float)codebook_multiplicands[i]*codebook_delta_value+codebook_minimum_value);
- AV_DEBUG(" multiplicand %d \n", codebook_multiplicands[i]);
- }
-
-// Weed out unused vlcs and build codevector vector
- codebook_setup->codevectors=(float *)av_mallocz(used_entries*codebook_setup->dimensions * sizeof(float));
- for(j=0, i=0;i<entries;++i) {
- uint_fast8_t dim=codebook_setup->dimensions;
-
- if (tmp_vlc_bits[i]) {
- float last=0.0;
- uint_fast32_t lookup_offset=i;
-
-#ifdef V_DEBUG
- av_log(vc->avccontext, AV_LOG_INFO, "Lookup offset %d ,", i);
-#endif
-
- for(k=0;k<dim;++k) {
- uint_fast32_t multiplicand_offset = lookup_offset % codebook_lookup_values;
- codebook_setup->codevectors[j*dim+k]=codebook_multiplicands[multiplicand_offset]*codebook_delta_value+codebook_minimum_value+last;
- if (codebook_sequence_p) {
- last=codebook_setup->codevectors[j*dim+k];
- }
- lookup_offset/=codebook_lookup_values;
- }
- tmp_vlc_bits[j]=tmp_vlc_bits[i];
-
-#ifdef V_DEBUG
- av_log(vc->avccontext, AV_LOG_INFO, "real lookup offset %d, vector: ", j);
- for(k=0;k<dim;++k) {
- av_log(vc->avccontext, AV_LOG_INFO, " %f ", codebook_setup->codevectors[j*dim+k]);
- }
- av_log(vc->avccontext, AV_LOG_INFO, "\n");
-#endif
-
- ++j;
- }
- }
- if (j!=used_entries) {
- av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
- return 1;
- }
- entries=used_entries;
- }
- else if (codebook_setup->lookup_type>=2) {
- av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
- return 1;
- }
-
-// Initialize VLC table
- if (vorbis_len2vlc(vc, tmp_vlc_bits, tmp_vlc_codes, entries)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
- return 1;
- }
- codebook_setup->maxdepth=0;
- for(t=0;t<entries;++t)
- if (tmp_vlc_bits[t]>=codebook_setup->maxdepth) codebook_setup->maxdepth=tmp_vlc_bits[t];
-
- codebook_setup->maxdepth=(codebook_setup->maxdepth+V_NB_BITS-1)/V_NB_BITS;
-
- if (init_vlc(&codebook_setup->vlc, V_NB_BITS, entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits), sizeof(*tmp_vlc_bits), tmp_vlc_codes, sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes), INIT_VLC_LE)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n");
- return 1;
- }
- }
- return 0;
-}
-
-// Process time domain transforms part (unused in Vorbis I)
-
-static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc) {
- GetBitContext *gb=&vc->gb;
- uint_fast8_t i;
- uint_fast8_t vorbis_time_count=get_bits(gb, 6)+1;
-
- for(i=0;i<vorbis_time_count;++i) {
- uint_fast16_t vorbis_tdtransform=get_bits(gb, 16);
-
- AV_DEBUG(" Vorbis time domain transform %d: %d \n", vorbis_time_count, vorbis_tdtransform);
-
- if (vorbis_tdtransform) {
- av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
- return 1;
- }
- }
- return 0;
-}
-
-// Process floors part - only floor type 1 is supported
-
-static int vorbis_parse_setup_hdr_floors(vorbis_context *vc) {
- GetBitContext *gb=&vc->gb;
- uint_fast16_t i,j,k;
-
- vc->floor_count=get_bits(gb, 6)+1;
-
- vc->floors=(vorbis_floor *)av_mallocz(vc->floor_count * sizeof(vorbis_floor));
-
- for (i=0;i<vc->floor_count;++i) {
- vorbis_floor *floor_setup=&vc->floors[i];
-
- floor_setup->floor_type=get_bits(gb, 16);
-
- AV_DEBUG(" %d. floor type %d \n", i, floor_setup->floor_type);
-
- if (floor_setup->floor_type==1) {
- uint_fast8_t maximum_class=0;
- uint_fast8_t rangebits;
- uint_fast16_t floor1_values=2;
-
- floor_setup->partitions=get_bits(gb, 5);
-
- AV_DEBUG(" %d.floor: %d partitions \n", i, floor_setup->partitions);
-
- for(j=0;j<floor_setup->partitions;++j) {
- floor_setup->partition_class[j]=get_bits(gb, 4);
- if (floor_setup->partition_class[j]>maximum_class) maximum_class=floor_setup->partition_class[j];
-
- AV_DEBUG(" %d. floor %d partition class %d \n", i, j, floor_setup->partition_class[j]);
-
- }
-
- AV_DEBUG(" maximum class %d \n", maximum_class);
-
- floor_setup->maximum_class=maximum_class;
-
- for(j=0;j<=maximum_class;++j) {
- floor_setup->class_dimensions[j]=get_bits(gb, 3)+1;
- floor_setup->class_subclasses[j]=get_bits(gb, 2);
-
- AV_DEBUG(" %d floor %d class dim: %d subclasses %d \n", i, j, floor_setup->class_dimensions[j], floor_setup->class_subclasses[j]);
-
- if (floor_setup->class_subclasses[j]) {
- floor_setup->class_masterbook[j]=get_bits(gb, 8);
-
- AV_DEBUG(" masterbook: %d \n", floor_setup->class_masterbook[j]);
- }
-
- for(k=0;k<(1<<floor_setup->class_subclasses[j]);++k) {
- floor_setup->subclass_books[j][k]=get_bits(gb, 8)-1;
-
- AV_DEBUG(" book %d. : %d \n", k, floor_setup->subclass_books[j][k]);
- }
- }
-
- floor_setup->multiplier=get_bits(gb, 2)+1;
- floor_setup->x_list_dim=2;
-
- for(j=0;j<floor_setup->partitions;++j) {
- floor_setup->x_list_dim+=floor_setup->class_dimensions[floor_setup->partition_class[j]];
- }
-
- floor_setup->x_list=(uint_fast16_t *)av_mallocz(floor_setup->x_list_dim * sizeof(uint_fast16_t));
- floor_setup->x_list_order=(uint_fast16_t *)av_mallocz(floor_setup->x_list_dim * sizeof(uint_fast16_t));
- floor_setup->low_neighbour=(uint_fast16_t *)av_mallocz(floor_setup->x_list_dim * sizeof(uint_fast16_t));
- floor_setup->high_neighbour=(uint_fast16_t *)av_mallocz(floor_setup->x_list_dim * sizeof(uint_fast16_t));
-
-
- rangebits=get_bits(gb, 4);
- floor_setup->x_list[0] = 0;
- floor_setup->x_list[1] = (1<<rangebits);
-
- for(j=0;j<floor_setup->partitions;++j) {
- for(k=0;k<floor_setup->class_dimensions[floor_setup->partition_class[j]];++k,++floor1_values) {
- floor_setup->x_list[floor1_values]=get_bits(gb, rangebits);
-
- AV_DEBUG(" %d. floor1 Y coord. %d \n", floor1_values, floor_setup->x_list[floor1_values]);
- }
- }
-
-// Precalculate order of x coordinates - needed for decode
-
- for(k=0;k<floor_setup->x_list_dim;++k) {
- floor_setup->x_list_order[k]=k;
- }
-
- for(k=0;k<floor_setup->x_list_dim-1;++k) { // FIXME optimize sorting ?
- for(j=k+1;j<floor_setup->x_list_dim;++j) {
- if(floor_setup->x_list[floor_setup->x_list_order[k]]>floor_setup->x_list[floor_setup->x_list_order[j]]) {
- uint_fast16_t tmp=floor_setup->x_list_order[k];
- floor_setup->x_list_order[k]=floor_setup->x_list_order[j];
- floor_setup->x_list_order[j]=tmp;
- }
- }
- }
-
-// Precalculate low and high neighbours
-
- for(k=2;k<floor_setup->x_list_dim;++k) {
- floor_setup->low_neighbour[k]=0;
- floor_setup->high_neighbour[k]=1; // correct according to SPEC requirements
-
- for (j=0;j<k;++j) {
- if ((floor_setup->x_list[j]<floor_setup->x_list[k]) &&
- (floor_setup->x_list[j]>floor_setup->x_list[floor_setup->low_neighbour[k]])) {
- floor_setup->low_neighbour[k]=j;
- }
- if ((floor_setup->x_list[j]>floor_setup->x_list[k]) &&
- (floor_setup->x_list[j]<floor_setup->x_list[floor_setup->high_neighbour[k]])) {
- floor_setup->high_neighbour[k]=j;
- }
- }
- }
- }
- else {
- av_log(vc->avccontext, AV_LOG_ERROR, "Only floor type 1 supported. \n");
- return 1;
- }
- }
- return 0;
-}
-
-// Process residues part
-
-static int vorbis_parse_setup_hdr_residues(vorbis_context *vc){
- GetBitContext *gb=&vc->gb;
- uint_fast8_t i, j, k;
-
- vc->residue_count=get_bits(gb, 6)+1;
- vc->residues=(vorbis_residue *)av_mallocz(vc->residue_count * sizeof(vorbis_residue));
-
- AV_DEBUG(" There are %d residues. \n", vc->residue_count);
-
- for(i=0;i<vc->residue_count;++i) {
- vorbis_residue *res_setup=&vc->residues[i];
- uint_fast8_t cascade[64];
- uint_fast8_t high_bits;
- uint_fast8_t low_bits;
-
- res_setup->type=get_bits(gb, 16);
-
- AV_DEBUG(" %d. residue type %d \n", i, res_setup->type);
-
- res_setup->begin=get_bits(gb, 24);
- res_setup->end=get_bits(gb, 24);
- res_setup->partition_size=get_bits(gb, 24)+1;
- res_setup->classifications=get_bits(gb, 6)+1;
- res_setup->classbook=get_bits(gb, 8);
-
- 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,
- res_setup->classifications, res_setup->classbook);
-
- for(j=0;j<res_setup->classifications;++j) {
- high_bits=0;
- low_bits=get_bits(gb, 3);
- if (get_bits1(gb)) {
- high_bits=get_bits(gb, 5);
- }
- cascade[j]=(high_bits<<3)+low_bits;
-
- AV_DEBUG(" %d class casscade depth: %d \n", j, ilog(cascade[j]));
- }
-
- res_setup->maxpass=0;
- for(j=0;j<res_setup->classifications;++j) {
- for(k=0;k<8;++k) {
- if (cascade[j]&(1<<k)) {
- res_setup->books[j][k]=get_bits(gb, 8);
-
- AV_DEBUG(" %d class casscade depth %d book: %d \n", j, k, res_setup->books[j][k]);
-
- if (k>res_setup->maxpass) {
- res_setup->maxpass=k;
- }
- } else {
- res_setup->books[j][k]=-1;
- }
- }
- }
- }
- return 0;
-}
-
-// Process mappings part
-
-static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc) {
- GetBitContext *gb=&vc->gb;
- uint_fast8_t i, j;
-
- vc->mapping_count=get_bits(gb, 6)+1;
- vc->mappings=(vorbis_mapping *)av_mallocz(vc->mapping_count * sizeof(vorbis_mapping));
-
- AV_DEBUG(" There are %d mappings. \n", vc->mapping_count);
-
- for(i=0;i<vc->mapping_count;++i) {
- vorbis_mapping *mapping_setup=&vc->mappings[i];
-
- if (get_bits(gb, 16)) {
- av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
- return 1;
- }
- if (get_bits1(gb)) {
- mapping_setup->submaps=get_bits(gb, 4)+1;
- } else {
- mapping_setup->submaps=1;
- }
-
- if (get_bits1(gb)) {
- mapping_setup->coupling_steps=get_bits(gb, 8)+1;
- mapping_setup->magnitude=(uint_fast8_t *)av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
- mapping_setup->angle=(uint_fast8_t *)av_mallocz(mapping_setup->coupling_steps * sizeof(uint_fast8_t));
- for(j=0;j<mapping_setup->coupling_steps;++j) {
- mapping_setup->magnitude[j]=get_bits(gb, ilog(vc->audio_channels-1));
- mapping_setup->angle[j]=get_bits(gb, ilog(vc->audio_channels-1));
- // FIXME: sanity checks
- }
- } else {
- mapping_setup->coupling_steps=0;
- }
-
- AV_DEBUG(" %d mapping coupling steps: %d \n", i, mapping_setup->coupling_steps);
-
- if(get_bits(gb, 2)) {
- av_log(vc->avccontext, AV_LOG_ERROR, "%d. mapping setup data invalid. \n", i);
- return 1; // following spec.
- }
-
- if (mapping_setup->submaps>1) {
- mapping_setup->mux=(uint_fast8_t *)av_mallocz(vc->audio_channels * sizeof(uint_fast8_t));
- for(j=0;j<vc->audio_channels;++j) {
- mapping_setup->mux[j]=get_bits(gb, 4);
- }
- }
-
- for(j=0;j<mapping_setup->submaps;++j) {
- get_bits(gb, 8); // FIXME check?
- mapping_setup->submap_floor[j]=get_bits(gb, 8);
- mapping_setup->submap_residue[j]=get_bits(gb, 8);
-
- AV_DEBUG(" %d mapping %d submap : floor %d, residue %d \n", i, j, mapping_setup->submap_floor[j], mapping_setup->submap_residue[j]);
- }
- }
- return 0;
-}
-
-// Process modes part
-
-static int vorbis_parse_setup_hdr_modes(vorbis_context *vc) {
- GetBitContext *gb=&vc->gb;
- uint_fast8_t i;
-
- vc->mode_count=get_bits(gb, 6)+1;
- vc->modes=(vorbis_mode *)av_mallocz(vc->mode_count * sizeof(vorbis_mode));
-
- AV_DEBUG(" There are %d modes.\n", vc->mode_count);
-
- for(i=0;i<vc->mode_count;++i) {
- vorbis_mode *mode_setup=&vc->modes[i];
-
- mode_setup->blockflag=get_bits(gb, 1);
- mode_setup->windowtype=get_bits(gb, 16); //FIXME check
- mode_setup->transformtype=get_bits(gb, 16); //FIXME check
- mode_setup->mapping=get_bits(gb, 8); //FIXME check
-
- 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);
- }
- return 0;
-}
-
-// Process the whole setup header using the functions above
-
-static int vorbis_parse_setup_hdr(vorbis_context *vc) {
- GetBitContext *gb=&vc->gb;
-
- if ((get_bits(gb, 8)!='v') || (get_bits(gb, 8)!='o') ||
- (get_bits(gb, 8)!='r') || (get_bits(gb, 8)!='b') ||
- (get_bits(gb, 8)!='i') || (get_bits(gb, 8)!='s')) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
- return 1;
- }
-
- if (vorbis_parse_setup_hdr_codebooks(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
- return 2;
- }
- if (vorbis_parse_setup_hdr_tdtransforms(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
- return 3;
- }
- if (vorbis_parse_setup_hdr_floors(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
- return 4;
- }
- if (vorbis_parse_setup_hdr_residues(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
- return 5;
- }
- if (vorbis_parse_setup_hdr_mappings(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
- return 6;
- }
- if (vorbis_parse_setup_hdr_modes(vc)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
- return 7;
- }
- if (!get_bits1(gb)) {
- av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
- return 8; // framing flag bit unset error
- }