FreeRDP
Loading...
Searching...
No Matches
dsp_ffmpeg.c
1
21#include <freerdp/config.h>
22
23#include <freerdp/log.h>
24
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>
32#else
33#error "libswresample or libavresample required"
34#endif
35
36#include "dsp.h"
37#include "dsp_ffmpeg.h"
38
39#define TAG FREERDP_TAG("dsp.ffmpeg")
40
41struct S_FREERDP_DSP_CONTEXT
42{
44
45 BOOL isOpen;
46
47 UINT32 bufferedSamples;
48
49 enum AVCodecID id;
50 const AVCodec* codec;
51 AVCodecContext* context;
52 AVFrame* frame;
53 AVFrame* resampled;
54 AVFrame* buffered;
55 AVPacket* packet;
56#if defined(SWRESAMPLE_FOUND)
57 SwrContext* rcontext;
58#else
59 AVAudioResampleContext* rcontext;
60#endif
61};
62
63static BOOL ffmpeg_codec_is_filtered(enum AVCodecID id, WINPR_ATTR_UNUSED BOOL encoder)
64{
65 switch (id)
66 {
67#if !defined(WITH_DSP_EXPERIMENTAL)
68
69 case AV_CODEC_ID_ADPCM_IMA_OKI:
70 case AV_CODEC_ID_MP3:
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:
76 return TRUE;
77#endif
78
79 case AV_CODEC_ID_NONE:
80 return TRUE;
81
82 case AV_CODEC_ID_AAC:
83 case AV_CODEC_ID_AAC_LATM:
84 return FALSE;
85
86 default:
87 return FALSE;
88 }
89}
90
91static enum AVCodecID ffmpeg_get_avcodec(const AUDIO_FORMAT* WINPR_RESTRICT format)
92{
93 if (!format)
94 return AV_CODEC_ID_NONE;
95
96 switch (format->wFormatTag)
97 {
98 case WAVE_FORMAT_UNKNOWN:
99 return AV_CODEC_ID_NONE;
100
101 case WAVE_FORMAT_PCM:
102 switch (format->wBitsPerSample)
103 {
104 case 16:
105 return AV_CODEC_ID_PCM_U16LE;
106
107 case 8:
108 return AV_CODEC_ID_PCM_U8;
109
110 default:
111 return AV_CODEC_ID_NONE;
112 }
113
114 case WAVE_FORMAT_DVI_ADPCM:
115 return AV_CODEC_ID_ADPCM_IMA_OKI;
116
117 case WAVE_FORMAT_ADPCM:
118 return AV_CODEC_ID_ADPCM_MS;
119
120 case WAVE_FORMAT_ALAW:
121 return AV_CODEC_ID_PCM_ALAW;
122
123 case WAVE_FORMAT_MULAW:
124 return AV_CODEC_ID_PCM_MULAW;
125
126 case WAVE_FORMAT_GSM610:
127 return AV_CODEC_ID_GSM_MS;
128
129 case WAVE_FORMAT_MSG723:
130 return AV_CODEC_ID_G723_1;
131
132 case WAVE_FORMAT_AAC_MS:
133 return AV_CODEC_ID_AAC;
134
135 case WAVE_FORMAT_OPUS:
136 return AV_CODEC_ID_OPUS;
137
138 default:
139 return AV_CODEC_ID_NONE;
140 }
141}
142
143static int ffmpeg_sample_format(const AUDIO_FORMAT* WINPR_RESTRICT format)
144{
145 switch (format->wFormatTag)
146 {
147 case WAVE_FORMAT_PCM:
148 switch (format->wBitsPerSample)
149 {
150 case 8:
151 return AV_SAMPLE_FMT_U8;
152
153 case 16:
154 return AV_SAMPLE_FMT_S16;
155
156 default:
157 return FALSE;
158 }
159
160 case WAVE_FORMAT_DVI_ADPCM:
161 case WAVE_FORMAT_ADPCM:
162 return AV_SAMPLE_FMT_S16P;
163
164 case WAVE_FORMAT_MPEGLAYER3:
165 case WAVE_FORMAT_AAC_MS:
166 return AV_SAMPLE_FMT_FLTP;
167
168 case WAVE_FORMAT_OPUS:
169 return AV_SAMPLE_FMT_S16;
170
171 case WAVE_FORMAT_MSG723:
172 case WAVE_FORMAT_GSM610:
173 return AV_SAMPLE_FMT_S16P;
174
175 case WAVE_FORMAT_ALAW:
176 return AV_SAMPLE_FMT_S16;
177
178 default:
179 return FALSE;
180 }
181}
182
183static void ffmpeg_close_context(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
184{
185 if (context)
186 {
187 if (context->context)
188 avcodec_free_context(&context->context);
189
190 if (context->frame)
191 av_frame_free(&context->frame);
192
193 if (context->resampled)
194 av_frame_free(&context->resampled);
195
196 if (context->buffered)
197 av_frame_free(&context->buffered);
198
199 if (context->packet)
200 av_packet_free(&context->packet);
201
202 if (context->rcontext)
203 {
204#if defined(SWRESAMPLE_FOUND)
205 swr_free(&context->rcontext);
206#else
207 avresample_free(&context->rcontext);
208#endif
209 }
210
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;
219 }
220}
221
222static void ffmpeg_setup_resample_frame(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
223 const AUDIO_FORMAT* WINPR_RESTRICT format)
224{
225 if (context->resampled->buf[0] != NULL)
226 av_frame_unref(context->resampled);
227
228 if (context->common.encoder)
229 {
230 context->resampled->format = context->context->sample_fmt;
231 context->resampled->sample_rate = context->context->sample_rate;
232 }
233 else
234 {
235 context->resampled->format = AV_SAMPLE_FMT_S16;
236
237 WINPR_ASSERT(format->nSamplesPerSec <= INT_MAX);
238 context->resampled->sample_rate = (int)format->nSamplesPerSec;
239 }
240
241#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
242 av_channel_layout_default(&context->resampled->ch_layout, format->nChannels);
243#else
244 const int64_t layout = av_get_default_channel_layout(format->nChannels);
245 context->resampled->channel_layout = layout;
246 context->resampled->channels = format->nChannels;
247#endif
248}
249
250static BOOL ffmpeg_open_context(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
251{
252 int ret = 0;
253
254 if (!context || context->isOpen)
255 return FALSE;
256
257 const AUDIO_FORMAT* format = &context->common.format;
258
259 if (!format)
260 return FALSE;
261 context->id = ffmpeg_get_avcodec(format);
262
263 if (ffmpeg_codec_is_filtered(context->id, context->common.encoder))
264 goto fail;
265
266 if (context->common.encoder)
267 context->codec = avcodec_find_encoder(context->id);
268 else
269 context->codec = avcodec_find_decoder(context->id);
270
271 if (!context->codec)
272 goto fail;
273
274 context->context = avcodec_alloc_context3(context->codec);
275
276 if (!context->context)
277 goto fail;
278
279 switch (context->id)
280 {
281 /* We need support for multichannel and sample rates != 8000 */
282 case AV_CODEC_ID_GSM_MS:
283 context->context->strict_std_compliance = FF_COMPLIANCE_UNOFFICIAL;
284 break;
285
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;
289#else
290 context->context->profile = AV_PROFILE_AAC_MAIN;
291#endif
292 break;
293
294 default:
295 break;
296 }
297
298 context->context->max_b_frames = 1;
299 context->context->delay = 0;
300
301#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
302 av_channel_layout_default(&context->context->ch_layout, format->nChannels);
303#else
304 context->context->channels = format->nChannels;
305 const int64_t layout = av_get_default_channel_layout(format->nChannels);
306 context->context->channel_layout = layout;
307#endif
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);
313
314 if ((ret = avcodec_open2(context->context, context->codec, NULL)) < 0)
315 {
316 const char* err = av_err2str(ret);
317 WLog_ERR(TAG, "Error avcodec_open2 %s [%d]", err, ret);
318 goto fail;
319 }
320
321 context->packet = av_packet_alloc();
322
323 if (!context->packet)
324 goto fail;
325
326 context->frame = av_frame_alloc();
327
328 if (!context->frame)
329 goto fail;
330
331 context->resampled = av_frame_alloc();
332
333 if (!context->resampled)
334 goto fail;
335
336 context->buffered = av_frame_alloc();
337
338 if (!context->buffered)
339 goto fail;
340
341#if defined(SWRESAMPLE_FOUND)
342 context->rcontext = swr_alloc();
343#else
344 context->rcontext = avresample_alloc_context();
345#endif
346
347 if (!context->rcontext)
348 goto fail;
349
350#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
351 av_channel_layout_default(&context->frame->ch_layout, format->nChannels);
352#else
353 context->frame->channel_layout = layout;
354 context->frame->channels = format->nChannels;
355#endif
356 WINPR_ASSERT(format->nSamplesPerSec <= INT_MAX);
357 context->frame->sample_rate = (int)format->nSamplesPerSec;
358 context->frame->format = AV_SAMPLE_FMT_S16;
359
360 ffmpeg_setup_resample_frame(context, format);
361
362 if (context->context->frame_size > 0)
363 {
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);
366 if (ret != 0)
367 goto fail;
368#else
369 context->buffered->channel_layout = context->resampled->channel_layout;
370 context->buffered->channels = context->resampled->channels;
371#endif
372 context->buffered->format = context->resampled->format;
373 context->buffered->nb_samples = context->context->frame_size;
374
375 ret = av_frame_get_buffer(context->buffered, 1);
376 if (ret < 0)
377 goto fail;
378 }
379
380 context->isOpen = TRUE;
381 return TRUE;
382fail:
383 ffmpeg_close_context(context);
384 return FALSE;
385}
386
387#if defined(SWRESAMPLE_FOUND)
388static BOOL ffmpeg_resample_frame(SwrContext* WINPR_RESTRICT context, AVFrame* WINPR_RESTRICT in,
389 AVFrame* WINPR_RESTRICT out)
390{
391 int ret = 0;
392
393 if (!swr_is_initialized(context))
394 {
395 if ((ret = swr_config_frame(context, out, in)) < 0)
396 {
397 const char* err = av_err2str(ret);
398 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
399 return FALSE;
400 }
401
402 if ((ret = (swr_init(context))) < 0)
403 {
404 const char* err = av_err2str(ret);
405 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
406 return FALSE;
407 }
408 }
409
410 if ((ret = swr_convert_frame(context, out, in)) < 0)
411 {
412 const char* err = av_err2str(ret);
413 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
414 return FALSE;
415 }
416
417 return TRUE;
418}
419#else
420static BOOL ffmpeg_resample_frame(AVAudioResampleContext* WINPR_RESTRICT context,
421 AVFrame* WINPR_RESTRICT in, AVFrame* WINPR_RESTRICT out)
422{
423 int ret;
424
425 if (!avresample_is_open(context))
426 {
427 if ((ret = avresample_config(context, out, in)) < 0)
428 {
429 const char* err = av_err2str(ret);
430 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
431 return FALSE;
432 }
433
434 if ((ret = (avresample_open(context))) < 0)
435 {
436 const char* err = av_err2str(ret);
437 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
438 return FALSE;
439 }
440 }
441
442 if ((ret = avresample_convert_frame(context, out, in)) < 0)
443 {
444 const char* err = av_err2str(ret);
445 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
446 return FALSE;
447 }
448
449 return TRUE;
450}
451#endif
452
453static BOOL ffmpeg_encode_frame(AVCodecContext* WINPR_RESTRICT context, AVFrame* WINPR_RESTRICT in,
454 AVPacket* WINPR_RESTRICT packet, wStream* WINPR_RESTRICT out)
455{
456 if (in->format == AV_SAMPLE_FMT_FLTP)
457 {
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;
461#else
462 const int nr_channels = in->ch_layout.nb_channels;
463#endif
464
465 for (int y = 0; y < nr_channels; y++)
466 {
467 float* data = (float*)pp[y];
468 for (int x = 0; x < in->nb_samples; x++)
469 {
470 const float val1 = data[x];
471 if (isnan(val1))
472 data[x] = 0.0f;
473 else if (isinf(val1))
474 {
475 if (val1 < 0.0f)
476 data[x] = -1.0f;
477 else
478 data[x] = 1.0f;
479 }
480 }
481 }
482 }
483 /* send the packet with the compressed data to the encoder */
484 int ret = avcodec_send_frame(context, in);
485
486 if (ret < 0)
487 {
488 const char* err = av_err2str(ret);
489 // Ignore errors: AAC encoder sometimes returns -22
490 // The log message from ffmpeg is '[aac @ 0x7f140db753c0] Input contains (near) NaN/+-Inf'
491 if (ret == AVERROR(EINVAL))
492 {
493 WLog_DBG(TAG, "Error submitting the packet to the encoder %s [%d], ignoring", err, ret);
494 return TRUE;
495 }
496
497 WLog_ERR(TAG, "Error submitting the packet to the encoder %s [%d]", err, ret);
498
499 return FALSE;
500 }
501
502 /* read all the output frames (in general there may be any number of them */
503 while (TRUE)
504 {
505 ret = avcodec_receive_packet(context, packet);
506
507 if ((ret == AVERROR(EAGAIN)) || (ret == AVERROR_EOF))
508 break;
509
510 if (ret < 0)
511 {
512 const char* err = av_err2str(ret);
513 WLog_ERR(TAG, "Error during encoding %s [%d]", err, ret);
514 return FALSE;
515 }
516
517 WINPR_ASSERT(packet->size >= 0);
518 if (!Stream_EnsureRemainingCapacity(out, (size_t)packet->size))
519 return FALSE;
520
521 Stream_Write(out, packet->data, (size_t)packet->size);
522 av_packet_unref(packet);
523 }
524
525 return TRUE;
526}
527
528static BOOL ffmpeg_fill_frame(AVFrame* WINPR_RESTRICT frame,
529 const AUDIO_FORMAT* WINPR_RESTRICT inputFormat,
530 const BYTE* WINPR_RESTRICT data, size_t size)
531{
532 int ret = 0;
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);
536#else
537 av_channel_layout_default(&frame->ch_layout, inputFormat->nChannels);
538#endif
539 WINPR_ASSERT(inputFormat->nSamplesPerSec <= INT_MAX);
540 frame->sample_rate = (int)inputFormat->nSamplesPerSec;
541 frame->format = ffmpeg_sample_format(inputFormat);
542
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;
548
549 if ((ret = avcodec_fill_audio_frame(frame, inputFormat->nChannels, frame->format, data,
550 (int)size, 1)) < 0)
551 {
552 const char* err = av_err2str(ret);
553 WLog_ERR(TAG, "Error during audio frame fill %s [%d]", err, ret);
554 return FALSE;
555 }
556
557 return TRUE;
558}
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)
563#else
564static BOOL ffmpeg_decode(AVCodecContext* dec_ctx, AVPacket* pkt, AVFrame* frame,
565 AVAudioResampleContext* resampleContext, AVFrame* resampled, wStream* out)
566#endif
567{
568 /* send the packet with the compressed data to the decoder */
569 int ret = avcodec_send_packet(dec_ctx, pkt);
570
571 if (ret < 0)
572 {
573 const char* err = av_err2str(ret);
574 WLog_ERR(TAG, "Error submitting the packet to the decoder %s [%d]", err, ret);
575 return FALSE;
576 }
577
578 /* read all the output frames (in general there may be any number of them */
579 while (ret >= 0)
580 {
581 ret = avcodec_receive_frame(dec_ctx, frame);
582
583 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
584 break;
585
586 if (ret < 0)
587 {
588 const char* err = av_err2str(ret);
589 WLog_ERR(TAG, "Error during decoding %s [%d]", err, ret);
590 return FALSE;
591 }
592
593#if defined(SWRESAMPLE_FOUND)
594 if (!swr_is_initialized(resampleContext))
595 {
596 if ((ret = swr_config_frame(resampleContext, resampled, frame)) < 0)
597 {
598#else
599 if (!avresample_is_open(resampleContext))
600 {
601 if ((ret = avresample_config(resampleContext, resampled, frame)) < 0)
602 {
603#endif
604 const char* err = av_err2str(ret);
605 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
606 return FALSE;
607 }
608
609#if defined(SWRESAMPLE_FOUND)
610 ret = (swr_init(resampleContext));
611#else
612 ret = (avresample_open(resampleContext));
613#endif
614 if (ret < 0)
615 {
616 const char* err = av_err2str(ret);
617 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
618 return FALSE;
619 }
620 }
621
622#if defined(SWRESAMPLE_FOUND)
623 ret = swr_convert_frame(resampleContext, resampled, frame);
624#else
625 ret = avresample_convert_frame(resampleContext, resampled, frame);
626#endif
627 if (ret < 0)
628 {
629 const char* err = av_err2str(ret);
630 WLog_ERR(TAG, "Error during resampling %s [%d]", err, ret);
631 return FALSE;
632 }
633
634 {
635
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;
639#else
640 const size_t nrchannels = resampled->channels;
641#endif
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))
645 return FALSE;
646 Stream_Write(out, resampled->data[0], data_size);
647 }
648 }
649
650 return TRUE;
651}
652
653BOOL freerdp_dsp_ffmpeg_supports_format(const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
654{
655 enum AVCodecID id = ffmpeg_get_avcodec(format);
656
657 if (ffmpeg_codec_is_filtered(id, encode))
658 return FALSE;
659
660 if (encode)
661 return avcodec_find_encoder(id) != NULL;
662 else
663 return avcodec_find_decoder(id) != NULL;
664}
665
666FREERDP_DSP_CONTEXT* freerdp_dsp_ffmpeg_context_new(BOOL encode)
667{
668 FREERDP_DSP_CONTEXT* context = NULL;
669#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 10, 100)
670 avcodec_register_all();
671#endif
672 context = calloc(1, sizeof(FREERDP_DSP_CONTEXT));
673
674 if (!context)
675 goto fail;
676
677 if (!freerdp_dsp_common_context_init(&context->common, encode))
678 goto fail;
679
680 return context;
681
682fail:
683 WINPR_PRAGMA_DIAG_PUSH
684 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
685 freerdp_dsp_ffmpeg_context_free(context);
686 WINPR_PRAGMA_DIAG_POP
687 return NULL;
688}
689
690void freerdp_dsp_ffmpeg_context_free(FREERDP_DSP_CONTEXT* context)
691{
692 if (context)
693 {
694 ffmpeg_close_context(context);
695 freerdp_dsp_common_context_uninit(&context->common);
696 free(context);
697 }
698}
699
700BOOL freerdp_dsp_ffmpeg_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
701 const AUDIO_FORMAT* WINPR_RESTRICT targetFormat)
702{
703 if (!context || !targetFormat)
704 return FALSE;
705
706 ffmpeg_close_context(context);
707 context->common.format = *targetFormat;
708 return ffmpeg_open_context(context);
709}
710
711static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
712 const BYTE* WINPR_RESTRICT src, size_t size,
713 const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
714 const BYTE** WINPR_RESTRICT data, size_t* WINPR_RESTRICT length,
715 AUDIO_FORMAT* WINPR_RESTRICT dstFormat)
716{
717 UINT32 bpp = 0;
718 size_t samples = 0;
719
720 if (!context || !data || !length || !dstFormat)
721 return FALSE;
722
723 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
724 return FALSE;
725
726 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
727 samples = size / bpp / srcFormat->nChannels;
728
729 *dstFormat = *srcFormat;
730 if (context->common.format.nChannels == srcFormat->nChannels)
731 {
732 *data = src;
733 *length = size;
734 return TRUE;
735 }
736
737 Stream_SetPosition(context->common.channelmix, 0);
738
739 /* Destination has more channels than source */
740 if (context->common.format.nChannels > srcFormat->nChannels)
741 {
742 switch (srcFormat->nChannels)
743 {
744 case 1:
745 if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
746 return FALSE;
747
748 for (size_t x = 0; x < samples; x++)
749 {
750 for (size_t y = 0; y < bpp; y++)
751 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
752
753 for (size_t y = 0; y < bpp; y++)
754 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
755 }
756
757 Stream_SealLength(context->common.channelmix);
758 *data = Stream_Buffer(context->common.channelmix);
759 *length = Stream_Length(context->common.channelmix);
760 dstFormat->nChannels = 2;
761 return TRUE;
762
763 case 2: /* We only support stereo, so we can not handle this case. */
764 default: /* Unsupported number of channels */
765 WLog_WARN(TAG, "[%s] unsupported source channel count %" PRIu16, __func__,
766 srcFormat->nChannels);
767 return FALSE;
768 }
769 }
770
771 /* Destination has less channels than source */
772 switch (srcFormat->nChannels)
773 {
774 case 2:
775 if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
776 return FALSE;
777
778 /* Simply drop second channel.
779 * TODO: Calculate average */
780 for (size_t x = 0; x < samples; x++)
781 {
782 for (size_t y = 0; y < bpp; y++)
783 Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
784 }
785
786 Stream_SealLength(context->common.channelmix);
787 *data = Stream_Buffer(context->common.channelmix);
788 *length = Stream_Length(context->common.channelmix);
789 dstFormat->nChannels = 1;
790 return TRUE;
791
792 case 1: /* Invalid, do we want to use a 0 channel sound? */
793 default: /* Unsupported number of channels */
794 WLog_WARN(TAG, "[%s] unsupported channel count %" PRIu16, __func__,
795 srcFormat->nChannels);
796 return FALSE;
797 }
798
799 return FALSE;
800}
801
802BOOL freerdp_dsp_ffmpeg_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
803 const AUDIO_FORMAT* WINPR_RESTRICT format,
804 const BYTE* WINPR_RESTRICT sdata, size_t length,
805 wStream* WINPR_RESTRICT out)
806{
807 AUDIO_FORMAT fmt = { 0 };
808
809 if (!context || !format || !sdata || !out || !context->common.encoder)
810 return FALSE;
811
812 if (!context || !sdata || !out)
813 return FALSE;
814
815 /* https://github.com/FreeRDP/FreeRDP/issues/7607
816 *
817 * we get noisy data with channel transformation, so do it ourselves.
818 */
819 const BYTE* data = NULL;
820 if (!freerdp_dsp_channel_mix(context, sdata, length, format, &data, &length, &fmt))
821 return FALSE;
822
823 /* Create input frame */
824 if (!ffmpeg_fill_frame(context->frame, format, data, length))
825 return FALSE;
826
827 ffmpeg_setup_resample_frame(context, format);
828 /* Resample to desired format. */
829 if (!ffmpeg_resample_frame(context->rcontext, context->frame, context->resampled))
830 return FALSE;
831
832 if (context->context->frame_size <= 0)
833 {
834 return ffmpeg_encode_frame(context->context, context->resampled, context->packet, out);
835 }
836 else
837 {
838 int copied = 0;
839 int rest = context->resampled->nb_samples;
840
841 do
842 {
843 int inSamples = rest;
844
845 if ((inSamples < 0) || (context->bufferedSamples > (UINT32)(INT_MAX - inSamples)))
846 return FALSE;
847
848 if (inSamples + (int)context->bufferedSamples > context->context->frame_size)
849 inSamples = context->context->frame_size - (int)context->bufferedSamples;
850
851#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 28, 100)
852 const int nrchannels = context->context->ch_layout.nb_channels;
853#else
854 const int nrchannels = context->context->channels;
855#endif
856 const int rc =
857 av_samples_copy(context->buffered->extended_data, context->resampled->extended_data,
858 (int)context->bufferedSamples, copied, inSamples, nrchannels,
859 context->context->sample_fmt);
860 if (rc < 0)
861 return FALSE;
862 rest -= inSamples;
863 copied += inSamples;
864 context->bufferedSamples += (UINT32)inSamples;
865
866 if (context->context->frame_size <= (int)context->bufferedSamples)
867 {
868 /* Encode in desired format. */
869 if (!ffmpeg_encode_frame(context->context, context->buffered, context->packet, out))
870 return FALSE;
871
872 context->bufferedSamples = 0;
873 }
874 } while (rest > 0);
875
876 return TRUE;
877 }
878}
879
880BOOL freerdp_dsp_ffmpeg_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
881 const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
882 const BYTE* WINPR_RESTRICT data, size_t length,
883 wStream* WINPR_RESTRICT out)
884{
885 if (!context || !srcFormat || !data || !out || context->common.encoder)
886 return FALSE;
887
888#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 133, 100)
889 av_init_packet(context->packet);
890#endif
891 context->packet->data = WINPR_CAST_CONST_PTR_AWAY(data, uint8_t*);
892
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);
897}