summaryrefslogtreecommitdiff
path: root/apps/codecs/Makefile
blob: f39eb55524cb48f230f3ed08ffd652c623f7ce59 (plain)
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
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
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
# $Id$
#

INCLUDES = -I$(FIRMDIR)/include -I$(FIRMDIR)/export -I$(FIRMDIR)/common \
 -I$(FIRMDIR)/drivers -I$(APPSDIR) -Ilib -I$(BUILDDIR)
CFLAGS = $(INCLUDES) $(GCCOPTS) $(TARGET) $(EXTRA_DEFINES) \
 -DTARGET_ID=$(TARGET_ID) -DMEM=${MEMORYSIZE} -DCODEC

ifdef APPEXTRA
   INCLUDES += $(patsubst %,-I$(APPSDIR)/%,$(subst :, ,$(APPEXTRA)))
endif

ifdef SOFTWARECODECS
   CODECLIBS = -lmad -la52 -lffmpegFLAC -lTremor -lwavpack -lmusepack -lalac -lfaad -lm4a
endif

# we "borrow" the plugin LDS file
LDS := $(APPSDIR)/plugins/plugin.lds

LINKCODEC := $(OBJDIR)/codeclink.lds
DEPFILE = $(OBJDIR)/dep-codecs

# This sets up 'SRC' based on the files mentioned in SOURCES
include $(TOOLSDIR)/makesrc.inc

ROCKS := $(SRC:%.c=$(OBJDIR)/%.codec)
SOURCES = $(SRC)
ELFS := $(SRC:%.c=$(OBJDIR)/%.elf)
OBJS := $(SRC:%.c=$(OBJDIR)/%.o)
# as created by the cross-compiler for win32:
DEFS := $(SRC:%.c=$(OBJDIR)/%.def)
DIRS = .

CODECDEPS = $(LINKCODEC) $(BUILDDIR)/libcodec.a

.PHONY: libmad liba52 libffmpegFLAC libTremor libwavpack dumb libmusepack libalac libfaad libm4a

OUTPUT = $(SOFTWARECODECS)

all: $(ROCKS)

ifndef SIMVER
$(OBJDIR)/wav.elf : $(OBJDIR)/wav.o
$(OBJDIR)/aiff.elf : $(OBJDIR)/aiff.o
$(OBJDIR)/mpa.elf : $(OBJDIR)/mpa.o $(BUILDDIR)/libmad.a
$(OBJDIR)/a52.elf : $(OBJDIR)/a52.o $(BUILDDIR)/liba52.a
$(OBJDIR)/flac.elf : $(OBJDIR)/flac.o $(BUILDDIR)/libffmpegFLAC.a
$(OBJDIR)/vorbis.elf : $(OBJDIR)/vorbis.o $(BUILDDIR)/libTremor.a
$(OBJDIR)/mpc.elf : $(OBJDIR)/mpc.o $(BUILDDIR)/libmusepack.a
$(OBJDIR)/wavpack.elf : $(OBJDIR)/wavpack.o $(BUILDDIR)/libwavpack.a
$(OBJDIR)/alac.elf : $(OBJDIR)/alac.o $(BUILDDIR)/libalac.a $(BUILDDIR)/libm4a.a
$(OBJDIR)/aac.elf : $(OBJDIR)/aac.o $(BUILDDIR)/libfaad.a $(BUILDDIR)/libm4a.a
$(OBJDIR)/shorten.elf : $(OBJDIR)/shorten.o $(BUILDDIR)/libffmpegFLAC.a

$(OBJDIR)/%.elf :
	@echo "LD $(notdir $@)"
	$(SILENT)$(CC) $(GCCOPTS) -O -nostdlib -o $@ $^ -L$(BUILDDIR) -lcodec \
		-lgcc -T$(LINKCODEC) -Wl,-Map,$(OBJDIR)/$*.map

$(BUILDDIR)/%.a : % $(CODECDEPS)
	

$(OBJDIR)/%.codec : $(OBJDIR)/%.elf
	@echo "OBJCOPY $(notdir $@)"
	$(SILENT)$(OC) -O binary $< $@
else

ifeq ($(SIMVER), x11)
###################################################
# This is the X11 simulator version

$(OBJDIR)/%.codec : $(OBJDIR)/%.o  $(BUILDDIR)/libcodec.a $(OUTPUT)
	@echo "LD $(notdir $@)"
	$(SILENT)$(CC) $(CFLAGS) -shared $< -L$(BUILDDIR) $(CODECLIBS) -lcodec -o $@
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
# 'x' must be kept or you'll have "Win32 error 5"
#     $ fgrep 5 /usr/include/w32api/winerror.h | head -1
#         #define ERROR_ACCESS_DENIED 5L
else
	$(SILENT)chmod -x $@
