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