History

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()). A redundant check has been dropped from av_image_fill_arrays().

Stefano Sabatini committed 11 years ago

Commit e2ad0b61 parent cf70ba36 files · +158 −89Browse files

Unified · Split

6 files
  1. doc/APIchanges +4 −0
  2. libavcodec/avcodec.h +7 −36
  3. libavcodec/avpicture.c +6 −52
  4. libavutil/imgutils.c +81 −0
  5. libavutil/imgutils.h +59 −0
  6. libavutil/version.h +1 −1
doc/APIchanges+4−0
@@ -22,6 +22,10 @@ API changes, most recent first: · 21 unmodified lines
2222 201x-xx-xx - xxxxxxx - lavc 56.8.0 - avcodec.h
2323 Add AVCodecContext.sw_pix_fmt.
2424
25+2014-11-xx - xxxxxxx - lavu 54.6.0 - imgutils.h
26+ Add utility functions for image manipulation: av_image_get_buffer_size()
27+ av_image_fill_arrays() and av_image_copy_to_buffer().
28+
2529 2014-11-xx - xxxxxxx - lavf 56.06.3 - avformat.h
2630 Add AVFormatContext.avoid_negative_ts.
2731
libavcodec/avcodec.h+7−36
@@ -4072,42 +4072,18 @@ int avpicture_alloc(AVPicture *picture, enum AVPixelFormat pix_fmt, int width, i · 4071 unmodified lines
40724072 void avpicture_free(AVPicture *picture);
40734073
40744074 /**
4075− * Fill in the AVPicture fields.
4076− * The fields of the given AVPicture are filled in by using the 'ptr' address
4077− * which points to the image data buffer. Depending on the specified picture
4078− * format, one or multiple image data pointers and line sizes will be set.
4079− * If a planar format is specified, several pointers will be set pointing to
4080− * the different picture planes and the line sizes of the different planes
4081− * will be stored in the lines_sizes array.
4082− * Call with ptr == NULL to get the required size for the ptr buffer.
4075+ * Fill in the AVPicture fields, always assume a linesize alignment of 1.
40834076 *
4084− * To allocate the buffer and fill in the AVPicture fields in one call,
4085− * use avpicture_alloc().
4086− *
4087− * @param picture AVPicture whose fields are to be filled in
4088− * @param ptr Buffer which will contain or contains the actual image data
4089− * @param pix_fmt The format in which the picture data is stored.
4090− * @param width the width of the image in pixels
4091− * @param height the height of the image in pixels
4092− * @return size of the image data in bytes
4077+ * @see av_image_fill_arrays().
40934078 */
40944079 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
40954080 enum AVPixelFormat pix_fmt, int width, int height);
40964081
40974082 /**
4098− * Copy pixel data from an AVPicture into a buffer.
4099− * The data is stored compactly, without any gaps for alignment or padding
4100− * which may be applied by avpicture_fill().
4101− *
4102− * @see avpicture_get_size()
4083+ * Copy pixel data from an AVPicture into a buffer, always assume a
4084+ * linesize alignment of 1.
41034085 *
4104− * @param[in] src AVPicture containing image data
4105− * @param[in] pix_fmt The format in which the picture data is stored.
4106− * @param[in] width the width of the image in pixels.
4107− * @param[in] height the height of the image in pixels.
4108− * @param[out] dest A buffer into which picture data will be copied.
4109− * @param[in] dest_size The size of 'dest'.
4110− * @return The number of bytes written to dest, or a negative value (error code) on error.
4086+ * @see av_image_copy_to_buffer().
41114087 */
41124088 int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt,
41134089 int width, int height,
@@ -4116,14 +4092,9 @@ int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, · 2 unmodified lines
41164092 /**
41174093 * Calculate the size in bytes that a picture of the given width and height
41184094 * would occupy if stored in the given picture format.
4119− * Note that this returns the size of a compact representation as generated
4120− * by avpicture_layout(), which can be smaller than the size required for e.g.
4121− * avpicture_fill().
4095+ * Always assume a linesize alignment of 1.
41224096 *
4123− * @param pix_fmt the given picture format
4124− * @param width the width of the image
4125− * @param height the height of the image
4126− * @return Image data size in bytes or -1 on error (e.g. too large dimensions).
4097+ * @see av_image_get_buffer_size().
41274098 */
41284099 int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height);
41294100
libavcodec/avpicture.c+6−52
@@ -34,68 +34,22 @@ · 33 unmodified lines
3434 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
3535 enum AVPixelFormat pix_fmt, int width, int height)
3636 {
37− int ret;
38−
39− if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
40− return ret;
41−
42− if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
43− return ret;
44−
45− return av_image_fill_pointers(picture->data, pix_fmt,
46− height, ptr, picture->linesize);
37+ return av_image_fill_arrays(picture->data, picture->linesize,
38+ ptr, pix_fmt, width, height, 1);
4739 }
4840
4941 int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt,
5042 int width, int height,
5143 unsigned char *dest, int dest_size)
5244 {
53− int i, j, nb_planes = 0, linesizes[4];
54− const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
55− int size = avpicture_get_size(pix_fmt, width, height);
56−
57− if (size > dest_size || size < 0)
58− return AVERROR(EINVAL);
59−
60− for (i = 0; i < desc->nb_components; i++)
61− nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
62−
63− nb_planes++;
64−
65− av_image_fill_linesizes(linesizes, pix_fmt, width);
66− for (i = 0; i < nb_planes; i++) {
67− int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
68− const unsigned char *s = src->data[i];
69− h = (height + (1 << shift) - 1) >> shift;
70−
71− for (j = 0; j < h; j++) {
72− memcpy(dest, s, linesizes[i]);
73− dest += linesizes[i];
74− s += src->linesize[i];
75− }
76− }
77−
78− if (desc->flags & AV_PIX_FMT_FLAG_PAL)
79− memcpy((unsigned char *)(((size_t)dest + 3) & ~3),
80− src->data[1], 256 * 4);
81−
82− return size;
45+ return av_image_copy_to_buffer(dest, dest_size,
46+ src->data, src->linesize,
47+ pix_fmt, width, height, 1);
8348 }
8449
8550 int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
8651 {
87− const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
88− AVPicture dummy_pict;
89− int ret;
90−
91− if (!desc)
92− return AVERROR(EINVAL);
93− if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
94− return ret;
95− if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
96− // do not include palette for these pseudo-paletted formats
97− return width * height;
98− return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
52+ return av_image_get_buffer_size(pix_fmt, width, height, 1);
9953 }
10054
10155 int avpicture_alloc(AVPicture *picture,
libavutil/imgutils.c+81−0
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424 #include "common.h"
2525 #include "imgutils.h"
2626 #include "internal.h"
27+#include "intreadwrite.h"
2728 #include "log.h"
2829 #include "mathematics.h"
2930 #include "pixdesc.h"
@@ -298,3 +299,83 @@ void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], · 268 unmodified lines
298299 }
299300 }
300301 }
302+
303+int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
304+ const uint8_t *src, enum AVPixelFormat pix_fmt,
305+ int width, int height, int align)
306+{
307+ int ret, i;
308+
309+ ret = av_image_check_size(width, height, 0, NULL);
310+ if (ret < 0)
311+ return ret;
312+
313+ ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
314+ if (ret < 0)
315+ return ret;
316+
317+ for (i = 0; i < 4; i++)
318+ dst_linesize[i] = FFALIGN(dst_linesize[i], align);
319+
320+ return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
321+}
322+
323+int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
324+ int width, int height, int align)
325+{
326+ uint8_t *data[4];
327+ int linesize[4];
328+ int ret;
329+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
330+ if (!desc)
331+ return AVERROR_BUG;
332+
333+ ret = av_image_check_size(width, height, 0, NULL);
334+ if (ret < 0)
335+ return ret;
336+
337+ // do not include palette for these pseudo-paletted formats
338+ if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
339+ return width * height;
340+
341+ return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
342+ width, height, align);
343+}
344+
345+int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
346+ const uint8_t * const src_data[4],
347+ const int src_linesize[4],
348+ enum AVPixelFormat pix_fmt,
349+ int width, int height, int align)
350+{
351+ int i, j, nb_planes = 0, linesize[4];
352+ int size = av_image_get_buffer_size(pix_fmt, width, height, align);
353+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
354+
355+ if (size > dst_size || size < 0 || !desc)
356+ return AVERROR(EINVAL);
357+
358+ for (i = 0; i < desc->nb_components; i++)
359+ nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
360+
361+ nb_planes++;
362+
363+ av_image_fill_linesizes(linesize, pix_fmt, width);
364+ for (i = 0; i < nb_planes; i++) {
365+ int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
366+ const uint8_t *src = src_data[i];
367+ h = (height + (1 << shift) - 1) >> shift;
368+
369+ for (j = 0; j < h; j++) {
370+ memcpy(dst, src, linesize[i]);
371+ dst += FFALIGN(linesize[i], align);
372+ src += src_linesize[i];
373+ }
374+ }
375+
376+ if (desc->flags & AV_PIX_FMT_FLAG_PAL)
377+ memcpy((unsigned char *)(((size_t)dst + 3) & ~3),
378+ src_data[1], 256 * 4);
379+
380+ return size;
381+}
libavutil/imgutils.h+59−0
@@ -117,6 +117,65 @@ void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], · 116 unmodified lines
117117 const uint8_t *src_data[4], const int src_linesizes[4],
118118 enum AVPixelFormat pix_fmt, int width, int height);
119119
120+/**
121+ * Setup the data pointers and linesizes based on the specified image
122+ * parameters and the provided array.
123+ *
124+ * The fields of the given image are filled in by using the src
125+ * address which points to the image data buffer. Depending on the
126+ * specified pixel format, one or multiple image data pointers and
127+ * line sizes will be set. If a planar format is specified, several
128+ * pointers will be set pointing to the different picture planes and
129+ * the line sizes of the different planes will be stored in the
130+ * lines_sizes array. Call with src == NULL to get the required
131+ * size for the src buffer.
132+ *
133+ * To allocate the buffer and fill in the dst_data and dst_linesize in
134+ * one call, use av_image_alloc().
135+ *
136+ * @param dst_data data pointers to be filled in
137+ * @param dst_linesizes linesizes for the image in dst_data to be filled in
138+ * @param src buffer which will contain or contains the actual image data, can be NULL
139+ * @param pix_fmt the pixel format of the image
140+ * @param width the width of the image in pixels
141+ * @param height the height of the image in pixels
142+ * @param align the value used in src for linesize alignment
143+ * @return the size in bytes required for src, a negative error code
144+ * in case of failure
145+ */
146+int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
147+ const uint8_t *src,
148+ enum PixelFormat pix_fmt, int width, int height, int align);
149+
150+/**
151+ * Return the size in bytes of the amount of data required to store an
152+ * image with the given parameters.
153+ *
154+ * @param[in] align the assumed linesize alignment
155+ */
156+int av_image_get_buffer_size(enum PixelFormat pix_fmt, int width, int height, int align);
157+
158+/**
159+ * Copy image data from an image into a buffer.
160+ *
161+ * av_image_get_buffer_size() can be used to compute the required size
162+ * for the buffer to fill.
163+ *
164+ * @param dst a buffer into which picture data will be copied
165+ * @param dst_size the size in bytes of dst
166+ * @param src_data pointers containing the source image data
167+ * @param src_linesizes linesizes for the image in src_data
168+ * @param pix_fmt the pixel format of the source image
169+ * @param width the width of the source image in pixels
170+ * @param height the height of the source image in pixels
171+ * @param align the assumed linesize alignment for dst
172+ * @return the number of bytes written to dst, or a negative value
173+ * (error code) on error
174+ */
175+int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
176+ const uint8_t * const src_data[4], const int src_linesize[4],
177+ enum PixelFormat pix_fmt, int width, int height, int align);
178+
120179 /**
121180 * Check if the given dimension of an image is valid, meaning that all
122181 * bytes of the image can be addressed with a signed int.
libavutil/version.h+1−1
@@ -54,7 +54,7 @@ · 53 unmodified lines
5454 */
5555
5656 #define LIBAVUTIL_VERSION_MAJOR 54
57−#define LIBAVUTIL_VERSION_MINOR 5
57+#define LIBAVUTIL_VERSION_MINOR 6
5858 #define LIBAVUTIL_VERSION_MICRO 0
5959
6060 #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