endif

else	# end of x11-simulator
ifeq ($(SIMVER), sdl)
###################################################
# This is the SDL simulator version

$(OBJDIR)/%.codec : $(OBJDIR)/%.o  $(BUILDDIR)/libcodec.a $(OUTPUT)
	@echo "LD $(notdir $@)"
	$(SILENT)$(CC) $(CFLAGS) -shared $< -L$(BUILDDIR) $(CODECLIBS) -lcodec -o $@
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
# 'x' must be kept or you'll have "Win32 error 5"
#     $ fgrep 5 /usr/include/w32api/winerror.h | head -1
#         #define ERROR_ACCESS_DENIED 5L
else
	$(SILENT)chmod -x $@
endif

else	# end of sdl-simulator
###################################################
# This is the win32 simulator version
DLLTOOLFLAGS = --export-all
DLLWRAPFLAGS = -s --entry _DllMain@12 --target=i386-mingw32 -mno-cygwin

$(OBJDIR)/%.codec : $(OBJDIR)/%.o  $(BUILDDIR)/libcodec.a $(OUTPUT)
	@echo "DLL $(notdir $@)"
	$(SILENT)$(DLLTOOL) $(DLLTOOLFLAGS) -z $(OBJDIR)/$*.def $<
	$(SILENT)$(DLLWRAP) $(DLLWRAPFLAGS) --def $(OBJDIR)/$*.def $< \
		$(BUILDDIR)/libcodec.a $(patsubst -l%,$(BUILDDIR)/lib%.a,$(CODECLIBS)) \
		-o $@
ifeq ($(findstring CYGWIN,$(UNAME)),CYGWIN)
# 'x' must be kept or you'll have "Win32 error 5"
#     $ fgrep 5 /usr/include/w32api/winerror.h | head -1
#         #define ERROR_ACCESS_DENIED 5L
else
	$(SILENT)chmod -x $@
endif
endif # end of win32-simulator
endif

endif # end of simulator section

include $(TOOLSDIR)/make.inc

$(BUILDDIR)/libcodec.a:
	@echo "MAKE in codecs/lib"
	$(SILENT)mkdir -p $(OBJDIR)/lib
	$(SILENT)$(MAKE) -C lib OBJDIR=$(OBJDIR)/lib

$(LINKCODEC): $(LDS)
	@echo "build $@"
	$(SILENT)cat $< | $(CC) -DMEMORYSIZE=$(MEMORYSIZE) -DCODEC $(INCLUDES) $(TARGET) $(DEFINES) -E -P - >$@

libmad:
	@echo "MAKE in libmad"
	$(SILENT)mkdir -p $(OBJDIR)/libmad
	$(SILENT)$(MAKE) -C libmad OBJDIR=$(OBJDIR)/libmad OUTPUT=$(BUILDDIR)/libmad.a

liba52:
	@echo "MAKE in liba52"
	$(SILENT)mkdir -p $(OBJDIR)/liba52
	$(SILENT)$(MAKE) -C liba52 OBJDIR=$(OBJDIR)/liba52 OUTPUT=$(BUILDDIR)/liba52.a

libffmpegFLAC:
	@echo "MAKE in libffmpegFLAC"
	$(SILENT)mkdir -p $(OBJDIR)/libffmpegFLAC
	$(SILENT)$(MAKE) -C libffmpegFLAC OBJDIR=$(OBJDIR)/libffmpegFLAC OUTPUT=$(BUILDDIR)/libffmpegFLAC.a

libTremor:
	@echo "MAKE in Tremor"
	$(SILENT)mkdir -p $(OBJDIR)/Tremor
	$(SILENT)$(MAKE) -C Tremor OBJDIR=$(OBJDIR)/Tremor OUTPUT=$(BUILDDIR)/libTremor.a

libwavpack:
	@echo "MAKE in libwavpack"
	$(SILENT)mkdir -p $(OBJDIR)/libwavpack
	$(SILENT)$(MAKE) -C libwavpack OBJDIR=$(OBJDIR)/libwavpack OUTPUT=$(BUILDDIR)/libwavpack.a

dumb:
	@echo "MAKE in dumb"
	$(SILENT)mkdir -p $(OBJDIR)/dumb
	$(SILENT)$(MAKE) -C dumb OBJDIR=$(OBJDIR)/dumb LIBDIR=$(BUILDDIR) OFLAGS="$(CFLAGS)"

