mlv_play.c 88.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 
 */

/*
 * Copyright (C) 2013 Magic Lantern Team
 * 
 * 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 program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include <module.h>
#include <dryos.h>
#include <property.h>
#include <bmp.h>
g3gg0's avatar
g3gg0 committed
29
#include <beep.h>
30
31
32
33
34
#include <menu.h>
#include <config.h>
#include <cropmarks.h>
#include <edmac.h>
#include <raw.h>
g3gg0's avatar
g3gg0 committed
35
#include <zebra.h>
36
#include <util.h>
37
#include <timer.h>
38
#include <shoot.h>
39

40
41
#include <string.h>

42
43
44
45
46
#include "../ime_base/ime_base.h"
#include "../trace/trace.h"
#include "../mlv_rec/mlv.h"
#include "../file_man/file_man.h"
#include "../lv_rec/lv_rec.h"
47
#include "../raw_twk/raw_twk.h"
48
#include "../silent/lossless.h"
49
#include "console.h"
50

51
52
53
/* uncomment for live debug messages */
//~ #define trace_write(trace, fmt, ...) { printf(fmt, ## __VA_ARGS__); printf("\n"); msleep(500); }

54
55
#define MAX_PATH                 100

56
57
58
59
/* declare the video.bmp we have added */
/* icon from: http://www.softicons.com/free-icons/system-icons/touch-of-gold-icons-by-skingcito/video-black-icon */
EXTLD(video_bmp);

60

61
/* only works if the raw_rec/mlv_rec module has its config variable non-static */
62
63
64
static int video_enabled_dummy = 0;
extern int WEAK_FUNC(video_enabled_dummy) raw_video_enabled;
extern int WEAK_FUNC(video_enabled_dummy) mlv_video_enabled;
65

66
67
static char *movie_filename_dummy = "";
extern char *WEAK_FUNC(movie_filename_dummy) raw_movie_filename;
68
extern char *WEAK_FUNC(movie_filename_dummy) mlv_movie_filename;
69
70
71
72

static int raw_exposure_adjust_dummy = 0;
extern int WEAK_FUNC(raw_exposure_adjust_dummy) raw_exposure_adjust;

73
74
static int res_x = 0;
static int res_y = 0;
75
static int frame_count = 0;
76
static int frame_size = 0;
77
static int bits_per_pixel = 0;
78
static unsigned int fps1000 = 0; /* used for RAW playback */
79

80
81
static volatile uint32_t mlv_play_render_abort = 0;
static volatile uint32_t mlv_play_rendering = 0;
82
static volatile uint32_t mlv_play_stopfile = 0;
83

84
static CONFIG_INT("play.quality", mlv_play_quality, 0); /* range: 0-1, RAW_PREVIEW_* in raw.h  */
85
static CONFIG_INT("play.exact_fps", mlv_play_exact_fps, 0);
86

87
88
89
static int mlv_play_zoom = 0;
static int mlv_play_zoom_x_pct = 0;
static int mlv_play_zoom_y_pct = 0;
90

91
92
static uint32_t mlv_play_trace_ctx = TRACE_ERROR;

93
94
95
96
97
98
99
100
/* OSD menu items */
#define MLV_PLAY_MENU_IDLE    0
#define MLV_PLAY_MENU_FADEIN  1
#define MLV_PLAY_MENU_FADEOUT 2
#define MLV_PLAY_MENU_SHOWN   3
#define MLV_PLAY_MENU_HIDDEN  4


g3gg0's avatar
g3gg0 committed
101
102
103
static uint32_t mlv_play_osd_state = MLV_PLAY_MENU_IDLE;
static int32_t mlv_play_osd_x = 360;
static int32_t mlv_play_osd_y = 0;
104
105
static uint32_t mlv_play_render_timestep = 10;
static uint32_t mlv_play_idle_timestep = 1000;
g3gg0's avatar
g3gg0 committed
106
static uint32_t mlv_play_osd_force_redraw = 0;
107
static uint32_t mlv_play_osd_idle = 1000;
g3gg0's avatar
g3gg0 committed
108
static uint32_t mlv_play_osd_item = 0;
109
static uint32_t mlv_play_paused = 0;
110
static uint32_t mlv_play_info = 1;
111
112
static uint32_t mlv_play_timer_stop = 1;
static uint32_t mlv_play_frames_skipped = 0;
113

114
115
116
117
118
/* this structure is used to build the mlv_xref_t table */
typedef struct 
{
    uint64_t    frameTime;
    uint64_t    frameOffset;
119
120
    uint16_t    fileNumber;
    uint16_t    frameType;
121
122
} frame_xref_t;

123
124
125
126
127
128
129
130
typedef struct
{
    char fullPath[MAX_PATH];
    uint32_t fileSize;
    uint32_t timestamp;
} playlist_entry_t;


131
#define SCREEN_MSG_LEN  64
132

133
134
135
136
137
138
139
140
141
142
143
144
typedef struct 
{
    char topLeft[SCREEN_MSG_LEN];
    char topRight[SCREEN_MSG_LEN];
    char botLeft[SCREEN_MSG_LEN];
    char botRight[SCREEN_MSG_LEN];
} screen_msg_t;

typedef struct 
{
    uint32_t frameSize;
    void *frameBuffer;
145
    void *frameBufferAligned;
146
147
148
149
    screen_msg_t messages;
    uint16_t xRes;
    uint16_t yRes;
    uint16_t bitDepth;
150
    uint16_t blackLevel;
151
    uint16_t whiteLevel;
152
153
154
155
156
} frame_buf_t;

