]> 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 "pixdesc.h"
27
28 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
29                                 const AVPixFmtDescriptor *pixdesc)
30 {
31     int i;
32     memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
33     if (max_pixstep_comps)
34         memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
35
36     for (i = 0; i < 4; i++) {
37         const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
38         if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
39             max_pixsteps[comp->plane] = comp->step_minus1+1;
40             if (max_pixstep_comps)
41                 max_pixstep_comps[comp->plane] = i;
42         }
43     }
44 }
45
46 static inline
47 int image_get_linesize(int width, int plane,
48                        int max_step, int max_step_comp,
49                        const AVPixFmtDescriptor *desc)
50 {
51     int s, shifted_w, linesize;
52
53     if (width < 0)
54         return AVERROR(EINVAL);
55     s = (max_step_comp == 1 || max_step_comp == 2) ? desc->log2_chroma_w : 0;
56     shifted_w = ((width + (1 << s) - 1)) >> s;
57     if (shifted_w && max_step > INT_MAX / shifted_w)
58         return AVERROR(EINVAL);
59     linesize = max_step * shifted_w;
60     if (desc->flags & PIX_FMT_BITSTREAM)
61         linesize = (linesize + 7) >> 3;
62     return linesize;
63 }
64
65 int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
66 {
67     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
68     int max_step     [4];       /* max pixel step for each plane */
69     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
70
71     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
72         return AVERROR(EINVAL);
73
74     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
75     return image_get_linesize(width, plane, max_step[plane], max_step_comp[plane], desc);
76 }
77
78 int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
79 {
80     int i, ret;
81     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
82     int max_step     [4];       /* max pixel step for each plane */
83     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
84
85     memset(linesizes, 0, 4*sizeof(linesizes[0]));
86
87     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
88         return AVERROR(EINVAL);
89
90     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
91     for (i = 0; i < 4; i++) {
92         if ((ret = image_get_linesize(width, i, max_step[i], max_step_comp[i], desc)) < 0)
93             return ret;
94         linesizes[i] = ret;
95     }
96
97     return 0;
98 }
99
100 int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
101                            uint8_t *ptr, const int linesizes[4])
102 {
103     int i, total_size, size[4], has_plane[4];
104
105     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
106     memset(data     , 0, sizeof(data[0])*4);
107     memset(size     , 0, sizeof(size));
108     memset(has_plane, 0, sizeof(has_plane));
109
110     if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
111         return AVERROR(EINVAL);
112
113     data[0] = ptr;
114     if (linesizes[0] > (INT_MAX - 1024) / height)
115         return AVERROR(EINVAL);
116     size[0] = linesizes[0] * height;
117
118     if (desc->flags & PIX_FMT_PAL) {
119         size[0] = (size[0] + 3) & ~3;
120         data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
121         return size[0] + 256 * 4;
122     }
123
124     for (i = 0; i < 4; i++)
125         has_plane[desc->comp[i].plane] = 1;
126
127     total_size = size[0];
128     for (i = 1; has_plane[i] && i < 4; i++) {
129         int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
130         data[i] = data[i-1] + size[i-1];
131         h = (height + (1 << s) - 1) >> s;
132         if (linesizes[i] > INT_MAX / h)
133             return AVERROR(EINVAL);
134         size[i] = h * linesizes[i];
135         if (total_size > INT_MAX - size[i])
136             return AVERROR(EINVAL);
137         total_size += size[i];
138     }
139
140     return total_size;
141 }
142
143 int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
144 {
145     int i;
146
147     for (i = 0; i < 256; i++) {
148         int r, g, b;
149
150         switch (pix_fmt) {
151         case PIX_FMT_RGB8:
152             r = (i>>5    )*36;
153             g = ((i>>2)&7)*36;
154             b = (i&3     )*85;
155             break;
156         case PIX_FMT_BGR8:
157             b = (i>>6    )*85;
158             g = ((i>>3)&7)*36;
159             r = (i&7     )*36;
160             break;
161         case PIX_FMT_RGB4_BYTE:
162             r = (i>>3    )*255;
163             g = ((i>>1)&3)*85;
164             b = (i&1     )*255;
165             break;
166         case PIX_FMT_BGR4_BYTE:
167             b = (i>>3    )*255;
168             g = ((i>>1)&3)*85;
169             r = (i&1     )*255;
170             break;
171         case PIX_FMT_GRAY8:
172             r = b = g = i;
173             break;
174         default:
175             return AVERROR(EINVAL);
176         }
177         pal[i] = b + (g<<8) + (r<<16);
178     }
179
180     return 0;
181 }
182
183 int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
184                    int w, int h, enum PixelFormat pix_fmt, int align)
185 {
186     int i, ret;
187     uint8_t *buf;
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 (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL)
207         ff_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
208
209     return ret;
210 }
211
212 typedef struct ImgUtils {
213     const AVClass *class;
214     int   log_offset;
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 PixelFormat pix_fmt, int width, int height)
247 {
248     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
249
250     if (desc->flags & PIX_FMT_HWACCEL)
251         return;
252
253     if (desc->flags & PIX_FMT_PAL) {
254         av_image_copy_plane(dst_data[0], dst_linesizes[0],
255                             src_data[0], src_linesizes[0],
256                             width, height);
257         /* copy the palette */
258         memcpy(dst_data[1], src_data[1], 4*256);
259     } else {
260         int i, planes_nb = 0;
261
262         for (i = 0; i < desc->nb_components; i++)
263             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
264
265         for (i = 0; i < planes_nb; i++) {
266             int h = height;
267             int bwidth = av_image_get_linesize(pix_fmt, width, i);
268             if (i == 1 || i == 2) {
269                 h= -((-height)>>desc->log2_chroma_h);
270             }
271             av_image_copy_plane(dst_data[i], dst_linesizes[i],
272                                 src_data[i], src_linesizes[i],
273                                 bwidth, h);
274         }
275     }
276 }