source: mainline/uspace/lib/draw/codec/webp.c@ b688fd8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b688fd8 was 490e21b, checked in by Martin Decky <martin@…>, 11 years ago

very initial support for WebP decoding

  • Property mode set to 100644
File size: 6.7 KB
RevLine 
[490e21b]1/*
2 * Copyright (c) 2014 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup draw
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <stdlib.h>
37#include <byteorder.h>
38#include <align.h>
39#include <stdbool.h>
40#include <fourcc.h>
41#include <pixconv.h>
42#include "webp.h"
43
44/** Check for input buffer overrun condition */
45#define CHECK_OVERRUN(state, retval) \
46 do { \
47 if ((state).overrun) \
48 return (retval); \
49 } while (false)
50
51#define SIGNATURE_WEBP_LOSSLESS UINT8_C(0x2f)
52
53enum {
54 FOURCC_RIFF = FOURCC('R', 'I', 'F', 'F'),
55 FOURCC_WEBP = FOURCC('W', 'E', 'B', 'P'),
56 FOURCC_WEBP_LOSSLESS = FOURCC('V', 'P', '8', 'L')
57};
58
59typedef enum {
60 TRANSFORM_PREDICTOR = 0,
61 TRANSFORM_COLOR = 1,
62 TRANSFORM_SUBTRACT = 2,
63 TRANSFORM_COLOR_INDEXING = 3
64} webp_transform_t;
65
66typedef struct {
67 fourcc_t fourcc;
68 uint32_t payload_size;
69} __attribute__((packed)) riff_header_t;
70
71typedef struct {
72 fourcc_t fourcc;
73 fourcc_t encoding;
74 uint32_t stream_size;
75 uint8_t signature;
76} __attribute__((packed)) webp_header_t;
77
78typedef struct {
79 uint32_t stream_size;
80 uint16_t width;
81 uint16_t height;
82 bool alpha_used;
83 uint8_t version;
84
85 uint8_t *src; /**< Input buffer */
86 size_t srclen; /**< Input buffer size */
87 size_t srccnt; /**< Position in the input buffer */
88
89 uint32_t bitbuf; /**< Bit buffer */
90 size_t bitlen; /**< Number of bits in the bit buffer */
91
92 bool overrun; /**< Overrun condition */
93} webp_t;
94
95/** Get bits from the bit buffer
96 *
97 * @param state WebP state.
98 * @param cnt Number of bits to return (at most 32).
99 *
100 * @return Returned bits.
101 *
102 */
103static inline uint32_t get_bits(webp_t *state, size_t cnt)
104{
105 /* Bit accumulator for at least 36 bits */
106 uint64_t val = state->bitbuf;
107
108 while (state->bitlen < cnt) {
109 if (state->srccnt == state->srclen) {
110 state->overrun = true;
111 return 0;
112 }
113
114 /* Load 8 more bits */
115 val |= ((uint64_t) state->src[state->srccnt]) << state->bitlen;
116 state->srccnt++;
117 state->bitlen += 8;
118 }
119
120 /* Update bits in the buffer */
121 state->bitbuf = (uint32_t) (val >> cnt);
122 state->bitlen -= cnt;
123
124 return ((uint32_t) (val & ((1 << cnt) - 1)));
125}
126
127/** Decode WebP header
128 *
129 * @param[in] data Memory representation of WebP.
130 * @param[in] size Size of the representation (in bytes).
131 * @param[out] webp Decoded WebP.
132 *
133 * @return True on succesful decoding.
134 * @return False on failure.
135 *
136 */
137static bool decode_webp_header(void *data, size_t size, webp_t *webp)
138{
139 /* Headers sanity check */
140 if ((size < sizeof(riff_header_t)) ||
141 (size - sizeof(riff_header_t) < sizeof(webp_header_t)))
142 return false;
143
144 riff_header_t *riff_header = (riff_header_t *) data;
145 if (riff_header->fourcc != FOURCC_RIFF)
146 return false;
147
148 /* Check payload size */
149 size_t payload_size = uint32_t_le2host(riff_header->payload_size);
150 if (payload_size + sizeof(riff_header_t) > size)
151 return false;
152
153 data += sizeof(riff_header_t);
154 webp_header_t *webp_header = (webp_header_t *) data;
155 if (webp_header->fourcc != FOURCC_WEBP)
156 return false;
157
158 /* Only lossless encoding supported so far */
159 if (webp_header->encoding != FOURCC_WEBP_LOSSLESS)
160 return false;
161
162 webp->stream_size = uint32_t_le2host(webp_header->stream_size);
163 if (webp->stream_size + sizeof(riff_header_t) +
164 sizeof(webp_header_t) > size)
165 return false;
166
167 if (webp_header->signature != SIGNATURE_WEBP_LOSSLESS)
168 return false;
169
170 data += sizeof(webp_header_t);
171
172 /* Setup decoding state */
173 webp->src = (uint8_t *) data;
174 webp->srclen = webp->stream_size - 1;
175 webp->srccnt = 0;
176 webp->bitbuf = 0;
177 webp->bitlen = 0;
178 webp->overrun = false;
179
180 /* Decode the rest of the metadata */
181 webp->width = get_bits(webp, 14) + 1;
182 CHECK_OVERRUN(*webp, false);
183
184 webp->height = get_bits(webp, 14) + 1;
185 CHECK_OVERRUN(*webp, false);
186
187 webp->alpha_used = get_bits(webp, 1);
188 CHECK_OVERRUN(*webp, false);
189
190 webp->version = get_bits(webp, 3);
191 CHECK_OVERRUN(*webp, false);
192
193 if (webp->version != 0)
194 return false;
195
196 return true;
197}
198
199/** Decode WebP format
200 *
201 * Decode WebP format and create a surface from it. The supported
202 * variants of WebP are currently limited to losslessly compressed
203 * ARGB images.
204 *
205 * @param[in] data Memory representation of WebP.
206 * @param[in] size Size of the representation (in bytes).
207 * @param[in] flags Surface creation flags.
208 *
209 * @return Newly allocated surface with the decoded content.
210 * @return NULL on error or unsupported format.
211 *
212 */
213surface_t *decode_webp(void *data, size_t size, surface_flags_t flags)
214{
215 webp_t webp;
216 if (!decode_webp_header(data, size, &webp))
217 return NULL;
218
219 bool transform_present = false;
220
221 do {
222 transform_present = get_bits(&webp, 1);
223 CHECK_OVERRUN(webp, NULL);
224
225 if (transform_present) {
226 webp_transform_t transform = get_bits(&webp, 2);
227 CHECK_OVERRUN(webp, NULL);
228
229 if (transform == TRANSFORM_PREDICTOR) {
230 // FIXME TODO
231 } else
232 return NULL;
233
234 // FIXME: decode other transforms
235 }
236 } while (transform_present);
237
238 // FIXME: decode image data
239
240 return NULL;
241}
242
243/** Encode WebP format
244 *
245 * Encode WebP format into an array.
246 *
247 * @param[in] surface Surface to be encoded into WebP.
248 * @param[out] pdata Pointer to the resulting array.
249 * @param[out] psize Pointer to the size of the resulting array.
250 *
251 * @return True on succesful encoding.
252 * @return False on failure.
253 *
254 */
255bool encode_webp(surface_t *surface, void **pdata, size_t *psize)
256{
257 // TODO
258 return false;
259}
260
261/** @}
262 */
Note: See TracBrowser for help on using the repository browser.