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
23namespace webrtc {
24
25namespace acm2 {
26
27// TODO(tlegrand): replace class ACMCodecDB with a namespace.
28class 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_AMR
61    , kGSMAMR
62#endif
63#ifdef WEBRTC_CODEC_AMRWB
64    , kGSMAMRWB
65#endif
66#ifdef WEBRTC_CODEC_CELT
67    // Mono
68    , kCELT32
69    // Stereo
70    , kCELT32_2ch
71#endif
72#ifdef WEBRTC_CODEC_G722
73    // Mono
74    , kG722
75    // Stereo
76    , kG722_2ch
77#endif
78#ifdef WEBRTC_CODEC_G722_1
79    , kG722_1_32
80    , kG722_1_24
81    , kG722_1_16
82#endif
83#ifdef WEBRTC_CODEC_G722_1C
84    , kG722_1C_48
85    , kG722_1C_32
86    , kG722_1C_24
87#endif
88#ifdef WEBRTC_CODEC_G729
89    , kG729
90#endif
91#ifdef WEBRTC_CODEC_G729_1
92    , kG729_1
93#endif
94#ifdef WEBRTC_CODEC_GSMFR
95    , kGSMFR
96#endif
97#ifdef WEBRTC_CODEC_OPUS
98    // Mono and stereo
99    , kOpus
100#endif
101#ifdef WEBRTC_CODEC_SPEEX
102    , kSPEEX8
103    , kSPEEX16
104#endif
105    , kCNNB
106    , kCNWB
107    , kCNSWB
108#ifdef ENABLE_48000_HZ
109    , kCNFB
110#endif
111#ifdef WEBRTC_CODEC_AVT
112    , kAVT
113#endif
114#ifdef WEBRTC_CODEC_RED
115    , kRED
116#endif
117    , kNumCodecs
118  };
119
120  // Set unsupported codecs to -1
121#ifndef WEBRTC_CODEC_ISAC
122  enum {kISACSWB = -1};
123  enum {kISACFB = -1};
124# ifndef WEBRTC_CODEC_ISACFX
125  enum {kISAC = -1};
126# endif
127#endif
128#ifndef WEBRTC_CODEC_PCM16
129  // Mono
130  enum {kPCM16B = -1};
131  enum {kPCM16Bwb = -1};
132  enum {kPCM16Bswb32kHz = -1};
133  // Stereo
134  enum {kPCM16B_2ch = -1};
135  enum {kPCM16Bwb_2ch = -1};
136  enum {kPCM16Bswb32kHz_2ch = -1};
137#endif
138  // 48 kHz not supported, always set to -1.
139  enum {kPCM16Bswb48kHz = -1};
140#ifndef WEBRTC_CODEC_ILBC
141  enum {kILBC = -1};
142#endif
143#ifndef WEBRTC_CODEC_AMR
144  enum {kGSMAMR = -1};
145#endif
146#ifndef WEBRTC_CODEC_AMRWB
147  enum {kGSMAMRWB = -1};
148#endif
149#ifndef WEBRTC_CODEC_CELT
150  // Mono
151  enum {kCELT32 = -1};
152  // Stereo
153  enum {kCELT32_2ch = -1};
154#endif
155#ifndef WEBRTC_CODEC_G722
156  // Mono
157  enum {kG722 = -1};
158  // Stereo
159  enum {kG722_2ch = -1};
160#endif
161#ifndef WEBRTC_CODEC_G722_1
162  enum {kG722_1_32 = -1};
163  enum {kG722_1_24 = -1};
164  enum {kG722_1_16 = -1};
165#endif
166#ifndef WEBRTC_CODEC_G722_1C
167  enum {kG722_1C_48 = -1};
168  enum {kG722_1C_32 = -1};
169  enum {kG722_1C_24 = -1};
170#endif
171#ifndef WEBRTC_CODEC_G729
172  enum {kG729 = -1};
173#endif
174#ifndef WEBRTC_CODEC_G729_1
175  enum {kG729_1 = -1};
176#endif
177#ifndef WEBRTC_CODEC_GSMFR
178  enum {kGSMFR = -1};
179#endif
180#ifndef WEBRTC_CODEC_SPEEX
181  enum {kSPEEX8 = -1};
182  enum {kSPEEX16 = -1};
183#endif
184#ifndef WEBRTC_CODEC_OPUS
185  // Mono and stereo
186  enum {kOpus = -1};
187#endif
188#ifndef WEBRTC_CODEC_AVT
189  enum {kAVT = -1};
190#endif
191#ifndef WEBRTC_CODEC_RED
192  enum {kRED = -1};
193#endif
194#ifndef ENABLE_48000_HZ
195  enum { kCNFB = -1 };
196#endif
197
198  // kMaxNumCodecs - Maximum number of codecs that can be activated in one
199  //                 build.
200  // kMaxNumPacketSize - Maximum number of allowed packet sizes for one codec.
201  // These might need to be increased if adding a new codec to the database
202  static const int kMaxNumCodecs =  50;
203  static const int kMaxNumPacketSize = 6;
204
205  // Codec specific settings
206  //
207  // num_packet_sizes     - number of allowed packet sizes.
208  // packet_sizes_samples - list of the allowed packet sizes.
209  // basic_block_samples  - assigned a value different from 0 if the codec
210  //                        requires to be fed with a specific number of samples
211  //                        that can be different from packet size.
212  // channel_support      - number of channels supported to encode;
213  //                        1 = mono, 2 = stereo, etc.
214  // owns_decoder         - if true, it means that the codec should own the
215  //                        decoder instance. In this case, the codec should
216  //                        implement ACMGenericCodec::Decoder(), which returns
217  //                        a pointer to AudioDecoder. This pointer is injected
218  //                        into NetEq when this codec is registered as receive
219  //                        codec.
220  struct CodecSettings {
221    int num_packet_sizes;
222    int packet_sizes_samples[kMaxNumPacketSize];
223    int basic_block_samples;
224    int channel_support;
225    bool owns_decoder;
226  };
227
228  // Gets codec information from database at the position in database given by
229  // [codec_id].
230  // Input:
231  //   [codec_id] - number that specifies at what position in the database to
232  //                get the information.
233  // Output:
234  //   [codec_inst] - filled with information about the codec.
235  // Return:
236  //   0 if successful, otherwise -1.
237  static int Codec(int codec_id, CodecInst* codec_inst);
238
239  // Returns codec id and mirror id from database, given the information
240  // received in the input [codec_inst]. Mirror id is a number that tells
241  // where to find the codec's memory (instance). The number is either the
242  // same as codec id (most common), or a number pointing at a different
243  // entry in the database, if the codec has several entries with different
244  // payload types. This is used for codecs that must share one struct even if
245  // the payload type differs.
246  // One example is the codec iSAC which has the same struct for both 16 and
247  // 32 khz, but they have different entries in the database. Let's say the
248  // function is called with iSAC 32kHz. The function will return 1 as that is
249  // the entry in the data base, and [mirror_id] = 0, as that is the entry for
250  // iSAC 16 kHz, which holds the shared memory.
251  // Input:
252  //   [codec_inst] - Information about the codec for which we require the
253  //                  database id.
254  // Output:
255  //   [mirror_id] - mirror id, which most often is the same as the return
256  //                 value, see above.
257  //   [err_message] - if present, in the event of a mismatch found between the
258  //                   input and the database, a descriptive error message is
259  //                   written here.
260  //   [err_message] - if present, the length of error message is returned here.
261  // Return:
262  //   codec id if successful, otherwise < 0.
263  static int CodecNumber(const CodecInst& codec_inst, int* mirror_id,
264                         char* err_message, int max_message_len_byte);
265  static int CodecNumber(const CodecInst& codec_inst, int* mirror_id);
266  static int CodecId(const CodecInst& codec_inst);
267  static int CodecId(const char* payload_name, int frequency, int channels);
268  static int ReceiverCodecNumber(const CodecInst& codec_inst, int* mirror_id);
269
270  // Returns the codec sampling frequency for codec with id = "codec_id" in
271  // database.
272  // TODO(tlegrand): Check if function is needed, or if we can change
273  // to access database directly.
274  // Input:
275  //   [codec_id] - number that specifies at what position in the database to
276  //                get the information.
277  // Return:
278  //   codec sampling frequency if successful, otherwise -1.
279  static int CodecFreq(int codec_id);
280
281  // Return the codec's basic coding block size in samples.
282  // TODO(tlegrand): Check if function is needed, or if we can change
283  // to access database directly.
284  // Input:
285  //   [codec_id] - number that specifies at what position in the database to
286  //                get the information.
287  // Return:
288  //   codec basic block size if successful, otherwise -1.
289  static int BasicCodingBlock(int codec_id);
290
291  // Returns the NetEQ decoder database.
292  static const NetEqDecoder* NetEQDecoders();
293
294  // Returns mirror id, which is a number that tells where to find the codec's
295  // memory (instance). It is either the same as codec id (most common), or a
296  // number pointing at a different entry in the database, if the codec have
297  // several entries with different payload types. This is used for codecs that
298  // must share struct even if the payload type differs.
299  // TODO(tlegrand): Check if function is needed, or if we can change
300  // to access database directly.
301  // Input:
302  //   [codec_id] - number that specifies codec's position in the database.
303  // Return:
304  //   Mirror id on success, otherwise -1.
305  static int MirrorID(int codec_id);
306
307  // Create memory/instance for storing codec state.
308  // Input:
309  //   [codec_inst] - information about codec. Only name of codec, "plname", is
310  //                  used in this function.
311  static ACMGenericCodec* CreateCodecInstance(const CodecInst& codec_inst);
312
313  // Specifies if the codec specified by |codec_id| MUST own its own decoder.
314  // This is the case for codecs which *should* share a single codec instance
315  // between encoder and decoder. Or for codecs which ACM should have control
316  // over the decoder. For instance iSAC is such a codec that encoder and
317  // decoder share the same codec instance.
318  static bool OwnsDecoder(int codec_id);
319
320  // Checks if the bitrate is valid for the codec.
321  // Input:
322  //   [codec_id] - number that specifies codec's position in the database.
323  //   [rate] - bitrate to check.
324  //   [frame_size_samples] - (used for iLBC) specifies which frame size to go
325  //                          with the rate.
326  static bool IsRateValid(int codec_id, int rate);
327  static bool IsISACRateValid(int rate);
328  static bool IsILBCRateValid(int rate, int frame_size_samples);
329  static bool IsAMRRateValid(int rate);
330  static bool IsAMRwbRateValid(int rate);
331  static bool IsG7291RateValid(int rate);
332  static bool IsSpeexRateValid(int rate);
333  static bool IsOpusRateValid(int rate);
334  static bool IsCeltRateValid(int rate);
335
336  // Check if the payload type is valid, meaning that it is in the valid range
337  // of 0 to 127.
338  // Input:
339  //   [payload_type] - payload type.
340  static bool ValidPayloadType(int payload_type);
341
342  // Databases with information about the supported codecs
343  // database_ - stored information about all codecs: payload type, name,
344  //             sampling frequency, packet size in samples, default channel
345  //             support, and default rate.
346  // codec_settings_ - stored codec settings: number of allowed packet sizes,
347  //                   a vector with the allowed packet sizes, basic block
348  //                   samples, and max number of channels that are supported.
349  // neteq_decoders_ - list of supported decoders in NetEQ.
350  static const CodecInst database_[kMaxNumCodecs];
351  static const CodecSettings codec_settings_[kMaxNumCodecs];
352  static const NetEqDecoder neteq_decoders_[kMaxNumCodecs];
353};
354
355}  // namespace acm2
356
357}  // namespace webrtc
358
359#endif  // WEBRTC_MODULES_AUDIO_CODING_MAIN_ACM2_ACM_CODEC_DATABASE_H_
360