1 /* Copyright (c) 2013-2015, 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 #include "mm_jpeg_interface.h"
31 #include "mm_jpeg_ionbuf.h"
32 #include <sys/time.h>
33 #include <stdlib.h>
34 
35 #define MAX_NUM_BUFS (12)
36 
37 /** DUMP_TO_FILE:
38  *  @filename: file name
39  *  @p_addr: address of the buffer
40  *  @len: buffer length
41  *
42  *  dump the image to the file
43  **/
44 #define DUMP_TO_FILE(filename, p_addr, len) ({ \
45   FILE *fp = fopen(filename, "w+"); \
46   if (fp) { \
47     fwrite(p_addr, 1, len, fp); \
48     fclose(fp); \
49   } else { \
50     CDBG_ERROR("%s:%d] cannot dump image", __func__, __LINE__); \
51   } \
52 })
53 
54 static uint32_t g_count = 1U, g_i;
55 
56 typedef struct {
57   mm_jpeg_color_format fmt;
58   cam_rational_type_t mult;
59   const char *str;
60 } mm_jpeg_intf_test_colfmt_t;
61 
62 typedef struct {
63   char *filename;
64   int width;
65   int height;
66   char *out_filename;
67   uint32_t burst_mode;
68   uint32_t min_out_bufs;
69   mm_jpeg_intf_test_colfmt_t col_fmt;
70   uint32_t encode_thumbnail;
71   int tmb_width;
72   int tmb_height;
73   int main_quality;
74   int thumb_quality;
75 } jpeg_test_input_t;
76 
77 /* Static constants */
78 /*  default Luma Qtable */
79 const uint8_t DEFAULT_QTABLE_0[QUANT_SIZE] = {
80   16, 11, 10, 16, 24, 40, 51, 61,
81   12, 12, 14, 19, 26, 58, 60, 55,
82   14, 13, 16, 24, 40, 57, 69, 56,
83   14, 17, 22, 29, 51, 87, 80, 62,
84   18, 22, 37, 56, 68, 109, 103, 77,
85   24, 35, 55, 64, 81, 104, 113, 92,
86   49, 64, 78, 87, 103, 121, 120, 101,
87   72, 92, 95, 98, 112, 100, 103, 99
88 };
89 
90 /*  default Chroma Qtable */
91 const uint8_t DEFAULT_QTABLE_1[QUANT_SIZE] = {
92   17, 18, 24, 47, 99, 99, 99, 99,
93   18, 21, 26, 66, 99, 99, 99, 99,
94   24, 26, 56, 99, 99, 99, 99, 99,
95   47, 66, 99, 99, 99, 99, 99, 99,
96   99, 99, 99, 99, 99, 99, 99, 99,
97   99, 99, 99, 99, 99, 99, 99, 99,
98   99, 99, 99, 99, 99, 99, 99, 99,
99   99, 99, 99, 99, 99, 99, 99, 99
100 };
101 
102 typedef struct {
103   char *filename[MAX_NUM_BUFS];
104   int width;
105   int height;
106   char *out_filename[MAX_NUM_BUFS];
107   pthread_mutex_t lock;
108   pthread_cond_t cond;
109   buffer_t input[MAX_NUM_BUFS];
110   buffer_t output[MAX_NUM_BUFS];
111   int use_ion;
112   uint32_t handle;
113   mm_jpeg_ops_t ops;
114   uint32_t job_id[MAX_NUM_BUFS];
115   mm_jpeg_encode_params_t params;
116   mm_jpeg_job_t job;
117   uint32_t session_id;
118   uint32_t num_bufs;
119   uint32_t min_out_bufs;
120   size_t buf_filled_len[MAX_NUM_BUFS];
121 } mm_jpeg_intf_test_t;
122 
123 
124 
125 static const mm_jpeg_intf_test_colfmt_t color_formats[] =
126 {
127   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2, {3, 2}, "YCRCBLP_H2V2" },
128   { MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2, {3, 2}, "YCBCRLP_H2V2" },
129   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1, {2, 1}, "YCRCBLP_H2V1" },
130   { MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1, {2, 1}, "YCBCRLP_H2V1" },
131   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2, {2, 1}, "YCRCBLP_H1V2" },
132   { MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2, {2, 1}, "YCBCRLP_H1V2" },
133   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1, {3, 1}, "YCRCBLP_H1V1" },
134   { MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1, {3, 1}, "YCBCRLP_H1V1" }
135 };
136 
137 static jpeg_test_input_t jpeg_input[] = {
138   { QCAMERA_DUMP_FRM_LOCATION"test_1.yuv", 4000, 3008, QCAMERA_DUMP_FRM_LOCATION"test_1.jpg", 0, 0,
139   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2, {3, 2}, "YCRCBLP_H2V2" }, 0, 320, 240, 80, 80}
140 };
141 
mm_jpeg_encode_callback(jpeg_job_status_t status,uint32_t client_hdl,uint32_t jobId,mm_jpeg_output_t * p_output,void * userData)142 static void mm_jpeg_encode_callback(jpeg_job_status_t status,
143   uint32_t client_hdl,
144   uint32_t jobId,
145   mm_jpeg_output_t *p_output,
146   void *userData)
147 {
148   mm_jpeg_intf_test_t *p_obj = (mm_jpeg_intf_test_t *)userData;
149 
150   pthread_mutex_lock(&p_obj->lock);
151 
152   if (status == JPEG_JOB_STATUS_ERROR) {
153     CDBG_ERROR("%s:%d] Encode error", __func__, __LINE__);
154   } else {
155     int i = 0;
156     for (i = 0; p_obj->job_id[i] && (jobId != p_obj->job_id[i]); i++)
157       ;
158     if (!p_obj->job_id[i]) {
159       CDBG_ERROR("%s:%d] Cannot find job ID!!!", __func__, __LINE__);
160       goto error;
161     }
162     CDBG_ERROR("%s:%d] Encode success addr %p len %zu idx %d",
163       __func__, __LINE__, p_output->buf_vaddr, p_output->buf_filled_len, i);
164 
165     p_obj->buf_filled_len[i] = p_output->buf_filled_len;
166     if (p_obj->min_out_bufs) {
167       CDBG_ERROR("%s:%d] Saving file%s addr %p len %zu",
168           __func__, __LINE__, p_obj->out_filename[i],
169           p_output->buf_vaddr, p_output->buf_filled_len);
170 
171       DUMP_TO_FILE(p_obj->out_filename[i], p_output->buf_vaddr,
172         p_output->buf_filled_len);
173     }
174   }
175   g_i++;
176 
177 error:
178 
179   if (g_i >= g_count) {
180     CDBG_ERROR("%s:%d] Signal the thread", __func__, __LINE__);
181     pthread_cond_signal(&p_obj->cond);
182   }
183   pthread_mutex_unlock(&p_obj->lock);
184 }
185 
mm_jpeg_test_alloc(buffer_t * p_buffer,int use_pmem)186 int mm_jpeg_test_alloc(buffer_t *p_buffer, int use_pmem)
187 {
188   int ret = 0;
189   /*Allocate buffers*/
190   if (use_pmem) {
191     p_buffer->addr = (uint8_t *)buffer_allocate(p_buffer, 0);
192     if (NULL == p_buffer->addr) {
193       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
194       return -1;
195     }
196   } else {
197     /* Allocate heap memory */
198     p_buffer->addr = (uint8_t *)malloc(p_buffer->size);
199     if (NULL == p_buffer->addr) {
200       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
201       return -1;
202     }
203   }
204   return ret;
205 }
206 
mm_jpeg_test_free(buffer_t * p_buffer)207 void mm_jpeg_test_free(buffer_t *p_buffer)
208 {
209   if (p_buffer->addr == NULL)
210     return;
211 
212   if (p_buffer->p_pmem_fd >= 0)
213     buffer_deallocate(p_buffer);
214   else
215     free(p_buffer->addr);
216 
217   memset(p_buffer, 0x0, sizeof(buffer_t));
218 }
219 
mm_jpeg_test_read(mm_jpeg_intf_test_t * p_obj,uint32_t idx)220 int mm_jpeg_test_read(mm_jpeg_intf_test_t *p_obj, uint32_t idx)
221 {
222   FILE *fp = NULL;
223   size_t file_size = 0;
224   fp = fopen(p_obj->filename[idx], "rb");
225   if (!fp) {
226     CDBG_ERROR("%s:%d] error", __func__, __LINE__);
227     return -1;
228   }
229   fseek(fp, 0, SEEK_END);
230   file_size = (size_t)ftell(fp);
231   fseek(fp, 0, SEEK_SET);
232   CDBG_ERROR("%s:%d] input file size is %zu buf_size %zu",
233     __func__, __LINE__, file_size, p_obj->input[idx].size);
234 
235   if (p_obj->input[idx].size > file_size) {
236     CDBG_ERROR("%s:%d] error", __func__, __LINE__);
237     fclose(fp);
238     return -1;
239   }
240   fread(p_obj->input[idx].addr, 1, p_obj->input[idx].size, fp);
241   fclose(fp);
242   return 0;
243 }
244 
encode_init(jpeg_test_input_t * p_input,mm_jpeg_intf_test_t * p_obj)245 static int encode_init(jpeg_test_input_t *p_input, mm_jpeg_intf_test_t *p_obj)
246 {
247   int rc = -1;
248   size_t size = (size_t)(p_input->width * p_input->height);
249   mm_jpeg_encode_params_t *p_params = &p_obj->params;
250   mm_jpeg_encode_job_t *p_job_params = &p_obj->job.encode_job;
251   uint32_t i = 0;
252   uint32_t burst_mode = p_input->burst_mode;
253   jpeg_test_input_t *p_in = p_input;
254 
255   do {
256     p_obj->filename[i] = p_in->filename;
257     p_obj->width = p_input->width;
258     p_obj->height = p_input->height;
259     p_obj->out_filename[i] = p_in->out_filename;
260     p_obj->use_ion = 1;
261     p_obj->min_out_bufs = p_input->min_out_bufs;
262 
263     /* allocate buffers */
264     p_obj->input[i].size = size * (size_t)p_input->col_fmt.mult.numerator /
265         (size_t)p_input->col_fmt.mult.denominator;
266     rc = mm_jpeg_test_alloc(&p_obj->input[i], p_obj->use_ion);
267     if (rc) {
268       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
269       return -1;
270     }
271 
272 
273     rc = mm_jpeg_test_read(p_obj, i);
274     if (rc) {
275       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
276       return -1;
277     }
278 
279     /* src buffer config*/
280     p_params->src_main_buf[i].buf_size = p_obj->input[i].size;
281     p_params->src_main_buf[i].buf_vaddr = p_obj->input[i].addr;
282     p_params->src_main_buf[i].fd = p_obj->input[i].p_pmem_fd;
283     p_params->src_main_buf[i].index = i;
284     p_params->src_main_buf[i].format = MM_JPEG_FMT_YUV;
285     p_params->src_main_buf[i].offset.mp[0].len = (uint32_t)size;
286     p_params->src_main_buf[i].offset.mp[0].stride = p_input->width;
287     p_params->src_main_buf[i].offset.mp[0].scanline = p_input->height;
288     p_params->src_main_buf[i].offset.mp[1].len = (uint32_t)(size >> 1);
289 
290     /* src buffer config*/
291     p_params->src_thumb_buf[i].buf_size = p_obj->input[i].size;
292     p_params->src_thumb_buf[i].buf_vaddr = p_obj->input[i].addr;
293     p_params->src_thumb_buf[i].fd = p_obj->input[i].p_pmem_fd;
294     p_params->src_thumb_buf[i].index = i;
295     p_params->src_thumb_buf[i].format = MM_JPEG_FMT_YUV;
296     p_params->src_thumb_buf[i].offset.mp[0].len = (uint32_t)size;
297     p_params->src_thumb_buf[i].offset.mp[0].stride = p_input->width;
298     p_params->src_thumb_buf[i].offset.mp[0].scanline = p_input->height;
299     p_params->src_thumb_buf[i].offset.mp[1].len = (uint32_t)(size >> 1);
300 
301 
302     i++;
303   } while((++p_in)->filename);
304 
305   p_obj->num_bufs = i;
306 
307   pthread_mutex_init(&p_obj->lock, NULL);
308   pthread_cond_init(&p_obj->cond, NULL);
309 
310 
311   /* set encode parameters */
312   p_params->jpeg_cb = mm_jpeg_encode_callback;
313   p_params->userdata = p_obj;
314   p_params->color_format = p_input->col_fmt.fmt;
315   p_params->thumb_color_format = p_input->col_fmt.fmt;
316 
317   if (p_obj->min_out_bufs) {
318     p_params->num_dst_bufs = 2;
319   } else {
320     p_params->num_dst_bufs = p_obj->num_bufs;
321   }
322 
323   for (i = 0; i < (uint32_t)p_params->num_dst_bufs; i++) {
324     p_obj->output[i].size = size * 3/2;
325     rc = mm_jpeg_test_alloc(&p_obj->output[i], 0);
326     if (rc) {
327       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
328       return -1;
329     }
330     /* dest buffer config */
331     p_params->dest_buf[i].buf_size = p_obj->output[i].size;
332     p_params->dest_buf[i].buf_vaddr = p_obj->output[i].addr;
333     p_params->dest_buf[i].fd = p_obj->output[i].p_pmem_fd;
334     p_params->dest_buf[i].index = i;
335   }
336 
337 
338   p_params->num_src_bufs = p_obj->num_bufs;
339   p_params->num_tmb_bufs = 0;
340   g_count = p_params->num_src_bufs;
341 
342   p_params->encode_thumbnail = p_input->encode_thumbnail;
343   if (p_params->encode_thumbnail) {
344       p_params->num_tmb_bufs = p_obj->num_bufs;
345   }
346   p_params->quality = (uint32_t)p_input->main_quality;
347   p_params->thumb_quality = (uint32_t)p_input->thumb_quality;
348 
349   p_job_params->dst_index = 0;
350   p_job_params->src_index = 0;
351   p_job_params->rotation = 0;
352 
353   /* main dimension */
354   p_job_params->main_dim.src_dim.width = p_obj->width;
355   p_job_params->main_dim.src_dim.height = p_obj->height;
356   p_job_params->main_dim.dst_dim.width = p_obj->width;
357   p_job_params->main_dim.dst_dim.height = p_obj->height;
358   p_job_params->main_dim.crop.top = 0;
359   p_job_params->main_dim.crop.left = 0;
360   p_job_params->main_dim.crop.width = p_obj->width;
361   p_job_params->main_dim.crop.height = p_obj->height;
362 
363   p_params->main_dim  = p_job_params->main_dim;
364 
365   /* thumb dimension */
366   p_job_params->thumb_dim.src_dim.width = p_obj->width;
367   p_job_params->thumb_dim.src_dim.height = p_obj->height;
368   p_job_params->thumb_dim.dst_dim.width = p_input->tmb_width;
369   p_job_params->thumb_dim.dst_dim.height = p_input->tmb_height;
370   p_job_params->thumb_dim.crop.top = 0;
371   p_job_params->thumb_dim.crop.left = 0;
372   p_job_params->thumb_dim.crop.width = 0;
373   p_job_params->thumb_dim.crop.height = 0;
374 
375   p_params->thumb_dim  = p_job_params->thumb_dim;
376 
377   p_job_params->exif_info.numOfEntries = 0;
378   p_params->burst_mode = burst_mode;
379 
380   /* Qtable */
381   p_job_params->qtable[0].eQuantizationTable =
382     OMX_IMAGE_QuantizationTableLuma;
383   p_job_params->qtable[1].eQuantizationTable =
384     OMX_IMAGE_QuantizationTableChroma;
385   p_job_params->qtable_set[0] = 1;
386   p_job_params->qtable_set[1] = 1;
387 
388   for (i = 0; i < QUANT_SIZE; i++) {
389     p_job_params->qtable[0].nQuantizationMatrix[i] = DEFAULT_QTABLE_0[i];
390     p_job_params->qtable[1].nQuantizationMatrix[i] = DEFAULT_QTABLE_1[i];
391   }
392 
393   return 0;
394 }
395 
encode_test(jpeg_test_input_t * p_input)396 static int encode_test(jpeg_test_input_t *p_input)
397 {
398   int rc = 0;
399   mm_jpeg_intf_test_t jpeg_obj;
400   uint32_t i = 0;
401 
402   memset(&jpeg_obj, 0x0, sizeof(jpeg_obj));
403   rc = encode_init(p_input, &jpeg_obj);
404   if (rc) {
405     CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
406     return -1;
407   }
408 
409   mm_dimension pic_size;
410   memset(&pic_size, 0, sizeof(mm_dimension));
411   pic_size.w = (uint32_t)p_input->width;
412   pic_size.h = (uint32_t)p_input->height;
413 
414   jpeg_obj.handle = jpeg_open(&jpeg_obj.ops, pic_size);
415   if (jpeg_obj.handle == 0) {
416     CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
417     goto end;
418   }
419 
420   rc = jpeg_obj.ops.create_session(jpeg_obj.handle, &jpeg_obj.params,
421     &jpeg_obj.job.encode_job.session_id);
422   if (jpeg_obj.job.encode_job.session_id == 0) {
423     CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
424     goto end;
425   }
426 
427   for (i = 0; i < jpeg_obj.num_bufs; i++) {
428     jpeg_obj.job.job_type = JPEG_JOB_TYPE_ENCODE;
429     jpeg_obj.job.encode_job.src_index = (int32_t) i;
430     jpeg_obj.job.encode_job.dst_index = (int32_t) i;
431     jpeg_obj.job.encode_job.thumb_index = (uint32_t) i;
432 
433     if (jpeg_obj.params.burst_mode && jpeg_obj.min_out_bufs) {
434       jpeg_obj.job.encode_job.dst_index = -1;
435     }
436 
437     rc = jpeg_obj.ops.start_job(&jpeg_obj.job, &jpeg_obj.job_id[i]);
438 
439     if (rc) {
440       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
441       goto end;
442     }
443   }
444   jpeg_obj.job_id[i] = 0;
445 
446   /*
447   usleep(5);
448   jpeg_obj.ops.abort_job(jpeg_obj.job_id[0]);
449   */
450   pthread_mutex_lock(&jpeg_obj.lock);
451   pthread_cond_wait(&jpeg_obj.cond, &jpeg_obj.lock);
452   pthread_mutex_unlock(&jpeg_obj.lock);
453 
454 
455   jpeg_obj.ops.destroy_session(jpeg_obj.job.encode_job.session_id);
456   jpeg_obj.ops.close(jpeg_obj.handle);
457 
458 end:
459   for (i = 0; i < jpeg_obj.num_bufs; i++) {
460     if (!jpeg_obj.min_out_bufs) {
461       // Save output files
462       CDBG_ERROR("%s:%d] Saving file%s addr %p len %zu",
463           __func__, __LINE__,jpeg_obj.out_filename[i],
464           jpeg_obj.output[i].addr, jpeg_obj.buf_filled_len[i]);
465 
466       DUMP_TO_FILE(jpeg_obj.out_filename[i], jpeg_obj.output[i].addr,
467         jpeg_obj.buf_filled_len[i]);
468     }
469     mm_jpeg_test_free(&jpeg_obj.input[i]);
470     mm_jpeg_test_free(&jpeg_obj.output[i]);
471   }
472   return 0;
473 }
474 
475 #define MAX_FILE_CNT (20)
mm_jpeg_test_get_input(int argc,char * argv[],jpeg_test_input_t * p_test)476 static int mm_jpeg_test_get_input(int argc, char *argv[],
477     jpeg_test_input_t *p_test)
478 {
479   int c;
480   size_t in_file_cnt = 0, out_file_cnt = 0, i;
481   int idx = 0;
482   jpeg_test_input_t *p_test_base = p_test;
483 
484   char *in_files[MAX_FILE_CNT];
485   char *out_files[MAX_FILE_CNT];
486 
487   while ((c = getopt(argc, argv, "-I:O:W:H:F:BTx:y:Q:q:")) != -1) {
488     switch (c) {
489     case 'B':
490       fprintf(stderr, "%-25s\n", "Using burst mode");
491       p_test->burst_mode = 1;
492       break;
493     case 'I':
494       for (idx = optind - 1; idx < argc; idx++) {
495         if (argv[idx][0] == '-') {
496           break;
497         }
498         in_files[in_file_cnt++] = argv[idx];
499       }
500       optind = idx -1;
501 
502       break;
503     case 'O':
504       for (idx = optind - 1; idx < argc; idx++) {
505         if (argv[idx][0] == '-') {
506           break;
507         }
508         out_files[out_file_cnt++] = argv[idx];
509       }
510       optind = idx -1;
511 
512       break;
513     case 'W':
514       p_test->width = atoi(optarg);
515       fprintf(stderr, "%-25s%d\n", "Width: ", p_test->width);
516       break;
517     case 'H':
518       p_test->height = atoi(optarg);
519       fprintf(stderr, "%-25s%d\n", "Height: ", p_test->height);
520       break;
521     case 'F':
522       p_test->col_fmt = color_formats[atoi(optarg)];
523       fprintf(stderr, "%-25s%s\n", "Format: ", p_test->col_fmt.str);
524       break;
525     case 'M':
526       p_test->min_out_bufs = 1;
527       fprintf(stderr, "%-25s\n", "Using minimum number of output buffers");
528       break;
529     case 'T':
530       p_test->encode_thumbnail = 1;
531       fprintf(stderr, "%-25s\n", "Encode thumbnail");
532       break;
533     case 'x':
534       p_test->tmb_width = atoi(optarg);
535       fprintf(stderr, "%-25s%d\n", "Tmb Width: ", p_test->tmb_width);
536       break;
537     case 'y':
538       p_test->tmb_height = atoi(optarg);
539       fprintf(stderr, "%-25s%d\n", "Tmb Height: ", p_test->tmb_height);
540       break;
541     case 'Q':
542       p_test->main_quality = atoi(optarg);
543       fprintf(stderr, "%-25s%d\n", "Main quality: ", p_test->main_quality);
544       break;
545     case 'q':
546       p_test->thumb_quality = atoi(optarg);
547       fprintf(stderr, "%-25s%d\n", "Thumb quality: ", p_test->thumb_quality);
548       break;
549     default:;
550     }
551   }
552   fprintf(stderr, "Infiles: %zu Outfiles: %zu\n", in_file_cnt, out_file_cnt);
553 
554   if (in_file_cnt > out_file_cnt) {
555     fprintf(stderr, "%-25s\n", "Insufficient number of output files!");
556     return 1;
557   }
558 
559   // Discard the extra out files
560   out_file_cnt = in_file_cnt;
561 
562   p_test = realloc(p_test, (in_file_cnt + 1) * sizeof(*p_test));
563   if (!p_test) {
564     CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
565     return 1;
566   }
567   memset(p_test+1, 0, (in_file_cnt) * sizeof(*p_test));
568 
569   for (i = 0; i < in_file_cnt; i++, p_test++) {
570     memcpy(p_test, p_test_base, sizeof(*p_test));
571     p_test->filename = in_files[i];
572     p_test->out_filename = out_files[i];
573     fprintf(stderr, "Inf: %s Outf: %s\n", in_files[i], out_files[i]);
574   }
575 
576 
577   return 0;
578 }
579 
mm_jpeg_test_print_usage()580 static void mm_jpeg_test_print_usage()
581 {
582   fprintf(stderr, "Usage: program_name [options]\n");
583   fprintf(stderr, "Mandatory options:\n");
584   fprintf(stderr, "  -I FILE1 [FILE2] [FILEN]\tList of input files\n");
585   fprintf(stderr, "  -O FILE1 [FILE2] [FILEN]\tList of output files\n");
586   fprintf(stderr, "  -W WIDTH\t\tOutput image width\n");
587   fprintf(stderr, "  -H HEIGHT\t\tOutput image height\n");
588   fprintf(stderr, "  -F \t\tColor format: \n");
589   fprintf(stderr, "\t\t\t\t%s (0), %s (1), %s (2) %s (3)\n"
590       "\t\t\t\t%s (4), %s (5), %s (6) %s (7)\n ",
591       color_formats[0].str, color_formats[1].str,
592       color_formats[2].str, color_formats[3].str,
593       color_formats[4].str, color_formats[5].str,
594       color_formats[6].str, color_formats[7].str);
595   fprintf(stderr, "Optional:\n");
596   fprintf(stderr, "  -T \t\Encode thumbnail\n");
597   fprintf(stderr, "  -x TMB_WIDTH\t\tThumbnail width\n");
598   fprintf(stderr, "  -y TMB_HEIGHT\t\tThumbnail height\n");
599   fprintf(stderr, "  -Q MAIN_QUALITY\t\tMain image quality\n");
600   fprintf(stderr, "  -q TMB_QUALITY\t\tThumbnail image quality\n");
601   fprintf(stderr, "  -B \t\tBurst mode. Utilize both encoder engines on"
602           "supported targets\n");
603   fprintf(stderr, "  -M \t\tUse minimum number of output buffers \n");
604   fprintf(stderr, "\n");
605 }
606 
607 /** main:
608  *
609  *  Arguments:
610  *    @argc
611  *    @argv
612  *
613  *  Return:
614  *       0 or -ve values
615  *
616  *  Description:
617  *       main function
618  *
619  **/
main(int argc,char * argv[])620 int main(int argc, char* argv[])
621 {
622   jpeg_test_input_t *p_test_input;
623   int ret = 0;
624   if (argc > 1) {
625     p_test_input = calloc(2, sizeof(*p_test_input));
626     if (!p_test_input) {
627       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
628       goto exit;
629     }
630     memcpy(p_test_input, &jpeg_input[0], sizeof(*p_test_input));
631     ret = mm_jpeg_test_get_input(argc, argv, p_test_input);
632     if (ret) {
633       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
634       goto exit;
635     }
636   } else {
637     mm_jpeg_test_print_usage();
638     return 1;
639   }
640   ret = encode_test(p_test_input);
641 
642 exit:
643   if (!ret) {
644     fprintf(stderr, "%-25s\n", "Success!");
645   } else {
646     fprintf(stderr, "%-25s\n", "Fail!");
647   }
648 
649   if (argc > 1) {
650     if (p_test_input) {
651       free(p_test_input);
652       p_test_input = NULL;
653     }
654   }
655 
656   return ret;
657 }
658 
659 
660