vorbisenc.c 38.3 KB
Newer Older
Monty's avatar
   
Monty committed
1
2
3
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
Monty's avatar
   
Monty committed
4
5
6
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
Monty's avatar
   
Monty committed
7
 *                                                                  *
Ralph Giles's avatar
Ralph Giles committed
8
 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2015             *
9
 * by the Xiph.Org Foundation http://www.xiph.org/                  *
Monty's avatar
   
Monty committed
10
 *                                                                  *
Monty's avatar
   
Monty committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 ********************************************************************

 function: simple programmatic interface for encoder mode setup

 ********************************************************************/

#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "vorbis/codec.h"
#include "vorbis/vorbisenc.h"

#include "codec_internal.h"

#include "os.h"
#include "misc.h"

Monty's avatar
   
Monty committed
29
30
/* careful with this; it's using static array sizing to make managing
   all the modes a little less annoying.  If we use a residue backend
Monty's avatar
   
Monty committed
31
   with > 12 partition types, or a different division of iteration,
Monty's avatar
   
Monty committed
32
33
   this needs to be updated. */
typedef struct {
Monty's avatar
Monty committed
34
  const static_codebook *books[12][4];
Monty's avatar
   
Monty committed
35
36
37
38
39
} static_bookblock;

typedef struct {
  int res_type;
  int limit_type; /* 0 lowpass limited, 1 point stereo limited */
Monty's avatar
Monty committed
40
  int grouping;
41
42
43
44
45
  const vorbis_info_residue0 *res;
  const static_codebook  *book_aux;
  const static_codebook  *book_aux_managed;
  const static_bookblock *books_base;
  const static_bookblock *books_base_managed;
Monty's avatar
   
Monty committed
46
47
} vorbis_residue_template;

Monty's avatar
   
Monty committed
48
typedef struct {
49
50
  const vorbis_info_mapping0    *map;
  const vorbis_residue_template *res;
Monty's avatar
   
Monty committed
51
} vorbis_mapping_template;
Monty's avatar
   
Monty committed
52
53

typedef struct vp_adjblock{
Monty's avatar
   
Monty committed
54
  int block[P_BANDS];
Monty's avatar
   
Monty committed
55
56
} vp_adjblock;

Monty's avatar
   
Monty committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
typedef struct {
  int data[NOISE_COMPAND_LEVELS];
} compandblock;

/* high level configuration information for setting things up
   step-by-step with the detailed vorbis_encode_ctl interface.
   There's a fair amount of redundancy such that interactive setup
   does not directly deal with any vorbis_info or codec_setup_info
   initialization; it's all stored (until full init) in this highlevel
   setup, then flushed out to the real codec setup structs later. */

typedef struct {
  int att[P_NOISECURVES];
  float boost;
  float decay;
} att3;
Monty's avatar
Monty committed
73
typedef struct { int data[P_NOISECURVES]; } adj3;
Monty's avatar
   
Monty committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

typedef struct {
  int   pre[PACKETBLOBS];
  int   post[PACKETBLOBS];
  float kHz[PACKETBLOBS];
  float lowpasskHz[PACKETBLOBS];
} adj_stereo;

typedef struct {
  int lo;
  int hi;
  int fixed;
} noiseguard;
typedef struct {
  int data[P_NOISECURVES][17];
} noise3;

typedef struct {
  int      mappings;
93
94
  const double  *rate_mapping;
  const double  *quality_mapping;
Monty's avatar
   
Monty committed
95
  int      coupling_restriction;
Monty's avatar
   
Monty committed
96
97
  long     samplerate_min_restriction;
  long     samplerate_max_restriction;
Monty's avatar
   
Monty committed
98
99


100
101
  const int     *blocksize_short;
  const int     *blocksize_long;
Monty's avatar
   
Monty committed
102

103
104
105
  const att3    *psy_tone_masteratt;
  const int     *psy_tone_0dB;
  const int     *psy_tone_dBsuppress;
Monty's avatar
   
Monty committed
106

107
108
109
  const vp_adjblock *psy_tone_adj_impulse;
  const vp_adjblock *psy_tone_adj_long;
  const vp_adjblock *psy_tone_adj_other;
Monty's avatar
   
Monty committed
110

111
112
113
114
115
116
  const noiseguard  *psy_noiseguards;
  const noise3      *psy_noise_bias_impulse;
  const noise3      *psy_noise_bias_padding;
  const noise3      *psy_noise_bias_trans;
  const noise3      *psy_noise_bias_long;
  const int         *psy_noise_dBsuppress;
Monty's avatar
   
Monty committed
117

118
119
120
  const compandblock  *psy_noise_compand;
  const double        *psy_noise_compand_short_mapping;
  const double        *psy_noise_compand_long_mapping;
Monty's avatar
   
Monty committed
121

122
123
124
  const int      *psy_noise_normal_start[2];
  const int      *psy_noise_normal_partition[2];
  const double   *psy_noise_normal_thresh;
Monty's avatar
   
Monty committed
125

126
127
  const int      *psy_ath_float;
  const int      *psy_ath_abs;
Monty's avatar
   
Monty committed
128

129
  const double   *psy_lowpass;
Monty's avatar
   
Monty committed
130

131
132
133
  const vorbis_info_psy_global *global_params;
  const double     *global_mapping;
  const adj_stereo *stereo_modes;
Monty's avatar
   
Monty committed
134

135
136
  const static_codebook *const *const *const floor_books;
  const vorbis_info_floor1 *floor_params;
137
138
  const int floor_mappings;
  const int **floor_mapping_list;
Monty's avatar
   
Monty committed
139

140
  const vorbis_mapping_template *maps;
Monty's avatar
   
Monty committed
141
142
} ve_setup_data_template;

Monty's avatar
   
Monty committed
143
/* a few static coder conventions */
144
static const vorbis_info_mode _mode_template[2]={
Monty's avatar
   
Monty committed
145
146
147
148
  {0,0,0,0},
  {1,0,0,1}
};

149
static const vorbis_info_mapping0 _map_nominal[2]={
Monty's avatar
   
Monty committed
150
151
152
153
  {1, {0,0}, {0}, {0}, 1,{0},{1}},
  {1, {0,0}, {1}, {1}, 1,{0},{1}}
};

