1
0

vorbisenc.c 38 KB

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