[FFmpeg-devel] [PATCH 1/4] Implement av_samples_alloc() and av_samples_fill_arrays().

Janne Grunau janne-ffmpeg
Wed Feb 16 10:39:32 CET 2011


From: Stefano Sabatini <stefano.sabatini-lala at poste.it>

With minor changes by michael

Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
Signed-off-by: Janne Grunau <janne-ffmpeg at jannau.net>
---
 libavutil/samplefmt.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/samplefmt.h |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/libavutil/samplefmt.c b/libavutil/samplefmt.c
index c9ffd1a..ea004d9 100644
--- a/libavutil/samplefmt.c
+++ b/libavutil/samplefmt.c
@@ -71,3 +71,50 @@ int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt)
     return sample_fmt < 0 || sample_fmt >= AV_SAMPLE_FMT_NB ?
         0 : sample_fmt_info[sample_fmt].bits;
 }
+
+int av_samples_fill_arrays(uint8_t *pointers[8], int linesizes[8],
+                           uint8_t *buf, int nb_channels, int nb_samples,
+                           enum AVSampleFormat sample_fmt, int planar, int align)
+{
+    int i, step_size = 0;
+    int sample_size = av_get_bits_per_sample_fmt(sample_fmt) >> 3;
+    int channel_step = planar ? FFALIGN(nb_samples*sample_size, align) : sample_size;
+
+    if(nb_channels * (uint64_t)nb_samples * sample_size >= INT_MAX - align*(uint64_t)nb_channels)
+        return AVERROR(EINVAL);
+
+    if (pointers) {
+        pointers[0] = buf;
+        for (i = 0; i < nb_channels; i++) {
+            pointers[i] = buf + step_size;
+            step_size += channel_step;
+        }
+        memset(&pointers[nb_channels], 0, (8-nb_channels) * sizeof(pointers[0]));
+    }
+
+    if (linesizes) {
+        linesizes[0] = planar ?  sample_size : nb_channels*sample_size;
+        memset(&linesizes[1], 0, (8-1) * sizeof(linesizes[0]));
+    }
+
+    return planar ? channel_step * nb_channels : FFALIGN(nb_channels*sample_size*nb_samples, align);
+}
+
+int av_samples_alloc(uint8_t *pointers[8], int linesizes[8],
+                     int nb_samples, int nb_channels,
+                     enum AVSampleFormat sample_fmt, int planar,
+                     int align)
+{
+    uint8_t *buf;
+    int size = av_samples_fill_arrays(NULL, NULL,
+                                      NULL, nb_channels, nb_samples,
+                                      sample_fmt, planar, align);
+
+    buf = av_mallocz(size);
+    if (!buf)
+        return AVERROR(ENOMEM);
+
+    return av_samples_fill_arrays(pointers, linesizes,
+                                  buf, nb_channels, nb_samples,
+                                  sample_fmt, planar, align);
+}
diff --git a/libavutil/samplefmt.h b/libavutil/samplefmt.h
index 0604580..5549e53 100644
--- a/libavutil/samplefmt.h
+++ b/libavutil/samplefmt.h
@@ -67,4 +67,43 @@ char *av_get_sample_fmt_string(char *buf, int buf_size, enum AVSampleFormat samp
  */
 int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt);
 
+/**
+ * Fill channel data pointers and linesizes for samples with sample
+ * format sample_fmt.
+ *
+ * The pointers array is filled with the pointers to the samples data:
+ * data[c] points to the first sample of channel c.
+ * data[c] + linesize[0] points to the second sample of channel c
+ *
+ * @param pointers array to be filled with the pointer for each plane, may be NULL
+ * @param linesizes array to be filled with the linesize, may be NULL
+ * @param buf the pointer to a buffer containing the samples
+ * @param nb_samples the number of samples in a single channel
+ * @param planar 1 if the samples layout is planar, 0 if it is packed
+ * @param nb_channels the number of channels
+ * @return the total size of the buffer, a negative
+ * error code in case of failure
+ */
+int av_samples_fill_arrays(uint8_t *pointers[8], int linesizes[8],
+                           uint8_t *buf, int nb_channels, int nb_samples,
+                           enum AVSampleFormat sample_fmt, int planar, int align);
+
+/**
+ * Allocate a samples buffer for nb_samples samples, and
+ * fill pointers and linesizes accordingly.
+ * The allocated samples buffer has to be freed by using
+ * av_freep(&pointers[0]).
+ *
+ * @param nb_samples number of samples per channel
+ * @param planar 1 if the samples layout is planar, 0 if packed,
+ * @param align the value to use for buffer size alignment
+ * @return the size in bytes required for the samples buffer, a negative
+ * error code in case of failure
+ * @see av_samples_fill_arrays()
+ */
+int av_samples_alloc(uint8_t *pointers[8], int linesizes[8],
+                     int nb_samples, int nb_channels,
+                     enum AVSampleFormat sample_fmt, int planar,
+                     int align);
+
 #endif /* AVUTIL_SAMPLEFMT_H */
-- 
1.7.4




More information about the ffmpeg-devel mailing list