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