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