[FFmpeg-devel] [PATCH] swresample: Add AVFrame based API

Michael Niedermayer michaelni at gmx.at
Mon Aug 11 01:49:19 CEST 2014


From: Luca Barbato <lu_zero at gentoo.org>

TODO:bump

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
 libswresample/Makefile           |    1 +
 libswresample/swresample.h       |   61 +++++++++++++++
 libswresample/swresample_frame.c |  158 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 220 insertions(+)
 create mode 100644 libswresample/swresample_frame.c

diff --git a/libswresample/Makefile b/libswresample/Makefile
index 75c6535..120ee33 100644
--- a/libswresample/Makefile
+++ b/libswresample/Makefile
@@ -13,6 +13,7 @@ OBJS = audioconvert.o                        \
        resample.o                            \
        resample_dsp.o                        \
        swresample.o                          \
+       swresample_frame.o                    \
 
 OBJS-$(CONFIG_LIBSOXR) += soxr_resample.o
 OBJS-$(CONFIG_SHARED)  += log2_tab.o
diff --git a/libswresample/swresample.h b/libswresample/swresample.h
index 4b8b045..687891b 100644
--- a/libswresample/swresample.h
+++ b/libswresample/swresample.h
@@ -121,6 +121,7 @@
  */
 
 #include <stdint.h>
+#include "libavutil/frame.h"
 #include "libavutil/samplefmt.h"
 
 #include "libswresample/version.h"
@@ -467,6 +468,66 @@ const char *swresample_license(void);
 
 /**
  * @}
+ *
+ * @name AVFrame based API
+ * @{
+ */
+
+/**
+ * Convert the samples in the input AVFrame and write them to the output AVFrame.
+ *
+ * Input and output AVFrames must have channel_layout, sample_rate and format set.
+ *
+ * If the output AVFrame does not have the data pointers allocated the nb_samples
+ * field will be set using av_frame_get_buffer()
+ * is called to allocate the frame.
+ *
+ * The output AVFrame can be NULL or have fewer allocated samples than required.
+ * In this case, any remaining samples not written to the output will be added
+ * to an internal FIFO buffer, to be returned at the next call to this function
+ * or to swr_convert() or to swr_convert_frame().
+ *
+ * If converting sample rate, there may be data remaining in the internal
+ * resampling delay buffer. swr_get_delay() tells the number of
+ * remaining samples. To get this data as output, call this function or
+ * swr_convert() with NULL input.
+ *
+ * If the SwrContext configuration does not match the output and
+ * input AVFrame settings the conversion does not take place and depending on
+ * which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED
+ * or AVERROR_OUTPUT_CHANGED|AVERROR_INPUT_CHANGED is returned.
+ *
+ * @see swr_delay()
+ * @see swr_convert()
+ * @see swr_get_delay()
+ *
+ * @param swr             audio resample context
+ * @param output          output AVFrame
+ * @param input           input AVFrame
+ * @return                0 on success, AVERROR on failure or nonmatching
+ *                        configuration.
+ */
+int swr_convert_frame(SwrContext *swr,
+                      AVFrame *output, AVFrame *input);
+
+/**
+ * Configure or reconfigure the SwrContext using the information
+ * provided by the AVFrames.
+ *
+ * The original resampling context is reset even on failure.
+ * The function calls swr_close() internally if the context is open.
+ *
+ * @see swr_close();
+ *
+ * @param swr             audio resample context
+ * @param output          output AVFrame
+ * @param input           input AVFrame
+ * @return                0 on success, AVERROR on failure.
+ */
+int swr_config_by_frames(SwrContext *swr, AVFrame *out, AVFrame *in);
+
+/**
+ * @}
  * @}
  */
 
