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