21#include <freerdp/config.h>
23#include <freerdp/log.h>
25#include <libavcodec/avcodec.h>
26#include <libavutil/avutil.h>
27#include <libavutil/opt.h>
28#if defined(SWRESAMPLE_FOUND)
29#include <libswresample/swresample.h>
30#elif defined(AVRESAMPLE_FOUND)
31#include <libavresample/avresample.h>
33#error "libswresample or libavresample required"
37#include "dsp_ffmpeg.h"
39#define TAG FREERDP_TAG("dsp.ffmpeg")
41struct S_FREERDP_DSP_CONTEXT
47 UINT32 bufferedSamples;
51 AVCodecContext* context;
56#if defined(SWRESAMPLE_FOUND)
59 AVAudioResampleContext* rcontext;
63static BOOL ffmpeg_codec_is_filtered(
enum AVCodecID
id, WINPR_ATTR_UNUSED BOOL encoder)
67#if !defined(WITH_DSP_EXPERIMENTAL)
69 case AV_CODEC_ID_ADPCM_IMA_OKI:
71 case AV_CODEC_ID_ADPCM_MS:
72 case AV_CODEC_ID_G723_1:
73 case AV_CODEC_ID_GSM_MS:
74 case AV_CODEC_ID_PCM_ALAW:
75 case AV_CODEC_ID_PCM_MULAW:
79 case AV_CODEC_ID_NONE:
83 case AV_CODEC_ID_AAC_LATM:
91static enum AVCodecID ffmpeg_get_avcodec(
const AUDIO_FORMAT* WINPR_RESTRICT format)
94 return AV_CODEC_ID_NONE;
96 switch (format->wFormatTag)
98 case WAVE_FORMAT_UNKNOWN:
99 return AV_CODEC_ID_NONE;
101 case WAVE_FORMAT_PCM:
102 switch (format->wBitsPerSample)
105 return AV_CODEC_ID_PCM_U16LE;
108 return AV_CODEC_ID_PCM_U8;
111 return AV_CODEC_ID_NONE;
114 case WAVE_FORMAT_DVI_ADPCM:
115 return AV_CODEC_ID_ADPCM_IMA_OKI;
117 case WAVE_FORMAT_ADPCM:
118 return AV_CODEC_ID_ADPCM_MS;
120 case WAVE_FORMAT_ALAW:
121 return AV_CODEC_ID_PCM_ALAW;
123 case WAVE_FORMAT_MULAW:
124 return AV_CODEC_ID_PCM_MULAW;
126 case WAVE_FORMAT_GSM610:
127 return AV_CODEC_ID_GSM_MS;
129 case WAVE_FORMAT_MSG723:
130 return AV_CODEC_ID_G723_1;
132 case WAVE_FORMAT_AAC_MS:
133 return AV_CODEC_ID_AAC;
135 case WAVE_FORMAT_OPUS:
136 return AV_CODEC_ID_OPUS;
139 return AV_CODEC_ID_NONE;
143static enum AVSampleFormat ffmpeg_sample_format(
const AUDIO_FORMAT* WINPR_RESTRICT format)
145 switch (format->wFormatTag)
147 case WAVE_FORMAT_PCM:
148 switch (format->wBitsPerSample)
151 return AV_SAMPLE_FMT_U8;
154 return AV_SAMPLE_FMT_S16;
157 return AV_SAMPLE_FMT_NONE;
160 case WAVE_FORMAT_DVI_ADPCM:
161 case WAVE_FORMAT_ADPCM:
162 return AV_SAMPLE_FMT_S16P;
164 case WAVE_FORMAT_MPEGLAYER3:
165 case WAVE_FORMAT_AAC_MS:
166 return AV_SAMPLE_FMT_FLTP;
168 case WAVE_FORMAT_OPUS:
169 return AV_SAMPLE_FMT_S16;
171 case WAVE_FORMAT_MSG723:
172 case WAVE_FORMAT_GSM610:
173 return AV_SAMPLE_FMT_S16P;
175 case WAVE_FORMAT_ALAW:
176 return AV_SAMPLE_FMT_S16;
179 return AV_SAMPLE_FMT_NONE;
183static void ffmpeg_close_context(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
187 if (context->context)
188 avcodec_free_context(&context->context);
191 av_frame_free(&context->frame);
193 if (context->resampled)
194 av_frame_free(&context->resampled);
196 if (context->buffered)
197 av_frame_free(&context->buffered);
198 context->bufferedSamples = 0;
201 av_packet_free(&context->packet);
203 if (context->rcontext)
205#if defined(SWRESAMPLE_FOUND)
206 swr_free(&context->rcontext);
208 avresample_free(&context->rcontext);
212 context->id = AV_CODEC_ID_NONE;
213 context->codec =
nullptr;
214 context->isOpen = FALSE;
215 context->context =
nullptr;
216 context->frame =
nullptr;
217 context->resampled =
nullptr;
218 context->packet =
nullptr;
219 context->rcontext =
nullptr;
223static void ffmpeg_setup_resample_frame(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
226 if (context->resampled->buf[0] !=
nullptr)
227 av_frame_unref(context->resampled);
229 if (context->common.encoder)
231 context->resampled->format = context->context->sample_fmt;
232 context->resampled->sample_rate = context->context->sample_rate;
236 context->resampled->format = AV_SAMPLE_FMT_S16;
238 WINPR_ASSERT(format->nSamplesPerSec <= INT_MAX);
239 context->resampled->sample_rate = (int)format->nSamplesPerSec;
242#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
243 av_channel_layout_default(&context->resampled->ch_layout, format->nChannels);
245 const int64_t layout = av_get_default_channel_layout(format->nChannels);
246 context->resampled->channel_layout = layout;
247 context->resampled->channels = format->nChannels;
251static BOOL ffmpeg_open_context(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
255 if (!context || context->isOpen)
262 context->id = ffmpeg_get_avcodec(format);
264 if (ffmpeg_codec_is_filtered(context->id, context->common.encoder))
267 if (context->common.encoder)
268 context->codec = avcodec_find_encoder(context->id);
270 context->codec = avcodec_find_decoder(context->id);
275 context->context = avcodec_alloc_context3(context->codec);
277 if (!context->context)
283 case AV_CODEC_ID_GSM_MS:
284 context->context->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL;
287 case AV_CODEC_ID_AAC:
288#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(60, 31, 102)
289 context->context->profile = FF_PROFILE_AAC_MAIN;
290#elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT(62, 11, 100)
291 context->context->profile = AV_PROFILE_AAC_MAIN;
293 context->context->profile = AV_PROFILE_AAC_LOW;
301 context->context->max_b_frames = 1;
302 context->context->delay = 0;
304#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
305 av_channel_layout_default(&context->context->ch_layout, format->nChannels);
307 context->context->channels = format->nChannels;
308 const int64_t layout = av_get_default_channel_layout(format->nChannels);
309 context->context->channel_layout = layout;
311 context->context->sample_rate = (int)format->nSamplesPerSec;
312 context->context->block_align = format->nBlockAlign;
313 context->context->bit_rate = format->nAvgBytesPerSec * 8LL;
314 context->context->sample_fmt = ffmpeg_sample_format(format);
315 context->context->time_base = av_make_q(1, context->context->sample_rate);
317 if ((ret = avcodec_open2(context->context, context->codec,
nullptr)) < 0)
319 const char* err = av_err2str(ret);
320 WLog_ERR(TAG,
"Error avcodec_open2 %s [%d]", err, ret);
324 context->packet = av_packet_alloc();
326 if (!context->packet)
329 context->frame = av_frame_alloc();
334 context->resampled = av_frame_alloc();
336 if (!context->resampled)
339 context->buffered = av_frame_alloc();
341 if (!context->buffered)
344#if defined(SWRESAMPLE_FOUND)
345 context->rcontext = swr_alloc();
347 context->rcontext = avresample_alloc_context();
350 if (!context->rcontext)
353#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
354 av_channel_layout_default(&context->frame->ch_layout, format->nChannels);
356 context->frame->channel_layout = layout;
357 context->frame->channels = format->nChannels;
359 WINPR_ASSERT(format->nSamplesPerSec <= INT_MAX);
360 context->frame->sample_rate = (int)format->nSamplesPerSec;
361 context->frame->format = AV_SAMPLE_FMT_S16;
363 ffmpeg_setup_resample_frame(context, format);
365 if (context->context->frame_size > 0)
367#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
368 ret = av_channel_layout_copy(&context->buffered->ch_layout, &context->resampled->ch_layout);
372 context->buffered->channel_layout = context->resampled->channel_layout;
373 context->buffered->channels = context->resampled->channels;
375 context->buffered->format = context->resampled->format;
376 context->buffered->nb_samples = context->context->frame_size;
378 ret = av_frame_get_buffer(context->buffered, 1);
383 context->isOpen = TRUE;
386 ffmpeg_close_context(context);
390#if defined(SWRESAMPLE_FOUND)
391static BOOL ffmpeg_resample_frame(SwrContext* WINPR_RESTRICT context, AVFrame* WINPR_RESTRICT in,
392 AVFrame* WINPR_RESTRICT out)
396 if (!swr_is_initialized(context))
398 if ((ret = swr_config_frame(context, out, in)) < 0)
400 const char* err = av_err2str(ret);
401 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
405 if ((ret = (swr_init(context))) < 0)
407 const char* err = av_err2str(ret);
408 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
413 if ((ret = swr_convert_frame(context, out, in)) < 0)
415 const char* err = av_err2str(ret);
416 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
423static BOOL ffmpeg_resample_frame(AVAudioResampleContext* WINPR_RESTRICT context,
424 AVFrame* WINPR_RESTRICT in, AVFrame* WINPR_RESTRICT out)
428 if (!avresample_is_open(context))
430 if ((ret = avresample_config(context, out, in)) < 0)
432 const char* err = av_err2str(ret);
433 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
437 if ((ret = (avresample_open(context))) < 0)
439 const char* err = av_err2str(ret);
440 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
445 if ((ret = avresample_convert_frame(context, out, in)) < 0)
447 const char* err = av_err2str(ret);
448 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
456static BOOL ffmpeg_encode_frame(AVCodecContext* WINPR_RESTRICT context, AVFrame* WINPR_RESTRICT in,
457 AVPacket* WINPR_RESTRICT packet,
wStream* WINPR_RESTRICT out)
459 if (in->format == AV_SAMPLE_FMT_FLTP)
461 uint8_t** pp = in->extended_data;
462#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100)
463 const int nr_channels = in->channels;
465 const int nr_channels = in->ch_layout.nb_channels;
468 for (
int y = 0; y < nr_channels; y++)
470 float* data = (
float*)pp[y];
471 for (
int x = 0; x < in->nb_samples; x++)
473 const float val1 = data[x];
476 else if (isinf(val1))
487 int ret = avcodec_send_frame(context, in);
491 const char* err = av_err2str(ret);
494 if (ret == AVERROR(EINVAL))
496 WLog_DBG(TAG,
"Error submitting the packet to the encoder %s [%d], ignoring", err, ret);
500 WLog_ERR(TAG,
"Error submitting the packet to the encoder %s [%d]", err, ret);
508 ret = avcodec_receive_packet(context, packet);
510 if ((ret == AVERROR(EAGAIN)) || (ret == AVERROR_EOF))
515 const char* err = av_err2str(ret);
516 WLog_ERR(TAG,
"Error during encoding %s [%d]", err, ret);
520 WINPR_ASSERT(packet->size >= 0);
521 if (!Stream_EnsureRemainingCapacity(out, (
size_t)packet->size))
524 Stream_Write(out, packet->data, (
size_t)packet->size);
525 av_packet_unref(packet);
531static BOOL ffmpeg_fill_frame(AVFrame* WINPR_RESTRICT frame,
533 const BYTE* WINPR_RESTRICT data,
size_t size)
536#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100)
537 frame->channels = inputFormat->nChannels;
538 frame->channel_layout = av_get_default_channel_layout(frame->channels);
540 av_channel_layout_default(&frame->ch_layout, inputFormat->nChannels);
542 WINPR_ASSERT(inputFormat->nSamplesPerSec <= INT_MAX);
543 frame->sample_rate = (int)inputFormat->nSamplesPerSec;
544 frame->format = ffmpeg_sample_format(inputFormat);
547 av_get_bytes_per_sample(WINPR_ASSERTING_INT_CAST(
enum AVSampleFormat, frame->format));
548 WINPR_ASSERT(bpp >= 0);
549 WINPR_ASSERT(size <= INT_MAX);
550 const size_t nb_samples = size / inputFormat->nChannels / (size_t)bpp;
551 frame->nb_samples = (int)nb_samples;
553 if ((ret = avcodec_fill_audio_frame(
554 frame, inputFormat->nChannels,
555 WINPR_ASSERTING_INT_CAST(
enum AVSampleFormat, frame->format), data, (
int)size, 1)) < 0)
557 const char* err = av_err2str(ret);
558 WLog_ERR(TAG,
"Error during audio frame fill %s [%d]", err, ret);
564#if defined(SWRESAMPLE_FOUND)
565static BOOL ffmpeg_decode(AVCodecContext* WINPR_RESTRICT dec_ctx, AVPacket* WINPR_RESTRICT pkt,
566 AVFrame* WINPR_RESTRICT frame, SwrContext* WINPR_RESTRICT resampleContext,
567 AVFrame* WINPR_RESTRICT resampled,
wStream* WINPR_RESTRICT out)
569static BOOL ffmpeg_decode(AVCodecContext* dec_ctx, AVPacket* pkt, AVFrame* frame,
570 AVAudioResampleContext* resampleContext, AVFrame* resampled,
wStream* out)
574 int ret = avcodec_send_packet(dec_ctx, pkt);
578 const char* err = av_err2str(ret);
579 WLog_ERR(TAG,
"Error submitting the packet to the decoder %s [%d]", err, ret);
586 ret = avcodec_receive_frame(dec_ctx, frame);
588 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
593 const char* err = av_err2str(ret);
594 WLog_ERR(TAG,
"Error during decoding %s [%d]", err, ret);
598#if defined(SWRESAMPLE_FOUND)
599 if (!swr_is_initialized(resampleContext))
601 if ((ret = swr_config_frame(resampleContext, resampled, frame)) < 0)
604 if (!avresample_is_open(resampleContext))
606 if ((ret = avresample_config(resampleContext, resampled, frame)) < 0)
609 const char* err = av_err2str(ret);
610 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
614#if defined(SWRESAMPLE_FOUND)
615 ret = (swr_init(resampleContext));
617 ret = (avresample_open(resampleContext));
621 const char* err = av_err2str(ret);
622 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
627#if defined(SWRESAMPLE_FOUND)
628 ret = swr_convert_frame(resampleContext, resampled, frame);
630 ret = avresample_convert_frame(resampleContext, resampled, frame);
634 const char* err = av_err2str(ret);
635 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
641#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
642 WINPR_ASSERT(resampled->ch_layout.nb_channels >= 0);
643 const size_t nrchannels = (size_t)resampled->ch_layout.nb_channels;
645 const size_t nrchannels = resampled->channels;
647 WINPR_ASSERT(resampled->nb_samples >= 0);
648 const size_t data_size = nrchannels * (size_t)resampled->nb_samples * 2ull;
649 if (!Stream_EnsureRemainingCapacity(out, data_size))
651 Stream_Write(out, resampled->data[0], data_size);
658BOOL freerdp_dsp_ffmpeg_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
660 enum AVCodecID
id = ffmpeg_get_avcodec(format);
662 if (ffmpeg_codec_is_filtered(
id, encode))
666 return avcodec_find_encoder(
id) !=
nullptr;
668 return avcodec_find_decoder(
id) !=
nullptr;
671FREERDP_DSP_CONTEXT* freerdp_dsp_ffmpeg_context_new(BOOL encode)
673 FREERDP_DSP_CONTEXT* context =
nullptr;
674#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
675 avcodec_register_all();
677 context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
682 if (!freerdp_dsp_common_context_init(&context->common, encode))
688 WINPR_PRAGMA_DIAG_PUSH
689 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
690 freerdp_dsp_ffmpeg_context_free(context);
691 WINPR_PRAGMA_DIAG_POP
695void freerdp_dsp_ffmpeg_context_free(FREERDP_DSP_CONTEXT* context)
699 ffmpeg_close_context(context);
700 freerdp_dsp_common_context_uninit(&context->common);
705BOOL freerdp_dsp_ffmpeg_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
708 if (!context || !targetFormat)
711 ffmpeg_close_context(context);
712 context->common.format = *targetFormat;
713 return ffmpeg_open_context(context);
716static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
717 const BYTE* WINPR_RESTRICT src,
size_t size,
719 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length,
725 if (!context || !data || !length || !dstFormat)
728 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
731 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
732 samples = size / bpp / srcFormat->nChannels;
734 *dstFormat = *srcFormat;
735 if (context->common.format.nChannels == srcFormat->nChannels)
742 Stream_ResetPosition(context->common.channelmix);
745 if (context->common.format.nChannels > srcFormat->nChannels)
747 switch (srcFormat->nChannels)
750 if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
753 for (
size_t x = 0; x < samples; x++)
755 for (
size_t y = 0; y < bpp; y++)
756 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
758 for (
size_t y = 0; y < bpp; y++)
759 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
762 Stream_SealLength(context->common.channelmix);
763 *data = Stream_Buffer(context->common.channelmix);
764 *length = Stream_Length(context->common.channelmix);
765 dstFormat->nChannels = 2;
770 WLog_WARN(TAG,
"[%s] unsupported source channel count %" PRIu16, __func__,
771 srcFormat->nChannels);
777 switch (srcFormat->nChannels)
780 if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
785 for (
size_t x = 0; x < samples; x++)
787 for (
size_t y = 0; y < bpp; y++)
788 Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
791 Stream_SealLength(context->common.channelmix);
792 *data = Stream_Buffer(context->common.channelmix);
793 *length = Stream_Length(context->common.channelmix);
794 dstFormat->nChannels = 1;
799 WLog_WARN(TAG,
"[%s] unsupported channel count %" PRIu16, __func__,
800 srcFormat->nChannels);
807BOOL freerdp_dsp_ffmpeg_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
809 const BYTE* WINPR_RESTRICT sdata,
size_t length,
814 if (!context || !format || !sdata || !out || !context->common.encoder)
821 const BYTE* data =
nullptr;
822 if (!freerdp_dsp_channel_mix(context, sdata, length, format, &data, &length, &fmt))
826 if (!ffmpeg_fill_frame(context->frame, format, data, length))
829 ffmpeg_setup_resample_frame(context, format);
831 if (!ffmpeg_resample_frame(context->rcontext, context->frame, context->resampled))
834 if (context->context->frame_size <= 0)
836 return ffmpeg_encode_frame(context->context, context->resampled, context->packet, out);
841 int rest = context->resampled->nb_samples;
845 int inSamples = rest;
847 if ((inSamples < 0) || (context->bufferedSamples > (UINT32)(INT_MAX - inSamples)))
850 if (inSamples + (
int)context->bufferedSamples > context->context->frame_size)
851 inSamples = context->context->frame_size - (int)context->bufferedSamples;
853#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
854 const int nrchannels = context->context->ch_layout.nb_channels;
856 const int nrchannels = context->context->channels;
859 av_samples_copy(context->buffered->extended_data, context->resampled->extended_data,
860 (
int)context->bufferedSamples, copied, inSamples, nrchannels,
861 context->context->sample_fmt);
866 context->bufferedSamples += (UINT32)inSamples;
868 if (context->context->frame_size <= (
int)context->bufferedSamples)
871 if (!ffmpeg_encode_frame(context->context, context->buffered, context->packet, out))
874 context->bufferedSamples = 0;
882BOOL freerdp_dsp_ffmpeg_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
884 const BYTE* WINPR_RESTRICT data,
size_t length,
887 if (!context || !srcFormat || !data || !out || context->common.encoder)
890#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
891 av_init_packet(context->packet);
893 context->packet->data = WINPR_CAST_CONST_PTR_AWAY(data, uint8_t*);
895 WINPR_ASSERT(length <= INT_MAX);
896 context->packet->size = (int)length;
897 return ffmpeg_decode(context->context, context->packet, context->frame, context->rcontext,
898 context->resampled, out);