Statistics
| Branch: | Revision:

ffmpeg / libavutil / imgutils.c @ 0d73227c

History | View | Annotate | Download (8.46 KB)

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 "libavutil/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
int av_image_get_linesize(enum PixelFormat pix_fmt, int width, int plane)
47
{
48
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
49
    int max_step     [4];       /* max pixel step for each plane */
50
    int max_step_comp[4];       /* the component for each plane which has the max pixel step */
51
    int s, linesize;
52

    
53
    if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
54
        return AVERROR(EINVAL);
55

    
56
    av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
57
    s = (max_step_comp[plane] == 1 || max_step_comp[plane] == 2) ? desc->log2_chroma_w : 0;
58
    linesize = max_step[plane] * (((width + (1 << s) - 1)) >> s);
59
    if (desc->flags & PIX_FMT_BITSTREAM)
60
        linesize = (linesize + 7) >> 3;
61
    return linesize;
62
}
63

    
64
int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
65
{
66
    int i;
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
    memset(linesizes, 0, 4*sizeof(linesizes[0]));
72

    
73
    if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
74
        return AVERROR(EINVAL);
75

    
76
    av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
77
    for (i = 0; i < 4; i++) {
78
        int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
79
        int shifted_w = ((width + (1 << s) - 1)) >> s;
80
        if (max_step[i] > INT_MAX / shifted_w)
81
            return AVERROR(EINVAL);
82
        linesizes[i] = max_step[i] * shifted_w;
83
        if (desc->flags & PIX_FMT_BITSTREAM)
84
            linesizes[i] = (linesizes[i] + 7) >> 3;
85
    }
86

    
87
    return 0;
88
}
89

    
90
int av_image_fill_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
91
                           uint8_t *ptr, const int linesizes[4])
92
{
93
    int i, total_size, size[4], has_plane[4];
94

    
95
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
96
    memset(data     , 0, sizeof(data[0])*4);
97
    memset(size     , 0, sizeof(size));
98
    memset(has_plane, 0, sizeof(has_plane));
99

    
100
    if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
101
        return AVERROR(EINVAL);
102

    
103
    data[0] = ptr;
104
    if (linesizes[0] > (INT_MAX - 1024) / height)
105
        return AVERROR(EINVAL);
106
    size[0] = linesizes[0] * height;
107

    
108
    if (desc->flags & PIX_FMT_PAL) {
109
        size[0] = (size[0] + 3) & ~3;
110
        data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
111
        return size[0] + 256 * 4;
112
    }
113

    
114
    for (i = 0; i < 4; i++)
115
        has_plane[desc->comp[i].plane] = 1;
116

    
117
    total_size = size[0];
118
    for (i = 1; has_plane[i] && i < 4; i++) {
119
        int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
120
        data[i] = data[i-1] + size[i-1];
121
        h = (height + (1 << s) - 1) >> s;
122
        if (linesizes[i] > INT_MAX / h)
123
            return AVERROR(EINVAL);
124
        size[i] = h * linesizes[i];
125
        if (total_size > INT_MAX - size[i])
126
            return AVERROR(EINVAL);
127
        total_size += size[i];
128
    }
129

    
130
    return total_size;
131
}
132

    
133
int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
134
{
135
    int i;
136

    
137
    for (i = 0; i < 256; i++) {
138
        int r, g, b;
139

    
140
        switch (pix_fmt) {
141
        case PIX_FMT_RGB8:
142
            r = (i>>5    )*36;
143
            g = ((i>>2)&7)*36;
144
            b = (i&3     )*85;
145
            break;
146
        case PIX_FMT_BGR8:
147
            b = (i>>6    )*85;
148
            g = ((i>>3)&7)*36;
149
            r = (i&7     )*36;
150
            break;
151
        case PIX_FMT_RGB4_BYTE:
152
            r = (i>>3    )*255;
153
            g = ((i>>1)&3)*85;
154
            b = (i&1     )*255;
155
            break;
156
        case PIX_FMT_BGR4_BYTE:
157
            b = (i>>3    )*255;
158
            g = ((i>>1)&3)*85;
159
            r = (i&1     )*255;
160
            break;
161
        case PIX_FMT_GRAY8:
162
            r = b = g = i;
163
            break;
164
        default:
165
            return AVERROR(EINVAL);
166
        }
167
        pal[i] = b + (g<<8) + (r<<16);
168
    }
169

    
170
    return 0;
171
}
172

    
173
int av_image_alloc(uint8_t *pointers[4], int linesizes[4],
174
                   int w, int h, enum PixelFormat pix_fmt, int align)
175
{
176
    int i, ret;
177
    uint8_t *buf;
178

    
179
    if ((ret = av_image_check_size(w, h, 0, NULL)) < 0)
180
        return ret;
181
    if ((ret = av_image_fill_linesizes(linesizes, pix_fmt, w)) < 0)
182
        return ret;
183

    
184
    for (i = 0; i < 4; i++)
185
        linesizes[i] = FFALIGN(linesizes[i], align);
186

    
187
    if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, NULL, linesizes)) < 0)
188
        return ret;
189
    buf = av_malloc(ret + align);
190
    if (!buf)
191
        return AVERROR(ENOMEM);
192
    if ((ret = av_image_fill_pointers(pointers, pix_fmt, h, buf, linesizes)) < 0) {
193
        av_free(buf);
194
        return ret;
195
    }
196
    if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_PAL)
197
        ff_set_systematic_pal2((uint32_t*)pointers[1], pix_fmt);
198

    
199
    return ret;
200
}
201

    
202
typedef struct ImgUtils {
203
    const AVClass *class;
204
    int   log_offset;
205
    void *log_ctx;
206
} ImgUtils;
207

    
208
static const AVClass imgutils_class = { "IMGUTILS", av_default_item_name, NULL, LIBAVUTIL_VERSION_INT, offsetof(ImgUtils, log_offset), offsetof(ImgUtils, log_ctx) };
209

    
210
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
211
{
212
    ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
213

    
214
    if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
215
        return 0;
216

    
217
    av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
218
    return AVERROR(EINVAL);
219
}
220

    
221
void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
222
                         const uint8_t *src, int src_linesize,
223
                         int bytewidth, int height)
224
{
225
    if (!dst || !src)
226
        return;
227
    for (;height > 0; height--) {
228
        memcpy(dst, src, bytewidth);
229
        dst += dst_linesize;
230
        src += src_linesize;
231
    }
232
}
233

    
234
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
235
                   const uint8_t *src_data[4], const int src_linesizes[4],
236
                   enum PixelFormat pix_fmt, int width, int height)
237
{
238
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
239

    
240
    if (desc->flags & PIX_FMT_HWACCEL)
241
        return;
242

    
243
    if (desc->flags & PIX_FMT_PAL) {
244
        av_image_copy_plane(dst_data[0], dst_linesizes[0],
245
                            src_data[0], src_linesizes[0],
246
                            width, height);
247
        /* copy the palette */
248
        memcpy(dst_data[1], src_data[1], 4*256);
249
    } else {
250
        int i, planes_nb = 0;
251

    
252
        for (i = 0; i < desc->nb_components; i++)
253
            planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
254

    
255
        for (i = 0; i < planes_nb; i++) {
256
            int h = height;
257
            int bwidth = av_image_get_linesize(pix_fmt, width, i);
258
            if (i == 1 || i == 2) {
259
                h= -((-height)>>desc->log2_chroma_h);
260
            }
261
            av_image_copy_plane(dst_data[i], dst_linesizes[i],
262
                                src_data[i], src_linesizes[i],
263
                                bwidth, h);
264
        }
265
    }
266
}