History

lavu/imgutils: create misc functions for dealing with buffers

Move the lavc/imgconvert functions and rename them as follows: avpicture_get_size -> av_image_get_buffer_size() avpicture_fill -> av_image_fill_arrays() avpicture_layout -> av_image_copy_to_buffer() The new functions have an align parameter, which allows to define the linesize alignment assumed in the buffer (which is set or read). The names of the functions are consistent with the lavu/samples API (av_samples_get_buffer_size(), av_samples_fill_arrays()).

Stefano Sabatini committed 14 years ago

Commit e6674e41 parent fd91a3e7 files · +154 −87Browse files

Unified · Split

7 files
  1. doc/APIchanges +6 −0
  2. libavcodec/avcodec.h +10 −38
  3. libavcodec/imgconvert.c +6 −47
  4. libavcodec/version.h +1 −1
  5. libavutil/imgutils.c +71 −0
  6. libavutil/imgutils.h +59 −0
  7. libavutil/version.h +1 −1
doc/APIchanges+6−0
@@ -15,6 +15,12 @@ libavutil: 2011-04-18 · 14 unmodified lines
1515
1616 API changes, most recent first:
1717
18+2012-06-26 - xxxxxxx - lavu 51.63.100 - imgutils.h
19+ Add functions to libavutil/imgutils.h:
20+ av_image_get_buffer_size()
21+ av_image_fill_arrays()
22+ av_image_copy_to_buffer()
23+
1824 2012-06-24 - xxxxxxx - lavu 51.62.100 - version.h
1925 version moved from avutil.h to version.h
2026
libavcodec/avcodec.h+10−38
@@ -4261,42 +4261,19 @@ int avpicture_alloc(AVPicture *picture, enum PixelFormat pix_fmt, int width, int · 4260 unmodified lines
42614261 void avpicture_free(AVPicture *picture);
42624262
42634263 /**
4264− * Fill in the AVPicture fields.
4265− * The fields of the given AVPicture are filled in by using the 'ptr' address
4266− * which points to the image data buffer. Depending on the specified picture
4267− * format, one or multiple image data pointers and line sizes will be set.
4268− * If a planar format is specified, several pointers will be set pointing to
4269− * the different picture planes and the line sizes of the different planes
4270− * will be stored in the lines_sizes array.
4271− * Call with ptr == NULL to get the required size for the ptr buffer.
4272− *
4273− * To allocate the buffer and fill in the AVPicture fields in one call,
4274− * use avpicture_alloc().
4275− *
4276− * @param picture AVPicture whose fields are to be filled in
4277− * @param ptr Buffer which will contain or contains the actual image data
4278− * @param pix_fmt The format in which the picture data is stored.
4279− * @param width the width of the image in pixels
4280− * @param height the height of the image in pixels
4281− * @return size of the image data in bytes
4264+ * Fill in the AVPicture fields, always assume a linesize alignment of
4265+ * 1.
4266+ *
4267+ * @see av_image_fill_arrays()
42824268 */
42834269 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
42844270 enum PixelFormat pix_fmt, int width, int height);
42854271
42864272 /**
4287− * Copy pixel data from an AVPicture into a buffer.
4288− * The data is stored compactly, without any gaps for alignment or padding
4289− * which may be applied by avpicture_fill().
4290− *
4291− * @see avpicture_get_size()
4273+ * Copy pixel data from an AVPicture into a buffer, always assume a
4274+ * linesize alignment of 1.
42924275 *
4293− * @param[in] src AVPicture containing image data
4294− * @param[in] pix_fmt The format in which the picture data is stored.
4295− * @param[in] width the width of the image in pixels.
4296− * @param[in] height the height of the image in pixels.
4297− * @param[out] dest A buffer into which picture data will be copied.
4298− * @param[in] dest_size The size of 'dest'.
4299− * @return The number of bytes written to dest, or a negative value (error code) on error.
4276+ * @see av_image_copy_to_buffer()
43004277 */
43014278 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
43024279 unsigned char *dest, int dest_size);
@@ -4304,14 +4281,9 @@ int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, · 1 unmodified line
43044281 /**
43054282 * Calculate the size in bytes that a picture of the given width and height
43064283 * would occupy if stored in the given picture format.
4307− * Note that this returns the size of a compact representation as generated
4308− * by avpicture_layout(), which can be smaller than the size required for e.g.
4309− * avpicture_fill().
4310− *
4311− * @param pix_fmt the given picture format
4312− * @param width the width of the image
4313− * @param height the height of the image
4314− * @return Image data size in bytes or -1 on error (e.g. too large dimensions).
4284+ * Always assume a linesize alignment of 1.
4285+ *
4286+ * @see av_image_get_buffer_size().
43154287 */
43164288 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height);
43174289
libavcodec/imgconvert.c+6−47
@@ -296,62 +296,21 @@ int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt) · 295 unmodified lines
296296 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
297297 enum PixelFormat pix_fmt, int width, int height)
298298 {
299− int ret;
300−
301− if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
302− return ret;
303−
304− if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
305− return ret;
306−
307− return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
299+ return av_image_fill_arrays(picture->data, picture->linesize,
300+ ptr, pix_fmt, width, height, 1);
308301 }
309302
310303 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
311304 unsigned char *dest, int dest_size)
312305 {
313− int i, j, nb_planes = 0, linesizes[4];
314− const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
315− int size = avpicture_get_size(pix_fmt, width, height);
316−
317− if (size > dest_size || size < 0)
318− return AVERROR(EINVAL);
319−
320− for (i = 0; i < desc->nb_components; i++)
321− nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
322− nb_planes++;
323−
324− av_image_fill_linesizes(linesizes, pix_fmt, width);
325− for (i = 0; i < nb_planes; i++) {
326− int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
327− const unsigned char *s = src->data[i];
328− h = (height + (1 << shift) - 1) >> shift;
329−
330− for (j = 0; j < h; j++) {
331− memcpy(dest, s, linesizes[i]);
332− dest += linesizes[i];
333− s += src->linesize[i];
334− }
335− }
336−
337− if (desc->flags & PIX_FMT_PAL) {
338− uint32_t *d32 = (unsigned char *)(((size_t)dest + 3) & ~3);
339− for (i = 0; i<256; i++)
340− AV_WL32(d32 + i, AV_RN32(src->data[1] + 4*i));
341− }
342−
343− return size;
306+ return av_image_copy_to_buffer(dest, dest_size,
307+ src->data, src->linesize,
308+ pix_fmt, width, height, 1);
344309 }
345310
346311 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
347312 {
348− AVPicture dummy_pict;
349− if (av_image_check_size(width, height, 0, NULL) < 0)
350− return AVERROR(EINVAL);
351− if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
352− // do not include palette for these pseudo-paletted formats
353− return width * height;
354− return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
313+ return av_image_get_buffer_size(pix_fmt, width, height, 1);
355314 }
356315
357316 static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
libavcodec/version.h+1−1
@@ -28,7 +28,7 @@ · 27 unmodified lines
2828
2929 #define LIBAVCODEC_VERSION_MAJOR 54
3030 #define LIBAVCODEC_VERSION_MINOR 29
31−#define LIBAVCODEC_VERSION_MICRO 100
31+#define LIBAVCODEC_VERSION_MICRO 101
3232
3333 #define LIBAVCODEC_VERSION_INT AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
3434 LIBAVCODEC_VERSION_MINOR, \
libavutil/imgutils.c+71−0
@@ -23,6 +23,7 @@ · 22 unmodified lines
2323
2424 #include "imgutils.h"
2525 #include "internal.h"
26+#include "intreadwrite.h"
2627 #include "log.h"
2728 #include "pixdesc.h"
2829
@@ -276,3 +277,73 @@ void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], · 247 unmodified lines
276277 }
277278 }
278279 }
280+
281+int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
282+ const uint8_t *src,
283+ enum PixelFormat pix_fmt, int width, int height, int align)
284+{
285+ int ret, i;
286+
287+ if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
288+ return ret;
289+
290+ if ((ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width)) < 0)
291+ return ret;
292+
293+ for (i = 0; i < 4; i++)
294+ dst_linesize[i] = FFALIGN(dst_linesize[i], align);
295+
296+ if ((ret = av_image_fill_pointers(dst_data, pix_fmt, width, NULL, dst_linesize)) < 0)
297+ return ret;
298+
299+ return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
300+}
301+
302+int av_image_get_buffer_size(enum PixelFormat pix_fmt, int width, int height, int align)
303+{
304+ uint8_t *data[4];
305+ int linesize[4];
306+ if (av_image_check_size(width, height, 0, NULL) < 0)
307+ return AVERROR(EINVAL);
308+ if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
309+ // do not include palette for these pseudo-paletted formats
310+ return width * height;
311+ return av_image_fill_arrays(data, linesize, NULL, pix_fmt, width, height, align);
312+}
313+
314+int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
315+ const uint8_t * const src_data[4], const int src_linesize[4],
316+ enum PixelFormat pix_fmt, int width, int height, int align)
317+{
318+ int i, j, nb_planes = 0, linesize[4];
319+ const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
320+ int size = av_image_get_buffer_size(pix_fmt, width, height, align);
321+
322+ if (size > dst_size || size < 0)
323+ return AVERROR(EINVAL);
324+
325+ for (i = 0; i < desc->nb_components; i++)
326+ nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
327+ nb_planes++;
328+
329+ av_image_fill_linesizes(linesize, pix_fmt, width);
330+ for (i = 0; i < nb_planes; i++) {
331+ int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
332+ const uint8_t *src = src_data[i];
333+ h = (height + (1 << shift) - 1) >> shift;
334+
335+ for (j = 0; j < h; j++) {
336+ memcpy(dst, src, linesize[i]);
337+ dst += FFALIGN(linesize[i], align);
338+ src += src_linesize[i];
339+ }
340+ }
341+
342+ if (desc->flags & PIX_FMT_PAL) {
343+ uint32_t *d32 = (uint8_t *)(((size_t)dst + 3) & ~3);
344+ for (i = 0; i<256; i++)
345+ AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
346+ }
347+
348+ return size;
349+}
libavutil/imgutils.h+59−0
@@ -116,6 +116,65 @@ void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], · 115 unmodified lines
116116 const uint8_t *src_data[4], const int src_linesizes[4],
117117 enum PixelFormat pix_fmt, int width, int height);
118118
119+/**
120+ * Setup the data pointers and linesizes based on the specified image
121+ * parameters and the provided array.
122+ *
123+ * The fields of the given image are filled in by using the src
124+ * address which points to the image data buffer. Depending on the
125+ * specified pixel format, one or multiple image data pointers and
126+ * line sizes will be set. If a planar format is specified, several
127+ * pointers will be set pointing to the different picture planes and
128+ * the line sizes of the different planes will be stored in the
129+ * lines_sizes array. Call with src == NULL to get the required
130+ * size for the src buffer.
131+ *
132+ * To allocate the buffer and fill in the dst_data and dst_linesize in
133+ * one call, use av_image_alloc().
134+ *
135+ * @param dst_data data pointers to be filled in
136+ * @param dst_linesizes linesizes for the image in dst_data to be filled in
137+ * @param src buffer which will contain or contains the actual image data, can be NULL
138+ * @param pix_fmt the pixel format of the image
139+ * @param width the width of the image in pixels
140+ * @param height the height of the image in pixels
141+ * @param align the value used in src for linesize alignment
142+ * @return the size in bytes required for src, a negative error code
143+ * in case of failure
144+ */
145+int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
146+ const uint8_t *src,
147+ enum PixelFormat pix_fmt, int width, int height, int align);
148+
149+/**
150+ * Return the size in bytes of the amount of data required to store an
151+ * image with the given parameters.
152+ *
153+ * @param[in] align the assumed linesize alignment
154+ */
155+int av_image_get_buffer_size(enum PixelFormat pix_fmt, int width, int height, int align);
156+
157+/**
158+ * Copy image data from an image into a buffer.
159+ *
160+ * av_image_get_buffer_size() can be used to compute the required size
161+ * for the buffer to fill.
162+ *
163+ * @param dst a buffer into which picture data will be copied
164+ * @param dst_size the size in bytes of dst
165+ * @param src_data pointers containing the source image data
166+ * @param src_linesizes linesizes for the image in src_data
167+ * @param pix_fmt the pixel format of the source image
168+ * @param width the width of the source image in pixels
169+ * @param height the height of the source image in pixels
170+ * @param align the assumed linesize alignment for dst
171+ * @return the number of bytes written to dst, or a negative value
172+ * (error code) on error
173+ */
174+int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
175+ const uint8_t * const src_data[4], const int src_linesize[4],
176+ enum PixelFormat pix_fmt, int width, int height, int align);
177+
119178 /**
120179 * Check if the given dimension of an image is valid, meaning that all
121180 * bytes of the image can be addressed with a signed int.
libavutil/version.h+1−1
@@ -31,7 +31,7 @@ · 30 unmodified lines
3131 */
3232
3333 #define LIBAVUTIL_VERSION_MAJOR 51
34−#define LIBAVUTIL_VERSION_MINOR 62
34+#define LIBAVUTIL_VERSION_MINOR 63
3535 #define LIBAVUTIL_VERSION_MICRO 100
3636
3737 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t