1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright 2010 Google Inc. All Rights Reserved.
2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Use of this source code is governed by a BSD-style license
4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// that can be found in the COPYING file in the root of the source
5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// tree. An additional intellectual property rights grant can be found
6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// in the file PATENTS. All contributing project authors may
7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// be found in the AUTHORS file in the root of the source tree.
8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// -----------------------------------------------------------------------------
9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  Low-level API for VP8 decoder
11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Author: Skal (pascal.massimino@gmail.com)
13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#ifndef WEBP_WEBP_DECODE_VP8_H_
15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define WEBP_WEBP_DECODE_VP8_H_
16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "../webp/decode.h"
18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#if defined(__cplusplus) || defined(c_plusplus)
20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerextern "C" {
21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//------------------------------------------------------------------------------
24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Lower-level API
25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// These functions provide fine-grained control of the decoding process.
27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// The call flow should resemble:
28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   VP8Io io;
30793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   VP8InitIo(&io);
31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   io.data = data;
32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   io.data_size = size;
33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   /* customize io's functions (setup()/put()/teardown()) if needed. */
34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   VP8Decoder* dec = VP8New();
36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   bool ok = VP8Decode(dec);
37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   if (!ok) printf("Error: %s\n", VP8StatusMessage(dec));
38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   VP8Delete(dec);
39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   return ok;
40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Input / Output
42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertypedef struct VP8Io VP8Io;
43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertypedef int (*VP8IoPutHook)(const VP8Io* io);
44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertypedef int (*VP8IoSetupHook)(VP8Io* io);
45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertypedef void (*VP8IoTeardownHook)(const VP8Io* io);
46793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstruct VP8Io {
48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // set by VP8GetHeaders()
49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int width, height;         // picture dimensions, in pixels (invariable).
50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                             // These are the original, uncropped dimensions.
51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                             // The actual area passed to put() is stored
52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                             // in mb_w / mb_h fields.
53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // set before calling put()
55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int mb_y;                  // position of the current rows (in pixels)
56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int mb_w;                  // number of columns in the sample
57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int mb_h;                  // number of rows in the sample
58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  const uint8_t* y, *u, *v;  // rows to copy (in yuv420 format)
59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int y_stride;              // row stride for luma
60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int uv_stride;             // row stride for chroma
61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  void* opaque;              // user data
63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // called when fresh samples are available. Currently, samples are in
65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // YUV420 format, and can be up to width x 24 in size (depending on the
66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // in-loop filtering level, e.g.). Should return false in case of error
67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // or abort request. The actual size of the area to update is mb_w x mb_h
68793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // in size, taking cropping into account.
69793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  VP8IoPutHook put;
70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
71793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // called just before starting to decode the blocks.
72793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // Must return false in case of setup error, true otherwise. If false is
73793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // returned, teardown() will NOT be called. But if the setup succeeded
74793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // and true is returned, then teardown() will always be called afterward.
75793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  VP8IoSetupHook setup;
76793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
77793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // Called just after block decoding is finished (or when an error occurred
78793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // during put()). Is NOT called if setup() failed.
79793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  VP8IoTeardownHook teardown;
80793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
81793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // this is a recommendation for the user-side yuv->rgb converter. This flag
82793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // is set when calling setup() hook and can be overwritten by it. It then
83793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // can be taken into consideration during the put() method.
84793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int fancy_upsampling;
85793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
86793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // Input buffer.
87793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  size_t data_size;
88793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  const uint8_t* data;
89793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
90793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // If true, in-loop filtering will not be performed even if present in the
91793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // bitstream. Switching off filtering may speed up decoding at the expense
92793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // of more visible blocking. Note that output will also be non-compliant
93793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // with the VP8 specifications.
94793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int bypass_filtering;
95793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
96793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // Cropping parameters.
97793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int use_cropping;
98793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int crop_left, crop_right, crop_top, crop_bottom;
99793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
100793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // Scaling parameters.
101793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int use_scaling;
102793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  int scaled_width, scaled_height;
103793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
104793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // If non NULL, pointer to the alpha data (if present) corresponding to the
105793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // start of the current row (That is: it is pre-offset by mb_y and takes
106793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  // cropping into account).
107793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  const uint8_t* a;
108793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
109793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
110793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Internal, version-checked, entry point
111793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint VP8InitIoInternal(VP8Io* const, int);
112793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
113793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Set the custom IO function pointers and user-data. The setter for IO hooks
114793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// should be called before initiating incremental decoding. Returns true if
115793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// WebPIDecoder object is successfully modified, false otherwise.
116793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint WebPISetIOHooks(WebPIDecoder* const idec,
117793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                    VP8IoPutHook put,
118793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                    VP8IoSetupHook setup,
119793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                    VP8IoTeardownHook teardown,
120793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                    void* user_data);
121793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
122793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Main decoding object. This is an opaque structure.
123793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslertypedef struct VP8Decoder VP8Decoder;
124793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
125793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Create a new decoder object.
126793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerVP8Decoder* VP8New(void);
127793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
128793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Must be called to make sure 'io' is initialized properly.
129793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Returns false in case of version mismatch. Upon such failure, no other
130793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// decoding function should be called (VP8Decode, VP8GetHeaders, ...)
131793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic WEBP_INLINE int VP8InitIo(VP8Io* const io) {
132793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return VP8InitIoInternal(io, WEBP_DECODER_ABI_VERSION);
133793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
134793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
135793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Start decoding a new picture. Returns true if ok.
136793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint VP8GetHeaders(VP8Decoder* const dec, VP8Io* const io);
137793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
138793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Decode a picture. Will call VP8GetHeaders() if it wasn't done already.
139793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Returns false in case of error.
140793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint VP8Decode(VP8Decoder* const dec, VP8Io* const io);
141793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
142793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Return current status of the decoder:
143793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerVP8StatusCode VP8Status(VP8Decoder* const dec);
144793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
145793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// return readable string corresponding to the last status.
146793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerconst char* VP8StatusMessage(VP8Decoder* const dec);
147793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
148793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Resets the decoder in its initial state, reclaiming memory.
149793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Not a mandatory call between calls to VP8Decode().
150793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid VP8Clear(VP8Decoder* const dec);
151793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
152793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Destroy the decoder object.
153793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid VP8Delete(VP8Decoder* const dec);
154793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
155793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//------------------------------------------------------------------------------
156793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Miscellaneous VP8/VP8L bitstream probing functions.
157793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
158793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Returns true if the next 3 bytes in data contain the VP8 signature.
159793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerWEBP_EXTERN(int) VP8CheckSignature(const uint8_t* const data, size_t data_size);
160793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
161793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Validates the VP8 data-header and retrieves basic header information viz
162793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// width and height. Returns 0 in case of formatting error. *width/*height
163793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// can be passed NULL.
164793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerWEBP_EXTERN(int) VP8GetInfo(
165793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const uint8_t* data,
166793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    size_t data_size,    // data available so far
167793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    size_t chunk_size,   // total data size expected in the chunk
168793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int* const width, int* const height);
169793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
170793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Returns true if the next byte(s) in data is a VP8L signature.
171793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerWEBP_EXTERN(int) VP8LCheckSignature(const uint8_t* const data, size_t size);
172793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
173793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Validates the VP8L data-header and retrieves basic header information viz
174793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// width, height and alpha. Returns 0 in case of formatting error.
175793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// width/height/has_alpha can be passed NULL.
176793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerWEBP_EXTERN(int) VP8LGetInfo(
177793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const uint8_t* data, size_t data_size,  // data available so far
178793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int* const width, int* const height, int* const has_alpha);
179793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
180793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#if defined(__cplusplus) || defined(c_plusplus)
181793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}    // extern "C"
182793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
183793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
184793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif  /* WEBP_WEBP_DECODE_VP8_H_ */
185