vpx_image.c 7.35 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license and patent
 *  grant that can be found in the LICENSE file in the root of the source
 *  tree. All contributing project authors may be found in the AUTHORS
 *  file in the root of the source tree.
 */


#include <stdlib.h>
#include <string.h>
#include "vpx_codec/vpx_image.h"

static vpx_image_t *img_alloc_helper(vpx_image_t  *img,
James Zern's avatar
James Zern committed
16
                                     vpx_img_fmt_t fmt,
John Koleszar's avatar
John Koleszar committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
                                     unsigned int  d_w,
                                     unsigned int  d_h,
                                     unsigned int  stride_align,
                                     unsigned char      *img_data)
{

    unsigned int  h, w, s, xcs, ycs, bps;
    int           align;

    /* Treat align==0 like align==1 */
    if (!stride_align)
        stride_align = 1;

    /* Validate alignment (must be power of 2) */
    if (stride_align & (stride_align - 1))
        goto fail;

    /* Get sample size for this format */
    switch (fmt)
    {
James Zern's avatar
James Zern committed
37
38
39
40
    case VPX_IMG_FMT_RGB32:
    case VPX_IMG_FMT_RGB32_LE:
    case VPX_IMG_FMT_ARGB:
    case VPX_IMG_FMT_ARGB_LE:
John Koleszar's avatar
John Koleszar committed
41
42
        bps = 32;
        break;
James Zern's avatar
James Zern committed
43
44
    case VPX_IMG_FMT_RGB24:
    case VPX_IMG_FMT_BGR24:
John Koleszar's avatar
John Koleszar committed
45
46
        bps = 24;
        break;
James Zern's avatar
James Zern committed
47
48
49
50
51
52
53
    case VPX_IMG_FMT_RGB565:
    case VPX_IMG_FMT_RGB565_LE:
    case VPX_IMG_FMT_RGB555:
    case VPX_IMG_FMT_RGB555_LE:
    case VPX_IMG_FMT_UYVY:
    case VPX_IMG_FMT_YUY2:
    case VPX_IMG_FMT_YVYU:
John Koleszar's avatar
John Koleszar committed
54
55
        bps = 16;
        break;
James Zern's avatar
James Zern committed
56
57
58
59
    case VPX_IMG_FMT_I420:
    case VPX_IMG_FMT_YV12:
    case VPX_IMG_FMT_VPXI420:
    case VPX_IMG_FMT_VPXYV12:
John Koleszar's avatar
John Koleszar committed
60
61
62
63
64
65
66
67
68
69
        bps = 12;
        break;
    default:
        bps = 16;
        break;
    }

    /* Get chroma shift values for this format */
    switch (fmt)
    {
James Zern's avatar
James Zern committed
70
71
72
73
    case VPX_IMG_FMT_I420:
    case VPX_IMG_FMT_YV12:
    case VPX_IMG_FMT_VPXI420:
    case VPX_IMG_FMT_VPXYV12:
John Koleszar's avatar
John Koleszar committed
74
75
76
77
78
79
80
81
82
        xcs = 1;
        break;
    default:
        xcs = 0;
        break;
    }

    switch (fmt)
    {
James Zern's avatar
James Zern committed
83
84
85
86
    case VPX_IMG_FMT_I420:
    case VPX_IMG_FMT_YV12:
    case VPX_IMG_FMT_VPXI420:
    case VPX_IMG_FMT_VPXYV12:
John Koleszar's avatar
John Koleszar committed
87
88
89
90
91
92
93
94
95
96
97
98
        ycs = 1;
        break;
    default:
        ycs = 0;
        break;
    }

    /* Calculate storage sizes given the chroma subsampling */
    align = (1 << xcs) - 1;
    w = (d_w + align) & ~align;
    align = (1 << ycs) - 1;
    h = (d_h + align) & ~align;
James Zern's avatar
James Zern committed
99
    s = (fmt & VPX_IMG_FMT_PLANAR) ? w : bps * w / 8;
John Koleszar's avatar
John Koleszar committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    s = (s + stride_align - 1) & ~(stride_align - 1);

    /* Allocate the new image */
    if (!img)
    {
        img = (vpx_image_t *)calloc(1, sizeof(vpx_image_t));

        if (!img)
            goto fail;

        img->self_allocd = 1;
    }
    else
    {
        memset(img, 0, sizeof(vpx_image_t));
    }

    img->img_data = img_data;

    if (!img_data)
    {
James Zern's avatar
James Zern committed
121
        img->img_data = malloc((fmt & VPX_IMG_FMT_PLANAR) ? h * w * bps / 8 : h * s);
John Koleszar's avatar
John Koleszar committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
        img->img_data_owner = 1;
    }

    if (!img->img_data)
        goto fail;

    img->fmt = fmt;
    img->w = w;
    img->h = h;
    img->x_chroma_shift = xcs;
    img->y_chroma_shift = ycs;
    img->bps = bps;

    /* Calculate strides */
    img->stride[PLANE_Y] = img->stride[PLANE_ALPHA] = s;
    img->stride[PLANE_U] = img->stride[PLANE_V] = s >> xcs;

    /* Default viewport to entire image */
    if (!vpx_img_set_rect(img, 0, 0, d_w, d_h))
        return img;

fail:
    vpx_img_free(img);
    return NULL;
}

