Libav
imgutils.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
24 #include "common.h"
25 #include "imgutils.h"
26 #include "internal.h"
27 #include "log.h"
28 #include "mathematics.h"
29 #include "pixdesc.h"
30 #include "rational.h"
31 
32 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
33  const AVPixFmtDescriptor *pixdesc)
34 {
35  int i;
36  memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
37  if (max_pixstep_comps)
38  memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
39 
40  for (i = 0; i < 4; i++) {
41  const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
42  if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
43  max_pixsteps[comp->plane] = comp->step_minus1+1;
44  if (max_pixstep_comps)
45  max_pixstep_comps[comp->plane] = i;
46  }
47  }
48 }
49 
51 {
52  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
53  int max_step [4]; /* max pixel step for each plane */
54  int max_step_comp[4]; /* the component for each plane which has the max pixel step */
55  int s;
56 
57  if (!desc)
58  return AVERROR(EINVAL);
59 
60  if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
61  return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
62 
63  av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
64  s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
65  return max_step[plane] * (((width + (1 << s) - 1)) >> s);
66 }
67 
68 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
69 {
70  int i;
71  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
72  int max_step [4]; /* max pixel step for each plane */
73  int max_step_comp[4]; /* the component for each plane which has the max pixel step */
74 
75  memset(linesizes, 0, 4*sizeof(linesizes[0]));
76 
77  if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
78  return AVERROR(EINVAL);
79 
80  if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
81  if (width > (INT_MAX -7) / (desc->comp[0].step_minus1+1))
82  return AVERROR(EINVAL);
83  linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
84  return 0;
85  }
86 
87  av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
88  for (i = 0; i < 4; i++) {
89  int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
90  int shifted_w = ((width + (1 << s) - 1)) >> s;
91  if (max_step[i] > INT_MAX / shifted_w)
92  return AVERROR(EINVAL);
93  linesizes[i] = max_step[i] * shifted_w;
94  }
95 
96  return 0;
97 }
98 
100  uint8_t *ptr, const int linesizes[4])
101 {
102  int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
103 
104  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
105  memset(data , 0, sizeof(data[0])*4);
106 
107  if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
108  return AVERROR(EINVAL);
109 
110  data[0] = ptr;
111  if (linesizes[0] > (INT_MAX - 1024) / height)
112  return AVERROR(EINVAL);
113  size[0] = linesizes[0] * height;
114 
115  if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
117  size[0] = (size[0] + 3) & ~3;
118  data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
119  return size[0] + 256 * 4;
120  }
121 
122  for (i = 0; i < 4; i++)
123  has_plane[desc->comp[i].plane] = 1;
124 
125  total_size = size[0];
126  for (i = 1; i < 4 && has_plane[i]; i++) {
127  int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
128  data[i] = data[i-1] + size[i-1];
129  h = (height + (1 << s) - 1) >> s;
130  if (linesizes[i] > INT_MAX / h)
131  return AVERROR(EINVAL);
132  size[i] = h * linesizes[i];
133  if (total_size > INT_MAX - size[i])
134  return AVERROR(EINVAL);
135  total_size += size[i];
136  }
137 
138  return total_size;
139 }
140 
142 {
143  int i;
144 
145  for (i = 0; i < 256; i++) {
146  int r, g, b;
147 
148  switch (pix_fmt) {
149  case AV_PIX_FMT_RGB8:
150  r = (i>>5 )*36;
151  g = ((i>>2)&7)*36;
152  b = (i&3 )*85;
153  break;
154  case AV_PIX_FMT_BGR8:
155  b = (i>>6 )*85;
156  g = ((i>>3)&7)*36;
157  r = (i&7 )*36;
158  break;
160  r = (i>>3 )*255;
161  g = ((i>>1)&3)*85;
162  b = (i&1 )*255;
163  break;
165  b = (i>>3 )*255;
166  g = ((i>>1)&3)*85;
167  r = (i&1 )*255;
168  break;
169  case AV_PIX_FMT_GRAY8:
170  r = b = g = i;
171  break;
172  default:
173  return AVERROR(EINVAL);
174  }
175  pal[i] = b + (g << 8) + (r << 16) + (0xFFU << 24);
176  }
177 
178  return 0;
179 }
180 
181 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
182  int w, int h, enum AVPixelFormat pix_fmt, int align)
183 {
184  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
185  int i, ret;
186  uint8_t *buf;
187 
188  if (!desc)
189  return AVERROR(EINVAL);
190 
191  if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
192  return ret;
193  if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
194  return ret;
195 
196  for (i = 0; i < 4; i++)
197  linesizes[i] = FFALIGN(linesizes[i], align);
198 
199  if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
200  return ret;
201  buf = av_malloc(ret + align);
202  if (!buf)
203  return AVERROR(ENOMEM);
204  if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
205  av_free(buf);
206  return ret;
207  }
209  avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
210 
211  return ret;
212 }
213 
214 typedef struct ImgUtils {
215  const AVClass *class;
217  void *log_ctx;
218 } ImgUtils;
219 
220 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
221 
222 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
223 {
224  ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
225 
226  if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
227  return 0;
228 
229  av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
230  return AVERROR(EINVAL);
231 }
232 
233 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
234 {
235  int64_t scaled_dim;
236 
237  if (!sar.den)
238  return AVERROR(EINVAL);
239 
240  if (!sar.num || sar.num == sar.den)
241  return 0;
242 
243  if (sar.num < sar.den)
244  scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
245  else
246  scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
247 
248  if (scaled_dim > 0)
249  return 0;
250 
251  return AVERROR(EINVAL);
252 }
253 
254 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
255  const uint8_t *src, int src_linesize,
256  int bytewidth, int height)
257 {
258  if (!dst || !src)
259  return;
260  for (;height > 0; height--) {
261  memcpy(dst, src, bytewidth);
262  dst += dst_linesize;
263  src += src_linesize;
264  }
265 }
266 
267 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
268  const uint8_t *src_data[4], const int src_linesizes[4],
269  enum AVPixelFormat pix_fmt, int width, int height)
270 {
271  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
272 
273  if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
274  return;
275 
276  if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
278  av_image_copy_plane(dst_data[0], dst_linesizes[0],
279  src_data[0], src_linesizes[0],
280  width, height);
281  /* copy the palette */
282  memcpy(dst_data[1], src_data[1], 4*256);
283  } else {
284  int i, planes_nb = 0;
285 
286  for (i = 0; i < desc->nb_components; i++)
287  planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
288 
289  for (i = 0; i < planes_nb; i++) {
290  int h = height;
291  int bwidth = av_image_get_linesize(pix_fmt, width, i);
292  if (i == 1 || i == 2) {
293  h= -((-height)>>desc->log2_chroma_h);
294  }
295  av_image_copy_plane(dst_data[i], dst_linesizes[i],
296  src_data[i], src_linesizes[i],
297  bwidth, h);
298  }
299  }
300 }
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:112
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane...
Definition: imgutils.c:50
int size
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1599
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:61
misc image utilities
int av_image_alloc(uint8_t *pointers[4], int linesizes[4], int w, int h, enum AVPixelFormat pix_fmt, int align)
Allocate an image with size w and h and pixel format pix_fmt, and fill pointers and linesizes accordi...
Definition: imgutils.c:181
int num
numerator
Definition: rational.h:44
static const AVClass imgutils_class
Definition: imgutils.c:220
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
#define FFALIGN(x, a)
Definition: common.h:62
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:88
void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4], const AVPixFmtDescriptor *pixdesc)
Compute the max pixel step for each plane of an image with a format described by pixdesc.
Definition: imgutils.c:32
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:97
uint8_t
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition: imgutils.c:141
#define b
Definition: input.c:52
void * log_ctx
Definition: imgutils.c:217
const char data[16]
Definition: mxf.c:70
int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
Check if the given sample aspect ratio of an image is valid.
Definition: imgutils.c:233
#define r
Definition: input.c:51
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
#define AVERROR(e)
Definition: error.h:43
g
Definition: yuv2rgb.c:535
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:120
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
#define FFMAX(a, b)
Definition: common.h:55
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4], const uint8_t *src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition: imgutils.c:267
common internal API header
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:222
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:91
Round toward zero.
Definition: mathematics.h:50
#define AV_PIX_FMT_FLAG_PSEUDOPAL
The pixel format is "pseudo-paletted".
Definition: pixdesc.h:134
enum AVPixelFormat pix_fmt
Definition: movenc.c:843
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
int log_offset
Definition: imgutils.c:216
NULL
Definition: eval.c:55
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:86
static int width
Definition: utils.c:156
int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height, uint8_t *ptr, const int linesizes[4])
Fill plane data pointers for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:99
av_default_item_name
Definition: dnxhdenc.c:52
uint8_t flags
Definition: pixdesc.h:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
Describe the class of an AVClass context structure.
Definition: log.h:33
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:68
rational number numerator/denominator
Definition: rational.h:43
uint16_t step_minus1
Number of elements between 2 horizontally consecutive pixels minus 1.
Definition: pixdesc.h:40
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:116
int height
Definition: gxfenc.c:72
uint16_t plane
Which of the 4 planes contains the component.
Definition: pixdesc.h:34
Y , 8bpp.
Definition: pixfmt.h:73
common internal and external API header
rational numbers
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:89
int den
denominator
Definition: rational.h:45
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:83
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:254
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
for(j=16;j >0;--j)
Definition: vf_drawbox.c:37