Update software recording engine to latest codec interface.

Basically, just give it a good rewrite.

Software codec recording can be implemented in a more straightforward
and simple manner and made more robust through the better codec
control now available.

Encoded audio buffer uses a packed format instead of fixed-size
chunks and uses smaller data headers leading to more efficient usage.
The greatest benefit is with a VBR format like wavpack which needs
to request a maximum size but only actually ends up committing part
of that request.

No guard buffers are used for either PCM or encoded audio. PCM is
read into the codec's provided buffer and mono conversion done at
that time in the core if required. Any highly-specialized sample
conversion is still done within the codec itself, such as 32-bit
(wavpack) or interleaved mono (mp3).

There is no longer a separate filename array. All metadata goes
onto the main encoded audio buffer, eliminating any predermined
file limit on the buffer as well as not wasting the space for
unused path queue slots.

The core and codec interface is less awkward and a bit more sensible.
Some less useful interface features were removed. Threads are kept
on narrow code paths ie. the audio thread never calls encoding
functions and the codec thread never calls file functions as before.

Codecs no longer call file functions directly. Writes are buffered
in the core and data written to storage in larger chunks to speed up
flushing of data. In fact, codecs are no longer aware of the stream
being a file at all and have no access to the fd.

SPDIF frequency detection no longer requires a restart of recording
or plugging the source before entering the screen. It will poll
for changes and update when stopped or prerecording (which does
discard now-invalid prerecorded data).

I've seen to it that writing a proper header on full disk works
when the format makes it reasonably practical to do so. Other cases
may have incorrect data sizes but sample info will be in tact. File
left that way may play anyway.

mp3_enc.codec acquires the ability to write 'Info' headers with LAME
tags to make it gapless (bonus).

Change-Id: I670685166d5eb32ef58ef317f50b8af766ceb653
Reviewed-on: http://gerrit.rockbox.org/493
Reviewed-by: Michael Sevakis <jethead71@rockbox.org>
Tested-by: Michael Sevakis <jethead71@rockbox.org>
This commit is contained in:
Michael Sevakis 2013-06-22 16:41:16 -04:00
parent a9ea1a4269
commit 4888131972
16 changed files with 2821 additions and 2949 deletions

View file

@ -108,6 +108,16 @@ static void NORETURN_ATTR audio_thread(void)
}
}
void audio_queue_post(long id, intptr_t data)
{
queue_post(&audio_queue, id, data);
}
intptr_t audio_queue_send(long id, intptr_t data)
{
return queue_send(&audio_queue, id, data);
}
/* Return the playback and recording status */
int audio_status(void)
{
@ -156,7 +166,9 @@ void audio_init(void)
audio_thread_id);
playback_init();
/* Recording doesn't need init call */
#ifdef AUDIO_HAVE_RECORDING
recording_init();
#endif
/* ...now...audio_reset_buffer must know the size of voicefile buffer so
init talk first which will init the buffers */

View file

@ -74,7 +74,10 @@ enum
Q_AUDIO_CLOSE_RECORDING,
Q_AUDIO_RECORDING_OPTIONS,
Q_AUDIO_RECORD,
Q_AUDIO_RESUME,
Q_AUDIO_RECORD_STOP,
Q_AUDIO_RECORD_PAUSE,
Q_AUDIO_RECORD_RESUME,
Q_AUDIO_RECORD_FLUSH,
#endif
/*- settings -*/
@ -99,4 +102,8 @@ void audio_playback_handler(struct queue_event *ev);
void audio_recording_handler(struct queue_event *ev);
#endif
/** --- audio_queue helpers --- **/
void audio_queue_post(long id, intptr_t data);
intptr_t audio_queue_send(long id, intptr_t data);
#endif /* AUDIO_THREAD_H */

View file

