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 BOOL ffmpeg_open_context(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
226 if (!context || context->isOpen)
233 context->id = ffmpeg_get_avcodec(format);
235 if (ffmpeg_codec_is_filtered(context->id, context->common.encoder))
238 if (context->common.encoder)
239 context->codec = avcodec_find_encoder(context->id);
241 context->codec = avcodec_find_decoder(context->id);
246 context->context = avcodec_alloc_context3(context->codec);
248 if (!context->context)
254 case AV_CODEC_ID_GSM_MS:
255 context->context->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL;
258 case AV_CODEC_ID_AAC:
259 context->context->profile = FF_PROFILE_AAC_MAIN;
266 context->context->max_b_frames = 1;
267 context->context->delay = 0;
269#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
270 av_channel_layout_default(&context->context->ch_layout, format->nChannels);
272 context->context->channels = format->nChannels;
273 const int64_t layout = av_get_default_channel_layout(format->nChannels);
274 context->context->channel_layout = layout;
276 context->context->sample_rate = (int)format->nSamplesPerSec;
277 context->context->block_align = format->nBlockAlign;
278 context->context->bit_rate = format->nAvgBytesPerSec * 8LL;
279 context->context->sample_fmt = ffmpeg_sample_format(format);
280 context->context->time_base = av_make_q(1, context->context->sample_rate);
282 if ((ret = avcodec_open2(context->context, context->codec, NULL)) < 0)
284 const char* err = av_err2str(ret);
285 WLog_ERR(TAG,
"Error avcodec_open2 %s [%d]", err, ret);
289 context->packet = av_packet_alloc();
291 if (!context->packet)
294 context->frame = av_frame_alloc();
299 context->resampled = av_frame_alloc();
301 if (!context->resampled)
304 context->buffered = av_frame_alloc();
306 if (!context->buffered)
309#if defined(SWRESAMPLE_FOUND)
310 context->rcontext = swr_alloc();
312 context->rcontext = avresample_alloc_context();
315 if (!context->rcontext)
318#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
319 av_channel_layout_default(&context->frame->ch_layout, format->nChannels);
321 context->frame->channel_layout = layout;
322 context->frame->channels = format->nChannels;
324 WINPR_ASSERT(format->nSamplesPerSec <= INT_MAX);
325 context->frame->sample_rate = (int)format->nSamplesPerSec;
326 context->frame->format = AV_SAMPLE_FMT_S16;
328 if (context->common.encoder)
330 context->resampled->format = context->context->sample_fmt;
331 context->resampled->sample_rate = context->context->sample_rate;
335 context->resampled->format = AV_SAMPLE_FMT_S16;
337 WINPR_ASSERT(format->nSamplesPerSec <= INT_MAX);
338 context->resampled->sample_rate = (int)format->nSamplesPerSec;
341#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
342 av_channel_layout_default(&context->resampled->ch_layout, format->nChannels);
344 context->resampled->channel_layout = layout;
345 context->resampled->channels = format->nChannels;
348 if (context->context->frame_size > 0)
350#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
351 ret = av_channel_layout_copy(&context->buffered->ch_layout, &context->resampled->ch_layout);
355 context->buffered->channel_layout = context->resampled->channel_layout;
356 context->buffered->channels = context->resampled->channels;
358 context->buffered->format = context->resampled->format;
359 context->buffered->nb_samples = context->context->frame_size;
361 ret = av_frame_get_buffer(context->buffered, 1);
366 context->isOpen = TRUE;
369 ffmpeg_close_context(context);
373#if defined(SWRESAMPLE_FOUND)
374static BOOL ffmpeg_resample_frame(SwrContext* WINPR_RESTRICT context, AVFrame* WINPR_RESTRICT in,
375 AVFrame* WINPR_RESTRICT out)
379 if (!swr_is_initialized(context))
381 if ((ret = swr_config_frame(context, out, in)) < 0)
383 const char* err = av_err2str(ret);
384 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
388 if ((ret = (swr_init(context))) < 0)
390 const char* err = av_err2str(ret);
391 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
396 if ((ret = swr_convert_frame(context, out, in)) < 0)
398 const char* err = av_err2str(ret);
399 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
406static BOOL ffmpeg_resample_frame(AVAudioResampleContext* WINPR_RESTRICT context,
407 AVFrame* WINPR_RESTRICT in, AVFrame* WINPR_RESTRICT out)
411 if (!avresample_is_open(context))
413 if ((ret = avresample_config(context, out, in)) < 0)
415 const char* err = av_err2str(ret);
416 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
420 if ((ret = (avresample_open(context))) < 0)
422 const char* err = av_err2str(ret);
423 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
428 if ((ret = avresample_convert_frame(context, out, in)) < 0)
430 const char* err = av_err2str(ret);
431 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
439static BOOL ffmpeg_encode_frame(AVCodecContext* WINPR_RESTRICT context, AVFrame* WINPR_RESTRICT in,
440 AVPacket* WINPR_RESTRICT packet,
wStream* WINPR_RESTRICT out)
442 if (in->format == AV_SAMPLE_FMT_FLTP)
444 uint8_t** pp = in->extended_data;
445#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100)
446 const int nr_channels = in->channels;
448 const int nr_channels = in->ch_layout.nb_channels;
451 for (
int y = 0; y < nr_channels; y++)
453 float* data = (
float*)pp[y];
454 for (
int x = 0; x < in->nb_samples; x++)
456 const float val1 = data[x];
459 else if (isinf(val1))
470 int ret = avcodec_send_frame(context, in);
474 const char* err = av_err2str(ret);
477 if (ret == AVERROR(EINVAL))
479 WLog_DBG(TAG,
"Error submitting the packet to the encoder %s [%d], ignoring", err, ret);
483 WLog_ERR(TAG,
"Error submitting the packet to the encoder %s [%d]", err, ret);
491 ret = avcodec_receive_packet(context, packet);
493 if ((ret == AVERROR(EAGAIN)) || (ret == AVERROR_EOF))
498 const char* err = av_err2str(ret);
499 WLog_ERR(TAG,
"Error during encoding %s [%d]", err, ret);
503 WINPR_ASSERT(packet->size >= 0);
504 if (!Stream_EnsureRemainingCapacity(out, (
size_t)packet->size))
507 Stream_Write(out, packet->data, (
size_t)packet->size);
508 av_packet_unref(packet);
514static BOOL ffmpeg_fill_frame(AVFrame* WINPR_RESTRICT frame,
516 const BYTE* WINPR_RESTRICT data,
size_t size)
519#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(57, 28, 100)
520 frame->channels = inputFormat->nChannels;
521 frame->channel_layout = av_get_default_channel_layout(frame->channels);
523 av_channel_layout_default(&frame->ch_layout, inputFormat->nChannels);
525 WINPR_ASSERT(inputFormat->nSamplesPerSec <= INT_MAX);
526 frame->sample_rate = (int)inputFormat->nSamplesPerSec;
527 frame->format = ffmpeg_sample_format(inputFormat);
529 const int bpp = av_get_bytes_per_sample(frame->format);
530 WINPR_ASSERT(bpp >= 0);
531 WINPR_ASSERT(size <= INT_MAX);
532 const size_t nb_samples = size / inputFormat->nChannels / (size_t)bpp;
533 frame->nb_samples = (int)nb_samples;
535 if ((ret = avcodec_fill_audio_frame(frame, inputFormat->nChannels, frame->format, data,
538 const char* err = av_err2str(ret);
539 WLog_ERR(TAG,
"Error during audio frame fill %s [%d]", err, ret);
545#if defined(SWRESAMPLE_FOUND)
546static BOOL ffmpeg_decode(AVCodecContext* WINPR_RESTRICT dec_ctx, AVPacket* WINPR_RESTRICT pkt,
547 AVFrame* WINPR_RESTRICT frame, SwrContext* WINPR_RESTRICT resampleContext,
548 AVFrame* WINPR_RESTRICT resampled,
wStream* WINPR_RESTRICT out)
550static BOOL ffmpeg_decode(AVCodecContext* dec_ctx, AVPacket* pkt, AVFrame* frame,
551 AVAudioResampleContext* resampleContext, AVFrame* resampled,
wStream* out)
555 int ret = avcodec_send_packet(dec_ctx, pkt);
559 const char* err = av_err2str(ret);
560 WLog_ERR(TAG,
"Error submitting the packet to the decoder %s [%d]", err, ret);
567 ret = avcodec_receive_frame(dec_ctx, frame);
569 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
574 const char* err = av_err2str(ret);
575 WLog_ERR(TAG,
"Error during decoding %s [%d]", err, ret);
579#if defined(SWRESAMPLE_FOUND)
580 if (!swr_is_initialized(resampleContext))
582 if ((ret = swr_config_frame(resampleContext, resampled, frame)) < 0)
585 if (!avresample_is_open(resampleContext))
587 if ((ret = avresample_config(resampleContext, resampled, frame)) < 0)
590 const char* err = av_err2str(ret);
591 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
595#if defined(SWRESAMPLE_FOUND)
596 ret = (swr_init(resampleContext));
598 ret = (avresample_open(resampleContext));
602 const char* err = av_err2str(ret);
603 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
608#if defined(SWRESAMPLE_FOUND)
609 ret = swr_convert_frame(resampleContext, resampled, frame);
611 ret = avresample_convert_frame(resampleContext, resampled, frame);
615 const char* err = av_err2str(ret);
616 WLog_ERR(TAG,
"Error during resampling %s [%d]", err, ret);
622#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
623 WINPR_ASSERT(resampled->ch_layout.nb_channels >= 0);
624 const size_t nrchannels = (size_t)resampled->ch_layout.nb_channels;
626 const size_t nrchannels = resampled->channels;
628 WINPR_ASSERT(resampled->nb_samples >= 0);
629 const size_t data_size = nrchannels * (size_t)resampled->nb_samples * 2ull;
630 if (!Stream_EnsureRemainingCapacity(out, data_size))
632 Stream_Write(out, resampled->data[0], data_size);
639BOOL freerdp_dsp_ffmpeg_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
641 enum AVCodecID
id = ffmpeg_get_avcodec(format);
643 if (ffmpeg_codec_is_filtered(
id, encode))
647 return avcodec_find_encoder(
id) != NULL;
649 return avcodec_find_decoder(
id) != NULL;
652FREERDP_DSP_CONTEXT* freerdp_dsp_ffmpeg_context_new(BOOL encode)
654 FREERDP_DSP_CONTEXT* context = NULL;
655#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
656 avcodec_register_all();
658 context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
663 if (!freerdp_dsp_common_context_init(&context->common, encode))
669 WINPR_PRAGMA_DIAG_PUSH
670 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
671 freerdp_dsp_ffmpeg_context_free(context);
672 WINPR_PRAGMA_DIAG_POP
676void freerdp_dsp_ffmpeg_context_free(FREERDP_DSP_CONTEXT* context)
680 ffmpeg_close_context(context);
681 freerdp_dsp_common_context_uninit(&context->common);
686BOOL freerdp_dsp_ffmpeg_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
689 if (!context || !targetFormat)
692 ffmpeg_close_context(context);
693 context->common.format = *targetFormat;
694 return ffmpeg_open_context(context);
697static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
698 const BYTE* WINPR_RESTRICT src,
size_t size,
700 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length,
706 if (!context || !data || !length || !dstFormat)
709 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
712 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
713 samples = size / bpp / srcFormat->nChannels;
715 *dstFormat = *srcFormat;
716 if (context->common.format.nChannels == srcFormat->nChannels)
723 Stream_SetPosition(context->common.channelmix, 0);
726 if (context->common.format.nChannels > srcFormat->nChannels)
728 switch (srcFormat->nChannels)
731 if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
734 for (
size_t x = 0; x < samples; x++)
736 for (
size_t y = 0; y < bpp; y++)
737 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
739 for (
size_t y = 0; y < bpp; y++)
740 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
743 Stream_SealLength(context->common.channelmix);
744 *data = Stream_Buffer(context->common.channelmix);
745 *length = Stream_Length(context->common.channelmix);
746 dstFormat->nChannels = 2;
751 WLog_WARN(TAG,
"[%s] unsupported source channel count %" PRIu16, __func__,
752 srcFormat->nChannels);
758 switch (srcFormat->nChannels)
761 if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
766 for (
size_t x = 0; x < samples; x++)
768 for (
size_t y = 0; y < bpp; y++)
769 Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
772 Stream_SealLength(context->common.channelmix);
773 *data = Stream_Buffer(context->common.channelmix);
774 *length = Stream_Length(context->common.channelmix);
775 dstFormat->nChannels = 1;
780 WLog_WARN(TAG,
"[%s] unsupported channel count %" PRIu16, __func__,
781 srcFormat->nChannels);
788BOOL freerdp_dsp_ffmpeg_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
790 const BYTE* WINPR_RESTRICT sdata,
size_t length,
795 if (!context || !format || !sdata || !out || !context->common.encoder)
798 if (!context || !sdata || !out)
805 const BYTE* data = NULL;
806 if (!freerdp_dsp_channel_mix(context, sdata, length, format, &data, &length, &fmt))
810 if (!ffmpeg_fill_frame(context->frame, format, data, length))
814 if (!ffmpeg_resample_frame(context->rcontext, context->frame, context->resampled))
817 if (context->context->frame_size <= 0)
819 return ffmpeg_encode_frame(context->context, context->resampled, context->packet, out);
824 int rest = context->resampled->nb_samples;
828 int inSamples = rest;
830 if ((inSamples < 0) || (context->bufferedSamples > (UINT32)(INT_MAX - inSamples)))
833 if (inSamples + (
int)context->bufferedSamples > context->context->frame_size)
834 inSamples = context->context->frame_size - (int)context->bufferedSamples;
836#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
837 const int nrchannels = context->context->ch_layout.nb_channels;
839 const int nrchannels = context->context->channels;
842 av_samples_copy(context->buffered->extended_data, context->resampled->extended_data,
843 (
int)context->bufferedSamples, copied, inSamples, nrchannels,
844 context->context->sample_fmt);
849 context->bufferedSamples += (UINT32)inSamples;
851 if (context->context->frame_size <= (
int)context->bufferedSamples)
854 if (!ffmpeg_encode_frame(context->context, context->buffered, context->packet, out))
857 context->bufferedSamples = 0;
865BOOL freerdp_dsp_ffmpeg_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
867 const BYTE* WINPR_RESTRICT data,
size_t length,
870 if (!context || !srcFormat || !data || !out || context->common.encoder)
873#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
874 av_init_packet(context->packet);
876 context->packet->data = WINPR_CAST_CONST_PTR_AWAY(data, uint8_t*);
878 WINPR_ASSERT(length <= INT_MAX);
879 context->packet->size = (int)length;
880 return ffmpeg_decode(context->context, context->packet, context->frame, context->rcontext,
881 context->resampled, out);