mm_jpeg.h revision f11b21ef2a1403f259cc039c930ce922683d510a
1/* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30#ifndef MM_JPEG_H_
31#define MM_JPEG_H_
32
33#include <cam_semaphore.h>
34#include "mm_jpeg_interface.h"
35#include "cam_list.h"
36#include "OMX_Types.h"
37#include "OMX_Index.h"
38#include "OMX_Core.h"
39#include "OMX_Component.h"
40#include "QOMX_JpegExtensions.h"
41#include "mm_jpeg_ionbuf.h"
42
43#define MM_JPEG_MAX_THREADS 30
44#define MM_JPEG_CIRQ_SIZE 30
45#define MM_JPEG_MAX_SESSION 10
46#define MAX_EXIF_TABLE_ENTRIES 50
47#define MAX_JPEG_SIZE 20000000
48#define MAX_OMX_HANDLES (5)
49#define ASPECT_TOLERANCE 0.001
50
51
52/** mm_jpeg_abort_state_t:
53 *  @MM_JPEG_ABORT_NONE: Abort is not issued
54 *  @MM_JPEG_ABORT_INIT: Abort is issued from the client
55 *  @MM_JPEG_ABORT_DONE: Abort is completed
56 *
57 *  State representing the abort state
58 **/
59typedef enum {
60  MM_JPEG_ABORT_NONE,
61  MM_JPEG_ABORT_INIT,
62  MM_JPEG_ABORT_DONE,
63} mm_jpeg_abort_state_t;
64
65
66/* define max num of supported concurrent jpeg jobs by OMX engine.
67 * Current, only one per time */
68#define NUM_MAX_JPEG_CNCURRENT_JOBS 2
69
70#define JOB_ID_MAGICVAL 0x1
71#define JOB_HIST_MAX 10000
72
73/** DUMP_TO_FILE:
74 *  @filename: file name
75 *  @p_addr: address of the buffer
76 *  @len: buffer length
77 *
78 *  dump the image to the file
79 **/
80#define DUMP_TO_FILE(filename, p_addr, len) ({ \
81  size_t rc = 0; \
82  FILE *fp = fopen(filename, "w+"); \
83  if (fp) { \
84    rc = fwrite(p_addr, 1, len, fp); \
85    CDBG_ERROR("%s:%d] written size %zu", __func__, __LINE__, len); \
86    fclose(fp); \
87  } else { \
88    CDBG_ERROR("%s:%d] open %s failed", __func__, __LINE__, filename); \
89  } \
90})
91
92/** DUMP_TO_FILE2:
93 *  @filename: file name
94 *  @p_addr: address of the buffer
95 *  @len: buffer length
96 *
97 *  dump the image to the file if the memory is non-contiguous
98 **/
99#define DUMP_TO_FILE2(filename, p_addr1, len1, paddr2, len2) ({ \
100  size_t rc = 0; \
101  FILE *fp = fopen(filename, "w+"); \
102  if (fp) { \
103    rc = fwrite(p_addr1, 1, len1, fp); \
104    rc = fwrite(p_addr2, 1, len2, fp); \
105    CDBG_ERROR("%s:%d] written %zu %zu", __func__, __LINE__, len1, len2); \
106    fclose(fp); \
107  } else { \
108    CDBG_ERROR("%s:%d] open %s failed", __func__, __LINE__, filename); \
109  } \
110})
111
112/** MM_JPEG_CHK_ABORT:
113 *  @p: client pointer
114 *  @ret: return value
115 *  @label: label to jump to
116 *
117 *  check the abort failure
118 **/
119#define MM_JPEG_CHK_ABORT(p, ret, label) ({ \
120  if (MM_JPEG_ABORT_INIT == p->abort_state) { \
121    CDBG_ERROR("%s:%d] jpeg abort", __func__, __LINE__); \
122    ret = OMX_ErrorNone; \
123    goto label; \
124  } \
125})
126
127#define GET_CLIENT_IDX(x) ((x) & 0xff)
128#define GET_SESSION_IDX(x) (((x) >> 8) & 0xff)
129#define GET_JOB_IDX(x) (((x) >> 16) & 0xff)
130
131typedef struct {
132  union {
133    int i_data[MM_JPEG_CIRQ_SIZE];
134    void *p_data[MM_JPEG_CIRQ_SIZE];
135  };
136  int front;
137  int rear;
138  int count;
139  pthread_mutex_t lock;
140} mm_jpeg_cirq_t;
141
142/** cirq_reset:
143 *
144 *  Arguments:
145 *    @q: circular queue
146 *
147 *  Return:
148 *       none
149 *
150 *  Description:
151 *       Resets the circular queue
152 *
153 **/
154static inline void cirq_reset(mm_jpeg_cirq_t *q)
155{
156  q->front = 0;
157  q->rear = 0;
158  q->count = 0;
159  pthread_mutex_init(&q->lock, NULL);
160}
161
162/** cirq_empty:
163 *
164 *  Arguments:
165 *    @q: circular queue
166 *
167 *  Return:
168 *       none
169 *
170 *  Description:
171 *       check if the curcular queue is empty
172 *
173 **/
174#define cirq_empty(q) (q->count == 0)
175
176/** cirq_full:
177 *
178 *  Arguments:
179 *    @q: circular queue
180 *
181 *  Return:
182 *       none
183 *
184 *  Description:
185 *       check if the curcular queue is full
186 *
187 **/
188#define cirq_full(q) (q->count == MM_JPEG_CIRQ_SIZE)
189
190/** cirq_enqueue:
191 *
192 *  Arguments:
193 *    @q: circular queue
194 *    @data: data to be inserted
195 *
196 *  Return:
197 *       true/false
198 *
199 *  Description:
200 *       enqueue an element into circular queue
201 *
202 **/
203#define cirq_enqueue(q, type, data) ({ \
204  int rc = 0; \
205  pthread_mutex_lock(&q->lock); \
206  if (cirq_full(q)) { \
207    rc = -1; \
208  } else { \
209    q->type[q->rear] = data; \
210    q->rear = (q->rear + 1) % MM_JPEG_CIRQ_SIZE; \
211    q->count++; \
212  } \
213  pthread_mutex_unlock(&q->lock); \
214  rc; \
215})
216
217/** cirq_dequeue:
218 *
219 *  Arguments:
220 *    @q: circular queue
221 *    @data: data to be popped
222 *
223 *  Return:
224 *       true/false
225 *
226 *  Description:
227 *       dequeue an element from the circular queue
228 *
229 **/
230#define cirq_dequeue(q, type, data) ({ \
231  int rc = 0; \
232  pthread_mutex_lock(&q->lock); \
233  if (cirq_empty(q)) { \
234    pthread_mutex_unlock(&q->lock); \
235    rc = -1; \
236  } else { \
237    data = q->type[q->front]; \
238    q->count--; \
239  } \
240  pthread_mutex_unlock(&q->lock); \
241  rc; \
242})
243
244
245typedef union {
246  uint32_t u32;
247  void* p;
248} mm_jpeg_q_data_t;
249
250  typedef struct {
251  struct cam_list list;
252  mm_jpeg_q_data_t data;
253} mm_jpeg_q_node_t;
254
255typedef struct {
256  mm_jpeg_q_node_t head; /* dummy head */
257  uint32_t size;
258  pthread_mutex_t lock;
259} mm_jpeg_queue_t;
260
261typedef enum {
262  MM_JPEG_CMD_TYPE_JOB,          /* job cmd */
263  MM_JPEG_CMD_TYPE_EXIT,         /* EXIT cmd for exiting jobMgr thread */
264  MM_JPEG_CMD_TYPE_DECODE_JOB,
265  MM_JPEG_CMD_TYPE_MAX
266} mm_jpeg_cmd_type_t;
267
268typedef struct mm_jpeg_job_session {
269  uint32_t client_hdl;           /* client handler */
270  uint32_t jobId;                /* job ID */
271  uint32_t sessionId;            /* session ID */
272  mm_jpeg_encode_params_t params; /* encode params */
273  mm_jpeg_decode_params_t dec_params; /* encode params */
274  mm_jpeg_encode_job_t encode_job;             /* job description */
275  mm_jpeg_decode_job_t decode_job;
276  pthread_t encode_pid;          /* encode thread handler*/
277
278  void *jpeg_obj;                /* ptr to mm_jpeg_obj */
279  jpeg_job_status_t job_status;  /* job status */
280
281  int state_change_pending;      /* flag to indicate if state change is pending */
282  OMX_ERRORTYPE error_flag;      /* variable to indicate error during encoding */
283  mm_jpeg_abort_state_t abort_state; /* variable to indicate abort during encoding */
284
285  /* OMX related */
286  OMX_HANDLETYPE omx_handle;                      /* handle to omx engine */
287  OMX_CALLBACKTYPE omx_callbacks;                 /* callbacks to omx engine */
288
289  /* buffer headers */
290  OMX_BUFFERHEADERTYPE *p_in_omx_buf[MM_JPEG_MAX_BUF];
291  OMX_BUFFERHEADERTYPE *p_in_omx_thumb_buf[MM_JPEG_MAX_BUF];
292  OMX_BUFFERHEADERTYPE *p_out_omx_buf[MM_JPEG_MAX_BUF];
293
294  OMX_PARAM_PORTDEFINITIONTYPE inputPort;
295  OMX_PARAM_PORTDEFINITIONTYPE outputPort;
296  OMX_PARAM_PORTDEFINITIONTYPE inputTmbPort;
297
298  /* event locks */
299  pthread_mutex_t lock;
300  pthread_cond_t cond;
301
302  QEXIF_INFO_DATA exif_info_local[MAX_EXIF_TABLE_ENTRIES];  //all exif tags for JPEG encoder
303  int exif_count_local;
304
305  mm_jpeg_cirq_t cb_q;
306  int32_t ebd_count;
307  int32_t fbd_count;
308
309  /* this flag represents whether the job is active */
310  OMX_BOOL active;
311
312  /* this flag indicates if the configration is complete */
313  OMX_BOOL config;
314
315  /* job history count to generate unique id */
316  unsigned int job_hist;
317
318  OMX_BOOL encoding;
319
320  buffer_t work_buffer;
321
322  OMX_EVENTTYPE omxEvent;
323  int event_pending;
324
325  uint8_t *meta_enc_key;
326  size_t meta_enc_keylen;
327
328  struct mm_jpeg_job_session *next_session;
329
330  uint32_t curr_out_buf_idx;
331
332  uint32_t num_omx_sessions;
333  OMX_BOOL auto_out_buf;
334
335  mm_jpeg_queue_t *session_handle_q;
336  mm_jpeg_queue_t *out_buf_q;
337
338  int thumb_from_main;
339  uint32_t job_index;
340} mm_jpeg_job_session_t;
341
342typedef struct {
343  mm_jpeg_encode_job_t encode_job;
344  uint32_t job_id;
345  uint32_t client_handle;
346} mm_jpeg_encode_job_info_t;
347
348typedef struct {
349  mm_jpeg_decode_job_t decode_job;
350  uint32_t job_id;
351  uint32_t client_handle;
352} mm_jpeg_decode_job_info_t;
353
354typedef struct {
355  mm_jpeg_cmd_type_t type;
356  union {
357    mm_jpeg_encode_job_info_t enc_info;
358    mm_jpeg_decode_job_info_t dec_info;
359  };
360} mm_jpeg_job_q_node_t;
361
362typedef struct {
363  uint8_t is_used;                /* flag: if is a valid client */
364  uint32_t client_handle;         /* client handle */
365  mm_jpeg_job_session_t session[MM_JPEG_MAX_SESSION];
366  pthread_mutex_t lock;           /* job lock */
367} mm_jpeg_client_t;
368
369typedef struct {
370  pthread_t pid;                  /* job cmd thread ID */
371  cam_semaphore_t job_sem;        /* semaphore for job cmd thread */
372  mm_jpeg_queue_t job_queue;      /* queue for job to do */
373} mm_jpeg_job_cmd_thread_t;
374
375#define MAX_JPEG_CLIENT_NUM 8
376typedef struct mm_jpeg_obj_t {
377  /* ClientMgr */
378  int num_clients;                                /* num of clients */
379  mm_jpeg_client_t clnt_mgr[MAX_JPEG_CLIENT_NUM]; /* client manager */
380
381  /* JobMkr */
382  pthread_mutex_t job_lock;                       /* job lock */
383  mm_jpeg_job_cmd_thread_t job_mgr;               /* job mgr thread including todo_q*/
384  mm_jpeg_queue_t ongoing_job_q;                  /* queue for ongoing jobs */
385  buffer_t ionBuffer[MM_JPEG_CONCURRENT_SESSIONS_COUNT];
386
387
388  /* Max pic dimension for work buf calc*/
389  uint32_t max_pic_w;
390  uint32_t max_pic_h;
391#ifdef LOAD_ADSP_RPC_LIB
392  void *adsprpc_lib_handle;
393#endif
394
395  uint32_t work_buf_cnt;
396
397  uint32_t num_sessions;
398
399} mm_jpeg_obj;
400
401/** mm_jpeg_pending_func_t:
402 *
403 * Intermediate function for transition change
404 **/
405typedef OMX_ERRORTYPE (*mm_jpeg_transition_func_t)(void *);
406
407extern int32_t mm_jpeg_init(mm_jpeg_obj *my_obj);
408extern int32_t mm_jpeg_deinit(mm_jpeg_obj *my_obj);
409extern uint32_t mm_jpeg_new_client(mm_jpeg_obj *my_obj);
410extern int32_t mm_jpeg_start_job(mm_jpeg_obj *my_obj,
411  mm_jpeg_job_t* job,
412  uint32_t* jobId);
413extern int32_t mm_jpeg_abort_job(mm_jpeg_obj *my_obj,
414  uint32_t jobId);
415extern int32_t mm_jpeg_close(mm_jpeg_obj *my_obj,
416  uint32_t client_hdl);
417extern int32_t mm_jpeg_create_session(mm_jpeg_obj *my_obj,
418  uint32_t client_hdl,
419  mm_jpeg_encode_params_t *p_params,
420  uint32_t* p_session_id);
421extern int32_t mm_jpeg_destroy_session_by_id(mm_jpeg_obj *my_obj,
422  uint32_t session_id);
423
424extern int32_t mm_jpegdec_init(mm_jpeg_obj *my_obj);
425extern int32_t mm_jpegdec_deinit(mm_jpeg_obj *my_obj);
426extern int32_t mm_jpeg_jobmgr_thread_release(mm_jpeg_obj * my_obj);
427extern int32_t mm_jpeg_jobmgr_thread_launch(mm_jpeg_obj *my_obj);
428extern int32_t mm_jpegdec_start_decode_job(mm_jpeg_obj *my_obj,
429  mm_jpeg_job_t* job,
430  uint32_t* jobId);
431
432extern int32_t mm_jpegdec_create_session(mm_jpeg_obj *my_obj,
433  uint32_t client_hdl,
434  mm_jpeg_decode_params_t *p_params,
435  uint32_t* p_session_id);
436
437extern int32_t mm_jpegdec_destroy_session_by_id(mm_jpeg_obj *my_obj,
438  uint32_t session_id);
439
440extern int32_t mm_jpegdec_abort_job(mm_jpeg_obj *my_obj,
441  uint32_t jobId);
442
443int32_t mm_jpegdec_process_decoding_job(mm_jpeg_obj *my_obj,
444    mm_jpeg_job_q_node_t* job_node);
445
446/* utiltity fucntion declared in mm-camera-inteface2.c
447 * and need be used by mm-camera and below*/
448uint32_t mm_jpeg_util_generate_handler(uint8_t index);
449uint8_t mm_jpeg_util_get_index_by_handler(uint32_t handler);
450
451/* basic queue functions */
452extern int32_t mm_jpeg_queue_init(mm_jpeg_queue_t* queue);
453extern int32_t mm_jpeg_queue_enq(mm_jpeg_queue_t* queue,
454    mm_jpeg_q_data_t data);
455extern int32_t mm_jpeg_queue_enq_head(mm_jpeg_queue_t* queue,
456    mm_jpeg_q_data_t data);
457extern mm_jpeg_q_data_t mm_jpeg_queue_deq(mm_jpeg_queue_t* queue);
458extern int32_t mm_jpeg_queue_deinit(mm_jpeg_queue_t* queue);
459extern int32_t mm_jpeg_queue_flush(mm_jpeg_queue_t* queue);
460extern uint32_t mm_jpeg_queue_get_size(mm_jpeg_queue_t* queue);
461extern mm_jpeg_q_data_t mm_jpeg_queue_peek(mm_jpeg_queue_t* queue);
462extern int32_t addExifEntry(QOMX_EXIF_INFO *p_exif_info, exif_tag_id_t tagid,
463  exif_tag_type_t type, uint32_t count, void *data);
464extern int32_t releaseExifEntry(QEXIF_INFO_DATA *p_exif_data);
465extern int process_meta_data(metadata_buffer_t *p_meta,
466  QOMX_EXIF_INFO *exif_info, mm_jpeg_exif_params_t *p_cam3a_params,
467  cam_hal_version_t hal_version);
468
469OMX_ERRORTYPE mm_jpeg_session_change_state(mm_jpeg_job_session_t* p_session,
470  OMX_STATETYPE new_state,
471  mm_jpeg_transition_func_t p_exec);
472
473int map_jpeg_format(mm_jpeg_color_format color_fmt);
474
475OMX_BOOL mm_jpeg_session_abort(mm_jpeg_job_session_t *p_session);
476/**
477 *
478 * special queue functions for job queue
479 **/
480mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_client_id(
481  mm_jpeg_queue_t* queue, uint32_t client_hdl);
482mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_job_id(
483  mm_jpeg_queue_t* queue, uint32_t job_id);
484mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_by_session_id(
485  mm_jpeg_queue_t* queue, uint32_t session_id);
486mm_jpeg_job_q_node_t* mm_jpeg_queue_remove_job_unlk(
487  mm_jpeg_queue_t* queue, uint32_t job_id);
488
489
490/** mm_jpeg_queue_func_t:
491 *
492 * Intermediate function for queue operation
493 **/
494typedef void (*mm_jpeg_queue_func_t)(void *);
495
496/** mm_jpeg_exif_flash_mode:
497 *
498 * Exif flash mode values
499 **/
500typedef enum {
501  MM_JPEG_EXIF_FLASH_MODE_ON   = 0x1,
502  MM_JPEG_EXIF_FLASH_MODE_OFF  = 0x2,
503  MM_JPEG_EXIF_FLASH_MODE_AUTO = 0x3,
504  MM_JPEG_EXIF_FLASH_MODE_MAX
505} mm_jpeg_exif_flash_mode;
506
507#endif /* MM_JPEG_H_ */
508
509
510