Monty's avatar
   
Monty committed
154
#include "modes/setup_44.h"
Monty's avatar
   
Monty committed
155
#include "modes/setup_44u.h"
Monty's avatar
Monty committed
156
#include "modes/setup_44p51.h"
Monty's avatar
   
Monty committed
157
#include "modes/setup_32.h"
Monty's avatar
   
Monty committed
158
159
160
161
#include "modes/setup_8.h"
#include "modes/setup_11.h"
#include "modes/setup_16.h"
#include "modes/setup_22.h"
Monty's avatar
   
Monty committed
162
#include "modes/setup_X.h"
Monty's avatar
   
Monty committed
163

164
static const ve_setup_data_template *const setup_list[]={
Monty's avatar
   
Monty committed
165
  &ve_setup_44_stereo,
Monty's avatar
Monty committed
166
  &ve_setup_44_51,
Monty's avatar
   
Monty committed
167
  &ve_setup_44_uncoupled,
Monty's avatar
   
Monty committed
168
169
170
171

  &ve_setup_32_stereo,
  &ve_setup_32_uncoupled,

Monty's avatar
   
Monty committed
172
173
174
175
176
177
178
179
180
181
  &ve_setup_22_stereo,
  &ve_setup_22_uncoupled,
  &ve_setup_16_stereo,
  &ve_setup_16_uncoupled,

  &ve_setup_11_stereo,
  &ve_setup_11_uncoupled,
  &ve_setup_8_stereo,
  &ve_setup_8_uncoupled,

Monty's avatar
   
Monty committed
182
183
  &ve_setup_X_stereo,
  &ve_setup_X_uncoupled,
Monty's avatar
   
Monty committed
184
185
  &ve_setup_XX_stereo,
  &ve_setup_XX_uncoupled,
Monty's avatar
   
Monty committed
186
187
188
  0
};

189
static void vorbis_encode_floor_setup(vorbis_info *vi,int s,
Monty's avatar
Monty committed
190
191
                                     const static_codebook *const *const *const books,
                                     const vorbis_info_floor1 *in,
192
                                     const int *x){
Monty's avatar
   
Monty committed
193
  int i,k,is=s;
Monty's avatar
   
Monty committed
194
  vorbis_info_floor1 *f=_ogg_calloc(1,sizeof(*f));
Monty's avatar
   
Monty committed
195
196
  codec_setup_info *ci=vi->codec_setup;

Monty's avatar
   
Monty committed
197
  memcpy(f,in+x[is],sizeof(*f));
Monty's avatar
   
Monty committed
198
199
200
201
202
203
204
205
206
207
208
209

  /* books */
  {
    int partitions=f->partitions;
    int maxclass=-1;
    int maxbook=-1;
    for(i=0;i<partitions;i++)
      if(f->partitionclass[i]>maxclass)maxclass=f->partitionclass[i];
    for(i=0;i<=maxclass;i++){
      if(f->class_book[i]>maxbook)maxbook=f->class_book[i];
      f->class_book[i]+=ci->books;
      for(k=0;k<(1<<f->class_subs[i]);k++){
210
211
        if(f->class_subbook[i][k]>maxbook)maxbook=f->class_subbook[i][k];
        if(f->class_subbook[i][k]>=0)f->class_subbook[i][k]+=ci->books;
Monty's avatar
   
Monty committed
212
213
214
215
      }
    }

    for(i=0;i<=maxbook;i++)
216
      ci->book_param[ci->books++]=(static_codebook *)books[x[is]][i];
Monty's avatar
   
Monty committed
217
218
219
220
221
222
223
  }

  /* for now, we're only using floor 1 */
  ci->floor_type[ci->floors]=1;
  ci->floor_param[ci->floors]=f;
  ci->floors++;

Monty's avatar
   
Monty committed
224
  return;
Monty's avatar
   
Monty committed
225
226
}

Monty's avatar
   
Monty committed
227
static void vorbis_encode_global_psych_setup(vorbis_info *vi,double s,
Monty's avatar
Monty committed
228
                                            const vorbis_info_psy_global *in,
229
                                            const double *x){
Monty's avatar
   
Monty committed
230
231
  int i,is=s;
  double ds=s-is;
Monty's avatar
   
Monty committed
232
233
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy_global *g=&ci->psy_g_param;
Monty's avatar
Monty committed
234

Monty's avatar
   
Monty committed
235
  memcpy(g,in+(int)x[is],sizeof(*g));
Monty's avatar
Monty committed
236

Monty's avatar
   
Monty committed
237
238
239
240
241
242
  ds=x[is]*(1.-ds)+x[is+1]*ds;
  is=(int)ds;
  ds-=is;
  if(ds==0 && is>0){
    is--;
    ds=1.;
Monty's avatar
   
Monty committed
243
  }
Monty's avatar
Monty committed
244

Monty's avatar
   
Monty committed
245
246
  /* interpolate the trigger threshholds */
  for(i=0;i<4;i++){
Monty's avatar
   
Monty committed
247
248
    g->preecho_thresh[i]=in[is].preecho_thresh[i]*(1.-ds)+in[is+1].preecho_thresh[i]*ds;
    g->postecho_thresh[i]=in[is].postecho_thresh[i]*(1.-ds)+in[is+1].postecho_thresh[i]*ds;
Monty's avatar
   
Monty committed
249
  }
Monty's avatar
   
Monty committed
250
  g->ampmax_att_per_sec=ci->hi.amplitude_track_dBpersec;
Monty's avatar
   
Monty committed
251
  return;
Monty's avatar
   
Monty committed
252
253
}

Monty's avatar
   
