dbf-halloween2015

annotate libs/vorbis/vorbisenc.c @ 3:c37fe5d8a4ed

windows port
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 01 Nov 2015 06:04:28 +0200
parents
children
rev   line source
nuclear@1 1 /********************************************************************
nuclear@1 2 * *
nuclear@1 3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
nuclear@1 4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
nuclear@1 5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
nuclear@1 6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
nuclear@1 7 * *
nuclear@1 8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
nuclear@1 9 * by the Xiph.Org Foundation http://www.xiph.org/ *
nuclear@1 10 * *
nuclear@1 11 ********************************************************************
nuclear@1 12
nuclear@1 13 function: simple programmatic interface for encoder mode setup
nuclear@1 14 last mod: $Id: vorbisenc.c 17028 2010-03-25 05:22:15Z xiphmont $
nuclear@1 15
nuclear@1 16 ********************************************************************/
nuclear@1 17
nuclear@1 18 #include <stdlib.h>
nuclear@1 19 #include <string.h>
nuclear@1 20 #include <math.h>
nuclear@1 21
nuclear@1 22 #include "vorbis/codec.h"
nuclear@1 23 #include "vorbis/vorbisenc.h"
nuclear@1 24
nuclear@1 25 #include "codec_internal.h"
nuclear@1 26
nuclear@1 27 #include "os.h"
nuclear@1 28 #include "misc.h"
nuclear@1 29
nuclear@1 30 /* careful with this; it's using static array sizing to make managing
nuclear@1 31 all the modes a little less annoying. If we use a residue backend
nuclear@1 32 with > 12 partition types, or a different division of iteration,
nuclear@1 33 this needs to be updated. */
nuclear@1 34 typedef struct {
nuclear@1 35 const static_codebook *books[12][4];
nuclear@1 36 } static_bookblock;
nuclear@1 37
nuclear@1 38 typedef struct {
nuclear@1 39 int res_type;
nuclear@1 40 int limit_type; /* 0 lowpass limited, 1 point stereo limited */
nuclear@1 41 int grouping;
nuclear@1 42 const vorbis_info_residue0 *res;
nuclear@1 43 const static_codebook *book_aux;
nuclear@1 44 const static_codebook *book_aux_managed;
nuclear@1 45 const static_bookblock *books_base;
nuclear@1 46 const static_bookblock *books_base_managed;
nuclear@1 47 } vorbis_residue_template;
nuclear@1 48
nuclear@1 49 typedef struct {
nuclear@1 50 const vorbis_info_mapping0 *map;
nuclear@1 51 const vorbis_residue_template *res;
nuclear@1 52 } vorbis_mapping_template;
nuclear@1 53
nuclear@1 54 typedef struct vp_adjblock{
nuclear@1 55 int block[P_BANDS];
nuclear@1 56 } vp_adjblock;
nuclear@1 57
nuclear@1 58 typedef struct {
nuclear@1 59 int data[NOISE_COMPAND_LEVELS];
nuclear@1 60 } compandblock;
nuclear@1 61
nuclear@1 62 /* high level configuration information for setting things up
nuclear@1 63 step-by-step with the detailed vorbis_encode_ctl interface.
nuclear@1 64 There's a fair amount of redundancy such that interactive setup
nuclear@1 65 does not directly deal with any vorbis_info or codec_setup_info
nuclear@1 66 initialization; it's all stored (until full init) in this highlevel
nuclear@1 67 setup, then flushed out to the real codec setup structs later. */
nuclear@1 68
nuclear@1 69 typedef struct {
nuclear@1 70 int att[P_NOISECURVES];
nuclear@1 71 float boost;
nuclear@1 72 float decay;
nuclear@1 73 } att3;
nuclear@1 74 typedef struct { int data[P_NOISECURVES]; } adj3;
nuclear@1 75
nuclear@1 76 typedef struct {
nuclear@1 77 int pre[PACKETBLOBS];
nuclear@1 78 int post[PACKETBLOBS];
nuclear@1 79 float kHz[PACKETBLOBS];
nuclear@1 80 float lowpasskHz[PACKETBLOBS];
nuclear@1 81 } adj_stereo;
nuclear@1 82
nuclear@1 83 typedef struct {
nuclear@1 84 int lo;
nuclear@1 85 int hi;
nuclear@1 86 int fixed;
nuclear@1 87 } noiseguard;
nuclear@1 88 typedef struct {
nuclear@1 89 int data[P_NOISECURVES][17];
nuclear@1 90 } noise3;
nuclear@1 91
nuclear@1 92 typedef struct {
nuclear@1 93 int mappings;
nuclear@1 94 const double *rate_mapping;
nuclear@1 95 const double *quality_mapping;
nuclear@1 96 int coupling_restriction;
nuclear@1 97 long samplerate_min_restriction;
nuclear@1 98 long samplerate_max_restriction;
nuclear@1 99
nuclear@1 100
nuclear@1 101 const int *blocksize_short;
nuclear@1 102 const int *blocksize_long;
nuclear@1 103
nuclear@1 104 const att3 *psy_tone_masteratt;
nuclear@1 105 const int *psy_tone_0dB;
nuclear@1 106 const int *psy_tone_dBsuppress;
nuclear@1 107
nuclear@1 108 const vp_adjblock *psy_tone_adj_impulse;
nuclear@1 109 const vp_adjblock *psy_tone_adj_long;
nuclear@1 110 const vp_adjblock *psy_tone_adj_other;
nuclear@1 111
nuclear@1 112 const noiseguard *psy_noiseguards;
nuclear@1 113 const noise3 *psy_noise_bias_impulse;
nuclear@1 114 const noise3 *psy_noise_bias_padding;
nuclear@1 115 const noise3 *psy_noise_bias_trans;
nuclear@1 116 const noise3 *psy_noise_bias_long;
nuclear@1 117 const int *psy_noise_dBsuppress;
nuclear@1 118
nuclear@1 119 const compandblock *psy_noise_compand;
nuclear@1 120 const double *psy_noise_compand_short_mapping;
nuclear@1 121 const double *psy_noise_compand_long_mapping;
nuclear@1 122
nuclear@1 123 const int *psy_noise_normal_start[2];
nuclear@1 124 const int *psy_noise_normal_partition[2];
nuclear@1 125 const double *psy_noise_normal_thresh;
nuclear@1 126
nuclear@1 127 const int *psy_ath_float;
nuclear@1 128 const int *psy_ath_abs;
nuclear@1 129
nuclear@1 130 const double *psy_lowpass;
nuclear@1 131
nuclear@1 132 const vorbis_info_psy_global *global_params;
nuclear@1 133 const double *global_mapping;
nuclear@1 134 const adj_stereo *stereo_modes;
nuclear@1 135
nuclear@1 136 const static_codebook *const *const *const floor_books;
nuclear@1 137 const vorbis_info_floor1 *floor_params;
nuclear@1 138 const int floor_mappings;
nuclear@1 139 const int **floor_mapping_list;
nuclear@1 140
nuclear@1 141 const vorbis_mapping_template *maps;
nuclear@1 142 } ve_setup_data_template;
nuclear@1 143
nuclear@1 144 /* a few static coder conventions */
nuclear@1 145 static const vorbis_info_mode _mode_template[2]={
nuclear@1 146 {0,0,0,0},
nuclear@1 147 {1,0,0,1}
nuclear@1 148 };
nuclear@1 149
nuclear@1 150 static const vorbis_info_mapping0 _map_nominal[2]={
nuclear@1 151 {1, {0,0}, {0}, {0}, 1,{0},{1}},
nuclear@1 152 {1, {0,0}, {1}, {1}, 1,{0},{1}}
nuclear@1 153 };
nuclear@1 154
nuclear@1 155 #include "modes/setup_44.h"
nuclear@1 156 #include "modes/setup_44u.h"
nuclear@1 157 #include "modes/setup_44p51.h"
nuclear@1 158 #include "modes/setup_32.h"
nuclear@1 159 #include "modes/setup_8.h"
nuclear@1 160 #include "modes/setup_11.h"
nuclear@1 161 #include "modes/setup_16.h"
nuclear@1 162 #include "modes/setup_22.h"
nuclear@1 163 #include "modes/setup_X.h"
nuclear@1 164
nuclear@1 165 static const ve_setup_data_template *const setup_list[]={
nuclear@1 166 &ve_setup_44_stereo,
nuclear@1 167 &ve_setup_44_51,
nuclear@1 168 &ve_setup_44_uncoupled,
nuclear@1 169
nuclear@1 170 &ve_setup_32_stereo,
nuclear@1 171 &ve_setup_32_uncoupled,
nuclear@1 172
nuclear@1 173 &ve_setup_22_stereo,
nuclear@1 174 &ve_setup_22_uncoupled,
nuclear@1 175 &ve_setup_16_stereo,
nuclear@1 176 &ve_setup_16_uncoupled,
nuclear@1 177
nuclear@1 178 &ve_setup_11_stereo,
nuclear@1 179 &ve_setup_11_uncoupled,
nuclear@1 180 &ve_setup_8_stereo,
nuclear@1 181 &ve_setup_8_uncoupled,
nuclear@1 182
nuclear@1 183 &ve_setup_X_stereo,
nuclear@1 184 &ve_setup_X_uncoupled,
nuclear@1 185 &ve_setup_XX_stereo,
nuclear@1 186 &ve_setup_XX_uncoupled,
nuclear@1 187 0
nuclear@1 188 };
nuclear@1 189
nuclear@1 190 static void vorbis_encode_floor_setup(vorbis_info *vi,int s,
nuclear@1 191 const static_codebook *const *const *const books,
nuclear@1 192 const vorbis_info_floor1 *in,
nuclear@1 193 const int *x){
nuclear@1 194 int i,k,is=s;
nuclear@1 195 vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f));
nuclear@1 196 codec_setup_info *ci=vi->codec_setup;
nuclear@1 197
nuclear@1 198 memcpy(f,in+x[is],sizeof(*f));
nuclear@1 199
nuclear@1 200 /* books */
nuclear@1 201 {
nuclear@1 202 int partitions=f->partitions;
nuclear@1 203 int maxclass=-1;
nuclear@1 204 int maxbook=-1;
nuclear@1 205 for(i=0;i<partitions;i++)
nuclear@1 206 if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
nuclear@1 207 for(i=0;i<=maxclass;i++){
nuclear@1 208 if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
nuclear@1 209 f->class_book[i]+=ci->books;
nuclear@1 210 for(k=0;k<(1<<f->class_subs[i]);k++){
nuclear@1 211 if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
nuclear@1 212 if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
nuclear@1 213 }
nuclear@1 214 }
nuclear@1 215
nuclear@1 216 for(i=0;i<=maxbook;i++)
nuclear@1 217 ci->book_param[ci->books++]=(static_codebook *)books[x[is]][i];
nuclear@1 218 }
nuclear@1 219
nuclear@1 220 /* for now, we're only using floor 1 */
nuclear@1 221 ci->floor_type[ci->floors]=1;
nuclear@1 222 ci->floor_param[ci->floors]=f;
nuclear@1 223 ci->floors++;
nuclear@1 224
nuclear@1 225 return;
nuclear@1 226 }
nuclear@1 227
nuclear@1 228 static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
nuclear@1 229 const vorbis_info_psy_global *in,
nuclear@1 230 const double *x){
nuclear@1 231 int i,is=s;
nuclear@1 232 double ds=s-is;
nuclear@1 233 codec_setup_info *ci=vi->codec_setup;
nuclear@1 234 vorbis_info_psy_global *g=&ci->psy_g_param;
nuclear@1 235
nuclear@1 236 memcpy(g,in+(int)x[is],sizeof(*g));
nuclear@1 237
nuclear@1 238 ds=x[is]*(1.-ds)+x[is+1]*ds;
nuclear@1 239 is=(int)ds;
nuclear@1 240 ds-=is;
nuclear@1 241 if(ds==0 && is>0){
nuclear@1 242 is--;
nuclear@1 243 ds=1.;
nuclear@1 244 }
nuclear@1 245
nuclear@1 246 /* interpolate the trigger threshholds */
nuclear@1 247 for(i=0;i<4;i++){
nuclear@1 248 g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
nuclear@1 249 g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
nuclear@1 250 }
nuclear@1 251 g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
nuclear@1 252 return;
nuclear@1 253 }
nuclear@1 254
nuclear@1 255 static void vorbis_encode_global_stereo(vorbis_info *vi,
nuclear@1 256 const highlevel_encode_setup *const hi,
nuclear@1 257 const adj_stereo *p){
nuclear@1 258 float s=hi->stereo_point_setting;
nuclear@1 259 int i,is=s;
nuclear@1 260 double ds=s-is;
nuclear@1 261 codec_setup_info *ci=vi->codec_setup;
nuclear@1 262 vorbis_info_psy_global *g=&ci->psy_g_param;
nuclear@1 263
nuclear@1 264 if(p){
nuclear@1 265 memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
nuclear@1 266 memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);
nuclear@1 267
nuclear@1 268 if(hi->managed){
nuclear@1 269 /* interpolate the kHz threshholds */
nuclear@1 270 for(i=0;i<PACKETBLOBS;i++){
nuclear@1 271 float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
nuclear@1 272 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
nuclear@1 273 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
nuclear@1 274 g->coupling_pkHz[i]=kHz;
nuclear@1 275
nuclear@1 276 kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
nuclear@1 277 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
nuclear@1 278 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
nuclear@1 279
nuclear@1 280 }
nuclear@1 281 }else{
nuclear@1 282 float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
nuclear@1 283 for(i=0;i<PACKETBLOBS;i++){
nuclear@1 284 g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
nuclear@1 285 g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
nuclear@1 286 g->coupling_pkHz[i]=kHz;
nuclear@1 287 }
nuclear@1 288
nuclear@1 289 kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
nuclear@1 290 for(i=0;i<PACKETBLOBS;i++){
nuclear@1 291 g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
nuclear@1 292 g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
nuclear@1 293 }
nuclear@1 294 }
nuclear@1 295 }else{
nuclear@1 296 for(i=0;i<PACKETBLOBS;i++){
nuclear@1 297 g->sliding_lowpass[0][i]=ci->blocksizes[0];
nuclear@1 298 g->sliding_lowpass[1][i]=ci->blocksizes[1];
nuclear@1 299 }
nuclear@1 300 }
nuclear@1 301 return;
nuclear@1 302 }
nuclear@1 303
nuclear@1 304 static void vorbis_encode_psyset_setup(vorbis_info *vi,double s,
nuclear@1 305 const int *nn_start,
nuclear@1 306 const int *nn_partition,
nuclear@1 307 const double *nn_thresh,
nuclear@1 308 int block){
nuclear@1 309 codec_setup_info *ci=vi->codec_setup;
nuclear@1 310 vorbis_info_psy *p=ci->psy_param[block];
nuclear@1 311 highlevel_encode_setup *hi=&ci->hi;
nuclear@1 312 int is=s;
nuclear@1 313
nuclear@1 314 if(block>=ci->psys)
nuclear@1 315 ci->psys=block+1;
nuclear@1 316 if(!p){
nuclear@1 317 p=_ogg_calloc(1,sizeof(*p));
nuclear@1 318 ci->psy_param[block]=p;
nuclear@1 319 }
nuclear@1 320
nuclear@1 321 memcpy(p,&_psy_info_template,sizeof(*p));
nuclear@1 322 p->blockflag=block>>1;
nuclear@1 323
nuclear@1 324 if(hi->noise_normalize_p){
nuclear@1 325 p->normal_p=1;
nuclear@1 326 p->normal_start=nn_start[is];
nuclear@1 327 p->normal_partition=nn_partition[is];
nuclear@1 328 p->normal_thresh=nn_thresh[is];
nuclear@1 329 }
nuclear@1 330
nuclear@1 331 return;
nuclear@1 332 }
nuclear@1 333
nuclear@1 334 static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
nuclear@1 335 const att3 *att,
nuclear@1 336 const int *max,
nuclear@1 337 const vp_adjblock *in){
nuclear@1 338 int i,is=s;
nuclear@1 339 double ds=s-is;
nuclear@1 340 codec_setup_info *ci=vi->codec_setup;
nuclear@1 341 vorbis_info_psy *p=ci->psy_param[block];
nuclear@1 342
nuclear@1 343 /* 0 and 2 are only used by bitmanagement, but there's no harm to always
nuclear@1 344 filling the values in here */
nuclear@1 345 p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds;
nuclear@1 346 p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds;
nuclear@1 347 p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds;
nuclear@1 348 p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds;
nuclear@1 349 p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds;
nuclear@1 350
nuclear@1 351 p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds;
nuclear@1 352
nuclear@1 353 for(i=0;i<P_BANDS;i++)
nuclear@1 354 p->toneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds;
nuclear@1 355 return;
nuclear@1 356 }
nuclear@1 357
nuclear@1 358
nuclear@1 359 static void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
nuclear@1 360 const compandblock *in,
nuclear@1 361 const double *x){
nuclear@1 362 int i,is=s;
nuclear@1 363 double ds=s-is;
nuclear@1 364 codec_setup_info *ci=vi->codec_setup;
nuclear@1 365 vorbis_info_psy *p=ci->psy_param[block];
nuclear@1 366
nuclear@1 367 ds=x[is]*(1.-ds)+x[is+1]*ds;
nuclear@1 368 is=(int)ds;
nuclear@1 369 ds-=is;
nuclear@1 370 if(ds==0 && is>0){
nuclear@1 371 is--;
nuclear@1 372 ds=1.;
nuclear@1 373 }
nuclear@1 374
nuclear@1 375 /* interpolate the compander settings */
nuclear@1 376 for(i=0;i<NOISE_COMPAND_LEVELS;i++)
nuclear@1 377 p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
nuclear@1 378 return;
nuclear@1 379 }
nuclear@1 380
nuclear@1 381 static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
nuclear@1 382 const int *suppress){
nuclear@1 383 int is=s;
nuclear@1 384 double ds=s-is;
nuclear@1 385 codec_setup_info *ci=vi->codec_setup;
nuclear@1 386 vorbis_info_psy *p=ci->psy_param[block];
nuclear@1 387
nuclear@1 388 p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;
nuclear@1 389
nuclear@1 390 return;
nuclear@1 391 }
nuclear@1 392
nuclear@1 393 static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
nuclear@1 394 const int *suppress,
nuclear@1 395 const noise3 *in,
nuclear@1 396 const noiseguard *guard,
nuclear@1 397 double userbias){
nuclear@1 398 int i,is=s,j;
nuclear@1 399 double ds=s-is;
nuclear@1 400 codec_setup_info *ci=vi->codec_setup;
nuclear@1 401 vorbis_info_psy *p=ci->psy_param[block];
nuclear@1 402
nuclear@1 403 p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
nuclear@1 404 p->noisewindowlomin=guard[block].lo;
nuclear@1 405 p->noisewindowhimin=guard[block].hi;
nuclear@1 406 p->noisewindowfixed=guard[block].fixed;
nuclear@1 407
nuclear@1 408 for(j=0;j<P_NOISECURVES;j++)
nuclear@1 409 for(i=0;i<P_BANDS;i++)
nuclear@1 410 p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;
nuclear@1 411
nuclear@1 412 /* impulse blocks may take a user specified bias to boost the
nuclear@1 413 nominal/high noise encoding depth */
nuclear@1 414 for(j=0;j<P_NOISECURVES;j++){
nuclear@1 415 float min=p->noiseoff[j][0]+6; /* the lowest it can go */
nuclear@1 416 for(i=0;i<P_BANDS;i++){
nuclear@1 417 p->noiseoff[j][i]+=userbias;
nuclear@1 418 if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
nuclear@1 419 }
nuclear@1 420 }
nuclear@1 421
nuclear@1 422 return;
nuclear@1 423 }
nuclear@1 424
nuclear@1 425 static void vorbis_encode_ath_setup(vorbis_info *vi,int block){
nuclear@1 426 codec_setup_info *ci=vi->codec_setup;
nuclear@1 427 vorbis_info_psy *p=ci->psy_param[block];
nuclear@1 428
nuclear@1 429 p->ath_adjatt=ci->hi.ath_floating_dB;
nuclear@1 430 p->ath_maxatt=ci->hi.ath_absolute_dB;
nuclear@1 431 return;
nuclear@1 432 }
nuclear@1 433
nuclear@1 434
nuclear@1 435 static int book_dup_or_new(codec_setup_info *ci,const static_codebook *book){
nuclear@1 436 int i;
nuclear@1 437 for(i=0;i<ci->books;i++)
nuclear@1 438 if(ci->book_param[i]==book)return(i);
nuclear@1 439
nuclear@1 440 return(ci->books++);
nuclear@1 441 }
nuclear@1 442
nuclear@1 443 static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
nuclear@1 444 const int *shortb,const int *longb){
nuclear@1 445
nuclear@1 446 codec_setup_info *ci=vi->codec_setup;
nuclear@1 447 int is=s;
nuclear@1 448
nuclear@1 449 int blockshort=shortb[is];
nuclear@1 450 int blocklong=longb[is];
nuclear@1 451 ci->blocksizes[0]=blockshort;
nuclear@1 452 ci->blocksizes[1]=blocklong;
nuclear@1 453
nuclear@1 454 }
nuclear@1 455
nuclear@1 456 static void vorbis_encode_residue_setup(vorbis_info *vi,
nuclear@1 457 int number, int block,
nuclear@1 458 const vorbis_residue_template *res){
nuclear@1 459
nuclear@1 460 codec_setup_info *ci=vi->codec_setup;
nuclear@1 461 int i;
nuclear@1 462
nuclear@1 463 vorbis_info_residue0 *r=ci->residue_param[number]=
nuclear@1 464 _ogg_malloc(sizeof(*r));
nuclear@1 465
nuclear@1 466 memcpy(r,res->res,sizeof(*r));
nuclear@1 467 if(ci->residues<=number)ci->residues=number+1;
nuclear@1 468
nuclear@1 469 r->grouping=res->grouping;
nuclear@1 470 ci->residue_type[number]=res->res_type;
nuclear@1 471
nuclear@1 472 /* fill in all the books */
nuclear@1 473 {
nuclear@1 474 int booklist=0,k;
nuclear@1 475
nuclear@1 476 if(ci->hi.managed){
nuclear@1 477 for(i=0;i<r->partitions;i++)
nuclear@1 478 for(k=0;k<4;k++)
nuclear@1 479 if(res->books_base_managed->books[i][k])
nuclear@1 480 r->secondstages[i]|=(1<<k);
nuclear@1 481
nuclear@1 482 r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
nuclear@1 483 ci->book_param[r->groupbook]=(static_codebook *)res->book_aux_managed;
nuclear@1 484
nuclear@1 485 for(i=0;i<r->partitions;i++){
nuclear@1 486 for(k=0;k<4;k++){
nuclear@1 487 if(res->books_base_managed->books[i][k]){
nuclear@1 488 int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
nuclear@1 489 r->booklist[booklist++]=bookid;
nuclear@1 490 ci->book_param[bookid]=(static_codebook *)res->books_base_managed->books[i][k];
nuclear@1 491 }
nuclear@1 492 }
nuclear@1 493 }
nuclear@1 494
nuclear@1 495 }else{
nuclear@1 496
nuclear@1 497 for(i=0;i<r->partitions;i++)
nuclear@1 498 for(k=0;k<4;k++)
nuclear@1 499 if(res->books_base->books[i][k])
nuclear@1 500 r->secondstages[i]|=(1<<k);
nuclear@1 501
nuclear@1 502 r->groupbook=book_dup_or_new(ci,res->book_aux);
nuclear@1 503 ci->book_param[r->groupbook]=(static_codebook *)res->book_aux;
nuclear@1 504
nuclear@1 505 for(i=0;i<r->partitions;i++){
nuclear@1 506 for(k=0;k<4;k++){
nuclear@1 507 if(res->books_base->books[i][k]){
nuclear@1 508 int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
nuclear@1 509 r->booklist[booklist++]=bookid;
nuclear@1 510 ci->book_param[bookid]=(static_codebook *)res->books_base->books[i][k];
nuclear@1 511 }
nuclear@1 512 }
nuclear@1 513 }
nuclear@1 514 }
nuclear@1 515 }
nuclear@1 516
nuclear@1 517 /* lowpass setup/pointlimit */
nuclear@1 518 {
nuclear@1 519 double freq=ci->hi.lowpass_kHz*1000.;
nuclear@1 520 vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */
nuclear@1 521 double nyq=vi->rate/2.;
nuclear@1 522 long blocksize=ci->blocksizes[block]>>1;
nuclear@1 523
nuclear@1 524 /* lowpass needs to be set in the floor and the residue. */
nuclear@1 525 if(freq>nyq)freq=nyq;
nuclear@1 526 /* in the floor, the granularity can be very fine; it doesn't alter
nuclear@1 527 the encoding structure, only the samples used to fit the floor
nuclear@1 528 approximation */
nuclear@1 529 f->n=freq/nyq*blocksize;
nuclear@1 530
nuclear@1 531 /* this res may by limited by the maximum pointlimit of the mode,
nuclear@1 532 not the lowpass. the floor is always lowpass limited. */
nuclear@1 533 switch(res->limit_type){
nuclear@1 534 case 1: /* point stereo limited */
nuclear@1 535 if(ci->hi.managed)
nuclear@1 536 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.;
nuclear@1 537 else
nuclear@1 538 freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.;
nuclear@1 539 if(freq>nyq)freq=nyq;
nuclear@1 540 break;
nuclear@1 541 case 2: /* LFE channel; lowpass at ~ 250Hz */
nuclear@1 542 freq=250;
nuclear@1 543 break;
nuclear@1 544 default:
nuclear@1 545 /* already set */
nuclear@1 546 break;
nuclear@1 547 }
nuclear@1 548
nuclear@1 549 /* in the residue, we're constrained, physically, by partition
nuclear@1 550 boundaries. We still lowpass 'wherever', but we have to round up
nuclear@1 551 here to next boundary, or the vorbis spec will round it *down* to
nuclear@1 552 previous boundary in encode/decode */
nuclear@1 553 if(ci->residue_type[number]==2){
nuclear@1 554 /* residue 2 bundles together multiple channels; used by stereo
nuclear@1 555 and surround. Count the channels in use */
nuclear@1 556 /* Multiple maps/submaps can point to the same residue. In the case
nuclear@1 557 of residue 2, they all better have the same number of
nuclear@1 558 channels/samples. */
nuclear@1 559 int j,k,ch=0;
nuclear@1 560 for(i=0;i<ci->maps&&ch==0;i++){
nuclear@1 561 vorbis_info_mapping0 *mi=(vorbis_info_mapping0 *)ci->map_param[i];
nuclear@1 562 for(j=0;j<mi->submaps && ch==0;j++)
nuclear@1 563 if(mi->residuesubmap[j]==number) /* we found a submap referencing theis residue backend */
nuclear@1 564 for(k=0;k<vi->channels;k++)
nuclear@1 565 if(mi->chmuxlist[k]==j) /* this channel belongs to the submap */
nuclear@1 566 ch++;
nuclear@1 567 }
nuclear@1 568
nuclear@1 569 r->end=(int)((freq/nyq*blocksize*ch)/r->grouping+.9)* /* round up only if we're well past */
nuclear@1 570 r->grouping;
nuclear@1 571 /* the blocksize and grouping may disagree at the end */
nuclear@1 572 if(r->end>blocksize*ch)r->end=blocksize*ch/r->grouping*r->grouping;
nuclear@1 573
nuclear@1 574 }else{
nuclear@1 575
nuclear@1 576 r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
nuclear@1 577 r->grouping;
nuclear@1 578 /* the blocksize and grouping may disagree at the end */
nuclear@1 579 if(r->end>blocksize)r->end=blocksize/r->grouping*r->grouping;
nuclear@1 580
nuclear@1 581 }
nuclear@1 582
nuclear@1 583 if(r->end==0)r->end=r->grouping; /* LFE channel */
nuclear@1 584
nuclear@1 585 }
nuclear@1 586 }
nuclear@1 587
nuclear@1 588 /* we assume two maps in this encoder */
nuclear@1 589 static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
nuclear@1 590 const vorbis_mapping_template *maps){
nuclear@1 591
nuclear@1 592 codec_setup_info *ci=vi->codec_setup;
nuclear@1 593 int i,j,is=s,modes=2;
nuclear@1 594 const vorbis_info_mapping0 *map=maps[is].map;
nuclear@1 595 const vorbis_info_mode *mode=_mode_template;
nuclear@1 596 const vorbis_residue_template *res=maps[is].res;
nuclear@1 597
nuclear@1 598 if(ci->blocksizes[0]==ci->blocksizes[1])modes=1;
nuclear@1 599
nuclear@1 600 for(i=0;i<modes;i++){
nuclear@1 601
nuclear@1 602 ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
nuclear@1 603 ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode));
nuclear@1 604
nuclear@1 605 memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
nuclear@1 606 if(i>=ci->modes)ci->modes=i+1;
nuclear@1 607
nuclear@1 608 ci->map_type[i]=0;
nuclear@1 609 memcpy(ci->map_param[i],map+i,sizeof(*map));
nuclear@1 610 if(i>=ci->maps)ci->maps=i+1;
nuclear@1 611
nuclear@1 612 for(j=0;j<map[i].submaps;j++)
nuclear@1 613 vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
nuclear@1 614 ,res+map[i].residuesubmap[j]);
nuclear@1 615 }
nuclear@1 616 }
nuclear@1 617
nuclear@1 618 static double setting_to_approx_bitrate(vorbis_info *vi){
nuclear@1 619 codec_setup_info *ci=vi->codec_setup;
nuclear@1 620 highlevel_encode_setup *hi=&ci->hi;
nuclear@1 621 ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
nuclear@1 622 int is=hi->base_setting;
nuclear@1 623 double ds=hi->base_setting-is;
nuclear@1 624 int ch=vi->channels;
nuclear@1 625 const double *r=setup->rate_mapping;
nuclear@1 626
nuclear@1 627 if(r==NULL)
nuclear@1 628 return(-1);
nuclear@1 629
nuclear@1 630 return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
nuclear@1 631 }
nuclear@1 632
nuclear@1 633 static const void *get_setup_template(long ch,long srate,
nuclear@1 634 double req,int q_or_bitrate,
nuclear@1 635 double *base_setting){
nuclear@1 636 int i=0,j;
nuclear@1 637 if(q_or_bitrate)req/=ch;
nuclear@1 638
nuclear@1 639 while(setup_list[i]){
nuclear@1 640 if(setup_list[i]->coupling_restriction==-1 ||
nuclear@1 641 setup_list[i]->coupling_restriction==ch){
nuclear@1 642 if(srate>=setup_list[i]->samplerate_min_restriction &&
nuclear@1 643 srate<=setup_list[i]->samplerate_max_restriction){
nuclear@1 644 int mappings=setup_list[i]->mappings;
nuclear@1 645 const double *map=(q_or_bitrate?
nuclear@1 646 setup_list[i]->rate_mapping:
nuclear@1 647 setup_list[i]->quality_mapping);
nuclear@1 648
nuclear@1 649 /* the template matches. Does the requested quality mode
nuclear@1 650 fall within this template's modes? */
nuclear@1 651 if(req<map[0]){++i;continue;}
nuclear@1 652 if(req>map[setup_list[i]->mappings]){++i;continue;}
nuclear@1 653 for(j=0;j<mappings;j++)
nuclear@1 654 if(req>=map[j] && req<map[j+1])break;
nuclear@1 655 /* an all-points match */
nuclear@1 656 if(j==mappings)
nuclear@1 657 *base_setting=j-.001;
nuclear@1 658 else{
nuclear@1 659 float low=map[j];
nuclear@1 660 float high=map[j+1];
nuclear@1 661 float del=(req-low)/(high-low);
nuclear@1 662 *base_setting=j+del;
nuclear@1 663 }
nuclear@1 664
nuclear@1 665 return(setup_list[i]);
nuclear@1 666 }
nuclear@1 667 }
nuclear@1 668 i++;
nuclear@1 669 }
nuclear@1 670
nuclear@1 671 return NULL;
nuclear@1 672 }
nuclear@1 673
nuclear@1 674 /* encoders will need to use vorbis_info_init beforehand and call
nuclear@1 675 vorbis_info clear when all done */
nuclear@1 676
nuclear@1 677 /* two interfaces; this, more detailed one, and later a convenience
nuclear@1 678 layer on top */
nuclear@1 679
nuclear@1 680 /* the final setup call */
nuclear@1 681 int vorbis_encode_setup_init(vorbis_info *vi){
nuclear@1 682 int i,i0=0,singleblock=0;
nuclear@1 683 codec_setup_info *ci=vi->codec_setup;
nuclear@1 684 ve_setup_data_template *setup=NULL;
nuclear@1 685 highlevel_encode_setup *hi=&ci->hi;
nuclear@1 686
nuclear@1 687 if(ci==NULL)return(OV_EINVAL);
nuclear@1 688 if(!hi->impulse_block_p)i0=1;
nuclear@1 689
nuclear@1 690 /* too low/high an ATH floater is nonsensical, but doesn't break anything */
nuclear@1 691 if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80;
nuclear@1 692 if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200;
nuclear@1 693
nuclear@1 694 /* again, bound this to avoid the app shooting itself int he foot
nuclear@1 695 too badly */
nuclear@1 696 if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.;
nuclear@1 697 if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.;
nuclear@1 698
nuclear@1 699 /* get the appropriate setup template; matches the fetch in previous
nuclear@1 700 stages */
nuclear@1 701 setup=(ve_setup_data_template *)hi->setup;
nuclear@1 702 if(setup==NULL)return(OV_EINVAL);
nuclear@1 703
nuclear@1 704 hi->set_in_stone=1;
nuclear@1 705 /* choose block sizes from configured sizes as well as paying
nuclear@1 706 attention to long_block_p and short_block_p. If the configured
nuclear@1 707 short and long blocks are the same length, we set long_block_p
nuclear@1 708 and unset short_block_p */
nuclear@1 709 vorbis_encode_blocksize_setup(vi,hi->base_setting,
nuclear@1 710 setup->blocksize_short,
nuclear@1 711 setup->blocksize_long);
nuclear@1 712 if(ci->blocksizes[0]==ci->blocksizes[1])singleblock=1;
nuclear@1 713
nuclear@1 714 /* floor setup; choose proper floor params. Allocated on the floor
nuclear@1 715 stack in order; if we alloc only a single long floor, it's 0 */
nuclear@1 716 for(i=0;i<setup->floor_mappings;i++)
nuclear@1 717 vorbis_encode_floor_setup(vi,hi->base_setting,
nuclear@1 718 setup->floor_books,
nuclear@1 719 setup->floor_params,
nuclear@1 720 setup->floor_mapping_list[i]);
nuclear@1 721
nuclear@1 722 /* setup of [mostly] short block detection and stereo*/
nuclear@1 723 vorbis_encode_global_psych_setup(vi,hi->trigger_setting,
nuclear@1 724 setup->global_params,
nuclear@1 725 setup->global_mapping);
nuclear@1 726 vorbis_encode_global_stereo(vi,hi,setup->stereo_modes);
nuclear@1 727
nuclear@1 728 /* basic psych setup and noise normalization */
nuclear@1 729 vorbis_encode_psyset_setup(vi,hi->base_setting,
nuclear@1 730 setup->psy_noise_normal_start[0],
nuclear@1 731 setup->psy_noise_normal_partition[0],
nuclear@1 732 setup->psy_noise_normal_thresh,
nuclear@1 733 0);
nuclear@1 734 vorbis_encode_psyset_setup(vi,hi->base_setting,
nuclear@1 735 setup->psy_noise_normal_start[0],
nuclear@1 736 setup->psy_noise_normal_partition[0],
nuclear@1 737 setup->psy_noise_normal_thresh,
nuclear@1 738 1);
nuclear@1 739 if(!singleblock){
nuclear@1 740 vorbis_encode_psyset_setup(vi,hi->base_setting,
nuclear@1 741 setup->psy_noise_normal_start[1],
nuclear@1 742 setup->psy_noise_normal_partition[1],
nuclear@1 743 setup->psy_noise_normal_thresh,
nuclear@1 744 2);
nuclear@1 745 vorbis_encode_psyset_setup(vi,hi->base_setting,
nuclear@1 746 setup->psy_noise_normal_start[1],
nuclear@1 747 setup->psy_noise_normal_partition[1],
nuclear@1 748 setup->psy_noise_normal_thresh,
nuclear@1 749 3);
nuclear@1 750 }
nuclear@1 751
nuclear@1 752 /* tone masking setup */
nuclear@1 753 vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0,
nuclear@1 754 setup->psy_tone_masteratt,
nuclear@1 755 setup->psy_tone_0dB,
nuclear@1 756 setup->psy_tone_adj_impulse);
nuclear@1 757 vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1,
nuclear@1 758 setup->psy_tone_masteratt,
nuclear@1 759 setup->psy_tone_0dB,
nuclear@1 760 setup->psy_tone_adj_other);
nuclear@1 761 if(!singleblock){
nuclear@1 762 vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2,
nuclear@1 763 setup->psy_tone_masteratt,
nuclear@1 764 setup->psy_tone_0dB,
nuclear@1 765 setup->psy_tone_adj_other);
nuclear@1 766 vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3,
nuclear@1 767 setup->psy_tone_masteratt,
nuclear@1 768 setup->psy_tone_0dB,
nuclear@1 769 setup->psy_tone_adj_long);
nuclear@1 770 }
nuclear@1 771
nuclear@1 772 /* noise companding setup */
nuclear@1 773 vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0,
nuclear@1 774 setup->psy_noise_compand,
nuclear@1 775 setup->psy_noise_compand_short_mapping);
nuclear@1 776 vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1,
nuclear@1 777 setup->psy_noise_compand,
nuclear@1 778 setup->psy_noise_compand_short_mapping);
nuclear@1 779 if(!singleblock){
nuclear@1 780 vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2,
nuclear@1 781 setup->psy_noise_compand,
nuclear@1 782 setup->psy_noise_compand_long_mapping);
nuclear@1 783 vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3,
nuclear@1 784 setup->psy_noise_compand,
nuclear@1 785 setup->psy_noise_compand_long_mapping);
nuclear@1 786 }
nuclear@1 787
nuclear@1 788 /* peak guarding setup */
nuclear@1 789 vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0,
nuclear@1 790 setup->psy_tone_dBsuppress);
nuclear@1 791 vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1,
nuclear@1 792 setup->psy_tone_dBsuppress);
nuclear@1 793 if(!singleblock){
nuclear@1 794 vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2,
nuclear@1 795 setup->psy_tone_dBsuppress);
nuclear@1 796 vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3,
nuclear@1 797 setup->psy_tone_dBsuppress);
nuclear@1 798 }
nuclear@1 799
nuclear@1 800 /* noise bias setup */
nuclear@1 801 vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0,
nuclear@1 802 setup->psy_noise_dBsuppress,
nuclear@1 803 setup->psy_noise_bias_impulse,
nuclear@1 804 setup->psy_noiseguards,
nuclear@1 805 (i0==0?hi->impulse_noisetune:0.));
nuclear@1 806 vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1,
nuclear@1 807 setup->psy_noise_dBsuppress,
nuclear@1 808 setup->psy_noise_bias_padding,
nuclear@1 809 setup->psy_noiseguards,0.);
nuclear@1 810 if(!singleblock){
nuclear@1 811 vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2,
nuclear@1 812 setup->psy_noise_dBsuppress,
nuclear@1 813 setup->psy_noise_bias_trans,
nuclear@1 814 setup->psy_noiseguards,0.);
nuclear@1 815 vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3,
nuclear@1 816 setup->psy_noise_dBsuppress,
nuclear@1 817 setup->psy_noise_bias_long,
nuclear@1 818 setup->psy_noiseguards,0.);
nuclear@1 819 }
nuclear@1 820
nuclear@1 821 vorbis_encode_ath_setup(vi,0);
nuclear@1 822 vorbis_encode_ath_setup(vi,1);
nuclear@1 823 if(!singleblock){
nuclear@1 824 vorbis_encode_ath_setup(vi,2);
nuclear@1 825 vorbis_encode_ath_setup(vi,3);
nuclear@1 826 }
nuclear@1 827
nuclear@1 828 vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps);
nuclear@1 829
nuclear@1 830 /* set bitrate readonlies and management */
nuclear@1 831 if(hi->bitrate_av>0)
nuclear@1 832 vi->bitrate_nominal=hi->bitrate_av;
nuclear@1 833 else{
nuclear@1 834 vi->bitrate_nominal=setting_to_approx_bitrate(vi);
nuclear@1 835 }
nuclear@1 836
nuclear@1 837 vi->bitrate_lower=hi->bitrate_min;
nuclear@1 838 vi->bitrate_upper=hi->bitrate_max;
nuclear@1 839 if(hi->bitrate_av)
nuclear@1 840 vi->bitrate_window=(double)hi->bitrate_reservoir/hi->bitrate_av;
nuclear@1 841 else
nuclear@1 842 vi->bitrate_window=0.;
nuclear@1 843
nuclear@1 844 if(hi->managed){
nuclear@1 845 ci->bi.avg_rate=hi->bitrate_av;
nuclear@1 846 ci->bi.min_rate=hi->bitrate_min;
nuclear@1 847 ci->bi.max_rate=hi->bitrate_max;
nuclear@1 848
nuclear@1 849 ci->bi.reservoir_bits=hi->bitrate_reservoir;
nuclear@1 850 ci->bi.reservoir_bias=
nuclear@1 851 hi->bitrate_reservoir_bias;
nuclear@1 852
nuclear@1 853 ci->bi.slew_damp=hi->bitrate_av_damp;
nuclear@1 854
nuclear@1 855 }
nuclear@1 856
nuclear@1 857 return(0);
nuclear@1 858
nuclear@1 859 }
nuclear@1 860
nuclear@1 861 static void vorbis_encode_setup_setting(vorbis_info *vi,
nuclear@1 862 long channels,
nuclear@1 863 long rate){
nuclear@1 864 int i,is;
nuclear@1 865 codec_setup_info *ci=vi->codec_setup;
nuclear@1 866 highlevel_encode_setup *hi=&ci->hi;
nuclear@1 867 const ve_setup_data_template *setup=hi->setup;
nuclear@1 868 double ds;
nuclear@1 869
nuclear@1 870 vi->version=0;
nuclear@1 871 vi->channels=channels;
nuclear@1 872 vi->rate=rate;
nuclear@1 873
nuclear@1 874 hi->impulse_block_p=1;
nuclear@1 875 hi->noise_normalize_p=1;
nuclear@1 876
nuclear@1 877 is=hi->base_setting;
nuclear@1 878 ds=hi->base_setting-is;
nuclear@1 879
nuclear@1 880 hi->stereo_point_setting=hi->base_setting;
nuclear@1 881
nuclear@1 882 if(!hi->lowpass_altered)
nuclear@1 883 hi->lowpass_kHz=
nuclear@1 884 setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;
nuclear@1 885
nuclear@1 886 hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
nuclear@1 887 setup->psy_ath_float[is+1]*ds;
nuclear@1 888 hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
nuclear@1 889 setup->psy_ath_abs[is+1]*ds;
nuclear@1 890
nuclear@1 891 hi->amplitude_track_dBpersec=-6.;
nuclear@1 892 hi->trigger_setting=hi->base_setting;
nuclear@1 893
nuclear@1 894 for(i=0;i<4;i++){
nuclear@1 895 hi->block[i].tone_mask_setting=hi->base_setting;
nuclear@1 896 hi->block[i].tone_peaklimit_setting=hi->base_setting;
nuclear@1 897 hi->block[i].noise_bias_setting=hi->base_setting;
nuclear@1 898 hi->block[i].noise_compand_setting=hi->base_setting;
nuclear@1 899 }
nuclear@1 900 }
nuclear@1 901
nuclear@1 902 int vorbis_encode_setup_vbr(vorbis_info *vi,
nuclear@1 903 long channels,
nuclear@1 904 long rate,
nuclear@1 905 float quality){
nuclear@1 906 codec_setup_info *ci=vi->codec_setup;
nuclear@1 907 highlevel_encode_setup *hi=&ci->hi;
nuclear@1 908
nuclear@1 909 quality+=.0000001;
nuclear@1 910 if(quality>=1.)quality=.9999;
nuclear@1 911
nuclear@1 912 hi->req=quality;
nuclear@1 913 hi->setup=get_setup_template(channels,rate,quality,0,&hi->base_setting);
nuclear@1 914 if(!hi->setup)return OV_EIMPL;
nuclear@1 915
nuclear@1 916 vorbis_encode_setup_setting(vi,channels,rate);
nuclear@1 917 hi->managed=0;
nuclear@1 918 hi->coupling_p=1;
nuclear@1 919
nuclear@1 920 return 0;
nuclear@1 921 }
nuclear@1 922
nuclear@1 923 int vorbis_encode_init_vbr(vorbis_info *vi,
nuclear@1 924 long channels,
nuclear@1 925 long rate,
nuclear@1 926
nuclear@1 927 float base_quality /* 0. to 1. */
nuclear@1 928 ){
nuclear@1 929 int ret=0;
nuclear@1 930
nuclear@1 931 ret=vorbis_encode_setup_vbr(vi,channels,rate,base_quality);
nuclear@1 932
nuclear@1 933 if(ret){
nuclear@1 934 vorbis_info_clear(vi);
nuclear@1 935 return ret;
nuclear@1 936 }
nuclear@1 937 ret=vorbis_encode_setup_init(vi);
nuclear@1 938 if(ret)
nuclear@1 939 vorbis_info_clear(vi);
nuclear@1 940 return(ret);
nuclear@1 941 }
nuclear@1 942
nuclear@1 943 int vorbis_encode_setup_managed(vorbis_info *vi,
nuclear@1 944 long channels,
nuclear@1 945 long rate,
nuclear@1 946
nuclear@1 947 long max_bitrate,
nuclear@1 948 long nominal_bitrate,
nuclear@1 949 long min_bitrate){
nuclear@1 950
nuclear@1 951 codec_setup_info *ci=vi->codec_setup;
nuclear@1 952 highlevel_encode_setup *hi=&ci->hi;
nuclear@1 953 double tnominal=nominal_bitrate;
nuclear@1 954
nuclear@1 955 if(nominal_bitrate<=0.){
nuclear@1 956 if(max_bitrate>0.){
nuclear@1 957 if(min_bitrate>0.)
nuclear@1 958 nominal_bitrate=(max_bitrate+min_bitrate)*.5;
nuclear@1 959 else
nuclear@1 960 nominal_bitrate=max_bitrate*.875;
nuclear@1 961 }else{
nuclear@1 962 if(min_bitrate>0.){
nuclear@1 963 nominal_bitrate=min_bitrate;
nuclear@1 964 }else{
nuclear@1 965 return(OV_EINVAL);
nuclear@1 966 }
nuclear@1 967 }
nuclear@1 968 }
nuclear@1 969
nuclear@1 970 hi->req=nominal_bitrate;
nuclear@1 971 hi->setup=get_setup_template(channels,rate,nominal_bitrate,1,&hi->base_setting);
nuclear@1 972 if(!hi->setup)return OV_EIMPL;
nuclear@1 973
nuclear@1 974 vorbis_encode_setup_setting(vi,channels,rate);
nuclear@1 975
nuclear@1 976 /* initialize management with sane defaults */
nuclear@1 977 hi->coupling_p=1;
nuclear@1 978 hi->managed=1;
nuclear@1 979 hi->bitrate_min=min_bitrate;
nuclear@1 980 hi->bitrate_max=max_bitrate;
nuclear@1 981 hi->bitrate_av=tnominal;
nuclear@1 982 hi->bitrate_av_damp=1.5f; /* full range in no less than 1.5 second */
nuclear@1 983 hi->bitrate_reservoir=nominal_bitrate*2;
nuclear@1 984 hi->bitrate_reservoir_bias=.1; /* bias toward hoarding bits */
nuclear@1 985
nuclear@1 986 return(0);
nuclear@1 987
nuclear@1 988 }
nuclear@1 989
nuclear@1 990 int vorbis_encode_init(vorbis_info *vi,
nuclear@1 991 long channels,
nuclear@1 992 long rate,
nuclear@1 993
nuclear@1 994 long max_bitrate,
nuclear@1 995 long nominal_bitrate,
nuclear@1 996 long min_bitrate){
nuclear@1 997
nuclear@1 998 int ret=vorbis_encode_setup_managed(vi,channels,rate,
nuclear@1 999 max_bitrate,
nuclear@1 1000 nominal_bitrate,
nuclear@1 1001 min_bitrate);
nuclear@1 1002 if(ret){
nuclear@1 1003 vorbis_info_clear(vi);
nuclear@1 1004 return(ret);
nuclear@1 1005 }
nuclear@1 1006
nuclear@1 1007 ret=vorbis_encode_setup_init(vi);
nuclear@1 1008 if(ret)
nuclear@1 1009 vorbis_info_clear(vi);
nuclear@1 1010 return(ret);
nuclear@1 1011 }
nuclear@1 1012
nuclear@1 1013 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
nuclear@1 1014 if(vi){
nuclear@1 1015 codec_setup_info *ci=vi->codec_setup;
nuclear@1 1016 highlevel_encode_setup *hi=&ci->hi;
nuclear@1 1017 int setp=(number&0xf); /* a read request has a low nibble of 0 */
nuclear@1 1018
nuclear@1 1019 if(setp && hi->set_in_stone)return(OV_EINVAL);
nuclear@1 1020
nuclear@1 1021 switch(number){
nuclear@1 1022
nuclear@1 1023 /* now deprecated *****************/
nuclear@1 1024 case OV_ECTL_RATEMANAGE_GET:
nuclear@1 1025 {
nuclear@1 1026
nuclear@1 1027 struct ovectl_ratemanage_arg *ai=
nuclear@1 1028 (struct ovectl_ratemanage_arg *)arg;
nuclear@1 1029
nuclear@1 1030 ai->management_active=hi->managed;
nuclear@1 1031 ai->bitrate_hard_window=ai->bitrate_av_window=
nuclear@1 1032 (double)hi->bitrate_reservoir/vi->rate;
nuclear@1 1033 ai->bitrate_av_window_center=1.;
nuclear@1 1034 ai->bitrate_hard_min=hi->bitrate_min;
nuclear@1 1035 ai->bitrate_hard_max=hi->bitrate_max;
nuclear@1 1036 ai->bitrate_av_lo=hi->bitrate_av;
nuclear@1 1037 ai->bitrate_av_hi=hi->bitrate_av;
nuclear@1 1038
nuclear@1 1039 }
nuclear@1 1040 return(0);
nuclear@1 1041
nuclear@1 1042 /* now deprecated *****************/
nuclear@1 1043 case OV_ECTL_RATEMANAGE_SET:
nuclear@1 1044 {
nuclear@1 1045 struct ovectl_ratemanage_arg *ai=
nuclear@1 1046 (struct ovectl_ratemanage_arg *)arg;
nuclear@1 1047 if(ai==NULL){
nuclear@1 1048 hi->managed=0;
nuclear@1 1049 }else{
nuclear@1 1050 hi->managed=ai->management_active;
nuclear@1 1051 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_AVG,arg);
nuclear@1 1052 vorbis_encode_ctl(vi,OV_ECTL_RATEMANAGE_HARD,arg);
nuclear@1 1053 }
nuclear@1 1054 }
nuclear@1 1055 return 0;
nuclear@1 1056
nuclear@1 1057 /* now deprecated *****************/
nuclear@1 1058 case OV_ECTL_RATEMANAGE_AVG:
nuclear@1 1059 {
nuclear@1 1060 struct ovectl_ratemanage_arg *ai=
nuclear@1 1061 (struct ovectl_ratemanage_arg *)arg;
nuclear@1 1062 if(ai==NULL){
nuclear@1 1063 hi->bitrate_av=0;
nuclear@1 1064 }else{
nuclear@1 1065 hi->bitrate_av=(ai->bitrate_av_lo+ai->bitrate_av_hi)*.5;
nuclear@1 1066 }
nuclear@1 1067 }
nuclear@1 1068 return(0);
nuclear@1 1069 /* now deprecated *****************/
nuclear@1 1070 case OV_ECTL_RATEMANAGE_HARD:
nuclear@1 1071 {
nuclear@1 1072 struct ovectl_ratemanage_arg *ai=
nuclear@1 1073 (struct ovectl_ratemanage_arg *)arg;
nuclear@1 1074 if(ai==NULL){
nuclear@1 1075 hi->bitrate_min=0;
nuclear@1 1076 hi->bitrate_max=0;
nuclear@1 1077 }else{
nuclear@1 1078 hi->bitrate_min=ai->bitrate_hard_min;
nuclear@1 1079 hi->bitrate_max=ai->bitrate_hard_max;
nuclear@1 1080 hi->bitrate_reservoir=ai->bitrate_hard_window*
nuclear@1 1081 (hi->bitrate_max+hi->bitrate_min)*.5;
nuclear@1 1082 }
nuclear@1 1083 if(hi->bitrate_reservoir<128.)
nuclear@1 1084 hi->bitrate_reservoir=128.;
nuclear@1 1085 }
nuclear@1 1086 return(0);
nuclear@1 1087
nuclear@1 1088 /* replacement ratemanage interface */
nuclear@1 1089 case OV_ECTL_RATEMANAGE2_GET:
nuclear@1 1090 {
nuclear@1 1091 struct ovectl_ratemanage2_arg *ai=
nuclear@1 1092 (struct ovectl_ratemanage2_arg *)arg;
nuclear@1 1093 if(ai==NULL)return OV_EINVAL;
nuclear@1 1094
nuclear@1 1095 ai->management_active=hi->managed;
nuclear@1 1096 ai->bitrate_limit_min_kbps=hi->bitrate_min/1000;
nuclear@1 1097 ai->bitrate_limit_max_kbps=hi->bitrate_max/1000;
nuclear@1 1098 ai->bitrate_average_kbps=hi->bitrate_av/1000;
nuclear@1 1099 ai->bitrate_average_damping=hi->bitrate_av_damp;
nuclear@1 1100 ai->bitrate_limit_reservoir_bits=hi->bitrate_reservoir;
nuclear@1 1101 ai->bitrate_limit_reservoir_bias=hi->bitrate_reservoir_bias;
nuclear@1 1102 }
nuclear@1 1103 return (0);
nuclear@1 1104 case OV_ECTL_RATEMANAGE2_SET:
nuclear@1 1105 {
nuclear@1 1106 struct ovectl_ratemanage2_arg *ai=
nuclear@1 1107 (struct ovectl_ratemanage2_arg *)arg;
nuclear@1 1108 if(ai==NULL){
nuclear@1 1109 hi->managed=0;
nuclear@1 1110 }else{
nuclear@1 1111 /* sanity check; only catch invariant violations */
nuclear@1 1112 if(ai->bitrate_limit_min_kbps>0 &&
nuclear@1 1113 ai->bitrate_average_kbps>0 &&
nuclear@1 1114 ai->bitrate_limit_min_kbps>ai->bitrate_average_kbps)
nuclear@1 1115 return OV_EINVAL;
nuclear@1 1116
nuclear@1 1117 if(ai->bitrate_limit_max_kbps>0 &&
nuclear@1 1118 ai->bitrate_average_kbps>0 &&
nuclear@1 1119 ai->bitrate_limit_max_kbps<ai->bitrate_average_kbps)
nuclear@1 1120 return OV_EINVAL;
nuclear@1 1121
nuclear@1 1122 if(ai->bitrate_limit_min_kbps>0 &&
nuclear@1 1123 ai->bitrate_limit_max_kbps>0 &&
nuclear@1 1124 ai->bitrate_limit_min_kbps>ai->bitrate_limit_max_kbps)
nuclear@1 1125 return OV_EINVAL;
nuclear@1 1126
nuclear@1 1127 if(ai->bitrate_average_damping <= 0.)
nuclear@1 1128 return OV_EINVAL;
nuclear@1 1129
nuclear@1 1130 if(ai->bitrate_limit_reservoir_bits < 0)
nuclear@1 1131 return OV_EINVAL;
nuclear@1 1132
nuclear@1 1133 if(ai->bitrate_limit_reservoir_bias < 0.)
nuclear@1 1134 return OV_EINVAL;
nuclear@1 1135
nuclear@1 1136 if(ai->bitrate_limit_reservoir_bias > 1.)
nuclear@1 1137 return OV_EINVAL;
nuclear@1 1138
nuclear@1 1139 hi->managed=ai->management_active;
nuclear@1 1140 hi->bitrate_min=ai->bitrate_limit_min_kbps * 1000;
nuclear@1 1141 hi->bitrate_max=ai->bitrate_limit_max_kbps * 1000;
nuclear@1 1142 hi->bitrate_av=ai->bitrate_average_kbps * 1000;
nuclear@1 1143 hi->bitrate_av_damp=ai->bitrate_average_damping;
nuclear@1 1144 hi->bitrate_reservoir=ai->bitrate_limit_reservoir_bits;
nuclear@1 1145 hi->bitrate_reservoir_bias=ai->bitrate_limit_reservoir_bias;
nuclear@1 1146 }
nuclear@1 1147 }
nuclear@1 1148 return 0;
nuclear@1 1149
nuclear@1 1150 case OV_ECTL_LOWPASS_GET:
nuclear@1 1151 {
nuclear@1 1152 double *farg=(double *)arg;
nuclear@1 1153 *farg=hi->lowpass_kHz;
nuclear@1 1154 }
nuclear@1 1155 return(0);
nuclear@1 1156 case OV_ECTL_LOWPASS_SET:
nuclear@1 1157 {
nuclear@1 1158 double *farg=(double *)arg;
nuclear@1 1159 hi->lowpass_kHz=*farg;
nuclear@1 1160
nuclear@1 1161 if(hi->lowpass_kHz<2.)hi->lowpass_kHz=2.;
nuclear@1 1162 if(hi->lowpass_kHz>99.)hi->lowpass_kHz=99.;
nuclear@1 1163 hi->lowpass_altered=1;
nuclear@1 1164 }
nuclear@1 1165 return(0);
nuclear@1 1166 case OV_ECTL_IBLOCK_GET:
nuclear@1 1167 {
nuclear@1 1168 double *farg=(double *)arg;
nuclear@1 1169 *farg=hi->impulse_noisetune;
nuclear@1 1170 }
nuclear@1 1171 return(0);
nuclear@1 1172 case OV_ECTL_IBLOCK_SET:
nuclear@1 1173 {
nuclear@1 1174 double *farg=(double *)arg;
nuclear@1 1175 hi->impulse_noisetune=*farg;
nuclear@1 1176
nuclear@1 1177 if(hi->impulse_noisetune>0.)hi->impulse_noisetune=0.;
nuclear@1 1178 if(hi->impulse_noisetune<-15.)hi->impulse_noisetune=-15.;
nuclear@1 1179 }
nuclear@1 1180 return(0);
nuclear@1 1181 case OV_ECTL_COUPLING_GET:
nuclear@1 1182 {
nuclear@1 1183 int *iarg=(int *)arg;
nuclear@1 1184 *iarg=hi->coupling_p;
nuclear@1 1185 }
nuclear@1 1186 return(0);
nuclear@1 1187 case OV_ECTL_COUPLING_SET:
nuclear@1 1188 {
nuclear@1 1189 const void *new_template;
nuclear@1 1190 double new_base=0.;
nuclear@1 1191 int *iarg=(int *)arg;
nuclear@1 1192 hi->coupling_p=((*iarg)!=0);
nuclear@1 1193
nuclear@1 1194 /* Fetching a new template can alter the base_setting, which
nuclear@1 1195 many other parameters are based on. Right now, the only
nuclear@1 1196 parameter drawn from the base_setting that can be altered
nuclear@1 1197 by an encctl is the lowpass, so that is explictly flagged
nuclear@1 1198 to not be overwritten when we fetch a new template and
nuclear@1 1199 recompute the dependant settings */
nuclear@1 1200 new_template = get_setup_template(hi->coupling_p?vi->channels:-1,
nuclear@1 1201 vi->rate,
nuclear@1 1202 hi->req,
nuclear@1 1203 hi->managed,
nuclear@1 1204 &new_base);
nuclear@1 1205 if(!hi->setup)return OV_EIMPL;
nuclear@1 1206 hi->setup=new_template;
nuclear@1 1207 hi->base_setting=new_base;
nuclear@1 1208 vorbis_encode_setup_setting(vi,vi->channels,vi->rate);
nuclear@1 1209 }
nuclear@1 1210 return(0);
nuclear@1 1211 }
nuclear@1 1212 return(OV_EIMPL);
nuclear@1 1213 }
nuclear@1 1214 return(OV_EINVAL);
nuclear@1 1215 }