1 /*
2  *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 /*
12  * This file generates databases with information about all supported audio
13  * codecs.
14  */
15 
16 #ifndef WEBRTC_MODULES_AUDIO_CODING_MAIN_ACM2_ACM_CODEC_DATABASE_H_
17 #define WEBRTC_MODULES_AUDIO_CODING_MAIN_ACM2_ACM_CODEC_DATABASE_H_
18 
19 #include "webrtc/common_types.h"
20 #include "webrtc/modules/audio_coding/main/acm2/acm_generic_codec.h"
21 #include "webrtc/modules/audio_coding/neteq/interface/neteq.h"
22 
23 namespace webrtc {
24 
25 namespace acm2 {
26 
27 // TODO(tlegrand): replace class ACMCodecDB with a namespace.
28 class ACMCodecDB {
29  public:
30   // Enum with array indexes for the supported codecs. NOTE! The order MUST
31   // be the same as when creating the database in acm_codec_database.cc.
32   enum {
33     kNone = -1
34 #if (defined(WEBRTC_CODEC_ISAC) || defined(WEBRTC_CODEC_ISACFX))
35     , kISAC
36 # if (defined(WEBRTC_CODEC_ISAC))
37     , kISACSWB
38     , kISACFB
39 # endif
40 #endif
41 #ifdef WEBRTC_CODEC_PCM16
42     // Mono
43     , kPCM16B
44     , kPCM16Bwb
45     , kPCM16Bswb32kHz
46     // Stereo
47     , kPCM16B_2ch
48     , kPCM16Bwb_2ch
49     , kPCM16Bswb32kHz_2ch
50 #endif
51     // Mono
52     , kPCMU
53     , kPCMA
54     // Stereo
55     , kPCMU_2ch
56     , kPCMA_2ch
57 #ifdef WEBRTC_CODEC_ILBC
58     , kILBC
59 #endif
60 #ifdef WEBRTC_CODEC_G722
61     // Mono
62     , kG722
63     // Stereo
64     , kG722_2ch
65 #endif
66 #ifdef WEBRTC_CODEC_OPUS
67     // Mono and stereo
68     , kOpus
69 #endif
70     , kCNNB
71     , kCNWB
72     , kCNSWB
73 #ifdef ENABLE_48000_HZ
74     , kCNFB
75 #endif
76 #ifdef WEBRTC_CODEC_AVT
77     , kAVT
78 #endif
79 #ifdef WEBRTC_CODEC_RED
80     , kRED
81 #endif
82     , kNumCodecs
83   };
84 
85   // Set unsupported codecs to -1
86 #ifndef WEBRTC_CODEC_ISAC
87   enum {kISACSWB = -1};
88   enum {kISACFB = -1};
89 # ifndef WEBRTC_CODEC_ISACFX
90   enum {kISAC = -1};
91 # endif
92 #endif
93 #ifndef WEBRTC_CODEC_PCM16
94   // Mono
95   enum {kPCM16B = -1};
96   enum {kPCM16Bwb = -1};
97   enum {kPCM16Bswb32kHz = -1};
98   // Stereo
99   enum {kPCM16B_2ch = -1};
100   enum {kPCM16Bwb_2ch = -1};
101   enum {kPCM16Bswb32kHz_2ch = -1};
102 #endif
103   // 48 kHz not supported, always set to -1.
104   enum {kPCM16Bswb48kHz = -1};
105 #ifndef WEBRTC_CODEC_ILBC
106   enum {kILBC = -1};
107 #endif
108 #ifndef WEBRTC_CODEC_G722
109   // Mono
110   enum {kG722 = -1};
111   // Stereo
112   enum {kG722_2ch = -1};
113 #endif
114 #ifndef WEBRTC_CODEC_OPUS
115   // Mono and stereo
116   enum {kOpus = -1};
117 #endif
118 #ifndef WEBRTC_CODEC_AVT
119   enum {kAVT = -1};
120 #endif
121 #ifndef WEBRTC_CODEC_RED
122   enum {kRED = -1};
123 #endif
124 #ifndef ENABLE_48000_HZ
125   enum { kCNFB = -1 };
126 #endif
127 
128   // kMaxNumCodecs - Maximum number of codecs that can be activated in one
129   //                 build.
130   // kMaxNumPacketSize - Maximum number of allowed packet sizes for one codec.
131   // These might need to be increased if adding a new codec to the database
132   static const int kMaxNumCodecs =  50;
133   static const int kMaxNumPacketSize = 6;
134 
135   // Codec specific settings
136   //
137   // num_packet_sizes     - number of allowed packet sizes.
138   // packet_sizes_samples - list of the allowed packet sizes.
139   // basic_block_samples  - assigned a value different from 0 if the codec
140   //                        requires to be fed with a specific number of samples
141   //                        that can be different from packet size.
142   // channel_support      - number of channels supported to encode;
143   //                        1 = mono, 2 = stereo, etc.
144   // owns_decoder         - if true, it means that the codec should own the
145   //                        decoder instance. In this case, the codec should
146   //                        implement ACMGenericCodec::Decoder(), which returns
147   //                        a pointer to AudioDecoder. This pointer is injected
148   //                        into NetEq when this codec is registered as receive
149   //                        codec.
150   struct CodecSettings {
151     int num_packet_sizes;
152     int packet_sizes_samples[kMaxNumPacketSize];
153     int basic_block_samples;
154     int channel_support;
155     bool owns_decoder;
156   };
157 
158   // Gets codec information from database at the position in database given by
159   // [codec_id].
160   // Input:
161   //   [codec_id] - number that specifies at what position in the database to
162   //                get the information.
163   // Output:
164   //   [codec_inst] - filled with information about the codec.
165   // Return:
166   //   0 if successful, otherwise -1.
167   static int Codec(int codec_id, CodecInst* codec_inst);
168 
169   // Returns codec id and mirror id from database, given the information
170   // received in the input [codec_inst]. Mirror id is a number that tells
171   // where to find the codec's memory (instance). The number is either the
172   // same as codec id (most common), or a number pointing at a different
173   // entry in the database, if the codec has several entries with different
174   // payload types. This is used for codecs that must share one struct even if
175   // the payload type differs.
176   // One example is the codec iSAC which has the same struct for both 16 and
177   // 32 khz, but they have different entries in the database. Let's say the
178   // function is called with iSAC 32kHz. The function will return 1 as that is
179   // the entry in the data base, and [mirror_id] = 0, as that is the entry for
180   // iSAC 16 kHz, which holds the shared memory.
181   // Input:
182   //   [codec_inst] - Information about the codec for which we require the
183   //                  database id.
184   // Output:
185   //   [mirror_id] - mirror id, which most often is the same as the return
186   //                 value, see above.
187   //   [err_message] - if present, in the event of a mismatch found between the
188   //                   input and the database, a descriptive error message is
189   //                   written here.
190   //   [err_message] - if present, the length of error message is returned here.
191   // Return:
192   //   codec id if successful, otherwise < 0.
193   static int CodecNumber(const CodecInst& codec_inst, int* mirror_id,
194                          char* err_message, int max_message_len_byte);
195   static int CodecNumber(const CodecInst& codec_inst, int* mirror_id);
196   static int CodecId(const CodecInst& codec_inst);
197   static int CodecId(const char* payload_name, int frequency, int channels);
198   static int ReceiverCodecNumber(const CodecInst& codec_inst, int* mirror_id);
199 
200   // Returns the codec sampling frequency for codec with id = "codec_id" in
201   // database.
202   // TODO(tlegrand): Check if function is needed, or if we can change
203   // to access database directly.
204   // Input:
205   //   [codec_id] - number that specifies at what position in the database to
206   //                get the information.
207   // Return:
208   //   codec sampling frequency if successful, otherwise -1.
209   static int CodecFreq(int codec_id);
210 
211   // Return the codec's basic coding block size in samples.
212   // TODO(tlegrand): Check if function is needed, or if we can change
213   // to access database directly.
214   // Input:
215   //   [codec_id] - number that specifies at what position in the database to
216   //                get the information.
217   // Return:
218   //   codec basic block size if successful, otherwise -1.
219   static int BasicCodingBlock(int codec_id);
220 
221   // Returns the NetEQ decoder database.
222   static const NetEqDecoder* NetEQDecoders();
223 
224   // Returns mirror id, which is a number that tells where to find the codec's
225   // memory (instance). It is either the same as codec id (most common), or a
226   // number pointing at a different entry in the database, if the codec have
227   // several entries with different payload types. This is used for codecs that
228   // must share struct even if the payload type differs.
229   // TODO(tlegrand): Check if function is needed, or if we can change
230   // to access database directly.
231   // Input:
232   //   [codec_id] - number that specifies codec's position in the database.
233   // Return:
234   //   Mirror id on success, otherwise -1.
235   static int MirrorID(int codec_id);
236 
237   // Creates a codec wrapper containing an AudioEncoder object (or an
238   // ACMGenericCodec subclass during the refactoring time). The type of
239   // AudioEncoder is decided by looking at the information in |codec_inst|.
240   // The |cng_pt_*| parameters should contain the RTP payload type used for each
241   // type of comfort noise; if not used (or not know when this function is
242   // called), -1 can be set. The parameter |enable_red| indicates that RED
243   // is enabled, and that |red_payload_type| should be used as RTP payload type
244   // for RED encodings.
245   static ACMGenericCodec* CreateCodecInstance(const CodecInst& codec_inst,
246                                               int cng_pt_nb,
247                                               int cng_pt_wb,
248                                               int cng_pt_swb,
249                                               int cng_pt_fb,
250                                               bool enable_red,
251                                               int red_payload_type);
252 
253   // Specifies if the codec specified by |codec_id| MUST own its own decoder.
254   // This is the case for codecs which *should* share a single codec instance
255   // between encoder and decoder. Or for codecs which ACM should have control
256   // over the decoder. For instance iSAC is such a codec that encoder and
257   // decoder share the same codec instance.
258   static bool OwnsDecoder(int codec_id);
259 
260   // Checks if the bitrate is valid for the codec.
261   // Input:
262   //   [codec_id] - number that specifies codec's position in the database.
263   //   [rate] - bitrate to check.
264   //   [frame_size_samples] - (used for iLBC) specifies which frame size to go
265   //                          with the rate.
266   static bool IsRateValid(int codec_id, int rate);
267   static bool IsISACRateValid(int rate);
268   static bool IsILBCRateValid(int rate, int frame_size_samples);
269   static bool IsAMRRateValid(int rate);
270   static bool IsAMRwbRateValid(int rate);
271   static bool IsG7291RateValid(int rate);
272   static bool IsSpeexRateValid(int rate);
273   static bool IsOpusRateValid(int rate);
274 
275   // Check if the payload type is valid, meaning that it is in the valid range
276   // of 0 to 127.
277   // Input:
278   //   [payload_type] - payload type.
279   static bool ValidPayloadType(int payload_type);
280 
281   // Databases with information about the supported codecs
282   // database_ - stored information about all codecs: payload type, name,
283   //             sampling frequency, packet size in samples, default channel
284   //             support, and default rate.
285   // codec_settings_ - stored codec settings: number of allowed packet sizes,
286   //                   a vector with the allowed packet sizes, basic block
287   //                   samples, and max number of channels that are supported.
288   // neteq_decoders_ - list of supported decoders in NetEQ.
289   static const CodecInst database_[kMaxNumCodecs];
290   static const CodecSettings codec_settings_[kMaxNumCodecs];
291   static const NetEqDecoder neteq_decoders_[kMaxNumCodecs];
292 };
293 
294 }  // namespace acm2
295 
296 }  // namespace webrtc
297 
298 #endif  // WEBRTC_MODULES_AUDIO_CODING_MAIN_ACM2_ACM_CODEC_DATABASE_H_
299