1/* Copyright (c) 2012-2016, 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_INTERFACE_H_
31#define MM_JPEG_INTERFACE_H_
32
33// System dependencies
34#include <stdbool.h>
35
36// Camera dependencies
37#include "QOMX_JpegExtensions.h"
38#include "cam_intf.h"
39
40#define MM_JPEG_MAX_PLANES 3
41#define MM_JPEG_MAX_BUF CAM_MAX_NUM_BUFS_PER_STREAM
42#define QUANT_SIZE 64
43#define QTABLE_MAX 2
44#define MM_JPEG_MAX_MPO_IMAGES 2
45
46/* bit mask for buffer usage*/
47#define MM_JPEG_HAS_READ_BUF CPU_HAS_READ
48#define MM_JPEG_HAS_WRITTEN_BUF CPU_HAS_WRITTEN
49
50typedef enum {
51  MM_JPEG_FMT_YUV,
52  MM_JPEG_FMT_BITSTREAM
53} mm_jpeg_format_t;
54
55typedef enum {
56  MM_JPEG_TYPE_JPEG,
57  MM_JPEG_TYPE_MPO
58} mm_jpeg_image_type_t;
59
60typedef struct {
61  cam_ae_exif_debug_t ae_debug_params;
62  cam_awb_exif_debug_t awb_debug_params;
63  cam_af_exif_debug_t af_debug_params;
64  cam_asd_exif_debug_t asd_debug_params;
65  cam_stats_buffer_exif_debug_t stats_debug_params;
66  cam_bestats_buffer_exif_debug_t bestats_debug_params;
67  cam_bhist_buffer_exif_debug_t bhist_debug_params;
68  cam_q3a_tuning_info_t q3a_tuning_debug_params;
69  uint8_t ae_debug_params_valid;
70  uint8_t awb_debug_params_valid;
71  uint8_t af_debug_params_valid;
72  uint8_t asd_debug_params_valid;
73  uint8_t stats_debug_params_valid;
74  uint8_t bestats_debug_params_valid;
75  uint8_t bhist_debug_params_valid;
76  uint8_t q3a_tuning_debug_params_valid;
77} mm_jpeg_debug_exif_params_t;
78
79typedef struct {
80  cam_3a_params_t cam_3a_params;
81  uint8_t cam_3a_params_valid;
82  cam_sensor_params_t sensor_params;
83  mm_jpeg_debug_exif_params_t *debug_params;
84} mm_jpeg_exif_params_t;
85
86typedef struct {
87  /* Indicates if it is a single jpeg or part of a multi picture sequence*/
88  mm_jpeg_image_type_t type;
89
90  /*Indicates if image is the primary image in a sequence of images.
91  Applicable only to multi picture formats*/
92  uint8_t is_primary;
93
94  /*Number of images in the sequence*/
95  uint32_t num_of_images;
96} mm_jpeg_multi_image_t;
97
98typedef struct {
99  uint32_t sequence;          /* for jpeg bit streams, assembling is based on sequence. sequence starts from 0 */
100  uint8_t *buf_vaddr;        /* ptr to buf */
101  int fd;                    /* fd of buf */
102  size_t buf_size;         /* total size of buf (header + image) */
103  mm_jpeg_format_t format;   /* buffer format*/
104  cam_frame_len_offset_t offset; /* offset of all the planes */
105  uint32_t index; /* index used to identify the buffers */
106} mm_jpeg_buf_t;
107
108typedef struct {
109  uint8_t *buf_vaddr;        /* ptr to buf */
110  int fd;                    /* fd of buf */
111  size_t buf_filled_len;   /* used for output image. filled by the client */
112} mm_jpeg_output_t;
113
114typedef enum {
115  MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2,
116  MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2,
117  MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1,
118  MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1,
119  MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2,
120  MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2,
121  MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1,
122  MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1,
123  MM_JPEG_COLOR_FORMAT_MONOCHROME,
124  MM_JPEG_COLOR_FORMAT_BITSTREAM_H2V2,
125  MM_JPEG_COLOR_FORMAT_BITSTREAM_H2V1,
126  MM_JPEG_COLOR_FORMAT_BITSTREAM_H1V2,
127  MM_JPEG_COLOR_FORMAT_BITSTREAM_H1V1,
128  MM_JPEG_COLOR_FORMAT_MAX
129} mm_jpeg_color_format;
130
131typedef enum {
132  JPEG_JOB_STATUS_DONE = 0,
133  JPEG_JOB_STATUS_ERROR
134} jpeg_job_status_t;
135
136typedef void (*jpeg_encode_callback_t)(jpeg_job_status_t status,
137  uint32_t client_hdl,
138  uint32_t jobId,
139  mm_jpeg_output_t *p_output,
140  void *userData);
141
142typedef struct {
143  /* src img dimension */
144  cam_dimension_t src_dim;
145
146  /* jpeg output dimension */
147  cam_dimension_t dst_dim;
148
149  /* crop information */
150  cam_rect_t crop;
151} mm_jpeg_dim_t;
152
153typedef struct {
154  /* num of buf in src img */
155  uint32_t num_src_bufs;
156
157  /* num of src tmb bufs */
158  uint32_t num_tmb_bufs;
159
160  /* num of buf in src img */
161  uint32_t num_dst_bufs;
162
163  /* should create thumbnail from main image or not */
164  uint32_t encode_thumbnail;
165
166  /* src img bufs */
167  mm_jpeg_buf_t src_main_buf[MM_JPEG_MAX_BUF];
168
169  /* this will be used only for bitstream */
170  mm_jpeg_buf_t src_thumb_buf[MM_JPEG_MAX_BUF];
171
172  /* this will be used only for bitstream */
173  mm_jpeg_buf_t dest_buf[MM_JPEG_MAX_BUF];
174
175  /* mainimage color format */
176  mm_jpeg_color_format color_format;
177
178  /* thumbnail color format */
179  mm_jpeg_color_format thumb_color_format;
180
181  /* jpeg quality: range 0~100 */
182  uint32_t quality;
183
184  /* jpeg thumbnail quality: range 0~100 */
185  uint32_t thumb_quality;
186
187  /* buf to exif entries, caller needs to
188   * take care of the memory manage with insider ptr */
189  QOMX_EXIF_INFO exif_info;
190
191  /*Callback registered to be called after encode*/
192  jpeg_encode_callback_t jpeg_cb;
193
194  /*Appdata passed by the user*/
195  void* userdata;
196
197  /* thumbnail dimension */
198  mm_jpeg_dim_t thumb_dim;
199
200  /* rotation informaiton */
201  uint32_t rotation;
202
203  /* thumb rotation informaiton */
204  uint32_t thumb_rotation;
205
206  /* main image dimension */
207  mm_jpeg_dim_t main_dim;
208
209  /* enable encoder burst mode */
210  uint32_t burst_mode;
211
212  /* get memory function ptr */
213  int (*get_memory)( omx_jpeg_ouput_buf_t *p_out_buf);
214
215  /* release memory function ptr */
216  int (*put_memory)( omx_jpeg_ouput_buf_t *p_out_buf);
217
218  /* Flag to indicate whether to generate thumbnail from postview */
219  bool thumb_from_postview;
220} mm_jpeg_encode_params_t;
221
222typedef struct {
223  /* num of buf in src img */
224  uint32_t num_src_bufs;
225
226  /* num of buf in src img */
227  uint32_t num_dst_bufs;
228
229  /* src img bufs */
230  mm_jpeg_buf_t src_main_buf[MM_JPEG_MAX_BUF];
231
232  /* this will be used only for bitstream */
233  mm_jpeg_buf_t dest_buf[MM_JPEG_MAX_BUF];
234
235  /* color format */
236  mm_jpeg_color_format color_format;
237
238  jpeg_encode_callback_t jpeg_cb;
239
240  void* userdata;
241
242} mm_jpeg_decode_params_t;
243
244/* This structure is populated by HAL to notify buffer
245  usage like has read or has written. This info is then
246  used to perform cache ops in jpeg */
247typedef struct {
248  /* main image source buff usage */
249  uint8_t main_src_buf;
250
251  /* thumbnail source buff usage */
252  uint8_t thumb_src_buf;
253
254  /* destination buff usage */
255  uint8_t dest_buf;
256
257  /* work buff usage */
258  uint8_t work_buf;
259
260} mm_jpeg_buf_usage_t;
261
262typedef struct {
263  /* active indices of the buffers for encoding */
264  int32_t src_index;
265  int32_t dst_index;
266  uint32_t thumb_index;
267  mm_jpeg_dim_t thumb_dim;
268
269  /* rotation informaiton */
270  uint32_t rotation;
271
272  /* main image dimension */
273  mm_jpeg_dim_t main_dim;
274
275  /*session id*/
276  uint32_t session_id;
277
278  /* jpeg output buffer ref count */
279  int32_t ref_count;
280
281  /* allocated jpeg output buffer */
282  void *alloc_out_buffer;
283
284  /*Metadata stream*/
285  metadata_buffer_t *p_metadata;
286
287  /*HAL version*/
288  cam_hal_version_t hal_version;
289
290  /* buf to exif entries, caller needs to
291   * take care of the memory manage with insider ptr */
292  QOMX_EXIF_INFO exif_info;
293
294  /* 3a parameters */
295  mm_jpeg_exif_params_t cam_exif_params;
296
297  /* jpeg encoder QTable */
298  uint8_t qtable_set[QTABLE_MAX];
299
300  OMX_IMAGE_PARAM_QUANTIZATIONTABLETYPE qtable[QTABLE_MAX];
301
302  /* flag to enable/disable mobicat */
303  uint8_t mobicat_mask;
304
305  /*Info associated with multiple image sequence*/
306  mm_jpeg_multi_image_t multi_image_info;
307
308  /* work buf */
309  mm_jpeg_buf_t work_buf;
310
311  /* Input from HAL notifing the prior usage of buffers,
312  this info will be used to perform cache ops*/
313  mm_jpeg_buf_usage_t buf_usage;
314
315} mm_jpeg_encode_job_t;
316
317typedef struct {
318  /* active indices of the buffers for encoding */
319  int32_t src_index;
320  int32_t dst_index;
321  uint32_t tmb_dst_index;
322
323  /* rotation informaiton */
324  uint32_t rotation;
325
326  /* main image  */
327  mm_jpeg_dim_t main_dim;
328
329  /*session id*/
330  uint32_t session_id;
331} mm_jpeg_decode_job_t;
332
333typedef enum {
334  JPEG_JOB_TYPE_ENCODE,
335  JPEG_JOB_TYPE_DECODE,
336  JPEG_JOB_TYPE_MAX
337} mm_jpeg_job_type_t;
338
339typedef struct {
340  mm_jpeg_job_type_t job_type;
341  union {
342    mm_jpeg_encode_job_t encode_job;
343    mm_jpeg_decode_job_t decode_job;
344  };
345} mm_jpeg_job_t;
346
347typedef struct {
348  uint32_t w;
349  uint32_t h;
350} mm_dimension;
351
352typedef struct {
353  /*Primary image in the MPO sequence*/
354  mm_jpeg_output_t primary_image;
355
356  /*All auxillary images in the sequence*/
357  mm_jpeg_output_t aux_images[MM_JPEG_MAX_MPO_IMAGES - 1];
358
359  /*Total number of images in the MPO sequence*/
360  int num_of_images;
361
362  /*Output MPO buffer*/
363  mm_jpeg_output_t output_buff;
364
365  /*Size of the allocated output buffer*/
366  size_t output_buff_size;
367} mm_jpeg_mpo_info_t;
368
369typedef struct {
370  /* config a job -- async call */
371  int (*start_job)(mm_jpeg_job_t* job, uint32_t* job_id);
372
373  /* abort a job -- sync call */
374  int (*abort_job)(uint32_t job_id);
375
376  /* create a session */
377  int (*create_session)(uint32_t client_hdl,
378    mm_jpeg_encode_params_t *p_params, uint32_t *p_session_id);
379
380  /* destroy session */
381  int (*destroy_session)(uint32_t session_id);
382
383  /* close a jpeg client -- sync call */
384  int (*close) (uint32_t clientHdl);
385
386} mm_jpeg_ops_t;
387
388typedef struct {
389  /* config a job -- async call */
390  int (*start_job)(mm_jpeg_job_t* job, uint32_t* job_id);
391
392  /* abort a job -- sync call */
393  int (*abort_job)(uint32_t job_id);
394
395  /* create a session */
396  int (*create_session)(uint32_t client_hdl,
397    mm_jpeg_decode_params_t *p_params, uint32_t *p_session_id);
398
399  /* destroy session */
400  int (*destroy_session)(uint32_t session_id);
401
402  /* close a jpeg client -- sync call */
403  int (*close) (uint32_t clientHdl);
404} mm_jpegdec_ops_t;
405
406typedef struct {
407
408  /* Get Mpo size*/
409  int (*get_mpo_size)(mm_jpeg_output_t jpeg_buffer[MM_JPEG_MAX_MPO_IMAGES],
410    int num_of_images);
411
412  /* Compose MPO*/
413  int (*compose_mpo)(mm_jpeg_mpo_info_t *mpo_info);
414
415} mm_jpeg_mpo_ops_t;
416
417/* open a jpeg client -- sync call
418 * returns client_handle.
419 * failed if client_handle=0
420 * jpeg ops tbl and mpo ops tbl will be filled in if open succeeds
421 * and jpeg meta data will be cached */
422uint32_t jpeg_open(mm_jpeg_ops_t *ops, mm_jpeg_mpo_ops_t *mpo_ops,
423  mm_dimension picture_size,
424  cam_jpeg_metadata_t *jpeg_metadata);
425
426/* open a jpeg client -- sync call
427 * returns client_handle.
428 * failed if client_handle=0
429 * jpeg ops tbl will be filled in if open succeeds */
430uint32_t jpegdec_open(mm_jpegdec_ops_t *ops);
431
432#endif /* MM_JPEG_INTERFACE_H_ */
433