Redo my previous segfault fix in a better way.
[Rockbox.git] / apps / codecs / wav_enc.c
blob5932fd319d7880d11996120baf5a6f3dacc35831
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 Antonius Hellmann
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #ifndef SIMULATOR
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 uint32_t sample_rate;
62 static uint32_t enc_size;
63 static int32_t err IBSS_ATTR;
65 static const struct riff_header riff_header =
67 /* "RIFF" header */
68 { 'R', 'I', 'F', 'F' }, /* riff_id */
69 0, /* riff_size (*) */
70 /* format header */
71 { 'W', 'A', 'V', 'E' }, /* format */
72 { 'f', 'm', 't', ' ' }, /* format_id */
73 H_TO_LE32(16), /* format_size */
74 /* format data */
75 H_TO_LE16(1), /* audio_format */
76 0, /* num_channels (*) */
77 0, /* sample_rate (*) */
78 0, /* byte_rate (*) */
79 0, /* block_align (*) */
80 H_TO_LE16(PCM_DEPTH_BITS), /* bits_per_sample */
81 /* data header */
82 { 'd', 'a', 't', 'a' }, /* data_id */
83 0 /* data_size (*) */
84 /* (*) updated during ENC_END_FILE event */
87 /* called version often - inline */
88 static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR;
89 static inline bool is_file_data_ok(struct enc_file_event_data *data)
91 return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
92 } /* is_file_data_ok */
94 /* called version often - inline */
95 static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
96 static inline bool on_write_chunk(struct enc_file_event_data *data)
98 if (!is_file_data_ok(data))
99 return false;
101 if (data->chunk->enc_data == NULL)
103 #ifdef ROCKBOX_HAS_LOGF
104 ci->logf("wav enc: NULL data");
105 #endif
106 return true;
109 if (ci->write(data->rec_file, data->chunk->enc_data,
110 data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
111 return false;
113 data->num_pcm_samples += data->chunk->num_pcm;
114 return true;
115 } /* on_write_chunk */
117 static bool on_start_file(struct enc_file_event_data *data)
119 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
120 return false;
122 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC);
124 if (data->rec_file < 0)
125 return false;
127 /* reset sample count */
128 data->num_pcm_samples = 0;
130 /* write template header */
131 if (ci->write(data->rec_file, &riff_header, sizeof (riff_header))
132 != sizeof (riff_header))
134 return false;
137 data->new_enc_size += sizeof (riff_header);
138 return true;
139 } /* on_start_file */
141 static bool on_end_file(struct enc_file_event_data *data)
143 /* update template header */
144 struct riff_header hdr;
145 uint32_t data_size;
147 if (data->rec_file < 0)
148 return false; /* file already closed, nothing more we can do */
150 /* always _try_ to write the file header, even on error */
151 if ((ci->lseek(data->rec_file, 0, SEEK_SET)) ||
152 (ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr)))
154 return false;
157 data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
159 /* "RIFF" header */
160 hdr.riff_size = htole32(RIFF_FMT_HEADER_SIZE + RIFF_FMT_DATA_SIZE
161 + RIFF_DATA_HEADER_SIZE + data_size);
163 /* format data */
164 hdr.num_channels = htole16(num_channels);
165 hdr.sample_rate = htole32(sample_rate);
166 hdr.byte_rate = htole32(sample_rate*num_channels* PCM_DEPTH_BYTES);
167 hdr.block_align = htole16(num_channels*PCM_DEPTH_BYTES);
169 /* data header */
170 hdr.data_size = htole32(data_size);
172 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
173 ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) ||
174 ci->close(data->rec_file) != 0)
176 return false;
179 data->rec_file = -1;
181 return true;
182 } /* on_end_file */
184 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
185 ICODE_ATTR;
186 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
188 if (event == ENC_WRITE_CHUNK)
190 if (on_write_chunk((struct enc_file_event_data *)data))
191 return;
193 else if (event == ENC_START_FILE)
195 if (on_start_file((struct enc_file_event_data *)data))
196 return;
198 else if (event == ENC_END_FILE)
200 if (on_end_file((struct enc_file_event_data *)data))
201 return;
203 else
205 return;
208 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
209 } /* enc_events_callback */
211 /* convert native pcm samples to wav format samples */
212 static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
214 int32_t lr1, lr2;
216 lr1 = *(*src)++;
217 lr1 = (int16_t)lr1 + (lr1 >> 16) + err;
218 err = lr1 & 1;
219 lr1 >>= 1;
221 lr2 = *(*src)++;
222 lr2 = (int16_t)lr2 + (lr2 >> 16) + err;
223 err = lr2 & 1;
224 lr2 >>= 1;
225 *(*dst)++ = swap_odd_even_be32((lr1 << 16) | (uint16_t)lr2);
226 } /* sample_to_mono */
228 STATICIRAM void chunk_to_wav_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
229 STATICIRAM void chunk_to_wav_format(uint32_t *src, uint32_t *dst)
231 if (num_channels == 1)
233 /* On big endian:
234 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
235 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
236 * |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
238 * On little endian:
239 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
240 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
241 * |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
243 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
247 sample_to_mono(&src, &dst);
248 sample_to_mono(&src, &dst);
249 sample_to_mono(&src, &dst);
250 sample_to_mono(&src, &dst);
251 sample_to_mono(&src, &dst);
252 sample_to_mono(&src, &dst);
253 sample_to_mono(&src, &dst);
254 sample_to_mono(&src, &dst);
256 while (src < src_end);
258 else
260 #ifdef ROCKBOX_BIG_ENDIAN
261 /* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
262 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
264 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
268 *dst++ = swap_odd_even32(*src++);
269 *dst++ = swap_odd_even32(*src++);
270 *dst++ = swap_odd_even32(*src++);
271 *dst++ = swap_odd_even32(*src++);
272 *dst++ = swap_odd_even32(*src++);
273 *dst++ = swap_odd_even32(*src++);
274 *dst++ = swap_odd_even32(*src++);
275 *dst++ = swap_odd_even32(*src++);
277 while (src < src_end);
278 #else
279 /* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
280 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
282 ci->memcpy(dst, src, PCM_CHUNK_SIZE);
283 #endif
285 } /* chunk_to_wav_format */
287 static bool init_encoder(void)
289 struct enc_inputs inputs;
290 struct enc_parameters params;
292 if (ci->enc_get_inputs == NULL ||
293 ci->enc_set_parameters == NULL ||
294 ci->enc_get_chunk == NULL ||
295 ci->enc_finish_chunk == NULL ||
296 ci->enc_get_pcm_data == NULL )
297 return false;
299 ci->enc_get_inputs(&inputs);
301 if (inputs.config->afmt != AFMT_PCM_WAV)
302 return false;
304 sample_rate = inputs.sample_rate;
305 num_channels = inputs.num_channels;
306 err = 0;
308 /* configure the buffer system */
309 params.afmt = AFMT_PCM_WAV;
310 enc_size = PCM_CHUNK_SIZE*inputs.num_channels / 2;
311 params.chunk_size = enc_size;
312 params.enc_sample_rate = sample_rate;
313 params.reserve_bytes = 0;
314 params.events_callback = enc_events_callback;
315 ci->enc_set_parameters(&params);
317 return true;
318 } /* init_encoder */
320 /* main codec entry point */
321 enum codec_status codec_main(void)
323 if (!init_encoder())
325 ci->enc_codec_loaded = -1;
326 return CODEC_ERROR;
329 /* main application waits for this flag during encoder loading */
330 ci->enc_codec_loaded = 1;
332 /* main encoding loop */
333 while(!ci->stop_encoder)
335 uint32_t *src;
337 while ((src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE)) != NULL)
339 struct enc_chunk_hdr *chunk;
341 if (ci->stop_encoder)
342 break;
344 chunk = ci->enc_get_chunk();
345 chunk->enc_size = enc_size;
346 chunk->num_pcm = PCM_SAMP_PER_CHUNK;
347 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
349 chunk_to_wav_format(src, (uint32_t *)chunk->enc_data);
351 ci->enc_finish_chunk();
352 ci->yield();
355 ci->yield();
358 /* reset parameters to initial state */
359 ci->enc_set_parameters(NULL);
361 /* main application waits for this flag during encoder removing */
362 ci->enc_codec_loaded = 0;
364 return CODEC_OK;
365 } /* codec_start */
367 #endif /* ndef SIMULATOR */