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