1 /* Copyright (c) 2013-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 #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 int g_count = 1, g_i;
55 
56 typedef struct {
57   mm_jpeg_color_format fmt;
58   float 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   int burst_mode;
68   int min_out_bufs;
69   mm_jpeg_intf_test_colfmt_t col_fmt;
70   int 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   int min_out_bufs;
120   uint32_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, 1.5, "YCRCBLP_H2V2" },
128   { MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2, 1.5, "YCBCRLP_H2V2" },
129   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1, 2.0, "YCRCBLP_H2V1" },
130   { MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1, 2.0, "YCBCRLP_H2V1" },
131   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2, 2.0, "YCRCBLP_H1V2" },
132   { MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2, 2.0, "YCBCRLP_H1V2" },
133   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1, 3.0, "YCRCBLP_H1V1" },
134   { MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1, 3.0, "YCBCRLP_H1V1" }
135 };
136 
137 static jpeg_test_input_t jpeg_input[] = {
138   {"/data/test_1.yuv", 4000, 3008, "/data/test_1.jpg", 0, 0,
139   { MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2, 1.5, "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 %d 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 %d",
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,int idx)220 int mm_jpeg_test_read(mm_jpeg_intf_test_t *p_obj, int idx)
221 {
222   FILE *fp = NULL;
223   int 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 = ftell(fp);
231   fseek(fp, 0, SEEK_SET);
232   CDBG_ERROR("%s:%d] input file size is %d buf_size %ld",
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   int size = 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   int i = 0;
252   int burst_mode = p_input->burst_mode;
253   jpeg_test_input_t *p_in = p_input;
254   do {
255     p_obj->filename[i] = p_in->filename;
256     p_obj->width = p_input->width;
257     p_obj->height = p_input->height;
258     p_obj->out_filename[i] = p_in->out_filename;
259     p_obj->use_ion = 1;
260     p_obj->min_out_bufs = p_input->min_out_bufs;
261 
262     /* allocate buffers */
263     p_obj->input[i].size = size * p_input->col_fmt.mult;
264     rc = mm_jpeg_test_alloc(&p_obj->input[i], p_obj->use_ion);
265     if (rc) {
266       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
267       return -1;
268     }
269 
270 
271     rc = mm_jpeg_test_read(p_obj, i);
272     if (rc) {
273       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
274       return -1;
275     }
276 
277     /* src buffer config*/
278     p_params->src_main_buf[i].buf_size = p_obj->input[i].size;
279     p_params->src_main_buf[i].buf_vaddr = p_obj->input[i].addr;
280     p_params->src_main_buf[i].fd = p_obj->input[i].p_pmem_fd;
281     p_params->src_main_buf[i].index = i;
282     p_params->src_main_buf[i].format = MM_JPEG_FMT_YUV;
283     p_params->src_main_buf[i].offset.mp[0].len = size;
284     p_params->src_main_buf[i].offset.mp[0].stride = p_input->width;
285     p_params->src_main_buf[i].offset.mp[0].scanline = p_input->height;
286     p_params->src_main_buf[i].offset.mp[1].len = size >> 1;
287 
288     /* src buffer config*/
289     p_params->src_thumb_buf[i].buf_size = p_obj->input[i].size;
290     p_params->src_thumb_buf[i].buf_vaddr = p_obj->input[i].addr;
291     p_params->src_thumb_buf[i].fd = p_obj->input[i].p_pmem_fd;
292     p_params->src_thumb_buf[i].index = i;
293     p_params->src_thumb_buf[i].format = MM_JPEG_FMT_YUV;
294     p_params->src_thumb_buf[i].offset.mp[0].len = size;
295     p_params->src_thumb_buf[i].offset.mp[0].stride = p_input->width;
296     p_params->src_thumb_buf[i].offset.mp[0].scanline = p_input->height;
297     p_params->src_thumb_buf[i].offset.mp[1].len = size >> 1;
298 
299 
300     i++;
301   } while((++p_in)->filename);
302 
303   p_obj->num_bufs = i;
304 
305   pthread_mutex_init(&p_obj->lock, NULL);
306   pthread_cond_init(&p_obj->cond, NULL);
307 
308 
309   /* set encode parameters */
310   p_params->jpeg_cb = mm_jpeg_encode_callback;
311   p_params->userdata = p_obj;
312   p_params->color_format = p_input->col_fmt.fmt;
313   p_params->thumb_color_format = p_input->col_fmt.fmt;
314 
315   if (p_obj->min_out_bufs) {
316     p_params->num_dst_bufs = 2;
317   } else {
318     p_params->num_dst_bufs = p_obj->num_bufs;
319   }
320 
321   for (i = 0; i < (int)p_params->num_dst_bufs; i++) {
322     p_obj->output[i].size = size * 3/2;
323     rc = mm_jpeg_test_alloc(&p_obj->output[i], 0);
324     if (rc) {
325       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
326       return -1;
327     }
328     /* dest buffer config */
329     p_params->dest_buf[i].buf_size = p_obj->output[i].size;
330     p_params->dest_buf[i].buf_vaddr = p_obj->output[i].addr;
331     p_params->dest_buf[i].fd = p_obj->output[i].p_pmem_fd;
332     p_params->dest_buf[i].index = i;
333   }
334 
335 
336   p_params->num_src_bufs = p_obj->num_bufs;
337   p_params->num_tmb_bufs = 0;
338   g_count = p_params->num_src_bufs;
339 
340   p_params->encode_thumbnail = p_input->encode_thumbnail;
341   if (p_params->encode_thumbnail) {
342       p_params->num_tmb_bufs = p_obj->num_bufs;
343   }
344   p_params->quality = p_input->main_quality;
345   p_params->thumb_quality = p_input->thumb_quality;
346 
347   p_job_params->dst_index = 0;
348   p_job_params->src_index = 0;
349   p_job_params->rotation = 0;
350 
351   /* main dimension */
352   p_job_params->main_dim.src_dim.width = p_obj->width;
353   p_job_params->main_dim.src_dim.height = p_obj->height;
354   p_job_params->main_dim.dst_dim.width = p_obj->width;
355   p_job_params->main_dim.dst_dim.height = p_obj->height;
356   p_job_params->main_dim.crop.top = 0;
357   p_job_params->main_dim.crop.left = 0;
358   p_job_params->main_dim.crop.width = p_obj->width;
359   p_job_params->main_dim.crop.height = p_obj->height;
360 
361   p_params->main_dim  = p_job_params->main_dim;
362 
363   /* thumb dimension */
364   p_job_params->thumb_dim.src_dim.width = p_obj->width;
365   p_job_params->thumb_dim.src_dim.height = p_obj->height;
366   p_job_params->thumb_dim.dst_dim.width = p_input->tmb_width;
367   p_job_params->thumb_dim.dst_dim.height = p_input->tmb_height;
368   p_job_params->thumb_dim.crop.top = 0;
369   p_job_params->thumb_dim.crop.left = 0;
370   p_job_params->thumb_dim.crop.width = 0;
371   p_job_params->thumb_dim.crop.height = 0;
372 
373   p_params->thumb_dim  = p_job_params->thumb_dim;
374 
375   p_job_params->exif_info.numOfEntries = 0;
376   p_params->burst_mode = burst_mode;
377 
378   /* Qtable */
379   p_job_params->qtable[0].eQuantizationTable =
380     OMX_IMAGE_QuantizationTableLuma;
381   p_job_params->qtable[1].eQuantizationTable =
382     OMX_IMAGE_QuantizationTableChroma;
383   p_job_params->qtable_set[0] = 1;
384   p_job_params->qtable_set[1] = 1;
385 
386   for (i = 0; i < QUANT_SIZE; i++) {
387     p_job_params->qtable[0].nQuantizationMatrix[i] = DEFAULT_QTABLE_0[i];
388     p_job_params->qtable[1].nQuantizationMatrix[i] = DEFAULT_QTABLE_1[i];
389   }
390 
391   return 0;
392 }
393 
encode_test(jpeg_test_input_t * p_input)394 static int encode_test(jpeg_test_input_t *p_input)
395 {
396   int rc = 0;
397   mm_jpeg_intf_test_t jpeg_obj;
398   unsigned int i = 0;
399 
400   memset(&jpeg_obj, 0x0, sizeof(jpeg_obj));
401   rc = encode_init(p_input, &jpeg_obj);
402   if (rc) {
403     CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
404     return -1;
405   }
406 
407   mm_dimension pic_size;
408   memset(&pic_size, 0, sizeof(mm_dimension));
409   pic_size.w = p_input->width;
410   pic_size.h = p_input->height;
411 
412   jpeg_obj.handle = jpeg_open(&jpeg_obj.ops, pic_size);
413   if (jpeg_obj.handle == 0) {
414     CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
415     goto end;
416   }
417 
418   rc = jpeg_obj.ops.create_session(jpeg_obj.handle, &jpeg_obj.params,
419     &jpeg_obj.job.encode_job.session_id);
420   if (jpeg_obj.job.encode_job.session_id == 0) {
421     CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
422     goto end;
423   }
424 
425   for (i = 0; i < jpeg_obj.num_bufs; i++) {
426     jpeg_obj.job.job_type = JPEG_JOB_TYPE_ENCODE;
427     jpeg_obj.job.encode_job.src_index = i;
428     jpeg_obj.job.encode_job.dst_index = i;
429     jpeg_obj.job.encode_job.thumb_index = i;
430 
431     if (jpeg_obj.params.burst_mode && jpeg_obj.min_out_bufs) {
432       jpeg_obj.job.encode_job.dst_index = -1;
433     }
434 
435     rc = jpeg_obj.ops.start_job(&jpeg_obj.job, &jpeg_obj.job_id[i]);
436 
437     if (rc) {
438       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
439       goto end;
440     }
441   }
442   jpeg_obj.job_id[i] = 0;
443 
444   /*
445   usleep(5);
446   jpeg_obj.ops.abort_job(jpeg_obj.job_id[0]);
447   */
448   pthread_mutex_lock(&jpeg_obj.lock);
449   pthread_cond_wait(&jpeg_obj.cond, &jpeg_obj.lock);
450   pthread_mutex_unlock(&jpeg_obj.lock);
451 
452 
453   jpeg_obj.ops.destroy_session(jpeg_obj.job.encode_job.session_id);
454   jpeg_obj.ops.close(jpeg_obj.handle);
455 
456 end:
457   for (i = 0; i < jpeg_obj.num_bufs; i++) {
458     if (!jpeg_obj.min_out_bufs) {
459       // Save output files
460       CDBG_ERROR("%s:%d] Saving file%s addr %p len %d",
461           __func__, __LINE__,jpeg_obj.out_filename[i],
462           jpeg_obj.output[i].addr, jpeg_obj.buf_filled_len[i]);
463 
464       DUMP_TO_FILE(jpeg_obj.out_filename[i], jpeg_obj.output[i].addr,
465         jpeg_obj.buf_filled_len[i]);
466     }
467     mm_jpeg_test_free(&jpeg_obj.input[i]);
468     mm_jpeg_test_free(&jpeg_obj.output[i]);
469   }
470   return 0;
471 }
472 
473 #define MAX_FILE_CNT (20)
mm_jpeg_test_get_input(int argc,char * argv[],jpeg_test_input_t * p_test)474 static int mm_jpeg_test_get_input(int argc, char *argv[],
475     jpeg_test_input_t *p_test)
476 {
477   int c, in_file_cnt = 0, out_file_cnt = 0, i;
478   int idx = 0;
479   jpeg_test_input_t *p_test_base = p_test;
480 
481   char *in_files[MAX_FILE_CNT];
482   char *out_files[MAX_FILE_CNT];
483 
484   while ((c = getopt(argc, argv, "-I:O:W:H:F:BTx:y:Q:q:")) != -1) {
485     switch (c) {
486     case 'B':
487       fprintf(stderr, "%-25s\n", "Using burst mode");
488       p_test->burst_mode = 1;
489       break;
490     case 'I':
491       for (idx = optind - 1; idx < argc; idx++) {
492         if (argv[idx][0] == '-') {
493           break;
494         }
495         in_files[in_file_cnt++] = argv[idx];
496       }
497       optind = idx -1;
498 
499       break;
500     case 'O':
501       for (idx = optind - 1; idx < argc; idx++) {
502         if (argv[idx][0] == '-') {
503           break;
504         }
505         out_files[out_file_cnt++] = argv[idx];
506       }
507       optind = idx -1;
508 
509       break;
510     case 'W':
511       p_test->width = atoi(optarg);
512       fprintf(stderr, "%-25s%d\n", "Width: ", p_test->width);
513       break;
514     case 'H':
515       p_test->height = atoi(optarg);
516       fprintf(stderr, "%-25s%d\n", "Height: ", p_test->height);
517       break;
518     case 'F':
519       p_test->col_fmt = color_formats[atoi(optarg)];
520       fprintf(stderr, "%-25s%s\n", "Format: ", p_test->col_fmt.str);
521       break;
522     case 'M':
523       p_test->min_out_bufs = 1;
524       fprintf(stderr, "%-25s\n", "Using minimum number of output buffers");
525       break;
526     case 'T':
527       p_test->encode_thumbnail = 1;
528       fprintf(stderr, "%-25s\n", "Encode thumbnail");
529       break;
530     case 'x':
531       p_test->tmb_width = atoi(optarg);
532       fprintf(stderr, "%-25s%d\n", "Tmb Width: ", p_test->tmb_width);
533       break;
534     case 'y':
535       p_test->tmb_height = atoi(optarg);
536       fprintf(stderr, "%-25s%d\n", "Tmb Height: ", p_test->tmb_height);
537       break;
538     case 'Q':
539       p_test->main_quality = atoi(optarg);
540       fprintf(stderr, "%-25s%d\n", "Main quality: ", p_test->main_quality);
541       break;
542     case 'q':
543       p_test->thumb_quality = atoi(optarg);
544       fprintf(stderr, "%-25s%d\n", "Thumb quality: ", p_test->thumb_quality);
545       break;
546     default:;
547     }
548   }
549   fprintf(stderr, "Infiles: %d Outfiles: %d\n", in_file_cnt, out_file_cnt);
550 
551   if (in_file_cnt > out_file_cnt) {
552     fprintf(stderr, "%-25s\n", "Insufficient number of output files!");
553     return 1;
554   }
555 
556   // Discard the extra out files
557   out_file_cnt = in_file_cnt;
558 
559   p_test = realloc(p_test, (in_file_cnt + 1) * sizeof(*p_test));
560   if (!p_test) {
561     CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
562     return 1;
563   }
564   memset(p_test+1, 0, (in_file_cnt) * sizeof(*p_test));
565 
566   for (i = 0; i < in_file_cnt; i++, p_test++) {
567     memcpy(p_test, p_test_base, sizeof(*p_test));
568     p_test->filename = in_files[i];
569     p_test->out_filename = out_files[i];
570     fprintf(stderr, "Inf: %s Outf: %s\n", in_files[i], out_files[i]);
571   }
572 
573 
574   return 0;
575 }
576 
mm_jpeg_test_print_usage()577 static void mm_jpeg_test_print_usage()
578 {
579   fprintf(stderr, "Usage: program_name [options]\n");
580   fprintf(stderr, "Mandatory options:\n");
581   fprintf(stderr, "  -I FILE1 [FILE2] [FILEN]\tList of input files\n");
582   fprintf(stderr, "  -O FILE1 [FILE2] [FILEN]\tList of output files\n");
583   fprintf(stderr, "  -W WIDTH\t\tOutput image width\n");
584   fprintf(stderr, "  -H HEIGHT\t\tOutput image height\n");
585   fprintf(stderr, "  -F \t\tColor format: \n");
586   fprintf(stderr, "\t\t\t\t%s (0), %s (1), %s (2) %s (3)\n"
587       "\t\t\t\t%s (4), %s (5), %s (6) %s (7)\n ",
588       color_formats[0].str, color_formats[1].str,
589       color_formats[2].str, color_formats[3].str,
590       color_formats[4].str, color_formats[5].str,
591       color_formats[6].str, color_formats[7].str);
592   fprintf(stderr, "Optional:\n");
593   fprintf(stderr, "  -T \t\Encode thumbnail\n");
594   fprintf(stderr, "  -x TMB_WIDTH\t\tThumbnail width\n");
595   fprintf(stderr, "  -y TMB_HEIGHT\t\tThumbnail height\n");
596   fprintf(stderr, "  -Q MAIN_QUALITY\t\tMain image quality\n");
597   fprintf(stderr, "  -q TMB_QUALITY\t\tThumbnail image quality\n");
598   fprintf(stderr, "  -B \t\tBurst mode. Utilize both encoder engines on"
599           "supported targets\n");
600   fprintf(stderr, "  -M \t\tUse minimum number of output buffers \n");
601   fprintf(stderr, "\n");
602 }
603 
604 /** main:
605  *
606  *  Arguments:
607  *    @argc
608  *    @argv
609  *
610  *  Return:
611  *       0 or -ve values
612  *
613  *  Description:
614  *       main function
615  *
616  **/
main(int argc,char * argv[])617 int main(int argc, char* argv[])
618 {
619   jpeg_test_input_t *p_test_input;
620   int ret = 0;
621   if (argc > 1) {
622     p_test_input = calloc(2, sizeof(*p_test_input));
623     if (!p_test_input) {
624       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
625       goto exit;
626     }
627     memcpy(p_test_input, &jpeg_input[0], sizeof(*p_test_input));
628     ret = mm_jpeg_test_get_input(argc, argv, p_test_input);
629     if (ret) {
630       CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
631       goto exit;
632     }
633   } else {
634     mm_jpeg_test_print_usage();
635     return 1;
636   }
637   ret = encode_test(p_test_input);
638 
639 exit:
640   if (!ret) {
641     fprintf(stderr, "%-25s\n", "Success!");
642   } else {
643     fprintf(stderr, "%-25s\n", "Fail!");
644   }
645 
646   if (argc > 1) {
647     if (p_test_input) {
648       free(p_test_input);
649       p_test_input = NULL;
650     }
651   }
652 
653   return ret;
654 }
655 
656 
657