Monty committed
254
static void vorbis_encode_global_stereo(vorbis_info *vi,
255
256
                                        const highlevel_encode_setup *const hi,
                                        const adj_stereo *p){
Monty's avatar
   
Monty committed
257
258
259
  float s=hi->stereo_point_setting;
  int i,is=s;
  double ds=s-is;
Monty's avatar
   
Monty committed
260
  codec_setup_info *ci=vi->codec_setup;
Monty's avatar
   
Monty committed
261
262
  vorbis_info_psy_global *g=&ci->psy_g_param;

Monty's avatar
   
Monty committed
263
264
265
266
267
268
269
  if(p){
    memcpy(g->coupling_prepointamp,p[is].pre,sizeof(*p[is].pre)*PACKETBLOBS);
    memcpy(g->coupling_postpointamp,p[is].post,sizeof(*p[is].post)*PACKETBLOBS);

    if(hi->managed){
      /* interpolate the kHz threshholds */
      for(i=0;i<PACKETBLOBS;i++){
270
271
272
273
        float kHz=p[is].kHz[i]*(1.-ds)+p[is+1].kHz[i]*ds;
        g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
        g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
        g->coupling_pkHz[i]=kHz;
Monty's avatar
Monty committed
274

275
276
277
        kHz=p[is].lowpasskHz[i]*(1.-ds)+p[is+1].lowpasskHz[i]*ds;
        g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
        g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
Monty's avatar
Monty committed
278

Monty's avatar
   
Monty committed
279
280
281
282
      }
    }else{
      float kHz=p[is].kHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].kHz[PACKETBLOBS/2]*ds;
      for(i=0;i<PACKETBLOBS;i++){
283
284
285
        g->coupling_pointlimit[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
        g->coupling_pointlimit[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
        g->coupling_pkHz[i]=kHz;
Monty's avatar
   
Monty committed
286
      }
Monty's avatar
Monty committed
287

Monty's avatar
   
Monty committed
288
289
      kHz=p[is].lowpasskHz[PACKETBLOBS/2]*(1.-ds)+p[is+1].lowpasskHz[PACKETBLOBS/2]*ds;
      for(i=0;i<PACKETBLOBS;i++){
290
291
        g->sliding_lowpass[0][i]=kHz*1000./vi->rate*ci->blocksizes[0];
        g->sliding_lowpass[1][i]=kHz*1000./vi->rate*ci->blocksizes[1];
Monty's avatar
   
Monty committed
292
      }
Monty's avatar
   
Monty committed
293
294
295
    }
  }else{
    for(i=0;i<PACKETBLOBS;i++){
Monty's avatar
   
Monty committed
296
297
      g->sliding_lowpass[0][i]=ci->blocksizes[0];
      g->sliding_lowpass[1][i]=ci->blocksizes[1];
Monty's avatar
   
Monty committed
298
299
    }
  }
Monty's avatar
   
Monty committed
300
  return;
Monty's avatar
   
Monty committed
301
302
}

Monty's avatar
   
Monty committed
303
static void vorbis_encode_psyset_setup(vorbis_info *vi,double s,
304
305
306
307
                                       const int *nn_start,
                                       const int *nn_partition,
                                       const double *nn_thresh,
                                       int block){
Monty's avatar
   
Monty committed
308
309
310
311
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];
  highlevel_encode_setup *hi=&ci->hi;
  int is=s;
Monty's avatar
Monty committed
312

Monty's avatar
   
Monty committed
313
314
315
  if(block>=ci->psys)
    ci->psys=block+1;
  if(!p){
Monty's avatar
   
Monty committed
316
    p=_ogg_calloc(1,sizeof(*p));
Monty's avatar
   
Monty committed
317
318
    ci->psy_param[block]=p;
  }
Monty's avatar
Monty committed
319

Monty's avatar
   
Monty committed
320
  memcpy(p,&_psy_info_template,sizeof(*p));
Monty's avatar
   
Monty committed
321
  p->blockflag=block>>1;
Monty's avatar
   
Monty committed
322

Monty's avatar
   
Monty committed
323
  if(hi->noise_normalize_p){
324
    p->normal_p=1;
Monty's avatar
   
Monty committed
325
326
327
328
    p->normal_start=nn_start[is];
    p->normal_partition=nn_partition[is];
    p->normal_thresh=nn_thresh[is];
  }
Monty's avatar
Monty committed
329

Monty's avatar
   
Monty committed
330
  return;
Monty's avatar
   
Monty committed
331
332
}

Monty's avatar
   
Monty committed
333
static void vorbis_encode_tonemask_setup(vorbis_info *vi,double s,int block,
334
335
336
                                         const att3 *att,
                                         const int  *max,
                                         const vp_adjblock *in){
Monty's avatar
   
Monty committed
337
338
  int i,is=s;
  double ds=s-is;
Monty's avatar
   
Monty committed
339
340
341
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

Monty's avatar
   
Monty committed
342
343
344
345
346
347
348
  /* 0 and 2 are only used by bitmanagement, but there's no harm to always
     filling the values in here */
  p->tone_masteratt[0]=att[is].att[0]*(1.-ds)+att[is+1].att[0]*ds;
  p->tone_masteratt[1]=att[is].att[1]*(1.-ds)+att[is+1].att[1]*ds;
  p->tone_masteratt[2]=att[is].att[2]*(1.-ds)+att[is+1].att[2]*ds;
  p->tone_centerboost=att[is].boost*(1.-ds)+att[is+1].boost*ds;
  p->tone_decay=att[is].decay*(1.-ds)+att[is+1].decay*ds;
Monty's avatar
   
Monty committed
349

Monty's avatar
   
Monty committed
350
  p->max_curve_dB=max[is]*(1.-ds)+max[is+1]*ds;
Monty's avatar
   
Monty committed
351

Monty's avatar
   
Monty committed
352
  for(i=0;i<P_BANDS;i++)
Monty's avatar
   
Monty committed
353
    p->toneatt[i]=in[is].block[i]*(1.-ds)+in[is+1].block[i]*ds;
Monty's avatar
   
Monty committed
354
  return;
Monty's avatar
   
Monty committed
355
356
}

Monty's avatar
   
Monty committed
357

Monty's avatar
   
Monty committed
358
static void vorbis_encode_compand_setup(vorbis_info *vi,double s,int block,
Monty's avatar
Monty committed
359
                                        const compandblock *in,
360
                                        const double *x){
Monty's avatar
   
Monty committed
361
362
  int i,is=s;
  double ds=s-is;
Monty's avatar
   
Monty committed
363
364
365
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

Monty's avatar
   
Monty committed
366
367
368
369
370
371
  ds=x[is]*(1.-ds)+x[is+1]*ds;
  is=(int)ds;
  ds-=is;
  if(ds==0 && is>0){
    is--;
    ds=1.;
Monty's avatar
   
Monty committed
372
373
374
375
  }

  /* interpolate the compander settings */
  for(i=0;i<NOISE_COMPAND_LEVELS;i++)
Monty's avatar
   
Monty committed
376
    p->noisecompand[i]=in[is].data[i]*(1.-ds)+in[is+1].data[i]*ds;
Monty's avatar
   
Monty committed
377
  return;
Monty's avatar
   
Monty committed
378
379
}