libmusepack:
	@echo "MAKE in libmusepack"
	$(SILENT)mkdir -p $(OBJDIR)/libmusepack
	$(SILENT)$(MAKE) -C libmusepack OBJDIR=$(OBJDIR)/libmusepack OUTPUT=$(BUILDDIR)/libmusepack.a

libalac:
	@echo "MAKE in libalac"
	$(SILENT)mkdir -p $(OBJDIR)/libalac
	$(SILENT)$(MAKE) -C libalac OBJDIR=$(OBJDIR)/libalac OUTPUT=$(BUILDDIR)/libalac.a

libm4a:
	@echo "MAKE in libm4a"
	$(SILENT)mkdir -p $(OBJDIR)/libm4a
	$(SILENT)$(MAKE) -C libm4a OBJDIR=$(OBJDIR)/libm4a OUTPUT=$(BUILDDIR)/libm4a.a

libfaad:
	@echo "MAKE in libfaad"
	$(SILENT)mkdir -p $(OBJDIR)/libfaad
	$(SILENT)$(MAKE) -C libfaad OBJDIR=$(OBJDIR)/libfaad OUTPUT=$(BUILDDIR)/libfaad.a

clean:
	@echo "cleaning codecs"
	$(SILENT)rm -fr $(OBJDIR)/libmad $(BUILDDIR)/libmad.a $(OBJDIR)/liba52 $(BUILDDIR)/liba52.a $(OBJDIR)/libffmpegFLAC $(BUILDDIR)/libffmpegFLAC.a $(OBJDIR)/Tremor $(BUILDDIR)/libTremor.a $(OBJDIR)/libwavpack $(BUILDDIR)/libwavpack.a $(OBJDIR)/dumb $(BUILDDIR)/libdumb.a $(BUILDDIR)/libdumbd.a $(OBJDIR)/libmusepack $(BUILDDIR)/libmusepack.a $(OBJDIR)/libalac $(BUILDDIR)/libalac.a $(OBJDIR)/libfaad $(BUILDDIR)/libfaad.a $(OBJDIR)/libm4a $(BUILDDIR)/libm4a.a
	$(SILENT)$(MAKE) -C libmad clean OBJDIR=$(OBJDIR)/libmad
	$(SILENT)$(MAKE) -C liba52 clean OBJDIR=$(OBJDIR)/liba52
	$(SILENT)$(MAKE) -C libffmpegFLAC clean OBJDIR=$(OBJDIR)/libffmpegFLAC
	$(SILENT)$(MAKE) -C Tremor clean OBJDIR=$(OBJDIR)/Tremor
	$(SILENT)$(MAKE) -C libwavpack clean OBJDIR=$(OBJDIR)/libwavpack
	$(SILENT)$(MAKE) -C libmusepack clean OBJDIR=$(OBJDIR)/libmusepack
	$(SILENT)$(MAKE) -C libalac clean OBJDIR=$(OBJDIR)/libalac
	$(SILENT)$(MAKE) -C libfaad clean OBJDIR=$(OBJDIR)/libfaad
	$(SILENT)$(MAKE) -C libm4a clean OBJDIR=$(OBJDIR)/libm4a
	$(SILENT)$(MAKE) -C dumb clean OBJDIR=$(OBJDIR)/dumb
	$(SILENT)$(MAKE) -C lib clean OBJDIR=$(OBJDIR)/lib

