76 if (*ptr >= end || val > INT_MAX - 254)
104 pkt->
code = (
i ) & 0x3;
105 pkt->
stereo = (i >> 2) & 0x1;
106 pkt->
config = (i >> 3) & 0x1F;
109 if (pkt->
code >= 2 && buf_size < 2)
118 if (self_delimiting) {
120 if (len < 0 || len > end - ptr)
123 buf_size = end - buf;
126 frame_bytes = end - ptr;
137 if (self_delimiting) {
139 if (len < 0 || 2 * len > end - ptr)
142 buf_size = end - buf;
145 frame_bytes = end - ptr;
163 if (self_delimiting) {
165 if (len < 0 || len + frame_bytes > end - ptr)
167 end = ptr + frame_bytes +
len;
168 buf_size = end - buf;
185 padding = (i >> 6) & 0x01;
186 pkt->
vbr = (i >> 7) & 0x01;
208 total_bytes += frame_bytes;
211 if (self_delimiting) {
213 if (len < 0 || len + total_bytes + padding > end - ptr)
215 end = ptr + total_bytes + len + padding;
216 buf_size = end - buf;
219 frame_bytes = end - ptr - padding;
220 if (total_bytes > frame_bytes)
229 if (self_delimiting) {
231 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
233 end = ptr + pkt->
frame_count * frame_bytes + padding;
234 buf_size = end - buf;
236 frame_bytes = end - ptr - padding;
264 }
else if (pkt->
config < 16) {
278 memset(pkt, 0,
sizeof(*pkt));
295 static const uint8_t default_channel_map[2] = { 0, 1 };
307 "Multichannel configuration without extradata.\n");
317 if (extradata_size < 19) {
323 version = extradata[8];
343 map_type = extradata[18];
347 "Channel mapping 0 is only specified for up to 2 channels\n");
352 stereo_streams = channels - 1;
353 channel_map = default_channel_map;
354 }
else if (map_type == 1 || map_type == 2 || map_type == 255) {
355 if (extradata_size < 21 + channels) {
361 streams = extradata[19];
362 stereo_streams = extradata[20];
363 if (!streams || stereo_streams > streams ||
364 streams + stereo_streams > 255) {
366 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
373 "Channel mapping 1 is only specified for up to 8 channels\n");
378 }
else if (map_type == 2) {
379 int ambisonic_order =
ff_sqrt(channels) - 1;
380 if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
381 channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
383 "Channel mapping 2 is only specified for channel counts" 384 " which can be written as (n + 1)^2 or (n + 1)^2 + 2" 385 " for nonnegative integer n\n");
388 if (channels > 227) {
396 channel_map = extradata + 21;
408 uint8_t idx = channel_map[channel_reorder(channels, i)];
413 }
else if (idx >= streams + stereo_streams) {
415 "Invalid channel map for output channel %d: %d\n", i, idx);
422 for (j = 0; j <
i; j++)
423 if (channel_map[channel_reorder(channels, j)] == idx) {
429 if (idx < 2 * stereo_streams) {
448 float lowband_scratch[8 * 22];
449 float norm1[2 * 8 * 100];
450 float *norm2 = norm1 + 8 * 100;
454 int update_lowband = 1;
455 int lowband_offset = 0;
465 float *norm_loc1, *norm_loc2;
468 int effective_lowband = -1;
475 if (i <= f->coded_bands - 1) {
481 i == f->
start_band + 1) && (update_lowband || lowband_offset == 0))
489 memcpy(&norm1[band_offset], &norm1[band_offset - count], count *
sizeof(
float));
492 memcpy(&norm2[band_offset], &norm2[band_offset - count], count *
sizeof(
float));
499 int foldstart, foldend;
504 foldstart = lowband_offset;
506 foldend = lowband_offset - 1;
510 for (j = foldstart; j < foldend; j++) {
520 norm1[j] = (norm1[j] + norm2[j]) / 2;
523 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->
size) :
NULL;
524 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->
size) :
NULL;
529 norm1 + band_offset, 0, 1.0f,
530 lowband_scratch, cm[0]);
534 norm2 + band_offset, 0, 1.0f,
535 lowband_scratch, cm[1]);
539 norm1 + band_offset, 0, 1.0f,
540 lowband_scratch, cm[0] | cm[1]);
549 update_lowband = (b > band_size << 3);
553 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2) 557 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
560 int intensitystereo_bit = 0;
561 int dualstereo_bit = 0;
587 for (i = f->
start_band; i < f->end_band; i++) {
589 int b_dynalloc = dynalloc;
591 quanta =
FFMIN(quanta << 3,
FFMAX(6 << 3, quanta));
596 is_boost = boost_amount--;
606 tbits_8ths -= quanta;
612 dynalloc =
FFMAX(dynalloc - 1, 2);
632 if (tbits_8ths >= 1 << 3)
634 tbits_8ths -= skip_bit;
639 if (intensitystereo_bit <= tbits_8ths) {
640 tbits_8ths -= intensitystereo_bit;
641 if (tbits_8ths >= 1 << 3) {
642 dualstereo_bit = 1 << 3;
643 tbits_8ths -= 1 << 3;
646 intensitystereo_bit = 0;
651 for (i = f->
start_band; i < f->end_band; i++) {
655 int scale = duration + f->
channels - 1;
662 trim_offset[
i] = trim * (band << scale) >> 6;
671 while (low <= high) {
672 int center = (low + high) >> 1;
679 bandbits =
FFMAX(bandbits + trim_offset[i], 0);
680 bandbits += boost[
i];
682 if (bandbits >= threshold[i] || done) {
685 }
else if (bandbits >= f->
channels << 3) {
690 if (total > tbits_8ths)
698 for (i = f->
start_band; i < f->end_band; i++) {
704 bits1[
i] =
FFMAX(bits1[i] + trim_offset[i], 0);
706 bits2[
i] =
FFMAX(bits2[i] + trim_offset[i], 0);
709 bits1[
i] += boost[
i];
710 bits2[
i] += boost[
i];
714 bits2[
i] =
FFMAX(bits2[i] - bits1[i], 0);
721 int center = (low + high) >> 1;
727 if (bandbits >= threshold[j] || done) {
730 }
else if (bandbits >= f->
channels << 3)
733 if (total > tbits_8ths)
744 if (bandbits >= threshold[i] || done)
747 bandbits = (bandbits >= f->
channels << 3) ?
760 if (j == skip_startband) {
762 tbits_8ths += skip_bit;
767 remaining = tbits_8ths - total;
775 if (allocation >=
FFMAX(threshold[j], (f->
channels + 1) << 3)) {
788 allocation -= 1 << 3;
793 if (intensitystereo_bit) {
794 total -= intensitystereo_bit;
796 total += intensitystereo_bit;
804 if (intensitystereo_bit) {
810 if (intensitystereo_bit)
816 tbits_8ths += dualstereo_bit;
817 else if (dualstereo_bit)
824 remaining = tbits_8ths - total;
827 for (i = f->
start_band; i < f->coded_bands; i++) {
834 for (i = f->
start_band; i < f->coded_bands; i++) {
836 int prev_extra = extrabits;
859 if (f->
pulses[i] + offset < 2 * (dof << 3))
861 else if (f->
pulses[i] + offset < 3 * (dof << 3))
864 fine_bits = (f->
pulses[
i] + offset + (dof << 2)) / (dof << 3);
866 max_bits =
FFMAX(max_bits, 0);
867 f->
fine_bits[
i] = av_clip(fine_bits, 0, max_bits);
891 extrabits -= fineextra;
int ff_opus_parse_packet(OpusPacket *pkt, const uint8_t *buf, int buf_size, int self_delimiting)
Parse Opus packet info from raw packet data.
static int channel_reorder_vorbis(int nb_channels, int channel_idx)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int frame_count
frame count
const uint8_t ff_celt_log_freq_range[]
static int channel_reorder_unknown(int nb_channels, int channel_idx)
float coeffs[CELT_MAX_FRAME_SIZE]
#define avpriv_request_sample(...)
const uint8_t ff_celt_freq_bands[]
void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
uint32_t ff_opus_rc_dec_log(OpusRangeCoder *rc, uint32_t bits)
#define AV_CH_LAYOUT_STEREO
const int * channel_map
channel index (or -1 if muted channel) map
void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size)
CELT: write a uniformly distributed integer.
int fine_priority[CELT_MAX_BANDS]
static av_cold int end(AVCodecContext *avctx)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static const uint8_t bits2[81]
ChannelMap * channel_maps
static const uint8_t opus_default_extradata[30]
const uint8_t ff_celt_log2_frac[]
#define i(width, name, range_min, range_max)
int alloc_boost[CELT_MAX_BANDS]
static av_always_inline double ff_exp10(double x)
Compute 10^x for floating point values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int tf_change[CELT_MAX_BANDS]
int pulses[CELT_MAX_BANDS]
static const uint8_t offset[127][2]
int fine_bits[CELT_MAX_BANDS]
uint32_t ff_opus_rc_dec_cdf(OpusRangeCoder *rc, const uint16_t *cdf)
uint64_t channel_layout
Audio channel layout.
int frame_size[MAX_FRAMES]
frame sizes
int frame_duration
frame duration, in samples @ 48kHz
const uint8_t ff_celt_freq_range[]
void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf)
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
const uint8_t ff_celt_static_caps[4][2][21]
static int xiph_lacing_16bit(const uint8_t **ptr, const uint8_t *end)
Read a 1- or 2-byte frame length.
static int xiph_lacing_full(const uint8_t **ptr, const uint8_t *end)
Read a multi-byte length (used for code 3 packet padding size)
const uint16_t ff_celt_model_spread[]
main external API structure.
const uint64_t ff_vorbis_channel_layouts[9]
static av_always_inline uint32_t opus_rc_tell(const OpusRangeCoder *rc)
CELT: estimate bits of entropy that have thus far been consumed for the current CELT frame...
#define CELT_MAX_FINE_BITS
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
int config
configuration: tells the audio mode, bandwidth, and frame duration
const uint8_t ff_celt_static_alloc[11][21]
int skip_samples
Number of audio samples to skip at the start of the next decoded frame.
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
const VDPAUPixFmtMap * map
int stereo
whether this packet is mono or stereo
int data_size
size of the useful data – packet size - padding
const uint16_t ff_celt_model_alloc_trim[]
internal math functions header
common internal api header.
static const uint16_t opus_frame_duration[32]
int packet_size
packet size
int channels
number of audio channels
struct AVCodecInternal * internal
Private context used for internal data.
int frame_offset[MAX_FRAMES]
frame offsets
enum OpusBandwidth bandwidth
bandwidth
uint32_t ff_opus_rc_dec_uint(OpusRangeCoder *rc, uint32_t size)
CELT: read a uniform distribution.
av_cold int ff_opus_parse_extradata(AVCodecContext *avctx, OpusContext *s)
const uint8_t ff_vorbis_channel_layout_offsets[8][8]
int code
packet code: specifies the frame layout
#define AV_CH_LAYOUT_MONO
uint8_t collapse_masks[CELT_MAX_BANDS]
static double val(void *priv, double ch)
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
static const uint8_t bits1[81]
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
static av_always_inline uint32_t opus_rc_tell_frac(const OpusRangeCoder *rc)