blob: 8423121d11ec7b51e19a45da905b743660d0803a [file] [log] [blame]
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -08001/*
2 * Copyright 2012 The LibYuv Project Authors. All rights reserved.
3 *
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
Hangyu Kuangf047e7c2016-07-06 14:21:45 -07007 * in the file PATENTS. All contributing project authors may
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -08008 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef INCLUDE_LIBYUV_MJPEG_DECODER_H_ // NOLINT
12#define INCLUDE_LIBYUV_MJPEG_DECODER_H_
13
14#include "libyuv/basic_types.h"
15
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070016#ifdef __cplusplus
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080017// NOTE: For a simplified public API use convert.h MJPGToI420().
18
19struct jpeg_common_struct;
20struct jpeg_decompress_struct;
21struct jpeg_source_mgr;
22
23namespace libyuv {
24
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070025#ifdef __cplusplus
26extern "C" {
27#endif
28
29LIBYUV_BOOL ValidateJpeg(const uint8* sample, size_t sample_size);
30
31#ifdef __cplusplus
32} // extern "C"
33#endif
34
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080035static const uint32 kUnknownDataSize = 0xFFFFFFFF;
36
37enum JpegSubsamplingType {
38 kJpegYuv420,
39 kJpegYuv422,
40 kJpegYuv411,
41 kJpegYuv444,
42 kJpegYuv400,
43 kJpegUnknown
44};
45
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070046struct Buffer {
47 const uint8* data;
48 int len;
49};
50
51struct BufferVector {
52 Buffer* buffers;
53 int len;
54 int pos;
55};
56
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080057struct SetJmpErrorMgr;
58
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
63// MJPEG frames.
64//
65// See http://tools.ietf.org/html/rfc2435
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070066class LIBYUV_API MJpegDecoder {
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080067 public:
68 typedef void (*CallbackFunction)(void* opaque,
69 const uint8* const* data,
70 const int* strides,
71 int rows);
72
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;
79
80 MJpegDecoder();
81 ~MJpegDecoder();
82
83 // Loads a new frame, reads its headers, and determines the uncompressed
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070084 // image format.
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.
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080088 // src_len is the size of the compressed mjpeg frame in bytes.
Hangyu Kuangf047e7c2016-07-06 14:21:45 -070089 LIBYUV_BOOL LoadFrame(const uint8* src, size_t src_len);
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -080090
91 // Returns width of the last loaded frame in pixels.
92 int GetWidth();
93
94 // Returns height of the last loaded frame in pixels.
95 int GetHeight();
96
97 // Returns format of the last loaded frame. The return value is one of the
98 // kColorSpace* constants.
99 int GetColorSpace();
100
101 // Number of color components in the color space.
102 int GetNumComponents();
103
104 // Sample factors of the n-th component.
105 int GetHorizSampFactor(int component);
106
107 int GetVertSampFactor(int component);
108
109 int GetHorizSubSampFactor(int component);
110
111 int GetVertSubSampFactor(int component);
112
113 // Public for testability.
114 int GetImageScanlinesPerImcuRow();
115
116 // Public for testability.
117 int GetComponentScanlinesPerImcuRow(int component);
118
119 // Width of a component in bytes.
120 int GetComponentWidth(int component);
121
122 // Height of a component.
123 int GetComponentHeight(int component);
124
125 // Width of a component in bytes with padding for DCTSIZE. Public for testing.
126 int GetComponentStride(int component);
127
128 // Size of a component in bytes.
129 int GetComponentSize(int component);
130
131 // Call this after LoadFrame() if you decide you don't want to decode it
132 // after all.
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700133 LIBYUV_BOOL UnloadFrame();
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800134
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.
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700142 LIBYUV_BOOL DecodeToBuffers(uint8** planes, int dst_width, int dst_height);
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800143
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
146 // image scanlines.
147 // TODO(fbarchard): Add dst_x, dst_y to allow specific rect to be decoded.
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700148 LIBYUV_BOOL DecodeToCallback(CallbackFunction fn, void* opaque,
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800149 int dst_width, int dst_height);
150
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);
154
155 private:
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800156 void AllocOutputBuffers(int num_outbufs);
157 void DestroyOutputBuffers();
158
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700159 LIBYUV_BOOL StartDecode();
160 LIBYUV_BOOL FinishDecode();
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800161
162 void SetScanlinePointers(uint8** data);
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700163 LIBYUV_BOOL DecodeImcuRow();
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800164
165 int GetComponentScanlinePadding(int component);
166
167 // A buffer holding the input data for a frame.
168 Buffer buf_;
169 BufferVector buf_vec_;
170
171 jpeg_decompress_struct* decompress_struct_;
172 jpeg_source_mgr* source_mgr_;
173 SetJmpErrorMgr* error_mgr_;
174
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700175 // LIBYUV_TRUE iff at least one component has scanline padding. (i.e.,
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800176 // GetComponentScanlinePadding() != 0.)
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700177 LIBYUV_BOOL has_scanline_padding_;
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800178
179 // Temporaries used to point to scanline outputs.
180 int num_outbufs_; // Outermost size of all arrays below.
181 uint8*** scanlines_;
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.
185 uint8** databuf_;
186 int* databuf_strides_;
187};
188
189} // namespace libyuv
190
Hangyu Kuangf047e7c2016-07-06 14:21:45 -0700191#endif // __cplusplus
Hendrik Dahlkamp33cfdeb2013-01-23 18:27:37 -0800192#endif // INCLUDE_LIBYUV_MJPEG_DECODER_H_ NOLINT