@ -365,10 +365,14 @@ static enum codec_command_action
queue_peek(&codec_queue, &ev); /* Find out what it is */
long id = ev.id;
intptr_t id = ev.id;
switch (id)
{
case Q_NULL:
LOGFQUEUE("codec < Q_NULL");
break;
case Q_CODEC_RUN: /* Already running */
LOGFQUEUE("codec < Q_CODEC_RUN");
break;
@ -388,8 +392,25 @@ static enum codec_command_action
break;
case Q_CODEC_STOP: /* Must only return 0 in main loop */
LOGFQUEUE("codec < Q_CODEC_STOP");
dsp_configure(ci.dsp, DSP_FLUSH, 0); /* Discontinuity */
LOGFQUEUE("codec < Q_CODEC_STOP: %ld", ev.data);
#ifdef HAVE_RECORDING
if (type_is_encoder(codec_type))
{
/* Stream finish request (soft stop)? */
if (ev.data && param)
{
/* ev.data is pointer to size */
*param = ev.data;
action = CODEC_ACTION_STREAM_FINISH;
break;
}
}
else
#endif /* HAVE_RECORDING */
{
dsp_configure(ci.dsp, DSP_FLUSH, 0); /* Discontinuity */
}
return CODEC_ACTION_HALT; /* Leave in queue */
default: /* This is in error in this context. */
@ -459,7 +480,8 @@ static void load_codec(const struct codec_load_info *ev_data)
}
}
if (status >= 0)
/* Types must agree */
if (status >= 0 && encoder == !!codec_get_enc_callback())
{
codec_type = data.afmt;
codec_queue_ack(Q_CODEC_LOAD);
@ -685,10 +707,33 @@ bool codec_pause(void)
void codec_stop(void)
{
/* Wait until it's in the main loop */
LOGFQUEUE("audio >| codec Q_CODEC_STOP");
LOGFQUEUE("audio >| codec Q_CODEC_STOP: 0");
while (codec_queue_send(Q_CODEC_STOP, 0) != Q_NULL);
}
#ifdef HAVE_RECORDING
/* Tells codec to take final encoding step and then exit -
Returns minimum buffer size required or 0 if complete */
size_t codec_finish_stream(void)
{
size_t size = 0;
LOGFQUEUE("audio >| codec Q_CODEC_STOP: &size");
if (codec_queue_send(Q_CODEC_STOP, (intptr_t)&size) != Q_NULL)
{
/* Sync to keep size in scope and get response */
LOGFQUEUE("audio >| codec Q_NULL");
codec_queue_send(Q_NULL, 0);
if (size == 0)
codec_stop(); /* Replied with 0 size */
}
/* else thread running in the main loop */
return size;
}
#endif /* HAVE_RECORDING */
/* Call the codec's exit routine and close all references */
void codec_unload(void)
{

View file

@ -46,6 +46,9 @@ void codec_go(void);
bool codec_pause(void);
void codec_seek(long time);
void codec_stop(void);
#ifdef HAVE_RECORDING
size_t codec_finish_stream(void);
#endif
void codec_unload(void);
int codec_loaded(void);

View file

@ -73,13 +73,6 @@ static size_t codec_size;
extern void* plugin_get_audio_buffer(size_t *buffer_size);
#if (CONFIG_PLATFORM & PLATFORM_NATIVE) && defined(HAVE_RECORDING)
#undef open
static int open(const char* pathname, int flags, ...)
{
return file_open(pathname, flags);
}
#endif
struct codec_api ci = {
0, /* filesize */
@ -147,21 +140,14 @@ struct codec_api ci = {
#endif
#ifdef HAVE_RECORDING
enc_get_inputs,
enc_set_parameters,
enc_get_chunk,
enc_finish_chunk,
enc_get_pcm_data,
enc_unget_pcm_data,
/* file */
(open_func)PREFIX(open),
PREFIX(close),
(read_func)PREFIX(read),
PREFIX(lseek),
(write_func)PREFIX(write),
NULL, /* enc_pcmbuf_read */
NULL, /* enc_pcmbuf_advance */
NULL, /* enc_encbuf_get_buffer */
NULL, /* enc_encbuf_finish_buffer */
NULL, /* enc_stream_read */
NULL, /* enc_stream_lseek */
NULL, /* enc_stream_write */
round_value_to_list32,
#endif /* HAVE_RECORDING */
/* new stuff at the end, sort into place next time
@ -299,3 +285,16 @@ int codec_close(void)
return status;
}
#ifdef HAVE_RECORDING
enc_callback_t codec_get_enc_callback(void)
{
if (curr_handle == NULL ||
c_hdr->lc_hdr.magic != CODEC_ENC_MAGIC) {
logf("Codec: not an encoder");
return NULL;
}
return c_hdr->rec_extension[0];
}
#endif /* HAVE_RECORDING */

View file

@ -351,18 +351,6 @@ void playback_voice_event(void *data)
pcmbuf_soft_mode(*(bool *)data);
}
/** --- audio_queue helpers --- **/
static void audio_queue_post(long id, intptr_t data)
{
queue_post(&audio_queue, id, data);
}
static intptr_t audio_queue_send(long id, intptr_t data)
{
return queue_send(&audio_queue, id, data);
}
/** --- MP3Entry --- **/
/* Does the mp3entry have enough info for us to use it? */

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,7 @@
* $Id$
*
* Copyright (C) 2005 by Linus Nielsen Feltzing
* Copyright (C) 2006-2013 by Michael Sevakis
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -18,47 +19,64 @@
* KIND, either express or implied.
*
****************************************************************************/
#ifndef PCM_RECORD_H
#define PCM_RECORD_H
#include "config.h"
/** Warnings (recording may continue with possible data loss)
** Reset of recording clears, otherwise see notes below
*/
/** Warnings **/
/* pcm (dma) buffer has overflowed */
/* PCM buffer has overflowed; PCM samples were dropped */
/* persists until: stop, new file, clear */
#define PCMREC_W_PCM_BUFFER_OVF 0x00000001
/* encoder output buffer has overflowed */
/* encoder output buffer has overflowed; encoded data was dropped */
/* persists until: stop, new file, clear */
#define PCMREC_W_ENC_BUFFER_OVF 0x00000002
/** Errors **/
/* failed to load encoder */
#define PCMREC_E_LOAD_ENCODER 0x80001000
/* error originating in encoder */
#define PCMREC_E_ENCODER 0x80002000
/* filename queue has desynced from stream markers */
#define PCMREC_E_FNQ_DESYNC 0x80004000
/* I/O error has occurred */
#define PCMREC_E_IO 0x80008000
#ifdef DEBUG
/* encoder has written past end of allocated space */
#define PCMREC_E_CHUNK_OVF 0x80010000
#endif /* DEBUG */
/* DMA callback has reported an error */
#define PCMREC_E_DMA 0x80020000
/* encoder and set/detected sample rates do not match */
/* persists until: rates match, clear */
#define PCMREC_W_SAMPR_MISMATCH 0x00000004
/* PCM frame skipped because of recoverable DMA error */
/* persists until: clear */
#define PCMREC_W_DMA 0x00000008
/* internal file size limit was reached; encoded data was dropped */
/* persists until: stop, new file, clear */
#define PCMREC_W_FILE_SIZE 0x00000010
/** General functions for high level codec recording **/
/* pcm_rec_error_clear is deprecated for general use. audio_error_clear
should be used */
/* all warning flags */
#define PCMREC_W_ALL 0x0000001f
/** Errors (recording should be reset)
**
** Stopping recording if recording clears internally and externally visible
** status must be cleared with audio_error_clear()
** reset of recording clears
*/
/* DMA callback has reported an error */
#define PCMREC_E_DMA 0x80010000
/* failed to load encoder */
#define PCMREC_E_LOAD_ENCODER 0x80020000
/* error originating in encoder */
#define PCMREC_E_ENCODER 0x80040000
/* error from encoder setup of stream parameters */
#define PCMREC_E_ENC_SETUP 0x80080000
/* error writing to output stream */
#define PCMREC_E_ENCODER_STREAM 0x80100000
/* I/O error has occurred */
#define PCMREC_E_IO 0x80200000
/* all error flags */
#define PCMREC_E_ALL 0x803f0000
/* Functions called by audio_thread.c */
void pcm_rec_error_clear(void);
/* pcm_rec_status is deprecated for general use. audio_status merges the
results for consistency with the hardware codec version */
unsigned int pcm_rec_status(void);
unsigned long pcm_rec_get_warnings(void);
void pcm_rec_init(void) INIT_ATTR;
int pcm_rec_current_bitrate(void);
int pcm_rec_encoder_afmt(void); /* AFMT_* value, AFMT_UNKNOWN if none */
int pcm_rec_rec_format(void); /* Format index or -1 otherwise */
uint32_t pcm_rec_get_warnings(void);
#ifdef HAVE_SPDIF_IN
unsigned long pcm_rec_sample_rate(void);
int pcm_get_num_unprocessed(void);
#endif
void recording_init(void);
/* audio.h contains audio_* recording functions */

View file

@ -1730,7 +1730,7 @@ bool recording_screen(bool no_source)
/* Don't use language string unless agreed upon to make this
method permanent - could do something in the statusbar */
snprintf(buf, sizeof(buf), "Warning: %08lX",
pcm_rec_get_warnings());
(unsigned long)pcm_rec_get_warnings());
}
else
#endif /* CONFIG_CODEC == SWCODEC */
@ -1755,8 +1755,16 @@ bool recording_screen(bool no_source)
if(audio_stat & AUDIO_STATUS_PRERECORD)
{
#if CONFIG_CODEC == SWCODEC
/* Tracks amount of prerecorded data in buffer */
snprintf(buf, sizeof(buf), "%s (%lu/%ds)...",
str(LANG_RECORD_PRERECORD),
audio_prerecorded_time() / HZ,
global_settings.rec_prerecord_time);
#else /* !SWCODEC */
snprintf(buf, sizeof(buf), "%s...",
str(LANG_RECORD_PRERECORD));
#endif /* CONFIG_CODEC == SWCODEC */
}
else
{
@ -1915,8 +1923,7 @@ bool recording_screen(bool no_source)
screens[i].update();
#if CONFIG_CODEC == SWCODEC
/* stop recording - some players like H10 freeze otherwise
TO DO: find out why it freezes and fix properly */
/* stop recording first and try to finish saving whatever it can */
rec_command(RECORDING_CMD_STOP);
audio_close_recording();
#endif

View file

@ -212,10 +212,7 @@ unsigned long audio_num_recorded_bytes(void);
#if CONFIG_CODEC == SWCODEC
/* SWCODEC recording functions */
/* playback.c */
bool audio_load_encoder(int afmt);
void audio_remove_encoder(void);
unsigned char *audio_get_recording_buffer(size_t *buffer_size);
unsigned long audio_prerecorded_time(void);
#endif /* CONFIG_CODEC == SWCODEC */
#endif /* HAVE_RECORDING */

View file

@ -9,7 +9,7 @@
*
* Base declarations for working with software encoders
*
* Copyright (C) 2006 Michael Sevakis
* Copyright (C) 2006-2013 Michael Sevakis
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -24,7 +24,9 @@
#ifndef ENC_BASE_H
#define ENC_BASE_H
/** encoder config structures **/
#include <sys/types.h>
/** Encoder config structures **/
/** aiff_enc.codec **/
struct aiff_enc_config
@ -57,18 +59,22 @@ struct aiff_enc_config
/* MPEG 1 */
#define MPEG1_SAMPR_CAPS (SAMPR_CAP_32 | SAMPR_CAP_48 | SAMPR_CAP_44)
#define MPEG1_BITR_CAPS (MP3_BITR_CAP_32 | MP3_BITR_CAP_40 | MP3_BITR_CAP_48 | \
MP3_BITR_CAP_56 | MP3_BITR_CAP_64 | MP3_BITR_CAP_80 | \
MP3_BITR_CAP_96 | MP3_BITR_CAP_112 | MP3_BITR_CAP_128 | \
MP3_BITR_CAP_160 | MP3_BITR_CAP_192 | MP3_BITR_CAP_224 | \
#define MPEG1_BITR_CAPS (MP3_BITR_CAP_32 | MP3_BITR_CAP_40 | \
MP3_BITR_CAP_48 | MP3_BITR_CAP_56 | \
MP3_BITR_CAP_64 | MP3_BITR_CAP_80 | \
MP3_BITR_CAP_96 | MP3_BITR_CAP_112 | \
MP3_BITR_CAP_128 | MP3_BITR_CAP_160 | \
MP3_BITR_CAP_192 | MP3_BITR_CAP_224 | \
MP3_BITR_CAP_256 | MP3_BITR_CAP_320)
/* MPEG 2 */
#define MPEG2_SAMPR_CAPS (SAMPR_CAP_22 | SAMPR_CAP_24 | SAMPR_CAP_16)
#define MPEG2_BITR_CAPS (MP3_BITR_CAP_8 | MP3_BITR_CAP_16 | MP3_BITR_CAP_24 | \
MP3_BITR_CAP_32 | MP3_BITR_CAP_40 | MP3_BITR_CAP_48 | \
MP3_BITR_CAP_56 | MP3_BITR_CAP_64 | MP3_BITR_CAP_80 | \
MP3_BITR_CAP_96 | MP3_BITR_CAP_112 | MP3_BITR_CAP_128 | \
#define MPEG2_BITR_CAPS (MP3_BITR_CAP_8 | MP3_BITR_CAP_16 | \
MP3_BITR_CAP_24 | MP3_BITR_CAP_32 | \
MP3_BITR_CAP_40 | MP3_BITR_CAP_48 | \
MP3_BITR_CAP_56 | MP3_BITR_CAP_64 | \
MP3_BITR_CAP_80 | MP3_BITR_CAP_96 | \
MP3_BITR_CAP_112 | MP3_BITR_CAP_128 | \
MP3_BITR_CAP_144 | MP3_BITR_CAP_160)
#if 0
@ -131,6 +137,7 @@ struct wavpack_enc_config
#endif
};
/* General config information about any encoder */
struct encoder_config
{
union
@ -149,144 +156,77 @@ struct encoder_config
};
/** Encoder chunk macros and definitions **/
#define CHUNKF_START_FILE 0x0001ul /* This chunk starts a new file */
#define CHUNKF_END_FILE 0x0002ul /* This chunk ends the current file */
#define CHUNKF_PRERECORD 0x0010ul /* This chunk is prerecord data,
a new file could start anytime */
#define CHUNKF_ABORT 0x0020ul /* Encoder should not finish this
chunk */
#define CHUNKF_ERROR (~0ul ^ (~0ul >> 1)) /* An error has occured
(passed to/from encoder). Use the
sign bit to check (long)flags < 0. */
#define CHUNKF_ALLFLAGS (0x0033ul | CHUNKF_ERROR)
/* Header at the beginning of every encoder chunk */
#ifdef DEBUG
#define H_TO_BE32 htobe32
#define ENC_CHUNK_MAGIC H_TO_BE32(('P' << 24) | ('T' << 16) | ('Y' << 8) | 'R')
#endif
struct enc_chunk_hdr
/* What sort of data does the header describe? */
enum CHUNK_T
{
#ifdef DEBUG
unsigned long id; /* overflow detection - 'PTYR' - acronym for
"PTYR Tells You Right" ;) */
#endif
unsigned long flags; /* in/out: flags used by encoder and file
writing */
size_t enc_size; /* out: amount of encoder data written to
chunk */
unsigned long num_pcm; /* out: number of PCM samples eaten during
processing
(<= size of allocated buffer) */
unsigned char *enc_data; /* out: pointer to enc_size_written bytes
of encoded audio data in chunk */
/* Encoder defined data follows header. Can be audio data + any other
stuff the encoder needs to handle on a per chunk basis */
CHUNK_T_DATA = 0x0, /* Encoded audio data */
CHUNK_T_STREAM_START = 0x1, /* Stream start marker */
CHUNK_T_STREAM_END = 0x2, /* Stream end marker */
CHUNK_T_WRAP = 0x3 /* Buffer early wrap marker */
};
/* Paranoia: be sure header size is 4-byte aligned */
#define ENC_CHUNK_HDR_SIZE \
ALIGN_UP_P2(sizeof (struct enc_chunk_hdr), 2)
/* Skip the chunk header and return data */
#define ENC_CHUNK_SKIP_HDR(t, hdr) \
((typeof (t))((char *)hdr + ENC_CHUNK_HDR_SIZE))
/* Cast p to struct enc_chunk_hdr * */
#define ENC_CHUNK_HDR(p) \
((struct enc_chunk_hdr *)(p))
enum enc_events
/* Header for every buffer slot and chunk */
union enc_chunk_hdr
{
/* File writing events - data points to enc_file_event_data */
ENC_START_FILE = 0, /* a new file has been opened and no data has yet
been written */
ENC_WRITE_CHUNK, /* write the current chunk to disk */
ENC_END_FILE, /* current file about to be closed and all valid
data has been written */
/* Encoder buffer events - data points to enc_buffer_event_data */
ENC_REC_NEW_STREAM, /* Take steps to finish current stream and start
new */
};
struct
{
uint32_t type : 2; /* Chunk type (CHUNK_T_*) */
uint32_t err : 1; /* Encoder error */
uint32_t pre : 1; /* Chunk is prerecorded data */
uint32_t aux0 : 1; /* Aux flag 0 - for encoder */
uint32_t unused : 3; /* */
uint32_t size : 24; /* size of data */
};
uint32_t zero; /* Zero-out struct access */
intptr_t reserved1; /* Want it at least pointer-sized */
} __attribute__((__may_alias__));
/**
* encoder can write extra data to the file such as headers or more encoded
* samples and must update sizes and samples accordingly.
*/
struct enc_file_event_data
#define ENC_HDR_SIZE (sizeof (union enc_chunk_hdr))
/* When hdr.type is CHUNK_T_STREAM_START */
struct enc_chunk_file
{
struct enc_chunk_hdr *chunk; /* Current chunk */
size_t new_enc_size; /* New size of chunk */
unsigned long new_num_pcm; /* New number of pcm in chunk */
const char *filename; /* filename to open if ENC_START_FILE */
int rec_file; /* Current file or < 0 if none */
unsigned long num_pcm_samples; /* Current pcm sample count written to
file so far. */
};
union enc_chunk_hdr hdr; /* This chunk's header */
/* hdr.size = slot count of chunk */
char path[]; /* NULL-terminated path of file */
} __attribute__((__may_alias__));
/**
* encoder may add some data to the end of the last and start of the next
* but must never yield when called so any encoding done should be absolutely
* minimal.
*/
struct enc_buffer_event_data
/* If flags = CHUNK_T_STREAM_END, just the header exists */
/* When hdr.type is CHUNK_T_DATA */
struct enc_chunk_data
{
unsigned long flags; /* in: One or more of:
* CHUNKF_PRERECORD
* CHUNKF_END_FILE
* CHUNKF_START_FILE
*/
struct enc_chunk_hdr *pre_chunk; /* in: pointer to first prerecord
* chunk
*/
struct enc_chunk_hdr *chunk; /* in,out: chunk were split occurs -
* first chunk of start
*/
};
union enc_chunk_hdr hdr; /* IN,OUT: This chunk's header */
/* hdr.size = total size of data[] */
uint32_t pcm_count; /* OUT: number of PCM samples encoded */
uint8_t data[]; /* OUT: encoded audio data */
} __attribute__((__may_alias__));
/** Callbacks called by encoder codec **/
/* CHUNK_T_STREAM_END and CHUNK_T_WRAP consist of only the header */
/* parameters passed to encoder by enc_get_inputs */
#define ENC_FILE_HDR(hdr) ((struct enc_chunk_file *)(hdr))
#define ENC_DATA_HDR(hdr) ((struct enc_chunk_data *)(hdr))
/* Audio and encoder stream parameters */
struct enc_inputs
{
unsigned long sample_rate; /* out - pcm frequency */
int num_channels; /* out - number of audio channels */
int rec_mono_mode; /* out - how to create mono */
struct encoder_config *config; /* out - encoder settings */
};
void enc_get_inputs(struct enc_inputs *inputs);
/* parameters pass from encoder to enc_set_parameters */
struct enc_parameters
{
/* IN parameters */
int afmt; /* AFMT_* id - sanity checker */
size_t chunk_size; /* max chunk size required */
unsigned long enc_sample_rate; /* actual sample rate used by encoder
(for recorded time calculation) */
size_t reserve_bytes; /* number of bytes to reserve immediately
following chunks */
void (*events_callback)(enum enc_events event,
void *data); /* pointer to events callback */
/* OUT parameters */
unsigned char *enc_buffer; /* pointer to enc_buffer */
size_t buf_chunk_size; /* size of chunks in enc_buffer */
int num_chunks; /* number of chunks allotted to encoder */
unsigned char *reserve_buffer; /* pointer to reserve_bytes bytes */
unsigned long sample_rate; /* PCM samplerate setting */
int num_channels; /* Number of audio channels */
struct encoder_config *config; /* Encoder settings */
/* IN,OUT parameters */
unsigned long enc_sample_rate; /* Actual sample rate accepted by encoder
(for recorded time calculation) */
};
/* set the encoder dimensions - called by encoder codec at initialization
and termination */
void enc_set_parameters(struct enc_parameters *params);
/* returns pointer to next write chunk in circular buffer */
struct enc_chunk_hdr * enc_get_chunk(void);
/* releases the current chunk into the available chunks */
void enc_finish_chunk(void);
enum enc_callback_reason
{
ENC_CB_INPUTS, /* 'params' is struct enc_inputs * */
ENC_CB_STREAM, /* 'params' is union enc_chunk_hdr * */
};
#define PCM_MAX_FEED_SIZE 20000 /* max pcm size passed to encoder */
/* passes a pointer to next chunk of unprocessed wav data */
unsigned char * enc_get_pcm_data(size_t size);
/* puts some pcm data back in the queue */
size_t enc_unget_pcm_data(size_t size);
typedef int (* enc_callback_t)(enum enc_callback_reason reason, void *params);
#endif /* ENC_BASE_H */

