66 bytestream_put_byte (&bs, 0x1);
67 bytestream_put_byte (&bs, avctx->
channels);
70 bytestream_put_le16 (&bs, 0x0);
71 bytestream_put_byte (&bs, 0x0);
76 int tmp = 0x0, extended_toc = 0;
80 { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 17, 0, 21, 25, 29 } },
81 { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 18, 0, 22, 26, 30 } },
82 { { 1, 5, 9, 0, 0 }, { 0, 0, 0, 13, 15 }, { 19, 0, 23, 27, 31 } },
83 { { 2, 6, 10, 0, 0 }, { 0, 0, 0, 14, 16 }, { 20, 0, 24, 28, 32 } },
84 { { 3, 7, 11, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } },
85 { { 4, 8, 12, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } },
87 int cfg = toc_cfg[
s->packet.framesize][
s->packet.mode][
s->packet.bandwidth];
91 if (
s->packet.frames == 2) {
92 if (
s->frame[0].framebits ==
s->frame[1].framebits) {
98 }
else if (
s->packet.frames > 2) {
102 tmp |= (
s->channels > 1) << 2;
103 tmp |= (cfg - 1) << 3;
106 for (
int i = 0;
i < (
s->packet.frames - 1);
i++)
107 *fsize_needed |= (
s->frame[
i].framebits !=
s->frame[
i + 1].framebits);
108 tmp = (*fsize_needed) << 7;
110 tmp |=
s->packet.frames;
113 *
size = 1 + extended_toc;
120 const int subframesize =
s->avctx->frame_size;
125 for (
int ch = 0; ch <
f->channels; ch++) {
134 for (
int sf = 0; sf < subframes; sf++) {
135 if (sf != (subframes - 1))
140 for (
int ch = 0; ch <
f->channels; ch++) {
146 memcpy(&
b->samples[sf*subframesize], input,
len);
151 if (sf != (subframes - 1))
159 const int subframesize =
s->avctx->frame_size;
163 for (
int ch = 0; ch <
f->channels; ch++) {
165 float m =
b->emph_coeff;
175 for (
int sf = 0; sf < subframes; sf++) {
176 for (
int ch = 0; ch <
f->channels; ch++) {
178 float m =
b->emph_coeff;
179 for (
int i = 0;
i < subframesize;
i++) {
180 float sample =
b->samples[sf*subframesize +
i];
181 b->samples[sf*subframesize +
i] =
sample - m;
184 if (sf != (subframes - 1))
193 float *
win =
s->scratch, *
temp =
s->scratch + 1920;
196 for (
int ch = 0; ch <
f->channels; ch++) {
198 float *
src1 =
b->overlap;
199 for (
int t = 0; t <
f->blocks; t++) {
205 s->mdct[0]->mdct(
s->mdct[0],
b->coeffs + t,
win,
f->blocks);
211 memset(
win, 0, wlen*
sizeof(
float));
212 for (
int ch = 0; ch <
f->channels; ch++) {
223 s->dsp->vector_fmul_reverse(
temp,
b->samples + rwin,
227 s->mdct[
f->size]->mdct(
s->mdct[
f->size],
b->coeffs,
win, 1);
231 for (
int ch = 0; ch <
f->channels; ch++) {
237 float *coeffs = &
block->coeffs[band_offset];
239 for (
int j = 0; j < band_size; j++)
240 ener += coeffs[j]*coeffs[j];
242 block->lin_energy[
i] = sqrtf(ener) + FLT_EPSILON;
243 ener = 1.0f/
block->lin_energy[
i];
245 for (
int j = 0; j < band_size; j++)
258 int tf_select = 0,
diff = 0, tf_changed = 0, tf_select_needed;
259 int bits =
f->transient ? 2 : 4;
263 for (
int i =
f->start_band; i < f->end_band;
i++) {
265 const int tbit = (
diff ^ 1) ==
f->tf_change[
i];
270 bits =
f->transient ? 4 : 5;
276 tf_select =
f->tf_select;
279 for (
int i =
f->start_band; i < f->end_band;
i++)
285 float gain =
f->pf_gain;
286 int txval, octave =
f->pf_octave, period =
f->pf_period, tapset =
f->pf_tapset;
293 txval =
FFMIN(octave, 6);
297 txval =
av_clip(period - (16 << octave) + 1, 0, (1 << (4 + octave)) - 1);
299 period = txval + (16 << octave) - 1;
301 txval =
FFMIN(((
int)(gain / 0.09375f)) - 1, 7);
303 gain = 0.09375f * (txval + 1);
310 for (
int i = 0;
i < 2;
i++) {
323 float alpha, beta, prev[2] = { 0, 0 };
334 beta = 1.0f - (4915.0f/32768.0f);
340 for (
int i =
f->start_band; i < f->end_band;
i++) {
341 for (
int ch = 0; ch <
f->channels; ch++) {
344 const float last =
FFMAX(-9.0f, last_energy[ch][
i]);
349 }
else if (left >= 2) {
352 }
else if (left >= 1) {
358 prev[ch] += beta * q_en;
366 uint32_t inter, intra;
385 for (
int i =
f->start_band; i < f->end_band;
i++) {
386 if (!
f->fine_bits[
i])
388 for (
int ch = 0; ch <
f->channels; ch++) {
390 int quant, lim = (1 <<
f->fine_bits[
i]);
394 offset = 0.5f - ((
quant + 0.5f) * (1 << (14 -
f->fine_bits[
i])) / 16384.0f);
402 for (
int priority = 0; priority < 2; priority++) {
403 for (
int i =
f->start_band; i < f->end_band && (
f->framebits -
opus_rc_tell(rc)) >=
f->channels;
i++) {
406 for (
int ch = 0; ch <
f->channels; ch++) {
408 const float err =
block->error_energy[
i];
409 const float offset = 0.5f * (1 << (14 -
f->fine_bits[
i] - 1)) / 16384.0f;
428 if (
f->framebits >= 16)
430 for (
int ch = 0; ch <
s->channels; ch++)
431 memset(
s->last_quantized_energy[ch], 0.0f,
sizeof(
float)*
CELT_MAX_BANDS);
470 if (
f->anticollapse_needed)
476 for (
int ch = 0; ch <
f->channels; ch++) {
479 s->last_quantized_energy[ch][
i] =
block->energy[
i] +
block->error_energy[
i];
486 dst[1] = v - dst[0] >> 2;
487 return 1 + (v >= 252);
499 for (
int i = 0;
i <
s->packet.frames - 1;
i++) {
501 s->frame[
i].framebits >> 3);
506 for (
int i = 0;
i <
s->packet.frames;
i++) {
508 s->frame[
i].framebits >> 3);
509 offset +=
s->frame[
i].framebits >> 3;
521 f->format =
s->avctx->sample_fmt;
522 f->nb_samples =
s->avctx->frame_size;
523 f->channel_layout =
s->avctx->channel_layout;
528 for (
int i = 0;
i <
s->channels;
i++) {
530 memset(
f->extended_data[
i], 0,
bps*
f->nb_samples);
559 int pad_empty =
s->packet.frames*(
frame_size/
s->avctx->frame_size) -
s->bufqueue.available + 1;
564 for (
int i = 0;
i < pad_empty;
i++) {
572 for (
int i = 0;
i <
s->packet.frames;
i++) {
574 alloc_size +=
s->frame[
i].framebits >> 3;
578 alloc_size += 2 +
s->packet.frames*2;
641 avctx->
bit_rate = coupled*(96000) + (
s->channels - coupled*2)*(48000);
643 int64_t clipped_rate =
av_clip(avctx->
bit_rate, 6000, 255000 *
s->channels);
644 av_log(avctx,
AV_LOG_ERROR,
"Unsupported bitrate %"PRId64
" kbps, clipping to %"PRId64
" kbps\n",
645 avctx->
bit_rate/1000, clipped_rate/1000);
670 for (
int ch = 0; ch <
s->channels; ch++)
671 memset(
s->last_quantized_energy[ch], 0.0f,
sizeof(
float)*
CELT_MAX_BANDS);
682 max_frames =
ceilf(
FFMIN(
s->options.max_delay_ms, 120.0f)/2.5f);
690 for (
int i = 0;
i < max_frames;
i++) {
691 s->frame[
i].dsp =
s->dsp;
692 s->frame[
i].avctx =
s->avctx;
693 s->frame[
i].seed = 0;
694 s->frame[
i].pvq =
s->pvq;
695 s->frame[
i].apply_phase_inv =
s->options.apply_phase_inv;
696 s->frame[
i].block[0].emph_coeff =
s->frame[
i].block[1].emph_coeff = 0.0f;
702 #define OPUSENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
704 {
"opus_delay",
"Maximum delay in milliseconds", offsetof(
OpusEncContext,
options.max_delay_ms),
AV_OPT_TYPE_FLOAT, { .dbl =
OPUS_MAX_LOOKAHEAD }, 2.5f,
OPUS_MAX_LOOKAHEAD,
OPUSENC_FLAGS,
"max_delay_ms" },
705 {
"apply_phase_inv",
"Apply intensity stereo phase inversion", offsetof(
OpusEncContext,
options.apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
OPUSENC_FLAGS,
"apply_phase_inv" },
718 {
"compression_level",
"10" },
735 .supported_samplerates = (
const int []){ 48000, 0 },
static float win(SuperEqualizerContext *s, float n, int N)
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
void ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, int64_t *pts, int64_t *duration)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
static av_cold int init(AVCodecContext *avctx)
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
static AVFrame * ff_bufqueue_get(struct FFBufQueue *queue)
Get the first buffer from the queue and remove it.
static av_always_inline void bytestream_put_buffer(uint8_t **b, const uint8_t *src, unsigned int size)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static __device__ float floor(float a)
static __device__ float ceilf(float a)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
const OptionDef options[]
#define AV_CH_LAYOUT_MONO
#define AV_CH_LAYOUT_STEREO
#define AV_CODEC_FLAG_BITEXACT
Use only bitexact stuff (except (I)DCT).
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
#define AV_CODEC_CAP_SMALL_LAST_FRAME
Codec can be fed a final frame with a smaller size.
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_FLTP
float, planar
#define LIBAVUTIL_VERSION_INT
static const int16_t alpha[]
#define FF_CODEC_CAP_INIT_THREADSAFE
The codec does not modify any global variables in the init function, allowing to call the init functi...
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
av_cold void ff_mdct15_uninit(MDCT15Context **ps)
av_cold int ff_mdct15_init(MDCT15Context **ps, int inverse, int N, double scale)
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
#define CELT_POSTFILTER_MINPERIOD
#define CELT_ENERGY_SILENCE
#define CELT_MAX_FINE_BITS
int av_cold ff_celt_pvq_init(CeltPVQ **pvq, int encode)
void av_cold ff_celt_pvq_uninit(CeltPVQ **pvq)
void ff_opus_rc_enc_end(OpusRangeCoder *rc, uint8_t *dst, int size)
void ff_opus_rc_enc_uint(OpusRangeCoder *rc, uint32_t val, uint32_t size)
CELT: write a uniformly distributed integer.
void ff_opus_rc_put_raw(OpusRangeCoder *rc, uint32_t val, uint32_t count)
CELT: write 0 - 31 bits to the rawbits buffer.
void ff_opus_rc_enc_cdf(OpusRangeCoder *rc, int val, const uint16_t *cdf)
void ff_opus_rc_enc_log(OpusRangeCoder *rc, int val, uint32_t bits)
void ff_opus_rc_enc_laplace(OpusRangeCoder *rc, int *value, uint32_t symbol, int decay)
void ff_opus_rc_enc_init(OpusRangeCoder *rc)
#define OPUS_RC_CHECKPOINT_ROLLBACK(rc)
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 OPUS_RC_CHECKPOINT_SPAWN(rc)
#define OPUS_RC_CHECKPOINT_BITS(rc)
static void celt_quant_coarse(CeltFrame *f, OpusRangeCoder *rc, float last_energy[][CELT_MAX_BANDS])
static const AVClass opusenc_class
static void celt_frame_setup_input(OpusEncContext *s, CeltFrame *f)
static const AVOption opusenc_options[]
static av_cold int opus_encode_init(AVCodecContext *avctx)
static int opus_gen_toc(OpusEncContext *s, uint8_t *toc, int *size, int *fsize_needed)
static AVFrame * spawn_empty_frame(OpusEncContext *s)
static int opus_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
static void celt_enc_tf(CeltFrame *f, OpusRangeCoder *rc)
static void celt_frame_mdct(OpusEncContext *s, CeltFrame *f)
static void opus_packet_assembler(OpusEncContext *s, AVPacket *avpkt)
static void exp_quant_coarse(OpusRangeCoder *rc, CeltFrame *f, float last_energy[][CELT_MAX_BANDS], int intra)
static const AVCodecDefault opusenc_defaults[]
static void celt_apply_preemph_filter(OpusEncContext *s, CeltFrame *f)
static void celt_encode_frame(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f, int index)
static void celt_quant_final(OpusEncContext *s, OpusRangeCoder *rc, CeltFrame *f)
static void opus_write_extradata(AVCodecContext *avctx)
static void celt_enc_quant_pfilter(OpusRangeCoder *rc, CeltFrame *f)
static av_cold int opus_encode_end(AVCodecContext *avctx)
static int write_opuslacing(uint8_t *dst, int v)
static void celt_quant_fine(CeltFrame *f, OpusRangeCoder *rc)
#define OPUS_MAX_CHANNELS
#define OPUS_BLOCK_SIZE(x)
#define OPUS_MAX_LOOKAHEAD
av_cold int ff_opus_psy_end(OpusPsyContext *s)
int ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index)
int ff_opus_psy_process(OpusPsyContext *s, OpusPacketInfo *p)
av_cold int ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx, struct FFBufQueue *bufqueue, OpusEncOptions *options)
void ff_opus_psy_signal_eof(OpusPsyContext *s)
void ff_opus_psy_celt_frame_init(OpusPsyContext *s, CeltFrame *f, int index)
void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f, OpusRangeCoder *rc)
const uint8_t ff_celt_freq_range[]
const uint8_t ff_celt_freq_bands[]
const float *const ff_celt_window
const uint16_t ff_celt_model_energy_small[]
const uint16_t ff_celt_model_tapset[]
const uint8_t ff_opus_default_coupled_streams[]
const int8_t ff_celt_tf_select[4][2][2][2]
const uint8_t ff_celt_coarse_energy_dist[4][2][42]
const float ff_celt_alpha_coef[]
const float ff_celt_beta_coef[]
const float ff_celt_mean_energy[]
const float ff_celt_postfilter_taps[3][3]
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
int64_t bit_rate
the average bitrate
int initial_padding
Audio only.
int sample_rate
samples per second
int frame_number
Frame counter, set by libavcodec.
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int channels
number of audio channels
int frame_size
Number of samples per channel in an audio frame.
const char * name
Name of the codec implementation.
This structure describes decoded (raw) audio or video data.
int nb_samples
number of audio samples (per channel) described by this frame
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
uint8_t ** extended_data
pointers to the data planes/channels.
This structure stores compressed data.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Structure holding the queue.
struct FFBufQueue bufqueue
float last_quantized_energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
MDCT15Context * mdct[CELT_BLOCK_NB]
static av_always_inline int diff(const uint32_t a, const uint32_t b)
static const uint8_t offset[127][2]