]> git.sesse.net Git - ffmpeg/blob - libavutil/imgutils.c
Merge commit '14f031d7ecfabba0ef02776d4516aa3dcb7c40d8'
[ffmpeg] / libavutil / imgutils.c
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 /**
20  * @file
21  * misc image utilities
22  */
23
24 #include "common.h"
25 #include "imgutils.h"
26 #include "internal.h"
27 #include "intreadwrite.h"
28 #include "log.h"
29 #include "pixdesc.h"
30
31 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
32                                 const AVPixFmtDescriptor *pixdesc)
33 {
34     int i;
35     memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
36     if (max_pixstep_comps)
37         memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
38
39     for (i = 0; i < 4; i++) {
40         const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
41         if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
42             max_pixsteps[comp->plane] = comp->step_minus1+1;
43             if (max_pixstep_comps)
44                 max_pixstep_comps[comp->plane] = i;
45         }
46     }
47 }
48
49 static inline
50 int image_get_linesize(int width, int plane,
51                        int max_step, int max_step_comp,
52                        const AVPixFmtDescriptor *desc)
53 {
54     int s, shifted_w, linesize;
55
56     if (!desc)
57         return AVERROR(EINVAL);
58
59     if (width < 0)
60         return AVERROR(EINVAL);
61     s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
62     shifted_w = ((width + (1 << s) - 1)) >> s;
63     if (shifted_w && max_step > INT_MAX / shifted_w)
64         return AVERROR(EINVAL);
65     linesize = max_step * shifted_w;
66
67     if (desc->flags & PIX_FMT_BITSTREAM)
68         linesize = (linesize + 7) >> 3;
69     return linesize;
70 }
71
72 int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
73 {
74     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
75     int max_step     [4];       /* max pixel step for each plane */
76     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
77
78     if ((unsigned)pix_fmt >= AV_PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
79         return AVERROR(EINVAL);
80
81     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
82     return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
83 }
84
85 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
86 {
87     int i, ret;
88     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
89     int max_step     [4];       /* max pixel step for each plane */
90     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
91
92     memset(linesizes, 0, 4*sizeof(linesizes[0]));
93
94     if (!desc || desc->flags & PIX_FMT_HWACCEL)
95         return AVERROR(EINVAL);
96
97     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
98     for (i = 0; i < 4; i++) {
99         if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
100             return ret;
101         linesizes[i] = ret;
102     }
103
104     return 0;
105 }
106
107 int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
108                            uint8_t *ptr, const int linesizes[4])
109 {
110     int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
111
112     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
113     memset(data     , 0, sizeof(data[0])*4);
114
115     if (!desc || desc->flags & PIX_FMT_HWACCEL)
116         return AVERROR(EINVAL);
117
118     data[0] = ptr;
119     if (linesizes[0] > (INT_MAX - 1024) / height)
120         return AVERROR(EINVAL);
121     size[0] = linesizes[0] * height;
122
123     if (desc->flags & PIX_FMT_PAL ||
124         desc->flags & PIX_FMT_PSEUDOPAL) {
125         size[0] = (size[0] + 3) & ~3;
126         data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
127         return size[0] + 256 * 4;
128     }
129
130     for (i = 0; i < 4; i++)
131         has_plane[desc->comp[i].plane] = 1;
132
133     total_size = size[0];
134     for (i = 1; i < 4 && has_plane[i]; i++) {
135         int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
136         data[i] = data[i-1] + size[i-1];
137         h = (height + (1 << s) - 1) >> s;
138         if (linesizes[i] > INT_MAX / h)
139             return AVERROR(EINVAL);
140         size[i] = h * linesizes[i];
141         if (total_size > INT_MAX - size[i])
142             return AVERROR(EINVAL);
143         total_size += size[i];
144     }
145
146     return total_size;
147 }
148
149 int avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat pix_fmt)
150 {
151     int i;
152
153     for (i = 0; i < 256; i++) {
154         int r, g, b;
155
156         switch (pix_fmt) {
157         case AV_PIX_FMT_RGB8:
158             r = (i>>5    )*36;
159             g = ((i>>2)&7)*36;
160             b = (i&3     )*85;
161             break;
162         case AV_PIX_FMT_BGR8:
163             b = (i>>6    )*85;
164             g = ((i>>3)&7)*36;
165             r = (i&7     )*36;
166             break;
167         case AV_PIX_FMT_RGB4_BYTE:
168             r = (i>>3    )*255;
169             g = ((i>>1)&3)*85;
170             b = (i&1     )*255;
171             break;
172         case AV_PIX_FMT_BGR4_BYTE:
173             b = (i>>3    )*255;
174             g = ((i>>1)&3)*85;
175             r = (i&1     )*255;
176             break;
177         case AV_PIX_FMT_GRAY8:
178             r = b = g = i;
179             break;
180         default:
181             return AVERROR(EINVAL);
182         }
183         pal[i] = b + (g<<8) + (r<<16) + (0xFFU<<24);
184     }
185
186     return 0;
187 }
188
189 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
190                    int w, int h, enum AVPixelFormat pix_fmt, int align)
191 {
192     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
193     int i, ret;
194     uint8_t *buf;
195
196     if (!desc)
197         return AVERROR(EINVAL);
198
199     if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
200         return ret;
201     if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, align>7 ? FFALIGN(w, 8) : w)) < 0)
202         return ret;
203
204     for (i = 0; i < 4; i++)
205         linesizes[i] = FFALIGN(linesizes[i], align);
206
207     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
208         return ret;
209     buf = av_malloc(ret + align);
210     if (!buf)
211         return AVERROR(ENOMEM);
212     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
213         av_free(buf);
214         return ret;
215     }
216     if (desc->flags & PIX_FMT_PAL || desc->flags & PIX_FMT_PSEUDOPAL)
217         avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
218
219     return ret;
220 }
221
222 typedef struct ImgUtils {
223     const AVClass *class;
224     int   log_offset;
225     void *log_ctx;
226 } ImgUtils;
227
228 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
229
230 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
231 {
232     ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
233
234     if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
235         return 0;
236
237     av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
238     return AVERROR(EINVAL);
239 }
240
241 void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
242                          const uint8_t *src, int src_linesize,
243                          int bytewidth, int height)
244 {
245     if (!dst || !src)
246         return;
247     for (;height > 0; height--) {
248         memcpy(dst, src, bytewidth);
249         dst += dst_linesize;
250         src += src_linesize;
251     }
252 }
253
254 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
255                    const uint8_t *src_data[4], const int src_linesizes[4],
256                    enum AVPixelFormat pix_fmt, int width, int height)
257 {
258     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
259
260     if (!desc || desc->flags & PIX_FMT_HWACCEL)
261         return;
262
263     if (desc->flags & PIX_FMT_PAL ||
264         desc->flags & PIX_FMT_PSEUDOPAL) {
265         av_image_copy_plane(dst_data[0], dst_linesizes[0],
266                             src_data[0], src_linesizes[0],
267                             width, height);
268         /* copy the palette */
269         memcpy(dst_data[1], src_data[1], 4*256);
270     } else {
271         int i, planes_nb = 0;
272
273         for (i = 0; i < desc->nb_components; i++)
274             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
275
276         for (i = 0; i < planes_nb; i++) {
277             int h = height;
278             int bwidth = av_image_get_linesize(pix_fmt, width, i);
279             if (i == 1 || i == 2) {
280                 h= -((-height)>>desc->log2_chroma_h);
281             }
282             av_image_copy_plane(dst_data[i], dst_linesizes[i],
283                                 src_data[i], src_linesizes[i],
284                                 bwidth, h);
285         }
286     }
287 }
288
289 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
290                          const uint8_t *src,
291                          enum AVPixelFormat pix_fmt, int width, int height, int align)
292 {
293     int ret, i;
294
295     if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
296         return ret;
297
298     if ((ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width)) < 0)
299         return ret;
300
301     for (i = 0; i < 4; i++)
302         dst_linesize[i] = FFALIGN(dst_linesize[i], align);
303
304     if ((ret = av_image_fill_pointers(dst_data, pix_fmt, width, NULL, dst_linesize)) < 0)
305         return ret;
306
307     return av_image_fill_pointers(dst_data, pix_fmt, height, (uint8_t *)src, dst_linesize);
308 }
309
310 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt, int width, int height, int align)
311 {
312     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
313     uint8_t *data[4];
314     int linesize[4];
315
316     if (!desc)
317         return AVERROR(EINVAL);
318     if (av_image_check_size(width, height, 0, NULL) < 0)
319         return AVERROR(EINVAL);
320     if (desc->flags & PIX_FMT_PSEUDOPAL)
321         // do not include palette for these pseudo-paletted formats
322         return width * height;
323     return av_image_fill_arrays(data, linesize, NULL, pix_fmt, width, height, align);
324 }
325
326 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
327                             const uint8_t * const src_data[4], const int src_linesize[4],
328                             enum AVPixelFormat pix_fmt, int width, int height, int align)
329 {
330     int i, j, nb_planes = 0, linesize[4];
331     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
332     int size = av_image_get_buffer_size(pix_fmt, width, height, align);
333
334     if (size > dst_size || size < 0)
335         return AVERROR(EINVAL);
336
337     for (i = 0; i < desc->nb_components; i++)
338         nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
339     nb_planes++;
340
341     av_image_fill_linesizes(linesize, pix_fmt, width);
342     for (i = 0; i < nb_planes; i++) {
343         int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
344         const uint8_t *src = src_data[i];
345         h = (height + (1 << shift) - 1) >> shift;
346
347         for (j = 0; j < h; j++) {
348             memcpy(dst, src, linesize[i]);
349             dst += FFALIGN(linesize[i], align);
350             src += src_linesize[i];
351         }
352     }
353
354     if (desc->flags & PIX_FMT_PAL) {
355         uint32_t *d32 = (uint32_t *)(((size_t)dst + 3) & ~3);
356         for (i = 0; i<256; i++)
357             AV_WL32(d32 + i, AV_RN32(src_data[1] + 4*i));
358     }
359
360     return size;
361 }