1*b2055c35SXin Li // Copyright 2011 Google Inc. All Rights Reserved. 2*b2055c35SXin Li // 3*b2055c35SXin Li // Use of this source code is governed by a BSD-style license 4*b2055c35SXin Li // that can be found in the COPYING file in the root of the source 5*b2055c35SXin Li // tree. An additional intellectual property rights grant can be found 6*b2055c35SXin Li // in the file PATENTS. All contributing project authors may 7*b2055c35SXin Li // be found in the AUTHORS file in the root of the source tree. 8*b2055c35SXin Li // ----------------------------------------------------------------------------- 9*b2055c35SXin Li // 10*b2055c35SXin Li // Internal header: WebP decoding parameters and custom IO on buffer 11*b2055c35SXin Li // 12*b2055c35SXin Li // Author: [email protected] (Somnath Banerjee) 13*b2055c35SXin Li 14*b2055c35SXin Li #ifndef WEBP_DEC_WEBPI_DEC_H_ 15*b2055c35SXin Li #define WEBP_DEC_WEBPI_DEC_H_ 16*b2055c35SXin Li 17*b2055c35SXin Li #ifdef __cplusplus 18*b2055c35SXin Li extern "C" { 19*b2055c35SXin Li #endif 20*b2055c35SXin Li 21*b2055c35SXin Li #include "src/utils/rescaler_utils.h" 22*b2055c35SXin Li #include "src/dec/vp8_dec.h" 23*b2055c35SXin Li #include "src/webp/decode.h" 24*b2055c35SXin Li 25*b2055c35SXin Li //------------------------------------------------------------------------------ 26*b2055c35SXin Li // WebPDecParams: Decoding output parameters. Transient internal object. 27*b2055c35SXin Li 28*b2055c35SXin Li typedef struct WebPDecParams WebPDecParams; 29*b2055c35SXin Li typedef int (*OutputFunc)(const VP8Io* const io, WebPDecParams* const p); 30*b2055c35SXin Li typedef int (*OutputAlphaFunc)(const VP8Io* const io, WebPDecParams* const p, 31*b2055c35SXin Li int expected_num_out_lines); 32*b2055c35SXin Li typedef int (*OutputRowFunc)(WebPDecParams* const p, int y_pos, 33*b2055c35SXin Li int max_out_lines); 34*b2055c35SXin Li 35*b2055c35SXin Li struct WebPDecParams { 36*b2055c35SXin Li WebPDecBuffer* output; // output buffer. 37*b2055c35SXin Li uint8_t* tmp_y, *tmp_u, *tmp_v; // cache for the fancy upsampler 38*b2055c35SXin Li // or used for tmp rescaling 39*b2055c35SXin Li 40*b2055c35SXin Li int last_y; // coordinate of the line that was last output 41*b2055c35SXin Li const WebPDecoderOptions* options; // if not NULL, use alt decoding features 42*b2055c35SXin Li 43*b2055c35SXin Li WebPRescaler* scaler_y, *scaler_u, *scaler_v, *scaler_a; // rescalers 44*b2055c35SXin Li void* memory; // overall scratch memory for the output work. 45*b2055c35SXin Li 46*b2055c35SXin Li OutputFunc emit; // output RGB or YUV samples 47*b2055c35SXin Li OutputAlphaFunc emit_alpha; // output alpha channel 48*b2055c35SXin Li OutputRowFunc emit_alpha_row; // output one line of rescaled alpha values 49*b2055c35SXin Li }; 50*b2055c35SXin Li 51*b2055c35SXin Li // Should be called first, before any use of the WebPDecParams object. 52*b2055c35SXin Li void WebPResetDecParams(WebPDecParams* const params); 53*b2055c35SXin Li 54*b2055c35SXin Li //------------------------------------------------------------------------------ 55*b2055c35SXin Li // Header parsing helpers 56*b2055c35SXin Li 57*b2055c35SXin Li // Structure storing a description of the RIFF headers. 58*b2055c35SXin Li typedef struct { 59*b2055c35SXin Li const uint8_t* data; // input buffer 60*b2055c35SXin Li size_t data_size; // input buffer size 61*b2055c35SXin Li int have_all_data; // true if all data is known to be available 62*b2055c35SXin Li size_t offset; // offset to main data chunk (VP8 or VP8L) 63*b2055c35SXin Li const uint8_t* alpha_data; // points to alpha chunk (if present) 64*b2055c35SXin Li size_t alpha_data_size; // alpha chunk size 65*b2055c35SXin Li size_t compressed_size; // VP8/VP8L compressed data size 66*b2055c35SXin Li size_t riff_size; // size of the riff payload (or 0 if absent) 67*b2055c35SXin Li int is_lossless; // true if a VP8L chunk is present 68*b2055c35SXin Li } WebPHeaderStructure; 69*b2055c35SXin Li 70*b2055c35SXin Li // Skips over all valid chunks prior to the first VP8/VP8L frame header. 71*b2055c35SXin Li // Returns: VP8_STATUS_OK, VP8_STATUS_BITSTREAM_ERROR (invalid header/chunk), 72*b2055c35SXin Li // VP8_STATUS_NOT_ENOUGH_DATA (partial input) or VP8_STATUS_UNSUPPORTED_FEATURE 73*b2055c35SXin Li // in the case of non-decodable features (animation for instance). 74*b2055c35SXin Li // In 'headers', compressed_size, offset, alpha_data, alpha_size, and lossless 75*b2055c35SXin Li // fields are updated appropriately upon success. 76*b2055c35SXin Li VP8StatusCode WebPParseHeaders(WebPHeaderStructure* const headers); 77*b2055c35SXin Li 78*b2055c35SXin Li //------------------------------------------------------------------------------ 79*b2055c35SXin Li // Misc utils 80*b2055c35SXin Li 81*b2055c35SXin Li // Returns true if crop dimensions are within image bounds. 82*b2055c35SXin Li int WebPCheckCropDimensions(int image_width, int image_height, 83*b2055c35SXin Li int x, int y, int w, int h); 84*b2055c35SXin Li 85*b2055c35SXin Li // Initializes VP8Io with custom setup, io and teardown functions. The default 86*b2055c35SXin Li // hooks will use the supplied 'params' as io->opaque handle. 87*b2055c35SXin Li void WebPInitCustomIo(WebPDecParams* const params, VP8Io* const io); 88*b2055c35SXin Li 89*b2055c35SXin Li // Setup crop_xxx fields, mb_w and mb_h in io. 'src_colorspace' refers 90*b2055c35SXin Li // to the *compressed* format, not the output one. 91*b2055c35SXin Li WEBP_NODISCARD int WebPIoInitFromOptions( 92*b2055c35SXin Li const WebPDecoderOptions* const options, VP8Io* const io, 93*b2055c35SXin Li WEBP_CSP_MODE src_colorspace); 94*b2055c35SXin Li 95*b2055c35SXin Li //------------------------------------------------------------------------------ 96*b2055c35SXin Li // Internal functions regarding WebPDecBuffer memory (in buffer.c). 97*b2055c35SXin Li // Don't really need to be externally visible for now. 98*b2055c35SXin Li 99*b2055c35SXin Li // Prepare 'buffer' with the requested initial dimensions width/height. 100*b2055c35SXin Li // If no external storage is supplied, initializes buffer by allocating output 101*b2055c35SXin Li // memory and setting up the stride information. Validate the parameters. Return 102*b2055c35SXin Li // an error code in case of problem (no memory, or invalid stride / size / 103*b2055c35SXin Li // dimension / etc.). If *options is not NULL, also verify that the options' 104*b2055c35SXin Li // parameters are valid and apply them to the width/height dimensions of the 105*b2055c35SXin Li // output buffer. This takes cropping / scaling / rotation into account. 106*b2055c35SXin Li // Also incorporates the options->flip flag to flip the buffer parameters if 107*b2055c35SXin Li // needed. 108*b2055c35SXin Li VP8StatusCode WebPAllocateDecBuffer(int width, int height, 109*b2055c35SXin Li const WebPDecoderOptions* const options, 110*b2055c35SXin Li WebPDecBuffer* const buffer); 111*b2055c35SXin Li 112*b2055c35SXin Li // Flip buffer vertically by negating the various strides. 113*b2055c35SXin Li VP8StatusCode WebPFlipBuffer(WebPDecBuffer* const buffer); 114*b2055c35SXin Li 115*b2055c35SXin Li // Copy 'src' into 'dst' buffer, making sure 'dst' is not marked as owner of the 116*b2055c35SXin Li // memory (still held by 'src'). No pixels are copied. 117*b2055c35SXin Li void WebPCopyDecBuffer(const WebPDecBuffer* const src, 118*b2055c35SXin Li WebPDecBuffer* const dst); 119*b2055c35SXin Li 120*b2055c35SXin Li // Copy and transfer ownership from src to dst (beware of parameter order!) 121*b2055c35SXin Li void WebPGrabDecBuffer(WebPDecBuffer* const src, WebPDecBuffer* const dst); 122*b2055c35SXin Li 123*b2055c35SXin Li // Copy pixels from 'src' into a *preallocated* 'dst' buffer. Returns 124*b2055c35SXin Li // VP8_STATUS_INVALID_PARAM if the 'dst' is not set up correctly for the copy. 125*b2055c35SXin Li VP8StatusCode WebPCopyDecBufferPixels(const WebPDecBuffer* const src, 126*b2055c35SXin Li WebPDecBuffer* const dst); 127*b2055c35SXin Li 128*b2055c35SXin Li // Returns true if decoding will be slow with the current configuration 129*b2055c35SXin Li // and bitstream features. 130*b2055c35SXin Li int WebPAvoidSlowMemory(const WebPDecBuffer* const output, 131*b2055c35SXin Li const WebPBitstreamFeatures* const features); 132*b2055c35SXin Li 133*b2055c35SXin Li //------------------------------------------------------------------------------ 134*b2055c35SXin Li 135*b2055c35SXin Li #ifdef __cplusplus 136*b2055c35SXin Li } // extern "C" 137*b2055c35SXin Li #endif 138*b2055c35SXin Li 139*b2055c35SXin Li #endif // WEBP_DEC_WEBPI_DEC_H_ 140