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         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             ff_vorbis_ready_floor1_list(floor_setup->data.t1.list, floor_setup->data.t1.x_list_dim);
00578         } else if (floor_setup->floor_type == 0) {
00579             unsigned max_codebook_dim = 0;
00580 
00581             floor_setup->decode = vorbis_floor0_decode;
00582 
00583             floor_setup->data.t0.order          = get_bits(gb,  8);
00584             floor_setup->data.t0.rate           = get_bits(gb, 16);
00585             floor_setup->data.t0.bark_map_size  = get_bits(gb, 16);
00586             floor_setup->data.t0.amplitude_bits = get_bits(gb,  6);
00587             /* zero would result in a div by zero later *
00588              * 2^0 - 1 == 0                             */
00589             if (floor_setup->data.t0.amplitude_bits == 0) {
00590                 av_log(vc->avccontext, AV_LOG_ERROR,
00591                        "Floor 0 amplitude bits is 0.\n");
00592                 return AVERROR_INVALIDDATA;
00593             }
00594             floor_setup->data.t0.amplitude_offset = get_bits(gb, 8);
00595             floor_setup->data.t0.num_books        = get_bits(gb, 4) + 1;
00596 
00597             /* allocate mem for booklist */
00598             floor_setup->data.t0.book_list =
00599                 av_malloc(floor_setup->data.t0.num_books);
00600             if (!floor_setup->data.t0.book_list)
00601                 return AVERROR(ENOMEM);
00602             /* read book indexes */
00603             {
00604                 int idx;
00605                 unsigned book_idx;
00606                 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
00607                     GET_VALIDATED_INDEX(book_idx, 8, vc->codebook_count)
00608                     floor_setup->data.t0.book_list[idx] = book_idx;
00609                     if (vc->codebooks[book_idx].dimensions > max_codebook_dim)
00610                         max_codebook_dim = vc->codebooks[book_idx].dimensions;
00611                 }
00612             }
00613 
00614             create_map(vc, i);
00615 
00616             /* codebook dim is for padding if codebook dim doesn't *
00617              * divide order+1 then we need to read more data       */
00618             floor_setup->data.t0.lsp =
00619                 av_malloc((floor_setup->data.t0.order + 1 + max_codebook_dim)
00620                           * sizeof(*floor_setup->data.t0.lsp));
00621             if (!floor_setup->data.t0.lsp)
00622                 return AVERROR(ENOMEM);
00623 
00624             /* debug output parsed headers */
00625             av_dlog(NULL, "floor0 order: %u\n", floor_setup->data.t0.order);
00626             av_dlog(NULL, "floor0 rate: %u\n", floor_setup->data.t0.rate);
00627             av_dlog(NULL, "floor0 bark map size: %u\n",
00628                     floor_setup->data.t0.bark_map_size);
00629             av_dlog(NULL, "floor0 amplitude bits: %u\n",
00630                     floor_setup->data.t0.amplitude_bits);
00631             av_dlog(NULL, "floor0 amplitude offset: %u\n",
00632                     floor_setup->data.t0.amplitude_offset);
00633             av_dlog(NULL, "floor0 number of books: %u\n",
00634                     floor_setup->data.t0.num_books);
00635             av_dlog(NULL, "floor0 book list pointer: %p\n",
00636                     floor_setup->data.t0.book_list);
00637             {
00638                 int idx;
00639                 for (idx = 0; idx < floor_setup->data.t0.num_books; ++idx) {
00640                     av_dlog(NULL, "  Book %d: %u\n", idx + 1,
00641                             floor_setup->data.t0.book_list[idx]);
00642                 }
00643             }
00644         } else {
00645             av_log(vc->avccontext, AV_LOG_ERROR, "Invalid floor type!\n");
00646             return AVERROR_INVALIDDATA;
00647         }
00648     }
00649     return 0;
00650 }
00651 
00652 // Process residues part
00653 
00654 static int vorbis_parse_setup_hdr_residues(vorbis_context *vc)
00655 {
00656     GetBitContext *gb = &vc->gb;
00657     unsigned i, j, k;
00658 
00659     vc->residue_count = get_bits(gb, 6)+1;
00660     vc->residues      = av_mallocz(vc->residue_count * sizeof(*vc->residues));
00661 
00662     av_dlog(NULL, " There are %d residues. \n", vc->residue_count);
00663 
00664     for (i = 0; i < vc->residue_count; ++i) {
00665         vorbis_residue *res_setup = &vc->residues[i];
00666         uint8_t cascade[64];
00667         unsigned high_bits, low_bits;
00668 
00669         res_setup->type = get_bits(gb, 16);
00670 
00671         av_dlog(NULL, " %u. residue type %d\n", i, res_setup->type);
00672 
00673         res_setup->begin          = get_bits(gb, 24);
00674         res_setup->end            = get_bits(gb, 24);
00675         res_setup->partition_size = get_bits(gb, 24) + 1;
00676         /* Validations to prevent a buffer overflow later. */
00677         if (res_setup->begin>res_setup->end ||
00678             res_setup->end > (res_setup->type == 2 ? vc->avccontext->channels : 1) * vc->blocksize[1] / 2 ||
00679             (res_setup->end-res_setup->begin) / res_setup->partition_size > V_MAX_PARTITIONS) {
00680             av_log(vc->avccontext, AV_LOG_ERROR,
00681                    "partition out of bounds: type, begin, end, size, blocksize: %"PRIu16", %"PRIu32", %"PRIu32", %u, %"PRIu32"\n",
00682                    res_setup->type, res_setup->begin, res_setup->end,
00683                    res_setup->partition_size, vc->blocksize[1] / 2);
00684             return AVERROR_INVALIDDATA;
00685         }
00686 
00687         res_setup->classifications = get_bits(gb, 6) + 1;
00688         GET_VALIDATED_INDEX(res_setup->classbook, 8, vc->codebook_count)
00689 
00690         res_setup->ptns_to_read =
00691             (res_setup->end - res_setup->begin) / res_setup->partition_size;
00692         res_setup->classifs = av_malloc(res_setup->ptns_to_read *
00693                                         vc->audio_channels *
00694                                         sizeof(*res_setup->classifs));
00695         if (!res_setup->classifs)
00696             return AVERROR(ENOMEM);
00697 
00698         av_dlog(NULL, "    begin %d end %d part.size %d classif.s %d classbook %d \n",
00699                 res_setup->begin, res_setup->end, res_setup->partition_size,
00700                 res_setup->classifications, res_setup->classbook);
00701 
00702         for (j = 0; j < res_setup->classifications; ++j) {
00703             high_bits = 0;
00704             low_bits  = get_bits(gb, 3);
00705             if (get_bits1(gb))
00706                 high_bits = get_bits(gb, 5);
00707             cascade[j] = (high_bits << 3) + low_bits;
00708 
00709             av_dlog(NULL, "     %u class cascade depth: %d\n", j, ilog(cascade[j]));
00710         }
00711 
00712         res_setup->maxpass = 0;
00713         for (j = 0; j < res_setup->classifications; ++j) {
00714             for (k = 0; k < 8; ++k) {
00715                 if (cascade[j]&(1 << k)) {
00716                     GET_VALIDATED_INDEX(res_setup->books[j][k], 8, vc->codebook_count)
00717 
00718                     av_dlog(NULL, "     %u class cascade depth %u book: %d\n",
00719                             j, k, res_setup->books[j][k]);
00720 
00721                     if (k>res_setup->maxpass)
00722                         res_setup->maxpass = k;
00723                 } else {
00724                     res_setup->books[j][k] = -1;
00725                 }
00726             }
00727         }
00728     }
00729     return 0;
00730 }
00731 
00732 // Process mappings part
00733 
00734 static int vorbis_parse_setup_hdr_mappings(vorbis_context *vc)
00735 {
00736     GetBitContext *gb = &vc->gb;
00737     unsigned i, j;
00738 
00739     vc->mapping_count = get_bits(gb, 6)+1;
00740     vc->mappings      = av_mallocz(vc->mapping_count * sizeof(*vc->mappings));
00741 
00742     av_dlog(NULL, " There are %d mappings. \n", vc->mapping_count);
00743 
00744     for (i = 0; i < vc->mapping_count; ++i) {
00745         vorbis_mapping *mapping_setup = &vc->mappings[i];
00746 
00747         if (get_bits(gb, 16)) {
00748             av_log(vc->avccontext, AV_LOG_ERROR, "Other mappings than type 0 are not compliant with the Vorbis I specification. \n");
00749             return AVERROR_INVALIDDATA;
00750         }
00751         if (get_bits1(gb)) {
00752             mapping_setup->submaps = get_bits(gb, 4) + 1;
00753         } else {
00754             mapping_setup->submaps = 1;
00755         }
00756 
00757         if (get_bits1(gb)) {
00758             mapping_setup->coupling_steps = get_bits(gb, 8) + 1;
00759             mapping_setup->magnitude      = av_mallocz(mapping_setup->coupling_steps *
00760                                                        sizeof(*mapping_setup->magnitude));
00761             mapping_setup->angle          = av_mallocz(mapping_setup->coupling_steps *
00762                                                        sizeof(*mapping_setup->angle));
00763             for (j = 0; j < mapping_setup->coupling_steps; ++j) {
00764                 GET_VALIDATED_INDEX(mapping_setup->magnitude[j], ilog(vc->audio_channels - 1), vc->audio_channels)
00765                 GET_VALIDATED_INDEX(mapping_setup->angle[j],     ilog(vc->audio_channels - 1), vc->audio_channels)
00766             }
00767         } else {
00768             mapping_setup->coupling_steps = 0;
00769         }
00770 
00771         av_dlog(NULL, "   %u mapping coupling steps: %d\n",
00772                 i, mapping_setup->coupling_steps);
00773 
00774         if (get_bits(gb, 2)) {
00775             av_log(vc->avccontext, AV_LOG_ERROR, "%u. mapping setup data invalid.\n", i);
00776             return AVERROR_INVALIDDATA; // following spec.
00777         }
00778 
00779         if (mapping_setup->submaps>1) {
00780             mapping_setup->mux = av_mallocz(vc->audio_channels *
00781                                             sizeof(*mapping_setup->mux));
00782             for (j = 0; j < vc->audio_channels; ++j)
00783                 mapping_setup->mux[j] = get_bits(gb, 4);
00784         }
00785 
00786         for (j = 0; j < mapping_setup->submaps; ++j) {
00787             skip_bits(gb, 8); // FIXME check?
00788             GET_VALIDATED_INDEX(mapping_setup->submap_floor[j],   8, vc->floor_count)
00789             GET_VALIDATED_INDEX(mapping_setup->submap_residue[j], 8, vc->residue_count)
00790 
00791             av_dlog(NULL, "   %u mapping %u submap : floor %d, residue %d\n", i, j,
00792                     mapping_setup->submap_floor[j],
00793                     mapping_setup->submap_residue[j]);
00794         }
00795     }
00796     return 0;
00797 }
00798 
00799 // Process modes part
00800 
00801 static void create_map(vorbis_context *vc, unsigned floor_number)
00802 {
00803     vorbis_floor *floors = vc->floors;
00804     vorbis_floor0 *vf;
00805     int idx;
00806     int blockflag, n;
00807     int32_t *map;
00808 
00809     for (blockflag = 0; blockflag < 2; ++blockflag) {
00810         n = vc->blocksize[blockflag] / 2;
00811         floors[floor_number].data.t0.map[blockflag] =
00812             av_malloc((n + 1) * sizeof(int32_t)); // n + sentinel
00813 
00814         map =  floors[floor_number].data.t0.map[blockflag];
00815         vf  = &floors[floor_number].data.t0;
00816 
00817         for (idx = 0; idx < n; ++idx) {
00818             map[idx] = floor(BARK((vf->rate * idx) / (2.0f * n)) *
00819                              (vf->bark_map_size / BARK(vf->rate / 2.0f)));
00820             if (vf->bark_map_size-1 < map[idx])
00821                 map[idx] = vf->bark_map_size - 1;
00822         }
00823         map[n] = -1;
00824         vf->map_size[blockflag] = n;
00825     }
00826 
00827     for (idx = 0; idx <= n; ++idx) {
00828         av_dlog(NULL, "floor0 map: map at pos %d is %d\n", idx, map[idx]);
00829     }
00830 }
00831 
00832 static int vorbis_parse_setup_hdr_modes(vorbis_context *vc)
00833 {
00834     GetBitContext *gb = &vc->gb;
00835     unsigned i;
00836 
00837     vc->mode_count = get_bits(gb, 6) + 1;
00838     vc->modes      = av_mallocz(vc->mode_count * sizeof(*vc->modes));
00839 
00840     av_dlog(NULL, " There are %d modes.\n", vc->mode_count);
00841 
00842     for (i = 0; i < vc->mode_count; ++i) {
00843         vorbis_mode *mode_setup = &vc->modes[i];
00844 
00845         mode_setup->blockflag     = get_bits1(gb);
00846         mode_setup->windowtype    = get_bits(gb, 16); //FIXME check
00847         mode_setup->transformtype = get_bits(gb, 16); //FIXME check
00848         GET_VALIDATED_INDEX(mode_setup->mapping, 8, vc->mapping_count);
00849 
00850         av_dlog(NULL, " %u mode: blockflag %d, windowtype %d, transformtype %d, mapping %d\n",
00851                 i, mode_setup->blockflag, mode_setup->windowtype,
00852                 mode_setup->transformtype, mode_setup->mapping);
00853     }
00854     return 0;
00855 }
00856 
00857 // Process the whole setup header using the functions above
00858 
00859 static int vorbis_parse_setup_hdr(vorbis_context *vc)
00860 {
00861     GetBitContext *gb = &vc->gb;
00862     int ret;
00863 
00864     if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
00865         (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
00866         (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
00867         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (no vorbis signature). \n");
00868         return AVERROR_INVALIDDATA;
00869     }
00870 
00871     if ((ret = vorbis_parse_setup_hdr_codebooks(vc))) {
00872         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (codebooks). \n");
00873         return ret;
00874     }
00875     if ((ret = vorbis_parse_setup_hdr_tdtransforms(vc))) {
00876         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (time domain transforms). \n");
00877         return ret;
00878     }
00879     if ((ret = vorbis_parse_setup_hdr_floors(vc))) {
00880         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (floors). \n");
00881         return ret;
00882     }
00883     if ((ret = vorbis_parse_setup_hdr_residues(vc))) {
00884         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (residues). \n");
00885         return ret;
00886     }
00887     if ((ret = vorbis_parse_setup_hdr_mappings(vc))) {
00888         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (mappings). \n");
00889         return ret;
00890     }
00891     if ((ret = vorbis_parse_setup_hdr_modes(vc))) {
00892         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (modes). \n");
00893         return ret;
00894     }
00895     if (!get_bits1(gb)) {
00896         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis setup header packet corrupt (framing flag). \n");
00897         return AVERROR_INVALIDDATA; // framing flag bit unset error
00898     }
00899 
00900     return 0;
00901 }
00902 
00903 // Process the identification header
00904 
00905 static int vorbis_parse_id_hdr(vorbis_context *vc)
00906 {
00907     GetBitContext *gb = &vc->gb;
00908     unsigned bl0, bl1;
00909 
00910     if ((get_bits(gb, 8) != 'v') || (get_bits(gb, 8) != 'o') ||
00911         (get_bits(gb, 8) != 'r') || (get_bits(gb, 8) != 'b') ||
00912         (get_bits(gb, 8) != 'i') || (get_bits(gb, 8) != 's')) {
00913         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (no vorbis signature). \n");
00914         return AVERROR_INVALIDDATA;
00915     }
00916 
00917     vc->version        = get_bits_long(gb, 32);    //FIXME check 0
00918     vc->audio_channels = get_bits(gb, 8);
00919     if (vc->audio_channels <= 0) {
00920         av_log(vc->avccontext, AV_LOG_ERROR, "Invalid number of channels\n");
00921         return AVERROR_INVALIDDATA;
00922     }
00923     vc->audio_samplerate = get_bits_long(gb, 32);
00924     if (vc->audio_samplerate <= 0) {
00925         av_log(vc->avccontext, AV_LOG_ERROR, "Invalid samplerate\n");
00926         return AVERROR_INVALIDDATA;
00927     }
00928     vc->bitrate_maximum = get_bits_long(gb, 32);
00929     vc->bitrate_nominal = get_bits_long(gb, 32);
00930     vc->bitrate_minimum = get_bits_long(gb, 32);
00931     bl0 = get_bits(gb, 4);
00932     bl1 = get_bits(gb, 4);
00933     vc->blocksize[0] = (1 << bl0);
00934     vc->blocksize[1] = (1 << bl1);
00935     if (bl0 > 13 || bl0 < 6 || bl1 > 13 || bl1 < 6 || bl1 < bl0) {
00936         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (illegal blocksize). \n");
00937         return AVERROR_INVALIDDATA;
00938     }
00939     vc->win[0] = ff_vorbis_vwin[bl0 - 6];
00940     vc->win[1] = ff_vorbis_vwin[bl1 - 6];
00941 
00942     if ((get_bits1(gb)) == 0) {
00943         av_log(vc->avccontext, AV_LOG_ERROR, " Vorbis id header packet corrupt (framing flag not set). \n");
00944         return AVERROR_INVALIDDATA;
00945     }
00946 
00947     vc->channel_residues =  av_malloc((vc->blocksize[1]  / 2) * vc->audio_channels * sizeof(*vc->channel_residues));
00948     vc->channel_floors   =  av_malloc((vc->blocksize[1]  / 2) * vc->audio_channels * sizeof(*vc->channel_floors));
00949     vc->saved            =  av_mallocz((vc->blocksize[1] / 4) * vc->audio_channels * sizeof(*vc->saved));
00950     vc->previous_window  = 0;
00951 
00952     ff_mdct_init(&vc->mdct[0], bl0, 1, -vc->scale_bias);
00953     ff_mdct_init(&vc->mdct[1], bl1, 1, -vc->scale_bias);
00954 
00955     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 ",
00956             vc->version, vc->audio_channels, vc->audio_samplerate, vc->bitrate_maximum, vc->bitrate_nominal, vc->bitrate_minimum, vc->blocksize[0], vc->blocksize[1]);
00957 
00958 /*
00959     BLK = vc->blocksize[0];
00960     for (i = 0; i < BLK / 2; ++i) {
00961         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)));
00962     }
00963 */
00964 
00965     return 0;
00966 }
00967 
00968 // Process the extradata using the functions above (identification header, setup header)
00969 
00970 static av_cold int vorbis_decode_init(AVCodecContext *avccontext)
00971 {
00972     vorbis_context *vc = avccontext->priv_data;
00973     uint8_t *headers   = avccontext->extradata;
00974     int headers_len    = avccontext->extradata_size;
00975     uint8_t *header_start[3];
00976     int header_len[3];
00977     GetBitContext *gb = &vc->gb;
00978     int hdr_type, ret;
00979 
00980     vc->avccontext = avccontext;
00981     dsputil_init(&vc->dsp, avccontext);
00982     ff_fmt_convert_init(&vc->fmt_conv, avccontext);
00983 
00984     if (avccontext->request_sample_fmt == AV_SAMPLE_FMT_FLT) {
00985         avccontext->sample_fmt = AV_SAMPLE_FMT_FLT;
00986         vc->scale_bias = 1.0f;
00987     } else {
00988         avccontext->sample_fmt = AV_SAMPLE_FMT_S16;
00989         vc->scale_bias = 32768.0f;
00990     }
00991 
00992     if (!headers_len) {
00993         av_log(avccontext, AV_LOG_ERROR, "Extradata missing.\n");
00994         return AVERROR_INVALIDDATA;
00995     }
00996 
00997     if ((ret = avpriv_split_xiph_headers(headers, headers_len, 30, header_start, header_len)) < 0) {
00998         av_log(avccontext, AV_LOG_ERROR, "Extradata corrupt.\n");
00999         return ret;
01000     }
01001 
01002     init_get_bits(gb, header_start[0], header_len[0]*8);
01003     hdr_type = get_bits(gb, 8);
01004     if (hdr_type != 1) {
01005         av_log(avccontext, AV_LOG_ERROR, "First header is not the id header.\n");
01006         return AVERROR_INVALIDDATA;
01007     }
01008     if ((ret = vorbis_parse_id_hdr(vc))) {
01009         av_log(avccontext, AV_LOG_ERROR, "Id header corrupt.\n");
01010         vorbis_free(vc);
01011         return ret;
01012     }
01013 
01014     init_get_bits(gb, header_start[2], header_len[2]*8);
01015     hdr_type = get_bits(gb, 8);
01016     if (hdr_type != 5) {
01017         av_log(avccontext, AV_LOG_ERROR, "Third header is not the setup header.\n");
01018         vorbis_free(vc);
01019         return AVERROR_INVALIDDATA;
01020     }
01021     if ((ret = vorbis_parse_setup_hdr(vc))) {
01022         av_log(avccontext, AV_LOG_ERROR, "Setup header corrupt.\n");
01023         vorbis_free(vc);
01024         return ret;
01025     }
01026 
01027     if (vc->audio_channels > 8)
01028         avccontext->channel_layout = 0;
01029     else
01030         avccontext->channel_layout = ff_vorbis_channel_layouts[vc->audio_channels - 1];
01031 
01032     avccontext->channels    = vc->audio_channels;
01033     avccontext->sample_rate = vc->audio_samplerate;
01034     avccontext->frame_size  = FFMIN(vc->blocksize[0], vc->blocksize[1]) >> 2;
01035 
01036     avcodec_get_frame_defaults(&vc->frame);
01037     avccontext->coded_frame = &vc->frame;
01038 
01039     return 0;
01040 }
01041 
01042 // Decode audiopackets -------------------------------------------------
01043 
01044 // Read and decode floor
01045 
01046 static int vorbis_floor0_decode(vorbis_context *vc,
01047                                 vorbis_floor_data *vfu, float *vec)
01048 {
01049     vorbis_floor0 *vf = &vfu->t0;
01050     float *lsp = vf->lsp;
01051     unsigned amplitude, book_idx;
01052     unsigned blockflag = vc->modes[vc->mode_number].blockflag;
01053 
01054     amplitude = get_bits(&vc->gb, vf->amplitude_bits);
01055     if (amplitude > 0) {
01056         float last = 0;
01057         unsigned idx, lsp_len = 0;
01058         vorbis_codebook codebook;
01059 
01060         book_idx = get_bits(&vc->gb, ilog(vf->num_books));
01061         if (book_idx >= vf->num_books) {
01062             av_log(vc->avccontext, AV_LOG_ERROR,
01063                     "floor0 dec: booknumber too high!\n");
01064             book_idx =  0;
01065         }
01066         av_dlog(NULL, "floor0 dec: booknumber: %u\n", book_idx);
01067         codebook = vc->codebooks[vf->book_list[book_idx]];
01068         /* Invalid codebook! */
01069         if (!codebook.codevectors)
01070             return AVERROR_INVALIDDATA;
01071 
01072         while (lsp_len<vf->order) {
01073             int vec_off;
01074 
01075             av_dlog(NULL, "floor0 dec: book dimension: %d\n", codebook.dimensions);
01076             av_dlog(NULL, "floor0 dec: maximum depth: %d\n", codebook.maxdepth);
01077             /* read temp vector */
01078             vec_off = get_vlc2(&vc->gb, codebook.vlc.table,
01079                                codebook.nb_bits, codebook.maxdepth)
01080                       * codebook.dimensions;
01081             av_dlog(NULL, "floor0 dec: vector offset: %d\n", vec_off);
01082             /* copy each vector component and add last to it */
01083             for (idx = 0; idx < codebook.dimensions; ++idx)
01084                 lsp[lsp_len+idx] = codebook.codevectors[vec_off+idx] + last;
01085             last = lsp[lsp_len+idx-1]; /* set last to last vector component */
01086 
01087             lsp_len += codebook.dimensions;
01088         }
01089         /* DEBUG: output lsp coeffs */
01090         {
01091             int idx;
01092             for (idx = 0; idx < lsp_len; ++idx)
01093                 av_dlog(NULL, "floor0 dec: coeff at %d is %f\n", idx, lsp[idx]);
01094         }
01095 
01096         /* synthesize floor output vector */
01097         {
01098             int i;
01099             int order = vf->order;
01100             float wstep = M_PI / vf->bark_map_size;
01101 
01102             for (i = 0; i < order; i++)
01103                 lsp[i] = 2.0f * cos(lsp[i]);
01104 
01105             av_dlog(NULL, "floor0 synth: map_size = %"PRIu32"; m = %d; wstep = %f\n",
01106                     vf->map_size[blockflag], order, wstep);
01107 
01108             i = 0;
01109             while (i < vf->map_size[blockflag]) {
01110                 int j, iter_cond = vf->map[blockflag][i];
01111                 float p = 0.5f;
01112                 float q = 0.5f;
01113                 float two_cos_w = 2.0f * cos(wstep * iter_cond); // needed all times
01114 
01115                 /* similar part for the q and p products */
01116                 for (j = 0; j + 1 < order; j += 2) {
01117                     q *= lsp[j]     - two_cos_w;
01118                     p *= lsp[j + 1] - two_cos_w;
01119                 }
01120                 if (j == order) { // even order
01121                     p *= p * (2.0f - two_cos_w);
01122                     q *= q * (2.0f + two_cos_w);
01123                 } else { // odd order
01124                     q *= two_cos_w-lsp[j]; // one more time for q
01125 
01126                     /* final step and square */
01127                     p *= p * (4.f - two_cos_w * two_cos_w);
01128                     q *= q;
01129                 }
01130 
01131                 /* calculate linear floor value */
01132                 q = exp((((amplitude*vf->amplitude_offset) /
01133                           (((1 << vf->amplitude_bits) - 1) * sqrt(p + q)))
01134                          - vf->amplitude_offset) * .11512925f);
01135 
01136                 /* fill vector */
01137                 do {
01138                     vec[i] = q; ++i;
01139                 } while (vf->map[blockflag][i] == iter_cond);
01140             }
01141         }
01142     } else {
01143         /* this channel is unused */
01144         return 1;
01145     }
01146 
01147     av_dlog(NULL, " Floor0 decoded\n");
01148 
01149     return 0;
01150 }
01151 
01152 static int vorbis_floor1_decode(vorbis_context *vc,
01153                                 vorbis_floor_data *vfu, float *vec)
01154 {
01155     vorbis_floor1 *vf = &vfu->t1;
01156     GetBitContext *gb = &vc->gb;
01157     uint16_t range_v[4] = { 256, 128, 86, 64 };
01158     unsigned range = range_v[vf->multiplier - 1];
01159     uint16_t floor1_Y[258];
01160     uint16_t floor1_Y_final[258];
01161     int floor1_flag[258];
01162     unsigned class, cdim, cbits, csub, cval, offset, i, j;
01163     int book, adx, ady, dy, off, predicted, err;
01164 
01165 
01166     if (!get_bits1(gb)) // silence
01167         return 1;
01168 
01169 // Read values (or differences) for the floor's points
01170 
01171     floor1_Y[0] = get_bits(gb, ilog(range - 1));
01172     floor1_Y[1] = get_bits(gb, ilog(range - 1));
01173 
01174     av_dlog(NULL, "floor 0 Y %d floor 1 Y %d \n", floor1_Y[0], floor1_Y[1]);
01175 
01176     offset = 2;
01177     for (i = 0; i < vf->partitions; ++i) {
01178         class = vf->partition_class[i];
01179         cdim   = vf->class_dimensions[class];
01180         cbits  = vf->class_subclasses[class];
01181         csub = (1 << cbits) - 1;
01182         cval = 0;
01183 
01184         av_dlog(NULL, "Cbits %u\n", cbits);
01185 
01186         if (cbits) // this reads all subclasses for this partition's class
01187             cval = get_vlc2(gb, vc->codebooks[vf->class_masterbook[class]].vlc.table,
01188                             vc->codebooks[vf->class_masterbook[class]].nb_bits, 3);
01189 
01190         for (j = 0; j < cdim; ++j) {
01191             book = vf->subclass_books[class][cval & csub];
01192 
01193             av_dlog(NULL, "book %d Cbits %u cval %u  bits:%d\n",
01194                     book, cbits, cval, get_bits_count(gb));
01195 
01196             cval = cval >> cbits;
01197             if (book > -1) {
01198                 floor1_Y[offset+j] = get_vlc2(gb, vc->codebooks[book].vlc.table,
01199                 vc->codebooks[book].nb_bits, 3);
01200             } else {
01201                 floor1_Y[offset+j] = 0;
01202             }
01203 
01204             av_dlog(NULL, " floor(%d) = %d \n",
01205                     vf->list[offset+j].x, floor1_Y[offset+j]);
01206         }
01207         offset+=cdim;
01208     }
01209 
01210 // Amplitude calculation from the differences
01211 
01212     floor1_flag[0] = 1;
01213     floor1_flag[1] = 1;
01214     floor1_Y_final[0] = floor1_Y[0];
01215     floor1_Y_final[1] = floor1_Y[1];
01216 
01217     for (i = 2; i < vf->x_list_dim; ++i) {
01218         unsigned val, highroom, lowroom, room, high_neigh_offs, low_neigh_offs;
01219 
01220         low_neigh_offs  = vf->list[i].low;
01221         high_neigh_offs = vf->list[i].high;
01222         dy  = floor1_Y_final[high_neigh_offs] - floor1_Y_final[low_neigh_offs];  // render_point begin
01223         adx = vf->list[high_neigh_offs].x - vf->list[low_neigh_offs].x;
01224         ady = FFABS(dy);
01225         err = ady * (vf->list[i].x - vf->list[low_neigh_offs].x);
01226         off = err / adx;
01227         if (dy < 0) {
01228             predicted = floor1_Y_final[low_neigh_offs] - off;
01229         } else {
01230             predicted = floor1_Y_final[low_neigh_offs] + off;
01231         } // render_point end
01232 
01233         val = floor1_Y[i];
01234         highroom = range-predicted;
01235         lowroom  = predicted;
01236         if (highroom < lowroom) {
01237             room = highroom * 2;
01238         } else {
01239             room = lowroom * 2;   // SPEC mispelling
01240         }
01241         if (val) {
01242             floor1_flag[low_neigh_offs]  = 1;
01243             floor1_flag[high_neigh_offs] = 1;
01244             floor1_flag[i]               = 1;
01245             if (val >= room) {
01246                 if (highroom > lowroom) {
01247                     floor1_Y_final[i] = av_clip_uint16(val - lowroom + predicted);
01248                 } else {
01249                     floor1_Y_final[i] = av_clip_uint16(predicted - val + highroom - 1);
01250                 }
01251             } else {
01252                 if (val & 1) {
01253                     floor1_Y_final[i] = av_clip_uint16(predicted - (val + 1) / 2);
01254                 } else {
01255                     floor1_Y_final[i] = av_clip_uint16(predicted + val / 2);
01256                 }
01257             }
01258         } else {
01259             floor1_flag[i]    = 0;
01260             floor1_Y_final[i] = av_clip_uint16(predicted);
01261         }
01262 
01263         av_dlog(NULL, " Decoded floor(%d) = %u / val %u\n",
01264                 vf->list[i].x, floor1_Y_final[i], val);
01265     }
01266 
01267 // Curve synth - connect the calculated dots and convert from dB scale FIXME optimize ?
01268 
01269     ff_vorbis_floor1_render_list(vf->list, vf->x_list_dim, floor1_Y_final, floor1_flag, vf->multiplier, vec, vf->list[1].x);
01270 
01271     av_dlog(NULL, " Floor decoded\n");
01272 
01273     return 0;
01274 }
01275 
01276 // Read and decode residue
01277 
01278 static av_always_inline int vorbis_residue_decode_internal(vorbis_context *vc,
01279                                                            vorbis_residue *vr,
01280                                                            unsigned ch,
01281                                                            uint8_t *do_not_decode,
01282                                                            float *vec,
01283                                                            unsigned vlen,
01284                                                            unsigned ch_left,
01285                                                            int vr_type)
01286 {
01287     GetBitContext *gb = &vc->gb;
01288     unsigned c_p_c        = vc->codebooks[vr->classbook].dimensions;
01289     unsigned ptns_to_read = vr->ptns_to_read;
01290     uint8_t *classifs = vr->classifs;
01291     unsigned pass, ch_used, i, j, k, l;
01292     unsigned max_output = (ch - 1) * vlen;
01293 
01294     if (vr_type == 2) {
01295         for (j = 1; j < ch; ++j)
01296             do_not_decode[0] &= do_not_decode[j];  // FIXME - clobbering input
01297         if (do_not_decode[0])
01298             return 0;
01299         ch_used = 1;
01300         max_output += vr->end / ch;
01301     } else {
01302         ch_used = ch;
01303         max_output += vr->end;
01304     }
01305 
01306     if (max_output > ch_left * vlen) {
01307         av_log(vc->avccontext, AV_LOG_ERROR, "Insufficient output buffer\n");
01308         return -1;
01309     }
01310 
01311     av_dlog(NULL, " residue type 0/1/2 decode begin, ch: %d  cpc %d  \n", ch, c_p_c);
01312 
01313     for (pass = 0; pass <= vr->maxpass; ++pass) { // FIXME OPTIMIZE?
01314         uint16_t voffset, partition_count, j_times_ptns_to_read;
01315 
01316         voffset = vr->begin;
01317         for (partition_count = 0; partition_count < ptns_to_read;) {  // SPEC        error
01318             if (!pass) {
01319                 unsigned inverse_class = ff_inverse[vr->classifications];
01320                 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
01321                     if (!do_not_decode[j]) {
01322                         unsigned temp = get_vlc2(gb, vc->codebooks[vr->classbook].vlc.table,
01323                                                  vc->codebooks[vr->classbook].nb_bits, 3);
01324 
01325                         av_dlog(NULL, "Classword: %u\n", temp);
01326 
01327                         assert(vr->classifications > 1 && temp <= 65536); //needed for inverse[]
01328                         for (i = 0; i < c_p_c; ++i) {
01329                             unsigned temp2;
01330 
01331                             temp2 = (((uint64_t)temp) * inverse_class) >> 32;
01332                             if (partition_count + c_p_c - 1 - i < ptns_to_read)
01333                                 classifs[j_times_ptns_to_read + partition_count + c_p_c - 1 - i] = temp - temp2 * vr->classifications;
01334                             temp = temp2;
01335                         }
01336                     }
01337                     j_times_ptns_to_read += ptns_to_read;
01338                 }
01339             }
01340             for (i = 0; (i < c_p_c) && (partition_count < ptns_to_read); ++i) {
01341                 for (j_times_ptns_to_read = 0, j = 0; j < ch_used; ++j) {
01342                     unsigned voffs;
01343 
01344                     if (!do_not_decode[j]) {
01345                         unsigned vqclass = classifs[j_times_ptns_to_read + partition_count];
01346                         int vqbook  = vr->books[vqclass][pass];
01347 
01348                         if (vqbook >= 0 && vc->codebooks[vqbook].codevectors) {
01349                             unsigned coffs;
01350                             unsigned dim  = vc->codebooks[vqbook].dimensions;
01351                             unsigned step = dim == 1 ? vr->partition_size
01352                                                      : FASTDIV(vr->partition_size, dim);
01353                             vorbis_codebook codebook = vc->codebooks[vqbook];
01354 
01355                             if (vr_type == 0) {
01356 
01357                                 voffs = voffset+j*vlen;
01358                                 for (k = 0; k < step; ++k) {
01359                                     coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01360                                     for (l = 0; l < dim; ++l)
01361                                         vec[voffs + k + l * step] += codebook.codevectors[coffs + l];  // FPMATH
01362                                 }
01363                             } else if (vr_type == 1) {
01364                                 voffs = voffset + j * vlen;
01365                                 for (k = 0; k < step; ++k) {
01366                                     coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01367                                     for (l = 0; l < dim; ++l, ++voffs) {
01368                                         vec[voffs]+=codebook.codevectors[coffs+l];  // FPMATH
01369 
01370                                         av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d  \n",
01371                                                 pass, voffs, vec[voffs], codebook.codevectors[coffs+l], coffs);
01372                                     }
01373                                 }
01374                             } else if (vr_type == 2 && ch == 2 && (voffset & 1) == 0 && (dim & 1) == 0) { // most frequent case optimized
01375                                 voffs = voffset >> 1;
01376 
01377                                 if (dim == 2) {
01378                                     for (k = 0; k < step; ++k) {
01379                                         coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 2;
01380                                         vec[voffs + k       ] += codebook.codevectors[coffs    ];  // FPMATH
01381                                         vec[voffs + k + vlen] += codebook.codevectors[coffs + 1];  // FPMATH
01382                                     }
01383                                 } else if (dim == 4) {
01384                                     for (k = 0; k < step; ++k, voffs += 2) {
01385                                         coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * 4;
01386                                         vec[voffs           ] += codebook.codevectors[coffs    ];  // FPMATH
01387                                         vec[voffs + 1       ] += codebook.codevectors[coffs + 2];  // FPMATH
01388                                         vec[voffs + vlen    ] += codebook.codevectors[coffs + 1];  // FPMATH
01389                                         vec[voffs + vlen + 1] += codebook.codevectors[coffs + 3];  // FPMATH
01390                                     }
01391                                 } else
01392                                 for (k = 0; k < step; ++k) {
01393                                     coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01394                                     for (l = 0; l < dim; l += 2, voffs++) {
01395                                         vec[voffs       ] += codebook.codevectors[coffs + l    ];  // FPMATH
01396                                         vec[voffs + vlen] += codebook.codevectors[coffs + l + 1];  // FPMATH
01397 
01398                                         av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d  \n",
01399                                                 pass, voffset / ch + (voffs % ch) * vlen,
01400                                                 vec[voffset / ch + (voffs % ch) * vlen],
01401                                                 codebook.codevectors[coffs + l], coffs, l);
01402                                     }
01403                                 }
01404 
01405                             } else if (vr_type == 2) {
01406                                 voffs = voffset;
01407 
01408                                 for (k = 0; k < step; ++k) {
01409                                     coffs = get_vlc2(gb, codebook.vlc.table, codebook.nb_bits, 3) * dim;
01410                                     for (l = 0; l < dim; ++l, ++voffs) {
01411                                         vec[voffs / ch + (voffs % ch) * vlen] += codebook.codevectors[coffs + l];  // FPMATH FIXME use if and counter instead of / and %
01412 
01413                                         av_dlog(NULL, " pass %d offs: %d curr: %f change: %f cv offs.: %d+%d  \n",
01414                                                 pass, voffset / ch + (voffs % ch) * vlen,
01415                                                 vec[voffset / ch + (voffs % ch) * vlen],
01416                                                 codebook.codevectors[coffs + l], coffs, l);
01417                                     }
01418                                 }
01419                             }
01420                         }
01421                     }
01422                     j_times_ptns_to_read += ptns_to_read;
01423                 }
01424                 ++partition_count;
01425                 voffset += vr->partition_size;
01426             }
01427         }
01428     }
01429     return 0;
01430 }
01431 
01432 static inline int vorbis_residue_decode(vorbis_context *vc, vorbis_residue *vr,
01433                                         unsigned ch,
01434                                         uint8_t *do_not_decode,
01435                                         float *vec, unsigned vlen,
01436                                         unsigned ch_left)
01437 {
01438     if (vr->type == 2)
01439         return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 2);
01440     else if (vr->type == 1)
01441         return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 1);
01442     else if (vr->type == 0)
01443         return vorbis_residue_decode_internal(vc, vr, ch, do_not_decode, vec, vlen, ch_left, 0);
01444     else {
01445         av_log(vc->avccontext, AV_LOG_ERROR, " Invalid residue type while residue decode?! \n");
01446         return AVERROR_INVALIDDATA;
01447     }
01448 }
01449 
01450 void vorbis_inverse_coupling(float *mag, float *ang, int blocksize)
01451 {
01452     int i;
01453     for (i = 0;  i < blocksize;  i++) {
01454         if (mag[i] > 0.0) {
01455             if (ang[i] > 0.0) {
01456                 ang[i] = mag[i] - ang[i];
01457             } else {
01458                 float temp = ang[i];
01459                 ang[i]     = mag[i];
01460                 mag[i]    += temp;
01461             }
01462         } else {
01463             if (ang[i] > 0.0) {
01464                 ang[i] += mag[i];
01465             } else {
01466                 float temp = ang[i];
01467                 ang[i]     = mag[i];
01468                 mag[i]    -= temp;
01469             }
01470         }
01471     }
01472 }
01473 
01474 // Decode the audio packet using the functions above
01475 
01476 static int vorbis_parse_audio_packet(vorbis_context *vc)
01477 {
01478     GetBitContext *gb = &vc->gb;
01479     FFTContext *mdct;
01480     unsigned previous_window = vc->previous_window;
01481     unsigned mode_number, blockflag, blocksize;
01482     int i, j;
01483     uint8_t no_residue[255];
01484     uint8_t do_not_decode[255];
01485     vorbis_mapping *mapping;
01486     float *ch_res_ptr   = vc->channel_residues;
01487     float *ch_floor_ptr = vc->channel_floors;
01488     uint8_t res_chan[255];
01489     unsigned res_num = 0;
01490     int retlen  = 0;
01491     unsigned ch_left = vc->audio_channels;
01492     unsigned vlen;
01493 
01494     if (get_bits1(gb)) {
01495         av_log(vc->avccontext, AV_LOG_ERROR, "Not a Vorbis I audio packet.\n");
01496         return AVERROR_INVALIDDATA; // packet type not audio
01497     }
01498 
01499     if (vc->mode_count == 1) {
01500         mode_number = 0;
01501     } else {
01502         GET_VALIDATED_INDEX(mode_number, ilog(vc->mode_count-1), vc->mode_count)
01503     }
01504     vc->mode_number = mode_number;
01505     mapping = &vc->mappings[vc->modes[mode_number].mapping];
01506 
01507     av_dlog(NULL, " Mode number: %u , mapping: %d , blocktype %d\n", mode_number,
01508             vc->modes[mode_number].mapping, vc->modes[mode_number].blockflag);
01509 
01510     blockflag = vc->modes[mode_number].blockflag;
01511     blocksize = vc->blocksize[blockflag];
01512     vlen = blocksize / 2;
01513     if (blockflag)
01514         skip_bits(gb, 2); // previous_window, next_window
01515 
01516     memset(ch_res_ptr,   0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
01517     memset(ch_floor_ptr, 0, sizeof(float) * vc->audio_channels * vlen); //FIXME can this be removed ?
01518 
01519 // Decode floor
01520 
01521     for (i = 0; i < vc->audio_channels; ++i) {
01522         vorbis_floor *floor;
01523         int ret;
01524         if (mapping->submaps > 1) {
01525             floor = &vc->floors[mapping->submap_floor[mapping->mux[i]]];
01526         } else {
01527             floor = &vc->floors[mapping->submap_floor[0]];
01528         }
01529 
01530         ret = floor->decode(vc, &floor->data, ch_floor_ptr);
01531 
01532         if (ret < 0) {
01533             av_log(vc->avccontext, AV_LOG_ERROR, "Invalid codebook in vorbis_floor_decode.\n");
01534             return AVERROR_INVALIDDATA;
01535         }
01536         no_residue[i] = ret;
01537         ch_floor_ptr += vlen;
01538     }
01539 
01540 // Nonzero vector propagate
01541 
01542     for (i = mapping->coupling_steps - 1; i >= 0; --i) {
01543         if (!(no_residue[mapping->magnitude[i]] & no_residue[mapping->angle[i]])) {
01544             no_residue[mapping->magnitude[i]] = 0;
01545             no_residue[mapping->angle[i]]     = 0;
01546         }
01547     }
01548 
01549 // Decode residue
01550 
01551     for (i = 0; i < mapping->submaps; ++i) {
01552         vorbis_residue *residue;
01553         unsigned ch = 0;
01554         int ret;
01555 
01556         for (j = 0; j < vc->audio_channels; ++j) {
01557             if ((mapping->submaps == 1) || (i == mapping->mux[j])) {
01558                 res_chan[j] = res_num;
01559                 if (no_residue[j]) {
01560                     do_not_decode[ch] = 1;
01561                 } else {
01562                     do_not_decode[ch] = 0;
01563                 }
01564                 ++ch;
01565                 ++res_num;
01566             }
01567         }
01568         residue = &vc->residues[mapping->submap_residue[i]];
01569         if (ch_left < ch) {
01570             av_log(vc->avccontext, AV_LOG_ERROR, "Too many channels in vorbis_floor_decode.\n");
01571             return -1;
01572         }
01573         if (ch) {
01574             ret = vorbis_residue_decode(vc, residue, ch, do_not_decode, ch_res_ptr, vlen, ch_left);
01575             if (ret < 0)
01576                 return ret;
01577         }
01578 
01579         ch_res_ptr += ch * vlen;
01580         ch_left -= ch;
01581     }
01582 
01583 // Inverse coupling
01584 
01585     for (i = mapping->coupling_steps - 1; i >= 0; --i) { //warning: i has to be signed
01586         float *mag, *ang;
01587 
01588         mag = vc->channel_residues+res_chan[mapping->magnitude[i]] * blocksize / 2;
01589         ang = vc->channel_residues+res_chan[mapping->angle[i]]     * blocksize / 2;
01590         vc->dsp.vorbis_inverse_coupling(mag, ang, blocksize / 2);
01591     }
01592 
01593 // Dotproduct, MDCT
01594 
01595     mdct = &vc->mdct[blockflag];
01596 
01597     for (j = vc->audio_channels-1;j >= 0; j--) {
01598         ch_floor_ptr = vc->channel_floors   + j           * blocksize / 2;
01599         ch_res_ptr   = vc->channel_residues + res_chan[j] * blocksize / 2;
01600         vc->dsp.vector_fmul(ch_floor_ptr, ch_floor_ptr, ch_res_ptr, blocksize / 2);
01601         mdct->imdct_half(mdct, ch_res_ptr, ch_floor_ptr);
01602     }
01603 
01604 // Overlap/add, save data for next overlapping  FPMATH
01605 
01606     retlen = (blocksize + vc->blocksize[previous_window]) / 4;
01607     for (j = 0; j < vc->audio_channels; j++) {
01608         unsigned bs0 = vc->blocksize[0];
01609         unsigned bs1 = vc->blocksize[1];
01610         float *residue    = vc->channel_residues + res_chan[j] * blocksize / 2;
01611         float *saved      = vc->saved + j * bs1 / 4;
01612         float *ret        = vc->channel_floors + j * retlen;
01613         float *buf        = residue;
01614         const float *win  = vc->win[blockflag & previous_window];
01615 
01616         if (blockflag == previous_window) {
01617             vc->dsp.vector_fmul_window(ret, saved, buf, win, blocksize / 4);
01618         } else if (blockflag > previous_window) {
01619             vc->dsp.vector_fmul_window(ret, saved, buf, win, bs0 / 4);
01620             memcpy(ret+bs0/2, buf+bs0/4, ((bs1-bs0)/4) * sizeof(float));
01621         } else {
01622             memcpy(ret, saved, ((bs1 - bs0) / 4) * sizeof(float));
01623             vc->dsp.vector_fmul_window(ret + (bs1 - bs0) / 4, saved + (bs1 - bs0) / 4, buf, win, bs0 / 4);
01624         }
01625         memcpy(saved, buf + blocksize / 4, blocksize / 4 * sizeof(float));
01626     }
01627 
01628     vc->previous_window = blockflag;
01629     return retlen;
01630 }
01631 
01632 // Return the decoded audio packet through the standard api
01633 
01634 static int vorbis_decode_frame(AVCodecContext *avccontext, void *data,
01635                                int *got_frame_ptr, AVPacket *avpkt)
01636 {
01637     const uint8_t *buf = avpkt->data;
01638     int buf_size       = avpkt->size;
01639     vorbis_context *vc = avccontext->priv_data;
01640     GetBitContext *gb = &vc->gb;
01641     const float *channel_ptrs[255];
01642     int i, len, ret;
01643 
01644     av_dlog(NULL, "packet length %d \n", buf_size);
01645 
01646     init_get_bits(gb, buf, buf_size*8);
01647 
01648     if ((len = vorbis_parse_audio_packet(vc)) <= 0)
01649         return len;
01650 
01651     if (!vc->first_frame) {
01652         vc->first_frame = 1;
01653         *got_frame_ptr = 0;
01654         return buf_size;
01655     }
01656 
01657     av_dlog(NULL, "parsed %d bytes %d bits, returned %d samples (*ch*bits) \n",
01658             get_bits_count(gb) / 8, get_bits_count(gb) % 8, len);
01659 
01660     /* get output buffer */
01661     vc->frame.nb_samples = len;
01662     if ((ret = avccontext->get_buffer(avccontext, &vc->frame)) < 0) {
01663         av_log(avccontext, AV_LOG_ERROR, "get_buffer() failed\n");
01664         return ret;
01665     }
01666 
01667     if (vc->audio_channels > 8) {
01668         for (i = 0; i < vc->audio_channels; i++)
01669             channel_ptrs[i] = vc->channel_floors + i * len;
01670     } else {
01671         for (i = 0; i < vc->audio_channels; i++)
01672             channel_ptrs[i] = vc->channel_floors +
01673                               len * ff_vorbis_channel_layout_offsets[vc->audio_channels - 1][i];
01674     }
01675 
01676     if (avccontext->sample_fmt == AV_SAMPLE_FMT_FLT)
01677         vc->fmt_conv.float_interleave((float *)vc->frame.data[0], channel_ptrs,
01678                                       len, vc->audio_channels);
01679     else
01680         vc->fmt_conv.float_to_int16_interleave((int16_t *)vc->frame.data[0],
01681                                                channel_ptrs, len,
01682                                                vc->audio_channels);
01683 
01684     *got_frame_ptr   = 1;
01685     *(AVFrame *)data = vc->frame;
01686 
01687     return buf_size;
01688 }
01689 
01690 // Close decoder
01691 
01692 static av_cold int vorbis_decode_close(AVCodecContext *avccontext)
01693 {
01694     vorbis_context *vc = avccontext->priv_data;
01695 
01696     vorbis_free(vc);
01697 
01698     return 0;
01699 }
01700 
01701 AVCodec ff_vorbis_decoder = {
01702     .name           = "vorbis",
01703     .type           = AVMEDIA_TYPE_AUDIO,
01704     .id             = CODEC_ID_VORBIS,
01705     .priv_data_size = sizeof(vorbis_context),
01706     .init           = vorbis_decode_init,
01707     .close          = vorbis_decode_close,
01708     .decode         = vorbis_decode_frame,
01709     .capabilities   = CODEC_CAP_DR1,
01710     .long_name = NULL_IF_CONFIG_SMALL("Vorbis"),
01711     .channel_layouts = ff_vorbis_channel_layouts,
01712     .sample_fmts = (const enum AVSampleFormat[]) {
01713         AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_NONE
01714     },
01715 };
01716