/* set up two queues - one with empty buffers and one with buffers to render */
static struct msg_queue *mlv_play_queue_empty;
static struct msg_queue *mlv_play_queue_render;
g3gg0's avatar
g3gg0 committed
157
static struct msg_queue *mlv_play_queue_osd;
158
static struct msg_queue *mlv_play_queue_fps;
159

160
161
162
163
164
165
166
167
168
169
/* queue for playlist item submits by scanner tasks */
static struct msg_queue *mlv_playlist_queue;
static struct msg_queue *mlv_playlist_scan_queue;
static playlist_entry_t *mlv_playlist = NULL;
static uint32_t mlv_playlist_entries = 0;

static char mlv_play_next_filename[MAX_PATH];
static char mlv_play_current_filename[MAX_PATH];
static playlist_entry_t mlv_playlist_next(playlist_entry_t current);
static playlist_entry_t mlv_playlist_prev(playlist_entry_t current);
g3gg0's avatar
g3gg0 committed
170
static void mlv_playlist_delete(playlist_entry_t current);
171
static void mlv_playlist_build(uint32_t priv);
172
static uint32_t mlv_play_should_stop();
173

174
175
/* microsecond durations for one frame */
static uint32_t mlv_play_frame_div_pos = 0;
176
static uint32_t mlv_play_frame_dividers[3];
177

178

179
180
181
182
183
184
185
186
187
188
189
190
191
static void mlv_play_flush_queue(struct msg_queue *queue)
{
    uint32_t messages = 0;

    msg_queue_count(queue, &messages);
    while(messages > 0)
    {
        uint32_t tmp_buf = 0;
        msg_queue_receive(queue, &tmp_buf, 0);
        msg_queue_count(queue, &messages);
    }
}

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
static void mlv_play_next()
{
    playlist_entry_t current;
    playlist_entry_t next;
    
    strncpy(current.fullPath, mlv_play_current_filename, sizeof(current.fullPath));
    
    next = mlv_playlist_next(current);
    
    if(strlen(next.fullPath))
    {
        strncpy(mlv_play_next_filename, next.fullPath, sizeof(mlv_play_next_filename));
        mlv_play_stopfile = 1;
        mlv_play_paused = 0;
    }
}

static void mlv_play_prev()
{
    playlist_entry_t current;
    playlist_entry_t next;
    
    strncpy(current.fullPath, mlv_play_current_filename, sizeof(current.fullPath));
    
    next = mlv_playlist_prev(current);
    
    if(strlen(next.fullPath))
    {
        strncpy(mlv_play_next_filename, next.fullPath, sizeof(mlv_play_next_filename));
        mlv_play_stopfile = 1;
        mlv_play_paused = 0;
    }
}
225

226
227
static void mlv_play_progressbar(int pct, char *msg)
{
228
    static int last_pct = -1;
229
230
231
232
233
234
235
236
237
238
239
240
    int border = 4;
    int height = 40;
    int width = 720 - 100;

    int x = (720 - width) / 2;
    int y = (480 - height) / 2;

    if(pct == 0)
    {
        bmp_fill(COLOR_BLACK, x, y - font_med.height - border, width, font_med.height);
        bmp_fill(COLOR_WHITE, x, y, width, height);
        bmp_fill(COLOR_BLACK, x + border - 1, y + border - 1, width - 2 * (border - 1), height - 2 * (border - 1));
241
        last_pct = -1;
242
243
    }
    
244
245
246
247
248
249
    if(last_pct != pct)
    {
        bmp_fill(COLOR_BLUE, x + border, y + border, ((width - 2 * border) * pct) / 100, height - 2 * border);
        bmp_printf(FONT_MED, x, y - font_med.height - border, msg);
        last_pct = pct;
    }
250
251
}

252
253
254
/* call with positive duration and some string => print it for a while, then clear it */
/* call with 0 duration and some string => print it without clearing */
/* call with 0 duration and null string => just clear it */
255
256
257
258
259
260
261
262
static void mlv_play_show_dlg(uint32_t duration, char *string)
{
    uint32_t width = 500;
    uint32_t height = 128 + 20;
    uint32_t pos_y = (480 - height) / 2;
    uint32_t pos_x = (720 - width) / 2;
    struct bmp_file_t *icon = NULL;
    
263
    if (string)
264
    {
265
266
267
268
269
270
        icon = bmp_load_ram((uint8_t *)LDVAR(video_bmp), LDLEN(video_bmp), 0);
        
        bmp_fill(COLOR_BG, pos_x, pos_y, width, height);
        
        /* redraw 4 times in case it gets overdrawn */
        for(int loop = 0; loop < 4; loop++)
271
        {
272
273
274
275
276
277
278
279
280
281
282
            if(icon)
            {
                bmp_draw_scaled_ex(icon, pos_x + 10, pos_y + 10, 128, 128, 0);
            }
            else
            {
                bmp_printf(FONT_CANON, pos_x + 10, pos_y + 10 - font_med.height / 2, "[X]");
            }
            
            bmp_printf(FONT_CANON, pos_x + 128 + 20, pos_y + (128 / 2) - 16, string);
            msleep(duration / 10);
283
284
        }
        
285
        free(icon);
286
287
    }
    
288
289
290
291
    if (duration || !string)
    {
        bmp_fill(COLOR_EMPTY, pos_x, pos_y, width, height);
    }
292
293
}

294
295
296
297
298
299
/* run deletion in the same task as playback, to sync with file closing etc */
static int mlv_play_delete_requested = 0;

static void mlv_play_delete_request()
{
    mlv_play_delete_requested = 1;
300
    mlv_play_show_dlg(0, "Stopping...");
301
302
}

