AIROC™ BTSDK v4.0 - Documentation | ||||
The repacketizer can be used to merge multiple Opus packets into a single packet or alternatively to split Opus packets that have previously been merged. More...
Typedefs | |
typedef struct OpusRepacketizer | OpusRepacketizer |
Functions | |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int | opus_repacketizer_get_size (void) |
Gets the size of an OpusRepacketizer structure. More... | |
OPUS_EXPORT OpusRepacketizer * | opus_repacketizer_init (OpusRepacketizer *rp) OPUS_ARG_NONNULL(1) |
(Re)initializes a previously allocated repacketizer state. More... | |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT OpusRepacketizer * | opus_repacketizer_create (void) |
Allocates memory and initializes the new repacketizer with opus_repacketizer_init(). | |
OPUS_EXPORT void | opus_repacketizer_destroy (OpusRepacketizer *rp) |
Frees an OpusRepacketizer allocated by opus_repacketizer_create(). More... | |
OPUS_EXPORT int | opus_repacketizer_cat (OpusRepacketizer *rp, const unsigned char *data, int len) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(2) |
Add a packet to the current repacketizer state. More... | |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int | opus_repacketizer_out_range (OpusRepacketizer *rp, int begin, int end, unsigned char *data, int maxlen) OPUS_ARG_NONNULL(1) OPUS_ARG_NONNULL(4) |
Construct a new packet from data previously submitted to the repacketizer state via opus_repacketizer_cat(). More... | |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int | opus_repacketizer_get_nb_frames (OpusRepacketizer *rp) OPUS_ARG_NONNULL(1) |
Return the total number of frames contained in packet data submitted to the repacketizer state so far via opus_repacketizer_cat() since the last call to opus_repacketizer_init() or opus_repacketizer_create(). More... | |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int | opus_repacketizer_out (OpusRepacketizer *rp, unsigned char *data, int maxlen) OPUS_ARG_NONNULL(1) |
Construct a new packet from data previously submitted to the repacketizer state via opus_repacketizer_cat(). More... | |
OPUS_EXPORT int | opus_packet_pad (unsigned char *data, int len, int new_len) |
Pads a given Opus packet to a larger size (possibly changing the TOC sequence). More... | |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int | opus_packet_unpad (unsigned char *data, int len) |
Remove all padding from a given Opus packet and rewrite the TOC sequence to minimize space usage. More... | |
OPUS_EXPORT int | opus_multistream_packet_pad (unsigned char *data, int len, int new_len, int nb_streams) |
Pads a given Opus multi-stream packet to a larger size (possibly changing the TOC sequence). More... | |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int | opus_multistream_packet_unpad (unsigned char *data, int len, int nb_streams) |
Remove all padding from a given Opus multi-stream packet and rewrite the TOC sequence to minimize space usage. More... | |
The repacketizer can be used to merge multiple Opus packets into a single packet or alternatively to split Opus packets that have previously been merged.
Splitting valid Opus packets is always guaranteed to succeed, whereas merging valid packets only succeeds if all frames have the same mode, bandwidth, and frame size, and when the total duration of the merged packet is no more than 120 ms. The repacketizer currently only operates on elementary Opus streams. It will not manipualte multistream packets successfully, except in the degenerate case where they consist of data from a single stream.
The repacketizing process starts with creating a repacketizer state, either by calling opus_repacketizer_create() or by allocating the memory yourself, e.g.,
Then the application should submit packets with opus_repacketizer_cat(), extract new packets with opus_repacketizer_out() or opus_repacketizer_out_range(), and then reset the state for the next set of input packets via opus_repacketizer_init().
For example, to split a sequence of packets into individual frames:
Alternatively, to combine a sequence of frames into packets that each contain up to TARGET_DURATION_MS
milliseconds of data:
An alternate way of merging packets is to simply call opus_repacketizer_cat() unconditionally until it fails. At that point, the merged packet can be obtained with opus_repacketizer_out() and the input packet for which opus_repacketizer_cat() needs to be re-added to a newly reinitialized repacketizer state.
OPUS_EXPORT int opus_multistream_packet_pad | ( | unsigned char * | data, |
int | len, | ||
int | new_len, | ||
int | nb_streams | ||
) |
Pads a given Opus multi-stream packet to a larger size (possibly changing the TOC sequence).
[in,out] | data | const unsigned char* : The buffer containing the packet to pad. |
len | opus_int32 : The size of the packet. This must be at least 1. | |
new_len | opus_int32 : The desired size of the packet after padding. This must be at least 1. | |
nb_streams | opus_int32 : The number of streams (not channels) in the packet. This must be at least as large as len. |
OPUS_OK | on success. |
OPUS_BAD_ARG | len was less than 1. |
OPUS_INVALID_PACKET | data did not contain a valid Opus packet. |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_multistream_packet_unpad | ( | unsigned char * | data, |
int | len, | ||
int | nb_streams | ||
) |
Remove all padding from a given Opus multi-stream packet and rewrite the TOC sequence to minimize space usage.
[in,out] | data | const unsigned char* : The buffer containing the packet to strip. |
len | opus_int32 : The size of the packet. This must be at least 1. | |
nb_streams | opus_int32 : The number of streams (not channels) in the packet. This must be at least 1. |
OPUS_BAD_ARG | len was less than 1 or new_len was less than len. |
OPUS_INVALID_PACKET | data did not contain a valid Opus packet. |
OPUS_EXPORT int opus_packet_pad | ( | unsigned char * | data, |
int | len, | ||
int | new_len | ||
) |
Pads a given Opus packet to a larger size (possibly changing the TOC sequence).
[in,out] | data | const unsigned char* : The buffer containing the packet to pad. |
len | opus_int32 : The size of the packet. This must be at least 1. | |
new_len | opus_int32 : The desired size of the packet after padding. This must be at least as large as len. |
OPUS_OK | on success. |
OPUS_BAD_ARG | len was less than 1 or new_len was less than len. |
OPUS_INVALID_PACKET | data did not contain a valid Opus packet. |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_packet_unpad | ( | unsigned char * | data, |
int | len | ||
) |
Remove all padding from a given Opus packet and rewrite the TOC sequence to minimize space usage.
[in,out] | data | const unsigned char* : The buffer containing the packet to strip. |
len | opus_int32 : The size of the packet. This must be at least 1. |
OPUS_BAD_ARG | len was less than 1. |
OPUS_INVALID_PACKET | data did not contain a valid Opus packet. |
OPUS_EXPORT int opus_repacketizer_cat | ( | OpusRepacketizer * | rp, |
const unsigned char * | data, | ||
int | len | ||
) |
Add a packet to the current repacketizer state.
This packet must match the configuration of any packets already submitted for repacketization since the last call to opus_repacketizer_init(). This means that it must have the same coding mode, audio bandwidth, frame size, and channel count. This can be checked in advance by examining the top 6 bits of the first byte of the packet, and ensuring they match the top 6 bits of the first byte of any previously submitted packet. The total duration of audio in the repacketizer state also must not exceed 120 ms, the maximum duration of a single packet, after adding this packet.
The contents of the current repacketizer state can be extracted into new packets using opus_repacketizer_out() or opus_repacketizer_out_range().
In order to add a packet with a different configuration or to add more audio beyond 120 ms, you must clear the repacketizer state by calling opus_repacketizer_init(). If a packet is too large to add to the current repacketizer state, no part of it is added, even if it contains multiple frames, some of which might fit. If you wish to be able to add parts of such packets, you should first use another repacketizer to split the packet into pieces and add them individually.
rp | OpusRepacketizer* : The repacketizer state to which to add the packet. | |
[in] | data | const unsigned char* : The packet data. The application must ensure this pointer remains valid until the next call to opus_repacketizer_init() or opus_repacketizer_destroy(). |
len | opus_int32 : The number of bytes in the packet data. |
OPUS_OK | The packet's contents have been added to the repacketizer state. |
OPUS_INVALID_PACKET | The packet did not have a valid TOC sequence, the packet's TOC sequence was not compatible with previously submitted packets (because the coding mode, audio bandwidth, frame size, or channel count did not match), or adding this packet would increase the total amount of audio stored in the repacketizer state to more than 120 ms. |
OPUS_EXPORT void opus_repacketizer_destroy | ( | OpusRepacketizer * | rp | ) |
Frees an OpusRepacketizer
allocated by opus_repacketizer_create().
[in] | rp | OpusRepacketizer* : State to be freed. |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_nb_frames | ( | OpusRepacketizer * | rp | ) |
Return the total number of frames contained in packet data submitted to the repacketizer state so far via opus_repacketizer_cat() since the last call to opus_repacketizer_init() or opus_repacketizer_create().
This defines the valid range of packets that can be extracted with opus_repacketizer_out_range() or opus_repacketizer_out().
rp | OpusRepacketizer* : The repacketizer state containing the frames. |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_get_size | ( | void | ) |
Gets the size of an OpusRepacketizer
structure.
OPUS_EXPORT OpusRepacketizer* opus_repacketizer_init | ( | OpusRepacketizer * | rp | ) |
(Re)initializes a previously allocated repacketizer state.
The state must be at least the size returned by opus_repacketizer_get_size(). This can be used for applications which use their own allocator instead of malloc(). It must also be called to reset the queue of packets waiting to be repacketized, which is necessary if the maximum packet duration of 120 ms is reached or if you wish to submit packets with a different Opus configuration (coding mode, audio bandwidth, frame size, or channel count). Failure to do so will prevent a new packet from being added with opus_repacketizer_cat().
rp | OpusRepacketizer* : The repacketizer state to (re)initialize. |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_out | ( | OpusRepacketizer * | rp, |
unsigned char * | data, | ||
int | maxlen | ||
) |
Construct a new packet from data previously submitted to the repacketizer state via opus_repacketizer_cat().
This is a convenience routine that returns all the data submitted so far in a single packet. It is equivalent to calling
rp | OpusRepacketizer* : The repacketizer state from which to construct the new packet. | |
[out] | data | const unsigned char* : The buffer in which to store the output packet. |
maxlen | opus_int32 : The maximum number of bytes to store in the output buffer. In order to guarantee success, this should be at least 1277*opus_repacketizer_get_nb_frames(rp) . However, 1*opus_repacketizer_get_nb_frames(rp) plus the size of all packet data submitted to the repacketizer since the last call to opus_repacketizer_init() or opus_repacketizer_create() is also sufficient, and possibly much smaller. |
OPUS_BUFFER_TOO_SMALL | maxlen was insufficient to contain the complete output packet. |
OPUS_EXPORT OPUS_WARN_UNUSED_RESULT int opus_repacketizer_out_range | ( | OpusRepacketizer * | rp, |
int | begin, | ||
int | end, | ||
unsigned char * | data, | ||
int | maxlen | ||
) |
Construct a new packet from data previously submitted to the repacketizer state via opus_repacketizer_cat().
rp | OpusRepacketizer* : The repacketizer state from which to construct the new packet. | |
begin | int : The index of the first frame in the current repacketizer state to include in the output. | |
end | int : One past the index of the last frame in the current repacketizer state to include in the output. | |
[out] | data | const unsigned char* : The buffer in which to store the output packet. |
maxlen | opus_int32 : The maximum number of bytes to store in the output buffer. In order to guarantee success, this should be at least 1276 for a single frame, or for multiple frames, 1277*(end-begin) . However, 1*(end-begin) plus the size of all packet data submitted to the repacketizer since the last call to opus_repacketizer_init() or opus_repacketizer_create() is also sufficient, and possibly much smaller. |
OPUS_BAD_ARG | [begin,end) was an invalid range of frames (begin < 0, begin >= end, or end > opus_repacketizer_get_nb_frames()). |
OPUS_BUFFER_TOO_SMALL | maxlen was insufficient to contain the complete output packet. |