OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkWebpCodec.h" | |
9 #include "SkImageGenerator.h" | |
10 #include "SkTemplates.h" | |
11 | |
12 // A WebP decoder on top of (subset of) libwebp | |
13 // For more information on WebP image format, and libwebp library, see: | |
14 // http://code.google.com/speed/webp/ | |
15 // http://www.webmproject.org/code/#libwebp_webp_image_decoder_library | |
jzern
2015/04/03 19:15:48
#libwebp-webp-image-library
scroggo
2015/04/03 20:00:19
Done.
| |
16 // http://review.webmproject.org/gitweb?p=libwebp.git | |
jzern
2015/04/03 19:15:49
long dead. review is on gerrit.chromium, but
https
scroggo
2015/04/03 20:00:19
Done.
| |
17 | |
18 extern "C" { | |
jzern
2015/04/03 19:15:49
I don't think this was ever necessary with the pub
scroggo
2015/04/03 20:00:19
Done.
| |
19 // If moving libwebp out of skia source tree, path for webp headers must be | |
20 // updated accordingly. Here, we enforce using local copy in webp sub-directory. | |
21 #include "webp/decode.h" | |
22 #include "webp/encode.h" | |
23 } | |
24 | |
25 bool SkWebpCodec::IsWebp(SkStream* stream) { | |
26 // WEBP starts with the following: | |
27 // RIFFXXXXWEBPVP | |
28 // Where XXXX is unspecified. | |
29 const char LENGTH = 14; | |
30 char bytes[LENGTH]; | |
31 if (stream->read(&bytes, LENGTH) != LENGTH) { | |
32 return false; | |
33 } | |
34 return !memcmp(bytes, "RIFF", 4) && !memcmp(&bytes[8], "WEBPVP", 6); | |
35 } | |
36 | |
37 static const size_t WEBP_VP8_HEADER_SIZE = 64; | |
jzern
2015/04/03 19:15:48
note we use 30 in blink to ensure a partial demux
scroggo
2015/04/03 20:00:19
Done.
| |
38 | |
39 // Parse headers of RIFF container, and check for valid Webp (VP8) content. | |
40 // NOTE: This calls peek instead of read, since onGetPixels will need these | |
41 // bytes again. | |
42 static bool webp_parse_header(SkStream* stream, SkImageInfo* info) { | |
43 unsigned char buffer[WEBP_VP8_HEADER_SIZE]; | |
44 if (!stream->peek(buffer, WEBP_VP8_HEADER_SIZE)) { | |
45 return false; | |
46 } | |
47 | |
48 WebPBitstreamFeatures features; | |
49 VP8StatusCode status = WebPGetFeatures(buffer, WEBP_VP8_HEADER_SIZE, &featur es); | |
50 if (VP8_STATUS_OK != status) { | |
51 return false; // Invalid WebP file. | |
52 } | |
53 | |
54 // sanity check for image size that's about to be decoded. | |
jzern
2015/04/03 19:15:48
this is OK, we have lib protection for this too
| |
55 { | |
56 const int64_t size = sk_64_mul(features.width, features.height); | |
57 if (!sk_64_isS32(size)) { | |
58 return false; | |
59 } | |
60 // now check that if we are 4-bytes per pixel, we also don't overflow | |
61 if (sk_64_asS32(size) > (0x7FFFFFFF >> 2)) { | |
62 return false; | |
63 } | |
64 } | |
65 | |
66 if (info) { | |
67 // FIXME: Is N32 the right type? | |
68 // Is unpremul the right type? Clients of SkImageGenerator may assume it 's the | |
69 // best type, when Skia currently cannot draw unpremul (and raster is fa ster | |
70 // with premul). | |
71 *info = SkImageInfo::Make(features.width, features.height, kN32_SkColorT ype, | |
72 SkToBool(features.has_alpha) ? kUnpremul_SkAlp haType | |
73 : kOpaque_SkAlphaT ype); | |
74 } | |
75 return true; | |
76 } | |
77 | |
78 SkCodec* SkWebpCodec::NewFromStream(SkStream* stream) { | |
79 SkAutoTDelete<SkStream> streamDeleter(stream); | |
80 SkImageInfo info; | |
81 if (webp_parse_header(stream, &info)) { | |
82 return SkNEW_ARGS(SkWebpCodec, (info, streamDeleter.detach())); | |
83 } | |
84 return NULL; | |
85 } | |
86 | |
87 static bool conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) { | |
88 // FIXME: Do we want to support 565? | |
scroggo
2015/04/02 21:31:55
The plan is to not downscale to 565, but if the da
jzern
2015/04/03 19:15:48
No. The format is natively yuv420 for lossy or 32-
scroggo
2015/04/03 20:00:19
Good to know. Thanks!
| |
89 switch (dst.colorType()) { | |
90 // Both byte orders are supported. | |
91 case kBGRA_8888_SkColorType: | |
92 case kRGBA_8888_SkColorType: | |
93 break; | |
94 default: | |
95 return false; | |
96 } | |
97 if (dst.profileType() != src.profileType()) { | |
98 return false; | |
99 } | |
100 if (dst.alphaType() == src.alphaType()) { | |
101 return true; | |
102 } | |
103 return kPremul_SkAlphaType == dst.alphaType() && | |
104 kUnpremul_SkAlphaType == src.alphaType(); | |
105 } | |
106 | |
107 SkISize SkWebpCodec::onGetScaledDimensions(float desiredScale) const { | |
108 SkISize dim = this->getInfo().dimensions(); | |
109 dim.fWidth *= desiredScale; | |
110 dim.fHeight *= desiredScale; | |
111 return dim; | |
112 } | |
113 | |
114 static WEBP_CSP_MODE webp_decode_mode(SkColorType ct, bool premultiply) { | |
115 switch (ct) { | |
116 case kBGRA_8888_SkColorType: | |
117 return premultiply ? MODE_bgrA : MODE_BGRA; | |
jzern
2015/04/03 19:15:48
note this is memory layout and isn't affected by e
scroggo
2015/04/03 20:00:19
The difference between kBGRA_8888_SkColorType and
| |
118 case kRGBA_8888_SkColorType: | |
119 return premultiply ? MODE_rgbA : MODE_RGBA; | |
120 default: | |
121 return MODE_LAST; | |
122 } | |
123 } | |
124 | |
125 // The WebP decoding API allows us to incrementally pass chunks of bytes as we r eceive them to the | |
126 // decoder with WebPIAppend. In order to do so, we need to read chunks from the SkStream. This size | |
127 // is arbitrary. | |
128 static const size_t BUFFER_SIZE = 4096; | |
129 | |
130 SkImageGenerator::Result SkWebpCodec::onGetPixels(const SkImageInfo& dstInfo, vo id* dst, | |
131 size_t rowBytes, const Options &, SkPMColor*, | |
132 int*) { | |
133 switch (this->rewindIfNeeded()) { | |
134 case kCouldNotRewind_RewindState: | |
135 return kCouldNotRewind; | |
136 case kRewound_RewindState: | |
137 // Rewound to the beginning. Since creation only does a peek, the st ream is at the | |
138 // correct position. | |
139 break; | |
140 case kNoRewindNecessary_RewindState: | |
141 // Already at the right spot for decoding. | |
142 break; | |
143 } | |
144 | |
145 if (!conversion_possible(dstInfo, this->getInfo())) { | |
146 return kInvalidConversion; | |
147 } | |
148 | |
149 WebPDecoderConfig config; | |
150 if (0 == WebPInitDecoderConfig(&config)) { | |
151 // Wait, this seems like an error in the decoder? | |
jzern
2015/04/03 19:15:49
would indicate an abi mismatch
scroggo
2015/04/03 20:00:19
Acknowledged.
| |
152 return kInvalidInput; | |
153 } | |
154 | |
155 // Free any memory associated with the buffer. Must be called last, so we de clare it first. | |
156 SkAutoTCallVProc<WebPDecBuffer, WebPFreeDecBuffer> autoFree(&(config.output) ); | |
157 | |
158 SkISize dimensions = dstInfo.dimensions(); | |
159 if (this->getInfo().dimensions() != dimensions) { | |
160 // Caller is requesting scaling. | |
161 config.options.use_scaling = 1; | |
162 config.options.scaled_width = dimensions.width(); | |
163 config.options.scaled_height = dimensions.height(); | |
164 } | |
165 | |
166 config.output.colorspace = webp_decode_mode(dstInfo.colorType(), | |
167 dstInfo.alphaType() == kPremul_SkAlphaType); | |
168 config.output.u.RGBA.rgba = (uint8_t*) dst; | |
msarett
2015/04/03 12:28:13
Does this mean we always convert to RGBA? It look
scroggo
2015/04/03 14:14:12
jzern@ can confirm, but if I understand correctly,
| |
169 config.output.u.RGBA.stride = (int) rowBytes; | |
170 config.output.u.RGBA.size = dstInfo.getSafeSize(rowBytes); | |
171 config.output.is_external_memory = 1; | |
172 | |
173 SkAutoTCallVProc<WebPIDecoder, WebPIDelete> idec(WebPIDecode(NULL, 0, &confi g)); | |
174 if (!idec) { | |
175 return kInvalidInput; | |
176 } | |
177 | |
178 SkAutoMalloc storage(BUFFER_SIZE); | |
179 uint8_t* buffer = static_cast<uint8_t*>(storage.get()); | |
180 while (true) { | |
181 const size_t bytesRead = stream()->read(buffer, BUFFER_SIZE); | |
182 if (0 == bytesRead) { | |
183 return kInvalidInput; // Or incomplete? | |
msarett
2015/04/03 12:28:13
I'm not familiar with webp format, but in this typ
scroggo
2015/04/03 14:14:12
Yeah, I'm a little torn about what to do in this s
jzern
2015/04/03 19:21:34
yes this is how we handle incremental decoding in
| |
184 } | |
185 | |
186 switch (WebPIAppend(idec, buffer, bytesRead)) { | |
187 case VP8_STATUS_OK: | |
188 return kSuccess; | |
189 case VP8_STATUS_SUSPENDED: | |
190 // Break out of the switch statement. Continue the loop. | |
191 break; | |
192 default: | |
193 return kInvalidInput; | |
194 } | |
195 } | |
196 } | |
197 | |
198 SkWebpCodec::SkWebpCodec(const SkImageInfo& info, SkStream* stream) | |
199 : INHERITED(info, stream) {} | |
OLD | NEW |