Theme Editor: Added support for upper-case wps file extensions
[kugel-rb.git] / apps / codecs / wav_enc.c
blobb11c3a2e99312f56e47f102666f7dd4fa077b299
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 riff_header
29 uint8_t riff_id[4]; /* 00h - "RIFF" */
30 uint32_t riff_size; /* 04h - sz following headers + data_size */
31 /* format header */
32 uint8_t format[4]; /* 08h - "WAVE" */
33 uint8_t format_id[4]; /* 0Ch - "fmt " */
34 uint32_t format_size; /* 10h - 16 for PCM (sz format data) */
35 /* format data */
36 uint16_t audio_format; /* 14h - 1=PCM */
37 uint16_t num_channels; /* 16h - 1=M, 2=S, etc. */
38 uint32_t sample_rate; /* 18h - HZ */
39 uint32_t byte_rate; /* 1Ch - num_channels*sample_rate*bits_per_sample/8 */
40 uint16_t block_align; /* 20h - num_channels*bits_per_samples/8 */
41 uint16_t bits_per_sample; /* 22h - 8=8 bits, 16=16 bits, etc. */
42 /* Not for audio_format=1 (PCM) */
43 /* unsigned short extra_param_size; 24h - size of extra data */
44 /* unsigned char *extra_params; */
45 /* data header */
46 uint8_t data_id[4]; /* 24h - "data" */
47 uint32_t data_size; /* 28h - num_samples*num_channels*bits_per_sample/8 */
48 /* unsigned char *data; 2ch - actual sound data */
49 } __attribute__((packed));
51 #define RIFF_FMT_HEADER_SIZE 12 /* format -> format_size */
52 #define RIFF_FMT_DATA_SIZE 16 /* audio_format -> bits_per_sample */
53 #define RIFF_DATA_HEADER_SIZE 8 /* data_id -> data_size */
55 #define PCM_DEPTH_BYTES 2
56 #define PCM_DEPTH_BITS 16
57 #define PCM_SAMP_PER_CHUNK 2048
58 #define PCM_CHUNK_SIZE (PCM_SAMP_PER_CHUNK*4)
60 static int num_channels IBSS_ATTR;
61 static int rec_mono_mode IBSS_ATTR;
62 static uint32_t sample_rate;
63 static uint32_t enc_size;
64 static int32_t err IBSS_ATTR;
66 static const struct riff_header riff_header =
68 /* "RIFF" header */
69 { 'R', 'I', 'F', 'F' }, /* riff_id */
70 0, /* riff_size (*) */
71 /* format header */
72 { 'W', 'A', 'V', 'E' }, /* format */
73 { 'f', 'm', 't', ' ' }, /* format_id */
74 H_TO_LE32(16), /* format_size */
75 /* format data */
76 H_TO_LE16(1), /* audio_format */
77 0, /* num_channels (*) */
78 0, /* sample_rate (*) */
79 0, /* byte_rate (*) */
80 0, /* block_align (*) */
81 H_TO_LE16(PCM_DEPTH_BITS), /* bits_per_sample */
82 /* data header */
83 { 'd', 'a', 't', 'a' }, /* data_id */
84 0 /* data_size (*) */
85 /* (*) updated during ENC_END_FILE event */
88 /* called version often - inline */
89 static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR;
90 static inline bool is_file_data_ok(struct enc_file_event_data *data)
92 return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
93 } /* is_file_data_ok */
95 /* called version often - inline */
96 static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
97 static inline bool on_write_chunk(struct enc_file_event_data *data)
99 if (!is_file_data_ok(data))
100 return false;
102 if (data->chunk->enc_data == NULL)
104 #ifdef ROCKBOX_HAS_LOGF
105 ci->logf("wav enc: NULL data");
106 #endif
107 return true;
110 if (ci->write(data->rec_file, data->chunk->enc_data,
111 data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
112 return false;
114 data->num_pcm_samples += data->chunk->num_pcm;
115 return true;
116 } /* on_write_chunk */
118 static bool on_start_file(struct enc_file_event_data *data)
120 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
121 return false;
123 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
125 if (data->rec_file < 0)
126 return false;
128 /* reset sample count */
129 data->num_pcm_samples = 0;
131 /* write template header */
132 if (ci->write(data->rec_file, &riff_header, sizeof (riff_header))
133 != sizeof (riff_header))
135 return false;
138 data->new_enc_size += sizeof (riff_header);
139 return true;
140 } /* on_start_file */
142 static bool on_end_file(struct enc_file_event_data *data)
144 /* update template header */
145 struct riff_header hdr;
146 uint32_t data_size;
148 if (data->rec_file < 0)
149 return false; /* file already closed, nothing more we can do */
151 /* always _try_ to write the file header, even on error */
152 if ((ci->lseek(data->rec_file, 0, SEEK_SET)) ||
153 (ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr)))
155 return false;
158 data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
160 /* "RIFF" header */
161 hdr.riff_size = htole32(RIFF_FMT_HEADER_SIZE + RIFF_FMT_DATA_SIZE
162 + RIFF_DATA_HEADER_SIZE + data_size);
164 /* format data */
165 hdr.num_channels = htole16(num_channels);
166 hdr.sample_rate = htole32(sample_rate);
167 hdr.byte_rate = htole32(sample_rate*num_channels* PCM_DEPTH_BYTES);
168 hdr.block_align = htole16(num_channels*PCM_DEPTH_BYTES);
170 /* data header */
171 hdr.data_size = htole32(data_size);
173 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
174 ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) ||
175 ci->close(data->rec_file) != 0)
177 return false;
180 data->rec_file = -1;
182 return true;
183 } /* on_end_file */
185 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
186 ICODE_ATTR;
187 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
189 switch (event)
191 case ENC_WRITE_CHUNK:
192 if (on_write_chunk((struct enc_file_event_data *)data))
193 return;
195 break;
197 case ENC_START_FILE:
198 if (on_start_file((struct enc_file_event_data *)data))
199 return;
201 break;
203 case ENC_END_FILE:
204 if (on_end_file((struct enc_file_event_data *)data))
205 return;
207 break;
209 default:
210 return;
213 /* Something failed above. Signal error back to core. */
214 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
215 } /* enc_events_callback */
217 /* convert native pcm samples to wav format samples */
218 static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
220 int32_t lr1, lr2;
222 switch(rec_mono_mode)
224 case 1:
225 /* mono = L */
226 lr1 = *(*src)++;
227 lr1 = lr1 >> 16;
228 lr2 = *(*src)++;
229 lr2 = lr2 >> 16;
230 break;
231 case 2:
232 /* mono = R */
233 lr1 = *(*src)++;
234 lr1 = (uint16_t)lr1;
235 lr2 = *(*src)++;
236 lr2 = (uint16_t)lr2;
237 break;
238 case 0:
239 default:
240 /* mono = (L+R)/2 */
241 lr1 = *(*src)++;
242 lr1 = (int16_t)lr1 + (lr1 >> 16) + err;
243 err = lr1 & 1;
244 lr1 >>= 1;
246 lr2 = *(*src)++;
247 lr2 = (int16_t)lr2 + (lr2 >> 16) + err;
248 err = lr2 & 1;
249 lr2 >>= 1;
250 break;
252 *(*dst)++ = htole32((lr2 << 16) | (uint16_t)lr1);
253 } /* sample_to_mono */
255 STATICIRAM void chunk_to_wav_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
256 STATICIRAM void chunk_to_wav_format(uint32_t *src, uint32_t *dst)
258 if (num_channels == 1)
260 /* On big endian:
261 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
262 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
263 * |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
265 * On little endian:
266 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
267 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
268 * |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
270 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
274 sample_to_mono(&src, &dst);
275 sample_to_mono(&src, &dst);
276 sample_to_mono(&src, &dst);
277 sample_to_mono(&src, &dst);
278 sample_to_mono(&src, &dst);
279 sample_to_mono(&src, &dst);
280 sample_to_mono(&src, &dst);
281 sample_to_mono(&src, &dst);
283 while (src < src_end);
285 else
287 #ifdef ROCKBOX_BIG_ENDIAN
288 /* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
289 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
291 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
295 *dst++ = swap_odd_even32(*src++);
296 *dst++ = swap_odd_even32(*src++);
297 *dst++ = swap_odd_even32(*src++);
298 *dst++ = swap_odd_even32(*src++);
299 *dst++ = swap_odd_even32(*src++);
300 *dst++ = swap_odd_even32(*src++);
301 *dst++ = swap_odd_even32(*src++);
302 *dst++ = swap_odd_even32(*src++);
304 while (src < src_end);
305 #else
306 /* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
307 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
309 ci->memcpy(dst, src, PCM_CHUNK_SIZE);
310 #endif
312 } /* chunk_to_wav_format */
314 static bool init_encoder(void)
316 struct enc_inputs inputs;
317 struct enc_parameters params;
319 if (ci->enc_get_inputs == NULL ||
320 ci->enc_set_parameters == NULL ||
321 ci->enc_get_chunk == NULL ||
322 ci->enc_finish_chunk == NULL ||
323 ci->enc_get_pcm_data == NULL )
324 return false;
326 ci->enc_get_inputs(&inputs);
328 if (inputs.config->afmt != AFMT_PCM_WAV)
329 return false;
331 sample_rate = inputs.sample_rate;
332 num_channels = inputs.num_channels;
333 rec_mono_mode = inputs.rec_mono_mode;
334 err = 0;
336 /* configure the buffer system */
337 params.afmt = AFMT_PCM_WAV;
338 enc_size = PCM_CHUNK_SIZE*inputs.num_channels / 2;
339 params.chunk_size = enc_size;
340 params.enc_sample_rate = sample_rate;
341 params.reserve_bytes = 0;
342 params.events_callback = enc_events_callback;
343 ci->enc_set_parameters(&params);
345 return true;
346 } /* init_encoder */
348 /* main codec entry point */
349 enum codec_status codec_main(void)
351 if (!init_encoder())
353 ci->enc_codec_loaded = -1;
354 return CODEC_ERROR;
357 /* main application waits for this flag during encoder loading */
358 ci->enc_codec_loaded = 1;
360 /* main encoding loop */
361 while(!ci->stop_encoder)
363 uint32_t *src;
365 while ((src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE)) != NULL)
367 struct enc_chunk_hdr *chunk;
369 if (ci->stop_encoder)
370 break;
372 chunk = ci->enc_get_chunk();
373 chunk->enc_size = enc_size;
374 chunk->num_pcm = PCM_SAMP_PER_CHUNK;
375 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
377 chunk_to_wav_format(src, (uint32_t *)chunk->enc_data);
379 ci->enc_finish_chunk();
380 ci->yield();
383 ci->yield();
386 /* reset parameters to initial state */
387 ci->enc_set_parameters(NULL);
389 /* main application waits for this flag during encoder removing */
390 ci->enc_codec_loaded = 0;
392 return CODEC_OK;
393 } /* codec_start */