react-native-audio-api 0.7.1-nightly-5a218c6-20250810 → 0.7.1-nightly-4d86c14-20250811

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/RNAudioAPI.podspec +31 -7
  2. package/android/src/main/cpp/audioapi/CMakeLists.txt +22 -1
  3. package/android/src/main/cpp/audioapi/android/core/AudioDecoder.cpp +81 -63
  4. package/common/cpp/audioapi/core/utils/AudioDecoder.h +54 -29
  5. package/common/cpp/audioapi/external/arm64-v8a/libogg.a +0 -0
  6. package/common/cpp/audioapi/external/arm64-v8a/libopus.a +0 -0
  7. package/common/cpp/audioapi/external/arm64-v8a/libopusfile.a +0 -0
  8. package/common/cpp/audioapi/external/arm64-v8a/libvorbis.a +0 -0
  9. package/common/cpp/audioapi/external/arm64-v8a/libvorbisenc.a +0 -0
  10. package/common/cpp/audioapi/external/arm64-v8a/libvorbisfile.a +0 -0
  11. package/common/cpp/audioapi/external/armeabi-v7a/libogg.a +0 -0
  12. package/common/cpp/audioapi/external/armeabi-v7a/libopus.a +0 -0
  13. package/common/cpp/audioapi/external/armeabi-v7a/libopusfile.a +0 -0
  14. package/common/cpp/audioapi/external/armeabi-v7a/libvorbis.a +0 -0
  15. package/common/cpp/audioapi/external/armeabi-v7a/libvorbisenc.a +0 -0
  16. package/common/cpp/audioapi/external/armeabi-v7a/libvorbisfile.a +0 -0
  17. package/common/cpp/audioapi/external/include/ogg/config_types.h +26 -0
  18. package/common/cpp/audioapi/external/include/ogg/config_types.h.in +26 -0
  19. package/common/cpp/audioapi/external/include/ogg/ogg.h +212 -0
  20. package/common/cpp/audioapi/external/include/ogg/os_types.h +157 -0
  21. package/common/cpp/audioapi/external/include/opus/opus.h +1286 -0
  22. package/common/cpp/audioapi/external/include/opus/opus_custom.h +390 -0
  23. package/common/cpp/audioapi/external/include/opus/opus_defines.h +896 -0
  24. package/common/cpp/audioapi/external/include/opus/opus_multistream.h +728 -0
  25. package/common/cpp/audioapi/external/include/opus/opus_projection.h +621 -0
  26. package/common/cpp/audioapi/external/include/opus/opus_types.h +170 -0
  27. package/common/cpp/audioapi/external/include/opusfile/opusfile.h +2151 -0
  28. package/common/cpp/audioapi/external/include/vorbis/codec.h +241 -0
  29. package/common/cpp/audioapi/external/include/vorbis/vorbisenc.h +446 -0
  30. package/common/cpp/audioapi/external/include/vorbis/vorbisfile.h +226 -0
  31. package/common/cpp/audioapi/external/iphoneos/libogg.a +0 -0
  32. package/common/cpp/audioapi/external/iphoneos/libopus.a +0 -0
  33. package/common/cpp/audioapi/external/iphoneos/libopusfile.a +0 -0
  34. package/common/cpp/audioapi/external/iphoneos/libvorbis.a +0 -0
  35. package/common/cpp/audioapi/external/iphoneos/libvorbisenc.a +0 -0
  36. package/common/cpp/audioapi/external/iphoneos/libvorbisfile.a +0 -0
  37. package/common/cpp/audioapi/external/iphonesimulator/libogg.a +0 -0
  38. package/common/cpp/audioapi/external/iphonesimulator/libopus.a +0 -0
  39. package/common/cpp/audioapi/external/iphonesimulator/libopusfile.a +0 -0
  40. package/common/cpp/audioapi/external/iphonesimulator/libvorbis.a +0 -0
  41. package/common/cpp/audioapi/external/iphonesimulator/libvorbisenc.a +0 -0
  42. package/common/cpp/audioapi/external/iphonesimulator/libvorbisfile.a +0 -0
  43. package/common/cpp/audioapi/external/x86/libogg.a +0 -0
  44. package/common/cpp/audioapi/external/x86/libopus.a +0 -0
  45. package/common/cpp/audioapi/external/x86/libopusfile.a +0 -0
  46. package/common/cpp/audioapi/external/x86/libvorbis.a +0 -0
  47. package/common/cpp/audioapi/external/x86/libvorbisenc.a +0 -0
  48. package/common/cpp/audioapi/external/x86/libvorbisfile.a +0 -0
  49. package/common/cpp/audioapi/external/x86_64/libogg.a +0 -0
  50. package/common/cpp/audioapi/external/x86_64/libopus.a +0 -0
  51. package/common/cpp/audioapi/external/x86_64/libopusfile.a +0 -0
  52. package/common/cpp/audioapi/external/x86_64/libvorbis.a +0 -0
  53. package/common/cpp/audioapi/external/x86_64/libvorbisenc.a +0 -0
  54. package/common/cpp/audioapi/external/x86_64/libvorbisfile.a +0 -0
  55. package/common/cpp/audioapi/libs/miniaudio/decoders/libopus/miniaudio_libopus.c +623 -0
  56. package/common/cpp/audioapi/libs/miniaudio/decoders/libopus/miniaudio_libopus.h +71 -0
  57. package/common/cpp/audioapi/libs/miniaudio/decoders/libvorbis/miniaudio_libvorbis.c +574 -0
  58. package/common/cpp/audioapi/libs/miniaudio/decoders/libvorbis/miniaudio_libvorbis.h +41 -0
  59. package/common/cpp/test/CMakeLists.txt +7 -0
  60. package/common/cpp/test/RunTests.sh +3 -8
  61. package/ios/audioapi/ios/core/AudioDecoder.mm +62 -51
  62. package/package.json +1 -1
