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 "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 & AV_PIX_FMT_FLAG_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 & AV_PIX_FMT_FLAG_HWACCEL)
76  return AVERROR(EINVAL);
77 
78  if (desc->flags & AV_PIX_FMT_FLAG_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 & AV_PIX_FMT_FLAG_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 & AV_PIX_FMT_FLAG_PAL ||
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  }
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 & AV_PIX_FMT_FLAG_HWACCEL)
251  return;
252 
253  if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
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 }