75 if (*ptr >= end || val > INT_MAX - 254)
103 pkt->
code = (i ) & 0x3;
104 pkt->
stereo = (i >> 2) & 0x1;
105 pkt->
config = (i >> 3) & 0x1F;
108 if (pkt->
code >= 2 && buf_size < 2)
117 if (self_delimiting) {
119 if (len < 0 || len > end - ptr)
122 buf_size = end -
buf;
125 frame_bytes = end - ptr;
136 if (self_delimiting) {
138 if (len < 0 || 2 * len > end - ptr)
141 buf_size = end -
buf;
144 frame_bytes = end - ptr;
162 if (self_delimiting) {
164 if (len < 0 || len + frame_bytes > end - ptr)
166 end = ptr + frame_bytes +
len;
167 buf_size = end -
buf;
184 padding = (i >> 6) & 0x01;
185 pkt->
vbr = (i >> 7) & 0x01;
207 total_bytes += frame_bytes;
210 if (self_delimiting) {
212 if (len < 0 || len + total_bytes + padding > end - ptr)
214 end = ptr + total_bytes + len + padding;
215 buf_size = end -
buf;
218 frame_bytes = end - ptr - padding;
219 if (total_bytes > frame_bytes)
228 if (self_delimiting) {
230 if (frame_bytes < 0 || pkt->frame_count * frame_bytes + padding > end - ptr)
232 end = ptr + pkt->
frame_count * frame_bytes + padding;
233 buf_size = end -
buf;
235 frame_bytes = end - ptr - padding;
263 }
else if (pkt->
config < 16) {
277 memset(pkt, 0,
sizeof(*pkt));
294 static const uint8_t default_channel_map[2] = { 0, 1 };
306 "Multichannel configuration without extradata.\n");
316 if (extradata_size < 19) {
322 version = extradata[8];
340 map_type = extradata[18];
344 "Channel mapping 0 is only specified for up to 2 channels\n");
349 stereo_streams = channels - 1;
350 channel_map = default_channel_map;
351 }
else if (map_type == 1 || map_type == 2 || map_type == 255) {
352 if (extradata_size < 21 + channels) {
358 streams = extradata[19];
359 stereo_streams = extradata[20];
360 if (!streams || stereo_streams > streams ||
361 streams + stereo_streams > 255) {
363 "Invalid stream/stereo stream count: %d/%d\n", streams, stereo_streams);
370 "Channel mapping 1 is only specified for up to 8 channels\n");
375 }
else if (map_type == 2) {
376 int ambisonic_order =
ff_sqrt(channels) - 1;
377 if (channels != ((ambisonic_order + 1) * (ambisonic_order + 1)) &&
378 channels != ((ambisonic_order + 1) * (ambisonic_order + 1) + 2)) {
380 "Channel mapping 2 is only specified for channel counts" 381 " which can be written as (n + 1)^2 or (n + 1)^2 + 2" 382 " for nonnegative integer n\n");
385 if (channels > 227) {
393 channel_map = extradata + 21;
405 uint8_t idx = channel_map[channel_reorder(channels, i)];
410 }
else if (idx >= streams + stereo_streams) {
412 "Invalid channel map for output channel %d: %d\n", i, idx);
419 for (j = 0; j < i; j++)
420 if (channel_map[channel_reorder(channels, j)] == idx) {
426 if (idx < 2 * stereo_streams) {
445 float lowband_scratch[8 * 22];
446 float norm1[2 * 8 * 100];
447 float *norm2 = norm1 + 8 * 100;
451 int update_lowband = 1;
452 int lowband_offset = 0;
462 float *norm_loc1, *norm_loc2;
465 int effective_lowband = -1;
472 if (i <= f->coded_bands - 1) {
478 i == f->
start_band + 1) && (update_lowband || lowband_offset == 0))
486 memcpy(&norm1[band_offset], &norm1[band_offset - count], count *
sizeof(
float));
489 memcpy(&norm2[band_offset], &norm2[band_offset - count], count *
sizeof(
float));
496 int foldstart, foldend;
501 foldstart = lowband_offset;
503 foldend = lowband_offset - 1;
507 for (j = foldstart; j < foldend; j++) {
517 norm1[j] = (norm1[j] + norm2[j]) / 2;
520 norm_loc1 = effective_lowband != -1 ? norm1 + (effective_lowband << f->
size) :
NULL;
521 norm_loc2 = effective_lowband != -1 ? norm2 + (effective_lowband << f->
size) :
NULL;
526 norm1 + band_offset, 0, 1.0f,
527 lowband_scratch, cm[0]);
531 norm2 + band_offset, 0, 1.0f,
532 lowband_scratch, cm[1]);
536 norm1 + band_offset, 0, 1.0f,
537 lowband_scratch, cm[0] | cm[1]);
546 update_lowband = (b > band_size << 3);
550 #define NORMC(bits) ((bits) << (f->channels - 1) << f->size >> 2) 554 int i, j, low, high, total, done, bandbits, remaining, tbits_8ths;
557 int intensitystereo_bit = 0;
558 int dualstereo_bit = 0;
584 for (i = f->
start_band; i < f->end_band; i++) {
586 int b_dynalloc = dynalloc;
588 quanta =
FFMIN(quanta << 3,
FFMAX(6 << 3, quanta));
593 is_boost = boost_amount--;
603 tbits_8ths -= quanta;
609 dynalloc =
FFMAX(dynalloc - 1, 2);
627 if (tbits_8ths >= 1 << 3)
629 tbits_8ths -= skip_bit;
634 if (intensitystereo_bit <= tbits_8ths) {
635 tbits_8ths -= intensitystereo_bit;
636 if (tbits_8ths >= 1 << 3) {
637 dualstereo_bit = 1 << 3;
638 tbits_8ths -= 1 << 3;
641 intensitystereo_bit = 0;
646 for (i = f->
start_band; i < f->end_band; i++) {
650 int scale = duration + f->
channels - 1;
657 trim_offset[i] = trim * (band << scale) >> 6;
666 while (low <= high) {
667 int center = (low + high) >> 1;
674 bandbits =
FFMAX(bandbits + trim_offset[i], 0);
675 bandbits += boost[i];
677 if (bandbits >= threshold[i] || done) {
680 }
else if (bandbits >= f->
channels << 3) {
685 if (total > tbits_8ths)
693 for (i = f->
start_band; i < f->end_band; i++) {
699 bits1[i] =
FFMAX(bits1[i] + trim_offset[i], 0);
701 bits2[i] =
FFMAX(bits2[i] + trim_offset[i], 0);
704 bits1[i] += boost[i];
705 bits2[i] += boost[i];
709 bits2[i] =
FFMAX(bits2[i] - bits1[i], 0);
716 int center = (low + high) >> 1;
722 if (bandbits >= threshold[j] || done) {
725 }
else if (bandbits >= f->
channels << 3)
728 if (total > tbits_8ths)
739 if (bandbits >= threshold[i] || done)
742 bandbits = (bandbits >= f->
channels << 3) ?
755 if (j == skip_startband) {
757 tbits_8ths += skip_bit;
762 remaining = tbits_8ths - total;
770 if (allocation >=
FFMAX(threshold[j], (f->
channels + 1) << 3)) {
783 allocation -= 1 << 3;
788 if (intensitystereo_bit) {
789 total -= intensitystereo_bit;
791 total += intensitystereo_bit;
799 if (intensitystereo_bit) {
805 if (intensitystereo_bit)
811 tbits_8ths += dualstereo_bit;
812 else if (dualstereo_bit)
819 remaining = tbits_8ths - total;
822 for (i = f->
start_band; i < f->coded_bands; i++) {
829 for (i = f->
start_band; i < f->coded_bands; i++) {
831 int prev_extra = extrabits;
832 f->
pulses[i] += extrabits;
844 f->
pulses[i] -= extrabits;
854 if (f->
pulses[i] + offset < 2 * (dof << 3))
856 else if (f->
pulses[i] + offset < 3 * (dof << 3))
859 fine_bits = (f->
pulses[i] + offset + (dof << 2)) / (dof << 3);
861 max_bits =
FFMAX(max_bits, 0);
862 f->
fine_bits[i] = av_clip(fine_bits, 0, max_bits);
873 f->
pulses[i] -= extrabits;
886 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)
const char const char void * val
#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]
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]
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
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[]
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]
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
static const uint16_t opus_frame_duration[32]
int packet_size
packet size
int channels
number of audio channels
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]
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)