Monty's avatar
   
Monty committed
380
static void vorbis_encode_peak_setup(vorbis_info *vi,double s,int block,
381
                                    const int *suppress){
Monty's avatar
   
Monty committed
382
383
  int is=s;
  double ds=s-is;
Monty's avatar
   
Monty committed
384
385
386
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

Monty's avatar
   
Monty committed
387
  p->tone_abs_limit=suppress[is]*(1.-ds)+suppress[is+1]*ds;
Monty's avatar
   
Monty committed
388

Monty's avatar
   
Monty committed
389
  return;
Monty's avatar
   
Monty committed
390
391
}

Monty's avatar
   
Monty committed
392
static void vorbis_encode_noisebias_setup(vorbis_info *vi,double s,int block,
393
394
395
396
                                         const int *suppress,
                                         const noise3 *in,
                                         const noiseguard *guard,
                                         double userbias){
Monty's avatar
   
Monty committed
397
398
  int i,is=s,j;
  double ds=s-is;
Monty's avatar
   
Monty committed
399
400
401
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

Monty's avatar
   
Monty committed
402
403
404
405
406
407
408
409
  p->noisemaxsupp=suppress[is]*(1.-ds)+suppress[is+1]*ds;
  p->noisewindowlomin=guard[block].lo;
  p->noisewindowhimin=guard[block].hi;
  p->noisewindowfixed=guard[block].fixed;

  for(j=0;j<P_NOISECURVES;j++)
    for(i=0;i<P_BANDS;i++)
      p->noiseoff[j][i]=in[is].data[j][i]*(1.-ds)+in[is+1].data[j][i]*ds;
Monty's avatar
   
Monty committed
410

Monty's avatar
   
Monty committed
411
412
413
414
415
416
417
418
419
420
  /* impulse blocks may take a user specified bias to boost the
     nominal/high noise encoding depth */
  for(j=0;j<P_NOISECURVES;j++){
    float min=p->noiseoff[j][0]+6; /* the lowest it can go */
    for(i=0;i<P_BANDS;i++){
      p->noiseoff[j][i]+=userbias;
      if(p->noiseoff[j][i]<min)p->noiseoff[j][i]=min;
    }
  }

Monty's avatar
   
Monty committed
421
  return;
Monty's avatar
   
Monty committed
422
}
Monty's avatar
   
Monty committed
423

Monty's avatar
   
Monty committed
424
static void vorbis_encode_ath_setup(vorbis_info *vi,int block){
Monty's avatar
   
Monty committed
425
426
427
  codec_setup_info *ci=vi->codec_setup;
  vorbis_info_psy *p=ci->psy_param[block];

Monty's avatar
   
Monty committed
428
429
  p->ath_adjatt=ci->hi.ath_floating_dB;
  p->ath_maxatt=ci->hi.ath_absolute_dB;
Monty's avatar
   
Monty committed
430
  return;
Monty's avatar
   
Monty committed
431
432
}

Monty's avatar
   
Monty committed
433

434
static int book_dup_or_new(codec_setup_info *ci,const static_codebook *book){
Monty's avatar
   
Monty committed
435
436
437
  int i;
  for(i=0;i<ci->books;i++)
    if(ci->book_param[i]==book)return(i);
Monty's avatar
Monty committed
438

Monty's avatar
   
Monty committed
439
440
441
  return(ci->books++);
}

Monty's avatar
   
Monty committed
442
static void vorbis_encode_blocksize_setup(vorbis_info *vi,double s,
443
                                         const int *shortb,const int *longb){
Monty's avatar
   
Monty committed
444
445

  codec_setup_info *ci=vi->codec_setup;
Monty's avatar
   
Monty committed
446
  int is=s;
Monty's avatar
Monty committed
447

Monty's avatar
   
Monty committed
448
449
450
451
  int blockshort=shortb[is];
  int blocklong=longb[is];
  ci->blocksizes[0]=blockshort;
  ci->blocksizes[1]=blocklong;
Monty's avatar
   
Monty committed
452

Monty's avatar
   
Monty committed
453
}
454

Monty's avatar
   
