]> git.sesse.net Git - ffmpeg/blob - libavutil/imgutils.c
avio: add a destructor for AVIOContext
[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 "imgutils_internal.h"
27 #include "internal.h"
28 #include "intreadwrite.h"
29 #include "log.h"
30 #include "mathematics.h"
31 #include "pixdesc.h"
32 #include "rational.h"
33
34 void av_image_fill_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
35                                 const AVPixFmtDescriptor *pixdesc)
36 {
37     int i;
38     memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
39     if (max_pixstep_comps)
40         memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
41
42     for (i = 0; i < 4; i++) {
43         const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
44         if (comp->step > max_pixsteps[comp->plane]) {
45             max_pixsteps[comp->plane] = comp->step;
46             if (max_pixstep_comps)
47                 max_pixstep_comps[comp->plane] = i;
48         }
49     }
50 }
51
52 int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
53 {
54     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
55     int max_step     [4];       /* max pixel step for each plane */
56     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
57     int s;
58
59     if (!desc)
60         return AVERROR(EINVAL);
61
62     if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM)
63         return (width * desc->comp[0].step + 7) >> 3;
64
65     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
66     s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
67     return max_step[plane] * (((width + (1 << s) - 1)) >> s);
68 }
69
70 int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
71 {
72     int i;
73     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
74     int max_step     [4];       /* max pixel step for each plane */
75     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
76
77     memset(linesizes, 0, 4*sizeof(linesizes[0]));
78
79     if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
80         return AVERROR(EINVAL);
81
82     if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
83         if (width > (INT_MAX - 7) / desc->comp[0].step)
84             return AVERROR(EINVAL);
85         linesizes[0] = (width * desc->comp[0].step + 7) >> 3;
86         return 0;
87     }
88
89     av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
90     for (i = 0; i < 4; i++) {
91         int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
92         int shifted_w = ((width + (1 << s) - 1)) >> s;
93         if (max_step[i] > INT_MAX / shifted_w)
94             return AVERROR(EINVAL);
95         linesizes[i] = max_step[i] * shifted_w;
96     }
97
98     return 0;
99 }
100
101 int av_image_fill_pointers(uint8_t *data[4], enum AVPixelFormat pix_fmt, int height,
102                            uint8_t *ptr, const int linesizes[4])
103 {
104     int i, total_size, size[4] = { 0 }, has_plane[4] = { 0 };
105
106     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
107     memset(data     , 0, sizeof(data[0])*4);
108
109     if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
110         return AVERROR(EINVAL);
111
112     data[0] = ptr;
113     if (linesizes[0] > (INT_MAX - 1024) / height)
114         return AVERROR(EINVAL);
115     size[0] = linesizes[0] * height;
116
117     if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
118         desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
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; i < 4 && has_plane[i]; 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 avpriv_set_systematic_pal2(uint32_t pal[256], enum AVPixelFormat 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 AV_PIX_FMT_RGB8:
152             r = (i>>5    )*36;
153             g = ((i>>2)&7)*36;
154             b = (i&3     )*85;
155             break;
156         case AV_PIX_FMT_BGR8:
157             b = (i>>6    )*85;
158             g = ((i>>3)&7)*36;
159             r = (i&7     )*36;
160             break;
161         case AV_PIX_FMT_RGB4_BYTE:
162             r = (i>>3    )*255;
163             g = ((i>>1)&3)*85;
164             b = (i&1     )*255;
165             break;
166         case AV_PIX_FMT_BGR4_BYTE:
167             b = (i>>3    )*255;
168             g = ((i>>1)&3)*85;
169             r = (i&1     )*255;
170             break;
171         case AV_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) + (0xFFU << 24);
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 AVPixelFormat pix_fmt, int align)
185 {
186     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
187     int i, ret;
188     uint8_t *buf;
189
190     if (!desc)
191         return AVERROR(EINVAL);
192
193     if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
194         return ret;
195     if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
196         return ret;
197
198     for (i = 0; i < 4; i++)
199         linesizes[i] = FFALIGN(linesizes[i], align);
200
201     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
202         return ret;
203     buf = av_malloc(ret + align);
204     if (!buf)
205         return AVERROR(ENOMEM);
206     if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
207         av_free(buf);
208         return ret;
209     }
210     if (desc->flags & AV_PIX_FMT_FLAG_PAL || desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
211         avpriv_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
212
213     return ret;
214 }
215
216 typedef struct ImgUtils {
217     const AVClass *class;
218     int   log_offset;
219     void *log_ctx;
220 } ImgUtils;
221
222 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
223
224 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
225 {
226     ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
227
228     if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
229         return 0;
230
231     av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
232     return AVERROR(EINVAL);
233 }
234
235 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
236 {
237     int64_t scaled_dim;
238
239     if (!sar.den)
240         return AVERROR(EINVAL);
241
242     if (!sar.num || sar.num == sar.den)
243         return 0;
244
245     if (sar.num < sar.den)
246         scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
247     else
248         scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
249
250     if (scaled_dim > 0)
251         return 0;
252
253     return AVERROR(EINVAL);
254 }
255
256 static void image_copy_plane(uint8_t       *dst, ptrdiff_t dst_linesize,
257                              const uint8_t *src, ptrdiff_t src_linesize,
258                              ptrdiff_t bytewidth, int height)
259 {
260     if (!dst || !src)
261         return;
262     for (;height > 0; height--) {
263         memcpy(dst, src, bytewidth);
264         dst += dst_linesize;
265         src += src_linesize;
266     }
267 }
268
269 static void image_copy_plane_uc_from(uint8_t       *dst, ptrdiff_t dst_linesize,
270                                      const uint8_t *src, ptrdiff_t src_linesize,
271                                      ptrdiff_t bytewidth, int height)
272 {
273     int ret = -1;
274
275 #if ARCH_X86
276     ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
277                                           bytewidth, height);
278 #endif
279
280     if (ret < 0)
281         image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
282 }
283
284 void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
285                          const uint8_t *src, int src_linesize,
286                          int bytewidth, int height)
287 {
288     image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
289 }
290
291 static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
292                        const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
293                        enum AVPixelFormat pix_fmt, int width, int height,
294                        void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
295                                           ptrdiff_t, ptrdiff_t, int))
296 {
297     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
298
299     if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
300         return;
301
302     if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
303         desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
304         copy_plane(dst_data[0], dst_linesizes[0],
305                    src_data[0], src_linesizes[0],
306                    width, height);
307         /* copy the palette */
308         memcpy(dst_data[1], src_data[1], 4*256);
309     } else {
310         int i, planes_nb = 0;
311
312         for (i = 0; i < desc->nb_components; i++)
313             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
314
315         for (i = 0; i < planes_nb; i++) {
316             int h = height;
317             ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
318             if (i == 1 || i == 2) {
319                 h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
320             }
321             copy_plane(dst_data[i], dst_linesizes[i],
322                        src_data[i], src_linesizes[i],
323                        bwidth, h);
324         }
325     }
326 }
327
328 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
329                    const uint8_t *src_data[4], const int src_linesizes[4],
330                    enum AVPixelFormat pix_fmt, int width, int height)
331 {
332     ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
333     int i;
334
335     for (i = 0; i < 4; i++) {
336         dst_linesizes1[i] = dst_linesizes[i];
337         src_linesizes1[i] = src_linesizes[i];
338     }
339
340     image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
341                width, height, image_copy_plane);
342 }
343
344 void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
345                            const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
346                            enum AVPixelFormat pix_fmt, int width, int height)
347 {
348     image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
349                width, height, image_copy_plane_uc_from);
350 }
351
352 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
353                          const uint8_t *src, enum AVPixelFormat pix_fmt,
354                          int width, int height, int align)
355 {
356     int ret, i;
357
358     ret = av_image_check_size(width, height, 0, NULL);
359     if (ret < 0)
360         return ret;
361
362     ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
363     if (ret < 0)
364         return ret;
365
366     for (i = 0; i < 4; i++)
367         dst_linesize[i] = FFALIGN(dst_linesize[i], align);
368
369     return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
370 }
371
372 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
373                              int width, int height, int align)
374 {
375     uint8_t *data[4];
376     int linesize[4];
377     int ret;
378     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
379     if (!desc)
380         return AVERROR_BUG;
381
382     ret = av_image_check_size(width, height, 0, NULL);
383     if (ret < 0)
384         return ret;
385
386     // do not include palette for these pseudo-paletted formats
387     if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
388         return width * height;
389
390     return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
391                                 width, height, align);
392 }
393
394 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
395                             const uint8_t * const src_data[4],
396                             const int src_linesize[4],
397                             enum AVPixelFormat pix_fmt,
398                             int width, int height, int align)
399 {
400     int i, j, nb_planes = 0, linesize[4];
401     int size = av_image_get_buffer_size(pix_fmt, width, height, align);
402     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
403
404     if (size > dst_size || size < 0 || !desc)
405         return AVERROR(EINVAL);
406
407     for (i = 0; i < desc->nb_components; i++)
408         nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
409
410     nb_planes++;
411
412     av_image_fill_linesizes(linesize, pix_fmt, width);
413     for (i = 0; i < nb_planes; i++) {
414         int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
415         const uint8_t *src = src_data[i];
416         h = (height + (1 << shift) - 1) >> shift;
417
418         for (j = 0; j < h; j++) {
419             memcpy(dst, src, linesize[i]);
420             dst += FFALIGN(linesize[i], align);
421             src += src_linesize[i];
422         }
423     }
424
425     if (desc->flags & AV_PIX_FMT_FLAG_PAL)
426         memcpy((unsigned char *)(((size_t)dst + 3) & ~3),
427                src_data[1], 256 * 4);
428
429     return size;
430 }