vorbisenc.c 38 KB

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