Monty committed
455
static void vorbis_encode_residue_setup(vorbis_info *vi,
456
457
                                        int number, int block,
                                        const vorbis_residue_template *res){
Monty's avatar
   
Monty committed
458

Monty's avatar
   
Monty committed
459
  codec_setup_info *ci=vi->codec_setup;
460
  int i;
Monty's avatar
Monty committed
461

Monty's avatar
   
Monty committed
462
463
  vorbis_info_residue0 *r=ci->residue_param[number]=
    _ogg_malloc(sizeof(*r));
Monty's avatar
Monty committed
464

Monty's avatar
   
Monty committed
465
466
  memcpy(r,res->res,sizeof(*r));
  if(ci->residues<=number)ci->residues=number+1;
Monty's avatar
   
Monty committed
467

Monty's avatar
Monty committed
468
  r->grouping=res->grouping;
Monty's avatar
   
Monty committed
469
  ci->residue_type[number]=res->res_type;
Monty's avatar
   
Monty committed
470

Monty's avatar
   
Monty committed
471
472
473
  /* fill in all the books */
  {
    int booklist=0,k;
Monty's avatar
Monty committed
474

Monty's avatar
   
Monty committed
475
    if(ci->hi.managed){
Monty's avatar
   
Monty committed
476
      for(i=0;i<r->partitions;i++)
Monty's avatar
Monty committed
477
        for(k=0;k<4;k++)
478
479
          if(res->books_base_managed->books[i][k])
            r->secondstages[i]|=(1<<k);
Monty's avatar
   
Monty committed
480

Monty's avatar
   
Monty committed
481
      r->groupbook=book_dup_or_new(ci,res->book_aux_managed);
Monty's avatar
Monty committed
482
483
      ci->book_param[r->groupbook]=(static_codebook *)res->book_aux_managed;

Monty's avatar
   
Monty committed
484
      for(i=0;i<r->partitions;i++){
Monty's avatar
Monty committed
485
        for(k=0;k<4;k++){
486
487
488
489
490
491
          if(res->books_base_managed->books[i][k]){
            int bookid=book_dup_or_new(ci,res->books_base_managed->books[i][k]);
            r->booklist[booklist++]=bookid;
            ci->book_param[bookid]=(static_codebook *)res->books_base_managed->books[i][k];
          }
        }
Monty's avatar
   
Monty committed
492
      }
Monty's avatar
   
Monty committed
493
494
495
496

    }else{

      for(i=0;i<r->partitions;i++)
Monty's avatar
Monty committed
497
        for(k=0;k<4;k++)
498
499
          if(res->books_base->books[i][k])
            r->secondstages[i]|=(1<<k);
Monty's avatar
Monty committed
500

Monty's avatar
   
Monty committed
501
      r->groupbook=book_dup_or_new(ci,res->book_aux);
502
      ci->book_param[r->groupbook]=(static_codebook *)res->book_aux;
Monty's avatar
Monty committed
503

Monty's avatar
   
Monty committed
504
      for(i=0;i<r->partitions;i++){
Monty's avatar
Monty committed
505
        for(k=0;k<4;k++){
506
507
508
509
510
511
          if(res->books_base->books[i][k]){
            int bookid=book_dup_or_new(ci,res->books_base->books[i][k]);
            r->booklist[booklist++]=bookid;
            ci->book_param[bookid]=(static_codebook *)res->books_base->books[i][k];
          }
        }
Monty's avatar
   
Monty committed
512
513
      }
    }
Monty's avatar
   
Monty committed
514
  }
Monty's avatar
Monty committed
515

Monty's avatar
   
Monty committed
516
517
518
519
520
521
522
  /* lowpass setup/pointlimit */
  {
    double freq=ci->hi.lowpass_kHz*1000.;
    vorbis_info_floor1 *f=ci->floor_param[block]; /* by convention */
    double nyq=vi->rate/2.;
    long blocksize=ci->blocksizes[block]>>1;

Monty's avatar
Monty committed
523
    /* lowpass needs to be set in the floor and the residue. */
Monty's avatar
   
Monty committed
524
525
526
527
    if(freq>nyq)freq=nyq;
    /* in the floor, the granularity can be very fine; it doesn't alter
       the encoding structure, only the samples used to fit the floor
       approximation */
Monty's avatar
Monty committed
528
    f->n=freq/nyq*blocksize;
Monty's avatar
   
Monty committed
529
530
531

    /* this res may by limited by the maximum pointlimit of the mode,
       not the lowpass. the floor is always lowpass limited. */
532
533
    switch(res->limit_type){
    case 1: /* point stereo limited */
Monty's avatar
   
Monty committed
534
      if(ci->hi.managed)
535
        freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS-1]*1000.;
Monty's avatar
   
Monty committed
536
      else
537
        freq=ci->psy_g_param.coupling_pkHz[PACKETBLOBS/2]*1000.;
Monty's avatar
   
Monty committed
538
      if(freq>nyq)freq=nyq;
539
540
541
542
543
544
545
      break;
    case 2: /* LFE channel; lowpass at ~ 250Hz */
      freq=250;
      break;
    default:
      /* already set */
      break;
Monty's avatar
   
Monty committed
546
    }
Monty's avatar
Monty committed
547

Monty's avatar
   
Monty committed
548
549
550
551
    /* in the residue, we're constrained, physically, by partition
       boundaries.  We still lowpass 'wherever', but we have to round up
       here to next boundary, or the vorbis spec will round it *down* to
       previous boundary in encode/decode */
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
    if(ci->residue_type[number]==2){
      /* residue 2 bundles together multiple channels; used by stereo
         and surround.  Count the channels in use */
      /* Multiple maps/submaps can point to the same residue.  In the case
         of residue 2, they all better have the same number of
         channels/samples. */
      int j,k,ch=0;
      for(i=0;i<ci->maps&&ch==0;i++){
        vorbis_info_mapping0 *mi=(vorbis_info_mapping0 *)ci->map_param[i];
        for(j=0;j<mi->submaps && ch==0;j++)
          if(mi->residuesubmap[j]==number) /* we found a submap referencing theis residue backend */
            for(k=0;k<vi->channels;k++)
              if(mi->chmuxlist[k]==j) /* this channel belongs to the submap */
                ch++;
      }

      r->end=(int)((freq/nyq*blocksize*ch)/r->grouping+.9)* /* round up only if we're well past */
569
        r->grouping;
Monty's avatar
Monty committed
570
571
572
573
574
      /* the blocksize and grouping may disagree at the end */
      if(r->end>blocksize*ch)r->end=blocksize*ch/r->grouping*r->grouping;

    }else{

Monty's avatar
   
Monty committed
575
      r->end=(int)((freq/nyq*blocksize)/r->grouping+.9)* /* round up only if we're well past */
576
        r->grouping;
Monty's avatar
Monty committed
577
578
579
580
581
      /* the blocksize and grouping may disagree at the end */
      if(r->end>blocksize)r->end=blocksize/r->grouping*r->grouping;

    }

582
    if(r->end==0)r->end=r->grouping; /* LFE channel */
Monty's avatar
Monty committed
583

Monty's avatar
   
Monty committed
584
  }
Monty's avatar
Monty committed
585
}
Monty's avatar
   
Monty committed
586

Monty's avatar
   