@@ -0,0 +1,1286 @@
1
+ /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2
+ Written by Jean-Marc Valin and Koen Vos */
3
+ /*
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions
6
+ are met:
7
+
8
+ - Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ - Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
+ ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19
+ OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
+ */
27
+
28
+ /**
29
+ * @file opus.h
30
+ * @brief Opus reference implementation API
31
+ */
32
+
33
+ #ifndef OPUS_H
34
+ #define OPUS_H
35
+
36
+ #include "opus_defines.h"
37
+ #include "opus_types.h"
38
+
39
+ #ifdef __cplusplus
40
+ extern "C" {
41
+ #endif
42
+
43
+ /**
44
+ * @mainpage Opus
45
+ *
46
+ * The Opus codec is designed for interactive speech and audio transmission over
47
+ the Internet.
48
+ * It is designed by the IETF Codec Working Group and incorporates technology
49
+ from
50
+ * Skype's SILK codec and Xiph.Org's CELT codec.
51
+ *
52
+ * The Opus codec is designed to handle a wide range of interactive audio
53
+ applications,
54
+ * including Voice over IP, videoconferencing, in-game chat, and even remote
55
+ live music
56
+ * performances. It can scale from low bit-rate narrowband speech to very high
57
+ quality
58
+ * stereo music. Its main features are:
59
+
60
+ * @li Sampling rates from 8 to 48 kHz
61
+ * @li Bit-rates from 6 kb/s to 510 kb/s
62
+ * @li Support for both constant bit-rate (CBR) and variable bit-rate (VBR)
63
+ * @li Audio bandwidth from narrowband to full-band
64
+ * @li Support for speech and music
65
+ * @li Support for mono and stereo
66
+ * @li Support for multichannel (up to 255 channels)
67
+ * @li Frame sizes from 2.5 ms to 60 ms
68
+ * @li Good loss robustness and packet loss concealment (PLC)
69
+ * @li Floating point and fixed-point implementation
70
+ *
71
+ * Documentation sections:
72
+ * @li @ref opus_encoder
73
+ * @li @ref opus_decoder
74
+ * @li @ref opus_repacketizer
75
+ * @li @ref opus_multistream
76
+ * @li @ref opus_libinfo
77
+ * @li @ref opus_custom
78
+ */
79
+
80
+ /** @defgroup opus_encoder Opus Encoder
81
+ * @{
82
+ *
83
+ * @brief This page describes the process and functions used to encode Opus.
84
+ *
85
+ * Since Opus is a stateful codec, the encoding process starts with creating an
86
+ * encoder state. This can be done with:
87
+ *
88
+ * @code
89
+ * int error;
90
+ * OpusEncoder *enc;
91
+ * enc = opus_encoder_create(Fs, channels, application, &error);
92
+ * @endcode
93
+ *
94
+ * From this point, @c enc can be used for encoding an audio stream. An encoder
95
+ * state
96
+ * @b must @b not be used for more than one stream at the same time. Similarly,
97
+ * the encoder state @b must @b not be re-initialized for each frame.
98
+ *
99
+ * While opus_encoder_create() allocates memory for the state, it's also
100
+ * possible to initialize pre-allocated memory:
101
+ *
102
+ * @code
103
+ * int size;
104
+ * int error;
105
+ * OpusEncoder *enc;
106
+ * size = opus_encoder_get_size(channels);
107
+ * enc = malloc(size);
108
+ * error = opus_encoder_init(enc, Fs, channels, application);
109
+ * @endcode
110
+ *
111
+ * where opus_encoder_get_size() returns the required size for the encoder
112
+ * state. Note that future versions of this code may change the size, so no
113
+ * assumptions should be made about it.
114
+ *
115
+ * The encoder state is always continuous in memory and only a shallow copy is
116
+ * sufficient to copy it (e.g. memcpy())
117
+ *
118
+ * It is possible to change some of the encoder's settings using the
119
+ * opus_encoder_ctl() interface. All these settings already default to the
120
+ * recommended value, so they should only be changed when necessary. The most
121
+ * common settings one may want to change are:
122
+ *
123
+ * @code
124
+ * opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrate));
125
+ * opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(complexity));
126
+ * opus_encoder_ctl(enc, OPUS_SET_SIGNAL(signal_type));
127
+ * @endcode
128
+ *
129
+ * where
130
+ *
131
+ * @arg bitrate is in bits per second (b/s)
132
+ * @arg complexity is a value from 1 to 10, where 1 is the lowest complexity and
133
+ * 10 is the highest
134
+ * @arg signal_type is either OPUS_AUTO (default), OPUS_SIGNAL_VOICE, or
135
+ * OPUS_SIGNAL_MUSIC
136
+ *
137
+ * See @ref opus_encoderctls and @ref opus_genericctls for a complete list of
138
+ * parameters that can be set or queried. Most parameters can be set or changed
139
+ * at any time during a stream.
140
+ *
141
+ * To encode a frame, opus_encode() or opus_encode_float() must be called with
142
+ * exactly one frame (2.5, 5, 10, 20, 40 or 60 ms) of audio data:
143
+ * @code
144
+ * len = opus_encode(enc, audio_frame, frame_size, packet, max_packet);
145
+ * @endcode
146
+ *
147
+ * where
148
+ * <ul>
149
+ * <li>audio_frame is the audio data in opus_int16 (or float for
150
+ * opus_encode_float())</li> <li>frame_size is the duration of the frame in
151
+ * samples (per channel)</li> <li>packet is the byte array to which the
152
+ * compressed data is written</li> <li>max_packet is the maximum number of bytes
153
+ * that can be written in the packet (4000 bytes is recommended). Do not use
154
+ * max_packet to control VBR target bitrate, instead use the #OPUS_SET_BITRATE
155
+ * CTL.</li>
156
+ * </ul>
157
+ *
158
+ * opus_encode() and opus_encode_float() return the number of bytes actually
159
+ * written to the packet. The return value <b>can be negative</b>, which
160
+ * indicates that an error has occurred. If the return value is 2 bytes or less,
161
+ * then the packet does not need to be transmitted (DTX).
162
+ *
163
+ * Once the encoder state if no longer needed, it can be destroyed with
164
+ *
165
+ * @code
166
+ * opus_encoder_destroy(enc);
167
+ * @endcode
168
+ *
169
+ * If the encoder was created with opus_encoder_init() rather than
170
+ * opus_encoder_create(), then no action is required aside from potentially
171
+ * freeing the memory that was manually allocated for it (calling free(enc) for
172
+ * the example above)
173
+ *
174
+ */
175
+
176
+ /** Opus encoder state.
177
+ * This contains the complete state of an Opus encoder.
178
+ * It is position independent and can be freely copied.
179
+ * @see opus_encoder_create,opus_encoder_init
180
+ */
181
+ typedef struct OpusEncoder OpusEncoder;
182
+
183
+ /** Gets the size of an <code>OpusEncoder</code> structure.
184
+ * @param[in] channels <tt>int</tt>: Number of channels.
185
+ * This must be 1 or 2.
186
+ * @returns The size in bytes.
187
+ */
188
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_encoder_get_size(int channels);
189
+
190
+ /**
191
+ */
192
+
193
+ /** Allocates and initializes an encoder state.
194
+ * There are three coding modes:
195
+ *
196
+ * @ref OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice
197
+ * signals. It enhances the input signal by high-pass filtering and
198
+ * emphasizing formants and harmonics. Optionally it includes in-band
199
+ * forward error correction to protect against packet loss. Use this
200
+ * mode for typical VoIP applications. Because of the enhancement,
201
+ * even at high bitrates the output may sound different from the input.
202
+ *
203
+ * @ref OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most
204
+ * non-voice signals like music. Use this mode for music and mixed
205
+ * (music/voice) content, broadcast, and applications requiring less
206
+ * than 15 ms of coding delay.
207
+ *
208
+ * @ref OPUS_APPLICATION_RESTRICTED_LOWDELAY configures low-delay mode that
209
+ * disables the speech-optimized mode in exchange for slightly reduced delay.
210
+ * This mode can only be set on an newly initialized or freshly reset encoder
211
+ * because it changes the codec delay.
212
+ *
213
+ * This is useful when the caller knows that the speech-optimized modes will not
214
+ * be needed (use with caution).
215
+ * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz)
216
+ * This must be one of 8000, 12000, 16000,
217
+ * 24000, or 48000.
218
+ * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input
219
+ * signal
220
+ * @param [in] application <tt>int</tt>: Coding mode (one of @ref
221
+ * OPUS_APPLICATION_VOIP, @ref OPUS_APPLICATION_AUDIO, or @ref
222
+ * OPUS_APPLICATION_RESTRICTED_LOWDELAY)
223
+ * @param [out] error <tt>int*</tt>: @ref opus_errorcodes
224
+ * @note Regardless of the sampling rate and number channels selected, the Opus
225
+ * encoder can switch to a lower audio bandwidth or number of channels if the
226
+ * bitrate selected is too low. This also means that it is safe to always use 48
227
+ * kHz stereo input and let the encoder optimize the encoding.
228
+ */
229
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusEncoder *
230
+ opus_encoder_create(opus_int32 Fs, int channels, int application, int *error);
231
+
232
+ /** Initializes a previously allocated encoder state
233
+ * The memory pointed to by st must be at least the size returned by
234
+ * opus_encoder_get_size(). This is intended for applications which use their
235
+ * own allocator instead of malloc.
236
+ * @see opus_encoder_create(),opus_encoder_get_size()
237
+ * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
238
+ * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
239
+ * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz)
240
+ * This must be one of 8000, 12000, 16000,
241
+ * 24000, or 48000.
242
+ * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input
243
+ * signal
244
+ * @param [in] application <tt>int</tt>: Coding mode (one of
245
+ * OPUS_APPLICATION_VOIP, OPUS_APPLICATION_AUDIO, or
246
+ * OPUS_APPLICATION_RESTRICTED_LOWDELAY)
247
+ * @retval #OPUS_OK Success or @ref opus_errorcodes
248
+ */
249
+ OPUS_EXPORT int
250
+ opus_encoder_init(OpusEncoder *st, opus_int32 Fs, int channels, int application)
251
+ OPUS_ARG_NONNULL(1);
252
+
253
+ /** Encodes an Opus frame.
254
+ * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
255
+ * @param [in] pcm <tt>opus_int16*</tt>: Input signal (interleaved if 2
256
+ * channels). length is frame_size*channels*sizeof(opus_int16)
257
+ * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
258
+ * input signal.
259
+ * This must be an Opus frame size for
260
+ * the encoder's sampling rate.
261
+ * For example, at 48 kHz the permitted
262
+ * values are 120, 240, 480, 960, 1920,
263
+ * and 2880.
264
+ * Passing in a duration of less than
265
+ * 10 ms (480 samples at 48 kHz) will
266
+ * prevent the encoder from using the LPC
267
+ * or hybrid modes.
268
+ * @param [out] data <tt>unsigned char*</tt>: Output payload.
269
+ * This must contain storage for at
270
+ * least \a max_data_bytes.
271
+ * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
272
+ * memory for the output
273
+ * payload. This may be
274
+ * used to impose an upper limit
275
+ * on the instant bitrate, but should not be used as the only bitrate control.
276
+ * Use #OPUS_SET_BITRATE to control the bitrate.
277
+ * @returns The length of the encoded packet (in bytes) on success or a
278
+ * negative error code (see @ref opus_errorcodes) on failure.
279
+ */
280
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode(
281
+ OpusEncoder *st,
282
+ const opus_int16 *pcm,
283
+ int frame_size,
284
+ unsigned char *data,
285
+ opus_int32 max_data_bytes) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2)
286
+ OPUS_ARG_NONNULL(4);
287
+
288
+ /** Encodes an Opus frame.
289
+ * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
290
+ * @param [in] pcm <tt>opus_int32*</tt>: Input signal (interleaved if 2
291
+ * channels) representing (or slightly exceeding) 24-bit values. length is
292
+ * frame_size*channels*sizeof(opus_int32)
293
+ * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
294
+ * input signal.
295
+ * This must be an Opus frame size for
296
+ * the encoder's sampling rate.
297
+ * For example, at 48 kHz the permitted
298
+ * values are 120, 240, 480, 960, 1920,
299
+ * and 2880.
300
+ * Passing in a duration of less than
301
+ * 10 ms (480 samples at 48 kHz) will
302
+ * prevent the encoder from using the LPC
303
+ * or hybrid modes.
304
+ * @param [out] data <tt>unsigned char*</tt>: Output payload.
305
+ * This must contain storage for at
306
+ * least \a max_data_bytes.
307
+ * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
308
+ * memory for the output
309
+ * payload. This may be
310
+ * used to impose an upper limit
311
+ * on the instant bitrate, but should not be used as the only bitrate control.
312
+ * Use #OPUS_SET_BITRATE to control the bitrate.
313
+ * @returns The length of the encoded packet (in bytes) on success or a
314
+ * negative error code (see @ref opus_errorcodes) on failure.
315
+ */
316
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode24(
317
+ OpusEncoder *st,
318
+ const opus_int32 *pcm,
319
+ int frame_size,
320
+ unsigned char *data,
321
+ opus_int32 max_data_bytes) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2)
322
+ OPUS_ARG_NONNULL(4);
323
+
324
+ /** Encodes an Opus frame from floating point input.
325
+ * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
326
+ * @param [in] pcm <tt>float*</tt>: Input in float format (interleaved if 2
327
+ * channels), with a normal range of +/-1.0. Samples with a range beyond +/-1.0
328
+ * are supported but will be clipped by decoders using the integer API and
329
+ * should only be used if it is known that the far end supports extended dynamic
330
+ * range. length is frame_size*channels*sizeof(float)
331
+ * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
332
+ * input signal.
333
+ * This must be an Opus frame size for
334
+ * the encoder's sampling rate.
335
+ * For example, at 48 kHz the permitted
336
+ * values are 120, 240, 480, 960, 1920,
337
+ * and 2880.
338
+ * Passing in a duration of less than
339
+ * 10 ms (480 samples at 48 kHz) will
340
+ * prevent the encoder from using the LPC
341
+ * or hybrid modes.
342
+ * @param [out] data <tt>unsigned char*</tt>: Output payload.
343
+ * This must contain storage for at
344
+ * least \a max_data_bytes.
345
+ * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
346
+ * memory for the output
347
+ * payload. This may be
348
+ * used to impose an upper limit
349
+ * on the instant bitrate, but should not be used as the only bitrate control.
350
+ * Use #OPUS_SET_BITRATE to control the bitrate.
351
+ * @returns The length of the encoded packet (in bytes) on success or a
352
+ * negative error code (see @ref opus_errorcodes) on failure.
353
+ */
354
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode_float(
355
+ OpusEncoder *st,
356
+ const float *pcm,
357
+ int frame_size,
358
+ unsigned char *data,
359
+ opus_int32 max_data_bytes) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2)
360
+ OPUS_ARG_NONNULL(4);
361
+
362
+ /** Frees an <code>OpusEncoder</code> allocated by opus_encoder_create().
363
+ * @param[in] st <tt>OpusEncoder*</tt>: State to be freed.
364
+ */
365
+ OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st);
366
+
367
+ /** Perform a CTL function on an Opus encoder.
368
+ *
369
+ * Generally the request and subsequent arguments are generated
370
+ * by a convenience macro.
371
+ * @param st <tt>OpusEncoder*</tt>: Encoder state.
372
+ * @param request This and all remaining parameters should be replaced by one
373
+ * of the convenience macros in @ref opus_genericctls or
374
+ * @ref opus_encoderctls.
375
+ * @see opus_genericctls
376
+ * @see opus_encoderctls
377
+ */
378
+ OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...)
379
+ OPUS_ARG_NONNULL(1);
380
+ /**@}*/
381
+
382
+ /** @defgroup opus_decoder Opus Decoder
383
+ * @{
384
+ *
385
+ * @brief This page describes the process and functions used to decode Opus.
386
+ *
387
+ * The decoding process also starts with creating a decoder
388
+ * state. This can be done with:
389
+ * @code
390
+ * int error;
391
+ * OpusDecoder *dec;
392
+ * dec = opus_decoder_create(Fs, channels, &error);
393
+ * @endcode
394
+ * where
395
+ * @li Fs is the sampling rate and must be 8000, 12000, 16000, 24000, or 48000
396
+ * @li channels is the number of channels (1 or 2)
397
+ * @li error will hold the error code in case of failure (or #OPUS_OK on
398
+ * success)
399
+ * @li the return value is a newly created decoder state to be used for decoding
400
+ *
401
+ * While opus_decoder_create() allocates memory for the state, it's also
402
+ * possible to initialize pre-allocated memory:
403
+ * @code
404
+ * int size;
405
+ * int error;
406
+ * OpusDecoder *dec;
407
+ * size = opus_decoder_get_size(channels);
408
+ * dec = malloc(size);
409
+ * error = opus_decoder_init(dec, Fs, channels);
410
+ * @endcode
411
+ * where opus_decoder_get_size() returns the required size for the decoder
412
+ * state. Note that future versions of this code may change the size, so no
413
+ * assumptions should be made about it.
414
+ *
415
+ * The decoder state is always continuous in memory and only a shallow copy is
416
+ * sufficient to copy it (e.g. memcpy())
417
+ *
418
+ * To decode a frame, opus_decode() or opus_decode_float() must be called with a
419
+ * packet of compressed audio data:
420
+ * @code
421
+ * frame_size = opus_decode(dec, packet, len, decoded, max_size, 0);
422
+ * @endcode
423
+ * where
424
+ *
425
+ * @li packet is the byte array containing the compressed data
426
+ * @li len is the exact number of bytes contained in the packet
427
+ * @li decoded is the decoded audio data in opus_int16 (or float for
428
+ * opus_decode_float())
429
+ * @li max_size is the max duration of the frame in samples (per channel) that
430
+ * can fit into the decoded_frame array
431
+ *
432
+ * opus_decode() and opus_decode_float() return the number of samples (per
433
+ * channel) decoded from the packet. If that value is negative, then an error
434
+ * has occurred. This can occur if the packet is corrupted or if the audio
435
+ * buffer is too small to hold the decoded audio.
436
+ *
437
+ * Opus is a stateful codec with overlapping blocks and as a result Opus
438
+ * packets are not coded independently of each other. Packets must be
439
+ * passed into the decoder serially and in the correct order for a correct
440
+ * decode. Lost packets can be replaced with loss concealment by calling
441
+ * the decoder with a null pointer and zero length for the missing packet.
442
+ *
443
+ * A single codec state may only be accessed from a single thread at
444
+ * a time and any required locking must be performed by the caller. Separate
445
+ * streams must be decoded with separate decoder states and can be decoded
446
+ * in parallel unless the library was compiled with NONTHREADSAFE_PSEUDOSTACK
447
+ * defined.
448
+ *
449
+ */
450
+
451
+ /** Opus decoder state.
452
+ * This contains the complete state of an Opus decoder.
453
+ * It is position independent and can be freely copied.
454
+ * @see opus_decoder_create,opus_decoder_init
455
+ */
456
+ typedef struct OpusDecoder OpusDecoder;
457
+
458
+ /** Opus DRED decoder.
459
+ * This contains the complete state of an Opus DRED decoder.
460
+ * It is position independent and can be freely copied.
461
+ * @see opus_dred_decoder_create,opus_dred_decoder_init
462
+ */
463
+ typedef struct OpusDREDDecoder OpusDREDDecoder;
464
+
465
+ /** Opus DRED state.
466
+ * This contains the complete state of an Opus DRED packet.
467
+ * It is position independent and can be freely copied.
468
+ * @see opus_dred_create,opus_dred_init
469
+ */
470
+ typedef struct OpusDRED OpusDRED;
471
+
472
+ /** Gets the size of an <code>OpusDecoder</code> structure.
473
+ * @param [in] channels <tt>int</tt>: Number of channels.
474
+ * This must be 1 or 2.
475
+ * @returns The size in bytes.
476
+ */
477
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_size(int channels);
478
+
479
+ /** Allocates and initializes a decoder state.
480
+ * @param [in] Fs <tt>opus_int32</tt>: Sample rate to decode at (Hz).
481
+ * This must be one of 8000, 12000, 16000,
482
+ * 24000, or 48000.
483
+ * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode
484
+ * @param [out] error <tt>int*</tt>: #OPUS_OK Success or @ref opus_errorcodes
485
+ *
486
+ * Internally Opus stores data at 48000 Hz, so that should be the default
487
+ * value for Fs. However, the decoder can efficiently decode to buffers
488
+ * at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use
489
+ * data at the full sample rate, or knows the compressed data doesn't
490
+ * use the full frequency range, it can request decoding at a reduced
491
+ * rate. Likewise, the decoder is capable of filling in either mono or
492
+ * interleaved stereo pcm buffers, at the caller's request.
493
+ */
494
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusDecoder *
495
+ opus_decoder_create(opus_int32 Fs, int channels, int *error);
496
+
497
+ /** Initializes a previously allocated decoder state.
498
+ * The state must be at least the size returned by opus_decoder_get_size().
499
+ * This is intended for applications which use their own allocator instead of
500
+ * malloc. @see opus_decoder_create,opus_decoder_get_size To reset a previously
501
+ * initialized state, use the #OPUS_RESET_STATE CTL.
502
+ * @param [in] st <tt>OpusDecoder*</tt>: Decoder state.
503
+ * @param [in] Fs <tt>opus_int32</tt>: Sampling rate to decode to (Hz).
504
+ * This must be one of 8000, 12000, 16000,
505
+ * 24000, or 48000.
506
+ * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode
507
+ * @retval #OPUS_OK Success or @ref opus_errorcodes
508
+ */
509
+ OPUS_EXPORT int opus_decoder_init(OpusDecoder *st, opus_int32 Fs, int channels)
510
+ OPUS_ARG_NONNULL(1);
511
+
512
+ /** Decode an Opus packet.
513
+ * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
514
+ * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to
515
+ * indicate packet loss
516
+ * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload*
517
+ * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2
518
+ * channels). length is frame_size*channels*sizeof(opus_int16)
519
+ * @param [in] frame_size Number of samples per channel of available space in \a
520
+ * pcm. If this is less than the maximum packet duration (120ms; 5760 for
521
+ * 48kHz), this function will not be capable of decoding some packets. In the
522
+ * case of PLC (data==NULL) or FEC (decode_fec=1), then frame_size needs to be
523
+ * exactly the duration of audio that is missing, otherwise the decoder will not
524
+ * be in the optimal state to decode the next incoming packet. For the PLC and
525
+ * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
526
+ * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any
527
+ * in-band forward error correction data be decoded. If no such data is
528
+ * available, the frame is decoded as if it were lost.
529
+ * @returns Number of decoded samples or @ref opus_errorcodes
530
+ */
531
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode(
532
+ OpusDecoder *st,
533
+ const unsigned char *data,
534
+ opus_int32 len,
535
+ opus_int16 *pcm,
536
+ int frame_size,
537
+ int decode_fec) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
538
+
539
+ /** Decode an Opus packet.
540
+ * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
541
+ * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to
542
+ * indicate packet loss
543
+ * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload*
544
+ * @param [out] pcm <tt>opus_int32*</tt>: Output signal (interleaved if 2
545
+ * channels) representing (or slightly exceeding) 24-bit values. length is
546
+ * frame_size*channels*sizeof(opus_int32)
547
+ * @param [in] frame_size Number of samples per channel of available space in \a
548
+ * pcm. If this is less than the maximum packet duration (120ms; 5760 for
549
+ * 48kHz), this function will not be capable of decoding some packets. In the
550
+ * case of PLC (data==NULL) or FEC (decode_fec=1), then frame_size needs to be
551
+ * exactly the duration of audio that is missing, otherwise the decoder will not
552
+ * be in the optimal state to decode the next incoming packet. For the PLC and
553
+ * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
554
+ * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any
555
+ * in-band forward error correction data be decoded. If no such data is
556
+ * available, the frame is decoded as if it were lost.
557
+ * @returns Number of decoded samples or @ref opus_errorcodes
558
+ */
559
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode24(
560
+ OpusDecoder *st,
561
+ const unsigned char *data,
562
+ opus_int32 len,
563
+ opus_int32 *pcm,
564
+ int frame_size,
565
+ int decode_fec) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
566
+
567
+ /** Decode an Opus packet with floating point output.
568
+ * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
569
+ * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to
570
+ * indicate packet loss
571
+ * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload
572
+ * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels).
573
+ * length is frame_size*channels*sizeof(float)
574
+ * @param [in] frame_size Number of samples per channel of available space in \a
575
+ * pcm. If this is less than the maximum packet duration (120ms; 5760 for
576
+ * 48kHz), this function will not be capable of decoding some packets. In the
577
+ * case of PLC (data==NULL) or FEC (decode_fec=1), then frame_size needs to be
578
+ * exactly the duration of audio that is missing, otherwise the decoder will not
579
+ * be in the optimal state to decode the next incoming packet. For the PLC and
580
+ * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
581
+ * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any
582
+ * in-band forward error correction data be decoded. If no such data is
583
+ * available the frame is decoded as if it were lost.
584
+ * @returns Number of decoded samples or @ref opus_errorcodes
585
+ */
586
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode_float(
587
+ OpusDecoder *st,
588
+ const unsigned char *data,
589
+ opus_int32 len,
590
+ float *pcm,
591
+ int frame_size,
592
+ int decode_fec) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
593
+
594
+ /** Perform a CTL function on an Opus decoder.
595
+ *
596
+ * Generally the request and subsequent arguments are generated
597
+ * by a convenience macro.
598
+ * @param st <tt>OpusDecoder*</tt>: Decoder state.
599
+ * @param request This and all remaining parameters should be replaced by one
600
+ * of the convenience macros in @ref opus_genericctls or
601
+ * @ref opus_decoderctls.
602
+ * @see opus_genericctls
603
+ * @see opus_decoderctls
604
+ */
605
+ OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...)
606
+ OPUS_ARG_NONNULL(1);
607
+
608
+ /** Frees an <code>OpusDecoder</code> allocated by opus_decoder_create().
609
+ * @param[in] st <tt>OpusDecoder*</tt>: State to be freed.
610
+ */
611
+ OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st);
612
+
613
+ /** Gets the size of an <code>OpusDREDDecoder</code> structure.
614
+ * @returns The size in bytes.
615
+ */
616
+ OPUS_EXPORT int opus_dred_decoder_get_size(void);
617
+
618
+ /** Allocates and initializes an OpusDREDDecoder state.
619
+ * @param [out] error <tt>int*</tt>: #OPUS_OK Success or @ref opus_errorcodes
620
+ */
621
+ OPUS_EXPORT OpusDREDDecoder *opus_dred_decoder_create(int *error);
622
+
623
+ /** Initializes an <code>OpusDREDDecoder</code> state.
624
+ * @param[in] dec <tt>OpusDREDDecoder*</tt>: State to be initialized.
625
+ */
626
+ OPUS_EXPORT int opus_dred_decoder_init(OpusDREDDecoder *dec);
627
+
628
+ /** Frees an <code>OpusDREDDecoder</code> allocated by
629
+ * opus_dred_decoder_create().
630
+ * @param[in] dec <tt>OpusDREDDecoder*</tt>: State to be freed.
631
+ */
632
+ OPUS_EXPORT void opus_dred_decoder_destroy(OpusDREDDecoder *dec);
633
+
634
+ /** Perform a CTL function on an Opus DRED decoder.
635
+ *
636
+ * Generally the request and subsequent arguments are generated
637
+ * by a convenience macro.
638
+ * @param dred_dec <tt>OpusDREDDecoder*</tt>: DRED Decoder state.
639
+ * @param request This and all remaining parameters should be replaced by one
640
+ * of the convenience macros in @ref opus_genericctls or
641
+ * @ref opus_decoderctls.
642
+ * @see opus_genericctls
643
+ * @see opus_decoderctls
644
+ */
645
+ OPUS_EXPORT int
646
+ opus_dred_decoder_ctl(OpusDREDDecoder *dred_dec, int request, ...);
647
+
648
+ /** Gets the size of an <code>OpusDRED</code> structure.
649
+ * @returns The size in bytes.
650
+ */
651
+ OPUS_EXPORT int opus_dred_get_size(void);
652
+
653
+ /** Allocates and initializes a DRED state.
654
+ * @param [out] error <tt>int*</tt>: #OPUS_OK Success or @ref opus_errorcodes
655
+ */
656
+ OPUS_EXPORT OpusDRED *opus_dred_alloc(int *error);
657
+
658
+ /** Frees an <code>OpusDRED</code> allocated by opus_dred_create().
659
+ * @param[in] dec <tt>OpusDRED*</tt>: State to be freed.
660
+ */
661
+ OPUS_EXPORT void opus_dred_free(OpusDRED *dec);
662
+
663
+ /** Decode an Opus DRED packet.
664
+ * @param [in] dred_dec <tt>OpusDRED*</tt>: DRED Decoder state
665
+ * @param [in] dred <tt>OpusDRED*</tt>: DRED state
666
+ * @param [in] data <tt>char*</tt>: Input payload
667
+ * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload
668
+ * @param [in] max_dred_samples <tt>opus_int32</tt>: Maximum number of DRED
669
+ * samples that may be needed (if available in the packet).
670
+ * @param [in] sampling_rate <tt>opus_int32</tt>: Sampling rate used for
671
+ * max_dred_samples argument. Needs not match the actual sampling rate of the
672
+ * decoder.
673
+ * @param [out] dred_end <tt>opus_int32*</tt>: Number of non-encoded (silence)
674
+ * samples between the DRED timestamp and the last DRED sample.
675
+ * @param [in] defer_processing <tt>int</tt>: Flag (0 or 1). If set to one, the
676
+ * CPU-intensive part of the DRED decoding is deferred until opus_dred_process()
677
+ * is called.
678
+ * @returns Offset (positive) of the first decoded DRED samples, zero if no DRED
679
+ * is present, or @ref opus_errorcodes
680
+ */
681
+ OPUS_EXPORT int opus_dred_parse(
682
+ OpusDREDDecoder *dred_dec,
683
+ OpusDRED *dred,
684
+ const unsigned char *data,
685
+ opus_int32 len,
686
+ opus_int32 max_dred_samples,
687
+ opus_int32 sampling_rate,
688
+ int *dred_end,
689
+ int defer_processing) OPUS_ARG_NONNULL(1);
690
+
691
+ /** Finish decoding an Opus DRED packet. The function only needs to be called if
692
+ * opus_dred_parse() was called with defer_processing=1. The source and
693
+ * destination will often be the same DRED state.
694
+ * @param [in] dred_dec <tt>OpusDRED*</tt>: DRED Decoder state
695
+ * @param [in] src <tt>OpusDRED*</tt>: Source DRED state to start the processing
696
+ * from.
697
+ * @param [out] dst <tt>OpusDRED*</tt>: Destination DRED state to store the
698
+ * updated state after processing.
699
+ * @returns @ref opus_errorcodes
700
+ */
701
+ OPUS_EXPORT int opus_dred_process(
702
+ OpusDREDDecoder *dred_dec,
703
+ const OpusDRED *src,
704
+ OpusDRED *dst);
705
+
706
+ /** Decode audio from an Opus DRED packet with 16-bit output.
707
+ * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
708
+ * @param [in] dred <tt>OpusDRED*</tt>: DRED state
709
+ * @param [in] dred_offset <tt>opus_int32</tt>: position of the redundancy to
710
+ * decode (in samples before the beginning of the real audio data in the
711
+ * packet).
712
+ * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2
713
+ * channels). length is frame_size*channels*sizeof(opus_int16)
714
+ * @param [in] frame_size Number of samples per channel to decode in \a pcm.
715
+ * frame_size <b>must</b> be a multiple of 2.5 ms.
716
+ * @returns Number of decoded samples or @ref opus_errorcodes
717
+ */
718
+ OPUS_EXPORT int opus_decoder_dred_decode(
719
+ OpusDecoder *st,
720
+ const OpusDRED *dred,
721
+ opus_int32 dred_offset,
722
+ opus_int16 *pcm,
723
+ opus_int32 frame_size);
724
+
725
+ /** Decode audio from an Opus DRED packet with 24-bit output.
726
+ * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
727
+ * @param [in] dred <tt>OpusDRED*</tt>: DRED state
728
+ * @param [in] dred_offset <tt>opus_int32</tt>: position of the redundancy to
729
+ * decode (in samples before the beginning of the real audio data in the
730
+ * packet).
731
+ * @param [out] pcm <tt>opus_int32*</tt>: Output signal (interleaved if 2
732
+ * channels). length is frame_size*channels*sizeof(opus_int16)
733
+ * @param [in] frame_size Number of samples per channel to decode in \a pcm.
734
+ * frame_size <b>must</b> be a multiple of 2.5 ms.
735
+ * @returns Number of decoded samples or @ref opus_errorcodes
736
+ */
737
+ OPUS_EXPORT int opus_decoder_dred_decode24(
738
+ OpusDecoder *st,
739
+ const OpusDRED *dred,
740
+ opus_int32 dred_offset,
741
+ opus_int32 *pcm,
742
+ opus_int32 frame_size);
743
+
744
+ /** Decode audio from an Opus DRED packet with floating point output.
745
+ * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
746
+ * @param [in] dred <tt>OpusDRED*</tt>: DRED state
747
+ * @param [in] dred_offset <tt>opus_int32</tt>: position of the redundancy to
748
+ * decode (in samples before the beginning of the real audio data in the
749
+ * packet).
750
+ * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels).
751
+ * length is frame_size*channels*sizeof(float)
752
+ * @param [in] frame_size Number of samples per channel to decode in \a pcm.
753
+ * frame_size <b>must</b> be a multiple of 2.5 ms.
754
+ * @returns Number of decoded samples or @ref opus_errorcodes
755
+ */
756
+ OPUS_EXPORT int opus_decoder_dred_decode_float(
757
+ OpusDecoder *st,
758
+ const OpusDRED *dred,
759
+ opus_int32 dred_offset,
760
+ float *pcm,
761
+ opus_int32 frame_size);
762
+
763
+ /** Parse an opus packet into one or more frames.
764
+ * Opus_decode will perform this operation internally so most applications do
765
+ * not need to use this function.
766
+ * This function does not copy the frames, the returned pointers are pointers
767
+ * into the input packet.
768
+ * @param [in] data <tt>char*</tt>: Opus packet to be parsed
769
+ * @param [in] len <tt>opus_int32</tt>: size of data
770
+ * @param [out] out_toc <tt>char*</tt>: TOC pointer
771
+ * @param [out] frames <tt>char*[48]</tt> encapsulated frames
772
+ * @param [out] size <tt>opus_int16[48]</tt> sizes of the encapsulated frames
773
+ * @param [out] payload_offset <tt>int*</tt>: returns the position of the
774
+ * payload within the packet (in bytes)
775
+ * @returns number of frames
776
+ */
777
+ OPUS_EXPORT int opus_packet_parse(
778
+ const unsigned char *data,
779
+ opus_int32 len,
780
+ unsigned char *out_toc,
781
+ const unsigned char *frames[48],
782
+ opus_int16 size[48],
783
+ int *payload_offset) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(5);
784
+
785
+ /** Gets the bandwidth of an Opus packet.
786
+ * @param [in] data <tt>char*</tt>: Opus packet
787
+ * @retval OPUS_BANDWIDTH_NARROWBAND Narrowband (4kHz bandpass)
788
+ * @retval OPUS_BANDWIDTH_MEDIUMBAND Mediumband (6kHz bandpass)
789
+ * @retval OPUS_BANDWIDTH_WIDEBAND Wideband (8kHz bandpass)
790
+ * @retval OPUS_BANDWIDTH_SUPERWIDEBAND Superwideband (12kHz bandpass)
791
+ * @retval OPUS_BANDWIDTH_FULLBAND Fullband (20kHz bandpass)
792
+ * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an
793
+ * unsupported type
794
+ */
795
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_bandwidth(
796
+ const unsigned char *data) OPUS_ARG_NONNULL(1);
797
+
798
+ /** Gets the number of samples per frame from an Opus packet.
799
+ * @param [in] data <tt>char*</tt>: Opus packet.
800
+ * This must contain at least one byte of
801
+ * data.
802
+ * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
803
+ * This must be a multiple of 400, or
804
+ * inaccurate results will be returned.
805
+ * @returns Number of samples per frame.
806
+ */
807
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_samples_per_frame(
808
+ const unsigned char *data,
809
+ opus_int32 Fs) OPUS_ARG_NONNULL(1);
810
+
811
+ /** Gets the number of channels from an Opus packet.
812
+ * @param [in] data <tt>char*</tt>: Opus packet
813
+ * @returns Number of channels
814
+ * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an
815
+ * unsupported type
816
+ */
817
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_channels(
818
+ const unsigned char *data) OPUS_ARG_NONNULL(1);
819
+
820
+ /** Gets the number of frames in an Opus packet.
821
+ * @param [in] packet <tt>char*</tt>: Opus packet
822
+ * @param [in] len <tt>opus_int32</tt>: Length of packet
823
+ * @returns Number of frames
824
+ * @retval OPUS_BAD_ARG Insufficient data was passed to the function
825
+ * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an
826
+ * unsupported type
827
+ */
828
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_frames(
829
+ const unsigned char packet[],
830
+ opus_int32 len) OPUS_ARG_NONNULL(1);
831
+
832
+ /** Gets the number of samples of an Opus packet.
833
+ * @param [in] packet <tt>char*</tt>: Opus packet
834
+ * @param [in] len <tt>opus_int32</tt>: Length of packet
835
+ * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
836
+ * This must be a multiple of 400, or
837
+ * inaccurate results will be returned.
838
+ * @returns Number of samples
839
+ * @retval OPUS_BAD_ARG Insufficient data was passed to the function
840
+ * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an
841
+ * unsupported type
842
+ */
843
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_samples(
844
+ const unsigned char packet[],
845
+ opus_int32 len,
846
+ opus_int32 Fs) OPUS_ARG_NONNULL(1);
847
+
848
+ /** Checks whether an Opus packet has LBRR.
849
+ * @param [in] packet <tt>char*</tt>: Opus packet
850
+ * @param [in] len <tt>opus_int32</tt>: Length of packet
851
+ * @returns 1 is LBRR is present, 0 otherwise
852
+ * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an
853
+ * unsupported type
854
+ */
855
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_has_lbrr(
856
+ const unsigned char packet[],
857
+ opus_int32 len);
858
+
859
+ /** Gets the number of samples of an Opus packet.
860
+ * @param [in] dec <tt>OpusDecoder*</tt>: Decoder state
861
+ * @param [in] packet <tt>char*</tt>: Opus packet
862
+ * @param [in] len <tt>opus_int32</tt>: Length of packet
863
+ * @returns Number of samples
864
+ * @retval OPUS_BAD_ARG Insufficient data was passed to the function
865
+ * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an
866
+ * unsupported type
867
+ */
868
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_nb_samples(
869
+ const OpusDecoder *dec,
870
+ const unsigned char packet[],
871
+ opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
872
+
873
+ /** Applies soft-clipping to bring a float signal within the [-1,1] range. If
874
+ * the signal is already in that range, nothing is done. If there are values
875
+ * outside of [-1,1], then the signal is clipped as smoothly as possible to
876
+ * both fit in the range and avoid creating excessive distortion in the
877
+ * process.
878
+ * @param [in,out] pcm <tt>float*</tt>: Input PCM and modified PCM
879
+ * @param [in] frame_size <tt>int</tt> Number of samples per channel to process
880
+ * @param [in] channels <tt>int</tt>: Number of channels
881
+ * @param [in,out] softclip_mem <tt>float*</tt>: State memory for the soft
882
+ * clipping process (one float per channel, initialized to zero)
883
+ */
884
+ OPUS_EXPORT void opus_pcm_soft_clip(
885
+ float *pcm,
886
+ int frame_size,
887
+ int channels,
888
+ float *softclip_mem);
889
+
890
+ /**@}*/
891
+
892
+ /** @defgroup opus_repacketizer Repacketizer
893
+ * @{
894
+ *
895
+ * The repacketizer can be used to merge multiple Opus packets into a single
896
+ * packet or alternatively to split Opus packets that have previously been
897
+ * merged. Splitting valid Opus packets is always guaranteed to succeed,
898
+ * whereas merging valid packets only succeeds if all frames have the same
899
+ * mode, bandwidth, and frame size, and when the total duration of the merged
900
+ * packet is no more than 120 ms. The 120 ms limit comes from the
901
+ * specification and limits decoder memory requirements at a point where
902
+ * framing overhead becomes negligible.
903
+ *
904
+ * The repacketizer currently only operates on elementary Opus
905
+ * streams. It will not manipualte multistream packets successfully, except in
906
+ * the degenerate case where they consist of data from a single stream.
907
+ *
908
+ * The repacketizing process starts with creating a repacketizer state, either
909
+ * by calling opus_repacketizer_create() or by allocating the memory yourself,
910
+ * e.g.,
911
+ * @code
912
+ * OpusRepacketizer *rp;
913
+ * rp = (OpusRepacketizer*)malloc(opus_repacketizer_get_size());
914
+ * if (rp != NULL)
915
+ * opus_repacketizer_init(rp);
916
+ * @endcode
917
+ *
918
+ * Then the application should submit packets with opus_repacketizer_cat(),
919
+ * extract new packets with opus_repacketizer_out() or
920
+ * opus_repacketizer_out_range(), and then reset the state for the next set of
921
+ * input packets via opus_repacketizer_init().
922
+ *
923
+ * For example, to split a sequence of packets into individual frames:
924
+ * @code
925
+ * unsigned char *data;
926
+ * int len;
927
+ * while (get_next_packet(&data, &len))
928
+ * {
929
+ * unsigned char out[1276];
930
+ * opus_int32 out_len;
931
+ * int nb_frames;
932
+ * int err;
933
+ * int i;
934
+ * err = opus_repacketizer_cat(rp, data, len);
935
+ * if (err != OPUS_OK)
936
+ * {
937
+ * release_packet(data);
938
+ * return err;
939
+ * }
940
+ * nb_frames = opus_repacketizer_get_nb_frames(rp);
941
+ * for (i = 0; i < nb_frames; i++)
942
+ * {
943
+ * out_len = opus_repacketizer_out_range(rp, i, i+1, out, sizeof(out));
944
+ * if (out_len < 0)
945
+ * {
946
+ * release_packet(data);
947
+ * return (int)out_len;
948
+ * }
949
+ * output_next_packet(out, out_len);
950
+ * }
951
+ * opus_repacketizer_init(rp);
952
+ * release_packet(data);
953
+ * }
954
+ * @endcode
955
+ *
956
+ * Alternatively, to combine a sequence of frames into packets that each
957
+ * contain up to <code>TARGET_DURATION_MS</code> milliseconds of data:
958
+ * @code
959
+ * // The maximum number of packets with duration TARGET_DURATION_MS occurs
960
+ * // when the frame size is 2.5 ms, for a total of (TARGET_DURATION_MS*2/5)
961
+ * // packets.
962
+ * unsigned char *data[(TARGET_DURATION_MS*2/5)+1];
963
+ * opus_int32 len[(TARGET_DURATION_MS*2/5)+1];
964
+ * int nb_packets;
965
+ * unsigned char out[1277*(TARGET_DURATION_MS*2/2)];
966
+ * opus_int32 out_len;
967
+ * int prev_toc;
968
+ * nb_packets = 0;
969
+ * while (get_next_packet(data+nb_packets, len+nb_packets))
970
+ * {
971
+ * int nb_frames;
972
+ * int err;
973
+ * nb_frames = opus_packet_get_nb_frames(data[nb_packets], len[nb_packets]);
974
+ * if (nb_frames < 1)
975
+ * {
976
+ * release_packets(data, nb_packets+1);
977
+ * return nb_frames;
978
+ * }
979
+ * nb_frames += opus_repacketizer_get_nb_frames(rp);
980
+ * // If adding the next packet would exceed our target, or it has an
981
+ * // incompatible TOC sequence, output the packets we already have before
982
+ * // submitting it.
983
+ * // N.B., The nb_packets > 0 check ensures we've submitted at least one
984
+ * // packet since the last call to opus_repacketizer_init(). Otherwise a
985
+ * // single packet longer than TARGET_DURATION_MS would cause us to try to
986
+ * // output an (invalid) empty packet. It also ensures that prev_toc has
987
+ * // been set to a valid value. Additionally, len[nb_packets] > 0 is
988
+ * // guaranteed by the call to opus_packet_get_nb_frames() above, so the
989
+ * // reference to data[nb_packets][0] should be valid.
990
+ * if (nb_packets > 0 && (
991
+ * ((prev_toc & 0xFC) != (data[nb_packets][0] & 0xFC)) ||
992
+ * opus_packet_get_samples_per_frame(data[nb_packets], 48000)*nb_frames >
993
+ * TARGET_DURATION_MS*48))
994
+ * {
995
+ * out_len = opus_repacketizer_out(rp, out, sizeof(out));
996
+ * if (out_len < 0)
997
+ * {
998
+ * release_packets(data, nb_packets+1);
999
+ * return (int)out_len;
1000
+ * }
1001
+ * output_next_packet(out, out_len);
1002
+ * opus_repacketizer_init(rp);
1003
+ * release_packets(data, nb_packets);
1004
+ * data[0] = data[nb_packets];
1005
+ * len[0] = len[nb_packets];
1006
+ * nb_packets = 0;
1007
+ * }
1008
+ * err = opus_repacketizer_cat(rp, data[nb_packets], len[nb_packets]);
1009
+ * if (err != OPUS_OK)
1010
+ * {
1011
+ * release_packets(data, nb_packets+1);
1012
+ * return err;
1013
+ * }
1014
+ * prev_toc = data[nb_packets][0];
1015
+ * nb_packets++;
1016
+ * }
1017
+ * // Output the final, partial packet.
1018
+ * if (nb_packets > 0)
1019
+ * {
1020
+ * out_len = opus_repacketizer_out(rp, out, sizeof(out));
1021
+ * release_packets(data, nb_packets);
1022
+ * if (out_len < 0)
1023
+ * return (int)out_len;
1024
+ * output_next_packet(out, out_len);
1025
+ * }
1026
+ * @endcode
1027
+ *
1028
+ * An alternate way of merging packets is to simply call opus_repacketizer_cat()
1029
+ * unconditionally until it fails. At that point, the merged packet can be
1030
+ * obtained with opus_repacketizer_out() and the input packet for which
1031
+ * opus_repacketizer_cat() needs to be re-added to a newly reinitialized
1032
+ * repacketizer state.
1033
+ */
1034
+
1035
+ typedef struct OpusRepacketizer OpusRepacketizer;
1036
+
1037
+ /** Gets the size of an <code>OpusRepacketizer</code> structure.
1038
+ * @returns The size in bytes.
1039
+ */
1040
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_size(void);
1041
+
1042
+ /** (Re)initializes a previously allocated repacketizer state.
1043
+ * The state must be at least the size returned by opus_repacketizer_get_size().
1044
+ * This can be used for applications which use their own allocator instead of
1045
+ * malloc().
1046
+ * It must also be called to reset the queue of packets waiting to be
1047
+ * repacketized, which is necessary if the maximum packet duration of 120 ms
1048
+ * is reached or if you wish to submit packets with a different Opus
1049
+ * configuration (coding mode, audio bandwidth, frame size, or channel count).
1050
+ * Failure to do so will prevent a new packet from being added with
1051
+ * opus_repacketizer_cat().
1052
+ * @see opus_repacketizer_create
1053
+ * @see opus_repacketizer_get_size
1054
+ * @see opus_repacketizer_cat
1055
+ * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to
1056
+ * (re)initialize.
1057
+ * @returns A pointer to the same repacketizer state that was passed in.
1058
+ */
1059
+ OPUS_EXPORT OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp)
1060
+ OPUS_ARG_NONNULL(1);
1061
+
1062
+ /** Allocates memory and initializes the new repacketizer with
1063
+ * opus_repacketizer_init().
1064
+ */
1065
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusRepacketizer *opus_repacketizer_create(
1066
+ void);
1067
+
1068
+ /** Frees an <code>OpusRepacketizer</code> allocated by
1069
+ * opus_repacketizer_create().
1070
+ * @param[in] rp <tt>OpusRepacketizer*</tt>: State to be freed.
1071
+ */
1072
+ OPUS_EXPORT void opus_repacketizer_destroy(OpusRepacketizer *rp);
1073
+
1074
+ /** Add a packet to the current repacketizer state.
1075
+ * This packet must match the configuration of any packets already submitted
1076
+ * for repacketization since the last call to opus_repacketizer_init().
1077
+ * This means that it must have the same coding mode, audio bandwidth, frame
1078
+ * size, and channel count.
1079
+ * This can be checked in advance by examining the top 6 bits of the first
1080
+ * byte of the packet, and ensuring they match the top 6 bits of the first
1081
+ * byte of any previously submitted packet.
1082
+ * The total duration of audio in the repacketizer state also must not exceed
1083
+ * 120 ms, the maximum duration of a single packet, after adding this packet.
1084
+ *
1085
+ * The contents of the current repacketizer state can be extracted into new
1086
+ * packets using opus_repacketizer_out() or opus_repacketizer_out_range().
1087
+ *
1088
+ * In order to add a packet with a different configuration or to add more
1089
+ * audio beyond 120 ms, you must clear the repacketizer state by calling
1090
+ * opus_repacketizer_init().
1091
+ * If a packet is too large to add to the current repacketizer state, no part
1092
+ * of it is added, even if it contains multiple frames, some of which might
1093
+ * fit.
1094
+ * If you wish to be able to add parts of such packets, you should first use
1095
+ * another repacketizer to split the packet into pieces and add them
1096
+ * individually.
1097
+ * @see opus_repacketizer_out_range
1098
+ * @see opus_repacketizer_out
1099
+ * @see opus_repacketizer_init
1100
+ * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to which to
1101
+ * add the packet.
1102
+ * @param[in] data <tt>const unsigned char*</tt>: The packet data.
1103
+ * The application must ensure
1104
+ * this pointer remains valid
1105
+ * until the next call to
1106
+ * opus_repacketizer_init() or
1107
+ * opus_repacketizer_destroy().
1108
+ * @param len <tt>opus_int32</tt>: The number of bytes in the packet data.
1109
+ * @returns An error code indicating whether or not the operation succeeded.
1110
+ * @retval #OPUS_OK The packet's contents have been added to the repacketizer
1111
+ * state.
1112
+ * @retval #OPUS_INVALID_PACKET The packet did not have a valid TOC sequence,
1113
+ * the packet's TOC sequence was not compatible
1114
+ * with previously submitted packets (because
1115
+ * the coding mode, audio bandwidth, frame size,
1116
+ * or channel count did not match), or adding
1117
+ * this packet would increase the total amount of
1118
+ * audio stored in the repacketizer state to more
1119
+ * than 120 ms.
1120
+ */
1121
+ OPUS_EXPORT int opus_repacketizer_cat(
1122
+ OpusRepacketizer *rp,
1123
+ const unsigned char *data,
1124
+ opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
1125
+
1126
+ /** Construct a new packet from data previously submitted to the repacketizer
1127
+ * state via opus_repacketizer_cat().
1128
+ * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to
1129
+ * construct the new packet.
1130
+ * @param begin <tt>int</tt>: The index of the first frame in the current
1131
+ * repacketizer state to include in the output.
1132
+ * @param end <tt>int</tt>: One past the index of the last frame in the
1133
+ * current repacketizer state to include in the
1134
+ * output.
1135
+ * @param[out] data <tt>const unsigned char*</tt>: The buffer in which to
1136
+ * store the output packet.
1137
+ * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in
1138
+ * the output buffer. In order to guarantee
1139
+ * success, this should be at least
1140
+ * <code>1276</code> for a single frame,
1141
+ * or for multiple frames,
1142
+ * <code>1277*(end-begin)</code>.
1143
+ * However, <code>1*(end-begin)</code> plus
1144
+ * the size of all packet data submitted to
1145
+ * the repacketizer since the last call to
1146
+ * opus_repacketizer_init() or
1147
+ * opus_repacketizer_create() is also
1148
+ * sufficient, and possibly much smaller.
1149
+ * @returns The total size of the output packet on success, or an error code
1150
+ * on failure.
1151
+ * @retval #OPUS_BAD_ARG <code>[begin,end)</code> was an invalid range of
1152
+ * frames (begin < 0, begin >= end, or end >
1153
+ * opus_repacketizer_get_nb_frames()).
1154
+ * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the
1155
+ * complete output packet.
1156
+ */
1157
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out_range(
1158
+ OpusRepacketizer *rp,
1159
+ int begin,
1160
+ int end,
1161
+ unsigned char *data,
1162
+ opus_int32 maxlen) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
1163
+
1164
+ /** Return the total number of frames contained in packet data submitted to
1165
+ * the repacketizer state so far via opus_repacketizer_cat() since the last
1166
+ * call to opus_repacketizer_init() or opus_repacketizer_create().
1167
+ * This defines the valid range of packets that can be extracted with
1168
+ * opus_repacketizer_out_range() or opus_repacketizer_out().
1169
+ * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state containing the
1170
+ * frames.
1171
+ * @returns The total number of frames contained in the packet data submitted
1172
+ * to the repacketizer state.
1173
+ */
1174
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_nb_frames(
1175
+ OpusRepacketizer *rp) OPUS_ARG_NONNULL(1);
1176
+
1177
+ /** Construct a new packet from data previously submitted to the repacketizer
1178
+ * state via opus_repacketizer_cat().
1179
+ * This is a convenience routine that returns all the data submitted so far
1180
+ * in a single packet.
1181
+ * It is equivalent to calling
1182
+ * @code
1183
+ * opus_repacketizer_out_range(rp, 0, opus_repacketizer_get_nb_frames(rp),
1184
+ * data, maxlen)
1185
+ * @endcode
1186
+ * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to
1187
+ * construct the new packet.
1188
+ * @param[out] data <tt>const unsigned char*</tt>: The buffer in which to
1189
+ * store the output packet.
1190
+ * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in
1191
+ * the output buffer. In order to guarantee
1192
+ * success, this should be at least
1193
+ * <code>1277*opus_repacketizer_get_nb_frames(rp)</code>.
1194
+ * However,
1195
+ * <code>1*opus_repacketizer_get_nb_frames(rp)</code>
1196
+ * plus the size of all packet data
1197
+ * submitted to the repacketizer since the
1198
+ * last call to opus_repacketizer_init() or
1199
+ * opus_repacketizer_create() is also
1200
+ * sufficient, and possibly much smaller.
1201
+ * @returns The total size of the output packet on success, or an error code
1202
+ * on failure.
1203
+ * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the
1204
+ * complete output packet.
1205
+ */
1206
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out(
1207
+ OpusRepacketizer *rp,
1208
+ unsigned char *data,
1209
+ opus_int32 maxlen) OPUS_ARG_NONNULL(1);
1210
+
1211
+ /** Pads a given Opus packet to a larger size (possibly changing the TOC
1212
+ * sequence).
1213
+ * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the
1214
+ * packet to pad.
1215
+ * @param len <tt>opus_int32</tt>: The size of the packet.
1216
+ * This must be at least 1.
1217
+ * @param new_len <tt>opus_int32</tt>: The desired size of the packet after
1218
+ * padding. This must be at least as large as len.
1219
+ * @returns an error code
1220
+ * @retval #OPUS_OK \a on success.
1221
+ * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len.
1222
+ * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
1223
+ */
1224
+ OPUS_EXPORT int
1225
+ opus_packet_pad(unsigned char *data, opus_int32 len, opus_int32 new_len);
1226
+
1227
+ /** Remove all padding from a given Opus packet and rewrite the TOC sequence to
1228
+ * minimize space usage.
1229
+ * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the
1230
+ * packet to strip.
1231
+ * @param len <tt>opus_int32</tt>: The size of the packet.
1232
+ * This must be at least 1.
1233
+ * @returns The new size of the output packet on success, or an error code
1234
+ * on failure.
1235
+ * @retval #OPUS_BAD_ARG \a len was less than 1.
1236
+ * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
1237
+ */
1238
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32
1239
+ opus_packet_unpad(unsigned char *data, opus_int32 len);
1240
+
1241
+ /** Pads a given Opus multi-stream packet to a larger size (possibly changing
1242
+ * the TOC sequence).
1243
+ * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the
1244
+ * packet to pad.
1245
+ * @param len <tt>opus_int32</tt>: The size of the packet.
1246
+ * This must be at least 1.
1247
+ * @param new_len <tt>opus_int32</tt>: The desired size of the packet after
1248
+ * padding. This must be at least 1.
1249
+ * @param nb_streams <tt>opus_int32</tt>: The number of streams (not channels)
1250
+ * in the packet. This must be at least as large as len.
1251
+ * @returns an error code
1252
+ * @retval #OPUS_OK \a on success.
1253
+ * @retval #OPUS_BAD_ARG \a len was less than 1.
1254
+ * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
1255
+ */
1256
+ OPUS_EXPORT int opus_multistream_packet_pad(
1257
+ unsigned char *data,
1258
+ opus_int32 len,
1259
+ opus_int32 new_len,
1260
+ int nb_streams);
1261
+
1262
+ /** Remove all padding from a given Opus multi-stream packet and rewrite the TOC
1263
+ * sequence to minimize space usage.
1264
+ * @param[in,out] data <tt>const unsigned char*</tt>: The buffer containing the
1265
+ * packet to strip.
1266
+ * @param len <tt>opus_int32</tt>: The size of the packet.
1267
+ * This must be at least 1.
1268
+ * @param nb_streams <tt>opus_int32</tt>: The number of streams (not channels)
1269
+ * in the packet. This must be at least 1.
1270
+ * @returns The new size of the output packet on success, or an error code
1271
+ * on failure.
1272
+ * @retval #OPUS_BAD_ARG \a len was less than 1 or new_len was less than len.
1273
+ * @retval #OPUS_INVALID_PACKET \a data did not contain a valid Opus packet.
1274
+ */
1275
+ OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_multistream_packet_unpad(
1276
+ unsigned char *data,
1277
+ opus_int32 len,
1278
+ int nb_streams);
1279
+
1280
+ /**@}*/
1281
+
1282
+ #ifdef __cplusplus
1283
+ }
1284
+ #endif
1285
+
1286
+ #endif /* OPUS_H */