Path: blob/master/thirdparty/libwebp/src/webp/decode.h
9913 views
// Copyright 2010 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Main decoding functions for WebP images.10//11// Author: Skal ([email protected])1213#ifndef WEBP_WEBP_DECODE_H_14#define WEBP_WEBP_DECODE_H_1516#include "./types.h"1718#ifdef __cplusplus19extern "C" {20#endif2122#define WEBP_DECODER_ABI_VERSION 0x0209 // MAJOR(8b) + MINOR(8b)2324// Note: forward declaring enumerations is not allowed in (strict) C and C++,25// the types are left here for reference.26// typedef enum VP8StatusCode VP8StatusCode;27// typedef enum WEBP_CSP_MODE WEBP_CSP_MODE;28typedef struct WebPRGBABuffer WebPRGBABuffer;29typedef struct WebPYUVABuffer WebPYUVABuffer;30typedef struct WebPDecBuffer WebPDecBuffer;31typedef struct WebPIDecoder WebPIDecoder;32typedef struct WebPBitstreamFeatures WebPBitstreamFeatures;33typedef struct WebPDecoderOptions WebPDecoderOptions;34typedef struct WebPDecoderConfig WebPDecoderConfig;3536// Return the decoder's version number, packed in hexadecimal using 8bits for37// each of major/minor/revision. E.g: v2.5.7 is 0x020507.38WEBP_EXTERN int WebPGetDecoderVersion(void);3940// Retrieve basic header information: width, height.41// This function will also validate the header, returning true on success,42// false otherwise. '*width' and '*height' are only valid on successful return.43// Pointers 'width' and 'height' can be passed NULL if deemed irrelevant.44// Note: The following chunk sequences (before the raw VP8/VP8L data) are45// considered valid by this function:46// RIFF + VP8(L)47// RIFF + VP8X + (optional chunks) + VP8(L)48// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.49// VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.50WEBP_NODISCARD WEBP_EXTERN int WebPGetInfo(51const uint8_t* data, size_t data_size, int* width, int* height);5253// Decodes WebP images pointed to by 'data' and returns RGBA samples, along54// with the dimensions in *width and *height. The ordering of samples in55// memory is R, G, B, A, R, G, B, A... in scan order (endian-independent).56// The returned pointer should be deleted calling WebPFree().57// Returns NULL in case of error.58WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeRGBA(59const uint8_t* data, size_t data_size, int* width, int* height);6061// Same as WebPDecodeRGBA, but returning A, R, G, B, A, R, G, B... ordered data.62WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeARGB(63const uint8_t* data, size_t data_size, int* width, int* height);6465// Same as WebPDecodeRGBA, but returning B, G, R, A, B, G, R, A... ordered data.66WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeBGRA(67const uint8_t* data, size_t data_size, int* width, int* height);6869// Same as WebPDecodeRGBA, but returning R, G, B, R, G, B... ordered data.70// If the bitstream contains transparency, it is ignored.71WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeRGB(72const uint8_t* data, size_t data_size, int* width, int* height);7374// Same as WebPDecodeRGB, but returning B, G, R, B, G, R... ordered data.75WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeBGR(76const uint8_t* data, size_t data_size, int* width, int* height);7778// Decode WebP images pointed to by 'data' to Y'UV format(*). The pointer79// returned is the Y samples buffer. Upon return, *u and *v will point to80// the U and V chroma data. These U and V buffers need NOT be passed to81// WebPFree(), unlike the returned Y luma one. The dimension of the U and V82// planes are both (*width + 1) / 2 and (*height + 1) / 2.83// Upon return, the Y buffer has a stride returned as '*stride', while U and V84// have a common stride returned as '*uv_stride'.85// 'width' and 'height' may be NULL, the other pointers must not be.86// Returns NULL in case of error.87// (*) Also named Y'CbCr. See: https://en.wikipedia.org/wiki/YCbCr88WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeYUV(89const uint8_t* data, size_t data_size, int* width, int* height,90uint8_t** u, uint8_t** v, int* stride, int* uv_stride);9192// These five functions are variants of the above ones, that decode the image93// directly into a pre-allocated buffer 'output_buffer'. The maximum storage94// available in this buffer is indicated by 'output_buffer_size'. If this95// storage is not sufficient (or an error occurred), NULL is returned.96// Otherwise, output_buffer is returned, for convenience.97// The parameter 'output_stride' specifies the distance (in bytes)98// between scanlines. Hence, output_buffer_size is expected to be at least99// output_stride x picture-height.100WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeRGBAInto(101const uint8_t* data, size_t data_size,102uint8_t* output_buffer, size_t output_buffer_size, int output_stride);103WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeARGBInto(104const uint8_t* data, size_t data_size,105uint8_t* output_buffer, size_t output_buffer_size, int output_stride);106WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeBGRAInto(107const uint8_t* data, size_t data_size,108uint8_t* output_buffer, size_t output_buffer_size, int output_stride);109110// RGB and BGR variants. Here too the transparency information, if present,111// will be dropped and ignored.112WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeRGBInto(113const uint8_t* data, size_t data_size,114uint8_t* output_buffer, size_t output_buffer_size, int output_stride);115WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeBGRInto(116const uint8_t* data, size_t data_size,117uint8_t* output_buffer, size_t output_buffer_size, int output_stride);118119// WebPDecodeYUVInto() is a variant of WebPDecodeYUV() that operates directly120// into pre-allocated luma/chroma plane buffers. This function requires the121// strides to be passed: one for the luma plane and one for each of the122// chroma ones. The size of each plane buffer is passed as 'luma_size',123// 'u_size' and 'v_size' respectively.124// Pointer to the luma plane ('*luma') is returned or NULL if an error occurred125// during decoding (or because some buffers were found to be too small).126WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPDecodeYUVInto(127const uint8_t* data, size_t data_size,128uint8_t* luma, size_t luma_size, int luma_stride,129uint8_t* u, size_t u_size, int u_stride,130uint8_t* v, size_t v_size, int v_stride);131132//------------------------------------------------------------------------------133// Output colorspaces and buffer134135// Colorspaces136// Note: the naming describes the byte-ordering of packed samples in memory.137// For instance, MODE_BGRA relates to samples ordered as B,G,R,A,B,G,R,A,...138// Non-capital names (e.g.:MODE_Argb) relates to pre-multiplied RGB channels.139// RGBA-4444 and RGB-565 colorspaces are represented by following byte-order:140// RGBA-4444: [r3 r2 r1 r0 g3 g2 g1 g0], [b3 b2 b1 b0 a3 a2 a1 a0], ...141// RGB-565: [r4 r3 r2 r1 r0 g5 g4 g3], [g2 g1 g0 b4 b3 b2 b1 b0], ...142// In the case WEBP_SWAP_16BITS_CSP is defined, the bytes are swapped for143// these two modes:144// RGBA-4444: [b3 b2 b1 b0 a3 a2 a1 a0], [r3 r2 r1 r0 g3 g2 g1 g0], ...145// RGB-565: [g2 g1 g0 b4 b3 b2 b1 b0], [r4 r3 r2 r1 r0 g5 g4 g3], ...146147typedef enum WEBP_CSP_MODE {148MODE_RGB = 0, MODE_RGBA = 1,149MODE_BGR = 2, MODE_BGRA = 3,150MODE_ARGB = 4, MODE_RGBA_4444 = 5,151MODE_RGB_565 = 6,152// RGB-premultiplied transparent modes (alpha value is preserved)153MODE_rgbA = 7,154MODE_bgrA = 8,155MODE_Argb = 9,156MODE_rgbA_4444 = 10,157// YUV modes must come after RGB ones.158MODE_YUV = 11, MODE_YUVA = 12, // yuv 4:2:0159MODE_LAST = 13160} WEBP_CSP_MODE;161162// Some useful macros:163static WEBP_INLINE int WebPIsPremultipliedMode(WEBP_CSP_MODE mode) {164return (mode == MODE_rgbA || mode == MODE_bgrA || mode == MODE_Argb ||165mode == MODE_rgbA_4444);166}167168static WEBP_INLINE int WebPIsAlphaMode(WEBP_CSP_MODE mode) {169return (mode == MODE_RGBA || mode == MODE_BGRA || mode == MODE_ARGB ||170mode == MODE_RGBA_4444 || mode == MODE_YUVA ||171WebPIsPremultipliedMode(mode));172}173174static WEBP_INLINE int WebPIsRGBMode(WEBP_CSP_MODE mode) {175return (mode < MODE_YUV);176}177178//------------------------------------------------------------------------------179// WebPDecBuffer: Generic structure for describing the output sample buffer.180181struct WebPRGBABuffer { // view as RGBA182uint8_t* rgba; // pointer to RGBA samples183int stride; // stride in bytes from one scanline to the next.184size_t size; // total size of the *rgba buffer.185};186187struct WebPYUVABuffer { // view as YUVA188uint8_t* y, *u, *v, *a; // pointer to luma, chroma U/V, alpha samples189int y_stride; // luma stride190int u_stride, v_stride; // chroma strides191int a_stride; // alpha stride192size_t y_size; // luma plane size193size_t u_size, v_size; // chroma planes size194size_t a_size; // alpha-plane size195};196197// Output buffer198struct WebPDecBuffer {199WEBP_CSP_MODE colorspace; // Colorspace.200int width, height; // Dimensions.201int is_external_memory; // If non-zero, 'internal_memory' pointer is not202// used. If value is '2' or more, the external203// memory is considered 'slow' and multiple204// read/write will be avoided.205union {206WebPRGBABuffer RGBA;207WebPYUVABuffer YUVA;208} u; // Nameless union of buffer parameters.209uint32_t pad[4]; // padding for later use210211uint8_t* private_memory; // Internally allocated memory (only when212// is_external_memory is 0). Should not be used213// externally, but accessed via the buffer union.214};215216// Internal, version-checked, entry point217WEBP_NODISCARD WEBP_EXTERN int WebPInitDecBufferInternal(WebPDecBuffer*, int);218219// Initialize the structure as empty. Must be called before any other use.220// Returns false in case of version mismatch221WEBP_NODISCARD static WEBP_INLINE int WebPInitDecBuffer(WebPDecBuffer* buffer) {222return WebPInitDecBufferInternal(buffer, WEBP_DECODER_ABI_VERSION);223}224225// Free any memory associated with the buffer. Must always be called last.226// Note: doesn't free the 'buffer' structure itself.227WEBP_EXTERN void WebPFreeDecBuffer(WebPDecBuffer* buffer);228229//------------------------------------------------------------------------------230// Enumeration of the status codes231232typedef enum WEBP_NODISCARD VP8StatusCode {233VP8_STATUS_OK = 0,234VP8_STATUS_OUT_OF_MEMORY,235VP8_STATUS_INVALID_PARAM,236VP8_STATUS_BITSTREAM_ERROR,237VP8_STATUS_UNSUPPORTED_FEATURE,238VP8_STATUS_SUSPENDED,239VP8_STATUS_USER_ABORT,240VP8_STATUS_NOT_ENOUGH_DATA241} VP8StatusCode;242243//------------------------------------------------------------------------------244// Incremental decoding245//246// This API allows streamlined decoding of partial data.247// Picture can be incrementally decoded as data become available thanks to the248// WebPIDecoder object. This object can be left in a SUSPENDED state if the249// picture is only partially decoded, pending additional input.250// Code example:251/*252WebPInitDecBuffer(&output_buffer);253output_buffer.colorspace = mode;254...255WebPIDecoder* idec = WebPINewDecoder(&output_buffer);256while (additional_data_is_available) {257// ... (get additional data in some new_data[] buffer)258status = WebPIAppend(idec, new_data, new_data_size);259if (status != VP8_STATUS_OK && status != VP8_STATUS_SUSPENDED) {260break; // an error occurred.261}262263// The above call decodes the current available buffer.264// Part of the image can now be refreshed by calling265// WebPIDecGetRGB()/WebPIDecGetYUVA() etc.266}267WebPIDelete(idec);268*/269270// Creates a new incremental decoder with the supplied buffer parameter.271// This output_buffer can be passed NULL, in which case a default output buffer272// is used (with MODE_RGB). Otherwise, an internal reference to 'output_buffer'273// is kept, which means that the lifespan of 'output_buffer' must be larger than274// that of the returned WebPIDecoder object.275// The supplied 'output_buffer' content MUST NOT be changed between calls to276// WebPIAppend() or WebPIUpdate() unless 'output_buffer.is_external_memory' is277// not set to 0. In such a case, it is allowed to modify the pointers, size and278// stride of output_buffer.u.RGBA or output_buffer.u.YUVA, provided they remain279// within valid bounds.280// All other fields of WebPDecBuffer MUST remain constant between calls.281// Returns NULL if the allocation failed.282WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPINewDecoder(283WebPDecBuffer* output_buffer);284285// This function allocates and initializes an incremental-decoder object, which286// will output the RGB/A samples specified by 'csp' into a preallocated287// buffer 'output_buffer'. The size of this buffer is at least288// 'output_buffer_size' and the stride (distance in bytes between two scanlines)289// is specified by 'output_stride'.290// Additionally, output_buffer can be passed NULL in which case the output291// buffer will be allocated automatically when the decoding starts. The292// colorspace 'csp' is taken into account for allocating this buffer. All other293// parameters are ignored.294// Returns NULL if the allocation failed, or if some parameters are invalid.295WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPINewRGB(296WEBP_CSP_MODE csp,297uint8_t* output_buffer, size_t output_buffer_size, int output_stride);298299// This function allocates and initializes an incremental-decoder object, which300// will output the raw luma/chroma samples into a preallocated planes if301// supplied. The luma plane is specified by its pointer 'luma', its size302// 'luma_size' and its stride 'luma_stride'. Similarly, the chroma-u plane303// is specified by the 'u', 'u_size' and 'u_stride' parameters, and the chroma-v304// plane by 'v' and 'v_size'. And same for the alpha-plane. The 'a' pointer305// can be pass NULL in case one is not interested in the transparency plane.306// Conversely, 'luma' can be passed NULL if no preallocated planes are supplied.307// In this case, the output buffer will be automatically allocated (using308// MODE_YUVA) when decoding starts. All parameters are then ignored.309// Returns NULL if the allocation failed or if a parameter is invalid.310WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPINewYUVA(311uint8_t* luma, size_t luma_size, int luma_stride,312uint8_t* u, size_t u_size, int u_stride,313uint8_t* v, size_t v_size, int v_stride,314uint8_t* a, size_t a_size, int a_stride);315316// Deprecated version of the above, without the alpha plane.317// Kept for backward compatibility.318WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPINewYUV(319uint8_t* luma, size_t luma_size, int luma_stride,320uint8_t* u, size_t u_size, int u_stride,321uint8_t* v, size_t v_size, int v_stride);322323// Deletes the WebPIDecoder object and associated memory. Must always be called324// if WebPINewDecoder, WebPINewRGB or WebPINewYUV succeeded.325WEBP_EXTERN void WebPIDelete(WebPIDecoder* idec);326327// Copies and decodes the next available data. Returns VP8_STATUS_OK when328// the image is successfully decoded. Returns VP8_STATUS_SUSPENDED when more329// data is expected. Returns error in other cases.330WEBP_EXTERN VP8StatusCode WebPIAppend(331WebPIDecoder* idec, const uint8_t* data, size_t data_size);332333// A variant of the above function to be used when data buffer contains334// partial data from the beginning. In this case data buffer is not copied335// to the internal memory.336// Note that the value of the 'data' pointer can change between calls to337// WebPIUpdate, for instance when the data buffer is resized to fit larger data.338WEBP_EXTERN VP8StatusCode WebPIUpdate(339WebPIDecoder* idec, const uint8_t* data, size_t data_size);340341// Returns the RGB/A image decoded so far. Returns NULL if output params342// are not initialized yet. The RGB/A output type corresponds to the colorspace343// specified during call to WebPINewDecoder() or WebPINewRGB().344// *last_y is the index of last decoded row in raster scan order. Some pointers345// (*last_y, *width etc.) can be NULL if corresponding information is not346// needed. The values in these pointers are only valid on successful (non-NULL)347// return.348WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPIDecGetRGB(349const WebPIDecoder* idec, int* last_y,350int* width, int* height, int* stride);351352// Same as above function to get a YUVA image. Returns pointer to the luma353// plane or NULL in case of error. If there is no alpha information354// the alpha pointer '*a' will be returned NULL.355WEBP_NODISCARD WEBP_EXTERN uint8_t* WebPIDecGetYUVA(356const WebPIDecoder* idec, int* last_y,357uint8_t** u, uint8_t** v, uint8_t** a,358int* width, int* height, int* stride, int* uv_stride, int* a_stride);359360// Deprecated alpha-less version of WebPIDecGetYUVA(): it will ignore the361// alpha information (if present). Kept for backward compatibility.362WEBP_NODISCARD static WEBP_INLINE uint8_t* WebPIDecGetYUV(363const WebPIDecoder* idec, int* last_y, uint8_t** u, uint8_t** v,364int* width, int* height, int* stride, int* uv_stride) {365return WebPIDecGetYUVA(idec, last_y, u, v, NULL, width, height,366stride, uv_stride, NULL);367}368369// Generic call to retrieve information about the displayable area.370// If non NULL, the left/right/width/height pointers are filled with the visible371// rectangular area so far.372// Returns NULL in case the incremental decoder object is in an invalid state.373// Otherwise returns the pointer to the internal representation. This structure374// is read-only, tied to WebPIDecoder's lifespan and should not be modified.375WEBP_NODISCARD WEBP_EXTERN const WebPDecBuffer* WebPIDecodedArea(376const WebPIDecoder* idec, int* left, int* top, int* width, int* height);377378//------------------------------------------------------------------------------379// Advanced decoding parametrization380//381// Code sample for using the advanced decoding API382/*383// A) Init a configuration object384WebPDecoderConfig config;385CHECK(WebPInitDecoderConfig(&config));386387// B) optional: retrieve the bitstream's features.388CHECK(WebPGetFeatures(data, data_size, &config.input) == VP8_STATUS_OK);389390// C) Adjust 'config', if needed391config.options.no_fancy_upsampling = 1;392config.output.colorspace = MODE_BGRA;393// etc.394395// Note that you can also make config.output point to an externally396// supplied memory buffer, provided it's big enough to store the decoded397// picture. Otherwise, config.output will just be used to allocate memory398// and store the decoded picture.399400// D) Decode!401CHECK(WebPDecode(data, data_size, &config) == VP8_STATUS_OK);402403// E) Decoded image is now in config.output (and config.output.u.RGBA)404405// F) Reclaim memory allocated in config's object. It's safe to call406// this function even if the memory is external and wasn't allocated407// by WebPDecode().408WebPFreeDecBuffer(&config.output);409*/410411// Features gathered from the bitstream412struct WebPBitstreamFeatures {413int width; // Width in pixels, as read from the bitstream.414int height; // Height in pixels, as read from the bitstream.415int has_alpha; // True if the bitstream contains an alpha channel.416int has_animation; // True if the bitstream is an animation.417int format; // 0 = undefined (/mixed), 1 = lossy, 2 = lossless418419uint32_t pad[5]; // padding for later use420};421422// Internal, version-checked, entry point423WEBP_EXTERN VP8StatusCode WebPGetFeaturesInternal(424const uint8_t*, size_t, WebPBitstreamFeatures*, int);425426// Retrieve features from the bitstream. The *features structure is filled427// with information gathered from the bitstream.428// Returns VP8_STATUS_OK when the features are successfully retrieved. Returns429// VP8_STATUS_NOT_ENOUGH_DATA when more data is needed to retrieve the430// features from headers. Returns error in other cases.431// Note: The following chunk sequences (before the raw VP8/VP8L data) are432// considered valid by this function:433// RIFF + VP8(L)434// RIFF + VP8X + (optional chunks) + VP8(L)435// ALPH + VP8 <-- Not a valid WebP format: only allowed for internal purpose.436// VP8(L) <-- Not a valid WebP format: only allowed for internal purpose.437static WEBP_INLINE VP8StatusCode WebPGetFeatures(438const uint8_t* data, size_t data_size,439WebPBitstreamFeatures* features) {440return WebPGetFeaturesInternal(data, data_size, features,441WEBP_DECODER_ABI_VERSION);442}443444// Decoding options445struct WebPDecoderOptions {446int bypass_filtering; // if true, skip the in-loop filtering447int no_fancy_upsampling; // if true, use faster pointwise upsampler448int use_cropping; // if true, cropping is applied _first_449int crop_left, crop_top; // top-left position for cropping.450// Will be snapped to even values.451int crop_width, crop_height; // dimension of the cropping area452int use_scaling; // if true, scaling is applied _afterward_453int scaled_width, scaled_height; // final resolution454int use_threads; // if true, use multi-threaded decoding455int dithering_strength; // dithering strength (0=Off, 100=full)456int flip; // if true, flip output vertically457int alpha_dithering_strength; // alpha dithering strength in [0..100]458459uint32_t pad[5]; // padding for later use460};461462// Main object storing the configuration for advanced decoding.463struct WebPDecoderConfig {464WebPBitstreamFeatures input; // Immutable bitstream features (optional)465WebPDecBuffer output; // Output buffer (can point to external mem)466WebPDecoderOptions options; // Decoding options467};468469// Internal, version-checked, entry point470WEBP_NODISCARD WEBP_EXTERN int WebPInitDecoderConfigInternal(WebPDecoderConfig*,471int);472473// Initialize the configuration as empty. This function must always be474// called first, unless WebPGetFeatures() is to be called.475// Returns false in case of mismatched version.476WEBP_NODISCARD static WEBP_INLINE int WebPInitDecoderConfig(477WebPDecoderConfig* config) {478return WebPInitDecoderConfigInternal(config, WEBP_DECODER_ABI_VERSION);479}480481// Instantiate a new incremental decoder object with the requested482// configuration. The bitstream can be passed using 'data' and 'data_size'483// parameter, in which case the features will be parsed and stored into484// config->input. Otherwise, 'data' can be NULL and no parsing will occur.485// Note that 'config' can be NULL too, in which case a default configuration486// is used. If 'config' is not NULL, it must outlive the WebPIDecoder object487// as some references to its fields will be used. No internal copy of 'config'488// is made.489// The return WebPIDecoder object must always be deleted calling WebPIDelete().490// Returns NULL in case of error (and config->status will then reflect491// the error condition, if available).492WEBP_NODISCARD WEBP_EXTERN WebPIDecoder* WebPIDecode(493const uint8_t* data, size_t data_size, WebPDecoderConfig* config);494495// Non-incremental version. This version decodes the full data at once, taking496// 'config' into account. Returns decoding status (which should be VP8_STATUS_OK497// if the decoding was successful). Note that 'config' cannot be NULL.498WEBP_EXTERN VP8StatusCode WebPDecode(const uint8_t* data, size_t data_size,499WebPDecoderConfig* config);500501#ifdef __cplusplus502} // extern "C"503#endif504505#endif // WEBP_WEBP_DECODE_H_506507508