Rename printf to prevent naming conflict. Also change comment to conform with Rockbox...
[kugel-rb.git] / apps / codecs / aiff_enc.c
blobcfbc95196ff4e5cd59e00ec0c7ea4f85b0919d30
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 #include <inttypes.h>
23 #include "codeclib.h"
25 CODEC_ENC_HEADER
27 struct aiff_header
29 uint8_t form_id[4]; /* 00h - 'FORM' */
30 uint32_t form_size; /* 04h - size of file - 8 */
31 uint8_t aiff_id[4]; /* 08h - 'AIFF' */
32 uint8_t comm_id[4]; /* 0Ch - 'COMM' */
33 int32_t comm_size; /* 10h - num_channels through sample_rate
34 (18) */
35 int16_t num_channels; /* 14h - 1=M, 2=S, etc. */
36 uint32_t num_sample_frames; /* 16h - num samples for each channel */
37 int16_t sample_size; /* 1ah - 1-32 bits per sample */
38 uint8_t sample_rate[10]; /* 1ch - IEEE 754 80-bit floating point */
39 uint8_t ssnd_id[4]; /* 26h - "SSND" */
40 int32_t ssnd_size; /* 2ah - size of chunk from offset to
41 end of pcm data */
42 uint32_t offset; /* 2eh - data offset from end of header */
43 uint32_t block_size; /* 32h - pcm data alignment */
44 /* 36h */
45 } __attribute__((packed));
47 #define PCM_DEPTH_BYTES 2
48 #define PCM_DEPTH_BITS 16
49 #define PCM_SAMP_PER_CHUNK 2048
50 #define PCM_CHUNK_SIZE (PCM_SAMP_PER_CHUNK*4)
52 /* Template headers */
53 struct aiff_header aiff_header =
55 { 'F', 'O', 'R', 'M' }, /* form_id */
56 0, /* form_size (*) */
57 { 'A', 'I', 'F', 'F' }, /* aiff_id */
58 { 'C', 'O', 'M', 'M' }, /* comm_id */
59 H_TO_BE32(18), /* comm_size */
60 0, /* num_channels (*) */
61 0, /* num_sample_frames (*) */
62 H_TO_BE16(PCM_DEPTH_BITS), /* sample_size */
63 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* sample_rate (*) */
64 { 'S', 'S', 'N', 'D' }, /* ssnd_id */
65 0, /* ssnd_size (*) */
66 H_TO_BE32(0), /* offset */
67 H_TO_BE32(0), /* block_size */
70 /* (*) updated when finalizing file */
72 static int num_channels IBSS_ATTR;
73 static int rec_mono_mode IBSS_ATTR;
74 static uint32_t sample_rate;
75 static uint32_t enc_size;
76 static int32_t err IBSS_ATTR;
78 /* convert unsigned 32 bit value to 80-bit floating point number */
79 STATICIRAM void uint32_h_to_ieee754_extended_be(uint8_t f[10], uint32_t l)
80 ICODE_ATTR;
81 STATICIRAM void uint32_h_to_ieee754_extended_be(uint8_t f[10], uint32_t l)
83 int32_t exp;
85 ci->memset(f, 0, 10);
87 if (l == 0)
88 return;
90 for (exp = 30; (l & (1ul << 31)) == 0; exp--)
91 l <<= 1;
93 /* sign always zero - bit 79 */
94 /* exponent is 0-31 (normalized: 31 - shift + 16383) - bits 64-78 */
95 f[0] = 0x40;
96 f[1] = (uint8_t)exp;
97 /* mantissa is value left justified with most significant non-zero
98 bit stored in bit 63 - bits 0-63 */
99 *(uint32_t *)&f[2] = htobe32(l);
100 } /* uint32_h_to_ieee754_extended_be */
102 /* called version often - inline */
103 static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR;
104 static inline bool is_file_data_ok(struct enc_file_event_data *data)
106 return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
107 } /* is_file_data_ok */
109 /* called version often - inline */
110 static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
111 static inline bool on_write_chunk(struct enc_file_event_data *data)
113 if (!is_file_data_ok(data))
114 return false;
116 if (data->chunk->enc_data == NULL)
118 #ifdef ROCKBOX_HAS_LOGF
119 ci->logf("aiff enc: NULL data");
120 #endif
121 return true;
124 if (ci->write(data->rec_file, data->chunk->enc_data,
125 data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
126 return false;
128 data->num_pcm_samples += data->chunk->num_pcm;
129 return true;
130 } /* on_write_chunk */
132 static bool on_start_file(struct enc_file_event_data *data)
134 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
135 return false;
137 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
139 if (data->rec_file < 0)
140 return false;
142 /* reset sample count */
143 data->num_pcm_samples = 0;
145 /* write template headers */
146 if (ci->write(data->rec_file, &aiff_header, sizeof (aiff_header))
147 != sizeof (aiff_header))
149 return false;
152 data->new_enc_size += sizeof(aiff_header);
153 return true;
154 } /* on_start_file */
156 static bool on_end_file(struct enc_file_event_data *data)
158 /* update template headers */
159 struct aiff_header hdr;
160 uint32_t data_size;
162 if (!is_file_data_ok(data))
163 return false;
165 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
166 ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr))
168 return false;
171 data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
173 /* 'FORM' chunk */
174 hdr.form_size = htobe32(data_size + sizeof (hdr) - 8);
176 /* 'COMM' chunk */
177 hdr.num_channels = htobe16(num_channels);
178 hdr.num_sample_frames = htobe32(data->num_pcm_samples);
179 uint32_h_to_ieee754_extended_be(hdr.sample_rate, sample_rate);
181 /* 'SSND' chunk */
182 hdr.ssnd_size = htobe32(data_size + 8);
184 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
185 ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) ||
186 ci->close(data->rec_file) != 0)
188 return false;
191 data->rec_file = -1;
193 return true;
194 } /* on_end_file */
196 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
197 ICODE_ATTR;
198 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
200 switch (event)
202 case ENC_WRITE_CHUNK:
203 if (on_write_chunk((struct enc_file_event_data *)data))
204 return;
206 break;
208 case ENC_START_FILE:
209 if (on_start_file((struct enc_file_event_data *)data))
210 return;
212 break;
214 case ENC_END_FILE:
215 if (on_end_file((struct enc_file_event_data *)data))
216 return;
218 break;
220 default:
221 return;
224 /* Something failed above. Signal error back to core. */
225 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
226 } /* enc_events_callback */
228 /* convert native pcm samples to aiff format samples */
229 static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
231 int32_t lr1, lr2;
233 switch(rec_mono_mode)
235 case 1:
236 /* mono = L */
237 lr1 = *(*src)++;
238 lr1 = lr1 >> 16;
239 lr2 = *(*src)++;
240 lr2 = lr2 >> 16;
241 break;
242 case 2:
243 /* mono = R */
244 lr1 = *(*src)++;
245 lr1 = (int16_t)lr1;
246 lr2 = *(*src)++;
247 lr2 = (int16_t)lr2;
248 break;
249 case 0:
250 default:
251 /* mono = (L+R)/2 */
252 lr1 = *(*src)++;
253 lr1 = (int16_t)lr1 + (lr1 >> 16) + err;
254 err = lr1 & 1;
255 lr1 >>= 1;
257 lr2 = *(*src)++;
258 lr2 = (int16_t)lr2 + (lr2 >> 16) + err;
259 err = lr2 & 1;
260 lr2 >>= 1;
261 break;
263 *(*dst)++ = htobe32((lr1 << 16) | (uint16_t)lr2);
264 } /* sample_to_mono */
266 STATICIRAM void chunk_to_aiff_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
267 STATICIRAM void chunk_to_aiff_format(uint32_t *src, uint32_t *dst)
269 if (num_channels == 1)
271 /* On big endian:
272 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
273 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
274 * |MMMMMMMMmmmmmmmm|MMMMMMMMmmmmmmmm|
276 * On little endian:
277 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
278 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
279 * |MMMMMMMMmmmmmmmm|MMMMMMMMmmmmmmmm|
281 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
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);
290 sample_to_mono(&src, &dst);
291 sample_to_mono(&src, &dst);
292 sample_to_mono(&src, &dst);
294 while (src < src_end);
296 else
298 #ifdef ROCKBOX_BIG_ENDIAN
299 /* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
300 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
302 ci->memcpy(dst, src, PCM_CHUNK_SIZE);
303 #else
304 /* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
305 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
307 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
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++);
316 *dst++ = swap_odd_even32(*src++);
317 *dst++ = swap_odd_even32(*src++);
318 *dst++ = swap_odd_even32(*src++);
320 while (src < src_end);
321 #endif
323 } /* chunk_to_aiff_format */
325 static bool init_encoder(void)
327 struct enc_inputs inputs;
328 struct enc_parameters params;
330 if (ci->enc_get_inputs == NULL ||
331 ci->enc_set_parameters == NULL ||
332 ci->enc_get_chunk == NULL ||
333 ci->enc_finish_chunk == NULL ||
334 ci->enc_get_pcm_data == NULL )
335 return false;
337 ci->enc_get_inputs(&inputs);
339 if (inputs.config->afmt != AFMT_AIFF)
340 return false;
342 sample_rate = inputs.sample_rate;
343 num_channels = inputs.num_channels;
344 rec_mono_mode = inputs.rec_mono_mode;
345 err = 0;
347 /* configure the buffer system */
348 params.afmt = AFMT_AIFF;
349 enc_size = PCM_CHUNK_SIZE*inputs.num_channels / 2;
350 params.chunk_size = enc_size;
351 params.enc_sample_rate = sample_rate;
352 params.reserve_bytes = 0;
353 params.events_callback = enc_events_callback;
354 ci->enc_set_parameters(&params);
356 return true;
357 } /* init_encoder */
359 /* main codec entry point */
360 enum codec_status codec_main(void)
362 if (!init_encoder())
364 ci->enc_codec_loaded = -1;
365 return CODEC_ERROR;
368 /* main application waits for this flag during encoder loading */
369 ci->enc_codec_loaded = 1;
371 /* main encoding loop */
372 while(!ci->stop_encoder)
374 uint32_t *src;
376 while ((src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE)) != NULL)
378 struct enc_chunk_hdr *chunk;
380 if (ci->stop_encoder)
381 break;
383 chunk = ci->enc_get_chunk();
384 chunk->enc_size = enc_size;
385 chunk->num_pcm = PCM_SAMP_PER_CHUNK;
386 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
388 chunk_to_aiff_format(src, (uint32_t *)chunk->enc_data);
390 ci->enc_finish_chunk();
391 ci->yield();
394 ci->yield();
397 /* reset parameters to initial state */
398 ci->enc_set_parameters(NULL);
400 /* main application waits for this flag during encoder removing */
401 ci->enc_codec_loaded = 0;
403 return CODEC_OK;
404 } /* codec_start */