vpx_image_t *vpx_img_alloc(vpx_image_t  *img,
James Zern's avatar
James Zern committed
149
                           vpx_img_fmt_t fmt,
John Koleszar's avatar
John Koleszar committed
150
151
152
153
154
155
156
157
                           unsigned int  d_w,
                           unsigned int  d_h,
                           unsigned int  stride_align)
{
    return img_alloc_helper(img, fmt, d_w, d_h, stride_align, NULL);
}

vpx_image_t *vpx_img_wrap(vpx_image_t  *img,
James Zern's avatar
James Zern committed
158
                          vpx_img_fmt_t fmt,
John Koleszar's avatar
John Koleszar committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
                          unsigned int  d_w,
                          unsigned int  d_h,
                          unsigned int  stride_align,
                          unsigned char       *img_data)
{
    return img_alloc_helper(img, fmt, d_w, d_h, stride_align, img_data);
}

int vpx_img_set_rect(vpx_image_t  *img,
                     unsigned int  x,
                     unsigned int  y,
                     unsigned int  w,
                     unsigned int  h)
{
    unsigned char      *data;

    if (x + w <= img->w && y + h <= img->h)
    {
        img->d_w = w;
        img->d_h = h;

        /* Calculate plane pointers */
James Zern's avatar
James Zern committed
181
        if (!(img->fmt & VPX_IMG_FMT_PLANAR))
John Koleszar's avatar
John Koleszar committed
182
183
184
185
186
187
188
189
        {
            img->planes[PLANE_PACKED] =
                img->img_data + x * img->bps / 8 + y * img->stride[PLANE_PACKED];
        }
        else
        {
            data = img->img_data;

James Zern's avatar
James Zern committed
190
            if (img->fmt & VPX_IMG_FMT_HAS_ALPHA)
John Koleszar's avatar
John Koleszar committed
191
192
193
194
195
196
197
198
199
            {
                img->planes[PLANE_ALPHA] =
                    data + x + y * img->stride[PLANE_ALPHA];
                data += img->h * img->stride[PLANE_ALPHA];
            }

            img->planes[PLANE_Y] = data + x + y * img->stride[PLANE_Y];
            data += img->h * img->stride[PLANE_Y];

James Zern's avatar
James Zern committed
200
            if (!(img->fmt & VPX_IMG_FMT_UV_FLIP))
John Koleszar's avatar
John Koleszar committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
            {
                img->planes[PLANE_U] = data
                                       + (x >> img->x_chroma_shift)
                                       + (y >> img->y_chroma_shift) * img->stride[PLANE_U];
                data += (img->h >> img->y_chroma_shift) * img->stride[PLANE_U];
                img->planes[PLANE_V] = data
                                       + (x >> img->x_chroma_shift)
                                       + (y >> img->y_chroma_shift) * img->stride[PLANE_V];
            }
            else
            {
                img->planes[PLANE_V] = data
                                       + (x >> img->x_chroma_shift)
                                       + (y >> img->y_chroma_shift) * img->stride[PLANE_V];
                data += (img->h >> img->y_chroma_shift) * img->stride[PLANE_V];
                img->planes[PLANE_U] = data
                                       + (x >> img->x_chroma_shift)
                                       + (y >> img->y_chroma_shift) * img->stride[PLANE_U];
            }
        }

        return 0;
    }

    return -1;
}

void vpx_img_flip(vpx_image_t *img)
{
    /* Note: In the calculation pointer adjustment calculation, we want the
     * rhs to be promoted to a signed type. Section 6.3.1.8 of the ISO C99
     * standard indicates that if the adjustment parameter is unsigned, the
     * stride parameter will be promoted to unsigned, causing errors when
     * the lhs is a larger type than the rhs.
     */
    img->planes[PLANE_Y] += (signed)(img->d_h - 1) * img->stride[PLANE_Y];
    img->stride[PLANE_Y] = -img->stride[PLANE_Y];

    img->planes[PLANE_U] += (signed)((img->d_h >> img->y_chroma_shift) - 1)
                            * img->stride[PLANE_U];
    img->stride[PLANE_U] = -img->stride[PLANE_U];

    img->planes[PLANE_V] += (signed)((img->d_h >> img->y_chroma_shift) - 1)
                            * img->stride[PLANE_V];
    img->stride[PLANE_V] = -img->stride[PLANE_V];

    img->planes[PLANE_ALPHA] += (signed)(img->d_h - 1) * img->stride[PLANE_ALPHA];
    img->stride[PLANE_ALPHA] = -img->stride[PLANE_ALPHA];
}

void vpx_img_free(vpx_image_t *img)
{
    if (img)
    {
        if (img->img_data && img->img_data_owner)
            free(img->img_data);

        if (img->self_allocd)
            free(img);
    }
}