303
304
/* returns 1 on success, 0 on failure */
static int mlv_play_delete_work(char *filename)
g3gg0's avatar
g3gg0 committed
305
306
{
    uint32_t size = 0;
307
    uint32_t state = 0;
g3gg0's avatar
g3gg0 committed
308
    uint32_t seq_number = 0;
309
    char current_ext[4];
g3gg0's avatar
g3gg0 committed
310
    char current_file[128];
g3gg0's avatar
g3gg0 committed
311
    
312
313
314
    /* original extension, to be used as read-only */
    const char* main_ext = filename + strlen(filename) - 3;
    
315
    trace_write(mlv_play_trace_ctx, "[Delete] Delete request for: '%s'", filename);
g3gg0's avatar
g3gg0 committed
316
    
317
    strncpy(current_file, filename, sizeof(current_file));
g3gg0's avatar
g3gg0 committed
318
    
319
    /* file does not exist. the specified base file must exist, so abort */
g3gg0's avatar
g3gg0 committed
320
321
    if(FIO_GetFileSize(filename, &size))
    {
322
        trace_write(mlv_play_trace_ctx, "  Does not exist!");
323
        return 0;
g3gg0's avatar
g3gg0 committed
324
325
    }
    
326
327
    int is_mlv = streq(main_ext, "MLV") || streq(main_ext, "mlv");
    
328
    while (1)
g3gg0's avatar
g3gg0 committed
329
    {
330
        switch(state)
g3gg0's avatar
g3gg0 committed
331
        {
332
            case 0:
333
334
                /* try to delete main file */
                strcpy(current_ext, main_ext);
335
                break;
336

337
            case 1:
338
339
340
341
342
343
344
345
346
347
348
                /* try to delete index file */
                if (is_mlv)
                {
                    strcpy(current_ext, "IDX");
                }
                else
                {
                    /* index files are only used on MLVs, skip for other files */
                    state++;
                    continue;
                }
349
350
351
352
                break;
                
            case 2:
                /* try to delete chunk files */
353
354
                snprintf(current_ext, 4, "X%02d", seq_number);
                current_ext[0] = main_ext[0];
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
                break;
                
            case 3:
                /* next chunk file */
                if(seq_number >= 99)
                {
                    /* that would be the hundreth chunk, so lets say that was it */
                    state++;
                }
                else
                {
                    /* increase chunk counter and delete next file */
                    seq_number++;
                    state--;
                }
g3gg0's avatar
g3gg0 committed
370
                continue;
371
372
373
                
            case 4:
                /* we are done */
374
                return 1;
g3gg0's avatar
g3gg0 committed
375
        }
376
377

        for(int drive = 0; drive < 2; drive++)
g3gg0's avatar
g3gg0 committed
378
        {
379
380
381
382
383
384
            /* set extension */
            strcpy(&current_file[strlen(current_file) - 3], current_ext);
            
            /* set drive letter */
            current_file[0] = drive ? 'A' : 'B';
            
385
386
387
388
389
390
391
392
393
394
            if (!is_mlv)
            {
                if (current_file[0] != filename[0])
                {
                    /* non-MLV files can't be spanned, do not delete the file with the same name from the other card */
                    trace_write(mlv_play_trace_ctx, "[Delete] Skip drive: '%s'", current_file);
                    continue;
                }
            }
            
395
396
397
            trace_write(mlv_play_trace_ctx, "[Delete] Next file: '%s'", current_file);
            
            /* check if file exists */
g3gg0's avatar
g3gg0 committed
398
399
            if(FIO_GetFileSize(current_file, &size))
            {
400
401
402
403
404
                if (state == 0 && current_file[0] == filename[0])
                {
                    trace_write(mlv_play_trace_ctx, "[Delete] main file does not exist '%s'", current_file);
                    return 0;
                }
405
406
                continue;
            }
407

408
409
410
411
412
413
            trace_write(mlv_play_trace_ctx, "  existing, deleting");
            
            /* if existing, try to delete and set fail flag if that doesnt work */
            if(FIO_RemoveFile(current_file))
            {
                trace_write(mlv_play_trace_ctx, "  FAILED");
414
                return 0;
g3gg0's avatar
g3gg0 committed
415
416
417
            }
            else
            {
418
                trace_write(mlv_play_trace_ctx, "  deleted");
g3gg0's avatar
g3gg0 committed
419
420
            }
        }
421
422
        
        /* if there was no failure in deleting the file, next state */
423
        state++;
g3gg0's avatar
g3gg0 committed
424
425
    }
    
426
    return 1;
g3gg0's avatar
g3gg0 committed
427
428
}

429
static int mlv_play_delete()
g3gg0's avatar
g3gg0 committed
430
431
432
433
434
{
    playlist_entry_t current;
    playlist_entry_t next;
    playlist_entry_t prev;
    
g3gg0's avatar
g3gg0 committed
435
    /* save the currently played file */
g3gg0's avatar
g3gg0 committed
436
    strncpy(current.fullPath, mlv_play_current_filename, sizeof(current.fullPath));
g3gg0's avatar
g3gg0 committed
437
438
    
    /* check next/prev */
g3gg0's avatar
g3gg0 committed
439
440
441
    next = mlv_playlist_next(current);
    prev = mlv_playlist_prev(current);
    
g3gg0's avatar
g3gg0 committed
442
    /* remove the currently played from the playlist */
g3gg0's avatar
g3gg0 committed
443
    mlv_playlist_delete(current);
444
445

    /* delete the files */
446
    int ok = mlv_play_delete_work(current.fullPath);
447
    trace_write(mlv_play_trace_ctx, "[Delete] returned %s", ok ? "success" : "FAILURE");
448
449

    /* check which of the files is available and play it */
g3gg0's avatar
g3gg0 committed
450
451
452
453
454
455
456
457
458
459
    if(strlen(next.fullPath))
    {
        strncpy(mlv_play_next_filename, next.fullPath, sizeof(mlv_play_next_filename));
    }
    else if(strlen(prev.fullPath))
    {
        strncpy(mlv_play_next_filename, prev.fullPath, sizeof(mlv_play_next_filename));
    }
    else
    {
g3gg0's avatar
g3gg0 committed
460
        /* if none is available, just stop playback */
g3gg0's avatar
g3gg0 committed
461
462
        mlv_play_render_abort = 1;
    }
463
464
    
    return ok;
465
466
467
468
469
470
}

