/apps/codecs/liba52/

f='#n8'>8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 Peter D'Hoye
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "lib/helper.h"
#include "lib/grey.h"

#ifdef HAVE_LCD_BITMAP

PLUGIN_IRAM_DECLARE

#if (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
    (CONFIG_KEYPAD == IPOD_1G2G_PAD)
#define FPS_QUIT BUTTON_MENU
#elif CONFIG_KEYPAD == IAUDIO_M3_PAD
#define FPS_QUIT BUTTON_RC_REC
#elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
#define FPS_QUIT BUTTON_PLAY
#elif CONFIG_KEYPAD == SANSA_FUZE_PAD
#define FPS_QUIT (BUTTON_HOME|BUTTON_REPEAT)
#elif defined(BUTTON_OFF)
#define FPS_QUIT BUTTON_OFF
#else
#define FPS_QUIT BUTTON_POWER
#endif

#define DURATION (2*HZ) /* longer duration gives more precise results */

PLUGIN_HEADER

/* Screen logging */
static int line;
static int max_line;
#ifdef HAVE_REMOTE_LCD
static int remote_line;
static int remote_max_line;
#endif
#if LCD_DEPTH < 4
static unsigned char *gbuf;
static size_t gbuf_size;
#endif

static void log_init(void)
{
    int h;

    rb->lcd_getstringsize("A", NULL, &h);
    max_line = LCD_HEIGHT / h;
    line = 0;
    rb->lcd_clear_display();
    rb->lcd_update();
#ifdef HAVE_REMOTE_LCD
    rb->lcd_remote_getstringsize("A", NULL, &h);
    remote_max_line = LCD_REMOTE_HEIGHT / h;
    remote_line = 0;
    rb->lcd_remote_clear_display();
    rb->lcd_remote_update();
#endif
}

static void log_text(char *text)
{
    rb->lcd_puts(0, line, text);
    if (++line >= max_line)
        line = 0;
    rb->lcd_update();
#ifdef HAVE_REMOTE_LCD
    rb->lcd_remote_puts(0, remote_line, text);
    if (++remote_line >= remote_max_line)
        remote_line = 0;
    rb->lcd_remote_update();
#endif
}

static int calc_tenth_fps(int framecount, long ticks)
{
    return (10*HZ) * framecount / ticks;
}

static void time_main_update(void)
{
    char str[32];     /* text buffer */
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    int frame_count;
    int fps;

    const int part14_x = LCD_WIDTH/4;   /* x-offset for 1/4 update test */
    const int part14_w = LCD_WIDTH/2;   /* x-size for 1/4 update test */
    const int part14_y = LCD_HEIGHT/4;  /* y-offset for 1/4 update test */
    const int part14_h = LCD_HEIGHT/2;  /* y-size for 1/4 update test */

    /* Test 1: full LCD update */
    frame_count = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        rb->lcd_update();
        frame_count++;
    }
    fps = calc_tenth_fps(frame_count, time_end - time_start);
    rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
    log_text(str);

    /* Test 2: quarter LCD update */
    frame_count = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        rb->lcd_update_rect(part14_x, part14_y, part14_w, part14_h);
        frame_count++;
    }
    fps = calc_tenth_fps(frame_count, time_end - time_start);
    rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
    log_text(str);
}

#if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)

#if LCD_WIDTH >= LCD_HEIGHT
#define YUV_WIDTH LCD_WIDTH
#define YUV_HEIGHT LCD_HEIGHT
#else /* Assume the screen is rotated on portrait LCDs */
#define YUV_WIDTH LCD_HEIGHT
#define YUV_HEIGHT LCD_WIDTH
#endif

static unsigned char ydata[YUV_HEIGHT][YUV_WIDTH];
static unsigned char udata[YUV_HEIGHT/2][YUV_WIDTH/2];
static unsigned char vdata[YUV_HEIGHT/2][YUV_WIDTH/2];

static unsigned char * const yuvbuf[3] = {
    (void*)ydata,
    (void*)udata,
    (void*)vdata
};

static void make_gradient_rect(int width, int height)
{
    unsigned char vline[YUV_WIDTH/2];
    int x, y;

    width /= 2;
    height /= 2;

    for (x = 0; x < width; x++)
        vline[x] = (x << 8) / width;
    for (y = 0; y < height; y++)
    {
        rb->memset(udata[y], (y << 8) / height, width);
        rb->memcpy(vdata[y], vline, width);
    }
}

static void time_main_yuv(void)
{
    char str[32];     /* text buffer */
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    int frame_count;
    int fps;

    const int part14_x = YUV_WIDTH/4;   /* x-offset for 1/4 update test */
    const int part14_w = YUV_WIDTH/2;   /* x-size for 1/4 update test */
    const int part14_y = YUV_HEIGHT/4;  /* y-offset for 1/4 update test */
    const int part14_h = YUV_HEIGHT/2;  /* y-size for 1/4 update test */
    
    rb->memset(ydata, 128, sizeof(ydata)); /* medium grey */

    /* Test 1: full LCD update */
    make_gradient_rect(YUV_WIDTH, YUV_HEIGHT);

    frame_count = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
                         0, 0, YUV_WIDTH, YUV_HEIGHT);
        frame_count++;
    }
    fps = calc_tenth_fps(frame_count, time_end - time_start);
    rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
    log_text(str);

    /* Test 2: quarter LCD update */
    make_gradient_rect(YUV_WIDTH/2, YUV_HEIGHT/2);

    frame_count = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        rb->lcd_blit_yuv(yuvbuf, 0, 0, YUV_WIDTH,
                         part14_x, part14_y, part14_w, part14_h);
        frame_count++;
    }
    fps = calc_tenth_fps(frame_count, time_end - time_start);
    rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
    log_text(str);
}
#endif

