]> git.sesse.net Git - ffmpeg/blob - libavcore/imgutils.c
Make av_fill_image_max_pixsteps() non static non inline.
[ffmpeg] / libavcore / 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 "libavutil/pixdesc.h"
26
27 void av_fill_image_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
28                                 const AVPixFmtDescriptor *pixdesc)
29 {
30     int i;
31     memset(max_pixsteps, 0, 4*sizeof(max_pixsteps[0]));
32     if (max_pixstep_comps)
33         memset(max_pixstep_comps, 0, 4*sizeof(max_pixstep_comps[0]));
34
35     for (i = 0; i < 4; i++) {
36         const AVComponentDescriptor *comp = &(pixdesc->comp[i]);
37         if ((comp->step_minus1+1) > max_pixsteps[comp->plane]) {
38             max_pixsteps[comp->plane] = comp->step_minus1+1;
39             if (max_pixstep_comps)
40                 max_pixstep_comps[comp->plane] = i;
41         }
42     }
43 }
44
45 int av_get_image_linesize(enum PixelFormat pix_fmt, int width, int plane)
46 {
47     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
48     int max_step     [4];       /* max pixel step for each plane */
49     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
50     int s;
51
52     if (desc->flags & PIX_FMT_BITSTREAM)
53         return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
54
55     av_fill_image_max_pixsteps(max_step, max_step_comp, desc);
56     s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
57     return max_step[plane] * (((width + (1 << s) - 1)) >> s);
58 }
59
60 int av_fill_image_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
61 {
62     int i;
63     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
64     int max_step     [4];       /* max pixel step for each plane */
65     int max_step_comp[4];       /* the component for each plane which has the max pixel step */
66
67     memset(linesizes, 0, 4*sizeof(linesizes[0]));
68
69     if (desc->flags & PIX_FMT_HWACCEL)
70         return AVERROR(EINVAL);
71
72     if (desc->flags & PIX_FMT_BITSTREAM) {
73         linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
74         return 0;
75     }
76
77     av_fill_image_max_pixsteps(max_step, max_step_comp, desc);
78     for (i = 0; i < 4; i++) {
79         int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
80         linesizes[i] = max_step[i] * (((width + (1 << s) - 1)) >> s);
81     }
82
83     return 0;
84 }
85
86 int av_fill_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
87                            uint8_t *ptr, const int linesizes[4])
88 {
89     int i, total_size, size[4], has_plane[4];
90
91     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
92     memset(data     , 0, sizeof(data[0])*4);
93     memset(size     , 0, sizeof(size));
94     memset(has_plane, 0, sizeof(has_plane));
95
96     if (desc->flags & PIX_FMT_HWACCEL)
97         return AVERROR(EINVAL);
98
99     data[0] = ptr;
100     size[0] = linesizes[0] * height;
101
102     if (desc->flags & PIX_FMT_PAL) {
103         size[0] = (size[0] + 3) & ~3;
104         data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
105         return size[0] + 256 * 4;
106     }
107
108     for (i = 0; i < 4; i++)
109         has_plane[desc->comp[i].plane] = 1;
110
111     total_size = size[0];
112     for (i = 1; has_plane[i] && i < 4; i++) {
113         int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
114         data[i] = data[i-1] + size[i-1];
115         h = (height + (1 << s) - 1) >> s;
116         size[i] = h * linesizes[i];
117         total_size += size[i];
118     }
119
120     return total_size;
121 }
122
123 typedef struct ImgUtils {
124     const AVClass *class;
125     int   log_offset;
126     void *log_ctx;
127 } ImgUtils;
128
129 static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
130
131 int av_check_image_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
132 {
133     ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
134
135     if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
136         return 0;
137
138     av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
139     return AVERROR(EINVAL);
140 }