summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/wav_enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/wav_enc.c')
-rw-r--r--lib/rbcodec/codecs/wav_enc.c386
1 files changed, 386 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/wav_enc.c b/lib/rbcodec/codecs/wav_enc.c
new file mode 100644
index 0000000000..e4afeaf93c
--- /dev/null
+++ b/lib/rbcodec/codecs/wav_enc.c
@@ -0,0 +1,386 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 Antonius Hellmann
11 *
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.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include <inttypes.h>
23#include "codeclib.h"
24
25CODEC_ENC_HEADER
26
27struct riff_header
28{
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));
50
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 */
54
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)
59
60static int num_channels IBSS_ATTR;
61static int rec_mono_mode IBSS_ATTR;
62static uint32_t sample_rate;
63static uint32_t enc_size;
64static int32_t err IBSS_ATTR;
65
66static const struct riff_header riff_header =
67{
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 htole32(16), /* format_size */
75 /* format data */
76 htole16(1), /* audio_format */
77 0, /* num_channels (*) */
78 0, /* sample_rate (*) */
79 0, /* byte_rate (*) */
80 0, /* block_align (*) */
81 htole16(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 */
86};
87
88/* called version often - inline */
89static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR;
90static inline bool is_file_data_ok(struct enc_file_event_data *data)
91{
92 return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
93} /* is_file_data_ok */
94
95/* called version often - inline */
96static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
97static inline bool on_write_chunk(struct enc_file_event_data *data)
98{
99 if (!is_file_data_ok(data))
100 return false;
101
102 if (data->chunk->enc_data == NULL)
103 {
104#ifdef ROCKBOX_HAS_LOGF
105 ci->logf("wav enc: NULL data");
106#endif
107 return true;
108 }
109
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;
113
114 data->num_pcm_samples += data->chunk->num_pcm;
115 return true;
116} /* on_write_chunk */
117
118static bool on_start_file(struct enc_file_event_data *data)
119{
120 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
121 return false;
122
123 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
124
125 if (data->rec_file < 0)
126 return false;
127
128 /* reset sample count */
129 data->num_pcm_samples = 0;
130
131 /* write template header */
132 if (ci->write(data->rec_file, &riff_header, sizeof (riff_header))
133 != sizeof (riff_header))
134 {
135 return false;
136 }
137
138 data->new_enc_size += sizeof (riff_header);
139 return true;
140} /* on_start_file */
141
142static bool on_end_file(struct enc_file_event_data *data)
143{
144 /* update template header */
145 struct riff_header hdr;
146 uint32_t data_size;
147
148 if (data->rec_file < 0)
149 return false; /* file already closed, nothing more we can do */
150
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)))
154 {
155 return false;
156 }
157
158 data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
159
160 /* "RIFF" header */
161 hdr.riff_size = htole32(RIFF_FMT_HEADER_SIZE + RIFF_FMT_DATA_SIZE
162 + RIFF_DATA_HEADER_SIZE + data_size);
163
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);
169
170 /* data header */
171 hdr.data_size = htole32(data_size);
172
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)
176 {
177 return false;
178 }
179
180 data->rec_file = -1;
181
182 return true;
183} /* on_end_file */
184
185STATICIRAM void enc_events_callback(enum enc_events event, void *data)
186 ICODE_ATTR;
187STATICIRAM void enc_events_callback(enum enc_events event, void *data)
188{
189 switch (event)
190 {
191 case ENC_WRITE_CHUNK:
192 if (on_write_chunk((struct enc_file_event_data *)data))
193 return;
194
195 break;
196
197 case ENC_START_FILE:
198 if (on_start_file((struct enc_file_event_data *)data))
199 return;
200
201 break;
202
203 case ENC_END_FILE:
204 if (on_end_file((struct enc_file_event_data *)data))
205 return;
206
207 break;
208
209 default:
210 return;
211 }
212
213 /* Something failed above. Signal error back to core. */
214 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
215} /* enc_events_callback */
216
217/* convert native pcm samples to wav format samples */
218static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
219{
220 int32_t lr1, lr2;
221
222 switch(rec_mono_mode)
223 {
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;
245
246 lr2 = *(*src)++;
247 lr2 = (int16_t)lr2 + (lr2 >> 16) + err;
248 err = lr2 & 1;
249 lr2 >>= 1;
250 break;
251 }
252 *(*dst)++ = htole32((lr2 << 16) | (uint16_t)lr1);
253} /* sample_to_mono */
254
255STATICIRAM void chunk_to_wav_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
256STATICIRAM void chunk_to_wav_format(uint32_t *src, uint32_t *dst)
257{
258 if (num_channels == 1)
259 {
260 /* On big endian:
261 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
262 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
263 * |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
264 *
265 * On little endian:
266 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
267 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
268 * |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
269 */
270 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
271
272 do
273 {
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);
282 }
283 while (src < src_end);
284 }
285 else
286 {
287#ifdef ROCKBOX_BIG_ENDIAN
288 /* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
289 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
290 */
291 uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
292
293 do
294 {
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++);
303 }
304 while (src < src_end);
305#else
306 /* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
307 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
308 */
309 ci->memcpy(dst, src, PCM_CHUNK_SIZE);
310#endif
311 }
312} /* chunk_to_wav_format */
313
314static bool init_encoder(void)
315{
316 struct enc_inputs inputs;
317 struct enc_parameters params;
318
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;
325
326 ci->enc_get_inputs(&inputs);
327
328 if (inputs.config->afmt != AFMT_PCM_WAV)
329 return false;
330
331 sample_rate = inputs.sample_rate;
332 num_channels = inputs.num_channels;
333 rec_mono_mode = inputs.rec_mono_mode;
334 err = 0;
335
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);
344
345 return true;
346} /* init_encoder */
347
348/* this is the codec entry point */
349enum codec_status codec_main(enum codec_entry_call_reason reason)
350{
351 if (reason == CODEC_LOAD) {
352 if (!init_encoder())
353 return CODEC_ERROR;
354 }
355 else if (reason == CODEC_UNLOAD) {
356 /* reset parameters to initial state */
357 ci->enc_set_parameters(NULL);
358 }
359
360 return CODEC_OK;
361}
362
363/* this is called for each file to process */
364enum codec_status codec_run(void)
365{
366 /* main encoding loop */
367 while(ci->get_command(NULL) != CODEC_ACTION_HALT)
368 {
369 uint32_t *src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE);
370 struct enc_chunk_hdr *chunk;
371
372 if(src == NULL)
373 continue;
374
375 chunk = ci->enc_get_chunk();
376 chunk->enc_size = enc_size;
377 chunk->num_pcm = PCM_SAMP_PER_CHUNK;
378 chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
379
380 chunk_to_wav_format(src, (uint32_t *)chunk->enc_data);
381
382 ci->enc_finish_chunk();
383 }
384
385 return CODEC_OK;
386}