|
|
|
|
|
|
1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* ***** BEGIN LICENSE BLOCK ***** |
| 3 |
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 4 |
* |
| 5 |
* The contents of this file are subject to the Mozilla Public License Version |
| 6 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* http://www.mozilla.org/MPL/ |
| 9 |
* |
| 10 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 11 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 12 |
* for the specific language governing rights and limitations under the |
| 13 |
* License. |
| 14 |
* |
| 15 |
* The Original Code is WebP Decoder. |
| 16 |
* |
| 17 |
* The Initial Developer of the Original Code is Google Inc. |
| 18 |
* Portions created by the Initial Developer are Copyright (C) 2010 |
| 19 |
* the Initial Developer. All Rights Reserved. |
| 20 |
* |
| 21 |
* Contributor(s): |
| 22 |
* Aaron Kaluszka <ask@swva.net> |
| 23 |
* |
| 24 |
* Alternatively, the contents of this file may be used under the terms of |
| 25 |
* either the GNU General Public License Version 2 or later (the "GPL"), or |
| 26 |
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 27 |
* in which case the provisions of the GPL or the LGPL are applicable instead |
| 28 |
* of those above. If you wish to allow use of your version of this file only |
| 29 |
* under the terms of either the GPL or the LGPL, and not to allow others to |
| 30 |
* use your version of this file under the terms of the MPL, indicate your |
| 31 |
* decision by deleting the provisions above and replace them with the notice |
| 32 |
* and other provisions required by the GPL or the LGPL. If you do not delete |
| 33 |
* the provisions above, a recipient may use your version of this file under |
| 34 |
* the terms of any one of the MPL, the GPL or the LGPL. |
| 35 |
* |
| 36 |
* ***** END LICENSE BLOCK ***** */ |
| 37 |
|
| 38 |
#include "nsWebPDecoder.h" |
| 39 |
|
| 40 |
#include "nsIInputStream.h" |
| 41 |
#include "RasterImage.h" |
| 42 |
#include "imgIContainerObserver.h" |
| 43 |
|
| 44 |
#include "prlog.h" |
| 45 |
|
| 46 |
namespace mozilla { |
| 47 |
namespace imagelib { |
| 48 |
|
| 49 |
#ifdef PR_LOGGING |
| 50 |
PRLogModuleInfo *gWebPLog = PR_NewLogModule("WebPDecoder"); |
| 51 |
#endif |
| 52 |
|
| 53 |
nsWebPDecoder::nsWebPDecoder() : |
| 54 |
mWidth(-1), |
| 55 |
mHeight(-1), |
| 56 |
mPos(0), |
| 57 |
mData(nsnull), |
| 58 |
mIter(nsnull) |
| 59 |
{ |
| 60 |
if (!sInitDone) { |
| 61 |
// Initialize look-up tables for YUV to RGB conversion |
| 62 |
for (PRInt32 i = 0; i < 256; ++i) |
| 63 |
{ |
| 64 |
sVToR[i] = ( 89858 * (i - 128) + RGB_HALF) >> RGB_FRAC; |
| 65 |
sUToG[i] = -22014 * (i - 128) + RGB_HALF; |
| 66 |
sVToG[i] = -45773 * (i - 128); |
| 67 |
sUToB[i] = (113618 * (i - 128) + RGB_HALF) >> RGB_FRAC; |
| 68 |
} |
| 69 |
for (PRInt32 i = RGB_RANGE_MIN; i < RGB_RANGE_MAX; ++i) { |
| 70 |
const PRInt32 j = ((i - 16) * 76283 + RGB_HALF) >> RGB_FRAC; |
| 71 |
sClip[i - RGB_RANGE_MIN] = (j < 0) ? 0 : (j > 255) ? 255 : j; |
| 72 |
} |
| 73 |
sInitDone = PR_TRUE; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
nsWebPDecoder::~nsWebPDecoder() |
| 78 |
{ |
| 79 |
if (mData) |
| 80 |
free(mData); |
| 81 |
} |
| 82 |
|
| 83 |
void nsWebPDecoder::InitInternal() |
| 84 |
{ |
| 85 |
// Initialize libvpx |
| 86 |
if (vpx_codec_dec_init(&mDec, &vpx_codec_vp8_dx_algo, NULL, 0) != VPX_CODEC_OK) { |
| 87 |
PostDecoderError(NS_ERROR_FAILURE); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
mPPCfg.post_proc_flag = VP8_NOFILTERING; |
| 92 |
vpx_codec_control(&mDec, VP8_SET_POSTPROC, &mPPCfg); |
| 93 |
} |
| 94 |
|
| 95 |
void |
| 96 |
nsWebPDecoder::WriteInternal(const char *aBuffer, PRUint32 aCount) |
| 97 |
{ |
| 98 |
NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!"); |
| 99 |
|
| 100 |
// aCount = 0 means EOF, mWidth set means image has been processed |
| 101 |
if (!aCount || mWidth != -1) |
| 102 |
return; |
| 103 |
|
| 104 |
// RIFF layout is: |
| 105 |
// Offset Data |
| 106 |
// 0...3 "RIFF" 4-byte tag |
| 107 |
// 4...7 Size of image data (including metadata) starting at offset 8 |
| 108 |
// 8...11 "WEBP" Form-type signature |
| 109 |
// 12..15 "VP8 " 4-byte tag describing the raw video format used |
| 110 |
// 16..19 Size of the raw WebP image data, starting at offset 20 |
| 111 |
// 20.... The WebP bytes |
| 112 |
// There can be extra chunks after the "VP8 " chunk (ICMT, ICOP, ...) |
| 113 |
// All 32-bits sizes are in little-endian order. |
| 114 |
// Note: chunk data must be padded to multiple of 2 in size |
| 115 |
if (mPos < RIFF_HEADER_LENGTH) { |
| 116 |
PRUint32 toCopy = RIFF_HEADER_LENGTH - mPos; |
| 117 |
if (toCopy > aCount) |
| 118 |
toCopy = aCount; |
| 119 |
memcpy(mHeaderData + mPos, aBuffer, toCopy); |
| 120 |
mPos += toCopy; |
| 121 |
aCount -= toCopy; |
| 122 |
aBuffer += toCopy; |
| 123 |
} |
| 124 |
|
| 125 |
if (mPos == RIFF_HEADER_LENGTH) { |
| 126 |
if (memcmp(mHeaderData, "RIFF", 4)) { |
| 127 |
PostDataError(); // wrong image file signature |
| 128 |
return; |
| 129 |
} |
| 130 |
if (memcmp(mHeaderData + 8, "WEBP", 4)) { |
| 131 |
PostDataError(); // wrong image file signature |
| 132 |
return; |
| 133 |
} |
| 134 |
if (memcmp(mHeaderData + 12, "VP8 ", 4)) { |
| 135 |
PostDataError(); // invalid compression format |
| 136 |
return; |
| 137 |
} |
| 138 |
mDataLength = LITTLE_TO_NATIVE32(*(PRUint32*)(mHeaderData + 16)); |
| 139 |
if ((mDataLength > LITTLE_TO_NATIVE32(*(PRUint32*)(mHeaderData + 4)) + 8) || (mDataLength & 1)) { |
| 140 |
PostDataError(); // inconsistent size information |
| 141 |
return; |
| 142 |
} |
| 143 |
mData = (PRUint8*)moz_malloc(mDataLength); |
| 144 |
if (!mData) { |
| 145 |
PostDecoderError(NS_ERROR_OUT_OF_MEMORY); |
| 146 |
return; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// XXX Currently no progressive decoding support, so grab all image data |
| 151 |
if (aCount && mPos >= RIFF_HEADER_LENGTH && mPos < RIFF_HEADER_LENGTH + mDataLength) { |
| 152 |
PRUint32 toCopy = RIFF_HEADER_LENGTH + mDataLength - mPos; |
| 153 |
if (toCopy > aCount) |
| 154 |
toCopy = aCount; |
| 155 |
memcpy(mData + mPos - RIFF_HEADER_LENGTH, aBuffer, toCopy); |
| 156 |
mPos += toCopy; |
| 157 |
aCount -= toCopy; |
| 158 |
aBuffer += toCopy; |
| 159 |
} |
| 160 |
|
| 161 |
if (mPos == RIFF_HEADER_LENGTH + mDataLength - 1) { |
| 162 |
// Avoid full decode for size query |
| 163 |
if (IsSizeDecode()) { |
| 164 |
vpx_codec_stream_info_t streamInfo; |
| 165 |
if (vpx_codec_peek_stream_info(&vpx_codec_vp8_dx_algo, mData, mDataLength, &streamInfo) != VPX_CODEC_OK) { |
| 166 |
PostDataError(); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
mWidth = streamInfo.w; |
| 171 |
mHeight = streamInfo.h; |
| 172 |
PR_LOG(gWebPLog, PR_LOG_DEBUG, ("WebP image is %lix%lix.\n", mWidth, mHeight)); |
| 173 |
// Post our size to the superclass |
| 174 |
PostSize(mWidth, mHeight); |
| 175 |
return; |
| 176 |
} |
| 177 |
|
| 178 |
// Decode data with libvpx |
| 179 |
if (vpx_codec_decode(&mDec, mData, mDataLength, NULL, 0) != VPX_CODEC_OK) { |
| 180 |
PostDataError(); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
// Get image frame from libvpx |
| 185 |
vpx_image_t* const imageFrame = vpx_codec_get_frame(&mDec, &mIter); |
| 186 |
if (!imageFrame) { |
| 187 |
PostDataError(); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
mWidth = imageFrame->d_w; |
| 192 |
mHeight = imageFrame->d_h; |
| 193 |
PR_LOG(gWebPLog, PR_LOG_DEBUG, ("WebP image is %lix%lix.\n", mWidth, mHeight)); |
| 194 |
// Post our size to the superclass |
| 195 |
PostSize(mWidth, mHeight); |
| 196 |
|
| 197 |
PRUint32 imageLength; |
| 198 |
PRUint32* imageData; |
| 199 |
nsresult rv = mImage->AppendFrame(0, 0, mWidth, mHeight, gfxASurface::ImageFormatARGB32, |
| 200 |
(PRUint8**)&imageData, &imageLength); |
| 201 |
if (NS_FAILED(rv) || !imageData) { |
| 202 |
PostDecoderError(NS_ERROR_FAILURE); |
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
// Tell the superclass we're starting a frame |
| 207 |
PostFrameStart(); |
| 208 |
|
| 209 |
// Get YUV data from decoder |
| 210 |
PRUint8* YSrc = imageFrame->planes[0]; |
| 211 |
PRUint8* USrc = imageFrame->planes[1]; |
| 212 |
PRUint8* VSrc = imageFrame->planes[2]; |
| 213 |
PRUint32* Dst = imageData; |
| 214 |
PRUint32 YStride = imageFrame->stride[0]; |
| 215 |
PRUint32 UStride = imageFrame->stride[1]; |
| 216 |
PRUint32 VStride = imageFrame->stride[2]; |
| 217 |
|
| 218 |
// Generate ARGB image from YUV data (with height upsampling of chroma data) |
| 219 |
for (PRInt32 y = 0; y < (mHeight >> 1); ++y) |
| 220 |
{ |
| 221 |
YUV420toARGBLine(Dst, YSrc, USrc, VSrc); |
| 222 |
YSrc += YStride; |
| 223 |
YUV420toARGBLine(Dst, YSrc, USrc, VSrc); |
| 224 |
YSrc += YStride; |
| 225 |
USrc += UStride; |
| 226 |
VSrc += VStride; |
| 227 |
} |
| 228 |
if(mHeight & 1) // Bottom row (if image has odd height) |
| 229 |
YUV420toARGBLine(Dst, YSrc, USrc, VSrc); |
| 230 |
|
| 231 |
// Invalidate |
| 232 |
nsIntRect r(0, 0, mWidth, mHeight); |
| 233 |
PostInvalidation(r); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void |
| 238 |
nsWebPDecoder::FinishInternal() |
| 239 |
{ |
| 240 |
// We shouldn't be called in error cases |
| 241 |
NS_ABORT_IF_FALSE(!HasError(), "Can't call FinishInternal on error!"); |
| 242 |
|
| 243 |
vpx_codec_destroy(&mDec); |
| 244 |
|
| 245 |
// We should never make multiple frames |
| 246 |
NS_ABORT_IF_FALSE(GetFrameCount() <= 1, "Multiple WebP frames?"); |
| 247 |
|
| 248 |
// Send notifications if appropriate |
| 249 |
if (!IsSizeDecode() && (GetFrameCount() == 1)) { |
| 250 |
PostFrameStop(); |
| 251 |
PostDecodeDone(); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
// Converts YUV to RGB and writes into a 32 bit pixel in endian-neutral fashion |
| 256 |
inline void |
| 257 |
nsWebPDecoder::SetPixel(PRUint32*& aDst, PRUint8 aY, PRUint8 aU, PRUint8 aV, PRUint8 aAlpha = 0xFF) |
| 258 |
{ |
| 259 |
*aDst++ = GFX_PACKED_PIXEL(aAlpha, |
| 260 |
sClip[aY + sVToR[aV] - RGB_RANGE_MIN], |
| 261 |
sClip[aY + ((sVToG[aV] + sUToG[aU]) >> RGB_FRAC) - RGB_RANGE_MIN], |
| 262 |
sClip[aY + sUToB[aU] - RGB_RANGE_MIN]); |
| 263 |
} |
| 264 |
|
| 265 |
// Generate ARGB row from an YUV row (with width upsampling of chroma data) |
| 266 |
void |
| 267 |
nsWebPDecoder::YUV420toARGBLine(PRUint32*& aDst, PRUint8* aYSrc, PRUint8* aUSrc, PRUint8* aVSrc) |
| 268 |
{ |
| 269 |
for (PRInt32 x = 0; x < (mWidth >> 1); ++x) { |
| 270 |
SetPixel(aDst, *aYSrc++, *aUSrc, *aVSrc ); |
| 271 |
SetPixel(aDst, *aYSrc++, *aUSrc++, *aVSrc++); |
| 272 |
} |
| 273 |
if (mWidth & 1) // Right-most pixel (if image has odd width) |
| 274 |
SetPixel(aDst, *aYSrc++, *aUSrc++, *aVSrc++); |
| 275 |
} |
| 276 |
|
| 277 |
PRBool nsWebPDecoder::sInitDone = PR_FALSE; |
| 278 |
PRInt16 nsWebPDecoder::sVToR[256]; |
| 279 |
PRInt16 nsWebPDecoder::sUToB[256]; |
| 280 |
PRInt32 nsWebPDecoder::sVToG[256]; |
| 281 |
PRInt32 nsWebPDecoder::sUToG[256]; |
| 282 |
PRUint8 nsWebPDecoder::sClip[RGB_RANGE_MAX - RGB_RANGE_MIN]; |
| 283 |
|
| 284 |
} // namespace imagelib |
| 285 |
} // namespace mozilla |