1a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Copyright 2010 Google Inc. All Rights Reserved. 2a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 3a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// This code is licensed under the same terms as WebM: 4a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Software License Agreement: http://www.webmproject.org/license/software/ 5a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Additional IP Rights Grant: http://www.webmproject.org/license/additional/ 6a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// ----------------------------------------------------------------------------- 7a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 8a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Low-level API for VP8 decoder 9a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 10a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Author: Skal (pascal.massimino@gmail.com) 11a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 12a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#ifndef WEBP_WEBP_DECODE_VP8_H_ 13a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#define WEBP_WEBP_DECODE_VP8_H_ 14a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 15a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#include "webp/decode.h" 16a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 17a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#if defined(__cplusplus) || defined(c_plusplus) 18a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraextern "C" { 19a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#endif 20a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 21a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 22a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Lower-level API 23a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 24a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// These functions provide fine-grained control of the decoding process. 25a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// The call flow should resemble: 26a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 27a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8Io io; 28a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8InitIo(&io); 29a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// io.data = data; 30a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// io.data_size = size; 31a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// /* customize io's functions (setup()/put()/teardown()) if needed. */ 32a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// 33a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8Decoder* dec = VP8New(); 34a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// bool ok = VP8Decode(dec); 35a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// if (!ok) printf("Error: %s\n", VP8StatusMessage(dec)); 36a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// VP8Delete(dec); 37a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// return ok; 38a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 39a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Input / Output 40a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroratypedef struct VP8Io VP8Io; 41a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroratypedef int (*VP8IoPutHook)(const VP8Io* io); 42a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroratypedef int (*VP8IoSetupHook)(VP8Io* io); 43a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroratypedef void (*VP8IoTeardownHook)(const VP8Io* io); 44a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 45a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastruct VP8Io { 46a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // set by VP8GetHeaders() 47a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int width, height; // picture dimensions, in pixels (invariable). 48a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // These are the original, uncropped dimensions. 49a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // The actual area passed to put() is stored 50a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // in mb_w / mb_h fields. 51a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 52a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // set before calling put() 53a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int mb_y; // position of the current rows (in pixels) 54a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int mb_w; // number of columns in the sample 55a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int mb_h; // number of rows in the sample 56a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* y, *u, *v; // rows to copy (in yuv420 format) 57a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int y_stride; // row stride for luma 58a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int uv_stride; // row stride for chroma 59a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 60a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora void* opaque; // user data 61a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 62a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // called when fresh samples are available. Currently, samples are in 63a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // YUV420 format, and can be up to width x 24 in size (depending on the 64a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // in-loop filtering level, e.g.). Should return false in case of error 65a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // or abort request. The actual size of the area to update is mb_w x mb_h 66a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // in size, taking cropping into account. 67a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8IoPutHook put; 68a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 69a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // called just before starting to decode the blocks. 70a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Must return false in case of setup error, true otherwise. If false is 71a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // returned, teardown() will NOT be called. But if the setup succeeded 72a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // and true is returned, then teardown() will always be called afterward. 73a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8IoSetupHook setup; 74a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 75a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Called just after block decoding is finished (or when an error occurred 76a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // during put()). Is NOT called if setup() failed. 77a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8IoTeardownHook teardown; 78a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 79a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // this is a recommendation for the user-side yuv->rgb converter. This flag 80a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // is set when calling setup() hook and can be overwritten by it. It then 81a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // can be taken into consideration during the put() method. 82a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int fancy_upsampling; 83a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 84a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Input buffer. 85a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t data_size; 86a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data; 87a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 88a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // If true, in-loop filtering will not be performed even if present in the 89a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // bitstream. Switching off filtering may speed up decoding at the expense 90a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // of more visible blocking. Note that output will also be non-compliant 91a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // with the VP8 specifications. 92a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int bypass_filtering; 93a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 94a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Cropping parameters. 95a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int use_cropping; 96a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int crop_left, crop_right, crop_top, crop_bottom; 97a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 98a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // Scaling parameters. 99a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int use_scaling; 100a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int scaled_width, scaled_height; 101a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 102a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // If non NULL, pointer to the alpha data (if present) corresponding to the 103a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // start of the current row (That is: it is pre-offset by mb_y and takes 104a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora // cropping into account). 105a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* a; 106a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora}; 107a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 108a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Internal, version-checked, entry point 109a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint VP8InitIoInternal(VP8Io* const, int); 110a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 111a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Set the custom IO function pointers and user-data. The setter for IO hooks 112a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// should be called before initiating incremental decoding. Returns true if 113a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// WebPIDecoder object is successfully modified, false otherwise. 114a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint WebPISetIOHooks(WebPIDecoder* const idec, 115a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8IoPutHook put, 116a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8IoSetupHook setup, 117a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora VP8IoTeardownHook teardown, 118a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora void* user_data); 119a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 120a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Main decoding object. This is an opaque structure. 121a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroratypedef struct VP8Decoder VP8Decoder; 122a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 123a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Create a new decoder object. 124a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraVP8Decoder* VP8New(void); 125a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 126a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Must be called to make sure 'io' is initialized properly. 127a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of version mismatch. Upon such failure, no other 128a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// decoding function should be called (VP8Decode, VP8GetHeaders, ...) 129a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arorastatic WEBP_INLINE int VP8InitIo(VP8Io* const io) { 130a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora return VP8InitIoInternal(io, WEBP_DECODER_ABI_VERSION); 131a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} 132a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 133a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Start decoding a new picture. Returns true if ok. 134a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io); 135a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 136a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Decode a picture. Will call VP8GetHeaders() if it wasn't done already. 137a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns false in case of error. 138a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraint VP8Decode(VP8Decoder* const dec, VP8Io* const io); 139a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 140a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Return current status of the decoder: 141a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraVP8StatusCode VP8Status(VP8Decoder* const dec); 142a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 143a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// return readable string corresponding to the last status. 144a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroraconst char* VP8StatusMessage(VP8Decoder* const dec); 145a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 146a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Resets the decoder in its initial state, reclaiming memory. 147a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Not a mandatory call between calls to VP8Decode(). 148a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroravoid VP8Clear(VP8Decoder* const dec); 149a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 150a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Destroy the decoder object. 151a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Aroravoid VP8Delete(VP8Decoder* const dec); 152a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 153a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora//------------------------------------------------------------------------------ 154a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Miscellaneous VP8/VP8L bitstream probing functions. 155a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 156a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns true if the next 3 bytes in data contain the VP8 signature. 157a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) VP8CheckSignature(const uint8_t* const data, size_t data_size); 158a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 159a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Validates the VP8 data-header and retrieves basic header information viz 160a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// width and height. Returns 0 in case of formatting error. *width/*height 161a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// can be passed NULL. 162a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) VP8GetInfo( 163a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, 164a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t data_size, // data available so far 165a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora size_t chunk_size, // total data size expected in the chunk 166a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* const width, int* const height); 167a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 168a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Returns true if the next byte(s) in data is a VP8L signature. 169a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) VP8LCheckSignature(const uint8_t* const data, size_t size); 170a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 171a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// Validates the VP8L data-header and retrieves basic header information viz 172a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// width, height and alpha. Returns 0 in case of formatting error. 173a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora// width/height/has_alpha can be passed NULL. 174a2415724fb3466168b2af5b08bd94ba732c0e753Vikas AroraWEBP_EXTERN(int) VP8LGetInfo( 175a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora const uint8_t* data, size_t data_size, // data available so far 176a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora int* const width, int* const height, int* const has_alpha); 177a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 178a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#if defined(__cplusplus) || defined(c_plusplus) 179a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora} // extern "C" 180a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#endif 181a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora 182a2415724fb3466168b2af5b08bd94ba732c0e753Vikas Arora#endif /* WEBP_WEBP_DECODE_VP8_H_ */ 183