[FFmpeg-devel] [PATCH 6/6] src_buffer: move audio format change check in a separate function.

Nicolas George nicolas.george at normalesup.org
Fri Apr 27 21:48:10 CEST 2012


Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 doc/examples/filtering_audio.c |    2 +-
 libavfilter/src_buffer.c       |  153 ++++++++++++++++++++++-----------------
 2 files changed, 87 insertions(+), 68 deletions(-)

diff --git a/doc/examples/filtering_audio.c b/doc/examples/filtering_audio.c
index 988dbfe..850e44a 100644
--- a/doc/examples/filtering_audio.c
+++ b/doc/examples/filtering_audio.c
@@ -198,7 +198,7 @@ int main(int argc, char **argv)
             av_free_packet(&packet);
             if (ret < 0) {
                 av_log(NULL, AV_LOG_ERROR, "Error decoding audio\n");
-                break;
+                continue;
             }
 
             if (got_frame) {
diff --git a/libavfilter/src_buffer.c b/libavfilter/src_buffer.c
index a6ebb57..0cd9e2d 100644
--- a/libavfilter/src_buffer.c
+++ b/libavfilter/src_buffer.c
@@ -70,6 +70,28 @@ typedef struct {
         return AVERROR(EINVAL);\
     }
 
+static int insert_filter(BufferSourceContext *abuffer,
+                         AVFilterLink *link, AVFilterContext **filt_ctx,
+                         const char *filt_name);
+static void remove_filter(AVFilterContext **filt_ctx);
+static int reconfigure_filter(BufferSourceContext *abuffer, AVFilterContext *filt_ctx);
+
+static inline void log_input_change(void *ctx, AVFilterLink *link, AVFilterBufferRef *ref)
+{
+    char old_layout_str[16], new_layout_str[16];
+    av_get_channel_layout_string(old_layout_str, sizeof(old_layout_str),
+                                 -1, link->channel_layout);
+    av_get_channel_layout_string(new_layout_str, sizeof(new_layout_str),
+                                 -1, ref->audio->channel_layout);
+    av_log(ctx, AV_LOG_INFO,
+           "Audio input format changed: "
+           "%s:%s:%d -> %s:%s:%d, normalizing\n",
+           av_get_sample_fmt_name(link->format),
+           old_layout_str, (int)link->sample_rate,
+           av_get_sample_fmt_name(ref->format),
+           new_layout_str, ref->audio->sample_rate);
+}
+
 static int check_format_change_video(AVFilterContext *buffer_filter,
                                      AVFilterBufferRef *picref)
 {
@@ -123,12 +145,72 @@ static int check_format_change_video(AVFilterContext *buffer_filter,
     return 0;
 }
 
+static int check_format_change_audio(AVFilterContext *ctx,
+                                     AVFilterBufferRef *samplesref)
+{
+    BufferSourceContext *abuffer = ctx->priv;
+    AVFilterLink *link;
+    int ret, logged = 0;
+
+    link = ctx->outputs[0];
+    if (samplesref->audio->sample_rate != link->sample_rate) {
+
+        log_input_change(ctx, link, samplesref);
+        logged = 1;
+
+        abuffer->sample_rate = samplesref->audio->sample_rate;
+
+        if (!abuffer->aresample) {
+            ret = insert_filter(abuffer, link, &abuffer->aresample, "aresample");
+            if (ret < 0) return ret;
+        } else {
+            link = abuffer->aresample->outputs[0];
+            if (samplesref->audio->sample_rate == link->sample_rate)
+                remove_filter(&abuffer->aresample);
+            else
+                if ((ret = reconfigure_filter(abuffer, abuffer->aresample)) < 0)
+                    return ret;
+        }
+    }
+
+    link = ctx->outputs[0];
+    if (samplesref->format                != link->format         ||
+        samplesref->audio->channel_layout != link->channel_layout ||
+        samplesref->audio->planar         != link->planar) {
+
+        if (!logged) log_input_change(ctx, link, samplesref);
+
+        abuffer->sample_format  = samplesref->format;
+        abuffer->channel_layout = samplesref->audio->channel_layout;
+        abuffer->packing_format = samplesref->audio->planar;
+
+        if (!abuffer->aconvert) {
+            ret = insert_filter(abuffer, link, &abuffer->aconvert, "aconvert");
+            if (ret < 0) return ret;
+        } else {
+            link = abuffer->aconvert->outputs[0];
+            if (samplesref->format                == link->format         &&
+                samplesref->audio->channel_layout == link->channel_layout &&
+                samplesref->audio->planar         == link->planar
+               )
+                remove_filter(&abuffer->aconvert);
+            else
+                if ((ret = reconfigure_filter(abuffer, abuffer->aconvert)) < 0)
+                    return ret;
+        }
+    }
+
+    return 0;
+}
+
 static int check_format_change(AVFilterContext *buffer_filter,
                                AVFilterBufferRef *picref)
 {
     switch (buffer_filter->outputs[0]->type) {
     case AVMEDIA_TYPE_VIDEO:
         return check_format_change_video(buffer_filter, picref);
+    case AVMEDIA_TYPE_AUDIO:
+        return check_format_change_audio(buffer_filter, picref);
     default:
         return AVERROR(ENOSYS);
     }
@@ -479,29 +561,12 @@ static void remove_filter(AVFilterContext **filt_ctx)
     set_link_source(src, outlink);
 }
 
-static inline void log_input_change(void *ctx, AVFilterLink *link, AVFilterBufferRef *ref)
-{
-    char old_layout_str[16], new_layout_str[16];
-    av_get_channel_layout_string(old_layout_str, sizeof(old_layout_str),
-                                 -1, link->channel_layout);
-    av_get_channel_layout_string(new_layout_str, sizeof(new_layout_str),
-                                 -1, ref->audio->channel_layout);
-    av_log(ctx, AV_LOG_INFO,
-           "Audio input format changed: "
-           "%s:%s:%d -> %s:%s:%d, normalizing\n",
-           av_get_sample_fmt_name(link->format),
-           old_layout_str, (int)link->sample_rate,
-           av_get_sample_fmt_name(ref->format),
-           new_layout_str, ref->audio->sample_rate);
-}
-
 int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
                                         AVFilterBufferRef *samplesref,
                                         int av_unused flags)
 {
     BufferSourceContext *abuffer = ctx->priv;
-    AVFilterLink *link;
-    int ret, logged = 0;
+    int ret;
 
     if (av_fifo_space(abuffer->fifo) < sizeof(samplesref)) {
         av_log(ctx, AV_LOG_ERROR,
@@ -510,55 +575,9 @@ int av_asrc_buffer_add_audio_buffer_ref(AVFilterContext *ctx,
         return AVERROR(EINVAL);
     }
 
-    // Normalize input
-
-    link = ctx->outputs[0];
-    if (samplesref->audio->sample_rate != link->sample_rate) {
-
-        log_input_change(ctx, link, samplesref);
-        logged = 1;
-
-        abuffer->sample_rate = samplesref->audio->sample_rate;
-
-        if (!abuffer->aresample) {
-            ret = insert_filter(abuffer, link, &abuffer->aresample, "aresample");
-            if (ret < 0) return ret;
-        } else {
-            link = abuffer->aresample->outputs[0];
-            if (samplesref->audio->sample_rate == link->sample_rate)
-                remove_filter(&abuffer->aresample);
-            else
-                if ((ret = reconfigure_filter(abuffer, abuffer->aresample)) < 0)
-                    return ret;
-        }
-    }
-
-    link = ctx->outputs[0];
-    if (samplesref->format                != link->format         ||
-        samplesref->audio->channel_layout != link->channel_layout ||
-        samplesref->audio->planar         != link->planar) {
-
-        if (!logged) log_input_change(ctx, link, samplesref);
-
-        abuffer->sample_format  = samplesref->format;
-        abuffer->channel_layout = samplesref->audio->channel_layout;
-        abuffer->packing_format = samplesref->audio->planar;
-
-        if (!abuffer->aconvert) {
-            ret = insert_filter(abuffer, link, &abuffer->aconvert, "aconvert");
-            if (ret < 0) return ret;
-        } else {
-            link = abuffer->aconvert->outputs[0];
-            if (samplesref->format                == link->format         &&
-                samplesref->audio->channel_layout == link->channel_layout &&
-                samplesref->audio->planar         == link->planar
-               )
-                remove_filter(&abuffer->aconvert);
-            else
-                if ((ret = reconfigure_filter(abuffer, abuffer->aconvert)) < 0)
-                    return ret;
-        }
-    }
+    ret = check_format_change(ctx, samplesref);
+    if (ret < 0)
+        return ret;
 
     if (sizeof(samplesref) != av_fifo_generic_write(abuffer->fifo, &samplesref,
                                                     sizeof(samplesref), NULL)) {
-- 
1.7.2.5



More information about the ffmpeg-devel mailing list