00001 /* 00002 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following conditions 00006 * are met: 00007 * * Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * * Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * * Neither the name of NVIDIA CORPORATION nor the names of its 00013 * contributors may be used to endorse or promote products derived 00014 * from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY 00017 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00019 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00020 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00021 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00022 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00023 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00024 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 */ 00028 00029 /** 00030 * @file 00031 * <b>NVIDIA Eagle Eye API: Image Encode API</b> 00032 * 00033 * @b This file declares the NvJpegEncoder APIs. 00034 */ 00035 00036 #ifndef __NV_JPEG_ENCODER_H__ 00037 #define __NV_JPEG_ENCODER_H__ 00038 00039 /** 00040 * 00041 * @defgroup ee_nvjpegencoder_group Image Encode 00042 * @ingroup ee_nvimage_group 00043 * 00044 * The NvJPEGEncoder API provides functionality for encoding JPEG 00045 * images using libjpeg APIs. 00046 * 00047 * @{ 00048 */ 00049 00050 00051 #ifndef TEGRA_ACCELERATE 00052 /** 00053 * @c TEGRA_ACCELERATE must be defined to enable hardware acceleration of 00054 * JPEG encoding. 00055 */ 00056 #define TEGRA_ACCELERATE 00057 #endif 00058 00059 #include <stdio.h> 00060 #include "jpeglib.h" 00061 #include "NvElement.h" 00062 #include "NvBuffer.h" 00063 00064 #ifndef MAX_CHANNELS 00065 /** 00066 * Specifies the maximum number of channels to be used with the 00067 * encoder. 00068 */ 00069 #define MAX_CHANNELS 3 00070 #endif 00071 00072 /** 00073 * 00074 * NvJpegEncoder uses the @c libjpeg APIs for encoding JPEG images. It 00075 * supports two methods for encoding: 00076 * 00077 * - Encode using a file descriptor (FD) of the MMAP buffer created by 00078 * a V4L2 element. (Supports YUV420 and NV12 color formats) 00079 * - Encode using data pointers in NvBuffer object, i.e., software allocated 00080 * memory (@c malloc). (Supports YUV420 color format) 00081 * 00082 * @note Only the JCS_YCbCr (YUV420) color space is currently 00083 * supported. 00084 */ 00085 class NvJPEGEncoder:public NvElement 00086 { 00087 public: 00088 /** 00089 * Create a new JPEG Encoder named @a comp_name. 00090 * 00091 * @return Reference to the newly created decoder object, or NULL 00092 * in case of failure during initialization. 00093 */ 00094 static NvJPEGEncoder *createJPEGEncoder(const char *comp_name); 00095 ~NvJPEGEncoder(); 00096 00097 /** 00098 * 00099 * Encodes a JPEG image from a file descriptor (FD) of hardware 00100 * buffer memory. 00101 * 00102 * The application may allocate the memory for storing the JPEG 00103 * image. If the allocation is less than what is required, @c 00104 * libjpeg allocates more memory. The @a out_buf pointer and @a 00105 * out_buf_size are updated accordingly. 00106 * 00107 * Supports YUV420 and NV12 formats. 00108 * 00109 * @attention The application must free the @a out_buf memory. 00110 * 00111 * @param[out] fd Indicates the file descriptor (FD) of the hardware buffer. 00112 * @param[out] color_space Indicates the color_space to use for encoding. 00113 * @param[in] out_buf Specifies a pointer to the memory for the JPEG image. 00114 * @param[in] out_buf_size Specifies the size of the output buffer in bytes. 00115 * @return 0 for success, -1 otherwise. 00116 */ 00117 int encodeFromFd(int fd, J_COLOR_SPACE color_space, 00118 unsigned char **out_buf, unsigned long &out_buf_size); 00119 00120 /** 00121 * Encodes a JPEG image from software buffer memory. 00122 * 00123 * The application may allocate the memory for storing the JPEG 00124 * image. If the allocation is less than what is required, @c 00125 * libjpeg allocates more memory. The @a out_buf pointer and @a 00126 * out_buf_size are updated accordingly. 00127 * 00128 * The @c encodeFromBuffer method is slower than @c 00129 * NvJPEGEncoder::encodeFromFd because @c encodeFromBuffer 00130 * involves conversion from software buffer memory to hardware 00131 * buffer memory. 00132 * 00133 * Supports YUV420 format only. 00134 * 00135 * @attention The application must free the @a out_buf memory. 00136 * 00137 * @param[out] buffer Indicates the NvBuffer object to contain the encoded 00138 image. 00139 * @param[out] color_space Indicates the color_space to use for encoding. 00140 * @param[in] out_buf Specifies a pointer to the memory for the JPEG image. 00141 * @param[in] out_buf_size Specifies the size of the output buffer in bytes. 00142 * @return 0 for success, -1 otherwise. 00143 */ 00144 int encodeFromBuffer(NvBuffer & buffer, J_COLOR_SPACE color_space, 00145 unsigned char **out_buf, unsigned long &out_buf_size); 00146 00147 /** 00148 * Set the cropping rectangle to be used by the jpeg encoder. This method 00149 * should be called before #encodeFromFd or #encodeFromBuffer for the 00150 * cropping to take effect. 00151 * 00152 * @attention The jpeg encoder resets the crop paramaters after each call 00153 * to jpeg_finish_compress. Hence, this method must be called before every 00154 * call to #encodeFromFd or #encodeFromBuffer. 00155 * 00156 * @param[in] left Horizontal offset of the cropping rectangle, in pixels 00157 * @param[in] top Vertical offset of the cropping rectangle, in pixels 00158 * @param[in] width Width of the cropping rectangle, in pixels 00159 * @param[in] height Height of the cropping rectangle, in pixels 00160 */ 00161 void setCropRect(uint32_t left, uint32_t top, uint32_t width, 00162 uint32_t height); 00163 00164 private: 00165 00166 NvJPEGEncoder(const char *comp_name); 00167 struct jpeg_compress_struct cinfo; 00168 struct jpeg_error_mgr jerr; 00169 00170 static const NvElementProfiler::ProfilerField valid_fields = 00171 NvElementProfiler::PROFILER_FIELD_TOTAL_UNITS | 00172 NvElementProfiler::PROFILER_FIELD_LATENCIES; 00173 }; 00174 #endif 00175 /** @} */