• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/vorbisdec.c

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

Generated on Fri Feb 22 2013 07:24:29 for FFmpeg by  doxygen 1.7.1