2006-08-28 22:38:41 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Antonius Hellmann
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef SIMULATOR
|
|
|
|
|
|
|
|
#include "codeclib.h"
|
|
|
|
#include "libwavpack/wavpack.h"
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
CODEC_ENC_HEADER
|
|
|
|
|
|
|
|
/** Types **/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t type; /* Type of metadata */
|
|
|
|
uint8_t word_size; /* Size of metadata in words */
|
2006-11-29 17:52:30 +00:00
|
|
|
} __attribute__((packed)) WavpackMetadataHeader;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
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_BITS 16
|
|
|
|
#define PCM_DEPTH_BYTES 2
|
|
|
|
#define PCM_SAMP_PER_CHUNK 5000
|
|
|
|
#define PCM_CHUNK_SIZE (4*PCM_SAMP_PER_CHUNK)
|
|
|
|
|
|
|
|
/** Data **/
|
|
|
|
static int8_t input_buffer[PCM_CHUNK_SIZE*2] IBSS_ATTR;
|
|
|
|
static WavpackConfig config IBSS_ATTR;
|
|
|
|
static WavpackContext *wpc;
|
|
|
|
static int32_t data_size, input_size, input_step IBSS_ATTR;
|
2007-02-09 18:11:11 +00:00
|
|
|
static int32_t err IBSS_ATTR;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
static const WavpackMetadataHeader wvpk_mdh =
|
|
|
|
{
|
|
|
|
ID_RIFF_HEADER,
|
|
|
|
sizeof (struct riff_header) / sizeof (uint16_t),
|
|
|
|
};
|
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 */
|
|
|
|
H_TO_LE32(16), /* format_size */
|
|
|
|
/* format data */
|
|
|
|
H_TO_LE16(1), /* audio_format */
|
|
|
|
0, /* num_channels (*) */
|
|
|
|
0, /* sample_rate (*) */
|
|
|
|
0, /* byte_rate (*) */
|
|
|
|
0, /* block_align (*) */
|
|
|
|
H_TO_LE16(PCM_DEPTH_BITS), /* bits_per_sample */
|
|
|
|
/* data header */
|
|
|
|
{ 'd', 'a', 't', 'a' }, /* data_id */
|
|
|
|
0 /* data_size (*) */
|
|
|
|
/* (*) updated during ENC_END_FILE event */
|
|
|
|
};
|
|
|
|
|
2007-03-29 01:55:47 +00:00
|
|
|
static inline void sample_to_int32_mono(int32_t **src, int32_t **dst)
|
|
|
|
{
|
|
|
|
int32_t t = *(*src)++;
|
|
|
|
/* endianness irrelevant */
|
|
|
|
t = (int16_t)t + (t >> 16) + err;
|
|
|
|
err = t & 1;
|
|
|
|
*(*dst)++ = t >> 1;
|
|
|
|
} /* sample_to_int32_mono */
|
|
|
|
|
|
|
|
static inline void sample_to_int32_stereo(int32_t **src, int32_t **dst)
|
|
|
|
{
|
|
|
|
int32_t t = *(*src)++;
|
|
|
|
#ifdef ROCKBOX_BIG_ENDIAN
|
|
|
|
*(*dst)++ = t >> 16, *(*dst)++ = (int16_t)t;
|
|
|
|
#else
|
|
|
|
*(*dst)++ = (int16_t)t, *(*dst)++ = t >> 16;
|
|
|
|
#endif
|
|
|
|
} /* sample_to_int32_stereo */
|
|
|
|
|
2007-02-17 11:19:14 +00:00
|
|
|
STATICIRAM void chunk_to_int32(int32_t *src) ICODE_ATTR;
|
|
|
|
STATICIRAM void chunk_to_int32(int32_t *src)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
2006-11-29 17:52:30 +00:00
|
|
|
int32_t *src_end, *dst;
|
|
|
|
#ifdef USE_IRAM
|
2006-11-06 18:07:30 +00:00
|
|
|
/* copy to IRAM before converting data */
|
2006-11-29 17:52:30 +00:00
|
|
|
dst = (int32_t *)input_buffer + PCM_SAMP_PER_CHUNK;
|
|
|
|
src_end = dst + PCM_SAMP_PER_CHUNK;
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
memcpy(dst, src, PCM_CHUNK_SIZE);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
src = dst;
|
2006-11-29 17:52:30 +00:00
|
|
|
#else
|
|
|
|
src_end = src + PCM_SAMP_PER_CHUNK;
|
|
|
|
#endif
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
dst = (int32_t *)input_buffer;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
if (config.num_channels == 1)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
/*
|
|
|
|
* |llllllllllllllll|rrrrrrrrrrrrrrrr| =>
|
|
|
|
* |mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm|
|
|
|
|
*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* read 10 longs and write 10 longs */
|
2007-03-29 01:55:47 +00:00
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
|
|
|
sample_to_int32_mono(&src, &dst);
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
2006-11-06 18:07:30 +00:00
|
|
|
while(src < src_end);
|
|
|
|
|
|
|
|
return;
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
/*
|
|
|
|
* |llllllllllllllll|rrrrrrrrrrrrrrrr| =>
|
|
|
|
* |llllllllllllllllllllllllllllllll|rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
|
|
|
|
*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* read 10 longs and write 20 longs */
|
2007-03-29 01:55:47 +00:00
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
|
|
|
sample_to_int32_stereo(&src, &dst);
|
2006-11-06 18:07:30 +00:00
|
|
|
}
|
|
|
|
while (src < src_end);
|
|
|
|
|
|
|
|
return;
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
2006-11-06 18:07:30 +00:00
|
|
|
} /* chunk_to_int32 */
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* called very 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 very 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;
|
|
|
|
|
|
|
|
if (data->chunk->enc_data == NULL)
|
|
|
|
{
|
|
|
|
#ifdef ROCKBOX_HAS_LOGF
|
|
|
|
ci->logf("wvpk enc: NULL data");
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update timestamp (block_index) */
|
|
|
|
((WavpackHeader *)data->chunk->enc_data)->block_index =
|
|
|
|
htole32(data->num_pcm_samples);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC);
|
|
|
|
|
|
|
|
if (data->rec_file < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* reset sample count */
|
|
|
|
data->num_pcm_samples = 0;
|
|
|
|
|
|
|
|
/* write template headers */
|
|
|
|
if (ci->write(data->rec_file, &wvpk_mdh, sizeof (wvpk_mdh))
|
|
|
|
!= sizeof (wvpk_mdh) ||
|
|
|
|
ci->write(data->rec_file, &riff_header, sizeof (riff_header))
|
|
|
|
!= sizeof (riff_header))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->new_enc_size += sizeof(wvpk_mdh) + sizeof(riff_header);
|
|
|
|
return true;
|
|
|
|
} /* on_start_file */
|
|
|
|
|
|
|
|
static bool on_end_file(struct enc_file_event_data *data)
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
WavpackMetadataHeader wpmdh;
|
|
|
|
struct riff_header rhdr;
|
|
|
|
WavpackHeader wph;
|
|
|
|
} __attribute__ ((packed)) h;
|
|
|
|
|
|
|
|
uint32_t data_size;
|
|
|
|
|
|
|
|
if (!is_file_data_ok(data))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* read template headers at start */
|
|
|
|
if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
|
|
|
|
ci->read(data->rec_file, &h, sizeof (h)) != sizeof (h))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
data_size = data->num_pcm_samples*config.num_channels*PCM_DEPTH_BYTES;
|
|
|
|
|
|
|
|
/** "RIFF" header **/
|
|
|
|
h.rhdr.riff_size = htole32(RIFF_FMT_HEADER_SIZE +
|
|
|
|
RIFF_FMT_DATA_SIZE + RIFF_DATA_HEADER_SIZE + data_size);
|
|
|
|
|
|
|
|
/* format data */
|
|
|
|
h.rhdr.num_channels = htole16(config.num_channels);
|
|
|
|
h.rhdr.sample_rate = htole32(config.sample_rate);
|
|
|
|
h.rhdr.byte_rate = htole32(config.sample_rate*config.num_channels*
|
|
|
|
PCM_DEPTH_BYTES);
|
|
|
|
h.rhdr.block_align = htole16(config.num_channels*PCM_DEPTH_BYTES);
|
|
|
|
|
|
|
|
/* data header */
|
|
|
|
h.rhdr.data_size = htole32(data_size);
|
|
|
|
|
|
|
|
/** Wavpack header **/
|
|
|
|
h.wph.ckSize = htole32(letoh32(h.wph.ckSize) + sizeof (h.wpmdh)
|
|
|
|
+ sizeof (h.rhdr));
|
|
|
|
h.wph.total_samples = htole32(data->num_pcm_samples);
|
|
|
|
|
|
|
|
/* MDH|RIFF|WVPK => WVPK|MDH|RIFF */
|
|
|
|
if (ci->lseek(data->rec_file, 0, SEEK_SET)
|
|
|
|
!= 0 ||
|
|
|
|
ci->write(data->rec_file, &h.wph, sizeof (h.wph))
|
|
|
|
!= sizeof (h.wph) ||
|
|
|
|
ci->write(data->rec_file, &h.wpmdh, sizeof (h.wpmdh))
|
|
|
|
!= sizeof (h.wpmdh) ||
|
|
|
|
ci->write(data->rec_file, &h.rhdr, sizeof (h.rhdr))
|
2007-04-25 19:53:34 +00:00
|
|
|
!= sizeof (h.rhdr) ||
|
|
|
|
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-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
if (event == ENC_WRITE_CHUNK)
|
|
|
|
{
|
|
|
|
if (on_write_chunk((struct enc_file_event_data *)data))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (event == ENC_START_FILE)
|
|
|
|
{
|
|
|
|
/* write metadata header and RIFF header */
|
|
|
|
if (on_start_file((struct enc_file_event_data *)data))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (event == ENC_END_FILE)
|
|
|
|
{
|
|
|
|
if (on_end_file((struct enc_file_event_data *)data))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
|
|
|
|
} /* enc_events_callback */
|
|
|
|
|
|
|
|
static bool init_encoder(void)
|
|
|
|
{
|
|
|
|
struct enc_inputs inputs;
|
|
|
|
struct enc_parameters params;
|
2006-09-23 14:38:04 +00:00
|
|
|
|
2006-11-26 19:10:26 +00:00
|
|
|
codec_init();
|
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 ||
|
2006-11-29 17:52:30 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->enc_pcm_buf_near_empty == NULL ||
|
2006-11-29 17:52:30 +00:00
|
|
|
#endif
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->enc_get_pcm_data == NULL ||
|
|
|
|
ci->enc_unget_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);
|
|
|
|
|
|
|
|
if (inputs.config->afmt != AFMT_WAVPACK)
|
|
|
|
return false;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
memset(&config, 0, sizeof (config));
|
|
|
|
config.bits_per_sample = PCM_DEPTH_BITS;
|
|
|
|
config.bytes_per_sample = PCM_DEPTH_BYTES;
|
|
|
|
config.sample_rate = inputs.sample_rate;
|
|
|
|
config.num_channels = inputs.num_channels;
|
|
|
|
|
|
|
|
wpc = WavpackOpenFileOutput ();
|
|
|
|
|
|
|
|
if (!WavpackSetConfiguration(wpc, &config, -1))
|
|
|
|
return false;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
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_WAVPACK;
|
|
|
|
input_size = PCM_CHUNK_SIZE*inputs.num_channels / 2;
|
|
|
|
data_size = 105*input_size / 100;
|
|
|
|
input_size *= 2;
|
|
|
|
input_step = input_size / 4;
|
|
|
|
params.chunk_size = data_size;
|
|
|
|
params.enc_sample_rate = inputs.sample_rate;
|
|
|
|
params.reserve_bytes = 0;
|
|
|
|
params.events_callback = enc_events_callback;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->enc_set_parameters(¶ms);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
return true;
|
|
|
|
} /* init_encoder */
|
|
|
|
|
2006-11-26 18:31:41 +00:00
|
|
|
enum codec_status codec_main(void)
|
2006-11-06 18:07:30 +00:00
|
|
|
{
|
2006-11-29 17:52:30 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2006-11-06 18:07:30 +00:00
|
|
|
bool cpu_boosted;
|
2006-11-29 17:52:30 +00:00
|
|
|
#endif
|
2006-11-06 18:07:30 +00:00
|
|
|
|
|
|
|
/* initialize params and config */
|
|
|
|
if (!init_encoder())
|
|
|
|
{
|
|
|
|
ci->enc_codec_loaded = -1;
|
2006-08-28 22:38:41 +00:00
|
|
|
return CODEC_ERROR;
|
2006-11-06 18:07:30 +00:00
|
|
|
}
|
2006-08-28 22:38:41 +00:00
|
|
|
|
|
|
|
/* main application waits for this flag during encoder loading */
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->enc_codec_loaded = 1;
|
|
|
|
|
2006-11-29 17:52:30 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->cpu_boost(true);
|
|
|
|
cpu_boosted = true;
|
2006-11-29 17:52:30 +00:00
|
|
|
#endif
|
2006-08-28 22:38:41 +00:00
|
|
|
|
|
|
|
/* main encoding loop */
|
2007-03-04 04:16:53 +00:00
|
|
|
while(!ci->stop_encoder)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
uint8_t *src;
|
|
|
|
|
|
|
|
while ((src = ci->enc_get_pcm_data(PCM_CHUNK_SIZE)) != NULL)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
struct enc_chunk_hdr *chunk;
|
|
|
|
bool abort_chunk;
|
|
|
|
uint8_t *dst;
|
|
|
|
uint8_t *src_end;
|
|
|
|
|
2007-03-04 04:16:53 +00:00
|
|
|
if(ci->stop_encoder)
|
2006-08-28 22:38:41 +00:00
|
|
|
break;
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
abort_chunk = true;
|
|
|
|
|
2006-11-29 17:52:30 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2006-11-06 18:07:30 +00:00
|
|
|
if (!cpu_boosted && ci->enc_pcm_buf_near_empty() == 0)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->cpu_boost(true);
|
|
|
|
cpu_boosted = true;
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
2006-11-29 17:52:30 +00:00
|
|
|
#endif
|
2006-11-06 18:07:30 +00:00
|
|
|
chunk = ci->enc_get_chunk();
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
/* reset counts and pointer */
|
|
|
|
chunk->enc_size = 0;
|
|
|
|
chunk->num_pcm = 0;
|
|
|
|
chunk->enc_data = NULL;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
dst = ENC_CHUNK_SKIP_HDR(dst, chunk);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
WavpackStartBlock(wpc, dst, dst + data_size);
|
|
|
|
|
|
|
|
chunk_to_int32((uint32_t*)src);
|
|
|
|
src = input_buffer;
|
|
|
|
src_end = src + input_size;
|
|
|
|
|
|
|
|
/* encode chunk in four steps yielding between each */
|
|
|
|
do
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
if (WavpackPackSamples(wpc, (int32_t *)src,
|
|
|
|
PCM_SAMP_PER_CHUNK/4))
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
chunk->num_pcm += PCM_SAMP_PER_CHUNK/4;
|
|
|
|
ci->yield();
|
|
|
|
/* could've been stopped in some way */
|
2007-03-04 04:16:53 +00:00
|
|
|
abort_chunk = ci->stop_encoder ||
|
2006-11-06 18:07:30 +00:00
|
|
|
(chunk->flags & CHUNKF_ABORT);
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
src += input_step;
|
|
|
|
}
|
|
|
|
while (!abort_chunk && src < src_end);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
2006-11-06 18:07:30 +00:00
|
|
|
if (!abort_chunk)
|
|
|
|
{
|
|
|
|
chunk->enc_data = dst;
|
|
|
|
if (chunk->num_pcm < PCM_SAMP_PER_CHUNK)
|
|
|
|
ci->enc_unget_pcm_data(PCM_CHUNK_SIZE - chunk->num_pcm*4);
|
2006-08-28 22:38:41 +00:00
|
|
|
/* finish the chunk and store chunk size info */
|
2006-11-06 18:07:30 +00:00
|
|
|
chunk->enc_size = WavpackFinishBlock(wpc);
|
|
|
|
ci->enc_finish_chunk();
|
|
|
|
}
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
|
|
|
|
2006-11-29 17:52:30 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2006-11-06 18:07:30 +00:00
|
|
|
if (cpu_boosted && ci->enc_pcm_buf_near_empty() != 0)
|
2006-08-28 22:38:41 +00:00
|
|
|
{
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->cpu_boost(false);
|
|
|
|
cpu_boosted = false;
|
2006-08-28 22:38:41 +00:00
|
|
|
}
|
2006-11-29 17:52:30 +00:00
|
|
|
#endif
|
2006-08-28 22:38:41 +00:00
|
|
|
ci->yield();
|
|
|
|
}
|
|
|
|
|
2006-11-29 17:52:30 +00:00
|
|
|
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
|
2006-11-06 18:07:30 +00:00
|
|
|
if (cpu_boosted) /* set initial boost state */
|
2006-08-28 22:38:41 +00:00
|
|
|
ci->cpu_boost(false);
|
2006-11-29 17:52:30 +00:00
|
|
|
#endif
|
2006-08-28 22:38:41 +00:00
|
|
|
|
|
|
|
/* reset parameters to initial state */
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->enc_set_parameters(NULL);
|
2006-08-28 22:38:41 +00:00
|
|
|
|
|
|
|
/* main application waits for this flag during encoder removing */
|
2006-11-06 18:07:30 +00:00
|
|
|
ci->enc_codec_loaded = 0;
|
2006-08-28 22:38:41 +00:00
|
|
|
|
|
|
|
return CODEC_OK;
|
2006-11-06 18:07:30 +00:00
|
|
|
} /* codec_start */
|
|
|
|
|
|
|
|
#endif /* ndef SIMULATOR */
|