aboutsummaryrefslogtreecommitdiffstats
path: root/src/encoder/flac_encoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoder/flac_encoder.c')
-rw-r--r--src/encoder/flac_encoder.c36
1 files changed, 20 insertions, 16 deletions
diff --git a/src/encoder/flac_encoder.c b/src/encoder/flac_encoder.c
index 8b315d710..9f60bdae5 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 <assert.h>
#include <string.h>
@@ -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 immediately outputs data through 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;
}