vorbisenc.c 37 KB

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