vorbisenc.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: simple programmatic interface for encoder mode setup
  13. last mod: $Id: vorbisenc.c 19457 2015-03-03 00:15:29Z giles $
  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;
  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(!hi->setup)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. }