Statistics
| Branch: | Revision:

ffmpeg / libavcore / imgutils.c @ 5d6e4c16

History | View | Annotate | Download (8.09 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;
52

    
53
    if (desc->flags & PIX_FMT_BITSTREAM)
54
        return (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
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
    return max_step[plane] * (((width + (1 << s) - 1)) >> s);
59
}
60

    
61
int av_image_fill_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
62
{
63
    int i;
64
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
65
    int max_step     [4];       /* max pixel step for each plane */
66
    int max_step_comp[4];       /* the component for each plane which has the max pixel step */
67

    
68
    memset(linesizes, 0, 4*sizeof(linesizes[0]));
69

    
70
    if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
71
        return AVERROR(EINVAL);
72

    
73
    if (desc->flags & PIX_FMT_BITSTREAM) {
74
        linesizes[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
75
        return 0;
76
    }
77

    
78
    av_image_fill_max_pixsteps(max_step, max_step_comp, desc);
79
    for (i = 0; i < 4; i++) {
80
        int s = (max_step_comp[i] == 1 || max_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
81
        linesizes[i] = max_step[i] * (((width + (1 << s) - 1)) >> s);
82
    }
83

    
84
    return 0;
85
}
86

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

    
92
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
93
    memset(data     , 0, sizeof(data[0])*4);
94
    memset(size     , 0, sizeof(size));
95
    memset(has_plane, 0, sizeof(has_plane));
96

    
97
    if ((unsigned)pix_fmt >= PIX_FMT_NB || desc->flags & PIX_FMT_HWACCEL)
98
        return AVERROR(EINVAL);
99

    
100
    data[0] = ptr;
101
    size[0] = linesizes[0] * height;
102

    
103
    if (desc->flags & PIX_FMT_PAL) {
104
        size[0] = (size[0] + 3) & ~3;
105
        data[1] = ptr + size[0]; /* palette is stored here as 256 32 bits words */
106
        return size[0] + 256 * 4;
107
    }
108

    
109
    for (i = 0; i < 4; i++)
110
        has_plane[desc->comp[i].plane] = 1;
111

    
112
    total_size = size[0];
113
    for (i = 1; has_plane[i] && i < 4; i++) {
114
        int h, s = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
115
        data[i] = data[i-1] + size[i-1];
116
        h = (height + (1 << s) - 1) >> s;
117
        size[i] = h * linesizes[i];
118
        total_size += size[i];
119
    }
120

    
121
    return total_size;
122
}
123

    
124
int ff_set_systematic_pal2(uint32_t pal[256], enum PixelFormat pix_fmt)
125
{
126
    int i;
127

    
128
    for (i = 0; i < 256; i++) {
129
        int r, g, b;
130

    
131
        switch (pix_fmt) {
132
        case PIX_FMT_RGB8:
133
            r = (i>>5    )*36;
134
            g = ((i>>2)&7)*36;
135
            b = (i&3     )*85;
136
            break;
137
        case PIX_FMT_BGR8:
138
            b = (i>>6    )*85;
139
            g = ((i>>3)&7)*36;
140
            r = (i&7     )*36;
141
            break;
142
        case PIX_FMT_RGB4_BYTE:
143
            r = (i>>3    )*255;
144
            g = ((i>>1)&3)*85;
145
            b = (i&1     )*255;
146
            break;
147
        case PIX_FMT_BGR4_BYTE:
148
            b = (i>>3    )*255;
149
            g = ((i>>1)&3)*85;
150
            r = (i&1     )*255;
151
            break;
152
        case PIX_FMT_GRAY8:
153
            r = b = g = i;
154
            break;
155
        default:
156
            return AVERROR(EINVAL);
157
        }
158
        pal[i] = b + (g<<8) + (r<<16);
159
    }
160

    
161
    return 0;
162
}
163

    
164
typedef struct ImgUtils {
165
    const AVClass *class;
166
    int   log_offset;
167
    void *log_ctx;
168
} ImgUtils;
169

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

    
172
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
173
{
174
    ImgUtils imgutils = { &imgutils_class, log_offset, log_ctx };
175

    
176
    if ((int)w>0 && (int)h>0 && (w+128)*(uint64_t)(h+128) < INT_MAX/8)
177
        return 0;
178

    
179
    av_log(&imgutils, AV_LOG_ERROR, "Picture size %ux%u is invalid\n", w, h);
180
    return AVERROR(EINVAL);
181
}
182

    
183
void av_image_copy_plane(uint8_t       *dst, int dst_linesize,
184
                         const uint8_t *src, int src_linesize,
185
                         int bytewidth, int height)
186
{
187
    if (!dst || !src)
188
        return;
189
    for (;height > 0; height--) {
190
        memcpy(dst, src, bytewidth);
191
        dst += dst_linesize;
192
        src += src_linesize;
193
    }
194
}
195

    
196
void av_image_copy(uint8_t *dst_data[4], int dst_linesizes[4],
197
                   const uint8_t *src_data[4], const int src_linesizes[4],
198
                   enum PixelFormat pix_fmt, int width, int height)
199
{
200
    const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
201

    
202
    if (desc->flags & PIX_FMT_HWACCEL)
203
        return;
204

    
205
    if (desc->flags & PIX_FMT_PAL) {
206
        av_image_copy_plane(dst_data[0], dst_linesizes[0],
207
                            src_data[0], src_linesizes[0],
208
                            width, height);
209
        /* copy the palette */
210
        memcpy(dst_data[1], src_data[1], 4*256);
211
    } else {
212
        int i, planes_nb = 0;
213

    
214
        for (i = 0; i < desc->nb_components; i++)
215
            planes_nb = FFMAX(planes_nb, desc->comp[i].plane + 1);
216

    
217
        for (i = 0; i < planes_nb; i++) {
218
            int h = height;
219
            int bwidth = av_image_get_linesize(pix_fmt, width, i);
220
            if (i == 1 || i == 2) {
221
                h= -((-height)>>desc->log2_chroma_h);
222
            }
223
            av_image_copy_plane(dst_data[i], dst_linesizes[i],
224
                                src_data[i], src_linesizes[i],
225
                                bwidth, h);
226
        }
227
    }
228
}
229

    
230
#if FF_API_OLD_IMAGE_NAMES
231
void av_fill_image_max_pixsteps(int max_pixsteps[4], int max_pixstep_comps[4],
232
                                const AVPixFmtDescriptor *pixdesc)
233
{
234
    av_image_fill_max_pixsteps(max_pixsteps, max_pixstep_comps, pixdesc);
235
}
236

    
237
int av_get_image_linesize(enum PixelFormat pix_fmt, int width, int plane)
238
{
239
    return av_image_get_linesize(pix_fmt, width, plane);
240
}
241

    
242
int av_fill_image_linesizes(int linesizes[4], enum PixelFormat pix_fmt, int width)
243
{
244
    return av_image_fill_linesizes(linesizes, pix_fmt, width);
245
}
246

    
247
int av_fill_image_pointers(uint8_t *data[4], enum PixelFormat pix_fmt, int height,
248
                           uint8_t *ptr, const int linesizes[4])
249
{
250
    return av_image_fill_pointers(data, pix_fmt, height, ptr, linesizes);
251
}
252

    
253
int av_check_image_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
254
{
255
    return av_image_check_size(w, h, log_offset, log_ctx);
256
}
257
#endif