aboutsummaryrefslogtreecommitdiff
path: root/camera/QCamera2/stack/mm-jpeg-interface
diff options
context:
space:
mode:
authorPrateek Chaubey <chaubeyprateek@gmail.com>2018-01-07 20:55:14 +0530
committerDavide Garberi <dade.garberi@gmail.com>2018-01-19 14:09:15 +0100
commit6616278131edd80a12545085e06ee6b0e0a0a788 (patch)
tree0aef88ed11809a9d67f6abe4dc2ff782a14737e2 /camera/QCamera2/stack/mm-jpeg-interface
parentcc4ccf34871da343111bf68d16ba4e4c67cac1dc (diff)
msm8996-common: zuk: Import OSS Camera HAL
Tag: LA.HB.1.3.2-32600-8x96.0 Signed-off-by: Davide Garberi <dade.garberi@gmail.com>
Diffstat (limited to 'camera/QCamera2/stack/mm-jpeg-interface')
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/Android.mk82
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg.h539
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_dbg.h55
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_inlines.h127
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_ionbuf.h105
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_mpo.h45
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg.c3788
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_exif.c652
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_interface.c409
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_ionbuf.c206
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_mpo_composer.c414
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_queue.c186
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec.c1185
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec_interface.c301
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/test/Android.mk87
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/test/mm_jpeg_test.c776
-rw-r--r--camera/QCamera2/stack/mm-jpeg-interface/test/mm_jpegdec_test.c479
17 files changed, 9436 insertions, 0 deletions
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/Android.mk b/camera/QCamera2/stack/mm-jpeg-interface/Android.mk
new file mode 100644
index 0000000..175796b
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/Android.mk
@@ -0,0 +1,82 @@
+OLD_LOCAL_PATH := $(LOCAL_PATH)
+LOCAL_PATH := $(call my-dir)
+
+include $(LOCAL_PATH)/../../../common.mk
+include $(CLEAR_VARS)
+
+LOCAL_32_BIT_ONLY := $(BOARD_QTI_CAMERA_32BIT_ONLY)
+LOCAL_CFLAGS+= -D_ANDROID_ -DQCAMERA_REDEFINE_LOG
+
+LOCAL_CFLAGS += -Wall -Wextra -Werror -Wno-unused-parameter
+
+LOCAL_C_INCLUDES+= $(kernel_includes)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps)
+
+LIB2D_ROTATION=false
+
+LOCAL_C_INCLUDES += \
+ frameworks/native/include/media/openmax \
+ $(LOCAL_PATH)/inc \
+ $(LOCAL_PATH)/../common \
+ $(LOCAL_PATH)/../mm-camera-interface/inc \
+ $(LOCAL_PATH)/../../.. \
+ $(LOCAL_PATH)/../../../mm-image-codec/qexif \
+ $(LOCAL_PATH)/../../../mm-image-codec/qomx_core
+
+ifeq ($(strip $(LIB2D_ROTATION)),true)
+ LOCAL_C_INCLUDES += $(LOCAL_PATH)/../mm-lib2d-interface/inc
+ LOCAL_CFLAGS += -DLIB2D_ROTATION_ENABLE
+endif
+
+
+ifeq ($(strip $(TARGET_USES_ION)),true)
+ LOCAL_CFLAGS += -DUSE_ION
+endif
+
+ifneq (,$(filter msm8610,$(TARGET_BOARD_PLATFORM)))
+ LOCAL_CFLAGS+= -DLOAD_ADSP_RPC_LIB
+endif
+
+DUAL_JPEG_TARGET_LIST := msm8974
+DUAL_JPEG_TARGET_LIST += msm8994
+
+ifneq (,$(filter $(DUAL_JPEG_TARGET_LIST),$(TARGET_BOARD_PLATFORM)))
+ LOCAL_CFLAGS+= -DMM_JPEG_CONCURRENT_SESSIONS_COUNT=2
+else
+ LOCAL_CFLAGS+= -DMM_JPEG_CONCURRENT_SESSIONS_COUNT=1
+endif
+
+JPEG_PIPELINE_TARGET_LIST := msm8994
+JPEG_PIPELINE_TARGET_LIST += msm8992
+JPEG_PIPELINE_TARGET_LIST += msm8996
+JPEG_PIPELINE_TARGET_LIST += msmcobalt
+
+ifneq (,$(filter $(JPEG_PIPELINE_TARGET_LIST),$(TARGET_BOARD_PLATFORM)))
+ LOCAL_CFLAGS+= -DMM_JPEG_USE_PIPELINE
+endif
+
+# System header file path prefix
+LOCAL_CFLAGS += -DSYSTEM_HEADER_PREFIX=sys
+
+LOCAL_SRC_FILES := \
+ src/mm_jpeg_queue.c \
+ src/mm_jpeg_exif.c \
+ src/mm_jpeg.c \
+ src/mm_jpeg_interface.c \
+ src/mm_jpeg_ionbuf.c \
+ src/mm_jpegdec_interface.c \
+ src/mm_jpegdec.c \
+ src/mm_jpeg_mpo_composer.c
+
+LOCAL_MODULE := libmmjpeg_interface
+LOCAL_PRELINK_MODULE := false
+LOCAL_SHARED_LIBRARIES := libdl libcutils liblog libqomx_core libmmcamera_interface
+ifeq ($(strip $(LIB2D_ROTATION)),true)
+ LOCAL_SHARED_LIBRARIES += libmmlib2d_interface
+endif
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_32_BIT_ONLY := $(BOARD_QTI_CAMERA_32BIT_ONLY)
+include $(BUILD_SHARED_LIBRARY)
+
+LOCAL_PATH := $(OLD_LOCAL_PATH)
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg.h b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg.h
new file mode 100644
index 0000000..885a4b8
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg.h
@@ -0,0 +1,539 @@
+/* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef MM_JPEG_H_
+#define MM_JPEG_H_
+
+// OpenMAX dependencies
+#include "OMX_Types.h"
+#include "OMX_Index.h"
+#include "OMX_Core.h"
+#include "OMX_Component.h"
+#include "QOMX_JpegExtensions.h"
+
+// JPEG dependencies
+#include "mm_jpeg_interface.h"
+#include "mm_jpeg_ionbuf.h"
+
+// Camera dependencies
+#include "cam_list.h"
+#include "cam_semaphore.h"
+
+#define MM_JPEG_MAX_THREADS 30
+#define MM_JPEG_CIRQ_SIZE 30
+#define MM_JPEG_MAX_SESSION 10
+#define MAX_EXIF_TABLE_ENTRIES 50
+#define MAX_JPEG_SIZE 20000000
+#define MAX_OMX_HANDLES (5)
+// Thumbnail src and dest aspect ratio diffrence tolerance
+#define ASPECT_TOLERANCE 0.001
+
+
+/** mm_jpeg_abort_state_t:
+ * @MM_JPEG_ABORT_NONE: Abort is not issued
+ * @MM_JPEG_ABORT_INIT: Abort is issued from the client
+ * @MM_JPEG_ABORT_DONE: Abort is completed
+ *
+ * State representing the abort state
+ **/
+typedef enum {
+ MM_JPEG_ABORT_NONE,
+ MM_JPEG_ABORT_INIT,
+ MM_JPEG_ABORT_DONE,
+} mm_jpeg_abort_state_t;
+
+
+/* define max num of supported concurrent jpeg jobs by OMX engine.
+ * Current, only one per time */
+#define NUM_MAX_JPEG_CNCURRENT_JOBS 2
+
+#define JOB_ID_MAGICVAL 0x1
+#define JOB_HIST_MAX 10000
+
+/** DUMP_TO_FILE:
+ * @filename: file name
+ * @p_addr: address of the buffer
+ * @len: buffer length
+ *
+ * dump the image to the file
+ **/
+#define DUMP_TO_FILE(filename, p_addr, len) ({ \
+ size_t rc = 0; \
+ FILE *fp = fopen(filename, "w+"); \
+ if (fp) { \
+ rc = fwrite(p_addr, 1, len, fp); \
+ LOGE("written size %zu", len); \
+ fclose(fp); \
+ } else { \
+ LOGE("open %s failed", filename); \
+ } \
+})
+
+/** DUMP_TO_FILE2:
+ * @filename: file name
+ * @p_addr: address of the buffer
+ * @len: buffer length
+ *
+ * dump the image to the file if the memory is non-contiguous
+ **/
+#define DUMP_TO_FILE2(filename, p_addr1, len1, paddr2, len2) ({ \
+ size_t rc = 0; \
+ FILE *fp = fopen(filename, "w+"); \
+ if (fp) { \
+ rc = fwrite(p_addr1, 1, len1, fp); \
+ rc = fwrite(p_addr2, 1, len2, fp); \
+ LOGE("written %zu %zu", len1, len2); \
+ fclose(fp); \
+ } else { \
+ LOGE("open %s failed", filename); \
+ } \
+})
+
+/** MM_JPEG_CHK_ABORT:
+ * @p: client pointer
+ * @ret: return value
+ * @label: label to jump to
+ *
+ * check the abort failure
+ **/
+#define MM_JPEG_CHK_ABORT(p, ret, label) ({ \
+ if (MM_JPEG_ABORT_INIT == p->abort_state) { \
+ LOGE("jpeg abort"); \
+ ret = OMX_ErrorNone; \
+ goto label; \
+ } \
+})
+
+#define GET_CLIENT_IDX(x) ((x) & 0xff)
+#define GET_SESSION_IDX(x) (((x) >> 8) & 0xff)
+#define GET_JOB_IDX(x) (((x) >> 16) & 0xff)
+
+typedef struct {
+ union {
+ int i_data[MM_JPEG_CIRQ_SIZE];
+ void *p_data[MM_JPEG_CIRQ_SIZE];
+ };
+ int front;
+ int rear;
+ int count;
+ pthread_mutex_t lock;
+} mm_jpeg_cirq_t;
+
+/** cirq_reset:
+ *
+ * Arguments:
+ * @q: circular queue
+ *
+ * Return:
+ * none
+ *
+ * Description:
+ * Resets the circular queue
+ *
+ **/
+static inline void cirq_reset(mm_jpeg_cirq_t *q)
+{
+ q->front = 0;
+ q->rear = 0;
+ q->count = 0;
+ pthread_mutex_init(&q->lock, NULL);
+}
+
+/** cirq_empty:
+ *
+ * Arguments:
+ * @q: circular queue
+ *
+ * Return:
+ * none
+ *
+ * Description:
+ * check if the curcular queue is empty
+ *
+ **/
+#define cirq_empty(q) (q->count == 0)
+
+/** cirq_full:
+ *
+ * Arguments:
+ * @q: circular queue
+ *
+ * Return:
+ * none
+ *
+ * Description:
+ * check if the curcular queue is full
+ *
+ **/
+#define cirq_full(q) (q->count == MM_JPEG_CIRQ_SIZE)
+
+/** cirq_enqueue:
+ *
+ * Arguments:
+ * @q: circular queue
+ * @data: data to be inserted
+ *
+ * Return:
+ * true/false
+ *
+ * Description:
+ * enqueue an element into circular queue
+ *
+ **/
+#define cirq_enqueue(q, type, data) ({ \
+ int rc = 0; \
+ pthread_mutex_lock(&q->lock); \
+ if (cirq_full(q)) { \
+ rc = -1; \
+ } else { \
+ q->type[q->rear] = data; \
+ q->rear = (q->rear + 1) % MM_JPEG_CIRQ_SIZE; \
+ q->count++; \
+ } \
+ pthread_mutex_unlock(&q->lock); \
+ rc; \
+})
+
+/** cirq_dequeue:
+ *
+ * Arguments:
+ * @q: circular queue
+ * @data: data to be popped
+ *
+ * Return:
+ * true/false
+ *
+ * Description:
+ * dequeue an element from the circular queue
+ *
+ **/
+#define cirq_dequeue(q, type, data) ({ \
+ int rc = 0; \
+ pthread_mutex_lock(&q->lock); \
+ if (cirq_empty(q)) { \
+ pthread_mutex_unlock(&q->lock); \
+ rc = -1; \
+ } else { \
+ data = q->type[q->front]; \
+ q->count--; \
+ } \
+ pthread_mutex_unlock(&q->lock); \
+ rc; \
+})
+
+
+typedef union {
+ uint32_t u32;
+ void* p;
+} mm_jpeg_q_data_t;
+
+ typedef struct {
+ struct cam_list list;
+ mm_jpeg_q_data_t data;
+} mm_jpeg_q_node_t;
+
+typedef struct {
+ mm_jpeg_q_node_t head; /* dummy head */
+ uint32_t size;
+ pthread_mutex_t lock;
+} mm_jpeg_queue_t;
+
+typedef enum {
+ MM_JPEG_CMD_TYPE_JOB, /* job cmd */
+ MM_JPEG_CMD_TYPE_EXIT, /* EXIT cmd for exiting jobMgr thread */
+ MM_JPEG_CMD_TYPE_DECODE_JOB,
+ MM_JPEG_CMD_TYPE_MAX
+} mm_jpeg_cmd_type_t;
+
+typedef struct mm_jpeg_job_session {
+ uint32_t client_hdl; /* client handler */
+ uint32_t jobId; /* job ID */
+ uint32_t sessionId; /* session ID */
+ mm_jpeg_encode_params_t params; /* encode params */
+ mm_jpeg_decode_params_t dec_params; /* encode params */
+ mm_jpeg_encode_job_t encode_job; /* job description */
+ mm_jpeg_decode_job_t decode_job;
+ pthread_t encode_pid; /* encode thread handler*/
+
+ void *jpeg_obj; /* ptr to mm_jpeg_obj */
+ jpeg_job_status_t job_status; /* job status */
+
+ int state_change_pending; /* flag to indicate if state change is pending */
+ OMX_ERRORTYPE error_flag; /* variable to indicate error during encoding */
+ mm_jpeg_abort_state_t abort_state; /* variable to indicate abort during encoding */
+
+ /* OMX related */
+ OMX_HANDLETYPE omx_handle; /* handle to omx engine */
+ OMX_CALLBACKTYPE omx_callbacks; /* callbacks to omx engine */
+
+ /* buffer headers */
+ OMX_BUFFERHEADERTYPE *p_in_omx_buf[MM_JPEG_MAX_BUF];
+ OMX_BUFFERHEADERTYPE *p_in_omx_thumb_buf[MM_JPEG_MAX_BUF];
+ OMX_BUFFERHEADERTYPE *p_out_omx_buf[MM_JPEG_MAX_BUF];
+ OMX_BUFFERHEADERTYPE *p_in_rot_omx_buf[MM_JPEG_MAX_BUF];
+ OMX_BUFFERHEADERTYPE *p_in_rot_omx_thumb_buf[MM_JPEG_MAX_BUF];
+
+ OMX_PARAM_PORTDEFINITIONTYPE inputPort;
+ OMX_PARAM_PORTDEFINITIONTYPE outputPort;
+ OMX_PARAM_PORTDEFINITIONTYPE inputTmbPort;
+
+ /* event locks */
+ pthread_mutex_t lock;
+ pthread_cond_t cond;
+
+ QEXIF_INFO_DATA exif_info_local[MAX_EXIF_TABLE_ENTRIES]; //all exif tags for JPEG encoder
+ int exif_count_local;
+
+ mm_jpeg_cirq_t cb_q;
+ int32_t ebd_count;
+ int32_t fbd_count;
+
+ /* this flag represents whether the job is active */
+ OMX_BOOL active;
+
+ /* this flag indicates if the configration is complete */
+ OMX_BOOL config;
+
+ /* job history count to generate unique id */
+ unsigned int job_hist;
+
+ OMX_BOOL encoding;
+
+ buffer_t work_buffer;
+ /* src rotate ion bufs */
+ buffer_t src_rot_ion_buffer[MM_JPEG_MAX_BUF];
+
+ OMX_EVENTTYPE omxEvent;
+ int event_pending;
+
+ uint8_t *meta_enc_key;
+ size_t meta_enc_keylen;
+
+ struct mm_jpeg_job_session *next_session;
+
+ uint32_t curr_out_buf_idx;
+
+ uint32_t num_omx_sessions;
+ OMX_BOOL auto_out_buf;
+
+ mm_jpeg_queue_t *session_handle_q;
+ mm_jpeg_queue_t *out_buf_q;
+
+ int thumb_from_main;
+ uint32_t job_index;
+
+ /* lib2d rotation flag*/
+ uint32_t lib2d_rotation_flag;
+
+ /* num of buf for input src rotation */
+ uint32_t num_src_rot_bufs;
+
+ /* src rotate img bufs */
+ mm_jpeg_buf_t src_rot_main_buf[MM_JPEG_MAX_BUF];
+
+ /* lib2d handle*/
+ void *lib2d_handle;
+} mm_jpeg_job_session_t;
+
+typedef struct {
+ mm_jpeg_encode_job_t encode_job;
+ uint32_t job_id;
+ uint32_t client_handle;
+} mm_jpeg_encode_job_info_t;
+
+typedef struct {
+ mm_jpeg_decode_job_t decode_job;
+ uint32_t job_id;
+ uint32_t client_handle;
+} mm_jpeg_decode_job_info_t;
+
+typedef struct {
+ mm_jpeg_cmd_type_t type;
+ union {
+ mm_jpeg_encode_job_info_t enc_info;
+ mm_jpeg_decode_job_info_t dec_info;
+ };
+} mm_jpeg_job_q_node_t;
+
+typedef struct {
+ uint8_t is_used; /* flag: if is a valid client */
+ uint32_t client_handle; /* client handle */
+ mm_jpeg_job_session_t session[MM_JPEG_MAX_SESSION];
+ pthread_mutex_t lock; /* job lock */
+} mm_jpeg_client_t;
+
+typedef struct {
+ pthread_t pid; /* job cmd thread ID */
+ cam_semaphore_t job_sem; /* semaphore for job cmd thread */
+ mm_jpeg_queue_t job_queue; /* queue for job to do */
+} mm_jpeg_job_cmd_thread_t;
+
+#define MAX_JPEG_CLIENT_NUM 8
+typedef struct mm_jpeg_obj_t {
+ /* ClientMgr */
+ int num_clients; /* num of clients */
+ mm_jpeg_client_t clnt_mgr[MAX_JPEG_CLIENT_NUM]; /* client manager */
+
+ /* JobMkr */
+ pthread_mutex_t job_lock; /* job lock */
+ mm_jpeg_job_cmd_thread_t job_mgr; /* job mgr thread including todo_q*/
+ mm_jpeg_queue_t ongoing_job_q; /* queue for ongoing jobs */
+ buffer_t ionBuffer[MM_JPEG_CONCURRENT_SESSIONS_COUNT];
+
+
+ /* Max pic dimension for work buf calc*/
+ uint32_t max_pic_w;
+ uint32_t max_pic_h;
+#ifdef LOAD_ADSP_RPC_LIB
+ void *adsprpc_lib_handle;
+#endif
+
+ uint32_t work_buf_cnt;
+
+ uint32_t num_sessions;
+ uint32_t reuse_reproc_buffer;
+
+ cam_jpeg_metadata_t *jpeg_metadata;
+
+ /* Pointer to the session in progress*/
+ mm_jpeg_job_session_t *p_session_inprogress;
+
+ // dummy OMX handle
+ OMX_HANDLETYPE dummy_handle;
+} mm_jpeg_obj;
+
+/** mm_jpeg_pending_func_t:
+ *
+ * Intermediate function for transition change
+ **/
+typedef OMX_ERRORTYPE (*mm_jpeg_transition_func_t)(void *);
+
+extern int32_t mm_jpeg_init(mm_jpeg_obj *my_obj);
+extern int32_t mm_jpeg_deinit(mm_jpeg_obj *my_obj);
+extern uint32_t mm_jpeg_new_client(mm_jpeg_obj *my_obj);
+extern int32_t mm_jpeg_start_job(mm_jpeg_obj *my_obj,
+ mm_jpeg_job_t* job,
+ uint32_t* jobId);
+extern int32_t mm_jpeg_abort_job(mm_jpeg_obj *my_obj,
+ uint32_t jobId);
+extern int32_t mm_jpeg_close(mm_jpeg_obj *my_obj,
+ uint32_t client_hdl);
+extern int32_t mm_jpeg_create_session(mm_jpeg_obj *my_obj,
+ uint32_t client_hdl,
+ mm_jpeg_encode_params_t *p_params,
+ uint32_t* p_session_id);
+extern int32_t mm_jpeg_destroy_session_by_id(mm_jpeg_obj *my_obj,
+ uint32_t session_id);
+
+extern int32_t mm_jpegdec_init(mm_jpeg_obj *my_obj);
+extern int32_t mm_jpegdec_deinit(mm_jpeg_obj *my_obj);
+extern int32_t mm_jpeg_jobmgr_thread_release(mm_jpeg_obj * my_obj);
+extern int32_t mm_jpeg_jobmgr_thread_launch(mm_jpeg_obj *my_obj);
+extern int32_t mm_jpegdec_start_decode_job(mm_jpeg_obj *my_obj,
+ mm_jpeg_job_t* job,
+ uint32_t* jobId);
+
+extern int32_t mm_jpegdec_create_session(mm_jpeg_obj *my_obj,
+ uint32_t client_hdl,
+ mm_jpeg_decode_params_t *p_params,
+ uint32_t* p_session_id);
+
+extern int32_t mm_jpegdec_destroy_session_by_id(mm_jpeg_obj *my_obj,
+ uint32_t session_id);
+
+extern int32_t mm_jpegdec_abort_job(mm_jpeg_obj *my_obj,
+ uint32_t jobId);
+
+int32_t mm_jpegdec_process_decoding_job(mm_jpeg_obj *my_obj,
+ mm_jpeg_job_q_node_t* job_node);
+
+/* utiltity fucntion declared in mm-camera-inteface2.c
+ * and need be used by mm-camera and below*/
+uint32_t mm_jpeg_util_generate_handler(uint8_t index);
+uint8_t mm_jpeg_util_get_index_by_handler(uint32_t handler);
+
+/* basic queue functions */
+extern int32_t mm_jpeg_queue_init(mm_jpeg_queue_t* queue);
+extern int32_t mm_jpeg_queue_enq(mm_jpeg_queue_t* queue,
+ mm_jpeg_q_data_t data);
+extern int32_t mm_jpeg_queue_enq_head(mm_jpeg_queue_t* queue,
+ mm_jpeg_q_data_t data);
+extern mm_jpeg_q_data_t mm_jpeg_queue_deq(mm_jpeg_queue_t* queue);
+extern int32_t mm_jpeg_queue_deinit(mm_jpeg_queue_t* queue);
+extern int32_t mm_jpeg_queue_flush(mm_jpeg_queue_t* queue);
+extern uint32_t mm_jpeg_queue_get_size(mm_jpeg_queue_t* queue);
+extern mm_jpeg_q_data_t mm_jpeg_queue_peek(mm_jpeg_queue_t* queue);
+extern int32_t addExifEntry(QOMX_EXIF_INFO *p_exif_info, exif_tag_id_t tagid,
+ exif_tag_type_t type, uint32_t count, void *data);
+extern int32_t releaseExifEntry(QEXIF_INFO_DATA *p_exif_data);
+extern int process_meta_data(metadata_buffer_t *p_meta,
+ QOMX_EXIF_INFO *exif_info, mm_jpeg_exif_params_t *p_cam3a_params,
+ cam_hal_version_t hal_version);
+
+OMX_ERRORTYPE mm_jpeg_session_change_state(mm_jpeg_job_session_t* p_session,
+ OMX_STATETYPE new_state,
+ mm_jpeg_transition_func_t p_exec);
+
+int map_jpeg_format(mm_jpeg_color_format color_fmt);
+
+OMX_BOOL mm_jpeg_session_abort(mm_jpeg_job_session_t *p_session);
+/**
+ *
+ * special queue functions for job queue
+ **/
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_client_id(
+ mm_jpeg_queue_t* queue, uint32_t client_hdl);
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_job_id(
+ mm_jpeg_queue_t* queue, uint32_t job_id);
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_session_id(
+ mm_jpeg_queue_t* queue, uint32_t session_id);
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_unlk(
+ mm_jpeg_queue_t* queue, uint32_t job_id);
+
+
+/** mm_jpeg_queue_func_t:
+ *
+ * Intermediate function for queue operation
+ **/
+typedef void (*mm_jpeg_queue_func_t)(void *);
+
+/** mm_jpeg_exif_flash_mode:
+ *
+ * Exif flash mode values
+ **/
+typedef enum {
+ MM_JPEG_EXIF_FLASH_MODE_ON = 0x1,
+ MM_JPEG_EXIF_FLASH_MODE_OFF = 0x2,
+ MM_JPEG_EXIF_FLASH_MODE_AUTO = 0x3,
+ MM_JPEG_EXIF_FLASH_MODE_MAX
+} mm_jpeg_exif_flash_mode;
+
+#endif /* MM_JPEG_H_ */
+
+
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_dbg.h b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_dbg.h
new file mode 100644
index 0000000..2269537
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_dbg.h
@@ -0,0 +1,55 @@
+/* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef __MM_JPEG_DBG_H__
+#define __MM_JPEG_DBG_H__
+
+#ifdef QCAMERA_REDEFINE_LOG
+#define CAM_MODULE CAM_JPEG_MODULE
+#include "mm_camera_dbg.h"
+#endif
+
+extern volatile uint32_t gKpiDebugLevel;
+
+#ifndef KPI_DEBUG
+#define KPI_DEBUG
+#define ATRACE_TAG ATRACE_TAG_CAMERA
+#include <cutils/trace.h>
+
+#define KPI_APT 1
+#define KPI_DBG 2
+
+#define KPI_ATRACE_INT(name,val) ({\
+if (gKpiDebugLevel >= KPI_APT) { \
+ atrace_int(ATRACE_TAG, name, val); \
+}\
+})
+
+#endif
+#endif /* __MM_JPEG_DBG_H__ */
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_inlines.h b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_inlines.h
new file mode 100644
index 0000000..d2ca63d
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_inlines.h
@@ -0,0 +1,127 @@
+/* Copyright (c) 2013, 2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef MM_JPEG_INLINES_H_
+#define MM_JPEG_INLINES_H_
+
+// JPEG dependencies
+#include "mm_jpeg.h"
+
+/** mm_jpeg_get_session:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_idx: client index
+ *
+ * Return:
+ * job index
+ *
+ * Description:
+ * Get job index by client id
+ *
+ **/
+static inline mm_jpeg_job_session_t *mm_jpeg_get_session(mm_jpeg_obj *my_obj, uint32_t job_id)
+{
+ mm_jpeg_job_session_t *p_session = NULL;
+ int client_idx = GET_CLIENT_IDX(job_id);
+ int session_idx= GET_SESSION_IDX(job_id);
+
+ LOGD("client_idx %d session_idx %d",
+ client_idx, session_idx);
+ if ((session_idx >= MM_JPEG_MAX_SESSION) ||
+ (client_idx >= MAX_JPEG_CLIENT_NUM)) {
+ LOGE("invalid job id %x",
+ job_id);
+ return NULL;
+ }
+ pthread_mutex_lock(&my_obj->clnt_mgr[client_idx].lock);
+ p_session = &my_obj->clnt_mgr[client_idx].session[session_idx];
+ pthread_mutex_unlock(&my_obj->clnt_mgr[client_idx].lock);
+ return p_session;
+}
+
+/** mm_jpeg_get_job_idx:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_idx: client index
+ *
+ * Return:
+ * job index
+ *
+ * Description:
+ * Get job index by client id
+ *
+ **/
+static inline int mm_jpeg_get_new_session_idx(mm_jpeg_obj *my_obj, int client_idx,
+ mm_jpeg_job_session_t **pp_session)
+{
+ int i = 0;
+ int index = -1;
+ for (i = 0; i < MM_JPEG_MAX_SESSION; i++) {
+ pthread_mutex_lock(&my_obj->clnt_mgr[client_idx].lock);
+ if (!my_obj->clnt_mgr[client_idx].session[i].active) {
+ *pp_session = &my_obj->clnt_mgr[client_idx].session[i];
+ my_obj->clnt_mgr[client_idx].session[i].active = OMX_TRUE;
+ index = i;
+ pthread_mutex_unlock(&my_obj->clnt_mgr[client_idx].lock);
+ break;
+ }
+ pthread_mutex_unlock(&my_obj->clnt_mgr[client_idx].lock);
+ }
+ return index;
+}
+
+/** mm_jpeg_get_job_idx:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_idx: client index
+ *
+ * Return:
+ * job index
+ *
+ * Description:
+ * Get job index by client id
+ *
+ **/
+static inline void mm_jpeg_remove_session_idx(mm_jpeg_obj *my_obj, uint32_t job_id)
+{
+ int client_idx = GET_CLIENT_IDX(job_id);
+ int session_idx= GET_SESSION_IDX(job_id);
+ LOGD("client_idx %d session_idx %d",
+ client_idx, session_idx);
+ pthread_mutex_lock(&my_obj->clnt_mgr[client_idx].lock);
+ my_obj->clnt_mgr[client_idx].session[session_idx].active = OMX_FALSE;
+ pthread_mutex_unlock(&my_obj->clnt_mgr[client_idx].lock);
+}
+
+
+
+#endif /* MM_JPEG_INLINES_H_ */
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_ionbuf.h b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_ionbuf.h
new file mode 100644
index 0000000..96b70d9
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_ionbuf.h
@@ -0,0 +1,105 @@
+/* Copyright (c) 2013-2014, 2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef __MM_JPEG_IONBUF_H__
+#define __MM_JPEG_IONBUF_H__
+
+// System dependencies
+#include <linux/msm_ion.h>
+
+// JPEG dependencies
+#include "mm_jpeg_dbg.h"
+
+typedef struct {
+ struct ion_fd_data ion_info_fd;
+ struct ion_allocation_data alloc;
+ int p_pmem_fd;
+ size_t size;
+ int ion_fd;
+ uint8_t *addr;
+} buffer_t;
+
+/** buffer_allocate:
+ *
+ * Arguments:
+ * @p_buffer: ION buffer
+ *
+ * Return:
+ * buffer address
+ *
+ * Description:
+ * allocates ION buffer
+ *
+ **/
+void* buffer_allocate(buffer_t *p_buffer, int cached);
+
+/** buffer_deallocate:
+ *
+ * Arguments:
+ * @p_buffer: ION buffer
+ *
+ * Return:
+ * error val
+ *
+ * Description:
+ * deallocates ION buffer
+ *
+ **/
+int buffer_deallocate(buffer_t *p_buffer);
+
+/** buffer_invalidate:
+ *
+ * Arguments:
+ * @p_buffer: ION buffer
+ *
+ * Return:
+ * error val
+ *
+ * Description:
+ * Invalidates the cached buffer
+ *
+ **/
+int buffer_invalidate(buffer_t *p_buffer);
+
+/** buffer_clean:
+ *
+ * Arguments:
+ * @p_buffer: ION buffer
+ *
+ * Return:
+ * error val
+ *
+ * Description:
+ * clean the cached buffer
+ *
+ **/
+int buffer_clean(buffer_t *p_buffer);
+
+#endif
+
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_mpo.h b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_mpo.h
new file mode 100644
index 0000000..6e8424c
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/inc/mm_jpeg_mpo.h
@@ -0,0 +1,45 @@
+/* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef MM_JPEG_MPO_H_
+#define MM_JPEG_MPO_H_
+
+// JPEG dependencies
+#include "mm_jpeg_interface.h"
+#include "qmpo.h"
+
+#define TRUE 1
+#define FALSE 0
+
+extern int mm_jpeg_mpo_compose(mm_jpeg_mpo_info_t *mpo_info);
+
+extern int get_mpo_size(mm_jpeg_output_t jpeg_buffer[MM_JPEG_MAX_MPO_IMAGES],
+ int num_of_images);
+
+#endif
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg.c b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg.c
new file mode 100644
index 0000000..19a8b29
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg.c
@@ -0,0 +1,3788 @@
+/* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// System dependencies
+#include <pthread.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <math.h>
+#define PRCTL_H <SYSTEM_HEADER_PREFIX/prctl.h>
+#include PRCTL_H
+
+#ifdef LOAD_ADSP_RPC_LIB
+#include <dlfcn.h>
+#include <stdlib.h>
+#endif
+
+// JPEG dependencies
+#include "mm_jpeg_dbg.h"
+#include "mm_jpeg_interface.h"
+#include "mm_jpeg.h"
+#include "mm_jpeg_inlines.h"
+#ifdef LIB2D_ROTATION_ENABLE
+#include "mm_lib2d.h"
+#endif
+
+#define ENCODING_MODE_PARALLEL 1
+
+#define META_KEYFILE QCAMERA_DUMP_FRM_LOCATION"metadata.key"
+
+/**
+ * minimal resolution needed for normal mode of ops
+ */
+#define MM_JPEG_MIN_NOM_RESOLUTION 7680000 /*8MP*/
+
+#ifdef MM_JPEG_USE_PIPELINE
+#undef MM_JPEG_CONCURRENT_SESSIONS_COUNT
+#define MM_JPEG_CONCURRENT_SESSIONS_COUNT 1
+#endif
+
+OMX_ERRORTYPE mm_jpeg_ebd(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_BUFFERHEADERTYPE* pBuffer);
+OMX_ERRORTYPE mm_jpeg_fbd(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_BUFFERHEADERTYPE* pBuffer);
+OMX_ERRORTYPE mm_jpeg_event_handler(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_EVENTTYPE eEvent,
+ OMX_U32 nData1,
+ OMX_U32 nData2,
+ OMX_PTR pEventData);
+
+static int32_t mm_jpegenc_destroy_job(mm_jpeg_job_session_t *p_session);
+static void mm_jpegenc_job_done(mm_jpeg_job_session_t *p_session);
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_dst_ptr(
+ mm_jpeg_queue_t* queue, void * dst_ptr);
+static OMX_ERRORTYPE mm_jpeg_session_configure(mm_jpeg_job_session_t *p_session);
+
+/** mm_jpeg_get_comp_name:
+ *
+ * Arguments:
+ * None
+ *
+ * Return:
+ * Encoder component name
+ *
+ * Description:
+ * Get the name of omx component to be used for jpeg encoding
+ *
+ **/
+inline char* mm_jpeg_get_comp_name()
+{
+#ifdef MM_JPEG_USE_PIPELINE
+ return "OMX.qcom.image.jpeg.encoder_pipeline";
+#else
+ return "OMX.qcom.image.jpeg.encoder";
+#endif
+}
+
+/** mm_jpeg_session_send_buffers:
+ *
+ * Arguments:
+ * @data: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Send the buffers to OMX layer
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_send_buffers(void *data)
+{
+ uint32_t i = 0;
+ mm_jpeg_job_session_t* p_session = (mm_jpeg_job_session_t *)data;
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ QOMX_BUFFER_INFO lbuffer_info;
+ mm_jpeg_encode_params_t *p_params = &p_session->params;
+
+ memset(&lbuffer_info, 0x0, sizeof(QOMX_BUFFER_INFO));
+
+ if (p_session->lib2d_rotation_flag) {
+ for (i = 0; i < p_session->num_src_rot_bufs; i++) {
+ lbuffer_info.fd = (OMX_U32)p_session->src_rot_main_buf[i].fd;
+ LOGD("Source rot buffer %d", i);
+ ret = OMX_UseBuffer(p_session->omx_handle,
+ &(p_session->p_in_rot_omx_buf[i]), 0,
+ &lbuffer_info, p_session->src_rot_main_buf[i].buf_size,
+ p_session->src_rot_main_buf[i].buf_vaddr);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+ } else {
+ for (i = 0; i < p_params->num_src_bufs; i++) {
+ LOGD("Source buffer %d", i);
+ lbuffer_info.fd = (OMX_U32)p_params->src_main_buf[i].fd;
+ ret = OMX_UseBuffer(p_session->omx_handle,
+ &(p_session->p_in_omx_buf[i]), 0,
+ &lbuffer_info, p_params->src_main_buf[i].buf_size,
+ p_params->src_main_buf[i].buf_vaddr);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+ }
+
+ if (p_session->params.encode_thumbnail) {
+ if (p_session->lib2d_rotation_flag && p_session->thumb_from_main) {
+ for (i = 0; i < p_session->num_src_rot_bufs; i++) {
+ LOGD("Source rot buffer thumb %d", i);
+ lbuffer_info.fd = (OMX_U32)p_session->src_rot_main_buf[i].fd;
+ ret = OMX_UseBuffer(p_session->omx_handle,
+ &(p_session->p_in_rot_omx_thumb_buf[i]), 2,
+ &lbuffer_info, p_session->src_rot_main_buf[i].buf_size,
+ p_session->src_rot_main_buf[i].buf_vaddr);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+ } else {
+ for (i = 0; i < p_params->num_tmb_bufs; i++) {
+ LOGD("Source tmb buffer %d", i);
+ lbuffer_info.fd = (OMX_U32)p_params->src_thumb_buf[i].fd;
+ ret = OMX_UseBuffer(p_session->omx_handle,
+ &(p_session->p_in_omx_thumb_buf[i]), 2,
+ &lbuffer_info, p_params->src_thumb_buf[i].buf_size,
+ p_params->src_thumb_buf[i].buf_vaddr);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+ }
+ }
+
+ for (i = 0; i < p_params->num_dst_bufs; i++) {
+ LOGD("Dest buffer %d", i);
+ ret = OMX_UseBuffer(p_session->omx_handle, &(p_session->p_out_omx_buf[i]),
+ 1, NULL, p_params->dest_buf[i].buf_size,
+ p_params->dest_buf[i].buf_vaddr);
+ if (ret) {
+ LOGE("Error");
+ return ret;
+ }
+ }
+
+ return ret;
+}
+
+
+/** mm_jpeg_session_free_buffers:
+ *
+ * Arguments:
+ * @data: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Free the buffers from OMX layer
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_free_buffers(void *data)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ uint32_t i = 0;
+ mm_jpeg_job_session_t* p_session = (mm_jpeg_job_session_t *)data;
+ mm_jpeg_encode_params_t *p_params = &p_session->params;
+
+
+ if (p_session->lib2d_rotation_flag) {
+ for (i = 0; i < p_session->num_src_rot_bufs; i++) {
+ LOGD("Source rot buffer %d", i);
+ ret = OMX_FreeBuffer(p_session->omx_handle, 0,
+ p_session->p_in_rot_omx_buf[i]);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+ } else {
+ for (i = 0; i < p_params->num_src_bufs; i++) {
+ LOGD("Source buffer %d", i);
+ ret = OMX_FreeBuffer(p_session->omx_handle, 0,
+ p_session->p_in_omx_buf[i]);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+ }
+
+ if (p_session->lib2d_rotation_flag && p_session->thumb_from_main) {
+ for (i = 0; i < p_session->num_src_rot_bufs; i++) {
+ LOGD("Source rot buffer thumb %d", i);
+ ret = OMX_FreeBuffer(p_session->omx_handle, 2,
+ p_session->p_in_rot_omx_thumb_buf[i]);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+ } else {
+ for (i = 0; i < p_params->num_tmb_bufs; i++) {
+ LOGD("Source buffer %d", i);
+ ret = OMX_FreeBuffer(p_session->omx_handle, 2,
+ p_session->p_in_omx_thumb_buf[i]);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+ }
+
+ for (i = 0; i < p_params->num_dst_bufs; i++) {
+ LOGD("Dest buffer %d", i);
+ ret = OMX_FreeBuffer(p_session->omx_handle, 1, p_session->p_out_omx_buf[i]);
+ if (ret) {
+ LOGE("Error");
+ return ret;
+ }
+ }
+ return ret;
+}
+
+
+
+
+/** mm_jpeg_session_change_state:
+ *
+ * Arguments:
+ * @p_session: job session
+ * @new_state: new state to be transitioned to
+ * @p_exec: transition function
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * This method is used for state transition
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_change_state(mm_jpeg_job_session_t* p_session,
+ OMX_STATETYPE new_state,
+ mm_jpeg_transition_func_t p_exec)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ OMX_STATETYPE current_state;
+ LOGD("new_state %d p_exec %p",
+ new_state, p_exec);
+
+
+ pthread_mutex_lock(&p_session->lock);
+
+ ret = OMX_GetState(p_session->omx_handle, &current_state);
+
+ if (ret) {
+ pthread_mutex_unlock(&p_session->lock);
+ return ret;
+ }
+
+ if (current_state == new_state) {
+ pthread_mutex_unlock(&p_session->lock);
+ return OMX_ErrorNone;
+ }
+
+ p_session->state_change_pending = OMX_TRUE;
+ pthread_mutex_unlock(&p_session->lock);
+ ret = OMX_SendCommand(p_session->omx_handle, OMX_CommandStateSet,
+ new_state, NULL);
+ pthread_mutex_lock(&p_session->lock);
+ if (ret) {
+ LOGE("Error %d", ret);
+ pthread_mutex_unlock(&p_session->lock);
+ return OMX_ErrorIncorrectStateTransition;
+ }
+ if ((OMX_ErrorNone != p_session->error_flag) &&
+ (OMX_ErrorOverflow != p_session->error_flag)) {
+ LOGE("Error %d", p_session->error_flag);
+ pthread_mutex_unlock(&p_session->lock);
+ return p_session->error_flag;
+ }
+ if (p_exec) {
+ ret = p_exec(p_session);
+ if (ret) {
+ LOGE("Error %d", ret);
+ pthread_mutex_unlock(&p_session->lock);
+ return ret;
+ }
+ }
+ if (p_session->state_change_pending) {
+ LOGL("before wait");
+ pthread_cond_wait(&p_session->cond, &p_session->lock);
+ LOGL("after wait");
+ }
+ pthread_mutex_unlock(&p_session->lock);
+ return ret;
+}
+
+/** mm_jpeg_session_create:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error types
+ *
+ * Description:
+ * Create a jpeg encode session
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_create(mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+ mm_jpeg_obj *my_obj = (mm_jpeg_obj *) p_session->jpeg_obj;
+
+ pthread_mutex_init(&p_session->lock, NULL);
+ pthread_cond_init(&p_session->cond, NULL);
+ cirq_reset(&p_session->cb_q);
+ p_session->state_change_pending = OMX_FALSE;
+ p_session->abort_state = MM_JPEG_ABORT_NONE;
+ p_session->error_flag = OMX_ErrorNone;
+ p_session->ebd_count = 0;
+ p_session->fbd_count = 0;
+ p_session->encode_pid = -1;
+ p_session->config = OMX_FALSE;
+ p_session->exif_count_local = 0;
+ p_session->auto_out_buf = OMX_FALSE;
+
+ p_session->omx_callbacks.EmptyBufferDone = mm_jpeg_ebd;
+ p_session->omx_callbacks.FillBufferDone = mm_jpeg_fbd;
+ p_session->omx_callbacks.EventHandler = mm_jpeg_event_handler;
+
+ p_session->thumb_from_main = 0;
+#ifdef MM_JPEG_USE_PIPELINE
+ p_session->thumb_from_main = !p_session->params.thumb_from_postview;
+#endif
+
+ rc = OMX_GetHandle(&p_session->omx_handle,
+ mm_jpeg_get_comp_name(),
+ (void *)p_session,
+ &p_session->omx_callbacks);
+ if (OMX_ErrorNone != rc) {
+ LOGE("OMX_GetHandle failed (%d)", rc);
+ return rc;
+ }
+
+ my_obj->num_sessions++;
+
+ return rc;
+}
+
+
+
+/** mm_jpeg_session_destroy:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * none
+ *
+ * Description:
+ * Destroy a jpeg encode session
+ *
+ **/
+void mm_jpeg_session_destroy(mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+ OMX_STATETYPE state;
+ uint32_t i;
+ mm_jpeg_obj *my_obj = (mm_jpeg_obj *) p_session->jpeg_obj;
+
+ LOGD("E");
+ if (NULL == p_session->omx_handle) {
+ LOGE("invalid handle");
+ return;
+ }
+
+ rc = OMX_GetState(p_session->omx_handle, &state);
+
+ //Check state before state transition
+ if ((state == OMX_StateExecuting) || (state == OMX_StatePause)) {
+ rc = mm_jpeg_session_change_state(p_session, OMX_StateIdle, NULL);
+ if (rc) {
+ LOGE("Error");
+ }
+ }
+
+ rc = OMX_GetState(p_session->omx_handle, &state);
+
+ if (state == OMX_StateIdle) {
+ rc = mm_jpeg_session_change_state(p_session, OMX_StateLoaded,
+ mm_jpeg_session_free_buffers);
+ if (rc) {
+ LOGE("Error");
+ }
+ }
+
+ if (p_session->lib2d_rotation_flag) {
+ for (i = 0; i < p_session->num_src_rot_bufs; i++) {
+ if (p_session->src_rot_ion_buffer[i].addr) {
+ buffer_deallocate(&p_session->src_rot_ion_buffer[i]);
+ }
+ }
+ }
+
+ /* If current session is the session in progress
+ set session in progress pointer to null*/
+ p_session->config = OMX_FALSE;
+ if (my_obj->p_session_inprogress == p_session) {
+ my_obj->p_session_inprogress = NULL;
+ }
+
+ rc = OMX_FreeHandle(p_session->omx_handle);
+ if (0 != rc) {
+ LOGE("OMX_FreeHandle failed (%d)", rc);
+ }
+ p_session->omx_handle = NULL;
+
+ pthread_mutex_destroy(&p_session->lock);
+ pthread_cond_destroy(&p_session->cond);
+
+ if (NULL != p_session->meta_enc_key) {
+ free(p_session->meta_enc_key);
+ p_session->meta_enc_key = NULL;
+ }
+
+ my_obj->num_sessions--;
+
+ // Destroy next session
+ if (p_session->next_session) {
+ mm_jpeg_session_destroy(p_session->next_session);
+ }
+
+ LOGD("Session destroy successful. X");
+}
+
+
+
+/** mm_jpeg_session_config_main_buffer_offset:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure the buffer offsets
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_config_main_buffer_offset(
+ mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = 0;
+ OMX_INDEXTYPE buffer_index;
+ QOMX_YUV_FRAME_INFO frame_info;
+ size_t totalSize = 0;
+ mm_jpeg_encode_params_t *p_params = &p_session->params;
+
+ mm_jpeg_buf_t *p_src_buf =
+ &p_params->src_main_buf[0];
+
+ memset(&frame_info, 0x0, sizeof(QOMX_YUV_FRAME_INFO));
+
+ frame_info.cbcrStartOffset[0] = p_src_buf->offset.mp[0].len;
+ frame_info.cbcrStartOffset[1] = p_src_buf->offset.mp[1].len;
+ if (!p_session->lib2d_rotation_flag) {
+ frame_info.yOffset = p_src_buf->offset.mp[0].offset;
+ frame_info.cbcrOffset[0] = p_src_buf->offset.mp[1].offset;
+ frame_info.cbcrOffset[1] = p_src_buf->offset.mp[2].offset;
+ }
+ totalSize = p_src_buf->buf_size;
+
+ rc = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_BUFFER_OFFSET_NAME, &buffer_index);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+
+ LOGD("yOffset = %d, cbcrOffset = (%d %d), totalSize = %zd,"
+ "cbcrStartOffset = (%d %d)",
+ (int)frame_info.yOffset,
+ (int)frame_info.cbcrOffset[0],
+ (int)frame_info.cbcrOffset[1],
+ totalSize,
+ (int)frame_info.cbcrStartOffset[0],
+ (int)frame_info.cbcrStartOffset[1]);
+
+ rc = OMX_SetParameter(p_session->omx_handle, buffer_index, &frame_info);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+ return rc;
+}
+
+/** mm_jpeg_encoding_mode:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure the serial or parallel encoding
+ * mode
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_encoding_mode(
+ mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = 0;
+ OMX_INDEXTYPE indextype;
+ QOMX_ENCODING_MODE encoding_mode;
+
+ rc = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_ENCODING_MODE_NAME, &indextype);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+
+ if (ENCODING_MODE_PARALLEL) {
+ encoding_mode = OMX_Parallel_Encoding;
+ } else {
+ encoding_mode = OMX_Serial_Encoding;
+ }
+ LOGD("encoding mode = %d ",
+ (int)encoding_mode);
+ rc = OMX_SetParameter(p_session->omx_handle, indextype, &encoding_mode);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+ return rc;
+}
+
+/** mm_jpeg_get_speed:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * ops speed type for jpeg
+ *
+ * Description:
+ * Configure normal or high speed jpeg
+ *
+ **/
+QOMX_JPEG_SPEED_MODE mm_jpeg_get_speed(
+ mm_jpeg_job_session_t* p_session)
+{
+ mm_jpeg_encode_params_t *p_params = &p_session->params;
+ cam_dimension_t *p_dim = &p_params->main_dim.src_dim;
+ if (p_params->burst_mode ||
+ (MM_JPEG_MIN_NOM_RESOLUTION < (p_dim->width * p_dim->height))) {
+ return QOMX_JPEG_SPEED_MODE_HIGH;
+ }
+ return QOMX_JPEG_SPEED_MODE_NORMAL;
+}
+
+/** mm_jpeg_speed_mode:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure normal or high speed jpeg
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_speed_mode(
+ mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = 0;
+ OMX_INDEXTYPE indextype;
+ QOMX_JPEG_SPEED jpeg_speed;
+
+ rc = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_JPEG_SPEED_NAME, &indextype);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+
+ jpeg_speed.speedMode = mm_jpeg_get_speed(p_session);
+ LOGH("speed %d", jpeg_speed.speedMode);
+
+ rc = OMX_SetParameter(p_session->omx_handle, indextype, &jpeg_speed);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+ return rc;
+}
+
+/** mm_jpeg_get_mem:
+ *
+ * Arguments:
+ * @p_out_buf : jpeg output buffer
+ * @p_jpeg_session: job session
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * gets the jpeg output buffer
+ *
+ **/
+static int32_t mm_jpeg_get_mem(
+ omx_jpeg_ouput_buf_t *p_out_buf, void* p_jpeg_session)
+{
+ int32_t rc = 0;
+ mm_jpeg_job_session_t *p_session = (mm_jpeg_job_session_t *)p_jpeg_session;
+ mm_jpeg_encode_params_t *p_params = NULL;
+ mm_jpeg_encode_job_t *p_encode_job = NULL;
+
+ if (!p_session) {
+ LOGE("Invalid input");
+ return -1;
+ }
+ p_params = &p_session->params;
+ p_encode_job = &p_session->encode_job;
+ if (!p_params || !p_encode_job || !p_params->get_memory) {
+ LOGE("Invalid jpeg encode params");
+ return -1;
+ }
+ p_params->get_memory(p_out_buf);
+ p_encode_job->ref_count++;
+ p_encode_job->alloc_out_buffer = p_out_buf;
+ LOGD("ref_count %d p_out_buf %p",
+ p_encode_job->ref_count, p_out_buf);
+ return rc;
+}
+
+/** mm_jpeg_put_mem:
+ *
+ * Arguments:
+ * @p_jpeg_session: job session
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * releases the jpeg output buffer
+ *
+ **/
+static int32_t mm_jpeg_put_mem(void* p_jpeg_session)
+{
+ int32_t rc = 0;
+ mm_jpeg_job_session_t *p_session = (mm_jpeg_job_session_t *)p_jpeg_session;
+ mm_jpeg_encode_params_t *p_params = NULL;
+ mm_jpeg_encode_job_t *p_encode_job = NULL;
+
+ if (!p_session) {
+ LOGE("Invalid input");
+ return -1;
+ }
+ p_params = &p_session->params;
+ p_encode_job = &p_session->encode_job;
+
+ if (!p_params->get_memory) {
+ LOGD("get_mem not defined, ignore put mem");
+ return 0;
+ }
+ if (!p_params || !p_encode_job || !p_params->put_memory) {
+ LOGE("Invalid jpeg encode params");
+ return -1;
+ }
+ if ((MM_JPEG_ABORT_NONE != p_session->abort_state) &&
+ p_encode_job->ref_count) {
+ omx_jpeg_ouput_buf_t *p_out_buf =
+ ( omx_jpeg_ouput_buf_t *) p_encode_job->alloc_out_buffer;
+ p_params->put_memory(p_out_buf);
+ p_encode_job->ref_count--;
+ p_encode_job->alloc_out_buffer = NULL;
+ } else if (p_encode_job->ref_count) {
+ p_encode_job->ref_count--;
+ } else {
+ LOGW("Buffer already released %d", p_encode_job->ref_count);
+ rc = -1;
+ }
+ LOGD("ref_count %d p_out_buf %p",
+ p_encode_job->ref_count, p_encode_job->alloc_out_buffer);
+ return rc;
+}
+
+/** mm_jpeg_mem_ops:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure the serial or parallel encoding
+ * mode
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_mem_ops(
+ mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = 0;
+ OMX_INDEXTYPE indextype;
+ QOMX_MEM_OPS mem_ops;
+ mm_jpeg_encode_params_t *p_params = &p_session->params;
+
+ if (p_params->get_memory) {
+ mem_ops.get_memory = mm_jpeg_get_mem;
+ } else {
+ mem_ops.get_memory = NULL;
+ LOGH("HAL get_mem handler undefined");
+ }
+
+ mem_ops.psession = p_session;
+ rc = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_MEM_OPS_NAME, &indextype);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+
+ rc = OMX_SetParameter(p_session->omx_handle, indextype, &mem_ops);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+ return rc;
+}
+
+/** mm_jpeg_metadata:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Pass meta data
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_metadata(
+ mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+ OMX_INDEXTYPE indexType;
+ QOMX_METADATA lMeta;
+ mm_jpeg_encode_job_t *p_jobparams = &p_session->encode_job;
+ mm_jpeg_obj *my_obj = (mm_jpeg_obj *) p_session->jpeg_obj;
+
+ rc = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_METADATA_NAME, &indexType);
+
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+
+ lMeta.metadata = (OMX_U8 *)p_jobparams->p_metadata;
+ lMeta.metaPayloadSize = sizeof(*p_jobparams->p_metadata);
+ lMeta.mobicat_mask = p_jobparams->mobicat_mask;
+ lMeta.static_metadata = (OMX_U8 *)my_obj->jpeg_metadata;
+
+ rc = OMX_SetConfig(p_session->omx_handle, indexType, &lMeta);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+ return OMX_ErrorNone;
+}
+
+/** mm_jpeg_meta_enc_key:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Pass metadata encrypt key
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_meta_enc_key(
+ mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+ OMX_INDEXTYPE indexType;
+ QOMX_META_ENC_KEY lKey;
+
+ lKey.metaKey = p_session->meta_enc_key;
+ lKey.keyLen = p_session->meta_enc_keylen;
+
+ if ((!lKey.metaKey) || (!lKey.keyLen)){
+ LOGD("Key is invalid");
+ return OMX_ErrorNone;
+ }
+
+ rc = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_META_ENC_KEY_NAME, &indexType);
+
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+
+ rc = OMX_SetConfig(p_session->omx_handle, indexType, &lKey);
+ if (rc != OMX_ErrorNone) {
+ LOGE("Failed");
+ return rc;
+ }
+ return OMX_ErrorNone;
+}
+
+/** map_jpeg_format:
+ *
+ * Arguments:
+ * @color_fmt: color format
+ *
+ * Return:
+ * OMX color format
+ *
+ * Description:
+ * Map mmjpeg color format to OMX color format
+ *
+ **/
+int map_jpeg_format(mm_jpeg_color_format color_fmt)
+{
+ switch (color_fmt) {
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2:
+ return (int)OMX_QCOM_IMG_COLOR_FormatYVU420SemiPlanar;
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2:
+ return (int)OMX_COLOR_FormatYUV420SemiPlanar;
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1:
+ return (int)OMX_QCOM_IMG_COLOR_FormatYVU422SemiPlanar;
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1:
+ return (int)OMX_COLOR_FormatYUV422SemiPlanar;
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2:
+ return (int)OMX_QCOM_IMG_COLOR_FormatYVU422SemiPlanar_h1v2;
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2:
+ return (int)OMX_QCOM_IMG_COLOR_FormatYUV422SemiPlanar_h1v2;
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1:
+ return (int)OMX_QCOM_IMG_COLOR_FormatYVU444SemiPlanar;
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1:
+ return (int)OMX_QCOM_IMG_COLOR_FormatYUV444SemiPlanar;
+ case MM_JPEG_COLOR_FORMAT_MONOCHROME:
+ return (int)OMX_COLOR_FormatMonochrome;
+ default:
+ LOGW("invalid format %d", color_fmt);
+ return (int)OMX_QCOM_IMG_COLOR_FormatYVU420SemiPlanar;
+ }
+}
+
+/** mm_jpeg_get_imgfmt_from_colorfmt:
+ *
+ * Arguments:
+ * @color_fmt: color format
+ *
+ * Return:
+ * cam format
+ *
+ * Description:
+ * Get camera image format from color format
+ *
+ **/
+cam_format_t mm_jpeg_get_imgfmt_from_colorfmt
+ (mm_jpeg_color_format color_fmt)
+{
+ switch (color_fmt) {
+ case MM_JPEG_COLOR_FORMAT_MONOCHROME:
+ return CAM_FORMAT_Y_ONLY;
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2:
+ return CAM_FORMAT_YUV_420_NV21;
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2:
+ return CAM_FORMAT_YUV_420_NV12;
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1:
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2:
+ return CAM_FORMAT_YUV_422_NV61;
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1:
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2:
+ return CAM_FORMAT_YUV_422_NV16;
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1:
+ return CAM_FORMAT_YUV_444_NV42;
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1:
+ return CAM_FORMAT_YUV_444_NV24;
+ default:
+ return CAM_FORMAT_Y_ONLY;
+ }
+}
+
+/** mm_jpeg_session_config_port:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure OMX ports
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_config_ports(mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_encode_params_t *p_params = &p_session->params;
+ OMX_CONFIG_ROTATIONTYPE rotate;
+
+ mm_jpeg_buf_t *p_src_buf =
+ &p_params->src_main_buf[0];
+
+ p_session->inputPort.nPortIndex = 0;
+ p_session->outputPort.nPortIndex = 1;
+ p_session->inputTmbPort.nPortIndex = 2;
+
+ ret = OMX_GetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->inputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ ret = OMX_GetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->inputTmbPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ ret = OMX_GetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->outputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ if (p_session->lib2d_rotation_flag &&
+ ((p_session->params.rotation == 90) ||
+ (p_session->params.rotation == 270))) {
+ p_session->inputPort.format.image.nFrameWidth =
+ (OMX_U32)p_params->main_dim.src_dim.height;
+ p_session->inputPort.format.image.nFrameHeight =
+ (OMX_U32)p_params->main_dim.src_dim.width;
+ p_session->inputPort.format.image.nStride =
+ p_src_buf->offset.mp[0].scanline;
+ p_session->inputPort.format.image.nSliceHeight =
+ (OMX_U32)p_src_buf->offset.mp[0].stride;
+ } else {
+ p_session->inputPort.format.image.nFrameWidth =
+ (OMX_U32)p_params->main_dim.src_dim.width;
+ p_session->inputPort.format.image.nFrameHeight =
+ (OMX_U32)p_params->main_dim.src_dim.height;
+ p_session->inputPort.format.image.nStride =
+ p_src_buf->offset.mp[0].stride;
+ p_session->inputPort.format.image.nSliceHeight =
+ (OMX_U32)p_src_buf->offset.mp[0].scanline;
+ }
+
+ p_session->inputPort.format.image.eColorFormat =
+ map_jpeg_format(p_params->color_format);
+ p_session->inputPort.nBufferSize =
+ p_params->src_main_buf[0/*p_jobparams->src_index*/].buf_size;
+
+ if (p_session->lib2d_rotation_flag) {
+ p_session->inputPort.nBufferCountActual =
+ (OMX_U32)p_session->num_src_rot_bufs;
+ } else {
+ p_session->inputPort.nBufferCountActual =
+ (OMX_U32)p_params->num_src_bufs;
+ }
+
+ ret = OMX_SetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->inputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ if (p_session->params.encode_thumbnail) {
+ mm_jpeg_buf_t *p_tmb_buf =
+ &p_params->src_thumb_buf[0];
+ if ((p_session->lib2d_rotation_flag && p_session->thumb_from_main) &&
+ ((p_session->params.rotation == 90) ||
+ (p_session->params.rotation == 270))) {
+ p_session->inputTmbPort.format.image.nFrameWidth =
+ (OMX_U32)p_params->thumb_dim.src_dim.height;
+ p_session->inputTmbPort.format.image.nFrameHeight =
+ (OMX_U32)p_params->thumb_dim.src_dim.width;
+ p_session->inputTmbPort.format.image.nStride =
+ p_tmb_buf->offset.mp[0].scanline;
+ p_session->inputTmbPort.format.image.nSliceHeight =
+ (OMX_U32)p_tmb_buf->offset.mp[0].stride;
+ } else {
+ p_session->inputTmbPort.format.image.nFrameWidth =
+ (OMX_U32)p_params->thumb_dim.src_dim.width;
+ p_session->inputTmbPort.format.image.nFrameHeight =
+ (OMX_U32)p_params->thumb_dim.src_dim.height;
+ p_session->inputTmbPort.format.image.nStride =
+ p_tmb_buf->offset.mp[0].stride;
+ p_session->inputTmbPort.format.image.nSliceHeight =
+ (OMX_U32)p_tmb_buf->offset.mp[0].scanline;
+ }
+
+ p_session->inputTmbPort.format.image.eColorFormat =
+ map_jpeg_format(p_params->thumb_color_format);
+ p_session->inputTmbPort.nBufferSize =
+ p_params->src_thumb_buf[0].buf_size;
+
+ if (p_session->lib2d_rotation_flag && p_session->thumb_from_main) {
+ p_session->inputTmbPort.nBufferCountActual =
+ (OMX_U32)p_session->num_src_rot_bufs;
+ } else {
+ p_session->inputTmbPort.nBufferCountActual =
+ (OMX_U32)p_params->num_tmb_bufs;
+ }
+
+ ret = OMX_SetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->inputTmbPort);
+
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ // Enable thumbnail port
+ ret = OMX_SendCommand(p_session->omx_handle, OMX_CommandPortEnable,
+ p_session->inputTmbPort.nPortIndex, NULL);
+
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+ } else {
+ // Disable thumbnail port
+ ret = OMX_SendCommand(p_session->omx_handle, OMX_CommandPortDisable,
+ p_session->inputTmbPort.nPortIndex, NULL);
+
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+ }
+
+ p_session->outputPort.nBufferSize =
+ p_params->dest_buf[0].buf_size;
+ p_session->outputPort.nBufferCountActual = (OMX_U32)p_params->num_dst_bufs;
+ ret = OMX_SetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->outputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ /* set rotation */
+ memset(&rotate, 0, sizeof(rotate));
+ rotate.nPortIndex = 1;
+
+ if (p_session->lib2d_rotation_flag) {
+ rotate.nRotation = 0;
+ } else {
+ rotate.nRotation = (OMX_S32)p_params->rotation;
+ }
+
+ ret = OMX_SetConfig(p_session->omx_handle, OMX_IndexConfigCommonRotate,
+ &rotate);
+ if (OMX_ErrorNone != ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ LOGD("Set rotation to %d at port_idx = %d",
+ (int)p_params->rotation, (int)rotate.nPortIndex);
+
+ return ret;
+}
+
+/** mm_jpeg_update_thumbnail_crop
+ *
+ * Arguments:
+ * @p_thumb_dim: thumbnail dimension
+ * @crop_width : flag indicating if width needs to be cropped
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Updates thumbnail crop aspect ratio based on
+ * thumbnail destination aspect ratio.
+ *
+ */
+OMX_ERRORTYPE mm_jpeg_update_thumbnail_crop(mm_jpeg_dim_t *p_thumb_dim,
+ uint8_t crop_width)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ int32_t cropped_width = 0, cropped_height = 0;
+
+ if (crop_width) {
+ // Keep height constant
+ cropped_height = p_thumb_dim->crop.height;
+ cropped_width = floor((cropped_height * p_thumb_dim->dst_dim.width) /
+ p_thumb_dim->dst_dim.height);
+ if (cropped_width % 2) {
+ cropped_width -= 1;
+ }
+ } else {
+ // Keep width constant
+ cropped_width = p_thumb_dim->crop.width;
+ cropped_height = floor((cropped_width * p_thumb_dim->dst_dim.height) /
+ p_thumb_dim->dst_dim.width);
+ if (cropped_height % 2) {
+ cropped_height -= 1;
+ }
+ }
+ p_thumb_dim->crop.left = p_thumb_dim->crop.left +
+ floor((p_thumb_dim->crop.width - cropped_width) / 2);
+ if (p_thumb_dim->crop.left % 2) {
+ p_thumb_dim->crop.left -= 1;
+ }
+ p_thumb_dim->crop.top = p_thumb_dim->crop.top +
+ floor((p_thumb_dim->crop.height - cropped_height) / 2);
+ if (p_thumb_dim->crop.top % 2) {
+ p_thumb_dim->crop.top -= 1;
+ }
+ p_thumb_dim->crop.width = cropped_width;
+ p_thumb_dim->crop.height = cropped_height;
+
+ LOGH("New thumbnail crop: left %d, top %d, crop width %d,"
+ " crop height %d", p_thumb_dim->crop.left,
+ p_thumb_dim->crop.top, p_thumb_dim->crop.width,
+ p_thumb_dim->crop.height);
+
+ return ret;
+}
+
+/** mm_jpeg_omx_config_thumbnail:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure OMX ports
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_config_thumbnail(mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ QOMX_THUMBNAIL_INFO thumbnail_info;
+ OMX_INDEXTYPE thumb_indextype;
+ mm_jpeg_encode_params_t *p_params = &p_session->params;
+ mm_jpeg_encode_job_t *p_jobparams = &p_session->encode_job;
+ mm_jpeg_dim_t *p_thumb_dim = &p_jobparams->thumb_dim;
+ mm_jpeg_dim_t *p_main_dim = &p_jobparams->main_dim;
+ QOMX_YUV_FRAME_INFO *p_frame_info = &thumbnail_info.tmbOffset;
+ mm_jpeg_buf_t *p_tmb_buf = &p_params->src_thumb_buf[p_jobparams->thumb_index];
+
+ LOGH("encode_thumbnail %u",
+ p_params->encode_thumbnail);
+ if (OMX_FALSE == p_params->encode_thumbnail) {
+ return ret;
+ }
+
+ if ((p_thumb_dim->dst_dim.width == 0) || (p_thumb_dim->dst_dim.height == 0)) {
+ LOGE("Error invalid output dim for thumbnail");
+ return OMX_ErrorBadParameter;
+ }
+
+ if ((p_thumb_dim->src_dim.width == 0) || (p_thumb_dim->src_dim.height == 0)) {
+ LOGE("Error invalid input dim for thumbnail");
+ return OMX_ErrorBadParameter;
+ }
+
+ if ((p_thumb_dim->crop.width == 0) || (p_thumb_dim->crop.height == 0)) {
+ p_thumb_dim->crop.width = p_thumb_dim->src_dim.width;
+ p_thumb_dim->crop.height = p_thumb_dim->src_dim.height;
+ }
+
+ /* check crop boundary */
+ if ((p_thumb_dim->crop.width + p_thumb_dim->crop.left > p_thumb_dim->src_dim.width) ||
+ (p_thumb_dim->crop.height + p_thumb_dim->crop.top > p_thumb_dim->src_dim.height)) {
+ LOGE("invalid crop boundary (%d, %d) offset (%d, %d) out of (%d, %d)",
+ p_thumb_dim->crop.width,
+ p_thumb_dim->crop.height,
+ p_thumb_dim->crop.left,
+ p_thumb_dim->crop.top,
+ p_thumb_dim->src_dim.width,
+ p_thumb_dim->src_dim.height);
+ return OMX_ErrorBadParameter;
+ }
+
+ memset(&thumbnail_info, 0x0, sizeof(QOMX_THUMBNAIL_INFO));
+ ret = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_THUMBNAIL_NAME,
+ &thumb_indextype);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+
+ /* fill thumbnail info */
+ thumbnail_info.scaling_enabled = 1;
+ thumbnail_info.input_width = (OMX_U32)p_thumb_dim->src_dim.width;
+ thumbnail_info.input_height = (OMX_U32)p_thumb_dim->src_dim.height;
+ thumbnail_info.rotation = (OMX_U32)p_params->thumb_rotation;
+ thumbnail_info.quality = (OMX_U32)p_params->thumb_quality;
+ thumbnail_info.output_width = (OMX_U32)p_thumb_dim->dst_dim.width;
+ thumbnail_info.output_height = (OMX_U32)p_thumb_dim->dst_dim.height;
+
+ if (p_session->thumb_from_main) {
+
+ if (p_session->lib2d_rotation_flag) {
+ thumbnail_info.rotation = 0;
+ } else {
+ if ((p_session->params.thumb_rotation == 90 ||
+ p_session->params.thumb_rotation == 270) &&
+ (p_session->params.rotation == 0 ||
+ p_session->params.rotation == 180)) {
+
+ thumbnail_info.output_width = (OMX_U32)p_thumb_dim->dst_dim.height;
+ thumbnail_info.output_height = (OMX_U32)p_thumb_dim->dst_dim.width;
+ thumbnail_info.rotation = p_session->params.rotation;
+ }
+ }
+
+ //Thumb FOV should be within main image FOV
+ if (p_thumb_dim->crop.left < p_main_dim->crop.left) {
+ p_thumb_dim->crop.left = p_main_dim->crop.left;
+ }
+
+ if (p_thumb_dim->crop.top < p_main_dim->crop.top) {
+ p_thumb_dim->crop.top = p_main_dim->crop.top;
+ }
+
+ while ((p_thumb_dim->crop.left + p_thumb_dim->crop.width) >
+ (p_main_dim->crop.left + p_main_dim->crop.width)) {
+ if (p_thumb_dim->crop.left == p_main_dim->crop.left) {
+ p_thumb_dim->crop.width = p_main_dim->crop.width;
+ } else {
+ p_thumb_dim->crop.left = p_main_dim->crop.left;
+ }
+ }
+
+ while ((p_thumb_dim->crop.top + p_thumb_dim->crop.height) >
+ (p_main_dim->crop.top + p_main_dim->crop.height)) {
+ if (p_thumb_dim->crop.top == p_main_dim->crop.top) {
+ p_thumb_dim->crop.height = p_main_dim->crop.height;
+ } else {
+ p_thumb_dim->crop.top = p_main_dim->crop.top;
+ }
+ }
+ } else if ((p_thumb_dim->dst_dim.width > p_thumb_dim->src_dim.width) ||
+ (p_thumb_dim->dst_dim.height > p_thumb_dim->src_dim.height)) {
+ LOGE("Incorrect thumbnail dim %dx%d resetting to %dx%d", p_thumb_dim->dst_dim.width,
+ p_thumb_dim->dst_dim.height, p_thumb_dim->src_dim.width,
+ p_thumb_dim->src_dim.height);
+ thumbnail_info.output_width = (OMX_U32)p_thumb_dim->src_dim.width;
+ thumbnail_info.output_height = (OMX_U32)p_thumb_dim->src_dim.height;
+ }
+
+ // If the thumbnail crop aspect ratio image and thumbnail dest aspect
+ // ratio are different, reset the thumbnail crop
+ double thumbcrop_aspect_ratio = (double)p_thumb_dim->crop.width /
+ (double)p_thumb_dim->crop.height;
+ double thumbdst_aspect_ratio = (double)p_thumb_dim->dst_dim.width /
+ (double)p_thumb_dim->dst_dim.height;
+ if ((thumbdst_aspect_ratio - thumbcrop_aspect_ratio) >
+ ASPECT_TOLERANCE) {
+ mm_jpeg_update_thumbnail_crop(p_thumb_dim, 0);
+ } else if ((thumbcrop_aspect_ratio - thumbdst_aspect_ratio) >
+ ASPECT_TOLERANCE) {
+ mm_jpeg_update_thumbnail_crop(p_thumb_dim, 1);
+ }
+
+ // Fill thumbnail crop info
+ thumbnail_info.crop_info.nWidth = (OMX_U32)p_thumb_dim->crop.width;
+ thumbnail_info.crop_info.nHeight = (OMX_U32)p_thumb_dim->crop.height;
+ thumbnail_info.crop_info.nLeft = p_thumb_dim->crop.left;
+ thumbnail_info.crop_info.nTop = p_thumb_dim->crop.top;
+
+ memset(p_frame_info, 0x0, sizeof(*p_frame_info));
+
+ p_frame_info->cbcrStartOffset[0] = p_tmb_buf->offset.mp[0].len;
+ p_frame_info->cbcrStartOffset[1] = p_tmb_buf->offset.mp[1].len;
+ p_frame_info->yOffset = p_tmb_buf->offset.mp[0].offset;
+ p_frame_info->cbcrOffset[0] = p_tmb_buf->offset.mp[1].offset;
+ p_frame_info->cbcrOffset[1] = p_tmb_buf->offset.mp[2].offset;
+
+ if (p_session->lib2d_rotation_flag && p_session->thumb_from_main) {
+ p_frame_info->yOffset = 0;
+ p_frame_info->cbcrOffset[0] = 0;
+ p_frame_info->cbcrOffset[1] = 0;
+ }
+
+ ret = OMX_SetConfig(p_session->omx_handle, thumb_indextype,
+ &thumbnail_info);
+ if (ret) {
+ LOGE("Error");
+ return ret;
+ }
+
+ return ret;
+}
+
+/** mm_jpeg_session_config_main_crop:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure main image crop
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_config_main_crop(mm_jpeg_job_session_t *p_session)
+{
+ OMX_CONFIG_RECTTYPE rect_type_in, rect_type_out;
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_encode_job_t *p_jobparams = &p_session->encode_job;
+ mm_jpeg_dim_t *dim = &p_jobparams->main_dim;
+
+ if ((dim->crop.width == 0) || (dim->crop.height == 0)) {
+ dim->crop.width = dim->src_dim.width;
+ dim->crop.height = dim->src_dim.height;
+ }
+ /* error check first */
+ if ((dim->crop.width + dim->crop.left > dim->src_dim.width) ||
+ (dim->crop.height + dim->crop.top > dim->src_dim.height)) {
+ LOGE("invalid crop boundary (%d, %d) out of (%d, %d)",
+ dim->crop.width + dim->crop.left,
+ dim->crop.height + dim->crop.top,
+ dim->src_dim.width,
+ dim->src_dim.height);
+ return OMX_ErrorBadParameter;
+ }
+
+ memset(&rect_type_in, 0, sizeof(rect_type_in));
+ memset(&rect_type_out, 0, sizeof(rect_type_out));
+ rect_type_in.nPortIndex = 0;
+ rect_type_out.nPortIndex = 0;
+
+ if ((dim->src_dim.width != dim->crop.width) ||
+ (dim->src_dim.height != dim->crop.height) ||
+ (dim->src_dim.width != dim->dst_dim.width) ||
+ (dim->src_dim.height != dim->dst_dim.height)) {
+ /* Scaler information */
+ rect_type_in.nWidth = CEILING2(dim->crop.width);
+ rect_type_in.nHeight = CEILING2(dim->crop.height);
+ rect_type_in.nLeft = dim->crop.left;
+ rect_type_in.nTop = dim->crop.top;
+
+ if (dim->dst_dim.width && dim->dst_dim.height) {
+ rect_type_out.nWidth = (OMX_U32)dim->dst_dim.width;
+ rect_type_out.nHeight = (OMX_U32)dim->dst_dim.height;
+ }
+ }
+
+ ret = OMX_SetConfig(p_session->omx_handle, OMX_IndexConfigCommonInputCrop,
+ &rect_type_in);
+ if (OMX_ErrorNone != ret) {
+ LOGE("Error");
+ return ret;
+ }
+
+ LOGH("OMX_IndexConfigCommonInputCrop w = %d, h = %d, l = %d, t = %d,"
+ " port_idx = %d",
+ (int)rect_type_in.nWidth, (int)rect_type_in.nHeight,
+ (int)rect_type_in.nLeft, (int)rect_type_in.nTop,
+ (int)rect_type_in.nPortIndex);
+
+ ret = OMX_SetConfig(p_session->omx_handle, OMX_IndexConfigCommonOutputCrop,
+ &rect_type_out);
+ if (OMX_ErrorNone != ret) {
+ LOGE("Error");
+ return ret;
+ }
+ LOGD("OMX_IndexConfigCommonOutputCrop w = %d, h = %d,"
+ " port_idx = %d",
+ (int)rect_type_out.nWidth, (int)rect_type_out.nHeight,
+ (int)rect_type_out.nPortIndex);
+
+ return ret;
+}
+
+/** mm_jpeg_session_config_main:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure main image
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_config_main(mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+
+ /* config port */
+ LOGD("config port");
+ rc = mm_jpeg_session_config_ports(p_session);
+ if (OMX_ErrorNone != rc) {
+ LOGE("config port failed");
+ return rc;
+ }
+
+ /* config buffer offset */
+ LOGD("config main buf offset");
+ rc = mm_jpeg_session_config_main_buffer_offset(p_session);
+ if (OMX_ErrorNone != rc) {
+ LOGE("config buffer offset failed");
+ return rc;
+ }
+
+ /* set the encoding mode */
+ rc = mm_jpeg_encoding_mode(p_session);
+ if (OMX_ErrorNone != rc) {
+ LOGE("config encoding mode failed");
+ return rc;
+ }
+
+ /* set the metadata encrypt key */
+ rc = mm_jpeg_meta_enc_key(p_session);
+ if (OMX_ErrorNone != rc) {
+ LOGE("config session failed");
+ return rc;
+ }
+
+ /* set the mem ops */
+ rc = mm_jpeg_mem_ops(p_session);
+ if (OMX_ErrorNone != rc) {
+ LOGE("config mem ops failed");
+ return rc;
+ }
+ /* set the jpeg speed mode */
+ rc = mm_jpeg_speed_mode(p_session);
+ if (OMX_ErrorNone != rc) {
+ LOGE("config speed mode failed");
+ return rc;
+ }
+
+ return rc;
+}
+
+/** mm_jpeg_session_config_common:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure common parameters
+ *
+ **/
+OMX_ERRORTYPE mm_jpeg_session_config_common(mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+ OMX_INDEXTYPE exif_idx;
+ OMX_CONFIG_ROTATIONTYPE rotate;
+ mm_jpeg_encode_job_t *p_jobparams = &p_session->encode_job;
+ QOMX_EXIF_INFO exif_info;
+
+ /* set rotation */
+ memset(&rotate, 0, sizeof(rotate));
+ rotate.nPortIndex = 1;
+
+ if (p_session->lib2d_rotation_flag) {
+ rotate.nRotation = 0;
+ } else {
+ rotate.nRotation = (OMX_S32)p_jobparams->rotation;
+ }
+
+ rc = OMX_SetConfig(p_session->omx_handle, OMX_IndexConfigCommonRotate,
+ &rotate);
+ if (OMX_ErrorNone != rc) {
+ LOGE("Error %d", rc);
+ return rc;
+ }
+ LOGD("Set rotation to %d at port_idx = %d",
+ (int)p_jobparams->rotation, (int)rotate.nPortIndex);
+
+ /* Set Exif data*/
+ memset(&p_session->exif_info_local[0], 0, sizeof(p_session->exif_info_local));
+ rc = OMX_GetExtensionIndex(p_session->omx_handle, QOMX_IMAGE_EXT_EXIF_NAME,
+ &exif_idx);
+ if (OMX_ErrorNone != rc) {
+ LOGE("Error %d", rc);
+ return rc;
+ }
+
+ LOGD("Num of exif entries passed from HAL: %d",
+ (int)p_jobparams->exif_info.numOfEntries);
+ if (p_jobparams->exif_info.numOfEntries > 0) {
+ rc = OMX_SetConfig(p_session->omx_handle, exif_idx,
+ &p_jobparams->exif_info);
+ if (OMX_ErrorNone != rc) {
+ LOGE("Error %d", rc);
+ return rc;
+ }
+ }
+ /*parse aditional exif data from the metadata*/
+ exif_info.numOfEntries = 0;
+ exif_info.exif_data = &p_session->exif_info_local[0];
+ process_meta_data(p_jobparams->p_metadata, &exif_info,
+ &p_jobparams->cam_exif_params, p_jobparams->hal_version);
+ /* After Parse metadata */
+ p_session->exif_count_local = (int)exif_info.numOfEntries;
+
+ if (exif_info.numOfEntries > 0) {
+ /* set exif tags */
+ LOGD("exif tags from metadata count %d",
+ (int)exif_info.numOfEntries);
+
+ rc = OMX_SetConfig(p_session->omx_handle, exif_idx,
+ &exif_info);
+ if (OMX_ErrorNone != rc) {
+ LOGE("Error %d", rc);
+ return rc;
+ }
+ }
+
+ return rc;
+}
+
+/** mm_jpeg_session_abort:
+ *
+ * Arguments:
+ * @p_session: jpeg session
+ *
+ * Return:
+ * OMX_BOOL
+ *
+ * Description:
+ * Abort ongoing job
+ *
+ **/
+OMX_BOOL mm_jpeg_session_abort(mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ int rc = 0;
+
+ LOGD("E");
+ pthread_mutex_lock(&p_session->lock);
+ if (MM_JPEG_ABORT_NONE != p_session->abort_state) {
+ pthread_mutex_unlock(&p_session->lock);
+ LOGH("**** ALREADY ABORTED");
+ return 0;
+ }
+ p_session->abort_state = MM_JPEG_ABORT_INIT;
+ if (OMX_TRUE == p_session->encoding) {
+ p_session->state_change_pending = OMX_TRUE;
+
+ LOGH("**** ABORTING");
+ pthread_mutex_unlock(&p_session->lock);
+
+ ret = OMX_SendCommand(p_session->omx_handle, OMX_CommandStateSet,
+ OMX_StateIdle, NULL);
+
+ if (ret != OMX_ErrorNone) {
+ LOGE("OMX_SendCommand returned error %d", ret);
+ return 1;
+ }
+ rc = mm_jpegenc_destroy_job(p_session);
+ if (rc != 0) {
+ LOGE("Destroy job returned error %d", rc);
+ }
+
+ pthread_mutex_lock(&p_session->lock);
+ if (MM_JPEG_ABORT_INIT == p_session->abort_state) {
+ LOGL("before wait");
+ pthread_cond_wait(&p_session->cond, &p_session->lock);
+ }
+ LOGL("after wait");
+ }
+ p_session->abort_state = MM_JPEG_ABORT_DONE;
+
+ mm_jpeg_put_mem((void *)p_session);
+
+ pthread_mutex_unlock(&p_session->lock);
+
+ // Abort next session
+ if (p_session->next_session) {
+ mm_jpeg_session_abort(p_session->next_session);
+ }
+
+ LOGD("X");
+ return 0;
+}
+
+/** mm_jpeg_config_multi_image_info
+ *
+ * Arguments:
+ * @p_session: encode session
+ *
+ * Return: OMX_ERRORTYPE
+ *
+ * Description:
+ * Configure multi image parameters
+ *
+ **/
+static OMX_ERRORTYPE mm_jpeg_config_multi_image_info(
+ mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ QOMX_JPEG_MULTI_IMAGE_INFO multi_image_info;
+ OMX_INDEXTYPE multi_image_index;
+ mm_jpeg_encode_job_t *p_jobparams = &p_session->encode_job;
+
+ ret = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_MULTI_IMAGE_NAME, &multi_image_index);
+ if (ret) {
+ LOGE("Error getting multi image info extention index %d", ret);
+ return ret;
+ }
+ memset(&multi_image_info, 0, sizeof(multi_image_info));
+ if (p_jobparams->multi_image_info.type == MM_JPEG_TYPE_MPO) {
+ multi_image_info.image_type = QOMX_JPEG_IMAGE_TYPE_MPO;
+ } else {
+ multi_image_info.image_type = QOMX_JPEG_IMAGE_TYPE_JPEG;
+ }
+ multi_image_info.is_primary_image = p_jobparams->multi_image_info.is_primary;
+ multi_image_info.num_of_images = p_jobparams->multi_image_info.num_of_images;
+ multi_image_info.enable_metadata = p_jobparams->multi_image_info.enable_metadata;
+
+ ret = OMX_SetConfig(p_session->omx_handle, multi_image_index,
+ &multi_image_info);
+ if (ret) {
+ LOGE("Error setting multi image config");
+ return ret;
+ }
+ return ret;
+}
+
+/** mm_jpeg_configure_params
+ *
+ * Arguments:
+ * @p_session: encode session
+ *
+ * Return:
+ * none
+ *
+ * Description:
+ * Configure the job specific params
+ *
+ **/
+static OMX_ERRORTYPE mm_jpeg_configure_job_params(
+ mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ OMX_IMAGE_PARAM_QFACTORTYPE q_factor;
+ QOMX_WORK_BUFFER work_buffer;
+ OMX_INDEXTYPE work_buffer_index;
+ mm_jpeg_encode_params_t *p_params = &p_session->params;
+ mm_jpeg_encode_job_t *p_jobparams = &p_session->encode_job;
+ int i;
+
+ /* common config */
+ ret = mm_jpeg_session_config_common(p_session);
+ if (OMX_ErrorNone != ret) {
+ LOGE("config common failed");
+ }
+
+ /* config Main Image crop */
+ LOGD("config main crop");
+ ret = mm_jpeg_session_config_main_crop(p_session);
+ if (OMX_ErrorNone != ret) {
+ LOGE("config crop failed");
+ return ret;
+ }
+
+ /* set quality */
+ memset(&q_factor, 0, sizeof(q_factor));
+ q_factor.nPortIndex = 0;
+ q_factor.nQFactor = p_params->quality;
+ ret = OMX_SetConfig(p_session->omx_handle, OMX_IndexParamQFactor, &q_factor);
+ LOGD("config QFactor: %d", (int)q_factor.nQFactor);
+ if (OMX_ErrorNone != ret) {
+ LOGE("Error setting Q factor %d", ret);
+ return ret;
+ }
+
+ /* config thumbnail */
+ ret = mm_jpeg_session_config_thumbnail(p_session);
+ if (OMX_ErrorNone != ret) {
+ LOGE("config thumbnail img failed");
+ return ret;
+ }
+
+ //Pass the ION buffer to be used as o/p for HW
+ memset(&work_buffer, 0x0, sizeof(QOMX_WORK_BUFFER));
+ ret = OMX_GetExtensionIndex(p_session->omx_handle,
+ QOMX_IMAGE_EXT_WORK_BUFFER_NAME,
+ &work_buffer_index);
+ if (ret) {
+ LOGE("Error getting work buffer index %d", ret);
+ return ret;
+ }
+ work_buffer.fd = p_session->work_buffer.p_pmem_fd;
+ work_buffer.vaddr = p_session->work_buffer.addr;
+ work_buffer.length = (uint32_t)p_session->work_buffer.size;
+ LOGH("Work buffer info %d %p WorkBufSize: %d invalidate",
+ work_buffer.fd, work_buffer.vaddr, work_buffer.length);
+
+ buffer_invalidate(&p_session->work_buffer);
+
+ ret = OMX_SetConfig(p_session->omx_handle, work_buffer_index,
+ &work_buffer);
+ if (ret) {
+ LOGE("Error");
+ return ret;
+ }
+
+ /* set metadata */
+ ret = mm_jpeg_metadata(p_session);
+ if (OMX_ErrorNone != ret) {
+ LOGE("config makernote data failed");
+ return ret;
+ }
+
+ /* set QTable */
+ for (i = 0; i < QTABLE_MAX; i++) {
+ if (p_jobparams->qtable_set[i]) {
+ ret = OMX_SetConfig(p_session->omx_handle,
+ OMX_IndexParamQuantizationTable, &p_jobparams->qtable[i]);
+ if (OMX_ErrorNone != ret) {
+ LOGE("set QTable Error");
+ return ret;
+ }
+ }
+ }
+
+ /* Set multi image data*/
+ ret = mm_jpeg_config_multi_image_info(p_session);
+ if (OMX_ErrorNone != ret) {
+ LOGE("config multi image data failed");
+ return ret;
+ }
+
+ return ret;
+}
+
+/** mm_jpeg_session_configure:
+ *
+ * Arguments:
+ * @data: encode session
+ *
+ * Return:
+ * none
+ *
+ * Description:
+ * Configure the session
+ *
+ **/
+static OMX_ERRORTYPE mm_jpeg_session_configure(mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+
+ LOGD("E ");
+
+ MM_JPEG_CHK_ABORT(p_session, ret, error);
+
+ /* config main img */
+ ret = mm_jpeg_session_config_main(p_session);
+ if (OMX_ErrorNone != ret) {
+ LOGE("config main img failed");
+ goto error;
+ }
+ ret = mm_jpeg_session_change_state(p_session, OMX_StateIdle,
+ mm_jpeg_session_send_buffers);
+ if (ret) {
+ LOGE("change state to idle failed %d", ret);
+ goto error;
+ }
+
+ ret = mm_jpeg_session_change_state(p_session, OMX_StateExecuting,
+ NULL);
+ if (ret) {
+ LOGE("change state to executing failed %d", ret);
+ goto error;
+ }
+
+error:
+ LOGD("X ret %d", ret);
+ return ret;
+}
+
+
+
+
+
+
+/** mm_jpeg_session_encode:
+ *
+ * Arguments:
+ * @p_session: encode session
+ *
+ * Return:
+ * OMX_ERRORTYPE
+ *
+ * Description:
+ * Start the encoding
+ *
+ **/
+static OMX_ERRORTYPE mm_jpeg_session_encode(mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_encode_job_t *p_jobparams = &p_session->encode_job;
+ mm_jpeg_obj *my_obj = (mm_jpeg_obj *) p_session->jpeg_obj;
+ OMX_BUFFERHEADERTYPE *p_in_buf = NULL;
+ OMX_BUFFERHEADERTYPE *p_in_thumb_buf = NULL;
+
+ pthread_mutex_lock(&p_session->lock);
+ p_session->abort_state = MM_JPEG_ABORT_NONE;
+ p_session->encoding = OMX_FALSE;
+ pthread_mutex_unlock(&p_session->lock);
+
+ if (p_session->thumb_from_main) {
+ if (0 > p_jobparams->src_index) {
+ LOGE("Error");
+ ret = OMX_ErrorUnsupportedIndex;
+ goto error;
+ }
+ p_jobparams->thumb_index = (uint32_t)p_jobparams->src_index;
+ p_jobparams->thumb_dim.crop = p_jobparams->main_dim.crop;
+ }
+
+ if (OMX_FALSE == p_session->config) {
+ /* If another session in progress clear that sessions configuration */
+ if (my_obj->p_session_inprogress != NULL) {
+ OMX_STATETYPE state;
+ mm_jpeg_job_session_t *p_session_inprogress = my_obj->p_session_inprogress;
+
+ OMX_GetState(p_session_inprogress->omx_handle, &state);
+
+ //Check state before state transition
+ if ((state == OMX_StateExecuting) || (state == OMX_StatePause)) {
+ ret = mm_jpeg_session_change_state(p_session_inprogress,
+ OMX_StateIdle, NULL);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+ }
+
+ OMX_GetState(p_session_inprogress->omx_handle, &state);
+
+ if (state == OMX_StateIdle) {
+ ret = mm_jpeg_session_change_state(p_session_inprogress,
+ OMX_StateLoaded, mm_jpeg_session_free_buffers);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+ }
+ p_session_inprogress->config = OMX_FALSE;
+ my_obj->p_session_inprogress = NULL;
+ }
+
+ ret = mm_jpeg_session_configure(p_session);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+ p_session->config = OMX_TRUE;
+ my_obj->p_session_inprogress = p_session;
+ }
+
+ ret = mm_jpeg_configure_job_params(p_session);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+ pthread_mutex_lock(&p_session->lock);
+ p_session->encoding = OMX_TRUE;
+ pthread_mutex_unlock(&p_session->lock);
+
+ MM_JPEG_CHK_ABORT(p_session, ret, error);
+
+ if (p_session->lib2d_rotation_flag) {
+ p_in_buf = p_session->p_in_rot_omx_buf[p_jobparams->src_index];
+ } else {
+ p_in_buf = p_session->p_in_omx_buf[p_jobparams->src_index];
+ }
+
+#ifdef MM_JPEG_DUMP_INPUT
+ char filename[256];
+ snprintf(filename, sizeof(filename),
+ QCAMERA_DUMP_FRM_LOCATION"jpeg/mm_jpeg_int%d.yuv", p_session->ebd_count);
+ DUMP_TO_FILE(filename, p_in_buf->pBuffer, (size_t)p_in_buf->nAllocLen);
+#endif
+ ret = OMX_EmptyThisBuffer(p_session->omx_handle, p_in_buf);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+
+ if (p_session->params.encode_thumbnail) {
+
+ if (p_session->thumb_from_main &&
+ p_session->lib2d_rotation_flag) {
+ p_in_thumb_buf = p_session->p_in_rot_omx_thumb_buf[p_jobparams->thumb_index];
+ } else {
+ p_in_thumb_buf = p_session->p_in_omx_thumb_buf[p_jobparams->thumb_index];
+ }
+
+#ifdef MM_JPEG_DUMP_INPUT
+ char thumb_filename[FILENAME_MAX];
+ snprintf(thumb_filename, sizeof(thumb_filename),
+ QCAMERA_DUMP_FRM_LOCATION"jpeg/mm_jpeg_int_t%d.yuv", p_session->ebd_count);
+ DUMP_TO_FILE(filename, p_in_thumb_buf->pBuffer,
+ (size_t)p_in_thumb_buf->nAllocLen);
+#endif
+ ret = OMX_EmptyThisBuffer(p_session->omx_handle, p_in_thumb_buf);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+ }
+
+ ret = OMX_FillThisBuffer(p_session->omx_handle,
+ p_session->p_out_omx_buf[p_jobparams->dst_index]);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+
+ MM_JPEG_CHK_ABORT(p_session, ret, error);
+
+error:
+
+ LOGD("X ");
+ return ret;
+}
+
+/** mm_jpeg_process_encoding_job:
+ *
+ * Arguments:
+ * @my_obj: jpeg client
+ * @job_node: job node
+ *
+ * Return:
+ * 0 for success -1 otherwise
+ *
+ * Description:
+ * Start the encoding job
+ *
+ **/
+int32_t mm_jpeg_process_encoding_job(mm_jpeg_obj *my_obj, mm_jpeg_job_q_node_t* job_node)
+{
+ mm_jpeg_q_data_t qdata;
+ int32_t rc = 0;
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_job_session_t *p_session = NULL;
+ uint32_t buf_idx;
+
+ /* check if valid session */
+ p_session = mm_jpeg_get_session(my_obj, job_node->enc_info.job_id);
+ if (NULL == p_session) {
+ LOGE("invalid job id %x",
+ job_node->enc_info.job_id);
+ return -1;
+ }
+
+ LOGD("before dequeue session %d", ret);
+
+ /* dequeue available omx handle */
+ qdata = mm_jpeg_queue_deq(p_session->session_handle_q);
+ p_session = qdata.p;
+
+ if (NULL == p_session) {
+ LOGH("No available sessions %d", ret);
+ /* No available handles */
+ qdata.p = job_node;
+ mm_jpeg_queue_enq_head(&my_obj->job_mgr.job_queue, qdata);
+
+ LOGH("end enqueue %d", ret);
+ return rc;
+
+ }
+
+ p_session->auto_out_buf = OMX_FALSE;
+ if (job_node->enc_info.encode_job.dst_index < 0) {
+ /* dequeue available output buffer idx */
+ qdata = mm_jpeg_queue_deq(p_session->out_buf_q);
+ buf_idx = qdata.u32;
+
+ if (0U == buf_idx) {
+ LOGE("No available output buffers %d", ret);
+ return OMX_ErrorUndefined;
+ }
+
+ buf_idx--;
+
+ job_node->enc_info.encode_job.dst_index = (int32_t)buf_idx;
+ p_session->auto_out_buf = OMX_TRUE;
+ }
+
+ /* sent encode cmd to OMX, queue job into ongoing queue */
+ qdata.p = job_node;
+ rc = mm_jpeg_queue_enq(&my_obj->ongoing_job_q, qdata);
+ if (rc) {
+ LOGE("jpeg enqueue failed %d", ret);
+ goto error;
+ }
+
+ p_session->encode_job = job_node->enc_info.encode_job;
+ p_session->jobId = job_node->enc_info.job_id;
+ ret = mm_jpeg_session_encode(p_session);
+ if (ret) {
+ LOGE("encode session failed");
+ goto error;
+ }
+
+ LOGH("Success X ");
+ return rc;
+
+error:
+
+ if ((OMX_ErrorNone != ret) &&
+ (NULL != p_session->params.jpeg_cb)) {
+ p_session->job_status = JPEG_JOB_STATUS_ERROR;
+ LOGE("send jpeg error callback %d",
+ p_session->job_status);
+ p_session->params.jpeg_cb(p_session->job_status,
+ p_session->client_hdl,
+ p_session->jobId,
+ NULL,
+ p_session->params.userdata);
+ }
+
+ /*remove the job*/
+ mm_jpegenc_job_done(p_session);
+ LOGD("Error X ");
+
+ return rc;
+}
+
+
+
+/** mm_jpeg_jobmgr_thread:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * job manager thread main function
+ *
+ **/
+static void *mm_jpeg_jobmgr_thread(void *data)
+{
+ mm_jpeg_q_data_t qdata;
+ int rc = 0;
+ int running = 1;
+ uint32_t num_ongoing_jobs = 0;
+ mm_jpeg_obj *my_obj = (mm_jpeg_obj*)data;
+ mm_jpeg_job_cmd_thread_t *cmd_thread = &my_obj->job_mgr;
+ mm_jpeg_job_q_node_t* node = NULL;
+ prctl(PR_SET_NAME, (unsigned long)"mm_jpeg_thread", 0, 0, 0);
+
+ do {
+ do {
+ rc = cam_sem_wait(&cmd_thread->job_sem);
+ if (rc != 0 && errno != EINVAL) {
+ LOGE("cam_sem_wait error (%s)",
+ strerror(errno));
+ return NULL;
+ }
+ } while (rc != 0);
+
+ /* check ongoing q size */
+ num_ongoing_jobs = mm_jpeg_queue_get_size(&my_obj->ongoing_job_q);
+
+ LOGD("ongoing job %d %d", num_ongoing_jobs, MM_JPEG_CONCURRENT_SESSIONS_COUNT);
+ if (num_ongoing_jobs >= MM_JPEG_CONCURRENT_SESSIONS_COUNT) {
+ LOGE("ongoing job already reach max %d", num_ongoing_jobs);
+ continue;
+ }
+
+ pthread_mutex_lock(&my_obj->job_lock);
+ /* can go ahead with new work */
+ qdata = mm_jpeg_queue_deq(&cmd_thread->job_queue);
+ node = (mm_jpeg_job_q_node_t*)qdata.p;
+ if (node != NULL) {
+ switch (node->type) {
+ case MM_JPEG_CMD_TYPE_JOB:
+ rc = mm_jpeg_process_encoding_job(my_obj, node);
+ break;
+ case MM_JPEG_CMD_TYPE_DECODE_JOB:
+ rc = mm_jpegdec_process_decoding_job(my_obj, node);
+ break;
+ case MM_JPEG_CMD_TYPE_EXIT:
+ default:
+ /* free node */
+ free(node);
+ /* set running flag to false */
+ running = 0;
+ break;
+ }
+ }
+ pthread_mutex_unlock(&my_obj->job_lock);
+
+ } while (running);
+ return NULL;
+}
+
+/** mm_jpeg_jobmgr_thread_launch:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * launches the job manager thread
+ *
+ **/
+int32_t mm_jpeg_jobmgr_thread_launch(mm_jpeg_obj *my_obj)
+{
+ int32_t rc = 0;
+ mm_jpeg_job_cmd_thread_t *job_mgr = &my_obj->job_mgr;
+
+ cam_sem_init(&job_mgr->job_sem, 0);
+ mm_jpeg_queue_init(&job_mgr->job_queue);
+
+ /* launch the thread */
+ pthread_create(&job_mgr->pid,
+ NULL,
+ mm_jpeg_jobmgr_thread,
+ (void *)my_obj);
+ pthread_setname_np(job_mgr->pid, "CAM_jpeg_jobmgr");
+ return rc;
+}
+
+/** mm_jpeg_jobmgr_thread_release:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Releases the job manager thread
+ *
+ **/
+int32_t mm_jpeg_jobmgr_thread_release(mm_jpeg_obj * my_obj)
+{
+ mm_jpeg_q_data_t qdata;
+ int32_t rc = 0;
+ mm_jpeg_job_cmd_thread_t * cmd_thread = &my_obj->job_mgr;
+ mm_jpeg_job_q_node_t* node =
+ (mm_jpeg_job_q_node_t *)malloc(sizeof(mm_jpeg_job_q_node_t));
+ if (NULL == node) {
+ LOGE("No memory for mm_jpeg_job_q_node_t");
+ return -1;
+ }
+
+ memset(node, 0, sizeof(mm_jpeg_job_q_node_t));
+ node->type = MM_JPEG_CMD_TYPE_EXIT;
+
+ qdata.p = node;
+ mm_jpeg_queue_enq(&cmd_thread->job_queue, qdata);
+ cam_sem_post(&cmd_thread->job_sem);
+
+ /* wait until cmd thread exits */
+ if (pthread_join(cmd_thread->pid, NULL) != 0) {
+ LOGD("pthread dead already");
+ }
+ mm_jpeg_queue_deinit(&cmd_thread->job_queue);
+
+ cam_sem_destroy(&cmd_thread->job_sem);
+ memset(cmd_thread, 0, sizeof(mm_jpeg_job_cmd_thread_t));
+ return rc;
+}
+
+/** mm_jpeg_alloc_workbuffer:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @work_bufs_need: number of work buffers required
+ * @work_buf_size: size of the work buffer
+ *
+ * Return:
+ * greater or equal to 0 for success else failure
+ *
+ * Description:
+ * Allocates work buffer
+ *
+ **/
+int32_t mm_jpeg_alloc_workbuffer(mm_jpeg_obj *my_obj,
+ uint32_t work_bufs_need,
+ uint32_t work_buf_size)
+{
+ int32_t rc = 0;
+ uint32_t i;
+ LOGH("work_bufs_need %d work_buf_cnt %d",
+ work_bufs_need, my_obj->work_buf_cnt);
+ for (i = my_obj->work_buf_cnt; i < work_bufs_need; i++) {
+ my_obj->ionBuffer[i].size = CEILING32(work_buf_size);
+ LOGH("Max picture size %d x %d, WorkBufSize = %zu",
+ my_obj->max_pic_w, my_obj->max_pic_h, my_obj->ionBuffer[i].size);
+ my_obj->ionBuffer[i].addr = (uint8_t *)buffer_allocate(&my_obj->ionBuffer[i], 1);
+ if (NULL == my_obj->ionBuffer[i].addr) {
+ LOGE("Ion allocation failed");
+ while (i--) {
+ buffer_deallocate(&my_obj->ionBuffer[i]);
+ my_obj->work_buf_cnt--;
+ }
+ return -1;
+ }
+ my_obj->work_buf_cnt++;
+ rc = i;
+ }
+ LOGH("rc %d ", rc);
+ return rc;
+}
+
+/** mm_jpeg_release_workbuffer:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @work_bufs_need: number of work buffers allocated
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Releases the allocated work buffer
+ *
+ **/
+int32_t mm_jpeg_release_workbuffer(mm_jpeg_obj *my_obj,
+ uint32_t work_bufs_need)
+{
+ int32_t rc = 0;
+ uint32_t i;
+ LOGH("release work_bufs %d ", work_bufs_need);
+ for (i = my_obj->work_buf_cnt; i < work_bufs_need; i++) {
+ buffer_deallocate(&my_obj->ionBuffer[i]);
+ }
+ return rc;
+}
+
+/** mm_jpeg_init:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Initializes the jpeg client
+ *
+ **/
+int32_t mm_jpeg_init(mm_jpeg_obj *my_obj)
+{
+ int32_t rc = 0;
+ uint32_t work_buf_size;
+ unsigned int initial_workbufs_cnt = 1;
+
+ /* init locks */
+ pthread_mutex_init(&my_obj->job_lock, NULL);
+
+ /* init ongoing job queue */
+ rc = mm_jpeg_queue_init(&my_obj->ongoing_job_q);
+ if (0 != rc) {
+ LOGE("Error");
+ pthread_mutex_destroy(&my_obj->job_lock);
+ return -1;
+ }
+
+
+ /* init job semaphore and launch jobmgr thread */
+ LOGD("Launch jobmgr thread rc %d", rc);
+ rc = mm_jpeg_jobmgr_thread_launch(my_obj);
+ if (0 != rc) {
+ LOGE("Error");
+ mm_jpeg_queue_deinit(&my_obj->ongoing_job_q);
+ pthread_mutex_destroy(&my_obj->job_lock);
+ return -1;
+ }
+
+ /* set work buf size from max picture size */
+ if (my_obj->max_pic_w <= 0 || my_obj->max_pic_h <= 0) {
+ LOGE("Width and height are not valid "
+ "dimensions, cannot calc work buf size");
+ mm_jpeg_jobmgr_thread_release(my_obj);
+ mm_jpeg_queue_deinit(&my_obj->ongoing_job_q);
+ pthread_mutex_destroy(&my_obj->job_lock);
+ return -1;
+ }
+
+ /* allocate work buffer if reproc source buffer is not supposed to be used */
+ if (!my_obj->reuse_reproc_buffer) {
+ work_buf_size = CEILING64((uint32_t)my_obj->max_pic_w) *
+ CEILING64((uint32_t)my_obj->max_pic_h) * 3U / 2U;
+ rc = mm_jpeg_alloc_workbuffer(my_obj, initial_workbufs_cnt, work_buf_size);
+ if (rc == -1) {
+ LOGE("Work buffer allocation failure");
+ return rc;
+ }
+ }
+
+ /* load OMX */
+ if (OMX_ErrorNone != OMX_Init()) {
+ /* roll back in error case */
+ LOGE("OMX_Init failed (%d)", rc);
+ if (!my_obj->reuse_reproc_buffer) {
+ mm_jpeg_release_workbuffer(my_obj, initial_workbufs_cnt);
+ }
+ mm_jpeg_jobmgr_thread_release(my_obj);
+ mm_jpeg_queue_deinit(&my_obj->ongoing_job_q);
+ pthread_mutex_destroy(&my_obj->job_lock);
+ }
+
+#ifdef LOAD_ADSP_RPC_LIB
+ my_obj->adsprpc_lib_handle = dlopen("libadsprpc.so", RTLD_NOW);
+ if (NULL == my_obj->adsprpc_lib_handle) {
+ LOGE("Cannot load the library");
+ /* not returning error here bcoz even if this loading fails
+ we can go ahead with SW JPEG enc */
+ }
+#endif
+
+ // create dummy OMX handle to avoid dlopen latency
+ OMX_GetHandle(&my_obj->dummy_handle, mm_jpeg_get_comp_name(), NULL, NULL);
+
+ return rc;
+}
+
+/** mm_jpeg_deinit:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Deinits the jpeg client
+ *
+ **/
+int32_t mm_jpeg_deinit(mm_jpeg_obj *my_obj)
+{
+ int32_t rc = 0;
+ uint32_t i = 0;
+
+ /* release jobmgr thread */
+ rc = mm_jpeg_jobmgr_thread_release(my_obj);
+ if (0 != rc) {
+ LOGE("Error");
+ }
+
+ if (my_obj->dummy_handle) {
+ OMX_FreeHandle(my_obj->dummy_handle);
+ }
+
+ /* unload OMX engine */
+ OMX_Deinit();
+
+ /* deinit ongoing job and cb queue */
+ rc = mm_jpeg_queue_deinit(&my_obj->ongoing_job_q);
+ if (0 != rc) {
+ LOGE("Error");
+ }
+
+ for (i = 0; i < my_obj->work_buf_cnt; i++) {
+ /*Release the ION buffer*/
+ rc = buffer_deallocate(&my_obj->ionBuffer[i]);
+ if (0 != rc) {
+ LOGE("Error releasing ION buffer");
+ }
+ }
+ my_obj->work_buf_cnt = 0;
+ my_obj->jpeg_metadata = NULL;
+
+ /* destroy locks */
+ pthread_mutex_destroy(&my_obj->job_lock);
+
+ return rc;
+}
+
+/** mm_jpeg_new_client:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Create new jpeg client
+ *
+ **/
+uint32_t mm_jpeg_new_client(mm_jpeg_obj *my_obj)
+{
+ uint32_t client_hdl = 0;
+ uint8_t idx;
+ int i = 0;
+
+ if (my_obj->num_clients >= MAX_JPEG_CLIENT_NUM) {
+ LOGE("num of clients reached limit");
+ return client_hdl;
+ }
+
+ for (idx = 0; idx < MAX_JPEG_CLIENT_NUM; idx++) {
+ if (0 == my_obj->clnt_mgr[idx].is_used) {
+ break;
+ }
+ }
+
+ if (idx < MAX_JPEG_CLIENT_NUM) {
+ /* client session avail */
+ /* generate client handler by index */
+ client_hdl = mm_jpeg_util_generate_handler(idx);
+
+ /* update client session */
+ my_obj->clnt_mgr[idx].is_used = 1;
+ my_obj->clnt_mgr[idx].client_handle = client_hdl;
+
+ pthread_mutex_init(&my_obj->clnt_mgr[idx].lock, NULL);
+ for (i = 0; i < MM_JPEG_MAX_SESSION; i++) {
+ memset(&my_obj->clnt_mgr[idx].session[i], 0x0, sizeof(mm_jpeg_job_session_t));
+ }
+
+ /* increse client count */
+ my_obj->num_clients++;
+ }
+
+ return client_hdl;
+}
+
+#ifdef LIB2D_ROTATION_ENABLE
+/**
+ * Function: mm_jpeg_lib2d_rotation_cb
+ *
+ * Description: Callback that is called on completion of requested job.
+ *
+ * Input parameters:
+ * userdata - App userdata
+ * jobid - job id that is finished execution
+ *
+ * Return values:
+ * MM_LIB2D_SUCCESS
+ * MM_LIB2D_ERR_GENERAL
+ *
+ * Notes: none
+ **/
+lib2d_error mm_jpeg_lib2d_rotation_cb(void *userdata, int jobid)
+{
+ LOGD("Received CB from lib2d\n");
+ return MM_LIB2D_SUCCESS;
+}
+
+/**
+ * Function: mm_jpeg_lib2d_rotation
+ *
+ * Description: lib2d rotation function.
+ *
+ * Input parameters:
+ * p_session - pointer to session
+ * p_node - pointer to job queue node
+ * p_job - pointer to job
+ * p_job_id - pointer to job id
+ *
+ * Return values:
+ * 0 - success
+ * -1 - failure
+ *
+ * Notes: none
+ **/
+int32_t mm_jpeg_lib2d_rotation(mm_jpeg_job_session_t *p_session,
+ mm_jpeg_job_q_node_t* p_node, mm_jpeg_job_t *p_job, uint32_t *p_job_id)
+{
+ lib2d_error lib2d_err = MM_LIB2D_SUCCESS;
+ mm_lib2d_buffer src_buffer;
+ mm_lib2d_buffer dst_buffer;
+ mm_jpeg_buf_t *p_src_main_buf = p_session->params.src_main_buf;
+ mm_jpeg_buf_t *p_src_rot_main_buf = p_session->src_rot_main_buf;
+ mm_jpeg_encode_job_t *p_jobparams = &p_job->encode_job;
+ mm_jpeg_encode_job_t *p_jobparams_node = &p_node->enc_info.encode_job;
+ cam_format_t format;
+ int32_t scanline = 0;
+
+ memset(&src_buffer, 0x0, sizeof(mm_lib2d_buffer));
+ memset(&dst_buffer, 0x0, sizeof(mm_lib2d_buffer));
+
+ switch (p_session->params.rotation) {
+ case 0:
+ break;
+ case 90:
+ p_jobparams_node->main_dim.src_dim.width =
+ p_jobparams->main_dim.src_dim.height;
+ p_jobparams_node->main_dim.src_dim.height =
+ p_jobparams->main_dim.src_dim.width;
+
+ p_jobparams_node->main_dim.dst_dim.width =
+ p_jobparams->main_dim.dst_dim.height;
+ p_jobparams_node->main_dim.dst_dim.height =
+ p_jobparams->main_dim.dst_dim.width;
+
+ p_jobparams_node->main_dim.crop.width =
+ p_jobparams->main_dim.crop.height;
+ p_jobparams_node->main_dim.crop.height =
+ p_jobparams->main_dim.crop.width;
+
+ if (p_jobparams->main_dim.crop.top ||
+ p_jobparams->main_dim.crop.height) {
+ p_jobparams_node->main_dim.crop.left =
+ p_jobparams->main_dim.src_dim.height -
+ (p_jobparams->main_dim.crop.top +
+ p_jobparams->main_dim.crop.height);
+ } else {
+ p_jobparams_node->main_dim.crop.left = 0;
+ }
+ p_jobparams_node->main_dim.crop.top =
+ p_jobparams->main_dim.crop.left;
+ break;
+ case 180:
+ if (p_jobparams->main_dim.crop.left ||
+ p_jobparams->main_dim.crop.width) {
+ p_jobparams_node->main_dim.crop.left =
+ p_jobparams->main_dim.src_dim.width -
+ (p_jobparams->main_dim.crop.left +
+ p_jobparams->main_dim.crop.width);
+ } else {
+ p_jobparams_node->main_dim.crop.left = 0;
+ }
+
+ if (p_jobparams->main_dim.crop.top ||
+ p_jobparams->main_dim.crop.height) {
+ p_jobparams_node->main_dim.crop.top =
+ p_jobparams->main_dim.src_dim.height -
+ (p_jobparams->main_dim.crop.top +
+ p_jobparams->main_dim.crop.height);
+ } else {
+ p_jobparams_node->main_dim.crop.top = 0;
+ }
+ break;
+ case 270:
+ p_jobparams_node->main_dim.src_dim.width =
+ p_jobparams->main_dim.src_dim.height;
+ p_jobparams_node->main_dim.src_dim.height =
+ p_jobparams->main_dim.src_dim.width;
+
+ p_jobparams_node->main_dim.dst_dim.width =
+ p_jobparams->main_dim.dst_dim.height;
+ p_jobparams_node->main_dim.dst_dim.height =
+ p_jobparams->main_dim.dst_dim.width;
+
+ p_jobparams_node->main_dim.crop.width =
+ p_jobparams->main_dim.crop.height;
+ p_jobparams_node->main_dim.crop.height =
+ p_jobparams->main_dim.crop.width;
+ p_jobparams_node->main_dim.crop.left =
+ p_jobparams->main_dim.crop.top;
+ if (p_jobparams->main_dim.crop.left ||
+ p_jobparams->main_dim.crop.width) {
+ p_jobparams_node->main_dim.crop.top =
+ p_jobparams->main_dim.src_dim.width -
+ (p_jobparams->main_dim.crop.left +
+ p_jobparams->main_dim.crop.width);
+ } else {
+ p_jobparams_node->main_dim.crop.top = 0;
+ }
+ break;
+ }
+
+ LOGD("crop wxh %dx%d txl %dx%d",
+ p_jobparams_node->main_dim.crop.width,
+ p_jobparams_node->main_dim.crop.height,
+ p_jobparams_node->main_dim.crop.top,
+ p_jobparams_node->main_dim.crop.left);
+
+ format = mm_jpeg_get_imgfmt_from_colorfmt(p_session->params.color_format);
+ src_buffer.buffer_type = MM_LIB2D_BUFFER_TYPE_YUV;
+ src_buffer.yuv_buffer.fd =
+ p_src_main_buf[p_jobparams->src_index].fd;
+ src_buffer.yuv_buffer.format = format;
+ src_buffer.yuv_buffer.width = p_jobparams->main_dim.src_dim.width;
+ src_buffer.yuv_buffer.height = p_jobparams->main_dim.src_dim.height;
+ src_buffer.yuv_buffer.plane0 =
+ p_src_main_buf[p_jobparams->src_index].buf_vaddr;
+ src_buffer.yuv_buffer.stride0 =
+ p_src_main_buf[p_jobparams->src_index].offset.mp[0].stride;
+ scanline = p_src_main_buf[p_jobparams->src_index].offset.mp[0].scanline;
+ src_buffer.yuv_buffer.plane1 =
+ (uint8_t*)src_buffer.yuv_buffer.plane0 +
+ (src_buffer.yuv_buffer.stride0 * scanline);
+ src_buffer.yuv_buffer.stride1 = src_buffer.yuv_buffer.stride0;
+
+ LOGD(" lib2d SRC wxh = %dx%d , stxsl = %dx%d\n",
+ src_buffer.yuv_buffer.width, src_buffer.yuv_buffer.height,
+ src_buffer.yuv_buffer.stride0, scanline);
+
+ dst_buffer.buffer_type = MM_LIB2D_BUFFER_TYPE_YUV;
+ dst_buffer.yuv_buffer.fd =
+ p_src_rot_main_buf[p_jobparams->src_index].fd;
+ dst_buffer.yuv_buffer.format = format;
+ dst_buffer.yuv_buffer.width = p_jobparams_node->main_dim.src_dim.width;
+ dst_buffer.yuv_buffer.height = p_jobparams_node->main_dim.src_dim.height;
+ dst_buffer.yuv_buffer.plane0 =
+ p_src_rot_main_buf[p_jobparams->src_index].buf_vaddr;
+
+ if ((p_session->params.rotation == 90) ||
+ (p_session->params.rotation == 270)) {
+ dst_buffer.yuv_buffer.stride0 =
+ p_src_main_buf[p_jobparams->src_index].offset.mp[0].scanline;
+ scanline = p_src_main_buf[p_jobparams->src_index].offset.mp[0].stride;
+ } else {
+ dst_buffer.yuv_buffer.stride0 =
+ p_src_main_buf[p_jobparams->src_index].offset.mp[0].stride;
+ scanline = p_src_main_buf[p_jobparams->src_index].offset.mp[0].scanline;
+ }
+
+ dst_buffer.yuv_buffer.plane1 =
+ (uint8_t*) dst_buffer.yuv_buffer.plane0 +
+ (dst_buffer.yuv_buffer.stride0 * scanline);
+ dst_buffer.yuv_buffer.stride1 = dst_buffer.yuv_buffer.stride0;
+
+ LOGD(" lib2d DEST wxh = %dx%d , stxsl = %dx%d\n",
+ dst_buffer.yuv_buffer.width, dst_buffer.yuv_buffer.height,
+ dst_buffer.yuv_buffer.stride0, scanline);
+
+ LOGD(" lib2d rotation = %d\n", p_session->params.rotation);
+
+ lib2d_err = mm_lib2d_start_job(p_session->lib2d_handle, &src_buffer, &dst_buffer,
+ *p_job_id, NULL, mm_jpeg_lib2d_rotation_cb, p_session->params.rotation);
+ if (lib2d_err != MM_LIB2D_SUCCESS) {
+ LOGE("Error in mm_lib2d_start_job \n");
+ return -1;
+ }
+
+ buffer_clean(&p_session->src_rot_ion_buffer[p_jobparams->src_index]);
+
+ return 0;
+}
+#endif
+
+/** mm_jpeg_start_job:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_hdl: client handle
+ * @job: pointer to encode job
+ * @jobId: job id
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Start the encoding job
+ *
+ **/
+int32_t mm_jpeg_start_job(mm_jpeg_obj *my_obj,
+ mm_jpeg_job_t *job,
+ uint32_t *job_id)
+{
+ mm_jpeg_q_data_t qdata;
+ int32_t rc = -1;
+ uint8_t session_idx = 0;
+ uint8_t client_idx = 0;
+ mm_jpeg_job_q_node_t* node = NULL;
+ mm_jpeg_job_session_t *p_session = NULL;
+ mm_jpeg_encode_job_t *p_jobparams = NULL;
+ uint32_t work_bufs_need;
+ uint32_t work_buf_size;
+
+ *job_id = 0;
+
+ if (!job) {
+ LOGE("invalid job !!!");
+ return rc;
+ }
+ p_jobparams = &job->encode_job;
+
+ /* check if valid session */
+ session_idx = GET_SESSION_IDX(p_jobparams->session_id);
+ client_idx = GET_CLIENT_IDX(p_jobparams->session_id);
+ LOGD("session_idx %d client idx %d",
+ session_idx, client_idx);
+
+ if ((session_idx >= MM_JPEG_MAX_SESSION) ||
+ (client_idx >= MAX_JPEG_CLIENT_NUM)) {
+ LOGE("invalid session id %x",
+ job->encode_job.session_id);
+ return rc;
+ }
+
+ p_session = &my_obj->clnt_mgr[client_idx].session[session_idx];
+
+ if (my_obj->reuse_reproc_buffer) {
+ p_session->work_buffer.addr = p_jobparams->work_buf.buf_vaddr;
+ p_session->work_buffer.size = p_jobparams->work_buf.buf_size;
+ p_session->work_buffer.ion_info_fd.fd = p_jobparams->work_buf.fd;
+ p_session->work_buffer.p_pmem_fd = p_jobparams->work_buf.fd;
+
+ work_bufs_need = my_obj->num_sessions + 1;
+ if (work_bufs_need > MM_JPEG_CONCURRENT_SESSIONS_COUNT) {
+ work_bufs_need = MM_JPEG_CONCURRENT_SESSIONS_COUNT;
+ }
+
+ if (p_session->work_buffer.addr) {
+ work_bufs_need--;
+ LOGD("HAL passed the work buffer of size = %d; don't alloc internally",
+ p_session->work_buffer.size);
+ } else {
+ p_session->work_buffer = my_obj->ionBuffer[0];
+ }
+
+ LOGD(">>>> Work bufs need %d, %d",
+ work_bufs_need, my_obj->work_buf_cnt);
+ if (work_bufs_need) {
+ work_buf_size = CEILING64(my_obj->max_pic_w) *
+ CEILING64(my_obj->max_pic_h) * 3 / 2;
+ rc = mm_jpeg_alloc_workbuffer(my_obj, work_bufs_need, work_buf_size);
+ if (rc == -1) {
+ LOGE("Work buffer allocation failure");
+ return rc;
+ } else {
+ p_session->work_buffer = my_obj->ionBuffer[rc];
+ }
+ }
+ }
+
+ if (OMX_FALSE == p_session->active) {
+ LOGE("session not active %x",
+ job->encode_job.session_id);
+ return rc;
+ }
+
+ if ((p_jobparams->src_index >= (int32_t)p_session->params.num_src_bufs) ||
+ (p_jobparams->dst_index >= (int32_t)p_session->params.num_dst_bufs)) {
+ LOGE("invalid buffer indices");
+ return rc;
+ }
+
+ /* enqueue new job into todo job queue */
+ node = (mm_jpeg_job_q_node_t *)malloc(sizeof(mm_jpeg_job_q_node_t));
+ if (NULL == node) {
+ LOGE("No memory for mm_jpeg_job_q_node_t");
+ return -1;
+ }
+
+ KPI_ATRACE_INT("Camera:JPEG",
+ (int32_t)((uint32_t)session_idx<<16 | ++p_session->job_index));
+
+ *job_id = job->encode_job.session_id |
+ (((uint32_t)p_session->job_hist++ % JOB_HIST_MAX) << 16);
+
+ memset(node, 0, sizeof(mm_jpeg_job_q_node_t));
+ node->enc_info.encode_job = job->encode_job;
+
+#ifdef LIB2D_ROTATION_ENABLE
+ if (p_session->lib2d_rotation_flag) {
+ rc = mm_jpeg_lib2d_rotation(p_session, node, job, job_id);
+ if (rc < 0) {
+ LOGE("Lib2d rotation failed");
+ return rc;
+ }
+ }
+#endif
+
+ if (p_session->thumb_from_main) {
+ node->enc_info.encode_job.thumb_dim.src_dim =
+ node->enc_info.encode_job.main_dim.src_dim;
+ node->enc_info.encode_job.thumb_dim.crop =
+ node->enc_info.encode_job.main_dim.crop;
+ if (p_session->lib2d_rotation_flag) {
+ if ((p_session->params.rotation == 90) ||
+ (p_session->params.rotation == 270)) {
+ node->enc_info.encode_job.thumb_dim.dst_dim.width =
+ job->encode_job.thumb_dim.dst_dim.height;
+ node->enc_info.encode_job.thumb_dim.dst_dim.height =
+ job->encode_job.thumb_dim.dst_dim.width;
+ }
+ }
+ }
+ node->enc_info.job_id = *job_id;
+ node->enc_info.client_handle = p_session->client_hdl;
+ node->type = MM_JPEG_CMD_TYPE_JOB;
+
+ qdata.p = node;
+ rc = mm_jpeg_queue_enq(&my_obj->job_mgr.job_queue, qdata);
+ if (0 == rc) {
+ cam_sem_post(&my_obj->job_mgr.job_sem);
+ }
+
+ LOGH("session_idx %u client_idx %u job_id %d X",
+ session_idx, client_idx, *job_id);
+
+ return rc;
+}
+
+
+
+/** mm_jpeg_abort_job:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_hdl: client handle
+ * @jobId: job id
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Abort the encoding session
+ *
+ **/
+int32_t mm_jpeg_abort_job(mm_jpeg_obj *my_obj,
+ uint32_t jobId)
+{
+ int32_t rc = -1;
+ mm_jpeg_job_q_node_t *node = NULL;
+ mm_jpeg_job_session_t *p_session = NULL;
+
+ pthread_mutex_lock(&my_obj->job_lock);
+
+ /* abort job if in todo queue */
+ node = mm_jpeg_queue_remove_job_by_job_id(&my_obj->job_mgr.job_queue, jobId);
+ if (NULL != node) {
+ free(node);
+ goto abort_done;
+ }
+
+ /* abort job if in ongoing queue */
+ node = mm_jpeg_queue_remove_job_by_job_id(&my_obj->ongoing_job_q, jobId);
+ if (NULL != node) {
+ /* find job that is OMX ongoing, ask OMX to abort the job */
+ p_session = mm_jpeg_get_session(my_obj, node->enc_info.job_id);
+ if (p_session) {
+ mm_jpeg_session_abort(p_session);
+ } else {
+ LOGE("Invalid job id 0x%x",
+ node->enc_info.job_id);
+ }
+ free(node);
+ goto abort_done;
+ }
+
+abort_done:
+ pthread_mutex_unlock(&my_obj->job_lock);
+
+ return rc;
+}
+
+
+#ifdef MM_JPEG_READ_META_KEYFILE
+static int32_t mm_jpeg_read_meta_keyfile(mm_jpeg_job_session_t *p_session,
+ const char *filename)
+{
+ int rc = 0;
+ FILE *fp = NULL;
+ size_t file_size = 0;
+ fp = fopen(filename, "r");
+ if (!fp) {
+ LOGE("Key not present");
+ return -1;
+ }
+ fseek(fp, 0, SEEK_END);
+ file_size = (size_t)ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ p_session->meta_enc_key = (uint8_t *) malloc((file_size + 1) * sizeof(uint8_t));
+
+ if (!p_session->meta_enc_key) {
+ LOGE("error");
+ return -1;
+ }
+
+ fread(p_session->meta_enc_key, 1, file_size, fp);
+ fclose(fp);
+
+ p_session->meta_enc_keylen = file_size;
+
+ return rc;
+}
+#endif // MM_JPEG_READ_META_KEYFILE
+
+/** mm_jpeg_create_session:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_hdl: client handle
+ * @p_params: pointer to encode params
+ * @p_session_id: session id
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Start the encoding session
+ *
+ **/
+int32_t mm_jpeg_create_session(mm_jpeg_obj *my_obj,
+ uint32_t client_hdl,
+ mm_jpeg_encode_params_t *p_params,
+ uint32_t* p_session_id)
+{
+ mm_jpeg_q_data_t qdata;
+ int32_t rc = 0;
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ uint8_t clnt_idx = 0;
+ int session_idx = -1;
+ mm_jpeg_job_session_t *p_session = NULL;
+ mm_jpeg_job_session_t * p_prev_session = NULL;
+ *p_session_id = 0;
+ uint32_t i = 0;
+ uint32_t j = 0;
+ uint32_t num_omx_sessions = 1;
+ uint32_t work_buf_size;
+ mm_jpeg_queue_t *p_session_handle_q, *p_out_buf_q;
+ uint32_t work_bufs_need;
+ char trace_tag[32];
+
+ /* validate the parameters */
+ if ((p_params->num_src_bufs > MM_JPEG_MAX_BUF)
+ || (p_params->num_dst_bufs > MM_JPEG_MAX_BUF)) {
+ LOGE("invalid num buffers");
+ return -1;
+ }
+
+ /* check if valid client */
+ clnt_idx = mm_jpeg_util_get_index_by_handler(client_hdl);
+ if (clnt_idx >= MAX_JPEG_CLIENT_NUM) {
+ LOGE("invalid client with handler (%d)", client_hdl);
+ return -1;
+ }
+
+ if (p_params->burst_mode) {
+ num_omx_sessions = MM_JPEG_CONCURRENT_SESSIONS_COUNT;
+ }
+
+ if (!my_obj->reuse_reproc_buffer) {
+ work_bufs_need = num_omx_sessions;
+ if (work_bufs_need > MM_JPEG_CONCURRENT_SESSIONS_COUNT) {
+ work_bufs_need = MM_JPEG_CONCURRENT_SESSIONS_COUNT;
+ }
+ LOGD(">>>> Work bufs need %d", work_bufs_need);
+ work_buf_size = CEILING64(my_obj->max_pic_w) *
+ CEILING64(my_obj->max_pic_h) * 3 / 2;
+ rc = mm_jpeg_alloc_workbuffer(my_obj, work_bufs_need, work_buf_size);
+ if (rc == -1) {
+ LOGE("Work buffer allocation failure");
+ return rc;
+ }
+ }
+
+
+ /* init omx handle queue */
+ p_session_handle_q = (mm_jpeg_queue_t *) malloc(sizeof(*p_session_handle_q));
+ if (NULL == p_session_handle_q) {
+ LOGE("Error");
+ goto error1;
+ }
+ rc = mm_jpeg_queue_init(p_session_handle_q);
+ if (0 != rc) {
+ LOGE("Error");
+ free(p_session_handle_q);
+ goto error1;
+ }
+
+ /* init output buf queue */
+ p_out_buf_q = (mm_jpeg_queue_t *) malloc(sizeof(*p_out_buf_q));
+ if (NULL == p_out_buf_q) {
+ LOGE("Error: Cannot allocate memory\n");
+ return -1;
+ }
+
+ /* init omx handle queue */
+ rc = mm_jpeg_queue_init(p_out_buf_q);
+ if (0 != rc) {
+ LOGE("Error");
+ free(p_out_buf_q);
+ goto error1;
+ }
+
+ for (i = 0; i < num_omx_sessions; i++) {
+ uint32_t buf_idx = 0U;
+ session_idx = mm_jpeg_get_new_session_idx(my_obj, clnt_idx, &p_session);
+ if (session_idx < 0 || NULL == p_session) {
+ LOGE("invalid session id (%d)", session_idx);
+ goto error2;
+ }
+
+ snprintf(trace_tag, sizeof(trace_tag), "Camera:JPEGsession%d", session_idx);
+ ATRACE_INT(trace_tag, 1);
+
+ p_session->job_index = 0;
+
+ p_session->next_session = NULL;
+
+ if (p_prev_session) {
+ p_prev_session->next_session = p_session;
+ }
+ p_prev_session = p_session;
+
+ buf_idx = i;
+ if (buf_idx < MM_JPEG_CONCURRENT_SESSIONS_COUNT) {
+ p_session->work_buffer = my_obj->ionBuffer[buf_idx];
+ } else {
+ LOGE("Invalid Index, Setting buffer add to null");
+ p_session->work_buffer.addr = NULL;
+ p_session->work_buffer.ion_fd = -1;
+ p_session->work_buffer.p_pmem_fd = -1;
+ }
+
+ p_session->jpeg_obj = (void*)my_obj; /* save a ptr to jpeg_obj */
+
+ /*copy the params*/
+ p_session->params = *p_params;
+ ret = mm_jpeg_session_create(p_session);
+ if (OMX_ErrorNone != ret) {
+ p_session->active = OMX_FALSE;
+ LOGE("jpeg session create failed");
+ goto error2;
+ }
+
+ uint32_t session_id = (JOB_ID_MAGICVAL << 24) |
+ ((uint32_t)session_idx << 8) | clnt_idx;
+
+ if (!*p_session_id) {
+ *p_session_id = session_id;
+ }
+
+ if (p_session->thumb_from_main) {
+ memcpy(p_session->params.src_thumb_buf, p_session->params.src_main_buf,
+ sizeof(p_session->params.src_thumb_buf));
+ p_session->params.num_tmb_bufs = p_session->params.num_src_bufs;
+ if (!p_session->params.encode_thumbnail) {
+ p_session->params.num_tmb_bufs = 0;
+ }
+ p_session->params.thumb_dim.src_dim = p_session->params.main_dim.src_dim;
+ p_session->params.thumb_dim.crop = p_session->params.main_dim.crop;
+ }
+#ifdef LIB2D_ROTATION_ENABLE
+ if (p_session->params.rotation) {
+ LOGD("Enable lib2d rotation");
+ p_session->lib2d_rotation_flag = 1;
+
+ cam_format_t lib2d_format;
+ lib2d_error lib2d_err = MM_LIB2D_SUCCESS;
+ lib2d_format = mm_jpeg_get_imgfmt_from_colorfmt(p_session->params.color_format);
+ lib2d_err = mm_lib2d_init(MM_LIB2D_SYNC_MODE, lib2d_format,
+ lib2d_format, &p_session->lib2d_handle);
+ if (lib2d_err != MM_LIB2D_SUCCESS) {
+ LOGE("lib2d init for rotation failed\n");
+ rc = -1;
+ p_session->lib2d_rotation_flag = 0;
+ goto error2;
+ }
+ } else {
+ LOGD("Disable lib2d rotation");
+ p_session->lib2d_rotation_flag = 0;
+ }
+#else
+ p_session->lib2d_rotation_flag = 0;
+#endif
+
+ if (p_session->lib2d_rotation_flag) {
+ p_session->num_src_rot_bufs = p_session->params.num_src_bufs;
+ memset(p_session->src_rot_main_buf, 0,
+ sizeof(p_session->src_rot_main_buf));
+
+ for (j = 0; j < p_session->num_src_rot_bufs; j++) {
+ p_session->src_rot_main_buf[j].buf_size =
+ p_session->params.src_main_buf[j].buf_size;
+ p_session->src_rot_main_buf[j].format =
+ p_session->params.src_main_buf[j].format;
+ p_session->src_rot_main_buf[j].index = j;
+
+ memset(&p_session->src_rot_ion_buffer[j], 0, sizeof(buffer_t));
+ p_session->src_rot_ion_buffer[j].size =
+ p_session->src_rot_main_buf[j].buf_size;
+ p_session->src_rot_ion_buffer[j].addr =
+ (uint8_t *)buffer_allocate(&p_session->src_rot_ion_buffer[j], 1);
+
+ if (NULL == p_session->src_rot_ion_buffer[j].addr) {
+ LOGE("Ion buff alloc for rotation failed");
+ // deallocate all previously allocated rotation ion buffs
+ for (j = 0; j < p_session->num_src_rot_bufs; j++) {
+ if (p_session->src_rot_ion_buffer[j].addr) {
+ buffer_deallocate(&p_session->src_rot_ion_buffer[j]);
+ }
+ }
+ //fall back to SW encoding for rotation
+ p_session->lib2d_rotation_flag = 0;
+ } else {
+ p_session->src_rot_main_buf[j].buf_vaddr =
+ p_session->src_rot_ion_buffer[j].addr;
+ p_session->src_rot_main_buf[j].fd =
+ p_session->src_rot_ion_buffer[j].p_pmem_fd;
+ }
+ }
+ }
+
+ p_session->client_hdl = client_hdl;
+ p_session->sessionId = session_id;
+ p_session->session_handle_q = p_session_handle_q;
+ p_session->out_buf_q = p_out_buf_q;
+
+ qdata.p = p_session;
+ mm_jpeg_queue_enq(p_session_handle_q, qdata);
+
+ p_session->meta_enc_key = NULL;
+ p_session->meta_enc_keylen = 0;
+
+#ifdef MM_JPEG_READ_META_KEYFILE
+ mm_jpeg_read_meta_keyfile(p_session, META_KEYFILE);
+#endif
+
+ pthread_mutex_lock(&my_obj->job_lock);
+ /* Configure session if not already configured and if
+ no other session configured*/
+ if ((OMX_FALSE == p_session->config) &&
+ (my_obj->p_session_inprogress == NULL)) {
+ rc = mm_jpeg_session_configure(p_session);
+ if (rc) {
+ LOGE("Error");
+ pthread_mutex_unlock(&my_obj->job_lock);
+ goto error2;
+ }
+ p_session->config = OMX_TRUE;
+ my_obj->p_session_inprogress = p_session;
+ }
+ pthread_mutex_unlock(&my_obj->job_lock);
+ p_session->num_omx_sessions = num_omx_sessions;
+
+ LOGH("session id %x thumb_from_main %d",
+ session_id, p_session->thumb_from_main);
+ }
+
+ // Queue the output buf indexes
+ for (i = 0; i < p_params->num_dst_bufs; i++) {
+ qdata.u32 = i + 1;
+ mm_jpeg_queue_enq(p_out_buf_q, qdata);
+ }
+
+ return rc;
+
+error1:
+ rc = -1;
+error2:
+ if (NULL != p_session) {
+ ATRACE_INT(trace_tag, 0);
+ }
+ return rc;
+}
+
+/** mm_jpegenc_destroy_job
+ *
+ * Arguments:
+ * @p_session: Session obj
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Destroy the job based paramenters
+ *
+ **/
+static int32_t mm_jpegenc_destroy_job(mm_jpeg_job_session_t *p_session)
+{
+ mm_jpeg_encode_job_t *p_jobparams = &p_session->encode_job;
+ int i = 0, rc = 0;
+
+ LOGD("Exif entry count %d %d",
+ (int)p_jobparams->exif_info.numOfEntries,
+ (int)p_session->exif_count_local);
+ for (i = 0; i < p_session->exif_count_local; i++) {
+ rc = releaseExifEntry(&p_session->exif_info_local[i]);
+ if (rc) {
+ LOGE("Exif release failed (%d)", rc);
+ }
+ }
+ p_session->exif_count_local = 0;
+
+ return rc;
+}
+
+/** mm_jpeg_session_encode:
+ *
+ * Arguments:
+ * @p_session: encode session
+ *
+ * Return:
+ * OMX_ERRORTYPE
+ *
+ * Description:
+ * Start the encoding
+ *
+ **/
+static void mm_jpegenc_job_done(mm_jpeg_job_session_t *p_session)
+{
+ mm_jpeg_q_data_t qdata;
+ mm_jpeg_obj *my_obj = (mm_jpeg_obj *)p_session->jpeg_obj;
+ mm_jpeg_job_q_node_t *node = NULL;
+
+ /*Destroy job related params*/
+ mm_jpegenc_destroy_job(p_session);
+
+ /*remove the job*/
+ node = mm_jpeg_queue_remove_job_by_job_id(&my_obj->ongoing_job_q,
+ p_session->jobId);
+ if (node) {
+ free(node);
+ }
+ p_session->encoding = OMX_FALSE;
+
+ // Queue to available sessions
+ qdata.p = p_session;
+ mm_jpeg_queue_enq(p_session->session_handle_q, qdata);
+
+ if (p_session->auto_out_buf) {
+ //Queue out buf index
+ qdata.u32 = (uint32_t)(p_session->encode_job.dst_index + 1);
+ mm_jpeg_queue_enq(p_session->out_buf_q, qdata);
+ }
+
+ /* wake up jobMgr thread to work on new job if there is any */
+ cam_sem_post(&my_obj->job_mgr.job_sem);
+}
+
+/** mm_jpeg_destroy_session:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @session_id: session index
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Destroy the encoding session
+ *
+ **/
+int32_t mm_jpeg_destroy_session(mm_jpeg_obj *my_obj,
+ mm_jpeg_job_session_t *p_session)
+{
+ mm_jpeg_q_data_t qdata;
+ int32_t rc = 0;
+ mm_jpeg_job_q_node_t *node = NULL;
+ uint32_t session_id = 0;
+ mm_jpeg_job_session_t *p_cur_sess;
+ char trace_tag[32];
+
+ if (NULL == p_session) {
+ LOGE("invalid session");
+ return rc;
+ }
+
+ session_id = p_session->sessionId;
+
+ pthread_mutex_lock(&my_obj->job_lock);
+
+ /* abort job if in todo queue */
+ LOGD("abort todo jobs");
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->job_mgr.job_queue, session_id);
+ while (NULL != node) {
+ free(node);
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->job_mgr.job_queue, session_id);
+ }
+
+ /* abort job if in ongoing queue */
+ LOGD("abort ongoing jobs");
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->ongoing_job_q, session_id);
+ while (NULL != node) {
+ free(node);
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->ongoing_job_q, session_id);
+ }
+
+ /* abort the current session */
+ mm_jpeg_session_abort(p_session);
+
+#ifdef LIB2D_ROTATION_ENABLE
+ lib2d_error lib2d_err = MM_LIB2D_SUCCESS;
+ if (p_session->lib2d_rotation_flag) {
+ lib2d_err = mm_lib2d_deinit(p_session->lib2d_handle);
+ if (lib2d_err != MM_LIB2D_SUCCESS) {
+ LOGE("Error in mm_lib2d_deinit \n");
+ }
+ }
+#endif
+
+ mm_jpeg_session_destroy(p_session);
+
+ p_cur_sess = p_session;
+
+ do {
+ mm_jpeg_remove_session_idx(my_obj, p_cur_sess->sessionId);
+ } while (NULL != (p_cur_sess = p_cur_sess->next_session));
+
+
+ pthread_mutex_unlock(&my_obj->job_lock);
+
+ while (1) {
+ qdata = mm_jpeg_queue_deq(p_session->session_handle_q);
+ if (NULL == qdata.p)
+ break;
+ }
+ mm_jpeg_queue_deinit(p_session->session_handle_q);
+ free(p_session->session_handle_q);
+ p_session->session_handle_q = NULL;
+
+ while (1) {
+ qdata = mm_jpeg_queue_deq(p_session->out_buf_q);
+ if (0U == qdata.u32)
+ break;
+ }
+ mm_jpeg_queue_deinit(p_session->out_buf_q);
+ free(p_session->out_buf_q);
+ p_session->out_buf_q = NULL;
+
+
+ /* wake up jobMgr thread to work on new job if there is any */
+ cam_sem_post(&my_obj->job_mgr.job_sem);
+
+ snprintf(trace_tag, sizeof(trace_tag), "Camera:JPEGsession%d", GET_SESSION_IDX(session_id));
+ ATRACE_INT(trace_tag, 0);
+
+ LOGH("destroy session successful. X");
+
+ return rc;
+}
+
+
+
+
+/** mm_jpeg_destroy_session:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @session_id: session index
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Destroy the encoding session
+ *
+ **/
+int32_t mm_jpeg_destroy_session_unlocked(mm_jpeg_obj *my_obj,
+ mm_jpeg_job_session_t *p_session)
+{
+ int32_t rc = -1;
+ mm_jpeg_job_q_node_t *node = NULL;
+ uint32_t session_id = 0;
+ if (NULL == p_session) {
+ LOGE("invalid session");
+ return rc;
+ }
+
+ session_id = p_session->sessionId;
+
+ /* abort job if in todo queue */
+ LOGD("abort todo jobs");
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->job_mgr.job_queue, session_id);
+ while (NULL != node) {
+ free(node);
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->job_mgr.job_queue, session_id);
+ }
+
+ /* abort job if in ongoing queue */
+ LOGD("abort ongoing jobs");
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->ongoing_job_q, session_id);
+ while (NULL != node) {
+ free(node);
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->ongoing_job_q, session_id);
+ }
+
+ /* abort the current session */
+ mm_jpeg_session_abort(p_session);
+ //mm_jpeg_remove_session_idx(my_obj, session_id);
+
+ return rc;
+}
+
+/** mm_jpeg_destroy_session:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @session_id: session index
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Destroy the encoding session
+ *
+ **/
+int32_t mm_jpeg_destroy_session_by_id(mm_jpeg_obj *my_obj, uint32_t session_id)
+{
+ mm_jpeg_job_session_t *p_session = mm_jpeg_get_session(my_obj, session_id);
+
+ return mm_jpeg_destroy_session(my_obj, p_session);
+}
+
+
+
+/** mm_jpeg_close:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_hdl: client handle
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Close the jpeg client
+ *
+ **/
+int32_t mm_jpeg_close(mm_jpeg_obj *my_obj, uint32_t client_hdl)
+{
+ int32_t rc = -1;
+ uint8_t clnt_idx = 0;
+ int i = 0;
+
+ /* check if valid client */
+ clnt_idx = mm_jpeg_util_get_index_by_handler(client_hdl);
+ if (clnt_idx >= MAX_JPEG_CLIENT_NUM) {
+ LOGE("invalid client with handler (%d)", client_hdl);
+ return rc;
+ }
+
+ LOGD("E");
+
+ /* abort all jobs from the client */
+ pthread_mutex_lock(&my_obj->job_lock);
+
+ for (i = 0; i < MM_JPEG_MAX_SESSION; i++) {
+ if (OMX_TRUE == my_obj->clnt_mgr[clnt_idx].session[i].active)
+ mm_jpeg_destroy_session_unlocked(my_obj,
+ &my_obj->clnt_mgr[clnt_idx].session[i]);
+ }
+
+#ifdef LOAD_ADSP_RPC_LIB
+ if (NULL != my_obj->adsprpc_lib_handle) {
+ dlclose(my_obj->adsprpc_lib_handle);
+ my_obj->adsprpc_lib_handle = NULL;
+ }
+#endif
+
+ pthread_mutex_unlock(&my_obj->job_lock);
+
+ /* invalidate client session */
+ pthread_mutex_destroy(&my_obj->clnt_mgr[clnt_idx].lock);
+ memset(&my_obj->clnt_mgr[clnt_idx], 0, sizeof(mm_jpeg_client_t));
+
+ rc = 0;
+ LOGD("X");
+ return rc;
+}
+
+OMX_ERRORTYPE mm_jpeg_ebd(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_BUFFERHEADERTYPE *pBuffer)
+{
+ mm_jpeg_job_session_t *p_session = (mm_jpeg_job_session_t *) pAppData;
+
+ LOGH("count %d ", p_session->ebd_count);
+ pthread_mutex_lock(&p_session->lock);
+ p_session->ebd_count++;
+ pthread_mutex_unlock(&p_session->lock);
+ return 0;
+}
+
+OMX_ERRORTYPE mm_jpeg_fbd(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_BUFFERHEADERTYPE *pBuffer)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_job_session_t *p_session = (mm_jpeg_job_session_t *) pAppData;
+ mm_jpeg_output_t output_buf;
+ LOGI("count %d ", p_session->fbd_count);
+ LOGI("KPI Perf] : PROFILE_JPEG_FBD");
+
+ pthread_mutex_lock(&p_session->lock);
+ KPI_ATRACE_INT("Camera:JPEG",
+ (int32_t)((uint32_t)GET_SESSION_IDX(
+ p_session->sessionId)<<16 | --p_session->job_index));
+ if (MM_JPEG_ABORT_NONE != p_session->abort_state) {
+ pthread_mutex_unlock(&p_session->lock);
+ return ret;
+ }
+#ifdef MM_JPEG_DUMP_OUT_BS
+ char filename[256];
+ static int bsc;
+ snprintf(filename, sizeof(filename),
+ QCAMERA_DUMP_FRM_LOCATION"jpeg/mm_jpeg_bs%d.jpg", bsc++);
+ DUMP_TO_FILE(filename,
+ pBuffer->pBuffer,
+ (size_t)(uint32_t)pBuffer->nFilledLen);
+#endif
+
+ p_session->fbd_count++;
+ if (NULL != p_session->params.jpeg_cb) {
+
+ p_session->job_status = JPEG_JOB_STATUS_DONE;
+ output_buf.buf_filled_len = (uint32_t)pBuffer->nFilledLen;
+ output_buf.buf_vaddr = pBuffer->pBuffer;
+ output_buf.fd = -1;
+ LOGH("send jpeg callback %d buf 0x%p len %u JobID %u",
+ p_session->job_status, pBuffer->pBuffer,
+ (unsigned int)pBuffer->nFilledLen, p_session->jobId);
+ p_session->params.jpeg_cb(p_session->job_status,
+ p_session->client_hdl,
+ p_session->jobId,
+ &output_buf,
+ p_session->params.userdata);
+
+ mm_jpegenc_job_done(p_session);
+
+ mm_jpeg_put_mem((void *)p_session);
+ }
+ pthread_mutex_unlock(&p_session->lock);
+
+ return ret;
+}
+
+
+
+OMX_ERRORTYPE mm_jpeg_event_handler(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_EVENTTYPE eEvent,
+ OMX_U32 nData1,
+ OMX_U32 nData2,
+ OMX_PTR pEventData)
+{
+ mm_jpeg_job_session_t *p_session = (mm_jpeg_job_session_t *) pAppData;
+
+ LOGD("%d %d %d state %d", eEvent, (int)nData1,
+ (int)nData2, p_session->abort_state);
+
+ pthread_mutex_lock(&p_session->lock);
+
+ if (MM_JPEG_ABORT_INIT == p_session->abort_state) {
+ p_session->abort_state = MM_JPEG_ABORT_DONE;
+ pthread_cond_signal(&p_session->cond);
+ pthread_mutex_unlock(&p_session->lock);
+ return OMX_ErrorNone;
+ }
+
+ if (eEvent == OMX_EventError) {
+ p_session->error_flag = nData2;
+ if (p_session->encoding == OMX_TRUE) {
+ LOGE("Error during encoding");
+
+ /* send jpeg callback */
+ if (NULL != p_session->params.jpeg_cb) {
+ p_session->job_status = JPEG_JOB_STATUS_ERROR;
+ LOGE("send jpeg error callback %d",
+ p_session->job_status);
+ p_session->params.jpeg_cb(p_session->job_status,
+ p_session->client_hdl,
+ p_session->jobId,
+ NULL,
+ p_session->params.userdata);
+ }
+
+ /* remove from ready queue */
+ mm_jpegenc_job_done(p_session);
+ }
+ pthread_cond_signal(&p_session->cond);
+ } else if (eEvent == OMX_EventCmdComplete) {
+ if (p_session->state_change_pending == OMX_TRUE) {
+ p_session->state_change_pending = OMX_FALSE;
+ pthread_cond_signal(&p_session->cond);
+ }
+ }
+
+ pthread_mutex_unlock(&p_session->lock);
+ return OMX_ErrorNone;
+}
+
+
+
+/* remove the first job from the queue with matching client handle */
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_client_id(
+ mm_jpeg_queue_t* queue, uint32_t client_hdl)
+{
+ mm_jpeg_q_node_t* node = NULL;
+ mm_jpeg_job_q_node_t* data = NULL;
+ mm_jpeg_job_q_node_t* job_node = NULL;
+ struct cam_list *head = NULL;
+ struct cam_list *pos = NULL;
+
+ pthread_mutex_lock(&queue->lock);
+ head = &queue->head.list;
+ pos = head->next;
+ while(pos != head) {
+ node = member_of(pos, mm_jpeg_q_node_t, list);
+ data = (mm_jpeg_job_q_node_t *)node->data.p;
+
+ if (data && (data->enc_info.client_handle == client_hdl)) {
+ LOGH("found matching client handle");
+ job_node = data;
+ cam_list_del_node(&node->list);
+ queue->size--;
+ free(node);
+ LOGH("queue size = %d", queue->size);
+ break;
+ }
+ pos = pos->next;
+ }
+
+ pthread_mutex_unlock(&queue->lock);
+
+ return job_node;
+}
+
+/* remove the first job from the queue with matching session id */
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_session_id(
+ mm_jpeg_queue_t* queue, uint32_t session_id)
+{
+ mm_jpeg_q_node_t* node = NULL;
+ mm_jpeg_job_q_node_t* data = NULL;
+ mm_jpeg_job_q_node_t* job_node = NULL;
+ struct cam_list *head = NULL;
+ struct cam_list *pos = NULL;
+
+ pthread_mutex_lock(&queue->lock);
+ head = &queue->head.list;
+ pos = head->next;
+ while(pos != head) {
+ node = member_of(pos, mm_jpeg_q_node_t, list);
+ data = (mm_jpeg_job_q_node_t *)node->data.p;
+
+ if (data && (data->enc_info.encode_job.session_id == session_id)) {
+ LOGH("found matching session id");
+ job_node = data;
+ cam_list_del_node(&node->list);
+ queue->size--;
+ free(node);
+ LOGH("queue size = %d", queue->size);
+ break;
+ }
+ pos = pos->next;
+ }
+
+ pthread_mutex_unlock(&queue->lock);
+
+ return job_node;
+}
+
+/* remove job from the queue with matching job id */
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_job_id(
+ mm_jpeg_queue_t* queue, uint32_t job_id)
+{
+ mm_jpeg_q_node_t* node = NULL;
+ mm_jpeg_job_q_node_t* data = NULL;
+ mm_jpeg_job_q_node_t* job_node = NULL;
+ struct cam_list *head = NULL;
+ struct cam_list *pos = NULL;
+ uint32_t lq_job_id;
+
+ pthread_mutex_lock(&queue->lock);
+ head = &queue->head.list;
+ pos = head->next;
+ while(pos != head) {
+ node = member_of(pos, mm_jpeg_q_node_t, list);
+ data = (mm_jpeg_job_q_node_t *)node->data.p;
+
+ if(NULL == data) {
+ LOGE("Data is NULL");
+ pthread_mutex_unlock(&queue->lock);
+ return NULL;
+ }
+
+ if (data->type == MM_JPEG_CMD_TYPE_DECODE_JOB) {
+ lq_job_id = data->dec_info.job_id;
+ } else {
+ lq_job_id = data->enc_info.job_id;
+ }
+
+ if (data && (lq_job_id == job_id)) {
+ LOGD("found matching job id");
+ job_node = data;
+ cam_list_del_node(&node->list);
+ queue->size--;
+ free(node);
+ break;
+ }
+ pos = pos->next;
+ }
+
+ pthread_mutex_unlock(&queue->lock);
+
+ return job_node;
+}
+
+/* remove job from the queue with matching job id */
+mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_unlk(
+ mm_jpeg_queue_t* queue, uint32_t job_id)
+{
+ mm_jpeg_q_node_t* node = NULL;
+ mm_jpeg_job_q_node_t* data = NULL;
+ mm_jpeg_job_q_node_t* job_node = NULL;
+ struct cam_list *head = NULL;
+ struct cam_list *pos = NULL;
+
+ head = &queue->head.list;
+ pos = head->next;
+ while(pos != head) {
+ node = member_of(pos, mm_jpeg_q_node_t, list);
+ data = (mm_jpeg_job_q_node_t *)node->data.p;
+
+ if (data && (data->enc_info.job_id == job_id)) {
+ job_node = data;
+ cam_list_del_node(&node->list);
+ queue->size--;
+ free(node);
+ break;
+ }
+ pos = pos->next;
+ }
+
+ return job_node;
+}
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_exif.c b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_exif.c
new file mode 100644
index 0000000..e56fc24
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_exif.c
@@ -0,0 +1,652 @@
+/* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// System dependencies
+#include <pthread.h>
+#include <string.h>
+#include <math.h>
+
+// JPEG dependencies
+#include "mm_jpeg_dbg.h"
+#include "mm_jpeg.h"
+
+
+#define LOWER(a) ((a) & 0xFFFF)
+#define UPPER(a) (((a)>>16) & 0xFFFF)
+#define CHANGE_ENDIAN_16(a) ((0x00FF & ((a)>>8)) | (0xFF00 & ((a)<<8)))
+#define ROUND(a) \
+ ((a >= 0) ? (uint32_t)(a + 0.5) : (uint32_t)(a - 0.5))
+
+
+/** addExifEntry:
+ *
+ * Arguments:
+ * @exif_info : Exif info struct
+ * @p_session: job session
+ * @tagid : exif tag ID
+ * @type : data type
+ * @count : number of data in uint of its type
+ * @data : input data ptr
+ *
+ * Retrun : int32_t type of status
+ * 0 -- success
+ * none-zero failure code
+ *
+ * Description:
+ * Function to add an entry to exif data
+ *
+ **/
+int32_t addExifEntry(QOMX_EXIF_INFO *p_exif_info, exif_tag_id_t tagid,
+ exif_tag_type_t type, uint32_t count, void *data)
+{
+ int32_t rc = 0;
+ uint32_t numOfEntries = (uint32_t)p_exif_info->numOfEntries;
+ QEXIF_INFO_DATA *p_info_data = p_exif_info->exif_data;
+ if(numOfEntries >= MAX_EXIF_TABLE_ENTRIES) {
+ LOGE("Number of entries exceeded limit");
+ return -1;
+ }
+
+ p_info_data[numOfEntries].tag_id = tagid;
+ p_info_data[numOfEntries].tag_entry.type = type;
+ p_info_data[numOfEntries].tag_entry.count = count;
+ p_info_data[numOfEntries].tag_entry.copy = 1;
+ switch (type) {
+ case EXIF_BYTE: {
+ if (count > 1) {
+ uint8_t *values = (uint8_t *)malloc(count);
+ if (values == NULL) {
+ LOGE("No memory for byte array");
+ rc = -1;
+ } else {
+ memcpy(values, data, count);
+ p_info_data[numOfEntries].tag_entry.data._bytes = values;
+ }
+ } else {
+ p_info_data[numOfEntries].tag_entry.data._byte = *(uint8_t *)data;
+ }
+ }
+ break;
+ case EXIF_ASCII: {
+ char *str = NULL;
+ str = (char *)malloc(count + 1);
+ if (str == NULL) {
+ LOGE("No memory for ascii string");
+ rc = -1;
+ } else {
+ memset(str, 0, count + 1);
+ memcpy(str, data, count);
+ p_info_data[numOfEntries].tag_entry.data._ascii = str;
+ }
+ }
+ break;
+ case EXIF_SHORT: {
+ if (count > 1) {
+ uint16_t *values = (uint16_t *)malloc(count * sizeof(uint16_t));
+ if (values == NULL) {
+ LOGE("No memory for short array");
+ rc = -1;
+ } else {
+ memcpy(values, data, count * sizeof(uint16_t));
+ p_info_data[numOfEntries].tag_entry.data._shorts = values;
+ }
+ } else {
+ p_info_data[numOfEntries].tag_entry.data._short = *(uint16_t *)data;
+ }
+ }
+ break;
+ case EXIF_LONG: {
+ if (count > 1) {
+ uint32_t *values = (uint32_t *)malloc(count * sizeof(uint32_t));
+ if (values == NULL) {
+ LOGE("No memory for long array");
+ rc = -1;
+ } else {
+ memcpy(values, data, count * sizeof(uint32_t));
+ p_info_data[numOfEntries].tag_entry.data._longs = values;
+ }
+ } else {
+ p_info_data[numOfEntries].tag_entry.data._long = *(uint32_t *)data;
+ }
+ }
+ break;
+ case EXIF_RATIONAL: {
+ if (count > 1) {
+ rat_t *values = (rat_t *)malloc(count * sizeof(rat_t));
+ if (values == NULL) {
+ LOGE("No memory for rational array");
+ rc = -1;
+ } else {
+ memcpy(values, data, count * sizeof(rat_t));
+ p_info_data[numOfEntries].tag_entry.data._rats = values;
+ }
+ } else {
+ p_info_data[numOfEntries].tag_entry.data._rat = *(rat_t *)data;
+ }
+ }
+ break;
+ case EXIF_UNDEFINED: {
+ uint8_t *values = (uint8_t *)malloc(count);
+ if (values == NULL) {
+ LOGE("No memory for undefined array");
+ rc = -1;
+ } else {
+ memcpy(values, data, count);
+ p_info_data[numOfEntries].tag_entry.data._undefined = values;
+ }
+ }
+ break;
+ case EXIF_SLONG: {
+ if (count > 1) {
+ int32_t *values = (int32_t *)malloc(count * sizeof(int32_t));
+ if (values == NULL) {
+ LOGE("No memory for signed long array");
+ rc = -1;
+ } else {
+ memcpy(values, data, count * sizeof(int32_t));
+ p_info_data[numOfEntries].tag_entry.data._slongs = values;
+ }
+ } else {
+ p_info_data[numOfEntries].tag_entry.data._slong = *(int32_t *)data;
+ }
+ }
+ break;
+ case EXIF_SRATIONAL: {
+ if (count > 1) {
+ srat_t *values = (srat_t *)malloc(count * sizeof(srat_t));
+ if (values == NULL) {
+ LOGE("No memory for signed rational array");
+ rc = -1;
+ } else {
+ memcpy(values, data, count * sizeof(srat_t));
+ p_info_data[numOfEntries].tag_entry.data._srats = values;
+ }
+ } else {
+ p_info_data[numOfEntries].tag_entry.data._srat = *(srat_t *)data;
+ }
+ }
+ break;
+ }
+
+ // Increase number of entries
+ p_exif_info->numOfEntries++;
+ return rc;
+}
+
+/** releaseExifEntry
+ *
+ * Arguments:
+ * @p_exif_data : Exif info struct
+ *
+ * Retrun : int32_t type of status
+ * 0 -- success
+ * none-zero failure code
+ *
+ * Description:
+ * Function to release an entry from exif data
+ *
+ **/
+int32_t releaseExifEntry(QEXIF_INFO_DATA *p_exif_data)
+{
+ switch (p_exif_data->tag_entry.type) {
+ case EXIF_BYTE: {
+ if (p_exif_data->tag_entry.count > 1 &&
+ p_exif_data->tag_entry.data._bytes != NULL) {
+ free(p_exif_data->tag_entry.data._bytes);
+ p_exif_data->tag_entry.data._bytes = NULL;
+ }
+ }
+ break;
+ case EXIF_ASCII: {
+ if (p_exif_data->tag_entry.data._ascii != NULL) {
+ free(p_exif_data->tag_entry.data._ascii);
+ p_exif_data->tag_entry.data._ascii = NULL;
+ }
+ }
+ break;
+ case EXIF_SHORT: {
+ if (p_exif_data->tag_entry.count > 1 &&
+ p_exif_data->tag_entry.data._shorts != NULL) {
+ free(p_exif_data->tag_entry.data._shorts);
+ p_exif_data->tag_entry.data._shorts = NULL;
+ }
+ }
+ break;
+ case EXIF_LONG: {
+ if (p_exif_data->tag_entry.count > 1 &&
+ p_exif_data->tag_entry.data._longs != NULL) {
+ free(p_exif_data->tag_entry.data._longs);
+ p_exif_data->tag_entry.data._longs = NULL;
+ }
+ }
+ break;
+ case EXIF_RATIONAL: {
+ if (p_exif_data->tag_entry.count > 1 &&
+ p_exif_data->tag_entry.data._rats != NULL) {
+ free(p_exif_data->tag_entry.data._rats);
+ p_exif_data->tag_entry.data._rats = NULL;
+ }
+ }
+ break;
+ case EXIF_UNDEFINED: {
+ if (p_exif_data->tag_entry.data._undefined != NULL) {
+ free(p_exif_data->tag_entry.data._undefined);
+ p_exif_data->tag_entry.data._undefined = NULL;
+ }
+ }
+ break;
+ case EXIF_SLONG: {
+ if (p_exif_data->tag_entry.count > 1 &&
+ p_exif_data->tag_entry.data._slongs != NULL) {
+ free(p_exif_data->tag_entry.data._slongs);
+ p_exif_data->tag_entry.data._slongs = NULL;
+ }
+ }
+ break;
+ case EXIF_SRATIONAL: {
+ if (p_exif_data->tag_entry.count > 1 &&
+ p_exif_data->tag_entry.data._srats != NULL) {
+ free(p_exif_data->tag_entry.data._srats);
+ p_exif_data->tag_entry.data._srats = NULL;
+ }
+ }
+ break;
+ } /*end of switch*/
+
+ return 0;
+}
+
+/** process_sensor_data:
+ *
+ * Arguments:
+ * @p_sensor_params : ptr to sensor data
+ *
+ * Return : int32_t type of status
+ * NO_ERROR -- success
+ * none-zero failure code
+ *
+ * Description:
+ * process sensor data
+ *
+ * Notes: this needs to be filled for the metadata
+ **/
+int process_sensor_data(cam_sensor_params_t *p_sensor_params,
+ QOMX_EXIF_INFO *exif_info)
+{
+ int rc = 0;
+ rat_t val_rat;
+
+ if (NULL == p_sensor_params) {
+ LOGE("Sensor params are null");
+ return 0;
+ }
+
+ LOGD("From metadata aperture = %f ",
+ p_sensor_params->aperture_value );
+
+ if (p_sensor_params->aperture_value >= 1.0) {
+ double apex_value;
+ apex_value = (double)2.0 * log(p_sensor_params->aperture_value) / log(2.0);
+ val_rat.num = (uint32_t)(apex_value * 100);
+ val_rat.denom = 100;
+ rc = addExifEntry(exif_info, EXIFTAGID_APERTURE, EXIF_RATIONAL, 1, &val_rat);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ val_rat.num = (uint32_t)(p_sensor_params->aperture_value * 100);
+ val_rat.denom = 100;
+ rc = addExifEntry(exif_info, EXIFTAGID_F_NUMBER, EXIF_RATIONAL, 1, &val_rat);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+ }
+
+ /*Flash*/
+ short val_short = 0;
+ int flash_mode_exif, flash_fired;
+ if (p_sensor_params->flash_state == CAM_FLASH_STATE_FIRED) {
+ flash_fired = 1;
+ } else {
+ flash_fired = 0;
+ }
+ LOGD("Flash mode %d flash state %d",
+ p_sensor_params->flash_mode, p_sensor_params->flash_state);
+
+ switch(p_sensor_params->flash_mode) {
+ case CAM_FLASH_MODE_OFF:
+ flash_mode_exif = MM_JPEG_EXIF_FLASH_MODE_OFF;
+ break;
+ case CAM_FLASH_MODE_ON:
+ flash_mode_exif = MM_JPEG_EXIF_FLASH_MODE_ON;
+ break;
+ case CAM_FLASH_MODE_AUTO:
+ flash_mode_exif = MM_JPEG_EXIF_FLASH_MODE_AUTO;
+ break;
+ default:
+ flash_mode_exif = MM_JPEG_EXIF_FLASH_MODE_AUTO;
+ LOGE(": Unsupported flash mode");
+ }
+ val_short = (short)(flash_fired | (flash_mode_exif << 3));
+
+ rc = addExifEntry(exif_info, EXIFTAGID_FLASH, EXIF_SHORT, 1, &val_short);
+ if (rc) {
+ LOGE(": Error adding flash exif entry");
+ }
+ /* Sensing Method */
+ val_short = (short) p_sensor_params->sensing_method;
+ rc = addExifEntry(exif_info, EXIFTAGID_SENSING_METHOD, EXIF_SHORT,
+ sizeof(val_short)/2, &val_short);
+ if (rc) {
+ LOGE(": Error adding flash Exif Entry");
+ }
+
+ /* Focal Length in 35 MM Film */
+ val_short = (short)
+ ((p_sensor_params->focal_length * p_sensor_params->crop_factor) + 0.5f);
+ rc = addExifEntry(exif_info, EXIFTAGID_FOCAL_LENGTH_35MM, EXIF_SHORT,
+ 1, &val_short);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ /* F Number */
+ val_rat.num = (uint32_t)(p_sensor_params->f_number * 100);
+ val_rat.denom = 100;
+ rc = addExifEntry(exif_info, EXIFTAGTYPE_F_NUMBER, EXIF_RATIONAL, 1, &val_rat);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+ return rc;
+}
+
+
+/** process_3a_data:
+ *
+ * Arguments:
+ * @p_3a_params : ptr to 3a data
+ * @exif_info : Exif info struct
+ *
+ * Return : int32_t type of status
+ * NO_ERROR -- success
+ * none-zero failure code
+ *
+ * Description:
+ * process 3a data
+ *
+ * Notes: this needs to be filled for the metadata
+ **/
+int process_3a_data(cam_3a_params_t *p_3a_params, QOMX_EXIF_INFO *exif_info)
+{
+ int rc = 0;
+ srat_t val_srat;
+ rat_t val_rat;
+ double shutter_speed_value;
+
+ if (NULL == p_3a_params) {
+ LOGE("3A params are null");
+ return 0;
+ }
+
+ LOGD("exp_time %f, iso_value %d, wb_mode %d",
+ p_3a_params->exp_time, p_3a_params->iso_value, p_3a_params->wb_mode);
+
+ /* Exposure time */
+ if (p_3a_params->exp_time <= 0.0f) {
+ val_rat.num = 0;
+ val_rat.denom = 0;
+ } else if (p_3a_params->exp_time < 1.0f) {
+ val_rat.num = 1;
+ val_rat.denom = ROUND(1.0/p_3a_params->exp_time);
+ } else {
+ val_rat.num = ROUND(p_3a_params->exp_time);
+ val_rat.denom = 1;
+ }
+ LOGD("numer %d denom %d %zd", val_rat.num, val_rat.denom,
+ sizeof(val_rat) / (8));
+
+ rc = addExifEntry(exif_info, EXIFTAGID_EXPOSURE_TIME, EXIF_RATIONAL,
+ (sizeof(val_rat)/(8)), &val_rat);
+ if (rc) {
+ LOGE(": Error adding Exif Entry Exposure time");
+ }
+
+ /* Shutter Speed*/
+ if (p_3a_params->exp_time > 0) {
+ shutter_speed_value = log10(1/p_3a_params->exp_time)/log10(2);
+ val_srat.num = (int32_t)(shutter_speed_value * 1000);
+ val_srat.denom = 1000;
+ } else {
+ val_srat.num = 0;
+ val_srat.denom = 0;
+ }
+ rc = addExifEntry(exif_info, EXIFTAGID_SHUTTER_SPEED, EXIF_SRATIONAL,
+ (sizeof(val_srat)/(8)), &val_srat);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ /*ISO*/
+ short val_short;
+ val_short = (short)p_3a_params->iso_value;
+ rc = addExifEntry(exif_info, EXIFTAGID_ISO_SPEED_RATING, EXIF_SHORT,
+ sizeof(val_short)/2, &val_short);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ /*WB mode*/
+ if (p_3a_params->wb_mode == CAM_WB_MODE_AUTO)
+ val_short = 0;
+ else
+ val_short = 1;
+ rc = addExifEntry(exif_info, EXIFTAGID_WHITE_BALANCE, EXIF_SHORT,
+ sizeof(val_short)/2, &val_short);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ /* Metering Mode */
+ val_short = (short) p_3a_params->metering_mode;
+ rc = addExifEntry(exif_info,EXIFTAGID_METERING_MODE, EXIF_SHORT,
+ sizeof(val_short)/2, &val_short);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ /*Exposure Program*/
+ val_short = (short) p_3a_params->exposure_program;
+ rc = addExifEntry(exif_info,EXIFTAGID_EXPOSURE_PROGRAM, EXIF_SHORT,
+ sizeof(val_short)/2, &val_short);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ /*Exposure Mode */
+ val_short = (short) p_3a_params->exposure_mode;
+ rc = addExifEntry(exif_info,EXIFTAGID_EXPOSURE_MODE, EXIF_SHORT,
+ sizeof(val_short)/2, &val_short);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ /*Scenetype*/
+ uint8_t val_undef;
+ val_undef = (uint8_t) p_3a_params->scenetype;
+ rc = addExifEntry(exif_info,EXIFTAGID_SCENE_TYPE, EXIF_UNDEFINED,
+ sizeof(val_undef), &val_undef);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ LOGD("brightness %f",
+ p_3a_params->brightness);
+
+ /* Brightness Value*/
+ val_srat.num = (int32_t) (p_3a_params->brightness * 100.0f);
+ val_srat.denom = 100;
+ rc = addExifEntry(exif_info,EXIFTAGID_BRIGHTNESS, EXIF_SRATIONAL,
+ (sizeof(val_srat)/(8)), &val_srat);
+ if (rc) {
+ LOGE(": Error adding Exif Entry");
+ }
+
+ return rc;
+}
+
+/** process_meta_data
+ *
+ * Arguments:
+ * @p_meta : ptr to metadata
+ * @exif_info: Exif info struct
+ * @mm_jpeg_exif_params: exif params
+ *
+ * Return : int32_t type of status
+ * NO_ERROR -- success
+ * none-zero failure code
+ *
+ * Description:
+ * Extract exif data from the metadata
+ **/
+int process_meta_data(metadata_buffer_t *p_meta, QOMX_EXIF_INFO *exif_info,
+ mm_jpeg_exif_params_t *p_cam_exif_params, cam_hal_version_t hal_version)
+{
+ int rc = 0;
+ cam_sensor_params_t p_sensor_params;
+ cam_3a_params_t p_3a_params;
+ bool is_3a_meta_valid = false, is_sensor_meta_valid = false;
+
+ memset(&p_3a_params, 0, sizeof(cam_3a_params_t));
+ memset(&p_sensor_params, 0, sizeof(cam_sensor_params_t));
+
+ if (p_meta) {
+ /* for HAL V1*/
+ if (hal_version == CAM_HAL_V1) {
+
+ IF_META_AVAILABLE(cam_3a_params_t, l_3a_params, CAM_INTF_META_AEC_INFO,
+ p_meta) {
+ p_3a_params = *l_3a_params;
+ is_3a_meta_valid = true;
+ }
+
+ IF_META_AVAILABLE(int32_t, wb_mode, CAM_INTF_PARM_WHITE_BALANCE, p_meta) {
+ p_3a_params.wb_mode = *wb_mode;
+ }
+
+ IF_META_AVAILABLE(cam_sensor_params_t, l_sensor_params,
+ CAM_INTF_META_SENSOR_INFO, p_meta) {
+ p_sensor_params = *l_sensor_params;
+ is_sensor_meta_valid = true;
+ }
+ } else {
+ /* HAL V3 */
+ IF_META_AVAILABLE(int32_t, iso, CAM_INTF_META_SENSOR_SENSITIVITY, p_meta) {
+ p_3a_params.iso_value= *iso;
+ } else {
+ LOGE("Cannot extract Iso value");
+ }
+
+ IF_META_AVAILABLE(int64_t, sensor_exposure_time,
+ CAM_INTF_META_SENSOR_EXPOSURE_TIME, p_meta) {
+ p_3a_params.exp_time =
+ (float)((double)(*sensor_exposure_time) / 1000000000.0);
+ } else {
+ LOGE("Cannot extract Exp time value");
+ }
+
+ IF_META_AVAILABLE(int32_t, wb_mode, CAM_INTF_PARM_WHITE_BALANCE, p_meta) {
+ p_3a_params.wb_mode = *wb_mode;
+ } else {
+ LOGE("Cannot extract white balance mode");
+ }
+
+ /* Process sensor data */
+ IF_META_AVAILABLE(float, aperture, CAM_INTF_META_LENS_APERTURE, p_meta) {
+ p_sensor_params.aperture_value = *aperture;
+ } else {
+ LOGE("Cannot extract Aperture value");
+ }
+
+ IF_META_AVAILABLE(uint32_t, flash_mode, CAM_INTF_META_FLASH_MODE, p_meta) {
+ p_sensor_params.flash_mode = *flash_mode;
+ } else {
+ LOGE("Cannot extract flash mode value");
+ }
+
+ IF_META_AVAILABLE(int32_t, flash_state, CAM_INTF_META_FLASH_STATE, p_meta) {
+ p_sensor_params.flash_state = (cam_flash_state_t) *flash_state;
+ } else {
+ LOGE("Cannot extract flash state value");
+ }
+ }
+ }
+
+ /* take the cached values if meta is invalid */
+ if ((!is_3a_meta_valid) && (hal_version == CAM_HAL_V1)) {
+ p_3a_params = p_cam_exif_params->cam_3a_params;
+ LOGW("Warning using cached values for 3a");
+ }
+
+ if ((!is_sensor_meta_valid) && (hal_version == CAM_HAL_V1)) {
+ p_sensor_params = p_cam_exif_params->sensor_params;
+ LOGW("Warning using cached values for sensor");
+ }
+
+ if ((hal_version != CAM_HAL_V1) || (p_sensor_params.sens_type != CAM_SENSOR_YUV)) {
+ rc = process_3a_data(&p_3a_params, exif_info);
+ if (rc) {
+ LOGE("Failed to add 3a exif params");
+ }
+ }
+
+ rc = process_sensor_data(&p_sensor_params, exif_info);
+ if (rc) {
+ LOGE("Failed to extract sensor params");
+ }
+
+ if (p_meta) {
+ short val_short = 0;
+ cam_asd_decision_t *scene_info = NULL;
+
+ IF_META_AVAILABLE(cam_asd_decision_t, scene_cap_type,
+ CAM_INTF_META_ASD_SCENE_INFO, p_meta) {
+ scene_info = (cam_asd_decision_t*)scene_cap_type;
+ val_short = (short) scene_info->detected_scene;
+ }
+
+ rc = addExifEntry(exif_info, EXIFTAGID_SCENE_CAPTURE_TYPE, EXIF_SHORT,
+ sizeof(val_short)/2, &val_short);
+ if (rc) {
+ LOGE(": Error adding ASD Exif Entry");
+ }
+ } else {
+ LOGE(": Error adding ASD Exif Entry, no meta");
+ }
+ return rc;
+}
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_interface.c b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_interface.c
new file mode 100644
index 0000000..5655c49
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_interface.c
@@ -0,0 +1,409 @@
+/* Copyright (c) 2012-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// To remove
+#include <cutils/properties.h>
+
+// System dependencies
+#include <stdlib.h>
+#include <pthread.h>
+
+// JPEG dependencies
+#include "mm_jpeg_dbg.h"
+#include "mm_jpeg_interface.h"
+#include "mm_jpeg.h"
+#include "mm_jpeg_mpo.h"
+
+static pthread_mutex_t g_intf_lock = PTHREAD_MUTEX_INITIALIZER;
+static mm_jpeg_obj* g_jpeg_obj = NULL;
+
+static pthread_mutex_t g_handler_lock = PTHREAD_MUTEX_INITIALIZER;
+static uint16_t g_handler_history_count = 0; /* history count for handler */
+volatile uint32_t gKpiDebugLevel = 0;
+
+/** mm_jpeg_util_generate_handler:
+ *
+ * Arguments:
+ * @index: client index
+ *
+ * Return:
+ * handle value
+ *
+ * Description:
+ * utility function to generate handler
+ *
+ **/
+uint32_t mm_jpeg_util_generate_handler(uint8_t index)
+{
+ uint32_t handler = 0;
+ pthread_mutex_lock(&g_handler_lock);
+ g_handler_history_count++;
+ if (0 == g_handler_history_count) {
+ g_handler_history_count++;
+ }
+ handler = g_handler_history_count;
+ handler = (handler<<8) | index;
+ pthread_mutex_unlock(&g_handler_lock);
+ return handler;
+}
+
+/** mm_jpeg_util_get_index_by_handler:
+ *
+ * Arguments:
+ * @handler: handle value
+ *
+ * Return:
+ * client index
+ *
+ * Description:
+ * get client index
+ *
+ **/
+uint8_t mm_jpeg_util_get_index_by_handler(uint32_t handler)
+{
+ return (handler & 0x000000ff);
+}
+
+/** mm_jpeg_intf_start_job:
+ *
+ * Arguments:
+ * @client_hdl: client handle
+ * @job: jpeg job object
+ * @jobId: job id
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * start the jpeg job
+ *
+ **/
+static int32_t mm_jpeg_intf_start_job(mm_jpeg_job_t* job, uint32_t* job_id)
+{
+ int32_t rc = -1;
+
+ if (NULL == job ||
+ NULL == job_id) {
+ LOGE("invalid parameters for job or jobId");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_intf_lock);
+ if (NULL == g_jpeg_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+ }
+ rc = mm_jpeg_start_job(g_jpeg_obj, job, job_id);
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+}
+
+/** mm_jpeg_intf_create_session:
+ *
+ * Arguments:
+ * @client_hdl: client handle
+ * @p_params: encode parameters
+ * @p_session_id: session id
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Create new jpeg session
+ *
+ **/
+static int32_t mm_jpeg_intf_create_session(uint32_t client_hdl,
+ mm_jpeg_encode_params_t *p_params,
+ uint32_t *p_session_id)
+{
+ int32_t rc = -1;
+
+ if (0 == client_hdl || NULL == p_params || NULL == p_session_id) {
+ LOGE("invalid client_hdl or jobId");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_intf_lock);
+ if (NULL == g_jpeg_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+ }
+
+ rc = mm_jpeg_create_session(g_jpeg_obj, client_hdl, p_params, p_session_id);
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+}
+
+/** mm_jpeg_intf_destroy_session:
+ *
+ * Arguments:
+ * @session_id: session id
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Destroy jpeg session
+ *
+ **/
+static int32_t mm_jpeg_intf_destroy_session(uint32_t session_id)
+{
+ int32_t rc = -1;
+
+ if (0 == session_id) {
+ LOGE("invalid client_hdl or jobId");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_intf_lock);
+ if (NULL == g_jpeg_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+ }
+
+ rc = mm_jpeg_destroy_session_by_id(g_jpeg_obj, session_id);
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+}
+
+/** mm_jpeg_intf_abort_job:
+ *
+ * Arguments:
+ * @jobId: job id
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Abort the jpeg job
+ *
+ **/
+static int32_t mm_jpeg_intf_abort_job(uint32_t job_id)
+{
+ int32_t rc = -1;
+
+ if (0 == job_id) {
+ LOGE("invalid jobId");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_intf_lock);
+ if (NULL == g_jpeg_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+ }
+
+ rc = mm_jpeg_abort_job(g_jpeg_obj, job_id);
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+}
+
+/** mm_jpeg_intf_close:
+ *
+ * Arguments:
+ * @client_hdl: client handle
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Close the jpeg job
+ *
+ **/
+static int32_t mm_jpeg_intf_close(uint32_t client_hdl)
+{
+ int32_t rc = -1;
+
+ if (0 == client_hdl) {
+ LOGE("invalid client_hdl");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_intf_lock);
+ if (NULL == g_jpeg_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+ }
+
+ rc = mm_jpeg_close(g_jpeg_obj, client_hdl);
+ g_jpeg_obj->num_clients--;
+ if(0 == rc) {
+ if (0 == g_jpeg_obj->num_clients) {
+ /* No client, close jpeg internally */
+ rc = mm_jpeg_deinit(g_jpeg_obj);
+ free(g_jpeg_obj);
+ g_jpeg_obj = NULL;
+ }
+ }
+
+ pthread_mutex_unlock(&g_intf_lock);
+ return rc;
+}
+
+/** mm_jpeg_intf_compose_mpo:
+ *
+ * Arguments:
+ * @mpo_info : MPO Information
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Compose MPO image from jpeg images
+ *
+ **/
+static int32_t mm_jpeg_intf_compose_mpo(mm_jpeg_mpo_info_t *mpo_info)
+{
+ int32_t rc = -1;
+ if (!mpo_info) {
+ LOGE("Invalid input");
+ return rc;
+ }
+
+ if (mpo_info->num_of_images > MM_JPEG_MAX_MPO_IMAGES) {
+ LOGE("Num of images exceeds max supported images in MPO");
+ return rc;
+ }
+ //Call MPo composition
+ rc = mm_jpeg_mpo_compose(mpo_info);
+
+ return rc;
+}
+
+/** jpeg_open:
+ *
+ * Arguments:
+ * @ops: ops table pointer
+ * @mpo_ops: mpo ops table ptr
+ * @picture_size: Max available dim
+ * @jpeg_metadata: Jpeg meta data
+ *
+ * Return:
+ * 0 failure, success otherwise
+ *
+ * Description:
+ * Open a jpeg client. Jpeg meta data will be cached
+ * but memory manegement has to be done by the cient.
+ *
+ **/
+uint32_t jpeg_open(mm_jpeg_ops_t *ops, mm_jpeg_mpo_ops_t *mpo_ops,
+ mm_dimension picture_size,
+ cam_jpeg_metadata_t *jpeg_metadata)
+{
+ int32_t rc = 0;
+ uint32_t clnt_hdl = 0;
+ mm_jpeg_obj* jpeg_obj = NULL;
+ char prop[PROPERTY_VALUE_MAX];
+
+ property_get("persist.camera.kpi.debug", prop, "0");
+ gKpiDebugLevel = atoi(prop);
+
+ pthread_mutex_lock(&g_intf_lock);
+ /* first time open */
+ if(NULL == g_jpeg_obj) {
+ jpeg_obj = (mm_jpeg_obj *)malloc(sizeof(mm_jpeg_obj));
+ if(NULL == jpeg_obj) {
+ LOGE("no mem");
+ pthread_mutex_unlock(&g_intf_lock);
+ return clnt_hdl;
+ }
+
+ /* initialize jpeg obj */
+ memset(jpeg_obj, 0, sizeof(mm_jpeg_obj));
+
+ /* by default reuse reproc source buffer if available */
+ if (mpo_ops == NULL) {
+ jpeg_obj->reuse_reproc_buffer = 1;
+ } else {
+ jpeg_obj->reuse_reproc_buffer = 0;
+ }
+ LOGH("reuse_reproc_buffer %d ",
+ jpeg_obj->reuse_reproc_buffer);
+
+ /* used for work buf calculation */
+ jpeg_obj->max_pic_w = picture_size.w;
+ jpeg_obj->max_pic_h = picture_size.h;
+
+ /*Cache OTP Data for the session*/
+ if (NULL != jpeg_metadata) {
+ jpeg_obj->jpeg_metadata = jpeg_metadata;
+ }
+
+ rc = mm_jpeg_init(jpeg_obj);
+ if(0 != rc) {
+ LOGE("mm_jpeg_init err = %d", rc);
+ free(jpeg_obj);
+ pthread_mutex_unlock(&g_intf_lock);
+ return clnt_hdl;
+ }
+
+ /* remember in global variable */
+ g_jpeg_obj = jpeg_obj;
+ }
+
+ /* open new client */
+ clnt_hdl = mm_jpeg_new_client(g_jpeg_obj);
+ if (clnt_hdl > 0) {
+ /* valid client */
+ if (NULL != ops) {
+ /* fill in ops tbl if ptr not NULL */
+ ops->start_job = mm_jpeg_intf_start_job;
+ ops->abort_job = mm_jpeg_intf_abort_job;
+ ops->create_session = mm_jpeg_intf_create_session;
+ ops->destroy_session = mm_jpeg_intf_destroy_session;
+ ops->close = mm_jpeg_intf_close;
+ }
+ if (NULL != mpo_ops) {
+ mpo_ops->compose_mpo = mm_jpeg_intf_compose_mpo;
+ }
+ } else {
+ /* failed new client */
+ LOGE("mm_jpeg_new_client failed");
+
+ if (0 == g_jpeg_obj->num_clients) {
+ /* no client, close jpeg */
+ mm_jpeg_deinit(g_jpeg_obj);
+ free(g_jpeg_obj);
+ g_jpeg_obj = NULL;
+ }
+ }
+
+ pthread_mutex_unlock(&g_intf_lock);
+ return clnt_hdl;
+}
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_ionbuf.c b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_ionbuf.c
new file mode 100644
index 0000000..34702e7
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_ionbuf.c
@@ -0,0 +1,206 @@
+/* Copyright (c) 2013-2014, 2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// System dependencies
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <linux/msm_ion.h>
+#define MMAN_H <SYSTEM_HEADER_PREFIX/mman.h>
+#include MMAN_H
+
+// JPEG dependencies
+#include "mm_jpeg_ionbuf.h"
+
+/** buffer_allocate:
+ *
+ * Arguments:
+ * @p_buffer: ION buffer
+ *
+ * Return:
+ * buffer address
+ *
+ * Description:
+ * allocates ION buffer
+ *
+ **/
+void *buffer_allocate(buffer_t *p_buffer, int cached)
+{
+ void *l_buffer = NULL;
+
+ int lrc = 0;
+ struct ion_handle_data lhandle_data;
+
+ p_buffer->alloc.len = p_buffer->size;
+ p_buffer->alloc.align = 4096;
+ p_buffer->alloc.flags = (cached) ? ION_FLAG_CACHED : 0;
+ p_buffer->alloc.heap_id_mask = 0x1 << ION_IOMMU_HEAP_ID;
+
+ p_buffer->ion_fd = open("/dev/ion", O_RDONLY);
+ if(p_buffer->ion_fd < 0) {
+ LOGE("Ion open failed");
+ goto ION_ALLOC_FAILED;
+ }
+
+ /* Make it page size aligned */
+ p_buffer->alloc.len = (p_buffer->alloc.len + 4095U) & (~4095U);
+ lrc = ioctl(p_buffer->ion_fd, ION_IOC_ALLOC, &p_buffer->alloc);
+ if (lrc < 0) {
+ LOGE("ION allocation failed len %zu",
+ p_buffer->alloc.len);
+ goto ION_ALLOC_FAILED;
+ }
+
+ p_buffer->ion_info_fd.handle = p_buffer->alloc.handle;
+ lrc = ioctl(p_buffer->ion_fd, ION_IOC_SHARE,
+ &p_buffer->ion_info_fd);
+ if (lrc < 0) {
+ LOGE("ION map failed %s", strerror(errno));
+ goto ION_MAP_FAILED;
+ }
+
+ p_buffer->p_pmem_fd = p_buffer->ion_info_fd.fd;
+
+ l_buffer = mmap(NULL, p_buffer->alloc.len, PROT_READ | PROT_WRITE,
+ MAP_SHARED,p_buffer->p_pmem_fd, 0);
+
+ if (l_buffer == MAP_FAILED) {
+ LOGE("ION_MMAP_FAILED: %s (%d)",
+ strerror(errno), errno);
+ goto ION_MAP_FAILED;
+ }
+
+ return l_buffer;
+
+ION_MAP_FAILED:
+ lhandle_data.handle = p_buffer->ion_info_fd.handle;
+ ioctl(p_buffer->ion_fd, ION_IOC_FREE, &lhandle_data);
+ return NULL;
+ION_ALLOC_FAILED:
+ return NULL;
+
+}
+
+/** buffer_deallocate:
+ *
+ * Arguments:
+ * @p_buffer: ION buffer
+ *
+ * Return:
+ * buffer address
+ *
+ * Description:
+ * deallocates ION buffer
+ *
+ **/
+int buffer_deallocate(buffer_t *p_buffer)
+{
+ int lrc = 0;
+ size_t lsize = (p_buffer->size + 4095U) & (~4095U);
+
+ struct ion_handle_data lhandle_data;
+ lrc = munmap(p_buffer->addr, lsize);
+
+ close(p_buffer->ion_info_fd.fd);
+
+ lhandle_data.handle = p_buffer->ion_info_fd.handle;
+ ioctl(p_buffer->ion_fd, ION_IOC_FREE, &lhandle_data);
+
+ close(p_buffer->ion_fd);
+ return lrc;
+}
+
+/** buffer_invalidate:
+ *
+ * Arguments:
+ * @p_buffer: ION buffer
+ *
+ * Return:
+ * error val
+ *
+ * Description:
+ * Invalidates the cached buffer
+ *
+ **/
+int buffer_invalidate(buffer_t *p_buffer)
+{
+ int lrc = 0;
+ struct ion_flush_data cache_inv_data;
+ struct ion_custom_data custom_data;
+
+ memset(&cache_inv_data, 0, sizeof(cache_inv_data));
+ memset(&custom_data, 0, sizeof(custom_data));
+ cache_inv_data.vaddr = p_buffer->addr;
+ cache_inv_data.fd = p_buffer->ion_info_fd.fd;
+ cache_inv_data.handle = p_buffer->ion_info_fd.handle;
+ cache_inv_data.length = (unsigned int)p_buffer->size;
+ custom_data.cmd = (unsigned int)ION_IOC_INV_CACHES;
+ custom_data.arg = (unsigned long)&cache_inv_data;
+
+ lrc = ioctl(p_buffer->ion_fd, ION_IOC_CUSTOM, &custom_data);
+ if (lrc < 0)
+ LOGW("Cache Invalidate failed: %s\n", strerror(errno));
+
+ return lrc;
+}
+
+/** buffer_clean:
+ *
+ * Arguments:
+ * @p_buffer: ION buffer
+ *
+ * Return:
+ * error val
+ *
+ * Description:
+ * Clean the cached buffer
+ *
+ **/
+int buffer_clean(buffer_t *p_buffer)
+{
+ int lrc = 0;
+ struct ion_flush_data cache_clean_data;
+ struct ion_custom_data custom_data;
+
+ memset(&cache_clean_data, 0, sizeof(cache_clean_data));
+ memset(&custom_data, 0, sizeof(custom_data));
+ cache_clean_data.vaddr = p_buffer->addr;
+ cache_clean_data.fd = p_buffer->ion_info_fd.fd;
+ cache_clean_data.handle = p_buffer->ion_info_fd.handle;
+ cache_clean_data.length = (unsigned int)p_buffer->size;
+ custom_data.cmd = (unsigned int)ION_IOC_CLEAN_CACHES;
+ custom_data.arg = (unsigned long)&cache_clean_data;
+
+ lrc = ioctl(p_buffer->ion_fd, ION_IOC_CUSTOM, &custom_data);
+ if (lrc < 0)
+ LOGW("Cache clean failed: %s\n", strerror(errno));
+
+ return lrc;
+}
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_mpo_composer.c b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_mpo_composer.c
new file mode 100644
index 0000000..fb9c222
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_mpo_composer.c
@@ -0,0 +1,414 @@
+/* Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#define ATRACE_TAG ATRACE_TAG_CAMERA
+
+// System dependencies
+#include <pthread.h>
+
+// JPEG dependencies
+#include "mm_jpeg_dbg.h"
+#include "mm_jpeg_mpo.h"
+
+#define M_APP0 0xe0
+#define M_APP1 0xe1
+#define M_APP2 0xe2
+#define M_EOI 0xd9
+#define M_SOI 0xd8
+
+/** READ_LONG:
+ * @b: Buffer start addr
+ * @o: Buffer offset to start reading
+ *
+ * Read long value from the specified buff addr at given offset
+ **/
+#define READ_LONG(b, o) \
+ (uint32_t)(((uint32_t)b[o] << 24) + \
+ ((uint32_t)b[o+1] << 16) + \
+ ((uint32_t)b[o+2] << 8) + \
+ ((uint32_t)b[o+3]))
+
+/** READ_LONG_LITTLE:
+ * @b: Buffer start addr
+ * @o: Buffer offset to start reading
+ *
+ * Read long value from the specified buff addr at given offset
+ * in Little Endian
+ **/
+#define READ_LONG_LITTLE(b, o) \
+ (uint32_t)(((uint32_t)b[o + 3] << 24) + \
+ ((uint32_t) b[o + 2] << 16) + \
+ ((uint32_t) b[o + 1] << 8) + \
+ (uint32_t) b[o]);
+
+/** READ_LONG:
+ * @b: Buffer start addr
+ * @o: Buffer offset to start reading
+ *
+ * Read short value from the specified buff addr at given
+ * offset
+ **/
+#define READ_SHORT(b, o) \
+ (uint16_t) (((uint16_t)b[o] << 8) + \
+ (uint16_t) b[o + 1]);
+
+/*Mutex to serializa MPO composition*/
+static pthread_mutex_t g_mpo_lock = PTHREAD_MUTEX_INITIALIZER;
+
+/** mm_jpeg_mpo_write_long_little_endian
+ *
+ * Arguments:
+ * @buffer_addr: image start addr
+ * @buff_offset: offset in the buffer
+ * @buffer_size: Size of the buffer
+ * @value: Value to write
+ * @overflow : Overflow flag
+ *
+ * Return:
+ * None
+ *
+ * Description:
+ * Write value at the given offset
+ *
+ **/
+void mm_jpeg_mpo_write_long_little_endian(uint8_t *buff_addr, uint32_t buff_offset,
+ uint32_t buffer_size, int value, uint8_t *overflow)
+{
+ if (buff_offset + 3 >= buffer_size) {
+ *overflow = TRUE;
+ }
+
+ if (!(*overflow)) {
+ buff_addr[buff_offset + 3] = (uint8_t)((value >> 24) & 0xFF);
+ buff_addr[buff_offset + 2] = (uint8_t)((value >> 16) & 0xFF);
+ buff_addr[buff_offset + 1] = (uint8_t)((value >> 8) & 0xFF);
+ buff_addr[buff_offset] = (uint8_t)(value & 0xFF);
+ }
+}
+
+/** mm_jpeg_mpo_write_long
+ *
+ * Arguments:
+ * @buffer_addr: image start addr
+ * @buff_offset: offset in the buffer
+ * @buffer_size: Size of the buffer
+ * @value: Value to write
+ * @overflow : Overflow flag
+ *
+ * Return:
+ * None
+ *
+ * Description:
+ * Write value at the given offset
+ *
+ **/
+void mm_jpeg_mpo_write_long(uint8_t *buff_addr, uint32_t buff_offset,
+ uint32_t buffer_size, int value, uint8_t *overflow)
+{
+ if ((buff_offset + 3) >= buffer_size) {
+ *overflow = TRUE;
+ }
+
+ if (!(*overflow)) {
+ buff_addr[buff_offset] = (uint8_t)((value >> 24) & 0xFF);
+ buff_addr[buff_offset+1] = (uint8_t)((value >> 16) & 0xFF);
+ buff_addr[buff_offset+2] = (uint8_t)((value >> 8) & 0xFF);
+ buff_addr[buff_offset+3] = (uint8_t)(value & 0xFF);
+ }
+}
+
+/** mm_jpeg_mpo_get_app_marker
+ *
+ * Arguments:
+ * @buffer_addr: Jpeg image start addr
+ * @buffer_size: Size of the Buffer
+ * @app_marker: app_marker to find
+ *
+ * Return:
+ * Start offset of the specified app marker
+ *
+ * Description:
+ * Gets the start offset of the given app marker
+ *
+ **/
+uint8_t *mm_jpeg_mpo_get_app_marker(uint8_t *buffer_addr, int buffer_size,
+ int app_marker)
+{
+ int32_t byte;
+ uint8_t *p_current_addr = NULL, *p_start_offset = NULL;
+ uint16_t app_marker_size = 0;
+
+ p_current_addr = buffer_addr;
+ do {
+ do {
+ byte = *(p_current_addr);
+ p_current_addr++;
+ }
+ while ((byte != 0xFF) &&
+ (p_current_addr < (buffer_addr + (buffer_size - 1))));
+
+ //If 0xFF is not found at all, break
+ if (byte != 0xFF) {
+ LOGD("0xFF not found");
+ break;
+ }
+
+ //Read the next byte after 0xFF
+ byte = *(p_current_addr);
+ LOGD("Byte %x", byte);
+ if (byte == app_marker) {
+ LOGD("Byte %x", byte);
+ p_start_offset = ++p_current_addr;
+ break;
+ } else if (byte != M_SOI) {
+ app_marker_size = READ_SHORT(p_current_addr, 1);
+ LOGD("size %d", app_marker_size);
+ p_current_addr += app_marker_size;
+ }
+ }
+ while ((byte != M_EOI) &&
+ (p_current_addr < (buffer_addr + (buffer_size - 1))));
+
+ return p_start_offset;
+}
+
+/** mm_jpeg_mpo_get_mp_header
+ *
+ * Arguments:
+ * @app2_marker: app2_marker start offset
+ *
+ * Return:
+ * Start offset of the MP header
+ *
+ * Description:
+ * Get the start offset of the MP header (before the MP
+ * Endian field). All offsets in the MP header need to be
+ * specified wrt this start offset.
+ *
+ **/
+uint8_t *mm_jpeg_mpo_get_mp_header(uint8_t *app2_start_offset)
+{
+ uint8_t *mp_headr_start_offset = NULL;
+
+ if (app2_start_offset != NULL) {
+ mp_headr_start_offset = app2_start_offset + MP_APP2_FIELD_LENGTH_BYTES +
+ MP_FORMAT_IDENTIFIER_BYTES;
+ }
+
+ return mp_headr_start_offset;
+}
+
+/** mm_jpeg_mpo_update_header
+ *
+ * Arguments:
+ * @mpo_info: MPO Info
+ *
+ * Return:
+ * 0 - Success
+ * -1 - otherwise
+ *
+ * Description:
+ * Update the MP Index IFD of the first image with info
+ * about about all other images.
+ *
+ **/
+int mm_jpeg_mpo_update_header(mm_jpeg_mpo_info_t *mpo_info)
+{
+ uint8_t *app2_start_off_addr = NULL, *mp_headr_start_off_addr = NULL;
+ uint32_t mp_index_ifd_offset = 0, current_offset = 0, mp_entry_val_offset = 0;
+ uint8_t *aux_start_addr = NULL;
+ uint8_t overflow_flag = 0;
+ int i = 0, rc = -1;
+ uint32_t endianess = MPO_LITTLE_ENDIAN, offset_to_nxt_ifd = 8;
+ uint16_t ifd_tag_count = 0;
+
+ //Get the addr of the App Marker
+ app2_start_off_addr = mm_jpeg_mpo_get_app_marker(
+ mpo_info->output_buff.buf_vaddr, mpo_info->primary_image.buf_filled_len, M_APP2);
+ if (!app2_start_off_addr) {
+ LOGE("Cannot find App2 marker. MPO composition failed" );
+ return rc;
+ }
+ LOGD("app2_start_off_addr %p = %x",
+ app2_start_off_addr, *app2_start_off_addr);
+
+ //Get the addr of the MP Headr start offset.
+ //All offsets in the MP header are wrt to this addr
+ mp_headr_start_off_addr = mm_jpeg_mpo_get_mp_header(app2_start_off_addr);
+ if (!mp_headr_start_off_addr) {
+ LOGE("mp headr start offset is NULL. MPO composition failed" );
+ return rc;
+ }
+ LOGD("mp_headr_start_off_addr %x",
+ *mp_headr_start_off_addr);
+
+ current_offset = mp_headr_start_off_addr - mpo_info->output_buff.buf_vaddr;
+
+ endianess = READ_LONG(mpo_info->output_buff.buf_vaddr, current_offset);
+ LOGD("Endianess %d", endianess);
+
+ //Add offset to first ifd
+ current_offset += MP_ENDIAN_BYTES;
+
+ //Read the value to get MP Index IFD.
+ if (endianess == MPO_LITTLE_ENDIAN) {
+ offset_to_nxt_ifd = READ_LONG_LITTLE(mpo_info->output_buff.buf_vaddr,
+ current_offset);
+ } else {
+ offset_to_nxt_ifd = READ_LONG(mpo_info->output_buff.buf_vaddr,
+ current_offset);
+ }
+ LOGD("offset_to_nxt_ifd %d", offset_to_nxt_ifd);
+
+ current_offset = ((mp_headr_start_off_addr + offset_to_nxt_ifd) -
+ mpo_info->output_buff.buf_vaddr);
+ mp_index_ifd_offset = current_offset;
+ LOGD("mp_index_ifd_offset %d",
+ mp_index_ifd_offset);
+
+ //Traverse to MP Entry value
+ ifd_tag_count = READ_SHORT(mpo_info->output_buff.buf_vaddr, current_offset);
+ LOGD("Tag count in MP entry %d", ifd_tag_count);
+ current_offset += MP_INDEX_COUNT_BYTES;
+
+ /* Get MP Entry Value offset - Count * 12 (Each tag is 12 bytes)*/
+ current_offset += (ifd_tag_count * 12);
+ /*Add Offset to next IFD*/
+ current_offset += MP_INDEX_OFFSET_OF_NEXT_IFD_BYTES;
+
+ mp_entry_val_offset = current_offset;
+ LOGD("MP Entry value offset %d",
+ mp_entry_val_offset);
+
+ //Update image size for primary image
+ current_offset += MP_INDEX_ENTRY_INDIVIDUAL_IMAGE_ATTRIBUTE_BYTES;
+ if (endianess == MPO_LITTLE_ENDIAN) {
+ mm_jpeg_mpo_write_long_little_endian(mpo_info->output_buff.buf_vaddr,
+ current_offset, mpo_info->output_buff_size,
+ mpo_info->primary_image.buf_filled_len, &overflow_flag);
+ } else {
+ mm_jpeg_mpo_write_long(mpo_info->output_buff.buf_vaddr,
+ current_offset, mpo_info->output_buff_size,
+ mpo_info->primary_image.buf_filled_len, &overflow_flag);
+ }
+
+ aux_start_addr = mpo_info->output_buff.buf_vaddr +
+ mpo_info->primary_image.buf_filled_len;
+
+ for (i = 0; i < mpo_info->num_of_images - 1; i++) {
+ //Go to MP Entry val for each image
+ mp_entry_val_offset += MP_INDEX_ENTRY_VALUE_BYTES;
+ current_offset = mp_entry_val_offset;
+
+ //Update image size
+ current_offset += MP_INDEX_ENTRY_INDIVIDUAL_IMAGE_ATTRIBUTE_BYTES;
+ if (endianess == MPO_LITTLE_ENDIAN) {
+ mm_jpeg_mpo_write_long_little_endian(mpo_info->output_buff.buf_vaddr,
+ current_offset, mpo_info->output_buff_size,
+ mpo_info->aux_images[i].buf_filled_len, &overflow_flag);
+ } else {
+ mm_jpeg_mpo_write_long(mpo_info->output_buff.buf_vaddr,
+ current_offset, mpo_info->output_buff_size,
+ mpo_info->aux_images[i].buf_filled_len, &overflow_flag);
+ }
+ LOGD("aux[start_addr %x", *aux_start_addr);
+ //Update the offset
+ current_offset += MP_INDEX_ENTRY_INDIVIDUAL_IMAGE_SIZE_BYTES;
+ if (endianess == MPO_LITTLE_ENDIAN) {
+ mm_jpeg_mpo_write_long_little_endian(mpo_info->output_buff.buf_vaddr,
+ current_offset, mpo_info->output_buff_size,
+ aux_start_addr - mp_headr_start_off_addr, &overflow_flag);
+ } else {
+ mm_jpeg_mpo_write_long(mpo_info->output_buff.buf_vaddr,
+ current_offset, mpo_info->output_buff_size,
+ aux_start_addr - mp_headr_start_off_addr, &overflow_flag);
+ }
+ aux_start_addr += mpo_info->aux_images[i].buf_filled_len;
+ }
+ if (!overflow_flag) {
+ rc = 0;
+ }
+ return rc;
+}
+
+/** mm_jpeg_mpo_compose
+ *
+ * Arguments:
+ * @mpo_info: MPO Info
+ *
+ * Return:
+ * 0 - Success
+ * -1 - otherwise
+ *
+ * Description:
+ * Compose MPO image from multiple JPEG images
+ *
+ **/
+int mm_jpeg_mpo_compose(mm_jpeg_mpo_info_t *mpo_info)
+{
+ uint8_t *aux_write_offset = NULL;
+ int i = 0, rc = -1;
+
+ pthread_mutex_lock(&g_mpo_lock);
+
+ //Primary image needs to be copied to the o/p buffer if its not already
+ if (mpo_info->output_buff.buf_filled_len == 0) {
+ if (mpo_info->primary_image.buf_filled_len < mpo_info->output_buff_size) {
+ memcpy(mpo_info->output_buff.buf_vaddr, mpo_info->primary_image.buf_vaddr,
+ mpo_info->primary_image.buf_filled_len);
+ mpo_info->output_buff.buf_filled_len +=
+ mpo_info->primary_image.buf_filled_len;
+ } else {
+ LOGE("O/P buffer not large enough. MPO composition failed");
+ pthread_mutex_unlock(&g_mpo_lock);
+ return rc;
+ }
+ }
+ //Append each Aux image to the buffer
+ for (i = 0; i < mpo_info->num_of_images - 1; i++) {
+ if ((mpo_info->output_buff.buf_filled_len +
+ mpo_info->aux_images[i].buf_filled_len) <= mpo_info->output_buff_size) {
+ aux_write_offset = mpo_info->output_buff.buf_vaddr +
+ mpo_info->output_buff.buf_filled_len;
+ memcpy(aux_write_offset, mpo_info->aux_images[i].buf_vaddr,
+ mpo_info->aux_images[i].buf_filled_len);
+ mpo_info->output_buff.buf_filled_len +=
+ mpo_info->aux_images[i].buf_filled_len;
+ } else {
+ LOGE("O/P buffer not large enough. MPO composition failed");
+ pthread_mutex_unlock(&g_mpo_lock);
+ return rc;
+ }
+ }
+
+ rc = mm_jpeg_mpo_update_header(mpo_info);
+ pthread_mutex_unlock(&g_mpo_lock);
+
+ return rc;
+}
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_queue.c b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_queue.c
new file mode 100644
index 0000000..2aeb78f
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpeg_queue.c
@@ -0,0 +1,186 @@
+/* Copyright (c) 2012-2014, 2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// System dependencies
+#include <pthread.h>
+
+// JPEG dependencies
+#include "mm_jpeg_dbg.h"
+#include "mm_jpeg.h"
+
+int32_t mm_jpeg_queue_init(mm_jpeg_queue_t* queue)
+{
+ pthread_mutex_init(&queue->lock, NULL);
+ cam_list_init(&queue->head.list);
+ queue->size = 0;
+ return 0;
+}
+
+int32_t mm_jpeg_queue_enq(mm_jpeg_queue_t* queue, mm_jpeg_q_data_t data)
+{
+ mm_jpeg_q_node_t* node =
+ (mm_jpeg_q_node_t *)malloc(sizeof(mm_jpeg_q_node_t));
+ if (NULL == node) {
+ LOGE("No memory for mm_jpeg_q_node_t");
+ return -1;
+ }
+
+ memset(node, 0, sizeof(mm_jpeg_q_node_t));
+ node->data = data;
+
+ pthread_mutex_lock(&queue->lock);
+ cam_list_add_tail_node(&node->list, &queue->head.list);
+ queue->size++;
+ pthread_mutex_unlock(&queue->lock);
+
+ return 0;
+
+}
+
+int32_t mm_jpeg_queue_enq_head(mm_jpeg_queue_t* queue, mm_jpeg_q_data_t data)
+{
+ struct cam_list *head = NULL;
+ struct cam_list *pos = NULL;
+ mm_jpeg_q_node_t* node =
+ (mm_jpeg_q_node_t *)malloc(sizeof(mm_jpeg_q_node_t));
+ if (NULL == node) {
+ LOGE("No memory for mm_jpeg_q_node_t");
+ return -1;
+ }
+
+ memset(node, 0, sizeof(mm_jpeg_q_node_t));
+ node->data = data;
+
+ head = &queue->head.list;
+ pos = head->next;
+
+ pthread_mutex_lock(&queue->lock);
+ cam_list_insert_before_node(&node->list, pos);
+ queue->size++;
+ pthread_mutex_unlock(&queue->lock);
+
+ return 0;
+}
+
+mm_jpeg_q_data_t mm_jpeg_queue_deq(mm_jpeg_queue_t* queue)
+{
+ mm_jpeg_q_data_t data;
+ mm_jpeg_q_node_t* node = NULL;
+ struct cam_list *head = NULL;
+ struct cam_list *pos = NULL;
+
+ memset(&data, 0, sizeof(data));
+
+ pthread_mutex_lock(&queue->lock);
+ head = &queue->head.list;
+ pos = head->next;
+ if (pos != head) {
+ node = member_of(pos, mm_jpeg_q_node_t, list);
+ cam_list_del_node(&node->list);
+ queue->size--;
+ }
+ pthread_mutex_unlock(&queue->lock);
+
+ if (NULL != node) {
+ data = node->data;
+ free(node);
+ }
+
+ return data;
+}
+
+uint32_t mm_jpeg_queue_get_size(mm_jpeg_queue_t* queue)
+{
+ uint32_t size = 0;
+
+ pthread_mutex_lock(&queue->lock);
+ size = queue->size;
+ pthread_mutex_unlock(&queue->lock);
+
+ return size;
+
+}
+
+int32_t mm_jpeg_queue_deinit(mm_jpeg_queue_t* queue)
+{
+ mm_jpeg_queue_flush(queue);
+ pthread_mutex_destroy(&queue->lock);
+ return 0;
+}
+
+int32_t mm_jpeg_queue_flush(mm_jpeg_queue_t* queue)
+{
+ mm_jpeg_q_node_t* node = NULL;
+ struct cam_list *head = NULL;
+ struct cam_list *pos = NULL;
+
+ pthread_mutex_lock(&queue->lock);
+ head = &queue->head.list;
+ pos = head->next;
+
+ while(pos != head) {
+ node = member_of(pos, mm_jpeg_q_node_t, list);
+ cam_list_del_node(&node->list);
+ queue->size--;
+
+ /* for now we only assume there is no ptr inside data
+ * so we free data directly */
+ if (NULL != node->data.p) {
+ free(node->data.p);
+ }
+ free(node);
+ pos = pos->next;
+ }
+ queue->size = 0;
+ pthread_mutex_unlock(&queue->lock);
+ return 0;
+}
+
+mm_jpeg_q_data_t mm_jpeg_queue_peek(mm_jpeg_queue_t* queue)
+{
+ mm_jpeg_q_data_t data;
+ mm_jpeg_q_node_t* node = NULL;
+ struct cam_list *head = NULL;
+ struct cam_list *pos = NULL;
+
+ memset(&data, 0, sizeof(data));
+
+ pthread_mutex_lock(&queue->lock);
+ head = &queue->head.list;
+ pos = head->next;
+ if (pos != head) {
+ node = member_of(pos, mm_jpeg_q_node_t, list);
+ }
+ pthread_mutex_unlock(&queue->lock);
+
+ if (NULL != node) {
+ data = node->data;
+ }
+ return data;
+}
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec.c b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec.c
new file mode 100644
index 0000000..b4ee1dc
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec.c
@@ -0,0 +1,1185 @@
+/* Copyright (c) 2013-2014, 2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// System dependencies
+#include <pthread.h>
+
+// JPEG dependencies
+#include "mm_jpeg_dbg.h"
+#include "mm_jpeg_interface.h"
+#include "mm_jpeg.h"
+#include "mm_jpeg_inlines.h"
+
+OMX_ERRORTYPE mm_jpegdec_ebd(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_BUFFERHEADERTYPE *pBuffer);
+OMX_ERRORTYPE mm_jpegdec_fbd(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_BUFFERHEADERTYPE* pBuffer);
+OMX_ERRORTYPE mm_jpegdec_event_handler(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_EVENTTYPE eEvent,
+ OMX_U32 nData1,
+ OMX_U32 nData2,
+ OMX_PTR pEventData);
+
+
+/** mm_jpegdec_destroy_job
+ *
+ * Arguments:
+ * @p_session: Session obj
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Destroy the job based paramenters
+ *
+ **/
+static int32_t mm_jpegdec_destroy_job(mm_jpeg_job_session_t *p_session)
+{
+ int32_t rc = 0;
+
+ return rc;
+}
+
+/** mm_jpeg_job_done:
+ *
+ * Arguments:
+ * @p_session: decode session
+ *
+ * Return:
+ * OMX_ERRORTYPE
+ *
+ * Description:
+ * Finalize the job
+ *
+ **/
+static void mm_jpegdec_job_done(mm_jpeg_job_session_t *p_session)
+{
+ mm_jpeg_obj *my_obj = (mm_jpeg_obj *)p_session->jpeg_obj;
+ mm_jpeg_job_q_node_t *node = NULL;
+
+ /*Destroy job related params*/
+ mm_jpegdec_destroy_job(p_session);
+
+ /*remove the job*/
+ node = mm_jpeg_queue_remove_job_by_job_id(&my_obj->ongoing_job_q,
+ p_session->jobId);
+ if (node) {
+ free(node);
+ }
+ p_session->encoding = OMX_FALSE;
+
+ /* wake up jobMgr thread to work on new job if there is any */
+ cam_sem_post(&my_obj->job_mgr.job_sem);
+}
+
+
+/** mm_jpegdec_session_send_buffers:
+ *
+ * Arguments:
+ * @data: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Send the buffers to OMX layer
+ *
+ **/
+OMX_ERRORTYPE mm_jpegdec_session_send_buffers(void *data)
+{
+ uint32_t i = 0;
+ mm_jpeg_job_session_t* p_session = (mm_jpeg_job_session_t *)data;
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ QOMX_BUFFER_INFO lbuffer_info;
+ mm_jpeg_decode_params_t *p_params = &p_session->dec_params;
+
+ memset(&lbuffer_info, 0x0, sizeof(QOMX_BUFFER_INFO));
+ for (i = 0; i < p_params->num_src_bufs; i++) {
+ LOGD("Source buffer %d", i);
+ lbuffer_info.fd = (OMX_U32)p_params->src_main_buf[i].fd;
+ ret = OMX_UseBuffer(p_session->omx_handle, &(p_session->p_in_omx_buf[i]), 0,
+ &lbuffer_info, p_params->src_main_buf[i].buf_size,
+ p_params->src_main_buf[i].buf_vaddr);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+
+ LOGD("Exit");
+ return ret;
+}
+
+/** mm_jpeg_session_free_buffers:
+ *
+ * Arguments:
+ * @data: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Free the buffers from OMX layer
+ *
+ **/
+OMX_ERRORTYPE mm_jpegdec_session_free_buffers(void *data)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ uint32_t i = 0;
+ mm_jpeg_job_session_t* p_session = (mm_jpeg_job_session_t *)data;
+ mm_jpeg_decode_params_t *p_params = &p_session->dec_params;
+
+ for (i = 0; i < p_params->num_src_bufs; i++) {
+ LOGD("Source buffer %d", i);
+ ret = OMX_FreeBuffer(p_session->omx_handle, 0, p_session->p_in_omx_buf[i]);
+ if (ret) {
+ LOGE("Error %d", ret);
+ return ret;
+ }
+ }
+
+ for (i = 0; i < p_params->num_dst_bufs; i++) {
+ LOGD("Dest buffer %d", i);
+ ret = OMX_FreeBuffer(p_session->omx_handle, 1, p_session->p_out_omx_buf[i]);
+ if (ret) {
+ LOGE("Error");
+ return ret;
+ }
+ }
+ LOGD("Exit");
+ return ret;
+}
+
+/** mm_jpegdec_session_create:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error types
+ *
+ * Description:
+ * Create a jpeg encode session
+ *
+ **/
+OMX_ERRORTYPE mm_jpegdec_session_create(mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+
+ pthread_mutex_init(&p_session->lock, NULL);
+ pthread_cond_init(&p_session->cond, NULL);
+ cirq_reset(&p_session->cb_q);
+ p_session->state_change_pending = OMX_FALSE;
+ p_session->abort_state = MM_JPEG_ABORT_NONE;
+ p_session->error_flag = OMX_ErrorNone;
+ p_session->ebd_count = 0;
+ p_session->fbd_count = 0;
+ p_session->encode_pid = -1;
+ p_session->config = OMX_FALSE;
+
+ p_session->omx_callbacks.EmptyBufferDone = mm_jpegdec_ebd;
+ p_session->omx_callbacks.FillBufferDone = mm_jpegdec_fbd;
+ p_session->omx_callbacks.EventHandler = mm_jpegdec_event_handler;
+ p_session->exif_count_local = 0;
+
+ rc = OMX_GetHandle(&p_session->omx_handle,
+ "OMX.qcom.image.jpeg.decoder",
+ (void *)p_session,
+ &p_session->omx_callbacks);
+
+ if (OMX_ErrorNone != rc) {
+ LOGE("OMX_GetHandle failed (%d)", rc);
+ return rc;
+ }
+ return rc;
+}
+
+/** mm_jpegdec_session_destroy:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * none
+ *
+ * Description:
+ * Destroy a jpeg encode session
+ *
+ **/
+void mm_jpegdec_session_destroy(mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+
+ LOGD("E");
+ if (NULL == p_session->omx_handle) {
+ LOGE("invalid handle");
+ return;
+ }
+
+ rc = mm_jpeg_session_change_state(p_session, OMX_StateIdle, NULL);
+ if (rc) {
+ LOGE("Error");
+ }
+
+ rc = mm_jpeg_session_change_state(p_session, OMX_StateLoaded,
+ mm_jpegdec_session_free_buffers);
+ if (rc) {
+ LOGE("Error");
+ }
+
+ rc = OMX_FreeHandle(p_session->omx_handle);
+ if (0 != rc) {
+ LOGE("OMX_FreeHandle failed (%d)", rc);
+ }
+ p_session->omx_handle = NULL;
+
+
+ pthread_mutex_destroy(&p_session->lock);
+ pthread_cond_destroy(&p_session->cond);
+ LOGD("X");
+}
+
+/** mm_jpeg_session_config_port:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure OMX ports
+ *
+ **/
+OMX_ERRORTYPE mm_jpegdec_session_config_ports(mm_jpeg_job_session_t* p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_decode_params_t *p_params = &p_session->dec_params;
+ mm_jpeg_decode_job_t *p_jobparams = &p_session->decode_job;
+
+ mm_jpeg_buf_t *p_src_buf =
+ &p_params->src_main_buf[p_jobparams->src_index];
+
+ p_session->inputPort.nPortIndex = 0;
+ p_session->outputPort.nPortIndex = 1;
+
+
+ ret = OMX_GetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->inputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ ret = OMX_GetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->outputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ p_session->inputPort.format.image.nFrameWidth =
+ (OMX_U32)p_jobparams->main_dim.src_dim.width;
+ p_session->inputPort.format.image.nFrameHeight =
+ (OMX_U32)p_jobparams->main_dim.src_dim.height;
+ p_session->inputPort.format.image.nStride =
+ p_src_buf->offset.mp[0].stride;
+ p_session->inputPort.format.image.nSliceHeight =
+ (OMX_U32)p_src_buf->offset.mp[0].scanline;
+ p_session->inputPort.format.image.eColorFormat =
+ map_jpeg_format(p_params->color_format);
+ p_session->inputPort.nBufferSize =
+ p_params->src_main_buf[p_jobparams->src_index].buf_size;
+ p_session->inputPort.nBufferCountActual = (OMX_U32)p_params->num_src_bufs;
+ ret = OMX_SetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->inputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ return ret;
+}
+
+
+/** mm_jpegdec_session_config_main:
+ *
+ * Arguments:
+ * @p_session: job session
+ *
+ * Return:
+ * OMX error values
+ *
+ * Description:
+ * Configure main image
+ *
+ **/
+OMX_ERRORTYPE mm_jpegdec_session_config_main(mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE rc = OMX_ErrorNone;
+
+ /* config port */
+ LOGD("config port");
+ rc = mm_jpegdec_session_config_ports(p_session);
+ if (OMX_ErrorNone != rc) {
+ LOGE("config port failed");
+ return rc;
+ }
+
+
+ /* TODO: config crop */
+
+ return rc;
+}
+
+/** mm_jpeg_session_configure:
+ *
+ * Arguments:
+ * @data: encode session
+ *
+ * Return:
+ * none
+ *
+ * Description:
+ * Configure the session
+ *
+ **/
+static OMX_ERRORTYPE mm_jpegdec_session_configure(mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+
+ LOGD("E ");
+
+ MM_JPEG_CHK_ABORT(p_session, ret, error);
+
+ /* config main img */
+ ret = mm_jpegdec_session_config_main(p_session);
+ if (OMX_ErrorNone != ret) {
+ LOGE("config main img failed");
+ goto error;
+ }
+
+ /* TODO: common config (if needed) */
+
+ ret = mm_jpeg_session_change_state(p_session, OMX_StateIdle,
+ mm_jpegdec_session_send_buffers);
+ if (ret) {
+ LOGE("change state to idle failed %d", ret);
+ goto error;
+ }
+
+ ret = mm_jpeg_session_change_state(p_session, OMX_StateExecuting,
+ NULL);
+ if (ret) {
+ LOGE("change state to executing failed %d", ret);
+ goto error;
+ }
+
+error:
+ LOGD("X ret %d", ret);
+ return ret;
+}
+
+static OMX_ERRORTYPE mm_jpeg_session_port_enable(
+ mm_jpeg_job_session_t *p_session,
+ OMX_U32 nPortIndex,
+ OMX_BOOL wait)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ OMX_EVENTTYPE lEvent;
+
+ pthread_mutex_lock(&p_session->lock);
+ p_session->event_pending = OMX_TRUE;
+ pthread_mutex_unlock(&p_session->lock);
+
+ ret = OMX_SendCommand(p_session->omx_handle, OMX_CommandPortEnable,
+ nPortIndex, NULL);
+
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ if (wait == OMX_TRUE) {
+ // Wait for cmd complete
+ pthread_mutex_lock(&p_session->lock);
+ if (p_session->event_pending == OMX_TRUE) {
+ LOGD("before wait");
+ pthread_cond_wait(&p_session->cond, &p_session->lock);
+ lEvent = p_session->omxEvent;
+ LOGD("after wait");
+ }
+ lEvent = p_session->omxEvent;
+ pthread_mutex_unlock(&p_session->lock);
+
+ if (lEvent != OMX_EventCmdComplete) {
+ LOGD("Unexpected event %d",lEvent);
+ return OMX_ErrorUndefined;
+ }
+ }
+ return OMX_ErrorNone;
+}
+
+static OMX_ERRORTYPE mm_jpeg_session_port_disable(
+ mm_jpeg_job_session_t *p_session,
+ OMX_U32 nPortIndex,
+ OMX_BOOL wait)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ OMX_EVENTTYPE lEvent;
+
+ pthread_mutex_lock(&p_session->lock);
+ p_session->event_pending = OMX_TRUE;
+ pthread_mutex_unlock(&p_session->lock);
+
+ ret = OMX_SendCommand(p_session->omx_handle, OMX_CommandPortDisable,
+ nPortIndex, NULL);
+
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+ if (wait == OMX_TRUE) {
+ // Wait for cmd complete
+ pthread_mutex_lock(&p_session->lock);
+ if (p_session->event_pending == OMX_TRUE) {
+ LOGD("before wait");
+ pthread_cond_wait(&p_session->cond, &p_session->lock);
+
+ LOGD("after wait");
+ }
+ lEvent = p_session->omxEvent;
+ pthread_mutex_unlock(&p_session->lock);
+
+ if (lEvent != OMX_EventCmdComplete) {
+ LOGD("Unexpected event %d",lEvent);
+ return OMX_ErrorUndefined;
+ }
+ }
+ return OMX_ErrorNone;
+}
+
+
+/** mm_jpegdec_session_decode:
+ *
+ * Arguments:
+ * @p_session: encode session
+ *
+ * Return:
+ * OMX_ERRORTYPE
+ *
+ * Description:
+ * Start the encoding
+ *
+ **/
+static OMX_ERRORTYPE mm_jpegdec_session_decode(mm_jpeg_job_session_t *p_session)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_decode_params_t *p_params = &p_session->dec_params;
+ mm_jpeg_decode_job_t *p_jobparams = &p_session->decode_job;
+ OMX_EVENTTYPE lEvent;
+ uint32_t i;
+ QOMX_BUFFER_INFO lbuffer_info;
+
+ pthread_mutex_lock(&p_session->lock);
+ p_session->abort_state = MM_JPEG_ABORT_NONE;
+ p_session->encoding = OMX_FALSE;
+ pthread_mutex_unlock(&p_session->lock);
+
+ if (OMX_FALSE == p_session->config) {
+ ret = mm_jpegdec_session_configure(p_session);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+ p_session->config = OMX_TRUE;
+ }
+
+ pthread_mutex_lock(&p_session->lock);
+ p_session->encoding = OMX_TRUE;
+ pthread_mutex_unlock(&p_session->lock);
+
+ MM_JPEG_CHK_ABORT(p_session, ret, error);
+
+ p_session->event_pending = OMX_TRUE;
+
+ ret = OMX_EmptyThisBuffer(p_session->omx_handle,
+ p_session->p_in_omx_buf[p_jobparams->src_index]);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+
+ // Wait for port settings changed
+ pthread_mutex_lock(&p_session->lock);
+ if (p_session->event_pending == OMX_TRUE) {
+ LOGD("before wait");
+ pthread_cond_wait(&p_session->cond, &p_session->lock);
+ }
+ lEvent = p_session->omxEvent;
+ LOGD("after wait");
+ pthread_mutex_unlock(&p_session->lock);
+
+ if (lEvent != OMX_EventPortSettingsChanged) {
+ LOGD("Unexpected event %d",lEvent);
+ goto error;
+ }
+
+ // Disable output port (wait)
+ mm_jpeg_session_port_disable(p_session,
+ p_session->outputPort.nPortIndex,
+ OMX_TRUE);
+
+ // Get port definition
+ ret = OMX_GetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->outputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ // Set port definition
+ p_session->outputPort.format.image.nFrameWidth =
+ (OMX_U32)p_jobparams->main_dim.dst_dim.width;
+ p_session->outputPort.format.image.nFrameHeight =
+ (OMX_U32)p_jobparams->main_dim.dst_dim.height;
+ p_session->outputPort.format.image.eColorFormat =
+ map_jpeg_format(p_params->color_format);
+
+ p_session->outputPort.nBufferSize =
+ p_params->dest_buf[p_jobparams->dst_index].buf_size;
+ p_session->outputPort.nBufferCountActual = (OMX_U32)p_params->num_dst_bufs;
+
+ p_session->outputPort.format.image.nSliceHeight =
+ (OMX_U32)
+ p_params->dest_buf[p_jobparams->dst_index].offset.mp[0].scanline;
+ p_session->outputPort.format.image.nStride =
+ p_params->dest_buf[p_jobparams->dst_index].offset.mp[0].stride;
+
+ ret = OMX_SetParameter(p_session->omx_handle, OMX_IndexParamPortDefinition,
+ &p_session->outputPort);
+ if (ret) {
+ LOGE("failed");
+ return ret;
+ }
+
+ // Enable port (no wait)
+ mm_jpeg_session_port_enable(p_session,
+ p_session->outputPort.nPortIndex,
+ OMX_FALSE);
+
+ memset(&lbuffer_info, 0x0, sizeof(QOMX_BUFFER_INFO));
+ // Use buffers
+ for (i = 0; i < p_params->num_dst_bufs; i++) {
+ lbuffer_info.fd = (OMX_U32)p_params->dest_buf[i].fd;
+ LOGD("Dest buffer %d", (unsigned int)i);
+ ret = OMX_UseBuffer(p_session->omx_handle, &(p_session->p_out_omx_buf[i]),
+ 1, &lbuffer_info, p_params->dest_buf[i].buf_size,
+ p_params->dest_buf[i].buf_vaddr);
+ if (ret) {
+ LOGE("Error");
+ return ret;
+ }
+ }
+
+ // Wait for port enable completion
+ pthread_mutex_lock(&p_session->lock);
+ if (p_session->event_pending == OMX_TRUE) {
+ LOGD("before wait");
+ pthread_cond_wait(&p_session->cond, &p_session->lock);
+ lEvent = p_session->omxEvent;
+ LOGD("after wait");
+ }
+ lEvent = p_session->omxEvent;
+ pthread_mutex_unlock(&p_session->lock);
+
+ if (lEvent != OMX_EventCmdComplete) {
+ LOGD("Unexpected event %d",lEvent);
+ goto error;
+ }
+
+ ret = OMX_FillThisBuffer(p_session->omx_handle,
+ p_session->p_out_omx_buf[p_jobparams->dst_index]);
+ if (ret) {
+ LOGE("Error");
+ goto error;
+ }
+
+ MM_JPEG_CHK_ABORT(p_session, ret, error);
+
+error:
+
+ LOGD("X ");
+ return ret;
+}
+
+/** mm_jpegdec_process_decoding_job:
+ *
+ * Arguments:
+ * @my_obj: jpeg client
+ * @job_node: job node
+ *
+ * Return:
+ * 0 for success -1 otherwise
+ *
+ * Description:
+ * Start the encoding job
+ *
+ **/
+int32_t mm_jpegdec_process_decoding_job(mm_jpeg_obj *my_obj, mm_jpeg_job_q_node_t* job_node)
+{
+ mm_jpeg_q_data_t qdata;
+ int32_t rc = 0;
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_job_session_t *p_session = NULL;
+
+ /* check if valid session */
+ p_session = mm_jpeg_get_session(my_obj, job_node->dec_info.job_id);
+ if (NULL == p_session) {
+ LOGE("invalid job id %x",
+ job_node->dec_info.job_id);
+ return -1;
+ }
+
+ /* sent encode cmd to OMX, queue job into ongoing queue */
+ qdata.p = job_node;
+ rc = mm_jpeg_queue_enq(&my_obj->ongoing_job_q, qdata);
+ if (rc) {
+ LOGE("jpeg enqueue failed %d", ret);
+ goto error;
+ }
+
+ p_session->decode_job = job_node->dec_info.decode_job;
+ p_session->jobId = job_node->dec_info.job_id;
+ ret = mm_jpegdec_session_decode(p_session);
+ if (ret) {
+ LOGE("encode session failed");
+ goto error;
+ }
+
+ LOGD("Success X ");
+ return rc;
+
+error:
+
+ if ((OMX_ErrorNone != ret) &&
+ (NULL != p_session->dec_params.jpeg_cb)) {
+ p_session->job_status = JPEG_JOB_STATUS_ERROR;
+ LOGD("send jpeg error callback %d",
+ p_session->job_status);
+ p_session->dec_params.jpeg_cb(p_session->job_status,
+ p_session->client_hdl,
+ p_session->jobId,
+ NULL,
+ p_session->dec_params.userdata);
+ }
+
+ /*remove the job*/
+ mm_jpegdec_job_done(p_session);
+ LOGD("Error X ");
+
+ return rc;
+}
+
+/** mm_jpeg_start_decode_job:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_hdl: client handle
+ * @job: pointer to encode job
+ * @jobId: job id
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Start the encoding job
+ *
+ **/
+int32_t mm_jpegdec_start_decode_job(mm_jpeg_obj *my_obj,
+ mm_jpeg_job_t *job,
+ uint32_t *job_id)
+{
+ mm_jpeg_q_data_t qdata;
+ int32_t rc = -1;
+ uint8_t session_idx = 0;
+ uint8_t client_idx = 0;
+ mm_jpeg_job_q_node_t* node = NULL;
+ mm_jpeg_job_session_t *p_session = NULL;
+ mm_jpeg_decode_job_t *p_jobparams = &job->decode_job;
+
+ *job_id = 0;
+
+ /* check if valid session */
+ session_idx = GET_SESSION_IDX(p_jobparams->session_id);
+ client_idx = GET_CLIENT_IDX(p_jobparams->session_id);
+ LOGD("session_idx %d client idx %d",
+ session_idx, client_idx);
+
+ if ((session_idx >= MM_JPEG_MAX_SESSION) ||
+ (client_idx >= MAX_JPEG_CLIENT_NUM)) {
+ LOGE("invalid session id %x",
+ job->decode_job.session_id);
+ return rc;
+ }
+
+ p_session = &my_obj->clnt_mgr[client_idx].session[session_idx];
+ if (OMX_FALSE == p_session->active) {
+ LOGE("session not active %x",
+ job->decode_job.session_id);
+ return rc;
+ }
+
+ if ((p_jobparams->src_index >= (int32_t)p_session->dec_params.num_src_bufs) ||
+ (p_jobparams->dst_index >= (int32_t)p_session->dec_params.num_dst_bufs)) {
+ LOGE("invalid buffer indices");
+ return rc;
+ }
+
+ /* enqueue new job into todo job queue */
+ node = (mm_jpeg_job_q_node_t *)malloc(sizeof(mm_jpeg_job_q_node_t));
+ if (NULL == node) {
+ LOGE("No memory for mm_jpeg_job_q_node_t");
+ return -1;
+ }
+
+ *job_id = job->decode_job.session_id |
+ ((p_session->job_hist++ % JOB_HIST_MAX) << 16);
+
+ memset(node, 0, sizeof(mm_jpeg_job_q_node_t));
+ node->dec_info.decode_job = job->decode_job;
+ node->dec_info.job_id = *job_id;
+ node->dec_info.client_handle = p_session->client_hdl;
+ node->type = MM_JPEG_CMD_TYPE_DECODE_JOB;
+
+ qdata.p = node;
+ rc = mm_jpeg_queue_enq(&my_obj->job_mgr.job_queue, qdata);
+ if (0 == rc) {
+ cam_sem_post(&my_obj->job_mgr.job_sem);
+ }
+
+ return rc;
+}
+
+/** mm_jpegdec_create_session:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_hdl: client handle
+ * @p_params: pointer to encode params
+ * @p_session_id: session id
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Start the encoding session
+ *
+ **/
+int32_t mm_jpegdec_create_session(mm_jpeg_obj *my_obj,
+ uint32_t client_hdl,
+ mm_jpeg_decode_params_t *p_params,
+ uint32_t* p_session_id)
+{
+ int32_t rc = 0;
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ uint8_t clnt_idx = 0;
+ int session_idx = -1;
+ mm_jpeg_job_session_t *p_session = NULL;
+ *p_session_id = 0;
+
+ /* validate the parameters */
+ if ((p_params->num_src_bufs > MM_JPEG_MAX_BUF)
+ || (p_params->num_dst_bufs > MM_JPEG_MAX_BUF)) {
+ LOGE("invalid num buffers");
+ return rc;
+ }
+
+ /* check if valid client */
+ clnt_idx = mm_jpeg_util_get_index_by_handler(client_hdl);
+ if (clnt_idx >= MAX_JPEG_CLIENT_NUM) {
+ LOGE("invalid client with handler (%d)", client_hdl);
+ return rc;
+ }
+
+ session_idx = mm_jpeg_get_new_session_idx(my_obj, clnt_idx, &p_session);
+ if (session_idx < 0) {
+ LOGE("invalid session id (%d)", session_idx);
+ return rc;
+ }
+
+ ret = mm_jpegdec_session_create(p_session);
+ if (OMX_ErrorNone != ret) {
+ p_session->active = OMX_FALSE;
+ LOGE("jpeg session create failed");
+ return rc;
+ }
+
+ *p_session_id = (JOB_ID_MAGICVAL << 24) |
+ ((unsigned)session_idx << 8) | clnt_idx;
+
+ /*copy the params*/
+ p_session->dec_params = *p_params;
+ p_session->client_hdl = client_hdl;
+ p_session->sessionId = *p_session_id;
+ p_session->jpeg_obj = (void*)my_obj; /* save a ptr to jpeg_obj */
+ LOGD("session id %x", *p_session_id);
+
+ return rc;
+}
+
+/** mm_jpegdec_destroy_session:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @session_id: session index
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Destroy the encoding session
+ *
+ **/
+int32_t mm_jpegdec_destroy_session(mm_jpeg_obj *my_obj,
+ mm_jpeg_job_session_t *p_session)
+{
+ int32_t rc = 0;
+ mm_jpeg_job_q_node_t *node = NULL;
+
+ if (NULL == p_session) {
+ LOGE("invalid session");
+ return rc;
+ }
+ uint32_t session_id = p_session->sessionId;
+ pthread_mutex_lock(&my_obj->job_lock);
+
+ /* abort job if in todo queue */
+ LOGD("abort todo jobs");
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->job_mgr.job_queue, session_id);
+ while (NULL != node) {
+ free(node);
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->job_mgr.job_queue, session_id);
+ }
+
+ /* abort job if in ongoing queue */
+ LOGD("abort ongoing jobs");
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->ongoing_job_q, session_id);
+ while (NULL != node) {
+ free(node);
+ node = mm_jpeg_queue_remove_job_by_session_id(&my_obj->ongoing_job_q, session_id);
+ }
+
+ /* abort the current session */
+ mm_jpeg_session_abort(p_session);
+ mm_jpegdec_session_destroy(p_session);
+ mm_jpeg_remove_session_idx(my_obj, session_id);
+ pthread_mutex_unlock(&my_obj->job_lock);
+
+ /* wake up jobMgr thread to work on new job if there is any */
+ cam_sem_post(&my_obj->job_mgr.job_sem);
+ LOGD("X");
+
+ return rc;
+}
+
+/** mm_jpegdec_destroy_session_by_id:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @session_id: session index
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Destroy the encoding session
+ *
+ **/
+int32_t mm_jpegdec_destroy_session_by_id(mm_jpeg_obj *my_obj, uint32_t session_id)
+{
+ int32_t rc = 0;
+ mm_jpeg_job_session_t *p_session = mm_jpeg_get_session(my_obj, session_id);
+
+ if (NULL == p_session) {
+ LOGE("session is not valid");
+ return rc;
+ }
+
+ return mm_jpegdec_destroy_session(my_obj, p_session);
+}
+
+
+
+OMX_ERRORTYPE mm_jpegdec_ebd(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_BUFFERHEADERTYPE *pBuffer)
+{
+ mm_jpeg_job_session_t *p_session = (mm_jpeg_job_session_t *) pAppData;
+
+ LOGD("count %d ", p_session->ebd_count);
+ pthread_mutex_lock(&p_session->lock);
+ p_session->ebd_count++;
+ pthread_mutex_unlock(&p_session->lock);
+ return 0;
+}
+
+OMX_ERRORTYPE mm_jpegdec_fbd(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_BUFFERHEADERTYPE *pBuffer)
+{
+ OMX_ERRORTYPE ret = OMX_ErrorNone;
+ mm_jpeg_job_session_t *p_session = (mm_jpeg_job_session_t *) pAppData;
+ mm_jpeg_output_t output_buf;
+
+ LOGD("count %d ", p_session->fbd_count);
+
+ pthread_mutex_lock(&p_session->lock);
+
+ if (MM_JPEG_ABORT_NONE != p_session->abort_state) {
+ pthread_mutex_unlock(&p_session->lock);
+ return ret;
+ }
+
+ p_session->fbd_count++;
+ if (NULL != p_session->dec_params.jpeg_cb) {
+ p_session->job_status = JPEG_JOB_STATUS_DONE;
+ output_buf.buf_filled_len = (uint32_t)pBuffer->nFilledLen;
+ output_buf.buf_vaddr = pBuffer->pBuffer;
+ output_buf.fd = -1;
+ LOGD("send jpeg callback %d",
+ p_session->job_status);
+ p_session->dec_params.jpeg_cb(p_session->job_status,
+ p_session->client_hdl,
+ p_session->jobId,
+ &output_buf,
+ p_session->dec_params.userdata);
+
+ /* remove from ready queue */
+ mm_jpegdec_job_done(p_session);
+ }
+ pthread_mutex_unlock(&p_session->lock);
+ LOGD("Exit");
+
+ return ret;
+}
+
+OMX_ERRORTYPE mm_jpegdec_event_handler(OMX_HANDLETYPE hComponent,
+ OMX_PTR pAppData,
+ OMX_EVENTTYPE eEvent,
+ OMX_U32 nData1,
+ OMX_U32 nData2,
+ OMX_PTR pEventData)
+{
+ mm_jpeg_job_session_t *p_session = (mm_jpeg_job_session_t *) pAppData;
+
+ LOGD("%d %d %d state %d", eEvent, (int)nData1,
+ (int)nData2, p_session->abort_state);
+
+ LOGD("AppData=%p ", pAppData);
+
+ pthread_mutex_lock(&p_session->lock);
+ p_session->omxEvent = eEvent;
+ if (MM_JPEG_ABORT_INIT == p_session->abort_state) {
+ p_session->abort_state = MM_JPEG_ABORT_DONE;
+ pthread_cond_signal(&p_session->cond);
+ pthread_mutex_unlock(&p_session->lock);
+ return OMX_ErrorNone;
+ }
+
+ if (eEvent == OMX_EventError) {
+ if (p_session->encoding == OMX_TRUE) {
+ LOGD("Error during encoding");
+
+ /* send jpeg callback */
+ if (NULL != p_session->dec_params.jpeg_cb) {
+ p_session->job_status = JPEG_JOB_STATUS_ERROR;
+ LOGD("send jpeg error callback %d",
+ p_session->job_status);
+ p_session->dec_params.jpeg_cb(p_session->job_status,
+ p_session->client_hdl,
+ p_session->jobId,
+ NULL,
+ p_session->dec_params.userdata);
+ }
+
+ /* remove from ready queue */
+ mm_jpegdec_job_done(p_session);
+ }
+ pthread_cond_signal(&p_session->cond);
+ } else if (eEvent == OMX_EventCmdComplete) {
+ p_session->state_change_pending = OMX_FALSE;
+ p_session->event_pending = OMX_FALSE;
+ pthread_cond_signal(&p_session->cond);
+ } else if (eEvent == OMX_EventPortSettingsChanged) {
+ p_session->event_pending = OMX_FALSE;
+ pthread_cond_signal(&p_session->cond);
+ }
+
+ pthread_mutex_unlock(&p_session->lock);
+ LOGD("Exit");
+ return OMX_ErrorNone;
+}
+
+/** mm_jpegdec_abort_job:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ * @client_hdl: client handle
+ * @jobId: job id
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Abort the encoding session
+ *
+ **/
+int32_t mm_jpegdec_abort_job(mm_jpeg_obj *my_obj,
+ uint32_t jobId)
+{
+ int32_t rc = -1;
+ mm_jpeg_job_q_node_t *node = NULL;
+ mm_jpeg_job_session_t *p_session = NULL;
+
+ LOGD("Enter");
+ pthread_mutex_lock(&my_obj->job_lock);
+
+ /* abort job if in todo queue */
+ node = mm_jpeg_queue_remove_job_by_job_id(&my_obj->job_mgr.job_queue, jobId);
+ if (NULL != node) {
+ free(node);
+ goto abort_done;
+ }
+
+ /* abort job if in ongoing queue */
+ node = mm_jpeg_queue_remove_job_by_job_id(&my_obj->ongoing_job_q, jobId);
+ if (NULL != node) {
+ /* find job that is OMX ongoing, ask OMX to abort the job */
+ p_session = mm_jpeg_get_session(my_obj, node->dec_info.job_id);
+ if (p_session) {
+ mm_jpeg_session_abort(p_session);
+ } else {
+ LOGE("Invalid job id 0x%x",
+ node->dec_info.job_id);
+ }
+ free(node);
+ goto abort_done;
+ }
+
+abort_done:
+ pthread_mutex_unlock(&my_obj->job_lock);
+
+ return rc;
+}
+/** mm_jpegdec_init:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Initializes the jpeg client
+ *
+ **/
+int32_t mm_jpegdec_init(mm_jpeg_obj *my_obj)
+{
+ int32_t rc = 0;
+
+ /* init locks */
+ pthread_mutex_init(&my_obj->job_lock, NULL);
+
+ /* init ongoing job queue */
+ rc = mm_jpeg_queue_init(&my_obj->ongoing_job_q);
+ if (0 != rc) {
+ LOGE("Error");
+ return -1;
+ }
+
+ /* init job semaphore and launch jobmgr thread */
+ LOGD("Launch jobmgr thread rc %d", rc);
+ rc = mm_jpeg_jobmgr_thread_launch(my_obj);
+ if (0 != rc) {
+ LOGE("Error");
+ return -1;
+ }
+
+ /* load OMX */
+ if (OMX_ErrorNone != OMX_Init()) {
+ /* roll back in error case */
+ LOGE("OMX_Init failed (%d)", rc);
+ mm_jpeg_jobmgr_thread_release(my_obj);
+ mm_jpeg_queue_deinit(&my_obj->ongoing_job_q);
+ pthread_mutex_destroy(&my_obj->job_lock);
+ }
+
+ return rc;
+}
+
+/** mm_jpegdec_deinit:
+ *
+ * Arguments:
+ * @my_obj: jpeg object
+ *
+ * Return:
+ * 0 for success else failure
+ *
+ * Description:
+ * Deinits the jpeg client
+ *
+ **/
+int32_t mm_jpegdec_deinit(mm_jpeg_obj *my_obj)
+{
+ int32_t rc = 0;
+
+ /* release jobmgr thread */
+ rc = mm_jpeg_jobmgr_thread_release(my_obj);
+ if (0 != rc) {
+ LOGE("Error");
+ }
+
+ /* unload OMX engine */
+ OMX_Deinit();
+
+ /* deinit ongoing job and cb queue */
+ rc = mm_jpeg_queue_deinit(&my_obj->ongoing_job_q);
+ if (0 != rc) {
+ LOGE("Error");
+ }
+
+ /* destroy locks */
+ pthread_mutex_destroy(&my_obj->job_lock);
+
+ return rc;
+}
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec_interface.c b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec_interface.c
new file mode 100644
index 0000000..df6656b
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/src/mm_jpegdec_interface.c
@@ -0,0 +1,301 @@
+/* Copyright (c) 2013-2014, 2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// System dependencies
+#include <pthread.h>
+
+// JPEG dependencies
+#include "mm_jpeg_dbg.h"
+#include "mm_jpeg_interface.h"
+#include "mm_jpeg.h"
+
+static pthread_mutex_t g_dec_intf_lock = PTHREAD_MUTEX_INITIALIZER;
+
+static mm_jpeg_obj* g_jpegdec_obj = NULL;
+
+/** mm_jpeg_intf_start_job:
+ *
+ * Arguments:
+ * @client_hdl: client handle
+ * @job: jpeg job object
+ * @jobId: job id
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * start the jpeg job
+ *
+ **/
+static int32_t mm_jpegdec_intf_start_job(mm_jpeg_job_t* job, uint32_t* job_id)
+{
+ int32_t rc = -1;
+
+ if (NULL == job ||
+ NULL == job_id) {
+ LOGE("invalid parameters for job or jobId");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_dec_intf_lock);
+ if (NULL == g_jpegdec_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+ }
+ rc = mm_jpegdec_start_decode_job(g_jpegdec_obj, job, job_id);
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+}
+
+/** mm_jpeg_intf_create_session:
+ *
+ * Arguments:
+ * @client_hdl: client handle
+ * @p_params: encode parameters
+ * @p_session_id: session id
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Create new jpeg session
+ *
+ **/
+static int32_t mm_jpegdec_intf_create_session(uint32_t client_hdl,
+ mm_jpeg_decode_params_t *p_params,
+ uint32_t *p_session_id)
+{
+ int32_t rc = -1;
+
+ if (0 == client_hdl || NULL == p_params || NULL == p_session_id) {
+ LOGE("invalid client_hdl or jobId");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_dec_intf_lock);
+ if (NULL == g_jpegdec_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+ }
+
+ rc = mm_jpegdec_create_session(g_jpegdec_obj, client_hdl, p_params, p_session_id);
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+}
+
+/** mm_jpeg_intf_destroy_session:
+ *
+ * Arguments:
+ * @session_id: session id
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Destroy jpeg session
+ *
+ **/
+static int32_t mm_jpegdec_intf_destroy_session(uint32_t session_id)
+{
+ int32_t rc = -1;
+
+ if (0 == session_id) {
+ LOGE("invalid client_hdl or jobId");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_dec_intf_lock);
+ if (NULL == g_jpegdec_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+ }
+
+ rc = mm_jpegdec_destroy_session_by_id(g_jpegdec_obj, session_id);
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+}
+
+/** mm_jpegdec_intf_abort_job:
+ *
+ * Arguments:
+ * @jobId: job id
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Abort the jpeg job
+ *
+ **/
+static int32_t mm_jpegdec_intf_abort_job(uint32_t job_id)
+{
+ int32_t rc = -1;
+
+ if (0 == job_id) {
+ LOGE("invalid jobId");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_dec_intf_lock);
+ if (NULL == g_jpegdec_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+ }
+
+ rc = mm_jpegdec_abort_job(g_jpegdec_obj, job_id);
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+}
+
+/** mm_jpeg_intf_close:
+ *
+ * Arguments:
+ * @client_hdl: client handle
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Close the jpeg job
+ *
+ **/
+static int32_t mm_jpegdec_intf_close(uint32_t client_hdl)
+{
+ int32_t rc = -1;
+
+ if (0 == client_hdl) {
+ LOGE("invalid client_hdl");
+ return rc;
+ }
+
+ pthread_mutex_lock(&g_dec_intf_lock);
+ if (NULL == g_jpegdec_obj) {
+ /* mm_jpeg obj not exists, return error */
+ LOGE("mm_jpeg is not opened yet");
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+ }
+
+ rc = mm_jpeg_close(g_jpegdec_obj, client_hdl);
+ g_jpegdec_obj->num_clients--;
+ if(0 == rc) {
+ if (0 == g_jpegdec_obj->num_clients) {
+ /* No client, close jpeg internally */
+ rc = mm_jpegdec_deinit(g_jpegdec_obj);
+ free(g_jpegdec_obj);
+ g_jpegdec_obj = NULL;
+ }
+ }
+
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return rc;
+}
+
+
+
+/** jpegdec_open:
+ *
+ * Arguments:
+ * @ops: ops table pointer
+ *
+ * Return:
+ * 0 failure, success otherwise
+ *
+ * Description:
+ * Open a jpeg client
+ *
+ **/
+uint32_t jpegdec_open(mm_jpegdec_ops_t *ops)
+{
+ int32_t rc = 0;
+ uint32_t clnt_hdl = 0;
+ mm_jpeg_obj* jpeg_obj = NULL;
+
+ pthread_mutex_lock(&g_dec_intf_lock);
+ /* first time open */
+ if(NULL == g_jpegdec_obj) {
+ jpeg_obj = (mm_jpeg_obj *)malloc(sizeof(mm_jpeg_obj));
+ if(NULL == jpeg_obj) {
+ LOGE("no mem");
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return clnt_hdl;
+ }
+
+ /* initialize jpeg obj */
+ memset(jpeg_obj, 0, sizeof(mm_jpeg_obj));
+ rc = mm_jpegdec_init(jpeg_obj);
+ if(0 != rc) {
+ LOGE("mm_jpeg_init err = %d", rc);
+ free(jpeg_obj);
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return clnt_hdl;
+ }
+
+ /* remember in global variable */
+ g_jpegdec_obj = jpeg_obj;
+ }
+
+ /* open new client */
+ clnt_hdl = mm_jpeg_new_client(g_jpegdec_obj);
+ if (clnt_hdl > 0) {
+ /* valid client */
+ if (NULL != ops) {
+ /* fill in ops tbl if ptr not NULL */
+ ops->start_job = mm_jpegdec_intf_start_job;
+ ops->abort_job = mm_jpegdec_intf_abort_job;
+ ops->create_session = mm_jpegdec_intf_create_session;
+ ops->destroy_session = mm_jpegdec_intf_destroy_session;
+ ops->close = mm_jpegdec_intf_close;
+ }
+ } else {
+ /* failed new client */
+ LOGE("mm_jpeg_new_client failed");
+
+ if (0 == g_jpegdec_obj->num_clients) {
+ /* no client, close jpeg */
+ mm_jpegdec_deinit(g_jpegdec_obj);
+ free(g_jpegdec_obj);
+ g_jpegdec_obj = NULL;
+ }
+ }
+
+ pthread_mutex_unlock(&g_dec_intf_lock);
+ return clnt_hdl;
+}
+
+
+
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/test/Android.mk b/camera/QCamera2/stack/mm-jpeg-interface/test/Android.mk
new file mode 100644
index 0000000..b42636c
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/test/Android.mk
@@ -0,0 +1,87 @@
+#encoder int test
+OLD_LOCAL_PATH := $(LOCAL_PATH)
+MM_JPEG_TEST_PATH := $(call my-dir)
+
+include $(LOCAL_PATH)/../../common.mk
+include $(CLEAR_VARS)
+LOCAL_PATH := $(MM_JPEG_TEST_PATH)
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_CFLAGS := -DCAMERA_ION_HEAP_ID=ION_IOMMU_HEAP_ID
+LOCAL_CFLAGS += -Wall -Wextra -Werror -Wno-unused-parameter
+LOCAL_CFLAGS += -D_ANDROID_
+
+ifeq ($(strip $(TARGET_USES_ION)),true)
+LOCAL_CFLAGS += -DUSE_ION
+endif
+
+# System header file path prefix
+LOCAL_CFLAGS += -DSYSTEM_HEADER_PREFIX=sys
+
+OMX_HEADER_DIR := frameworks/native/include/media/openmax
+OMX_CORE_DIR := hardware/qcom/camera/mm-image-codec
+
+LOCAL_C_INCLUDES := $(MM_JPEG_TEST_PATH)
+LOCAL_C_INCLUDES += $(MM_JPEG_TEST_PATH)/../inc
+LOCAL_C_INCLUDES += $(MM_JPEG_TEST_PATH)/../../common
+LOCAL_C_INCLUDES += $(MM_JPEG_TEST_PATH)/../../mm-camera-interface/inc
+LOCAL_C_INCLUDES += $(OMX_HEADER_DIR)
+LOCAL_C_INCLUDES += $(OMX_CORE_DIR)/qexif
+LOCAL_C_INCLUDES += $(OMX_CORE_DIR)/qomx_core
+
+LOCAL_C_INCLUDES+= $(kernel_includes)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps)
+
+LOCAL_SRC_FILES := mm_jpeg_test.c
+
+LOCAL_32_BIT_ONLY := $(BOARD_QTI_CAMERA_32BIT_ONLY)
+LOCAL_MODULE := mm-jpeg-interface-test
+LOCAL_PRELINK_MODULE := false
+LOCAL_SHARED_LIBRARIES := libcutils libdl libmmjpeg_interface
+
+include $(BUILD_EXECUTABLE)
+
+
+
+#decoder int test
+
+include $(CLEAR_VARS)
+LOCAL_PATH := $(MM_JPEG_TEST_PATH)
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_CFLAGS := -DCAMERA_ION_HEAP_ID=ION_IOMMU_HEAP_ID
+LOCAL_CFLAGS += -Wall -Wextra -Werror -Wno-unused-parameter
+
+LOCAL_CFLAGS += -D_ANDROID_
+
+ifeq ($(strip $(TARGET_USES_ION)),true)
+LOCAL_CFLAGS += -DUSE_ION
+endif
+
+# System header file path prefix
+LOCAL_CFLAGS += -DSYSTEM_HEADER_PREFIX=sys
+
+OMX_HEADER_DIR := frameworks/native/include/media/openmax
+OMX_CORE_DIR := hardware/qcom/camera/mm-image-codec
+
+LOCAL_C_INCLUDES := $(MM_JPEG_TEST_PATH)
+LOCAL_C_INCLUDES += $(MM_JPEG_TEST_PATH)/../inc
+LOCAL_C_INCLUDES += $(MM_JPEG_TEST_PATH)/../../common
+LOCAL_C_INCLUDES += $(MM_JPEG_TEST_PATH)/../../mm-camera-interface/inc
+LOCAL_C_INCLUDES += $(OMX_HEADER_DIR)
+LOCAL_C_INCLUDES += $(OMX_CORE_DIR)/qexif
+LOCAL_C_INCLUDES += $(OMX_CORE_DIR)/qomx_core
+
+LOCAL_C_INCLUDES+= $(kernel_includes)
+LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps)
+
+LOCAL_SRC_FILES := mm_jpegdec_test.c
+
+LOCAL_32_BIT_ONLY := $(BOARD_QTI_CAMERA_32BIT_ONLY)
+LOCAL_MODULE := mm-jpegdec-interface-test
+LOCAL_PRELINK_MODULE := false
+LOCAL_SHARED_LIBRARIES := libcutils libdl libmmjpeg_interface
+
+include $(BUILD_EXECUTABLE)
+
+LOCAL_PATH := $(OLD_LOCAL_PATH) \ No newline at end of file
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/test/mm_jpeg_test.c b/camera/QCamera2/stack/mm-jpeg-interface/test/mm_jpeg_test.c
new file mode 100644
index 0000000..b1ddafc
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/test/mm_jpeg_test.c
@@ -0,0 +1,776 @@
+/* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// System dependencies
+#include <pthread.h>
+#include <stdlib.h>
+#define TIME_H <SYSTEM_HEADER_PREFIX/time.h>
+#include TIME_H
+
+// JPEG dependencies
+#include "mm_jpeg_interface.h"
+#include "mm_jpeg_ionbuf.h"
+
+// Camera dependencies
+#include "mm_camera_dbg.h"
+
+#define MAX_NUM_BUFS (12)
+#define MAX_NUM_CLIENT (8)
+
+/** DUMP_TO_FILE:
+ * @filename: file name
+ * @p_addr: address of the buffer
+ * @len: buffer length
+ *
+ * dump the image to the file
+ **/
+#define DUMP_TO_FILE(filename, p_addr, len) ({ \
+ FILE *fp = fopen(filename, "w+"); \
+ if (fp) { \
+ fwrite(p_addr, 1, len, fp); \
+ fclose(fp); \
+ } else { \
+ LOGE("cannot dump image"); \
+ } \
+})
+
+static uint32_t g_count = 1U, g_i;
+
+typedef struct {
+ mm_jpeg_color_format fmt;
+ cam_rational_type_t mult;
+ const char *str;
+} mm_jpeg_intf_test_colfmt_t;
+
+typedef struct {
+ char *filename;
+ int width;
+ int height;
+ char *out_filename;
+ uint32_t burst_mode;
+ uint32_t min_out_bufs;
+ mm_jpeg_intf_test_colfmt_t col_fmt;
+ uint32_t encode_thumbnail;
+ int tmb_width;
+ int tmb_height;
+ int main_quality;
+ int thumb_quality;
+ char *qtable_luma_file;
+ char *qtable_chroma_file;
+ int client_cnt;
+} jpeg_test_input_t;
+
+/* Static constants */
+/* default Luma Qtable */
+uint8_t DEFAULT_QTABLE_0[QUANT_SIZE] = {
+ 16, 11, 10, 16, 24, 40, 51, 61,
+ 12, 12, 14, 19, 26, 58, 60, 55,
+ 14, 13, 16, 24, 40, 57, 69, 56,
+ 14, 17, 22, 29, 51, 87, 80, 62,
+ 18, 22, 37, 56, 68, 109, 103, 77,
+ 24, 35, 55, 64, 81, 104, 113, 92,
+ 49, 64, 78, 87, 103, 121, 120, 101,
+ 72, 92, 95, 98, 112, 100, 103, 99
+};
+
+/* default Chroma Qtable */
+uint8_t DEFAULT_QTABLE_1[QUANT_SIZE] = {
+ 17, 18, 24, 47, 99, 99, 99, 99,
+ 18, 21, 26, 66, 99, 99, 99, 99,
+ 24, 26, 56, 99, 99, 99, 99, 99,
+ 47, 66, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99,
+ 99, 99, 99, 99, 99, 99, 99, 99
+};
+
+typedef struct {
+ char *filename[MAX_NUM_BUFS];
+ int width;
+ int height;
+ char *out_filename[MAX_NUM_BUFS];
+ pthread_mutex_t lock;
+ pthread_cond_t cond;
+ pthread_t thread_id;
+ buffer_t input[MAX_NUM_BUFS];
+ buffer_t output[MAX_NUM_BUFS];
+ int use_ion;
+ uint32_t handle;
+ mm_jpeg_ops_t ops;
+ uint32_t job_id[MAX_NUM_BUFS];
+ mm_jpeg_encode_params_t params;
+ mm_jpeg_job_t job;
+ uint32_t session_id;
+ uint32_t num_bufs;
+ uint32_t min_out_bufs;
+ size_t buf_filled_len[MAX_NUM_BUFS];
+ mm_dimension pic_size;
+ int ret;
+ int clinet_id;
+} mm_jpeg_intf_test_t;
+
+
+
+static const mm_jpeg_intf_test_colfmt_t color_formats[] =
+{
+ { MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2, {3, 2}, "YCRCBLP_H2V2" },
+ { MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2, {3, 2}, "YCBCRLP_H2V2" },
+ { MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1, {2, 1}, "YCRCBLP_H2V1" },
+ { MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1, {2, 1}, "YCBCRLP_H2V1" },
+ { MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2, {2, 1}, "YCRCBLP_H1V2" },
+ { MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2, {2, 1}, "YCBCRLP_H1V2" },
+ { MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1, {3, 1}, "YCRCBLP_H1V1" },
+ { MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1, {3, 1}, "YCBCRLP_H1V1" }
+};
+
+static jpeg_test_input_t jpeg_input[] = {
+ { QCAMERA_DUMP_FRM_LOCATION"test_1.yuv", 4000, 3008, QCAMERA_DUMP_FRM_LOCATION"test_1.jpg", 0, 0,
+ { MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2, {3, 2}, "YCRCBLP_H2V2" },
+ 0, 320, 240, 80, 80, NULL, NULL, 1}
+};
+
+static void mm_jpeg_encode_callback(jpeg_job_status_t status,
+ uint32_t client_hdl,
+ uint32_t jobId,
+ mm_jpeg_output_t *p_output,
+ void *userData)
+{
+ mm_jpeg_intf_test_t *p_obj = (mm_jpeg_intf_test_t *)userData;
+
+ pthread_mutex_lock(&p_obj->lock);
+
+ if (status == JPEG_JOB_STATUS_ERROR) {
+ LOGE("Encode error");
+ } else {
+ int i = 0;
+ for (i = 0; p_obj->job_id[i] && (jobId != p_obj->job_id[i]); i++)
+ ;
+ if (!p_obj->job_id[i]) {
+ LOGE("Cannot find job ID!!!");
+ goto error;
+ }
+ LOGE("Encode success addr %p len %zu idx %d",
+ p_output->buf_vaddr, p_output->buf_filled_len, i);
+
+ p_obj->buf_filled_len[i] = p_output->buf_filled_len;
+ if (p_obj->min_out_bufs) {
+ LOGE("Saving file%s addr %p len %zu",
+ p_obj->out_filename[i],
+ p_output->buf_vaddr, p_output->buf_filled_len);
+ DUMP_TO_FILE(p_obj->out_filename[i], p_output->buf_vaddr,
+ p_output->buf_filled_len);
+ }
+ }
+ g_i++;
+
+error:
+
+ if (g_i >= g_count) {
+ LOGE("Signal the thread");
+ pthread_cond_signal(&p_obj->cond);
+ }
+ pthread_mutex_unlock(&p_obj->lock);
+}
+
+int mm_jpeg_test_alloc(buffer_t *p_buffer, int use_pmem)
+{
+ int ret = 0;
+ /*Allocate buffers*/
+ if (use_pmem) {
+ p_buffer->addr = (uint8_t *)buffer_allocate(p_buffer, 0);
+ if (NULL == p_buffer->addr) {
+ LOGE("Error");
+ return -1;
+ }
+ } else {
+ /* Allocate heap memory */
+ p_buffer->addr = (uint8_t *)malloc(p_buffer->size);
+ if (NULL == p_buffer->addr) {
+ LOGE("Error");
+ return -1;
+ }
+ }
+ return ret;
+}
+
+void mm_jpeg_test_free(buffer_t *p_buffer)
+{
+ if (p_buffer->addr == NULL)
+ return;
+
+ if (p_buffer->p_pmem_fd >= 0)
+ buffer_deallocate(p_buffer);
+ else
+ free(p_buffer->addr);
+
+ memset(p_buffer, 0x0, sizeof(buffer_t));
+}
+
+int mm_jpeg_test_read(mm_jpeg_intf_test_t *p_obj, uint32_t idx)
+{
+ FILE *fp = NULL;
+ size_t file_size = 0;
+ fp = fopen(p_obj->filename[idx], "rb");
+ if (!fp) {
+ LOGE("error");
+ return -1;
+ }
+ fseek(fp, 0, SEEK_END);
+ file_size = (size_t)ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+ LOGE("input file size is %zu buf_size %zu",
+ file_size, p_obj->input[idx].size);
+
+ if (p_obj->input[idx].size > file_size) {
+ LOGE("error");
+ fclose(fp);
+ return -1;
+ }
+ fread(p_obj->input[idx].addr, 1, p_obj->input[idx].size, fp);
+ fclose(fp);
+ return 0;
+}
+
+/** mm_jpeg_test_read_qtable:
+ *
+ * Arguments:
+ * @filename: Qtable filename
+ * @chroma_flag: Flag indicating chroma qtable
+ *
+ * Return:
+ * 0 success, failure otherwise
+ *
+ * Description:
+ * Reads qtable from file and sets it in appropriate qtable
+ * based on flag.
+ **/
+int mm_jpeg_test_read_qtable(const char *filename, bool chroma_flag)
+{
+ FILE *fp = NULL;
+ int i;
+
+ if (filename == NULL)
+ return 0;
+
+ fp = fopen(filename, "r");
+ if (!fp) {
+ LOGE("error cannot open file");
+ return -1;
+ }
+
+ if (chroma_flag) {
+ for (i = 0; i < QUANT_SIZE; i++)
+ fscanf(fp, "%hhu,", &DEFAULT_QTABLE_1[i]);
+ } else {
+ for (i = 0; i < QUANT_SIZE; i++)
+ fscanf(fp, "%hhu,", &DEFAULT_QTABLE_0[i]);
+ }
+
+ fclose(fp);
+ return 0;
+}
+
+static int encode_init(jpeg_test_input_t *p_input, mm_jpeg_intf_test_t *p_obj,
+ int client_id)
+{
+ int rc = -1;
+ size_t size = (size_t)(p_input->width * p_input->height);
+ mm_jpeg_encode_params_t *p_params = &p_obj->params;
+ mm_jpeg_encode_job_t *p_job_params = &p_obj->job.encode_job;
+ uint32_t i = 0;
+ uint32_t burst_mode = p_input->burst_mode;
+ jpeg_test_input_t *p_in = p_input;
+
+ do {
+ p_obj->filename[i] = p_in->filename;
+ p_obj->width = p_input->width;
+ p_obj->height = p_input->height;
+ p_obj->out_filename[i] = p_in->out_filename;
+ p_obj->use_ion = 1;
+ p_obj->min_out_bufs = p_input->min_out_bufs;
+
+ /* allocate buffers */
+ p_obj->input[i].size = size * (size_t)p_input->col_fmt.mult.numerator /
+ (size_t)p_input->col_fmt.mult.denominator;
+ rc = mm_jpeg_test_alloc(&p_obj->input[i], p_obj->use_ion);
+ if (rc) {
+ LOGE("Error");
+ return -1;
+ }
+
+
+ rc = mm_jpeg_test_read(p_obj, i);
+ if (rc) {
+ LOGE("Error, unable to read input image");
+ return -1;
+ }
+
+ mm_jpeg_test_read_qtable(p_input->qtable_luma_file, false);
+ if (rc) {
+ LOGE("Error, unable to read luma qtable");
+ return -1;
+ }
+
+ mm_jpeg_test_read_qtable(p_input->qtable_chroma_file, true);
+ if (rc) {
+ LOGE("Error, unable to read chrome qtable");
+ return -1;
+ }
+
+ /* src buffer config*/
+ p_params->src_main_buf[i].buf_size = p_obj->input[i].size;
+ p_params->src_main_buf[i].buf_vaddr = p_obj->input[i].addr;
+ p_params->src_main_buf[i].fd = p_obj->input[i].p_pmem_fd;
+ p_params->src_main_buf[i].index = i;
+ p_params->src_main_buf[i].format = MM_JPEG_FMT_YUV;
+ p_params->src_main_buf[i].offset.mp[0].len = (uint32_t)size;
+ p_params->src_main_buf[i].offset.mp[0].stride = p_input->width;
+ p_params->src_main_buf[i].offset.mp[0].scanline = p_input->height;
+ p_params->src_main_buf[i].offset.mp[1].len = (uint32_t)(size >> 1);
+
+ /* src buffer config*/
+ p_params->src_thumb_buf[i].buf_size = p_obj->input[i].size;
+ p_params->src_thumb_buf[i].buf_vaddr = p_obj->input[i].addr;
+ p_params->src_thumb_buf[i].fd = p_obj->input[i].p_pmem_fd;
+ p_params->src_thumb_buf[i].index = i;
+ p_params->src_thumb_buf[i].format = MM_JPEG_FMT_YUV;
+ p_params->src_thumb_buf[i].offset.mp[0].len = (uint32_t)size;
+ p_params->src_thumb_buf[i].offset.mp[0].stride = p_input->width;
+ p_params->src_thumb_buf[i].offset.mp[0].scanline = p_input->height;
+ p_params->src_thumb_buf[i].offset.mp[1].len = (uint32_t)(size >> 1);
+
+
+ i++;
+ } while((++p_in)->filename);
+
+ p_obj->num_bufs = i;
+
+ pthread_mutex_init(&p_obj->lock, NULL);
+ pthread_cond_init(&p_obj->cond, NULL);
+
+
+ /* set encode parameters */
+ p_params->jpeg_cb = mm_jpeg_encode_callback;
+ p_params->userdata = p_obj;
+ p_params->color_format = p_input->col_fmt.fmt;
+ p_params->thumb_color_format = p_input->col_fmt.fmt;
+
+ if (p_obj->min_out_bufs) {
+ p_params->num_dst_bufs = 2;
+ } else {
+ p_params->num_dst_bufs = p_obj->num_bufs;
+ }
+
+ for (i = 0; i < (uint32_t)p_params->num_dst_bufs; i++) {
+ p_obj->output[i].size = size * 3/2;
+ rc = mm_jpeg_test_alloc(&p_obj->output[i], 0);
+ if (rc) {
+ LOGE("Error");
+ return -1;
+ }
+ /* dest buffer config */
+ p_params->dest_buf[i].buf_size = p_obj->output[i].size;
+ p_params->dest_buf[i].buf_vaddr = p_obj->output[i].addr;
+ p_params->dest_buf[i].fd = p_obj->output[i].p_pmem_fd;
+ p_params->dest_buf[i].index = i;
+ }
+
+
+ p_params->num_src_bufs = p_obj->num_bufs;
+ p_params->num_tmb_bufs = 0;
+ g_count = p_params->num_src_bufs;
+
+ p_params->encode_thumbnail = p_input->encode_thumbnail;
+ if (p_params->encode_thumbnail) {
+ p_params->num_tmb_bufs = p_obj->num_bufs;
+ }
+ p_params->quality = (uint32_t)p_input->main_quality;
+ p_params->thumb_quality = (uint32_t)p_input->thumb_quality;
+
+ p_job_params->dst_index = 0;
+ p_job_params->src_index = 0;
+ p_job_params->rotation = 0;
+
+ /* main dimension */
+ p_job_params->main_dim.src_dim.width = p_obj->width;
+ p_job_params->main_dim.src_dim.height = p_obj->height;
+ p_job_params->main_dim.dst_dim.width = p_obj->width;
+ p_job_params->main_dim.dst_dim.height = p_obj->height;
+ p_job_params->main_dim.crop.top = 0;
+ p_job_params->main_dim.crop.left = 0;
+ p_job_params->main_dim.crop.width = p_obj->width;
+ p_job_params->main_dim.crop.height = p_obj->height;
+
+ p_params->main_dim = p_job_params->main_dim;
+
+ /* thumb dimension */
+ p_job_params->thumb_dim.src_dim.width = p_obj->width;
+ p_job_params->thumb_dim.src_dim.height = p_obj->height;
+ p_job_params->thumb_dim.dst_dim.width = p_input->tmb_width;
+ p_job_params->thumb_dim.dst_dim.height = p_input->tmb_height;
+ p_job_params->thumb_dim.crop.top = 0;
+ p_job_params->thumb_dim.crop.left = 0;
+ p_job_params->thumb_dim.crop.width = 0;
+ p_job_params->thumb_dim.crop.height = 0;
+
+ p_params->thumb_dim = p_job_params->thumb_dim;
+
+ p_job_params->exif_info.numOfEntries = 0;
+ p_params->burst_mode = burst_mode;
+
+ /* Qtable */
+ p_job_params->qtable[0].eQuantizationTable =
+ OMX_IMAGE_QuantizationTableLuma;
+ p_job_params->qtable[1].eQuantizationTable =
+ OMX_IMAGE_QuantizationTableChroma;
+ p_job_params->qtable_set[0] = 1;
+ p_job_params->qtable_set[1] = 1;
+
+ for (i = 0; i < QUANT_SIZE; i++) {
+ p_job_params->qtable[0].nQuantizationMatrix[i] = DEFAULT_QTABLE_0[i];
+ p_job_params->qtable[1].nQuantizationMatrix[i] = DEFAULT_QTABLE_1[i];
+ }
+
+ p_obj->pic_size.w = (uint32_t)p_input->width;
+ p_obj->pic_size.h = (uint32_t)p_input->height;
+
+ p_obj->clinet_id = client_id;
+
+ return 0;
+}
+
+static void *encode_test(void *data)
+{
+ int rc = 0;
+ mm_jpeg_intf_test_t *jpeg_obj = (mm_jpeg_intf_test_t *)data;
+ char file_name[64];
+
+ uint32_t i = 0;
+ jpeg_obj->handle = jpeg_open(&jpeg_obj->ops, NULL, jpeg_obj->pic_size, NULL);
+ if (jpeg_obj->handle == 0) {
+ LOGE("Error");
+ jpeg_obj->ret = -1;
+ goto end;
+ }
+
+ rc = jpeg_obj->ops.create_session(jpeg_obj->handle, &jpeg_obj->params,
+ &jpeg_obj->job.encode_job.session_id);
+ if (jpeg_obj->job.encode_job.session_id == 0) {
+ LOGE("Error");
+ jpeg_obj->ret = -1;
+ goto end;
+ }
+
+ for (i = 0; i < jpeg_obj->num_bufs; i++) {
+ jpeg_obj->job.job_type = JPEG_JOB_TYPE_ENCODE;
+ jpeg_obj->job.encode_job.src_index = (int32_t) i;
+ jpeg_obj->job.encode_job.dst_index = (int32_t) i;
+ jpeg_obj->job.encode_job.thumb_index = (uint32_t) i;
+
+ if (jpeg_obj->params.burst_mode && jpeg_obj->min_out_bufs) {
+ jpeg_obj->job.encode_job.dst_index = -1;
+ }
+
+ rc = jpeg_obj->ops.start_job(&jpeg_obj->job, &jpeg_obj->job_id[i]);
+ if (rc) {
+ LOGE("Error");
+ jpeg_obj->ret = rc;
+ goto end;
+ }
+ }
+ jpeg_obj->job_id[i] = 0;
+
+ /*
+ usleep(5);
+ jpeg_obj->ops.abort_job(jpeg_obj->job_id[0]);
+ */
+ pthread_mutex_lock(&jpeg_obj->lock);
+ pthread_cond_wait(&jpeg_obj->cond, &jpeg_obj->lock);
+ pthread_mutex_unlock(&jpeg_obj->lock);
+
+ jpeg_obj->ops.destroy_session(jpeg_obj->job.encode_job.session_id);
+ jpeg_obj->ops.close(jpeg_obj->handle);
+
+end:
+ for (i = 0; i < jpeg_obj->num_bufs; i++) {
+ if (!jpeg_obj->min_out_bufs) {
+ // Save output files
+ LOGE("Saving file%s addr %p len %zu",
+ jpeg_obj->out_filename[i],
+ jpeg_obj->output[i].addr, jpeg_obj->buf_filled_len[i]);
+
+ snprintf(file_name, sizeof(file_name), "%s_%d.jpg",
+ jpeg_obj->out_filename[i], jpeg_obj->clinet_id);
+ fprintf(stderr, "Output file for client %d = %s\n",
+ jpeg_obj->clinet_id, file_name);
+
+ DUMP_TO_FILE(file_name, jpeg_obj->output[i].addr,
+ jpeg_obj->buf_filled_len[i]);
+ }
+ mm_jpeg_test_free(&jpeg_obj->input[i]);
+ mm_jpeg_test_free(&jpeg_obj->output[i]);
+ }
+ return NULL;
+}
+
+#define MAX_FILE_CNT (20)
+static int mm_jpeg_test_get_input(int argc, char *argv[],
+ jpeg_test_input_t *p_test)
+{
+ int c;
+ size_t in_file_cnt = 0, out_file_cnt = 0, i;
+ int idx = 0;
+ jpeg_test_input_t *p_test_base = p_test;
+
+ char *in_files[MAX_FILE_CNT];
+ char *out_files[MAX_FILE_CNT];
+
+ while ((c = getopt(argc, argv, "-I:O:W:H:F:BTx:y:Q:J:K:C:q:")) != -1) {
+ switch (c) {
+ case 'B':
+ fprintf(stderr, "%-25s\n", "Using burst mode");
+ p_test->burst_mode = 1;
+ break;
+ case 'I':
+ for (idx = optind - 1; idx < argc; idx++) {
+ if (argv[idx][0] == '-') {
+ break;
+ }
+ in_files[in_file_cnt++] = argv[idx];
+ }
+ optind = idx -1;
+
+ break;
+ case 'O':
+ for (idx = optind - 1; idx < argc; idx++) {
+ if (argv[idx][0] == '-') {
+ break;
+ }
+ out_files[out_file_cnt++] = argv[idx];
+ }
+ optind = idx -1;
+
+ break;
+ case 'W':
+ p_test->width = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Width: ", p_test->width);
+ break;
+ case 'H':
+ p_test->height = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Height: ", p_test->height);
+ break;
+ case 'F':
+ p_test->col_fmt = color_formats[atoi(optarg)];
+ fprintf(stderr, "%-25s%s\n", "Format: ", p_test->col_fmt.str);
+ break;
+ case 'M':
+ p_test->min_out_bufs = 1;
+ fprintf(stderr, "%-25s\n", "Using minimum number of output buffers");
+ break;
+ case 'T':
+ p_test->encode_thumbnail = 1;
+ fprintf(stderr, "%-25s\n", "Encode thumbnail");
+ break;
+ case 'x':
+ p_test->tmb_width = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Tmb Width: ", p_test->tmb_width);
+ break;
+ case 'y':
+ p_test->tmb_height = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Tmb Height: ", p_test->tmb_height);
+ break;
+ case 'Q':
+ p_test->main_quality = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Main quality: ", p_test->main_quality);
+ break;
+ case 'q':
+ p_test->thumb_quality = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Thumb quality: ", p_test->thumb_quality);
+ break;
+ case 'J':
+ p_test->qtable_luma_file = optarg;
+ fprintf(stderr, "%-25s%s\n", "Qtable luma path",
+ p_test->qtable_luma_file);
+ break;
+ case 'K':
+ p_test->qtable_chroma_file = optarg;
+ fprintf(stderr, "%-25s%s\n", "Qtable chroma path",
+ p_test->qtable_chroma_file);
+ break;
+ case 'C':
+ p_test->client_cnt = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Number of clients ",
+ p_test->client_cnt);
+ default:;
+ }
+ }
+ fprintf(stderr, "Infiles: %zu Outfiles: %zu\n", in_file_cnt, out_file_cnt);
+
+ if (p_test->client_cnt > MAX_NUM_CLIENT) {
+ fprintf(stderr, "Clients requested exceeds max limit %d\n",
+ MAX_NUM_CLIENT);
+ return 1;
+ }
+ if (in_file_cnt > out_file_cnt) {
+ fprintf(stderr, "%-25s\n", "Insufficient number of output files!");
+ return 1;
+ }
+
+ // Discard the extra out files
+ out_file_cnt = in_file_cnt;
+
+ p_test = realloc(p_test, (in_file_cnt + 1) * sizeof(*p_test));
+ if (!p_test) {
+ LOGE("Error");
+ return 1;
+ }
+ memset(p_test+1, 0, (in_file_cnt) * sizeof(*p_test));
+
+ for (i = 0; i < in_file_cnt; i++, p_test++) {
+ memcpy(p_test, p_test_base, sizeof(*p_test));
+ p_test->filename = in_files[i];
+ p_test->out_filename = out_files[i];
+ fprintf(stderr, "Inf: %s Outf: %s\n", in_files[i], out_files[i]);
+ }
+
+ return 0;
+}
+
+static void mm_jpeg_test_print_usage()
+{
+ fprintf(stderr, "Usage: program_name [options]\n");
+ fprintf(stderr, "Mandatory options:\n");
+ fprintf(stderr, " -I FILE1 [FILE2] [FILEN]\tList of input files\n");
+ fprintf(stderr, " -O FILE1 [FILE2] [FILEN]\tList of output files\n");
+ fprintf(stderr, " -W WIDTH\t\tOutput image width\n");
+ fprintf(stderr, " -H HEIGHT\t\tOutput image height\n");
+ fprintf(stderr, " -F \t\tColor format: \n");
+ fprintf(stderr, "\t\t\t\t%s (0), %s (1), %s (2) %s (3)\n"
+ "\t\t\t\t%s (4), %s (5), %s (6) %s (7)\n ",
+ color_formats[0].str, color_formats[1].str,
+ color_formats[2].str, color_formats[3].str,
+ color_formats[4].str, color_formats[5].str,
+ color_formats[6].str, color_formats[7].str);
+ fprintf(stderr, "Optional:\n");
+ fprintf(stderr, " -T \t\Encode thumbnail\n");
+ fprintf(stderr, " -x TMB_WIDTH\t\tThumbnail width\n");
+ fprintf(stderr, " -y TMB_HEIGHT\t\tThumbnail height\n");
+ fprintf(stderr, " -Q MAIN_QUALITY\t\tMain image quality\n");
+ fprintf(stderr, " -q TMB_QUALITY\t\tThumbnail image quality\n");
+ fprintf(stderr, " -B \t\tBurst mode. Utilize both encoder engines on"
+ "supported targets\n");
+ fprintf(stderr, " -M \t\tUse minimum number of output buffers \n");
+ fprintf(stderr, " -J \t\tLuma QTable filename. Comma separated 8x8"
+ " matrix\n");
+ fprintf(stderr, " -K \t\tChroma QTable filename. Comma separated"
+ " 8x8 matrix\n");
+ fprintf(stderr, " -C \t\tNumber of clients to run in parllel\n");
+ fprintf(stderr, "\n");
+}
+
+/** main:
+ *
+ * Arguments:
+ * @argc
+ * @argv
+ *
+ * Return:
+ * 0 or -ve values
+ *
+ * Description:
+ * main function
+ *
+ **/
+int main(int argc, char* argv[])
+{
+ jpeg_test_input_t *p_test_input;
+ mm_jpeg_intf_test_t client[MAX_NUM_CLIENT];
+ int ret = 0;
+ int i = 0;
+ int thread_cnt = 0;
+
+ if (argc > 1) {
+ p_test_input = calloc(2, sizeof(*p_test_input));
+ if (!p_test_input) {
+ LOGE("Error");
+ goto exit;
+ }
+ memcpy(p_test_input, &jpeg_input[0], sizeof(*p_test_input));
+ ret = mm_jpeg_test_get_input(argc, argv, p_test_input);
+ if (ret) {
+ LOGE("Error");
+ goto exit;
+ }
+ } else {
+ mm_jpeg_test_print_usage();
+ return 1;
+ }
+
+ for (i = 0; i < p_test_input->client_cnt; i++) {
+ memset(&client[i], 0x0, sizeof(mm_jpeg_intf_test_t));
+ ret = encode_init(p_test_input, &client[i], i);
+ if (ret) {
+ LOGE("Error");
+ return -1;
+ }
+
+ ret = pthread_create(&client[i].thread_id, NULL, encode_test,
+ &client[i]);
+ if (ret != 0) {
+ fprintf(stderr, "Error in thread creation\n");
+ break;
+ }
+ }
+
+ thread_cnt = i;
+ for (i = 0; i < thread_cnt; i++) {
+ pthread_join(client[i].thread_id, NULL);
+ }
+
+exit:
+ for (i = 0; i < thread_cnt; i++) {
+ if (!client[i].ret) {
+ fprintf(stderr, "%-25s %d %s\n", "Client", i, "Success!");
+ } else {
+ fprintf(stderr, "%-25s %d %s\n", "Client", i, "Fail!");
+ }
+ }
+
+ if (argc > 1) {
+ if (p_test_input) {
+ free(p_test_input);
+ p_test_input = NULL;
+ }
+ }
+
+ return ret;
+}
+
+
diff --git a/camera/QCamera2/stack/mm-jpeg-interface/test/mm_jpegdec_test.c b/camera/QCamera2/stack/mm-jpeg-interface/test/mm_jpegdec_test.c
new file mode 100644
index 0000000..beb62f5
--- /dev/null
+++ b/camera/QCamera2/stack/mm-jpeg-interface/test/mm_jpegdec_test.c
@@ -0,0 +1,479 @@
+/* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ * * Neither the name of The Linux Foundation nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
+ * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
+ * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+// System dependencies
+#include <pthread.h>
+#include <stdlib.h>
+#define TIME_H <SYSTEM_HEADER_PREFIX/time.h>
+#include TIME_H
+
+// JPEG dependencies
+#include "mm_jpeg_interface.h"
+#include "mm_jpeg_ionbuf.h"
+
+// Camera dependencies
+#include "mm_camera_dbg.h"
+
+#define MIN(a,b) (((a) < (b)) ? (a) : (b))
+#define MAX(a,b) (((a) > (b)) ? (a) : (b))
+#define CLAMP(x, min, max) MIN(MAX((x), (min)), (max))
+
+#define TIME_IN_US(r) ((uint64_t)r.tv_sec * 1000000LL + (uint64_t)r.tv_usec)
+struct timeval dtime[2];
+
+
+/** DUMP_TO_FILE:
+ * @filename: file name
+ * @p_addr: address of the buffer
+ * @len: buffer length
+ *
+ * dump the image to the file
+ **/
+#define DUMP_TO_FILE(filename, p_addr, len) ({ \
+ size_t rc = 0; \
+ FILE *fp = fopen(filename, "w+"); \
+ if (fp) { \
+ rc = fwrite(p_addr, 1, len, fp); \
+ fclose(fp); \
+ } else { \
+ LOGE("cannot dump image"); \
+ } \
+})
+
+static int g_count = 1, g_i;
+
+typedef struct {
+ char *filename;
+ int width;
+ int height;
+ char *out_filename;
+ int format;
+} jpeg_test_input_t;
+
+typedef struct {
+ char *filename;
+ int width;
+ int height;
+ char *out_filename;
+ pthread_mutex_t lock;
+ pthread_cond_t cond;
+ buffer_t input;
+ buffer_t output;
+ int use_ion;
+ uint32_t handle;
+ mm_jpegdec_ops_t ops;
+ uint32_t job_id[5];
+ mm_jpeg_decode_params_t params;
+ mm_jpeg_job_t job;
+ uint32_t session_id;
+} mm_jpegdec_intf_test_t;
+
+typedef struct {
+ char *format_str;
+ int eColorFormat;
+} mm_jpegdec_col_fmt_t;
+
+#define ARR_SZ(a) (sizeof(a)/sizeof(a[0]))
+
+static const mm_jpegdec_col_fmt_t col_formats[] =
+{
+ { "YCRCBLP_H2V2", (int)MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2 },
+ { "YCBCRLP_H2V2", (int)MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2 },
+ { "YCRCBLP_H2V1", (int)MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1 },
+ { "YCBCRLP_H2V1", (int)MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1 },
+ { "YCRCBLP_H1V2", (int)MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2 },
+ { "YCBCRLP_H1V2", (int)MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2 },
+ { "YCRCBLP_H1V1", (int)MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1 },
+ { "YCBCRLP_H1V1", (int)MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1 }
+};
+
+static void mm_jpegdec_decode_callback(jpeg_job_status_t status,
+ uint32_t client_hdl,
+ uint32_t jobId,
+ mm_jpeg_output_t *p_output,
+ void *userData)
+{
+ mm_jpegdec_intf_test_t *p_obj = (mm_jpegdec_intf_test_t *)userData;
+
+ if (status == JPEG_JOB_STATUS_ERROR) {
+ LOGE("Decode error");
+ } else {
+ gettimeofday(&dtime[1], NULL);
+ LOGE("Decode time %llu ms",
+ ((TIME_IN_US(dtime[1]) - TIME_IN_US(dtime[0]))/1000));
+
+ LOGE("Decode success file%s addr %p len %zu",
+ p_obj->out_filename,
+ p_output->buf_vaddr, p_output->buf_filled_len);
+ DUMP_TO_FILE(p_obj->out_filename, p_output->buf_vaddr, p_output->buf_filled_len);
+ }
+ g_i++;
+ if (g_i >= g_count) {
+ LOGE("Signal the thread");
+ pthread_cond_signal(&p_obj->cond);
+ }
+}
+
+int mm_jpegdec_test_alloc(buffer_t *p_buffer, int use_pmem)
+{
+ int ret = 0;
+ /*Allocate buffers*/
+ if (use_pmem) {
+ p_buffer->addr = (uint8_t *)buffer_allocate(p_buffer, 0);
+ if (NULL == p_buffer->addr) {
+ LOGE("Error");
+ return -1;
+ }
+ } else {
+ /* Allocate heap memory */
+ p_buffer->addr = (uint8_t *)malloc(p_buffer->size);
+ if (NULL == p_buffer->addr) {
+ LOGE("Error");
+ return -1;
+ }
+ }
+ return ret;
+}
+
+void mm_jpegdec_test_free(buffer_t *p_buffer)
+{
+ if (p_buffer->addr == NULL)
+ return;
+
+ if (p_buffer->p_pmem_fd >= 0)
+ buffer_deallocate(p_buffer);
+ else
+ free(p_buffer->addr);
+
+ memset(p_buffer, 0x0, sizeof(buffer_t));
+}
+
+int mm_jpegdec_test_read(mm_jpegdec_intf_test_t *p_obj)
+{
+ int rc = 0;
+ FILE *fp = NULL;
+ size_t file_size = 0;
+ fp = fopen(p_obj->filename, "rb");
+ if (!fp) {
+ LOGE("error");
+ return -1;
+ }
+ fseek(fp, 0, SEEK_END);
+ file_size = (size_t)ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ LOGE("input file size is %zu",
+ file_size);
+
+ p_obj->input.size = file_size;
+
+ /* allocate buffers */
+ rc = mm_jpegdec_test_alloc(&p_obj->input, p_obj->use_ion);
+ if (rc) {
+ LOGE("Error");
+ return -1;
+ }
+
+ fread(p_obj->input.addr, 1, p_obj->input.size, fp);
+ fclose(fp);
+ return 0;
+}
+
+void chromaScale(mm_jpeg_color_format format, double *cScale)
+{
+ double scale;
+
+ switch(format) {
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2:
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2:
+ scale = 1.5;
+ break;
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1:
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1:
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2:
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2:
+ scale = 2.0;
+ break;
+ case MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1:
+ case MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1:
+ scale = 3.0;
+ break;
+ case MM_JPEG_COLOR_FORMAT_MONOCHROME:
+ scale = 1.0;
+ break;
+ default:
+ scale = 0;
+ LOGE("color format Error");
+ }
+
+ *cScale = scale;
+}
+
+static int decode_init(jpeg_test_input_t *p_input, mm_jpegdec_intf_test_t *p_obj)
+{
+ int rc = -1;
+ size_t size = (size_t)(CEILING16(p_input->width) * CEILING16(p_input->height));
+ double cScale;
+ mm_jpeg_decode_params_t *p_params = &p_obj->params;
+ mm_jpeg_decode_job_t *p_job_params = &p_obj->job.decode_job;
+
+ p_obj->filename = p_input->filename;
+ p_obj->width = p_input->width;
+ p_obj->height = p_input->height;
+ p_obj->out_filename = p_input->out_filename;
+ p_obj->use_ion = 1;
+
+ pthread_mutex_init(&p_obj->lock, NULL);
+ pthread_cond_init(&p_obj->cond, NULL);
+
+ chromaScale(p_input->format, &cScale);
+ p_obj->output.size = (size_t)((double)size * cScale);
+ rc = mm_jpegdec_test_alloc(&p_obj->output, p_obj->use_ion);
+ if (rc) {
+ LOGE("Error");
+ return -1;
+ }
+
+ rc = mm_jpegdec_test_read(p_obj);
+ if (rc) {
+ LOGE("Error");
+ return -1;
+ }
+
+ /* set encode parameters */
+ p_params->jpeg_cb = mm_jpegdec_decode_callback;
+ p_params->userdata = p_obj;
+ p_params->color_format = p_input->format;
+
+ /* dest buffer config */
+ p_params->dest_buf[0].buf_size = p_obj->output.size;
+ p_params->dest_buf[0].buf_vaddr = p_obj->output.addr;
+ p_params->dest_buf[0].fd = p_obj->output.p_pmem_fd;
+ p_params->dest_buf[0].format = MM_JPEG_FMT_YUV;
+ p_params->dest_buf[0].offset.mp[0].len = (uint32_t)size;
+ p_params->dest_buf[0].offset.mp[1].len =
+ (uint32_t)((double)size * (cScale - 1.0));
+ p_params->dest_buf[0].offset.mp[0].stride = CEILING16(p_input->width);
+ p_params->dest_buf[0].offset.mp[0].scanline = CEILING16(p_input->height);
+ p_params->dest_buf[0].offset.mp[1].stride = CEILING16(p_input->width);
+ p_params->dest_buf[0].offset.mp[1].scanline = CEILING16(p_input->height);
+ p_params->dest_buf[0].index = 0;
+ p_params->num_dst_bufs = 1;
+
+ /* src buffer config*/
+ p_params->src_main_buf[0].buf_size = p_obj->input.size;
+ p_params->src_main_buf[0].buf_vaddr = p_obj->input.addr;
+ p_params->src_main_buf[0].fd = p_obj->input.p_pmem_fd;
+ p_params->src_main_buf[0].index = 0;
+ p_params->src_main_buf[0].format = MM_JPEG_FMT_BITSTREAM;
+ /*
+ p_params->src_main_buf[0].offset.mp[0].len = size;
+ p_params->src_main_buf[0].offset.mp[1].len = size >> 1;
+ */
+ p_params->num_src_bufs = 1;
+
+ p_job_params->dst_index = 0;
+ p_job_params->src_index = 0;
+ p_job_params->rotation = 0;
+
+ /* main dimension */
+ p_job_params->main_dim.src_dim.width = p_obj->width;
+ p_job_params->main_dim.src_dim.height = p_obj->height;
+ p_job_params->main_dim.dst_dim.width = p_obj->width;
+ p_job_params->main_dim.dst_dim.height = p_obj->height;
+ p_job_params->main_dim.crop.top = 0;
+ p_job_params->main_dim.crop.left = 0;
+ p_job_params->main_dim.crop.width = p_obj->width;
+ p_job_params->main_dim.crop.height = p_obj->height;
+
+
+ return 0;
+}
+
+void omx_test_dec_print_usage()
+{
+ fprintf(stderr, "Usage: program_name [options]\n");
+ fprintf(stderr, "Mandatory options:\n");
+ fprintf(stderr, " -I FILE\t\tPath to the input file.\n");
+ fprintf(stderr, " -O FILE\t\tPath for the output file.\n");
+ fprintf(stderr, " -W WIDTH\t\tOutput image width\n");
+ fprintf(stderr, " -H HEIGHT\t\tOutput image height\n");
+ fprintf(stderr, "Optional:\n");
+ fprintf(stderr, " -F FORMAT\t\tDefault image format:\n");
+ fprintf(stderr, "\t\t\t\t%s (0), %s (1), %s (2) %s (3)\n"
+ "%s (4), %s (5), %s (6) %s (7)\n",
+ col_formats[0].format_str, col_formats[1].format_str,
+ col_formats[2].format_str, col_formats[3].format_str,
+ col_formats[4].format_str, col_formats[5].format_str,
+ col_formats[6].format_str, col_formats[7].format_str
+ );
+
+ fprintf(stderr, "\n");
+}
+
+static int mm_jpegdec_test_get_input(int argc, char *argv[],
+ jpeg_test_input_t *p_test)
+{
+ int c;
+
+ while ((c = getopt(argc, argv, "I:O:W:H:F:")) != -1) {
+ switch (c) {
+ case 'O':
+ p_test->out_filename = optarg;
+ fprintf(stderr, "%-25s%s\n", "Output image path",
+ p_test->out_filename);
+ break;
+ case 'I':
+ p_test->filename = optarg;
+ fprintf(stderr, "%-25s%s\n", "Input image path", p_test->filename);
+ break;
+ case 'W':
+ p_test->width = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Default width", p_test->width);
+ break;
+ case 'H':
+ p_test->height = atoi(optarg);
+ fprintf(stderr, "%-25s%d\n", "Default height", p_test->height);
+ break;
+ case 'F': {
+ int format = 0;
+ format = atoi(optarg);
+ int num_formats = ARR_SZ(col_formats);
+ format = CLAMP(format, 0, num_formats);
+ p_test->format = col_formats[format].eColorFormat;
+ fprintf(stderr, "%-25s%s\n", "Default image format",
+ col_formats[format].format_str);
+ break;
+ }
+ default:;
+ }
+ }
+ if (!p_test->filename || !p_test->filename || !p_test->width ||
+ !p_test->height) {
+ fprintf(stderr, "Missing required arguments.\n");
+ omx_test_dec_print_usage();
+ return -1;
+ }
+ return 0;
+}
+
+static int decode_test(jpeg_test_input_t *p_input)
+{
+ int rc = 0;
+ mm_jpegdec_intf_test_t jpeg_obj;
+ int i = 0;
+
+ memset(&jpeg_obj, 0x0, sizeof(jpeg_obj));
+ rc = decode_init(p_input, &jpeg_obj);
+ if (rc) {
+ LOGE("Error");
+ return -1;
+ }
+
+ jpeg_obj.handle = jpegdec_open(&jpeg_obj.ops);
+ if (jpeg_obj.handle == 0) {
+ LOGE("Error");
+ goto end;
+ }
+
+ rc = jpeg_obj.ops.create_session(jpeg_obj.handle, &jpeg_obj.params,
+ &jpeg_obj.job.decode_job.session_id);
+ if (jpeg_obj.job.decode_job.session_id == 0) {
+ LOGE("Error");
+ goto end;
+ }
+
+ for (i = 0; i < g_count; i++) {
+ jpeg_obj.job.job_type = JPEG_JOB_TYPE_DECODE;
+
+ LOGE("Starting decode job");
+ gettimeofday(&dtime[0], NULL);
+
+ fprintf(stderr, "Starting decode of %s into %s outw %d outh %d\n\n",
+ p_input->filename, p_input->out_filename,
+ p_input->width, p_input->height);
+ rc = jpeg_obj.ops.start_job(&jpeg_obj.job, &jpeg_obj.job_id[i]);
+ if (rc) {
+ LOGE("Error");
+ goto end;
+ }
+ }
+
+ /*
+ usleep(5);
+ jpeg_obj.ops.abort_job(jpeg_obj.job_id[0]);
+ */
+ pthread_mutex_lock(&jpeg_obj.lock);
+ pthread_cond_wait(&jpeg_obj.cond, &jpeg_obj.lock);
+ pthread_mutex_unlock(&jpeg_obj.lock);
+
+ fprintf(stderr, "Decode time %llu ms\n",
+ ((TIME_IN_US(dtime[1]) - TIME_IN_US(dtime[0]))/1000));
+
+
+ jpeg_obj.ops.destroy_session(jpeg_obj.job.decode_job.session_id);
+
+ jpeg_obj.ops.close(jpeg_obj.handle);
+
+
+end:
+ mm_jpegdec_test_free(&jpeg_obj.input);
+ mm_jpegdec_test_free(&jpeg_obj.output);
+ return 0;
+}
+
+/** main:
+ *
+ * Arguments:
+ * @argc
+ * @argv
+ *
+ * Return:
+ * 0 or -ve values
+ *
+ * Description:
+ * main function
+ *
+ **/
+int main(int argc, char* argv[])
+{
+ jpeg_test_input_t dec_test_input;
+ int ret;
+
+ memset(&dec_test_input, 0, sizeof(dec_test_input));
+ ret = mm_jpegdec_test_get_input(argc, argv, &dec_test_input);
+
+ if (ret) {
+ return -1;
+ }
+
+ return decode_test(&dec_test_input);
+}
+
+