static void mlv_play_delete_if_requested()
{
    if (mlv_play_delete_requested)
    {
471
        mlv_play_show_dlg(0, "Deleting...");
472
473
        int ok = mlv_play_delete();
        mlv_play_show_dlg(3000, ok ? "Deleted." : "Delete failed.");
474
475
        mlv_play_delete_requested = 0;
    }
g3gg0's avatar
g3gg0 committed
476
477
}

g3gg0's avatar
g3gg0 committed
478
static void mlv_play_osd_quality(char *msg, uint32_t msg_len, uint32_t selected)
479
480
481
{
    if(selected)
    {
482
        mlv_play_quality = MOD(mlv_play_quality + 1, 2);
483
484
485
486
    }
    
    if(msg)
    {
g3gg0's avatar
g3gg0 committed
487
        snprintf(msg, msg_len, mlv_play_quality?"fast":"color");
g3gg0's avatar
g3gg0 committed
488
489
490
    }
}

491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
static void mlv_play_osd_exact(char *msg, uint32_t msg_len, uint32_t selected)
{
    if(selected)
    {
        mlv_play_exact_fps = MOD(mlv_play_exact_fps + 1, 2);
    }
    
    if(msg)
    {
        /* any idea for a meaningful text here? feature description in long: 
                if enabled: 
                  play at exact fps with dropping if necessary
                  grayscale might keep up with video rate, but color requires dropping for sure
                if disabled:
                  play frame by frame as fast as possible
       */
        snprintf(msg, msg_len, mlv_play_exact_fps?"exact":"all");
    }
}

511

g3gg0's avatar
g3gg0 committed
512
513
514
515
static void mlv_play_osd_prev(char *msg, uint32_t msg_len, uint32_t selected)
{
    if(selected)
    {
516
        mlv_play_prev();
g3gg0's avatar
g3gg0 committed
517
518
519
520
521
522
523
524
525
526
527
528
    }
    
    if(msg)
    {
        snprintf(msg, msg_len, "<<");
    }
}

static void mlv_play_osd_next(char *msg, uint32_t msg_len, uint32_t selected)
{
    if(selected)
    {
529
        mlv_play_next();
g3gg0's avatar
g3gg0 committed
530
531
532
533
534
    }
    
    if(msg)
    {
        snprintf(msg, msg_len, ">>");
535
536
537
    }
}

g3gg0's avatar
g3gg0 committed
538
static void mlv_play_osd_pause(char *msg, uint32_t msg_len, uint32_t selected)
539
540
541
{
    if(selected)
    {
542
        mlv_play_paused = MOD(mlv_play_paused + 1, 2);
543
544
545
546
    }
    
    if(msg)
    {
g3gg0's avatar
g3gg0 committed
547
        snprintf(msg, msg_len, mlv_play_paused?"|>":"||");
548
549
550
    }
}

g3gg0's avatar
g3gg0 committed
551
static void mlv_play_osd_quit(char *msg, uint32_t msg_len, uint32_t selected)
552
553
554
555
556
557
558
559
{
    if(selected)
    {
        mlv_play_render_abort = 1;
    }
    
    if(msg)
    {
g3gg0's avatar
g3gg0 committed
560
        snprintf(msg, msg_len, "Exit");
561
562
563
    }
}

564
static uint32_t mlv_play_osd_delete_selected = 0;
565

g3gg0's avatar
g3gg0 committed
566
567
568
569
570
571
static void mlv_play_osd_delete(char *msg, uint32_t msg_len, uint32_t selected)
{
    uint32_t max_time = 5000;

    if(selected)
    {
572
        if(!mlv_play_osd_delete_selected || selected == 2)
g3gg0's avatar
g3gg0 committed
573
574
        {
            mlv_play_osd_force_redraw = 1;
575
            mlv_play_osd_delete_selected = get_ms_clock();
g3gg0's avatar
g3gg0 committed
576
577
578
579
        }
        else
        {
            mlv_play_osd_force_redraw = 0;
580
            mlv_play_osd_delete_selected = 0;
581
            mlv_play_delete_request();
g3gg0's avatar
g3gg0 committed
582
583
584
585
586
        }
    }
    
    if(msg)
    {
587
        uint32_t time_passed = get_ms_clock() - mlv_play_osd_delete_selected;
g3gg0's avatar
g3gg0 committed
588
589
        uint32_t seconds = (max_time - time_passed) / 1000;
        
590
        if(mlv_play_osd_delete_selected && seconds > 0)
g3gg0's avatar
g3gg0 committed
591
592
593
594
595
        {
            snprintf(msg, msg_len, "[delete? %ds]", seconds);
        }
        else
        {
596
            mlv_play_osd_delete_selected = 0;
g3gg0's avatar
g3gg0 committed
597
598
599
600
601
602
            mlv_play_osd_force_redraw = 0;
            snprintf(msg, msg_len, "Del");
        }
    }
}

603
static void(*mlv_play_osd_items[])(char *, uint32_t,  uint32_t) = { &mlv_play_osd_prev, &mlv_play_osd_pause, &mlv_play_osd_next, &mlv_play_osd_quality, &mlv_play_osd_exact, &mlv_play_osd_delete, &mlv_play_osd_quit };
604