View file

@ -8,6 +8,7 @@
* $Id$
*
* Copyright (C) 2006 Antonius Hellmann
* Copyright (C) 2006-2013 Michael Sevakis
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -47,10 +48,15 @@ struct aiff_header
#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)
static int num_channels;
static uint32_t sample_rate;
static size_t frame_size;
static size_t pcm_size;
static uint32_t num_sample_frames;
/* Template headers */
struct aiff_header aiff_header =
static const struct aiff_header aiff_template_header =
{
{ 'F', 'O', 'R', 'M' }, /* form_id */
0, /* form_size (*) */
@ -65,336 +71,193 @@ struct aiff_header aiff_header =
0, /* ssnd_size (*) */
htobe32(0), /* offset */
htobe32(0), /* block_size */
/* (*) updated when finalizing stream */
};
/* (*) updated when finalizing file */
static int num_channels IBSS_ATTR;
static int rec_mono_mode IBSS_ATTR;
static uint32_t sample_rate;
static uint32_t enc_size;
static int32_t err IBSS_ATTR;
static inline void frame_htobe(uint32_t *p, size_t size)
{
#ifdef ROCKBOX_LITTLE_ENDIAN
/* Byte-swap samples, stereo or mono */
do
{
uint32_t t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
}
while (size -= 8 * 2 * PCM_DEPTH_BYTES);
#endif /* ROCKBOX_LITTLE_ENDIAN */
(void)p; (void)size;
}
/* convert unsigned 32 bit value to 80-bit floating point number */
static void uint32_h_to_ieee754_extended_be(uint8_t f[10], uint32_t l)
ICODE_ATTR;
static void uint32_h_to_ieee754_extended_be(uint8_t f[10], uint32_t l)
{
int32_t exp;
ci->memset(f, 0, 10);
if (l == 0)
return;
for (exp = 30; (l & (1ul << 31)) == 0; exp--)
l <<= 1;
int shift = __builtin_clz(l);
/* sign always zero - bit 79 */
/* exponent is 0-31 (normalized: 31 - shift + 16383) - bits 64-78 */
/* exponent is 0-31 (normalized: 30 - shift + 16383) - bits 64-78 */
f[0] = 0x40;
f[1] = (uint8_t)exp;
f[1] = (uint8_t)(30 - shift);
/* mantissa is value left justified with most significant non-zero
bit stored in bit 63 - bits 0-63 */
l <<= shift;
f[2] = (uint8_t)(l >> 24);
f[3] = (uint8_t)(l >> 16);
f[4] = (uint8_t)(l >> 8);
f[5] = (uint8_t)(l >> 0);
} /* uint32_h_to_ieee754_extended_be */
}
/* 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)
static int on_stream_data(struct enc_chunk_data *data)
{
return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
} /* is_file_data_ok */
size_t size = data->hdr.size;
/* 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 (ci->enc_stream_write(data->data, size) != (ssize_t)size)
return -1;
pcm_size += size;
num_sample_frames += data->pcm_count;
return 0;
}
static int on_stream_start(void)
{
if (!is_file_data_ok(data))
return false;
if (data->chunk->enc_data == NULL)
{
#ifdef ROCKBOX_HAS_LOGF
ci->logf("aiff 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)
{
if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
return false;
data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
if (data->rec_file < 0)
return false;
/* reset sample count */
data->num_pcm_samples = 0;
pcm_size = 0;
num_sample_frames = 0;
/* write template headers */
if (ci->write(data->rec_file, &aiff_header, sizeof (aiff_header))
!= sizeof (aiff_header))
{
return false;
}
/* write template header */
if (ci->enc_stream_write(&aiff_template_header,
sizeof (struct aiff_header))
!= sizeof (struct aiff_header))
return -1;
data->new_enc_size += sizeof(aiff_header);
return true;
} /* on_start_file */
return 0;
}
static bool on_end_file(struct enc_file_event_data *data)
static int on_stream_end(union enc_chunk_hdr *hdr)
{
/* update template headers */
struct aiff_header hdr;
uint32_t data_size;
/* update template header */
struct aiff_header aiff;
if (!is_file_data_ok(data))
return false;
if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
ci->read(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr))
if (hdr->err)
{
return false;
/* Called for stream error; get correct data size */
ssize_t size = ci->enc_stream_lseek(0, SEEK_END);
if (size > (ssize_t)sizeof (aiff))
{
pcm_size = size - sizeof (aiff);
num_sample_frames = pcm_size / (PCM_DEPTH_BYTES*num_channels);
}
}
data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
if (ci->enc_stream_lseek(0, SEEK_SET) != 0)
return -1;
if (ci->enc_stream_read(&aiff, sizeof (aiff)) != sizeof (aiff))
return -2;
/* 'FORM' chunk */
hdr.form_size = htobe32(data_size + sizeof (hdr) - 8);
aiff.form_size = htobe32(pcm_size + sizeof (aiff) - 8);
/* 'COMM' chunk */
hdr.num_channels = htobe16(num_channels);
hdr.num_sample_frames = htobe32(data->num_pcm_samples);
uint32_h_to_ieee754_extended_be(hdr.sample_rate, sample_rate);
aiff.num_channels = htobe16(num_channels);
aiff.num_sample_frames = htobe32(num_sample_frames);
uint32_h_to_ieee754_extended_be(aiff.sample_rate, sample_rate);
/* 'SSND' chunk */
hdr.ssnd_size = htobe32(data_size + 8);
aiff.ssnd_size = htobe32(pcm_size + 8);
if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) ||
ci->close(data->rec_file) != 0)
{
return false;
}
if (ci->enc_stream_lseek(0, SEEK_SET) != 0)
return -3;
data->rec_file = -1;
if (ci->enc_stream_write(&aiff, sizeof (aiff)) != sizeof (aiff))
return -4;
return true;
} /* on_end_file */
static void enc_events_callback(enum enc_events event, void *data)
ICODE_ATTR;
static void enc_events_callback(enum enc_events event, void *data)
{
switch (event)
{
case ENC_WRITE_CHUNK:
if (on_write_chunk((struct enc_file_event_data *)data))
return;
break;
case ENC_START_FILE:
if (on_start_file((struct enc_file_event_data *)data))
return;
break;
case ENC_END_FILE:
if (on_end_file((struct enc_file_event_data *)data))
return;
break;
default:
return;
}
/* Something failed above. Signal error back to core. */
((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
} /* enc_events_callback */
/* convert native pcm samples to aiff format samples */
static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
{
int32_t lr1, lr2;
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 = (int16_t)lr1;
lr2 = *(*src)++;
lr2 = (int16_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;
}
*(*dst)++ = htobe32((lr1 << 16) | (uint16_t)lr2);
} /* sample_to_mono */
static void chunk_to_aiff_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
static void chunk_to_aiff_format(uint32_t *src, uint32_t *dst)
{
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
{
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);
}
while (src < src_end);
}
else
{
#ifdef ROCKBOX_BIG_ENDIAN
/* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
*/
ci->memcpy(dst, src, PCM_CHUNK_SIZE);
#else
/* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR| =>
* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr|
*/
uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
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);
#endif
}
} /* chunk_to_aiff_format */
static bool init_encoder(void)
{
struct enc_inputs inputs;
struct enc_parameters params;
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;
ci->enc_get_inputs(&inputs);
if (inputs.config->afmt != AFMT_AIFF)
return false;
sample_rate = inputs.sample_rate;
num_channels = inputs.num_channels;
rec_mono_mode = inputs.rec_mono_mode;
err = 0;
/* configure the buffer system */
params.afmt = AFMT_AIFF;
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(&params);
return true;
} /* init_encoder */
return 0;
}
/* this is the codec entry point */
enum codec_status codec_main(enum codec_entry_call_reason reason)
{
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);
}
return CODEC_OK;
(void)reason;
}
/* this is called for each file to process */
enum codec_status codec_run(void)
enum codec_status ICODE_ATTR codec_run(void)
{
enum { GETBUF_ENC, GETBUF_PCM } getbuf = GETBUF_ENC;
struct enc_chunk_data *data = NULL;
/* main encoding loop */
while (ci->get_command(NULL) != CODEC_ACTION_HALT)
while (1)
{
uint32_t *src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE);
struct enc_chunk_hdr *chunk;
enum codec_command_action action = ci->get_command(NULL);
if (src == NULL)
continue;
if (action != CODEC_ACTION_NULL)
break;
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);
/* First obtain output buffer; when available, get PCM data */
switch (getbuf)
{
case GETBUF_ENC:
if (!(data = ci->enc_encbuf_get_buffer(frame_size)))
continue;
getbuf = GETBUF_PCM;
case GETBUF_PCM:
if (!ci->enc_pcmbuf_read(data->data, PCM_SAMP_PER_CHUNK))
continue;
getbuf = GETBUF_ENC;
}
chunk_to_aiff_format(src, (uint32_t *)chunk->enc_data);
data->hdr.size = frame_size;
data->pcm_count = PCM_SAMP_PER_CHUNK;
ci->enc_finish_chunk();
frame_htobe((uint32_t *)data->data, frame_size);
ci->enc_pcmbuf_advance(PCM_SAMP_PER_CHUNK);
ci->enc_encbuf_finish_buffer();
}
return CODEC_OK;
}
/* this is called by recording system */
int ICODE_ATTR enc_callback(enum enc_callback_reason reason,
void *params)
{
if (LIKELY(reason == ENC_CB_STREAM))
{
switch (((union enc_chunk_hdr *)params)->type)
{
case CHUNK_T_DATA:
return on_stream_data(params);
case CHUNK_T_STREAM_START:
return on_stream_start();
case CHUNK_T_STREAM_END:
return on_stream_end(params);
}
}
else if (reason == ENC_CB_INPUTS)
{
struct enc_inputs *inputs = params;
sample_rate = inputs->sample_rate;
num_channels = inputs->num_channels;
frame_size = PCM_SAMP_PER_CHUNK*PCM_DEPTH_BYTES*num_channels;
}
return 0;
}

