Put the WMA windowing and output buffer into IRAM on targets with > 96KB of it. ...
[kugel-rb.git] / apps / codecs / aiff_enc.c
blob7e085d57210da871f6118988faf7a867f11a7ece
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Antonius Hellmann
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #ifndef SIMULATOR
24 #include <inttypes.h>
25 #include "codeclib.h"
27 CODEC_ENC_HEADER
29 struct aiff_header
31 uint8_t form_id[4]; /* 00h - 'FORM' */
32 uint32_t form_size; /* 04h - size of file - 8 */
33 uint8_t aiff_id[4]; /* 08h - 'AIFF' */
34 uint8_t comm_id[4]; /* 0Ch - 'COMM' */
35 int32_t comm_size; /* 10h - num_channels through sample_rate
36 (18) */
37 int16_t num_channels; /* 14h - 1=M, 2=S, etc. */
38 uint32_t num_sample_frames; /* 16h - num samples for each channel */
39 int16_t sample_size; /* 1ah - 1-32 bits per sample */
40 uint8_t sample_rate[10]; /* 1ch - IEEE 754 80-bit floating point */
41 uint8_t ssnd_id[4]; /* 26h - "SSND" */
42 int32_t ssnd_size; /* 2ah - size of chunk from offset to
43 end of pcm data */
44 uint32_t offset; /* 2eh - data offset from end of header */
45 uint32_t block_size; /* 32h - pcm data alignment */
46 /* 36h */
47 } __attribute__((packed));
49 #define PCM_DEPTH_BYTES 2
50 #define PCM_DEPTH_BITS 16
51 #define PCM_SAMP_PER_CHUNK 2048
52 #define PCM_CHUNK_SIZE (PCM_SAMP_PER_CHUNK*4)
54 /* Template headers */
55 struct aiff_header aiff_header =
57 { 'F', 'O', 'R', 'M' }, /* form_id */
58 0, /* form_size (*) */
59 { 'A', 'I', 'F', 'F' }, /* aiff_id */
60 { 'C', 'O', 'M', 'M' }, /* comm_id */
61 H_TO_BE32(18), /* comm_size */
62 0, /* num_channels (*) */
63 0, /* num_sample_frames (*) */
64 H_TO_BE16(PCM_DEPTH_BITS), /* sample_size */
65 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* sample_rate (*) */
66 { 'S', 'S', 'N', 'D' }, /* ssnd_id */
67 0, /* ssnd_size (*) */
68 H_TO_BE32(0), /* offset */
69 H_TO_BE32(0), /* block_size */
72 /* (*) updated when finalizing file */
74 static int num_channels IBSS_ATTR;
75 static uint32_t sample_rate;
76 static uint32_t enc_size;
77 static int32_t err IBSS_ATTR;
79 /* convert unsigned 32 bit value to 80-bit floating point number */
80 STATICIRAM void uint32_h_to_ieee754_extended_be(uint8_t f[10], uint32_t l)
81 ICODE_ATTR;
82 STATICIRAM void uint32_h_to_ieee754_extended_be(uint8_t f[10], uint32_t l)
84 int32_t exp;
86 ci->memset(f, 0, 10);
88 if (l == 0)
89 return;
91 for (exp = 30; (l & (1ul << 31)) == 0; exp--)
92 l <<= 1;
94 /* sign always zero - bit 79 */
95 /* exponent is 0-31 (normalized: 31 - shift + 16383) - bits 64-78 */
96 f[0] = 0x40;
97 f[1] = (uint8_t)exp;
98 /* mantissa is value left justified with most significant non-zero
99 bit stored in bit 63 - bits 0-63 */
100 *(uint32_t *)&f[2] = htobe32(l);
101 } /* uint32_h_to_ieee754_extended_be */
103 /* called version often - inline */
104 static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR;
105 static inline bool is_file_data_ok(struct enc_file_event_data *data)
107 return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
108 } /* is_file_data_ok */
110 /* called version often - inline */
111 static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
112 static inline bool on_write_chunk(struct enc_file_event_data *data)
114 if (!is_file_data_ok(data))
115 return false;
117 if (data->chunk->enc_data == NULL)
119 #ifdef ROCKBOX_HAS_LOGF
120 ci->logf("aiff enc: NULL data");
121 #endif
122 return true;
125 if (ci->write(data->rec_file, data->chunk->enc_data,
126 data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
127 return false;
129 data->num_pcm_samples += data->chunk->num_pcm;
130 return true;
131 } /* on_write_chunk */
133 static bool on_start_file(struct enc_file_event_data *data)
135 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
136 return false;
138 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC);
140 if (data->rec_file < 0)
141 return false;
143 /* reset sample count */
144 data->num_pcm_samples = 0;
146 /* write template headers */
147 if (ci->write(data->rec_file, &aiff_header, sizeof (aiff_header))
148 != sizeof (aiff_header))
150 return false;
153 data->new_enc_size += sizeof(aiff_header);
154 return true;
155 } /* on_start_file */
157 static bool on_end_file(struct enc_file_event_data *data)
159 /* update template headers */
160 struct aiff_header hdr;
161 uint32_t data_size;
163 if (!is_file_data_ok(data))
164 return false;
166 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
167 ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr))
169 return false;
172 data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
174 /* 'FORM' chunk */
175 hdr.form_size = htobe32(data_size + sizeof (hdr) - 8);
177 /* 'COMM' chunk */
178 hdr.num_channels = htobe16(num_channels);
179 hdr.num_sample_frames = htobe32(data->num_pcm_samples);
180 uint32_h_to_ieee754_extended_be(hdr.sample_rate, sample_rate);
182 /* 'SSND' chunk */
183 hdr.ssnd_size = htobe32(data_size + 8);
185 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
186 ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) ||
187 ci->close(data->rec_file) != 0)
189 return false;
192 data->rec_file = -1;
194 return true;
195 } /* on_end_file */
197 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
198 ICODE_ATTR;
199 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
201 if (event == ENC_WRITE_CHUNK)
203 if (on_write_chunk((struct enc_file_event_data *)data))
204 return;
206 else if (event == ENC_START_FILE)
208 if (on_start_file((struct enc_file_event_data *)data))
209 return;
211 else if (event == ENC_END_FILE)
213 if (on_end_file((struct enc_file_event_data *)data))
214 return;
216 else
218 return;
221 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
222 } /* enc_events_callback */
224 /* convert native pcm samples to aiff format samples */
225 static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
227 int32_t lr1, lr2;
229 lr1 = *(*src)++;
230 lr1 = (int16_t)lr1 + (lr1 >> 16) + err;
231 err = lr1 & 1;
232 lr1 >>= 1;
234 lr2 = *(*src)++;
235 lr2 = (int16_t)lr2 + (lr2 >> 16) + err;
236 err = lr2 & 1;
237 lr2 >>= 1;
238 *(*dst)++ = swap_odd_even_le32((lr1 << 16) | (uint16_t)lr2);
239 } /* sample_to_mono */
241 STATICIRAM void chunk_to_aiff_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
242 STATICIRAM void chunk_to_aiff_format(uint32_t *src, uint32_t *dst)
244 if (num_channels == 1)
246 /* On big endian:
247 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
248 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
249 * |MMMMMMMMmmmmmmmm|MMMMMMMMmmmmmmmm|
251 * On little endian:
252 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
253 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
254 * |MMMMMMMMmmmmmmmm|MMMMMMMMmmmmmmmm|
256 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
260 sample_to_mono(&src, &dst);
261 sample_to_mono(&src, &dst);
262 sample_to_mono(&src, &dst);
263 sample_to_mono(&src, &dst);
264 sample_to_mono(&src, &dst);
265 sample_to_mono(&src, &dst);
266 sample_to_mono(&src, &dst);
267 sample_to_mono(&src, &dst);
269 while (src < src_end);
271 else
273 #ifdef ROCKBOX_BIG_ENDIAN
274 /* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
275 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
277 ci->memcpy(dst, src, PCM_CHUNK_SIZE);
278 #else
279 /* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
280 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
282 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
286 *dst++ = swap_odd_even32(*src++);
287 *dst++ = swap_odd_even32(*src++);
288 *dst++ = swap_odd_even32(*src++);
289 *dst++ = swap_odd_even32(*src++);
290 *dst++ = swap_odd_even32(*src++);
291 *dst++ = swap_odd_even32(*src++);
292 *dst++ = swap_odd_even32(*src++);
293 *dst++ = swap_odd_even32(*src++);
295 while (src < src_end);
296 #endif
298 } /* chunk_to_aiff_format */
300 static bool init_encoder(void)
302 struct enc_inputs inputs;
303 struct enc_parameters params;
305 if (ci->enc_get_inputs == NULL ||
306 ci->enc_set_parameters == NULL ||
307 ci->enc_get_chunk == NULL ||
308 ci->enc_finish_chunk == NULL ||
309 ci->enc_get_pcm_data == NULL )
310 return false;
312 ci->enc_get_inputs(&inputs);
314 if (inputs.config->afmt != AFMT_AIFF)
315 return false;
317 sample_rate = inputs.sample_rate;
318 num_channels = inputs.num_channels;
319 err = 0;
321 /* configure the buffer system */
322 params.afmt = AFMT_AIFF;
323 enc_size = PCM_CHUNK_SIZE*inputs.num_channels / 2;
324 params.chunk_size = enc_size;
325 params.enc_sample_rate = sample_rate;
326 params.reserve_bytes = 0;
327 params.events_callback = enc_events_callback;
328 ci->enc_set_parameters(&params);
330 return true;
331 } /* init_encoder */
333 /* main codec entry point */
334 enum codec_status codec_main(void)
336 if (!init_encoder())
338 ci->enc_codec_loaded = -1;
339 return CODEC_ERROR;
342 /* main application waits for this flag during encoder loading */
343 ci->enc_codec_loaded = 1;
345 /* main encoding loop */
346 while(!ci->stop_encoder)
348 uint32_t *src;
350 while ((src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE)) != NULL)
352 struct enc_chunk_hdr *chunk;
354 if (ci->stop_encoder)
355 break;
357 chunk = ci->enc_get_chunk();
358 chunk->enc_size = enc_size;
359 chunk->num_pcm = PCM_SAMP_PER_CHUNK;
360 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
362 chunk_to_aiff_format(src, (uint32_t *)chunk->enc_data);
364 ci->enc_finish_chunk();
365 ci->yield();
368 ci->yield();
371 /* reset parameters to initial state */
372 ci->enc_set_parameters(NULL);
374 /* main application waits for this flag during encoder removing */
375 ci->enc_codec_loaded = 0;
377 return CODEC_OK;
378 } /* codec_start */
380 #endif /* ndef SIMULATOR */