1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
29 uint8_t riff_id
[4]; /* 00h - "RIFF" */
30 uint32_t riff_size
; /* 04h - sz following headers + data_size */
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) */
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; */
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
=
69 { 'R', 'I', 'F', 'F' }, /* riff_id */
70 0, /* riff_size (*) */
72 { 'W', 'A', 'V', 'E' }, /* format */
73 { 'f', 'm', 't', ' ' }, /* format_id */
74 H_TO_LE32(16), /* format_size */
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 */
83 { 'd', 'a', 't', 'a' }, /* data_id */
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
))
102 if (data
->chunk
->enc_data
== NULL
)
104 #ifdef ROCKBOX_HAS_LOGF
105 ci
->logf("wav enc: NULL data");
110 if (ci
->write(data
->rec_file
, data
->chunk
->enc_data
,
111 data
->chunk
->enc_size
) != (ssize_t
)data
->chunk
->enc_size
)
114 data
->num_pcm_samples
+= data
->chunk
->num_pcm
;
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')
123 data
->rec_file
= ci
->open(data
->filename
, O_RDWR
|O_CREAT
|O_TRUNC
, 0666);
125 if (data
->rec_file
< 0)
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
))
138 data
->new_enc_size
+= sizeof (riff_header
);
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
;
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
)))
158 data_size
= data
->num_pcm_samples
*num_channels
*PCM_DEPTH_BYTES
;
161 hdr
.riff_size
= htole32(RIFF_FMT_HEADER_SIZE
+ RIFF_FMT_DATA_SIZE
162 + RIFF_DATA_HEADER_SIZE
+ data_size
);
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
);
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)
185 STATICIRAM
void enc_events_callback(enum enc_events event
, void *data
)
187 STATICIRAM
void enc_events_callback(enum enc_events event
, void *data
)
191 case ENC_WRITE_CHUNK
:
192 if (on_write_chunk((struct enc_file_event_data
*)data
))
198 if (on_start_file((struct enc_file_event_data
*)data
))
204 if (on_end_file((struct enc_file_event_data
*)data
))
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
)
222 switch(rec_mono_mode
)
242 lr1
= (int16_t)lr1
+ (lr1
>> 16) + err
;
247 lr2
= (int16_t)lr2
+ (lr2
>> 16) + err
;
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)
261 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
262 * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
263 * |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
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
);
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
);
306 /* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
307 * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
309 ci
->memcpy(dst
, src
, PCM_CHUNK_SIZE
);
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
)
326 ci
->enc_get_inputs(&inputs
);
328 if (inputs
.config
->afmt
!= AFMT_PCM_WAV
)
331 sample_rate
= inputs
.sample_rate
;
332 num_channels
= inputs
.num_channels
;
333 rec_mono_mode
= inputs
.rec_mono_mode
;
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(¶ms
);
348 /* main codec entry point */
349 enum codec_status
codec_main(void)
353 ci
->enc_codec_loaded
= -1;
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
)
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
)
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();
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;