1 /* Copyright (c) 2013, 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 MIN(a,b) (((a) < (b)) ? (a) : (b))
36 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
37 #define CLAMP(x, min, max) x = MIN(MAX((x), (min)), (max))
38
39 #define TIME_IN_US(r) ((uint64_t)r.tv_sec * 1000000LL + r.tv_usec)
40 struct timeval dtime[2];
41
42
43 /** DUMP_TO_FILE:
44 * @filename: file name
45 * @p_addr: address of the buffer
46 * @len: buffer length
47 *
48 * dump the image to the file
49 **/
50 #define DUMP_TO_FILE(filename, p_addr, len) ({ \
51 FILE *fp = fopen(filename, "w+"); \
52 if (fp) { \
53 fwrite(p_addr, 1, len, fp); \
54 fclose(fp); \
55 } else { \
56 CDBG_ERROR("%s:%d] cannot dump image", __func__, __LINE__); \
57 } \
58 })
59
60 static int g_count = 1, g_i;
61
62 typedef struct {
63 char *filename;
64 int width;
65 int height;
66 char *out_filename;
67 int format;
68 } jpeg_test_input_t;
69
70 typedef struct {
71 char *filename;
72 int width;
73 int height;
74 char *out_filename;
75 pthread_mutex_t lock;
76 pthread_cond_t cond;
77 buffer_t input;
78 buffer_t output;
79 int use_ion;
80 uint32_t handle;
81 mm_jpegdec_ops_t ops;
82 uint32_t job_id[5];
83 mm_jpeg_decode_params_t params;
84 mm_jpeg_job_t job;
85 uint32_t session_id;
86 } mm_jpegdec_intf_test_t;
87
88 typedef struct {
89 char *format_str;
90 int eColorFormat;
91 } mm_jpegdec_col_fmt_t;
92
93 #define ARR_SZ(a) (sizeof(a)/sizeof(a[0]))
94
95 static const mm_jpegdec_col_fmt_t col_formats[] =
96 {
97 { "YCRCBLP_H2V2", (int)MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2 },
98 { "YCBCRLP_H2V2", (int)MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2 },
99 { "YCRCBLP_H2V1", (int)MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1 },
100 { "YCBCRLP_H2V1", (int)MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1 },
101 { "YCRCBLP_H1V2", (int)MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2 },
102 { "YCBCRLP_H1V2", (int)MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2 },
103 { "YCRCBLP_H1V1", (int)MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1 },
104 { "YCBCRLP_H1V1", (int)MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1 }
105 };
106
mm_jpegdec_decode_callback(jpeg_job_status_t status,uint32_t client_hdl,uint32_t jobId,mm_jpeg_output_t * p_output,void * userData)107 static void mm_jpegdec_decode_callback(jpeg_job_status_t status,
108 uint32_t client_hdl,
109 uint32_t jobId,
110 mm_jpeg_output_t *p_output,
111 void *userData)
112 {
113 mm_jpegdec_intf_test_t *p_obj = (mm_jpegdec_intf_test_t *)userData;
114
115 if (status == JPEG_JOB_STATUS_ERROR) {
116 CDBG_ERROR("%s:%d] Decode error", __func__, __LINE__);
117 } else {
118 gettimeofday(&dtime[1], NULL);
119 CDBG_ERROR("%s:%d] Decode time %llu ms",
120 __func__, __LINE__, ((TIME_IN_US(dtime[1]) - TIME_IN_US(dtime[0]))/1000));
121
122 CDBG_ERROR("%s:%d] Decode success file%s addr %p len %d",
123 __func__, __LINE__, p_obj->out_filename,
124 p_output->buf_vaddr, p_output->buf_filled_len);
125 DUMP_TO_FILE(p_obj->out_filename, p_output->buf_vaddr, p_output->buf_filled_len);
126 }
127 g_i++;
128 if (g_i >= g_count) {
129 CDBG_ERROR("%s:%d] Signal the thread", __func__, __LINE__);
130 pthread_cond_signal(&p_obj->cond);
131 }
132 }
133
mm_jpegdec_test_alloc(buffer_t * p_buffer,int use_pmem)134 int mm_jpegdec_test_alloc(buffer_t *p_buffer, int use_pmem)
135 {
136 int ret = 0;
137 /*Allocate buffers*/
138 if (use_pmem) {
139 p_buffer->addr = (uint8_t *)buffer_allocate(p_buffer, 0);
140 if (NULL == p_buffer->addr) {
141 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
142 return -1;
143 }
144 } else {
145 /* Allocate heap memory */
146 p_buffer->addr = (uint8_t *)malloc(p_buffer->size);
147 if (NULL == p_buffer->addr) {
148 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
149 return -1;
150 }
151 }
152 return ret;
153 }
154
mm_jpegdec_test_free(buffer_t * p_buffer)155 void mm_jpegdec_test_free(buffer_t *p_buffer)
156 {
157 if (p_buffer->addr == NULL)
158 return;
159
160 if (p_buffer->p_pmem_fd > 0)
161 buffer_deallocate(p_buffer);
162 else
163 free(p_buffer->addr);
164
165 memset(p_buffer, 0x0, sizeof(buffer_t));
166 }
167
mm_jpegdec_test_read(mm_jpegdec_intf_test_t * p_obj)168 int mm_jpegdec_test_read(mm_jpegdec_intf_test_t *p_obj)
169 {
170 int rc = 0;
171 FILE *fp = NULL;
172 int file_size = 0;
173 fp = fopen(p_obj->filename, "rb");
174 if (!fp) {
175 CDBG_ERROR("%s:%d] error", __func__, __LINE__);
176 return -1;
177 }
178 fseek(fp, 0, SEEK_END);
179 file_size = ftell(fp);
180 fseek(fp, 0, SEEK_SET);
181
182 CDBG_ERROR("%s:%d] input file size is %d",
183 __func__, __LINE__, file_size);
184
185 p_obj->input.size = file_size;
186
187 /* allocate buffers */
188 rc = mm_jpegdec_test_alloc(&p_obj->input, p_obj->use_ion);
189 if (rc) {
190 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
191 return -1;
192 }
193
194 fread(p_obj->input.addr, 1, p_obj->input.size, fp);
195 fclose(fp);
196 return 0;
197 }
198
chromaScale(mm_jpeg_color_format format,float * cScale)199 void chromaScale(mm_jpeg_color_format format, float *cScale)
200 {
201 float scale;
202
203 switch(format) {
204 case MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V2:
205 case MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V2:
206 scale = 1.5;
207 break;
208 case MM_JPEG_COLOR_FORMAT_YCRCBLP_H2V1:
209 case MM_JPEG_COLOR_FORMAT_YCBCRLP_H2V1:
210 case MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V2:
211 case MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V2:
212 scale = 2.0;
213 break;
214 case MM_JPEG_COLOR_FORMAT_YCRCBLP_H1V1:
215 case MM_JPEG_COLOR_FORMAT_YCBCRLP_H1V1:
216 scale = 3.0;
217 break;
218 case MM_JPEG_COLOR_FORMAT_MONOCHROME:
219 scale = 1.0;
220 break;
221 default:
222 scale = 0;
223 CDBG_ERROR("%s:%d] color format Error",__func__, __LINE__);
224 }
225
226 *cScale = scale;
227 }
228
decode_init(jpeg_test_input_t * p_input,mm_jpegdec_intf_test_t * p_obj)229 static int decode_init(jpeg_test_input_t *p_input, mm_jpegdec_intf_test_t *p_obj)
230 {
231 int rc = -1;
232 int size = CEILING16(p_input->width) * CEILING16(p_input->height);
233 float cScale;
234 mm_jpeg_decode_params_t *p_params = &p_obj->params;
235 mm_jpeg_decode_job_t *p_job_params = &p_obj->job.decode_job;
236
237 p_obj->filename = p_input->filename;
238 p_obj->width = p_input->width;
239 p_obj->height = p_input->height;
240 p_obj->out_filename = p_input->out_filename;
241 p_obj->use_ion = 1;
242
243 pthread_mutex_init(&p_obj->lock, NULL);
244 pthread_cond_init(&p_obj->cond, NULL);
245
246 chromaScale(p_input->format, &cScale);
247 p_obj->output.size = size * cScale;
248 rc = mm_jpegdec_test_alloc(&p_obj->output, p_obj->use_ion);
249 if (rc) {
250 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
251 return -1;
252 }
253
254 rc = mm_jpegdec_test_read(p_obj);
255 if (rc) {
256 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
257 return -1;
258 }
259
260 /* set encode parameters */
261 p_params->jpeg_cb = mm_jpegdec_decode_callback;
262 p_params->userdata = p_obj;
263 p_params->color_format = p_input->format;
264
265 /* dest buffer config */
266 p_params->dest_buf[0].buf_size = p_obj->output.size;
267 p_params->dest_buf[0].buf_vaddr = p_obj->output.addr;
268 p_params->dest_buf[0].fd = p_obj->output.p_pmem_fd;
269 p_params->dest_buf[0].format = MM_JPEG_FMT_YUV;
270 p_params->dest_buf[0].offset.mp[0].len = size;
271 p_params->dest_buf[0].offset.mp[1].len = size * (cScale-1.0);
272 p_params->dest_buf[0].offset.mp[0].stride = CEILING16(p_input->width);
273 p_params->dest_buf[0].offset.mp[0].scanline = CEILING16(p_input->height);
274 p_params->dest_buf[0].offset.mp[1].stride = CEILING16(p_input->width);
275 p_params->dest_buf[0].offset.mp[1].scanline = CEILING16(p_input->height);
276 p_params->dest_buf[0].index = 0;
277 p_params->num_dst_bufs = 1;
278
279 /* src buffer config*/
280 p_params->src_main_buf[0].buf_size = p_obj->input.size;
281 p_params->src_main_buf[0].buf_vaddr = p_obj->input.addr;
282 p_params->src_main_buf[0].fd = p_obj->input.p_pmem_fd;
283 p_params->src_main_buf[0].index = 0;
284 p_params->src_main_buf[0].format = MM_JPEG_FMT_BITSTREAM;
285 /*
286 p_params->src_main_buf[0].offset.mp[0].len = size;
287 p_params->src_main_buf[0].offset.mp[1].len = size >> 1;
288 */
289 p_params->num_src_bufs = 1;
290
291 p_job_params->dst_index = 0;
292 p_job_params->src_index = 0;
293 p_job_params->rotation = 0;
294
295 /* main dimension */
296 p_job_params->main_dim.src_dim.width = p_obj->width;
297 p_job_params->main_dim.src_dim.height = p_obj->height;
298 p_job_params->main_dim.dst_dim.width = p_obj->width;
299 p_job_params->main_dim.dst_dim.height = p_obj->height;
300 p_job_params->main_dim.crop.top = 0;
301 p_job_params->main_dim.crop.left = 0;
302 p_job_params->main_dim.crop.width = p_obj->width;
303 p_job_params->main_dim.crop.height = p_obj->height;
304
305
306 return 0;
307 }
308
omx_test_dec_print_usage()309 void omx_test_dec_print_usage()
310 {
311 fprintf(stderr, "Usage: program_name [options]\n");
312 fprintf(stderr, "Mandatory options:\n");
313 fprintf(stderr, " -I FILE\t\tPath to the input file.\n");
314 fprintf(stderr, " -O FILE\t\tPath for the output file.\n");
315 fprintf(stderr, " -W WIDTH\t\tOutput image width\n");
316 fprintf(stderr, " -H HEIGHT\t\tOutput image height\n");
317 fprintf(stderr, "Optional:\n");
318 fprintf(stderr, " -F FORMAT\t\tDefault image format:\n");
319 fprintf(stderr, "\t\t\t\t%s (0), %s (1), %s (2) %s (3)\n"
320 "%s (4), %s (5), %s (6) %s (7)\n",
321 col_formats[0].format_str, col_formats[1].format_str,
322 col_formats[2].format_str, col_formats[3].format_str,
323 col_formats[4].format_str, col_formats[5].format_str,
324 col_formats[6].format_str, col_formats[7].format_str
325 );
326
327 fprintf(stderr, "\n");
328 }
329
mm_jpegdec_test_get_input(int argc,char * argv[],jpeg_test_input_t * p_test)330 static int mm_jpegdec_test_get_input(int argc, char *argv[],
331 jpeg_test_input_t *p_test)
332 {
333 int c;
334
335 while ((c = getopt(argc, argv, "I:O:W:H:F:")) != -1) {
336 switch (c) {
337 case 'O':
338 p_test->out_filename = optarg;
339 fprintf(stderr, "%-25s%s\n", "Output image path",
340 p_test->out_filename);
341 break;
342 case 'I':
343 p_test->filename = optarg;
344 fprintf(stderr, "%-25s%s\n", "Input image path", p_test->filename);
345 break;
346 case 'W':
347 p_test->width = atoi(optarg);
348 fprintf(stderr, "%-25s%d\n", "Default width", p_test->width);
349 break;
350 case 'H':
351 p_test->height = atoi(optarg);
352 fprintf(stderr, "%-25s%d\n", "Default height", p_test->height);
353 break;
354 case 'F': {
355 int format = 0;
356 format = atoi(optarg);
357 int num_formats = ARR_SZ(col_formats);
358 CLAMP(format, 0, num_formats);
359 p_test->format = col_formats[format].eColorFormat;
360 fprintf(stderr, "%-25s%s\n", "Default image format",
361 col_formats[format].format_str);
362 break;
363 }
364 default:;
365 }
366 }
367 if (!p_test->filename || !p_test->filename || !p_test->width ||
368 !p_test->height) {
369 fprintf(stderr, "Missing required arguments.\n");
370 omx_test_dec_print_usage();
371 return -1;
372 }
373 return 0;
374 }
375
decode_test(jpeg_test_input_t * p_input)376 static int decode_test(jpeg_test_input_t *p_input)
377 {
378 int rc = 0;
379 mm_jpegdec_intf_test_t jpeg_obj;
380 int i = 0;
381
382 memset(&jpeg_obj, 0x0, sizeof(jpeg_obj));
383 rc = decode_init(p_input, &jpeg_obj);
384 if (rc) {
385 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
386 return -1;
387 }
388
389 jpeg_obj.handle = jpegdec_open(&jpeg_obj.ops);
390 if (jpeg_obj.handle == 0) {
391 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
392 goto end;
393 }
394
395 rc = jpeg_obj.ops.create_session(jpeg_obj.handle, &jpeg_obj.params,
396 &jpeg_obj.job.decode_job.session_id);
397 if (jpeg_obj.job.decode_job.session_id == 0) {
398 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
399 goto end;
400 }
401
402 for (i = 0; i < g_count; i++) {
403 jpeg_obj.job.job_type = JPEG_JOB_TYPE_DECODE;
404
405 CDBG_ERROR("%s:%d] Starting decode job",__func__, __LINE__);
406 gettimeofday(&dtime[0], NULL);
407
408 fprintf(stderr, "Starting decode of %s into %s outw %d outh %d\n\n",
409 p_input->filename, p_input->out_filename,
410 p_input->width, p_input->height);
411 rc = jpeg_obj.ops.start_job(&jpeg_obj.job, &jpeg_obj.job_id[i]);
412 if (rc) {
413 CDBG_ERROR("%s:%d] Error",__func__, __LINE__);
414 goto end;
415 }
416 }
417
418 /*
419 usleep(5);
420 jpeg_obj.ops.abort_job(jpeg_obj.job_id[0]);
421 */
422 pthread_mutex_lock(&jpeg_obj.lock);
423 pthread_cond_wait(&jpeg_obj.cond, &jpeg_obj.lock);
424 pthread_mutex_unlock(&jpeg_obj.lock);
425
426 fprintf(stderr, "Decode time %llu ms\n",
427 ((TIME_IN_US(dtime[1]) - TIME_IN_US(dtime[0]))/1000));
428
429
430 jpeg_obj.ops.destroy_session(jpeg_obj.job.decode_job.session_id);
431
432 jpeg_obj.ops.close(jpeg_obj.handle);
433
434
435 end:
436 mm_jpegdec_test_free(&jpeg_obj.input);
437 mm_jpegdec_test_free(&jpeg_obj.output);
438 return 0;
439 }
440
441 /** main:
442 *
443 * Arguments:
444 * @argc
445 * @argv
446 *
447 * Return:
448 * 0 or -ve values
449 *
450 * Description:
451 * main function
452 *
453 **/
main(int argc,char * argv[])454 int main(int argc, char* argv[])
455 {
456 jpeg_test_input_t dec_test_input;
457 int ret;
458
459 memset(&dec_test_input, 0, sizeof(dec_test_input));
460 ret = mm_jpegdec_test_get_input(argc, argv, &dec_test_input);
461
462 if (ret) {
463 return -1;
464 }
465
466 return decode_test(&dec_test_input);
467 }
468
469
470