[FFmpeg-cvslog] swresample: Add AVFrame based API

Michael Niedermayer git at videolan.org
Sat Aug 16 20:49:02 CEST 2014


ffmpeg | branch: master | Michael Niedermayer <michaelni at gmx.at> | Mon Aug 11 01:43:26 2014 +0200| [b7d5e016a3f3b2c60f8dc6725ab9628ef58a7644] | committer: Michael Niedermayer

swresample: Add AVFrame based API

Based on commit fb1ddcdc8f51b9d261ae8e9c26b91e81f7b6bf45 by Luca Barbato <lu_zero at gentoo.org>
Adapted for libswresample by Michael Niedermayer
Signed-off-by: Michael Niedermayer <michaelni at gmx.at>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=b7d5e016a3f3b2c60f8dc6725ab9628ef58a7644
---

 doc/APIchanges                   |    3 +
 libswresample/Makefile           |    1 +
 libswresample/swresample.h       |   61 +++++++++++++++
 libswresample/swresample_frame.c |  158 ++++++++++++++++++++++++++++++++++++++
 libswresample/version.h          |    2 +-
 5 files changed, 224 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index d8ab714..8b3051c 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil:     2014-08-09
 
 API changes, most recent first:
 
+2014-08-16 - xxxxxxx - lswr 1.1.100 - swresample.h
+  Add AVFrame based API
+
 2014-xx-xx - xxxxxxx - lavu 54.4.100 - dict.h
   Add av_dict_set_int helper function.
 
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..37656a6 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().
+ *
+ * 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 the result of a bitwise-OR of them 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, const 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_frame(SwrContext *swr, const AVFrame *out, const AVFrame *in);
+
+/**
+ * @}
  * @}
  */
 
diff --git a/libswresample/swresample_frame.c b/libswresample/swresample_frame.c
new file mode 100644
index 0000000..71d3ed7
--- /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_frame(SwrContext *s, const AVFrame *out, const 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,
+                          const AVFrame *out, const 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, const 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, const AVFrame *in)
+{
+    int ret, setup = 0;
+
+    if (!swr_is_initialized(s)) {
+        if ((ret = swr_config_frame(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);
+}
+
diff --git a/libswresample/version.h b/libswresample/version.h
index 316d304..61c76fa 100644
--- a/libswresample/version.h
+++ b/libswresample/version.h
@@ -29,7 +29,7 @@
 #include "libavutil/avutil.h"
 
 #define LIBSWRESAMPLE_VERSION_MAJOR   1
-#define LIBSWRESAMPLE_VERSION_MINOR   0
+#define LIBSWRESAMPLE_VERSION_MINOR   1
 #define LIBSWRESAMPLE_VERSION_MICRO 100
 
 #define LIBSWRESAMPLE_VERSION_INT  AV_VERSION_INT(LIBSWRESAMPLE_VERSION_MAJOR, \



More information about the ffmpeg-cvslog mailing list