vrshoot

annotate libs/vorbis/vorbisenc.c @ 1:e7ca128b8713

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