View file

@ -36,7 +36,7 @@
#endif
#if (CONFIG_CODEC == SWCODEC)
#ifdef HAVE_RECORDING
#include "pcm_record.h"
#include "enc_base.h"
#endif
#include "dsp_core.h"
#include "dsp_misc.h"
@ -72,12 +72,12 @@
#define CODEC_ENC_MAGIC 0x52454E43 /* RENC */
/* increase this every time the api struct changes */
#define CODEC_API_VERSION 45
#define CODEC_API_VERSION 46
/* update this to latest version if a change to the api struct breaks
backwards compatibility (and please take the opportunity to sort in any
new function which are "waiting" at the end of the function table) */
#define CODEC_MIN_API_VERSION 45
#define CODEC_MIN_API_VERSION 46
/* reasons for calling codec main entrypoint */
enum codec_entry_call_reason {
@ -96,6 +96,9 @@ enum codec_command_action {
CODEC_ACTION_HALT = -1,
CODEC_ACTION_NULL = 0,
CODEC_ACTION_SEEK_TIME = 1,
#ifdef HAVE_RECORDING
CODEC_ACTION_STREAM_FINISH = 2,
#endif
};
/* NOTE: To support backwards compatibility, only add new functions at
@ -200,24 +203,18 @@ struct codec_api {
#endif
#ifdef HAVE_RECORDING
void (*enc_get_inputs)(struct enc_inputs *inputs);
void (*enc_set_parameters)(struct enc_parameters *params);
struct enc_chunk_hdr * (*enc_get_chunk)(void);
void (*enc_finish_chunk)(void);
unsigned char * (*enc_get_pcm_data)(size_t size);
size_t (*enc_unget_pcm_data)(size_t size);
/* file */
int (*open)(const char* pathname, int flags, ...);
int (*close)(int fd);
ssize_t (*read)(int fd, void* buf, size_t count);
off_t (*lseek)(int fd, off_t offset, int whence);
ssize_t (*write)(int fd, const void* buf, size_t count);
int (*enc_pcmbuf_read)(void *buf, int count);
int (*enc_pcmbuf_advance)(int count);
struct enc_chunk_data * (*enc_encbuf_get_buffer)(size_t need);
void (*enc_encbuf_finish_buffer)(void);
ssize_t (*enc_stream_read)(void *buf, size_t count);
off_t (*enc_stream_lseek)(off_t offset, int whence);
ssize_t (*enc_stream_write)(const void *buf, size_t count);
int (*round_value_to_list32)(unsigned long value,
const unsigned long list[],
int count,
bool signd);
#endif
#endif /* HAVE_RECORDING */
/* new stuff at the end, sort into place next time
the API gets incompatible */
@ -229,6 +226,7 @@ struct codec_header {
enum codec_status(*entry_point)(enum codec_entry_call_reason reason);
enum codec_status(*run_proc)(void);
struct codec_api **api;
void * rec_extension[]; /* extension for encoders */
};
#ifdef CODEC
@ -249,7 +247,7 @@ extern unsigned char plugin_end_addr[];
__attribute__ ((section (".header")))= { \
{ CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, \
plugin_start_addr, plugin_end_addr }, codec_start, \
codec_run, &ci };
codec_run, &ci, { enc_callback } };
#else /* def SIMULATOR */
/* decoders */
@ -262,7 +260,7 @@ extern unsigned char plugin_end_addr[];
#define CODEC_ENC_HEADER \
const struct codec_header __header = { \
{ CODEC_ENC_MAGIC, TARGET_ID, CODEC_API_VERSION, NULL, NULL }, \
codec_start, codec_run, &ci };
codec_start, codec_run, &ci, { enc_callback } };
#endif /* SIMULATOR */
#endif /* CODEC */
@ -277,12 +275,19 @@ void *codec_get_buffer_callback(size_t *size);
int codec_load_buf(int hid, struct codec_api *api);
int codec_load_file(const char* codec, struct codec_api *api);
int codec_run_proc(void);
int codec_halt(void);
int codec_close(void);
#if CONFIG_CODEC == SWCODEC && defined(HAVE_RECORDING)
enc_callback_t codec_get_enc_callback(void);
#else
#define codec_get_enc_callback() NULL
#endif
/* defined by the codec */
enum codec_status codec_start(enum codec_entry_call_reason reason);
enum codec_status codec_main(enum codec_entry_call_reason reason);
enum codec_status codec_run(void);
#if CONFIG_CODEC == SWCODEC && defined(HAVE_RECORDING)
int enc_callback(enum enc_callback_reason reason, void *params);
#endif
#endif /* _CODECS_H_ */

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,7 @@
* $Id$
*
* Copyright (C) 2006 Antonius Hellmann
* Copyright (C) 2006-2013 Michael Sevakis
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -40,12 +41,12 @@ struct riff_header
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; */
/* uint16_t extra_param_size; 24h - size of extra data */
/* uint8_t extra_params[extra_param_size]; */
/* 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 */
/* uint8_t data[data_size]; 2Ch - actual sound data */
} __attribute__((packed));
#define RIFF_FMT_HEADER_SIZE 12 /* format -> format_size */
@ -55,19 +56,17 @@ struct riff_header
#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)
static int num_channels IBSS_ATTR;
static int rec_mono_mode IBSS_ATTR;
static int num_channels;
static uint32_t sample_rate;
static uint32_t enc_size;
static int32_t err IBSS_ATTR;
static size_t frame_size;
static size_t data_size;
static const struct riff_header riff_header =
static const struct riff_header riff_template_header =
{
/* "RIFF" header */
{ 'R', 'I', 'F', 'F' }, /* riff_id */
0, /* riff_size (*) */
0, /* riff_size (*) */
/* format header */
{ 'W', 'A', 'V', 'E' }, /* format */
{ 'f', 'm', 't', ' ' }, /* format_id */
@ -82,305 +81,164 @@ static const struct riff_header riff_header =
/* data header */
{ 'd', 'a', 't', 'a' }, /* data_id */
0 /* data_size (*) */
/* (*) updated during ENC_END_FILE event */
/* (*) updated when finalizing stream */
};
/* 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)
static inline void frame_htole(uint32_t *p, size_t size)
{
return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
} /* is_file_data_ok */
/* 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;
if (data->chunk->enc_data == NULL)
#ifdef ROCKBOX_BIG_ENDIAN
/* Byte-swap samples, stereo or mono */
do
{
#ifdef ROCKBOX_HAS_LOGF
ci->logf("wav enc: NULL data");
#endif
return true;
uint32_t t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
t = swap_odd_even32(*p); *p++ = t;
}
while (size -= 8 * 2 * PCM_DEPTH_BYTES);
#endif /* ROCKBOX_BIG_ENDIAN */
(void)p; (void)size;
}
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)
static int on_stream_data(struct enc_chunk_data *data)
{
if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
return false;
size_t size = data->hdr.size;
data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
if (ci->enc_stream_write(data->data, size) != (ssize_t)size)
return -1;
if (data->rec_file < 0)
return false;
data_size += size;
return 0;
}
static int on_stream_start(void)
{
/* reset sample count */
data->num_pcm_samples = 0;
data_size = 0;
/* write template header */
if (ci->write(data->rec_file, &riff_header, sizeof (riff_header))
!= sizeof (riff_header))
{
return false;
}
if (ci->enc_stream_write(&riff_template_header, sizeof (struct riff_header))
!= sizeof (struct riff_header))
return -1;
data->new_enc_size += sizeof (riff_header);
return true;
} /* on_start_file */
return 0;
}
static bool on_end_file(struct enc_file_event_data *data)
static int on_stream_end(union enc_chunk_hdr *hdr)
{
/* update template header */
struct riff_header hdr;
uint32_t data_size;
struct riff_header riff;
if (data->rec_file < 0)
return false; /* file already closed, nothing more we can do */
/* 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)))
if (hdr->err)
{
return false;
/* Called for stream error; get correct data size */
ssize_t size = ci->enc_stream_lseek(0, SEEK_END);
if (size > (ssize_t)sizeof (riff))
data_size = size - sizeof (riff);
}
data_size = data->num_pcm_samples*num_channels*PCM_DEPTH_BYTES;
if (ci->enc_stream_lseek(0, SEEK_SET) ||
ci->enc_stream_read(&riff, sizeof (riff)) != sizeof (riff))
return -1;
/* "RIFF" header */
hdr.riff_size = htole32(RIFF_FMT_HEADER_SIZE + RIFF_FMT_DATA_SIZE
+ RIFF_DATA_HEADER_SIZE + data_size);
riff.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);
riff.num_channels = htole16(num_channels);
riff.sample_rate = htole32(sample_rate);
riff.byte_rate = htole32(sample_rate*num_channels*PCM_DEPTH_BYTES);
riff.block_align = htole16(num_channels*PCM_DEPTH_BYTES);
/* data header */
hdr.data_size = htole32(data_size);
riff.data_size = htole32(data_size);
if (ci->lseek(data->rec_file, 0, SEEK_SET) != 0 ||
ci->write(data->rec_file, &hdr, sizeof (hdr)) != sizeof (hdr) ||
ci->close(data->rec_file) != 0)
{
return false;
}
if (ci->enc_stream_lseek(0, SEEK_SET) != 0)
return -2;
data->rec_file = -1;
if (ci->enc_stream_write(&riff, sizeof (riff)) != sizeof (riff))
return -3;
return true;
} /* on_end_file */
static void enc_events_callback(enum enc_events event, void *data)
ICODE_ATTR;
static void enc_events_callback(enum enc_events event, void *data)
{
switch (event)
{
case ENC_WRITE_CHUNK:
if (on_write_chunk((struct enc_file_event_data *)data))
return;
break;
case ENC_START_FILE:
if (on_start_file((struct enc_file_event_data *)data))
return;
break;
case ENC_END_FILE:
if (on_end_file((struct enc_file_event_data *)data))
return;
break;
default:
return;
}
/* Something failed above. Signal error back to core. */
((struct enc_file_event_data *)data)->chunk->flags |= CHUNKF_ERROR;
} /* enc_events_callback */
/* convert native pcm samples to wav format samples */
static inline void sample_to_mono(uint32_t **src, uint32_t **dst)
{
int32_t lr1, lr2;
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;
}
*(*dst)++ = htole32((lr2 << 16) | (uint16_t)lr1);
} /* sample_to_mono */
static void chunk_to_wav_format(uint32_t *src, uint32_t *dst) ICODE_ATTR;
static void chunk_to_wav_format(uint32_t *src, uint32_t *dst)
{
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
{
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);
}
while (src < src_end);
}
else
{
#ifdef ROCKBOX_BIG_ENDIAN
/* |LLLLLLLLllllllll|RRRRRRRRrrrrrrrr| =>
* |llllllllLLLLLLLL|rrrrrrrrRRRRRRRR|
*/
uint32_t *src_end = src + PCM_SAMP_PER_CHUNK;
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)
{
struct enc_inputs inputs;
struct enc_parameters params;
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;
ci->enc_get_inputs(&inputs);
if (inputs.config->afmt != AFMT_PCM_WAV)
return false;
sample_rate = inputs.sample_rate;
num_channels = inputs.num_channels;
rec_mono_mode = inputs.rec_mono_mode;
err = 0;
/* configure the buffer system */
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(&params);
return true;
} /* init_encoder */
return 0;
}
/* this is the codec entry point */
enum codec_status codec_main(enum codec_entry_call_reason reason)
{
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);
}
return CODEC_OK;
(void)reason;
}
/* this is called for each file to process */
enum codec_status codec_run(void)
enum codec_status ICODE_ATTR codec_run(void)
{
enum { GETBUF_ENC, GETBUF_PCM } getbuf = GETBUF_ENC;
struct enc_chunk_data *data = NULL;
/* main encoding loop */
while(ci->get_command(NULL) != CODEC_ACTION_HALT)
while (1)
{
uint32_t *src = (uint32_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE);
struct enc_chunk_hdr *chunk;
enum codec_command_action action = ci->get_command(NULL);
if(src == NULL)
continue;
if (action != CODEC_ACTION_NULL)
break;
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);
/* First obtain output buffer; when available, get PCM data */
switch (getbuf)
{
case GETBUF_ENC:
if (!(data = ci->enc_encbuf_get_buffer(frame_size)))
continue;
getbuf = GETBUF_PCM;
case GETBUF_PCM:
if (!ci->enc_pcmbuf_read(data->data, PCM_SAMP_PER_CHUNK))
continue;
getbuf = GETBUF_ENC;
}
chunk_to_wav_format(src, (uint32_t *)chunk->enc_data);
data->hdr.size = frame_size;
data->pcm_count = PCM_SAMP_PER_CHUNK;
ci->enc_finish_chunk();
frame_htole((uint32_t *)data->data, frame_size);
ci->enc_pcmbuf_advance(PCM_SAMP_PER_CHUNK);
ci->enc_encbuf_finish_buffer();
}
return CODEC_OK;
}
/* this is called by recording system */
int ICODE_ATTR enc_callback(enum enc_callback_reason reason,
void *params)
{
if (LIKELY(reason == ENC_CB_STREAM))
{
switch (((union enc_chunk_hdr *)params)->type)
{
case CHUNK_T_DATA:
return on_stream_data(params);
case CHUNK_T_STREAM_START:
return on_stream_start();
case CHUNK_T_STREAM_END:
return on_stream_end(params);
}
}
else if (reason == ENC_CB_INPUTS)
{
struct enc_inputs *inputs = params;
sample_rate = inputs->sample_rate;
num_channels = inputs->num_channels;
frame_size = PCM_SAMP_PER_CHUNK*PCM_DEPTH_BYTES*num_channels;
}
return 0;
}

