2006-08-28 22:38:41 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Antonius Hellmann
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-08-28 22:38:41 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
#include <inttypes.h>
|
2006-08-28 22:38:41 +00:00
|
|
|
#include "codeclib.h"
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
CODEC_ENC_HEADER
|
|
|
|
|
|
|
|
struct riff_header
|
|
|
|
{
|
|
|
|
uint8_t riff_id[4]; /* 00h - "RIFF" */
|
|
|
|
uint32_t riff_size; /* 04h - sz following headers + data_size */
|
|
|
|
/* format header */
|
|
|
|
uint8_t format[4]; /* 08h - "WAVE" */
|
|
|
|
uint8_t format_id[4]; /* 0Ch - "fmt " */
|
|
|
|
uint32_t format_size; /* 10h - 16 for PCM (sz format data) */
|
|
|
|
/* format data */
|
|
|
|
uint16_t audio_format; /* 14h - 1=PCM */
|
|
|
|
uint16_t num_channels; /* 16h - 1=M, 2=S, etc. */
|
|
|
|
uint32_t sample_rate; /* 18h - HZ */
|
|
|
|
uint32_t byte_rate; /* 1Ch - num_channels*sample_rate*bits_per_sample/8 */
|
|
|
|
uint16_t block_align; /* 20h - num_channels*bits_per_samples/8 */
|
|
|
|
uint16_t bits_per_sample; /* 22h - 8=8 bits, 16=16 bits, etc. */
|
|
|
|
/* Not for audio_format=1 (PCM) */
|
|
|
|
/* unsigned short extra_param_size; 24h - size of extra data */
|
|
|
|
/* unsigned char *extra_params; */
|
|
|
|
/* data header */
|
|
|
|
uint8_t data_id[4]; /* 24h - "data" */
|
|
|
|
uint32_t data_size; /* 28h - num_samples*num_channels*bits_per_sample/8 */
|
|
|
|
/* unsigned char *data; 2ch - actual sound data */
|
2006-11-29 17:52:30 +00:00
|
|
|
} __attribute__((packed));
|
2006-11-06 18:07:30 +00:00
|
|
|
|
|
|
|
#define RIFF_FMT_HEADER_SIZE 12 /* format -> format_size */
|
|
|
|
#define RIFF_FMT_DATA_SIZE 16 /* audio_format -> bits_per_sample */
|
|
|
|
#define RIFF_DATA_HEADER_SIZE 8 /* data_id -> data_size */
|
|
|
|
|
|
|
|
#define PCM_DEPTH_BYTES 2
|
|
|
|
#define PCM_DEPTH_BITS 16
|
|
|
|
#define PCM_SAMP_PER_CHUNK 2048
|
|
|
|
#define PCM_CHUNK_SIZE (PCM_SAMP_PER_CHUNK*4)
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2007-02-09 18:11:11 +00:00
|
|
|
static int num_channels IBSS_ATTR;
|
2008-10-08 22:18:16 +00:00
|
|
|
static int rec_mono_mode IBSS_ATTR;
|
2007-02-09 18:11:11 +00:00
|
|
|
static uint32_t sample_rate;
|
|
|
|
static uint32_t enc_size;
|
|
|
|
static int32_t err IBSS_ATTR;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
static const struct riff_header riff_header =
|
|
|
|
{
|
|
|
|
/* "RIFF" header */
|
|
|
|
{ 'R', 'I', 'F', 'F' }, /* riff_id */
|
|
|
|
0, /* riff_size (*) */
|
|
|
|
/* format header */
|
|
|
|
{ 'W', 'A', 'V', 'E' }, /* format */
|
|
|
|
{ 'f', 'm', 't', ' ' }, /* format_id */
|
2011-01-30 00:58:45 +00:00
|
|
|
htole32(16), /* format_size */
|
2006-11-06 18:07:30 +00:00
|
|
|
/* format data */
|
2011-01-30 00:58:45 +00:00
|
|
|
htole16(1), /* audio_format */
|
2006-11-06 18:07:30 +00:00
|
|
|
0, /* num_channels (*) */
|
|
|
|
0, /* sample_rate (*) */
|
|
|
|
0, /* byte_rate (*) */
|
|
|
|
0, /* block_align (*) */
|
2011-01-30 00:58:45 +00:00
|
|
|
htole16(PCM_DEPTH_BITS), /* bits_per_sample */
|
2006-11-06 18:07:30 +00:00
|
|
|
/* data header */
|
|
|
|
{ 'd', 'a', 't', 'a' }, /* data_id */
|
|
|
|
0 /* data_size (*) */
|
|
|
|
/* (*) updated during ENC_END_FILE event */
|
|
|
|
};
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* called version often - inline */
|
|
|
|
static inline bool is_file_data_ok(struct enc_file_event_data *data) ICODE_ATTR;
|
|
|
|
static inline bool is_file_data_ok(struct enc_file_event_data *data)
|
|
|
|
{
|
|
|
|
return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
|
|
|
|
} /* is_file_data_ok */
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* called version often - inline */
|
|
|
|
static inline bool on_write_chunk(struct enc_file_event_data *data) ICODE_ATTR;
|
|
|
|
static inline bool on_write_chunk(struct enc_file_event_data *data)
|
|
|
|
{
|
|
|
|
if (!is_file_data_ok(data))
|
|
|
|
return false;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
if (data->chunk->enc_data == NULL)
|
|
|
|
{
|
|
|
|
#ifdef ROCKBOX_HAS_LOGF
|
|
|
|
ci->logf("wav enc: NULL data");
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ci->write(data->rec_file, data->chunk->enc_data,
|
|
|
|
data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
data->num_pcm_samples += data->chunk->num_pcm;
|
|
|
|
return true;
|
|
|
|
} /* on_write_chunk */
|
|
|
|
|
|
|
|
static bool on_start_file(struct enc_file_event_data *data)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
|
|
|
|
return false;
|
|
|
|
|
2010-05-06 17:35:13 +00:00
|
|
|
data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
|
2006-11-06 18:07:30 +00:00
|
|
|
|
|
|
|
if (data->rec_file < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* reset sample count */
|
|
|
|
data->num_pcm_samples = 0;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* write template header */
|
|
|
|
if (ci->write(data->rec_file, &riff_header, sizeof (riff_header))
|
|
|
|
!= sizeof (riff_header))
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->new_enc_size += sizeof (riff_header);
|
|
|
|
return true;
|
|
|
|
} /* on_start_file */
|
|
|
|
|
|
|
|
static bool on_end_file(struct enc_file_event_data *data)
|
|
|
|
{
|
|
|
|
/* update template header */
|
|
|
|
struct riff_header hdr;
|
|
|
|
uint32_t data_size;
|
|
|
|
|
2007-12-13 23:36:22 +00:00
|
|
|
if (data->rec_file < 0)
|
|
|
|
return false; /* file already closed, nothing more we can do */
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2007-12-13 23:36:22 +00:00
|
|
|
/* always _try_ to write the file header, even on error */
|
|
|
|
if ((ci->lseek(data->rec_file, 0, SEEK_SET)) ||
|
|
|
|
(ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr)))
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
|
|
|
|
|
|
|
|
/* "RIFF" header */
|
|
|
|
hdr.riff_size = htole32(RIFF_FMT_HEADER_SIZE + RIFF_FMT_DATA_SIZE
|
|
|
|
+ RIFF_DATA_HEADER_SIZE + data_size);
|
|
|
|
|
|
|
|
/* format data */
|
|
|
|
hdr.num_channels = htole16(num_channels);
|
|
|
|
hdr.sample_rate = htole32(sample_rate);
|
|
|
|
hdr.byte_rate = htole32(sample_rate*num_channels* PCM_DEPTH_BYTES);
|
|
|
|
hdr.block_align = htole16(num_channels*PCM_DEPTH_BYTES);
|
|
|
|
|
|
|
|
/* data header */
|
|
|
|
hdr.data_size = htole32(data_size);
|
|
|
|
|
|
|
|
if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
|
2007-04-25 19:53:34 +00:00
|
|
|
ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) ||
|
|
|
|
ci->close(data->rec_file) != 0)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->rec_file = -1;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} /* on_end_file */
|
|
|
|
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM void enc_events_callback(enum enc_events event, void *data)
|
|
|
|
ICODE_ATTR;
|
|
|
|
STATICIRAM void enc_events_callback(enum enc_events event, void *data)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
2008-11-28 07:23:13 +00:00
|
|
|
switch (event)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
2008-11-28 07:23:13 +00:00
|
|
|
case ENC_WRITE_CHUNK:
|
2006-11-06 18:07:30 +00:00
|
|
|
if (on_write_chunk((struct enc_file_event_data *)data))
|
|
|
|
return;
|
2008-11-28 07:23:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENC_START_FILE:
|
2006-11-06 18:07:30 +00:00
|
|
|
if (on_start_file((struct enc_file_event_data *)data))
|
|
|
|
return;
|
2008-11-28 07:23:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENC_END_FILE:
|
2006-11-06 18:07:30 +00:00
|
|
|
if (on_end_file((struct enc_file_event_data *)data))
|
|
|
|
return;
|
2008-11-28 07:23:13 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-11-06 18:07:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-28 07:23:13 +00:00
|
|
|
/* Something failed above. Signal error back to core. */
|
2006-11-06 18:07:30 +00:00
|
|
|
((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
|
|
|
|
} /* enc_events_callback */
|
|
|
|
|
|
|
|
/* convert native pcm samples to wav format samples */
|
2007-03-29 01:55:47 +00:00
|
|
|
static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
|
|
|
|
{
|
|
|
|
int32_t lr1, lr2;
|
|
|
|
|
2008-10-08 22:18:16 +00:00
|
|
|
switch(rec_mono_mode)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
/* mono = L */
|
|
|
|
lr1 = *(*src)++;
|
|
|
|
lr1 = lr1 >> 16;
|
|
|
|
lr2 = *(*src)++;
|
|
|
|
lr2 = lr2 >> 16;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
/* mono = R */
|
|
|
|
lr1 = *(*src)++;
|
|
|
|
lr1 = (uint16_t)lr1;
|
|
|
|
lr2 = *(*src)++;
|
|
|
|
lr2 = (uint16_t)lr2;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
/* mono = (L+R)/2 */
|
|
|
|
lr1 = *(*src)++;
|
|
|
|
lr1 = (int16_t)lr1 + (lr1 >> 16) + err;
|
|
|
|
err = lr1 & 1;
|
|
|
|
lr1 >>= 1;
|
|
|
|
|
|
|
|
lr2 = *(*src)++;
|
|
|
|
lr2 = (int16_t)lr2 + (lr2 >> 16) + err;
|
|
|
|
err = lr2 & 1;
|
|
|
|
lr2 >>= 1;
|
|
|
|
break;
|
|
|
|
}
|
2009-07-23 11:27:09 +00:00
|
|
|
*(*dst)++ = htole32((lr2 << 16) | (uint16_t)lr1);
|
2007-03-29 01:55:47 +00:00
|
|
|
} /* sample_to_mono */
|
|
|
|
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM void chunk_to_wav_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
|
|
|
|
STATICIRAM void chunk_to_wav_format(uint32_t *src, uint32_t *dst)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
|
|
|
if (num_channels == 1)
|
|
|
|
{
|
|
|
|
/* On big endian:
|
|
|
|
* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
|
|
|
|
* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
|
|
|
|
* |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
|
|
|
|
*
|
|
|
|
* On little endian:
|
|
|
|
* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
|
|
|
|
* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
|
|
|
|
* |mmmmmmmmMMMMMMMM|mmmmmmmmMMMMMMMM|
|
|
|
|
*/
|
|
|
|
uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2007-03-29 01:55:47 +00:00
|
|
|
sample_to_mono(&src, &dst);
|
|
|
|
sample_to_mono(&src, &dst);
|
|
|
|
sample_to_mono(&src, &dst);
|
|
|
|
sample_to_mono(&src, &dst);
|
|
|
|
sample_to_mono(&src, &dst);
|
|
|
|
sample_to_mono(&src, &dst);
|
|
|
|
sample_to_mono(&src, &dst);
|
|
|
|
sample_to_mono(&src, &dst);
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
2006-11-06 18:07:30 +00:00
|
|
|
while (src < src_end);
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
2006-11-06 18:07:30 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef ROCKBOX_BIG_ENDIAN
|
|
|
|
/* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
|
|
|
|
* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
|
|
|
|
*/
|
|
|
|
uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
*dst++ = swap_odd_even32(*src++);
|
|
|
|
*dst++ = swap_odd_even32(*src++);
|
|
|
|
*dst++ = swap_odd_even32(*src++);
|
|
|
|
*dst++ = swap_odd_even32(*src++);
|
|
|
|
*dst++ = swap_odd_even32(*src++);
|
|
|
|
*dst++ = swap_odd_even32(*src++);
|
|
|
|
*dst++ = swap_odd_even32(*src++);
|
|
|
|
*dst++ = swap_odd_even32(*src++);
|
|
|
|
}
|
|
|
|
while (src < src_end);
|
|
|
|
#else
|
|
|
|
/* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
|
|
|
|
* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
|
|
|
|
*/
|
|
|
|
ci->memcpy(dst, src, PCM_CHUNK_SIZE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} /* chunk_to_wav_format */
|
|
|
|
|
|
|
|
static bool init_encoder(void)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
struct enc_inputs inputs;
|
|
|
|
struct enc_parameters params;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
if (ci->enc_get_inputs == NULL ||
|
|
|
|
ci->enc_set_parameters == NULL ||
|
|
|
|
ci->enc_get_chunk == NULL ||
|
|
|
|
ci->enc_finish_chunk == NULL ||
|
|
|
|
ci->enc_get_pcm_data == NULL )
|
|
|
|
return false;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->enc_get_inputs(&inputs);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
if (inputs.config->afmt != AFMT_PCM_WAV)
|
|
|
|
return false;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
sample_rate = inputs.sample_rate;
|
|
|
|
num_channels = inputs.num_channels;
|
2008-10-08 22:18:16 +00:00
|
|
|
rec_mono_mode = inputs.rec_mono_mode;
|
2007-02-09 18:11:11 +00:00
|
|
|
err = 0;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
|
|
|
/* configure the buffer system */
|
2006-11-06 18:07:30 +00:00
|
|
|
params.afmt = AFMT_PCM_WAV;
|
|
|
|
enc_size = PCM_CHUNK_SIZE*inputs.num_channels / 2;
|
|
|
|
params.chunk_size = enc_size;
|
|
|
|
params.enc_sample_rate = sample_rate;
|
|
|
|
params.reserve_bytes = 0;
|
|
|
|
params.events_callback = enc_events_callback;
|
|
|
|
ci->enc_set_parameters(¶ms);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} /* init_encoder */
|
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
/* this is the codec entry point */
|
|
|
|
enum codec_status codec_main(enum codec_entry_call_reason reason)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
if (reason == CODEC_LOAD) {
|
|
|
|
if (!init_encoder())
|
|
|
|
return CODEC_ERROR;
|
|
|
|
}
|
|
|
|
else if (reason == CODEC_UNLOAD) {
|
|
|
|
/* reset parameters to initial state */
|
|
|
|
ci->enc_set_parameters(NULL);
|
|
|
|
}
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
return CODEC_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is called for each file to process */
|
|
|
|
enum codec_status codec_run(void)
|
|
|
|
{
|
2006-08-28 22:38:41 +00:00
|
|
|
/* main encoding loop */
|
2011-04-27 03:08:23 +00:00
|
|
|
while(ci->get_command(NULL) != CODEC_ACTION_HALT)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2011-04-27 03:08:23 +00:00
|
|
|
uint32_t *src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE);
|
|
|
|
struct enc_chunk_hdr *chunk;
|
2006-11-06 18:07:30 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
if(src == NULL)
|
|
|
|
continue;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
chunk = ci->enc_get_chunk();
|
|
|
|
chunk->enc_size = enc_size;
|
|
|
|
chunk->num_pcm = PCM_SAMP_PER_CHUNK;
|
|
|
|
chunk->enc_data = ENC_CHUNK_SKIP_HDR(chunk->enc_data, chunk);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
chunk_to_wav_format(src, (uint32_t *)chunk->enc_data);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2011-04-27 03:08:23 +00:00
|
|
|
ci->enc_finish_chunk();
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CODEC_OK;
|
2011-04-27 03:08:23 +00:00
|
|
|
}
|