ifneq ($(MAKECMDGOALS),clean)
-include $(DEPFILE)
endif
"plugin.h" #ifdef HAVE_LCD_BITMAP #include "recorder/widgets.h" #endif #include <ctype.h> #ifndef SIMULATOR #include <ctype.c> #endif #if PLUGIN_API_VERSION < 3 #error Scrollbar function requires PLUGIN_API_VERSION 3 at least #endif #define WRAP_TRIM 44 /* Max number of spaces to trim (arbitrary) */ #define MAX_COLUMNS 64 /* Max displayable string len (over-estimate) */ #define MAX_WIDTH 910 /* Max line length in WIDE mode */ #define READ_PREV_ZONE 910 /* Arbitrary number less than SMALL_BLOCK_SIZE */ #define SMALL_BLOCK_SIZE 0x1000 /* 4k: Smallest file chunk we will read */ #define LARGE_BLOCK_SIZE 0x2000 /* 8k: Preferable size of file chunk to read */ #define BUFFER_SIZE 0x3000 /* 12k: Mem reserved for buffered file data */ #define TOP_SECTOR buffer #define MID_SECTOR (buffer + SMALL_BLOCK_SIZE) #define BOTTOM_SECTOR (buffer + 2*(SMALL_BLOCK_SIZE)) /* Out-Of-Bounds test for any pointer to data in the buffer */ #define BUFFER_OOB(p) ((p) < buffer || (p) >= buffer_end) /* Does the buffer contain the beginning of the file? */ #define BUFFER_BOF() (file_pos==0) /* Does the buffer contain the end of the file? */ #define BUFFER_EOF() (file_size-file_pos <= BUFFER_SIZE) /* Formula for the endpoint address outside of buffer data */ #define BUFFER_END() \ ((BUFFER_EOF()) ? (file_size-file_pos+buffer) : (buffer+BUFFER_SIZE)) /* Is the entire file being shown in one screen? */ #define ONE_SCREEN_FITS_ALL() \ (next_screen_ptr==NULL && screen_top_ptr==buffer && BUFFER_BOF()) /* Is a scrollbar called for on the current screen? */ #define NEED_SCROLLBAR() ((!(ONE_SCREEN_FITS_ALL())) && \ (view_mode==WIDE? scrollbar_mode[WIDE]==SB_ON: scrollbar_mode[NARROW]==SB_ON)) enum { WRAP=0, CHOP, WORD_MODES } word_mode = 0; static unsigned char *word_mode_str[] = {"wrap", "chop", "words"}; enum { NORMAL=0, JOIN, EXPAND, LINE_MODES } line_mode = 0; static unsigned char *line_mode_str[] = {"normal", "join", "expand", "lines"}; enum { NARROW=0, WIDE, VIEW_MODES } view_mode = 0; static unsigned char *view_mode_str[] = {"narrow", "wide", "view"}; #ifdef HAVE_LCD_BITMAP enum { SB_OFF=0, SB_ON, SCROLLBAR_MODES } scrollbar_mode[VIEW_MODES] = {SB_OFF, SB_ON}; static unsigned char *scrollbar_mode_str[] = {"off", "on", "scrollbar"}; static bool need_scrollbar; enum { NO_OVERLAP=0, OVERLAP, PAGE_MODES } page_mode = 0; static unsigned char *page_mode_str[] = {"don't overlap", "overlap", "pages"}; #endif static unsigned char buffer[BUFFER_SIZE + 1]; static unsigned char line_break[] = {0,0x20,'-',9,0xB,0xC}; static int display_columns; /* number of columns on the display */ static int display_lines; /* number of lines on the display */ static int fd; static long file_size; static bool mac_text; static long file_pos; /* Position of the top of the buffer in the file */ static unsigned char *buffer_end; /*Set to BUFFER_END() when file_pos changes*/ static int max_line_len; static unsigned char *screen_top_ptr; static unsigned char *next_screen_ptr; static unsigned char *next_screen_to_draw_ptr; static unsigned char *next_line_ptr; static struct plugin_api* rb; static unsigned char* find_first_feed(const unsigned char* p, int size) { int i; for (i=0; i < size; i++) if (p[i] == 0) return (unsigned char*) p+i; return NULL; } static unsigned char* find_last_feed(const unsigned char* p, int size) { int i; for (i=size-1; i>=0; i--) if (p[i] == 0) return (unsigned char*) p+i; return NULL; } static unsigned char* find_last_space(const unsigned char* p, int size) { int i, j, k; k = line_mode==JOIN? 0:1; for (i=size-1; i>=0; i--) for (j=k; j < (int) sizeof(line_break); j++) if (p[i] == line_break[j]) return (unsigned char*) p+i; return NULL; } static unsigned char* find_next_line(const unsigned char* cur_line) { const unsigned char *next_line = NULL; int size, i, j, k, chop_len, search_len, spaces, newlines, draw_columns; unsigned char c; if BUFFER_OOB(cur_line) return NULL; #ifdef HAVE_LCD_BITMAP draw_columns = need_scrollbar? display_columns-1: display_columns; #else draw_columns = display_columns; #endif if (view_mode == WIDE) { search_len = chop_len = MAX_WIDTH; } else { /* view_mode == NARROW */ chop_len = draw_columns; search_len = chop_len + 1; } size = BUFFER_OOB(cur_line+search_len) ? buffer_end-cur_line : search_len; if (line_mode == JOIN) { /* Need to scan ahead and possibly increase search_len and size, or possibly set next_line at second hard return in a row. */ next_line = NULL; for (j=k=spaces=newlines=0; j < size; j++) { if (k == MAX_COLUMNS) break; c = cur_line[j]; switch (c) { case ' ': spaces++; break; case 0: if (newlines > 0) { size = j; next_line = cur_line + size - spaces - 1; if (next_line != cur_line) return (unsigned char*) next_line; break; } newlines++; size += spaces; if (BUFFER_OOB(cur_line+size) || size > 2*search_len) return NULL; search_len = size; spaces = 0; k++; break; default: newlines = 0; while (spaces) { spaces--; k++; if (k == MAX_COLUMNS - 1) break; } k++; break; } } } else { /* find first hard return */ next_line = find_first_feed(cur_line, size); } if (next_line == NULL) if (size == search_len) { if (word_mode == WRAP) /* Find last space */ next_line = find_last_space(cur_line, size); if (next_line == NULL) next_line = cur_line + chop_len; else if (word_mode == WRAP) for (i=0; i<WRAP_TRIM && isspace(next_line[0]) && !BUFFER_OOB(next_line); i++) next_line++; } if (line_mode == EXPAND) if (!BUFFER_OOB(next_line)) /* Not Null & not out of bounds */ if (next_line[0] == 0) if (next_line != cur_line) return (unsigned char*) next_line; /* If next_line is pointing to a zero, increment it; i.e., leave the terminator at the end of cur_line. If pointing to a hyphen, increment only if there is room to display the hyphen on current line (won't apply in WIDE mode, since it's guarenteed there won't be room). */ if (!BUFFER_OOB(next_line)) /* Not Null & not out of bounds */ if (next_line[0] == 0 || (next_line[0] == '-' && next_line-cur_line < draw_columns)) next_line++; if (BUFFER_OOB(next_line)) return NULL; return (unsigned char*) next_line; } static unsigned char* find_prev_line(const unsigned char* cur_line) { const unsigned char *prev_line = NULL; const unsigned char *p; if BUFFER_OOB(cur_line) return NULL; /* To wrap consistently at the same places, we must start with a known hard return, then work downwards. We can either search backwards for a hard return, or simply start wrapping downwards from top of buffer. If current line is not near top of buffer, this is a file with long lines (paragraphs). We would need to read earlier sectors before we could decide how to properly wrap the lines above the current line, but it probably is not worth the disk access. Instead, start with top of buffer and wrap down from there. This may result in some lines wrapping at different points from where they wrap when scrolling down. If buffer is at top of file, start at top of buffer. */ if (line_mode == JOIN) prev_line = p = NULL; else prev_line = p = find_last_feed(buffer, cur_line-buffer-1); /* Null means no line feeds in buffer above current line. */ if (prev_line == NULL) if (BUFFER_BOF() || cur_line - buffer > READ_PREV_ZONE) prev_line = p = buffer; /* (else return NULL and read previous block) */ /* Wrap downwards until too far, then use the one before. */ while (p < cur_line && p != NULL) { prev_line = p; p = find_next_line(prev_line); } if (BUFFER_OOB(prev_line)) return NULL; return (unsigned char*) prev_line; } static void fill_buffer(long pos, unsigned char* buf, unsigned size) { /* Read from file and preprocess the data */ /* To minimize disk access, always read on sector boundaries */ unsigned numread, i; bool found_CR = false; rb->lseek(fd, pos, SEEK_SET); numread = rb->read(fd, buf, size); while (rb->button_get(false)); /* clear button queue */ for(i = 0; i < numread; i++) { switch(buf[i]) { case '\r': if (mac_text) { buf[i] = 0; } else { buf[i] = ' '; found_CR = true; } break; case '\n': buf[i] = 0; found_CR = false; break; case 0: /* No break between case 0 and default, intentionally */ buf[i] = ' '; default: if (found_CR) { buf[i - 1] = 0; found_CR = false; mac_text = true; } break; } } } static int read_and_synch(int direction) { /* Read next (or prev) block, and reposition global pointers. */ /* direction: 1 for down (i.e., further into file), -1 for up */ int move_size, move_vector, offset; unsigned char *fill_buf; if (direction == -1) /* up */ { move_size = SMALL_BLOCK_SIZE; offset = 0; fill_buf = TOP_SECTOR; rb->memcpy(BOTTOM_SECTOR, MID_SECTOR, SMALL_BLOCK_SIZE); rb->memcpy(MID_SECTOR, TOP_SECTOR, SMALL_BLOCK_SIZE); } else /* down */ { if (view_mode == WIDE) { /* WIDE mode needs more buffer so we have to read smaller blocks */ move_size = SMALL_BLOCK_SIZE; offset = LARGE_BLOCK_SIZE;