[FFmpeg-devel] [PATCH] lavd/avfoundation: Allow to change interleaving of samples on the fly

Thilo Borgmann thilo.borgmann at mail.de
Mon Apr 5 19:54:54 EEST 2021


Hi,

seems like someone at Apple thought its a good idea to allow this inside the framework....


-Thilo
-------------- next part --------------
From e44b0e0e338043660fe59f66b01cc24729cc241f Mon Sep 17 00:00:00 2001
From: Thilo Borgmann <thilo.borgmann at mail.de>
Date: Tue, 2 Mar 2021 23:23:06 +0100
Subject: [PATCH] lavd/avfoundation: Allow to change interleaving of samples on
 the fly

---
 libavdevice/avfoundation.m | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/libavdevice/avfoundation.m b/libavdevice/avfoundation.m
index 59d5b0af4f..20d2a3b241 100644
--- a/libavdevice/avfoundation.m
+++ b/libavdevice/avfoundation.m
@@ -736,7 +736,6 @@ static int get_audio_config(AVFormatContext *s)
         return 1;
     }
 
-    if (ctx->audio_non_interleaved) {
         CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame);
         ctx->audio_buffer_size        = CMBlockBufferGetDataLength(block_buffer);
         ctx->audio_buffer             = av_malloc(ctx->audio_buffer_size);
@@ -744,7 +743,6 @@ static int get_audio_config(AVFormatContext *s)
             av_log(s, AV_LOG_ERROR, "error allocating audio buffer\n");
             return 1;
         }
-    }
 
     CFRelease(ctx->current_audio_frame);
     ctx->current_audio_frame = nil;
@@ -1103,12 +1101,24 @@ static int avf_read_packet(AVFormatContext *s, AVPacket *pkt)
             CMBlockBufferRef block_buffer = CMSampleBufferGetDataBuffer(ctx->current_audio_frame);
             int block_buffer_size         = CMBlockBufferGetDataLength(block_buffer);
 
+            CMFormatDescriptionRef format_desc      = CMSampleBufferGetFormatDescription(ctx->current_audio_frame);
+            AudioStreamBasicDescription *basic_desc = CMAudioFormatDescriptionGetStreamBasicDescription(format_desc);
+
             if (!block_buffer || !block_buffer_size) {
                 return AVERROR(EIO);
             }
 
-            if (ctx->audio_non_interleaved && block_buffer_size > ctx->audio_buffer_size) {
-                return AVERROR_BUFFER_TOO_SMALL;
+            if (basic_desc) {
+                ctx->audio_non_interleaved = basic_desc->mFormatFlags & kAudioFormatFlagIsNonInterleaved;
+            }
+
+            if (block_buffer_size > ctx->audio_buffer_size) {
+                ctx->audio_buffer_size = block_buffer_size;
+                ctx->audio_buffer      = av_realloc(ctx->audio_buffer, block_buffer_size);
+                if (!ctx->audio_buffer) {
+                    av_log(s, AV_LOG_ERROR, "error allocating audio buffer\n");
+                    return 1;
+                }
             }
 
             if (av_new_packet(pkt, block_buffer_size) < 0) {
-- 
2.20.1 (Apple Git-117)



More information about the ffmpeg-devel mailing list