vorbisenc.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. * *
  11. ********************************************************************
  12. function: simple programmatic interface for encoder mode setup
  13. last mod: $Id: vorbisenc.c,v 1.23.2.2 2001/12/18 23:49:17 xiphmont Exp $
  14. ********************************************************************/
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #include <stdarg.h>
  19. #include "vorbis/codec.h"
  20. #include "vorbis/vorbisenc.h"
  21. #include "codec_internal.h"
  22. #include "registry-api.h"
  23. #include "os.h"
  24. #include "misc.h"
  25. /* careful with this; it's using static array sizing to make managing
  26. all the modes a little less annoying. If we use a residue backend
  27. with > 10 partition types, or a different division of iteration,
  28. this needs to be updated. */
  29. typedef struct {
  30. vorbis_info_residue0 *res[2];
  31. static_codebook *book_aux[2];
  32. static_codebook *books_base[5][10][3];
  33. static_codebook *books_stereo_backfill[5][10];
  34. static_codebook *books_residue_backfill[5][10][2];
  35. } vorbis_residue_template;
  36. static double stereo_threshholds[]={0.0, 2.5, 4.5, 8.5, 16.5};
  37. typedef struct vp_adjblock{
  38. int block[P_BANDS][P_LEVELS];
  39. } vp_adjblock;
  40. #include "modes/residue_44.h"
  41. #include "modes/psych_44.h"
  42. #include "modes/floor_44.h"
  43. /* a few static coder conventions */
  44. static vorbis_info_time0 _time_dummy={0};
  45. static vorbis_info_mode _mode_set_short={0,0,0,0};
  46. static vorbis_info_mode _mode_set_long={1,0,0,1};
  47. /* mapping conventions:
  48. only one submap (this would change for efficient 5.1 support for example)*/
  49. /* Four psychoacoustic profiles are used, one for each blocktype */
  50. static vorbis_info_mapping0 _mapping_set_short={
  51. 1, {0,0}, {0}, {0}, {0}, {0,1}, 0,{0},{0}};
  52. static vorbis_info_mapping0 _mapping_set_long={
  53. 1, {0,0}, {0}, {1}, {1}, {2,3}, 0,{0},{0}};
  54. static int vorbis_encode_toplevel_setup(vorbis_info *vi,int small,int large,int ch,long rate){
  55. if(vi && vi->codec_setup){
  56. codec_setup_info *ci=vi->codec_setup;
  57. vi->version=0;
  58. vi->channels=ch;
  59. vi->rate=rate;
  60. ci->blocksizes[0]=small;
  61. ci->blocksizes[1]=large;
  62. /* time mapping hooks are unused in vorbis I */
  63. ci->times=1;
  64. ci->time_type[0]=0;
  65. ci->time_param[0]=calloc(1,sizeof(_time_dummy));
  66. memcpy(ci->time_param[0],&_time_dummy,sizeof(_time_dummy));
  67. /* by convention, two modes: one for short, one for long blocks.
  68. short block mode uses mapping sero, long block uses mapping 1 */
  69. ci->modes=2;
  70. ci->mode_param[0]=calloc(1,sizeof(_mode_set_short));
  71. memcpy(ci->mode_param[0],&_mode_set_short,sizeof(_mode_set_short));
  72. ci->mode_param[1]=calloc(1,sizeof(_mode_set_long));
  73. memcpy(ci->mode_param[1],&_mode_set_long,sizeof(_mode_set_long));
  74. /* by convention two mappings, both mapping type zero (polyphonic
  75. PCM), first for short, second for long blocks */
  76. ci->maps=2;
  77. ci->map_type[0]=0;
  78. ci->map_param[0]=calloc(1,sizeof(_mapping_set_short));
  79. memcpy(ci->map_param[0],&_mapping_set_short,sizeof(_mapping_set_short));
  80. ci->map_type[1]=0;
  81. ci->map_param[1]=calloc(1,sizeof(_mapping_set_long));
  82. memcpy(ci->map_param[1],&_mapping_set_long,sizeof(_mapping_set_long));
  83. return(0);
  84. }
  85. return(OV_EINVAL);
  86. }
  87. static int vorbis_encode_floor_setup(vorbis_info *vi,double q,int block,
  88. static_codebook ***books,
  89. vorbis_info_floor1 *in,
  90. ...){
  91. int x[11],i,k,iq=rint(q*10);
  92. vorbis_info_floor1 *f=calloc(1,sizeof(*f));
  93. codec_setup_info *ci=vi->codec_setup;
  94. va_list ap;
  95. va_start(ap,in);
  96. for(i=0;i<11;i++)
  97. x[i]=va_arg(ap,int);
  98. va_end(ap);
  99. memcpy(f,in+x[iq],sizeof(*f));
  100. /* fill in the lowpass field, even if it's temporary */
  101. f->n=ci->blocksizes[block]>>1;
  102. /* books */
  103. {
  104. int partitions=f->partitions;
  105. int maxclass=-1;
  106. int maxbook=-1;
  107. for(i=0;i<partitions;i++)
  108. if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
  109. for(i=0;i<=maxclass;i++){
  110. if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
  111. f->class_book[i]+=ci->books;
  112. for(k=0;k<(1<<f->class_subs[i]);k++){
  113. if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
  114. if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
  115. }
  116. }
  117. for(i=0;i<=maxbook;i++)
  118. ci->book_param[ci->books++]=books[x[iq]][i];
  119. }
  120. /* for now, we're only using floor 1 */
  121. ci->floor_type[ci->floors]=1;
  122. ci->floor_param[ci->floors]=f;
  123. ci->floors++;
  124. return(0);
  125. }
  126. static int vorbis_encode_global_psych_setup(vorbis_info *vi,double q,
  127. vorbis_info_psy_global *in, ...){
  128. int i,iq=q*10;
  129. double x[11],dq;
  130. codec_setup_info *ci=vi->codec_setup;
  131. vorbis_info_psy_global *g=&ci->psy_g_param;
  132. va_list ap;
  133. va_start(ap,in);
  134. for(i=0;i<11;i++)
  135. x[i]=va_arg(ap,double);
  136. va_end(ap);
  137. if(iq==10){
  138. iq=9;
  139. dq=1.;
  140. }else{
  141. dq=q*10.-iq;
  142. }
  143. memcpy(g,in+(int)x[iq],sizeof(*g));
  144. dq=x[iq]*(1.-dq)+x[iq+1]*dq;
  145. iq=(int)dq;
  146. dq-=iq;
  147. if(dq==0 && iq>0){
  148. iq--;
  149. dq=1.;
  150. }
  151. /* interpolate the trigger threshholds */
  152. for(i=0;i<4;i++){
  153. g->preecho_thresh[i]=in[iq].preecho_thresh[i]*(1.-dq)+in[iq+1].preecho_thresh[i]*dq;
  154. g->postecho_thresh[i]=in[iq].postecho_thresh[i]*(1.-dq)+in[iq+1].postecho_thresh[i]*dq;
  155. }
  156. g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
  157. return(0);
  158. }
  159. static int vorbis_encode_psyset_setup(vorbis_info *vi,int block){
  160. codec_setup_info *ci=vi->codec_setup;
  161. vorbis_info_psy *p=ci->psy_param[block];
  162. if(block>=ci->psys)
  163. ci->psys=block+1;
  164. if(!p){
  165. p=calloc(1,sizeof(*p));
  166. ci->psy_param[block]=p;
  167. }
  168. memcpy(p,&_psy_info_template,sizeof(*p));
  169. return 0;
  170. }
  171. static int vorbis_encode_tonemask_setup(vorbis_info *vi,double q,int block,
  172. double *att,
  173. double *max,
  174. int *peaklimit_bands,
  175. vp_adjblock *in){
  176. int i,j,iq;
  177. double dq;
  178. codec_setup_info *ci=vi->codec_setup;
  179. vorbis_info_psy *p=ci->psy_param[block];
  180. iq=q*10;
  181. if(iq==10){
  182. iq=9;
  183. dq=1.;
  184. }else{
  185. dq=q*10.-iq;
  186. }
  187. p->tone_masteratt=att[iq]*(1.-dq)+att[iq+1]*dq;
  188. p->max_curve_dB=max[iq];
  189. p->curvelimitp=peaklimit_bands[iq];
  190. iq=q*5.;
  191. if(iq==5){
  192. iq=5;
  193. dq=1.;
  194. }else{
  195. dq=q*5.-iq;
  196. }
  197. for(i=0;i<P_BANDS;i++)
  198. for(j=0;j<P_LEVELS;j++)
  199. p->toneatt.block[i][j]=(j<4?4:j)*-10.+
  200. in[iq].block[i][j]*(1.-dq)+in[iq+1].block[i][j]*dq;
  201. return(0);
  202. }
  203. static int vorbis_encode_compand_setup(vorbis_info *vi,double q,int block,
  204. float in[][NOISE_COMPAND_LEVELS], ...){
  205. int i,iq=q*10;
  206. double x[11],dq;
  207. codec_setup_info *ci=vi->codec_setup;
  208. vorbis_info_psy *p=ci->psy_param[block];
  209. va_list ap;
  210. va_start(ap,in);
  211. for(i=0;i<11;i++)
  212. x[i]=va_arg(ap,double);
  213. va_end(ap);
  214. if(iq==10){
  215. iq=9;
  216. dq=1.;
  217. }else{
  218. dq=q*10.-iq;
  219. }
  220. dq=x[iq]*(1.-dq)+x[iq+1]*dq;
  221. iq=(int)dq;
  222. dq-=iq;
  223. if(dq==0 && iq>0){
  224. iq--;
  225. dq=1.;
  226. }
  227. /* interpolate the compander settings */
  228. for(i=0;i<NOISE_COMPAND_LEVELS;i++)
  229. p->noisecompand[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
  230. return(0);
  231. }
  232. static int vorbis_encode_peak_setup(vorbis_info *vi,double q,int block,
  233. double *guard,
  234. double *suppress,
  235. vp_adjblock *in){
  236. int i,j,iq;
  237. double dq;
  238. codec_setup_info *ci=vi->codec_setup;
  239. vorbis_info_psy *p=ci->psy_param[block];
  240. iq=q*10;
  241. if(iq==10){
  242. iq=9;
  243. dq=1.;
  244. }else{
  245. dq=q*10.-iq;
  246. }
  247. p->peakattp=1;
  248. p->tone_guard=guard[iq]*(1.-dq)+guard[iq+1]*dq;
  249. p->tone_abs_limit=suppress[iq]*(1.-dq)+suppress[iq+1]*dq;
  250. iq=q*5.;
  251. if(iq==5){
  252. iq=5;
  253. dq=1.;
  254. }else{
  255. dq=q*5.-iq;
  256. }
  257. for(i=0;i<P_BANDS;i++)
  258. for(j=0;j<P_LEVELS;j++)
  259. p->peakatt.block[i][j]=(j<4?4:j)*-10.+
  260. in[iq].block[i][j]*(1.-dq)+in[iq+1].block[i][j]*dq;
  261. return(0);
  262. }
  263. static int vorbis_encode_noisebias_setup(vorbis_info *vi,double q,int block,
  264. double *suppress,
  265. int in[][17],int guard[33]){
  266. int i,iq=q*10;
  267. double dq;
  268. codec_setup_info *ci=vi->codec_setup;
  269. vorbis_info_psy *p=ci->psy_param[block];
  270. if(iq==10){
  271. iq=9;
  272. dq=1.;
  273. }else{
  274. dq=q*10.-iq;
  275. }
  276. p->noisemaxsupp=suppress[iq]*(1.-dq)+suppress[iq+1]*dq;
  277. p->noisewindowlomin=guard[iq*3];
  278. p->noisewindowhimin=guard[iq*3+1];
  279. p->noisewindowfixed=guard[iq*3+2];
  280. for(i=0;i<P_BANDS;i++)
  281. p->noiseoff[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
  282. return(0);
  283. }
  284. static int vorbis_encode_ath_setup(vorbis_info *vi,double q,int block,
  285. float in[][27], ...){
  286. int i,iq=q*10;
  287. double x[11],dq;
  288. codec_setup_info *ci=vi->codec_setup;
  289. vorbis_info_psy *p=ci->psy_param[block];
  290. va_list ap;
  291. va_start(ap,in);
  292. for(i=0;i<11;i++)
  293. x[i]=va_arg(ap,double);
  294. va_end(ap);
  295. p->ath_adjatt=ci->hi.ath_floating_dB;
  296. p->ath_maxatt=ci->hi.ath_absolute_dB;
  297. if(iq==10){
  298. iq=9;
  299. dq=1.;
  300. }else{
  301. dq=q*10.-iq;
  302. }
  303. dq=x[iq]*(1.-dq)+x[iq+1]*dq;
  304. iq=(int)dq;
  305. dq-=iq;
  306. if(dq==0 && iq>0){
  307. iq--;
  308. dq=1.;
  309. }
  310. for(i=0;i<27;i++)
  311. p->ath[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
  312. return(0);
  313. }
  314. static int book_dup_or_new(codec_setup_info *ci,static_codebook *book){
  315. int i;
  316. for(i=0;i<ci->books;i++)
  317. if(ci->book_param[i]==book)return(i);
  318. return(ci->books++);
  319. }
  320. static int vorbis_encode_residue_setup(vorbis_info *vi,double q,int block,
  321. int coupled_p,
  322. int stereo_backfill_p,
  323. int residue_backfill_p,
  324. vorbis_residue_template *in,
  325. int point_dB,
  326. double point_kHz){
  327. int i,iq=q*10;
  328. int n,k;
  329. int partition_position;
  330. int res_position;
  331. int iterations=1;
  332. int amplitude_select=0;
  333. codec_setup_info *ci=vi->codec_setup;
  334. vorbis_info_residue0 *r;
  335. vorbis_info_psy *psy=ci->psy_param[block*2];
  336. /* may be re-called due to ctl */
  337. if(ci->residue_param[block])
  338. /* free preexisting instance */
  339. residue_free_info(ci->residue_param[block],ci->residue_type[block]);
  340. r=ci->residue_param[block]=malloc(sizeof(*r));
  341. memcpy(r,in[iq].res[block],sizeof(*r));
  342. if(ci->residues<=block)ci->residues=block+1;
  343. if(block){
  344. r->grouping=32;
  345. }else{
  346. r->grouping=16;
  347. }
  348. /* for uncoupled, we use type 1, else type 2 */
  349. if(coupled_p){
  350. ci->residue_type[block]=2;
  351. }else{
  352. ci->residue_type[block]=1;
  353. }
  354. switch(ci->residue_type[block]){
  355. case 1:
  356. n=r->end=ci->blocksizes[block?1:0]>>1; /* to be adjusted by lowpass later */
  357. partition_position=rint(point_kHz*1000./(vi->rate/2)*n/r->grouping);
  358. res_position=partition_position*r->grouping;
  359. break;
  360. case 2:
  361. n=r->end=(ci->blocksizes[block?1:0]>>1)*vi->channels; /* to be adjusted by lowpass later */
  362. partition_position=rint(point_kHz*1000./(vi->rate/2)*n/r->grouping);
  363. res_position=partition_position*r->grouping/vi->channels;
  364. break;
  365. }
  366. for(i=0;i<r->partitions;i++)
  367. if(r->blimit[i]<0)r->blimit[i]=partition_position;
  368. for(i=0;i<r->partitions;i++)
  369. for(k=0;k<3;k++)
  370. if(in[iq].books_base[point_dB][i][k])
  371. r->secondstages[i]|=(1<<k);
  372. ci->passlimit[0]=3;
  373. if(coupled_p){
  374. vorbis_info_mapping0 *map=ci->map_param[block];
  375. map->coupling_steps=1;
  376. map->coupling_mag[0]=0;
  377. map->coupling_ang[0]=1;
  378. psy->couple_pass[0].granulem=1.;
  379. psy->couple_pass[0].igranulem=1.;
  380. psy->couple_pass[0].couple_pass[0].limit=res_position;
  381. psy->couple_pass[0].couple_pass[0].outofphase_redundant_flip_p=1;
  382. psy->couple_pass[0].couple_pass[0].outofphase_requant_limit=9e10;
  383. psy->couple_pass[0].couple_pass[0].amppost_point=0;
  384. psy->couple_pass[0].couple_pass[1].limit=9999;
  385. psy->couple_pass[0].couple_pass[1].outofphase_redundant_flip_p=1;
  386. psy->couple_pass[0].couple_pass[1].outofphase_requant_limit=9e10;
  387. psy->couple_pass[0].couple_pass[1].amppost_point=
  388. stereo_threshholds[point_dB];
  389. amplitude_select=point_dB;
  390. if(stereo_backfill_p && amplitude_select){
  391. memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
  392. sizeof(*psy->couple_pass));
  393. psy->couple_pass[1].couple_pass[1].amppost_point=stereo_threshholds[amplitude_select-1];
  394. ci->passlimit[1]=4;
  395. for(i=0;i<r->partitions;i++)
  396. if(in[iq].books_stereo_backfill[amplitude_select][i])
  397. r->secondstages[i]|=8;
  398. amplitude_select=amplitude_select-1;
  399. iterations++;
  400. }
  401. if(residue_backfill_p){
  402. memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
  403. sizeof(*psy->couple_pass));
  404. psy->couple_pass[iterations].granulem=.333333333;
  405. psy->couple_pass[iterations].igranulem=3.;
  406. psy->couple_pass[iterations].couple_pass[0].outofphase_requant_limit=1.;
  407. psy->couple_pass[iterations].couple_pass[1].outofphase_requant_limit=1.;
  408. for(i=0;i<r->partitions;i++)
  409. if(in[iq].books_residue_backfill[amplitude_select][i][0])
  410. r->secondstages[i]|=(1<<(iterations+2));
  411. ci->passlimit[iterations]=ci->passlimit[iterations-1]+1;
  412. iterations++;
  413. memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
  414. sizeof(*psy->couple_pass));
  415. psy->couple_pass[iterations].granulem=.1111111111;
  416. psy->couple_pass[iterations].igranulem=9.;
  417. psy->couple_pass[iterations].couple_pass[0].outofphase_requant_limit=.3;
  418. psy->couple_pass[iterations].couple_pass[1].outofphase_requant_limit=.3;
  419. for(i=0;i<r->partitions;i++)
  420. if(in[iq].books_residue_backfill[amplitude_select][i][1])
  421. r->secondstages[i]|=(1<<(iterations+2));
  422. ci->passlimit[iterations]=ci->passlimit[iterations-1]+1;
  423. iterations++;
  424. }
  425. ci->coupling_passes=iterations;
  426. }else{
  427. if(residue_backfill_p){
  428. for(i=0;i<r->partitions;i++){
  429. if(in[iq].books_residue_backfill[0][i][0])
  430. r->secondstages[i]|=8;
  431. if(in[iq].books_residue_backfill[0][i][1])
  432. r->secondstages[i]|=16;
  433. }
  434. ci->passlimit[1]=4;
  435. ci->passlimit[2]=5;
  436. ci->coupling_passes=3;
  437. }else
  438. ci->coupling_passes=1;
  439. }
  440. memcpy(&ci->psy_param[block*2+1]->couple_pass,
  441. &ci->psy_param[block*2]->couple_pass,
  442. sizeof(psy->couple_pass));
  443. /* fill in all the books */
  444. {
  445. int booklist=0,k;
  446. r->groupbook=ci->books;
  447. ci->book_param[ci->books++]=in[iq].book_aux[block];
  448. for(i=0;i<r->partitions;i++){
  449. for(k=0;k<3;k++){
  450. if(in[iq].books_base[point_dB][i][k]){
  451. int bookid=book_dup_or_new(ci,in[iq].books_base[point_dB][i][k]);
  452. r->booklist[booklist++]=bookid;
  453. ci->book_param[bookid]=in[iq].books_base[point_dB][i][k];
  454. }
  455. }
  456. if(coupled_p && stereo_backfill_p && point_dB &&
  457. in[iq].books_stereo_backfill[point_dB][i]){
  458. int bookid=book_dup_or_new(ci,in[iq].books_stereo_backfill[point_dB][i]);
  459. r->booklist[booklist++]=bookid;
  460. ci->book_param[bookid]=in[iq].books_stereo_backfill[point_dB][i];
  461. }
  462. if(residue_backfill_p){
  463. for(k=0;k<2;k++){
  464. if(in[iq].books_residue_backfill[amplitude_select][i][k]){
  465. int bookid=book_dup_or_new(ci,in[iq].books_residue_backfill[amplitude_select][i][k]);
  466. r->booklist[booklist++]=bookid;
  467. ci->book_param[bookid]=in[iq].books_residue_backfill[amplitude_select][i][k];
  468. }
  469. }
  470. }
  471. }
  472. }
  473. return(0);
  474. }
  475. static int vorbis_encode_lowpass_setup(vorbis_info *vi,double q,int block){
  476. int iq=q*10;
  477. double dq;
  478. double freq;
  479. codec_setup_info *ci=vi->codec_setup;
  480. vorbis_info_floor1 *f=ci->floor_param[block];
  481. vorbis_info_residue0 *r=ci->residue_param[block];
  482. int blocksize=ci->blocksizes[block]>>1;
  483. double nyq=vi->rate/2.;
  484. if(iq==10){
  485. iq=9;
  486. dq=1.;
  487. }else{
  488. dq=q*10.-iq;
  489. }
  490. freq=ci->hi.lowpass_kHz[block]*1000.;
  491. if(freq>vi->rate/2)freq=vi->rate/2;
  492. /* lowpass needs to be set in the floor and the residue. */
  493. /* in the floor, the granularity can be very fine; it doesn't alter
  494. the encoding structure, only the samples used to fit the floor
  495. approximation */
  496. f->n=freq/nyq*blocksize;
  497. /* in the residue, we're constrained, physically, by partition
  498. boundaries. We still lowpass 'wherever', but we have to round up
  499. here to next boundary, or the vorbis spec will round it *down* to
  500. previous boundary in encode/decode */
  501. if(ci->residue_type[block]==2)
  502. r->end=(int)((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
  503. r->grouping;
  504. else
  505. r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
  506. r->grouping;
  507. return(0);
  508. }
  509. /* encoders will need to use vorbis_info_init beforehand and call
  510. vorbis_info clear when all done */
  511. /* two interfaces; this, more detailed one, and later a convenience
  512. layer on top */
  513. /* the final setup call */
  514. int vorbis_encode_setup_init(vorbis_info *vi){
  515. int ret=0;
  516. long rate=vi->rate;
  517. long channels=vi->channels;
  518. codec_setup_info *ci=vi->codec_setup;
  519. highlevel_encode_setup *hi=&ci->hi;
  520. ret|=vorbis_encode_floor_setup(vi,hi->base_quality_short,0,
  521. _floor_44_128_books,_floor_44_128,
  522. 0,1,1,2,2,2,2,2,2,2,2);
  523. ret|=vorbis_encode_floor_setup(vi,hi->base_quality_long,1,
  524. _floor_44_1024_books,_floor_44_1024,
  525. 0,0,0,0,0,0,0,0,0,0,0);
  526. ret|=vorbis_encode_global_psych_setup(vi,hi->trigger_quality,_psy_global_44,
  527. 0., 1., 1.5, 2., 2., 2., 2., 2., 2., 2., 2.);
  528. ret|=vorbis_encode_psyset_setup(vi,0);
  529. ret|=vorbis_encode_psyset_setup(vi,1);
  530. ret|=vorbis_encode_psyset_setup(vi,2);
  531. ret|=vorbis_encode_psyset_setup(vi,3);
  532. ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[0].tone_mask_quality,0,
  533. _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
  534. _vp_tonemask_adj_otherblock);
  535. ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[1].tone_mask_quality,1,
  536. _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
  537. _vp_tonemask_adj_otherblock);
  538. ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[2].tone_mask_quality,2,
  539. _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
  540. _vp_tonemask_adj_otherblock);
  541. ret|=vorbis_encode_tonemask_setup(vi,hi->blocktype[3].tone_mask_quality,3,
  542. _psy_tone_masteratt,_psy_tone_0dB,_psy_ehmer_bandlimit,
  543. _vp_tonemask_adj_longblock);
  544. ret|=vorbis_encode_compand_setup(vi,hi->blocktype[0].noise_compand_quality,
  545. 0,_psy_compand_44_short,
  546. 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
  547. ret|=vorbis_encode_compand_setup(vi,hi->blocktype[1].noise_compand_quality,
  548. 1,_psy_compand_44_short,
  549. 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
  550. ret|=vorbis_encode_compand_setup(vi,hi->blocktype[2].noise_compand_quality,
  551. 2,_psy_compand_44,
  552. 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
  553. ret|=vorbis_encode_compand_setup(vi,hi->blocktype[3].noise_compand_quality,
  554. 3,_psy_compand_44,
  555. 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
  556. ret|=vorbis_encode_peak_setup(vi,hi->blocktype[0].tone_peaklimit_quality,
  557. 0,_psy_tone_masterguard,_psy_tone_suppress,
  558. _vp_peakguard);
  559. ret|=vorbis_encode_peak_setup(vi,hi->blocktype[1].tone_peaklimit_quality,
  560. 1,_psy_tone_masterguard,_psy_tone_suppress,
  561. _vp_peakguard);
  562. ret|=vorbis_encode_peak_setup(vi,hi->blocktype[2].tone_peaklimit_quality,
  563. 2,_psy_tone_masterguard,_psy_tone_suppress,
  564. _vp_peakguard);
  565. ret|=vorbis_encode_peak_setup(vi,hi->blocktype[3].tone_peaklimit_quality,
  566. 3,_psy_tone_masterguard,_psy_tone_suppress,
  567. _vp_peakguard);
  568. ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[0].noise_bias_quality,
  569. 0,_psy_noise_suppress,_psy_noisebias_impulse,
  570. _psy_noiseguards_short);
  571. ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[1].noise_bias_quality,
  572. 1,_psy_noise_suppress,_psy_noisebias_other,
  573. _psy_noiseguards_short);
  574. ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[2].noise_bias_quality,
  575. 2,_psy_noise_suppress,_psy_noisebias_other,
  576. _psy_noiseguards_long);
  577. ret|=vorbis_encode_noisebias_setup(vi,hi->blocktype[3].noise_bias_quality,
  578. 3,_psy_noise_suppress,_psy_noisebias_long,
  579. _psy_noiseguards_long);
  580. ret|=vorbis_encode_ath_setup(vi,hi->blocktype[0].ath_quality,0,ATH_Bark_dB,
  581. 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
  582. ret|=vorbis_encode_ath_setup(vi,hi->blocktype[1].ath_quality,1,ATH_Bark_dB,
  583. 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
  584. ret|=vorbis_encode_ath_setup(vi,hi->blocktype[2].ath_quality,2,ATH_Bark_dB,
  585. 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
  586. ret|=vorbis_encode_ath_setup(vi,hi->blocktype[3].ath_quality,3,ATH_Bark_dB,
  587. 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
  588. if(ret){
  589. vorbis_info_clear(vi);
  590. return ret;
  591. }
  592. if(channels==2 && hi->stereo_couple_p){
  593. /* setup specific to stereo coupling */
  594. ret|=vorbis_encode_residue_setup(vi,hi->base_quality_short,0,
  595. 1, /* coupled */
  596. hi->stereo_backfill_p,
  597. hi->residue_backfill_p,
  598. _residue_template_44_stereo,
  599. hi->stereo_point_dB,
  600. hi->stereo_point_kHz[0]);
  601. ret|=vorbis_encode_residue_setup(vi,hi->base_quality_long,1,
  602. 1, /* coupled */
  603. hi->stereo_backfill_p,
  604. hi->residue_backfill_p,
  605. _residue_template_44_stereo,
  606. hi->stereo_point_dB,
  607. hi->stereo_point_kHz[1]);
  608. }else{
  609. /* setup specific to non-stereo (mono or uncoupled polyphonic)
  610. coupling */
  611. ret|=vorbis_encode_residue_setup(vi,hi->base_quality_short,0,
  612. 0, /* uncoupled */
  613. 0,
  614. hi->residue_backfill_p,
  615. _residue_template_44_uncoupled,
  616. 0,
  617. hi->stereo_point_kHz[0]); /* just
  618. used as an encoding partitioning
  619. point */
  620. ret|=vorbis_encode_residue_setup(vi,hi->base_quality_long,1,
  621. 0, /* uncoupled */
  622. 0,
  623. hi->residue_backfill_p,
  624. _residue_template_44_uncoupled,
  625. 0,
  626. hi->stereo_point_kHz[1]); /* just
  627. used as an encoding partitioning
  628. point */
  629. }
  630. ret|=vorbis_encode_lowpass_setup(vi,hi->lowpass_kHz[0],0);
  631. ret|=vorbis_encode_lowpass_setup(vi,hi->lowpass_kHz[1],1);
  632. if(ret)
  633. vorbis_info_clear(vi);
  634. return(ret);
  635. }
  636. /* only populates the high-level settings so that we can tweak with ctl before final setup */
  637. int vorbis_encode_setup_vbr(vorbis_info *vi,
  638. long channels,
  639. long rate,
  640. float base_quality){
  641. int ret=0,i,iq;
  642. double dq;
  643. codec_setup_info *ci=vi->codec_setup;
  644. highlevel_encode_setup *hi=&ci->hi;
  645. base_quality+=.0001;
  646. if(base_quality<0.)base_quality=0.;
  647. if(base_quality>.999)base_quality=.999;
  648. iq=base_quality*10;
  649. if(iq==10){
  650. iq=9;
  651. dq=1.;
  652. }else{
  653. dq=base_quality*10.-iq;
  654. }
  655. ret|=vorbis_encode_toplevel_setup(vi,256,2048,channels,rate);
  656. hi->base_quality=base_quality;
  657. hi->base_quality_short=base_quality;
  658. hi->base_quality_long=base_quality;
  659. hi->trigger_quality=base_quality;
  660. for(i=0;i<4;i++){
  661. hi->blocktype[i].tone_mask_quality=base_quality;
  662. hi->blocktype[i].tone_peaklimit_quality=base_quality;
  663. hi->blocktype[i].noise_bias_quality=base_quality;
  664. hi->blocktype[i].noise_compand_quality=base_quality;
  665. hi->blocktype[i].ath_quality=base_quality;
  666. }
  667. hi->short_block_p=1;
  668. hi->long_block_p=1;
  669. hi->impulse_block_p=1;
  670. hi->amplitude_track_dBpersec=-6.;
  671. hi->stereo_couple_p=1; /* only relevant if a two channel input */
  672. hi->stereo_backfill_p=0;
  673. hi->residue_backfill_p=0;
  674. /* set the ATH floaters */
  675. hi->ath_floating_dB=_psy_ath_floater[iq]*(1.-dq)+_psy_ath_floater[iq+1]*dq;
  676. hi->ath_absolute_dB=_psy_ath_abs[iq]*(1.-dq)+_psy_ath_abs[iq+1]*dq;
  677. /* set stereo dB and Hz */
  678. hi->stereo_point_dB=_psy_stereo_point_dB_44[iq];
  679. hi->stereo_point_kHz[0]=_psy_stereo_point_kHz_44[0][iq]*(1.-dq)+
  680. _psy_stereo_point_kHz_44[0][iq+1]*dq;
  681. hi->stereo_point_kHz[1]=_psy_stereo_point_kHz_44[1][iq]*(1.-dq)+
  682. _psy_stereo_point_kHz_44[1][iq+1]*dq;
  683. /* set lowpass */
  684. hi->lowpass_kHz[0]=
  685. hi->lowpass_kHz[1]=
  686. _psy_lowpass_44[iq]*(1.-dq)+_psy_lowpass_44[iq+1]*dq;
  687. return(ret);
  688. }
  689. int vorbis_encode_init_vbr(vorbis_info *vi,
  690. long channels,
  691. long rate,
  692. float base_quality /* 0. to 1. */
  693. ){
  694. int ret=0;
  695. ret=vorbis_encode_setup_vbr(vi,channels,rate,1.);
  696. {
  697. codec_setup_info *ci=vi->codec_setup;
  698. highlevel_encode_setup *hi=&ci->hi;
  699. hi->stereo_couple_p=0;
  700. hi->residue_backfill_p=1;
  701. }
  702. if(ret){
  703. vorbis_info_clear(vi);
  704. return ret;
  705. }
  706. ret=vorbis_encode_setup_init(vi);
  707. if(ret)
  708. vorbis_info_clear(vi);
  709. return(ret);
  710. }
  711. int vorbis_encode_init(vorbis_info *vi,
  712. long channels,
  713. long rate,
  714. long max_bitrate,
  715. long nominal_bitrate,
  716. long min_bitrate){
  717. /* it's temporary while I do the merge; relax */
  718. if(rate>40000){
  719. if(nominal_bitrate>360000){
  720. return(vorbis_encode_init_vbr(vi,channels,rate, 1.));
  721. }else if(nominal_bitrate>270000){
  722. return(vorbis_encode_init_vbr(vi,channels,rate, .9));
  723. }else if(nominal_bitrate>230000){
  724. return(vorbis_encode_init_vbr(vi,channels,rate, .8));
  725. }else if(nominal_bitrate>200000){
  726. return(vorbis_encode_init_vbr(vi,channels,rate, .7));
  727. }else if(nominal_bitrate>180000){
  728. return(vorbis_encode_init_vbr(vi,channels,rate, .6));
  729. }else if(nominal_bitrate>140000){
  730. return(vorbis_encode_init_vbr(vi,channels,rate, .5));
  731. }else if(nominal_bitrate>120000){
  732. return(vorbis_encode_init_vbr(vi,channels,rate, .4));
  733. }else if(nominal_bitrate>100000){
  734. return(vorbis_encode_init_vbr(vi,channels,rate, .3));
  735. }else if(nominal_bitrate>90000){
  736. return(vorbis_encode_init_vbr(vi,channels,rate, .2));
  737. }else if(nominal_bitrate>75000){
  738. return(vorbis_encode_init_vbr(vi,channels,rate, .1));
  739. }else{
  740. return(vorbis_encode_init_vbr(vi,channels,rate, .0));
  741. }
  742. }
  743. return(OV_EIMPL);
  744. }
  745. int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
  746. return(OV_EIMPL);
  747. }