Monty committed
587
588
/* we assume two maps in this encoder */
static void vorbis_encode_map_n_res_setup(vorbis_info *vi,double s,
589
                                          const vorbis_mapping_template *maps){
Monty's avatar
   
Monty committed
590

Monty's avatar
   
Monty committed
591
  codec_setup_info *ci=vi->codec_setup;
Monty's avatar
   
Monty committed
592
  int i,j,is=s,modes=2;
593
594
595
  const vorbis_info_mapping0 *map=maps[is].map;
  const vorbis_info_mode *mode=_mode_template;
  const vorbis_residue_template *res=maps[is].res;
Monty's avatar
   
Monty committed
596

Monty's avatar
   
Monty committed
597
598
599
  if(ci->blocksizes[0]==ci->blocksizes[1])modes=1;

  for(i=0;i<modes;i++){
Monty's avatar
   
Monty committed
600
601
602

    ci->map_param[i]=_ogg_calloc(1,sizeof(*map));
    ci->mode_param[i]=_ogg_calloc(1,sizeof(*mode));
Monty's avatar
Monty committed
603

Monty's avatar
   
Monty committed
604
605
606
607
608
609
    memcpy(ci->mode_param[i],mode+i,sizeof(*_mode_template));
    if(i>=ci->modes)ci->modes=i+1;

    ci->map_type[i]=0;
    memcpy(ci->map_param[i],map+i,sizeof(*map));
    if(i>=ci->maps)ci->maps=i+1;
Monty's avatar
Monty committed
610

Monty's avatar
   
Monty committed
611
612
    for(j=0;j<map[i].submaps;j++)
      vorbis_encode_residue_setup(vi,map[i].residuesubmap[j],i
613
                                  ,res+map[i].residuesubmap[j]);
Monty's avatar
   
Monty committed
614
  }
Monty's avatar
   
Monty committed
615
616
617
618
619
620
621
622
623
}

static double setting_to_approx_bitrate(vorbis_info *vi){
  codec_setup_info *ci=vi->codec_setup;
  highlevel_encode_setup *hi=&ci->hi;
  ve_setup_data_template *setup=(ve_setup_data_template *)hi->setup;
  int is=hi->base_setting;
  double ds=hi->base_setting-is;
  int ch=vi->channels;
624
  const double *r=setup->rate_mapping;
Monty's avatar
   
Monty committed
625
626
627

  if(r==NULL)
    return(-1);
Monty's avatar
Monty committed
628
629

  return((r[is]*(1.-ds)+r[is+1]*ds)*ch);
Monty's avatar
   
Monty committed
630
631
}

632
633
634
static const void *get_setup_template(long ch,long srate,
                                      double req,int q_or_bitrate,
                                      double *base_setting){
Monty's avatar
   
Monty committed
635
  int i=0,j;
Monty's avatar
   
Monty committed
636
  if(q_or_bitrate)req/=ch;
Monty's avatar
   
Monty committed
637
638
639
640

  while(setup_list[i]){
    if(setup_list[i]->coupling_restriction==-1 ||
       setup_list[i]->coupling_restriction==ch){
Monty's avatar
   
Monty committed
641
      if(srate>=setup_list[i]->samplerate_min_restriction &&
642
643
644
645
646
647
648
649
650
651
652
653
654
655
         srate<=setup_list[i]->samplerate_max_restriction){
        int mappings=setup_list[i]->mappings;
        const double *map=(q_or_bitrate?
                     setup_list[i]->rate_mapping:
                     setup_list[i]->quality_mapping);

        /* the template matches.  Does the requested quality mode
           fall within this template's modes? */
        if(req<map[0]){++i;continue;}
        if(req>map[setup_list[i]->mappings]){++i;continue;}
        for(j=0;j<mappings;j++)
          if(req>=map[j] && req<map[j+1])break;
        /* an all-points match */
        if(j==mappings)
656
          *base_setting=j-.001;
657
658
659
660
        else{
          float low=map[j];
          float high=map[j+1];
          float del=(req-low)/(high-low);
661
          *base_setting=j+del;
662
663
        }

664
        return(setup_list[i]);
Monty's avatar
   
Monty committed
665
666
      }
    }
667
    i++;
Monty's avatar
   
Monty committed
668
  }
Monty's avatar
Monty committed
669

670
  return NULL;
Monty's avatar
   
Monty committed
671
672
673
674
675
}

/* encoders will need to use vorbis_info_init beforehand and call
   vorbis_info clear when all done */

Monty's avatar
   
Monty committed
676
677
678
679
680
/* two interfaces; this, more detailed one, and later a convenience
   layer on top */

