]> git.sesse.net Git - ffmpeg/blob - libavutil/imgutils.c
Merge remote-tracking branch 'qatar/master'
[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 "imgutils.h"
25 #include "internal.h"
26 #include "log.h"
27 #include "pixdesc.h"
28
29 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
30                                 const AVPixFmtDescriptor *pixdesc)
31 {
32     int i;
33     memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
34     if (max_pixstep_comps)
35         memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
36
37     for (i = 0; i < 4; i++) {
38         const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
39         if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
40             max_pixsteps[comp->plane] = comp->step_minus1+1;
41             if (max_pixstep_comps)
42                 max_pixstep_comps[comp->plane] = i;
43         }
44     }
45 }
46
47 static inline
48 int image_get_linesize(int width, int plane,
49                        int max_step, int max_step_comp,
50                        const AVPixFmtDescriptor *desc)
51 {
52     int s, shifted_w, linesize;
53
54     if (width < 0)
55         return AVERROR(EINVAL);
56     s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
57     shifted_w = ((width + (1 << s) - 1)) >> s;
58     if (shifted_w && max_step > INT_MAX / shifted_w)
59         return AVERROR(EINVAL);
60     linesize = max_step * shifted_w;
61     if (desc->flags & PIX_FMT_BITSTREAM)
62         linesize = (linesize + 7) >> 3;
63     return linesize;
64 }
65
66 int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
67 {
68     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
69     int max_step     [4];       /* max pixel step for each plane */
70     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
71
72     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
73         return AVERROR(EINVAL);
74
75     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
76     return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
77 }
78
79 int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
80 {
81     int i, ret;
82     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
83     int max_step     [4];       /* max pixel step for each plane */
84     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
85
86     memset(linesizes, 0, 4*sizeof(linesizes[0]));
87
88     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
89         return AVERROR(EINVAL);
90
91     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
92     for (i = 0; i < 4; i++) {
93         if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
94             return ret;
95         linesizes[i] = ret;
96     }
97
98     return 0;
99 }
100
101 int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
102                            uint8_t *ptr, const int linesizes[4])
103 {
104     int i, total_size, size[4], has_plane[4];
105
106     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
107     memset(data     , 0, sizeof(data[0])*4);
108     memset(size     , 0, sizeof(size));
109     memset(has_plane, 0, sizeof(has_plane));
110
111     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
112         return AVERROR(EINVAL);
113
114     data[0] = ptr;
115     if (linesizes[0] > (INT_MAX - 1024) / height)
116         return AVERROR(EINVAL);
117     size[0] = linesizes[0] * height;
118
119     if (desc->flags & PIX_FMT_PAL ||
120         desc->flags & PIX_FMT_PSEUDOPAL) {
121         size[0] = (size[0] + 3) & ~3;
122         data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
123         return size[0] + 256 * 4;
124     }
125
126     for (i = 0; i < 4; i++)
127         has_plane[desc->comp[i].plane] = 1;
128
129     total_size = size[0];
130     for (i = 1; i < 4 && has_plane[i]; i++) {
131         int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
132         data[i] = data[i-1] + size[i-1];
133         h = (height + (1 << s) - 1) >> s;
134         if (linesizes[i] > INT_MAX / h)
135             return AVERROR(EINVAL);
136         size[i] = h * linesizes[i];
137         if (total_size > INT_MAX - size[i])
138             return AVERROR(EINVAL);
139         total_size += size[i];
140     }
141
142     return total_size;
143 }
144
145 int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
146 {
147     int i;
148
149     for (i = 0; i < 256; i++) {
150         int r, g, b;
151
152         switch (pix_fmt) {
153         case PIX_FMT_RGB8:
154             r = (i>>5    )*36;
155             g = ((i>>2)&7)*36;
156             b = (i&3     )*85;
157             break;
158         case PIX_FMT_BGR8:
159             b = (i>>6    )*85;
160             g = ((i>>3)&7)*36;
161             r = (i&7     )*36;
162             break;
163         case PIX_FMT_RGB4_BYTE:
164             r = (i>>3    )*255;
165             g = ((i>>1)&3)*85;
166             b = (i&1     )*255;
167             break;
168         case PIX_FMT_BGR4_BYTE:
169             b = (i>>3    )*255;
170             g = ((i>>1)&3)*85;
171             r = (i&1     )*255;
172             break;
173         case PIX_FMT_GRAY8:
174             r = b = g = i;
175             break;
176         default:
177             return AVERROR(EINVAL);
178         }
179         pal[i] = b + (g<<8) + (r<<16) + (0xFF<<24);
180     }
181
182     return 0;
183 }
184
185 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
186                    int w, int h, enum PixelFormat pix_fmt, int align)
187 {
188     int i, ret;
189     uint8_t *buf;
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     }
208     if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL ||
209         av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PSEUDOPAL)
210         ff_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
211
212     return ret;
213 }
214
215 typedef struct ImgUtils {
216     const AVClass *class;
217     int   log_offset;
218     void *log_ctx;
219 } ImgUtils;
220
221 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
222
223 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
224 {
225     ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
226
227     if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
228         return 0;
229
230     av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
231     return AVERROR(EINVAL);
232 }
233
234 void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
235                          const uint8_t *src, int src_linesize,
236                          int bytewidth, int height)
237 {
238     if (!dst || !src)
239         return;
240     for (;height > 0; height--) {
241         memcpy(dst, src, bytewidth);
242         dst += dst_linesize;
243         src += src_linesize;
244     }
245 }
246
247 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
248                    const uint8_t *src_data[4], const int src_linesizes[4],
249                    enum PixelFormat pix_fmt, int width, int height)
250 {
251     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
252
253     if (desc->flags & PIX_FMT_HWACCEL)
254         return;
255
256     if (desc->flags & PIX_FMT_PAL ||
257         desc->flags & PIX_FMT_PSEUDOPAL) {
258         av_image_copy_plane(dst_data[0], dst_linesizes[0],
259                             src_data[0], src_linesizes[0],
260                             width, height);
261         /* copy the palette */
262         memcpy(dst_data[1], src_data[1], 4*256);
263     } else {
264         int i, planes_nb = 0;
265
266         for (i = 0; i < desc->nb_components; i++)
267             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
268
269         for (i = 0; i < planes_nb; i++) {
270             int h = height;
271             int bwidth = av_image_get_linesize(pix_fmt, width, i);
272             if (i == 1 || i == 2) {
273                 h= -((-height)>>desc->log2_chroma_h);
274             }
275             av_image_copy_plane(dst_data[i], dst_linesizes[i],
276                                 src_data[i], src_linesizes[i],
277                                 bwidth, h);
278         }
279     }
280 }