2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_INTERFACE_ISAC_H_
12 #define WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_INTERFACE_ISAC_H_
15 * Define the fixed-point numeric formats
17 #include "webrtc/typedefs.h"
19 typedef struct WebRtcISACStruct ISACStruct;
21 #if defined(__cplusplus)
25 /******************************************************************************
26 * WebRtcIsac_AssignSize(...)
28 * This function returns the size of the ISAC instance, so that the instance
29 * can be created outside iSAC.
32 * - samplingRate : sampling rate of the input/output audio.
35 * - sizeinbytes : number of bytes needed to allocate for the
38 * Return value : 0 - Ok
42 int16_t WebRtcIsac_AssignSize(
46 /******************************************************************************
47 * WebRtcIsac_Assign(...)
49 * This function assignes the memory already created to the ISAC instance.
52 * - *ISAC_main_inst : a pointer to the coder instance.
53 * - samplingRate : sampling rate of the input/output audio.
54 * - ISAC_inst_Addr : the already allocated memory, where we put the
57 * Return value : 0 - Ok
61 int16_t WebRtcIsac_Assign(
62 ISACStruct** ISAC_main_inst,
63 void* ISAC_inst_Addr);
66 /******************************************************************************
67 * WebRtcIsac_Create(...)
69 * This function creates an ISAC instance, which will contain the state
70 * information for one coding/decoding channel.
73 * - *ISAC_main_inst : a pointer to the coder instance.
75 * Return value : 0 - Ok
79 int16_t WebRtcIsac_Create(
80 ISACStruct** ISAC_main_inst);
83 /******************************************************************************
84 * WebRtcIsac_Free(...)
86 * This function frees the ISAC instance created at the beginning.
89 * - ISAC_main_inst : an ISAC instance.
91 * Return value : 0 - Ok
95 int16_t WebRtcIsac_Free(
96 ISACStruct* ISAC_main_inst);
99 /******************************************************************************
100 * WebRtcIsac_EncoderInit(...)
102 * This function initializes an ISAC instance prior to the encoder calls.
105 * - ISAC_main_inst : ISAC instance.
106 * - CodingMode : 0 -> Bit rate and frame length are
107 * automatically adjusted to available bandwidth
108 * on transmission channel, just valid if codec
109 * is created to work in wideband mode.
110 * 1 -> User sets a frame length and a target bit
111 * rate which is taken as the maximum
112 * short-term average bit rate.
114 * Return value : 0 - Ok
118 int16_t WebRtcIsac_EncoderInit(
119 ISACStruct* ISAC_main_inst,
123 /******************************************************************************
124 * WebRtcIsac_Encode(...)
126 * This function encodes 10ms audio blocks and inserts it into a package.
127 * Input speech length has 160 samples if operating at 16 kHz sampling
128 * rate, or 320 if operating at 32 kHz sampling rate. The encoder buffers the
129 * input audio until the whole frame is buffered then proceeds with encoding.
133 * - ISAC_main_inst : ISAC instance.
134 * - speechIn : input speech vector.
137 * - encoded : the encoded data vector
140 * : >0 - Length (in bytes) of coded data
141 * : 0 - The buffer didn't reach the chosen
142 * frame-size so it keeps buffering speech
147 int16_t WebRtcIsac_Encode(
148 ISACStruct* ISAC_main_inst,
149 const int16_t* speechIn,
153 /******************************************************************************
154 * WebRtcIsac_DecoderInit(...)
156 * This function initializes an ISAC instance prior to the decoder calls.
159 * - ISAC_main_inst : ISAC instance.
166 int16_t WebRtcIsac_DecoderInit(
167 ISACStruct* ISAC_main_inst);
170 /******************************************************************************
171 * WebRtcIsac_UpdateBwEstimate(...)
173 * This function updates the estimate of the bandwidth.
176 * - ISAC_main_inst : ISAC instance.
177 * - encoded : encoded ISAC frame(s).
178 * - packet_size : size of the packet.
179 * - rtp_seq_number : the RTP number of the packet.
180 * - send_ts : the RTP send timestamp, given in samples
181 * - arr_ts : the arrival time of the packet (from NetEq)
184 * Return value : 0 - Ok
188 int16_t WebRtcIsac_UpdateBwEstimate(
189 ISACStruct* ISAC_main_inst,
190 const uint16_t* encoded,
192 uint16_t rtp_seq_number,
197 /******************************************************************************
198 * WebRtcIsac_Decode(...)
200 * This function decodes an ISAC frame. At 16 kHz sampling rate, the length
201 * of the output audio could be either 480 or 960 samples, equivalent to
202 * 30 or 60 ms respectively. At 32 kHz sampling rate, the length of the
203 * output audio is 960 samples, which is 30 ms.
206 * - ISAC_main_inst : ISAC instance.
207 * - encoded : encoded ISAC frame(s).
208 * - len : bytes in encoded vector.
211 * - decoded : The decoded vector.
213 * Return value : >0 - number of samples in decoded vector.
217 int16_t WebRtcIsac_Decode(
218 ISACStruct* ISAC_main_inst,
219 const uint16_t* encoded,
222 int16_t* speechType);
225 /******************************************************************************
226 * WebRtcIsac_DecodePlc(...)
228 * This function conducts PLC for ISAC frame(s). Output speech length
229 * will be a multiple of frames, i.e. multiples of 30 ms audio. Therefore,
230 * the output is multiple of 480 samples if operating at 16 kHz and multiple
231 * of 960 if operating at 32 kHz.
234 * - ISAC_main_inst : ISAC instance.
235 * - noOfLostFrames : Number of PLC frames to produce.
238 * - decoded : The decoded vector.
240 * Return value : >0 - number of samples in decoded PLC vector
244 int16_t WebRtcIsac_DecodePlc(
245 ISACStruct* ISAC_main_inst,
247 int16_t noOfLostFrames);
250 /******************************************************************************
251 * WebRtcIsac_Control(...)
253 * This function sets the limit on the short-term average bit-rate and the
254 * frame length. Should be used only in Instantaneous mode. At 16 kHz sampling
255 * rate, an average bit-rate between 10000 to 32000 bps is valid and a
256 * frame-size of 30 or 60 ms is acceptable. At 32 kHz, an average bit-rate
257 * between 10000 to 56000 is acceptable, and the valid frame-size is 30 ms.
260 * - ISAC_main_inst : ISAC instance.
261 * - rate : limit on the short-term average bit rate,
263 * - framesize : frame-size in millisecond.
265 * Return value : 0 - ok
269 int16_t WebRtcIsac_Control(
270 ISACStruct* ISAC_main_inst,
275 /******************************************************************************
276 * WebRtcIsac_ControlBwe(...)
278 * This function sets the initial values of bottleneck and frame-size if
279 * iSAC is used in channel-adaptive mode. Therefore, this API is not
280 * applicable if the codec is created to operate in super-wideband mode.
282 * Through this API, users can enforce a frame-size for all values of
283 * bottleneck. Then iSAC will not automatically change the frame-size.
287 * - ISAC_main_inst : ISAC instance.
288 * - rateBPS : initial value of bottleneck in bits/second
289 * 10000 <= rateBPS <= 56000 is accepted
290 * For default bottleneck set rateBPS = 0
291 * - frameSizeMs : number of milliseconds per frame (30 or 60)
292 * - enforceFrameSize : 1 to enforce the given frame-size through
293 * out the adaptation process, 0 to let iSAC
294 * change the frame-size if required.
296 * Return value : 0 - ok
300 int16_t WebRtcIsac_ControlBwe(
301 ISACStruct* ISAC_main_inst,
304 int16_t enforceFrameSize);
307 /******************************************************************************
308 * WebRtcIsac_ReadFrameLen(...)
310 * This function returns the length of the frame represented in the packet.
313 * - encoded : Encoded bit-stream
316 * - frameLength : Length of frame in packet (in samples)
320 int16_t WebRtcIsac_ReadFrameLen(
321 ISACStruct* ISAC_main_inst,
322 const int16_t* encoded,
323 int16_t* frameLength);
326 /******************************************************************************
327 * WebRtcIsac_version(...)
329 * This function returns the version number.
332 * - version : Pointer to character string
336 void WebRtcIsac_version(
340 /******************************************************************************
341 * WebRtcIsac_GetErrorCode(...)
343 * This function can be used to check the error code of an iSAC instance. When
344 * a function returns -1 a error code will be set for that instance. The
345 * function below extract the code of the last error that occurred in the
346 * specified instance.
349 * - ISAC_main_inst : ISAC instance
351 * Return value : Error code
354 int16_t WebRtcIsac_GetErrorCode(
355 ISACStruct* ISAC_main_inst);
358 /****************************************************************************
359 * WebRtcIsac_GetUplinkBw(...)
361 * This function outputs the target bottleneck of the codec. In
362 * channel-adaptive mode, the target bottleneck is specified through in-band
363 * signalling retreived by bandwidth estimator.
364 * In channel-independent, also called instantaneous mode, the target
365 * bottleneck is provided to the encoder by calling xxx_control(...). If
366 * xxx_control is never called the default values is returned. The default
367 * value for bottleneck at 16 kHz encoder sampling rate is 32000 bits/sec,
368 * and it is 56000 bits/sec for 32 kHz sampling rate.
369 * Note that the output is the iSAC internal operating bottleneck which might
370 * differ slightly from the one provided through xxx_control().
373 * - ISAC_main_inst : iSAC instance
376 * - *bottleneck : bottleneck in bits/sec
378 * Return value : -1 if error happens
379 * 0 bit-rates computed correctly.
382 int16_t WebRtcIsac_GetUplinkBw(
383 ISACStruct* ISAC_main_inst,
384 int32_t* bottleneck);
387 /******************************************************************************
388 * WebRtcIsac_SetMaxPayloadSize(...)
390 * This function sets a limit for the maximum payload size of iSAC. The same
391 * value is used both for 30 and 60 ms packets. If the encoder sampling rate
392 * is 16 kHz the maximum payload size is between 120 and 400 bytes. If the
393 * encoder sampling rate is 32 kHz the maximum payload size is between 120
396 * If an out of range limit is used, the function returns -1, but the closest
397 * valid value will be applied.
402 * The size of a packet is limited to the minimum of 'max-payload-size' and
403 * 'max-rate.' For instance, let's assume the max-payload-size is set to
404 * 170 bytes, and max-rate is set to 40 kbps. Note that a limit of 40 kbps
405 * translates to 150 bytes for 30ms frame-size & 300 bytes for 60ms
406 * frame-size. Then a packet with a frame-size of 30 ms is limited to 150,
407 * i.e. min(170, 150), and a packet with 60 ms frame-size is limited to
408 * 170 bytes, i.e. min(170, 300).
411 * - ISAC_main_inst : iSAC instance
412 * - maxPayloadBytes : maximum size of the payload in bytes
413 * valid values are between 120 and 400 bytes
414 * if encoder sampling rate is 16 kHz. For
415 * 32 kHz encoder sampling rate valid values
416 * are between 120 and 600 bytes.
418 * Return value : 0 if successful
419 * -1 if error happens
422 int16_t WebRtcIsac_SetMaxPayloadSize(
423 ISACStruct* ISAC_main_inst,
424 int16_t maxPayloadBytes);
427 /******************************************************************************
428 * WebRtcIsac_SetMaxRate(...)
430 * This function sets the maximum rate which the codec may not exceed for
431 * any signal packet. The maximum rate is defined and payload-size per
432 * frame-size in bits per second.
434 * The codec has a maximum rate of 53400 bits per second (200 bytes per 30
435 * ms) if the encoder sampling rate is 16kHz, and 160 kbps (600 bytes/30 ms)
436 * if the encoder sampling rate is 32 kHz.
438 * It is possible to set a maximum rate between 32000 and 53400 bits/sec
439 * in wideband mode, and 32000 to 160000 bits/sec in super-wideband mode.
441 * If an out of range limit is used, the function returns -1, but the closest
442 * valid value will be applied.
447 * The size of a packet is limited to the minimum of 'max-payload-size' and
448 * 'max-rate.' For instance, let's assume the max-payload-size is set to
449 * 170 bytes, and max-rate is set to 40 kbps. Note that a limit of 40 kbps
450 * translates to 150 bytes for 30ms frame-size & 300 bytes for 60ms
451 * frame-size. Then a packet with a frame-size of 30 ms is limited to 150,
452 * i.e. min(170, 150), and a packet with 60 ms frame-size is limited to
453 * 170 bytes, min(170, 300).
456 * - ISAC_main_inst : iSAC instance
457 * - maxRate : maximum rate in bits per second,
458 * valid values are 32000 to 53400 bits/sec in
459 * wideband mode, and 32000 to 160000 bits/sec in
460 * super-wideband mode.
462 * Return value : 0 if successful
463 * -1 if error happens
466 int16_t WebRtcIsac_SetMaxRate(
467 ISACStruct* ISAC_main_inst,
471 /******************************************************************************
472 * WebRtcIsac_DecSampRate()
473 * Return the sampling rate of the decoded audio.
476 * - ISAC_main_inst : iSAC instance
478 * Return value : sampling frequency in Hertz.
482 uint16_t WebRtcIsac_DecSampRate(ISACStruct* ISAC_main_inst);
485 /******************************************************************************
486 * WebRtcIsac_EncSampRate()
489 * - ISAC_main_inst : iSAC instance
491 * Return value : sampling rate in Hertz.
495 uint16_t WebRtcIsac_EncSampRate(ISACStruct* ISAC_main_inst);
498 /******************************************************************************
499 * WebRtcIsac_SetDecSampRate()
500 * Set the sampling rate of the decoder. Initialization of the decoder WILL
501 * NOT overwrite the sampling rate of the encoder. The default value is 16 kHz
502 * which is set when the instance is created.
505 * - ISAC_main_inst : iSAC instance
506 * - sampRate : sampling rate in Hertz.
508 * Return value : 0 if successful
512 int16_t WebRtcIsac_SetDecSampRate(ISACStruct* ISAC_main_inst,
513 uint16_t samp_rate_hz);
516 /******************************************************************************
517 * WebRtcIsac_SetEncSampRate()
518 * Set the sampling rate of the encoder. Initialization of the encoder WILL
519 * NOT overwrite the sampling rate of the encoder. The default value is 16 kHz
520 * which is set when the instance is created. The encoding-mode and the
521 * bottleneck remain unchanged by this call, however, the maximum rate and
522 * maximum payload-size will reset to their default value.
525 * - ISAC_main_inst : iSAC instance
526 * - sampRate : sampling rate in Hertz.
528 * Return value : 0 if successful
532 int16_t WebRtcIsac_SetEncSampRate(ISACStruct* ISAC_main_inst,
533 uint16_t sample_rate_hz);
537 /******************************************************************************
538 * WebRtcIsac_GetNewBitStream(...)
540 * This function returns encoded data, with the recieved bwe-index in the
541 * stream. If the rate is set to a value less than bottleneck of codec
542 * the new bistream will be re-encoded with the given target rate.
543 * It should always return a complete packet, i.e. only called once
544 * even for 60 msec frames.
546 * NOTE 1! This function does not write in the ISACStruct, it is not allowed.
547 * NOTE 2! Currently not implemented for SWB mode.
548 * NOTE 3! Rates larger than the bottleneck of the codec will be limited
549 * to the current bottleneck.
552 * - ISAC_main_inst : ISAC instance.
553 * - bweIndex : Index of bandwidth estimate to put in new
555 * - rate : target rate of the transcoder is bits/sec.
556 * Valid values are the accepted rate in iSAC,
557 * i.e. 10000 to 56000.
558 * - isRCU : if the new bit-stream is an RCU stream.
559 * Note that the rate parameter always indicates
560 * the target rate of the main payload, regardless
564 * - encoded : The encoded data vector
566 * Return value : >0 - Length (in bytes) of coded data
567 * -1 - Error or called in SWB mode
568 * NOTE! No error code is written to
569 * the struct since it is only allowed to read
572 int16_t WebRtcIsac_GetNewBitStream(
573 ISACStruct* ISAC_main_inst,
582 /****************************************************************************
583 * WebRtcIsac_GetDownLinkBwIndex(...)
585 * This function returns index representing the Bandwidth estimate from
586 * other side to this side.
589 * - ISAC_main_inst : iSAC struct
592 * - bweIndex : Bandwidth estimate to transmit to other side.
596 int16_t WebRtcIsac_GetDownLinkBwIndex(
597 ISACStruct* ISAC_main_inst,
599 int16_t* jitterInfo);
602 /****************************************************************************
603 * WebRtcIsac_UpdateUplinkBw(...)
605 * This function takes an index representing the Bandwidth estimate from
606 * this side to other side and updates BWE.
609 * - ISAC_main_inst : iSAC struct
610 * - bweIndex : Bandwidth estimate from other side.
614 int16_t WebRtcIsac_UpdateUplinkBw(
615 ISACStruct* ISAC_main_inst,
619 /****************************************************************************
620 * WebRtcIsac_ReadBwIndex(...)
622 * This function returns the index of the Bandwidth estimate from the bitstream.
625 * - encoded : Encoded bitstream
628 * - frameLength : Length of frame in packet (in samples)
629 * - bweIndex : Bandwidth estimate in bitstream
633 int16_t WebRtcIsac_ReadBwIndex(
634 const int16_t* encoded,
639 /*******************************************************************************
640 * WebRtcIsac_GetNewFrameLen(...)
642 * returns the frame lenght (in samples) of the next packet. In the case of channel-adaptive
643 * mode, iSAC decides on its frame lenght based on the estimated bottleneck
644 * this allows a user to prepare for the next packet (at the encoder)
646 * The primary usage is in CE to make the iSAC works in channel-adaptive mode
649 * - ISAC_main_inst : iSAC struct
651 * Return Value : frame lenght in samples
655 int16_t WebRtcIsac_GetNewFrameLen(
656 ISACStruct* ISAC_main_inst);
659 /****************************************************************************
660 * WebRtcIsac_GetRedPayload(...)
662 * Populates "encoded" with the redundant payload of the recently encoded
663 * frame. This function has to be called once that WebRtcIsac_Encode(...)
664 * returns a positive value. Regardless of the frame-size this function will
665 * be called only once after encoding is completed.
668 * - ISAC_main_inst : iSAC struct
671 * - encoded : the encoded data vector
675 * : >0 - Length (in bytes) of coded data
680 int16_t WebRtcIsac_GetRedPayload(
681 ISACStruct* ISAC_main_inst,
685 /****************************************************************************
686 * WebRtcIsac_DecodeRcu(...)
688 * This function decodes a redundant (RCU) iSAC frame. Function is called in
689 * NetEq with a stored RCU payload i case of packet loss. Output speech length
690 * will be a multiple of 480 samples: 480 or 960 samples,
691 * depending on the framesize (30 or 60 ms).
694 * - ISAC_main_inst : ISAC instance.
695 * - encoded : encoded ISAC RCU frame(s)
696 * - len : bytes in encoded vector
699 * - decoded : The decoded vector
701 * Return value : >0 - number of samples in decoded vector
704 int16_t WebRtcIsac_DecodeRcu(
705 ISACStruct* ISAC_main_inst,
706 const uint16_t* encoded,
709 int16_t* speechType);
712 #if defined(__cplusplus)
718 #endif /* WEBRTC_MODULES_AUDIO_CODING_CODECS_ISAC_MAIN_INTERFACE_ISAC_H_ */