libhwjpeg: resolve compilation errors
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos.git] / include / exynos-hwjpeg.h
1 /*
2 * Copyright Samsung Electronics Co.,LTD.
3 * Copyright (C) 2015 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #ifndef __EXYNOS_HWJPEG_H__
19 #define __EXYNOS_HWJPEG_H__
20
21 #include <cstddef> // size_t
22 /*
23 * exynos-hwjpeg.h does not include videodev2.h because Exynos HAL code may
24 * define its version of videodev2.h that may differ from <linux/videodev2.h>
25 * of the current Linux version.
26 * To prevent conflict different versions of videodev2.h, this header file does
27 * not include videodev2.h even though it depends on the data types defined in
28 * videodev2.h.
29 * Therefore, the source files that include this header file, they should
30 * include their proper version of videodev2.h.
31 */
32 #ifndef VIDEO_MAX_PLANES
33 #error 'linux/videodev2.h' should be included before 'exynos-hwjpeg.h'
34 #endif
35
36 #if VIDEO_MAX_PLANES < 6
37 #error VIDEO_MAX_PLANES should not be smaller than 6
38 #endif
39
40 // Exynos JPEG specific device capabilities
41 // Defined in the driver. Not in videodev2.h
42 #define V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION 0x0100
43 #define V4L2_CAP_EXYNOS_JPEG_B2B_COMPRESSION 0x0200
44 #define V4L2_CAP_EXYNOS_JPEG_HWFC 0x0400
45 #define V4L2_CAP_EXYNOS_JPEG_HWFC_EMBEDDED 0x0800
46 #define V4L2_CAP_EXYNOS_JPEG_MAX_STREAMSIZE 0x1000
47 #define V4L2_CAP_EXYNOS_JPEG_NO_STREAMBASE_ALIGN 0x2000
48 #define V4L2_CAP_EXYNOS_JPEG_NO_IMAGEBASE_ALIGN 0x4000
49 #define V4L2_CAP_EXYNOS_JPEG_NO_BUFFER_OVERRUN 0x8000
50 #define V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_FROM_SOS 0x10000
51 #define V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_CROP 0x20000
52 #define V4L2_CAP_EXYNOS_JPEG_DOWNSCALING 0x40000
53 // EXYNOS HWJPEG specific auxiliary option flags
54 // The flags are common to all derived classes of CHWJpegCompressor
55 // but if a derived class does not support for a specified flag,
56 // it is discarded and ignored silently.
57 #define EXYNOS_HWJPEG_AUXOPT_ENABLE_HWFC (1 << 4)
58 #define EXYNOS_HWJPEG_AUXOPT_SRC_NOCACHECLEAN (1 << 8)
59 #define EXYNOS_HWJPEG_AUXOPT_DST_NOCACHECLEAN (1 << 9)
60
61 /*
62 * CHWJpegBase - The base class of JPEG compression and decompression
63 *
64 * This class contains the following information:
65 * - The open file descriptor of the device node
66 * - The flags to describe the state of the operations
67 * - The falgs to indicate the capability of HWJPEG
68 * This class also defines the getters and the setters of flags.
69 */
70 class CHWJpegBase {
71 int m_iFD;
72 unsigned int m_uiDeviceCaps;
73 /*
74 * Auxiliary option flags are implementation specific to derived classes
75 * of CHWJpegCompressor. Even though the flags are common to all derived
76 * classes, they should identify their supporting flags and ignore the
77 * other flags.
78 * NOTE that the flag is volatile. That means the flags stored in
79 * m_uiAuxFlags is cleared when it is read by GetAuxFlags().
80 *
81 */
82 unsigned int m_uiAuxFlags;
83 protected:
84 CHWJpegBase(const char *path);
85 virtual ~CHWJpegBase();
86 int GetDeviceFD() { return m_iFD; }
87 void SetDeviceCapabilities(unsigned int cap) { m_uiDeviceCaps = cap; }
88 unsigned int GetAuxFlags() { return m_uiAuxFlags; }
89 public:
90 unsigned int GetDeviceCapabilities() { return m_uiDeviceCaps; }
91 bool IsDeviceCapability(unsigned int cap_flags) { return (m_uiDeviceCaps & cap_flags) == cap_flags; }
92
93 /*
94 * Okay - Test if the object is correctly initialized
95 * @return: true if this object is correctly initialized and ready to use.
96 * false, otherwise.
97 *
98 * A user that creates this object *must* test if the object is successfully
99 * created because some initialization in the constructor may fail.
100 */
101 bool Okay() { return m_iFD >= 0; }
102 operator bool() { return Okay(); }
103
104 /*
105 * SetAuxFlags - Configure HWJPEG auxiliary options
106 * @auxflags: a set of bit flags. The flags are prefixed by EXYNOS_HWJPEG_AUXOPT
107 * and defined separately in this file.
108 *
109 * SetAuxFlags() is *not* thread-safe. Racing calls to SetAuxFlags() between
110 * multiple threads may cause the flags inconsistent. Moreover, Racing between
111 * SetAuxFlags() call by the users and reading the flags by the libhwjpeg
112 * causes incomplete hwjpeg configuration.
113 */
114 void SetAuxFlags(unsigned int auxflags);
115
116 /*
117 * ClearAuxFlags - Removes HWJPEG auxiliary options
118 * @auxflags: a set of bit flags to clear. The flags are prefixed by EXYNOS_HWJPEG_AUXOPT
119 * and defined separately in this file.
120 *
121 * ClearAuxFlags() is *not* thread-safe. Racing calls to ClearAuxFlags() between
122 * multiple threads may cause the flags inconsistent. Moreover, Racing between
123 * ClearAuxFlags() call by the users and reading the flags by the libhwjpeg
124 * causes incomplete hwjpeg configuration.
125 */
126 void ClearAuxFlags(unsigned int auxflags);
127 };
128
129 /*
130 * CHWJpegCompressor - The abstract class of HW JPEG compression accelerator
131 *
132 * This class is *not* thread-safe. If an instance of this class is handled by
133 * multiple threads, the users of the instance should care about the thread
134 * synchronization.
135 *
136 * CHWJpegCompressor is able to check if the number of configured image buffers
137 * are sufficient. It depends on the configured image format. Therefore, it is
138 * better to configure the image format and the size prior to configure image
139 * buffers. If back-to-back compression is required, it is *important* to know
140 * how many buffers are required for an image. Thus it is *strongly* recommented
141 * to configure image format and sizes before configuring image buffers.
142 */
143 class CHWJpegCompressor : public CHWJpegBase {
144 size_t m_nLastStreamSize;
145 size_t m_nLastThumbStreamSize;
146 protected:
147 void SetStreamSize(size_t main_size, size_t secondary_size = 0) {
148 m_nLastStreamSize = main_size;
149 m_nLastThumbStreamSize = secondary_size;
150 }
151
152 ssize_t GetStreamSize(size_t *secondary_size) {
153 if (secondary_size)
154 *secondary_size = m_nLastThumbStreamSize;
155 return static_cast<ssize_t>(m_nLastStreamSize);
156 }
157 public:
158 CHWJpegCompressor(const char *path): CHWJpegBase(path), m_nLastStreamSize(0), m_nLastThumbStreamSize(0) { }
159
160 /*
161 * SetImageFormat - Configure uncompressed image format, width and height
162 * @v4l2_fmt[in] : Image pixel format defined in <linux/videodev2.h>
163 * @width[in] : Width of the primary uncompressed image in the number of pixels
164 * @height[in] : Height of the primary uncompressed image in the number of pixels
165 * @sec_width[in] : Width of the secondary uncompressed image in the number of pixels (optional)
166 * @sec_height[in] : Height of the secondary uncompressed image in the number of pixels (optional)
167 * @return : true if configuration of image pixel format and size is successful.
168 * false, otherwise.
169 *
170 * The primary and the secondary image format should be same. There is no way
171 * to configure different image formats for them.
172 */
173 virtual bool SetImageFormat(unsigned int v4l2_fmt, unsigned int width, unsigned int height,
174 unsigned int sec_width = 0, unsigned int sec_height = 0) = 0;
175
176 /*
177 * GetImageBufferSizes - Ask the required buffer sizes for the given image format
178 * @buf_sizes[out] : array of buffer sizes.
179 * @num_buffers[in/out]: number of elements in @buf_sizes intially.
180 * number assigned buffer sizes to @buf_sizes on return.
181 * @return: true if the @buf_sizes and @num_buffers are initialized successfully.
182 * false, otherwise
183 *
184 * It should be called after SetImageFormat() SetImageSize() are called. Otherwise,
185 * the returned buffer sizes are not correct.
186 */
187 virtual bool GetImageBufferSizes(size_t buf_sizes[], unsigned int *num_bufffers) = 0;
188 /*
189 * SetChromaSampFactor - Configure the chroma subsampling factor for JPEG stream
190 * @horizontal[in] : horizontal chroma subsampling factor
191 * @vertical[in] : vertical chroma subsampling factor
192 * @return: true if chroma subsamping factors are configured successfully,
193 * false if the factors are invalid.
194 */
195 virtual bool SetChromaSampFactor(unsigned int horizontal, unsigned int vertical) = 0;
196 /*
197 * SetQuality - Configure quality factor for JPEG compression
198 * @quality_factor[in] : JPEG compression quality factor between 1 and 100 for the primary image
199 * @quality_factor2[in] : JPEG compression quality factor for the secondary image (optional)
200 * @return: true if quality factors are configured successfully.
201 * false, otherwise.
202 */
203 virtual bool SetQuality(unsigned int quality_factor, unsigned int quality_factor2 = 0) = 0;
204 /*
205 * SetQuality - Configure quantization tables for JPEG compression
206 * @qtable[in] : The 128 element array of quantization tables that contributes to JPEG
207 * compression. The first 64 elements are the quantization tables of the luma
208 * component. The other 64 elements are the quantization table of the chroma
209 * components. All the quantizers in the tables should be specified in the
210 * zig-zag scan order.
211 * @return: true if the given quantization tables are configured successfully.
212 * false, otherwise.
213 */
214 virtual bool SetQuality(const unsigned char qtable[] __unused) { return false; };
215 /*
216 * SetImageBuffer - Configure the uncompressed primary image buffers (userptr)
217 * @buffers[in] : addresses of the buffers
218 * @len_buffers[in] : sizes of the buffers
219 * @num_buffers[in] : the number of elements of @buffers and @len_buffers
220 * @return : true if buffer configuration is successful.
221 * false, otherwise.
222 */
223 virtual bool SetImageBuffer(char *buffers[], size_t len_buffers[], unsigned int num_buffers) = 0;
224 /*
225 * SetImageBuffer - Configure the uncompressed primary image buffers (dmabuf)
226 * @buffers[in] : file descriptors of the buffers exported by dma-buf
227 * @len_buffers[in] : sizes of the buffers
228 * @num_buffers[in] : the number of elements of @buffers and @len_buffers
229 * @return : true if buffer configuration is successful.
230 * false, otherwise.
231 */
232 virtual bool SetImageBuffer(int buffers[], size_t len_buffers[], unsigned int num_buffers) = 0;
233 /*
234 * SetImageBuffer2 - Configure the uncompressed secondary image buffers (userptr)
235 * @buffers[in] : addresses of the buffers
236 * @len_buffers[in] : sizes of the buffers
237 * @num_buffers[in] : the number of elements of @buffers and @len_buffers
238 * @return : true if buffer configuration is successful.
239 * false, otherwise.
240 */
241 virtual bool SetImageBuffer2(char *buffers[] __unused, size_t len_buffers[] __unused, unsigned int num_buffers __unused) { return false; }
242 /*
243 * SetImageBuffer2 - Configure the uncompressed secondary image buffers (dmabuf)
244 * @buffers[in] : file descriptors of the buffers exported by dma-buf
245 * @len_buffers[in] : sizes of the buffers
246 * @num_buffers[in] : the number of elements of @buffers and @len_buffers
247 * @return : true if buffer configuration is successful.
248 * false, otherwise.
249 */
250 virtual bool SetImageBuffer2(int buffers[] __unused, size_t len_buffers[] __unused, unsigned int num_buffers __unused) { return false; }
251 /*
252 * SetJpegBuffer - Configure the buffer of JPEG stream of the primary image (userptr)
253 * @buffer [in] : The address of the buffer
254 * @len_buffer [in] : The size of @buffer
255 * @return : true if buffer configuration is successful.
256 * false, otherwise.
257 */
258 virtual bool SetJpegBuffer(char *buffer, size_t len_buffer) = 0;
259 /*
260 * SetJpegBuffer - Configure the buffer of JPEG stream of the primary image (dmabuf)
261 * @buffer [in] : The file descriptor of the buffer exported by dma-buf
262 * @len_buffer [in] : The size of @buffer
263 * @return : true if buffer configuration is successful.
264 * false, otherwise.
265 */
266 virtual bool SetJpegBuffer(int buffer, size_t len_buffer) = 0;
267 /*
268 * SetJpegBuffer2 - Configure the buffer of JPEG stream of the secondary image (userptr)
269 * @buffer [in] : The address of the buffer
270 * @len_buffer [in] : The size of @buffer
271 * @return : true if buffer configuration is successful.
272 * false, otherwise.
273 * The secondary image configuration is ignored if the secondary image size
274 * is not configured with SetImageSize().
275 */
276 virtual bool SetJpegBuffer2(char *buffer __unused, size_t len_buffer __unused) { return false; }
277 /*
278 * SetJpegBuffer2 - Configure the buffer of JPEG stream of the secondary image (dmabuf)
279 * @buffer [in] : The file descriptor of the buffer exported by dma-buf
280 * @len_buffer [in] : The size of @buffer
281 * @return : true if buffer configuration is successful.
282 * false, otherwise.
283 * The secondary image configuration is ignored if the secondary image size
284 * is not configured with SetImageSize().
285 */
286 virtual bool SetJpegBuffer2(int buffer __unused, size_t len_buffer __unused) { return false; }
287 /*
288 * Compress - Compress the given image
289 * secondary_stream_size[out] : The size of secondary JPEG stream
290 * block_mode[in] : If @block_mode is true this function does not return
291 * until the compression finishes or error occurrs.
292 * If a derived function does not support for non-block mode,
293 * errur is returned.
294 * @return : The size of the compressed JPEG stream
295 * (the offset of EOI from SOI plus sizeof(EOI))
296 * Zero If @block_mode is false and no error is occurred.
297 * Negative value on error.
298 */
299 virtual ssize_t Compress(size_t *secondary_stream_size = NULL, bool block_mode = true) = 0;
300 /*
301 * WaitForCompression - Wait for the compression finishes
302 * secondary_stream_size[out] : The size of secondary JPEG stream
303 * @return : The size of the compressed JPEG stream
304 * (the offset of EOI from SOI plus sizeof(EOI))
305 * Negative value on error.
306 *
307 * This function waits until the HWJPEG finishes JPEG compression if the second parameter
308 * to Compress() is false. If the parameter is true, WaitForCompression() immeidately
309 * returns and the returned size will be the stream sizes obtained by the last call to
310 * Compress().
311 */
312 virtual ssize_t WaitForCompression(size_t *secondary_stream_size = NULL) { return GetStreamSize(secondary_stream_size); }
313 /*
314 * GetImageBuffers - Retrieve the configured uncompressed image buffer information (dmabuf)
315 * @buffers[out]: The file descriptors of the buffers exported by dma-buf
316 * @len_buffers[out]: The size of each buffers in @buffers
317 * @num_buffers[in]: The number of elements in @buffers and @len_buffers array
318 * return: true if retrieving the buffer information is successful.
319 * false if no buffer is configured or the configured buffer is userptr type.
320 * DEPREDCATED. DO NOT USE THIS FUNCTION.
321 * This function is just provided to support the legacy ExynosJpegEncoder API.
322 */
323 virtual bool GetImageBuffers(int buffers[] __unused, size_t len_buffers[] __unused, unsigned int num_buffers __unused) { return false; }
324 /*
325 * GetImageBuffers - Retrieve the configured uncompressed image buffer information (userptr)
326 * @buffers[out]: The addresses of the buffers
327 * @len_buffers[out]: The size of each buffers in @buffers
328 * @num_buffers[in]: The number of elements in @buffers and @len_buffers array
329 * return: true if retrieving the buffer information is successful.
330 * false if no buffer is configured or the configured buffer is dmabuf type.
331 * DEPREDCATED. DO NOT USE THIS FUNCTION.
332 * This function is just provided to support the legacy ExynosJpegEncoder API.
333 */
334 virtual bool GetImageBuffers(char *buffers[] __unused, size_t len_buffers[] __unused, unsigned int num_buffers __unused) { return false; }
335 /*
336 * GetJpegBuffers - Retrieve the configured JPEG stream image buffer information (dmabuf)
337 * @buffers[out]: The file descriptor of the buffer exported by dma-buf
338 * @len_buffers[out]: The size of @buffer
339 * return: true if retrieving the buffer information is successful.
340 * false if no buffer is configured or the configured buffer is userptr type.
341 * DEPREDCATED. DO NOT USE THIS FUNCTION.
342 * This function is just provided to support the legacy ExynosJpegEncoder API.
343 */
344 virtual bool GetJpegBuffer(int *buffers __unused, size_t *len_buffer __unused) { return false; }
345 /*
346 * GetJpegBuffers - Retrieve the configured JPEG stream buffer information (userptr)
347 * @buffers[out]: The address of the buffer
348 * @len_buffers[out]: The size of @buffers
349 * return: true if retrieving the buffer information is successful.
350 * false if no buffer is configured or the configured buffer is dmabuf type.
351 * DEPREDCATED. DO NOT USE THIS FUNCTION.
352 * This function is just provided to support the legacy ExynosJpegEncoder API.
353 */
354 virtual bool GetJpegBuffer(char **buffers __unused, size_t *len_buffer __unused) { return false; }
355 /*
356 * Release - release the buffers acquired by CHWJpegCompressor
357 */
358 virtual void Release() { }
359 };
360
361 /*
362 * CHWJpegDecompressor - The abstract class of HW JPEG accelerator for decompression
363 *
364 * This class is *not* thread-safe. If an instance of this class is handled by
365 * multiple threads, the users of the instance should care about the thread
366 * synchronization.
367 *
368 * CHWJpegDecompressor supports for downscaling during decompression by 1/2, 1/4 and
369 * 1/8 if HWJPEG supports. The users should test if the HWJPEG supports for downscaling
370 * before configuring smaller output image size.
371 * The users also test if the HWJPEG (driver) requires the address SOI or SOS. If it
372 * needs SOI, there is no need to feed the driver DHT and DQT. If it needs SOS, DHT
373 * DQT should be informed to the driver because it is unable to find DHT and DQT
374 * from SOS.
375 *
376 * V4L2_CAP_EXYNOS_JPEG_DOWNSCALING is set if HWJPEG supports for downscaling.
377 * V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_FROM_SOS is set if HWJPEG driver needs the
378 * address of SOS and the users to specify DHT and DQT to the driver.
379 */
380 class CHWJpegDecompressor : public CHWJpegBase {
381 public:
382 CHWJpegDecompressor(const char *path) : CHWJpegBase(path) { }
383 virtual ~CHWJpegDecompressor() { }
384 /*
385 * SetImageFormat - Configure decompressed image pixel format
386 * @v4l2_fmt[in] : Image pixel format defined in <linux/videodev2.h>
387 * @width[in] : Width of the decompressed image in the number of pixels
388 * @height[in] : Height of the decompressed image in the number of pixels
389 * @return : true if configuration of image pixel format is successful.
390 * false, otherwise.
391 *
392 * @width and @height can be smaller than the compressed image size specified
393 * by SetStreamPixelSize() if downscaling during decompression is supported.
394 * The subclasses should test if V4L2_CAP_EXYNOS_JPEG_DOWNSCALING is set
395 * in the device capabilities. Even though downscaling is supported by HWJPEG,
396 * it has strict limitation that the downscaling factor should be one of
397 * 1, 2, 4 and 8. If the specified decompressed image size is not one of
398 * the compressed image size divided by 1, 2, 4 or 8, decompression should fail.
399 */
400 virtual bool SetImageFormat(unsigned int v4l2_fmt, unsigned int width, unsigned int height) = 0;
401
402 /*
403 * SetImageBuffer - Configure the decompressed image buffer (userptr)
404 * @buffer[in] : address of the buffer
405 * @len_buffer[in] : size of the buffer
406 * @return : true if buffer configuration is successful.
407 * false, otherwise.
408 */
409 virtual bool SetImageBuffer(char *buffer, size_t len_buffer) = 0;
410
411 /*
412 * SetImageBuffer - Configure the decompressed image buffer (dmabuf)
413 * @buffer[in] : file descriptor of the buffer exported by dma-buf
414 * @len_buffer[in] : size of the buffer
415 * @return : true if buffer configuration is successful.
416 * false, otherwise.
417 */
418 virtual bool SetImageBuffer(int buffer, size_t len_buffer) = 0;
419
420 /*
421 * SetStreamPixelSize - Configure the width and the height of the compressed stream
422 * @width[in] : The number of horizontal pixels of the compressed image
423 * @height[in] : The number of vertical pixels of the compressed image
424 */
425 virtual bool SetStreamPixelSize(unsigned int width __unused, unsigned int height __unused) { return true; }
426
427 /*
428 * SetChromaSampFactor - Configure the chroma subsampling factor for JPEG stream
429 * @horizontal[in] : horizontal chroma subsampling factor
430 * @vertical[in] : vertical chroma subsampling factor
431 * @return: true if chroma subsamping factors are configured successfully,
432 * false if the factors are invalid.
433 *
434 * If V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_FROM_SOS is specified in the device
435 * capabilities, it is needed to configure chroma subsampling fractors because
436 * Neither of HWJPEG nor its driver is able to find the chroma subsampling factors
437 * of the compressed stream because it is specified in SOF and SOF is written
438 * ahead of SOS in the JPEG stream.
439 * If it is required to specify chroma subsampling factors separately, you should
440 * override SetChromaSampFactor().
441 */
442 virtual bool SetChromaSampFactor(unsigned int horizontal __unused, unsigned int vertical __unused) { return true; }
443
444 /*
445 * SetDQT - Configure the address of DQT
446 * @dqt[in] : The address of DQT in the JPEG stream
447 * @return: true if the specified DQT has no problem. false if DQT does not exist
448 * in @dqt or the tables in @dqt are incomplete.
449 *
450 * If V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_FROM_SOS is specified in the device
451 * capabilities, the HWJPEG needs DQT separately. Therefore every subcalss
452 * will need to override SetDQT().
453 */
454 virtual bool SetDQT(const char *dqt __unused) { return true; }
455
456 /*
457 * SetDHT - Configure the address of DHT
458 * @dht[in] : The address of DHT in the JPEG stream
459 * @return: true if the specified DHT has no problem. false if DHT does not exist
460 * in @dht or the tables in @dqt are incomplete.
461 *
462 * If V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_FROM_SOS is specified in the device
463 * capabilities, the HWJPEG needs DHT separately. Therefore every subcalss
464 * will need to override SetDHT().
465 */
466 virtual bool SetDHT(const char *dht __unused) { return true; }
467
468 /*
469 * Decompress - Decompress the given JPEG stream
470 * @buffer[in] : The buffer of JPEG stream.
471 * @len[in] : The length of the JPEG stream. It includes EOI marker.
472 * @return : true if the decompression succeeded. false, otherwise.
473 *
474 * If V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_FROM_SOS is set in the device capability
475 * SOS marker should be at the start of @buffer. Otherwise, SOI marker should be
476 * at the start. If @buffer is start with SOS marker, DHT, DQT and chroma
477 * subsampling factors should be separately configured with SetDHT(), SetDQT() and
478 * SetChromaSampFactor(), respectively.
479 */
480 virtual bool Decompress(const char *buffer, size_t len) = 0;
481 };
482
483 class CHWJpegFlagManager {
484 unsigned int m_uiHWConfigFlags;
485 public:
486 CHWJpegFlagManager() : m_uiHWConfigFlags(0) { }
487 void SetFlag(unsigned int flag) { m_uiHWConfigFlags |= flag; }
488 void ClearFlag(unsigned int flag) { m_uiHWConfigFlags &= ~flag; }
489 bool TestFlag(unsigned int flag) { return (m_uiHWConfigFlags & flag) == flag; }
490 bool TestFlagEither(unsigned int flag) { return !!(m_uiHWConfigFlags & flag); }
491 unsigned int GetFlags() { return m_uiHWConfigFlags; }
492 };
493 /*
494 class CHWJpegM2M1SHOTCompressor: public CHWJpegCompressor {
495 };
496 */
497
498 #define TO_SEC_IMG_SIZE(val) (((val) >> 16) & 0xFFFF)
499
500 class CHWJpegV4L2Compressor : public CHWJpegCompressor, private CHWJpegFlagManager {
501 enum {
502 HWJPEG_CTRL_CHROMFACTOR = 0,
503 HWJPEG_CTRL_QFACTOR,
504 HWJPEG_CTRL_QFACTOR2,
505 HWJPEG_CTRL_HWFC,
506 HWJPEG_CTRL_NUM,
507 };
508
509 enum {
510 HWJPEG_FLAG_PIX_FMT = 0x1, // Set if unapplied image format exists
511
512 HWJPEG_FLAG_QBUF_OUT = 0x100, // Set if the image buffer is queued
513 HWJPEG_FLAG_QBUF_CAP = 0x200, // Set if the JPEG stream buffer is queued
514 HWJPEG_FLAG_REQBUFS = 0x400,
515 HWJPEG_FLAG_STREAMING = 0x800,
516
517 HWJPEG_FLAG_SRC_BUFFER = 0x10000, // Set if SetImageBuffer() is invoked successfully
518 HWJPEG_FLAG_SRC_BUFFER2 = 0x20000, // Set if SetImageBuffer2() is invoked successfully
519 HWJPEG_FLAG_DST_BUFFER = 0x40000, // Set if SetJpegBuffer() is invoked successfully
520 HWJPEG_FLAG_DST_BUFFER2 = 0x80000, // Set if SetJpegBuffer2() is invoked successfully
521 };
522
523 struct hwjpeg_v4l2_controls {
524 __u32 id;
525 __s32 value;
526 } m_v4l2Controls[HWJPEG_CTRL_NUM];
527
528 unsigned int m_uiControlsToSet;
529 // H/W delay of the last compressoin in usec.
530 // Only valid after Compression() successes.
531 unsigned int m_uiHWDelay;
532
533 v4l2_format m_v4l2Format; // v4l2 format for the source image
534 v4l2_buffer m_v4l2SrcBuffer; // v4l2 source buffer
535 v4l2_plane m_v4l2SrcPlanes[6];
536 v4l2_buffer m_v4l2DstBuffer;
537 v4l2_plane m_v4l2DstPlanes[2];
538
539 bool m_bEnableHWFC;
540
541 bool IsB2BCompression() {
542 return (TO_SEC_IMG_SIZE(m_v4l2Format.fmt.pix_mp.width) +
543 TO_SEC_IMG_SIZE(m_v4l2Format.fmt.pix_mp.height)) != 0;
544 }
545
546 // V4L2 Helpers
547 bool TryFormat();
548 bool SetFormat();
549 bool UpdateControls();
550 bool ReqBufs(unsigned int count = 1);
551 bool StreamOn();
552 bool StreamOff();
553 bool QBuf();
554 ssize_t DQBuf(size_t *secondary_stream_size);
555 bool StopStreaming();
556 public:
557 CHWJpegV4L2Compressor();
558 virtual ~CHWJpegV4L2Compressor();
559
560 unsigned int GetHWDelay() { return m_uiHWDelay; }
561
562 // SetChromaSampFactor can be called during streaming
563 virtual bool SetChromaSampFactor(unsigned int horizontal,
564 unsigned int vertical);
565 virtual bool SetQuality(unsigned int quality_factor,
566 unsigned int quality_factor2 = 0);
567 virtual bool SetQuality(const unsigned char qtable[]);
568
569 virtual bool SetImageFormat(unsigned int v4l2_fmt, unsigned int width, unsigned int height,
570 unsigned int sec_width = 0, unsigned sec_height = 0);
571 virtual bool GetImageBufferSizes(size_t buf_sizes[], unsigned int *num_bufffers);
572 virtual bool SetImageBuffer(char *buffers[], size_t len_buffers[], unsigned int num_buffers);
573 virtual bool SetImageBuffer(int buffers[], size_t len_buffers[], unsigned int num_buffers);
574 virtual bool SetImageBuffer2(char *buffers[], size_t len_buffers[], unsigned int num_buffers);
575 virtual bool SetImageBuffer2(int buffers[], size_t len_buffers[], unsigned int num_buffers);
576 virtual bool SetJpegBuffer(char *buffer, size_t len_buffer);
577 virtual bool SetJpegBuffer(int buffer, size_t len_buffer);
578 virtual bool SetJpegBuffer2(char *buffer, size_t len_buffer);
579 virtual bool SetJpegBuffer2(int buffer, size_t len_buffer);
580 virtual ssize_t Compress(size_t *secondary_stream_size = NULL, bool bock_mode = true);
581 virtual bool GetImageBuffers(int buffers[], size_t len_buffers[], unsigned int num_buffers);
582 virtual bool GetImageBuffers(char *buffers[], size_t len_buffers[], unsigned int num_buffers);
583 virtual bool GetJpegBuffer(char **buffer, size_t *len_buffer);
584 virtual bool GetJpegBuffer(int *buffer, size_t *len_buffer);
585 virtual ssize_t WaitForCompression(size_t *secondary_stream_size = NULL);
586 virtual void Release();
587 };
588
589 class CHWJpegV4L2Decompressor : public CHWJpegDecompressor, private CHWJpegFlagManager {
590 enum {
591 HWJPEG_FLAG_OUTPUT_READY = 0x10, /* the output stream is ready */
592 HWJPEG_FLAG_CAPTURE_READY = 0x20, /* the capture stream is ready */
593 };
594
595 unsigned int m_uiHWDelay;
596
597 v4l2_format m_v4l2Format;
598 v4l2_buffer m_v4l2DstBuffer; /* multi-planar foramt is not supported */
599
600 bool PrepareCapture();
601 void CancelCapture();
602
603 bool PrepareStream();
604 void CancelStream();
605 bool QBufAndWait(const char *buffer, size_t len);
606 public:
607 CHWJpegV4L2Decompressor();
608 virtual ~CHWJpegV4L2Decompressor();
609 virtual bool SetImageFormat(unsigned int v4l2_fmt, unsigned int width, unsigned int height);
610 virtual bool SetImageBuffer(char *buffer, size_t len_buffer);
611 virtual bool SetImageBuffer(int buffer, size_t len_buffer);
612 virtual bool Decompress(const char *buffer, size_t len);
613
614 unsigned int GetHWDelay() { return m_uiHWDelay; }
615 };
616 #endif /* __EXYNOS_HWJPEG_H__ */