1 /********************************************************************
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. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************
13 function: simple programmatic interface for encoder mode setup
14 last mod: $Id: vorbisenc.c,v 1.22 2001/12/14 09:37:58 xiphmont Exp $
16 ********************************************************************/
23 #include "vorbis/codec.h"
24 #include "vorbis/vorbisenc.h"
26 #include "codec_internal.h"
27 #include "registry-api.h"
32 /* careful with this; it's using static array sizing to make managing
33 all the modes a little less annoying. If we use a residue backend
34 with > 10 partition types, or a different division of iteration,
35 this needs to be updated. */
37 vorbis_info_residue0 *res[2];
38 static_codebook *book_aux[2];
39 static_codebook *books_base[5][10][3];
40 static_codebook *books_stereo_backfill[5][10];
41 static_codebook *books_residue_backfill[5][10][2];
42 } vorbis_residue_template;
44 static double stereo_threshholds[]={0.0, 2.5, 4.5, 8.5, 16.5};
46 typedef struct vp_adjblock{
47 int block[P_BANDS][P_LEVELS];
50 #include "modes/residue_44.h"
51 #include "modes/psych_44.h"
52 #include "modes/floor_44.h"
54 /* a few static coder conventions */
55 static vorbis_info_time0 _time_dummy={0};
56 static vorbis_info_mode _mode_set_short={0,0,0,0};
57 static vorbis_info_mode _mode_set_long={1,0,0,1};
59 /* mapping conventions:
60 only one submap (this would change for efficient 5.1 support for example)*/
61 /* Four psychoacoustic profiles are used, one for each blocktype */
62 static vorbis_info_mapping0 _mapping_set_short={
63 1, {0,0}, {0}, {0}, {0}, {0,1}, 0,{0},{0}};
64 static vorbis_info_mapping0 _mapping_set_long={
65 1, {0,0}, {0}, {1}, {1}, {2,3}, 0,{0},{0}};
67 static int vorbis_encode_toplevel_init(vorbis_info *vi,int small,int large,int ch,long rate){
68 if(vi && vi->codec_setup){
69 codec_setup_info *ci=vi->codec_setup;
75 ci->blocksizes[0]=small;
76 ci->blocksizes[1]=large;
78 /* time mapping hooks are unused in vorbis I */
81 ci->time_param[0]=calloc(1,sizeof(_time_dummy));
82 memcpy(ci->time_param[0],&_time_dummy,sizeof(_time_dummy));
84 /* by convention, two modes: one for short, one for long blocks.
85 short block mode uses mapping sero, long block uses mapping 1 */
87 ci->mode_param[0]=calloc(1,sizeof(_mode_set_short));
88 memcpy(ci->mode_param[0],&_mode_set_short,sizeof(_mode_set_short));
89 ci->mode_param[1]=calloc(1,sizeof(_mode_set_long));
90 memcpy(ci->mode_param[1],&_mode_set_long,sizeof(_mode_set_long));
92 /* by convention two mappings, both mapping type zero (polyphonic
93 PCM), first for short, second for long blocks */
96 ci->map_param[0]=calloc(1,sizeof(_mapping_set_short));
97 memcpy(ci->map_param[0],&_mapping_set_short,sizeof(_mapping_set_short));
99 ci->map_param[1]=calloc(1,sizeof(_mapping_set_long));
100 memcpy(ci->map_param[1],&_mapping_set_long,sizeof(_mapping_set_long));
107 static int vorbis_encode_floor_init(vorbis_info *vi,double q,int block,
108 static_codebook ***books,
109 vorbis_info_floor1 *in,
111 int x[11],i,k,iq=rint(q*10);
112 vorbis_info_floor1 *f=calloc(1,sizeof(*f));
113 codec_setup_info *ci=vi->codec_setup;
121 memcpy(f,in+x[iq],sizeof(*f));
122 /* fill in the lowpass field, even if it's temporary */
123 f->n=ci->blocksizes[block]>>1;
127 int partitions=f->partitions;
130 for(i=0;i<partitions;i++)
131 if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
132 for(i=0;i<=maxclass;i++){
133 if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
134 f->class_book[i]+=ci->books;
135 for(k=0;k<(1<<f->class_subs[i]);k++){
136 if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
137 if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
141 for(i=0;i<=maxbook;i++)
142 ci->book_param[ci->books++]=books[x[iq]][i];
145 /* for now, we're only using floor 1 */
146 ci->floor_type[ci->floors]=1;
147 ci->floor_param[ci->floors]=f;
153 static int vorbis_encode_global_psych_init(vorbis_info *vi,double q,
154 vorbis_info_psy_global *in, ...){
157 codec_setup_info *ci=vi->codec_setup;
158 vorbis_info_psy_global *g=&ci->psy_g_param;
163 x[i]=va_arg(ap,double);
173 memcpy(g,in+(int)x[iq],sizeof(*g));
175 dq=x[iq]*(1.-dq)+x[iq+1]*dq;
183 /* interpolate the trigger threshholds */
185 g->preecho_thresh[i]=in[iq].preecho_thresh[i]*(1.-dq)+in[iq+1].preecho_thresh[i]*dq;
186 g->postecho_thresh[i]=in[iq].postecho_thresh[i]*(1.-dq)+in[iq+1].postecho_thresh[i]*dq;
188 g->ampmax_att_per_sec=in[iq].ampmax_att_per_sec*(1.-dq)+in[iq+1].ampmax_att_per_sec*dq;
192 static int vorbis_encode_psyset_init(vorbis_info *vi,double q,int block,
193 vorbis_info_psy *in){
196 codec_setup_info *ci=vi->codec_setup;
197 vorbis_info_psy *p=ci->psy_param[block];
209 p=calloc(1,sizeof(*p));
210 ci->psy_param[block]=p;
213 memcpy(p,in+(int)(q*10.),sizeof(*p));
215 p->ath_adjatt=in[iq].ath_adjatt*(1.-dq)+in[iq+1].ath_adjatt*dq;
216 p->ath_maxatt=in[iq].ath_maxatt*(1.-dq)+in[iq+1].ath_maxatt*dq;
218 p->tone_masteratt=in[iq].tone_masteratt*(1.-dq)+in[iq+1].tone_masteratt*dq;
219 p->tone_guard=in[iq].tone_guard*(1.-dq)+in[iq+1].tone_guard*dq;
220 p->tone_abs_limit=in[iq].tone_abs_limit*(1.-dq)+in[iq+1].tone_abs_limit*dq;
222 p->noisemaxsupp=in[iq].noisemaxsupp*(1.-dq)+in[iq+1].noisemaxsupp*dq;
227 static int vorbis_encode_compand_init(vorbis_info *vi,double q,int block,
228 float in[][NOISE_COMPAND_LEVELS], ...){
231 codec_setup_info *ci=vi->codec_setup;
232 vorbis_info_psy *p=ci->psy_param[block];
237 x[i]=va_arg(ap,double);
247 dq=x[iq]*(1.-dq)+x[iq+1]*dq;
255 /* interpolate the compander settings */
256 for(i=0;i<NOISE_COMPAND_LEVELS;i++)
257 p->noisecompand[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
261 static int vorbis_encode_tonemask_init(vorbis_info *vi,double q,int block,
265 codec_setup_info *ci=vi->codec_setup;
266 vorbis_info_psy *p=ci->psy_param[block];
275 for(i=0;i<P_BANDS;i++)
276 for(j=0;j<P_LEVELS;j++)
277 p->toneatt.block[i][j]=(j<4?4:j)*-10.+
278 in[iq].block[i][j]*(1.-dq)+in[iq+1].block[i][j]*dq;
282 static int vorbis_encode_peak_init(vorbis_info *vi,double q,int block,
286 codec_setup_info *ci=vi->codec_setup;
287 vorbis_info_psy *p=ci->psy_param[block];
296 for(i=0;i<P_BANDS;i++)
297 for(j=0;j<P_LEVELS;j++)
298 p->peakatt.block[i][j]=(j<4?4:j)*-10.+
299 in[iq].block[i][j]*(1.-dq)+in[iq+1].block[i][j]*dq;
303 static int vorbis_encode_noisebias_init(vorbis_info *vi,double q,int block,
304 int in[][17],int guard[33]){
307 codec_setup_info *ci=vi->codec_setup;
308 vorbis_info_psy *p=ci->psy_param[block];
317 p->noisewindowlomin=guard[iq*3];
318 p->noisewindowhimin=guard[iq*3+1];
319 p->noisewindowfixed=guard[iq*3+2];
321 for(i=0;i<P_BANDS;i++)
322 p->noiseoff[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
326 static int vorbis_encode_ath_init(vorbis_info *vi,double q,int block,
327 float in[][27], ...){
330 codec_setup_info *ci=vi->codec_setup;
331 vorbis_info_psy *p=ci->psy_param[block];
336 x[i]=va_arg(ap,double);
346 dq=x[iq]*(1.-dq)+x[iq+1]*dq;
355 p->ath[i]=in[iq][i]*(1.-dq)+in[iq+1][i]*dq;
359 static int vorbis_encode_residue_init(vorbis_info *vi,double q,int block,
361 int stereo_backfill_p,
362 int residue_backfill_p,
363 vorbis_residue_template *in, ...){
369 int partition_position;
372 int amplitude_select;
374 codec_setup_info *ci=vi->codec_setup;
375 vorbis_info_residue0 *r;
376 vorbis_info_psy *psy=ci->psy_param[block*2];
383 c[i]=va_arg(ap,double);
386 /* may be re-called due to ctl */
387 if(ci->residue_param[block])
388 /* free preexisting instance */
389 residue_free_info(ci->residue_param[block],ci->residue_type[block]);
391 r=ci->residue_param[block]=malloc(sizeof(*r));
392 memcpy(r,in[iq].res[block],sizeof(*r));
393 if(ci->residues<=block)ci->residues=block+1;
401 /* for uncoupled, we use type 1, else type 2 */
403 ci->residue_type[block]=2;
405 ci->residue_type[block]=1;
408 switch(ci->residue_type[block]){
410 n=r->end=ci->blocksizes[block?1:0]>>1; /* to be adjusted by lowpass later */
411 partition_position=rint((double)c[iq]*1000/(vi->rate/2)*n/r->grouping);
412 res_position=partition_position*r->grouping;
415 n=r->end=(ci->blocksizes[block?1:0]>>1)*vi->channels; /* to be adjusted by lowpass later */
416 partition_position=rint((double)c[iq]*1000/(vi->rate/2)*n/r->grouping);
417 res_position=partition_position*r->grouping/vi->channels;
421 for(i=0;i<r->partitions;i++)
422 if(r->blimit[i]<0)r->blimit[i]=partition_position;
426 vorbis_info_mapping0 *map=ci->map_param[block];
428 map->coupling_steps=1;
429 map->coupling_mag[0]=0;
430 map->coupling_ang[0]=1;
432 psy->couple_pass[0].granulem=1.;
433 psy->couple_pass[0].igranulem=1.;
435 psy->couple_pass[0].couple_pass[0].limit=res_position;
436 psy->couple_pass[0].couple_pass[0].outofphase_redundant_flip_p=1;
437 psy->couple_pass[0].couple_pass[0].outofphase_requant_limit=9e10;
438 psy->couple_pass[0].couple_pass[0].amppost_point=0;
439 psy->couple_pass[0].couple_pass[1].limit=9999;
440 psy->couple_pass[0].couple_pass[1].outofphase_redundant_flip_p=1;
441 psy->couple_pass[0].couple_pass[1].outofphase_requant_limit=9e10;
442 psy->couple_pass[0].couple_pass[1].amppost_point=stereo_threshholds[a[iq]];
443 amplitude_select=a[iq];
445 for(i=0;i<r->partitions;i++)
447 if(in[iq].books_base[a[iq]][i][k])
448 r->secondstages[i]|=(1<<k);
452 if(stereo_backfill_p && a[iq]){
453 memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
454 sizeof(*psy->couple_pass));
455 amplitude_select=a[iq]-1;
456 psy->couple_pass[1].couple_pass[1].amppost_point=stereo_threshholds[a[iq]-1];
458 for(i=0;i<r->partitions;i++)
459 if(in[iq].books_stereo_backfill[a[iq]-1][i])
460 r->secondstages[i]|=8;
464 if(residue_backfill_p){
465 memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
466 sizeof(*psy->couple_pass));
467 psy->couple_pass[iterations].granulem=.333333333;
468 psy->couple_pass[iterations].igranulem=3.;
469 for(i=0;i<r->partitions;i++)
470 if(in[iq].books_residue_backfill[amplitude_select][i][0])
471 r->secondstages[i]|=(1<<(iterations+2));
472 ci->passlimit[iterations]=ci->passlimit[iterations-1]+1;
475 memcpy(psy->couple_pass+iterations,psy->couple_pass+iterations-1,
476 sizeof(*psy->couple_pass));
477 psy->couple_pass[iterations].granulem=.1111111111;
478 psy->couple_pass[iterations].igranulem=9.;
479 for(i=0;i<r->partitions;i++)
480 if(in[iq].books_residue_backfill[amplitude_select][i][1])
481 r->secondstages[i]|=(1<<(iterations+2));
482 ci->passlimit[iterations]=ci->passlimit[iterations-1]+1;
485 ci->coupling_passes=iterations;
490 if(residue_backfill_p){
491 for(i=0;i<r->partitions;i++){
492 if(in[iq].books_residue_backfill[amplitude_select][i][0])
493 r->secondstages[i]|=8;
494 if(in[iq].books_residue_backfill[amplitude_select][i][1])
495 r->secondstages[i]|=16;
499 ci->coupling_passes=3;
501 ci->coupling_passes=1;
504 memcpy(&ci->psy_param[block*2+1]->couple_pass,
505 &ci->psy_param[block*2]->couple_pass,
506 sizeof(psy->couple_pass[0]));
508 /* fill in all the books */
511 r->groupbook=ci->books;
512 ci->book_param[ci->books++]=in[iq].book_aux[block];
513 for(i=0;i<r->partitions;i++){
515 if(in[iq].books_base[a[iq]][i][k]){
516 r->booklist[booklist++]=ci->books;
517 ci->book_param[ci->books++]=in[iq].books_base[a[iq]][i][k];
519 if(coupled_p && stereo_backfill_p && a[iq] &&
520 in[iq].books_stereo_backfill[a[iq]][i]){
521 r->booklist[booklist++]=ci->books;
522 ci->book_param[ci->books++]=in[iq].books_stereo_backfill[a[iq]][i];
524 if(residue_backfill_p)
526 if(in[iq].books_residue_backfill[amplitude_select][i][k]){
527 r->booklist[booklist++]=ci->books;
528 ci->book_param[ci->books++]=in[iq].books_residue_backfill[amplitude_select][i][k];
538 static int vorbis_encode_lowpass_init(vorbis_info *vi,double q,int block,...){
542 codec_setup_info *ci=vi->codec_setup;
543 vorbis_info_floor1 *f=ci->floor_param[block];
544 vorbis_info_residue0 *r=ci->residue_param[block];
545 int blocksize=ci->blocksizes[block]>>1;
546 double nyq=vi->rate/2.;
551 x[i]=va_arg(ap,double);
561 freq=(x[iq]*(1.-dq)+x[iq+1]*dq)*1000.;
562 if(freq>vi->rate/2)freq=vi->rate/2;
563 /* lowpass needs to be set in the floor and the residue. */
565 /* in the floor, the granularity can be very fine; it doesn't alter
566 the encoding structure, only the samples used to fit the floor
568 f->n=freq/nyq*blocksize;
570 /* in the residue, we're constrained, physically, by partition
571 boundaries. We still lowpass 'wherever', but we have to round up
572 here to next boundary, or the vorbis spec will round it *down* to
573 previous boundary in encode/decode */
574 if(ci->residue_type[block]==2)
575 r->end=((freq/nyq*blocksize*2)/r->grouping+.9)* /* round up only if we're well past */
578 r->end=((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
583 /* encoders will need to use vorbis_info_init beforehand and call
584 vorbis_info clear when all done */
586 int vorbis_encode_init_vbr(vorbis_info *vi,
590 float base_quality /* 0. to 1. */
595 if(base_quality<0.)base_quality=0.;
596 if(base_quality>.999)base_quality=.999;
599 ret|=vorbis_encode_toplevel_init(vi,256,2048,channels,rate);
600 ret|=vorbis_encode_floor_init(vi,base_quality,0,_floor_44_128_books,_floor_44_128,
601 0,1,1,2,2,2,2,2,2,2,2);
602 ret|=vorbis_encode_floor_init(vi,base_quality,1,_floor_44_1024_books,_floor_44_1024,
603 0,0,0,0,0,0,0,0,0,0,0);
605 ret|=vorbis_encode_global_psych_init(vi,base_quality,_psy_global_44,
606 0., 1., 1.5, 2., 2., 2., 2., 2., 2., 2., 2.);
608 ret|=vorbis_encode_psyset_init(vi,base_quality,0,_psy_settings);
609 ret|=vorbis_encode_psyset_init(vi,base_quality,1,_psy_settings);
610 ret|=vorbis_encode_psyset_init(vi,base_quality,2,_psy_settings);
611 ret|=vorbis_encode_psyset_init(vi,base_quality,3,_psy_settings);
613 ret|=vorbis_encode_tonemask_init(vi,base_quality,0,_vp_tonemask_adj_otherblock);
614 ret|=vorbis_encode_tonemask_init(vi,base_quality,1,_vp_tonemask_adj_otherblock);
615 ret|=vorbis_encode_tonemask_init(vi,base_quality,2,_vp_tonemask_adj_otherblock);
616 ret|=vorbis_encode_tonemask_init(vi,base_quality,3,_vp_tonemask_adj_longblock);
618 ret|=vorbis_encode_compand_init(vi,base_quality,0,_psy_compand_44_short,
619 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
620 ret|=vorbis_encode_compand_init(vi,base_quality,1,_psy_compand_44_short,
621 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
622 ret|=vorbis_encode_compand_init(vi,base_quality,2,_psy_compand_44,
623 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
624 ret|=vorbis_encode_compand_init(vi,base_quality,3,_psy_compand_44,
625 1., 1., 1.3, 1.6, 2., 2., 2., 2., 2., 2., 2.);
627 ret|=vorbis_encode_peak_init(vi,base_quality,0,_vp_peakguard);
628 ret|=vorbis_encode_peak_init(vi,base_quality,1,_vp_peakguard);
629 ret|=vorbis_encode_peak_init(vi,base_quality,2,_vp_peakguard);
630 ret|=vorbis_encode_peak_init(vi,base_quality,3,_vp_peakguard);
632 ret|=vorbis_encode_noisebias_init(vi,base_quality,0,_psy_noisebias_impulse,
633 _psy_noiseguards_short);
634 ret|=vorbis_encode_noisebias_init(vi,base_quality,1,_psy_noisebias_other,
635 _psy_noiseguards_short);
636 ret|=vorbis_encode_noisebias_init(vi,base_quality,2,_psy_noisebias_other,
637 _psy_noiseguards_long);
638 ret|=vorbis_encode_noisebias_init(vi,base_quality,3,_psy_noisebias_long,
639 _psy_noiseguards_long);
641 ret|=vorbis_encode_ath_init(vi,base_quality,0,ATH_Bark_dB,
642 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
643 ret|=vorbis_encode_ath_init(vi,base_quality,1,ATH_Bark_dB,
644 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
645 ret|=vorbis_encode_ath_init(vi,base_quality,2,ATH_Bark_dB,
646 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
647 ret|=vorbis_encode_ath_init(vi,base_quality,3,ATH_Bark_dB,
648 0., 0., 0., 0., .2, .5, 1., 1., 1.5, 2., 2.);
651 vorbis_info_clear(vi);
657 /* setup specific to stereo coupling */
659 /* unmanaged, one iteration residue setup */
660 ret|=vorbis_encode_residue_init(vi,base_quality,0,
662 0, /* no mid stereo backfill */
663 0, /* no residue backfill */
664 _residue_template_44_stereo,
665 4, 3, 2, 2, 1, 0, 0, 0, 0, 0, 0,
666 4., 6., 6., 6., 10., 6., 6., 4., 4., 4., 4.);
668 ret|=vorbis_encode_residue_init(vi,base_quality,1,
670 0, /* no mid stereo backfill */
671 0, /* no residue backfill */
672 _residue_template_44_stereo,
673 4, 3, 2, 2, 1, 0, 0, 0, 0, 0, 0,
674 6., 6., 6., 10., 10., 6., 6., 4., 4., 4., 4.);
676 ret|=vorbis_encode_lowpass_init(vi,base_quality,0,
677 15.1,15.8,16.5,17.9,20.5,
678 999.,999.,999.,999.,999.,999.);
679 ret|=vorbis_encode_lowpass_init(vi,base_quality,1,
680 15.1,15.8,16.5,17.9,20.5,
681 999.,999.,999.,999.,999.,999.);
687 /* setup specific to non-stereo (mono or uncoupled polyphonic)
700 vorbis_info_clear(vi);
705 int vorbis_encode_init(vorbis_info *vi,
710 long nominal_bitrate,
713 /* it's temporary while I do the merge; relax */
715 if(nominal_bitrate>360000){
716 return(vorbis_encode_init_vbr(vi,channels,rate, 1.));
717 }else if(nominal_bitrate>270000){
718 return(vorbis_encode_init_vbr(vi,channels,rate, .9));
719 }else if(nominal_bitrate>230000){
720 return(vorbis_encode_init_vbr(vi,channels,rate, .8));
721 }else if(nominal_bitrate>200000){
722 return(vorbis_encode_init_vbr(vi,channels,rate, .7));
723 }else if(nominal_bitrate>180000){
724 return(vorbis_encode_init_vbr(vi,channels,rate, .6));
725 }else if(nominal_bitrate>140000){
726 return(vorbis_encode_init_vbr(vi,channels,rate, .5));
727 }else if(nominal_bitrate>120000){
728 return(vorbis_encode_init_vbr(vi,channels,rate, .4));
729 }else if(nominal_bitrate>100000){
730 return(vorbis_encode_init_vbr(vi,channels,rate, .3));
731 }else if(nominal_bitrate>90000){
732 return(vorbis_encode_init_vbr(vi,channels,rate, .2));
733 }else if(nominal_bitrate>75000){
734 return(vorbis_encode_init_vbr(vi,channels,rate, .1));
736 return(vorbis_encode_init_vbr(vi,channels,rate, .0));
743 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){