38#define __STDC_CONSTANT_MACROS
50#include <libavcodec/avcodec.h>
51#include <libavutil/channel_layout.h>
52#include <libavutil/mem.h>
64#define MAGIC 0xb135ca11
65#define LOW_LATENCY_AUDIOENC_FRAME_DURATION 2.5
70#define MOD_NAME "[lavcd aud.] "
75 bool silent,
int bitrate);
88 [
AC_ALAW] = {AV_CODEC_ID_PCM_ALAW, NULL, 0 },
89 [
AC_MULAW] = { AV_CODEC_ID_PCM_MULAW, NULL, 0 },
90 [
AC_SPEEX] = { AV_CODEC_ID_SPEEX, NULL, 0 },
91 [
AC_OPUS] = { AV_CODEC_ID_OPUS,
"libopus", 96000},
92 [
AC_G722] = { AV_CODEC_ID_ADPCM_G722, NULL, 0 },
93 [
AC_FLAC] = { AV_CODEC_ID_FLAC, NULL, 0 },
94 [
AC_MP3] = { AV_CODEC_ID_MP3, NULL, 0 },
95 [
AC_AAC] = { AV_CODEC_ID_AAC,
"libfdk_aac", 0 },
115 bool context_initialized;
118 unsigned char *tmp_buffer;
119 size_t tmp_buffer_size;
124 if (new_size <= *cur_size) {
127 unsigned char *new_buf = realloc(*tmp_buffer, new_size);
129 *tmp_buffer = new_buf;
130 *cur_size = new_size;
139 av_strerror(rc, errbuf,
sizeof(errbuf));
144static const struct AVCodec *
148 const char *
const requested_encoder =
150 if (requested_encoder != NULL) {
151 preferred_encoder = requested_encoder;
153 const struct AVCodec *ret = NULL;
154 if (preferred_encoder) {
155 ret = avcodec_find_encoder_by_name(preferred_encoder);
156 if (ret != NULL && ret->id != codec_id) {
158 "Requested encoder %s cannot handle "
159 "specified codec!\n",
161 assert(preferred_encoder == requested_encoder);
164 if (ret == NULL && requested_encoder != NULL) {
165 MSG(ERROR,
"Requested encoder %s was not found!\n",
171 ret = avcodec_find_encoder(codec_id);
173 if (ret == NULL || silent ||
174 (ret->id != AV_CODEC_ID_MP3 && ret->id != AV_CODEC_ID_AAC)) {
178 const int level = strcmp(ret->name,
"aac") == 0
184 "Consider using Opus instead of encoder %s,\nsee also: "
185 "https://github.com/CESNET/UltraGrid/wiki/Audio-Settings#codecs\n",
191static const struct AVCodec *
195 if (pref_dec != NULL) {
196 const struct AVCodec *ret =
197 avcodec_find_decoder_by_name(pref_dec);
198 if (ret && ret->id == codec_id) {
203 ret ?
"cannot handle received codec"
207 return avcodec_find_decoder(codec_id);
211 "audioenc-frame-duration",
212 "* audioenc-frame-duration=<ms>\n"
213 " Sets audio encoder frame duration (in ms), default is " TOSTRING(
215ADD_TO_PARAM(
"audio-lavc-decoder",
"* audio-lavc-decoder=<decoder_name>\n"
216 " Use selected audio lavc decoder\n");
217ADD_TO_PARAM(
"audio-lavc-encoder",
"* audio-lavc-encoder=<encoder_name>\n"
218 " Use selected audio lavc encoder\n");
233 enum AVCodecID codec_id = AV_CODEC_ID_NONE;
247#if LIBAVCODEC_VERSION_INT <= AV_VERSION_INT(58, 9, 100)
248 avcodec_register_all();
253 s->direction = direction;
282 s->
codec_ctx->strict_std_compliance = -2;
284 s->bitrate = bitrate;
287 s->
pkt = av_packet_alloc();
290 s->output_channel.data = s->output_channel_data;
293 s->output_channel.
codec = audio_codec;
304 const enum AVSampleFormat *p =
codec->sample_fmts;
306 while (*p != AV_SAMPLE_FMT_NONE) {
307 if (*p == sample_fmt)
318 if (s->bitrate > 0) {
319 MSG(INFO,
"Setting bit rate to: %d bps\n", s->bitrate);
322 const int default_rate =
324 if (default_rate > 0) {
325 MSG(INFO,
"Setting defaulat bit rate: %d bps\n", default_rate);
340 s->
codec_ctx->strict_std_compliance = -2;
346 enum AVSampleFormat sample_fmts[AV_SAMPLE_FMT_NB];
351 sample_fmts[
count++] = AV_SAMPLE_FMT_U8;
352 sample_fmts[
count++] = AV_SAMPLE_FMT_U8P;
355 sample_fmts[
count++] = AV_SAMPLE_FMT_S16;
356 sample_fmts[
count++] = AV_SAMPLE_FMT_S16P;
360 sample_fmts[
count++] = AV_SAMPLE_FMT_S32;
361 sample_fmts[
count++] = AV_SAMPLE_FMT_S32P;
362 sample_fmts[
count++] = AV_SAMPLE_FMT_FLT;
363 sample_fmts[
count++] = AV_SAMPLE_FMT_FLTP;
367 s->
codec_ctx->sample_fmt = AV_SAMPLE_FMT_NONE;
369 for (
int i = 0; i <
count; ++i) {
371 s->
codec_ctx->sample_fmt = sample_fmts[i];
376 if (s->
codec_ctx->sample_fmt == AV_SAMPLE_FMT_NONE) {
378 while (s->
codec->sample_fmts[i] != AV_SAMPLE_FMT_NONE) {
379 if (s->
codec->sample_fmts[i] != AV_SAMPLE_FMT_DBL &&
380 s->
codec->sample_fmts[i] != AV_SAMPLE_FMT_DBLP) {
388 if (s->
codec_ctx->sample_fmt == AV_SAMPLE_FMT_NONE) {
394#if FF_API_NEW_CHANNEL_LAYOUT
395 s->
codec_ctx->ch_layout = (
struct AVChannelLayout) AV_CHANNEL_LAYOUT_MONO;
397 s->
codec_ctx->channel_layout = AV_CH_LAYOUT_MONO;
400 if (strcmp(s->
codec->name,
"libopus") == 0) {
401 int ret = av_opt_set(s->codec_ctx->priv_data,
"application",
"lowdelay", 0);
403 print_libav_audio_error(LOG_LEVEL_WARNING,
"Could not set Opus low delay app type", ret);
405 }
else if (strcmp(s->codec->name,
"opus") == 0) {
406 char warn[] = MOD_NAME
"Native FFmpeg Opus encoder seems to be currently broken "
407 "with UltraGrid. You may be able to use 'libopus' encoder instead. Please let "
408 "us know to " PACKAGE_BUGREPORT
" if you either want to use the native encoder "
409 "or it even works for you.\n";
410 log_msg(LOG_LEVEL_WARNING,
"%s", wrap_paragraph(warn));
411 int ret = av_opt_set_double(s->codec_ctx->priv_data,
"opus_delay", 5, 0);
413 print_libav_audio_error(LOG_LEVEL_WARNING,
"Cannot set Opus delay to 5", ret);
421 if (s->codec->id == AV_CODEC_ID_OPUS) {
422 int ret = av_opt_set_double(s->codec_ctx->priv_data,
"frame_duration", frame_duration, 0);
427 if (s->codec->id == AV_CODEC_ID_FLAC) {
428 s->codec_ctx->frame_size =
desc.sample_rate * frame_duration /
MS_IN_SEC;
433 int ret = avcodec_open2(s->codec_ctx, s->codec, NULL);
439 if(s->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) {
440 s->codec_ctx->frame_size = 1;
443 s->av_frame->nb_samples = s->codec_ctx->frame_size;
444 s->av_frame->format = s->codec_ctx->sample_fmt;
445#if FF_API_NEW_CHANNEL_LAYOUT
446 s->av_frame->ch_layout = (
struct AVChannelLayout) AV_CHANNEL_LAYOUT_MONO;
448 s->av_frame->channel_layout = AV_CH_LAYOUT_MONO;
450 s->av_frame->sample_rate = s->codec_ctx->sample_rate;
452 "Setting AV frame: %d samples, format %d, sample rate %d\n",
453 s->av_frame->nb_samples, s->av_frame->format,
454 s->av_frame->sample_rate);
456 ret = av_frame_get_buffer(s->av_frame, 0);
463 s->output_channel.sample_rate =
desc.sample_rate;
464 s->output_channel.bps = av_get_bytes_per_sample(s->codec_ctx->sample_fmt);
465 s->saved_desc =
desc;
467 s->context_initialized =
true;
481 s->
codec_ctx->strict_std_compliance = -2;
487 s->
codec_ctx->time_base = (AVRational) { 1,
desc.sample_rate };
497 s->context_initialized =
true;
505 if (channel == NULL) {
513 MSG(ERROR,
"Unable to reinitialize audio compress!\n");
518 if (s->output_channel.bps == channel->
bps &&
519 s->
codec_ctx->sample_fmt != AV_SAMPLE_FMT_FLT &&
520 s->
codec_ctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {
526 if (s->
codec_ctx->sample_fmt != AV_SAMPLE_FMT_FLT &&
527 s->
codec_ctx->sample_fmt != AV_SAMPLE_FMT_FLTP) {
531 s->output_channel.bps;
534 if (s->output_channel.bps == channel->
bps) {
536 MSG(ERROR,
"Auxiliary buffer overflow!\n");
543 int data_len = channel->
data_len / channel->
bps * 4;
549 MSG(ERROR,
"Auxiliary buffer overflow!\n");
552 (
char *) s->tmp_buffer, data_len);
564 return AV_NOPTS_VALUE;
567 return ts - cached_samples;
573 if (recv_ret != 0 || s->output_channel.timestamp != -1 ||
577 s->output_channel.timestamp =
586 MSG(ERROR,
"Output buffer overflow!\n");
588 if (s->
pkt->pts != AV_NOPTS_VALUE && s->
pkt->pts < 0) {
591 memcpy(s->output_channel_data + s->output_channel.data_len,
592 s->
pkt->data, s->
pkt->size);
593 s->output_channel.data_len += s->
pkt->size;
594 s->output_channel.duration +=
595 s->
codec_ctx->frame_size / (double) s->output_channel.sample_rate;
603 assert(s->
codec_ctx->sample_fmt != AV_SAMPLE_FMT_DBL &&
604 s->
codec_ctx->sample_fmt != AV_SAMPLE_FMT_DBLP);
610 if (channel != NULL) {
612 get_in_ts(channel, cached_bytes / s->output_channel.bps);
615 int bps = s->output_channel.bps;
617 s->output_channel.data_len = 0;
618 s->output_channel.duration = 0.0;
619 s->output_channel.timestamp = -1;
620 int chunk_size = s->
codec_ctx->frame_size * bps;
627 MSG(DEBUG2,
"Sending PTS %" PRId64
"\n", s->
av_frame->pts);
633 if (s->
av_frame->pts != AV_NOPTS_VALUE) {
640 av_packet_unref(s->
pkt);
643 if (ret != AVERROR(EAGAIN) && ret != 0) {
645 "Receive packet error", ret);
651 if(!(s->
codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) && s->output_channel.data_len > 0)
659 if(s->output_channel.data_len) {
660 return &s->output_channel;
681 memcpy(s->tmp_buffer, channel->
data, channel->
data_len);
683 s->
pkt->data = s->tmp_buffer;
685 s->output_channel.data_len = 0;
697 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
706 int data_size = av_samples_get_buffer_size(NULL,
channels,
709 memcpy(s->output_channel_data + s->output_channel.data_len, s->
av_frame->data[0],
711 s->output_channel.data_len += data_size;
712 s->
pkt->dts = s->
pkt->pts = AV_NOPTS_VALUE;
718 assert(s->
codec_ctx->sample_fmt != AV_SAMPLE_FMT_DBL &&
719 s->
codec_ctx->sample_fmt != AV_SAMPLE_FMT_DBLP);
722 if (s->
codec_ctx->sample_fmt == AV_SAMPLE_FMT_FLT ||
723 s->
codec_ctx->sample_fmt == AV_SAMPLE_FMT_FLTP) {
724 float2int(s->output_channel_data, s->output_channel.data, s->output_channel.data_len);
725 }
else if (s->
codec_ctx->sample_fmt == AV_SAMPLE_FMT_U8) {
726 signed2unsigned(s->output_channel_data, s->output_channel.data, s->output_channel.data_len);
729 s->output_channel.bps = av_get_bytes_per_sample(s->
codec_ctx->sample_fmt);
730 s->output_channel.sample_rate = s->
codec_ctx->sample_rate;
732 return &s->output_channel;
739 return s->
codec->supported_samplerates;
744 if (s->context_initialized) {
749 ret = avcodec_send_frame(s->
codec_ctx, NULL);
756 av_packet_unref(s->
pkt);
757 }
while (ret >= 0 && ret != AVERROR_EOF && ret != AVERROR(EAGAIN));
758 if (ret < 0 && ret != AVERROR_EOF && ret != AVERROR(EAGAIN)) {
768 s->context_initialized =
false;
779 av_packet_free(&s->
pkt);
788append(
char *dst,
size_t dst_len,
const char *
name,
bool prefered)
790 const char *
const end = dst + dst_len - 1;
808 const AVCodec *
codec = NULL;
809#if LIBAVCODEC_VERSION_INT > AV_VERSION_INT(58, 9, 100)
811 while ((
codec = av_codec_iterate(&i)) != NULL) {
812 if (
codec->id !=
id) {
815 char *dst = av_codec_is_encoder(
codec) ? encoders :
decoders;
816 const bool prefered = av_codec_is_encoder(
codec) &&
821 codec = avcodec_find_encoder(
id);
822 while (
codec != NULL) {
823 if (
codec->id ==
id && av_codec_is_encoder(
codec)) {
824 const bool prefered = strcmp(
codec->
name, prefenc) == 0;
830 codec = avcodec_find_decoder(
id);
831 while (
codec != NULL) {
832 if (
codec->id ==
id && av_codec_is_decoder(
codec)) {
838 snprintf(buf, buflen,
"encoders:%s decoders:%s", encoders,
decoders);
847 const char *
const end = buffer + buflen - 1;
851 const char *prefenc =
853 if (prefenc == NULL) {
static bool reinitialize_decoder(struct libavcodec_codec_state *s, struct audio_desc desc)
static void cleanup_common(struct libavcodec_codec_state *s)
#define LOW_LATENCY_AUDIOENC_FRAME_DURATION
static const char * libavcodec_get_codec_desc(void *state, size_t buflen, char *buffer)
static audio_channel * libavcodec_compress(void *, audio_channel *)
static bool process_input(struct libavcodec_codec_state *s, const audio_channel *channel)
static int get_bitrate(struct libavcodec_codec_state *s)
static const struct AVCodec * init_encoder(enum AVCodecID codec_id, const char *preferred_encoder, bool silent)
static const audio_codec_t supported_codecs[]
static void set_out_ts(int recv_ret, struct libavcodec_codec_state *s)
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
static void libavcodec_done(void *)
static const struct audio_compress_info libavcodec_audio_codec
static void append(char *dst, size_t dst_len, const char *name, bool prefered)
static void get_encoders_decoders(enum AVCodecID id, const char *prefenc, char *buf, size_t buflen)
static void print_libav_audio_error(int verbosity, const char *msg, int rc)
static const struct codec_param mapping[AC_COUNT]
static void write_out_data(struct libavcodec_codec_state *s)
static void * libavcodec_init(audio_codec_t audio_codec, audio_codec_direction_t direction, bool silent, int bitrate)
static audio_channel * libavcodec_decompress(void *, audio_channel *)
static const int * libavcodec_get_sample_rates(void *state)
static int64_t get_in_ts(const audio_channel *channel, int cached_samples)
static void resize_tmp_buffer(unsigned char **tmp_buffer, size_t *cur_size, size_t new_size)
static bool reinitialize_encoder(struct libavcodec_codec_state *s, struct audio_desc desc)
static const struct AVCodec * init_decoder(enum AVCodecID codec_id)
const char * get_name_to_audio_codec(audio_codec_t codec)
#define AUDIO_COMPRESS_ABI_VERSION
static __device__ const float offset
void log_msg(int level, const char *format,...)
#define LOG_LEVEL_NOTICE
information that may be interesting
#define LOG_LEVEL_INFO
normal reporting
#define LOG_LEVEL_ERROR
general errors
#define LOG_LEVEL_WARNING
less severe errors
void handle_error(int status)
const char * get_commandline_param(const char *key)
#define ADD_TO_PARAM(param, doc)
void print_decoder_error(const char *mod_name, int rc)
void lavd_flush(AVCodecContext *codec_ctx)
void ug_set_av_logging()
Sets specified log level either given explicitly or from UG-wide log_level.
#define AVCODECCTX_CHANNELS(context)
@ LIBRARY_CLASS_AUDIO_COMPRESS
#define REGISTER_MODULE(name, info, lclass, abi)
Registers module to global modules' registry.
@ STR_LEN
"standard" string length placeholder
static const struct decoder_item decoders[]
static void strappend(char **dst, size_t *dst_alloc_len, const char *src)
Audio channel is a non-owning view on audio_frame2 isolating one channel.
const char * preferred_encoder
reallocate tmp_buffer if requested size is greater than currently allocated size
struct audio_desc saved_desc
struct AVCodecContext * codec_ctx
const struct AVCodec * codec
@ kHz90
timestamp time base
bool audio_desc_eq(struct audio_desc a1, struct audio_desc a2)
void signed2unsigned(char *out, const char *in, int in_len)
void float2int(char *out, const char *in, int len)
void change_bps(char *out, int out_bps, const char *in, int in_bps, int in_len)
void int2float(char *out, const char *in, int len)
struct audio_desc audio_desc_from_audio_channel(const audio_channel *channel)