2 * Copyright 2012 The LibYuv Project Authors. All rights reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 #ifndef INCLUDE_LIBYUV_MJPEG_DECODER_H_ // NOLINT
12 #define INCLUDE_LIBYUV_MJPEG_DECODER_H_
14 #include "libyuv/basic_types.h"
17 // NOTE: For a simplified public API use convert.h MJPGToI420().
19 struct jpeg_common_struct;
20 struct jpeg_decompress_struct;
21 struct jpeg_source_mgr;
29 LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size);
35 static const uint32 kUnknownDataSize = 0xFFFFFFFF;
37 enum JpegSubsamplingType {
57 struct SetJmpErrorMgr;
59 // MJPEG ("Motion JPEG") is a pseudo-standard video codec where the frames are
60 // simply independent JPEG images with a fixed huffman table (which is omitted).
61 // It is rarely used in video transmission, but is common as a camera capture
62 // format, especially in Logitech devices. This class implements a decoder for
65 // See http://tools.ietf.org/html/rfc2435
66 class LIBYUV_API MJpegDecoder {
68 typedef void (*CallbackFunction)(void* opaque,
69 const uint8* const* data,
73 static const int kColorSpaceUnknown;
74 static const int kColorSpaceGrayscale;
75 static const int kColorSpaceRgb;
76 static const int kColorSpaceYCbCr;
77 static const int kColorSpaceCMYK;
78 static const int kColorSpaceYCCK;
83 // Loads a new frame, reads its headers, and determines the uncompressed
85 // Returns LIBYUV_TRUE if image looks valid and format is supported.
86 // If return value is LIBYUV_TRUE, then the values for all the following
87 // getters are populated.
88 // src_len is the size of the compressed mjpeg frame in bytes.
89 LIBYUV_BOOL LoadFrame(const uint8* src, size_t src_len);
91 // Returns width of the last loaded frame in pixels.
94 // Returns height of the last loaded frame in pixels.
97 // Returns format of the last loaded frame. The return value is one of the
98 // kColorSpace* constants.
101 // Number of color components in the color space.
102 int GetNumComponents();
104 // Sample factors of the n-th component.
105 int GetHorizSampFactor(int component);
107 int GetVertSampFactor(int component);
109 int GetHorizSubSampFactor(int component);
111 int GetVertSubSampFactor(int component);
113 // Public for testability.
114 int GetImageScanlinesPerImcuRow();
116 // Public for testability.
117 int GetComponentScanlinesPerImcuRow(int component);
119 // Width of a component in bytes.
120 int GetComponentWidth(int component);
122 // Height of a component.
123 int GetComponentHeight(int component);
125 // Width of a component in bytes with padding for DCTSIZE. Public for testing.
126 int GetComponentStride(int component);
128 // Size of a component in bytes.
129 int GetComponentSize(int component);
131 // Call this after LoadFrame() if you decide you don't want to decode it
133 LIBYUV_BOOL UnloadFrame();
135 // Decodes the entire image into a one-buffer-per-color-component format.
136 // dst_width must match exactly. dst_height must be <= to image height; if
137 // less, the image is cropped. "planes" must have size equal to at least
138 // GetNumComponents() and they must point to non-overlapping buffers of size
139 // at least GetComponentSize(i). The pointers in planes are incremented
140 // to point to after the end of the written data.
141 // TODO(fbarchard): Add dst_x, dst_y to allow specific rect to be decoded.
142 LIBYUV_BOOL DecodeToBuffers(uint8** planes, int dst_width, int dst_height);
144 // Decodes the entire image and passes the data via repeated calls to a
145 // callback function. Each call will get the data for a whole number of
147 // TODO(fbarchard): Add dst_x, dst_y to allow specific rect to be decoded.
148 LIBYUV_BOOL DecodeToCallback(CallbackFunction fn, void* opaque,
149 int dst_width, int dst_height);
151 // The helper function which recognizes the jpeg sub-sampling type.
152 static JpegSubsamplingType JpegSubsamplingTypeHelper(
153 int* subsample_x, int* subsample_y, int number_of_components);
156 void AllocOutputBuffers(int num_outbufs);
157 void DestroyOutputBuffers();
159 LIBYUV_BOOL StartDecode();
160 LIBYUV_BOOL FinishDecode();
162 void SetScanlinePointers(uint8** data);
163 LIBYUV_BOOL DecodeImcuRow();
165 int GetComponentScanlinePadding(int component);
167 // A buffer holding the input data for a frame.
169 BufferVector buf_vec_;
171 jpeg_decompress_struct* decompress_struct_;
172 jpeg_source_mgr* source_mgr_;
173 SetJmpErrorMgr* error_mgr_;
175 // LIBYUV_TRUE iff at least one component has scanline padding. (i.e.,
176 // GetComponentScanlinePadding() != 0.)
177 LIBYUV_BOOL has_scanline_padding_;
179 // Temporaries used to point to scanline outputs.
180 int num_outbufs_; // Outermost size of all arrays below.
182 int* scanlines_sizes_;
183 // Temporary buffer used for decoding when we can't decode directly to the
184 // output buffers. Large enough for just one iMCU row.
186 int* databuf_strides_;
189 } // namespace libyuv
191 #endif // __cplusplus
192 #endif // INCLUDE_LIBYUV_MJPEG_DECODER_H_ NOLINT