New makefile solution: A single invocation of 'make' to build the entire tree. Fully...
[kugel-rb.git] / apps / codecs / aiff_enc.c
blob78c25b8711d557562295e920026509388935270c
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 int rec_mono_mode IBSS_ATTR;
76 static uint32_t sample_rate;
77 static uint32_t enc_size;
78 static int32_t err IBSS_ATTR;
80 /* convert unsigned 32 bit value to 80-bit floating point number */
81 STATICIRAM void uint32_h_to_ieee754_extended_be(uint8_t f[10], uint32_t l)
82 ICODE_ATTR;
83 STATICIRAM void uint32_h_to_ieee754_extended_be(uint8_t f[10], uint32_t l)
85 int32_t exp;
87 ci->memset(f, 0, 10);
89 if (l == 0)
90 return;
92 for (exp = 30; (l & (1ul << 31)) == 0; exp--)
93 l <<= 1;
95 /* sign always zero - bit 79 */
96 /* exponent is 0-31 (normalized: 31 - shift + 16383) - bits 64-78 */
97 f[0] = 0x40;
98 f[1] = (uint8_t)exp;
99 /* mantissa is value left justified with most significant non-zero
100 bit stored in bit 63 - bits 0-63 */
101 *(uint32_t *)&f[2] = htobe32(l);
102 } /* uint32_h_to_ieee754_extended_be */
104 /* called version often - inline */
105 static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR;
106 static inline bool is_file_data_ok(struct enc_file_event_data *data)
108 return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
109 } /* is_file_data_ok */
111 /* called version often - inline */
112 static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
113 static inline bool on_write_chunk(struct enc_file_event_data *data)
115 if (!is_file_data_ok(data))
116 return false;
118 if (data->chunk->enc_data == NULL)
120 #ifdef ROCKBOX_HAS_LOGF
121 ci->logf("aiff enc: NULL data");
122 #endif
123 return true;
126 if (ci->write(data->rec_file, data->chunk->enc_data,
127 data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
128 return false;
130 data->num_pcm_samples += data->chunk->num_pcm;
131 return true;
132 } /* on_write_chunk */
134 static bool on_start_file(struct enc_file_event_data *data)
136 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
137 return false;
139 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC);
141 if (data->rec_file < 0)
142 return false;
144 /* reset sample count */
145 data->num_pcm_samples = 0;
147 /* write template headers */
148 if (ci->write(data->rec_file, &aiff_header, sizeof (aiff_header))
149 != sizeof (aiff_header))
151 return false;
154 data->new_enc_size += sizeof(aiff_header);
155 return true;
156 } /* on_start_file */
158 static bool on_end_file(struct enc_file_event_data *data)
160 /* update template headers */
161 struct aiff_header hdr;
162 uint32_t data_size;
164 if (!is_file_data_ok(data))
165 return false;
167 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
168 ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr))
170 return false;
173 data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
175 /* 'FORM' chunk */
176 hdr.form_size = htobe32(data_size + sizeof (hdr) - 8);
178 /* 'COMM' chunk */
179 hdr.num_channels = htobe16(num_channels);
180 hdr.num_sample_frames = htobe32(data->num_pcm_samples);
181 uint32_h_to_ieee754_extended_be(hdr.sample_rate, sample_rate);
183 /* 'SSND' chunk */
184 hdr.ssnd_size = htobe32(data_size + 8);
186 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
187 ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) ||
188 ci->close(data->rec_file) != 0)
190 return false;
193 data->rec_file = -1;
195 return true;
196 } /* on_end_file */
198 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
199 ICODE_ATTR;
200 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
202 if (event == ENC_WRITE_CHUNK)
204 if (on_write_chunk((struct enc_file_event_data *)data))
205 return;
207 else if (event == ENC_START_FILE)
209 if (on_start_file((struct enc_file_event_data *)data))
210 return;
212 else if (event == ENC_END_FILE)
214 if (on_end_file((struct enc_file_event_data *)data))
215 return;
217 else
219 return;
222 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
223 } /* enc_events_callback */
225 /* convert native pcm samples to aiff format samples */
226 static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
228 int32_t lr1, lr2;
230 switch(rec_mono_mode)
232 case 1:
233 /* mono = L */
234 lr1 = *(*src)++;
235 lr1 = lr1 >> 16;
236 lr2 = *(*src)++;
237 lr2 = lr2 >> 16;
238 break;
239 case 2:
240 /* mono = R */
241 lr1 = *(*src)++;
242 lr1 = (int16_t)lr1;
243 lr2 = *(*src)++;
244 lr2 = (int16_t)lr2;
245 break;
246 case 0:
247 default:
248 /* mono = (L+R)/2 */
249 lr1 = *(*src)++;
250 lr1 = (int16_t)lr1 + (lr1 >> 16) + err;
251 err = lr1 & 1;
252 lr1 >>= 1;
254 lr2 = *(*src)++;
255 lr2 = (int16_t)lr2 + (lr2 >> 16) + err;
256 err = lr2 & 1;
257 lr2 >>= 1;
258 break;
260 *(*dst)++ = swap_odd_even_be32((lr1 << 16) | (uint16_t)lr2);
261 } /* sample_to_mono */
263 STATICIRAM void chunk_to_aiff_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
264 STATICIRAM void chunk_to_aiff_format(uint32_t *src, uint32_t *dst)
266 if (num_channels == 1)
268 /* On big endian:
269 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
270 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
271 * |MMMMMMMMmmmmmmmm|MMMMMMMMmmmmmmmm|
273 * On little endian:
274 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
275 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
276 * |MMMMMMMMmmmmmmmm|MMMMMMMMmmmmmmmm|
278 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
282 sample_to_mono(&src, &dst);
283 sample_to_mono(&src, &dst);
284 sample_to_mono(&src, &dst);
285 sample_to_mono(&src, &dst);
286 sample_to_mono(&src, &dst);
287 sample_to_mono(&src, &dst);
288 sample_to_mono(&src, &dst);
289 sample_to_mono(&src, &dst);
291 while (src < src_end);
293 else
295 #ifdef ROCKBOX_BIG_ENDIAN
296 /* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
297 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
299 ci->memcpy(dst, src, PCM_CHUNK_SIZE);
300 #else
301 /* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
302 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
304 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
308 *dst++ = swap_odd_even32(*src++);
309 *dst++ = swap_odd_even32(*src++);
310 *dst++ = swap_odd_even32(*src++);
311 *dst++ = swap_odd_even32(*src++);
312 *dst++ = swap_odd_even32(*src++);
313 *dst++ = swap_odd_even32(*src++);
314 *dst++ = swap_odd_even32(*src++);
315 *dst++ = swap_odd_even32(*src++);
317 while (src < src_end);
318 #endif
320 } /* chunk_to_aiff_format */
322 static bool init_encoder(void)
324 struct enc_inputs inputs;
325 struct enc_parameters params;
327 if (ci->enc_get_inputs == NULL ||
328 ci->enc_set_parameters == NULL ||
329 ci->enc_get_chunk == NULL ||
330 ci->enc_finish_chunk == NULL ||
331 ci->enc_get_pcm_data == NULL )
332 return false;
334 ci->enc_get_inputs(&inputs);
336 if (inputs.config->afmt != AFMT_AIFF)
337 return false;
339 sample_rate = inputs.sample_rate;
340 num_channels = inputs.num_channels;
341 rec_mono_mode = inputs.rec_mono_mode;
342 err = 0;
344 /* configure the buffer system */
345 params.afmt = AFMT_AIFF;
346 enc_size = PCM_CHUNK_SIZE*inputs.num_channels / 2;
347 params.chunk_size = enc_size;
348 params.enc_sample_rate = sample_rate;
349 params.reserve_bytes = 0;
350 params.events_callback = enc_events_callback;
351 ci->enc_set_parameters(&params);
353 return true;
354 } /* init_encoder */
356 /* main codec entry point */
357 enum codec_status codec_main(void)
359 if (!init_encoder())
361 ci->enc_codec_loaded = -1;
362 return CODEC_ERROR;
365 /* main application waits for this flag during encoder loading */
366 ci->enc_codec_loaded = 1;
368 /* main encoding loop */
369 while(!ci->stop_encoder)
371 uint32_t *src;
373 while ((src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE)) != NULL)
375 struct enc_chunk_hdr *chunk;
377 if (ci->stop_encoder)
378 break;
380 chunk = ci->enc_get_chunk();
381 chunk->enc_size = enc_size;
382 chunk->num_pcm = PCM_SAMP_PER_CHUNK;
383 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
385 chunk_to_aiff_format(src, (uint32_t *)chunk->enc_data);
387 ci->enc_finish_chunk();
388 ci->yield();
391 ci->yield();
394 /* reset parameters to initial state */
395 ci->enc_set_parameters(NULL);
397 /* main application waits for this flag during encoder removing */
398 ci->enc_codec_loaded = 0;
400 return CODEC_OK;
401 } /* codec_start */
403 #endif /* ndef SIMULATOR */