Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: include/opus.h

Issue 12388030: Update Opus to 1.0.2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/opus
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « doc/Makefile.in ('k') | include/opus_defines.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited 1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */ 2 Written by Jean-Marc Valin and Koen Vos */
3 /* 3 /*
4 Redistribution and use in source and binary forms, with or without 4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions 5 modification, are permitted provided that the following conditions
6 are met: 6 are met:
7 7
8 - Redistributions of source code must retain the above copyright 8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer. 9 notice, this list of conditions and the following disclaimer.
10 10
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 * @li Support for mono and stereo 60 * @li Support for mono and stereo
61 * @li Support for multichannel (up to 255 channels) 61 * @li Support for multichannel (up to 255 channels)
62 * @li Frame sizes from 2.5 ms to 60 ms 62 * @li Frame sizes from 2.5 ms to 60 ms
63 * @li Good loss robustness and packet loss concealment (PLC) 63 * @li Good loss robustness and packet loss concealment (PLC)
64 * @li Floating point and fixed-point implementation 64 * @li Floating point and fixed-point implementation
65 * 65 *
66 * Documentation sections: 66 * Documentation sections:
67 * @li @ref opus_encoder 67 * @li @ref opus_encoder
68 * @li @ref opus_decoder 68 * @li @ref opus_decoder
69 * @li @ref opus_repacketizer 69 * @li @ref opus_repacketizer
70 * @li @ref opus_multistream
70 * @li @ref opus_libinfo 71 * @li @ref opus_libinfo
71 * @li @ref opus_custom 72 * @li @ref opus_custom
72 */ 73 */
73 74
74 /** @defgroup opus_encoder Opus Encoder 75 /** @defgroup opus_encoder Opus Encoder
75 * @{ 76 * @{
76 * 77 *
77 * @brief This page describes the process and functions used to encode Opus. 78 * @brief This page describes the process and functions used to encode Opus.
78 * 79 *
79 * Since Opus is a stateful codec, the encoding process starts with creating an encoder 80 * Since Opus is a stateful codec, the encoding process starts with creating an encoder
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 * To encode a frame, opus_encode() or opus_encode_float() must be called with exactly one frame (2.5, 5, 10, 20, 40 or 60 ms) of audio data: 129 * To encode a frame, opus_encode() or opus_encode_float() must be called with exactly one frame (2.5, 5, 10, 20, 40 or 60 ms) of audio data:
129 * @code 130 * @code
130 * len = opus_encode(enc, audio_frame, frame_size, packet, max_packet); 131 * len = opus_encode(enc, audio_frame, frame_size, packet, max_packet);
131 * @endcode 132 * @endcode
132 * 133 *
133 * where 134 * where
134 * <ul> 135 * <ul>
135 * <li>audio_frame is the audio data in opus_int16 (or float for opus_encode_fl oat())</li> 136 * <li>audio_frame is the audio data in opus_int16 (or float for opus_encode_fl oat())</li>
136 * <li>frame_size is the duration of the frame in samples (per channel)</li> 137 * <li>frame_size is the duration of the frame in samples (per channel)</li>
137 * <li>packet is the byte array to which the compressed data is written</li> 138 * <li>packet is the byte array to which the compressed data is written</li>
138 * <li>max_packet is the maximum number of bytes that can be written in the pac ket (4000 bytes is recommended)</li> 139 * <li>max_packet is the maximum number of bytes that can be written in the pac ket (4000 bytes is recommended).
140 * Do not use max_packet to control VBR target bitrate, instead use the #OP US_SET_BITRATE CTL.</li>
139 * </ul> 141 * </ul>
140 * 142 *
141 * opus_encode() and opus_encode_frame() return the number of bytes actually wr itten to the packet. 143 * opus_encode() and opus_encode_float() return the number of bytes actually wr itten to the packet.
142 * The return value <b>can be negative</b>, which indicates that an error has o ccurred. If the return value 144 * The return value <b>can be negative</b>, which indicates that an error has o ccurred. If the return value
143 * is 1 byte, then the packet does not need to be transmitted (DTX). 145 * is 1 byte, then the packet does not need to be transmitted (DTX).
144 * 146 *
145 * Once the encoder state if no longer needed, it can be destroyed with 147 * Once the encoder state if no longer needed, it can be destroyed with
146 * 148 *
147 * @code 149 * @code
148 * opus_encoder_destroy(enc); 150 * opus_encoder_destroy(enc);
149 * @endcode 151 * @endcode
150 * 152 *
151 * If the encoder was created with opus_encoder_init() rather than opus_encoder _create(), 153 * If the encoder was created with opus_encoder_init() rather than opus_encoder _create(),
152 * then no action is required aside from potentially freeing the memory that wa s manually 154 * then no action is required aside from potentially freeing the memory that wa s manually
153 * allocated for it (calling free(enc) for the example above) 155 * allocated for it (calling free(enc) for the example above)
154 * 156 *
155 */ 157 */
156 158
157 /** Opus encoder state. 159 /** Opus encoder state.
158 * This contains the complete state of an Opus encoder. 160 * This contains the complete state of an Opus encoder.
159 * It is position independent and can be freely copied. 161 * It is position independent and can be freely copied.
160 * @see opus_encoder_create,opus_encoder_init 162 * @see opus_encoder_create,opus_encoder_init
161 */ 163 */
162 typedef struct OpusEncoder OpusEncoder; 164 typedef struct OpusEncoder OpusEncoder;
163 165
166 /** Gets the size of an <code>OpusEncoder</code> structure.
167 * @param[in] channels <tt>int</tt>: Number of channels.
168 * This must be 1 or 2.
169 * @returns The size in bytes.
170 */
164 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_encoder_get_size(int channels); 171 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_encoder_get_size(int channels);
165 172
166 /** 173 /**
167 */ 174 */
168 175
169 /** Allocates and initializes an encoder state. 176 /** Allocates and initializes an encoder state.
170 * There are three coding modes: 177 * There are three coding modes:
171 * 178 *
172 * @ref OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice 179 * @ref OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice
173 * signals. It enhances the input signal by high-pass filtering and 180 * signals. It enhances the input signal by high-pass filtering and
174 * emphasizing formants and harmonics. Optionally it includes in-band 181 * emphasizing formants and harmonics. Optionally it includes in-band
175 * forward error correction to protect against packet loss. Use this 182 * forward error correction to protect against packet loss. Use this
176 * mode for typical VoIP applications. Because of the enhancement, 183 * mode for typical VoIP applications. Because of the enhancement,
177 * even at high bitrates the output may sound different from the input. 184 * even at high bitrates the output may sound different from the input.
178 * 185 *
179 * @ref OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most 186 * @ref OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most
180 * non-voice signals like music. Use this mode for music and mixed 187 * non-voice signals like music. Use this mode for music and mixed
181 * (music/voice) content, broadcast, and applications requiring less 188 * (music/voice) content, broadcast, and applications requiring less
182 * than 15 ms of coding delay. 189 * than 15 ms of coding delay.
183 * 190 *
184 * @ref OPUS_APPLICATION_RESTRICTED_LOWDELAY configures low-delay mode that 191 * @ref OPUS_APPLICATION_RESTRICTED_LOWDELAY configures low-delay mode that
185 * disables the speech-optimized mode in exchange for slightly reduced delay. 192 * disables the speech-optimized mode in exchange for slightly reduced delay.
186 * This mode can only be set on an newly initialized or freshly reset encoder 193 * This mode can only be set on an newly initialized or freshly reset encoder
187 * because it changes the codec delay. 194 * because it changes the codec delay.
188 * 195 *
189 * This is useful when the caller knows that the speech-optimized modes will not be needed (use with caution). 196 * This is useful when the caller knows that the speech-optimized modes will not be needed (use with caution).
190 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz) 197 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz)
191 * @param [in] channels <tt>int</tt>: Number of channels (1/2) in input signal 198 * This must be one of 8000, 12000, 16000,
199 * 24000, or 48000.
200 * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input signa l
192 * @param [in] application <tt>int</tt>: Coding mode (@ref OPUS_APPLICATION_VOIP /@ref OPUS_APPLICATION_AUDIO/@ref OPUS_APPLICATION_RESTRICTED_LOWDELAY) 201 * @param [in] application <tt>int</tt>: Coding mode (@ref OPUS_APPLICATION_VOIP /@ref OPUS_APPLICATION_AUDIO/@ref OPUS_APPLICATION_RESTRICTED_LOWDELAY)
193 * @param [out] error <tt>int*</tt>: @ref opus_errorcodes 202 * @param [out] error <tt>int*</tt>: @ref opus_errorcodes
194 * @note Regardless of the sampling rate and number channels selected, the Opus encoder 203 * @note Regardless of the sampling rate and number channels selected, the Opus encoder
195 * can switch to a lower audio audio bandwidth or number of channels if the bitr ate 204 * can switch to a lower audio bandwidth or number of channels if the bitrate
196 * selected is too low. This also means that it is safe to always use 48 kHz ste reo input 205 * selected is too low. This also means that it is safe to always use 48 kHz ste reo input
197 * and let the encoder optimize the encoding. 206 * and let the encoder optimize the encoding.
198 */ 207 */
199 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusEncoder *opus_encoder_create( 208 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusEncoder *opus_encoder_create(
200 opus_int32 Fs, 209 opus_int32 Fs,
201 int channels, 210 int channels,
202 int application, 211 int application,
203 int *error 212 int *error
204 ); 213 );
205 214
206 /** Initializes a previously allocated encoder state 215 /** Initializes a previously allocated encoder state
207 * The memory pointed to by st must be the size returned by opus_encoder_get_si ze. 216 * The memory pointed to by st must be at least the size returned by opus_encod er_get_size().
208 * This is intended for applications which use their own allocator instead of m alloc. 217 * This is intended for applications which use their own allocator instead of m alloc.
209 * @see opus_encoder_create(),opus_encoder_get_size() 218 * @see opus_encoder_create(),opus_encoder_get_size()
210 * To reset a previously initialized state use the OPUS_RESET_STATE CTL. 219 * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
211 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state 220 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
212 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz) 221 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate of input signal (Hz)
213 * @param [in] channels <tt>int</tt>: Number of channels (1/2) in input signal 222 * This must be one of 8000, 12000, 16000,
223 * 24000, or 48000.
224 * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) in input sign al
214 * @param [in] application <tt>int</tt>: Coding mode (OPUS_APPLICATION_VOIP/OPU S_APPLICATION_AUDIO/OPUS_APPLICATION_RESTRICTED_LOWDELAY) 225 * @param [in] application <tt>int</tt>: Coding mode (OPUS_APPLICATION_VOIP/OPU S_APPLICATION_AUDIO/OPUS_APPLICATION_RESTRICTED_LOWDELAY)
215 * @retval OPUS_OK Success or @ref opus_errorcodes 226 * @retval #OPUS_OK Success or @ref opus_errorcodes
216 */ 227 */
217 OPUS_EXPORT int opus_encoder_init( 228 OPUS_EXPORT int opus_encoder_init(
218 OpusEncoder *st, 229 OpusEncoder *st,
219 opus_int32 Fs, 230 opus_int32 Fs,
220 int channels, 231 int channels,
221 int application 232 int application
222 ) OPUS_ARG_NONNULL(1); 233 ) OPUS_ARG_NONNULL(1);
223 234
224 /** Encodes an Opus frame. 235 /** Encodes an Opus frame.
225 * The passed frame_size must an opus frame size for the encoder's sampling rat e.
226 * For example, at 48kHz the permitted values are 120, 240, 480, 960, 1920, and 2880.
227 * Passing in a duration of less than 10ms (480 samples at 48kHz) will
228 * prevent the encoder from using the LPC or hybrid modes.
229 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state 236 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
230 * @param [in] pcm <tt>opus_int16*</tt>: Input signal (interleaved if 2 channel s). length is frame_size*channels*sizeof(opus_int16) 237 * @param [in] pcm <tt>opus_int16*</tt>: Input signal (interleaved if 2 channel s). length is frame_size*channels*sizeof(opus_int16)
231 * @param [in] frame_size <tt>int</tt>: Number of samples per frame of input si gnal 238 * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
232 * @param [out] data <tt>char*</tt>: Output payload (at least max_data_bytes lo ng) 239 * input signal.
233 * @param [in] max_data_bytes <tt>opus_int32</tt>: Allocated memory for payload ; don't use for controlling bitrate 240 * This must be an Opus frame size for
234 * @returns length of the data payload (in bytes) or @ref opus_errorcodes 241 * the encoder's sampling rate.
242 * For example, at 48 kHz the permitted
243 * values are 120, 240, 480, 960, 1920,
244 * and 2880.
245 * Passing in a duration of less than
246 * 10 ms (480 samples at 48 kHz) will
247 * prevent the encoder from using the LPC
248 * or hybrid modes.
249 * @param [out] data <tt>unsigned char*</tt>: Output payload.
250 * This must contain storage for at
251 * least \a max_data_bytes.
252 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
253 * memory for the output
254 * payload. This may be
255 * used to impose an upper limi t on
256 * the instant bitrate, but sho uld
257 * not be used as the only bitr ate
258 * control. Use #OPUS_SET_BITRA TE to
259 * control the bitrate.
260 * @returns The length of the encoded packet (in bytes) on success or a
261 * negative error code (see @ref opus_errorcodes) on failure.
235 */ 262 */
236 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode( 263 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode(
237 OpusEncoder *st, 264 OpusEncoder *st,
238 const opus_int16 *pcm, 265 const opus_int16 *pcm,
239 int frame_size, 266 int frame_size,
240 unsigned char *data, 267 unsigned char *data,
241 opus_int32 max_data_bytes 268 opus_int32 max_data_bytes
242 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4); 269 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
243 270
244 /** Encodes an Opus frame from floating point input. 271 /** Encodes an Opus frame from floating point input.
245 * The passed frame_size must an opus frame size for the encoder's sampling rat e.
246 * For example, at 48kHz the permitted values are 120, 240, 480, 960, 1920, and 2880.
247 * Passing in a duration of less than 10ms (480 samples at 48kHz) will
248 * prevent the encoder from using the LPC or hybrid modes.
249 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state 272 * @param [in] st <tt>OpusEncoder*</tt>: Encoder state
250 * @param [in] pcm <tt>float*</tt>: Input in float format (interleaved if 2 cha nnels), with a normal range of +/-1.0. 273 * @param [in] pcm <tt>float*</tt>: Input in float format (interleaved if 2 cha nnels), with a normal range of +/-1.0.
251 * Samples with a range beyond +/-1.0 are supported but will 274 * Samples with a range beyond +/-1.0 are supported but will
252 * be clipped by decoders using the integer API and should 275 * be clipped by decoders using the integer API and should
253 * only be used if it is known that the far end supports 276 * only be used if it is known that the far end supports
254 * extended dynamic range. 277 * extended dynamic range.
255 * length is frame_size*channels*sizeof(float) 278 * length is frame_size*channels*sizeof(float)
256 * @param [in] frame_size <tt>int</tt>: Number of samples per frame of input si gnal 279 * @param [in] frame_size <tt>int</tt>: Number of samples per channel in the
257 * @param [out] data <tt>char*</tt>: Output payload (at least max_data_bytes lo ng) 280 * input signal.
258 * @param [in] max_data_bytes <tt>opus_int32</tt>: Allocated memory for payload ; don't use for controlling bitrate 281 * This must be an Opus frame size for
259 * @returns length of the data payload (in bytes) or @ref opus_errorcodes 282 * the encoder's sampling rate.
283 * For example, at 48 kHz the permitted
284 * values are 120, 240, 480, 960, 1920,
285 * and 2880.
286 * Passing in a duration of less than
287 * 10 ms (480 samples at 48 kHz) will
288 * prevent the encoder from using the LPC
289 * or hybrid modes.
290 * @param [out] data <tt>unsigned char*</tt>: Output payload.
291 * This must contain storage for at
292 * least \a max_data_bytes.
293 * @param [in] max_data_bytes <tt>opus_int32</tt>: Size of the allocated
294 * memory for the output
295 * payload. This may be
296 * used to impose an upper limi t on
297 * the instant bitrate, but sho uld
298 * not be used as the only bitr ate
299 * control. Use #OPUS_SET_BITRA TE to
300 * control the bitrate.
301 * @returns The length of the encoded packet (in bytes) on success or a
302 * negative error code (see @ref opus_errorcodes) on failure.
260 */ 303 */
261 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode_float( 304 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_encode_float(
262 OpusEncoder *st, 305 OpusEncoder *st,
263 const float *pcm, 306 const float *pcm,
264 int frame_size, 307 int frame_size,
265 unsigned char *data, 308 unsigned char *data,
266 opus_int32 max_data_bytes 309 opus_int32 max_data_bytes
267 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4); 310 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) OPUS_ARG_NONNULL(4);
268 311
269 /** Frees an OpusEncoder allocated by opus_encoder_create. 312 /** Frees an <code>OpusEncoder</code> allocated by opus_encoder_create().
270 * @param[in] st <tt>OpusEncoder*</tt>: State to be freed. 313 * @param[in] st <tt>OpusEncoder*</tt>: State to be freed.
271 */ 314 */
272 OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st); 315 OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st);
273 316
274 /** Perform a CTL function on an Opus encoder. 317 /** Perform a CTL function on an Opus encoder.
275 * 318 *
276 * Generally the request and subsequent arguments are generated 319 * Generally the request and subsequent arguments are generated
277 * by a convenience macro. 320 * by a convenience macro.
321 * @param st <tt>OpusEncoder*</tt>: Encoder state.
322 * @param request This and all remaining parameters should be replaced by one
323 * of the convenience macros in @ref opus_genericctls or
324 * @ref opus_encoderctls.
325 * @see opus_genericctls
278 * @see opus_encoderctls 326 * @see opus_encoderctls
279 */ 327 */
280 OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...) OPUS_ARG_NON NULL(1); 328 OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...) OPUS_ARG_NON NULL(1);
281 /**@}*/ 329 /**@}*/
282 330
283 /** @defgroup opus_decoder Opus Decoder 331 /** @defgroup opus_decoder Opus Decoder
284 * @{ 332 * @{
285 * 333 *
286 * @brief This page describes the process and functions used to decode Opus. 334 * @brief This page describes the process and functions used to decode Opus.
287 * 335 *
288 * The decoding process also starts with creating a decoder 336 * The decoding process also starts with creating a decoder
289 * state. This can be done with: 337 * state. This can be done with:
290 * @code 338 * @code
291 * int error; 339 * int error;
292 * OpusDecoder *dec; 340 * OpusDecoder *dec;
293 * dec = opus_decoder_create(Fs, channels, &error); 341 * dec = opus_decoder_create(Fs, channels, &error);
294 * @endcode 342 * @endcode
295 * where 343 * where
296 * @li Fs is the sampling rate and must be 8000, 12000, 16000, 24000, or 48000 344 * @li Fs is the sampling rate and must be 8000, 12000, 16000, 24000, or 48000
297 * @li channels is the number of channels (1 or 2) 345 * @li channels is the number of channels (1 or 2)
298 * @li error will hold the error code in case or failure (or OPUS_OK on success ) 346 * @li error will hold the error code in case of failure (or #OPUS_OK on succes s)
299 * @li the return value is a newly created decoder state to be used for decodin g 347 * @li the return value is a newly created decoder state to be used for decodin g
300 * 348 *
301 * While opus_decoder_create() allocates memory for the state, it's also possib le 349 * While opus_decoder_create() allocates memory for the state, it's also possib le
302 * to initialize pre-allocated memory: 350 * to initialize pre-allocated memory:
303 * @code 351 * @code
304 * int size; 352 * int size;
305 * int error; 353 * int error;
306 * OpusDecoder *dec; 354 * OpusDecoder *dec;
307 * size = opus_decoder_get_size(channels); 355 * size = opus_decoder_get_size(channels);
308 * dec = malloc(size); 356 * dec = malloc(size);
(...skipping 10 matching lines...) Expand all
319 * frame_size = opus_decode(dec, packet, len, decoded, max_size, 0); 367 * frame_size = opus_decode(dec, packet, len, decoded, max_size, 0);
320 * @endcode 368 * @endcode
321 * where 369 * where
322 * 370 *
323 * @li packet is the byte array containing the compressed data 371 * @li packet is the byte array containing the compressed data
324 * @li len is the exact number of bytes contained in the packet 372 * @li len is the exact number of bytes contained in the packet
325 * @li decoded is the decoded audio data in opus_int16 (or float for opus_decod e_float()) 373 * @li decoded is the decoded audio data in opus_int16 (or float for opus_decod e_float())
326 * @li max_size is the max duration of the frame in samples (per channel) that can fit into the decoded_frame array 374 * @li max_size is the max duration of the frame in samples (per channel) that can fit into the decoded_frame array
327 * 375 *
328 * opus_decode() and opus_decode_float() return the number of samples (per chan nel) decoded from the packet. 376 * opus_decode() and opus_decode_float() return the number of samples (per chan nel) decoded from the packet.
329 * If that value is negative, then an error has occured. This can occur if the packet is corrupted or if the audio 377 * If that value is negative, then an error has occurred. This can occur if the packet is corrupted or if the audio
330 * buffer is too small to hold the decoded audio. 378 * buffer is too small to hold the decoded audio.
331 * 379 *
332 * Opus is a stateful codec with overlapping blocks and as a result Opus 380 * Opus is a stateful codec with overlapping blocks and as a result Opus
333 * packets are not coded independently of each other. Packets must be 381 * packets are not coded independently of each other. Packets must be
334 * passed into the decoder serially and in the correct order for a correct 382 * passed into the decoder serially and in the correct order for a correct
335 * decode. Lost packets can be replaced with loss concealment by calling 383 * decode. Lost packets can be replaced with loss concealment by calling
336 * the decoder with a null pointer and zero length for the missing packet. 384 * the decoder with a null pointer and zero length for the missing packet.
337 * 385 *
338 * A single codec state may only be accessed from a single thread at 386 * A single codec state may only be accessed from a single thread at
339 * a time and any required locking must be performed by the caller. Separate 387 * a time and any required locking must be performed by the caller. Separate
340 * streams must be decoded with separate decoder states and can be decoded 388 * streams must be decoded with separate decoder states and can be decoded
341 * in parallel unless the library was compiled with NONTHREADSAFE_PSEUDOSTACK 389 * in parallel unless the library was compiled with NONTHREADSAFE_PSEUDOSTACK
342 * defined. 390 * defined.
343 * 391 *
344 */ 392 */
345 393
346 /** Opus decoder state. 394 /** Opus decoder state.
347 * This contains the complete state of an Opus decoder. 395 * This contains the complete state of an Opus decoder.
348 * It is position independent and can be freely copied. 396 * It is position independent and can be freely copied.
349 * @see opus_decoder_create,opus_decoder_init 397 * @see opus_decoder_create,opus_decoder_init
350 */ 398 */
351 typedef struct OpusDecoder OpusDecoder; 399 typedef struct OpusDecoder OpusDecoder;
352 400
353 /** Gets the size of an OpusDecoder structure. 401 /** Gets the size of an <code>OpusDecoder</code> structure.
354 * @param [in] channels <tt>int</tt>: Number of channels 402 * @param [in] channels <tt>int</tt>: Number of channels.
355 * @returns size 403 * This must be 1 or 2.
404 * @returns The size in bytes.
356 */ 405 */
357 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_size(int channels); 406 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_size(int channels);
358 407
359 /** Allocates and initializes a decoder state. 408 /** Allocates and initializes a decoder state.
360 * @param [in] Fs <tt>opus_int32</tt>: Sample rate to decode at (Hz) 409 * @param [in] Fs <tt>opus_int32</tt>: Sample rate to decode at (Hz).
361 * @param [in] channels <tt>int</tt>: Number of channels (1/2) to decode 410 * This must be one of 8000, 12000, 16000,
362 * @param [out] error <tt>int*</tt>: OPUS_OK Success or @ref opus_errorcodes 411 * 24000, or 48000.
412 * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode
413 * @param [out] error <tt>int*</tt>: #OPUS_OK Success or @ref opus_errorcodes
363 * 414 *
364 * Internally Opus stores data at 48000 Hz, so that should be the default 415 * Internally Opus stores data at 48000 Hz, so that should be the default
365 * value for Fs. However, the decoder can efficiently decode to buffers 416 * value for Fs. However, the decoder can efficiently decode to buffers
366 * at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use 417 * at 8, 12, 16, and 24 kHz so if for some reason the caller cannot use
367 * data at the full sample rate, or knows the compressed data doesn't 418 * data at the full sample rate, or knows the compressed data doesn't
368 * use the full frequency range, it can request decoding at a reduced 419 * use the full frequency range, it can request decoding at a reduced
369 * rate. Likewise, the decoder is capable of filling in either mono or 420 * rate. Likewise, the decoder is capable of filling in either mono or
370 * interleaved stereo pcm buffers, at the caller's request. 421 * interleaved stereo pcm buffers, at the caller's request.
371 */ 422 */
372 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusDecoder *opus_decoder_create( 423 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusDecoder *opus_decoder_create(
373 opus_int32 Fs, 424 opus_int32 Fs,
374 int channels, 425 int channels,
375 int *error 426 int *error
376 ); 427 );
377 428
378 /** Initializes a previously allocated decoder state. 429 /** Initializes a previously allocated decoder state.
379 * The state must be the size returned by opus_decoder_get_size. 430 * The state must be at least the size returned by opus_decoder_get_size().
380 * This is intended for applications which use their own allocator instead of m alloc. @see opus_decoder_create,opus_decoder_get_size 431 * This is intended for applications which use their own allocator instead of m alloc. @see opus_decoder_create,opus_decoder_get_size
381 * To reset a previously initialized state use the OPUS_RESET_STATE CTL. 432 * To reset a previously initialized state, use the #OPUS_RESET_STATE CTL.
382 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state. 433 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state.
383 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate to decode to (Hz) 434 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate to decode to (Hz).
384 * @param [in] channels <tt>int</tt>: Number of channels (1/2) to decode 435 * This must be one of 8000, 12000, 16000,
385 * @retval OPUS_OK Success or @ref opus_errorcodes 436 * 24000, or 48000.
437 * @param [in] channels <tt>int</tt>: Number of channels (1 or 2) to decode
438 * @retval #OPUS_OK Success or @ref opus_errorcodes
386 */ 439 */
387 OPUS_EXPORT int opus_decoder_init( 440 OPUS_EXPORT int opus_decoder_init(
388 OpusDecoder *st, 441 OpusDecoder *st,
389 opus_int32 Fs, 442 opus_int32 Fs,
390 int channels 443 int channels
391 ) OPUS_ARG_NONNULL(1); 444 ) OPUS_ARG_NONNULL(1);
392 445
393 /** Decode an Opus frame 446 /** Decode an Opus packet.
394 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state 447 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
395 * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indica te packet loss 448 * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indica te packet loss
396 * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload* 449 * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload*
397 * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2 chann els). length 450 * @param [out] pcm <tt>opus_int16*</tt>: Output signal (interleaved if 2 chann els). length
398 * is frame_size*channels*sizeof(opus_int16) 451 * is frame_size*channels*sizeof(opus_int16)
399 * @param [in] frame_size Number of samples per channel of available space in * pcm, 452 * @param [in] frame_size Number of samples per channel of available space in \ a pcm.
400 * if less than the maximum frame size (120ms) some frames can not be decoded 453 * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), t his function will
401 * @param [in] decode_fec <tt>int</tt>: Flag (0/1) to request that any in-band forward error correction data be 454 * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1),
402 * decoded. If no such data is available the frame is decoded as if it were lo st. 455 * then frame_size needs to be exactly the duration of audio that is missing, otherwise the
456 * decoder will not be in the optimal state to decode the next incoming packet . For the PLC and
457 * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
458 * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-ba nd forward error correction data be
459 * decoded. If no such data is available, the frame is decoded as if it were l ost.
403 * @returns Number of decoded samples or @ref opus_errorcodes 460 * @returns Number of decoded samples or @ref opus_errorcodes
404 */ 461 */
405 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode( 462 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode(
406 OpusDecoder *st, 463 OpusDecoder *st,
407 const unsigned char *data, 464 const unsigned char *data,
408 opus_int32 len, 465 opus_int32 len,
409 opus_int16 *pcm, 466 opus_int16 *pcm,
410 int frame_size, 467 int frame_size,
411 int decode_fec 468 int decode_fec
412 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); 469 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
413 470
414 /** Decode an opus frame with floating point output 471 /** Decode an Opus packet with floating point output.
415 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state 472 * @param [in] st <tt>OpusDecoder*</tt>: Decoder state
416 * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indica te packet loss 473 * @param [in] data <tt>char*</tt>: Input payload. Use a NULL pointer to indica te packet loss
417 * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload 474 * @param [in] len <tt>opus_int32</tt>: Number of bytes in payload
418 * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels). length 475 * @param [out] pcm <tt>float*</tt>: Output signal (interleaved if 2 channels). length
419 * is frame_size*channels*sizeof(float) 476 * is frame_size*channels*sizeof(float)
420 * @param [in] frame_size Number of samples per channel of available space in * pcm, 477 * @param [in] frame_size Number of samples per channel of available space in \ a pcm.
421 * if less than the maximum frame size (120ms) some frames can not be decoded 478 * If this is less than the maximum packet duration (120ms; 5760 for 48kHz), t his function will
422 * @param [in] decode_fec <tt>int</tt>: Flag (0/1) to request that any in-band forward error correction data be 479 * not be capable of decoding some packets. In the case of PLC (data==NULL) or FEC (decode_fec=1),
480 * then frame_size needs to be exactly the duration of audio that is missing, otherwise the
481 * decoder will not be in the optimal state to decode the next incoming packet . For the PLC and
482 * FEC cases, frame_size <b>must</b> be a multiple of 2.5 ms.
483 * @param [in] decode_fec <tt>int</tt>: Flag (0 or 1) to request that any in-ba nd forward error correction data be
423 * decoded. If no such data is available the frame is decoded as if it were lo st. 484 * decoded. If no such data is available the frame is decoded as if it were lo st.
424 * @returns Number of decoded samples or @ref opus_errorcodes 485 * @returns Number of decoded samples or @ref opus_errorcodes
425 */ 486 */
426 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode_float( 487 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decode_float(
427 OpusDecoder *st, 488 OpusDecoder *st,
428 const unsigned char *data, 489 const unsigned char *data,
429 opus_int32 len, 490 opus_int32 len,
430 float *pcm, 491 float *pcm,
431 int frame_size, 492 int frame_size,
432 int decode_fec 493 int decode_fec
433 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); 494 ) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
434 495
435 /** Perform a CTL function on an Opus decoder. 496 /** Perform a CTL function on an Opus decoder.
436 * 497 *
437 * Generally the request and subsequent arguments are generated 498 * Generally the request and subsequent arguments are generated
438 * by a convenience macro. 499 * by a convenience macro.
500 * @param st <tt>OpusDecoder*</tt>: Decoder state.
501 * @param request This and all remaining parameters should be replaced by one
502 * of the convenience macros in @ref opus_genericctls or
503 * @ref opus_decoderctls.
439 * @see opus_genericctls 504 * @see opus_genericctls
505 * @see opus_decoderctls
440 */ 506 */
441 OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...) OPUS_ARG_NON NULL(1); 507 OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...) OPUS_ARG_NON NULL(1);
442 508
443 /** Frees an OpusDecoder allocated by opus_decoder_create. 509 /** Frees an <code>OpusDecoder</code> allocated by opus_decoder_create().
444 * @param[in] st <tt>OpusDecoder*</tt>: State to be freed. 510 * @param[in] st <tt>OpusDecoder*</tt>: State to be freed.
445 */ 511 */
446 OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st); 512 OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st);
447 513
448 /** Parse an opus packet into one or more frames. 514 /** Parse an opus packet into one or more frames.
449 * Opus_decode will perform this operation internally so most applications do 515 * Opus_decode will perform this operation internally so most applications do
450 * not need to use this function. 516 * not need to use this function.
451 * This function does not copy the frames, the returned pointers are pointers i nto 517 * This function does not copy the frames, the returned pointers are pointers i nto
452 * the input packet. 518 * the input packet.
453 * @param [in] data <tt>char*</tt>: Opus packet to be parsed 519 * @param [in] data <tt>char*</tt>: Opus packet to be parsed
(...skipping 18 matching lines...) Expand all
472 * @retval OPUS_BANDWIDTH_NARROWBAND Narrowband (4kHz bandpass) 538 * @retval OPUS_BANDWIDTH_NARROWBAND Narrowband (4kHz bandpass)
473 * @retval OPUS_BANDWIDTH_MEDIUMBAND Mediumband (6kHz bandpass) 539 * @retval OPUS_BANDWIDTH_MEDIUMBAND Mediumband (6kHz bandpass)
474 * @retval OPUS_BANDWIDTH_WIDEBAND Wideband (8kHz bandpass) 540 * @retval OPUS_BANDWIDTH_WIDEBAND Wideband (8kHz bandpass)
475 * @retval OPUS_BANDWIDTH_SUPERWIDEBAND Superwideband (12kHz bandpass) 541 * @retval OPUS_BANDWIDTH_SUPERWIDEBAND Superwideband (12kHz bandpass)
476 * @retval OPUS_BANDWIDTH_FULLBAND Fullband (20kHz bandpass) 542 * @retval OPUS_BANDWIDTH_FULLBAND Fullband (20kHz bandpass)
477 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 543 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
478 */ 544 */
479 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_bandwidth(const unsigned char *data) OPUS_ARG_NONNULL(1); 545 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_bandwidth(const unsigned char *data) OPUS_ARG_NONNULL(1);
480 546
481 /** Gets the number of samples per frame from an Opus packet. 547 /** Gets the number of samples per frame from an Opus packet.
482 * @param [in] data <tt>char*</tt>: Opus packet 548 * @param [in] data <tt>char*</tt>: Opus packet.
483 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz 549 * This must contain at least one byte of
484 * @returns Number of samples per frame 550 * data.
485 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 551 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
552 * This must be a multiple of 400, or
553 * inaccurate results will be returned.
554 * @returns Number of samples per frame.
486 */ 555 */
487 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_samples_per_frame(const unsigned char *data, opus_int32 Fs) OPUS_ARG_NONNULL(1); 556 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_samples_per_frame(const unsigned char *data, opus_int32 Fs) OPUS_ARG_NONNULL(1);
488 557
489 /** Gets the number of channels from an Opus packet. 558 /** Gets the number of channels from an Opus packet.
490 * @param [in] data <tt>char*</tt>: Opus packet 559 * @param [in] data <tt>char*</tt>: Opus packet
491 * @returns Number of channels 560 * @returns Number of channels
492 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 561 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
493 */ 562 */
494 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_channels(const unsign ed char *data) OPUS_ARG_NONNULL(1); 563 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_channels(const unsign ed char *data) OPUS_ARG_NONNULL(1);
495 564
496 /** Gets the number of frames in an Opus packet. 565 /** Gets the number of frames in an Opus packet.
497 * @param [in] packet <tt>char*</tt>: Opus packet 566 * @param [in] packet <tt>char*</tt>: Opus packet
498 * @param [in] len <tt>opus_int32</tt>: Length of packet 567 * @param [in] len <tt>opus_int32</tt>: Length of packet
499 * @returns Number of frames 568 * @returns Number of frames
500 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 569 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
501 */ 570 */
502 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1); 571 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_frames(const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1);
503 572
504 /** Gets the number of samples of an Opus packet. 573 /** Gets the number of samples of an Opus packet.
574 * @param [in] packet <tt>char*</tt>: Opus packet
575 * @param [in] len <tt>opus_int32</tt>: Length of packet
576 * @param [in] Fs <tt>opus_int32</tt>: Sampling rate in Hz.
577 * This must be a multiple of 400, or
578 * inaccurate results will be returned.
579 * @returns Number of samples
580 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
581 */
582 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_get_nb_samples(const unsigne d char packet[], opus_int32 len, opus_int32 Fs) OPUS_ARG_NONNULL(1);
583
584 /** Gets the number of samples of an Opus packet.
505 * @param [in] dec <tt>OpusDecoder*</tt>: Decoder state 585 * @param [in] dec <tt>OpusDecoder*</tt>: Decoder state
506 * @param [in] packet <tt>char*</tt>: Opus packet 586 * @param [in] packet <tt>char*</tt>: Opus packet
507 * @param [in] len <tt>opus_int32</tt>: Length of packet 587 * @param [in] len <tt>opus_int32</tt>: Length of packet
508 * @returns Number of samples 588 * @returns Number of samples
509 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type 589 * @retval OPUS_INVALID_PACKET The compressed data passed is corrupted or of an unsupported type
510 */ 590 */
511 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_nb_samples(const OpusDe coder *dec, const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1) OP US_ARG_NONNULL(2); 591 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_decoder_get_nb_samples(const OpusDe coder *dec, const unsigned char packet[], opus_int32 len) OPUS_ARG_NONNULL(1) OP US_ARG_NONNULL(2);
512 /**@}*/ 592 /**@}*/
513 593
514 /** @defgroup opus_repacketizer Repacketizer 594 /** @defgroup opus_repacketizer Repacketizer
515 * @{ 595 * @{
516 * 596 *
517 * The repacketizer can be used to merge multiple Opus packets into a single pa cket 597 * The repacketizer can be used to merge multiple Opus packets into a single
518 * or alternatively to split Opus packets that have previously been merged. 598 * packet or alternatively to split Opus packets that have previously been
519 * 599 * merged. Splitting valid Opus packets is always guaranteed to succeed,
600 * whereas merging valid packets only succeeds if all frames have the same
601 * mode, bandwidth, and frame size, and when the total duration of the merged
602 * packet is no more than 120 ms.
603 * The repacketizer currently only operates on elementary Opus
604 * streams. It will not manipualte multistream packets successfully, except in
605 * the degenerate case where they consist of data from a single stream.
606 *
607 * The repacketizing process starts with creating a repacketizer state, either
608 * by calling opus_repacketizer_create() or by allocating the memory yourself,
609 * e.g.,
610 * @code
611 * OpusRepacketizer *rp;
612 * rp = (OpusRepacketizer*)malloc(opus_repacketizer_get_size());
613 * if (rp != NULL)
614 * opus_repacketizer_init(rp);
615 * @endcode
616 *
617 * Then the application should submit packets with opus_repacketizer_cat(),
618 * extract new packets with opus_repacketizer_out() or
619 * opus_repacketizer_out_range(), and then reset the state for the next set of
620 * input packets via opus_repacketizer_init().
621 *
622 * For example, to split a sequence of packets into individual frames:
623 * @code
624 * unsigned char *data;
625 * int len;
626 * while (get_next_packet(&data, &len))
627 * {
628 * unsigned char out[1276];
629 * opus_int32 out_len;
630 * int nb_frames;
631 * int err;
632 * int i;
633 * err = opus_repacketizer_cat(rp, data, len);
634 * if (err != OPUS_OK)
635 * {
636 * release_packet(data);
637 * return err;
638 * }
639 * nb_frames = opus_repacketizer_get_nb_frames(rp);
640 * for (i = 0; i < nb_frames; i++)
641 * {
642 * out_len = opus_repacketizer_out_range(rp, i, i+1, out, sizeof(out));
643 * if (out_len < 0)
644 * {
645 * release_packet(data);
646 * return (int)out_len;
647 * }
648 * output_next_packet(out, out_len);
649 * }
650 * opus_repacketizer_init(rp);
651 * release_packet(data);
652 * }
653 * @endcode
654 *
655 * Alternatively, to combine a sequence of frames into packets that each
656 * contain up to <code>TARGET_DURATION_MS</code> milliseconds of data:
657 * @code
658 * // The maximum number of packets with duration TARGET_DURATION_MS occurs
659 * // when the frame size is 2.5 ms, for a total of (TARGET_DURATION_MS*2/5)
660 * // packets.
661 * unsigned char *data[(TARGET_DURATION_MS*2/5)+1];
662 * opus_int32 len[(TARGET_DURATION_MS*2/5)+1];
663 * int nb_packets;
664 * unsigned char out[1277*(TARGET_DURATION_MS*2/2)];
665 * opus_int32 out_len;
666 * int prev_toc;
667 * nb_packets = 0;
668 * while (get_next_packet(data+nb_packets, len+nb_packets))
669 * {
670 * int nb_frames;
671 * int err;
672 * nb_frames = opus_packet_get_nb_frames(data[nb_packets], len[nb_packets]);
673 * if (nb_frames < 1)
674 * {
675 * release_packets(data, nb_packets+1);
676 * return nb_frames;
677 * }
678 * nb_frames += opus_repacketizer_get_nb_frames(rp);
679 * // If adding the next packet would exceed our target, or it has an
680 * // incompatible TOC sequence, output the packets we already have before
681 * // submitting it.
682 * // N.B., The nb_packets > 0 check ensures we've submitted at least one
683 * // packet since the last call to opus_repacketizer_init(). Otherwise a
684 * // single packet longer than TARGET_DURATION_MS would cause us to try to
685 * // output an (invalid) empty packet. It also ensures that prev_toc has
686 * // been set to a valid value. Additionally, len[nb_packets] > 0 is
687 * // guaranteed by the call to opus_packet_get_nb_frames() above, so the
688 * // reference to data[nb_packets][0] should be valid.
689 * if (nb_packets > 0 && (
690 * ((prev_toc & 0xFC) != (data[nb_packets][0] & 0xFC)) ||
691 * opus_packet_get_samples_per_frame(data[nb_packets], 48000)*nb_frames >
692 * TARGET_DURATION_MS*48))
693 * {
694 * out_len = opus_repacketizer_out(rp, out, sizeof(out));
695 * if (out_len < 0)
696 * {
697 * release_packets(data, nb_packets+1);
698 * return (int)out_len;
699 * }
700 * output_next_packet(out, out_len);
701 * opus_repacketizer_init(rp);
702 * release_packets(data, nb_packets);
703 * data[0] = data[nb_packets];
704 * len[0] = len[nb_packets];
705 * nb_packets = 0;
706 * }
707 * err = opus_repacketizer_cat(rp, data[nb_packets], len[nb_packets]);
708 * if (err != OPUS_OK)
709 * {
710 * release_packets(data, nb_packets+1);
711 * return err;
712 * }
713 * prev_toc = data[nb_packets][0];
714 * nb_packets++;
715 * }
716 * // Output the final, partial packet.
717 * if (nb_packets > 0)
718 * {
719 * out_len = opus_repacketizer_out(rp, out, sizeof(out));
720 * release_packets(data, nb_packets);
721 * if (out_len < 0)
722 * return (int)out_len;
723 * output_next_packet(out, out_len);
724 * }
725 * @endcode
726 *
727 * An alternate way of merging packets is to simply call opus_repacketizer_cat( )
728 * unconditionally until it fails. At that point, the merged packet can be
729 * obtained with opus_repacketizer_out() and the input packet for which
730 * opus_repacketizer_cat() needs to be re-added to a newly reinitialized
731 * repacketizer state.
520 */ 732 */
521 733
522 typedef struct OpusRepacketizer OpusRepacketizer; 734 typedef struct OpusRepacketizer OpusRepacketizer;
523 735
736 /** Gets the size of an <code>OpusRepacketizer</code> structure.
737 * @returns The size in bytes.
738 */
524 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_size(void); 739 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_size(void);
525 740
741 /** (Re)initializes a previously allocated repacketizer state.
742 * The state must be at least the size returned by opus_repacketizer_get_size() .
743 * This can be used for applications which use their own allocator instead of
744 * malloc().
745 * It must also be called to reset the queue of packets waiting to be
746 * repacketized, which is necessary if the maximum packet duration of 120 ms
747 * is reached or if you wish to submit packets with a different Opus
748 * configuration (coding mode, audio bandwidth, frame size, or channel count).
749 * Failure to do so will prevent a new packet from being added with
750 * opus_repacketizer_cat().
751 * @see opus_repacketizer_create
752 * @see opus_repacketizer_get_size
753 * @see opus_repacketizer_cat
754 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to
755 * (re)initialize.
756 * @returns A pointer to the same repacketizer state that was passed in.
757 */
526 OPUS_EXPORT OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp) OPUS_ ARG_NONNULL(1); 758 OPUS_EXPORT OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp) OPUS_ ARG_NONNULL(1);
527 759
760 /** Allocates memory and initializes the new repacketizer with
761 * opus_repacketizer_init().
762 */
528 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusRepacketizer *opus_repacketizer_create(v oid); 763 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusRepacketizer *opus_repacketizer_create(v oid);
529 764
765 /** Frees an <code>OpusRepacketizer</code> allocated by
766 * opus_repacketizer_create().
767 * @param[in] rp <tt>OpusRepacketizer*</tt>: State to be freed.
768 */
530 OPUS_EXPORT void opus_repacketizer_destroy(OpusRepacketizer *rp); 769 OPUS_EXPORT void opus_repacketizer_destroy(OpusRepacketizer *rp);
531 770
771 /** Add a packet to the current repacketizer state.
772 * This packet must match the configuration of any packets already submitted
773 * for repacketization since the last call to opus_repacketizer_init().
774 * This means that it must have the same coding mode, audio bandwidth, frame
775 * size, and channel count.
776 * This can be checked in advance by examining the top 6 bits of the first
777 * byte of the packet, and ensuring they match the top 6 bits of the first
778 * byte of any previously submitted packet.
779 * The total duration of audio in the repacketizer state also must not exceed
780 * 120 ms, the maximum duration of a single packet, after adding this packet.
781 *
782 * The contents of the current repacketizer state can be extracted into new
783 * packets using opus_repacketizer_out() or opus_repacketizer_out_range().
784 *
785 * In order to add a packet with a different configuration or to add more
786 * audio beyond 120 ms, you must clear the repacketizer state by calling
787 * opus_repacketizer_init().
788 * If a packet is too large to add to the current repacketizer state, no part
789 * of it is added, even if it contains multiple frames, some of which might
790 * fit.
791 * If you wish to be able to add parts of such packets, you should first use
792 * another repacketizer to split the packet into pieces and add them
793 * individually.
794 * @see opus_repacketizer_out_range
795 * @see opus_repacketizer_out
796 * @see opus_repacketizer_init
797 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state to which to
798 * add the packet.
799 * @param[in] data <tt>const unsigned char*</tt>: The packet data.
800 * The application must ensure
801 * this pointer remains valid
802 * until the next call to
803 * opus_repacketizer_init() or
804 * opus_repacketizer_destroy().
805 * @param len <tt>opus_int32</tt>: The number of bytes in the packet data.
806 * @returns An error code indicating whether or not the operation succeeded.
807 * @retval #OPUS_OK The packet's contents have been added to the repacketizer
808 * state.
809 * @retval #OPUS_INVALID_PACKET The packet did not have a valid TOC sequence,
810 * the packet's TOC sequence was not compatible
811 * with previously submitted packets (because
812 * the coding mode, audio bandwidth, frame size,
813 * or channel count did not match), or adding
814 * this packet would increase the total amount of
815 * audio stored in the repacketizer state to more
816 * than 120 ms.
817 */
532 OPUS_EXPORT int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2); 818 OPUS_EXPORT int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, opus_int32 len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2);
533 819
820
821 /** Construct a new packet from data previously submitted to the repacketizer
822 * state via opus_repacketizer_cat().
823 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to
824 * construct the new packet.
825 * @param begin <tt>int</tt>: The index of the first frame in the current
826 * repacketizer state to include in the output.
827 * @param end <tt>int</tt>: One past the index of the last frame in the
828 * current repacketizer state to include in the
829 * output.
830 * @param[out] data <tt>const unsigned char*</tt>: The buffer in which to
831 * store the output packet.
832 * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in
833 * the output buffer. In order to guarantee
834 * success, this should be at least
835 * <code>1276</code> for a single frame,
836 * or for multiple frames,
837 * <code>1277*(end-begin)</code>.
838 * However, <code>1*(end-begin)</code> plus
839 * the size of all packet data submitted to
840 * the repacketizer since the last call to
841 * opus_repacketizer_init() or
842 * opus_repacketizer_create() is also
843 * sufficient, and possibly much smaller.
844 * @returns The total size of the output packet on success, or an error code
845 * on failure.
846 * @retval #OPUS_BAD_ARG <code>[begin,end)</code> was an invalid range of
847 * frames (begin < 0, begin >= end, or end >
848 * opus_repacketizer_get_nb_frames()).
849 * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the
850 * complete output packet.
851 */
534 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out_range(OpusR epacketizer *rp, int begin, int end, unsigned char *data, opus_int32 maxlen) OPU S_ARG_NONNULL(1) OPUS_ARG_NONNULL(4); 852 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out_range(OpusR epacketizer *rp, int begin, int end, unsigned char *data, opus_int32 maxlen) OPU S_ARG_NONNULL(1) OPUS_ARG_NONNULL(4);
535 853
854 /** Return the total number of frames contained in packet data submitted to
855 * the repacketizer state so far via opus_repacketizer_cat() since the last
856 * call to opus_repacketizer_init() or opus_repacketizer_create().
857 * This defines the valid range of packets that can be extracted with
858 * opus_repacketizer_out_range() or opus_repacketizer_out().
859 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state containing the
860 * frames.
861 * @returns The total number of frames contained in the packet data submitted
862 * to the repacketizer state.
863 */
536 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_nb_frames(OpusRepa cketizer *rp) OPUS_ARG_NONNULL(1); 864 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_nb_frames(OpusRepa cketizer *rp) OPUS_ARG_NONNULL(1);
537 865
866 /** Construct a new packet from data previously submitted to the repacketizer
867 * state via opus_repacketizer_cat().
868 * This is a convenience routine that returns all the data submitted so far
869 * in a single packet.
870 * It is equivalent to calling
871 * @code
872 * opus_repacketizer_out_range(rp, 0, opus_repacketizer_get_nb_frames(rp),
873 * data, maxlen)
874 * @endcode
875 * @param rp <tt>OpusRepacketizer*</tt>: The repacketizer state from which to
876 * construct the new packet.
877 * @param[out] data <tt>const unsigned char*</tt>: The buffer in which to
878 * store the output packet.
879 * @param maxlen <tt>opus_int32</tt>: The maximum number of bytes to store in
880 * the output buffer. In order to guarantee
881 * success, this should be at least
882 * <code>1277*opus_repacketizer_get_nb_frame s(rp)</code>.
883 * However,
884 * <code>1*opus_repacketizer_get_nb_frames(r p)</code>
885 * plus the size of all packet data
886 * submitted to the repacketizer since the
887 * last call to opus_repacketizer_init() or
888 * opus_repacketizer_create() is also
889 * sufficient, and possibly much smaller.
890 * @returns The total size of the output packet on success, or an error code
891 * on failure.
892 * @retval #OPUS_BUFFER_TOO_SMALL \a maxlen was insufficient to contain the
893 * complete output packet.
894 */
538 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out(OpusRepacke tizer *rp, unsigned char *data, opus_int32 maxlen) OPUS_ARG_NONNULL(1); 895 OPUS_EXPORT OPUS_WARN_UNUSED_RESULT opus_int32 opus_repacketizer_out(OpusRepacke tizer *rp, unsigned char *data, opus_int32 maxlen) OPUS_ARG_NONNULL(1);
539 896
540 /**@}*/ 897 /**@}*/
541 898
542 #ifdef __cplusplus 899 #ifdef __cplusplus
543 } 900 }
544 #endif 901 #endif
545 902
546 #endif /* OPUS_H */ 903 #endif /* OPUS_H */
OLDNEW
« no previous file with comments | « doc/Makefile.in ('k') | include/opus_defines.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698