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 int 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;
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;
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);
200 av_packet_free(&context->packet);
202 if (context->rcontext)
204#if defined(SWRESAMPLE_FOUND)
205 swr_free(&context->rcontext);
207 avresample_free(&context->rcontext);
211 context->id = AV_CODEC_ID_NONE;
212 context->codec = NULL;
213 context->isOpen = FALSE;
214 context->context = NULL;
215 context->frame = NULL;
216 context->resampled = NULL;
217 context->packet = NULL;
218 context->rcontext = NULL;
222static void ffmpeg_setup_resample_frame(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
225 if (context->resampled->buf[0] != NULL)
226 av_frame_unref(context->resampled);
228 if (context->common.encoder)
230 context->resampled->format = context->context->sample_fmt;
231 context->resampled->sample_rate = context->context->sample_rate;
235 context->resampled->format = AV_SAMPLE_FMT_S16;
237 WINPR_ASSERT(format->nSamplesPerSec <= INT_MAX);
238 context->resampled->sample_rate = (int)format->nSamplesPerSec;
241#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
242 av_channel_layout_default(&context->resampled->ch_layout, format->nChannels);
244 const int64_t layout = av_get_default_channel_layout(format->nChannels);
245 context->resampled->channel_layout = layout;
246 context->resampled->channels = format->nChannels;
250static BOOL ffmpeg_open_context(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
254 if (!context || context->isOpen)
261 context->id = ffmpeg_get_avcodec(format);
263 if (ffmpeg_codec_is_filtered(context->id, context->common.encoder))
266 if (context->common.encoder)
267 context->codec = avcodec_find_encoder(context->id);
269 context->codec = avcodec_find_decoder(context->id);
274 context->context = avcodec_alloc_context3(context->codec);
276 if (!context->context)
282 case AV_CODEC_ID_GSM_MS:
283 context->context->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL;
286 case AV_CODEC_ID_AAC:
287#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(60, 31, 102)
288 context->context->profile = FF_PROFILE_AAC_MAIN;
290 context->context->profile = AV_PROFILE_AAC_MAIN;
298 context->context->max_b_frames = 1;
299 context->context->delay = 0;
301#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
302 av_channel_layout_default(&context->context->ch_layout, format->nChannels);
304 context->context->channels = format->nChannels;
305 const int64_t layout = av_get_default_channel_layout(format->nChannels);
306 context->context->channel_layout = layout;
308 context->context->sample_rate = (int)format->nSamplesPerSec;
309 context->context->block_align = format->nBlockAlign;
310 context->context->bit_rate = format->nAvgBytesPerSec * 8LL;
311 context->context->sample_fmt = ffmpeg_sample_format(format);
312 context->context->time_base = av_make_q(1, context->context->sample_rate);
314 if ((ret = avcodec_open2(context->context, context->codec, NULL)) < 0)
316 const char* err = av_err2str(ret);
317 WLog_ERR(TAG,
"Error avcodec_open2 %s [%d]", err, ret);
321 context->packet = av_packet_alloc();
323 if (!context->packet)
326 context->frame = av_frame_alloc();
331 context->resampled = av_frame_alloc();
333 if (!context->resampled)
336 context->buffered = av_frame_alloc();
338 if (!context->buffered)
341#if defined(SWRESAMPLE_FOUND)
342 context->rcontext = swr_alloc();
344 context->rcontext = avresample_alloc_context();
347 if (!context->rcontext)
350#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
351 av_channel_layout_default(&context->frame->ch_layout, format->nChannels);
353 context->frame->channel_layout = layout;
354 context->frame->channels = format->nChannels;
356 WINPR_ASSERT(format->nSamplesPerSec <= INT_MAX);
357 context->frame->sample_rate = (int)format->nSamplesPerSec;
358 context->frame->format = AV_SAMPLE_FMT_S16;
360 ffmpeg_setup_resample_frame(context, format);
362 if (context->context->frame_size > 0)
364#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
365 ret = av_channel_layout_copy(&context->buffered->ch_layout, &context->resampled->ch_layout);
369 context->buffered->channel_layout = context->resampled->channel_layout;
370 context->buffered->channels = context->resampled->channels;
372 context->buffered->format = context->resampled->format;
373 context->buffered->nb_samples = context->context->frame_size;
375 ret = av_frame_get_buffer(context->buffered, 1);
380 context->isOpen = TRUE;
383 ffmpeg_close_context(context);
387#if defined(SWRESAMPLE_FOUND)
388static BOOL ffmpeg_resample_frame(SwrContext* WINPR_RESTRICT context, AVFrame* WINPR_RESTRICT in,
389 AVFrame* WINPR_RESTRICT out)
393 if (!swr_is_initialized(context))
395 if ((ret = swr_config_frame(context, out, in)) < 0)
397 const char* err = av_err2str(ret);
398 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
402 if ((ret = (swr_init(context))) < 0)
404 const char* err = av_err2str(ret);
405 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
410 if ((ret = swr_convert_frame(context, out, in)) < 0)
412 const char* err = av_err2str(ret);
413 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
420static BOOL ffmpeg_resample_frame(AVAudioResampleContext* WINPR_RESTRICT context,
421 AVFrame* WINPR_RESTRICT in, AVFrame* WINPR_RESTRICT out)
425 if (!avresample_is_open(context))
427 if ((ret = avresample_config(context, out, in)) < 0)
429 const char* err = av_err2str(ret);
430 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
434 if ((ret = (avresample_open(context))) < 0)
436 const char* err = av_err2str(ret);
437 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
442 if ((ret = avresample_convert_frame(context, out, in)) < 0)
444 const char* err = av_err2str(ret);
445 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
453static BOOL ffmpeg_encode_frame(AVCodecContext* WINPR_RESTRICT context, AVFrame* WINPR_RESTRICT in,
454 AVPacket* WINPR_RESTRICT packet,
wStream* WINPR_RESTRICT out)
456 if (in->format == AV_SAMPLE_FMT_FLTP)
458 uint8_t** pp = in->extended_data;
459#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100)
460 const int nr_channels = in->channels;
462 const int nr_channels = in->ch_layout.nb_channels;
465 for (
int y = 0; y < nr_channels; y++)
467 float* data = (
float*)pp[y];
468 for (
int x = 0; x < in->nb_samples; x++)
470 const float val1 = data[x];
473 else if (isinf(val1))
484 int ret = avcodec_send_frame(context, in);
488 const char* err = av_err2str(ret);
491 if (ret == AVERROR(EINVAL))
493 WLog_DBG(TAG,
"Error submitting the packet to the encoder %s [%d], ignoring", err, ret);
497 WLog_ERR(TAG,
"Error submitting the packet to the encoder %s [%d]", err, ret);
505 ret = avcodec_receive_packet(context, packet);
507 if ((ret == AVERROR(EAGAIN)) || (ret == AVERROR_EOF))
512 const char* err = av_err2str(ret);
513 WLog_ERR(TAG,
"Error during encoding %s [%d]", err, ret);
517 WINPR_ASSERT(packet->size >= 0);
518 if (!Stream_EnsureRemainingCapacity(out, (
size_t)packet->size))
521 Stream_Write(out, packet->data, (
size_t)packet->size);
522 av_packet_unref(packet);
528static BOOL ffmpeg_fill_frame(AVFrame* WINPR_RESTRICT frame,
530 const BYTE* WINPR_RESTRICT data,
size_t size)
533#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100)
534 frame->channels = inputFormat->nChannels;
535 frame->channel_layout = av_get_default_channel_layout(frame->channels);
537 av_channel_layout_default(&frame->ch_layout, inputFormat->nChannels);
539 WINPR_ASSERT(inputFormat->nSamplesPerSec <= INT_MAX);
540 frame->sample_rate = (int)inputFormat->nSamplesPerSec;
541 frame->format = ffmpeg_sample_format(inputFormat);
543 const int bpp = av_get_bytes_per_sample(frame->format);
544 WINPR_ASSERT(bpp >= 0);
545 WINPR_ASSERT(size <= INT_MAX);
546 const size_t nb_samples = size / inputFormat->nChannels / (size_t)bpp;
547 frame->nb_samples = (int)nb_samples;
549 if ((ret = avcodec_fill_audio_frame(frame, inputFormat->nChannels, frame->format, data,
552 const char* err = av_err2str(ret);
553 WLog_ERR(TAG,
"Error during audio frame fill %s [%d]", err, ret);
559#if defined(SWRESAMPLE_FOUND)
560static BOOL ffmpeg_decode(AVCodecContext* WINPR_RESTRICT dec_ctx, AVPacket* WINPR_RESTRICT pkt,
561 AVFrame* WINPR_RESTRICT frame, SwrContext* WINPR_RESTRICT resampleContext,
562 AVFrame* WINPR_RESTRICT resampled,
wStream* WINPR_RESTRICT out)
564static BOOL ffmpeg_decode(AVCodecContext* dec_ctx, AVPacket* pkt, AVFrame* frame,
565 AVAudioResampleContext* resampleContext, AVFrame* resampled,
wStream* out)
569 int ret = avcodec_send_packet(dec_ctx, pkt);
573 const char* err = av_err2str(ret);
574 WLog_ERR(TAG,
"Error submitting the packet to the decoder %s [%d]", err, ret);
581 ret = avcodec_receive_frame(dec_ctx, frame);
583 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
588 const char* err = av_err2str(ret);
589 WLog_ERR(TAG,
"Error during decoding %s [%d]", err, ret);
593#if defined(SWRESAMPLE_FOUND)
594 if (!swr_is_initialized(resampleContext))
596 if ((ret = swr_config_frame(resampleContext, resampled, frame)) < 0)
599 if (!avresample_is_open(resampleContext))
601 if ((ret = avresample_config(resampleContext, resampled, frame)) < 0)
604 const char* err = av_err2str(ret);
605 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
609#if defined(SWRESAMPLE_FOUND)
610 ret = (swr_init(resampleContext));
612 ret = (avresample_open(resampleContext));
616 const char* err = av_err2str(ret);
617 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
622#if defined(SWRESAMPLE_FOUND)
623 ret = swr_convert_frame(resampleContext, resampled, frame);
625 ret = avresample_convert_frame(resampleContext, resampled, frame);
629 const char* err = av_err2str(ret);
630 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
636#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
637 WINPR_ASSERT(resampled->ch_layout.nb_channels >= 0);
638 const size_t nrchannels = (size_t)resampled->ch_layout.nb_channels;
640 const size_t nrchannels = resampled->channels;
642 WINPR_ASSERT(resampled->nb_samples >= 0);
643 const size_t data_size = nrchannels * (size_t)resampled->nb_samples * 2ull;
644 if (!Stream_EnsureRemainingCapacity(out, data_size))
646 Stream_Write(out, resampled->data[0], data_size);
653BOOL freerdp_dsp_ffmpeg_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
655 enum AVCodecID
id = ffmpeg_get_avcodec(format);
657 if (ffmpeg_codec_is_filtered(
id, encode))
661 return avcodec_find_encoder(
id) != NULL;
663 return avcodec_find_decoder(
id) != NULL;
666FREERDP_DSP_CONTEXT* freerdp_dsp_ffmpeg_context_new(BOOL encode)
668 FREERDP_DSP_CONTEXT* context = NULL;
669#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
670 avcodec_register_all();
672 context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
677 if (!freerdp_dsp_common_context_init(&context->common, encode))
683 WINPR_PRAGMA_DIAG_PUSH
684 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
685 freerdp_dsp_ffmpeg_context_free(context);
686 WINPR_PRAGMA_DIAG_POP
690void freerdp_dsp_ffmpeg_context_free(FREERDP_DSP_CONTEXT* context)
694 ffmpeg_close_context(context);
695 freerdp_dsp_common_context_uninit(&context->common);
700BOOL freerdp_dsp_ffmpeg_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
703 if (!context || !targetFormat)
706 ffmpeg_close_context(context);
707 context->common.format = *targetFormat;
708 return ffmpeg_open_context(context);
711static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
712 const BYTE* WINPR_RESTRICT src,
size_t size,
714 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length,
720 if (!context || !data || !length || !dstFormat)
723 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
726 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
727 samples = size / bpp / srcFormat->nChannels;
729 *dstFormat = *srcFormat;
730 if (context->common.format.nChannels == srcFormat->nChannels)
737 Stream_SetPosition(context->common.channelmix, 0);
740 if (context->common.format.nChannels > srcFormat->nChannels)
742 switch (srcFormat->nChannels)
745 if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
748 for (
size_t x = 0; x < samples; x++)
750 for (
size_t y = 0; y < bpp; y++)
751 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
753 for (
size_t y = 0; y < bpp; y++)
754 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
757 Stream_SealLength(context->common.channelmix);
758 *data = Stream_Buffer(context->common.channelmix);
759 *length = Stream_Length(context->common.channelmix);
760 dstFormat->nChannels = 2;
765 WLog_WARN(TAG,
"[%s] unsupported source channel count %" PRIu16, __func__,
766 srcFormat->nChannels);
772 switch (srcFormat->nChannels)
775 if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
780 for (
size_t x = 0; x < samples; x++)
782 for (
size_t y = 0; y < bpp; y++)
783 Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
786 Stream_SealLength(context->common.channelmix);
787 *data = Stream_Buffer(context->common.channelmix);
788 *length = Stream_Length(context->common.channelmix);
789 dstFormat->nChannels = 1;
794 WLog_WARN(TAG,
"[%s] unsupported channel count %" PRIu16, __func__,
795 srcFormat->nChannels);
802BOOL freerdp_dsp_ffmpeg_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
804 const BYTE* WINPR_RESTRICT sdata,
size_t length,
809 if (!context || !format || !sdata || !out || !context->common.encoder)
812 if (!context || !sdata || !out)
819 const BYTE* data = NULL;
820 if (!freerdp_dsp_channel_mix(context, sdata, length, format, &data, &length, &fmt))
824 if (!ffmpeg_fill_frame(context->frame, format, data, length))
827 ffmpeg_setup_resample_frame(context, format);
829 if (!ffmpeg_resample_frame(context->rcontext, context->frame, context->resampled))
832 if (context->context->frame_size <= 0)
834 return ffmpeg_encode_frame(context->context, context->resampled, context->packet, out);
839 int rest = context->resampled->nb_samples;
843 int inSamples = rest;
845 if ((inSamples < 0) || (context->bufferedSamples > (UINT32)(INT_MAX - inSamples)))
848 if (inSamples + (
int)context->bufferedSamples > context->context->frame_size)
849 inSamples = context->context->frame_size - (int)context->bufferedSamples;
851#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
852 const int nrchannels = context->context->ch_layout.nb_channels;
854 const int nrchannels = context->context->channels;
857 av_samples_copy(context->buffered->extended_data, context->resampled->extended_data,
858 (
int)context->bufferedSamples, copied, inSamples, nrchannels,
859 context->context->sample_fmt);
864 context->bufferedSamples += (UINT32)inSamples;
866 if (context->context->frame_size <= (
int)context->bufferedSamples)
869 if (!ffmpeg_encode_frame(context->context, context->buffered, context->packet, out))
872 context->bufferedSamples = 0;
880BOOL freerdp_dsp_ffmpeg_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
882 const BYTE* WINPR_RESTRICT data,
size_t length,
885 if (!context || !srcFormat || !data || !out || context->common.encoder)
888#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
889 av_init_packet(context->packet);
891 context->packet->data = WINPR_CAST_CONST_PTR_AWAY(data, uint8_t*);
893 WINPR_ASSERT(length <= INT_MAX);
894 context->packet->size = (int)length;
895 return ffmpeg_decode(context->context, context->packet, context->frame, context->rcontext,
896 context->resampled, out);