Use __builtin_constant_p() to select the best byteswapping method: constant or target...
[kugel-rb.git] / apps / codecs / wavpack_enc.c
blob66263cf1a1c272311dc1ce0f05afc89b05252b17
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 "codeclib.h"
23 #include "libwavpack/wavpack.h"
25 CODEC_ENC_HEADER
27 /** Types **/
28 typedef struct
30 uint8_t type; /* Type of metadata */
31 uint8_t word_size; /* Size of metadata in words */
32 } __attribute__((packed)) WavpackMetadataHeader;
34 struct riff_header
36 uint8_t riff_id[4]; /* 00h - "RIFF" */
37 uint32_t riff_size; /* 04h - sz following headers + data_size */
38 /* format header */
39 uint8_t format[4]; /* 08h - "WAVE" */
40 uint8_t format_id[4]; /* 0Ch - "fmt " */
41 uint32_t format_size; /* 10h - 16 for PCM (sz format data) */
42 /* format data */
43 uint16_t audio_format; /* 14h - 1=PCM */
44 uint16_t num_channels; /* 16h - 1=M, 2=S, etc. */
45 uint32_t sample_rate; /* 18h - HZ */
46 uint32_t byte_rate; /* 1Ch - num_channels*sample_rate*bits_per_sample/8 */
47 uint16_t block_align; /* 20h - num_channels*bits_per_samples/8 */
48 uint16_t bits_per_sample; /* 22h - 8=8 bits, 16=16 bits, etc. */
49 /* Not for audio_format=1 (PCM) */
50 /* unsigned short extra_param_size; 24h - size of extra data */
51 /* unsigned char *extra_params; */
52 /* data header */
53 uint8_t data_id[4]; /* 24h - "data" */
54 uint32_t data_size; /* 28h - num_samples*num_channels*bits_per_sample/8 */
55 /* unsigned char *data; 2ch - actual sound data */
56 } __attribute__((packed));
58 #define RIFF_FMT_HEADER_SIZE 12 /* format -> format_size */
59 #define RIFF_FMT_DATA_SIZE 16 /* audio_format -> bits_per_sample */
60 #define RIFF_DATA_HEADER_SIZE 8 /* data_id -> data_size */
62 #define PCM_DEPTH_BITS 16
63 #define PCM_DEPTH_BYTES 2
64 #define PCM_SAMP_PER_CHUNK 5000
65 #define PCM_CHUNK_SIZE (4*PCM_SAMP_PER_CHUNK)
67 /** Data **/
68 static int8_t input_buffer[PCM_CHUNK_SIZE*2] IBSS_ATTR;
69 static WavpackConfig config IBSS_ATTR;
70 static WavpackContext *wpc;
71 static int32_t data_size, input_size, input_step IBSS_ATTR;
72 static int32_t err IBSS_ATTR;
74 static const WavpackMetadataHeader wvpk_mdh =
76 ID_RIFF_HEADER,
77 sizeof (struct riff_header) / sizeof (uint16_t),
80 static const struct riff_header riff_header =
82 /* "RIFF" header */
83 { 'R', 'I', 'F', 'F' }, /* riff_id */
84 0, /* riff_size (*) */
85 /* format header */
86 { 'W', 'A', 'V', 'E' }, /* format */
87 { 'f', 'm', 't', ' ' }, /* format_id */
88 htole32(16), /* format_size */
89 /* format data */
90 htole16(1), /* audio_format */
91 0, /* num_channels (*) */
92 0, /* sample_rate (*) */
93 0, /* byte_rate (*) */
94 0, /* block_align (*) */
95 htole16(PCM_DEPTH_BITS), /* bits_per_sample */
96 /* data header */
97 { 'd', 'a', 't', 'a' }, /* data_id */
98 0 /* data_size (*) */
99 /* (*) updated during ENC_END_FILE event */
102 static inline void sample_to_int32_mono(int32_t **src, int32_t **dst)
104 int32_t t = *(*src)++;
105 /* endianness irrelevant */
106 t = (int16_t)t + (t >> 16) + err;
107 err = t & 1;
108 *(*dst)++ = t >> 1;
109 } /* sample_to_int32_mono */
111 static inline void sample_to_int32_stereo(int32_t **src, int32_t **dst)
113 int32_t t = *(*src)++;
114 #ifdef ROCKBOX_BIG_ENDIAN
115 *(*dst)++ = t >> 16, *(*dst)++ = (int16_t)t;
116 #else
117 *(*dst)++ = (int16_t)t, *(*dst)++ = t >> 16;
118 #endif
119 } /* sample_to_int32_stereo */
121 STATICIRAM void chunk_to_int32(int32_t *src) ICODE_ATTR;
122 STATICIRAM void chunk_to_int32(int32_t *src)
124 int32_t *src_end, *dst;
125 #ifdef USE_IRAM
126 /* copy to IRAM before converting data */
127 dst = (int32_t *)input_buffer + PCM_SAMP_PER_CHUNK;
128 src_end = dst + PCM_SAMP_PER_CHUNK;
130 memcpy(dst, src, PCM_CHUNK_SIZE);
132 src = dst;
133 #else
134 src_end = src + PCM_SAMP_PER_CHUNK;
135 #endif
137 dst = (int32_t *)input_buffer;
139 if (config.num_channels == 1)
142 * |llllllllllllllll|rrrrrrrrrrrrrrrr| =>
143 * |mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm|
147 /* read 10 longs and write 10 longs */
148 sample_to_int32_mono(&src, &dst);
149 sample_to_int32_mono(&src, &dst);
150 sample_to_int32_mono(&src, &dst);
151 sample_to_int32_mono(&src, &dst);
152 sample_to_int32_mono(&src, &dst);
153 sample_to_int32_mono(&src, &dst);
154 sample_to_int32_mono(&src, &dst);
155 sample_to_int32_mono(&src, &dst);
156 sample_to_int32_mono(&src, &dst);
157 sample_to_int32_mono(&src, &dst);
159 while(src < src_end);
161 return;
163 else
166 * |llllllllllllllll|rrrrrrrrrrrrrrrr| =>
167 * |llllllllllllllllllllllllllllllll|rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
171 /* read 10 longs and write 20 longs */
172 sample_to_int32_stereo(&src, &dst);
173 sample_to_int32_stereo(&src, &dst);
174 sample_to_int32_stereo(&src, &dst);
175 sample_to_int32_stereo(&src, &dst);
176 sample_to_int32_stereo(&src, &dst);
177 sample_to_int32_stereo(&src, &dst);
178 sample_to_int32_stereo(&src, &dst);
179 sample_to_int32_stereo(&src, &dst);
180 sample_to_int32_stereo(&src, &dst);
181 sample_to_int32_stereo(&src, &dst);
183 while (src < src_end);
185 return;
187 } /* chunk_to_int32 */
189 /* called very often - inline */
190 static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR;
191 static inline bool is_file_data_ok(struct enc_file_event_data *data)
193 return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
194 } /* is_file_data_ok */
196 /* called very often - inline */
197 static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
198 static inline bool on_write_chunk(struct enc_file_event_data *data)
200 if (!is_file_data_ok(data))
201 return false;
203 if (data->chunk->enc_data == NULL)
205 #ifdef ROCKBOX_HAS_LOGF
206 ci->logf("wvpk enc: NULL data");
207 #endif
208 return true;
211 /* update timestamp (block_index) */
212 ((WavpackHeader *)data->chunk->enc_data)->block_index =
213 htole32(data->num_pcm_samples);
215 if (ci->write(data->rec_file, data->chunk->enc_data,
216 data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
217 return false;
219 data->num_pcm_samples += data->chunk->num_pcm;
220 return true;
221 } /* on_write_chunk */
223 static bool on_start_file(struct enc_file_event_data *data)
225 if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
226 return false;
228 data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
230 if (data->rec_file < 0)
231 return false;
233 /* reset sample count */
234 data->num_pcm_samples = 0;
236 /* write template headers */
237 if (ci->write(data->rec_file, &wvpk_mdh, sizeof (wvpk_mdh))
238 != sizeof (wvpk_mdh) ||
239 ci->write(data->rec_file, &riff_header, sizeof (riff_header))
240 != sizeof (riff_header))
242 return false;
245 data->new_enc_size += sizeof(wvpk_mdh) + sizeof(riff_header);
246 return true;
247 } /* on_start_file */
249 static bool on_end_file(struct enc_file_event_data *data)
251 struct
253 WavpackMetadataHeader wpmdh;
254 struct riff_header rhdr;
255 WavpackHeader wph;
256 } __attribute__ ((packed)) h;
258 uint32_t data_size;
260 if (data->rec_file < 0)
261 return false; /* file already closed, nothing more we can do */
263 /* always _try_ to write the file header, even on error */
265 /* read template headers at start */
266 if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
267 ci->read(data->rec_file, &h, sizeof (h)) != sizeof (h))
268 return false;
270 data_size = data->num_pcm_samples*config.num_channels*PCM_DEPTH_BYTES;
272 /** "RIFF" header **/
273 h.rhdr.riff_size = htole32(RIFF_FMT_HEADER_SIZE +
274 RIFF_FMT_DATA_SIZE + RIFF_DATA_HEADER_SIZE + data_size);
276 /* format data */
277 h.rhdr.num_channels = htole16(config.num_channels);
278 h.rhdr.sample_rate = htole32(config.sample_rate);
279 h.rhdr.byte_rate = htole32(config.sample_rate*config.num_channels*
280 PCM_DEPTH_BYTES);
281 h.rhdr.block_align = htole16(config.num_channels*PCM_DEPTH_BYTES);
283 /* data header */
284 h.rhdr.data_size = htole32(data_size);
286 /** Wavpack header **/
287 h.wph.ckSize = htole32(letoh32(h.wph.ckSize) + sizeof (h.wpmdh)
288 + sizeof (h.rhdr));
289 h.wph.total_samples = htole32(data->num_pcm_samples);
291 /* MDH|RIFF|WVPK => WVPK|MDH|RIFF */
292 if (ci->lseek(data->rec_file, 0, SEEK_SET)
293 != 0 ||
294 ci->write(data->rec_file, &h.wph, sizeof (h.wph))
295 != sizeof (h.wph) ||
296 ci->write(data->rec_file, &h.wpmdh, sizeof (h.wpmdh))
297 != sizeof (h.wpmdh) ||
298 ci->write(data->rec_file, &h.rhdr, sizeof (h.rhdr))
299 != sizeof (h.rhdr) ||
300 ci->close(data->rec_file) != 0 )
302 return false;
305 data->rec_file = -1;
307 return true;
308 } /* on_end_file */
310 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
311 ICODE_ATTR;
312 STATICIRAM void enc_events_callback(enum enc_events event, void *data)
314 switch (event)
316 case ENC_WRITE_CHUNK:
317 if (on_write_chunk((struct enc_file_event_data *)data))
318 return;
320 break;
322 case ENC_START_FILE:
323 /* write metadata header and RIFF header */
324 if (on_start_file((struct enc_file_event_data *)data))
325 return;
327 break;
329 case ENC_END_FILE:
330 if (on_end_file((struct enc_file_event_data *)data))
331 return;
333 break;
335 default:
336 return;
339 /* Something failed above. Signal error back to core. */
340 ((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
341 } /* enc_events_callback */
343 static bool init_encoder(void)
345 struct enc_inputs inputs;
346 struct enc_parameters params;
348 codec_init();
350 if (ci->enc_get_inputs == NULL ||
351 ci->enc_set_parameters == NULL ||
352 ci->enc_get_chunk == NULL ||
353 ci->enc_finish_chunk == NULL ||
354 ci->enc_get_pcm_data == NULL ||
355 ci->enc_unget_pcm_data == NULL )
356 return false;
358 ci->enc_get_inputs(&inputs);
360 if (inputs.config->afmt != AFMT_WAVPACK)
361 return false;
363 memset(&config, 0, sizeof (config));
364 config.bits_per_sample = PCM_DEPTH_BITS;
365 config.bytes_per_sample = PCM_DEPTH_BYTES;
366 config.sample_rate = inputs.sample_rate;
367 config.num_channels = inputs.num_channels;
369 wpc = WavpackOpenFileOutput ();
371 if (!WavpackSetConfiguration(wpc, &config, -1))
372 return false;
374 err = 0;
376 /* configure the buffer system */
377 params.afmt = AFMT_WAVPACK;
378 input_size = PCM_CHUNK_SIZE*inputs.num_channels / 2;
379 data_size = 105*input_size / 100;
380 input_size *= 2;
381 input_step = input_size / 4;
382 params.chunk_size = data_size;
383 params.enc_sample_rate = inputs.sample_rate;
384 params.reserve_bytes = 0;
385 params.events_callback = enc_events_callback;
387 ci->enc_set_parameters(&params);
389 return true;
390 } /* init_encoder */
392 enum codec_status codec_main(void)
394 /* initialize params and config */
395 if (!init_encoder())
397 ci->enc_codec_loaded = -1;
398 return CODEC_ERROR;
401 /* main application waits for this flag during encoder loading */
402 ci->enc_codec_loaded = 1;
404 /* main encoding loop */
405 while(!ci->stop_encoder)
407 uint8_t *src;
409 while ((src = ci->enc_get_pcm_data(PCM_CHUNK_SIZE)) != NULL)
411 struct enc_chunk_hdr *chunk;
412 bool abort_chunk;
413 uint8_t *dst;
414 uint8_t *src_end;
416 if(ci->stop_encoder)
417 break;
419 abort_chunk = true;
421 chunk = ci->enc_get_chunk();
423 /* reset counts and pointer */
424 chunk->enc_size = 0;
425 chunk->num_pcm = 0;
426 chunk->enc_data = NULL;
428 dst = ENC_CHUNK_SKIP_HDR(dst, chunk);
430 WavpackStartBlock(wpc, dst, dst + data_size);
432 chunk_to_int32((uint32_t*)src);
433 src = input_buffer;
434 src_end = src + input_size;
436 /* encode chunk in four steps yielding between each */
439 if (WavpackPackSamples(wpc, (int32_t *)src,
440 PCM_SAMP_PER_CHUNK/4))
442 chunk->num_pcm += PCM_SAMP_PER_CHUNK/4;
443 ci->yield();
444 /* could've been stopped in some way */
445 abort_chunk = ci->stop_encoder ||
446 (chunk->flags & CHUNKF_ABORT);
449 src += input_step;
451 while (!abort_chunk && src < src_end);
453 if (!abort_chunk)
455 chunk->enc_data = dst;
456 if (chunk->num_pcm < PCM_SAMP_PER_CHUNK)
457 ci->enc_unget_pcm_data(PCM_CHUNK_SIZE - chunk->num_pcm*4);
458 /* finish the chunk and store chunk size info */
459 chunk->enc_size = WavpackFinishBlock(wpc);
460 ci->enc_finish_chunk();
464 ci->yield();
467 /* reset parameters to initial state */
468 ci->enc_set_parameters(NULL);
470 /* main application waits for this flag during encoder removing */
471 ci->enc_codec_loaded = 0;
473 return CODEC_OK;
474 } /* codec_start */