From cee5036aca85f37a28f2d52a2fc5ad19878ad511 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 28 Nov 2011 08:00:10 +0100 Subject: encoder/flac: use fifo_buffer instead of pcm_buffer This fixes a buffer corruption bug; pcm_buffer is not designed to be a persistent buffers, and will discard anything between two consecutive calls. --- src/encoder/flac_encoder.c | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'src/encoder') diff --git a/src/encoder/flac_encoder.c b/src/encoder/flac_encoder.c index c34faad00..6389513ef 100644 --- a/src/encoder/flac_encoder.c +++ b/src/encoder/flac_encoder.c @@ -22,6 +22,8 @@ #include "encoder_plugin.h" #include "audio_format.h" #include "pcm_buffer.h" +#include "fifo_buffer.h" +#include "growing_fifo.h" #include #include @@ -38,8 +40,11 @@ struct flac_encoder { struct pcm_buffer expand_buffer; - struct pcm_buffer buffer; - size_t buffer_length; + /** + * This buffer will hold encoded data from libFLAC until it is + * picked up with flac_encoder_read(). + */ + struct fifo_buffer *output_buffer; }; extern const struct encoder_plugin flac_encoder_plugin; @@ -140,11 +145,8 @@ flac_write_callback(G_GNUC_UNUSED const FLAC__StreamEncoder *fse, { struct flac_encoder *encoder = (struct flac_encoder *) client_data; - char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length + bytes); - //transfer data to buffer - memcpy( buffer + encoder->buffer_length, data, bytes); - encoder->buffer_length += bytes; + growing_fifo_append(&encoder->output_buffer, data, bytes); return FLAC__STREAM_ENCODER_WRITE_STATUS_OK; } @@ -156,8 +158,8 @@ flac_encoder_close(struct encoder *_encoder) FLAC__stream_encoder_delete(encoder->fse); - pcm_buffer_deinit(&encoder->buffer); pcm_buffer_deinit(&encoder->expand_buffer); + fifo_buffer_free(encoder->output_buffer); } static bool @@ -201,10 +203,10 @@ flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, return false; } - encoder->buffer_length = 0; - pcm_buffer_init(&encoder->buffer); pcm_buffer_init(&encoder->expand_buffer); + encoder->output_buffer = growing_fifo_new(); + /* this immediatelly outputs data throught callback */ #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7 @@ -325,16 +327,18 @@ static size_t flac_encoder_read(struct encoder *_encoder, void *dest, size_t length) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; - char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length); - - if (length > encoder->buffer_length) - length = encoder->buffer_length; - memcpy(dest, buffer, length); + size_t max_length; + const char *src = fifo_buffer_read(encoder->output_buffer, + &max_length); + if (src == NULL) + return 0; - encoder->buffer_length -= length; - memmove(buffer, buffer + length, encoder->buffer_length); + if (length > max_length) + length = max_length; + memcpy(dest, src, length); + fifo_buffer_consume(encoder->output_buffer, length); return length; } -- cgit v1.2.3 From 399a3abefc00f913945189fdb3a646759f082054 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 28 Nov 2011 08:09:49 +0100 Subject: encoder/wave: use fifo_buffer instead of pcm_buffer This fixes a buffer corruption bug; pcm_buffer is not designed to be a persistent buffers, and will discard anything between two consecutive calls. --- src/encoder/wave_encoder.c | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to 'src/encoder') diff --git a/src/encoder/wave_encoder.c b/src/encoder/wave_encoder.c index 938be5e5e..4e830bd14 100644 --- a/src/encoder/wave_encoder.c +++ b/src/encoder/wave_encoder.c @@ -20,7 +20,8 @@ #include "config.h" #include "encoder_api.h" #include "encoder_plugin.h" -#include "pcm_buffer.h" +#include "fifo_buffer.h" +#include "growing_fifo.h" #include #include @@ -29,8 +30,7 @@ struct wave_encoder { struct encoder encoder; unsigned bits; - struct pcm_buffer buffer; - size_t buffer_length; + struct fifo_buffer *buffer; }; struct wave_header { @@ -92,7 +92,6 @@ wave_encoder_init(G_GNUC_UNUSED const struct config_param *param, encoder = g_new(struct wave_encoder, 1); encoder_struct_init(&encoder->encoder, &wave_encoder_plugin); - pcm_buffer_init(&encoder->buffer); return &encoder->encoder; } @@ -102,7 +101,6 @@ wave_encoder_finish(struct encoder *_encoder) { struct wave_encoder *encoder = (struct wave_encoder *)_encoder; - pcm_buffer_deinit(&encoder->buffer); g_free(encoder); } @@ -112,7 +110,6 @@ wave_encoder_open(struct encoder *_encoder, G_GNUC_UNUSED GError **error) { struct wave_encoder *encoder = (struct wave_encoder *)_encoder; - void *buffer; assert(audio_format_valid(audio_format)); @@ -139,19 +136,29 @@ wave_encoder_open(struct encoder *_encoder, break; } - buffer = pcm_buffer_get(&encoder->buffer, sizeof(struct wave_header) ); + encoder->buffer = growing_fifo_new(); + struct wave_header *header = + growing_fifo_write(&encoder->buffer, sizeof(*header)); /* create PCM wave header in initial buffer */ - fill_wave_header((struct wave_header *) buffer, + fill_wave_header(header, audio_format->channels, encoder->bits, audio_format->sample_rate, (encoder->bits / 8) * audio_format->channels ); + fifo_buffer_append(encoder->buffer, sizeof(*header)); - encoder->buffer_length = sizeof(struct wave_header); return true; } +static void +wave_encoder_close(struct encoder *_encoder) +{ + struct wave_encoder *encoder = (struct wave_encoder *)_encoder; + + fifo_buffer_free(encoder->buffer); +} + static inline size_t pcm16_to_wave(uint16_t *dst16, const uint16_t *src16, size_t length) { @@ -198,9 +205,8 @@ wave_encoder_write(struct encoder *_encoder, G_GNUC_UNUSED GError **error) { struct wave_encoder *encoder = (struct wave_encoder *)_encoder; - void *dst; - dst = pcm_buffer_get(&encoder->buffer, encoder->buffer_length + length); + void *dst = growing_fifo_write(&encoder->buffer, length); #if (G_BYTE_ORDER == G_LITTLE_ENDIAN) switch (encoder->bits) { @@ -232,7 +238,7 @@ wave_encoder_write(struct encoder *_encoder, #error G_BYTE_ORDER set to G_PDP_ENDIAN is not supported by wave_encoder #endif - encoder->buffer_length += length; + fifo_buffer_append(encoder->buffer, length); return true; } @@ -240,16 +246,17 @@ static size_t wave_encoder_read(struct encoder *_encoder, void *dest, size_t length) { struct wave_encoder *encoder = (struct wave_encoder *)_encoder; - uint8_t *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length ); - - if (length > encoder->buffer_length) - length = encoder->buffer_length; - memcpy(dest, buffer, length); + size_t max_length; + const void *src = fifo_buffer_read(encoder->buffer, &max_length); + if (src == NULL) + return 0; - encoder->buffer_length -= length; - memmove(buffer, buffer + length, encoder->buffer_length); + if (length > max_length) + length = max_length; + memcpy(dest, src, length); + fifo_buffer_consume(encoder->buffer, length); return length; } @@ -264,6 +271,7 @@ const struct encoder_plugin wave_encoder_plugin = { .init = wave_encoder_init, .finish = wave_encoder_finish, .open = wave_encoder_open, + .close = wave_encoder_close, .write = wave_encoder_write, .read = wave_encoder_read, .get_mime_type = wave_encoder_get_mime_type, -- cgit v1.2.3 From 74beefcaf6a2a82785649752bf346e5c8fb9ec62 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 28 Nov 2011 08:27:59 +0100 Subject: encoder/null: use fifo_buffer instead of pcm_buffer This fixes a buffer corruption bug; pcm_buffer is not designed to be a persistent buffers, and will discard anything between two consecutive calls. --- src/encoder/null_encoder.c | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'src/encoder') diff --git a/src/encoder/null_encoder.c b/src/encoder/null_encoder.c index bf7e61c3b..4e45f4345 100644 --- a/src/encoder/null_encoder.c +++ b/src/encoder/null_encoder.c @@ -20,7 +20,8 @@ #include "config.h" #include "encoder_api.h" #include "encoder_plugin.h" -#include "pcm_buffer.h" +#include "fifo_buffer.h" +#include "growing_fifo.h" #include #include @@ -28,8 +29,7 @@ struct null_encoder { struct encoder encoder; - struct pcm_buffer buffer; - size_t buffer_length; + struct fifo_buffer *buffer; }; extern const struct encoder_plugin null_encoder_plugin; @@ -65,7 +65,7 @@ null_encoder_close(struct encoder *_encoder) { struct null_encoder *encoder = (struct null_encoder *)_encoder; - pcm_buffer_deinit(&encoder->buffer); + fifo_buffer_free(encoder->buffer); } @@ -76,9 +76,7 @@ null_encoder_open(struct encoder *_encoder, { struct null_encoder *encoder = (struct null_encoder *)_encoder; - encoder->buffer_length = 0; - pcm_buffer_init(&encoder->buffer); - + encoder->buffer = growing_fifo_new(); return true; } @@ -88,28 +86,26 @@ null_encoder_write(struct encoder *_encoder, G_GNUC_UNUSED GError **error) { struct null_encoder *encoder = (struct null_encoder *)_encoder; - char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length + length); - - memcpy(buffer+encoder->buffer_length, data, length); - encoder->buffer_length += length; - return true; + growing_fifo_append(&encoder->buffer, data, length); + return length; } static size_t null_encoder_read(struct encoder *_encoder, void *dest, size_t length) { struct null_encoder *encoder = (struct null_encoder *)_encoder; - char *buffer = pcm_buffer_get(&encoder->buffer, encoder->buffer_length); - - if (length > encoder->buffer_length) - length = encoder->buffer_length; - memcpy(dest, buffer, length); + size_t max_length; + const void *src = fifo_buffer_read(encoder->buffer, &max_length); + if (src == NULL) + return 0; - encoder->buffer_length -= length; - memmove(buffer, buffer + length, encoder->buffer_length); + if (length > max_length) + length = max_length; + memcpy(dest, src, length); + fifo_buffer_consume(encoder->buffer, length); return length; } -- cgit v1.2.3 From 0a218ee56ae37bcebeb716d5faa18f91e6e75556 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 28 Nov 2011 09:25:42 +0100 Subject: encoder/wave: support packed 24 bit samples Convert to padded 24 bit samples, instead of falling back to 16 bit. --- src/encoder/wave_encoder.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/encoder') diff --git a/src/encoder/wave_encoder.c b/src/encoder/wave_encoder.c index 4e830bd14..3a64c5197 100644 --- a/src/encoder/wave_encoder.c +++ b/src/encoder/wave_encoder.c @@ -122,6 +122,11 @@ wave_encoder_open(struct encoder *_encoder, encoder->bits = 16; break; + case SAMPLE_FORMAT_S24: + audio_format->format = SAMPLE_FORMAT_S24_P32; + encoder->bits = 24; + break; + case SAMPLE_FORMAT_S24_P32: encoder->bits = 24; break; -- cgit v1.2.3