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