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