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