libavcodec/vorbisdec.c
Go to the documentation of this file.
00001 /*
00002  * This file is part of Libav.
00003  *
00004  * Libav is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * Libav is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with Libav; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00017  */
00018 
00025 #include <inttypes.h>
00026 #include <math.h>
00027 
00028 #define BITSTREAM_READER_LE
00029 #include "avcodec.h"
00030 #include "get_bits.h"
00031 #include "dsputil.h"
00032 #include "fft.h"
00033 #include "fmtconvert.h"
00034 
00035 #include "vorbis.h"
00036 #include "xiph.h"
00037 
00038 #define V_NB_BITS 8
00039 #define V_NB_BITS2 11
00040 #define V_MAX_VLCS (1 << 16)
00041 #define V_MAX_PARTITIONS (1 << 20)
00042 
00043 #undef NDEBUG
00044 #include <assert.h>
00045 
00046 typedef struct {
00047     uint8_t      dimensions;
00048     uint8_t      lookup_type;
00049     uint8_t      maxdepth;
00050     VLC          vlc;
00051     float       *codevectors;
00052     unsigned int nb_bits;
00053 } vorbis_codebook;
00054 
00055 typedef union  vorbis_floor_u  vorbis_floor_data;
00056 typedef struct vorbis_floor0_s vorbis_floor0;
00057 typedef struct vorbis_floor1_s vorbis_floor1;
00058 struct vorbis_context_s;
00059 typedef
00060 int (* vorbis_floor_decode_func)
00061     (struct vorbis_context_s *, vorbis_floor_data *, float *);
00062 typedef struct {
00063     uint8_t floor_type;
00064     vorbis_floor_decode_func decode;
00065     union vorbis_floor_u {
00066         struct vorbis_floor0_s {
00067             uint8_t       order;
00068             uint16_t      rate;
00069             uint16_t      bark_map_size;
00070             int32_t      *map[2];
00071             uint32_t      map_size[2];
00072             uint8_t       amplitude_bits;
00073             uint8_t       amplitude_offset;
00074             uint8_t       num_books;
00075             uint8_t      *book_list;
00076             float        *lsp;
00077         } t0;
00078         struct vorbis_floor1_s {
00079             uint8_t       partitions;
00080             uint8_t       partition_class[32];
00081             uint8_t       class_dimensions[16];
00082             uint8_t       class_subclasses[16];
00083             uint8_t       class_masterbook[16];
00084             int16_t       subclass_books[16][8];
00085             uint8_t       multiplier;
00086             uint16_t      x_list_dim;
00087             vorbis_floor1_entry *list;
00088         } t1;
00089     } data;
00090 } vorbis_floor;
00091 
00092 typedef struct {
00093     uint16_t      type;
00094     uint32_t      begin;
00095     uint32_t      end;
00096     unsigned      partition_size;
00097     uint8_t       classifications;
00098     uint8_t       classbook;
00099     int16_t       books[64][8];
00100     uint8_t       maxpass;
00101     uint16_t      ptns_to_read;
00102     uint8_t      *classifs;
00103 } vorbis_residue;
00104 
00105 typedef struct {
00106     uint8_t       submaps;
00107     uint16_t      coupling_steps;
00108     uint8_t      *magnitude;
00109     uint8_t      *angle;
00110     uint8_t      *mux;
00111     uint8_t       submap_floor[16];
00112     uint8_t       submap_residue[16];
00113 } vorbis_mapping;
00114 
00115 typedef struct {
00116     uint8_t       blockflag;
00117     uint16_t      windowtype;
00118     uint16_t      transformtype;
00119     uint8_t       mapping;
00120 } vorbis_mode;
00121 
00122 typedef struct vorbis_context_s {
00123     AVCodecContext *avccontext;
00124     AVFrame frame;
00125     GetBitContext gb;
00126     DSPContext dsp;
00127     FmtConvertContext fmt_conv;
00128 
00129     FFTContext mdct[2];
00130     uint8_t       first_frame;
00131     uint32_t      version;
00132     uint8_t       audio_channels;
00133     uint32_t      audio_samplerate;
00134     uint32_t      bitrate_maximum;
00135     uint32_t      bitrate_nominal;
00136     uint32_t      bitrate_minimum;
00137     uint32_t      blocksize[2];
00138     const float  *win[2];
00139     uint16_t      codebook_count;
00140     vorbis_codebook *codebooks;
00141     uint8_t       floor_count;
00142     vorbis_floor *floors;
00143     uint8_t       residue_count;
00144     vorbis_residue *residues;
00145     uint8_t       mapping_count;
00146     vorbis_mapping *mappings;
00147     uint8_t       mode_count;
00148     vorbis_mode  *modes;
00149     uint8_t       mode_number; // mode number for the current packet
00150     uint8_t       previous_window;
00151     float        *channel_residues;
00152     float        *channel_floors;
00153     float        *saved;
00154     float         scale_bias; // for float->int conversion
00155 } vorbis_context;
00156 
00157 /* Helper functions */
00158 
00159 #define BARK(x) \
00160     (13.1f * atan(0.00074f * (x)) + 2.24f * atan(1.85e-8f * (x) * (x)) + 1e-4f * (x))
00161 
00162 static const char idx_err_str[] = "Index value %d out of range (0 - %d) for %s at %s:%i\n";
00163 #define VALIDATE_INDEX(idx, limit) \
00164     if (idx >= limit) {\
00165         av_log(vc->avccontext, AV_LOG_ERROR,\
00166                idx_err_str,\
00167                (int)(idx), (int)(limit - 1), #idx, __FILE__, __LINE__);\
00168         return AVERROR_INVALIDDATA;\
00169     }
00170 #define GET_VALIDATED_INDEX(idx, bits, limit) \
00171     {\
00172         idx = get_bits(gb, bits);\
00173         VALIDATE_INDEX(idx, limit)\
00174     }
00175 
00176 static float vorbisfloat2float(unsigned val)
00177 {
00178     double mant = val & 0x1fffff;
00179     long exp    = (val & 0x7fe00000L) >> 21;
00180     if (val & 0x80000000)
00181         mant = -mant;
00182     return ldexp(mant, exp - 20 - 768);
00183 }
00184 
00185 
00186 // Free all allocated memory -----------------------------------------
00187 
00188 static void vorbis_free(vorbis_context *vc)
00189 {
00190     int i;
00191 
00192     av_freep(&vc->channel_residues);
00193     av_freep(&vc->channel_floors);
00194     av_freep(&vc->saved);
00195 
00196     for (i = 0; i < vc->residue_count; i++)
00197         av_free(vc->residues[i].classifs);
00198     av_freep(&vc->residues);
00199     av_freep(&vc->modes);
00200 
00201     ff_mdct_end(&vc->mdct[0]);
00202     ff_mdct_end(&vc->mdct[1]);
00203 
00204     for (i = 0; i < vc->codebook_count; ++i) {
00205         av_free(vc->codebooks[i].codevectors);
00206         ff_free_vlc(&vc->codebooks[i].vlc);
00207     }
00208     av_freep(&vc->codebooks);
00209 
00210     for (i = 0; i < vc->floor_count; ++i) {
00211         if (vc->floors[i].floor_type == 0) {
00212             av_free(vc->floors[i].data.t0.map[0]);
00213             av_free(vc->floors[i].data.t0.map[1]);
00214             av_free(vc->floors[i].data.t0.book_list);
00215             av_free(vc->floors[i].data.t0.lsp);
00216         } else {
00217             av_free(vc->floors[i].data.t1.list);
00218         }
00219     }
00220     av_freep(&vc->floors);
00221 
00222     for (i = 0; i < vc->mapping_count; ++i) {
00223         av_free(vc->mappings[i].magnitude);
00224         av_free(vc->mappings[i].angle);
00225         av_free(vc->mappings[i].mux);
00226     }
00227     av_freep(&vc->mappings);
00228 }
00229 
00230 // Parse setup header -------------------------------------------------
00231 
00232 // Process codebooks part
00233 
00234 static int vorbis_parse_setup_hdr_codebooks(vorbis_context *vc)
00235 {
00236     unsigned cb;
00237     uint8_t  *tmp_vlc_bits;
00238     uint32_t *tmp_vlc_codes;
00239     GetBitContext *gb = &vc->gb;
00240     uint16_t *codebook_multiplicands;
00241     int ret = 0;
00242 
00243     vc->codebook_count = get_bits(gb, 8) + 1;
00244 
00245     av_dlog(NULL, " Codebooks: %d \n", vc->codebook_count);
00246 
00247     vc->codebooks = av_mallocz(vc->codebook_count * sizeof(*vc->codebooks));
00248     tmp_vlc_bits  = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_bits));
00249     tmp_vlc_codes = av_mallocz(V_MAX_VLCS * sizeof(*tmp_vlc_codes));
00250     codebook_multiplicands = av_malloc(V_MAX_VLCS * sizeof(*codebook_multiplicands));
00251 
00252     for (cb = 0; cb < vc->codebook_count; ++cb) {
00253         vorbis_codebook *codebook_setup = &vc->codebooks[cb];
00254         unsigned ordered, t, entries, used_entries = 0;
00255 
00256         av_dlog(NULL, " %u. Codebook\n", cb);
00257 
00258         if (get_bits(gb, 24) != 0x564342) {
00259             av_log(vc->avccontext, AV_LOG_ERROR,
00260                    " %u. Codebook setup data corrupt.\n", cb);
00261             ret = AVERROR_INVALIDDATA;
00262             goto error;
00263         }
00264 
00265         codebook_setup->dimensions=get_bits(gb, 16);
00266         if (codebook_setup->dimensions > 16 || codebook_setup->dimensions == 0) {
00267             av_log(vc->avccontext, AV_LOG_ERROR,
00268                    " %u. Codebook's dimension is invalid (%d).\n",
00269                    cb, codebook_setup->dimensions);
00270             ret = AVERROR_INVALIDDATA;
00271             goto error;
00272         }
00273         entries = get_bits(gb, 24);
00274         if (entries > V_MAX_VLCS) {
00275             av_log(vc->avccontext, AV_LOG_ERROR,
00276                    " %u. Codebook has too many entries (%u).\n",
00277                    cb, entries);
00278             ret = AVERROR_INVALIDDATA;
00279             goto error;
00280         }
00281 
00282         ordered = get_bits1(gb);
00283 
00284         av_dlog(NULL, " codebook_dimensions %d, codebook_entries %u\n",
00285                 codebook_setup->dimensions, entries);
00286 
00287         if (!ordered) {
00288             unsigned ce, flag;
00289             unsigned sparse = get_bits1(gb);
00290 
00291             av_dlog(NULL, " not ordered \n");
00292 
00293             if (sparse) {
00294                 av_dlog(NULL, " sparse \n");
00295 
00296                 used_entries = 0;
00297                 for (ce = 0; ce < entries; ++ce) {
00298                     flag = get_bits1(gb);
00299                     if (flag) {
00300                         tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
00301                         ++used_entries;
00302                     } else
00303                         tmp_vlc_bits[ce] = 0;
00304                 }
00305             } else {
00306                 av_dlog(NULL, " not sparse \n");
00307 
00308                 used_entries = entries;
00309                 for (ce = 0; ce < entries; ++ce)
00310                     tmp_vlc_bits[ce] = get_bits(gb, 5) + 1;
00311             }
00312         } else {
00313             unsigned current_entry  = 0;
00314             unsigned current_length = get_bits(gb, 5) + 1;
00315 
00316             av_dlog(NULL, " ordered, current length: %u\n", current_length);  //FIXME
00317 
00318             used_entries = entries;
00319             for (; current_entry < used_entries && current_length <= 32; ++current_length) {
00320                 unsigned i, number;
00321 
00322                 av_dlog(NULL, " number bits: %u ", ilog(entries - current_entry));
00323 
00324                 number = get_bits(gb, ilog(entries - current_entry));
00325 
00326                 av_dlog(NULL, " number: %u\n", number);
00327 
00328                 for (i = current_entry; i < number+current_entry; ++i)
00329                     if (i < used_entries)
00330                         tmp_vlc_bits[i] = current_length;
00331 
00332                 current_entry+=number;
00333             }
00334             if (current_entry>used_entries) {
00335                 av_log(vc->avccontext, AV_LOG_ERROR, " More codelengths than codes in codebook. \n");
00336                 ret = AVERROR_INVALIDDATA;
00337                 goto error;
00338             }
00339         }
00340 
00341         codebook_setup->lookup_type = get_bits(gb, 4);
00342 
00343         av_dlog(NULL, " lookup type: %d : %s \n", codebook_setup->lookup_type,
00344                 codebook_setup->lookup_type ? "vq" : "no lookup");
00345 
00346 // If the codebook is used for (inverse) VQ, calculate codevectors.
00347 
00348         if (codebook_setup->lookup_type == 1) {
00349             unsigned i, j, k;
00350             unsigned codebook_lookup_values = ff_vorbis_nth_root(entries, codebook_setup->dimensions);
00351 
00352             float codebook_minimum_value = vorbisfloat2float(get_bits_long(gb, 32));
00353             float codebook_delta_value   = vorbisfloat2float(get_bits_long(gb, 32));
00354             unsigned codebook_value_bits = get_bits(gb, 4) + 1;
00355             unsigned codebook_sequence_p = get_bits1(gb);
00356 
00357             av_dlog(NULL, " We expect %d numbers for building the codevectors. \n",
00358                     codebook_lookup_values);
00359             av_dlog(NULL, "  delta %f minmum %f \n",
00360                     codebook_delta_value, codebook_minimum_value);
00361 
00362             for (i = 0; i < codebook_lookup_values; ++i) {
00363                 codebook_multiplicands[i] = get_bits(gb, codebook_value_bits);
00364 
00365                 av_dlog(NULL, " multiplicands*delta+minmum : %e \n",
00366                         (float)codebook_multiplicands[i] * codebook_delta_value + codebook_minimum_value);
00367                 av_dlog(NULL, " multiplicand %u\n", codebook_multiplicands[i]);
00368             }
00369 
00370 // Weed out unused vlcs and build codevector vector
00371             codebook_setup->codevectors = used_entries ? av_mallocz(used_entries *
00372                                                                     codebook_setup->dimensions *
00373                                                                     sizeof(*codebook_setup->codevectors))
00374                                                        : NULL;
00375             for (j = 0, i = 0; i < entries; ++i) {
00376                 unsigned dim = codebook_setup->dimensions;
00377 
00378                 if (tmp_vlc_bits[i]) {
00379                     float last = 0.0;
00380                     unsigned lookup_offset = i;
00381 
00382                     av_dlog(vc->avccontext, "Lookup offset %u ,", i);
00383 
00384                     for (k = 0; k < dim; ++k) {
00385                         unsigned multiplicand_offset = lookup_offset % codebook_lookup_values;
00386                         codebook_setup->codevectors[j * dim + k] = codebook_multiplicands[multiplicand_offset] * codebook_delta_value + codebook_minimum_value + last;
00387                         if (codebook_sequence_p)
00388                             last = codebook_setup->codevectors[j * dim + k];
00389                         lookup_offset/=codebook_lookup_values;
00390                     }
00391                     tmp_vlc_bits[j] = tmp_vlc_bits[i];
00392 
00393                     av_dlog(vc->avccontext, "real lookup offset %u, vector: ", j);
00394                     for (k = 0; k < dim; ++k)
00395                         av_dlog(vc->avccontext, " %f ",
00396                                 codebook_setup->codevectors[j * dim + k]);
00397                     av_dlog(vc->avccontext, "\n");
00398 
00399                     ++j;
00400                 }
00401             }
00402             if (j != used_entries) {
00403                 av_log(vc->avccontext, AV_LOG_ERROR, "Bug in codevector vector building code. \n");
00404                 ret = AVERROR_INVALIDDATA;
00405                 goto error;
00406             }
00407             entries = used_entries;
00408         } else if (codebook_setup->lookup_type >= 2) {
00409             av_log(vc->avccontext, AV_LOG_ERROR, "Codebook lookup type not supported. \n");
00410             ret = AVERROR_INVALIDDATA;
00411             goto error;
00412         }
00413 
00414 // Initialize VLC table
00415         if (ff_vorbis_len2vlc(tmp_vlc_bits, tmp_vlc_codes, entries)) {
00416             av_log(vc->avccontext, AV_LOG_ERROR, " Invalid code lengths while generating vlcs. \n");
00417             ret = AVERROR_INVALIDDATA;
00418             goto error;
00419         }
00420         codebook_setup->maxdepth = 0;
00421         for (t = 0; t < entries; ++t)
00422             if (tmp_vlc_bits[t] >= codebook_setup->maxdepth)
00423                 codebook_setup->maxdepth = tmp_vlc_bits[t];
00424 
00425         if (codebook_setup->maxdepth > 3 * V_NB_BITS)
00426             codebook_setup->nb_bits = V_NB_BITS2;
00427         else
00428             codebook_setup->nb_bits = V_NB_BITS;
00429 
00430         codebook_setup->maxdepth = (codebook_setup->maxdepth+codebook_setup->nb_bits - 1) / codebook_setup->nb_bits;
00431 
00432         if ((ret = init_vlc(&codebook_setup->vlc, codebook_setup->nb_bits,
00433                             entries, tmp_vlc_bits, sizeof(*tmp_vlc_bits),
00434                             sizeof(*tmp_vlc_bits), tmp_vlc_codes,
00435                             sizeof(*tmp_vlc_codes), sizeof(*tmp_vlc_codes),
00436                             INIT_VLC_LE))) {
00437             av_log(vc->avccontext, AV_LOG_ERROR, " Error generating vlc tables. \n");
00438             goto error;
00439         }
00440     }
00441 
00442     av_free(tmp_vlc_bits);
00443     av_free(tmp_vlc_codes);
00444     av_free(codebook_multiplicands);
00445     return 0;
00446 
00447 // Error:
00448 error:
00449     av_free(tmp_vlc_bits);
00450     av_free(tmp_vlc_codes);
00451     av_free(codebook_multiplicands);
00452     return ret;
00453 }
00454 
00455 // Process time domain transforms part (unused in Vorbis I)
00456 
00457 static int vorbis_parse_setup_hdr_tdtransforms(vorbis_context *vc)
00458 {
00459     GetBitContext *gb = &vc->gb;
00460     unsigned i, vorbis_time_count = get_bits(gb, 6) + 1;
00461 
00462     for (i = 0; i < vorbis_time_count; ++i) {
00463         unsigned vorbis_tdtransform = get_bits(gb, 16);
00464 
00465         av_dlog(NULL, " Vorbis time domain transform %u: %u\n",
00466                 vorbis_time_count, vorbis_tdtransform);
00467 
00468         if (vorbis_tdtransform) {
00469             av_log(vc->avccontext, AV_LOG_ERROR, "Vorbis time domain transform data nonzero. \n");
00470             return AVERROR_INVALIDDATA;
00471         }
00472     }
00473     return 0;
00474 }
00475 
00476 // Process floors part
00477 
00478 static int vorbis_floor0_decode(vorbis_context *vc,
00479                                 vorbis_floor_data *vfu, float *vec);
00480 static void create_map(vorbis_context *vc, unsigned floor_number);
00481 static int vorbis_floor1_decode(vorbis_context *vc,
00482                                 vorbis_floor_data *vfu, float *vec);
00483 static int vorbis_parse_setup_hdr_floors(vorbis_context *vc)
00484 {
00485     GetBitContext *gb = &vc->gb;
00486     int i,j,k;
00487 
00488     vc->floor_count = get_bits(gb, 6) + 1;
00489 
00490     vc->floors = av_mallocz(vc->floor_count * sizeof(*vc->floors));
00491 
00492     for (i = 0; i < vc->floor_count; ++i) {
00493         vorbis_floor *floor_setup = &vc->floors[i];
00494 
00495         floor_setup->floor_type = get_bits(gb, 16);
00496 
00497         av_dlog(NULL, " %d. floor type %d \n", i, floor_setup->floor_type);
00498 
00499         if (floor_setup->floor_type == 1) {
00500             int maximum_class = -1;
00501             unsigned rangebits, rangemax, floor1_values = 2;
00502 
00503             floor_setup->decode = vorbis_floor1_decode;
00504 
00505             floor_setup->data.t1.partitions = get_bits(gb, 5);
00506 
00507             av_dlog(NULL, " %d.floor: %d partitions \n",
00508                     i, floor_setup->data.t1.partitions);
00509 
00510             for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
00511                 floor_setup->data.t1.partition_class[j] = get_bits(gb, 4);
00512                 if (floor_setup->data.t1.partition_class[j] > maximum_class)
00513                     maximum_class = floor_setup->data.t1.partition_class[j];
00514 
00515                 av_dlog(NULL, " %d. floor %d partition class %d \n",
00516                         i, j, floor_setup->data.t1.partition_class[j]);
00517 
00518             }
00519 
00520             av_dlog(NULL, " maximum class %d \n", maximum_class);
00521 
00522             for (j = 0; j <= maximum_class; ++j) {
00523                 floor_setup->data.t1.class_dimensions[j] = get_bits(gb, 3) + 1;
00524                 floor_setup->data.t1.class_subclasses[j] = get_bits(gb, 2);
00525 
00526                 av_dlog(NULL, " %d floor %d class dim: %d subclasses %d \n", i, j,
00527                         floor_setup->data.t1.class_dimensions[j],
00528                         floor_setup->data.t1.class_subclasses[j]);
00529 
00530                 if (floor_setup->data.t1.class_subclasses[j]) {
00531                     GET_VALIDATED_INDEX(floor_setup->data.t1.class_masterbook[j], 8, vc->codebook_count)
00532 
00533                     av_dlog(NULL, "   masterbook: %d \n", floor_setup->data.t1.class_masterbook[j]);
00534                 }
00535 
00536                 for (k = 0; k < (1 << floor_setup->data.t1.class_subclasses[j]); ++k) {
00537                     int16_t bits = get_bits(gb, 8) - 1;
00538                     if (bits != -1)
00539                         VALIDATE_INDEX(bits, vc->codebook_count)
00540                     floor_setup->data.t1.subclass_books[j][k] = bits;
00541 
00542                     av_dlog(NULL, "    book %d. : %d \n", k, floor_setup->data.t1.subclass_books[j][k]);
00543                 }
00544             }
00545 
00546             floor_setup->data.t1.multiplier = get_bits(gb, 2) + 1;
00547             floor_setup->data.t1.x_list_dim = 2;
00548 
00549             for (j = 0; j < floor_setup->data.t1.partitions; ++j)
00550                 floor_setup->data.t1.x_list_dim+=floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]];
00551 
00552             floor_setup->data.t1.list = av_mallocz(floor_setup->data.t1.x_list_dim *
00553                                                    sizeof(*floor_setup->data.t1.list));
00554 
00555 
00556             rangebits = get_bits(gb, 4);
00557             rangemax = (1 << rangebits);
00558             if (rangemax > vc->blocksize[1] / 2) {
00559                 av_log(vc->avccontext, AV_LOG_ERROR,
00560                        "Floor value is too large for blocksize: %u (%"PRIu32")\n",
00561                        rangemax, vc->blocksize[1] / 2);
00562                 return AVERROR_INVALIDDATA;
00563             }
00564             floor_setup->data.t1.list[0].x = 0;
00565             floor_setup->data.t1.list[1].x = rangemax;
00566 
00567             for (j = 0; j < floor_setup->data.t1.partitions; ++j) {
00568                 for (k = 0; k < floor_setup->data.t1.class_dimensions[floor_setup->data.t1.partition_class[j]]; ++k, ++floor1_values) {
00569                     floor_setup->data.t1.list[floor1_values].x = get_bits(gb, rangebits);
00570 
00571                     av_dlog(NULL, " %u. floor1 Y coord. %d\n", floor1_values,
00572                             floor_setup->data.t1.list[floor1_values].x);
00573                 }
00574             }
00575 
00576 // Precalculate order of x coordinates - needed for decode
00577             if (ff_vorbis_ready_floor1_list(vc->avccontext,
00578                                             floor_setup->data.t1.list,
00579                                             floor_setup->data.t1.x_list_dim)) {
00580                 return AVERROR_INVALIDDATA;
00581             }
00582         } else if (floor_setup->floor_type == 0) {
00583             unsigned max_codebook_dim = 0;
00584 
00585             floor_setup->decode = vorbis_floor0_decode;
00586 
00587             floor_setup->data.t0.order          = get_bits(gb,  8);
00588             floor_setup->data.t0.rate           = get_bits(gb, 16);
00589             floor_setup->data.t0.bark_map_size  = get_bits(gb, 16);
00590             floor_setup->data.t0.amplitude_bits = get_bits(gb,  6);
00591             /* zero would result in a div by zero later *
00592              * 2^0 - 1 == 0                             */
00593             if (floor_setup->data.t0.amplitude_bits == 0) {
00594                 av_log(vc->avccontext, AV_LOG_ERROR,
00595                        "Floor 0 amplitude bits is 0.\n");
00596                 return AVERROR_INVALIDDATA;
00597             }
00598             floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
00599             floor_setup->data.t0.num_books        = get_bits(gb, 4) + 1;
00600 
00601             /* allocate mem for booklist */
00602             floor_setup->data.t0.book_list =
00603                 av_malloc(floor_setup->data.t0.num_books);
00604             if (!floor_setup->data.t0.book_list)
00605                 return AVERROR(ENOMEM);
00606             /* read book indexes */
00607             {
00608                 int idx;
00609                 unsigned book_idx;
00610                 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
00611                     GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
00612                     floor_setup->data.t0.book_list[idx] = book_idx;
00613                     if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
00614                         max_codebook_dim = vc->codebooks[book_idx].dimensions;
00615                 }
00616             }
00617 
00618             create_map(vc, i);
00619 
00620             /* codebook dim is for padding if codebook dim doesn't *
00621              * divide order+1 then we need to read more data       */
00622             floor_setup->data.t0.lsp =
00623                 av_malloc((floor_setup->data.t0.order + 1 + max_codebook_dim)
00624                           * sizeof(*floor_setup->data.t0.lsp));
00625             if (!floor_setup->data.t0.lsp)
00626                 return AVERROR(ENOMEM);
00627 
00628             /* debug output parsed headers */
00629             av_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
00630             av_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
00631             av_dlog(NULL, "floor0 bark map size: %u\n",
00632                     floor_setup->data.t0.bark_map_size);
00633             av_dlog(NULL, "floor0 amplitude bits: %u\n",
00634                     floor_setup->data.t0.amplitude_bits);
00635             av_dlog(NULL, "floor0 amplitude offset: %u\n",
00636                     floor_setup->data.t0.amplitude_offset);
00637             av_dlog(NULL, "floor0 number of books: %u\n",
00638                     floor_setup->data.t0.num_books);
00639             av_dlog(NULL, "floor0 book list pointer: %p\n",
00640                     floor_setup->data.t0.book_list);
00641             {
00642                 int idx;
00643                 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
00644                     av_dlog(NULL, "  Book %d: %u\n", idx + 1,
00645                             floor_setup->data.t0.book_list[idx]);
00646                 }
00647             }
00648         } else {
00649             av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
00650             return AVERROR_INVALIDDATA;
00651         }
00652     }
00653     return 0;
00654 }
00655 
00656 // Process residues part
00657 
00658 static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
00659 {
00660     GetBitContext *gb = &vc->gb;
00661     unsigned i, j, k;
00662 
00663     vc->residue_count = get_bits(gb, 6)+1;
00664     vc->residues      = av_mallocz(vc->residue_count * sizeof(*vc->residues));
00665 
00666     av_dlog(NULL, " There are %d residues. \n", vc->residue_count);
00667 
00668     for (i = 0; i < vc->residue_count; ++i) {
00669         vorbis_residue *res_setup = &vc->residues[i];
00670         uint8_t cascade[64];
00671         unsigned high_bits, low_bits;
00672 
00673         res_setup->type = get_bits(gb, 16);
00674 
00675         av_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
00676 
00677         res_setup->begin          = get_bits(gb, 24);
00678         res_setup->end            = get_bits(gb, 24);
00679         res_setup->partition_size = get_bits(gb, 24) + 1;
00680         /* Validations to prevent a buffer overflow later. */
00681         if (res_setup->begin>res_setup->end ||
00682             res_setup->end > (res_setup->type == 2 ? vc->avccontext->channels : 1) * vc->blocksize[1] / 2 ||
00683             (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
00684             av_log(vc->avccontext, AV_LOG_ERROR,
00685                    "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
00686                    res_setup->type, res_setup->begin, res_setup->end,
00687                    res_setup->partition_size, vc->blocksize[1] / 2);
00688             return AVERROR_INVALIDDATA;
00689         }
00690 
00691         res_setup->classifications = get_bits(gb, 6) + 1;
00692         GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
00693 
00694         res_setup->ptns_to_read =
00695             (res_setup->end - res_setup->begin) / res_setup->partition_size;
00696         res_setup->classifs = av_malloc(res_setup->ptns_to_read *
00697                                         vc->audio_channels *
00698                                         sizeof(*res_setup->classifs));
00699         if (!res_setup->classifs)
00700             return AVERROR(ENOMEM);
00701 
00702         av_dlog(NULL, "    begin %d end %d part.size %d classif.s %d classbook %d \n",
00703                 res_setup->begin, res_setup->end, res_setup->partition_size,
00704                 res_setup->classifications, res_setup->classbook);
00705 
00706         for (j = 0; j < res_setup->classifications; ++j) {
00707             high_bits = 0;
00708             low_bits  = get_bits(gb, 3);
00709             if (get_bits1(gb))
00710                 high_bits = get_bits(gb, 5);
00711             cascade[j] = (high_bits << 3) + low_bits;
00712 
00713             av_dlog(NULL, "     %u class cascade depth: %d\n", j, ilog(cascade[j]));
00714         }
00715 
00716         res_setup->maxpass = 0;
00717         for (j = 0; j < res_setup->classifications; ++j) {
00718             for (k = 0; k < 8; ++k) {
00719                 if (cascade[j]&(1 << k)) {
00720                     GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
00721 
00722                     av_dlog(NULL, "     %u class cascade depth %u book: %d\n",
00723                             j, k, res_setup->books[j][k]);
00724 
00725                     if (k>res_setup->maxpass)
00726                         res_setup->maxpass = k;
00727                 } else {
00728                     res_setup->books[j][k] = -1;
00729                 }
00730             }
00731         }
00732     }
00733     return 0;
00734 }
00735 
00736 // Process mappings part
00737 
00738 static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
00739 {
00740     GetBitContext *gb = &vc->gb;
00741     unsigned i, j;
00742 
00743     vc->mapping_count = get_bits(gb, 6)+1;
00744     vc->mappings      = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
00745 
00746     av_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
00747 
00748     for (i = 0; i < vc->mapping_count; ++i) {
00749         vorbis_mapping *mapping_setup = &vc->mappings[i];
00750 
00751         if (get_bits(gb, 16)) {
00752             av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
00753             return AVERROR_INVALIDDATA;
00754         }
00755         if (get_bits1(gb)) {
00756             mapping_setup->submaps = get_bits(gb, 4) + 1;
00757         } else {
00758             mapping_setup->submaps = 1;
00759         }
00760 
00761         if (get_bits1(gb)) {
00762             mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
00763             mapping_setup->magnitude      = av_mallocz(mapping_setup->coupling_steps *
00764                                                        sizeof(*mapping_setup->magnitude));
00765             mapping_setup->angle          = av_mallocz(mapping_setup->coupling_steps *
00766                                                        sizeof(*mapping_setup->angle));
00767             for (j = 0; j < mapping_setup->coupling_steps; ++j) {
00768                 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
00769                 GET_VALIDATED_INDEX(mapping_setup->angle[j],     ilog(vc->audio_channels - 1), vc->audio_channels)
00770             }
00771         } else {
00772             mapping_setup->coupling_steps = 0;
00773         }
00774 
00775         av_dlog(NULL, "   %u mapping coupling steps: %d\n",
00776                 i, mapping_setup->coupling_steps);
00777 
00778         if (get_bits(gb, 2)) {
00779             av_log(vc->avccontext, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
00780             return AVERROR_INVALIDDATA; // following spec.
00781         }
00782 
00783         if (mapping_setup->submaps>1) {
00784             mapping_setup->mux = av_mallocz(vc->audio_channels *
00785                                             sizeof(*mapping_setup->mux));
00786             for (j = 0; j < vc->audio_channels; ++j)
00787                 mapping_setup->mux[j] = get_bits(gb, 4);
00788         }
00789 
00790         for (j = 0; j < mapping_setup->submaps; ++j) {
00791             skip_bits(gb, 8); // FIXME check?
00792             GET_VALIDATED_INDEX(mapping_setup->submap_floor[j],   8, vc->floor_count)
00793             GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
00794 
00795             av_dlog(NULL, "   %u mapping %u submap : floor %d, residue %d\n", i, j,
00796                     mapping_setup->submap_floor[j],
00797                     mapping_setup->submap_residue[j]);
00798         }
00799     }
00800     return 0;
00801 }
00802 
00803 // Process modes part
00804 
00805 static void create_map(vorbis_context *vc, unsigned floor_number)
00806 {
00807     vorbis_floor *floors = vc->floors;
00808     vorbis_floor0 *vf;
00809     int idx;
00810     int blockflag, n;
00811     int32_t *map;
00812 
00813     for (blockflag = 0; blockflag < 2; ++blockflag) {
00814         n = vc->blocksize[blockflag] / 2;
00815         floors[floor_number].data.t0.map[blockflag] =
00816             av_malloc((n + 1) * sizeof(int32_t)); // n + sentinel
00817 
00818         map =  floors[floor_number].data.t0.map[blockflag];
00819         vf  = &floors[floor_number].data.t0;
00820 
00821         for (idx = 0; idx < n; ++idx) {
00822             map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
00823                              (vf->bark_map_size / BARK(vf->rate / 2.0f)));
00824             if (vf->bark_map_size-1 < map[idx])
00825                 map[idx] = vf->bark_map_size - 1;
00826         }
00827         map[n] = -1;
00828         vf->map_size[blockflag] = n;
00829     }
00830 
00831     for (idx = 0; idx <= n; ++idx) {
00832         av_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
00833     }
00834 }
00835 
00836 static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
00837 {
00838     GetBitContext *gb = &vc->gb;
00839     unsigned i;
00840 
00841     vc->mode_count = get_bits(gb, 6) + 1;
00842     vc->modes      = av_mallocz(vc->mode_count * sizeof(*vc->modes));
00843 
00844     av_dlog(NULL, " There are %d modes.\n", vc->mode_count);
00845 
00846     for (i = 0; i < vc->mode_count; ++i) {
00847         vorbis_mode *mode_setup = &vc->modes[i];
00848 
00849         mode_setup->blockflag     = get_bits1(gb);
00850         mode_setup->windowtype    = get_bits(gb, 16); //FIXME check
00851         mode_setup->transformtype = get_bits(gb, 16); //FIXME check
00852         GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
00853 
00854         av_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
00855                 i, mode_setup->blockflag, mode_setup->windowtype,
00856                 mode_setup->transformtype, mode_setup->mapping);
00857     }
00858     return 0;
00859 }
00860 
00861 // Process the whole setup header using the functions above
00862 
00863 static int vorbis_parse_setup_hdr(vorbis_context *vc)
00864 {
00865     GetBitContext *gb = &vc->gb;
00866     int ret;
00867 
00868     if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
00869         (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
00870         (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
00871         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
00872         return AVERROR_INVALIDDATA;
00873     }
00874 
00875     if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) {
00876         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
00877         return ret;
00878     }
00879     if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) {
00880         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
00881         return ret;
00882     }
00883     if ((ret = vorbis_parse_setup_hdr_floors(vc))) {
00884         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
00885         return ret;
00886     }
00887     if ((ret = vorbis_parse_setup_hdr_residues(vc))) {
00888         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
00889         return ret;
00890     }
00891     if ((ret = vorbis_parse_setup_hdr_mappings(vc))) {
00892         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
00893         return ret;
00894     }
00895     if ((ret = vorbis_parse_setup_hdr_modes(vc))) {
00896         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
00897         return ret;
00898     }
00899     if (!get_bits1(gb)) {
00900         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
00901         return AVERROR_INVALIDDATA; // framing flag bit unset error
00902     }
00903 
00904     return 0;
00905 }
00906 
00907 // Process the identification header
00908 
00909 static int vorbis_parse_id_hdr(vorbis_context *vc)
00910 {
00911     GetBitContext *gb = &vc->gb;
00912     unsigned bl0, bl1;
00913 
00914     if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
00915         (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
00916         (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
00917         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
00918         return AVERROR_INVALIDDATA;
00919     }
00920 
00921     vc->version        = get_bits_long(gb, 32);    //FIXME check 0
00922     vc->audio_channels = get_bits(gb, 8);
00923     if (vc->audio_channels <= 0) {
00924         av_log(vc->avccontext, AV_LOG_ERROR, "Invalid number of channels\n");
00925         return AVERROR_INVALIDDATA;
00926     }
00927     vc->audio_samplerate = get_bits_long(gb, 32);
00928     if (vc->audio_samplerate <= 0) {
00929         av_log(vc->avccontext, AV_LOG_ERROR, "Invalid samplerate\n");
00930         return AVERROR_INVALIDDATA;
00931     }
00932     vc->bitrate_maximum = get_bits_long(gb, 32);
00933     vc->bitrate_nominal = get_bits_long(gb, 32);
00934     vc->bitrate_minimum = get_bits_long(gb, 32);
00935     bl0 = get_bits(gb, 4);
00936     bl1 = get_bits(gb, 4);
00937     vc->blocksize[0] = (1 << bl0);
00938     vc->blocksize[1] = (1 << bl1);
00939     if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
00940         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
00941         return AVERROR_INVALIDDATA;
00942     }
00943     vc->win[0] = ff_vorbis_vwin[bl0 - 6];
00944     vc->win[1] = ff_vorbis_vwin[bl1 - 6];
00945 
00946     if ((get_bits1(gb)) == 0) {
00947         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
00948         return AVERROR_INVALIDDATA;
00949     }
00950 
00951     vc->channel_residues =  av_malloc((vc->blocksize[1]  / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
00952     vc->channel_floors   =  av_malloc((vc->blocksize[1]  / 2) * vc->audio_channels * sizeof(*vc->channel_floors));
00953     vc->saved            =  av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
00954     vc->previous_window  = 0;
00955 
00956     ff_mdct_init(&vc->mdct[0], bl0, 1, -vc->scale_bias);
00957     ff_mdct_init(&vc->mdct[1], bl1, 1, -vc->scale_bias);
00958 
00959     av_dlog(NULL, " vorbis version %d \n audio_channels %d \n audio_samplerate %d \n bitrate_max %d \n bitrate_nom %d \n bitrate_min %d \n blk_0 %d blk_1 %d \n ",
00960             vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
00961 
00962 /*
00963     BLK = vc->blocksize[0];
00964     for (i = 0; i < BLK / 2; ++i) {
00965         vc->win[0][i] = sin(0.5*3.14159265358*(sin(((float)i + 0.5) / (float)BLK*3.14159265358))*(sin(((float)i + 0.5) / (float)BLK*3.14159265358)));
00966     }
00967 */
00968 
00969     return 0;
00970 }
00971 
00972 // Process the extradata using the functions above (identification header, setup header)
00973 
00974 static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
00975 {
00976     vorbis_context *vc = avccontext->priv_data;
00977     uint8_t *headers   = avccontext->extradata;
00978     int headers_len    = avccontext->extradata_size;
00979     uint8_t *header_start[3];
00980     int header_len[3];
00981     GetBitContext *gb = &vc->gb;
00982     int hdr_type, ret;
00983 
00984     vc->avccontext = avccontext;
00985     dsputil_init(&vc->dsp, avccontext);
00986     ff_fmt_convert_init(&vc->fmt_conv, avccontext);
00987 
00988     if (avccontext->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
00989         avccontext->sample_fmt = AV_SAMPLE_FMT_FLT;
00990         vc->scale_bias = 1.0f;
00991     } else {
00992         avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
00993         vc->scale_bias = 32768.0f;
00994     }
00995 
00996     if (!headers_len) {
00997         av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
00998         return AVERROR_INVALIDDATA;
00999     }
01000 
01001     if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) {
01002         av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
01003         return ret;
01004     }
01005 
01006     init_get_bits(gb, header_start[0], header_len[0]*8);
01007     hdr_type = get_bits(gb, 8);
01008     if (hdr_type != 1) {
01009         av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
01010         return AVERROR_INVALIDDATA;
01011     }
01012     if ((ret = vorbis_parse_id_hdr(vc))) {
01013         av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
01014         vorbis_free(vc);
01015         return ret;
01016     }
01017 
01018     init_get_bits(gb, header_start[2], header_len[2]*8);
01019     hdr_type = get_bits(gb, 8);
01020     if (hdr_type != 5) {
01021         av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
01022         vorbis_free(vc);
01023         return AVERROR_INVALIDDATA;
01024     }
01025     if ((ret = vorbis_parse_setup_hdr(vc))) {
01026         av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
01027         vorbis_free(vc);
01028         return ret;
01029     }
01030 
01031     if (vc->audio_channels > 8)
01032         avccontext->channel_layout = 0;
01033     else
01034         avccontext->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
01035 
01036     avccontext->channels    = vc->audio_channels;
01037     avccontext->sample_rate = vc->audio_samplerate;
01038     avccontext->frame_size  = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
01039 
01040     avcodec_get_frame_defaults(&vc->frame);
01041     avccontext->coded_frame = &vc->frame;
01042 
01043     return 0;
01044 }
01045 
01046 // Decode audiopackets -------------------------------------------------
01047 
01048 // Read and decode floor
01049 
01050 static int vorbis_floor0_decode(vorbis_context *vc,
01051                                 vorbis_floor_data *vfu, float *vec)
01052 {
01053     vorbis_floor0 *vf = &vfu->t0;
01054     float *lsp = vf->lsp;
01055     unsigned amplitude, book_idx;
01056     unsigned blockflag = vc->modes[vc->mode_number].blockflag;
01057 
01058     amplitude = get_bits(&vc->gb, vf->amplitude_bits);
01059     if (amplitude > 0) {
01060         float last = 0;
01061         unsigned idx, lsp_len = 0;
01062         vorbis_codebook codebook;
01063 
01064         book_idx = get_bits(&vc->gb, ilog(vf->num_books));
01065         if (book_idx >= vf->num_books) {
01066             av_log(vc->avccontext, AV_LOG_ERROR,
01067                     "floor0 dec: booknumber too high!\n");
01068             book_idx =  0;
01069         }
01070         av_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
01071         codebook = vc->codebooks[vf->book_list[book_idx]];
01072         /* Invalid codebook! */
01073         if (!codebook.codevectors)
01074             return AVERROR_INVALIDDATA;
01075 
01076         while (lsp_len<vf->order) {
01077             int vec_off;
01078 
01079             av_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
01080             av_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
01081             /* read temp vector */
01082             vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
01083                                codebook.nb_bits, codebook.maxdepth)
01084                       * codebook.dimensions;
01085             av_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
01086             /* copy each vector component and add last to it */
01087             for (idx = 0; idx < codebook.dimensions; ++idx)
01088                 lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
01089             last = lsp[lsp_len+idx-1]; /* set last to last vector component */
01090 
01091             lsp_len += codebook.dimensions;
01092         }
01093         /* DEBUG: output lsp coeffs */
01094         {
01095             int idx;
01096             for (idx = 0; idx < lsp_len; ++idx)
01097                 av_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
01098         }
01099 
01100         /* synthesize floor output vector */
01101         {
01102             int i;
01103             int order = vf->order;
01104             float wstep = M_PI / vf->bark_map_size;
01105 
01106             for (i = 0; i < order; i++)
01107                 lsp[i] = 2.0f * cos(lsp[i]);
01108 
01109             av_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
01110                     vf->map_size[blockflag], order, wstep);
01111 
01112             i = 0;
01113             while (i < vf->map_size[blockflag]) {
01114                 int j, iter_cond = vf->map[blockflag][i];
01115                 float p = 0.5f;
01116                 float q = 0.5f;
01117                 float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
01118 
01119                 /* similar part for the q and p products */
01120                 for (j = 0; j + 1 < order; j += 2) {
01121                     q *= lsp[j]     - two_cos_w;
01122                     p *= lsp[j + 1] - two_cos_w;
01123                 }
01124                 if (j == order) { // even order
01125                     p *= p * (2.0f - two_cos_w);
01126                     q *= q * (2.0f + two_cos_w);
01127                 } else { // odd order
01128                     q *= two_cos_w-lsp[j]; // one more time for q
01129 
01130                     /* final step and square */
01131                     p *= p * (4.f - two_cos_w * two_cos_w);
01132                     q *= q;
01133                 }
01134 
01135                 /* calculate linear floor value */
01136                 q = exp((((amplitude*vf->amplitude_offset) /
01137                           (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
01138                          - vf->amplitude_offset) * .11512925f);
01139 
01140                 /* fill vector */
01141                 do {
01142                     vec[i] = q; ++i;
01143                 } while (vf->map[blockflag][i] == iter_cond);
01144             }
01145         }
01146     } else {
01147         /* this channel is unused */
01148         return 1;
01149     }
01150 
01151     av_dlog(NULL, " Floor0 decoded\n");
01152 
01153     return 0;
01154 }
01155 
01156 static int vorbis_floor1_decode(vorbis_context *vc,
01157                                 vorbis_floor_data *vfu, float *vec)
01158 {
01159     vorbis_floor1 *vf = &vfu->t1;
01160     GetBitContext *gb = &vc->gb;
01161     uint16_t range_v[4] = { 256, 128, 86, 64 };
01162     unsigned range = range_v[vf->multiplier - 1];
01163     uint16_t floor1_Y[258];
01164     uint16_t floor1_Y_final[258];
01165     int floor1_flag[258];
01166     unsigned class, cdim, cbits, csub, cval, offset, i, j;
01167     int book, adx, ady, dy, off, predicted, err;
01168 
01169 
01170     if (!get_bits1(gb)) // silence
01171         return 1;
01172 
01173 // Read values (or differences) for the floor's points
01174 
01175     floor1_Y[0] = get_bits(gb, ilog(range - 1));
01176     floor1_Y[1] = get_bits(gb, ilog(range - 1));
01177 
01178     av_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
01179 
01180     offset = 2;
01181     for (i = 0; i < vf->partitions; ++i) {
01182         class = vf->partition_class[i];
01183         cdim   = vf->class_dimensions[class];
01184         cbits  = vf->class_subclasses[class];
01185         csub = (1 << cbits) - 1;
01186         cval = 0;
01187 
01188         av_dlog(NULL, "Cbits %u\n", cbits);
01189 
01190         if (cbits) // this reads all subclasses for this partition's class
01191             cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[class]].vlc.table,
01192                             vc->codebooks[vf->class_masterbook[class]].nb_bits, 3);
01193 
01194         for (j = 0; j < cdim; ++j) {
01195             book = vf->subclass_books[class][cval & csub];
01196 
01197             av_dlog(NULL, "book %d Cbits %u cval %u  bits:%d\n",
01198                     book, cbits, cval, get_bits_count(gb));
01199 
01200             cval = cval >> cbits;
01201             if (book > -1) {
01202                 floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
01203                 vc->codebooks[book].nb_bits, 3);
01204             } else {
01205                 floor1_Y[offset+j] = 0;
01206             }
01207 
01208             av_dlog(NULL, " floor(%d) = %d \n",
01209                     vf->list[offset+j].x, floor1_Y[offset+j]);
01210         }
01211         offset+=cdim;
01212     }
01213 
01214 // Amplitude calculation from the differences
01215 
01216     floor1_flag[0] = 1;
01217     floor1_flag[1] = 1;
01218     floor1_Y_final[0] = floor1_Y[0];
01219     floor1_Y_final[1] = floor1_Y[1];
01220 
01221     for (i = 2; i < vf->x_list_dim; ++i) {
01222         unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
01223 
01224         low_neigh_offs  = vf->list[i].low;
01225         high_neigh_offs = vf->list[i].high;
01226         dy  = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs];  // render_point begin
01227         adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
01228         ady = FFABS(dy);
01229         err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
01230         off = err / adx;
01231         if (dy < 0) {
01232             predicted = floor1_Y_final[low_neigh_offs] - off;
01233         } else {
01234             predicted = floor1_Y_final[low_neigh_offs] + off;
01235         } // render_point end
01236 
01237         val = floor1_Y[i];
01238         highroom = range-predicted;
01239         lowroom  = predicted;
01240         if (highroom < lowroom) {
01241             room = highroom * 2;
01242         } else {
01243             room = lowroom * 2;   // SPEC mispelling
01244         }
01245         if (val) {
01246             floor1_flag[low_neigh_offs]  = 1;
01247             floor1_flag[high_neigh_offs] = 1;
01248             floor1_flag[i]               = 1;
01249             if (val >= room) {
01250                 if (highroom > lowroom) {
01251                     floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
01252                 } else {
01253                     floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
01254                 }
01255             } else {
01256                 if (val & 1) {
01257                     floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
01258                 } else {
01259                     floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
01260                 }
01261             }
01262         } else {
01263             floor1_flag[i]    = 0;
01264             floor1_Y_final[i] = av_clip_uint16(predicted);
01265         }
01266 
01267         av_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
01268                 vf->list[i].x, floor1_Y_final[i], val);
01269     }
01270 
01271 // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
01272 
01273     ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
01274 
01275     av_dlog(NULL, " Floor decoded\n");
01276 
01277     return 0;
01278 }
01279 
01280 // Read and decode residue
01281 
01282 static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
01283                                                            vorbis_residue *vr,
01284                                                            unsigned ch,
01285                                                            uint8_t *do_not_decode,
01286                                                            float *vec,
01287                                                            unsigned vlen,
01288                                                            unsigned ch_left,
01289                                                            int vr_type)
01290 {
01291     GetBitContext *gb = &vc->gb;
01292     unsigned c_p_c        = vc->codebooks[vr->classbook].dimensions;
01293     unsigned ptns_to_read = vr->ptns_to_read;
01294     uint8_t *classifs = vr->classifs;
01295     unsigned pass, ch_used, i, j, k, l;
01296     unsigned max_output = (ch - 1) * vlen;
01297 
01298     if (vr_type == 2) {
01299         for (j = 1; j < ch; ++j)
01300             do_not_decode[0] &= do_not_decode[j];  // FIXME - clobbering input
01301         if (do_not_decode[0])
01302             return 0;
01303         ch_used = 1;
01304         max_output += vr->end / ch;
01305     } else {
01306         ch_used = ch;
01307         max_output += vr->end;
01308     }
01309 
01310     if (max_output > ch_left * vlen) {
01311         av_log(vc->avccontext, AV_LOG_ERROR, "Insufficient output buffer\n");
01312         return -1;
01313     }
01314 
01315     av_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d  cpc %d  \n", ch, c_p_c);
01316 
01317     for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
01318         uint16_t voffset, partition_count, j_times_ptns_to_read;
01319 
01320         voffset = vr->begin;
01321         for (partition_count = 0; partition_count < ptns_to_read;) {  // SPEC        error
01322             if (!pass) {
01323                 unsigned inverse_class = ff_inverse[vr->classifications];
01324                 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
01325                     if (!do_not_decode[j]) {
01326                         unsigned temp = get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
01327                                                  vc->codebooks[vr->classbook].nb_bits, 3);
01328 
01329                         av_dlog(NULL, "Classword: %u\n", temp);
01330 
01331                         assert(vr->classifications > 1 && temp <= 65536); //needed for inverse[]
01332                         for (i = 0; i < c_p_c; ++i) {
01333                             unsigned temp2;
01334 
01335                             temp2 = (((uint64_t)temp) * inverse_class) >> 32;
01336                             if (partition_count + c_p_c - 1 - i < ptns_to_read)
01337                                 classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->classifications;
01338                             temp = temp2;
01339                         }
01340                     }
01341                     j_times_ptns_to_read += ptns_to_read;
01342                 }
01343             }
01344             for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
01345                 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
01346                     unsigned voffs;
01347 
01348                     if (!do_not_decode[j]) {
01349                         unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
01350                         int vqbook  = vr->books[vqclass][pass];
01351 
01352                         if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
01353                             unsigned coffs;
01354                             unsigned dim  = vc->codebooks[vqbook].dimensions;
01355                             unsigned step = dim == 1 ? vr->partition_size
01356                                                      : FASTDIV(vr->partition_size, dim);
01357                             vorbis_codebook codebook = vc->codebooks[vqbook];
01358 
01359                             if (vr_type == 0) {
01360 
01361                                 voffs = voffset+j*vlen;
01362                                 for (k = 0; k < step; ++k) {
01363                                     coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01364                                     for (l = 0; l < dim; ++l)
01365                                         vec[voffs + k + l * step] += codebook.codevectors[coffs + l];  // FPMATH
01366                                 }
01367                             } else if (vr_type == 1) {
01368                                 voffs = voffset + j * vlen;
01369                                 for (k = 0; k < step; ++k) {
01370                                     coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01371                                     for (l = 0; l < dim; ++l, ++voffs) {
01372                                         vec[voffs]+=codebook.codevectors[coffs+l];  // FPMATH
01373 
01374                                         av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d  \n",
01375                                                 pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
01376                                     }
01377                                 }
01378                             } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
01379                                 voffs = voffset >> 1;
01380 
01381                                 if (dim == 2) {
01382                                     for (k = 0; k < step; ++k) {
01383                                         coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
01384                                         vec[voffs + k       ] += codebook.codevectors[coffs    ];  // FPMATH
01385                                         vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];  // FPMATH
01386                                     }
01387                                 } else if (dim == 4) {
01388                                     for (k = 0; k < step; ++k, voffs += 2) {
01389                                         coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
01390                                         vec[voffs           ] += codebook.codevectors[coffs    ];  // FPMATH
01391                                         vec[voffs + 1       ] += codebook.codevectors[coffs + 2];  // FPMATH
01392                                         vec[voffs + vlen    ] += codebook.codevectors[coffs + 1];  // FPMATH
01393                                         vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];  // FPMATH
01394                                     }
01395                                 } else
01396                                 for (k = 0; k < step; ++k) {
01397                                     coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01398                                     for (l = 0; l < dim; l += 2, voffs++) {
01399                                         vec[voffs       ] += codebook.codevectors[coffs + l    ];  // FPMATH
01400                                         vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];  // FPMATH
01401 
01402                                         av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d  \n",
01403                                                 pass, voffset / ch + (voffs % ch) * vlen,
01404                                                 vec[voffset / ch + (voffs % ch) * vlen],
01405                                                 codebook.codevectors[coffs + l], coffs, l);
01406                                     }
01407                                 }
01408 
01409                             } else if (vr_type == 2) {
01410                                 voffs = voffset;
01411 
01412                                 for (k = 0; k < step; ++k) {
01413                                     coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01414                                     for (l = 0; l < dim; ++l, ++voffs) {
01415                                         vec[voffs / ch + (voffs % ch) * vlen] += codebook.codevectors[coffs + l];  // FPMATH FIXME use if and counter instead of / and %
01416 
01417                                         av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d  \n",
01418                                                 pass, voffset / ch + (voffs % ch) * vlen,
01419                                                 vec[voffset / ch + (voffs % ch) * vlen],
01420                                                 codebook.codevectors[coffs + l], coffs, l);
01421                                     }
01422                                 }
01423                             }
01424                         }
01425                     }
01426                     j_times_ptns_to_read += ptns_to_read;
01427                 }
01428                 ++partition_count;
01429                 voffset += vr->partition_size;
01430             }
01431         }
01432     }
01433     return 0;
01434 }
01435 
01436 static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
01437                                         unsigned ch,
01438                                         uint8_t *do_not_decode,
01439                                         float *vec, unsigned vlen,
01440                                         unsigned ch_left)
01441 {
01442     if (vr->type == 2)
01443         return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
01444     else if (vr->type == 1)
01445         return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
01446     else if (vr->type == 0)
01447         return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
01448     else {
01449         av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
01450         return AVERROR_INVALIDDATA;
01451     }
01452 }
01453 
01454 void vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
01455 {
01456     int i;
01457     for (i = 0;  i < blocksize;  i++) {
01458         if (mag[i] > 0.0) {
01459             if (ang[i] > 0.0) {
01460                 ang[i] = mag[i] - ang[i];
01461             } else {
01462                 float temp = ang[i];
01463                 ang[i]     = mag[i];
01464                 mag[i]    += temp;
01465             }
01466         } else {
01467             if (ang[i] > 0.0) {
01468                 ang[i] += mag[i];
01469             } else {
01470                 float temp = ang[i];
01471                 ang[i]     = mag[i];
01472                 mag[i]    -= temp;
01473             }
01474         }
01475     }
01476 }
01477 
01478 // Decode the audio packet using the functions above
01479 
01480 static int vorbis_parse_audio_packet(vorbis_context *vc)
01481 {
01482     GetBitContext *gb = &vc->gb;
01483     FFTContext *mdct;
01484     unsigned previous_window = vc->previous_window;
01485     unsigned mode_number, blockflag, blocksize;
01486     int i, j;
01487     uint8_t no_residue[255];
01488     uint8_t do_not_decode[255];
01489     vorbis_mapping *mapping;
01490     float *ch_res_ptr   = vc->channel_residues;
01491     float *ch_floor_ptr = vc->channel_floors;
01492     uint8_t res_chan[255];
01493     unsigned res_num = 0;
01494     int retlen  = 0;
01495     unsigned ch_left = vc->audio_channels;
01496     unsigned vlen;
01497 
01498     if (get_bits1(gb)) {
01499         av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
01500         return AVERROR_INVALIDDATA; // packet type not audio
01501     }
01502 
01503     if (vc->mode_count == 1) {
01504         mode_number = 0;
01505     } else {
01506         GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
01507     }
01508     vc->mode_number = mode_number;
01509     mapping = &vc->mappings[vc->modes[mode_number].mapping];
01510 
01511     av_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
01512             vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
01513 
01514     blockflag = vc->modes[mode_number].blockflag;
01515     blocksize = vc->blocksize[blockflag];
01516     vlen = blocksize / 2;
01517     if (blockflag)
01518         skip_bits(gb, 2); // previous_window, next_window
01519 
01520     memset(ch_res_ptr,   0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
01521     memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
01522 
01523 // Decode floor
01524 
01525     for (i = 0; i < vc->audio_channels; ++i) {
01526         vorbis_floor *floor;
01527         int ret;
01528         if (mapping->submaps > 1) {
01529             floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
01530         } else {
01531             floor = &vc->floors[mapping->submap_floor[0]];
01532         }
01533 
01534         ret = floor->decode(vc, &floor->data, ch_floor_ptr);
01535 
01536         if (ret < 0) {
01537             av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
01538             return AVERROR_INVALIDDATA;
01539         }
01540         no_residue[i] = ret;
01541         ch_floor_ptr += vlen;
01542     }
01543 
01544 // Nonzero vector propagate
01545 
01546     for (i = mapping->coupling_steps - 1; i >= 0; --i) {
01547         if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
01548             no_residue[mapping->magnitude[i]] = 0;
01549             no_residue[mapping->angle[i]]     = 0;
01550         }
01551     }
01552 
01553 // Decode residue
01554 
01555     for (i = 0; i < mapping->submaps; ++i) {
01556         vorbis_residue *residue;
01557         unsigned ch = 0;
01558         int ret;
01559 
01560         for (j = 0; j < vc->audio_channels; ++j) {
01561             if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
01562                 res_chan[j] = res_num;
01563                 if (no_residue[j]) {
01564                     do_not_decode[ch] = 1;
01565                 } else {
01566                     do_not_decode[ch] = 0;
01567                 }
01568                 ++ch;
01569                 ++res_num;
01570             }
01571         }
01572         residue = &vc->residues[mapping->submap_residue[i]];
01573         if (ch_left < ch) {
01574             av_log(vc->avccontext, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
01575             return -1;
01576         }
01577         if (ch) {
01578             ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
01579             if (ret < 0)
01580                 return ret;
01581         }
01582 
01583         ch_res_ptr += ch * vlen;
01584         ch_left -= ch;
01585     }
01586 
01587 // Inverse coupling
01588 
01589     for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
01590         float *mag, *ang;
01591 
01592         mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
01593         ang = vc->channel_residues+res_chan[mapping->angle[i]]     * blocksize / 2;
01594         vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
01595     }
01596 
01597 // Dotproduct, MDCT
01598 
01599     mdct = &vc->mdct[blockflag];
01600 
01601     for (j = vc->audio_channels-1;j >= 0; j--) {
01602         ch_floor_ptr = vc->channel_floors   + j           * blocksize / 2;
01603         ch_res_ptr   = vc->channel_residues + res_chan[j] * blocksize / 2;
01604         vc->dsp.vector_fmul(ch_floor_ptr, ch_floor_ptr, ch_res_ptr, blocksize / 2);
01605         mdct->imdct_half(mdct, ch_res_ptr, ch_floor_ptr);
01606     }
01607 
01608 // Overlap/add, save data for next overlapping  FPMATH
01609 
01610     retlen = (blocksize + vc->blocksize[previous_window]) / 4;
01611     for (j = 0; j < vc->audio_channels; j++) {
01612         unsigned bs0 = vc->blocksize[0];
01613         unsigned bs1 = vc->blocksize[1];
01614         float *residue    = vc->channel_residues + res_chan[j] * blocksize / 2;
01615         float *saved      = vc->saved + j * bs1 / 4;
01616         float *ret        = vc->channel_floors + j * retlen;
01617         float *buf        = residue;
01618         const float *win  = vc->win[blockflag & previous_window];
01619 
01620         if (blockflag == previous_window) {
01621             vc->dsp.vector_fmul_window(ret, saved, buf, win, blocksize / 4);
01622         } else if (blockflag > previous_window) {
01623             vc->dsp.vector_fmul_window(ret, saved, buf, win, bs0 / 4);
01624             memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
01625         } else {
01626             memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
01627             vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
01628         }
01629         memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
01630     }
01631 
01632     vc->previous_window = blockflag;
01633     return retlen;
01634 }
01635 
01636 // Return the decoded audio packet through the standard api
01637 
01638 static int vorbis_decode_frame(AVCodecContext *avccontext, void *data,
01639                                int *got_frame_ptr, AVPacket *avpkt)
01640 {
01641     const uint8_t *buf = avpkt->data;
01642     int buf_size       = avpkt->size;
01643     vorbis_context *vc = avccontext->priv_data;
01644     GetBitContext *gb = &vc->gb;
01645     const float *channel_ptrs[255];
01646     int i, len, ret;
01647 
01648     av_dlog(NULL, "packet length %d \n", buf_size);
01649 
01650     init_get_bits(gb, buf, buf_size*8);
01651 
01652     if ((len = vorbis_parse_audio_packet(vc)) <= 0)
01653         return len;
01654 
01655     if (!vc->first_frame) {
01656         vc->first_frame = 1;
01657         *got_frame_ptr = 0;
01658         return buf_size;
01659     }
01660 
01661     av_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
01662             get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
01663 
01664     /* get output buffer */
01665     vc->frame.nb_samples = len;
01666     if ((ret = avccontext->get_buffer(avccontext, &vc->frame)) < 0) {
01667         av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
01668         return ret;
01669     }
01670 
01671     if (vc->audio_channels > 8) {
01672         for (i = 0; i < vc->audio_channels; i++)
01673             channel_ptrs[i] = vc->channel_floors + i * len;
01674     } else {
01675         for (i = 0; i < vc->audio_channels; i++)
01676             channel_ptrs[i] = vc->channel_floors +
01677                               len * ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
01678     }
01679 
01680     if (avccontext->sample_fmt == AV_SAMPLE_FMT_FLT)
01681         vc->fmt_conv.float_interleave((float *)vc->frame.data[0], channel_ptrs,
01682                                       len, vc->audio_channels);
01683     else
01684         vc->fmt_conv.float_to_int16_interleave((int16_t *)vc->frame.data[0],
01685                                                channel_ptrs, len,
01686                                                vc->audio_channels);
01687 
01688     *got_frame_ptr   = 1;
01689     *(AVFrame *)data = vc->frame;
01690 
01691     return buf_size;
01692 }
01693 
01694 // Close decoder
01695 
01696 static av_cold int vorbis_decode_close(AVCodecContext *avccontext)
01697 {
01698     vorbis_context *vc = avccontext->priv_data;
01699 
01700     vorbis_free(vc);
01701 
01702     return 0;
01703 }
01704 
01705 AVCodec ff_vorbis_decoder = {
01706     .name           = "vorbis",
01707     .type           = AVMEDIA_TYPE_AUDIO,
01708     .id             = CODEC_ID_VORBIS,
01709     .priv_data_size = sizeof(vorbis_context),
01710     .init           = vorbis_decode_init,
01711     .close          = vorbis_decode_close,
01712     .decode         = vorbis_decode_frame,
01713     .capabilities   = CODEC_CAP_DR1,
01714     .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
01715     .channel_layouts = ff_vorbis_channel_layouts,
01716     .sample_fmts = (const enum AVSampleFormat[]) {
01717         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
01718     },
01719 };
01720