Plugin JPEG decoder for data in memory, along with test_mem_jpeg.c and bench_mem_jpeg...
[kugel-rb.git] / apps / plugins / bench_mem_jpeg.c
blob958e5208e84cc9fa1790588ce07bdb851f169eb3
1 /*****************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// __ \_/ ___\| |/ /| __ \ / __ \ \/ /
5 * Jukebox | | ( (__) ) \___| ( | \_\ ( (__) ) (
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 Andrew Mahone
12 * In-memory JPEG decode benchmark.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #include "plugin.h"
25 #include "lib/jpeg_mem.h"
26 PLUGIN_HEADER
28 /* a null output plugin to save memory and better isolate decode cost */
29 static unsigned int get_size_null(struct bitmap *bm)
31 (void) bm;
32 return 1;
35 static void output_row_null(uint32_t row, void * row_in,
36 struct scaler_context *ctx)
38 (void) row;
39 (void) row_in;
40 (void) ctx;
41 return;
44 const struct custom_format format_null = {
45 #ifdef HAVE_LCD_COLOR
46 .output_row = {
47 output_row_null,
48 output_row_null
50 #else
51 .output_row = output_row_null,
52 #endif
53 .get_size = get_size_null
56 static char output_buf[256];
57 static int output_y = 0;
58 static int font_h;
60 #define lcd_printf(...) \
61 do { \
62 rb->snprintf(output_buf, sizeof(output_buf), __VA_ARGS__); \
63 rb->lcd_putsxy(0, output_y, output_buf); \
64 rb->lcd_update_rect(0, output_y, LCD_WIDTH, font_h); \
65 output_y += font_h; \
66 } while (0)
68 /* this is the plugin entry point */
69 enum plugin_status plugin_start(const void* parameter)
71 size_t plugin_buf_len;
72 unsigned char * plugin_buf =
73 (unsigned char *)rb->plugin_get_buffer(&plugin_buf_len);
74 static char filename[MAX_PATH];
75 struct bitmap bm = {
76 .width = LCD_WIDTH,
77 .height = LCD_HEIGHT,
79 int ret;
81 if(!parameter) return PLUGIN_ERROR;
83 rb->strcpy(filename, parameter);
84 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
85 rb->lcd_fillrect(0, 0, LCD_WIDTH, LCD_HEIGHT);
86 rb->lcd_set_drawmode(DRMODE_SOLID);
87 rb->lcd_getstringsize("A", NULL, &font_h);
88 int fd = rb->open(filename, O_RDONLY);
89 if (fd < 0)
91 lcd_printf("file open failed: %d", fd);
92 goto wait;
94 unsigned long filesize = rb->filesize(fd);
95 if (filesize > plugin_buf_len)
97 lcd_printf("file too large");
98 goto wait;
100 plugin_buf_len -= filesize;
101 unsigned char *jpeg_buf = plugin_buf;
102 plugin_buf += filesize;
103 rb->read(fd, jpeg_buf, filesize);
104 rb->close(fd);
105 bm.data = plugin_buf;
106 struct dim jpeg_size;
107 get_jpeg_dim_mem(jpeg_buf, filesize, &jpeg_size);
108 lcd_printf("jpeg file size: %dx%d",jpeg_size.width, jpeg_size.height);
109 bm.width = jpeg_size.width;
110 bm.height = jpeg_size.height;
111 char *size_str[] = { "1/1", "1/2", "1/4", "1/8" };
112 int i;
113 for (i = 0; i < 4; i++)
115 lcd_printf("timing %s decode", size_str[i]);
116 ret = decode_jpeg_mem(jpeg_buf, filesize, &bm, plugin_buf_len,
117 FORMAT_NATIVE|FORMAT_RESIZE|FORMAT_KEEP_ASPECT,
118 &format_null);
119 if (ret == 1)
121 long t1, t2;
122 int count = 0;
123 t2 = *(rb->current_tick);
124 while (t2 != (t1 = *(rb->current_tick)));
125 do {
126 decode_jpeg_mem(jpeg_buf, filesize, &bm, plugin_buf_len,
127 FORMAT_NATIVE|FORMAT_RESIZE|FORMAT_KEEP_ASPECT,
128 &format_null);
129 count++;
130 t2 = *(rb->current_tick);
131 } while (t2 - t1 < HZ || count < 10);
132 t2 -= t1;
133 t2 *= 10;
134 t2 += count >> 1;
135 t2 /= count;
136 t1 = t2 / 1000;
137 t2 -= t1 * 1000;
138 lcd_printf("%01d.%03d secs/decode", (int)t1, (int)t2);
139 bm.width >>= 1;
140 bm.height >>= 1;
141 if (!(bm.width && bm.height))
142 break;
143 } else
144 lcd_printf("insufficient memory");
147 wait:
148 while (rb->get_action(CONTEXT_STD,1) != ACTION_STD_OK) rb->yield();
149 return PLUGIN_OK;