/* the final setup call */
int vorbis_encode_setup_init(vorbis_info *vi){
681
  int i,i0=0,singleblock=0;
Monty's avatar
   
Monty committed
682
  codec_setup_info *ci=vi->codec_setup;
Monty's avatar
   
Monty committed
683
  ve_setup_data_template *setup=NULL;
Monty's avatar
   
Monty committed
684
685
  highlevel_encode_setup *hi=&ci->hi;

Monty's avatar
   
Monty committed
686
687
  if(ci==NULL)return(OV_EINVAL);
  if(!hi->impulse_block_p)i0=1;
Monty's avatar
   
Monty committed
688

Monty's avatar
   
Monty committed
689
690
691
692
693
694
695
696
  /* too low/high an ATH floater is nonsensical, but doesn't break anything */
  if(hi->ath_floating_dB>-80)hi->ath_floating_dB=-80;
  if(hi->ath_floating_dB<-200)hi->ath_floating_dB=-200;

  /* again, bound this to avoid the app shooting itself int he foot
     too badly */
  if(hi->amplitude_track_dBpersec>0.)hi->amplitude_track_dBpersec=0.;
  if(hi->amplitude_track_dBpersec<-99999.)hi->amplitude_track_dBpersec=-99999.;
Monty's avatar
Monty committed
697

Monty's avatar
   
Monty committed
698
699
700
701
702
  /* get the appropriate setup template; matches the fetch in previous
     stages */
  setup=(ve_setup_data_template *)hi->setup;
  if(setup==NULL)return(OV_EINVAL);

Monty's avatar
   
Monty committed
703
  hi->set_in_stone=1;
Monty's avatar
   
Monty committed
704
705
706
707
708
  /* choose block sizes from configured sizes as well as paying
     attention to long_block_p and short_block_p.  If the configured
     short and long blocks are the same length, we set long_block_p
     and unset short_block_p */
  vorbis_encode_blocksize_setup(vi,hi->base_setting,
709
710
                                setup->blocksize_short,
                                setup->blocksize_long);
Monty's avatar
   
Monty committed
711
  if(ci->blocksizes[0]==ci->blocksizes[1])singleblock=1;
Monty's avatar
Monty committed
712

Monty's avatar
   
Monty committed
713
  /* floor setup; choose proper floor params.  Allocated on the floor
714
715
716
     stack in order; if we alloc only a single long floor, it's 0 */
  for(i=0;i<setup->floor_mappings;i++)
    vorbis_encode_floor_setup(vi,hi->base_setting,
717
718
                              setup->floor_books,
                              setup->floor_params,
719
                              setup->floor_mapping_list[i]);
Monty's avatar
Monty committed
720

Monty's avatar
   
Monty committed
721
  /* setup of [mostly] short block detection and stereo*/
Monty's avatar
   
Monty committed
722
  vorbis_encode_global_psych_setup(vi,hi->trigger_setting,
723
724
                                   setup->global_params,
                                   setup->global_mapping);
Monty's avatar
   
Monty committed
725
  vorbis_encode_global_stereo(vi,hi,setup->stereo_modes);
Monty's avatar
   
Monty committed
726
727

  /* basic psych setup and noise normalization */
728
  vorbis_encode_psyset_setup(vi,hi->base_setting,
729
                             setup->psy_noise_normal_start[0],
Monty's avatar
Monty committed
730
731
                             setup->psy_noise_normal_partition[0],
                             setup->psy_noise_normal_thresh,
732
                             0);
733
  vorbis_encode_psyset_setup(vi,hi->base_setting,
734
                             setup->psy_noise_normal_start[0],
Monty's avatar
Monty committed
735
736
                             setup->psy_noise_normal_partition[0],
                             setup->psy_noise_normal_thresh,
737
                             1);
Monty's avatar
   
Monty committed
738
  if(!singleblock){
739
    vorbis_encode_psyset_setup(vi,hi->base_setting,
740
                               setup->psy_noise_normal_start[1],
Monty's avatar
Monty committed
741
742
                               setup->psy_noise_normal_partition[1],
                                    setup->psy_noise_normal_thresh,
743
                               2);
744
    vorbis_encode_psyset_setup(vi,hi->base_setting,
745
                               setup->psy_noise_normal_start[1],
Monty's avatar
Monty committed
746
747
                               setup->psy_noise_normal_partition[1],
                               setup->psy_noise_normal_thresh,
748
                               3);
Monty's avatar
   
Monty committed
749
  }
Monty's avatar
   
Monty committed
750
751

  /* tone masking setup */
Monty's avatar
   
Monty committed
752
  vorbis_encode_tonemask_setup(vi,hi->block[i0].tone_mask_setting,0,
753
754
755
                               setup->psy_tone_masteratt,
                               setup->psy_tone_0dB,
                               setup->psy_tone_adj_impulse);
Monty's avatar
   
Monty committed
756
  vorbis_encode_tonemask_setup(vi,hi->block[1].tone_mask_setting,1,
757
758
759
                               setup->psy_tone_masteratt,
                               setup->psy_tone_0dB,
                               setup->psy_tone_adj_other);
Monty's avatar
   
Monty committed
760
761
  if(!singleblock){
    vorbis_encode_tonemask_setup(vi,hi->block[2].tone_mask_setting,2,
762
763
764
                                 setup->psy_tone_masteratt,
                                 setup->psy_tone_0dB,
                                 setup->psy_tone_adj_other);
Monty's avatar
   
Monty committed
765
    vorbis_encode_tonemask_setup(vi,hi->block[3].tone_mask_setting,3,
766
767
768
                                 setup->psy_tone_masteratt,
                                 setup->psy_tone_0dB,
                                 setup->psy_tone_adj_long);
Monty's avatar
   
Monty committed
769
  }
Monty's avatar
   
Monty committed
770
771

  /* noise companding setup */
Monty's avatar
   
Monty committed
772
  vorbis_encode_compand_setup(vi,hi->block[i0].noise_compand_setting,0,
773
774
                              setup->psy_noise_compand,
                              setup->psy_noise_compand_short_mapping);
Monty's avatar
   
Monty committed
775
  vorbis_encode_compand_setup(vi,hi->block[1].noise_compand_setting,1,
776
777
                              setup->psy_noise_compand,
                              setup->psy_noise_compand_short_mapping);
Monty's avatar
   
Monty committed
778
779
  if(!singleblock){
    vorbis_encode_compand_setup(vi,hi->block[2].noise_compand_setting,2,
780
781
                                setup->psy_noise_compand,
                                setup->psy_noise_compand_long_mapping);
Monty's avatar
   
Monty committed
782
    vorbis_encode_compand_setup(vi,hi->block[3].noise_compand_setting,3,
783
784
                                setup->psy_noise_compand,
                                setup->psy_noise_compand_long_mapping);
Monty's avatar
   
Monty committed
785
  }
Monty's avatar
   
Monty committed
786
787

  /* peak guarding setup  */
Monty's avatar
   
Monty committed
788
  vorbis_encode_peak_setup(vi,hi->block[i0].tone_peaklimit_setting,0,
789
                           setup->psy_tone_dBsuppress);
Monty's avatar
   
Monty committed
790
  vorbis_encode_peak_setup(vi,hi->block[1].tone_peaklimit_setting,1,
791
                           setup->psy_tone_dBsuppress);
Monty's avatar
   
Monty committed
792
793
  if(!singleblock){
    vorbis_encode_peak_setup(vi,hi->block[2].tone_peaklimit_setting,2,
794
                             setup->psy_tone_dBsuppress);
Monty's avatar
   
Monty committed
795
    vorbis_encode_peak_setup(vi,hi->block[3].tone_peaklimit_setting,3,
796
                             setup->psy_tone_dBsuppress);
Monty's avatar
   
Monty committed
797
  }
Monty's avatar
   
Monty committed
798
799

  /* noise bias setup */
Monty's avatar
   
Monty committed
800
  vorbis_encode_noisebias_setup(vi,hi->block[i0].noise_bias_setting,0,
801
802
803
804
                                setup->psy_noise_dBsuppress,
                                setup->psy_noise_bias_impulse,
                                setup->psy_noiseguards,
                                (i0==0?hi->impulse_noisetune:0.));
Monty's avatar
   
Monty committed
805
  vorbis_encode_noisebias_setup(vi,hi->block[1].noise_bias_setting,1,
806
807
808
                                setup->psy_noise_dBsuppress,
                                setup->psy_noise_bias_padding,
                                setup->psy_noiseguards,0.);
Monty's avatar
   
Monty committed
809
810
  if(!singleblock){
    vorbis_encode_noisebias_setup(vi,hi->block[2].noise_bias_setting,2,
811
812
813
                                  setup->psy_noise_dBsuppress,
                                  setup->psy_noise_bias_trans,
                                  setup->psy_noiseguards,0.);
Monty's avatar
   
Monty committed
814
    vorbis_encode_noisebias_setup(vi,hi->block[3].noise_bias_setting,3,
815
816
817
                                  setup->psy_noise_dBsuppress,
                                  setup->psy_noise_bias_long,
                                  setup->psy_noiseguards,0.);
Monty's avatar
   
Monty committed
818
819
820
821
822
823
824
  }

  vorbis_encode_ath_setup(vi,0);
  vorbis_encode_ath_setup(vi,1);
  if(!singleblock){
    vorbis_encode_ath_setup(vi,2);
    vorbis_encode_ath_setup(vi,3);
Monty's avatar
   
Monty committed
825
826
  }

Monty's avatar
   
Monty committed
827
  vorbis_encode_map_n_res_setup(vi,hi->base_setting,setup->maps);
Monty's avatar
   
Monty committed
828

Monty's avatar
   
Monty committed
829
  /* set bitrate readonlies and management */
830
831
832
833
834
835
  if(hi->bitrate_av>0)
    vi->bitrate_nominal=hi->bitrate_av;
  else{
    vi->bitrate_nominal=setting_to_approx_bitrate(vi);
  }

Monty's avatar
   
Monty committed
836
837
  vi->bitrate_lower=hi->bitrate_min;
  vi->bitrate_upper=hi->bitrate_max;
838
839
840
841
  if(hi->bitrate_av)
    vi->bitrate_window=(double)hi->bitrate_reservoir/hi->bitrate_av;
  else
    vi->bitrate_window=0.;
Monty's avatar
   
Monty committed
842

Monty's avatar
   
Monty committed
843
  if(hi->managed){
Monty's avatar
   
Monty committed
844
845
846
847
848
849
850
    ci->bi.avg_rate=hi->bitrate_av;
    ci->bi.min_rate=hi->bitrate_min;
    ci->bi.max_rate=hi->bitrate_max;

    ci->bi.reservoir_bits=hi->bitrate_reservoir;
    ci->bi.reservoir_bias=
      hi->bitrate_reservoir_bias;
Monty's avatar
Monty committed
851

Monty's avatar
   
Monty committed
852
853
    ci->bi.slew_damp=hi->bitrate_av_damp;

Monty's avatar
   
Monty committed
854
855
  }

Monty's avatar
   
Monty committed
856
  return(0);
Monty's avatar
Monty committed
857

Monty's avatar
   
Monty committed
858
859
}