#ifdef HAVE_REMOTE_LCD
static void time_remote_update(void)
{
    char str[32];     /* text buffer */
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    int frame_count;
    int fps;

    const int part14_x = LCD_REMOTE_WIDTH/4;   /* x-offset for 1/4 update test */
    const int part14_w = LCD_REMOTE_WIDTH/2;   /* x-size for 1/4 update test */
    const int part14_y = LCD_REMOTE_HEIGHT/4;  /* y-offset for 1/4 update test */
    const int part14_h = LCD_REMOTE_HEIGHT/2;  /* y-size for 1/4 update test */

    /* Test 1: full LCD update */
    frame_count = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        rb->lcd_remote_update();
        frame_count++;
    }
    fps = calc_tenth_fps(frame_count, time_end - time_start);
    rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
    log_text(str);

    /* Test 2: quarter LCD update */
    frame_count = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        rb->lcd_remote_update_rect(part14_x, part14_y, part14_w, part14_h);
        frame_count++;
    }
    fps = calc_tenth_fps(frame_count, time_end - time_start);
    rb->snprintf(str, sizeof(str), "1/4: %d.%d fps", fps / 10, fps % 10);
    log_text(str);
}
#endif

#if LCD_DEPTH < 4

GREY_INFO_STRUCT_IRAM
static unsigned char greydata[LCD_HEIGHT][LCD_WIDTH];

static void make_grey_rect(int width, int height)
{
    unsigned char vline[LCD_WIDTH];
    int x, y;

    for (x = 0; x < width; x++)
        vline[x] = (x << 8) / width;
    for (y = 0; y < height; y++)
        rb->memcpy(greydata[y], vline, width);
}

static void time_greyscale(void)
{
    char str[32];     /* text buffer */
    long time_start;  /* start tickcount */
    long time_end;    /* end tickcount */
    long time_1, time_2;
    int frames_1, frames_2;
    int fps, load;

    gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size);
    if (!grey_init(gbuf, gbuf_size, GREY_ON_COP,
                   LCD_WIDTH, LCD_HEIGHT, NULL))
    {
        log_text("greylib: out of memory.");
        return;
    }
    make_grey_rect(LCD_WIDTH, LCD_HEIGHT);

    /* Test 1 - greyscale overlay not yet enabled */
    frames_1 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
        frames_1++;
    }
    time_1 = time_end - time_start;
    
    /* Test 2 - greyscale overlay enabled */
    grey_show(true);
    frames_2 = 0;
    rb->sleep(0); /* sync to tick */
    time_start = *rb->current_tick;
    while((time_end = *rb->current_tick) - time_start < DURATION)
    {
        grey_ub_gray_bitmap(greydata[0], 0, 0, LCD_WIDTH, LCD_HEIGHT);
        frames_2++;
    }
    time_2 = time_end - time_start;

    grey_release();
    fps = calc_tenth_fps(frames_2, time_2);
    load = 100 - (100 * frames_2 * time_1) / (frames_1 * time_2);
    rb->snprintf(str, sizeof(str), "1/1: %d.%d fps", fps / 10, fps % 10);
    log_text(str);

    if (load > 0 && load < 100)
    {
        rb->snprintf(str, sizeof(str), "CPU load: %d%%", load);
        log_text(str);
    }
    else
        log_text("CPU load err (boost?)");
}
#endif

/* plugin entry point */
enum plugin_status plugin_start(const void* parameter)
{
#ifndef SIMULATOR
    char str[32];
    int cpu_freq;
#endif

    /* standard stuff */
    PLUGIN_IRAM_INIT(rb)
    (void)parameter;
    
    log_init();
#ifndef SIMULATOR
    cpu_freq = *rb->cpu_frequency; /* remember CPU frequency */
#endif
    backlight_force_on(); /* backlight control in lib/helper.c */

    log_text("Main LCD Update");
    time_main_update();
#if defined(HAVE_LCD_COLOR) && (MEMORYSIZE > 2)
    log_text("Main LCD YUV");
    time_main_yuv();
#endif
#if LCD_DEPTH < 4
    log_text("Greyscale library");
    time_greyscale();
#endif
#ifdef HAVE_REMOTE_LCD
    log_text("Remote LCD Update");
    time_remote_update();
#endif

#ifndef SIMULATOR
    if (*rb->cpu_frequency != cpu_freq)
        rb->snprintf(str, sizeof(str), "CPU clock changed!");
    else
        rb->snprintf(str, sizeof(str), "CPU: %d MHz",
                     (cpu_freq + 500000) / 1000000);
    log_text(str);
#endif
    backlight_use_settings(); /* backlight control in lib/helper.c */

    /* wait until user closes plugin */
    while (rb->button_get(true) != FPS_QUIT);

    return PLUGIN_OK;
}
#endif