diff --git a/libswresample/swresample_frame.c b/libswresample/swresample_frame.c
new file mode 100644
index 0000000..1b23433
--- /dev/null
+++ b/libswresample/swresample_frame.c
@@ -0,0 +1,158 @@
+/*
+ * Copyright (c) 2014 Luca Barbato <lu_zero at gentoo.org>
+ * Copyright (c) 2014 Michael Niedermayer <michaelni at gmx.at>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "swresample_internal.h"
+#include "libavutil/frame.h"
+#include "libavutil/opt.h"
+
+int swr_config_by_frames(SwrContext *s, AVFrame *out, AVFrame *in)
+{
+    swr_close(s);
+
+    if (in) {
+        if (av_opt_set_int(s, "icl", in->channel_layout, 0) < 0)
+            goto fail;
+        if (av_opt_set_int(s, "isf", in->format, 0) < 0)
+            goto fail;
+        if (av_opt_set_int(s, "isr", in->sample_rate, 0) < 0)
+            goto fail;
+    }
+
+    if (out) {
+        if (av_opt_set_int(s, "ocl", out->channel_layout, 0) < 0)
+            goto fail;
+        if (av_opt_set_int(s, "osf", out->format,  0) < 0)
+            goto fail;
+        if (av_opt_set_int(s, "osr", out->sample_rate, 0) < 0)
+            goto fail;
+    }
+
+    return 0;
+fail:
+    av_log(s, AV_LOG_ERROR, "Failed to set option\n");
+    return AVERROR(EINVAL);
+}
+
+static int config_changed(SwrContext *s,
+                          AVFrame *out, AVFrame *in)
+{
+    int ret = 0;
+
+    if (in) {
+        if (s->in_ch_layout   != in->channel_layout ||
+            s->in_sample_rate != in->sample_rate ||
+            s->in_sample_fmt  != in->format) {
+            ret |= AVERROR_INPUT_CHANGED;
+        }
+    }
+
+    if (out) {
+        if (s->out_ch_layout   != out->channel_layout ||
+            s->out_sample_rate != out->sample_rate ||
+            s->out_sample_fmt  != out->format) {
+            ret |= AVERROR_OUTPUT_CHANGED;
+        }
+    }
+
+    return ret;
+}
+
+static inline int convert_frame(SwrContext *s,
+                                AVFrame *out, AVFrame *in)
+{
+    int ret;
+    uint8_t **out_data = NULL;
+    const uint8_t **in_data = NULL;
+    int out_nb_samples = 0, in_nb_samples = 0;
+
+    if (out) {
+        out_data       = out->extended_data;
+        out_nb_samples = out->nb_samples;
+    }
+
+    if (in) {
+        in_data       = (const uint8_t **)in->extended_data;
+        in_nb_samples = in->nb_samples;
+    }
+
+    ret = swr_convert(s, out_data, out_nb_samples, in_data, in_nb_samples);
+
+    if (ret < 0) {
+        if (out)
+            out->nb_samples = 0;
+        return ret;
+    }
+
+    if (out)
+        out->nb_samples = ret;
+
+    return 0;
+}
+
+static inline int available_samples(AVFrame *out)
+{
+    int bytes_per_sample = av_get_bytes_per_sample(out->format);
+    int samples = out->linesize[0] / bytes_per_sample;
+
+    if (av_sample_fmt_is_planar(out->format)) {
+        return samples;
+    } else {
+        int channels = av_get_channel_layout_nb_channels(out->channel_layout);
+        return samples / channels;
+    }
+}
+
+int swr_convert_frame(SwrContext *s,
+                      AVFrame *out, AVFrame *in)
+{
+    int ret, setup = 0;
+
+    if (!swr_is_initialized(s)) {
+        if ((ret = swr_config_by_frames(s, out, in)) < 0)
+            return ret;
+        if ((ret = swr_init(s)) < 0)
+            return ret;
+        setup = 1;
+    } else {
+        // return as is or reconfigure for input changes?
+        if ((ret = config_changed(s, out, in)))
+            return ret;
+    }
+
+    if (out) {
+        if (!out->linesize[0]) {
+            out->nb_samples =   swr_get_delay(s, s->out_sample_rate)
+                              + in->nb_samples*(int64_t)s->out_sample_rate / s->in_sample_rate
+                              + 3;
+            if ((ret = av_frame_get_buffer(out, 0)) < 0) {
+                if (setup)
+                    swr_close(s);
+                return ret;
+            }
+        } else {
+            if (!out->nb_samples)
+                out->nb_samples = available_samples(out);
+        }
+    }
+
+    return convert_frame(s, out, in);
+}
+
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list