// Copyright 2010 Google Inc. All Rights Reserved. // // Use of this source code is governed by a BSD-style license // that can be found in the COPYING file in the root of the source // tree. An additional intellectual property rights grant can be found // in the file PATENTS. All contributing project authors may // be found in the AUTHORS file in the root of the source tree. // ----------------------------------------------------------------------------- // // Main decoding functions for WEBP images. // // Author: Skal ([email protected]) #include <stdlib.h> #include "src/dec/vp8_dec.h" #include "src/dec/vp8i_dec.h" #include "src/dec/vp8li_dec.h" #include "src/dec/webpi_dec.h" #include "src/utils/utils.h" #include "src/webp/mux_types.h" // ALPHA_FLAG #include "src/webp/decode.h" #include "src/webp/types.h" //------------------------------------------------------------------------------ // RIFF layout is: // Offset tag // 0...3 "RIFF" 4-byte tag // 4...7 size of image data (including metadata) starting at offset 8 // 8...11 "WEBP" our form-type signature // The RIFF container (12 bytes) is followed by appropriate chunks: // 12..15 "VP8 ": 4-bytes tags, signaling the use of VP8 video format // 16..19 size of the raw VP8 image data, starting at offset 20 // 20.... the VP8 bytes // Or, // 12..15 "VP8L": 4-bytes tags, signaling the use of VP8L lossless format // 16..19 size of the raw VP8L image data, starting at offset 20 // 20.... the VP8L bytes // Or, // 12..15 "VP8X": 4-bytes tags, describing the extended-VP8 chunk. // 16..19 size of the VP8X chunk starting at offset 20. // 20..23 VP8X flags bit-map corresponding to the chunk-types present. // 24..26 Width of the Canvas Image. // 27..29 Height of the Canvas Image. // There can be extra chunks after the "VP8X" chunk (ICCP, ANMF, VP8, VP8L, // XMP, EXIF ...) // All sizes are in little-endian order. // Note: chunk data size must be padded to multiple of 2 when written. // Validates the RIFF container (if detected) and skips over it. // If a RIFF container is detected, returns: // VP8_STATUS_BITSTREAM_ERROR for invalid header, // VP8_STATUS_NOT_ENOUGH_DATA for truncated data if have_all_data is true, // and VP8_STATUS_OK otherwise. // In case there are not enough bytes (partial RIFF container), return 0 for // *riff_size. Else return the RIFF size extracted from the header. static VP8StatusCode ParseRIFF(const uint8_t** const data, size_t* const data_size, int have_all_data, size_t* const riff_size) { … } // Validates the VP8X header and skips over it. // Returns VP8_STATUS_BITSTREAM_ERROR for invalid VP8X header, // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and // VP8_STATUS_OK otherwise. // If a VP8X chunk is found, found_vp8x is set to true and *width_ptr, // *height_ptr and *flags_ptr are set to the corresponding values extracted // from the VP8X chunk. static VP8StatusCode ParseVP8X(const uint8_t** const data, size_t* const data_size, int* const found_vp8x, int* const width_ptr, int* const height_ptr, uint32_t* const flags_ptr) { … } // Skips to the next VP8/VP8L chunk header in the data given the size of the // RIFF chunk 'riff_size'. // Returns VP8_STATUS_BITSTREAM_ERROR if any invalid chunk size is encountered, // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and // VP8_STATUS_OK otherwise. // If an alpha chunk is found, *alpha_data and *alpha_size are set // appropriately. static VP8StatusCode ParseOptionalChunks(const uint8_t** const data, size_t* const data_size, size_t const riff_size, const uint8_t** const alpha_data, size_t* const alpha_size) { … } // Validates the VP8/VP8L Header ("VP8 nnnn" or "VP8L nnnn") and skips over it. // Returns VP8_STATUS_BITSTREAM_ERROR for invalid (chunk larger than // riff_size) VP8/VP8L header, // VP8_STATUS_NOT_ENOUGH_DATA in case of insufficient data, and // VP8_STATUS_OK otherwise. // If a VP8/VP8L chunk is found, *chunk_size is set to the total number of bytes // extracted from the VP8/VP8L chunk header. // The flag '*is_lossless' is set to 1 in case of VP8L chunk / raw VP8L data. static VP8StatusCode ParseVP8Header(const uint8_t** const data_ptr, size_t* const data_size, int have_all_data, size_t riff_size, size_t* const chunk_size, int* const is_lossless) { … } //------------------------------------------------------------------------------ // Fetch '*width', '*height', '*has_alpha' and fill out 'headers' based on // 'data'. All the output parameters may be NULL. If 'headers' is NULL only the // minimal amount will be read to fetch the remaining parameters. // If 'headers' is non-NULL this function will attempt to locate both alpha // data (with or without a VP8X chunk) and the bitstream chunk (VP8/VP8L). // Note: The following chunk sequences (before the raw VP8/VP8L data) are // considered valid by this function: // RIFF + VP8(L) // RIFF + VP8X + (optional chunks) + VP8(L) // ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose. // VP8(L) <-- Not a valid WebP format: only allowed for internal purpose. static VP8StatusCode ParseHeadersInternal(const uint8_t* data, size_t data_size, int* const width, int* const height, int* const has_alpha, int* const has_animation, int* const format, WebPHeaderStructure* const headers) { … } VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers) { … } //------------------------------------------------------------------------------ // WebPDecParams void WebPResetDecParams(WebPDecParams* const params) { … } //------------------------------------------------------------------------------ // "Into" decoding variants // Main flow WEBP_NODISCARD static VP8StatusCode DecodeInto(const uint8_t* const data, size_t data_size, WebPDecParams* const params) { … } // Helpers WEBP_NODISCARD static uint8_t* DecodeIntoRGBABuffer(WEBP_CSP_MODE colorspace, const uint8_t* const data, size_t data_size, uint8_t* const rgba, int stride, size_t size) { … } uint8_t* WebPDecodeRGBInto(const uint8_t* data, size_t data_size, uint8_t* output, size_t size, int stride) { … } uint8_t* WebPDecodeRGBAInto(const uint8_t* data, size_t data_size, uint8_t* output, size_t size, int stride) { … } uint8_t* WebPDecodeARGBInto(const uint8_t* data, size_t data_size, uint8_t* output, size_t size, int stride) { … } uint8_t* WebPDecodeBGRInto(const uint8_t* data, size_t data_size, uint8_t* output, size_t size, int stride) { … } uint8_t* WebPDecodeBGRAInto(const uint8_t* data, size_t data_size, uint8_t* output, size_t size, int stride) { … } uint8_t* WebPDecodeYUVInto(const uint8_t* data, size_t data_size, uint8_t* luma, size_t luma_size, int luma_stride, uint8_t* u, size_t u_size, int u_stride, uint8_t* v, size_t v_size, int v_stride) { … } //------------------------------------------------------------------------------ WEBP_NODISCARD static uint8_t* Decode(WEBP_CSP_MODE mode, const uint8_t* const data, size_t data_size, int* const width, int* const height, WebPDecBuffer* const keep_info) { … } uint8_t* WebPDecodeRGB(const uint8_t* data, size_t data_size, int* width, int* height) { … } uint8_t* WebPDecodeRGBA(const uint8_t* data, size_t data_size, int* width, int* height) { … } uint8_t* WebPDecodeARGB(const uint8_t* data, size_t data_size, int* width, int* height) { … } uint8_t* WebPDecodeBGR(const uint8_t* data, size_t data_size, int* width, int* height) { … } uint8_t* WebPDecodeBGRA(const uint8_t* data, size_t data_size, int* width, int* height) { … } uint8_t* WebPDecodeYUV(const uint8_t* data, size_t data_size, int* width, int* height, uint8_t** u, uint8_t** v, int* stride, int* uv_stride) { … } static void DefaultFeatures(WebPBitstreamFeatures* const features) { … } static VP8StatusCode GetFeatures(const uint8_t* const data, size_t data_size, WebPBitstreamFeatures* const features) { … } //------------------------------------------------------------------------------ // WebPGetInfo() int WebPGetInfo(const uint8_t* data, size_t data_size, int* width, int* height) { … } //------------------------------------------------------------------------------ // Advance decoding API int WebPInitDecoderConfigInternal(WebPDecoderConfig* config, int version) { … } VP8StatusCode WebPGetFeaturesInternal(const uint8_t* data, size_t data_size, WebPBitstreamFeatures* features, int version) { … } VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size, WebPDecoderConfig* config) { … } //------------------------------------------------------------------------------ // Cropping and rescaling. int WebPCheckCropDimensions(int image_width, int image_height, int x, int y, int w, int h) { … } int WebPIoInitFromOptions(const WebPDecoderOptions* const options, VP8Io* const io, WEBP_CSP_MODE src_colorspace) { … } //------------------------------------------------------------------------------