73 bytestream_put_byte (&bs, 0x1);
77 bytestream_put_le16 (&bs, 0x0);
78 bytestream_put_byte (&bs, 0x0);
83 int tmp = 0x0, extended_toc = 0;
87 { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 17, 0, 21, 25, 29 } },
88 { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 18, 0, 22, 26, 30 } },
89 { { 1, 5, 9, 0, 0 }, { 0, 0, 0, 13, 15 }, { 19, 0, 23, 27, 31 } },
90 { { 2, 6, 10, 0, 0 }, { 0, 0, 0, 14, 16 }, { 20, 0, 24, 28, 32 } },
91 { { 3, 7, 11, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } },
92 { { 4, 8, 12, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 } },
94 int cfg = toc_cfg[
s->packet.framesize][
s->packet.mode][
s->packet.bandwidth];
98 if (
s->packet.frames == 2) {
99 if (
s->frame[0].framebits ==
s->frame[1].framebits) {
105 }
else if (
s->packet.frames > 2) {
109 tmp |= (
s->channels > 1) << 2;
113 for (
int i = 0;
i < (
s->packet.frames - 1);
i++)
114 *fsize_needed |= (
s->frame[
i].framebits !=
s->frame[
i + 1].framebits);
115 tmp = (*fsize_needed) << 7;
117 tmp |=
s->packet.frames;
120 *
size = 1 + extended_toc;
127 const int subframesize =
s->avctx->frame_size;
132 for (
int ch = 0; ch <
f->channels; ch++) {
152 for (
int sf = 0; sf < subframes; sf++) {
153 if (sf != (subframes - 1))
158 for (
int ch = 0; ch <
f->channels; ch++) {
164 memcpy(&
b->samples[sf*subframesize], input,
len);
169 if (sf != (subframes - 1))
177 const int subframesize =
s->avctx->frame_size;
182 for (
int ch = 0; ch <
f->channels; ch++) {
184 float m =
b->emph_coeff;
194 for (
int sf = 0; sf < subframes; sf++) {
195 for (
int ch = 0; ch <
f->channels; ch++) {
197 float m =
b->emph_coeff;
198 for (
int i = 0;
i < subframesize;
i++) {
199 float sample =
b->samples[sf*subframesize +
i];
200 b->samples[sf*subframesize +
i] =
sample - m;
203 if (sf != (subframes - 1))
212 float *
win =
s->scratch, *
temp =
s->scratch + 1920;
215 for (
int ch = 0; ch <
f->channels; ch++) {
217 float *
src1 =
b->overlap;
218 for (
int t = 0; t <
f->blocks; t++) {
224 s->tx_fn[0](
s->tx[0],
b->coeffs + t,
win,
sizeof(
float)*
f->blocks);
230 memset(
win, 0, wlen*
sizeof(
float));
231 for (
int ch = 0; ch <
f->channels; ch++) {
242 s->dsp->vector_fmul_reverse(
temp,
b->samples + rwin,
246 s->tx_fn[
f->size](
s->tx[
f->size],
b->coeffs,
win,
sizeof(
float));
250 for (
int ch = 0; ch <
f->channels; ch++) {
256 float *coeffs = &
block->coeffs[band_offset];
258 for (
int j = 0; j < band_size; j++)
259 ener += coeffs[j]*coeffs[j];
262 ener = 1.0f/
block->lin_energy[
i];
264 for (
int j = 0; j < band_size; j++)
277 int tf_select = 0,
diff = 0, tf_changed = 0, tf_select_needed;
278 int bits =
f->transient ? 2 : 4;
282 for (
int i =
f->start_band;
i < f->end_band;
i++) {
284 const int tbit = (
diff ^ 1) ==
f->tf_change[
i];
289 bits =
f->transient ? 4 : 5;
295 tf_select =
f->tf_select;
298 for (
int i =
f->start_band;
i < f->end_band;
i++)
304 float gain =
f->pf_gain;
305 int txval, octave =
f->pf_octave, period =
f->pf_period, tapset =
f->pf_tapset;
312 txval =
FFMIN(octave, 6);
316 txval =
av_clip(period - (16 << octave) + 1, 0, (1 << (4 + octave)) - 1);
318 period = txval + (16 << octave) - 1;
320 txval =
FFMIN(((
int)(gain / 0.09375f)) - 1, 7);
322 gain = 0.09375f * (txval + 1);
329 for (
int i = 0;
i < 2;
i++) {
342 float alpha, beta, prev[2] = { 0, 0 };
353 beta = 1.0f - (4915.0f/32768.0f);
359 for (
int i =
f->start_band;
i < f->end_band;
i++) {
360 for (
int ch = 0; ch <
f->channels; ch++) {
363 const float last =
FFMAX(-9.0f, last_energy[ch][
i]);
368 }
else if (
left >= 2) {
371 }
else if (
left >= 1) {
377 prev[ch] += beta * q_en;
385 uint32_t inter, intra;
404 for (
int i =
f->start_band;
i < f->end_band;
i++) {
405 if (!
f->fine_bits[
i])
407 for (
int ch = 0; ch <
f->channels; ch++) {
409 int quant, lim = (1 <<
f->fine_bits[
i]);
413 offset = 0.5f - ((
quant + 0.5f) * (1 << (14 -
f->fine_bits[
i])) / 16384.0f);
421 for (
int priority = 0; priority < 2; priority++) {
425 for (
int ch = 0; ch <
f->channels; ch++) {
427 const float err =
block->error_energy[
i];
428 const float offset = 0.5f * (1 << (14 -
f->fine_bits[
i] - 1)) / 16384.0f;
447 if (
f->framebits >= 16)
449 for (
int ch = 0; ch <
s->channels; ch++)
450 memset(
s->last_quantized_energy[ch], 0.0f,
sizeof(
float)*
CELT_MAX_BANDS);
489 if (
f->anticollapse_needed)
495 for (
int ch = 0; ch <
f->channels; ch++) {
498 s->last_quantized_energy[ch][
i] =
block->energy[
i] +
block->error_energy[
i];
506 return 1 + (v >= 252);
518 for (
int i = 0;
i <
s->packet.frames - 1;
i++) {
520 s->frame[
i].framebits >> 3);
525 for (
int i = 0;
i <
s->packet.frames;
i++) {
527 s->frame[
i].framebits >> 3);
528 offset +=
s->frame[
i].framebits >> 3;
541 f->format =
s->avctx->sample_fmt;
542 f->nb_samples =
s->avctx->frame_size;
552 for (
int i = 0;
i <
s->channels;
i++) {
554 memset(
f->extended_data[
i], 0,
bps*
f->nb_samples);
563 int ret,
frame_size, discard_padding, alloc_size = 0;
571 if (!
s->afq.remaining_samples || !avctx->
frame_num)
583 int pad_empty =
s->packet.frames*(
frame_size/
s->avctx->frame_size) -
s->bufqueue.available + 1;
588 for (
int i = 0;
i < pad_empty;
i++) {
596 for (
int i = 0;
i <
s->packet.frames;
i++) {
598 alloc_size +=
s->frame[
i].framebits >> 3;
602 alloc_size += 2 +
s->packet.frames*2;
619 if (discard_padding > 0) {
623 AV_WL32(&side[4], discard_padding);
657 int max_delay_samples = (
s->options.max_delay_ms *
s->avctx->sample_rate) / 1000;
664 avctx->
bit_rate = coupled*(96000) + (
s->channels - coupled*2)*(48000);
667 av_log(avctx,
AV_LOG_ERROR,
"Unsupported bitrate %"PRId64
" kbps, clipping to %"PRId64
" kbps\n",
668 avctx->
bit_rate/1000, clipped_rate/1000);
695 for (
int ch = 0; ch <
s->channels; ch++)
696 memset(
s->last_quantized_energy[ch], 0.0f,
sizeof(
float)*
CELT_MAX_BANDS);
707 max_frames =
ceilf(
FFMIN(
s->options.max_delay_ms, 120.0f)/2.5f);
715 for (
int i = 0;
i < max_frames;
i++) {
716 s->frame[
i].dsp =
s->dsp;
717 s->frame[
i].avctx =
s->avctx;
718 s->frame[
i].seed = 0;
719 s->frame[
i].pvq =
s->pvq;
720 s->frame[
i].apply_phase_inv =
s->options.apply_phase_inv;
721 s->frame[
i].block[0].emph_coeff =
s->frame[
i].block[1].emph_coeff = 0.0f;
727#define OPUSENC_FLAGS AV_OPT_FLAG_ENCODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM
729 {
"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, .unit =
"max_delay_ms" },
730 {
"apply_phase_inv",
"Apply intensity stereo phase inversion", offsetof(
OpusEncContext,
options.apply_phase_inv),
AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1,
OPUSENC_FLAGS, .unit =
"apply_phase_inv" },
735 .class_name =
"Opus encoder",
743 {
"compression_level",
"10" },
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
static float win(SuperEqualizerContext *s, float n, int N)
const FFCodec ff_opus_encoder
av_cold void ff_af_queue_close(AudioFrameQueue *afq)
Close AudioFrameQueue.
av_cold void ff_af_queue_init(AVCodecContext *avctx, AudioFrameQueue *afq)
Initialize AudioFrameQueue.
int ff_af_queue_remove(AudioFrameQueue *afq, int nb_samples, AVPacket *pkt)
Remove frame(s) from the queue.
int ff_af_queue_add(AudioFrameQueue *afq, const AVFrame *f)
Add a frame to the queue.
static int BS_FUNC left(const BSCTX *bc)
Return the number of the bits left in a buffer.
static void ff_bufqueue_add(void *log, struct FFBufQueue *queue, AVFrame *buf)
Add a buffer to the queue.
static void ff_bufqueue_discard_all(struct FFBufQueue *queue)
Unref and remove all buffers from the queue.
static AVFrame * ff_bufqueue_peek(struct FFBufQueue *queue, unsigned index)
Get a buffer from the queue without altering it.
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 i(width, name, range_min, range_max)
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
Public libavutil channel layout APIs header.
#define CODEC_CH_LAYOUTS(...)
#define CODEC_SAMPLERATES(...)
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define CODEC_SAMPLEFMTS(...)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static __device__ float sqrtf(float a)
static __device__ float floor(float a)
static __device__ float ceilf(float a)
int(* init)(AVBSFContext *ctx)
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 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 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 AVFrame * spawn_empty_frame(OpusEncContext *s)
static int write_opuslacing(uint8_t *dst, int v)
static void celt_quant_fine(CeltFrame *f, OpusRangeCoder *rc)
static const FFCodecDefault opusenc_defaults[]
#define OPUS_SAMPLES_TO_BLOCK_SIZE(x)
#define OPUS_MAX_CHANNELS
#define OPUS_BLOCK_SIZE(x)
#define OPUS_MAX_LOOKAHEAD
void ff_opus_psy_postencode_update(OpusPsyContext *s, CeltFrame *f)
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)
int ff_alloc_packet(AVCodecContext *avctx, AVPacket *avpkt, int64_t size)
Check AVPacket size and allocate data.
static av_always_inline int64_t ff_samples_from_time_base(const AVCodecContext *avctx, int64_t duration)
Rescale from time base to AVCodecContext.sample_rate.
static CheckasmConfig cfg
static const uint8_t bits[8]
static const uint8_t frame_size[4]
@ AV_OPT_TYPE_FLOAT
Underlying C type is float.
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
#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_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
#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
Recommends skipping the specified number of samples.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
#define AV_CHANNEL_LAYOUT_STEREO
#define AV_CHANNEL_LAYOUT_MONO
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
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.
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
@ AV_SAMPLE_FMT_FLTP
float, planar
#define LIBAVUTIL_VERSION_INT
static const int16_t alpha[]
static void scale(int *out, const int *in, const int w, const int h, const int shift)
av_cold AVFloatDSPContext * avpriv_float_dsp_alloc(int bit_exact)
Allocate a float DSP context.
Memory handling functions.
#define DECLARE_ALIGNED(n, t, v)
Declare a variable that is aligned in memory.
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)
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
main external API structure.
AVChannelLayout ch_layout
Audio channel layout.
int64_t frame_num
Frame counter, set by libavcodec.
int64_t bit_rate
the average bitrate
int initial_padding
Audio only.
int sample_rate
samples per second
int flags
AV_CODEC_FLAG_*.
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
int frame_size
Number of samples per channel in an audio frame.
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.
Structure holding the queue.
struct FFBufQueue bufqueue
av_tx_fn tx_fn[CELT_BLOCK_NB]
float last_quantized_energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]
AVTXContext * tx[CELT_BLOCK_NB]
const uint8_t ff_celt_freq_range[]
const uint8_t ff_celt_freq_bands[]
const float ff_celt_window_padded[136]
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_opus_deemph_weights[]
const float ff_celt_beta_coef[]
const float ff_celt_mean_energy[]
const float ff_celt_postfilter_taps[3][3]
#define ff_celt_model_energy_small
av_cold void av_tx_uninit(AVTXContext **ctx)
Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type, int inv, int len, const void *scale, uint64_t flags)
Initialize a transform context with the given configuration (i)MDCTs with an odd length are currently...
@ AV_TX_FLOAT_MDCT
Standard MDCT with a sample data type of float, double or int32_t, respectively.
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static const uint8_t quant[64]