Drop requirement for shared memory segment for win32
[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.16 2001/09/13 21:38:45 cwolf 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  * If this module gets built into a separate shared library 
34  * on win32 (DLL), then the backend mappings from registry.c
35  * need to be referenced from a file-mapped shared memory segment.
36  */
37 #if defined(_MSC_VER) && defined(STANDALONE_VORBISENC_DLL)
38 # include "shmmap.h"
39
40   SHARED_MAP *g_shared_map;
41
42 # define _time_P    g_shared_map->p_time_P
43 # define _floor_P   g_shared_map->p_floor_P
44 # define _residue_P g_shared_map->p_residue_P
45 # define _mapping_P g_shared_map->p_mapping_P
46 #endif
47
48 /* deepcopy all but the codebooks; in this usage, they're static
49    (don't copy as they could be big) */
50 static void codec_setup_partialcopy(codec_setup_info *ci,
51                                  codec_setup_info *cs){
52   int i;
53
54 #if defined(_MSC_VER) && defined(STANDALONE_VORBISENC_DLL)
55   int maplen;
56
57   if ((g_shared_map = table_map2mem(&maplen)) == (SHARED_MAP*)0)
58   ; /* some error handling for memory exhaustion */
59 #endif
60
61   memcpy(ci,cs,sizeof(codec_setup_info)); /* to get the flat numbers */
62
63   /* codebooks */
64   for(i=0;i<ci->books;i++){
65     ci->book_param[i]=cs->book_param[i];
66   }
67
68   /* time backend settings */
69   for(i=0;i<ci->times;i++){
70     ci->time_param[i]=_time_P[ci->time_type[i]]->
71       copy_info(cs->time_param[i]);
72   }
73
74   /* floor backend settings */
75   for(i=0;i<ci->floors;i++){
76     ci->floor_param[i]=_floor_P[ci->floor_type[i]]->
77       copy_info(cs->floor_param[i]);
78   }
79
80   /* residue backend settings */
81   for(i=0;i<ci->residues;i++){
82     ci->residue_param[i]=_residue_P[ci->residue_type[i]]->
83       copy_info(cs->residue_param[i]);
84   }
85
86   /* map backend settings */
87   for(i=0;i<ci->maps;i++){
88     ci->map_param[i]=_mapping_P[ci->map_type[i]]->
89       copy_info(cs->map_param[i]);
90   }
91   
92   /* mode settings */
93   for(i=0;i<ci->modes;i++){
94     ci->mode_param[i]=_ogg_calloc(1,sizeof(vorbis_info_mode));
95     ci->mode_param[i]->blockflag=cs->mode_param[i]->blockflag;
96     ci->mode_param[i]->windowtype=cs->mode_param[i]->windowtype;
97     ci->mode_param[i]->transformtype=cs->mode_param[i]->transformtype;
98     ci->mode_param[i]->mapping=cs->mode_param[i]->mapping;
99   }
100
101   /* psy settings */
102   for(i=0;i<ci->psys;i++){
103     ci->psy_param[i]=_vi_psy_copy(cs->psy_param[i]);
104   }
105
106 }
107
108 /* encoders will need to use vorbis_info_init beforehand and call
109    vorbis_info clear when all done */
110
111 int vorbis_encode_init_vbr(vorbis_info *vi,
112                            long channels,
113                            long rate,
114                            
115                            float base_quality /* 0. to 1. */
116                            ){
117
118   switch(channels){
119   case 2:
120     return(OV_EIMPL);
121
122
123     break;
124   default:
125     return(OV_EIMPL);
126
127     break;
128   }
129 }
130
131
132 int vorbis_encode_init(vorbis_info *vi,
133                        long channels,
134                        long rate,
135
136                        long max_bitrate,
137                        long nominal_bitrate,
138                        long min_bitrate){
139
140   long bpch;
141   int i,j;
142   codec_setup_info *ci=vi->codec_setup;
143   codec_setup_info *mode=NULL;
144   if(!ci)return(OV_EFAULT);
145
146   vi->version=0;
147   vi->channels=channels;
148   vi->rate=rate;
149   
150   vi->bitrate_upper=max_bitrate;
151   vi->bitrate_nominal=nominal_bitrate;
152   vi->bitrate_lower=min_bitrate;
153   vi->bitrate_window=2;
154
155   /* copy a mode into our allocated storage */
156   bpch=nominal_bitrate/channels;
157
158 #if 0
159   switch(channels){
160   case 2:
161 /*      if(rate>40000){ */
162
163     if(bpch<35000){
164       mode=&info_44c_Z;
165     }else if(bpch<45000){
166       mode=&info_44c_Y;
167     }else if(bpch<55000){
168       mode=&info_44c_X;
169     }else if(bpch<75000){
170       mode=&info_44c_A;
171     }else if(bpch<90000){
172       mode=&info_44c_B;
173     }else if(bpch<110000){
174       mode=&info_44c_C;
175     }else if(bpch<160000){
176       mode=&info_44c_D;
177     }else{
178       mode=&info_44c_E;
179     }
180 /*      }       */
181
182     break;
183   default:
184
185     if(bpch<40000){
186       mode=&info_44_Z;
187     }else if(bpch<50000){
188       mode=&info_44_Y;
189     }else if(bpch<70000){
190       mode=&info_44_X;
191     }else if(bpch<90000){
192       mode=&info_44_A;
193     }else if(bpch<120000){
194       mode=&info_44_B;
195     }else{
196       mode=&info_44_C;
197     }
198     break;
199   }
200 #endif
201
202   mode=&info_44c_A;
203
204   /* now we have to deepcopy */
205   codec_setup_partialcopy(ci,mode);
206
207   /* adjust for sample rate */
208   /* we don't have any floor 0 modes anymore 
209   for(i=0;i<ci->floors;i++)
210     if(ci->floor_type[i]==0)
211     ((vorbis_info_floor0 *)(ci->floor_param[i]))->rate=rate; */
212
213   /* adjust for channels */
214   /* but all our mappings use submap zero now! */
215
216   return(0);
217 }
218
219 int vorbis_encode_ctl(vorbis_info *vi,int number,void *arg){
220   return(OV_EIMPL);
221 }
222