[PATCH] Implement av_samples_alloc() and av_samples_fill_arrays().

Stefano Sabatini stefano.sabatini-lala
Sat Jan 15 00:00:00 CET 2011


---
 libavcore/samplefmt.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 libavcore/samplefmt.h |   45 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+), 0 deletions(-)

diff --git a/libavcore/samplefmt.c b/libavcore/samplefmt.c
index 532acd9..c7adee3 100644
--- a/libavcore/samplefmt.c
+++ b/libavcore/samplefmt.c
@@ -68,3 +68,52 @@ 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 linesize0, int nb_channels,
+                           enum AVSampleFormat sample_fmt, int planar)
+{
+    int i, step_size = 0;
+
+    if (pointers) {
+        pointers[0] = buf;
+        for (i = 0; i < nb_channels; i++) {
+            pointers[i] = buf + step_size;
+            if (planar)
+                step_size += linesize0;
+        }
+        memset(&pointers[nb_channels], 0, (8-nb_channels) * sizeof(pointers[0]));
+    }
+
+    if (linesizes) {
+        linesizes[0] = linesize0;
+        for (i = 1; planar && pointers && i < nb_channels; i++)
+            linesizes[i] = pointers[i] - pointers[0];
+        memset(&linesizes[i], 0, (8-i) * sizeof(linesizes[0]));
+    }
+
+    return planar ? linesize0 * nb_channels : linesize0;
+}
+
+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;
+    int sample_size = av_get_bits_per_sample_fmt(sample_fmt) >> 3;
+    int linesize0 = planar ? sample_size * nb_samples :
+                             sample_size * nb_samples * nb_channels; /* packed */
+
+    linesize0 = FFALIGN(linesize0, align);
+    size = planar ? FFALIGN(nb_channels * linesize0, align) :
+                    FFALIGN(linesize0, align);    /* packed */
+    buf = av_mallocz(size);
+    if (!buf)
+        return AVERROR(ENOMEM);
+
+    return av_samples_fill_arrays(pointers, linesizes,
+                                  buf, linesize0, nb_channels,
+                                  sample_fmt, planar);
+}
diff --git a/libavcore/samplefmt.h b/libavcore/samplefmt.h
index 9701efe..6f56e65 100644
--- a/libavcore/samplefmt.h
+++ b/libavcore/samplefmt.h
@@ -69,4 +69,49 @@ 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:
+ * for planar, set the start point of each plane's data within the buffer,
+ * for packed, set the start point of the entire buffer only.
+ *
+ * linesize0 specifies the aligned size for each data plane.
+ * linesize[0] is set to linesize0, if the format is planar and i>0
+ * linesize[i] is set to data[i] - data[0] for all the other planes.
+ *
+ * @param pointers array to be filled with the pointer for each plane, may be NULL
+ * @param linesizes array to be filled with the linesize for each plane, may be NULL
+ * @param buf the pointer to a buffer containing the samples
+ * @param linesize0 the size of linesizes[0], usually the aligned
+ * size of each plane buffer
+ * @param planar >0 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 linesize0,
+                           enum AVSampleFormat sample_fmt, int planar,
+                           int nb_channels);
+
+/**
+ * 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 >0 if the samples layout is planar, packed otherwise
+ * @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 /* AVCORE_SAMPLEFMT_H */
-- 
1.7.2.3


--BOKacYhQ+x31HxR3--



More information about the ffmpeg-devel mailing list