g3gg0's avatar
g3gg0 committed
605
static uint32_t mlv_play_osd_handle(uint32_t msg)
606
607
608
609
{
    switch(msg)
    {
        case MODULE_KEY_PRESS_SET:
610
        case MODULE_KEY_JOY_CENTER:
611
612
        {
            /* execute menu item */
g3gg0's avatar
g3gg0 committed
613
            mlv_play_osd_items[mlv_play_osd_item](NULL, 0, 1);
614
615
616
617
            break;
        }
        
        case MODULE_KEY_WHEEL_UP:
618
        //case MODULE_KEY_WHEEL_LEFT:
619
        case MODULE_KEY_PRESS_LEFT:
620
        {
g3gg0's avatar
g3gg0 committed
621
            if(mlv_play_osd_item > 0)
622
            {
g3gg0's avatar
g3gg0 committed
623
                mlv_play_osd_item--;
624
625
626
627
628
            }
            break;
        }
        
        case MODULE_KEY_WHEEL_DOWN:
629
        //case MODULE_KEY_WHEEL_RIGHT:
630
        case MODULE_KEY_PRESS_RIGHT:
631
        {
g3gg0's avatar
g3gg0 committed
632
            if(mlv_play_osd_item < COUNT(mlv_play_osd_items) - 1)
633
            {
g3gg0's avatar
g3gg0 committed
634
                mlv_play_osd_item++;
635
636
637
638
            }
            break;
        }
    }
639
640
    
    return 0;
641
642
}

g3gg0's avatar
g3gg0 committed
643
static uint32_t mlv_play_osd_draw()
644
645
{
    uint32_t redraw = 0;
646
647
    uint32_t border = 4;
    uint32_t y_offset = 28;
648

649
    /* undraw last drawn OSD item */
g3gg0's avatar
g3gg0 committed
650
    static char osd_line[64] = "";
651
    
g3gg0's avatar
g3gg0 committed
652
    uint32_t w = bmp_string_width(FONT_LARGE, osd_line);
653
    uint32_t h = fontspec_height(FONT_LARGE);
654
    bmp_fill(COLOR_EMPTY, mlv_play_osd_x - w/2 - border, mlv_play_osd_y - border, w + 2 * border, h + 2 * border);
655
656
    
    /* handle animation */
g3gg0's avatar
g3gg0 committed
657
    switch(mlv_play_osd_state)
658
659
660
661
662
663
664
665
666
667
    {
        case MLV_PLAY_MENU_SHOWN:
        {
            redraw = 0;
            break;
        }
        
        case MLV_PLAY_MENU_HIDDEN:
        case MLV_PLAY_MENU_IDLE:
        {
g3gg0's avatar
g3gg0 committed
668
            mlv_play_osd_y = os.y_max + 1;
669
670
671
672
673
674
            redraw = 0;
            break;
        }
        
        case MLV_PLAY_MENU_FADEIN:
        {
675
676
677
            int y_top = os.y_max - font_large.height - y_offset;
            mlv_play_osd_y = MAX(mlv_play_osd_y - border, y_top);
            if(mlv_play_osd_y <= y_top)
678
            {
g3gg0's avatar
g3gg0 committed
679
                mlv_play_osd_state = MLV_PLAY_MENU_SHOWN;
680
681
682
683
684
685
686
            }
            redraw = 1;
            break;
        }
        
        case MLV_PLAY_MENU_FADEOUT:
        {
687
688
689
            int y_bottom = os.y_max + 1;
            mlv_play_osd_y = MIN(mlv_play_osd_y + border, y_bottom);
            if(mlv_play_osd_y >= y_bottom)
690
            {
g3gg0's avatar
g3gg0 committed
691
                mlv_play_osd_state = MLV_PLAY_MENU_HIDDEN;
692
693
694
695
696
697
            }
            redraw = 1;
            break;
        }
    }
    
g3gg0's avatar
g3gg0 committed
698
699
700
701
702
    /* draw a line with all OSD buttons */
    char selected_item[64];
    uint32_t selected_x = 0;
    
    strcpy(osd_line, "");
703
    for(uint32_t pos = 0; pos < COUNT(mlv_play_osd_items); pos++)
704
    {
g3gg0's avatar
g3gg0 committed
705
706
707
708
709
710
711
712
713
714
715
716
717
        char msg[64];
        mlv_play_osd_items[pos](msg, sizeof(msg), 0);
        
        /* if this is the selected one, keep the position for redrawing highlighted */
        if(pos == mlv_play_osd_item)
        {
            strcpy(selected_item, msg);
            selected_x = bmp_string_width(FONT_LARGE, osd_line);
        }
        
        strcat(osd_line, "  ");
        strcat(osd_line, msg);
        strcat(osd_line, "  ");
718
719
    }
    
g3gg0's avatar
g3gg0 committed
720
    w = bmp_string_width(FONT_LARGE, osd_line);
721
    bmp_fill(COLOR_BG, mlv_play_osd_x - w/2 - border, mlv_play_osd_y - border, w + 2 * border, h + 2 * border);
g3gg0's avatar
g3gg0 committed
722
723
724
725
726
    bmp_printf(FONT(FONT_LARGE,COLOR_WHITE,COLOR_BG), mlv_play_osd_x - w/2, mlv_play_osd_y, osd_line);
    
    /* draw selected item over with blue background */
    bmp_printf(FONT(FONT_LARGE,COLOR_WHITE,COLOR_BLUE), mlv_play_osd_x - w/2 + selected_x, mlv_play_osd_y, "  %s  ", selected_item);
    
727
728
729
    return redraw;
}