860
static void vorbis_encode_setup_setting(vorbis_info *vi,
861
862
                                       long  channels,
                                       long  rate){
863
  int i,is;
Monty's avatar
   
Monty committed
864
865
  codec_setup_info *ci=vi->codec_setup;
  highlevel_encode_setup *hi=&ci->hi;
866
  const ve_setup_data_template *setup=hi->setup;
Monty's avatar
   
Monty committed
867
  double ds;
Monty's avatar
   
Monty committed
868

869
870
871
  vi->version=0;
  vi->channels=channels;
  vi->rate=rate;
Monty's avatar
   
Monty committed
872

Monty's avatar
   
Monty committed
873
874
875
  hi->impulse_block_p=1;
  hi->noise_normalize_p=1;

876
877
878
  is=hi->base_setting;
  ds=hi->base_setting-is;

Monty's avatar
   
Monty committed
879
  hi->stereo_point_setting=hi->base_setting;
880
881
882
883

  if(!hi->lowpass_altered)
    hi->lowpass_kHz=
      setup->psy_lowpass[is]*(1.-ds)+setup->psy_lowpass[is+1]*ds;
Monty's avatar
Monty committed
884

Monty's avatar
   
Monty committed
885
886
887
888
  hi->ath_floating_dB=setup->psy_ath_float[is]*(1.-ds)+
    setup->psy_ath_float[is+1]*ds;
  hi->ath_absolute_dB=setup->psy_ath_abs[is]*(1.-ds)+
    setup->psy_ath_abs[is+1]*ds;
Monty's avatar
   
Monty committed
889

Monty's avatar
   
Monty committed
890
891
  hi->amplitude_track_dBpersec=-6.;
  hi->trigger_setting=hi->base_setting;
Monty's avatar
   
Monty committed
892
893

  for(i=0;i<4;i++){
Monty's avatar
   
Monty committed
894
895
896
897
    hi->block[i].tone_mask_setting=hi->base_setting;
    hi->block[i].tone_peaklimit_setting=hi->base_setting;
    hi->block[i].noise_bias_setting=hi->base_setting;
    hi->block[i].noise_compand_setting=hi->base_setting;
Monty's avatar
   
Monty committed
898
  }
Monty's avatar
   
Monty committed
899
}
Monty's avatar
   
Monty committed
900

Monty's avatar
   
Monty committed
901
int vorbis_encode_setup_vbr(vorbis_info *vi,
902
                            long  channels,
Monty's avatar
Monty committed
903
                            long  rate,
904
                            float quality){
905
906
907
  codec_setup_info *ci;
  highlevel_encode_setup *hi;
  if(rate<=0) return OV_EINVAL;
908

909
910
  ci=vi->codec_setup;
  hi=&ci->hi;
Monty's avatar
   
Monty committed
911

912
  quality+=.0000001;
Monty's avatar
   
Monty committed
913
  if(quality>=1.)quality=.9999;
Monty's avatar
   
Monty committed
914

915
916
  hi->req=quality;
  hi->setup=get_setup_template(channels,rate,quality,0,&hi->base_setting);
Monty's avatar
   
Monty committed
917
  if(!hi->setup)return OV_EIMPL;
Monty's avatar
Monty committed
918

919
920
921
922
923
  vorbis_encode_setup_setting(vi,channels,rate);
  hi->managed=0;
  hi->coupling_p=1;

  return 0;
Monty's avatar
   
Monty committed
924
925
}

Monty's avatar
   
Monty committed
926
int vorbis_encode_init_vbr(vorbis_info *vi,
927
928
                           long channels,
                           long rate,
Monty's avatar
Monty committed
929