add mode D and E
[platform/upstream/libvorbis.git] / lib / vorbisenc.c
1 /********************************************************************
2  *                                                                  *
3  * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE.   *
4  * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS     *
5  * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6  * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING.       *
7  *                                                                  *
8  * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2001             *
9  * by the XIPHOPHORUS Company http://www.xiph.org/                  *
10
11  ********************************************************************
12
13  function: simple programmatic interface for encoder mode setup
14  last mod: $Id: vorbisenc.c,v 1.11 2001/08/13 08:39:30 xiphmont Exp $
15
16  ********************************************************************/
17
18 #include <stdlib.h>
19 #include <string.h>
20 #include <math.h>
21
22 #include "vorbis/codec.h"
23 #include "vorbis/vorbisenc.h"
24
25 #include "codec_internal.h"
26 #include "registry.h"
27 #include "modes/modes.h"
28
29 #include "os.h"
30 #include "misc.h"
31
32
33 /* deepcopy all but the codebooks; in this usage, they're static
34    (don't copy as they could be big) */
35 static void codec_setup_partialcopy(codec_setup_info *ci,
36                                  codec_setup_info *cs){
37   int i;
38
39   memcpy(ci,cs,sizeof(codec_setup_info)); /* to get the flat numbers */
40
41   /* codebooks */
42   for(i=0;i<ci->books;i++){
43     ci->book_param[i]=cs->book_param[i];
44   }
45
46   /* time backend settings */
47   for(i=0;i<ci->times;i++){
48     ci->time_param[i]=_time_P[ci->time_type[i]]->
49       copy_info(cs->time_param[i]);
50   }
51
52   /* floor backend settings */
53   for(i=0;i<ci->floors;i++){
54     ci->floor_param[i]=_floor_P[ci->floor_type[i]]->
55       copy_info(cs->floor_param[i]);
56   }
57
58   /* residue backend settings */
59   for(i=0;i<ci->residues;i++){
60     ci->residue_param[i]=_residue_P[ci->residue_type[i]]->
61       copy_info(cs->residue_param[i]);
62   }
63
64   /* map backend settings */
65   for(i=0;i<ci->maps;i++){
66     ci->map_param[i]=_mapping_P[ci->map_type[i]]->
67       copy_info(cs->map_param[i]);
68   }
69   
70   /* mode settings */
71   for(i=0;i<ci->modes;i++){
72     ci->mode_param[i]=_ogg_calloc(1,sizeof(vorbis_info_mode));
73     ci->mode_param[i]->blockflag=cs->mode_param[i]->blockflag;
74     ci->mode_param[i]->windowtype=cs->mode_param[i]->windowtype;
75     ci->mode_param[i]->transformtype=cs->mode_param[i]->transformtype;
76     ci->mode_param[i]->mapping=cs->mode_param[i]->mapping;
77   }
78
79   /* psy settings */
80   for(i=0;i<ci->psys;i++){
81     ci->psy_param[i]=_vi_psy_copy(cs->psy_param[i]);
82   }
83
84 }
85
86 /* encoders will need to use vorbis_info_init beforehand and call
87    vorbis_info clear when all done */
88
89 int vorbis_encode_init_vbr(vorbis_info *vi,
90                            long channels,
91                            long rate,
92                            
93                            float base_quality /* 0. to 1. */
94                            ){
95
96   switch(channels){
97   case 2:
98     return(OV_EIMPL);
99
100
101     break;
102   default:
103     return(OV_EIMPL);
104
105     break;
106   }
107 }
108
109
110 int vorbis_encode_init(vorbis_info *vi,
111                        long channels,
112                        long rate,
113
114                        long max_bitrate,
115                        long nominal_bitrate,
116                        long min_bitrate){
117
118   long bpch;
119   int i,j;
120   codec_setup_info *ci=vi->codec_setup;
121   codec_setup_info *mode=NULL;
122   if(!ci)return(OV_EFAULT);
123
124   vi->version=0;
125   vi->channels=channels;
126   vi->rate=rate;
127   
128   vi->bitrate_upper=max_bitrate;
129   vi->bitrate_nominal=nominal_bitrate;
130   vi->bitrate_lower=min_bitrate;
131   vi->bitrate_window=2;
132
133   /* copy a mode into our allocated storage */
134   bpch=nominal_bitrate/channels;
135
136   switch(channels){
137   case 2:
138     //if(rate>40000){
139
140     if(bpch<35000){
141       mode=&info_44c_Z;
142     }else if(bpch<45000){
143       mode=&info_44c_Y;
144     }else if(bpch<55000){
145       mode=&info_44c_X;
146     }else if(bpch<75000){
147       mode=&info_44c_A;
148     }else if(bpch<90000){
149       mode=&info_44c_B;
150     }else if(bpch<110000){
151       mode=&info_44c_C;
152     }else if(bpch<160000){
153       mode=&info_44c_D;
154     }else{
155       mode=&info_44c_E;
156     }
157     //}
158
159     break;
160   default:
161 #if 0
162     if(bpch<60000){
163       /* mode AA */
164       mode=&info_AA;
165     }else if(bpch<75000){
166       /* mode A */
167       mode=&info_A;
168     }else if(bpch<90000){
169       /* mode B */
170       mode=&info_B;
171     }else if(bpch<110000){
172       /* mode C */
173       mode=&info_C;
174     }else if(bpch<160000){
175       /* mode D */
176       mode=&info_D;
177     }else{
178       /* mode E */
179       mode=&info_E;
180     }
181 #endif
182   }
183
184   /* now we have to deepcopy */
185   codec_setup_partialcopy(ci,mode);
186
187   /* adjust for sample rate */
188   /* we don't have any floor 0 modes anymore 
189   for(i=0;i<ci->floors;i++)
190     if(ci->floor_type[i]==0)
191     ((vorbis_info_floor0 *)(ci->floor_param[i]))->rate=rate; */
192
193   /* adjust for channels */
194   /* but all our mappings use submap zero now! */
195
196   return(0);
197 }
198
199 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
200   return(OV_EIMPL);
201 }
202