[FFmpeg-devel] [PATCH] avcodec/audiotoolbox Support float sample format

jason at jasonfry.co.uk jason at jasonfry.co.uk
Fri Aug 6 16:29:49 EEST 2021


Signed-off-by: Jason Fry <jason at jasonfry.co.uk>
---
libavcodec/audiotoolboxdec.c | 19 +++++++++++++++----
libavcodec/audiotoolboxenc.c |  2 +-
2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/libavcodec/audiotoolboxdec.c b/libavcodec/audiotoolboxdec.c
index a222cde62e..f2a897c8c1 100644
--- a/libavcodec/audiotoolboxdec.c
+++ b/libavcodec/audiotoolboxdec.c
@@ -304,8 +304,16 @@ static av_cold int ffat_create_decoder(AVCodecContext *avctx,
     OSStatus status;
     int i;

-    enum AVSampleFormat sample_fmt = (avctx->bits_per_raw_sample == 32) ?
+    enum AVSampleFormat sample_fmt = avctx->sample_fmt;
+    if (sample_fmt == AV_SAMPLE_FMT_NONE) {
+      sample_fmt = (avctx->bits_per_raw_sample == 32) ?
                                      AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
+      avctx->sample_fmt = sample_fmt;
+    } else if (sample_fmt != AV_SAMPLE_FMT_S16 &&
+               sample_fmt != AV_SAMPLE_FMT_S32 &&
+               sample_fmt != AV_SAMPLE_FMT_FLT) {
+      return AVERROR_UNKNOWN;
+    }

     AudioStreamBasicDescription in_format = {
         .mFormatID = ffat_get_format_id(avctx->codec_id, avctx->profile),
@@ -313,12 +321,13 @@ static av_cold int ffat_create_decoder(AVCodecContext *avctx,
     };
     AudioStreamBasicDescription out_format = {
         .mFormatID = kAudioFormatLinearPCM,
-        .mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
+        .mFormatFlags = (sample_fmt == AV_SAMPLE_FMT_FLT) ?
+                                    kAudioFormatFlagIsFloat :
+                                    kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
         .mFramesPerPacket = 1,
         .mBitsPerChannel = av_get_bytes_per_sample(sample_fmt) * 8,
     };

-    avctx->sample_fmt = sample_fmt;

     if (ffat_usable_extradata(avctx)) {
         UInt32 format_size = sizeof(in_format);
@@ -468,8 +477,10 @@ static void ffat_copy_samples(AVCodecContext *avctx, AVFrame *frame)
     ATDecodeContext *at = avctx->priv_data;
     if (avctx->sample_fmt == AV_SAMPLE_FMT_S32) {
         COPY_SAMPLES(int32_t);
-    } else {
+    } else if (avctx->sample_fmt == AV_SAMPLE_FMT_S16) {
         COPY_SAMPLES(int16_t);
+    } else {
+        COPY_SAMPLES(float_t);
     }
}

diff --git a/libavcodec/audiotoolboxenc.c b/libavcodec/audiotoolboxenc.c
index 9245aa9dc4..fddbd390f6 100644
--- a/libavcodec/audiotoolboxenc.c
+++ b/libavcodec/audiotoolboxenc.c
@@ -632,7 +632,7 @@ static const AVOption options[] = {
         .capabilities   = AV_CODEC_CAP_DELAY | \
                           AV_CODEC_CAP_ENCODER_FLUSH __VA_ARGS__, \
         .sample_fmts    = (const enum AVSampleFormat[]) { \
-            AV_SAMPLE_FMT_S16, \
+            AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT, \
             AV_SAMPLE_FMT_U8,  AV_SAMPLE_FMT_NONE \
         }, \
         .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE, \
--
2.30.0




More information about the ffmpeg-devel mailing list