g3gg0's avatar
g3gg0 committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
static void mlv_play_osd_act(void *handler)
{
    int entry = 0;
    
    for(entry = 0; entry < COUNT(mlv_play_osd_items); entry++)
    {
        if(mlv_play_osd_items[entry] == handler)
        {
            mlv_play_osd_item = entry;
            mlv_play_osd_state = MLV_PLAY_MENU_FADEIN;
            return;
        }
    }
}

g3gg0's avatar
g3gg0 committed
745
static void mlv_play_osd_task(void *priv)
746
{
747
    uint32_t next_render_time = get_ms_clock() + mlv_play_render_timestep;
748
 
g3gg0's avatar
g3gg0 committed
749
    mlv_play_osd_state = MLV_PLAY_MENU_IDLE;
750
    mlv_play_osd_item = 1;
751
752
    mlv_play_paused = 0;   
    
753
    uint32_t last_keypress_time = get_ms_clock();
754
755
    TASK_LOOP
    {
756
        uint32_t key;
757
        uint32_t timeout = next_render_time - get_ms_clock();
758
759
760
        
        timeout = MIN(timeout, mlv_play_idle_timestep);
        
g3gg0's avatar
g3gg0 committed
761
        if(!msg_queue_receive(mlv_play_queue_osd, &key, timeout))
762
        {
763
            /* there was a keypress */
764
            last_keypress_time = get_ms_clock();
765
766
767
768
769
770
771
772
773
774
775
            
            /* no matter which state - these are handled */
            switch(key)
            {
                case MODULE_KEY_WHEEL_LEFT:
                    mlv_play_prev();
                    if(mlv_play_osd_state != MLV_PLAY_MENU_SHOWN)
                    {
                        mlv_play_osd_state = MLV_PLAY_MENU_FADEIN;
                    }
                    break;
g3gg0's avatar
g3gg0 committed
776
                    
777
778
779
780
781
782
783
                case MODULE_KEY_WHEEL_RIGHT:
                    mlv_play_next();
                    if(mlv_play_osd_state != MLV_PLAY_MENU_SHOWN)
                    {
                        mlv_play_osd_state = MLV_PLAY_MENU_FADEIN;
                    }
                    break;
g3gg0's avatar
g3gg0 committed
784
785
786
787
788
                    
                case MODULE_KEY_TRASH:
                    mlv_play_osd_act(&mlv_play_osd_delete);
                    mlv_play_osd_delete(NULL, 0, 2);
                    break;
789
790
791
792
793

                case MODULE_KEY_PLAY:
                    mlv_play_osd_act(&mlv_play_osd_pause);
                    mlv_play_osd_pause(NULL, 0, 1);
                    break;
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819

                case MODULE_KEY_PRESS_ZOOMIN:
                    mlv_play_osd_state = MLV_PLAY_MENU_HIDDEN;
                    mlv_play_zoom = (mlv_play_zoom + 1) % 4;
                    raw_twk_set_zoom(mlv_play_zoom, mlv_play_zoom_x_pct, mlv_play_zoom_y_pct);
                    break;
                    
                case MODULE_KEY_PRESS_UP:
                    mlv_play_zoom_y_pct = MAX(0, mlv_play_zoom_y_pct - 10);
                    raw_twk_set_zoom(mlv_play_zoom, mlv_play_zoom_x_pct, mlv_play_zoom_y_pct);
                    break;

                case MODULE_KEY_PRESS_DOWN:
                    mlv_play_zoom_y_pct = MIN(100, mlv_play_zoom_y_pct + 10);
                    raw_twk_set_zoom(mlv_play_zoom, mlv_play_zoom_x_pct, mlv_play_zoom_y_pct);
                    break;

                case MODULE_KEY_PRESS_LEFT:
                    mlv_play_zoom_x_pct = MAX(0, mlv_play_zoom_x_pct - 10);
                    raw_twk_set_zoom(mlv_play_zoom, mlv_play_zoom_x_pct, mlv_play_zoom_y_pct);
                    break;

                case MODULE_KEY_PRESS_RIGHT:
                    mlv_play_zoom_x_pct = MIN(100, mlv_play_zoom_x_pct + 10);
                    raw_twk_set_zoom(mlv_play_zoom, mlv_play_zoom_x_pct, mlv_play_zoom_y_pct);
                    break;
820
821
            }
            
g3gg0's avatar
g3gg0 committed
822
            switch(mlv_play_osd_state)
823
824
825
826
            {
                case MLV_PLAY_MENU_SHOWN:
                case MLV_PLAY_MENU_FADEIN:
                {
827
                    if(key == MODULE_KEY_Q || key == MODULE_KEY_PICSTYLE)
828
                    {
829
830
831
832
                        if (!mlv_play_osd_force_redraw)
                        {
                            mlv_play_osd_state = MLV_PLAY_MENU_FADEOUT;
                        }
833
834
835
                    }
                    else
                    {
g3gg0's avatar
g3gg0 committed
836
                        mlv_play_osd_handle(key);
837
838
839
                    }
                    break;
                }
840
841
842
843

                /* when fading out, still handle and fade back in */
                case MLV_PLAY_MENU_FADEOUT:
                {
844
845
846
847
848
                    if(!mlv_play_zoom)
                    {
                        mlv_play_osd_state = MLV_PLAY_MENU_FADEIN;
                        mlv_play_osd_handle(key);
                    }
849
850
                    break;
                }
851
852
853
854
                
                case MLV_PLAY_MENU_IDLE:
                case MLV_PLAY_MENU_HIDDEN:
                {
855
                    if(!mlv_play_zoom)
856
                    {
857
858
859
860
861
862
863
864
865
                        if (key == MODULE_KEY_PRESS_SET || key == MODULE_KEY_Q || key == MODULE_KEY_PICSTYLE)
                        {
                            mlv_play_osd_state = MLV_PLAY_MENU_FADEIN;
                        }
                        if (key == MODULE_KEY_INFO)
                        {
                            clrscr();
                            mlv_play_info = MOD(mlv_play_info + 1, 2) ? 2 : 0;
                        }
866
                    }
867
868
869
870
                    break;
                }
            }
        }
871
        uint32_t idle_time = get_ms_clock() - last_keypress_time;
872
873
874
875
876
877
        
        if(mlv_play_render_abort)
        {
            break;
        }
        
g3gg0's avatar
g3gg0 committed
878
        if(mlv_play_osd_draw())
879
        {
880
            next_render_time = get_ms_clock() + mlv_play_render_timestep;
881
882
883
        }
        else
        {
884
            next_render_time = get_ms_clock() + mlv_play_idle_timestep;
885
            
g3gg0's avatar
g3gg0 committed
886
887
888
889
890
891
            /* when redrawing is forced, keep OSD shown */
            if(mlv_play_osd_force_redraw)
            {
                mlv_play_osd_state = MLV_PLAY_MENU_SHOWN;
            }
            else if(idle_time > mlv_play_osd_idle && mlv_play_osd_state == MLV_PLAY_MENU_SHOWN)
892
893
894
            {
                mlv_play_osd_state = MLV_PLAY_MENU_FADEOUT;
            }
895
896
897
898
899
900
        }
        continue;
    }
}


