diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/aiff_enc.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/aiff_enc.c')
-rw-r--r-- | lib/rbcodec/codecs/aiff_enc.c | 400 |
1 files changed, 400 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/aiff_enc.c b/lib/rbcodec/codecs/aiff_enc.c new file mode 100644 index 0000000000..fc44196eb0 --- /dev/null +++ b/lib/rbcodec/codecs/aiff_enc.c | |||
@@ -0,0 +1,400 @@ | |||
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 | |||
25 | CODEC_ENC_HEADER | ||
26 | |||
27 | struct aiff_header | ||
28 | { | ||
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)); | ||
46 | |||
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) | ||
51 | |||
52 | /* Template headers */ | ||
53 | struct aiff_header aiff_header = | ||
54 | { | ||
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 | htobe32(18), /* comm_size */ | ||
60 | 0, /* num_channels (*) */ | ||
61 | 0, /* num_sample_frames (*) */ | ||
62 | htobe16(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 | htobe32(0), /* offset */ | ||
67 | htobe32(0), /* block_size */ | ||
68 | }; | ||
69 | |||
70 | /* (*) updated when finalizing file */ | ||
71 | |||
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; | ||
77 | |||
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) | ||
82 | { | ||
83 | int32_t exp; | ||
84 | |||
85 | ci->memset(f, 0, 10); | ||
86 | |||
87 | if (l == 0) | ||
88 | return; | ||
89 | |||
90 | for (exp = 30; (l & (1ul << 31)) == 0; exp--) | ||
91 | l <<= 1; | ||
92 | |||
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 | f[2] = (uint8_t)(l >> 24); | ||
100 | f[3] = (uint8_t)(l >> 16); | ||
101 | f[4] = (uint8_t)(l >> 8); | ||
102 | f[5] = (uint8_t)(l >> 0); | ||
103 | } /* uint32_h_to_ieee754_extended_be */ | ||
104 | |||
105 | /* called version often - inline */ | ||
106 | static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR; | ||
107 | static inline bool is_file_data_ok(struct enc_file_event_data *data) | ||
108 | { | ||
109 | return data->rec_file >= 0 && (long)data->chunk->flags >= 0; | ||
110 | } /* is_file_data_ok */ | ||
111 | |||
112 | /* called version often - inline */ | ||
113 | static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR; | ||
114 | static inline bool on_write_chunk(struct enc_file_event_data *data) | ||
115 | { | ||
116 | if (!is_file_data_ok(data)) | ||
117 | return false; | ||
118 | |||
119 | if (data->chunk->enc_data == NULL) | ||
120 | { | ||
121 | #ifdef ROCKBOX_HAS_LOGF | ||
122 | ci->logf("aiff enc: NULL data"); | ||
123 | #endif | ||
124 | return true; | ||
125 | } | ||
126 | |||
127 | if (ci->write(data->rec_file, data->chunk->enc_data, | ||
128 | data->chunk->enc_size) != (ssize_t)data->chunk->enc_size) | ||
129 | return false; | ||
130 | |||
131 | data->num_pcm_samples += data->chunk->num_pcm; | ||
132 | return true; | ||
133 | } /* on_write_chunk */ | ||
134 | |||
135 | static bool on_start_file(struct enc_file_event_data *data) | ||
136 | { | ||
137 | if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0') | ||
138 | return false; | ||
139 | |||
140 | data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666); | ||
141 | |||
142 | if (data->rec_file < 0) | ||
143 | return false; | ||
144 | |||
145 | /* reset sample count */ | ||
146 | data->num_pcm_samples = 0; | ||
147 | |||
148 | /* write template headers */ | ||
149 | if (ci->write(data->rec_file, &aiff_header, sizeof (aiff_header)) | ||
150 | != sizeof (aiff_header)) | ||
151 | { | ||
152 | return false; | ||
153 | } | ||
154 | |||
155 | data->new_enc_size += sizeof(aiff_header); | ||
156 | return true; | ||
157 | } /* on_start_file */ | ||
158 | |||
159 | static bool on_end_file(struct enc_file_event_data *data) | ||
160 | { | ||
161 | /* update template headers */ | ||
162 | struct aiff_header hdr; | ||
163 | uint32_t data_size; | ||
164 | |||
165 | if (!is_file_data_ok(data)) | ||
166 | return false; | ||
167 | |||
168 | if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 || | ||
169 | ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr)) | ||
170 | { | ||
171 | return false; | ||
172 | } | ||
173 | |||
174 | data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES; | ||
175 | |||
176 | /* 'FORM' chunk */ | ||
177 | hdr.form_size = htobe32(data_size + sizeof (hdr) - 8); | ||
178 | |||
179 | /* 'COMM' chunk */ | ||
180 | hdr.num_channels = htobe16(num_channels); | ||
181 | hdr.num_sample_frames = htobe32(data->num_pcm_samples); | ||
182 | uint32_h_to_ieee754_extended_be(hdr.sample_rate, sample_rate); | ||
183 | |||
184 | /* 'SSND' chunk */ | ||
185 | hdr.ssnd_size = htobe32(data_size + 8); | ||
186 | |||
187 | if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 || | ||
188 | ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) || | ||
189 | ci->close(data->rec_file) != 0) | ||
190 | { | ||
191 | return false; | ||
192 | } | ||
193 | |||
194 | data->rec_file = -1; | ||
195 | |||
196 | return true; | ||
197 | } /* on_end_file */ | ||
198 | |||
199 | STATICIRAM void enc_events_callback(enum enc_events event, void *data) | ||
200 | ICODE_ATTR; | ||
201 | STATICIRAM void enc_events_callback(enum enc_events event, void *data) | ||
202 | { | ||
203 | switch (event) | ||
204 | { | ||
205 | case ENC_WRITE_CHUNK: | ||
206 | if (on_write_chunk((struct enc_file_event_data *)data)) | ||
207 | return; | ||
208 | |||
209 | break; | ||
210 | |||
211 | case ENC_START_FILE: | ||
212 | if (on_start_file((struct enc_file_event_data *)data)) | ||
213 | return; | ||
214 | |||
215 | break; | ||
216 | |||
217 | case ENC_END_FILE: | ||
218 | if (on_end_file((struct enc_file_event_data *)data)) | ||
219 | return; | ||
220 | |||
221 | break; | ||
222 | |||
223 | default: | ||
224 | return; | ||
225 | } | ||
226 | |||
227 | /* Something failed above. Signal error back to core. */ | ||
228 | ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR; | ||
229 | } /* enc_events_callback */ | ||
230 | |||
231 | /* convert native pcm samples to aiff format samples */ | ||
232 | static inline void sample_to_mono(uint32_t **src, uint32_t **dst) | ||
233 | { | ||
234 | int32_t lr1, lr2; | ||
235 | |||
236 | switch(rec_mono_mode) | ||
237 | { | ||
238 | case 1: | ||
239 | /* mono = L */ | ||
240 | lr1 = *(*src)++; | ||
241 | lr1 = lr1 >> 16; | ||
242 | lr2 = *(*src)++; | ||
243 | lr2 = lr2 >> 16; | ||
244 | break; | ||
245 | case 2: | ||
246 | /* mono = R */ | ||
247 | lr1 = *(*src)++; | ||
248 | lr1 = (int16_t)lr1; | ||
249 | lr2 = *(*src)++; | ||
250 | lr2 = (int16_t)lr2; | ||
251 | break; | ||
252 | case 0: | ||
253 | default: | ||
254 | /* mono = (L+R)/2 */ | ||
255 | lr1 = *(*src)++; | ||
256 | lr1 = (int16_t)lr1 + (lr1 >> 16) + err; | ||
257 | err = lr1 & 1; | ||
258 | lr1 >>= 1; | ||
259 | |||
260 | lr2 = *(*src)++; | ||
261 | lr2 = (int16_t)lr2 + (lr2 >> 16) + err; | ||
262 | err = lr2 & 1; | ||
263 | lr2 >>= 1; | ||
264 | break; | ||
265 | } | ||
266 | *(*dst)++ = htobe32((lr1 << 16) | (uint16_t)lr2); | ||
267 | } /* sample_to_mono */ | ||
268 | |||
269 | STATICIRAM void chunk_to_aiff_format(uint32_t *src, uint32_t *dst) ICODE_ATTR; | ||
270 | STATICIRAM void chunk_to_aiff_format(uint32_t *src, uint32_t *dst) | ||
271 | { | ||
272 | if (num_channels == 1) | ||
273 | { | ||
274 | /* On big endian: | ||
275 | * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| | ||
276 | * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| => | ||
277 | * |MMMMMMMMmmmmmmmm|MMMMMMMMmmmmmmmm| | ||
278 | * | ||
279 | * On little endian: | ||
280 | * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| | ||
281 | * |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| => | ||
282 | * |MMMMMMMMmmmmmmmm|MMMMMMMMmmmmmmmm| | ||
283 | */ | ||
284 | uint32_t *src_end = src + PCM_SAMP_PER_CHUNK; | ||
285 | |||
286 | do | ||
287 | { | ||
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); | ||
293 | sample_to_mono(&src, &dst); | ||
294 | sample_to_mono(&src, &dst); | ||
295 | sample_to_mono(&src, &dst); | ||
296 | } | ||
297 | while (src < src_end); | ||
298 | } | ||
299 | else | ||
300 | { | ||
301 | #ifdef ROCKBOX_BIG_ENDIAN | ||
302 | /* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| => | ||
303 | * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| | ||
304 | */ | ||
305 | ci->memcpy(dst, src, PCM_CHUNK_SIZE); | ||
306 | #else | ||
307 | /* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| => | ||
308 | * |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| | ||
309 | */ | ||
310 | uint32_t *src_end = src + PCM_SAMP_PER_CHUNK; | ||
311 | |||
312 | do | ||
313 | { | ||
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++); | ||
319 | *dst++ = swap_odd_even32(*src++); | ||
320 | *dst++ = swap_odd_even32(*src++); | ||
321 | *dst++ = swap_odd_even32(*src++); | ||
322 | } | ||
323 | while (src < src_end); | ||
324 | #endif | ||
325 | } | ||
326 | } /* chunk_to_aiff_format */ | ||
327 | |||
328 | static bool init_encoder(void) | ||
329 | { | ||
330 | struct enc_inputs inputs; | ||
331 | struct enc_parameters params; | ||
332 | |||
333 | if (ci->enc_get_inputs == NULL || | ||
334 | ci->enc_set_parameters == NULL || | ||
335 | ci->enc_get_chunk == NULL || | ||
336 | ci->enc_finish_chunk == NULL || | ||
337 | ci->enc_get_pcm_data == NULL ) | ||
338 | return false; | ||
339 | |||
340 | ci->enc_get_inputs(&inputs); | ||
341 | |||
342 | if (inputs.config->afmt != AFMT_AIFF) | ||
343 | return false; | ||
344 | |||
345 | sample_rate = inputs.sample_rate; | ||
346 | num_channels = inputs.num_channels; | ||
347 | rec_mono_mode = inputs.rec_mono_mode; | ||
348 | err = 0; | ||
349 | |||
350 | /* configure the buffer system */ | ||
351 | params.afmt = AFMT_AIFF; | ||
352 | enc_size = PCM_CHUNK_SIZE*inputs.num_channels / 2; | ||
353 | params.chunk_size = enc_size; | ||
354 | params.enc_sample_rate = sample_rate; | ||
355 | params.reserve_bytes = 0; | ||
356 | params.events_callback = enc_events_callback; | ||
357 | ci->enc_set_parameters(¶ms); | ||
358 | |||
359 | return true; | ||
360 | } /* init_encoder */ | ||
361 | |||
362 | /* this is the codec entry point */ | ||
363 | enum codec_status codec_main(enum codec_entry_call_reason reason) | ||
364 | { | ||
365 | if (reason == CODEC_LOAD) { | ||
366 | if (!init_encoder()) | ||
367 | return CODEC_ERROR; | ||
368 | } | ||
369 | else if (reason == CODEC_UNLOAD) { | ||
370 | /* reset parameters to initial state */ | ||
371 | ci->enc_set_parameters(NULL); | ||
372 | } | ||
373 | |||
374 | return CODEC_OK; | ||
375 | } | ||
376 | |||
377 | /* this is called for each file to process */ | ||
378 | enum codec_status codec_run(void) | ||
379 | { | ||
380 | /* main encoding loop */ | ||
381 | while (ci->get_command(NULL) != CODEC_ACTION_HALT) | ||
382 | { | ||
383 | uint32_t *src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE); | ||
384 | struct enc_chunk_hdr *chunk; | ||
385 | |||
386 | if (src == NULL) | ||
387 | continue; | ||
388 | |||
389 | chunk = ci->enc_get_chunk(); | ||
390 | chunk->enc_size = enc_size; | ||
391 | chunk->num_pcm = PCM_SAMP_PER_CHUNK; | ||
392 | chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk); | ||
393 | |||
394 | chunk_to_aiff_format(src, (uint32_t *)chunk->enc_data); | ||
395 | |||
396 | ci->enc_finish_chunk(); | ||
397 | } | ||
398 | |||
399 | return CODEC_OK; | ||
400 | } | ||