View file

@ -8,6 +8,7 @@
* $Id$
*
* Copyright (C) 2006 Antonius Hellmann
* Copyright (C) 2006-2013 Michael Sevakis
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -47,29 +48,39 @@ struct riff_header
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; */
/* uint16_t extra_param_size; 24h - size of extra data */
/* uint8_t extra_params[extra_param_size]; */
/* 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 */
/* uint8_t data[data_size]; 2Ch - actual sound data */
} __attribute__((packed));
#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 */
struct wvpk_chunk_data
{
struct enc_chunk_data ckhdr; /* The base data chunk header */
WavpackHeader wphdr; /* The block wavpack info */
uint8_t data[]; /* Encoded audio data */
};
#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 int32_t input_buffer[PCM_SAMP_PER_CHUNK*2] IBSS_ATTR;
static WavpackConfig config IBSS_ATTR;
static WavpackContext *wpc;
static int32_t data_size, input_size, input_step IBSS_ATTR;
static int32_t err IBSS_ATTR;
static uint32_t sample_rate;
static int num_channels;
static uint32_t total_samples;
static size_t out_reqsize;
static size_t frame_size;
static const WavpackMetadataHeader wvpk_mdh =
{
@ -77,7 +88,7 @@ static const WavpackMetadataHeader wvpk_mdh =
sizeof (struct riff_header) / sizeof (uint16_t),
};
static const struct riff_header riff_header =
static const struct riff_header riff_template_header =
{
/* "RIFF" header */
{ 'R', 'I', 'F', 'F' }, /* riff_id */
@ -96,157 +107,75 @@ static const struct riff_header riff_header =
/* data header */
{ 'd', 'a', 't', 'a' }, /* data_id */
0 /* data_size (*) */
/* (*) updated during ENC_END_FILE event */
/* (*) updated when finalizing stream */
};
static inline void sample_to_int32_mono(int32_t **src, int32_t **dst)
static inline void sample_to_int32(int32_t **dst, int32_t **src)
{
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)++;
uint32_t t = *(*src)++;
#ifdef ROCKBOX_BIG_ENDIAN
*(*dst)++ = t >> 16, *(*dst)++ = (int16_t)t;
*(*dst)++ = (int32_t)t >> 16;
*(*dst)++ = (int16_t)t;
#else
*(*dst)++ = (int16_t)t, *(*dst)++ = t >> 16;
*(*dst)++ = (int16_t)t;
*(*dst)++ = (int32_t)t >> 16;
#endif
} /* sample_to_int32_stereo */
}
static void chunk_to_int32(int32_t *src) ICODE_ATTR;
static void chunk_to_int32(int32_t *src)
static void ICODE_ATTR input_buffer_to_int32(size_t size)
{
int32_t *src_end, *dst;
#ifdef USE_IRAM
/* copy to IRAM before converting data */
dst = (int32_t *)input_buffer + PCM_SAMP_PER_CHUNK;
src_end = dst + PCM_SAMP_PER_CHUNK;
int32_t *dst = input_buffer;
int32_t *src = input_buffer + PCM_SAMP_PER_CHUNK;
memcpy(dst, src, PCM_CHUNK_SIZE);
src = dst;
#else
src_end = src + PCM_SAMP_PER_CHUNK;
#endif
dst = (int32_t *)input_buffer;
if (config.num_channels == 1)
do
{
/*
* |llllllllllllllll|rrrrrrrrrrrrrrrr| =>
* |mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm|
*/
do
{
/* read 10 longs and write 10 longs */
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);
}
while(src < src_end);
return;
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
sample_to_int32(&dst, &src);
}
else
{
/*
* |llllllllllllllll|rrrrrrrrrrrrrrrr| =>
* |llllllllllllllllllllllllllllllll|rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr|
*/
do
{
/* read 10 longs and write 20 longs */
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);
}
while (src < src_end);
while (size -= 10 * 2 * PCM_DEPTH_BYTES);
}
return;
}
} /* chunk_to_int32 */
/* 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)
static int on_stream_data(struct wvpk_chunk_data *wpdata)
{
return data->rec_file >= 0 && (long)data->chunk->flags >= 0;
} /* is_file_data_ok */
/* 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);
wpdata->wphdr.block_index = htole32(total_samples);
if (ci->write(data->rec_file, data->chunk->enc_data,
data->chunk->enc_size) != (ssize_t)data->chunk->enc_size)
return false;
size_t size = wpdata->ckhdr.hdr.size;
if (ci->enc_stream_write(wpdata->ckhdr.data, size) != (ssize_t)size)
return -1;
data->num_pcm_samples += data->chunk->num_pcm;
return true;
} /* on_write_chunk */
total_samples += wpdata->ckhdr.pcm_count;
static bool on_start_file(struct enc_file_event_data *data)
return 0;
}
static int on_stream_start(void)
{
if ((data->chunk->flags & CHUNKF_ERROR) || *data->filename == '\0')
return false;
data->rec_file = ci->open(data->filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
if (data->rec_file < 0)
return false;
/* reset sample count */
data->num_pcm_samples = 0;
total_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;
}
if (ci->enc_stream_write(&wvpk_mdh, sizeof (wvpk_mdh))
!= sizeof (wvpk_mdh))
return -1;
data->new_enc_size += sizeof(wvpk_mdh) + sizeof(riff_header);
return true;
} /* on_start_file */
if (ci->enc_stream_write(&riff_template_header,
sizeof (riff_template_header))
!= sizeof (riff_template_header))
return -2;
static bool on_end_file(struct enc_file_event_data *data)
return 0;
}
static int on_stream_end(void)
{
struct
{
@ -255,19 +184,16 @@ static bool on_end_file(struct enc_file_event_data *data)
WavpackHeader wph;
} __attribute__ ((packed)) h;
uint32_t data_size;
if (data->rec_file < 0)
return false; /* file already closed, nothing more we can do */
/* always _try_ to write the file header, even on error */
/* Correcting sizes on error is a bit of a pain */
/* 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;
if (ci->enc_stream_lseek(0, SEEK_SET) != 0)
return -1;
data_size = data->num_pcm_samples*config.num_channels*PCM_DEPTH_BYTES;
if (ci->enc_stream_read(&h, sizeof (h)) != sizeof (h))
return -2;
size_t data_size = total_samples*config.num_channels*PCM_DEPTH_BYTES;
/** "RIFF" header **/
h.rhdr.riff_size = htole32(RIFF_FMT_HEADER_SIZE +
@ -286,121 +212,29 @@ static bool on_end_file(struct enc_file_event_data *data)
/** 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);
h.wph.total_samples = htole32(total_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))
!= sizeof (h.rhdr) ||
ci->close(data->rec_file) != 0 )
{
return false;
}
if (ci->enc_stream_lseek(0, SEEK_SET) != 0)
return -3;
data->rec_file = -1;
if (ci->enc_stream_write(&h.wph, sizeof (h.wph)) != sizeof (h.wph))
return -4;
return true;
} /* on_end_file */
if (ci->enc_stream_write(&h.wpmdh, sizeof (h.wpmdh)) != sizeof (h.wpmdh))
return -5;
static void enc_events_callback(enum enc_events event, void *data)
ICODE_ATTR;
static void enc_events_callback(enum enc_events event, void *data)
{
switch (event)
{
case ENC_WRITE_CHUNK:
if (on_write_chunk((struct enc_file_event_data *)data))
return;
if (ci->enc_stream_write(&h.rhdr, sizeof (h.rhdr)) != sizeof (h.rhdr))
return -6;
break;
case ENC_START_FILE:
/* write metadata header and RIFF header */
if (on_start_file((struct enc_file_event_data *)data))
return;
break;
case ENC_END_FILE:
if (on_end_file((struct enc_file_event_data *)data))
return;
break;
default:
return;
}
/* Something failed above. Signal error back to core. */
((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;
codec_init();
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 ||
ci->enc_unget_pcm_data == NULL )
return false;
ci->enc_get_inputs(&inputs);
if (inputs.config->afmt != AFMT_WAVPACK)
return false;
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;
err = 0;
/* configure the buffer system */
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;
ci->enc_set_parameters(&params);
return true;
} /* init_encoder */
return 0;
}
/* this is the codec entry point */
enum codec_status codec_main(enum codec_entry_call_reason reason)
{
if (reason == CODEC_LOAD) {
/* initialize params and config */
if (!init_encoder())
return CODEC_ERROR;
}
else if (reason == CODEC_UNLOAD) {
/* reset parameters to initial state */
ci->enc_set_parameters(NULL);
}
if (reason == CODEC_LOAD)
codec_init();
return CODEC_OK;
}
@ -408,60 +242,89 @@ enum codec_status codec_main(enum codec_entry_call_reason reason)
/* this is called for each file to process */
enum codec_status codec_run(void)
{
enum { GETBUF_ENC, GETBUF_PCM } getbuf = GETBUF_ENC;
struct enc_chunk_data *data = NULL;
/* main encoding loop */
while(ci->get_command(NULL) != CODEC_ACTION_HALT)
while (1)
{
uint8_t *src = (uint8_t *)ci->enc_get_pcm_data(PCM_CHUNK_SIZE);
struct enc_chunk_hdr *chunk;
bool abort_chunk;
uint8_t *dst;
uint8_t *src_end;
enum codec_command_action action = ci->get_command(NULL);
if(src == NULL)
continue;
if (action != CODEC_ACTION_NULL)
break;
chunk = ci->enc_get_chunk();
/* reset counts and pointer */
chunk->enc_size = 0;
chunk->num_pcm = 0;
chunk->enc_data = NULL;
dst = ENC_CHUNK_SKIP_HDR(dst, chunk);
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
/* First obtain output buffer; when available, get PCM data */
switch (getbuf)
{
abort_chunk = true;
if (WavpackPackSamples(wpc, (int32_t *)src,
PCM_SAMP_PER_CHUNK/4))
{
chunk->num_pcm += PCM_SAMP_PER_CHUNK/4;
ci->yield();
/* could've been stopped in some way */
abort_chunk = chunk->flags & CHUNKF_ABORT;
}
src += input_step;
case GETBUF_ENC:
if (!(data = ci->enc_encbuf_get_buffer(out_reqsize)))
continue;
getbuf = GETBUF_PCM;
case GETBUF_PCM:
if (!ci->enc_pcmbuf_read(input_buffer + PCM_SAMP_PER_CHUNK,
PCM_SAMP_PER_CHUNK))
continue;
getbuf = GETBUF_ENC;
}
while (!abort_chunk && src < src_end);
if (!abort_chunk)
input_buffer_to_int32(frame_size);
if (WavpackStartBlock(wpc, data->data, data->data + out_reqsize) &&
WavpackPackSamples(wpc, input_buffer, PCM_SAMP_PER_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);
/* finish the chunk and store chunk size info */
chunk->enc_size = WavpackFinishBlock(wpc);
ci->enc_finish_chunk();
data->hdr.size = WavpackFinishBlock(wpc);
data->pcm_count = PCM_SAMP_PER_CHUNK;
}
else
{
data->hdr.err = 1;
}
ci->enc_pcmbuf_advance(PCM_SAMP_PER_CHUNK);
ci->enc_encbuf_finish_buffer();
}
return CODEC_OK;
}
/* this is called by recording system */
int ICODE_ATTR enc_callback(enum enc_callback_reason reason,
void *params)
{
if (LIKELY(reason == ENC_CB_STREAM))
{
switch (((union enc_chunk_hdr *)params)->type)
{
case CHUNK_T_DATA:
return on_stream_data(params);
case CHUNK_T_STREAM_START:
return on_stream_start();
case CHUNK_T_STREAM_END:
return on_stream_end();
}
}
else if (reason == ENC_CB_INPUTS)
{
/* Save parameters */
struct enc_inputs *inputs = params;
sample_rate = inputs->sample_rate;
num_channels = inputs->num_channels;
frame_size = PCM_SAMP_PER_CHUNK*PCM_DEPTH_BYTES*num_channels;
out_reqsize = frame_size*110 / 100; /* Add 10% */
/* Setup Wavpack encoder */
memset(&config, 0, sizeof (config));
config.bits_per_sample = PCM_DEPTH_BITS;
config.bytes_per_sample = PCM_DEPTH_BYTES;
config.sample_rate = sample_rate;
config.num_channels = num_channels;
wpc = WavpackOpenFileOutput();
if (!WavpackSetConfiguration(wpc, &config, -1))
return -1;
}
return 0;
}