901
static void mlv_play_xref_resize(frame_xref_t **table, uint32_t entries, uint32_t *allocated)
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
{
    /* make sure there is no crappy pointer before using */
    if(*allocated == 0)
    {
        *table = NULL;
    }
    
    /* only resize if the buffer is too small */
    if(entries * sizeof(frame_xref_t) > *allocated)
    {
        *allocated += (entries + 1) * sizeof(frame_xref_t);
        *table = realloc(*table, *allocated);
    }
}

917
static void mlv_play_xref_sort(frame_xref_t *table, uint32_t entries)
918
{
919
920
    if (!entries) return;
    
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
    uint32_t n = entries;
    do
    {
        uint32_t newn = 1;
        for (uint32_t i = 0; i < n-1; ++i)
        {
            if (table[i].frameTime > table[i+1].frameTime)
            {
                frame_xref_t tmp = table[i+1];
                table[i+1] = table[i];
                table[i] = tmp;
                newn = i + 1;
            }
        }
        n = newn;
    } while (n > 1);
}

939
static mlv_xref_hdr_t *mlv_play_load_index(char *base_filename)
940
941
942
943
944
945
946
947
{
    mlv_xref_hdr_t *block_hdr = NULL;
    char filename[128];
    FILE *in_file = NULL;

    strncpy(filename, base_filename, sizeof(filename));
    strcpy(&filename[strlen(filename) - 3], "IDX");
    
948
    in_file = FIO_OpenFile(filename, O_RDONLY | O_SYNC);
949
    
950
    if (!in_file)
951
952
953
954
    {
        return NULL;
    }
    
955
    TASK_LOOP
956
957
    {
        mlv_hdr_t buf;
958
        int64_t position = 0;
959
        
960
        position = FIO_SeekSkipFile(in_file, 0, SEEK_CUR);
961
962
963
964
965
966
967
        
        if(FIO_ReadFile(in_file, &buf, sizeof(mlv_hdr_t)) != sizeof(mlv_hdr_t))
        {
            break;
        }
        
        /* jump back to the beginning of the block just read */
968
        FIO_SeekSkipFile(in_file, position, SEEK_SET);
969
970
971
972

        /* we should check the MLVI header for matching UID value to make sure its the right index... */
        if(!memcmp(buf.blockType, "XREF", 4))
        {
973
            block_hdr = fio_malloc(buf.blockSize);
974

975
            if(FIO_ReadFile(in_file, block_hdr, buf.blockSize) != (int32_t)buf.blockSize)
976
977
978
979
980
981
982
            {
                free(block_hdr);
                block_hdr = NULL;
            }
        }
        else
        {
983
            FIO_SeekSkipFile(in_file, position + buf.blockSize, SEEK_SET);
984
985
986
        }
        
        /* we are at the same position as before, so abort */
987
        if(position == FIO_SeekSkipFile(in_file, 0, SEEK_CUR))
988
989
990
991
992
993
994
995
996
997
        {
            break;
        }
    }
    
    FIO_CloseFile(in_file);
    
    return block_hdr;
}

