diff mbox series

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

Message ID 5842508d-32f6-3073-8d1f-9ea8819d92a4@mail.de
State New
Headers show
Series [FFmpeg-devel] lavd/avfoundation: Allow to change interleaving of samples on the fly
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

Thilo Borgmann April 5, 2021, 4:54 p.m. UTC
Hi,

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


-Thilo
From e44b0e0e338043660fe59f66b01cc24729cc241f Mon Sep 17 00:00:00 2001
From: Thilo Borgmann <thilo.borgmann@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(-)

Comments

Andreas Rheinhardt April 5, 2021, 4:59 p.m. UTC | #1
Thilo Borgmann:
> Hi,
> 
> seems like someone at Apple thought its a good idea to allow this inside the framework....
> 
> 
> 
> +            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;
> +                }
>              }

Leak on error; furthermore setting the new size before it has been
successfully allocated is probably not good.
(Not commenting on the rest of the patch.)

- Andreas
diff mbox series

Patch

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) {