diff options
-rw-r--r-- | libhwjpeg/AppMarkerWriter.cpp | 199 | ||||
-rw-r--r-- | libhwjpeg/AppMarkerWriter.h | 26 | ||||
-rw-r--r-- | libhwjpeg/ExynosJpegEncoder.cpp | 109 | ||||
-rw-r--r-- | libhwjpeg/ExynosJpegEncoderForCamera.cpp | 350 | ||||
-rw-r--r-- | libhwjpeg/IFDWriter.h | 35 | ||||
-rw-r--r-- | libhwjpeg/LibScalerForJpeg.cpp | 177 | ||||
-rw-r--r-- | libhwjpeg/LibScalerForJpeg.h | 40 | ||||
-rw-r--r-- | libhwjpeg/ThumbnailScaler.cpp | 6 | ||||
-rw-r--r-- | libhwjpeg/ThumbnailScaler.h | 10 | ||||
-rw-r--r-- | libhwjpeg/hwjpeg-base.cpp | 70 | ||||
-rw-r--r-- | libhwjpeg/hwjpeg-internal.h | 28 | ||||
-rw-r--r-- | libhwjpeg/hwjpeg-v4l2.cpp | 263 | ||||
-rw-r--r-- | libhwjpeg/include/ExynosJpegApi.h | 34 | ||||
-rw-r--r-- | libhwjpeg/include/ExynosJpegEncoderForCamera.h | 27 | ||||
-rw-r--r-- | libhwjpeg/include/exynos-hwjpeg.h | 151 | ||||
-rw-r--r-- | libhwjpeg/include/hwjpeglib-exynos.h | 34 | ||||
-rw-r--r-- | libhwjpeg/libhwjpeg-exynos.cpp | 126 |
17 files changed, 765 insertions, 920 deletions
diff --git a/libhwjpeg/AppMarkerWriter.cpp b/libhwjpeg/AppMarkerWriter.cpp index 4a5edbb..b764025 100644 --- a/libhwjpeg/AppMarkerWriter.cpp +++ b/libhwjpeg/AppMarkerWriter.cpp @@ -15,22 +15,22 @@ * limitations under the License. */ -#include "hwjpeg-internal.h" #include "AppMarkerWriter.h" + #include "IFDWriter.h" +#include "hwjpeg-internal.h" -static const char ExifAsciiPrefix[] = { 'A', 'S', 'C', 'I', 'I', 0x0, 0x0, 0x0 }; -static const char ExifIdentifierCode[6] = { 'E', 'x', 'i', 'f', 0x00, 0x00 }; -static char TiffHeader[8] = { 'I', 'I', 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00 }; -static const unsigned char ComponentsConfiguration[4] = { 1, 2, 3, 0 }; // YCbCr -static const unsigned char SceneType[4] = { 1, 0, 0, 0 }; // A directly photographed image +static const char ExifAsciiPrefix[] = {'A', 'S', 'C', 'I', 'I', 0x0, 0x0, 0x0}; +static const char ExifIdentifierCode[6] = {'E', 'x', 'i', 'f', 0x00, 0x00}; +static char TiffHeader[8] = {'I', 'I', 0x2A, 0x00, 0x08, 0x00, 0x00, 0x00}; +static const unsigned char ComponentsConfiguration[4] = {1, 2, 3, 0}; // YCbCr +static const unsigned char SceneType[4] = {1, 0, 0, 0}; // A directly photographed image #ifndef __LITTLE_ENDIAN__ CEndianessChecker __LITTLE_ENDIAN__; #endif -CEndianessChecker::CEndianessChecker() -{ +CEndianessChecker::CEndianessChecker() { int num = 1; __little = (*reinterpret_cast<char *>(&num) == 1); if (__little) { @@ -42,15 +42,12 @@ CEndianessChecker::CEndianessChecker() } } - CAppMarkerWriter::CAppMarkerWriter() - : m_pAppBase(NULL), m_pApp1End(NULL), m_pExif(NULL), m_pExtra(NULL) -{ + : m_pAppBase(NULL), m_pApp1End(NULL), m_pExif(NULL), m_pExtra(NULL) { Init(); } -CAppMarkerWriter::CAppMarkerWriter(char *base, exif_attribute_t *exif, debug_attribute_t *debug) -{ +CAppMarkerWriter::CAppMarkerWriter(char *base, exif_attribute_t *exif, debug_attribute_t *debug) { extra_appinfo_t extraInfo; app_info_t appInfo[15]; @@ -64,8 +61,7 @@ CAppMarkerWriter::CAppMarkerWriter(char *base, exif_attribute_t *exif, debug_att PrepareAppWriter(base, exif, &extraInfo); } -void CAppMarkerWriter::Init() -{ +void CAppMarkerWriter::Init() { m_pApp1End = NULL; m_szApp1 = 0; @@ -85,8 +81,8 @@ void CAppMarkerWriter::Init() m_pThumbSizePlaceholder = NULL; } -void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, extra_appinfo_t *extra) -{ +void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, + extra_appinfo_t *extra) { m_pAppBase = base; m_pExif = exif; @@ -96,8 +92,7 @@ void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, extr if (exif) { // APP1 - applen += JPEG_SEGMENT_LENFIELD_SIZE + - ARRSIZE(ExifIdentifierCode) + ARRSIZE(TiffHeader); + applen += JPEG_SEGMENT_LENFIELD_SIZE + ARRSIZE(ExifIdentifierCode) + ARRSIZE(TiffHeader); // 0th IFD: Make, Model, Orientation, Software, // DateTime, YCbCrPositioning, X/Y Resolutions, Exif and GPS @@ -112,24 +107,21 @@ void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, extr if (m_szMake > 0) { m_n0thIFDFields++; applen += IFD_FIELD_SIZE; - if (m_szMake > 3) - applen += m_szMake + 1; + if (m_szMake > 3) applen += m_szMake + 1; } m_szSoftware = strlen(m_pExif->software); if (m_szSoftware > 0) { m_n0thIFDFields++; applen += IFD_FIELD_SIZE; - if (m_szSoftware > 3) - applen += m_szSoftware + 1; + if (m_szSoftware > 3) applen += m_szSoftware + 1; } m_szModel = strlen(m_pExif->model); if (m_szModel > 0) { m_n0thIFDFields++; applen += IFD_FIELD_SIZE; - if (m_szModel > 3) - applen += m_szModel + 1; + if (m_szModel > 3) applen += m_szModel + 1; } if (m_pExif->enableGps) { @@ -144,11 +136,11 @@ void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, extr * - Flash, FlashPixVersion, ColorSpace, PixelXDimension, PixelYDimension, * - ExposureMode, WhiteBalance, FocalLengthIn35mmFilm, SceneCaptureType, * - ComponentsConfiguration - * - SceneType, CustomRendered, Contrast, Saturation, Sharpness + * - SceneType, CustomRendered, Contrast, Saturation, Sharpness * (S)Rational Fields: 9 * - ExposureTime, FNumber, ShutterSpeedValue, ApertureValue, * - BrightnessValue, ExposureBiasValue, MaxApertureValue, FocalLength - * - DigitalZoomRatio + * - DigitalZoomRatio * ASCII Fields: 6 * - DateTimeOriginal, DateTimeDigitized, SubsecTime, SubsecTimeOriginal, * - SubsecTimeDigitized, ImageUniqueID @@ -174,28 +166,24 @@ void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, extr if (m_szUniqueID > 0) { m_nExifIFDFields++; applen += IFD_FIELD_SIZE; - if (m_szUniqueID > 3) - applen += m_szUniqueID + 1; + if (m_szUniqueID > 3) applen += m_szUniqueID + 1; } if (m_pExif->maker_note_size > 0) { m_nExifIFDFields++; applen += IFD_FIELD_SIZE; - if (m_pExif->maker_note_size > 4) - applen += m_pExif->maker_note_size; + if (m_pExif->maker_note_size > 4) applen += m_pExif->maker_note_size; } if (m_pExif->user_comment_size > 0) { m_nExifIFDFields++; applen += IFD_FIELD_SIZE; - if (m_pExif->user_comment_size > 4) - applen += m_pExif->user_comment_size; + if (m_pExif->user_comment_size > 4) applen += m_pExif->user_comment_size; } // Interoperability SubIFD m_nExifIFDFields++; // Interoperability is sub IFD of Exif sub IFD - applen += IFD_FIELD_SIZE + - IFD_FIELDCOUNT_SIZE + IFD_VALOFF_SIZE + IFD_FIELD_SIZE * 2; + applen += IFD_FIELD_SIZE + IFD_FIELDCOUNT_SIZE + IFD_VALOFF_SIZE + IFD_FIELD_SIZE * 2; if (m_pExif->enableGps) { size_t len; @@ -233,8 +221,8 @@ void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, extr * - JPEGInterchangeFormat, JPEGInterchangeFormatLength */ if ((m_pExif->widthThumb < 16) || (m_pExif->heightThumb < 16)) { - ALOGE("Insufficient thumbnail information %dx%d", - m_pExif->widthThumb, m_pExif->heightThumb); + ALOGE("Insufficient thumbnail information %dx%d", m_pExif->widthThumb, + m_pExif->heightThumb); return; } @@ -251,13 +239,17 @@ void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, extr if (extra) { for (int idx = 0; idx < extra->num_of_appmarker; idx++) { - if ((extra->appInfo[idx].appid < EXTRA_APPMARKER_MIN) || (extra->appInfo[idx].appid >= EXTRA_APPMARKER_LIMIT)) { + if ((extra->appInfo[idx].appid < EXTRA_APPMARKER_MIN) || + (extra->appInfo[idx].appid >= EXTRA_APPMARKER_LIMIT)) { ALOGE("Invalid extra APP segment ID %d", extra->appInfo[idx].appid); return; } - if ((extra->appInfo[idx].dataSize == 0) || (extra->appInfo[idx].dataSize > (JPEG_MAX_SEGMENT_SIZE - JPEG_SEGMENT_LENFIELD_SIZE))) { - ALOGE("Invalid APP%d segment size, %u bytes", extra->appInfo[idx].appid, extra->appInfo[idx].dataSize); + if ((extra->appInfo[idx].dataSize == 0) || + (extra->appInfo[idx].dataSize > + (JPEG_MAX_SEGMENT_SIZE - JPEG_SEGMENT_LENFIELD_SIZE))) { + ALOGE("Invalid APP%d segment size, %u bytes", extra->appInfo[idx].appid, + extra->appInfo[idx].dataSize); return; } @@ -283,20 +275,16 @@ void CAppMarkerWriter::PrepareAppWriter(char *base, exif_attribute_t *exif, extr } #define APPMARKLEN (JPEG_MARKER_SIZE + JPEG_SEGMENT_LENFIELD_SIZE) -char *CAppMarkerWriter::WriteAPP11(char *current, size_t dummy, size_t align) -{ +char *CAppMarkerWriter::WriteAPP11(char *current, size_t dummy, size_t align) { ALOG_ASSERT((align & ~align) == 0); - if ((dummy == 0) && (align == 1)) - return current; + if ((dummy == 0) && (align == 1)) return current; - if (!m_pExif && !m_pExtra) - return current; + if (!m_pExif && !m_pExtra) return current; uint16_t len = PTR_TO_ULONG(current + APPMARKLEN) & (align - 1); - if (len) - len = align - len; + if (len) len = align - len; len += dummy + JPEG_SEGMENT_LENFIELD_SIZE; @@ -307,10 +295,8 @@ char *CAppMarkerWriter::WriteAPP11(char *current, size_t dummy, size_t align) return current + len; } -char *CAppMarkerWriter::WriteAPPX(char *current, bool just_reserve) -{ - if (!m_pExtra) - return current; +char *CAppMarkerWriter::WriteAPPX(char *current, bool just_reserve) { + if (!m_pExtra) return current; for (int idx = 0; idx < m_pExtra->num_of_appmarker; idx++) { int appid = m_pExtra->appInfo[idx].appid; @@ -330,10 +316,8 @@ char *CAppMarkerWriter::WriteAPPX(char *current, bool just_reserve) return current; } -char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, bool updating) -{ - if (!m_pExif) - return current; +char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, bool updating) { + if (!m_pExif) return current; // APP1 Marker *current++ = 0xFF; @@ -344,18 +328,15 @@ char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, b current += JPEG_SEGMENT_LENFIELD_SIZE; } else { uint16_t len = m_szApp1; - if (reserve_thumbnail_space) - len += m_szMaxThumbSize + JPEG_APP1_OEM_RESERVED; + if (reserve_thumbnail_space) len += m_szMaxThumbSize + JPEG_APP1_OEM_RESERVED; current = WriteDataInBig(current, len); } // Exif Identifier - for (size_t i = 0; i < ARRSIZE(ExifIdentifierCode); i++) - *current++ = ExifIdentifierCode[i]; + for (size_t i = 0; i < ARRSIZE(ExifIdentifierCode); i++) *current++ = ExifIdentifierCode[i]; char *tiffheader = current; - for (size_t i = 0; i < ARRSIZE(TiffHeader); i++) - *current++ = TiffHeader[i]; + for (size_t i = 0; i < ARRSIZE(TiffHeader); i++) *current++ = TiffHeader[i]; CIFDWriter writer(tiffheader, current, m_n0thIFDFields); @@ -364,12 +345,9 @@ char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, b writer.WriteRational(EXIF_TAG_X_RESOLUTION, 1, &m_pExif->x_resolution); writer.WriteRational(EXIF_TAG_Y_RESOLUTION, 1, &m_pExif->y_resolution); writer.WriteShort(EXIF_TAG_RESOLUTION_UNIT, 1, &m_pExif->resolution_unit); - if (m_szMake > 0) - writer.WriteASCII(EXIF_TAG_MAKE, m_szMake + 1, m_pExif->maker); - if (m_szModel > 0) - writer.WriteASCII(EXIF_TAG_MODEL, m_szModel + 1, m_pExif->model); - if (m_szSoftware > 0) - writer.WriteASCII(EXIF_TAG_SOFTWARE, m_szSoftware + 1, m_pExif->software); + if (m_szMake > 0) writer.WriteASCII(EXIF_TAG_MAKE, m_szMake + 1, m_pExif->maker); + if (m_szModel > 0) writer.WriteASCII(EXIF_TAG_MODEL, m_szModel + 1, m_pExif->model); + if (m_szSoftware > 0) writer.WriteASCII(EXIF_TAG_SOFTWARE, m_szSoftware + 1, m_pExif->software); writer.WriteCString(EXIF_TAG_DATE_TIME, EXIF_DATETIME_LENGTH, m_pExif->date_time); char *pSubIFDBase = writer.BeginSubIFD(EXIF_TAG_EXIF_IFD_POINTER); @@ -379,9 +357,11 @@ char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, b exifwriter.WriteRational(EXIF_TAG_FNUMBER, 1, &m_pExif->fnumber); exifwriter.WriteShort(EXIF_TAG_EXPOSURE_PROGRAM, 1, &m_pExif->exposure_program); exifwriter.WriteShort(EXIF_TAG_ISO_SPEED_RATING, 1, &m_pExif->iso_speed_rating); - exifwriter.WriteUndef(EXIF_TAG_EXIF_VERSION, 4, reinterpret_cast<unsigned char *>(m_pExif->exif_version)); + exifwriter.WriteUndef(EXIF_TAG_EXIF_VERSION, 4, + reinterpret_cast<unsigned char *>(m_pExif->exif_version)); exifwriter.WriteCString(EXIF_TAG_DATE_TIME_ORG, EXIF_DATETIME_LENGTH, m_pExif->date_time); - exifwriter.WriteCString(EXIF_TAG_DATE_TIME_DIGITIZE, EXIF_DATETIME_LENGTH, m_pExif->date_time); + exifwriter.WriteCString(EXIF_TAG_DATE_TIME_DIGITIZE, EXIF_DATETIME_LENGTH, + m_pExif->date_time); exifwriter.WriteSRational(EXIF_TAG_SHUTTER_SPEED, 1, &m_pExif->shutter_speed); exifwriter.WriteRational(EXIF_TAG_APERTURE, 1, &m_pExif->aperture); exifwriter.WriteSRational(EXIF_TAG_BRIGHTNESS, 1, &m_pExif->brightness); @@ -389,16 +369,21 @@ char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, b exifwriter.WriteRational(EXIF_TAG_MAX_APERTURE, 1, &m_pExif->max_aperture); exifwriter.WriteShort(EXIF_TAG_METERING_MODE, 1, &m_pExif->metering_mode); exifwriter.WriteShort(EXIF_TAG_FLASH, 1, &m_pExif->flash); - exifwriter.WriteUndef(EXIF_TAG_FLASHPIX_VERSION, 4, reinterpret_cast<const unsigned char *>("0100")); + exifwriter.WriteUndef(EXIF_TAG_FLASHPIX_VERSION, 4, + reinterpret_cast<const unsigned char *>("0100")); exifwriter.WriteUndef(EXIF_TAG_COMPONENTS_CONFIGURATION, 4, ComponentsConfiguration); exifwriter.WriteRational(EXIF_TAG_FOCAL_LENGTH, 1, &m_pExif->focal_length); exifwriter.WriteCString(EXIF_TAG_SUBSEC_TIME, EXIF_SUBSECTIME_LENGTH, m_pExif->sec_time); - exifwriter.WriteCString(EXIF_TAG_SUBSEC_TIME_ORIG, EXIF_SUBSECTIME_LENGTH, m_pExif->sec_time); - exifwriter.WriteCString(EXIF_TAG_SUBSEC_TIME_DIG, EXIF_SUBSECTIME_LENGTH, m_pExif->sec_time); + exifwriter.WriteCString(EXIF_TAG_SUBSEC_TIME_ORIG, EXIF_SUBSECTIME_LENGTH, + m_pExif->sec_time); + exifwriter.WriteCString(EXIF_TAG_SUBSEC_TIME_DIG, EXIF_SUBSECTIME_LENGTH, + m_pExif->sec_time); if (m_pExif->maker_note_size > 0) - exifwriter.WriteUndef(EXIF_TAG_MAKER_NOTE, m_pExif->maker_note_size, m_pExif->maker_note); + exifwriter.WriteUndef(EXIF_TAG_MAKER_NOTE, m_pExif->maker_note_size, + m_pExif->maker_note); if (m_pExif->user_comment_size > 0) - exifwriter.WriteUndef(EXIF_TAG_USER_COMMENT, m_pExif->user_comment_size, m_pExif->user_comment); + exifwriter.WriteUndef(EXIF_TAG_USER_COMMENT, m_pExif->user_comment_size, + m_pExif->user_comment); exifwriter.WriteShort(EXIF_TAG_COLOR_SPACE, 1, &m_pExif->color_space); exifwriter.WriteLong(EXIF_TAG_PIXEL_X_DIMENSION, 1, &m_pExif->width); exifwriter.WriteLong(EXIF_TAG_PIXEL_Y_DIMENSION, 1, &m_pExif->height); @@ -407,7 +392,8 @@ char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, b exifwriter.WriteShort(EXIF_TAG_EXPOSURE_MODE, 1, &m_pExif->exposure_mode); exifwriter.WriteShort(EXIF_TAG_WHITE_BALANCE, 1, &m_pExif->white_balance); exifwriter.WriteRational(EXIF_TAG_DIGITAL_ZOOM_RATIO, 1, &m_pExif->digital_zoom_ratio); - exifwriter.WriteShort(EXIF_TAG_FOCA_LENGTH_IN_35MM_FILM, 1, &m_pExif->focal_length_in_35mm_length); + exifwriter.WriteShort(EXIF_TAG_FOCA_LENGTH_IN_35MM_FILM, 1, + &m_pExif->focal_length_in_35mm_length); exifwriter.WriteShort(EXIF_TAG_SCENCE_CAPTURE_TYPE, 1, &m_pExif->scene_capture_type); exifwriter.WriteShort(EXIF_TAG_CONTRAST, 1, &m_pExif->contrast); exifwriter.WriteShort(EXIF_TAG_SATURATION, 1, &m_pExif->saturation); @@ -451,9 +437,9 @@ char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, b size_t idx; len = min(len, static_cast<size_t>(99UL)); unsigned char buf[sizeof(ExifAsciiPrefix) + len + 1]; - for (idx = 0; idx < sizeof(ExifAsciiPrefix); idx++) - buf[idx] = ExifAsciiPrefix[idx]; - strncpy(reinterpret_cast<char *>(buf) + idx, m_pExif->gps_processing_method, len + 1); + for (idx = 0; idx < sizeof(ExifAsciiPrefix); idx++) buf[idx] = ExifAsciiPrefix[idx]; + strncpy(reinterpret_cast<char *>(buf) + idx, m_pExif->gps_processing_method, + len + 1); len += idx; buf[len] = '\0'; gpswriter.WriteUndef(EXIF_TAG_GPS_PROCESSING_METHOD, len + 1, buf); @@ -466,8 +452,7 @@ char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, b } // thumbnail and the next IFD pointer is never updated. - if (updating) - return NULL; + if (updating) return NULL; if (m_pExif->enableThumb) { writer.Finish(false); @@ -496,16 +481,14 @@ char *CAppMarkerWriter::WriteAPP1(char *current, bool reserve_thumbnail_space, b return writer.GetNextIFDBase(); } -void CAppMarkerWriter::Finalize(size_t thumbsize) -{ +void CAppMarkerWriter::Finalize(size_t thumbsize) { if (m_pThumbSizePlaceholder) { uint32_t len = static_cast<uint32_t>(thumbsize); WriteData(m_pThumbSizePlaceholder, len); m_pThumbSizePlaceholder = NULL; } } -void CAppMarkerWriter::UpdateApp1Size(size_t amount) -{ +void CAppMarkerWriter::UpdateApp1Size(size_t amount) { if (m_pAppBase) { uint16_t len = m_szApp1 + amount; WriteDataInBig(m_pAppBase + JPEG_MARKER_SIZE, len); @@ -514,24 +497,25 @@ void CAppMarkerWriter::UpdateApp1Size(size_t amount) static const char *dbgerrmsg = "Updating debug data failed"; -static inline size_t GetSegLen(char *p) -{ +static inline size_t GetSegLen(char *p) { size_t len = (*reinterpret_cast<unsigned char *>(p) & 0xFF) << 8; return len | (*reinterpret_cast<unsigned char *>(p + 1) & 0xFF); } -static inline size_t GetExtraAPPSize(extra_appinfo_t *info) -{ +static inline size_t GetExtraAPPSize(extra_appinfo_t *info) { size_t len = 0; for (int idx = 0; idx < info->num_of_appmarker; idx++) { - if ((info->appInfo[idx].appid < EXTRA_APPMARKER_MIN) || (info->appInfo[idx].appid >= EXTRA_APPMARKER_LIMIT)) { + if ((info->appInfo[idx].appid < EXTRA_APPMARKER_MIN) || + (info->appInfo[idx].appid >= EXTRA_APPMARKER_LIMIT)) { ALOGE("%s: Invalid extra APP segment ID %d", dbgerrmsg, info->appInfo[idx].appid); return 0; } - if ((info->appInfo[idx].dataSize == 0) || (info->appInfo[idx].dataSize > (JPEG_MAX_SEGMENT_SIZE - JPEG_SEGMENT_LENFIELD_SIZE))) { - ALOGE("%s: Invalid APP%d segment size, %u bytes.", dbgerrmsg, info->appInfo[idx].appid, info->appInfo[idx].dataSize); + if ((info->appInfo[idx].dataSize == 0) || + (info->appInfo[idx].dataSize > (JPEG_MAX_SEGMENT_SIZE - JPEG_SEGMENT_LENFIELD_SIZE))) { + ALOGE("%s: Invalid APP%d segment size, %u bytes.", dbgerrmsg, info->appInfo[idx].appid, + info->appInfo[idx].dataSize); return 0; } @@ -594,34 +578,33 @@ bool UpdateDebugData(char *jpeg, size_t jpeglen, extra_appinfo_t *extra) // incl } appid = marker & 0xF; - if (((marker & 0xF0) == 0xE0) && ((appid >= EXTRA_APPMARKER_MIN) && (appid <= EXTRA_APPMARKER_LIMIT))) { + if (((marker & 0xF0) == 0xE0) && + ((appid >= EXTRA_APPMARKER_MIN) && (appid <= EXTRA_APPMARKER_LIMIT))) { if (appid != extra->appInfo[idx].appid) { - ALOGE("%s: stored appid(%d) is different with updated appid(%d)", - dbgerrmsg, appid, extra->appInfo[idx].appid); + ALOGE("%s: stored appid(%d) is different with updated appid(%d)", dbgerrmsg, appid, + extra->appInfo[idx].appid); return false; } seglen = GetSegLen(jpeg); if (seglen < (extra->appInfo[idx].dataSize + JPEG_SEGMENT_LENFIELD_SIZE)) { - ALOGE("%s: too small APP%d length %zu to store %u bytes", - dbgerrmsg, appid, seglen, extra->appInfo[idx].dataSize); + ALOGE("%s: too small APP%d length %zu to store %u bytes", dbgerrmsg, appid, seglen, + extra->appInfo[idx].dataSize); return false; } - memcpy(jpeg + JPEG_SEGMENT_LENFIELD_SIZE, - extra->appInfo[idx].appData, extra->appInfo[idx].dataSize); + memcpy(jpeg + JPEG_SEGMENT_LENFIELD_SIZE, extra->appInfo[idx].appData, + extra->appInfo[idx].dataSize); ALOGD("Successfully updated %u bytes to APP%d", extra->appInfo[idx].dataSize, appid); - validlen -= extra->appInfo[idx].dataSize + JPEG_MARKER_SIZE + JPEG_SEGMENT_LENFIELD_SIZE; + validlen -= + extra->appInfo[idx].dataSize + JPEG_MARKER_SIZE + JPEG_SEGMENT_LENFIELD_SIZE; idx++; } else { // just skip all other segments seglen = GetSegLen(jpeg); - if (seglen == 0) - seglen++; // fixup for invalid segment lengths - if (jpeglen < seglen) - seglen = jpeglen; - + if (seglen == 0) seglen++; // fixup for invalid segment lengths + if (jpeglen < seglen) seglen = jpeglen; } jpeg += seglen; @@ -633,8 +616,7 @@ bool UpdateDebugData(char *jpeg, size_t jpeglen, extra_appinfo_t *extra) // incl static const char *exiferrmsg = "Updating exif failed"; -bool UpdateExif(char *jpeg, size_t jpeglen, exif_attribute_t *exif) -{ +bool UpdateExif(char *jpeg, size_t jpeglen, exif_attribute_t *exif) { if (!exif) { ALOGI("No Exif to update"); return true; @@ -668,8 +650,7 @@ bool UpdateExif(char *jpeg, size_t jpeglen, exif_attribute_t *exif) return true; } -void ExtractDebugAttributeInfo(debug_attribute_t *debug, extra_appinfo_t *extra) -{ +void ExtractDebugAttributeInfo(debug_attribute_t *debug, extra_appinfo_t *extra) { if (!debug) { extra->num_of_appmarker = 0; return; diff --git a/libhwjpeg/AppMarkerWriter.h b/libhwjpeg/AppMarkerWriter.h index 5232e31..9531339 100644 --- a/libhwjpeg/AppMarkerWriter.h +++ b/libhwjpeg/AppMarkerWriter.h @@ -18,6 +18,8 @@ #define __HARDWARE_SAMSUNG_SLSI_EXYNOS_APPMARKER_WRITER_H__ #include <ExynosExif.h> + +#include "hwjpeg-internal.h" #include "include/hardware/exynos/ExynosExif.h" #define JPEG_MAX_SEGMENT_SIZE ((1 << 16) - 1) @@ -33,8 +35,7 @@ #define IFD_COUNT_SIZE 4 #define IFD_VALOFF_SIZE 4 -#define IFD_FIELD_SIZE \ - (IFD_TAG_SIZE + IFD_TYPE_SIZE + IFD_COUNT_SIZE + IFD_VALOFF_SIZE) +#define IFD_FIELD_SIZE (IFD_TAG_SIZE + IFD_TYPE_SIZE + IFD_COUNT_SIZE + IFD_VALOFF_SIZE) #define EXTRA_APPMARKER_MIN 4 #define EXTRA_APPMARKER_LIMIT 10 @@ -49,8 +50,8 @@ class CAppMarkerWriter { char *m_pAppBase; char *m_pApp1End; size_t m_szMaxThumbSize; // Maximum available thumbnail stream size minus JPEG_MARKER_SIZE - uint16_t m_szApp1; // The size of APP1 segment without marker - uint16_t m_szApp11; // The size of APP11 segment without marker + uint16_t m_szApp1; // The size of APP1 segment without marker + uint16_t m_szApp11; // The size of APP11 segment without marker uint16_t m_n0thIFDFields; uint16_t m_n1stIFDFields; uint16_t m_nExifIFDFields; @@ -76,6 +77,7 @@ class CAppMarkerWriter { char *WriteAPP1(char *base, bool reserve_thumbnail_space, bool updating = false); char *WriteAPPX(char *base, bool just_reserve); char *WriteAPP11(char *current, size_t dummy, size_t align); + public: // dummy: number of dummy bytes written by the compressor of the main image // this dummy size should be added to the APP1 length. Howerver, this dummy area @@ -84,7 +86,7 @@ public: CAppMarkerWriter(); CAppMarkerWriter(char *base, exif_attribute_t *exif, debug_attribute_t *debug); - ~CAppMarkerWriter() { } + ~CAppMarkerWriter() {} void PrepareAppWriter(char *base, exif_attribute_t *exif, extra_appinfo_t *info); @@ -100,15 +102,14 @@ public: // CalculateAPPSize() is valid after Write() is successful. size_t CalculateAPPSize(size_t thumblen = JPEG_MAX_SEGMENT_SIZE) { size_t appsize = 0; - if (m_szApp1 > 0) - appsize += m_szApp1 + JPEG_MARKER_SIZE; + if (m_szApp1 > 0) appsize += m_szApp1 + JPEG_MARKER_SIZE; if (m_pExtra) { for (int idx = 0; idx < m_pExtra->num_of_appmarker; idx++) - appsize += m_pExtra->appInfo[idx].dataSize + - + JPEG_MARKER_SIZE + JPEG_SEGMENT_LENFIELD_SIZE; + appsize += m_pExtra->appInfo[idx].dataSize + +JPEG_MARKER_SIZE + + JPEG_SEGMENT_LENFIELD_SIZE; } if (IsThumbSpaceReserved()) - appsize += m_szMaxThumbSize + JPEG_APP1_OEM_RESERVED ; + appsize += m_szMaxThumbSize + JPEG_APP1_OEM_RESERVED; else appsize += min(m_szMaxThumbSize, thumblen); @@ -117,7 +118,8 @@ public: char *GetApp1End() { return m_pApp1End; } - void Write(bool reserve_thumbnail_space, size_t dummy, size_t align, bool reserve_debug = false) { + void Write(bool reserve_thumbnail_space, size_t dummy, size_t align, + bool reserve_debug = false) { m_pApp1End = WriteAPP1(m_pAppBase, reserve_thumbnail_space); char *appXend = WriteAPPX(m_pApp1End, reserve_debug); char *app11end = WriteAPP11(appXend, dummy, align); @@ -129,7 +131,7 @@ public: bool IsThumbSpaceReserved() { return PTR_DIFF(m_pAppBase, m_pApp1End) == - (m_szApp1 + m_szMaxThumbSize + JPEG_APP1_OEM_RESERVED + JPEG_MARKER_SIZE); + (m_szApp1 + m_szMaxThumbSize + JPEG_APP1_OEM_RESERVED + JPEG_MARKER_SIZE); } void Finalize(size_t thumbsize); diff --git a/libhwjpeg/ExynosJpegEncoder.cpp b/libhwjpeg/ExynosJpegEncoder.cpp index f3ae368..f25d9b4 100644 --- a/libhwjpeg/ExynosJpegEncoder.cpp +++ b/libhwjpeg/ExynosJpegEncoder.cpp @@ -15,25 +15,18 @@ * limitations under the License. */ -#include <linux/videodev2.h> - -// This header is non-hermetic and needs to be after videodev2.h #include <ExynosJpegApi.h> +#include <linux/videodev2.h> #include "hwjpeg-internal.h" - -int ExynosJpegEncoder::setJpegConfig(void* pConfig) -{ +int ExynosJpegEncoder::setJpegConfig(void *pConfig) { ExynosJpegEncoder *that = reinterpret_cast<ExynosJpegEncoder *>(pConfig); - if (!setColorFormat(that->m_v4l2Format)) - return -1; + if (!setColorFormat(that->m_v4l2Format)) return -1; - if (!setJpegFormat(that->m_jpegFormat)) - return -1; + if (!setJpegFormat(that->m_jpegFormat)) return -1; - if (!setSize(that->m_nWidth, that->m_nHeight)) - return -1; + if (!setSize(that->m_nWidth, that->m_nHeight)) return -1; m_iInBufType = that->m_iInBufType; m_iOutBufType = that->m_iOutBufType; @@ -41,127 +34,101 @@ int ExynosJpegEncoder::setJpegConfig(void* pConfig) return 0; } -int ExynosJpegEncoder::getInBuf(int *piBuf, int *piInputSize, int iSize) -{ +int ExynosJpegEncoder::getInBuf(int *piBuf, int *piInputSize, int iSize) { if (iSize < 1) { ALOGE("Invalid array size %d for getInBuf()", iSize); return -1; } size_t len_buffers[iSize]; - if (!m_hwjpeg.GetImageBuffers(piBuf, len_buffers, static_cast<unsigned int>(iSize))) - return -1; + if (!m_hwjpeg.GetImageBuffers(piBuf, len_buffers, static_cast<unsigned int>(iSize))) return -1; - for (int i = 0; i < iSize; i++) - piInputSize[i] = static_cast<int>(len_buffers[i]); + for (int i = 0; i < iSize; i++) piInputSize[i] = static_cast<int>(len_buffers[i]); return 0; } -int ExynosJpegEncoder::getOutBuf(int *piBuf, int *piOutputSize) -{ +int ExynosJpegEncoder::getOutBuf(int *piBuf, int *piOutputSize) { size_t len; - if (!m_hwjpeg.GetJpegBuffer(piBuf, &len)) - return -1; + if (!m_hwjpeg.GetJpegBuffer(piBuf, &len)) return -1; *piOutputSize = static_cast<int>(len); return 0; } -int ExynosJpegEncoder::setInBuf(int *piBuf, int *iSize) -{ +int ExynosJpegEncoder::setInBuf(int *piBuf, int *iSize) { size_t buflen[3]; unsigned int bufnum = 3; - if (!EnsureFormatIsApplied()) - return -1; + if (!EnsureFormatIsApplied()) return -1; - if (!m_hwjpeg.GetImageBufferSizes(buflen, &bufnum)) - return -1; + if (!m_hwjpeg.GetImageBufferSizes(buflen, &bufnum)) return -1; - for (unsigned int i = 0; i < bufnum; i++) - buflen[i] = static_cast<size_t>(iSize[i]); + for (unsigned int i = 0; i < bufnum; i++) buflen[i] = static_cast<size_t>(iSize[i]); - if (!m_hwjpeg.SetImageBuffer(piBuf, buflen, bufnum)) - return -1; + if (!m_hwjpeg.SetImageBuffer(piBuf, buflen, bufnum)) return -1; m_iInBufType = JPEG_BUF_TYPE_DMA_BUF; return 0; } -int ExynosJpegEncoder::setOutBuf(int iBuf, int iSize, int offset) -{ - if (!m_hwjpeg.SetJpegBuffer(iBuf, static_cast<size_t>(iSize), offset)) - return -1; +int ExynosJpegEncoder::setOutBuf(int iBuf, int iSize, int offset) { + if (!m_hwjpeg.SetJpegBuffer(iBuf, static_cast<size_t>(iSize), offset)) return -1; m_iOutBufType = JPEG_BUF_TYPE_DMA_BUF; return 0; } -int ExynosJpegEncoder::getInBuf(char **pcBuf, int *piInputSize, int iSize) -{ +int ExynosJpegEncoder::getInBuf(char **pcBuf, int *piInputSize, int iSize) { if (iSize < 1) { ALOGE("Invalid array size %d for getInBuf()", iSize); return -1; } size_t len_buffers[iSize]; - if (!m_hwjpeg.GetImageBuffers(pcBuf, len_buffers, static_cast<unsigned int>(iSize))) - return -1; + if (!m_hwjpeg.GetImageBuffers(pcBuf, len_buffers, static_cast<unsigned int>(iSize))) return -1; - for (int i = 0; i < iSize; i++) - piInputSize[i] = static_cast<int>(len_buffers[i]); + for (int i = 0; i < iSize; i++) piInputSize[i] = static_cast<int>(len_buffers[i]); return 0; } -int ExynosJpegEncoder::getOutBuf(char **pcBuf, int *piOutputSize) -{ +int ExynosJpegEncoder::getOutBuf(char **pcBuf, int *piOutputSize) { size_t len; - if (!m_hwjpeg.GetJpegBuffer(pcBuf, &len)) - return -1; + if (!m_hwjpeg.GetJpegBuffer(pcBuf, &len)) return -1; *piOutputSize = static_cast<int>(len); return 0; } -int ExynosJpegEncoder::setInBuf(char **pcBuf, int *iSize) -{ +int ExynosJpegEncoder::setInBuf(char **pcBuf, int *iSize) { size_t buflen[3]; unsigned int bufnum = 3; - if (!EnsureFormatIsApplied()) - return -1; + if (!EnsureFormatIsApplied()) return -1; - if (!m_hwjpeg.GetImageBufferSizes(buflen, &bufnum)) - return -1; + if (!m_hwjpeg.GetImageBufferSizes(buflen, &bufnum)) return -1; - for (unsigned int i = 0; i < bufnum; i++) - buflen[i] = static_cast<size_t>(iSize[i]); + for (unsigned int i = 0; i < bufnum; i++) buflen[i] = static_cast<size_t>(iSize[i]); - if (!m_hwjpeg.SetImageBuffer(pcBuf, buflen, bufnum)) - return -1; + if (!m_hwjpeg.SetImageBuffer(pcBuf, buflen, bufnum)) return -1; m_iInBufType = JPEG_BUF_TYPE_USER_PTR; return 0; } -int ExynosJpegEncoder::setOutBuf(char *pcBuf, int iSize) -{ - if (!m_hwjpeg.SetJpegBuffer(pcBuf, static_cast<size_t>(iSize))) - return -1; +int ExynosJpegEncoder::setOutBuf(char *pcBuf, int iSize) { + if (!m_hwjpeg.SetJpegBuffer(pcBuf, static_cast<size_t>(iSize))) return -1; m_iOutBufType = JPEG_BUF_TYPE_USER_PTR; return 0; } -int ExynosJpegEncoder::setJpegFormat(int iV4l2JpegFormat) -{ - if (m_jpegFormat == iV4l2JpegFormat) - return 0; +int ExynosJpegEncoder::setJpegFormat(int iV4l2JpegFormat) { + if (m_jpegFormat == iV4l2JpegFormat) return 0; unsigned int hfactor, vfactor; switch (iV4l2JpegFormat) { @@ -194,31 +161,27 @@ int ExynosJpegEncoder::setJpegFormat(int iV4l2JpegFormat) return -1; } - if (!m_hwjpeg.SetChromaSampFactor(hfactor, vfactor)) - return -1; + if (!m_hwjpeg.SetChromaSampFactor(hfactor, vfactor)) return -1; m_jpegFormat = iV4l2JpegFormat; return 0; } -int ExynosJpegEncoder::setColorBufSize(int *piBufSize, int iSize) -{ +int ExynosJpegEncoder::setColorBufSize(int *piBufSize, int iSize) { size_t len[3]; unsigned int num = static_cast<unsigned int>(iSize); - if (!m_hwjpeg.GetImageBufferSizes(len, &num)) - return -1; + if (!m_hwjpeg.GetImageBufferSizes(len, &num)) return -1; - for (unsigned int i = 0; i < num; i++) - piBufSize[i] = static_cast<int>(len[i]); + for (unsigned int i = 0; i < num; i++) piBufSize[i] = static_cast<int>(len[i]); return 0; } bool ExynosJpegEncoder::__EnsureFormatIsApplied() { if (TestStateEither(STATE_SIZE_CHANGED | STATE_PIXFMT_CHANGED) && - !m_hwjpeg.SetImageFormat(m_v4l2Format, m_nWidth, m_nHeight)) + !m_hwjpeg.SetImageFormat(m_v4l2Format, m_nWidth, m_nHeight)) return false; ClearState(STATE_SIZE_CHANGED | STATE_PIXFMT_CHANGED); diff --git a/libhwjpeg/ExynosJpegEncoderForCamera.cpp b/libhwjpeg/ExynosJpegEncoderForCamera.cpp index dd12721..5e003aa 100644 --- a/libhwjpeg/ExynosJpegEncoderForCamera.cpp +++ b/libhwjpeg/ExynosJpegEncoderForCamera.cpp @@ -15,31 +15,24 @@ * limitations under the License. */ +#include <ExynosJpegEncoderForCamera.h> +#include <hardware/exynos/ion.h> +#include <linux/videodev2.h> #include <sys/mman.h> #include <sys/types.h> - -#include <linux/videodev2.h> - -#include <hardware/exynos/ion.h> #include <system/graphics.h> -// This header is non-hermetic and needs to be after videodev2.h -#include <ExynosJpegEncoderForCamera.h> - -#include "hwjpeg-internal.h" -// This header is non-hermetic and needs to be after hwjpeg-internal.h #include "AppMarkerWriter.h" -#include "IFDWriter.h" #include "ThumbnailScaler.h" +#include "hwjpeg-internal.h" // Data length written by H/W without the scan data. -#define NECESSARY_JPEG_LENGTH (0x24B + 2 * JPEG_MARKER_SIZE) +#define NECESSARY_JPEG_LENGTH (0x24B + 2 * JPEG_MARKER_SIZE) -static size_t GetImageLength(unsigned int width, unsigned int height, int v4l2Format) -{ +static size_t GetImageLength(unsigned int width, unsigned int height, int v4l2Format) { size_t size = width * height; - switch(v4l2Format) { + switch (v4l2Format) { case V4L2_PIX_FMT_YUYV: case V4L2_PIX_FMT_YVYU: case V4L2_PIX_FMT_UYVY: @@ -59,22 +52,29 @@ static size_t GetImageLength(unsigned int width, unsigned int height, int v4l2Fo return 0; } -static int GetThumbnailFormat(int v4l2Format) -{ - if (v4l2Format == V4L2_PIX_FMT_NV12M) - return V4L2_PIX_FMT_NV12; - else if (v4l2Format == V4L2_PIX_FMT_NV21M) - return V4L2_PIX_FMT_NV21; - else - return v4l2Format; +static int GetThumbnailFormat(int v4l2Format) { + if (v4l2Format == V4L2_PIX_FMT_NV12M) + return V4L2_PIX_FMT_NV12; + else if (v4l2Format == V4L2_PIX_FMT_NV21M) + return V4L2_PIX_FMT_NV21; + else + return v4l2Format; } ExynosJpegEncoderForCamera::ExynosJpegEncoderForCamera(bool bBTBComp) - : m_phwjpeg4thumb(NULL), m_fdIONClient(-1), m_fdIONThumbImgBuffer(-1), m_pIONThumbImgBuffer(NULL), - m_szIONThumbImgBuffer(0), m_pIONThumbJpegBuffer(NULL), m_fdIONThumbJpegBuffer(-1), m_szIONThumbJpegBuffer(0), - m_nThumbWidth(0), m_nThumbHeight(0), m_nThumbQuality(0), - m_pStreamBase(NULL), m_fThumbBufferType(0) -{ + : m_phwjpeg4thumb(NULL), + m_fdIONClient(-1), + m_fdIONThumbImgBuffer(-1), + m_pIONThumbImgBuffer(NULL), + m_szIONThumbImgBuffer(0), + m_pIONThumbJpegBuffer(NULL), + m_fdIONThumbJpegBuffer(-1), + m_szIONThumbJpegBuffer(0), + m_nThumbWidth(0), + m_nThumbHeight(0), + m_nThumbQuality(0), + m_pStreamBase(NULL), + m_fThumbBufferType(0) { m_pAppWriter = new CAppMarkerWriter(); if (!m_pAppWriter) { ALOGE("Failed to allocated an instance of CAppMarkerWriter"); @@ -96,52 +96,41 @@ ExynosJpegEncoderForCamera::ExynosJpegEncoderForCamera(bool bBTBComp) ALOGERR("Failed to create ION client for thumbnail conversion"); } - if (!bBTBComp) - SetState(STATE_NO_BTBCOMP); + if (!bBTBComp) SetState(STATE_NO_BTBCOMP); // STATE_THUMBSIZE_CHANGED is to know if thumbnail image size need to be // configured to HWJPEG. If HWJPEG does not support for back-to-back // compression, it should not be configured. - if (IsBTBCompressionSupported()) - SetState(STATE_THUMBSIZE_CHANGED); + if (IsBTBCompressionSupported()) SetState(STATE_THUMBSIZE_CHANGED); m_extraInfo.appInfo = m_appInfo; mThumbnailScaler.reset(ThumbnailScaler::createInstance()); - if (!mThumbnailScaler->available()) - ALOGW("Thumbnail scaler is not available."); + if (!mThumbnailScaler->available()) ALOGW("Thumbnail scaler is not available."); ALOGD("ExynosJpegEncoderForCamera Created: %p, ION %d", this, m_fdIONClient); } -ExynosJpegEncoderForCamera::~ExynosJpegEncoderForCamera() -{ +ExynosJpegEncoderForCamera::~ExynosJpegEncoderForCamera() { delete m_pAppWriter; delete m_phwjpeg4thumb; - if (m_pIONThumbImgBuffer != NULL) - munmap(m_pIONThumbImgBuffer, m_szIONThumbImgBuffer); + if (m_pIONThumbImgBuffer != NULL) munmap(m_pIONThumbImgBuffer, m_szIONThumbImgBuffer); - if (m_fdIONThumbImgBuffer >= 0) - close(m_fdIONThumbImgBuffer); + if (m_fdIONThumbImgBuffer >= 0) close(m_fdIONThumbImgBuffer); - if (m_pIONThumbJpegBuffer) - munmap(m_pIONThumbJpegBuffer, m_szIONThumbJpegBuffer); + if (m_pIONThumbJpegBuffer) munmap(m_pIONThumbJpegBuffer, m_szIONThumbJpegBuffer); - if (m_fdIONThumbJpegBuffer >= 0) - close(m_fdIONThumbJpegBuffer); + if (m_fdIONThumbJpegBuffer >= 0) close(m_fdIONThumbJpegBuffer); - if (m_fdIONClient >= 0) - exynos_ion_close(m_fdIONClient); + if (m_fdIONClient >= 0) exynos_ion_close(m_fdIONClient); - ALOGD("ExynosJpegEncoderForCamera Destroyed: %p, ION %d, ThumIMG %d", - this, m_fdIONClient, m_fdIONThumbImgBuffer); + ALOGD("ExynosJpegEncoderForCamera Destroyed: %p, ION %d, ThumIMG %d", this, m_fdIONClient, + m_fdIONThumbImgBuffer); } -int ExynosJpegEncoderForCamera::setThumbnailSize(int w, int h) -{ - if ((m_nThumbWidth == w) && (m_nThumbHeight == h)) - return 0; +int ExynosJpegEncoderForCamera::setThumbnailSize(int w, int h) { + if ((m_nThumbWidth == w) && (m_nThumbHeight == h)) return 0; // w == 0 and h == 0 resets thumbnail configuration if (((w | h) != 0) && ((w < 16) || (h < 16))) { @@ -152,16 +141,13 @@ int ExynosJpegEncoderForCamera::setThumbnailSize(int w, int h) m_nThumbWidth = w; m_nThumbHeight = h; - if (IsBTBCompressionSupported()) - SetState(STATE_THUMBSIZE_CHANGED); + if (IsBTBCompressionSupported()) SetState(STATE_THUMBSIZE_CHANGED); return 0; } -int ExynosJpegEncoderForCamera::setThumbnailQuality(int quality) -{ - if (m_nThumbQuality == quality) - return 0; +int ExynosJpegEncoderForCamera::setThumbnailQuality(int quality) { + if (m_nThumbQuality == quality) return 0; if ((quality > 100) || (quality < 1)) { ALOGE("Invalid quality factor %d for thumbnail image", quality); @@ -191,8 +177,8 @@ bool ExynosJpegEncoderForCamera::EnsureFormatIsApplied() { } getSize(&width, &height); - if (!GetCompressor().SetImageFormat( - getColorFormat(), width, height, thumb_width, thumb_height)) + if (!GetCompressor().SetImageFormat(getColorFormat(), width, height, thumb_width, + thumb_height)) return false; ClearState(STATE_PIXFMT_CHANGED | STATE_SIZE_CHANGED | STATE_THUMBSIZE_CHANGED); @@ -201,8 +187,7 @@ bool ExynosJpegEncoderForCamera::EnsureFormatIsApplied() { return true; } -size_t ExynosJpegEncoderForCamera::RemoveTrailingDummies(char *base, size_t len) -{ +size_t ExynosJpegEncoderForCamera::RemoveTrailingDummies(char *base, size_t len) { ALOG_ASSERT(len > 4); ALOG_ASSERT((base[0] == 0xFF) && (base[1] == 0xD8)); // SOI marker @@ -222,18 +207,15 @@ size_t ExynosJpegEncoderForCamera::RemoveTrailingDummies(char *base, size_t len) return 0; } -void *ExynosJpegEncoderForCamera::tCompressThumbnail(void *p) -{ +void *ExynosJpegEncoderForCamera::tCompressThumbnail(void *p) { ExynosJpegEncoderForCamera *encoder = reinterpret_cast<ExynosJpegEncoderForCamera *>(p); size_t thumblen = encoder->CompressThumbnail(); return reinterpret_cast<void *>(thumblen); } -bool ExynosJpegEncoderForCamera::ProcessExif(char *base, size_t limit, - exif_attribute_t *exifInfo, - extra_appinfo_t *extra) -{ +bool ExynosJpegEncoderForCamera::ProcessExif(char *base, size_t limit, exif_attribute_t *exifInfo, + extra_appinfo_t *extra) { // PREREQUISITES: The main and the thumbnail image size should be configured before. // Sanity chck @@ -244,16 +226,16 @@ bool ExynosJpegEncoderForCamera::ProcessExif(char *base, size_t limit, if (exifInfo) { if ((exifInfo->width != width) || (exifInfo->height != height)) { - ALOGE("Inconsistant image dimension: Exif %dx%d, Thumb %dx%d", - exifInfo->width, exifInfo->height, width, height); + ALOGE("Inconsistent image dimension: Exif %dx%d, Thumb %dx%d", exifInfo->width, + exifInfo->height, width, height); return false; } if (exifInfo->enableThumb) { if ((exifInfo->widthThumb != static_cast<uint32_t>(m_nThumbWidth)) || - (exifInfo->heightThumb != static_cast<uint32_t>(m_nThumbHeight))) { - ALOGE("Inconsistant thumbnail information: Exif %dx%d, Thumb %dx%d", - exifInfo->widthThumb, exifInfo->heightThumb, m_nThumbWidth, m_nThumbHeight); + (exifInfo->heightThumb != static_cast<uint32_t>(m_nThumbHeight))) { + ALOGE("Inconsistent thumbnail information: Exif %dx%d, Thumb %dx%d", + exifInfo->widthThumb, exifInfo->heightThumb, m_nThumbWidth, m_nThumbHeight); return false; } } @@ -262,8 +244,7 @@ bool ExynosJpegEncoderForCamera::ProcessExif(char *base, size_t limit, // Giving appwriter the address beyond SOS marker // because it is handled by this class size_t align = 16; - if (!!(GetDeviceCapabilities() & V4L2_CAP_EXYNOS_JPEG_NO_STREAMBASE_ALIGN)) - align = 1; + if (!!(GetDeviceCapabilities() & V4L2_CAP_EXYNOS_JPEG_NO_STREAMBASE_ALIGN)) align = 1; m_pAppWriter->PrepareAppWriter(base + JPEG_MARKER_SIZE, exifInfo, extra); @@ -287,24 +268,21 @@ bool ExynosJpegEncoderForCamera::ProcessExif(char *base, size_t limit, if (!exifInfo || !exifInfo->enableThumb || (limit < (JPEG_MAX_SEGMENT_SIZE * 10))) reserve_thumbspace = false; - m_pAppWriter->Write(reserve_thumbspace, JPEG_MARKER_SIZE, align, - TestState(STATE_HWFC_ENABLED)); + m_pAppWriter->Write(reserve_thumbspace, JPEG_MARKER_SIZE, align, TestState(STATE_HWFC_ENABLED)); ALOGD("Image compression starts from offset %zu (APPx size %zu, HWFC? %d, NBTB? %d)", - PTR_DIFF(base, m_pAppWriter->GetMainStreamBase()), m_pAppWriter->CalculateAPPSize(), - TestState(STATE_HWFC_ENABLED),TestState(STATE_NO_BTBCOMP)); + PTR_DIFF(base, m_pAppWriter->GetMainStreamBase()), m_pAppWriter->CalculateAPPSize(), + TestState(STATE_HWFC_ENABLED), TestState(STATE_NO_BTBCOMP)); return true; } -bool ExynosJpegEncoderForCamera::PrepareCompression(bool thumbnail) -{ - if (!thumbnail) - return true; +bool ExynosJpegEncoderForCamera::PrepareCompression(bool thumbnail) { + if (!thumbnail) return true; if (IsThumbGenerationNeeded()) { - if (pthread_create(&m_threadWorker, NULL, - tCompressThumbnail, reinterpret_cast<void *>(this)) != 0) { + if (pthread_create(&m_threadWorker, NULL, tCompressThumbnail, + reinterpret_cast<void *>(this)) != 0) { ALOGERR("Failed to create thumbnail generation thread"); return false; } @@ -319,14 +297,14 @@ bool ExynosJpegEncoderForCamera::PrepareCompression(bool thumbnail) if (!TestState(STATE_NO_BTBCOMP) && IsBTBCompressionSupported()) { if (checkOutBufType() == JPEG_BUF_TYPE_USER_PTR) { if (!GetCompressor().SetJpegBuffer2(m_pIONThumbJpegBuffer, m_szIONThumbJpegBuffer)) { - ALOGE("Failed to configure thumbnail buffer @ %p(size %zu)", - m_pIONThumbJpegBuffer, m_szIONThumbJpegBuffer); + ALOGE("Failed to configure thumbnail buffer @ %p(size %zu)", m_pIONThumbJpegBuffer, + m_szIONThumbJpegBuffer); return false; } } else { if (!GetCompressor().SetJpegBuffer2(m_fdIONThumbJpegBuffer, m_szIONThumbJpegBuffer)) { - ALOGE("Failed to configure thumbnail buffer @ %d(size %zu)", - m_fdIONThumbJpegBuffer, m_szIONThumbJpegBuffer); + ALOGE("Failed to configure thumbnail buffer @ %d(size %zu)", m_fdIONThumbJpegBuffer, + m_szIONThumbJpegBuffer); return false; } } @@ -335,16 +313,13 @@ bool ExynosJpegEncoderForCamera::PrepareCompression(bool thumbnail) return true; } -int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, - char** pcJpegBuffer, debug_attribute_t *debugInfo) -{ +int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, char **pcJpegBuffer, + debug_attribute_t *debugInfo) { return encode(size, exifInfo, -1, pcJpegBuffer, debugInfo); } -int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, - int fdJpegBuffer, char** pcJpegBuffer, - debug_attribute_t *debugInfo) -{ +int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, int fdJpegBuffer, + char **pcJpegBuffer, debug_attribute_t *debugInfo) { if ((!debugInfo) || (debugInfo->num_of_appmarker == 0)) { extra_appinfo_t *extra = NULL; return encode(size, exifInfo, fdJpegBuffer, pcJpegBuffer, extra); @@ -358,10 +333,8 @@ int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, return encode(size, exifInfo, fdJpegBuffer, pcJpegBuffer, &m_extraInfo); } -int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, - int fdJpegBuffer, char** pcJpegBuffer, - extra_appinfo_t *appInfo) -{ +int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, int fdJpegBuffer, + char **pcJpegBuffer, extra_appinfo_t *appInfo) { if (!(*pcJpegBuffer)) { ALOGE("Target stream buffer is not specified"); return -1; @@ -378,27 +351,26 @@ int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, char *jpeg_base = m_pStreamBase; ALOGI_IF(!exifInfo, "Exif is not specified. Skipping writing APP1 marker"); - ALOGI_IF(!appInfo, - "Debugging information is not specified. Skipping writing APP4 marker"); + ALOGI_IF(!appInfo, "Debugging information is not specified. Skipping writing APP4 marker"); ALOGD("Given stream buffer size: %d bytes", *size); CStopWatch stopwatch(true); - if (!ProcessExif(jpeg_base, m_nStreamSize, exifInfo, appInfo)) - return -1; + if (!ProcessExif(jpeg_base, m_nStreamSize, exifInfo, appInfo)) return -1; int offset = PTR_DIFF(m_pStreamBase, m_pAppWriter->GetMainStreamBase()); int buffsize = static_cast<int>(m_nStreamSize - offset); - if ((fdJpegBuffer < 0) || !(GetDeviceCapabilities() & V4L2_CAP_EXYNOS_JPEG_DMABUF_OFFSET)) { // JPEG_BUF_TYPE_USER_PTR + if ((fdJpegBuffer < 0) || + !(GetDeviceCapabilities() & V4L2_CAP_EXYNOS_JPEG_DMABUF_OFFSET)) { // JPEG_BUF_TYPE_USER_PTR if (setOutBuf(m_pAppWriter->GetMainStreamBase(), buffsize) < 0) { - ALOGE("Failed to configure stream buffer : fd %d, addr %p, streamSize %d", - fdJpegBuffer, m_pAppWriter->GetMainStreamBase(), buffsize); + ALOGE("Failed to configure stream buffer : fd %d, addr %p, streamSize %d", fdJpegBuffer, + m_pAppWriter->GetMainStreamBase(), buffsize); return -1; } } else { // JPEG_BUF_TYPE_DMA_BUF if (setOutBuf(fdJpegBuffer, buffsize, offset) < 0) { - ALOGE("Failed to configure stream buffer : fd %d, addr %p, streamSize %d", - fdJpegBuffer, m_pAppWriter->GetMainStreamBase(), buffsize); + ALOGE("Failed to configure stream buffer : fd %d, addr %p, streamSize %d", fdJpegBuffer, + m_pAppWriter->GetMainStreamBase(), buffsize); return -1; } } @@ -419,10 +391,10 @@ int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, // CASE1 = thumbenc && IsThumbGenerationNeeded() && block_mode // CASE2 = thumbenc && IsThumbGenerationNeeded() && !block_mode // CASE3 = thumbenc && !IsThumbGenerationNeeded() && !IsBTBCompressionSupported() && !block_mode - // CASE4 = thumbenc && !IsThumbGenerationNeeded() && !STATE_NO_BTBCOMP && IsBTBCompressionSupported() && !block_mode - // CASE5 = thumbenc && !IsThumbGenerationNeeded() && !STATE_NO_BTBCOMP && IsBTBCompressionSupported() && block_mode - // CASE6 = !thumbenc - // CASE7 = thumbenc && !IsThumbGenerationNeeded() && STATE_NO_BTBCOMP && block_mode + // CASE4 = thumbenc && !IsThumbGenerationNeeded() && !STATE_NO_BTBCOMP && + // IsBTBCompressionSupported() && !block_mode CASE5 = thumbenc && !IsThumbGenerationNeeded() && + // !STATE_NO_BTBCOMP && IsBTBCompressionSupported() && block_mode CASE6 = !thumbenc CASE7 = + // thumbenc && !IsThumbGenerationNeeded() && STATE_NO_BTBCOMP && block_mode if (!thumbenc) { // Confirm that no thumbnail information is transferred to HWJPEG @@ -430,7 +402,7 @@ int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, } else if (!IsThumbGenerationNeeded() && IsBTBCompressionSupported() && (m_fThumbBufferType != checkInBufType())) { ALOGE("Buffer types of thumbnail(%d) and main(%d) images should be the same", - m_fThumbBufferType, checkInBufType()); + m_fThumbBufferType, checkInBufType()); return -1; } else if (!IsThumbGenerationNeeded() && (m_fThumbBufferType == 0)) { // Thumbnail buffer configuration failed but the client forces to compress with thumbnail @@ -460,17 +432,14 @@ int ExynosJpegEncoderForCamera::encode(int *size, exif_attribute_t *exifInfo, } *size = static_cast<int>(FinishCompression(mainlen, thumblen)); - if (*size < 0) - return -1; + if (*size < 0) return -1; - ALOGD("....compression delay(usec.): HW %u, Total %lu)", - GetHWDelay(), stopwatch.GetElapsed()); + ALOGD("....compression delay(usec.): HW %u, Total %lu)", GetHWDelay(), stopwatch.GetElapsed()); return 0; } -ssize_t ExynosJpegEncoderForCamera::FinishCompression(size_t mainlen, size_t thumblen) -{ +ssize_t ExynosJpegEncoderForCamera::FinishCompression(size_t mainlen, size_t thumblen) { bool btb = false; size_t max_streamsize = m_nStreamSize; char *mainbase = m_pAppWriter->GetMainStreamBase(); @@ -498,33 +467,37 @@ ssize_t ExynosJpegEncoderForCamera::FinishCompression(size_t mainlen, size_t thu thumblen = reinterpret_cast<size_t>(len); } else if (TestState(STATE_NO_BTBCOMP) || !IsBTBCompressionSupported()) { - thumblen = CompressThumbnailOnly(m_pAppWriter->GetMaxThumbnailSize(), m_nThumbQuality, getColorFormat(), checkInBufType()); + thumblen = CompressThumbnailOnly(m_pAppWriter->GetMaxThumbnailSize(), m_nThumbQuality, + getColorFormat(), checkInBufType()); } else { btb = true; } - size_t max_thumb = min(m_pAppWriter->GetMaxThumbnailSize(), max_streamsize - m_pAppWriter->CalculateAPPSize(0) - mainlen); + size_t max_thumb = min(m_pAppWriter->GetMaxThumbnailSize(), + max_streamsize - m_pAppWriter->CalculateAPPSize(0) - mainlen); if (thumblen > max_thumb) { ALOGI("Too large thumbnail (%dx%d) stream size %zu (max: %zu, quality factor %d)", m_nThumbWidth, m_nThumbHeight, thumblen, max_thumb, m_nThumbQuality); ALOGI("Retrying thumbnail compression with quality factor 50"); thumblen = CompressThumbnailOnly(max_thumb, 50, getColorFormat(), checkInBufType()); - if (thumblen == 0) - return -1; + if (thumblen == 0) return -1; } if (!m_pAppWriter->IsThumbSpaceReserved()) { if (PTR_TO_ULONG(m_pStreamBase + max_streamsize) < - PTR_TO_ULONG(mainbase + mainlen + thumblen - JPEG_MARKER_SIZE)) { + PTR_TO_ULONG(mainbase + mainlen + thumblen - JPEG_MARKER_SIZE)) { ALOGE("Too small JPEG buffer length %zu (APP %zu, Main %zu, Thumb %zu)", max_streamsize, m_pAppWriter->CalculateAPPSize(thumblen), mainlen, thumblen); return -1; } - // the SOI of the stream of the main image is stored after the APP4 or APP11 segment if they exist. + // the SOI of the stream of the main image is stored after the APP4 or APP11 segment if + // they exist. memmove(m_pAppWriter->GetApp1End() + thumblen, m_pAppWriter->GetApp1End(), - mainlen + PTR_DIFF(m_pAppWriter->GetApp1End(), m_pAppWriter->GetMainStreamBase())); + mainlen + + PTR_DIFF(m_pAppWriter->GetApp1End(), + m_pAppWriter->GetMainStreamBase())); m_pAppWriter->UpdateApp1Size(thumblen); // m_nAppLength has the value of appwriter.GetExactAPPSize() @@ -539,7 +512,8 @@ ssize_t ExynosJpegEncoderForCamera::FinishCompression(size_t mainlen, size_t thu if (m_pAppWriter->IsThumbSpaceReserved()) { // clear the possible stale data in the dummy area after the thumbnail stream memset(m_pAppWriter->GetThumbStreamBase() + thumblen, 0, - m_pAppWriter->GetMaxThumbnailSize() - thumblen + m_pAppWriter->GetAPP1ResrevedSize()); + m_pAppWriter->GetMaxThumbnailSize() - thumblen + + m_pAppWriter->GetAPP1ResrevedSize()); } } else { thumblen = 0; @@ -553,8 +527,8 @@ ssize_t ExynosJpegEncoderForCamera::FinishCompression(size_t mainlen, size_t thu * Note that 2 byte(size of SOI marker) is included in APP1 segment size. * Thus the size of SOI marker in front of the stream is not added. */ - ALOGD("Completed image compression (%zd(thumb %zu) bytes, HWFC? %d, BTB? %d)", - mainlen, thumblen, TestState(STATE_HWFC_ENABLED), btb); + ALOGD("Completed image compression (%zd(thumb %zu) bytes, HWFC? %d, BTB? %d)", mainlen, + thumblen, TestState(STATE_HWFC_ENABLED), btb); m_pStreamBase[0] = 0xFF; m_pStreamBase[1] = 0xD8; @@ -563,21 +537,17 @@ ssize_t ExynosJpegEncoderForCamera::FinishCompression(size_t mainlen, size_t thu } /* The logic in WaitForHWFC() is the same with encode() */ -ssize_t ExynosJpegEncoderForCamera::WaitForCompression() -{ - if (!TestState(STATE_HWFC_ENABLED)) - return m_nStreamSize; +ssize_t ExynosJpegEncoderForCamera::WaitForCompression() { + if (!TestState(STATE_HWFC_ENABLED)) return m_nStreamSize; size_t thumblen = 0; ssize_t streamlen = GetCompressor().WaitForCompression(&thumblen); - if (streamlen < 0) - return streamlen; + if (streamlen < 0) return streamlen; return FinishCompression(streamlen, thumblen); } -bool ExynosJpegEncoderForCamera::GenerateThumbnailImage() -{ +bool ExynosJpegEncoderForCamera::GenerateThumbnailImage() { int main_width, main_height; if (getSize(&main_width, &main_height) < 0) { ALOGE("Failed to get main image size"); @@ -586,11 +556,10 @@ bool ExynosJpegEncoderForCamera::GenerateThumbnailImage() int v4l2Format = getColorFormat(); - if (!AllocThumbBuffer(v4l2Format)) - return false; + if (!AllocThumbBuffer(v4l2Format)) return false; - ALOGD("Generating thumbnail image: %dx%d -> %dx%d", - main_width, main_height, m_nThumbWidth, m_nThumbHeight); + ALOGD("Generating thumbnail image: %dx%d -> %dx%d", main_width, main_height, m_nThumbWidth, + m_nThumbHeight); if (!mThumbnailScaler) { ALOGE("Thumbnail scaler is not prepared"); @@ -602,8 +571,8 @@ bool ExynosJpegEncoderForCamera::GenerateThumbnailImage() return false; } - - if (!mThumbnailScaler->SetDstImage(m_nThumbWidth, m_nThumbHeight, GetThumbnailFormat(v4l2Format))) { + if (!mThumbnailScaler->SetDstImage(m_nThumbWidth, m_nThumbHeight, + GetThumbnailFormat(v4l2Format))) { ALOGE("Failed to configure the target image to the thumbnail scaler"); return false; } @@ -619,7 +588,8 @@ bool ExynosJpegEncoderForCamera::GenerateThumbnailImage() return false; } - okay = mThumbnailScaler->RunStream(bufs, len_srcbufs, m_fdIONThumbImgBuffer, m_szIONThumbImgBuffer); + okay = mThumbnailScaler->RunStream(bufs, len_srcbufs, m_fdIONThumbImgBuffer, + m_szIONThumbImgBuffer); } else { // mainbuftype == JPEG_BUF_TYPE_DMA_BUF int bufs[ThumbnailScaler::SCALER_MAX_PLANES]; int len_srcbufs[ThumbnailScaler::SCALER_MAX_PLANES]; @@ -628,7 +598,8 @@ bool ExynosJpegEncoderForCamera::GenerateThumbnailImage() ALOGE("Failed to retrieve the main image buffers"); return false; } - okay = mThumbnailScaler->RunStream(bufs, len_srcbufs, m_fdIONThumbImgBuffer, m_szIONThumbImgBuffer); + okay = mThumbnailScaler->RunStream(bufs, len_srcbufs, m_fdIONThumbImgBuffer, + m_szIONThumbImgBuffer); } if (!okay) { @@ -639,14 +610,12 @@ bool ExynosJpegEncoderForCamera::GenerateThumbnailImage() return true; } -size_t ExynosJpegEncoderForCamera::CompressThumbnail() -{ +size_t ExynosJpegEncoderForCamera::CompressThumbnail() { unsigned int v4l2Format = getColorFormat(); int buftype = checkInBufType(); if (IsThumbGenerationNeeded()) { - if (!GenerateThumbnailImage()) - return 0; + if (!GenerateThumbnailImage()) return 0; // libcsc output configured by this class is always NV21. v4l2Format = GetThumbnailFormat(getColorFormat()); @@ -657,11 +626,11 @@ size_t ExynosJpegEncoderForCamera::CompressThumbnail() m_szThumbnailImageLen[0] = m_szIONThumbImgBuffer; } - return CompressThumbnailOnly(m_pAppWriter->GetMaxThumbnailSize(), m_nThumbQuality, v4l2Format, buftype); + return CompressThumbnailOnly(m_pAppWriter->GetMaxThumbnailSize(), m_nThumbQuality, v4l2Format, + buftype); } -bool ExynosJpegEncoderForCamera::AllocThumbBuffer(int v4l2Format) -{ +bool ExynosJpegEncoderForCamera::AllocThumbBuffer(int v4l2Format) { if (m_fdIONClient < 0) { ALOGE("ION client is not created"); return false; @@ -674,11 +643,9 @@ bool ExynosJpegEncoderForCamera::AllocThumbBuffer(int v4l2Format) } if (m_fdIONThumbImgBuffer >= 0) { - if (m_szIONThumbImgBuffer >= thumbbufsize) - return true; + if (m_szIONThumbImgBuffer >= thumbbufsize) return true; - if (m_pIONThumbImgBuffer != NULL) - munmap(m_pIONThumbImgBuffer, m_szIONThumbImgBuffer); + if (m_pIONThumbImgBuffer != NULL) munmap(m_pIONThumbImgBuffer, m_szIONThumbImgBuffer); close(m_fdIONThumbImgBuffer); @@ -687,9 +654,11 @@ bool ExynosJpegEncoderForCamera::AllocThumbBuffer(int v4l2Format) m_szIONThumbImgBuffer = 0; } - m_fdIONThumbImgBuffer = exynos_ion_alloc(m_fdIONClient, thumbbufsize, EXYNOS_ION_HEAP_SYSTEM_MASK, 0); + m_fdIONThumbImgBuffer = + exynos_ion_alloc(m_fdIONClient, thumbbufsize, EXYNOS_ION_HEAP_SYSTEM_MASK, 0); if (m_fdIONThumbImgBuffer < 0) { - ALOGERR("Failed to allocate %zu bytes for NV12 %ux%u", thumbbufsize, m_nThumbHeight, m_nThumbWidth); + ALOGERR("Failed to allocate %zu bytes for NV12 %ux%u", thumbbufsize, m_nThumbHeight, + m_nThumbWidth); m_fdIONThumbImgBuffer = -1; return false; } @@ -699,8 +668,7 @@ bool ExynosJpegEncoderForCamera::AllocThumbBuffer(int v4l2Format) return AllocThumbJpegBuffer(); } -bool ExynosJpegEncoderForCamera::AllocThumbJpegBuffer() -{ +bool ExynosJpegEncoderForCamera::AllocThumbJpegBuffer() { if (m_fdIONClient < 0) { ALOGE("ION client is not created"); return false; @@ -709,8 +677,7 @@ bool ExynosJpegEncoderForCamera::AllocThumbJpegBuffer() size_t thumbbufsize = m_nThumbHeight * m_nThumbWidth * 3; if (m_pIONThumbJpegBuffer) { - if (m_szIONThumbJpegBuffer >= thumbbufsize) - return true; + if (m_szIONThumbJpegBuffer >= thumbbufsize) return true; munmap(m_pIONThumbJpegBuffer, m_szIONThumbJpegBuffer); close(m_fdIONThumbJpegBuffer); @@ -720,16 +687,18 @@ bool ExynosJpegEncoderForCamera::AllocThumbJpegBuffer() m_fdIONThumbJpegBuffer = -1; } - m_fdIONThumbJpegBuffer = exynos_ion_alloc(m_fdIONClient, thumbbufsize, EXYNOS_ION_HEAP_SYSTEM_MASK, - ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC); + m_fdIONThumbJpegBuffer = + exynos_ion_alloc(m_fdIONClient, thumbbufsize, EXYNOS_ION_HEAP_SYSTEM_MASK, + ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC); if (m_fdIONThumbJpegBuffer < 0) { - ALOGERR("Failed to allocate %zu bytes for thumbnail stream buffer of %ux%u", - thumbbufsize, m_nThumbHeight, m_nThumbWidth); + ALOGERR("Failed to allocate %zu bytes for thumbnail stream buffer of %ux%u", thumbbufsize, + m_nThumbHeight, m_nThumbWidth); return false; } - m_pIONThumbJpegBuffer = reinterpret_cast<char *>( - mmap(NULL, thumbbufsize, PROT_READ | PROT_WRITE, MAP_SHARED, m_fdIONThumbJpegBuffer, 0)); + m_pIONThumbJpegBuffer = + reinterpret_cast<char *>(mmap(NULL, thumbbufsize, PROT_READ | PROT_WRITE, MAP_SHARED, + m_fdIONThumbJpegBuffer, 0)); if (m_pIONThumbJpegBuffer == MAP_FAILED) { ALOGERR("Failed to map thumbnail stream buffer (%zu bytes)", thumbbufsize); @@ -742,11 +711,10 @@ bool ExynosJpegEncoderForCamera::AllocThumbJpegBuffer() } size_t ExynosJpegEncoderForCamera::CompressThumbnailOnly(size_t limit, int quality, - unsigned int v4l2Format, int src_buftype) -{ + unsigned int v4l2Format, int src_buftype) { if (!m_phwjpeg4thumb->SetImageFormat(v4l2Format, m_nThumbWidth, m_nThumbHeight)) { - ALOGE("Failed to configure thumbnail source image format to %#010x, %ux%u", - v4l2Format, m_nThumbWidth, m_nThumbHeight); + ALOGE("Failed to configure thumbnail source image format to %#010x, %ux%u", v4l2Format, + m_nThumbWidth, m_nThumbHeight); return 0; } @@ -761,18 +729,18 @@ size_t ExynosJpegEncoderForCamera::CompressThumbnailOnly(size_t limit, int quali [[fallthrough]]; case V4L2_PIX_FMT_NV21M: num_buffers++; - break; + break; } if (src_buftype == JPEG_BUF_TYPE_USER_PTR) { - if (!m_phwjpeg4thumb->SetImageBuffer(m_pThumbnailImageBuffer, - m_szThumbnailImageLen, num_buffers)) { + if (!m_phwjpeg4thumb->SetImageBuffer(m_pThumbnailImageBuffer, m_szThumbnailImageLen, + num_buffers)) { ALOGE("Failed to configure thumbnail buffers(userptr) for thumbnail"); return 0; } } else { // JPEG_BUF_TYPE_DMA_BUF - if (!m_phwjpeg4thumb->SetImageBuffer(m_fdThumbnailImageBuffer, - m_szThumbnailImageLen, num_buffers)) { + if (!m_phwjpeg4thumb->SetImageBuffer(m_fdThumbnailImageBuffer, m_szThumbnailImageLen, + num_buffers)) { ALOGE("Failed to configure thumbnail buffers(dmabuf) for thumbnail"); return 0; } @@ -780,7 +748,7 @@ size_t ExynosJpegEncoderForCamera::CompressThumbnailOnly(size_t limit, int quali if (!m_phwjpeg4thumb->SetJpegBuffer(m_fdIONThumbJpegBuffer, m_szIONThumbJpegBuffer)) { ALOGE("Failed to configure thumbnail stream buffer (fd %d, size %zu)", - m_fdIONThumbJpegBuffer, m_szIONThumbJpegBuffer); + m_fdIONThumbJpegBuffer, m_szIONThumbJpegBuffer); return 0; } @@ -818,12 +786,10 @@ size_t ExynosJpegEncoderForCamera::CompressThumbnailOnly(size_t limit, int quali return 0; } -int ExynosJpegEncoderForCamera::setInBuf2(int *piBuf, int *iSize) -{ +int ExynosJpegEncoderForCamera::setInBuf2(int *piBuf, int *iSize) { NoThumbGenerationNeeded(); - if (!EnsureFormatIsApplied()) - return -1; + if (!EnsureFormatIsApplied()) return -1; CHWJpegCompressor &hwjpeg = GetCompressor(); unsigned int num_buffers = 3; @@ -838,7 +804,7 @@ int ExynosJpegEncoderForCamera::setInBuf2(int *piBuf, int *iSize) } if (IsBTBCompressionSupported() && - !hwjpeg.SetImageBuffer2(m_fdThumbnailImageBuffer, m_szThumbnailImageLen, num_buffers)) { + !hwjpeg.SetImageBuffer2(m_fdThumbnailImageBuffer, m_szThumbnailImageLen, num_buffers)) { ALOGE("Failed to configure thumbnail buffers"); return -1; } @@ -848,12 +814,10 @@ int ExynosJpegEncoderForCamera::setInBuf2(int *piBuf, int *iSize) return 0; } -int ExynosJpegEncoderForCamera::setInBuf2(char **pcBuf, int *iSize) -{ +int ExynosJpegEncoderForCamera::setInBuf2(char **pcBuf, int *iSize) { NoThumbGenerationNeeded(); - if (!EnsureFormatIsApplied()) - return -1; + if (!EnsureFormatIsApplied()) return -1; CHWJpegCompressor &hwjpeg = GetCompressor(); unsigned int num_buffers = 3; @@ -868,7 +832,7 @@ int ExynosJpegEncoderForCamera::setInBuf2(char **pcBuf, int *iSize) } if (IsBTBCompressionSupported() && - !hwjpeg.SetImageBuffer2(m_pThumbnailImageBuffer, m_szThumbnailImageLen, num_buffers)) { + !hwjpeg.SetImageBuffer2(m_pThumbnailImageBuffer, m_szThumbnailImageLen, num_buffers)) { ALOGE("Failed to configure thumbnail buffers"); return -1; } @@ -878,8 +842,7 @@ int ExynosJpegEncoderForCamera::setInBuf2(char **pcBuf, int *iSize) return 0; } -size_t ExynosJpegEncoderForCamera::GetThumbnailImage(char *buffer, size_t buflen) -{ +size_t ExynosJpegEncoderForCamera::GetThumbnailImage(char *buffer, size_t buflen) { if (m_fdIONThumbImgBuffer < 0) { ALOGE("No internal thumbnail buffer is allocated"); return 0; @@ -895,8 +858,8 @@ size_t ExynosJpegEncoderForCamera::GetThumbnailImage(char *buffer, size_t buflen "m_szIONThumbImgBuffer(%zu) is smaller than the thumbnail (%zu)", m_szIONThumbImgBuffer, thumbbufsize); if (m_pIONThumbImgBuffer == NULL) { - m_pIONThumbImgBuffer = reinterpret_cast<char *>(mmap( - NULL, m_szIONThumbImgBuffer, PROT_READ, MAP_SHARED, m_fdIONThumbImgBuffer, 0)); + m_pIONThumbImgBuffer = reinterpret_cast<char *>( + mmap(NULL, m_szIONThumbImgBuffer, PROT_READ, MAP_SHARED, m_fdIONThumbImgBuffer, 0)); if (m_pIONThumbImgBuffer == MAP_FAILED) { m_pIONThumbImgBuffer = NULL; ALOGERR("Failed to map thumbnail image buffer (%zu bytes)", m_szIONThumbImgBuffer); @@ -911,8 +874,7 @@ size_t ExynosJpegEncoderForCamera::GetThumbnailImage(char *buffer, size_t buflen return m_szIONThumbImgBuffer; } -int ExynosJpegEncoderForCamera::destroy() -{ +int ExynosJpegEncoderForCamera::destroy() { GetCompressor().Release(); return 0; } diff --git a/libhwjpeg/IFDWriter.h b/libhwjpeg/IFDWriter.h index 33f7ff8..61e6d17 100644 --- a/libhwjpeg/IFDWriter.h +++ b/libhwjpeg/IFDWriter.h @@ -21,6 +21,7 @@ class CEndianessChecker { bool __little; + public: CEndianessChecker(); operator bool() { return __little; } @@ -30,8 +31,7 @@ extern CEndianessChecker __LITTLE_ENDIAN__; #endif template <typename T> -char *WriteDataInBig(char *p, T val) -{ +char *WriteDataInBig(char *p, T val) { if (sizeof(val) == 1) { *p++ = val; } else if (__LITTLE_ENDIAN__) { @@ -50,7 +50,7 @@ char *WriteDataInBig(char *p, T val) } else { switch (sizeof(val)) { case 2: - *p++ = static_cast<char>(val & 0xFF); + *p++ = static_cast<char>(val & 0xFF); *p++ = static_cast<char>((val >> 8) & 0xFF); break; case 4: @@ -66,11 +66,9 @@ char *WriteDataInBig(char *p, T val) } template <typename T> -char *WriteData(char *p, T val) -{ +char *WriteData(char *p, T val) { const char *pt = reinterpret_cast<char *>(&val); - for (size_t i = 0; i < sizeof(val); i++) - *p++ = *pt++; + for (size_t i = 0; i < sizeof(val); i++) *p++ = *pt++; return p; } @@ -107,13 +105,14 @@ class CIFDWriter { m_nTags--; } + public: CIFDWriter(char *offset_base, char *ifdbase, uint16_t tagcount) { m_nTags = tagcount; m_pBase = offset_base; m_pIFDBase = ifdbase; - m_pValue = m_pIFDBase + IFD_FIELDCOUNT_SIZE + - IFD_FIELD_SIZE * tagcount + IFD_NEXTIFDOFFSET_SIZE; + m_pValue = m_pIFDBase + IFD_FIELDCOUNT_SIZE + IFD_FIELD_SIZE * tagcount + + IFD_NEXTIFDOFFSET_SIZE; // COUNT field of IFD const char *pval = reinterpret_cast<char *>(&m_nTags); @@ -136,8 +135,7 @@ public: *m_pValue++ = static_cast<char>(value[i]); } } else { - for (uint32_t i = 0; i < count; i++) - *m_pIFDBase++ = static_cast<char>(value[i]); + for (uint32_t i = 0; i < count; i++) *m_pIFDBase++ = static_cast<char>(value[i]); m_pIFDBase += IFD_VALOFF_SIZE - count; } } @@ -192,8 +190,7 @@ public: m_pValue[count - 1] = '\0'; m_pValue += count; } else { - for (uint32_t i = 0; i < count; i++) - *m_pIFDBase++ = value[i]; + for (uint32_t i = 0; i < count; i++) *m_pIFDBase++ = value[i]; *(m_pIFDBase - 1) = '\0'; m_pIFDBase += IFD_VALOFF_SIZE - count; } @@ -212,11 +209,9 @@ public: } else { uint32_t i; - for (i = 0; (i < (count - 1)) && (string[i] != '\0'); i++) - *m_pIFDBase++ = string[i]; + for (i = 0; (i < (count - 1)) && (string[i] != '\0'); i++) *m_pIFDBase++ = string[i]; - while (i++ < count) - *m_pIFDBase++ = '\0'; + while (i++ < count) *m_pIFDBase++ = '\0'; m_pIFDBase += IFD_VALOFF_SIZE - count; } @@ -250,8 +245,7 @@ public: m_pIFDBase = WriteOffset(m_pIFDBase, m_pValue); const char *pt = reinterpret_cast<const char *>(value); - for (uint32_t i = 0; i < sizeof(srational_t) * count; i++) - *m_pValue++ = *pt++; + for (uint32_t i = 0; i < sizeof(srational_t) * count; i++) *m_pValue++ = *pt++; } void WriteUndef(uint16_t tag, uint32_t count, const unsigned char *value) { @@ -263,8 +257,7 @@ public: memcpy(m_pValue, value, count); m_pValue += count; } else { - for (uint32_t i = 0; i < count; i++) - *m_pIFDBase++ = static_cast<char>(value[i]); + for (uint32_t i = 0; i < count; i++) *m_pIFDBase++ = static_cast<char>(value[i]); m_pIFDBase += IFD_VALOFF_SIZE - count; } } diff --git a/libhwjpeg/LibScalerForJpeg.cpp b/libhwjpeg/LibScalerForJpeg.cpp index 1165495..1886d49 100644 --- a/libhwjpeg/LibScalerForJpeg.cpp +++ b/libhwjpeg/LibScalerForJpeg.cpp @@ -15,67 +15,57 @@ * limitations under the License. */ -#include "hwjpeg-internal.h" #include "LibScalerForJpeg.h" +#include "hwjpeg-internal.h" + #define SCALER_DEV_NODE "/dev/video50" -static const char *getBufTypeString(unsigned int buftype) -{ - if (buftype == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) - return "destination"; - if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) - return "source"; +static const char *getBufTypeString(unsigned int buftype) { + if (buftype == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) return "destination"; + if (buftype == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) return "source"; return "unknown"; } -bool LibScalerForJpeg::RunStream(int srcBuf[SCALER_MAX_PLANES], int __unused srcLen[SCALER_MAX_PLANES], int dstBuf, size_t __unused dstLen) -{ - if (!mSrcImage.begin(V4L2_MEMORY_DMABUF) || !mDstImage.begin(V4L2_MEMORY_DMABUF)) - return false; +bool LibScalerForJpeg::RunStream(int srcBuf[SCALER_MAX_PLANES], + int __unused srcLen[SCALER_MAX_PLANES], int dstBuf, + size_t __unused dstLen) { + if (!mSrcImage.begin(V4L2_MEMORY_DMABUF) || !mDstImage.begin(V4L2_MEMORY_DMABUF)) return false; return queue(srcBuf, dstBuf); } -bool LibScalerForJpeg::RunStream(char *srcBuf[SCALER_MAX_PLANES], int __unused srcLen[SCALER_MAX_PLANES], int dstBuf, size_t __unused dstLen) -{ - if (!mSrcImage.begin(V4L2_MEMORY_USERPTR) || !mDstImage.begin(V4L2_MEMORY_DMABUF)) - return false; +bool LibScalerForJpeg::RunStream(char *srcBuf[SCALER_MAX_PLANES], + int __unused srcLen[SCALER_MAX_PLANES], int dstBuf, + size_t __unused dstLen) { + if (!mSrcImage.begin(V4L2_MEMORY_USERPTR) || !mDstImage.begin(V4L2_MEMORY_DMABUF)) return false; return queue(srcBuf, dstBuf); } -bool LibScalerForJpeg::Image::set(unsigned int width, unsigned int height, unsigned int format) -{ - if (same(width, height, format)) - return true; +bool LibScalerForJpeg::Image::set(unsigned int width, unsigned int height, unsigned int format) { + if (same(width, height, format)) return true; if (memoryType != 0) { - if (!mDevice.requestBuffers(bufferType, memoryType, 0)) - return false; + if (!mDevice.requestBuffers(bufferType, memoryType, 0)) return false; } - if (!mDevice.setFormat(bufferType, format, width, height, planeLen)) - return false; + if (!mDevice.setFormat(bufferType, format, width, height, planeLen)) return false; memoryType = 0; // new reqbufs is required. return true; } -bool LibScalerForJpeg::Image::begin(unsigned int memtype) -{ +bool LibScalerForJpeg::Image::begin(unsigned int memtype) { if (memoryType != memtype) { if (memoryType != 0) { - if (!mDevice.requestBuffers(bufferType, memoryType, 0)) - return false; + if (!mDevice.requestBuffers(bufferType, memoryType, 0)) return false; } - if (!mDevice.requestBuffers(bufferType, memtype, 1)) - return false; + if (!mDevice.requestBuffers(bufferType, memtype, 1)) return false; - if (!mDevice.streamOn(bufferType)) - return false; + if (!mDevice.streamOn(bufferType)) return false; memoryType = memtype; } @@ -83,41 +73,34 @@ bool LibScalerForJpeg::Image::begin(unsigned int memtype) return true; } -bool LibScalerForJpeg::Image::cancelBuffer() -{ - if (!mDevice.streamOff(bufferType)) - return false; +bool LibScalerForJpeg::Image::cancelBuffer() { + if (!mDevice.streamOff(bufferType)) return false; - if (!mDevice.streamOn(bufferType)) - return false; + if (!mDevice.streamOn(bufferType)) return false; return true; } -LibScalerForJpeg::Device::Device() -{ +LibScalerForJpeg::Device::Device() { mFd = ::open(SCALER_DEV_NODE, O_RDWR); - if (mFd < 0) - ALOGERR("failed to open %s", SCALER_DEV_NODE); + if (mFd < 0) ALOGERR("failed to open %s", SCALER_DEV_NODE); } -LibScalerForJpeg::Device::~Device() -{ - if (mFd >= 0) - ::close(mFd); +LibScalerForJpeg::Device::~Device() { + if (mFd >= 0) ::close(mFd); } -bool LibScalerForJpeg::Device::requestBuffers(unsigned int buftype, unsigned int memtype, unsigned int count) -{ - // count==0 means this port should be reconfigured and it is successful under streaming is finished. - if (!count) - streamOff(buftype); +bool LibScalerForJpeg::Device::requestBuffers(unsigned int buftype, unsigned int memtype, + unsigned int count) { + // count==0 means this port should be reconfigured and it is successful under streaming is + // finished. + if (!count) streamOff(buftype); v4l2_requestbuffers reqbufs{}; - reqbufs.type = buftype; - reqbufs.memory = memtype; - reqbufs.count = count; + reqbufs.type = buftype; + reqbufs.memory = memtype; + reqbufs.count = count; if (ioctl(mFd, VIDIOC_REQBUFS, &reqbufs) < 0) { ALOGERR("failed REQBUFS(%s, mem=%d, count=%d)", getBufTypeString(buftype), memtype, count); @@ -127,29 +110,30 @@ bool LibScalerForJpeg::Device::requestBuffers(unsigned int buftype, unsigned int return true; } -bool LibScalerForJpeg::Device::setFormat(unsigned int buftype, unsigned int format, unsigned int width, unsigned int height, unsigned int planelen[SCALER_MAX_PLANES]) -{ +bool LibScalerForJpeg::Device::setFormat(unsigned int buftype, unsigned int format, + unsigned int width, unsigned int height, + unsigned int planelen[SCALER_MAX_PLANES]) { v4l2_format fmt{}; fmt.type = buftype; fmt.fmt.pix_mp.pixelformat = format; - fmt.fmt.pix_mp.width = width; + fmt.fmt.pix_mp.width = width; fmt.fmt.pix_mp.height = height; if (ioctl(mFd, VIDIOC_S_FMT, &fmt) < 0) { - ALOGERR("failed S_FMT(%s, fmt=h'%x, %ux%u)", getBufTypeString(buftype), format, width, height); + ALOGERR("failed S_FMT(%s, fmt=h'%x, %ux%u)", getBufTypeString(buftype), format, width, + height); return false; } - for (uint32_t i = 0; i < fmt.fmt.pix_mp.num_planes ; i++) { + for (uint32_t i = 0; i < fmt.fmt.pix_mp.num_planes; i++) { planelen[i] = fmt.fmt.pix_mp.plane_fmt[i].sizeimage; } return true; } -bool LibScalerForJpeg::Device::streamOn(unsigned int buftype) -{ +bool LibScalerForJpeg::Device::streamOn(unsigned int buftype) { if (ioctl(mFd, VIDIOC_STREAMON, &buftype) < 0) { ALOGERR("failed STREAMON for %s", getBufTypeString(buftype)); return false; @@ -158,8 +142,7 @@ bool LibScalerForJpeg::Device::streamOn(unsigned int buftype) return true; } -bool LibScalerForJpeg::Device::streamOff(unsigned int buftype) -{ +bool LibScalerForJpeg::Device::streamOff(unsigned int buftype) { if (ioctl(mFd, VIDIOC_STREAMOFF, &buftype) < 0) { ALOGERR("failed STREAMOFF for %s", getBufTypeString(buftype)); return false; @@ -168,8 +151,8 @@ bool LibScalerForJpeg::Device::streamOff(unsigned int buftype) return true; } -bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, std::function<void(v4l2_buffer &)> bufferFiller) -{ +bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, + std::function<void(v4l2_buffer &)> bufferFiller) { v4l2_buffer buffer{}; v4l2_plane plane[SCALER_MAX_PLANES]; @@ -183,47 +166,50 @@ bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, std::function<v return ioctl(mFd, VIDIOC_QBUF, &buffer) >= 0; } -bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, int buf[SCALER_MAX_PLANES], unsigned int len[SCALER_MAX_PLANES]) -{ - if (!queueBuffer(buftype, [buf, len] (v4l2_buffer &buffer) { - buffer.memory = V4L2_MEMORY_DMABUF; - buffer.length = SCALER_MAX_PLANES; - for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) { - buffer.m.planes[i].m.fd = buf[i]; - buffer.m.planes[i].length = len[i]; - } })) { - ALOGERR("failed QBUF(%s, fd[]=%d %d, len[0]=%d %d)", getBufTypeString(buftype), buf[0], buf[1], len[0], len[1]); +bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, int buf[SCALER_MAX_PLANES], + unsigned int len[SCALER_MAX_PLANES]) { + if (!queueBuffer(buftype, [buf, len](v4l2_buffer &buffer) { + buffer.memory = V4L2_MEMORY_DMABUF; + buffer.length = SCALER_MAX_PLANES; + for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) { + buffer.m.planes[i].m.fd = buf[i]; + buffer.m.planes[i].length = len[i]; + } + })) { + ALOGERR("failed QBUF(%s, fd[]=%d %d, len[0]=%d %d)", getBufTypeString(buftype), buf[0], + buf[1], len[0], len[1]); return false; } return true; } -bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, char *buf[SCALER_MAX_PLANES], unsigned int len[SCALER_MAX_PLANES]) -{ - if (!queueBuffer(buftype, [buf, len] (v4l2_buffer &buffer) { - buffer.memory = V4L2_MEMORY_USERPTR; - buffer.length = SCALER_MAX_PLANES; - for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) { - buffer.m.planes[i].m.userptr = reinterpret_cast<unsigned long>(buf[i]); - buffer.m.planes[i].length = len[i]; - } })) { - ALOGERR("failed QBUF(%s, ptr[]=%p %p, len[0]=%d %d)", getBufTypeString(buftype), buf[0], buf[1], len[0], len[1]); +bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, char *buf[SCALER_MAX_PLANES], + unsigned int len[SCALER_MAX_PLANES]) { + if (!queueBuffer(buftype, [buf, len](v4l2_buffer &buffer) { + buffer.memory = V4L2_MEMORY_USERPTR; + buffer.length = SCALER_MAX_PLANES; + for (unsigned int i = 0; i < SCALER_MAX_PLANES; i++) { + buffer.m.planes[i].m.userptr = reinterpret_cast<unsigned long>(buf[i]); + buffer.m.planes[i].length = len[i]; + } + })) { + ALOGERR("failed QBUF(%s, ptr[]=%p %p, len[0]=%d %d)", getBufTypeString(buftype), buf[0], + buf[1], len[0], len[1]); return false; } return true; } -bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, int buf, unsigned int len[SCALER_MAX_PLANES]) -{ - if (!queueBuffer(buftype, [buf, len] (v4l2_buffer &buffer) - { - buffer.memory = V4L2_MEMORY_DMABUF; - buffer.length = 1; - buffer.m.planes[0].m.fd = buf; - buffer.m.planes[0].length = len[0]; - })) { +bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, int buf, + unsigned int len[SCALER_MAX_PLANES]) { + if (!queueBuffer(buftype, [buf, len](v4l2_buffer &buffer) { + buffer.memory = V4L2_MEMORY_DMABUF; + buffer.length = 1; + buffer.m.planes[0].m.fd = buf; + buffer.m.planes[0].length = len[0]; + })) { ALOGERR("failed QBUF(%s, fd=%d, len=%d", getBufTypeString(buftype), buf, len[0]); return false; } @@ -231,8 +217,7 @@ bool LibScalerForJpeg::Device::queueBuffer(unsigned int buftype, int buf, unsign return true; } -bool LibScalerForJpeg::Device::dequeueBuffer(unsigned int buftype, unsigned int memtype) -{ +bool LibScalerForJpeg::Device::dequeueBuffer(unsigned int buftype, unsigned int memtype) { v4l2_buffer buffer{}; v4l2_plane plane[SCALER_MAX_PLANES]; @@ -244,7 +229,7 @@ bool LibScalerForJpeg::Device::dequeueBuffer(unsigned int buftype, unsigned int buffer.m.planes = plane; - if (ioctl(mFd, VIDIOC_DQBUF, &buffer) < 0 ) { + if (ioctl(mFd, VIDIOC_DQBUF, &buffer) < 0) { ALOGERR("failed DQBUF(%s)", getBufTypeString(buftype)); return false; } diff --git a/libhwjpeg/LibScalerForJpeg.h b/libhwjpeg/LibScalerForJpeg.h index f8914d7..4c90e43 100644 --- a/libhwjpeg/LibScalerForJpeg.h +++ b/libhwjpeg/LibScalerForJpeg.h @@ -16,16 +16,16 @@ #ifndef __HARDWARE_EXYNOS_LIBSCALERFORJPEG_H__ #define __HARDWARE_EXYNOS_LIBSCALERFORJPEG_H__ -#include <functional> - #include <linux/videodev2.h> +#include <functional> + #include "ThumbnailScaler.h" class LibScalerForJpeg : public ThumbnailScaler { public: - LibScalerForJpeg() { } - ~LibScalerForJpeg() { } + LibScalerForJpeg() {} + ~LibScalerForJpeg() {} bool SetSrcImage(unsigned int width, unsigned int height, unsigned int v4l2_format) { return mSrcImage.set(width, height, v4l2_format); @@ -35,10 +35,13 @@ public: return mDstImage.set(width, height, v4l2_format); } - bool RunStream(int srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, size_t dstLen); - bool RunStream(char *srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, size_t dstLen); + bool RunStream(int srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, + size_t dstLen); + bool RunStream(char *srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, + size_t dstLen); bool available() { return mDevice.mFd >= 0; } + private: struct Device { int mFd; @@ -46,12 +49,15 @@ private: Device(); ~Device(); bool requestBuffers(unsigned int buftype, unsigned int memtype, unsigned int count); - bool setFormat(unsigned int buftype, unsigned int format, unsigned int width, unsigned int height, unsigned int planelen[SCALER_MAX_PLANES]); + bool setFormat(unsigned int buftype, unsigned int format, unsigned int width, + unsigned int height, unsigned int planelen[SCALER_MAX_PLANES]); bool streamOn(unsigned int buftype); bool streamOff(unsigned int buftype); bool queueBuffer(unsigned int buftype, std::function<void(v4l2_buffer &)> bufferFiller); - bool queueBuffer(unsigned int buftype, int buf[SCALER_MAX_PLANES], unsigned int len[SCALER_MAX_PLANES]); - bool queueBuffer(unsigned int buftype, char *buf[SCALER_MAX_PLANES], unsigned int len[SCALER_MAX_PLANES]); + bool queueBuffer(unsigned int buftype, int buf[SCALER_MAX_PLANES], + unsigned int len[SCALER_MAX_PLANES]); + bool queueBuffer(unsigned int buftype, char *buf[SCALER_MAX_PLANES], + unsigned int len[SCALER_MAX_PLANES]); bool queueBuffer(unsigned int buftype, int buf, unsigned int len[SCALER_MAX_PLANES]); bool dequeueBuffer(unsigned int buftype, unsigned int memtype); }; @@ -66,24 +72,26 @@ private: unsigned int planeLen[SCALER_MAX_PLANES]; Image(Device &dev, unsigned int w, unsigned int h, unsigned int f, unsigned int buftype) - : mDevice(dev), width(w), height(h), format(f), bufferType(buftype) - { } + : mDevice(dev), width(w), height(h), format(f), bufferType(buftype) {} bool set(unsigned int width, unsigned int height, unsigned int format); bool begin(unsigned int memtype); bool cancelBuffer(); template <class tBuf> - bool queueBuffer(tBuf buf) { return mDevice.queueBuffer(bufferType, buf, planeLen); } + bool queueBuffer(tBuf buf) { + return mDevice.queueBuffer(bufferType, buf, planeLen); + } bool dequeueBuffer() { return mDevice.dequeueBuffer(bufferType, memoryType); } - bool same(unsigned int w, unsigned int h, unsigned int f) { return width == w && height == h && format == f; } + bool same(unsigned int w, unsigned int h, unsigned int f) { + return width == w && height == h && format == f; + } }; - template<class T> + template <class T> bool queue(T srcBuf[SCALER_MAX_PLANES], int dstBuf) { - if (!mSrcImage.queueBuffer(srcBuf)) - return false; + if (!mSrcImage.queueBuffer(srcBuf)) return false; if (!mDstImage.queueBuffer(dstBuf)) { mSrcImage.cancelBuffer(); diff --git a/libhwjpeg/ThumbnailScaler.cpp b/libhwjpeg/ThumbnailScaler.cpp index 4d6e5de..3f4edcf 100644 --- a/libhwjpeg/ThumbnailScaler.cpp +++ b/libhwjpeg/ThumbnailScaler.cpp @@ -14,13 +14,13 @@ * limitations under the License. */ +#include "ThumbnailScaler.h" + #include <log/log.h> -#include "ThumbnailScaler.h" #include "LibScalerForJpeg.h" -ThumbnailScaler *ThumbnailScaler::createInstance() -{ +ThumbnailScaler *ThumbnailScaler::createInstance() { ALOGD("Created thumbnail scaler: legacy V4L2 Scaler"); return new LibScalerForJpeg(); } diff --git a/libhwjpeg/ThumbnailScaler.h b/libhwjpeg/ThumbnailScaler.h index cb23365..14d6e7d 100644 --- a/libhwjpeg/ThumbnailScaler.h +++ b/libhwjpeg/ThumbnailScaler.h @@ -6,14 +6,16 @@ class ThumbnailScaler { public: const static unsigned int SCALER_MAX_PLANES = 3; - ThumbnailScaler() { } - virtual ~ThumbnailScaler() { } + ThumbnailScaler() {} + virtual ~ThumbnailScaler() {} virtual bool SetSrcImage(unsigned int width, unsigned int height, unsigned int v4l2_format) = 0; virtual bool SetDstImage(unsigned int width, unsigned int height, unsigned int v4l2_format) = 0; - virtual bool RunStream(int srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, size_t dstLen) = 0; - virtual bool RunStream(char *srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, size_t dstLen) = 0; + virtual bool RunStream(int srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], int dstBuf, + size_t dstLen) = 0; + virtual bool RunStream(char *srcBuf[SCALER_MAX_PLANES], int srcLen[SCALER_MAX_PLANES], + int dstBuf, size_t dstLen) = 0; static ThumbnailScaler *createInstance(); diff --git a/libhwjpeg/hwjpeg-base.cpp b/libhwjpeg/hwjpeg-base.cpp index e593c02..f319d50 100644 --- a/libhwjpeg/hwjpeg-base.cpp +++ b/libhwjpeg/hwjpeg-base.cpp @@ -15,52 +15,41 @@ * limitations under the License. */ -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/mman.h> +#include <exynos-hwjpeg.h> #include <fcntl.h> - -#include <linux/videodev2.h> #include <linux/v4l2-controls.h> +#include <linux/videodev2.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> -#include <exynos-hwjpeg.h> #include "hwjpeg-internal.h" -CHWJpegBase::CHWJpegBase(const char *path) - : m_iFD(-1), m_uiDeviceCaps(0), m_uiAuxFlags(0) -{ +CHWJpegBase::CHWJpegBase(const char *path) : m_iFD(-1), m_uiDeviceCaps(0), m_uiAuxFlags(0) { m_iFD = open(path, O_RDWR); - if (m_iFD < 0) - ALOGERR("Failed to open '%s'", path); + if (m_iFD < 0) ALOGERR("Failed to open '%s'", path); } -CHWJpegBase::~CHWJpegBase() -{ - if (m_iFD >= 0) - close(m_iFD); +CHWJpegBase::~CHWJpegBase() { + if (m_iFD >= 0) close(m_iFD); } -void CHWJpegBase::SetAuxFlags(unsigned int auxflags) -{ +void CHWJpegBase::SetAuxFlags(unsigned int auxflags) { ALOGW_IF(!!(m_uiAuxFlags & auxflags), - "Configuration auxiliary flags %#x overrides previous flags %#x", - auxflags , m_uiAuxFlags); + "Configuration auxiliary flags %#x overrides previous flags %#x", auxflags, + m_uiAuxFlags); m_uiAuxFlags |= auxflags; } -void CHWJpegBase::ClearAuxFlags(unsigned int auxflags) -{ - +void CHWJpegBase::ClearAuxFlags(unsigned int auxflags) { ALOGW_IF(!!(m_uiAuxFlags & auxflags) && ((m_uiAuxFlags & auxflags) != auxflags), - "Clearing auxiliary flags %#x overrides previous flags %#x", - auxflags, m_uiAuxFlags); + "Clearing auxiliary flags %#x overrides previous flags %#x", auxflags, m_uiAuxFlags); m_uiAuxFlags &= ~auxflags; } -bool CStopWatch::Start() -{ +bool CStopWatch::Start() { int ret = clock_gettime(CLOCK_MONOTONIC, &m_tBegin); if (ret) { ALOGERR("Failed to get current clock"); @@ -71,8 +60,7 @@ bool CStopWatch::Start() return true; } -unsigned long CStopWatch::GetElapsed() -{ +unsigned long CStopWatch::GetElapsed() { timespec tp; int ret = clock_gettime(CLOCK_MONOTONIC, &tp); if (ret) { @@ -81,13 +69,11 @@ unsigned long CStopWatch::GetElapsed() } unsigned long elapsed = (tp.tv_sec - m_tBegin.tv_sec) * 1000000; - return (m_tBegin.tv_nsec > tp.tv_nsec) - ? elapsed - (m_tBegin.tv_nsec - tp.tv_nsec) / 1000 - : elapsed + (tp.tv_nsec - m_tBegin.tv_nsec) / 1000; + return (m_tBegin.tv_nsec > tp.tv_nsec) ? elapsed - (m_tBegin.tv_nsec - tp.tv_nsec) / 1000 + : elapsed + (tp.tv_nsec - m_tBegin.tv_nsec) / 1000; } -unsigned long CStopWatch::GetElapsedUpdate() -{ +unsigned long CStopWatch::GetElapsedUpdate() { timespec tp; int ret = clock_gettime(CLOCK_MONOTONIC, &tp); if (ret) { @@ -96,17 +82,15 @@ unsigned long CStopWatch::GetElapsedUpdate() } unsigned long elapsed = (tp.tv_sec - m_tBegin.tv_sec) * 1000000; - elapsed = (m_tBegin.tv_nsec > tp.tv_nsec) - ? elapsed - (m_tBegin.tv_nsec - tp.tv_nsec) / 1000 - : elapsed + (tp.tv_nsec - m_tBegin.tv_nsec) / 1000; + elapsed = (m_tBegin.tv_nsec > tp.tv_nsec) ? elapsed - (m_tBegin.tv_nsec - tp.tv_nsec) / 1000 + : elapsed + (tp.tv_nsec - m_tBegin.tv_nsec) / 1000; m_tBegin = tp; return elapsed; } -bool WriteToFile(const char *path, const char *data, size_t len) -{ - int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IWGRP ); +bool WriteToFile(const char *path, const char *data, size_t len) { + int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IWGRP); if (fd < 0) { ALOGERR("Failed to open '%s' for write/create", path); return false; @@ -124,15 +108,15 @@ bool WriteToFile(const char *path, const char *data, size_t len) return true; } -bool WriteToFile(const char *path, int dmabuf, size_t len) -{ - char *p = reinterpret_cast<char *>(mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf, 0)); +bool WriteToFile(const char *path, int dmabuf, size_t len) { + char *p = reinterpret_cast<char *>( + mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf, 0)); if (p == MAP_FAILED) { ALOGERR("Filed to map the given dmabuf fd %d", dmabuf); return false; } - int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IWGRP ); + int fd = open(path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | S_IWGRP); if (fd < 0) { ALOGERR("Failed to open '%s' for write/create", path); munmap(p, len); diff --git a/libhwjpeg/hwjpeg-internal.h b/libhwjpeg/hwjpeg-internal.h index d7518da..bb14a35 100644 --- a/libhwjpeg/hwjpeg-internal.h +++ b/libhwjpeg/hwjpeg-internal.h @@ -22,18 +22,18 @@ #error "LOG_TAG is not defined!" #endif -#include <cerrno> -#include <cstring> - -#include <unistd.h> #include <log/log.h> -#include <time.h> #include <sys/ioctl.h> +#include <time.h> +#include <unistd.h> + +#include <cerrno> +#include <cstring> #ifdef __GNUC__ -# define __UNUSED__ __attribute__((__unused__)) +#define __UNUSED__ __attribute__((__unused__)) #else -# define __UNUSED__ +#define __UNUSED__ #endif #ifndef ALOGERR @@ -41,16 +41,16 @@ #endif #define V4L2_CID_JPEG_SEC_COMP_QUALITY (V4L2_CID_JPEG_CLASS_BASE + 20) -#define V4L2_CID_JPEG_QTABLES2 (V4L2_CID_JPEG_CLASS_BASE + 22) -#define V4L2_CID_JPEG_HWFC_ENABLE (V4L2_CID_JPEG_CLASS_BASE + 25) +#define V4L2_CID_JPEG_QTABLES2 (V4L2_CID_JPEG_CLASS_BASE + 22) +#define V4L2_CID_JPEG_HWFC_ENABLE (V4L2_CID_JPEG_CLASS_BASE + 25) #define V4L2_CID_JPEG_PADDING (V4L2_CID_JPEG_CLASS_BASE + 26) #define V4L2_CID_JPEG_SEC_PADDING (V4L2_CID_JPEG_CLASS_BASE + 27) -#define TO_MAIN_SIZE(val) ((val) & 0xFFFF) -#define TO_THUMB_SIZE(val) (((val) & 0xFFFF) << 16) +#define TO_MAIN_SIZE(val) ((val)&0xFFFF) +#define TO_THUMB_SIZE(val) (((val)&0xFFFF) << 16) #define TO_IMAGE_SIZE(main, thumb) (TO_MAIN_SIZE(main) | TO_THUMB_SIZE(thumb)) -#define PTR_TO_ULONG(ptr) reinterpret_cast<unsigned long>(ptr) +#define PTR_TO_ULONG(ptr) reinterpret_cast<unsigned long>(ptr) #define PTR_DIFF(ptr1, ptr2) (reinterpret_cast<size_t>(ptr2) - reinterpret_cast<size_t>(ptr1)) #define ARRSIZE(v) (sizeof(v) / sizeof(v[0])) @@ -76,10 +76,10 @@ static inline T max(T val1, T val2) { class CStopWatch { timespec m_tBegin; + public: CStopWatch(bool start = false) { - if (start) - Start(); + if (start) Start(); } bool Start(); unsigned long GetElapsed(); diff --git a/libhwjpeg/hwjpeg-v4l2.cpp b/libhwjpeg/hwjpeg-v4l2.cpp index 58a3b12..b822e24 100644 --- a/libhwjpeg/hwjpeg-v4l2.cpp +++ b/libhwjpeg/hwjpeg-v4l2.cpp @@ -15,17 +15,14 @@ * limitations under the License. */ -#include <linux/videodev2.h> -#include <linux/v4l2-controls.h> - -// This header is non-hermetic and needs to be after videodev2.h #include <exynos-hwjpeg.h> +#include <linux/v4l2-controls.h> +#include <linux/videodev2.h> #include "hwjpeg-internal.h" #include "log/log_main.h" -CHWJpegV4L2Compressor::CHWJpegV4L2Compressor(): CHWJpegCompressor("/dev/video12") -{ +CHWJpegV4L2Compressor::CHWJpegV4L2Compressor() : CHWJpegCompressor("/dev/video12") { memset(&m_v4l2Format, 0, sizeof(m_v4l2Format)); memset(&m_v4l2SrcBuffer, 0, sizeof(m_v4l2SrcBuffer)); memset(&m_v4l2DstBuffer, 0, sizeof(m_v4l2DstBuffer)); @@ -66,27 +63,34 @@ CHWJpegV4L2Compressor::CHWJpegV4L2Compressor(): CHWJpegCompressor("/dev/video12" ALOGD("CHWJpegV4L2Compressor Created: %p, FD %d", this, GetDeviceFD()); } -CHWJpegV4L2Compressor::~CHWJpegV4L2Compressor() -{ +CHWJpegV4L2Compressor::~CHWJpegV4L2Compressor() { StopStreaming(); ALOGD("CHWJpegV4L2Compressor Destroyed: %p, FD %d", this, GetDeviceFD()); } -bool CHWJpegV4L2Compressor::SetChromaSampFactor( - unsigned int horizontal, unsigned int vertical) -{ +bool CHWJpegV4L2Compressor::SetChromaSampFactor(unsigned int horizontal, unsigned int vertical) { __s32 value; switch ((horizontal << 4) | vertical) { - case 0x00: value = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY; break; - case 0x11: value = V4L2_JPEG_CHROMA_SUBSAMPLING_444; break; - case 0x21: value = V4L2_JPEG_CHROMA_SUBSAMPLING_422; break; - case 0x22: value = V4L2_JPEG_CHROMA_SUBSAMPLING_420; break; - case 0x41: value = V4L2_JPEG_CHROMA_SUBSAMPLING_411; break; + case 0x00: + value = V4L2_JPEG_CHROMA_SUBSAMPLING_GRAY; + break; + case 0x11: + value = V4L2_JPEG_CHROMA_SUBSAMPLING_444; + break; + case 0x21: + value = V4L2_JPEG_CHROMA_SUBSAMPLING_422; + break; + case 0x22: + value = V4L2_JPEG_CHROMA_SUBSAMPLING_420; + break; + case 0x41: + value = V4L2_JPEG_CHROMA_SUBSAMPLING_411; + break; case 0x12: default: - ALOGE("Unsupported chroma subsampling %ux%u", horizontal, vertical); - return false; + ALOGE("Unsupported chroma subsampling %ux%u", horizontal, vertical); + return false; } m_v4l2Controls[HWJPEG_CTRL_CHROMFACTOR].id = V4L2_CID_JPEG_CHROMA_SUBSAMPLING; @@ -96,17 +100,14 @@ bool CHWJpegV4L2Compressor::SetChromaSampFactor( return true; } -bool CHWJpegV4L2Compressor::SetQuality( - unsigned int quality_factor, unsigned int quality_factor2) -{ +bool CHWJpegV4L2Compressor::SetQuality(unsigned int quality_factor, unsigned int quality_factor2) { if (quality_factor > 100) { ALOGE("Unsupported quality factor %u", quality_factor); return false; } if (quality_factor2 > 100) { - ALOGE("Unsupported quality factor %u for the secondary image", - quality_factor2); + ALOGE("Unsupported quality factor %u for the secondary image", quality_factor2); return false; } @@ -125,8 +126,7 @@ bool CHWJpegV4L2Compressor::SetQuality( return true; } -bool CHWJpegV4L2Compressor::SetQuality(const unsigned char qtable[]) -{ +bool CHWJpegV4L2Compressor::SetQuality(const unsigned char qtable[]) { v4l2_ext_controls ctrls; v4l2_ext_control ctrl; @@ -194,10 +194,9 @@ bool CHWJpegV4L2Compressor::SetPadding2(unsigned char padding[], unsigned int nu return true; } -bool CHWJpegV4L2Compressor::SetImageFormat(unsigned int v4l2_fmt, - unsigned int width, unsigned int height, - unsigned int width2, unsigned int height2) -{ +bool CHWJpegV4L2Compressor::SetImageFormat(unsigned int v4l2_fmt, unsigned int width, + unsigned int height, unsigned int width2, + unsigned int height2) { if ((m_v4l2Format.fmt.pix_mp.pixelformat == v4l2_fmt) && (m_v4l2Format.fmt.pix_mp.width == TO_IMAGE_SIZE(width, width2)) && (m_v4l2Format.fmt.pix_mp.height == TO_IMAGE_SIZE(height, height2))) @@ -212,8 +211,7 @@ bool CHWJpegV4L2Compressor::SetImageFormat(unsigned int v4l2_fmt, return TryFormat(); } -bool CHWJpegV4L2Compressor::GetImageBufferSizes(size_t buf_sizes[], unsigned int *num_buffers) -{ +bool CHWJpegV4L2Compressor::GetImageBufferSizes(size_t buf_sizes[], unsigned int *num_buffers) { if (buf_sizes) { for (unsigned int i = 0; i < m_v4l2Format.fmt.pix_mp.num_planes; i++) buf_sizes[i] = m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage; @@ -222,7 +220,7 @@ bool CHWJpegV4L2Compressor::GetImageBufferSizes(size_t buf_sizes[], unsigned int if (num_buffers) { if (*num_buffers < m_v4l2Format.fmt.pix_mp.num_planes) { ALOGE("The size array length %u is smaller than the number of required buffers %u", - *num_buffers, m_v4l2Format.fmt.pix_mp.num_planes); + *num_buffers, m_v4l2Format.fmt.pix_mp.num_planes); return false; } @@ -233,19 +231,18 @@ bool CHWJpegV4L2Compressor::GetImageBufferSizes(size_t buf_sizes[], unsigned int } bool CHWJpegV4L2Compressor::SetImageBuffer(char *buffers[], size_t len_buffers[], - unsigned int num_buffers) -{ + unsigned int num_buffers) { if (num_buffers < m_v4l2Format.fmt.pix_mp.num_planes) { - ALOGE("The number of buffers %u is smaller than the required %u", - num_buffers,m_v4l2Format.fmt.pix_mp.num_planes); + ALOGE("The number of buffers %u is smaller than the required %u", num_buffers, + m_v4l2Format.fmt.pix_mp.num_planes); return false; } for (unsigned int i = 0; i < m_v4l2Format.fmt.pix_mp.num_planes; i++) { m_v4l2SrcPlanes[i].m.userptr = reinterpret_cast<unsigned long>(buffers[i]); if (len_buffers[i] < m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage) { - ALOGE("The size of the buffer[%u] %zu is smaller than required %u", - i, len_buffers[i], m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage); + ALOGE("The size of the buffer[%u] %zu is smaller than required %u", i, len_buffers[i], + m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage); return false; } m_v4l2SrcPlanes[i].bytesused = m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage; @@ -260,19 +257,18 @@ bool CHWJpegV4L2Compressor::SetImageBuffer(char *buffers[], size_t len_buffers[] } bool CHWJpegV4L2Compressor::SetImageBuffer(int buffers[], size_t len_buffers[], - unsigned int num_buffers) -{ + unsigned int num_buffers) { if (num_buffers < m_v4l2Format.fmt.pix_mp.num_planes) { - ALOGE("The number of buffers %u is smaller than the required %u", - num_buffers,m_v4l2Format.fmt.pix_mp.num_planes); + ALOGE("The number of buffers %u is smaller than the required %u", num_buffers, + m_v4l2Format.fmt.pix_mp.num_planes); return false; } for (unsigned int i = 0; i < m_v4l2Format.fmt.pix_mp.num_planes; i++) { m_v4l2SrcPlanes[i].m.fd = buffers[i]; if (len_buffers[i] < m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage) { - ALOGE("The size of the buffer[%u] %zu is smaller than required %u", - i, len_buffers[i], m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage); + ALOGE("The size of the buffer[%u] %zu is smaller than required %u", i, len_buffers[i], + m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage); return false; } m_v4l2SrcPlanes[i].bytesused = m_v4l2Format.fmt.pix_mp.plane_fmt[i].sizeimage; @@ -286,23 +282,22 @@ bool CHWJpegV4L2Compressor::SetImageBuffer(int buffers[], size_t len_buffers[], return true; } - bool CHWJpegV4L2Compressor::SetImageBuffer2(char *buffers[], size_t len_buffers[], - unsigned int num_buffers) -{ +bool CHWJpegV4L2Compressor::SetImageBuffer2(char *buffers[], size_t len_buffers[], + unsigned int num_buffers) { if (!IsDeviceCapability(V4L2_CAP_EXYNOS_JPEG_B2B_COMPRESSION)) { ALOGE("Back-to-back compression is not suppored by H/W"); return false; } if (num_buffers < m_v4l2Format.fmt.pix_mp.num_planes) { - ALOGE("The number of buffers %u is smaller than the required %u (secondary)", - num_buffers,m_v4l2Format.fmt.pix_mp.num_planes); + ALOGE("The number of buffers %u is smaller than the required %u (secondary)", num_buffers, + m_v4l2Format.fmt.pix_mp.num_planes); return false; } unsigned int ibuf = 0; for (unsigned int i = m_v4l2Format.fmt.pix_mp.num_planes; - i < (m_v4l2Format.fmt.pix_mp.num_planes * 2); i++, ibuf++) { + i < (m_v4l2Format.fmt.pix_mp.num_planes * 2); i++, ibuf++) { m_v4l2SrcPlanes[i].m.userptr = reinterpret_cast<unsigned long>(buffers[ibuf]); // size check is ignored for the secondary image buffers m_v4l2SrcPlanes[i].bytesused = len_buffers[ibuf]; @@ -315,23 +310,22 @@ bool CHWJpegV4L2Compressor::SetImageBuffer(int buffers[], size_t len_buffers[], return true; } - bool CHWJpegV4L2Compressor::SetImageBuffer2(int buffers[], size_t len_buffers[], - unsigned int num_buffers) -{ +bool CHWJpegV4L2Compressor::SetImageBuffer2(int buffers[], size_t len_buffers[], + unsigned int num_buffers) { if (!IsDeviceCapability(V4L2_CAP_EXYNOS_JPEG_B2B_COMPRESSION)) { ALOGE("Back-to-back compression is not suppored by H/W"); return false; } if (num_buffers < m_v4l2Format.fmt.pix_mp.num_planes) { - ALOGE("The number of buffers %u is smaller than the required %u (secondary)", - num_buffers,m_v4l2Format.fmt.pix_mp.num_planes); + ALOGE("The number of buffers %u is smaller than the required %u (secondary)", num_buffers, + m_v4l2Format.fmt.pix_mp.num_planes); return false; } unsigned int ibuf = 0; for (unsigned int i = m_v4l2Format.fmt.pix_mp.num_planes; - i < (m_v4l2Format.fmt.pix_mp.num_planes * 2); i++, ibuf++) { + i < (m_v4l2Format.fmt.pix_mp.num_planes * 2); i++, ibuf++) { m_v4l2SrcPlanes[i].m.fd = buffers[ibuf]; // size check is ignored for the secondary image buffers m_v4l2SrcPlanes[i].bytesused = len_buffers[ibuf]; @@ -345,8 +339,7 @@ bool CHWJpegV4L2Compressor::SetImageBuffer(int buffers[], size_t len_buffers[], return true; } -bool CHWJpegV4L2Compressor::SetJpegBuffer(char *buffer, size_t len_buffer) -{ +bool CHWJpegV4L2Compressor::SetJpegBuffer(char *buffer, size_t len_buffer) { m_v4l2DstPlanes[0].m.userptr = reinterpret_cast<unsigned long>(buffer); m_v4l2DstPlanes[0].length = len_buffer; m_v4l2DstBuffer.memory = V4L2_MEMORY_USERPTR; @@ -354,8 +347,7 @@ bool CHWJpegV4L2Compressor::SetJpegBuffer(char *buffer, size_t len_buffer) return true; } -bool CHWJpegV4L2Compressor::SetJpegBuffer(int buffer, size_t len_buffer, int offset) -{ +bool CHWJpegV4L2Compressor::SetJpegBuffer(int buffer, size_t len_buffer, int offset) { m_v4l2DstPlanes[0].m.fd = buffer; m_v4l2DstPlanes[0].length = len_buffer; m_v4l2DstPlanes[0].data_offset = offset; @@ -364,8 +356,7 @@ bool CHWJpegV4L2Compressor::SetJpegBuffer(int buffer, size_t len_buffer, int off return true; } -bool CHWJpegV4L2Compressor::SetJpegBuffer2(char *buffer, size_t len_buffer) -{ +bool CHWJpegV4L2Compressor::SetJpegBuffer2(char *buffer, size_t len_buffer) { if (!IsDeviceCapability(V4L2_CAP_EXYNOS_JPEG_B2B_COMPRESSION)) { ALOGE("Back-to-back compression is not suppored by H/W"); return false; @@ -377,8 +368,7 @@ bool CHWJpegV4L2Compressor::SetJpegBuffer2(char *buffer, size_t len_buffer) return true; } -bool CHWJpegV4L2Compressor::SetJpegBuffer2(int buffer, size_t len_buffer) -{ +bool CHWJpegV4L2Compressor::SetJpegBuffer2(int buffer, size_t len_buffer) { if (!IsDeviceCapability(V4L2_CAP_EXYNOS_JPEG_B2B_COMPRESSION)) { ALOGE("Back-to-back compression is not suppored by H/W"); return false; @@ -390,11 +380,9 @@ bool CHWJpegV4L2Compressor::SetJpegBuffer2(int buffer, size_t len_buffer) return true; } -bool CHWJpegV4L2Compressor::StopStreaming() -{ +bool CHWJpegV4L2Compressor::StopStreaming() { if (TestFlag(HWJPEG_FLAG_STREAMING)) { - if (!StreamOff()) - return false; + if (!StreamOff()) return false; ClearFlag(HWJPEG_FLAG_STREAMING); } @@ -403,19 +391,16 @@ bool CHWJpegV4L2Compressor::StopStreaming() // It is OK to skip DQBUF because STREAMOFF dequeues all queued buffers if (TestFlag(HWJPEG_FLAG_REQBUFS)) { - if (!ReqBufs(0)) - return false; + if (!ReqBufs(0)) return false; ClearFlag(HWJPEG_FLAG_REQBUFS); } return true; } -ssize_t CHWJpegV4L2Compressor::Compress(size_t *secondary_stream_size, bool block_mode) -{ +ssize_t CHWJpegV4L2Compressor::Compress(size_t *secondary_stream_size, bool block_mode) { if (TestFlag(HWJPEG_FLAG_PIX_FMT)) { - if (!StopStreaming() || !SetFormat()) - return -1; + if (!StopStreaming() || !SetFormat()) return -1; } if (!TestFlag(HWJPEG_FLAG_SRC_BUFFER)) { @@ -432,7 +417,8 @@ ssize_t CHWJpegV4L2Compressor::Compress(size_t *secondary_stream_size, bool bloc m_v4l2DstBuffer.length = 1; if (IsB2BCompression()) { if (!TestFlag(HWJPEG_FLAG_SRC_BUFFER2 | HWJPEG_FLAG_DST_BUFFER2)) { - ALOGE("Either of source or destination buffer of secondary image is not specified (%#x)", + ALOGE("Either of source or destination buffer of secondary image is not specified " + "(%#x)", GetFlags()); return -1; } @@ -447,14 +433,12 @@ ssize_t CHWJpegV4L2Compressor::Compress(size_t *secondary_stream_size, bool bloc if (!!(GetAuxFlags() & EXYNOS_HWJPEG_AUXOPT_DST_NOCACHECLEAN)) m_v4l2DstBuffer.flags |= V4L2_BUF_FLAG_NO_CACHE_CLEAN; - if (!ReqBufs() || !StreamOn() || !UpdateControls() || !QBuf()) - return -1; + if (!ReqBufs() || !StreamOn() || !UpdateControls() || !QBuf()) return -1; return block_mode ? DQBuf(secondary_stream_size) : 0; } -bool CHWJpegV4L2Compressor::TryFormat() -{ +bool CHWJpegV4L2Compressor::TryFormat() { if (ioctl(GetDeviceFD(), VIDIOC_TRY_FMT, &m_v4l2Format) < 0) { ALOGERR("Failed to TRY_FMT for compression"); return false; @@ -463,8 +447,7 @@ bool CHWJpegV4L2Compressor::TryFormat() return true; } -bool CHWJpegV4L2Compressor::SetFormat() -{ +bool CHWJpegV4L2Compressor::SetFormat() { if (ioctl(GetDeviceFD(), VIDIOC_S_FMT, &m_v4l2Format) < 0) { ALOGERR("Failed to S_FMT for image to compress"); return false; @@ -488,12 +471,10 @@ bool CHWJpegV4L2Compressor::SetFormat() return true; } -bool CHWJpegV4L2Compressor::UpdateControls() -{ +bool CHWJpegV4L2Compressor::UpdateControls() { bool enable_hwfc = !!(GetAuxFlags() & EXYNOS_HWJPEG_AUXOPT_ENABLE_HWFC); - if ((m_uiControlsToSet == 0) && (enable_hwfc == m_bEnableHWFC)) - return true; + if ((m_uiControlsToSet == 0) && (enable_hwfc == m_bEnableHWFC)) return true; v4l2_ext_controls ctrls; v4l2_ext_control ctrl[HWJPEG_CTRL_NUM]; @@ -529,14 +510,12 @@ bool CHWJpegV4L2Compressor::UpdateControls() return true; } -bool CHWJpegV4L2Compressor::ReqBufs(unsigned int count) -{ +bool CHWJpegV4L2Compressor::ReqBufs(unsigned int count) { // - count > 0 && REQBUFS is set: Just return true // - count > 0 && REQBUFS is unset: REQBUFS(count) is required // - count == 0 && REQBUFS is set: REQBUFS(0) is required // - count == 0 && REQBUFS is unset: Just return true; - if ((count > 0) == TestFlag(HWJPEG_FLAG_REQBUFS)) - return true; + if ((count > 0) == TestFlag(HWJPEG_FLAG_REQBUFS)) return true; v4l2_requestbuffers reqbufs; @@ -571,10 +550,8 @@ bool CHWJpegV4L2Compressor::ReqBufs(unsigned int count) return true; } -bool CHWJpegV4L2Compressor::StreamOn() -{ - if (TestFlag(HWJPEG_FLAG_STREAMING)) - return true; +bool CHWJpegV4L2Compressor::StreamOn() { + if (TestFlag(HWJPEG_FLAG_STREAMING)) return true; if (!TestFlag(HWJPEG_FLAG_REQBUFS)) { ALOGE("Trying to STREAMON before REQBUFS"); @@ -597,10 +574,8 @@ bool CHWJpegV4L2Compressor::StreamOn() return true; } -bool CHWJpegV4L2Compressor::StreamOff() -{ - if (!TestFlag(HWJPEG_FLAG_STREAMING)) - return true; +bool CHWJpegV4L2Compressor::StreamOff() { + if (!TestFlag(HWJPEG_FLAG_STREAMING)) return true; // error during stream off do not need further handling because of nothing to do if (ioctl(GetDeviceFD(), VIDIOC_STREAMOFF, &m_v4l2SrcBuffer.type) < 0) @@ -614,8 +589,7 @@ bool CHWJpegV4L2Compressor::StreamOff() return true; } -bool CHWJpegV4L2Compressor::QBuf() -{ +bool CHWJpegV4L2Compressor::QBuf() { if (!TestFlag(HWJPEG_FLAG_REQBUFS)) { ALOGE("QBuf is not permitted until REQBUFS is performed"); return false; @@ -640,8 +614,7 @@ bool CHWJpegV4L2Compressor::QBuf() return true; } -ssize_t CHWJpegV4L2Compressor::DQBuf(size_t *secondary_stream_size) -{ +ssize_t CHWJpegV4L2Compressor::DQBuf(size_t *secondary_stream_size) { bool failed = false; v4l2_buffer buffer_src, buffer_dst; v4l2_plane planes_src[6], planes_dst[2]; @@ -675,8 +648,7 @@ ssize_t CHWJpegV4L2Compressor::DQBuf(size_t *secondary_stream_size) ClearFlag(HWJPEG_FLAG_QBUF_OUT | HWJPEG_FLAG_QBUF_CAP); - if (failed) - return -1; + if (failed) return -1; if (!!((buffer_src.flags | buffer_dst.flags) & V4L2_BUF_FLAG_ERROR)) { ALOGE("Error occurred during compression"); @@ -694,14 +666,12 @@ ssize_t CHWJpegV4L2Compressor::DQBuf(size_t *secondary_stream_size) return GetStreamSize(secondary_stream_size); } -ssize_t CHWJpegV4L2Compressor::WaitForCompression(size_t *secondary_stream_size) -{ +ssize_t CHWJpegV4L2Compressor::WaitForCompression(size_t *secondary_stream_size) { return DQBuf(secondary_stream_size); } bool CHWJpegV4L2Compressor::GetImageBuffers(int buffers[], size_t len_buffers[], - unsigned int num_buffers) -{ + unsigned int num_buffers) { if (m_v4l2SrcBuffer.memory != V4L2_MEMORY_DMABUF) { ALOGE("Current image buffer type is not dma-buf but attempted to retrieve dma-buf buffers"); return false; @@ -709,7 +679,7 @@ bool CHWJpegV4L2Compressor::GetImageBuffers(int buffers[], size_t len_buffers[], if (num_buffers < m_v4l2Format.fmt.pix_mp.num_planes) { ALOGE("Number of planes are %u but attemts to retrieve %u buffers", - m_v4l2Format.fmt.pix_mp.num_planes, num_buffers); + m_v4l2Format.fmt.pix_mp.num_planes, num_buffers); return false; } @@ -722,8 +692,7 @@ bool CHWJpegV4L2Compressor::GetImageBuffers(int buffers[], size_t len_buffers[], } bool CHWJpegV4L2Compressor::GetImageBuffers(char *buffers[], size_t len_buffers[], - unsigned int num_buffers) -{ + unsigned int num_buffers) { if (m_v4l2SrcBuffer.memory != V4L2_MEMORY_USERPTR) { ALOGE("Current image buffer type is not userptr but attempted to retrieve userptr buffers"); return false; @@ -731,7 +700,7 @@ bool CHWJpegV4L2Compressor::GetImageBuffers(char *buffers[], size_t len_buffers[ if (num_buffers < m_v4l2Format.fmt.pix_mp.num_planes) { ALOGE("Number of planes are %u but attemts to retrieve %u buffers", - m_v4l2Format.fmt.pix_mp.num_planes, num_buffers); + m_v4l2Format.fmt.pix_mp.num_planes, num_buffers); return false; } @@ -743,8 +712,7 @@ bool CHWJpegV4L2Compressor::GetImageBuffers(char *buffers[], size_t len_buffers[ return true; } -bool CHWJpegV4L2Compressor::GetJpegBuffer(int *buffer, size_t *len_buffer) -{ +bool CHWJpegV4L2Compressor::GetJpegBuffer(int *buffer, size_t *len_buffer) { if (m_v4l2DstBuffer.memory != V4L2_MEMORY_DMABUF) { ALOGE("Current jpeg buffer type is not dma-buf but attempted to retrieve dma-buf buffer"); return false; @@ -756,8 +724,7 @@ bool CHWJpegV4L2Compressor::GetJpegBuffer(int *buffer, size_t *len_buffer) return true; } -bool CHWJpegV4L2Compressor::GetJpegBuffer(char **buffer, size_t *len_buffer) -{ +bool CHWJpegV4L2Compressor::GetJpegBuffer(char **buffer, size_t *len_buffer) { if (m_v4l2DstBuffer.memory != V4L2_MEMORY_USERPTR) { ALOGE("Current jpeg buffer type is not userptr but attempted to retrieve userptr buffer"); return false; @@ -769,8 +736,7 @@ bool CHWJpegV4L2Compressor::GetJpegBuffer(char **buffer, size_t *len_buffer) return true; } -void CHWJpegV4L2Compressor::Release() -{ +void CHWJpegV4L2Compressor::Release() { StopStreaming(); } @@ -778,8 +744,7 @@ void CHWJpegV4L2Compressor::Release() /********* D E C O M P R E S S I O N S U P P O R T **************************/ /******************************************************************************/ -CHWJpegV4L2Decompressor::CHWJpegV4L2Decompressor() : CHWJpegDecompressor("/dev/video12") -{ +CHWJpegV4L2Decompressor::CHWJpegV4L2Decompressor() : CHWJpegDecompressor("/dev/video12") { m_v4l2Format.type = 0; // inidication of uninitialized state memset(&m_v4l2DstBuffer, 0, sizeof(m_v4l2DstBuffer)); @@ -796,21 +761,18 @@ CHWJpegV4L2Decompressor::CHWJpegV4L2Decompressor() : CHWJpegDecompressor("/dev/v } } -CHWJpegV4L2Decompressor::~CHWJpegV4L2Decompressor() -{ +CHWJpegV4L2Decompressor::~CHWJpegV4L2Decompressor() { CancelCapture(); } -bool CHWJpegV4L2Decompressor::PrepareCapture() -{ +bool CHWJpegV4L2Decompressor::PrepareCapture() { if (m_v4l2DstBuffer.length < m_v4l2Format.fmt.pix.sizeimage) { - ALOGE("The size of the buffer %u is smaller than required %u", - m_v4l2DstBuffer.length, m_v4l2Format.fmt.pix.sizeimage); + ALOGE("The size of the buffer %u is smaller than required %u", m_v4l2DstBuffer.length, + m_v4l2Format.fmt.pix.sizeimage); return false; } - if (TestFlag(HWJPEG_FLAG_CAPTURE_READY)) - return true; + if (TestFlag(HWJPEG_FLAG_CAPTURE_READY)) return true; v4l2_requestbuffers reqbufs; @@ -836,10 +798,8 @@ bool CHWJpegV4L2Decompressor::PrepareCapture() return true; } -void CHWJpegV4L2Decompressor::CancelCapture() -{ - if (!TestFlag(HWJPEG_FLAG_CAPTURE_READY)) - return; +void CHWJpegV4L2Decompressor::CancelCapture() { + if (!TestFlag(HWJPEG_FLAG_CAPTURE_READY)) return; v4l2_requestbuffers reqbufs; @@ -853,14 +813,12 @@ void CHWJpegV4L2Decompressor::CancelCapture() ClearFlag(HWJPEG_FLAG_CAPTURE_READY); } -bool CHWJpegV4L2Decompressor::SetImageFormat(unsigned int v4l2_fmt, - unsigned int width, unsigned int height) -{ +bool CHWJpegV4L2Decompressor::SetImageFormat(unsigned int v4l2_fmt, unsigned int width, + unsigned int height) { // Test if new format is the same as the current configured format if (m_v4l2Format.type != 0) { v4l2_pix_format *p = &m_v4l2Format.fmt.pix; - if ((p->pixelformat == v4l2_fmt) && - (p->width == width) && (p->height == height)) + if ((p->pixelformat == v4l2_fmt) && (p->width == width) && (p->height == height)) return true; } @@ -874,16 +832,14 @@ bool CHWJpegV4L2Decompressor::SetImageFormat(unsigned int v4l2_fmt, m_v4l2Format.fmt.pix.height = height; if (ioctl(GetDeviceFD(), VIDIOC_S_FMT, &m_v4l2Format) < 0) { - ALOGERR("Failed to S_FMT for decompressed image (%08X,%ux%u)", - v4l2_fmt, width, height); + ALOGERR("Failed to S_FMT for decompressed image (%08X,%ux%u)", v4l2_fmt, width, height); return false; } return true; } -bool CHWJpegV4L2Decompressor::SetImageBuffer(char *buffer, size_t len_buffer) -{ +bool CHWJpegV4L2Decompressor::SetImageBuffer(char *buffer, size_t len_buffer) { m_v4l2DstBuffer.m.userptr = reinterpret_cast<unsigned long>(buffer); m_v4l2DstBuffer.bytesused = m_v4l2Format.fmt.pix.sizeimage; m_v4l2DstBuffer.length = len_buffer; @@ -892,8 +848,7 @@ bool CHWJpegV4L2Decompressor::SetImageBuffer(char *buffer, size_t len_buffer) return true; } -bool CHWJpegV4L2Decompressor::SetImageBuffer(int buffer, size_t len_buffer) -{ +bool CHWJpegV4L2Decompressor::SetImageBuffer(int buffer, size_t len_buffer) { m_v4l2DstBuffer.m.fd = buffer; m_v4l2DstBuffer.bytesused = m_v4l2Format.fmt.pix.sizeimage; m_v4l2DstBuffer.length = len_buffer; @@ -902,10 +857,8 @@ bool CHWJpegV4L2Decompressor::SetImageBuffer(int buffer, size_t len_buffer) return true; } -bool CHWJpegV4L2Decompressor::PrepareStream() -{ - if (TestFlag(HWJPEG_FLAG_OUTPUT_READY)) - return true; +bool CHWJpegV4L2Decompressor::PrepareStream() { + if (TestFlag(HWJPEG_FLAG_OUTPUT_READY)) return true; /* * S_FMT for output stream is unneccessary because the driver assumes that @@ -941,10 +894,8 @@ bool CHWJpegV4L2Decompressor::PrepareStream() return true; } -void CHWJpegV4L2Decompressor::CancelStream() -{ - if (!TestFlag(HWJPEG_FLAG_OUTPUT_READY)) - return; +void CHWJpegV4L2Decompressor::CancelStream() { + if (!TestFlag(HWJPEG_FLAG_OUTPUT_READY)) return; v4l2_requestbuffers rb; memset(&rb, 0, sizeof(rb)); @@ -959,8 +910,7 @@ void CHWJpegV4L2Decompressor::CancelStream() ClearFlag(HWJPEG_FLAG_OUTPUT_READY); } -bool CHWJpegV4L2Decompressor::QBufAndWait(const char *buffer, size_t len) -{ +bool CHWJpegV4L2Decompressor::QBufAndWait(const char *buffer, size_t len) { v4l2_buffer buf; memset(&buf, 0, sizeof(buf)); buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT; @@ -1000,8 +950,7 @@ bool CHWJpegV4L2Decompressor::QBufAndWait(const char *buffer, size_t len) return ret; } -bool CHWJpegV4L2Decompressor::Decompress(const char *buffer, size_t len) -{ +bool CHWJpegV4L2Decompressor::Decompress(const char *buffer, size_t len) { if (m_v4l2Format.type == 0) { ALOGE("Decompressed image format is not specified"); return false; @@ -1014,11 +963,9 @@ bool CHWJpegV4L2Decompressor::Decompress(const char *buffer, size_t len) // Do not change the order of PrepareCapture() and PrepareStream(). // Otherwise, decompression will fail. - if (!PrepareCapture() || !PrepareStream()) - return false; + if (!PrepareCapture() || !PrepareStream()) return false; - if (!QBufAndWait(buffer, len)) - return false; + if (!QBufAndWait(buffer, len)) return false; return true; } diff --git a/libhwjpeg/include/ExynosJpegApi.h b/libhwjpeg/include/ExynosJpegApi.h index 70c6664..6addf51 100644 --- a/libhwjpeg/include/ExynosJpegApi.h +++ b/libhwjpeg/include/ExynosJpegApi.h @@ -33,7 +33,7 @@ #endif #define JPEG_BUF_TYPE_USER_PTR 1 -#define JPEG_BUF_TYPE_DMA_BUF 2 +#define JPEG_BUF_TYPE_DMA_BUF 2 // CUSTOM V4L2 4CC FORMATS FOR LEGACY JPEG LIBRARY AND DRIVERS #ifndef V4L2_PIX_FMT_JPEG_444 @@ -78,11 +78,12 @@ class ExynosJpegEncoder { int m_nStreamSize; bool __EnsureFormatIsApplied(); + protected: enum { - STATE_SIZE_CHANGED = 1 << 0, - STATE_PIXFMT_CHANGED = 1 << 1, - STATE_BASE_MAX = 1 << 16, + STATE_SIZE_CHANGED = 1 << 0, + STATE_PIXFMT_CHANGED = 1 << 1, + STATE_BASE_MAX = 1 << 16, }; unsigned int GetDeviceCapabilities() { return m_hwjpeg.GetDeviceCapabilities(); } @@ -95,11 +96,19 @@ protected: bool TestStateEither(unsigned int state) { return (m_uiState & state) != 0; } virtual bool EnsureFormatIsApplied() { return __EnsureFormatIsApplied(); } + public: - ExynosJpegEncoder(): m_hwjpeg(), - m_iInBufType(JPEG_BUF_TYPE_USER_PTR), m_iOutBufType(JPEG_BUF_TYPE_USER_PTR), m_uiState(0), - m_nQFactor(0), m_nWidth(0), m_nHeight(0), m_v4l2Format(0), m_jpegFormat(0), m_nStreamSize(0) - { + ExynosJpegEncoder() + : m_hwjpeg(), + m_iInBufType(JPEG_BUF_TYPE_USER_PTR), + m_iOutBufType(JPEG_BUF_TYPE_USER_PTR), + m_uiState(0), + m_nQFactor(0), + m_nWidth(0), + m_nHeight(0), + m_v4l2Format(0), + m_jpegFormat(0), + m_nStreamSize(0) { /* To detect setInBuf() call without format setting */ SetState(STATE_SIZE_CHANGED | STATE_PIXFMT_CHANGED); } @@ -113,7 +122,7 @@ public: int setCache(int __unused val) { return 0; } void *getJpegConfig() { return reinterpret_cast<void *>(this); } - int setJpegConfig(void* pConfig); + int setJpegConfig(void *pConfig); int checkInBufType(void) { return m_iInBufType; } int checkOutBufType(void) { return m_iOutBufType; } @@ -155,8 +164,7 @@ public: int setQuality(int iQuality) { if (m_nQFactor != iQuality) { - if (!m_hwjpeg.SetQuality(static_cast<unsigned int>(iQuality))) - return -1; + if (!m_hwjpeg.SetQuality(static_cast<unsigned int>(iQuality))) return -1; m_nQFactor = iQuality; } return 0; @@ -169,13 +177,11 @@ public: int getJpegSize(void) { return m_nStreamSize; } int encode(void) { - if (!__EnsureFormatIsApplied()) - return false; + if (!__EnsureFormatIsApplied()) return false; m_nStreamSize = static_cast<int>(m_hwjpeg.Compress()); return (m_nStreamSize < 0) ? -1 : 0; } - }; #endif //__HARDWARE_EXYNOS_EXYNOS_JPEG_API_H__ diff --git a/libhwjpeg/include/ExynosJpegEncoderForCamera.h b/libhwjpeg/include/ExynosJpegEncoderForCamera.h index 5cb62e5..e7a2eec 100644 --- a/libhwjpeg/include/ExynosJpegEncoderForCamera.h +++ b/libhwjpeg/include/ExynosJpegEncoderForCamera.h @@ -27,9 +27,9 @@ #include "ExynosJpegApi.h" class CAppMarkerWriter; // defined in libhwjpeg/AppMarkerWriter.h -class ThumbnailScaler; // defined in libhwjpeg/thumbnail_scaler.h +class ThumbnailScaler; // defined in libhwjpeg/thumbnail_scaler.h -class ExynosJpegEncoderForCamera: public ExynosJpegEncoder { +class ExynosJpegEncoderForCamera : public ExynosJpegEncoder { enum { STATE_THUMBSIZE_CHANGED = STATE_BASE_MAX << 0, STATE_HWFC_ENABLED = STATE_BASE_MAX << 1, @@ -64,7 +64,7 @@ class ExynosJpegEncoderForCamera: public ExynosJpegEncoder { union { char *m_pThumbnailImageBuffer[3]; // checkInBufType() == JPEG_BUF_TYPE_USER_PTR - int m_fdThumbnailImageBuffer[3]; // checkInBufType() == JPEG_BUF_TYPE_DMA_BUF + int m_fdThumbnailImageBuffer[3]; // checkInBufType() == JPEG_BUF_TYPE_DMA_BUF }; size_t m_szThumbnailImageLen[3]; @@ -76,10 +76,11 @@ class ExynosJpegEncoderForCamera: public ExynosJpegEncoder { app_info_t m_appInfo[15]; bool AllocThumbBuffer(int v4l2Format); /* For single compression */ - bool AllocThumbJpegBuffer(); /* For BTB compression */ + bool AllocThumbJpegBuffer(); /* For BTB compression */ bool GenerateThumbnailImage(); size_t CompressThumbnail(); - size_t CompressThumbnailOnly(size_t limit, int quality, unsigned int v4l2Format, int src_buftype); + size_t CompressThumbnailOnly(size_t limit, int quality, unsigned int v4l2Format, + int src_buftype); size_t RemoveTrailingDummies(char *base, size_t len); ssize_t FinishCompression(size_t mainlen, size_t thumblen); bool ProcessExif(char *base, size_t limit, exif_attribute_t *exifInfo, extra_appinfo_t *extra); @@ -87,24 +88,30 @@ class ExynosJpegEncoderForCamera: public ExynosJpegEncoder { bool PrepareCompression(bool thumbnail); // IsThumbGenerationNeeded - true if thumbnail image needed to be generated from the main image - // It also implies that a worker thread is generated to generate thumbnail concurrently. + // It also implies that a worker thread is generated to generate + // thumbnail concurrently. inline bool IsThumbGenerationNeeded() { return !TestState(STATE_NO_CREATE_THUMBIMAGE); } inline void NoThumbGenerationNeeded() { SetState(STATE_NO_CREATE_THUMBIMAGE); } inline void ThumbGenerationNeeded() { ClearState(STATE_NO_CREATE_THUMBIMAGE); } inline bool IsBTBCompressionSupported() { return !!(GetDeviceCapabilities() & V4L2_CAP_EXYNOS_JPEG_B2B_COMPRESSION) && - !TestState(STATE_NO_BTBCOMP); + !TestState(STATE_NO_BTBCOMP); } + protected: virtual bool EnsureFormatIsApplied(); + public: ExynosJpegEncoderForCamera(bool bBTBComp = true); virtual ~ExynosJpegEncoderForCamera(); - int encode(int *size, exif_attribute_t *exifInfo, char** pcJpegBuffer, debug_attribute_t *debugInfo = 0); - int encode(int *size, exif_attribute_t *exifInfo, int fdJpegBuffer, char** pcJpegBuffer, debug_attribute_t *debugInfo = 0); - int encode(int *size, exif_attribute_t *exifInfo, int fdJpegBuffer, char** pcJpegBuffer, extra_appinfo_t *appInfo = 0); + int encode(int *size, exif_attribute_t *exifInfo, char **pcJpegBuffer, + debug_attribute_t *debugInfo = 0); + int encode(int *size, exif_attribute_t *exifInfo, int fdJpegBuffer, char **pcJpegBuffer, + debug_attribute_t *debugInfo = 0); + int encode(int *size, exif_attribute_t *exifInfo, int fdJpegBuffer, char **pcJpegBuffer, + extra_appinfo_t *appInfo = 0); int setInBuf2(int *piBuf, int *iSize); int setInBuf2(char **pcBuf, int *iSize); int setThumbnailSize(int w, int h); diff --git a/libhwjpeg/include/exynos-hwjpeg.h b/libhwjpeg/include/exynos-hwjpeg.h index 7ca732c..3d3d7e5 100644 --- a/libhwjpeg/include/exynos-hwjpeg.h +++ b/libhwjpeg/include/exynos-hwjpeg.h @@ -18,20 +18,9 @@ #ifndef __EXYNOS_HWJPEG_H__ #define __EXYNOS_HWJPEG_H__ +#include <linux/videodev2.h> + #include <cstddef> // size_t -/* - * exynos-hwjpeg.h does not include videodev2.h because Exynos HAL code may - * define its version of videodev2.h that may differ from <linux/videodev2.h> - * of the current Linux version. - * To prevent conflict different versions of videodev2.h, this header file does - * not include videodev2.h even though it depends on the data types defined in - * videodev2.h. - * Therefore, the source files that include this header file, they should - * include their proper version of videodev2.h. - */ -#ifndef VIDEO_MAX_PLANES -#error 'linux/videodev2.h' should be included before 'exynos-hwjpeg.h' -#endif #if VIDEO_MAX_PLANES < 6 #error VIDEO_MAX_PLANES should not be smaller than 6 @@ -39,25 +28,25 @@ // Exynos JPEG specific device capabilities // Defined in the driver. Not in videodev2.h -#define V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION 0x0100 -#define V4L2_CAP_EXYNOS_JPEG_B2B_COMPRESSION 0x0200 -#define V4L2_CAP_EXYNOS_JPEG_HWFC 0x0400 -#define V4L2_CAP_EXYNOS_JPEG_HWFC_EMBEDDED 0x0800 -#define V4L2_CAP_EXYNOS_JPEG_MAX_STREAMSIZE 0x1000 +#define V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION 0x0100 +#define V4L2_CAP_EXYNOS_JPEG_B2B_COMPRESSION 0x0200 +#define V4L2_CAP_EXYNOS_JPEG_HWFC 0x0400 +#define V4L2_CAP_EXYNOS_JPEG_HWFC_EMBEDDED 0x0800 +#define V4L2_CAP_EXYNOS_JPEG_MAX_STREAMSIZE 0x1000 #define V4L2_CAP_EXYNOS_JPEG_NO_STREAMBASE_ALIGN 0x2000 -#define V4L2_CAP_EXYNOS_JPEG_NO_IMAGEBASE_ALIGN 0x4000 -#define V4L2_CAP_EXYNOS_JPEG_NO_BUFFER_OVERRUN 0x8000 +#define V4L2_CAP_EXYNOS_JPEG_NO_IMAGEBASE_ALIGN 0x4000 +#define V4L2_CAP_EXYNOS_JPEG_NO_BUFFER_OVERRUN 0x8000 #define V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_FROM_SOS 0x10000 -#define V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_CROP 0x20000 -#define V4L2_CAP_EXYNOS_JPEG_DOWNSCALING 0x40000 -#define V4L2_CAP_EXYNOS_JPEG_DMABUF_OFFSET 0x80000 +#define V4L2_CAP_EXYNOS_JPEG_DECOMPRESSION_CROP 0x20000 +#define V4L2_CAP_EXYNOS_JPEG_DOWNSCALING 0x40000 +#define V4L2_CAP_EXYNOS_JPEG_DMABUF_OFFSET 0x80000 // EXYNOS HWJPEG specific auxiliary option flags // The flags are common to all derived classes of CHWJpegCompressor // but if a derived class does not support for a specified flag, // it is discarded and ignored silently. -#define EXYNOS_HWJPEG_AUXOPT_ENABLE_HWFC (1 << 4) -#define EXYNOS_HWJPEG_AUXOPT_SRC_NOCACHECLEAN (1 << 8) -#define EXYNOS_HWJPEG_AUXOPT_DST_NOCACHECLEAN (1 << 9) +#define EXYNOS_HWJPEG_AUXOPT_ENABLE_HWFC (1 << 4) +#define EXYNOS_HWJPEG_AUXOPT_SRC_NOCACHECLEAN (1 << 8) +#define EXYNOS_HWJPEG_AUXOPT_DST_NOCACHECLEAN (1 << 9) /* * CHWJpegBase - The base class of JPEG compression and decompression @@ -81,15 +70,19 @@ class CHWJpegBase { * */ unsigned int m_uiAuxFlags; + protected: CHWJpegBase(const char *path); virtual ~CHWJpegBase(); int GetDeviceFD() { return m_iFD; } void SetDeviceCapabilities(unsigned int cap) { m_uiDeviceCaps = cap; } unsigned int GetAuxFlags() { return m_uiAuxFlags; } + public: unsigned int GetDeviceCapabilities() { return m_uiDeviceCaps; } - bool IsDeviceCapability(unsigned int cap_flags) { return (m_uiDeviceCaps & cap_flags) == cap_flags; } + bool IsDeviceCapability(unsigned int cap_flags) { + return (m_uiDeviceCaps & cap_flags) == cap_flags; + } /* * Okay - Test if the object is correctly initialized @@ -144,6 +137,7 @@ public: class CHWJpegCompressor : public CHWJpegBase { size_t m_nLastStreamSize; size_t m_nLastThumbStreamSize; + protected: void SetStreamSize(size_t main_size, size_t secondary_size = 0) { m_nLastStreamSize = main_size; @@ -151,20 +145,23 @@ protected: } ssize_t GetStreamSize(size_t *secondary_size) { - if (secondary_size) - *secondary_size = m_nLastThumbStreamSize; + if (secondary_size) *secondary_size = m_nLastThumbStreamSize; return static_cast<ssize_t>(m_nLastStreamSize); } + public: - CHWJpegCompressor(const char *path): CHWJpegBase(path), m_nLastStreamSize(0), m_nLastThumbStreamSize(0) { } + CHWJpegCompressor(const char *path) + : CHWJpegBase(path), m_nLastStreamSize(0), m_nLastThumbStreamSize(0) {} /* * SetImageFormat - Configure uncompressed image format, width and height * @v4l2_fmt[in] : Image pixel format defined in <linux/videodev2.h> * @width[in] : Width of the primary uncompressed image in the number of pixels * @height[in] : Height of the primary uncompressed image in the number of pixels - * @sec_width[in] : Width of the secondary uncompressed image in the number of pixels (optional) - * @sec_height[in] : Height of the secondary uncompressed image in the number of pixels (optional) + * @sec_width[in] : Width of the secondary uncompressed image in the number of pixels + * (optional) + * @sec_height[in] : Height of the secondary uncompressed image in the number of pixels + * (optional) * @return : true if configuration of image pixel format and size is successful. * false, otherwise. * @@ -172,7 +169,7 @@ public: * to configure different image formats for them. */ virtual bool SetImageFormat(unsigned int v4l2_fmt, unsigned int width, unsigned int height, - unsigned int sec_width = 0, unsigned int sec_height = 0) = 0; + unsigned int sec_width = 0, unsigned int sec_height = 0) = 0; /* * GetImageBufferSizes - Ask the required buffer sizes for the given image format @@ -196,7 +193,8 @@ public: virtual bool SetChromaSampFactor(unsigned int horizontal, unsigned int vertical) = 0; /* * SetQuality - Configure quality factor for JPEG compression - * @quality_factor[in] : JPEG compression quality factor between 1 and 100 for the primary image + * @quality_factor[in] : JPEG compression quality factor between 1 and 100 for the primary + * image * @quality_factor2[in] : JPEG compression quality factor for the secondary image (optional) * @return: true if quality factors are configured successfully. * false, otherwise. @@ -239,7 +237,8 @@ public: * @return : true if buffer configuration is successful. * false, otherwise. */ - virtual bool SetImageBuffer(char *buffers[], size_t len_buffers[], unsigned int num_buffers) = 0; + virtual bool SetImageBuffer(char *buffers[], size_t len_buffers[], + unsigned int num_buffers) = 0; /* * SetImageBuffer - Configure the uncompressed primary image buffers (dmabuf) * @buffers[in] : file descriptors of the buffers exported by dma-buf @@ -257,7 +256,10 @@ public: * @return : true if buffer configuration is successful. * false, otherwise. */ - virtual bool SetImageBuffer2(char __unused *buffers[], size_t __unused len_buffers[], unsigned int __unused num_buffers) { return false; } + virtual bool SetImageBuffer2(char __unused *buffers[], size_t __unused len_buffers[], + unsigned int __unused num_buffers) { + return false; + } /* * SetImageBuffer2 - Configure the uncompressed secondary image buffers (dmabuf) * @buffers[in] : file descriptors of the buffers exported by dma-buf @@ -266,7 +268,10 @@ public: * @return : true if buffer configuration is successful. * false, otherwise. */ - virtual bool SetImageBuffer2(int __unused buffers[], size_t __unused len_buffers[], unsigned int __unused num_buffers) { return false; } + virtual bool SetImageBuffer2(int __unused buffers[], size_t __unused len_buffers[], + unsigned int __unused num_buffers) { + return false; + } /* * SetJpegBuffer - Configure the buffer of JPEG stream of the primary image (userptr) * @buffer [in] : The address of the buffer @@ -329,7 +334,9 @@ public: * returns and the returned size will be the stream sizes obtained by the last call to * Compress(). */ - virtual ssize_t WaitForCompression(size_t __unused *secondary_stream_size = NULL) { return GetStreamSize(secondary_stream_size); } + virtual ssize_t WaitForCompression(size_t __unused *secondary_stream_size = NULL) { + return GetStreamSize(secondary_stream_size); + } /* * GetImageBuffers - Retrieve the configured uncompressed image buffer information (dmabuf) * @buffers[out]: The file descriptors of the buffers exported by dma-buf @@ -340,7 +347,10 @@ public: * DEPREDCATED. DO NOT USE THIS FUNCTION. * This function is just provided to support the legacy ExynosJpegEncoder API. */ - virtual bool GetImageBuffers(int __unused buffers[], size_t __unused len_buffers[], unsigned int __unused num_buffers) { return false; } + virtual bool GetImageBuffers(int __unused buffers[], size_t __unused len_buffers[], + unsigned int __unused num_buffers) { + return false; + } /* * GetImageBuffers - Retrieve the configured uncompressed image buffer information (userptr) * @buffers[out]: The addresses of the buffers @@ -351,7 +361,10 @@ public: * DEPREDCATED. DO NOT USE THIS FUNCTION. * This function is just provided to support the legacy ExynosJpegEncoder API. */ - virtual bool GetImageBuffers(char __unused *buffers[], size_t __unused len_buffers[], unsigned int __unused num_buffers) { return false; } + virtual bool GetImageBuffers(char __unused *buffers[], size_t __unused len_buffers[], + unsigned int __unused num_buffers) { + return false; + } /* * GetJpegBuffers - Retrieve the configured JPEG stream image buffer information (dmabuf) * @buffers[out]: The file descriptor of the buffer exported by dma-buf @@ -371,11 +384,13 @@ public: * DEPREDCATED. DO NOT USE THIS FUNCTION. * This function is just provided to support the legacy ExynosJpegEncoder API. */ - virtual bool GetJpegBuffer(char __unused **buffers, size_t __unused *len_buffer) { return false; } + virtual bool GetJpegBuffer(char __unused **buffers, size_t __unused *len_buffer) { + return false; + } /* * Release - release the buffers acquired by CHWJpegCompressor */ - virtual void Release() { } + virtual void Release() {} }; /* @@ -399,8 +414,8 @@ public: */ class CHWJpegDecompressor : public CHWJpegBase { public: - CHWJpegDecompressor(const char *path) : CHWJpegBase(path) { } - virtual ~CHWJpegDecompressor() { } + CHWJpegDecompressor(const char *path) : CHWJpegBase(path) {} + virtual ~CHWJpegDecompressor() {} /* * SetImageFormat - Configure decompressed image pixel format * @v4l2_fmt[in] : Image pixel format defined in <linux/videodev2.h> @@ -442,7 +457,9 @@ public: * @width[in] : The number of horizontal pixels of the compressed image * @height[in] : The number of vertical pixels of the compressed image */ - virtual bool SetStreamPixelSize(unsigned int __unused width, unsigned int __unused height) { return true; } + virtual bool SetStreamPixelSize(unsigned int __unused width, unsigned int __unused height) { + return true; + } /* * SetChromaSampFactor - Configure the chroma subsampling factor for JPEG stream @@ -459,7 +476,10 @@ public: * If it is required to specify chroma subsampling factors separately, you should * override SetChromaSampFactor(). */ - virtual bool SetChromaSampFactor(unsigned int __unused horizontal, unsigned int __unused vertical) { return true; } + virtual bool SetChromaSampFactor(unsigned int __unused horizontal, + unsigned int __unused vertical) { + return true; + } /* * SetDQT - Configure the address of DQT @@ -502,8 +522,9 @@ public: class CHWJpegFlagManager { unsigned int m_uiHWConfigFlags; + public: - CHWJpegFlagManager() : m_uiHWConfigFlags(0) { } + CHWJpegFlagManager() : m_uiHWConfigFlags(0) {} void SetFlag(unsigned int flag) { m_uiHWConfigFlags |= flag; } void ClearFlag(unsigned int flag) { m_uiHWConfigFlags &= ~flag; } bool TestFlag(unsigned int flag) { return (m_uiHWConfigFlags & flag) == flag; } @@ -515,7 +536,7 @@ class CHWJpegM2M1SHOTCompressor: public CHWJpegCompressor { }; */ -#define TO_SEC_IMG_SIZE(val) (((val) >> 16) & 0xFFFF) +#define TO_SEC_IMG_SIZE(val) (((val) >> 16) & 0xFFFF) class CHWJpegV4L2Compressor : public CHWJpegCompressor, private CHWJpegFlagManager { enum { @@ -528,17 +549,17 @@ class CHWJpegV4L2Compressor : public CHWJpegCompressor, private CHWJpegFlagManag HWJPEG_CTRL_NUM, }; - enum { - HWJPEG_FLAG_PIX_FMT = 0x1, // Set if unapplied image format exists + enum { + HWJPEG_FLAG_PIX_FMT = 0x1, // Set if unapplied image format exists - HWJPEG_FLAG_QBUF_OUT = 0x100, // Set if the image buffer is queued - HWJPEG_FLAG_QBUF_CAP = 0x200, // Set if the JPEG stream buffer is queued - HWJPEG_FLAG_REQBUFS = 0x400, - HWJPEG_FLAG_STREAMING = 0x800, + HWJPEG_FLAG_QBUF_OUT = 0x100, // Set if the image buffer is queued + HWJPEG_FLAG_QBUF_CAP = 0x200, // Set if the JPEG stream buffer is queued + HWJPEG_FLAG_REQBUFS = 0x400, + HWJPEG_FLAG_STREAMING = 0x800, - HWJPEG_FLAG_SRC_BUFFER = 0x10000, // Set if SetImageBuffer() is invoked successfully + HWJPEG_FLAG_SRC_BUFFER = 0x10000, // Set if SetImageBuffer() is invoked successfully HWJPEG_FLAG_SRC_BUFFER2 = 0x20000, // Set if SetImageBuffer2() is invoked successfully - HWJPEG_FLAG_DST_BUFFER = 0x40000, // Set if SetJpegBuffer() is invoked successfully + HWJPEG_FLAG_DST_BUFFER = 0x40000, // Set if SetJpegBuffer() is invoked successfully HWJPEG_FLAG_DST_BUFFER2 = 0x80000, // Set if SetJpegBuffer2() is invoked successfully }; @@ -552,7 +573,7 @@ class CHWJpegV4L2Compressor : public CHWJpegCompressor, private CHWJpegFlagManag // Only valid after Compression() successes. unsigned int m_uiHWDelay; - v4l2_format m_v4l2Format; // v4l2 format for the source image + v4l2_format m_v4l2Format; // v4l2 format for the source image v4l2_buffer m_v4l2SrcBuffer; // v4l2 source buffer v4l2_plane m_v4l2SrcPlanes[6]; v4l2_buffer m_v4l2DstBuffer; @@ -562,7 +583,7 @@ class CHWJpegV4L2Compressor : public CHWJpegCompressor, private CHWJpegFlagManag bool IsB2BCompression() { return (TO_SEC_IMG_SIZE(m_v4l2Format.fmt.pix_mp.width) + - TO_SEC_IMG_SIZE(m_v4l2Format.fmt.pix_mp.height)) != 0; + TO_SEC_IMG_SIZE(m_v4l2Format.fmt.pix_mp.height)) != 0; } // V4L2 Helpers @@ -575,6 +596,7 @@ class CHWJpegV4L2Compressor : public CHWJpegCompressor, private CHWJpegFlagManag bool QBuf(); ssize_t DQBuf(size_t *secondary_stream_size); bool StopStreaming(); + public: CHWJpegV4L2Compressor(); virtual ~CHWJpegV4L2Compressor(); @@ -582,16 +604,14 @@ public: unsigned int GetHWDelay() { return m_uiHWDelay; } // SetChromaSampFactor can be called during streaming - virtual bool SetChromaSampFactor(unsigned int horizontal, - unsigned int vertical); - virtual bool SetQuality(unsigned int quality_factor, - unsigned int quality_factor2 = 0); + virtual bool SetChromaSampFactor(unsigned int horizontal, unsigned int vertical); + virtual bool SetQuality(unsigned int quality_factor, unsigned int quality_factor2 = 0); virtual bool SetQuality(const unsigned char qtable[]); virtual bool SetPadding(unsigned char padding[], unsigned int num_planes); virtual bool SetPadding2(unsigned char padding[], unsigned int num_planes); virtual bool SetImageFormat(unsigned int v4l2_fmt, unsigned int width, unsigned int height, - unsigned int sec_width = 0, unsigned sec_height = 0); + unsigned int sec_width = 0, unsigned sec_height = 0); virtual bool GetImageBufferSizes(size_t buf_sizes[], unsigned int *num_bufffers); virtual bool SetImageBuffer(char *buffers[], size_t len_buffers[], unsigned int num_buffers); virtual bool SetImageBuffer(int buffers[], size_t len_buffers[], unsigned int num_buffers); @@ -611,8 +631,8 @@ public: }; class CHWJpegV4L2Decompressor : public CHWJpegDecompressor, private CHWJpegFlagManager { - enum { - HWJPEG_FLAG_OUTPUT_READY = 0x10, /* the output stream is ready */ + enum { + HWJPEG_FLAG_OUTPUT_READY = 0x10, /* the output stream is ready */ HWJPEG_FLAG_CAPTURE_READY = 0x20, /* the capture stream is ready */ }; @@ -627,6 +647,7 @@ class CHWJpegV4L2Decompressor : public CHWJpegDecompressor, private CHWJpegFlagM bool PrepareStream(); void CancelStream(); bool QBufAndWait(const char *buffer, size_t len); + public: CHWJpegV4L2Decompressor(); virtual ~CHWJpegV4L2Decompressor(); diff --git a/libhwjpeg/include/hwjpeglib-exynos.h b/libhwjpeg/include/hwjpeglib-exynos.h index ba95320..5d23630 100644 --- a/libhwjpeg/include/hwjpeglib-exynos.h +++ b/libhwjpeg/include/hwjpeglib-exynos.h @@ -25,16 +25,19 @@ extern "C" { * hwjpeg_decompress_ptr - handle of decompressor instance */ typedef struct hwjpeg_decompressor_struct { - unsigned int image_width; /* width of the compressed image */ - unsigned int image_height; /* height of the compressed image */ - unsigned char num_components; /* number of components of the compressed image */ - unsigned char chroma_h_samp_factor; /* horizontal chroma sampling factor of the compressed image */ - unsigned char chroma_v_samp_factor; /* vertical chroma sampling factor of the compressed image */ - unsigned char scale_factor; /* down-scaling factor during decompression: one of 1, 2, 4 and 8 */ - - unsigned int output_width; /* width of the output image (image_width/scale_factor) */ - unsigned int output_height; /* height of the output image (image_height/scale_factor) */ - __u32 output_format; /* 4CC style format identifier of the output image defined in videodev2.h */ + unsigned int image_width; /* width of the compressed image */ + unsigned int image_height; /* height of the compressed image */ + unsigned char num_components; /* number of components of the compressed image */ + unsigned char + chroma_h_samp_factor; /* horizontal chroma sampling factor of the compressed image */ + unsigned char + chroma_v_samp_factor; /* vertical chroma sampling factor of the compressed image */ + unsigned char scale_factor; /* down-scaling factor during decompression: one of 1, 2, 4 and 8 */ + + unsigned int output_width; /* width of the output image (image_width/scale_factor) */ + unsigned int output_height; /* height of the output image (image_height/scale_factor) */ + __u32 output_format; /* 4CC style format identifier of the output image defined in videodev2.h + */ } *hwjpeg_decompress_ptr; /* @@ -79,7 +82,8 @@ bool hwjpeg_dmabuf_src(hwjpeg_decompress_ptr cinfo, int infd, size_t insize, siz * @dummybytes: The available dummy bytes after @insize. * @return: false on failure */ -bool hwjpeg_mem_src(hwjpeg_decompress_ptr cinfo, unsigned char *inbuffer, size_t insize, size_t dummybytes); +bool hwjpeg_mem_src(hwjpeg_decompress_ptr cinfo, unsigned char *inbuffer, size_t insize, + size_t dummybytes); /* * hwjpeg_config_image_format - configure output image format @@ -100,7 +104,8 @@ void hwjpeg_config_image_format(hwjpeg_decompress_ptr cinfo, __u32 v4l2_pix_fmt) * @num_buffers: The number of elements in @outsizes and @outbuffer * @return: false on failure. */ -bool hwjpeg_mem_dst(hwjpeg_decompress_ptr cinfo, unsigned char *outbuffer[], size_t outsize[], unsigned int num_buffers); +bool hwjpeg_mem_dst(hwjpeg_decompress_ptr cinfo, unsigned char *outbuffer[], size_t outsize[], + unsigned int num_buffers); /* * hwjpeg_dmabuf_dst - configure the buffer to store decompressed image @@ -113,7 +118,8 @@ bool hwjpeg_mem_dst(hwjpeg_decompress_ptr cinfo, unsigned char *outbuffer[], siz * @num_buffers: The number of elements in @outsizes and @outfd * @return: false on failure. */ -bool hwjpeg_dmabuf_dst(hwjpeg_decompress_ptr cinfo, int outfd[], size_t outsize[], unsigned int num_buffers); +bool hwjpeg_dmabuf_dst(hwjpeg_decompress_ptr cinfo, int outfd[], size_t outsize[], + unsigned int num_buffers); /* * hwjpeg_set_downscale_factor - configure the downscaling factor during decompression @@ -171,7 +177,7 @@ bool hwjpeg_start_decompress(hwjpeg_decompress_ptr cinfo); */ void hwjpeg_destroy_decompress(hwjpeg_decompress_ptr cinfo); -}; /* extern "C" */ +}; /* extern "C" */ #endif /* __cplusplus */ #endif /*__HARDWARE_SAMSUNG_EXYNOS7420_HWJPEGDECOMPRESSOR_H__*/ diff --git a/libhwjpeg/libhwjpeg-exynos.cpp b/libhwjpeg/libhwjpeg-exynos.cpp index 9af30d3..7f82da9 100644 --- a/libhwjpeg/libhwjpeg-exynos.cpp +++ b/libhwjpeg/libhwjpeg-exynos.cpp @@ -15,28 +15,26 @@ * limitations under the License. */ -#include <cstdio> - -#include <cstring> -#include <unistd.h> -#include <sys/ioctl.h> -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/mman.h> +#include <exynos-hwjpeg.h> #include <fcntl.h> - +#include <hwjpeglib-exynos.h> #include <linux/videodev2.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> -#include <exynos-hwjpeg.h> -#include <hwjpeglib-exynos.h> +#include <cstdio> +#include <cstring> #include "hwjpeg-internal.h" #define ALOGERR(fmt, args...) ((void)ALOG(LOG_ERROR, LOG_TAG, fmt " [%s]", ##args, strerror(errno))) -#define ROUND_DOWN(val, denom) ((val) & ~((denom) - 1)) -#define ROUND_UP(val, denom) ROUND_DOWN((val) + (denom) - 1, denom) -#define TO_MASK(val) ((val) - 1) +#define ROUND_DOWN(val, denom) ((val) & ~((denom)-1)) +#define ROUND_UP(val, denom) ROUND_DOWN((val) + (denom)-1, denom) +#define TO_MASK(val) ((val)-1) class CJpegStreamParser { private: @@ -61,8 +59,8 @@ public: unsigned char m_iHorizontalFactor; unsigned char m_iVerticalFactor; - CJpegStreamParser() : m_pStreamBase(NULL), m_nStreamSize(0) { } - ~CJpegStreamParser() { } + CJpegStreamParser() : m_pStreamBase(NULL), m_nStreamSize(0) {} + ~CJpegStreamParser() {} bool Parse(unsigned char *streambase, size_t length); @@ -72,8 +70,7 @@ public: unsigned int GetNumComponents() { return m_nComponents; } }; -void CJpegStreamParser::Initialize() -{ +void CJpegStreamParser::Initialize() { m_nComponents = 0; m_nWidth = 0; m_nHeight = 0; @@ -81,14 +78,12 @@ void CJpegStreamParser::Initialize() m_iVerticalFactor = 1; } -size_t CJpegStreamParser::GetLength(unsigned char *addr) -{ +size_t CJpegStreamParser::GetLength(unsigned char *addr) { size_t len = static_cast<size_t>(*addr++) * 0x100; return len + *addr; } -bool CJpegStreamParser::Parse(unsigned char *streambase, size_t length) -{ +bool CJpegStreamParser::Parse(unsigned char *streambase, size_t length) { Initialize(); m_pStreamBase = streambase; @@ -130,10 +125,9 @@ bool CJpegStreamParser::Parse(unsigned char *streambase, size_t length) return false; } - if (!ParseFrame(addr)) - return false; + if (!ParseFrame(addr)) return false; - return true; // this is the successful exit point + return true; // this is the successful exit point } else if (marker == 0xD9) { // EOI // This will not meet. ALOGE("Unexpected EOI found at %lu\n", GetOffset(addr - 2)); @@ -171,8 +165,7 @@ bool CJpegStreamParser::Parse(unsigned char *streambase, size_t length) return false; } -bool CJpegStreamParser::ParseFrame(unsigned char *addr) -{ // 2 bytes of length +bool CJpegStreamParser::ParseFrame(unsigned char *addr) { // 2 bytes of length // 1 byte of bits per sample // 2 bytes of height // 2 bytes of width @@ -223,7 +216,7 @@ bool CJpegStreamParser::ParseFrame(unsigned char *addr) return true; } -class CLibhwjpegDecompressor: public hwjpeg_decompressor_struct { +class CLibhwjpegDecompressor : public hwjpeg_decompressor_struct { enum { HWJPG_FLAG_NEED_MUNMAP = 1, }; @@ -237,6 +230,7 @@ class CLibhwjpegDecompressor: public hwjpeg_decompressor_struct { size_t m_nDummyBytes; CJpegStreamParser m_jpegStreamParser; + public: CLibhwjpegDecompressor() : m_flags(0) { // members of hwjpeg_decompressor_struct @@ -249,7 +243,7 @@ public: output_width = 0; output_height = 0; m_bPrepared = false; - m_pStreamBuffer = NULL; + m_pStreamBuffer = NULL; output_format = V4L2_PIX_FMT_RGB32; @@ -296,8 +290,7 @@ public: m_nDummyBytes = 0; m_pStreamBuffer = reinterpret_cast<unsigned char *>( - mmap(NULL, m_nStreamLength, - PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)); + mmap(NULL, m_nStreamLength, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)); if (m_pStreamBuffer == MAP_FAILED) { m_pStreamBuffer = NULL; close(fd); @@ -338,8 +331,8 @@ public: m_nDummyBytes = dummybytes; m_pStreamBuffer = reinterpret_cast<unsigned char *>( - mmap(NULL, m_nStreamLength + m_nDummyBytes, - PROT_READ | PROT_WRITE, MAP_SHARED, buffer, 0)); + mmap(NULL, m_nStreamLength + m_nDummyBytes, PROT_READ | PROT_WRITE, MAP_SHARED, + buffer, 0)); if (m_pStreamBuffer == MAP_FAILED) { m_pStreamBuffer = NULL; ALOGERR("Failed to mmap %zu bytes of dmabuf fd %d", m_nStreamLength, buffer); @@ -379,15 +372,13 @@ public: bool IsEnoughStreamBuffer() { return true; } }; -bool CLibhwjpegDecompressor::PrepareDecompression() -{ +bool CLibhwjpegDecompressor::PrepareDecompression() { if (!m_hwjpeg) { ALOGE("device node is not opened!"); return false; } - if ((scale_factor != 1) && (scale_factor != 2) && - (scale_factor != 4) && (scale_factor != 8)) { + if ((scale_factor != 1) && (scale_factor != 2) && (scale_factor != 4) && (scale_factor != 8)) { ALOGE("Invalid downscaling factor %d", scale_factor); return false; } @@ -397,8 +388,7 @@ bool CLibhwjpegDecompressor::PrepareDecompression() return false; } - if (!m_jpegStreamParser.Parse(m_pStreamBuffer, m_nStreamLength)) - return false; + if (!m_jpegStreamParser.Parse(m_pStreamBuffer, m_nStreamLength)) return false; image_width = m_jpegStreamParser.GetWidth(); image_height = m_jpegStreamParser.GetHeight(); @@ -407,9 +397,10 @@ bool CLibhwjpegDecompressor::PrepareDecompression() chroma_v_samp_factor = m_jpegStreamParser.m_iVerticalFactor; if (((image_width % (chroma_h_samp_factor * scale_factor)) != 0) || - ((image_height % (chroma_v_samp_factor * scale_factor)) != 0)) { - ALOGE("Downscaling by factor %d of compressed image size %dx%d(chroma %d:%d) is not supported", - scale_factor, image_width, image_height, chroma_h_samp_factor, chroma_v_samp_factor); + ((image_height % (chroma_v_samp_factor * scale_factor)) != 0)) { + ALOGE("Downscaling by factor %d of compressed image size %dx%d(chroma %d:%d) is not " + "supported", + scale_factor, image_width, image_height, chroma_h_samp_factor, chroma_v_samp_factor); return false; } @@ -422,7 +413,8 @@ bool CLibhwjpegDecompressor::PrepareDecompression() } if (!m_hwjpeg->SetImageFormat(output_format, output_width, output_height)) { - ALOGE("Failed to configure image format (%ux%u/%08X)", output_width, output_height, output_format); + ALOGE("Failed to configure image format (%ux%u/%08X)", output_width, output_height, + output_format); return false; } @@ -431,8 +423,7 @@ bool CLibhwjpegDecompressor::PrepareDecompression() return true; } -bool CLibhwjpegDecompressor::Decompress() -{ +bool CLibhwjpegDecompressor::Decompress() { if (!m_bPrepared) { ALOGE("JPEG header is not parsed"); return false; @@ -453,77 +444,64 @@ bool CLibhwjpegDecompressor::Decompress() return true; } -hwjpeg_decompress_ptr hwjpeg_create_decompress() -{ +hwjpeg_decompress_ptr hwjpeg_create_decompress() { hwjpeg_decompress_ptr p = new CLibhwjpegDecompressor(); - if (!p) - ALOGE("Failed to create decompress struct"); + if (!p) ALOGE("Failed to create decompress struct"); return p; } -bool hwjpeg_file_src(hwjpeg_decompress_ptr cinfo, const char *path) -{ +bool hwjpeg_file_src(hwjpeg_decompress_ptr cinfo, const char *path) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); return decomp->SetStreamPath(path); } -void hwjpeg_config_image_format(hwjpeg_decompress_ptr cinfo, __u32 v4l2_pix_fmt) -{ +void hwjpeg_config_image_format(hwjpeg_decompress_ptr cinfo, __u32 v4l2_pix_fmt) { cinfo->output_format = v4l2_pix_fmt; } -bool hwjpeg_dmabuf_src(hwjpeg_decompress_ptr cinfo, int infd, size_t insize, size_t dummybytes) -{ +bool hwjpeg_dmabuf_src(hwjpeg_decompress_ptr cinfo, int infd, size_t insize, size_t dummybytes) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); return decomp->SetStreamBuffer(infd, insize, dummybytes); } -bool hwjpeg_mem_src(hwjpeg_decompress_ptr cinfo, - unsigned char *inbuffer, size_t insize, size_t dummybytes) -{ +bool hwjpeg_mem_src(hwjpeg_decompress_ptr cinfo, unsigned char *inbuffer, size_t insize, + size_t dummybytes) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); return decomp->SetStreamBuffer(inbuffer, insize, dummybytes); } -bool hwjpeg_mem_dst(hwjpeg_decompress_ptr cinfo, - unsigned char *outbuffer[], size_t outsize[], unsigned int num_buffers) -{ +bool hwjpeg_mem_dst(hwjpeg_decompress_ptr cinfo, unsigned char *outbuffer[], size_t outsize[], + unsigned int num_buffers) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); return decomp->SetImageBuffer(outbuffer, outsize, num_buffers); } -bool hwjpeg_dmabuf_dst(hwjpeg_decompress_ptr cinfo, - int outfd[], size_t outsize[], unsigned int num_buffers) -{ +bool hwjpeg_dmabuf_dst(hwjpeg_decompress_ptr cinfo, int outfd[], size_t outsize[], + unsigned int num_buffers) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); return decomp->SetImageBuffer(outfd, outsize, num_buffers); } -void hwjpeg_set_downscale_factor(hwjpeg_decompress_ptr cinfo, unsigned int factor) -{ +void hwjpeg_set_downscale_factor(hwjpeg_decompress_ptr cinfo, unsigned int factor) { cinfo->scale_factor = factor; } -bool hwjpeg_read_header(hwjpeg_decompress_ptr cinfo) -{ +bool hwjpeg_read_header(hwjpeg_decompress_ptr cinfo) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); return decomp->PrepareDecompression(); } -bool hwjpeg_start_decompress(hwjpeg_decompress_ptr cinfo) -{ +bool hwjpeg_start_decompress(hwjpeg_decompress_ptr cinfo) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); return decomp->Decompress(); } -void hwjpeg_destroy_decompress(hwjpeg_decompress_ptr cinfo) -{ +void hwjpeg_destroy_decompress(hwjpeg_decompress_ptr cinfo) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); delete decomp; } -bool hwjpeg_has_enough_stream_buffer(hwjpeg_decompress_ptr cinfo) -{ +bool hwjpeg_has_enough_stream_buffer(hwjpeg_decompress_ptr cinfo) { CLibhwjpegDecompressor *decomp = reinterpret_cast<CLibhwjpegDecompressor *>(cinfo); return decomp->IsEnoughStreamBuffer(); } |