998
static void mlv_play_save_index(char *base_filename, mlv_file_hdr_t *ref_file_hdr, int fileCount, frame_xref_t *index, int entries)
999
1000
1001
1002
1003
1004
1005
{
    char filename[128];
    FILE *out_file = NULL;

    strncpy(filename, base_filename, sizeof(filename));
    strcpy(&filename[strlen(filename) - 3], "IDX");
    
1006
    out_file = FIO_CreateFile(filename);
1007
    
1008
    if (!out_file)
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
    {
        return;
    }
    
    /* first write MLVI header */
    mlv_file_hdr_t file_hdr = *ref_file_hdr;
    
    /* update fields */
    file_hdr.blockSize = sizeof(mlv_file_hdr_t);
    file_hdr.videoFrameCount = 0;
    file_hdr.audioFrameCount = 0;
    file_hdr.fileNum = fileCount + 1;
    
    FIO_WriteFile(out_file, &file_hdr, sizeof(mlv_file_hdr_t));

    /* now write XREF block */
    mlv_xref_hdr_t hdr;
    
1027
    memset(&hdr, 0x00, sizeof(mlv_xref_hdr_t));
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
    memcpy(hdr.blockType, "XREF", 4);
    hdr.blockSize = sizeof(mlv_xref_hdr_t) + entries * sizeof(mlv_xref_t);
    hdr.entryCount = entries;
    
    if(FIO_WriteFile(out_file, &hdr, sizeof(mlv_xref_hdr_t)) != sizeof(mlv_xref_hdr_t))
    {
        FIO_CloseFile(out_file);
        return;
    }
    
    uint32_t last_pct = 0;
1039
    mlv_play_progressbar(0, "");
1040
    
g3gg0's avatar
g3gg0 committed
1041
1042
1043
    uint32_t buffer_size = 256;
    mlv_xref_t *buffer = malloc(sizeof(mlv_xref_t) * buffer_size);
    
1044
1045
1046
    /* and then the single entries */
    for(int entry = 0; entry < entries; entry++)
    {
g3gg0's avatar
g3gg0 committed
1047
1048
        uint32_t buffer_pos = entry % buffer_size;
        mlv_xref_t *field = &buffer[buffer_pos];
1049
1050
1051
1052
        uint32_t pct = (entry*100)/entries;
        
        if(last_pct != pct)
        {
1053
1054
            char msg[100];
            
1055
            snprintf(msg, sizeof(msg), "Saving index (%d entries)...", entries);
1056
            mlv_play_progressbar(pct, msg);
1057
1058
            last_pct = pct;
        }
g3gg0's avatar
g3gg0 committed
1059
        memset(field, 0x00, sizeof(mlv_xref_t));
1060
        
g3gg0's avatar
g3gg0 committed
1061
1062
1063
        field->frameOffset = index[entry].frameOffset;
        field->fileNumber = index[entry].fileNumber;
        field->frameType = index[entry].frameType;
1064
        
g3gg0's avatar
g3gg0 committed
1065
        if((buffer_pos + 1) == buffer_size || ((entry + 1) == entries))
1066
        {
g3gg0's avatar
g3gg0 committed
1067
1068
1069
1070
1071
1072
1073
            uint32_t write_size = (buffer_pos + 1) * sizeof(mlv_xref_t);
            
            if(FIO_WriteFile(out_file, buffer, write_size) != write_size)
            {
                FIO_CloseFile(out_file);
                return;
            }
1074
1075
1076
        }
    }
    
g3gg0's avatar
g3gg0 committed
1077
1078
    free(buffer);
    
1079
1080
1081
    FIO_CloseFile(out_file);
}

1082
static void mlv_play_build_index(char *filename, FILE **chunk_files, uint32_t chunk_count)
1083
1084
1085
1086
1087
1088
{
    frame_xref_t *frame_xref_table = NULL;
    uint32_t frame_xref_entries = 0;
    uint32_t frame_xref_allocated = 0;
    mlv_file_hdr_t main_header;
    
1089
    for(uint32_t chunk = 0; chunk < chunk_count; chunk++)
1090
1091
    {
        uint32_t last_pct = 0;
1092
1093
        int64_t size = 0;
        int64_t position = 0;
1094
        
1095
        size = FIO_SeekSkipFile(chunk_files[chunk], 0, SEEK_END);
1096
        FIO_SeekSkipFile(chunk_files[chunk], 0, SEEK_SET);
1097
        
1098
        mlv_play_progressbar(0, "");
1099
1100
1101
        
        while(1)
        {
1102
1103
1104
1105
1106
            if(ml_shutdown_requested)
            {
                break;
            }
            
1107
            mlv_hdr_t buf;
1108
            uint64_t timestamp = 0;
1109
1110
1111
1112
1113
            
            uint32_t pct = ((position / 10) / (size / 1000));
            
            if(last_pct != pct)
            {
1114
1115
1116
1117
                char msg[100];
                
                snprintf(msg, sizeof(msg), "Building index... (%d/%d)", chunk + 1, chunk_count);
                mlv_play_progressbar(pct, msg);
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
                last_pct = pct;
            }
            
            int read = FIO_ReadFile(chunk_files[chunk], &buf, sizeof(mlv_hdr_t));
            
            if(read != sizeof(mlv_hdr_t))
            {
                if(read <= 0)
                {
                    break;
                }
                else
                {
1131
                    bmp_printf(FONT_MED, 30, 190, "File #%d ends prematurely, %d bytes read", chunk, read);
1132
                    beep();
1133
1134
                    msleep(2000);
                    return;
1135
1136
1137
                }
            }
            
1138
            /* unexpected block header size? */
a1ex's avatar
a1ex committed
1139
            if(buf.blockSize < sizeof(mlv_hdr_t) || buf.blockSize > 50 * 1024 * 1024)
1140
1141
1142
            {
                bmp_printf(FONT_MED, 30, 190, "Invalid header size: %d bytes at 0x%08X", buf.blockSize, position);
                beep();
1143
1144
                msleep(2000);
                return;
1145
1146
            }

1147
1148
1149
1150
1151
            /* file header */
            if(!memcmp(buf.blockType, "MLVI", 4))
            {
                mlv_file_hdr_t file_hdr;
                uint32_t hdr_size = MIN(sizeof(mlv_file_hdr_t), buf.blockSize);
1152
                
1153
                FIO_SeekSkipFile(chunk_files[chunk], position, SEEK_SET);
1154
                
1155
                /* read the whole header block, but limit size to either our local type size or the written block size */
1156
                if(FIO_ReadFile(chunk_files[chunk], &file_hdr, hdr_size) != (int32_t)hdr_size)
1157
                {