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 "pixdesc.h"
29 
30 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
31  const AVPixFmtDescriptor *pixdesc)
32 {
33  int i;
34  memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
35  if (max_pixstep_comps)
36  memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
37 
38  for (i = 0; i < 4; i++) {
39  const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
40  if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
41  max_pixsteps[comp->plane] = comp->step_minus1+1;
42  if (max_pixstep_comps)
43  max_pixstep_comps[comp->plane] = i;
44  }
45  }
46 }
47 
49 {
50  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
51  int max_step [4]; /* max pixel step for each plane */
52  int max_step_comp[4]; /* the component for each plane which has the max pixel step */
53  int s;
54 
55  if (!desc)
56  return AVERROR(EINVAL);
57 
58  if (desc->flags & PIX_FMT_BITSTREAM)
59  return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
60 
61  av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
62  s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
63  return max_step[plane] * (((width + (1 << s) - 1)) >> s);
64 }
65 
66 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
67 {
68  int i;
69  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
70  int max_step [4]; /* max pixel step for each plane */
71  int max_step_comp[4]; /* the component for each plane which has the max pixel step */
72 
73  memset(linesizes, 0, 4*sizeof(linesizes[0]));
74 
75  if (!desc || desc->flags & PIX_FMT_HWACCEL)
76  return AVERROR(EINVAL);
77 
78  if (desc->flags & PIX_FMT_BITSTREAM) {
79  if (width > (INT_MAX -7) / (desc->comp[0].step_minus1+1))
80  return AVERROR(EINVAL);
81  linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
82  return 0;
83  }
84 
85  av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
86  for (i = 0; i < 4; i++) {
87  int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
88  int shifted_w = ((width + (1 << s) - 1)) >> s;
89  if (max_step[i] > INT_MAX / shifted_w)
90  return AVERROR(EINVAL);
91  linesizes[i] = max_step[i] * shifted_w;
92  }
93 
94  return 0;
95 }
96 
98  uint8_t *ptr, const int linesizes[4])
99 {
100  int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
101 
102  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
103  memset(data , 0, sizeof(data[0])*4);
104 
105  if (!desc || desc->flags & PIX_FMT_HWACCEL)
106  return AVERROR(EINVAL);
107 
108  data[0] = ptr;
109  if (linesizes[0] > (INT_MAX - 1024) / height)
110  return AVERROR(EINVAL);
111  size[0] = linesizes[0] * height;
112 
113  if (desc->flags & PIX_FMT_PAL ||
114  desc->flags & PIX_FMT_PSEUDOPAL) {
115  size[0] = (size[0] + 3) & ~3;
116  data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
117  return size[0] + 256 * 4;
118  }
119 
120  for (i = 0; i < 4; i++)
121  has_plane[desc->comp[i].plane] = 1;
122 
123  total_size = size[0];
124  for (i = 1; i < 4 && has_plane[i]; i++) {
125  int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
126  data[i] = data[i-1] + size[i-1];
127  h = (height + (1 << s) - 1) >> s;
128  if (linesizes[i] > INT_MAX / h)
129  return AVERROR(EINVAL);
130  size[i] = h * linesizes[i];
131  if (total_size > INT_MAX - size[i])
132  return AVERROR(EINVAL);
133  total_size += size[i];
134  }
135 
136  return total_size;
137 }
138 
140 {
141  int i;
142 
143  for (i = 0; i < 256; i++) {
144  int r, g, b;
145 
146  switch (pix_fmt) {
147  case AV_PIX_FMT_RGB8:
148  r = (i>>5 )*36;
149  g = ((i>>2)&7)*36;
150  b = (i&3 )*85;
151  break;
152  case AV_PIX_FMT_BGR8:
153  b = (i>>6 )*85;
154  g = ((i>>3)&7)*36;
155  r = (i&7 )*36;
156  break;
158  r = (i>>3 )*255;
159  g = ((i>>1)&3)*85;
160  b = (i&1 )*255;
161  break;
163  b = (i>>3 )*255;
164  g = ((i>>1)&3)*85;
165  r = (i&1 )*255;
166  break;
167  case AV_PIX_FMT_GRAY8:
168  r = b = g = i;
169  break;
170  default:
171  return AVERROR(EINVAL);
172  }
173  pal[i] = b + (g<<8) + (r<<16);
174  }
175 
176  return 0;
177 }
178 
179 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
180  int w, int h, enum AVPixelFormat pix_fmt, int align)
181 {
182  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
183  int i, ret;
184  uint8_t *buf;
185 
186  if (!desc)
187  return AVERROR(EINVAL);
188 
189  if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
190  return ret;
191  if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
192  return ret;
193 
194  for (i = 0; i < 4; i++)
195  linesizes[i] = FFALIGN(linesizes[i], align);
196 
197  if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
198  return ret;
199  buf = av_malloc(ret + align);
200  if (!buf)
201  return AVERROR(ENOMEM);
202  if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
203  av_free(buf);
204  return ret;
205  }
206  if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL)
207  avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
208 
209  return ret;
210 }
211 
212 typedef struct ImgUtils {
213  const AVClass *class;
215  void *log_ctx;
216 } ImgUtils;
217 
218 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
219 
220 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
221 {
222  ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
223 
224  if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
225  return 0;
226 
227  av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
228  return AVERROR(EINVAL);
229 }
230 
231 void av_image_copy_plane(uint8_t *dst, int dst_linesize,
232  const uint8_t *src, int src_linesize,
233  int bytewidth, int height)
234 {
235  if (!dst || !src)
236  return;
237  for (;height > 0; height--) {
238  memcpy(dst, src, bytewidth);
239  dst += dst_linesize;
240  src += src_linesize;
241  }
242 }
243 
244 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
245  const uint8_t *src_data[4], const int src_linesizes[4],
246  enum AVPixelFormat pix_fmt, int width, int height)
247 {
248  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
249 
250  if (!desc || desc->flags & PIX_FMT_HWACCEL)
251  return;
252 
253  if (desc->flags & PIX_FMT_PAL ||
254  desc->flags & PIX_FMT_PSEUDOPAL) {
255  av_image_copy_plane(dst_data[0], dst_linesizes[0],
256  src_data[0], src_linesizes[0],
257  width, height);
258  /* copy the palette */
259  memcpy(dst_data[1], src_data[1], 4*256);
260  } else {
261  int i, planes_nb = 0;
262 
263  for (i = 0; i < desc->nb_components; i++)
264  planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
265 
266  for (i = 0; i < planes_nb; i++) {
267  int h = height;
268  int bwidth = av_image_get_linesize(pix_fmt, width, i);
269  if (i == 1 || i == 2) {
270  h= -((-height)>>desc->log2_chroma_h);
271  }
272  av_image_copy_plane(dst_data[i], dst_linesizes[i],
273  src_data[i], src_linesizes[i],
274  bwidth, h);
275  }
276  }
277 }
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:61
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:48
int size
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1435
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:179
static const AVClass imgutils_class
Definition: imgutils.c:218
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:66
packed RGB 1:2:1, 8bpp, (msb)1B 2G 1R(lsb)
Definition: pixfmt.h:86
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:30
AVComponentDescriptor comp[4]
Parameters that describe how pixels are packed.
Definition: pixdesc.h:83
uint8_t
#define b
Definition: input.c:52
void * log_ctx
Definition: imgutils.c:215
const char data[16]
Definition: mxf.c:66
#define r
Definition: input.c:51
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
g
Definition: yuv2rgb.c:540
#define PIX_FMT_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:87
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
Definition: imgutils.c:139
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:244
struct ImgUtils ImgUtils
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:220
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:57
packed RGB 1:2:1, 8bpp, (msb)1R 2G 1B(lsb)
Definition: pixfmt.h:89
#define PIX_FMT_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:88
enum AVPixelFormat pix_fmt
Definition: movenc.c:801
LIBAVUTIL_VERSION_INT
Definition: eval.c:52
int log_offset
Definition: imgutils.c:214
#define PIX_FMT_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:89
NULL
Definition: eval.c:52
packed RGB 3:3:2, 8bpp, (msb)2B 3G 3R(lsb)
Definition: pixfmt.h:84
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:97
av_default_item_name
Definition: dnxhdenc.c:43
uint8_t flags
Definition: pixdesc.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
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:66
uint16_t step_minus1
Number of elements between 2 horizontally consecutive pixels minus 1.
Definition: pixdesc.h:35
int height
Definition: gxfenc.c:72
uint16_t plane
which of the 4 planes contains the component
Definition: pixdesc.h:29
Y , 8bpp.
Definition: pixfmt.h:73
common internal and external API header
packed RGB 3:3:2, 8bpp, (msb)2R 3G 3B(lsb)
Definition: pixfmt.h:87
#define PIX_FMT_PSEUDOPAL
The pixel format is "pseudo-paletted".
Definition: pixdesc.h:97
static void comp(unsigned char *dst, int dst_stride, unsigned char *src, int src_stride, int add)
Definition: eamad.c:74
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:231
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
for(j=16;j >0;--j)