]> git.sesse.net Git - ffmpeg/blob - libavutil/imgutils.c
time_internal: Prefix fallback versions of gmtime_r/localtime_r with ff_
[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 = {
223     .class_name                = "IMGUTILS",
224     .item_name                 = av_default_item_name,
225     .option                    = NULL,
226     .version                   = LIBAVUTIL_VERSION_INT,
227     .log_level_offset_offset   = offsetof(ImgUtils, log_offset),
228     .parent_log_context_offset = offsetof(ImgUtils, log_ctx),
229 };
230
231 int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
232 {
233     ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
234
235     if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
236         return 0;
237
238     av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
239     return AVERROR(EINVAL);
240 }
241
242 int av_image_check_sar(unsigned int w, unsigned int h, AVRational sar)
243 {
244     int64_t scaled_dim;
245
246     if (!sar.den)
247         return AVERROR(EINVAL);
248
249     if (!sar.num || sar.num == sar.den)
250         return 0;
251
252     if (sar.num < sar.den)
253         scaled_dim = av_rescale_rnd(w, sar.num, sar.den, AV_ROUND_ZERO);
254     else
255         scaled_dim = av_rescale_rnd(h, sar.den, sar.num, AV_ROUND_ZERO);
256
257     if (scaled_dim > 0)
258         return 0;
259
260     return AVERROR(EINVAL);
261 }
262
263 static void image_copy_plane(uint8_t       *dst, ptrdiff_t dst_linesize,
264                              const uint8_t *src, ptrdiff_t src_linesize,
265                              ptrdiff_t bytewidth, int height)
266 {
267     if (!dst || !src)
268         return;
269     for (;height > 0; height--) {
270         memcpy(dst, src, bytewidth);
271         dst += dst_linesize;
272         src += src_linesize;
273     }
274 }
275
276 static void image_copy_plane_uc_from(uint8_t       *dst, ptrdiff_t dst_linesize,
277                                      const uint8_t *src, ptrdiff_t src_linesize,
278                                      ptrdiff_t bytewidth, int height)
279 {
280     int ret = -1;
281
282 #if ARCH_X86
283     ret = ff_image_copy_plane_uc_from_x86(dst, dst_linesize, src, src_linesize,
284                                           bytewidth, height);
285 #endif
286
287     if (ret < 0)
288         image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
289 }
290
291 void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
292                          const uint8_t *src, int src_linesize,
293                          int bytewidth, int height)
294 {
295     image_copy_plane(dst, dst_linesize, src, src_linesize, bytewidth, height);
296 }
297
298 static void image_copy(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
299                        const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
300                        enum AVPixelFormat pix_fmt, int width, int height,
301                        void (*copy_plane)(uint8_t *, ptrdiff_t, const uint8_t *,
302                                           ptrdiff_t, ptrdiff_t, int))
303 {
304     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
305
306     if (!desc || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
307         return;
308
309     if (desc->flags & AV_PIX_FMT_FLAG_PAL ||
310         desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL) {
311         copy_plane(dst_data[0], dst_linesizes[0],
312                    src_data[0], src_linesizes[0],
313                    width, height);
314         /* copy the palette */
315         memcpy(dst_data[1], src_data[1], 4*256);
316     } else {
317         int i, planes_nb = 0;
318
319         for (i = 0; i < desc->nb_components; i++)
320             planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
321
322         for (i = 0; i < planes_nb; i++) {
323             int h = height;
324             ptrdiff_t bwidth = av_image_get_linesize(pix_fmt, width, i);
325             if (i == 1 || i == 2) {
326                 h = AV_CEIL_RSHIFT(height, desc->log2_chroma_h);
327             }
328             copy_plane(dst_data[i], dst_linesizes[i],
329                        src_data[i], src_linesizes[i],
330                        bwidth, h);
331         }
332     }
333 }
334
335 void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
336                    const uint8_t *src_data[4], const int src_linesizes[4],
337                    enum AVPixelFormat pix_fmt, int width, int height)
338 {
339     ptrdiff_t dst_linesizes1[4], src_linesizes1[4];
340     int i;
341
342     for (i = 0; i < 4; i++) {
343         dst_linesizes1[i] = dst_linesizes[i];
344         src_linesizes1[i] = src_linesizes[i];
345     }
346
347     image_copy(dst_data, dst_linesizes1, src_data, src_linesizes1, pix_fmt,
348                width, height, image_copy_plane);
349 }
350
351 void av_image_copy_uc_from(uint8_t *dst_data[4], const ptrdiff_t dst_linesizes[4],
352                            const uint8_t *src_data[4], const ptrdiff_t src_linesizes[4],
353                            enum AVPixelFormat pix_fmt, int width, int height)
354 {
355     image_copy(dst_data, dst_linesizes, src_data, src_linesizes, pix_fmt,
356                width, height, image_copy_plane_uc_from);
357 }
358
359 int av_image_fill_arrays(uint8_t *dst_data[4], int dst_linesize[4],
360                          const uint8_t *src, enum AVPixelFormat pix_fmt,
361                          int width, int height, int align)
362 {
363     int ret, i;
364
365     ret = av_image_check_size(width, height, 0, NULL);
366     if (ret < 0)
367         return ret;
368
369     ret = av_image_fill_linesizes(dst_linesize, pix_fmt, width);
370     if (ret < 0)
371         return ret;
372
373     for (i = 0; i < 4; i++)
374         dst_linesize[i] = FFALIGN(dst_linesize[i], align);
375
376     return av_image_fill_pointers(dst_data, pix_fmt, height, src, dst_linesize);
377 }
378
379 int av_image_get_buffer_size(enum AVPixelFormat pix_fmt,
380                              int width, int height, int align)
381 {
382     uint8_t *data[4];
383     int linesize[4];
384     int ret;
385     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
386     if (!desc)
387         return AVERROR_BUG;
388
389     ret = av_image_check_size(width, height, 0, NULL);
390     if (ret < 0)
391         return ret;
392
393     // do not include palette for these pseudo-paletted formats
394     if (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL)
395         return width * height;
396
397     return av_image_fill_arrays(data, linesize, NULL, pix_fmt,
398                                 width, height, align);
399 }
400
401 int av_image_copy_to_buffer(uint8_t *dst, int dst_size,
402                             const uint8_t * const src_data[4],
403                             const int src_linesize[4],
404                             enum AVPixelFormat pix_fmt,
405                             int width, int height, int align)
406 {
407     int i, j, nb_planes = 0, linesize[4];
408     int size = av_image_get_buffer_size(pix_fmt, width, height, align);
409     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
410
411     if (size > dst_size || size < 0 || !desc)
412         return AVERROR(EINVAL);
413
414     for (i = 0; i < desc->nb_components; i++)
415         nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
416
417     nb_planes++;
418
419     av_image_fill_linesizes(linesize, pix_fmt, width);
420     for (i = 0; i < nb_planes; i++) {
421         int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
422         const uint8_t *src = src_data[i];
423         h = (height + (1 << shift) - 1) >> shift;
424
425         for (j = 0; j < h; j++) {
426             memcpy(dst, src, linesize[i]);
427             dst += FFALIGN(linesize[i], align);
428             src += src_linesize[i];
429         }
430     }
431
432     if (desc->flags & AV_PIX_FMT_FLAG_PAL)
433         memcpy((unsigned char *)(((size_t)dst + 3) & ~3),
434                src_data[1], 256 * 4);
435
436     return size;
437 }
438
439 // Fill dst[0..dst_size] with the bytes in clear[0..clear_size]. The clear
440 // bytes are repeated until dst_size is reached. If dst_size is unaligned (i.e.
441 // dst_size%clear_size!=0), the remaining data will be filled with the beginning
442 // of the clear data only.
443 static void memset_bytes(uint8_t *dst, size_t dst_size, uint8_t *clear,
444                          size_t clear_size)
445 {
446     size_t pos = 0;
447     int same = 1;
448     int i;
449
450     if (!clear_size)
451         return;
452
453     // Reduce to memset() if possible.
454     for (i = 0; i < clear_size; i++) {
455         if (clear[i] != clear[0]) {
456             same = 0;
457             break;
458         }
459     }
460     if (same)
461         clear_size = 1;
462
463     if (clear_size == 1) {
464         memset(dst, clear[0], dst_size);
465         dst_size = 0;
466     } else if (clear_size == 2) {
467         uint16_t val = AV_RN16(clear);
468         for (; dst_size >= 2; dst_size -= 2) {
469             AV_WN16(dst, val);
470             dst += 2;
471         }
472     } else if (clear_size == 4) {
473         uint32_t val = AV_RN32(clear);
474         for (; dst_size >= 4; dst_size -= 4) {
475             AV_WN32(dst, val);
476             dst += 4;
477         }
478     } else if (clear_size == 8) {
479         uint32_t val = AV_RN64(clear);
480         for (; dst_size >= 8; dst_size -= 8) {
481             AV_WN64(dst, val);
482             dst += 8;
483         }
484     }
485
486     for (; dst_size; dst_size--)
487         *dst++ = clear[pos++ % clear_size];
488 }
489
490 // Maximum size in bytes of a plane element (usually a pixel, or multiple pixels
491 // if it's a subsampled packed format).
492 #define MAX_BLOCK_SIZE 32
493
494 int av_image_fill_black(uint8_t *dst_data[4], const ptrdiff_t dst_linesize[4],
495                         enum AVPixelFormat pix_fmt, enum AVColorRange range,
496                         int width, int height)
497 {
498     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
499     int nb_planes = av_pix_fmt_count_planes(pix_fmt);
500     // A pixel or a group of pixels on each plane, with a value that represents black.
501     // Consider e.g. AV_PIX_FMT_UYVY422 for non-trivial cases.
502     uint8_t clear_block[4][MAX_BLOCK_SIZE] = {0}; // clear padding with 0
503     int clear_block_size[4] = {0};
504     ptrdiff_t plane_line_bytes[4] = {0};
505     int rgb, limited;
506     int plane, c;
507
508     if (!desc || nb_planes < 1 || nb_planes > 4 || desc->flags & AV_PIX_FMT_FLAG_HWACCEL)
509         return AVERROR(EINVAL);
510
511     rgb = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
512     limited = !rgb && range != AVCOL_RANGE_JPEG;
513
514     if (desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) {
515         ptrdiff_t bytewidth = av_image_get_linesize(pix_fmt, width, 0);
516         uint8_t *data;
517         int mono = pix_fmt == AV_PIX_FMT_MONOWHITE || pix_fmt == AV_PIX_FMT_MONOBLACK;
518         int fill = pix_fmt == AV_PIX_FMT_MONOWHITE ? 0xFF : 0;
519         if (nb_planes != 1 || !(rgb || mono) || bytewidth < 1)
520             return AVERROR(EINVAL);
521
522         if (!dst_data)
523             return 0;
524
525         data = dst_data[0];
526
527         // (Bitstream + alpha will be handled incorrectly - it'll remain transparent.)
528         for (;height > 0; height--) {
529             memset(data, fill, bytewidth);
530             data += dst_linesize[0];
531         }
532         return 0;
533     }
534
535     for (c = 0; c < desc->nb_components; c++) {
536         const AVComponentDescriptor comp = desc->comp[c];
537
538         // We try to operate on entire non-subsampled pixel groups (for
539         // AV_PIX_FMT_UYVY422 this would mean two consecutive pixels).
540         clear_block_size[comp.plane] = FFMAX(clear_block_size[comp.plane], comp.step);
541
542         if (clear_block_size[comp.plane] > MAX_BLOCK_SIZE)
543             return AVERROR(EINVAL);
544     }
545
546     // Create a byte array for clearing 1 pixel (sometimes several pixels).
547     for (c = 0; c < desc->nb_components; c++) {
548         const AVComponentDescriptor comp = desc->comp[c];
549         // (Multiple pixels happen e.g. with AV_PIX_FMT_UYVY422.)
550         int w = clear_block_size[comp.plane] / comp.step;
551         uint8_t *c_data[4];
552         const int c_linesize[4] = {0};
553         uint16_t src_array[MAX_BLOCK_SIZE];
554         uint16_t src = 0;
555         int x;
556
557         if (comp.depth > 16)
558             return AVERROR(EINVAL);
559         if (!rgb && comp.depth < 8)
560             return AVERROR(EINVAL);
561         if (w < 1)
562             return AVERROR(EINVAL);
563
564         if (c == 0 && limited) {
565             src = 16 << (comp.depth - 8);
566         } else if ((c == 1 || c == 2) && !rgb) {
567             src = 128 << (comp.depth - 8);
568         } else if (c == 3) {
569             // (Assume even limited YUV uses full range alpha.)
570             src = (1 << comp.depth) - 1;
571         }
572
573         for (x = 0; x < w; x++)
574             src_array[x] = src;
575
576         for (x = 0; x < 4; x++)
577             c_data[x] = &clear_block[x][0];
578
579         av_write_image_line(src_array, c_data, c_linesize, desc, 0, 0, c, w);
580     }
581
582     for (plane = 0; plane < nb_planes; plane++) {
583         plane_line_bytes[plane] = av_image_get_linesize(pix_fmt, width, plane);
584         if (plane_line_bytes[plane] < 0)
585             return AVERROR(EINVAL);
586     }
587
588     if (!dst_data)
589         return 0;
590
591     for (plane = 0; plane < nb_planes; plane++) {
592         size_t bytewidth = plane_line_bytes[plane];
593         uint8_t *data = dst_data[plane];
594         int chroma_div = plane == 1 || plane == 2 ? desc->log2_chroma_h : 0;
595         int plane_h = ((height + ( 1 << chroma_div) - 1)) >> chroma_div;
596
597         for (; plane_h > 0; plane_h--) {
598             memset_bytes(data, bytewidth, &clear_block[plane][0], clear_block_size[plane]);
599             data += dst_linesize[plane];
600         }
601     }
602
603     return 0;
604 }