summaryrefslogtreecommitdiffstats
path: root/audio
diff options
context:
space:
mode:
authorStefano Pigozzi <stefano.pigozzi@gmail.com>2013-05-23 21:23:32 +0200
committerStefano Pigozzi <stefano.pigozzi@gmail.com>2013-06-16 18:20:39 +0200
commitbff03a181f8c4102a75144f818ea15ea53165170 (patch)
treee3c34701fdc88782cde8772baadcece5ad62b12a /audio
parentb537467fd33d55b743bbf8fcac562d3ade7cb7c3 (diff)
downloadmpv-bff03a181f8c4102a75144f818ea15ea53165170.tar.bz2
mpv-bff03a181f8c4102a75144f818ea15ea53165170.tar.xz
core: add a spsc ringbuffer implementation
Currently every single AO was implementing it's own ringbuffer, many times with slightly different semantics. This is an attempt to fix the problem. I stole some good ideas from ao_portaudio's ringbuffer and went from there. The main difference is this one stores wpos and rpos which are absolute positions in an "infinite" buffer. To find the actual position for writing / reading just apply modulo size. The producer only modifies wpos while the consumer only modifies rpos. This makes it pretty easy to reason about and make the operations thread safe by using barriers (thread safety is guaranteed only in the Single-Producer/Single- Consumer case). Also adapted ao_coreaudio to use this ringbuffer.
Diffstat (limited to 'audio')
-rw-r--r--audio/format.c10
-rw-r--r--audio/format.h1
-rw-r--r--audio/out/ao_coreaudio.c43
-rw-r--r--audio/out/ao_coreaudio/ca_ringbuffer.c106
-rw-r--r--audio/out/ao_coreaudio/ca_ringbuffer_internal.h37
5 files changed, 33 insertions, 164 deletions
diff --git a/audio/format.c b/audio/format.c
index 5b1262956c..f9bfcb5ba8 100644
--- a/audio/format.c
+++ b/audio/format.c
@@ -110,6 +110,16 @@ static bool af_fmt_valid(int format)
return (format & AF_FORMAT_MASK) == format;
}
+int af_fmt_seconds_to_bytes(int format, float seconds, int channels)
+{
+ int bps = (af_fmt2bits(format) / 8);
+ int framelen = channels * bps;
+ int bytes = seconds * bps;
+ if (bytes % framelen)
+ bytes += framelen - (bytes % framelen);
+ return bytes;
+}
+
int af_str2fmt_short(bstr str)
{
if (bstr_startswith0(str, "0x")) {
diff --git a/audio/format.h b/audio/format.h
index 30a4aa1cea..bbc7f0fa69 100644
--- a/audio/format.h
+++ b/audio/format.h
@@ -132,6 +132,7 @@ extern const struct af_fmt_entry af_fmtstr_table[];
int af_str2fmt_short(bstr str);
int af_fmt2bits(int format);
char* af_fmt2str(int format, char* str, int size);
+int af_fmt_seconds_to_bytes(int format, float seconds, int channels);
const char* af_fmt2str_short(int format);
#endif /* MPLAYER_AF_FORMAT_H */
diff --git a/audio/out/ao_coreaudio.c b/audio/out/ao_coreaudio.c
index 930bb2d741..d3491f17f8 100644
--- a/audio/out/ao_coreaudio.c
+++ b/audio/out/ao_coreaudio.c
@@ -49,8 +49,7 @@
#include "audio/format.h"
#include "osdep/timer.h"
#include "core/subopt-helper.h"
-
-#include "ao_coreaudio/ca_ringbuffer_internal.h"
+#include "core/mp_ring.h"
#define ca_msg(a, b, c ...) mp_msg(a, b, "AO: [coreaudio] " c)
@@ -58,10 +57,10 @@ static void audio_pause(struct ao *ao);
static void audio_resume(struct ao *ao);
static void reset(struct ao *ao);
-static void print_buffer(struct ca_ringbuffer *buffer)
+static void print_buffer(struct mp_ring *buffer)
{
void *tctx = talloc_new(NULL);
- ca_msg(MSGT_AO, MSGL_V, "%s\n", ca_ringbuffer_repr(buffer, tctx));
+ ca_msg(MSGT_AO, MSGL_V, "%s\n", mp_ring_repr(buffer, tctx));
talloc_free(tctx);
}
@@ -92,7 +91,7 @@ struct priv
int packetSize;
int paused;
- struct ca_ringbuffer *buffer;
+ struct mp_ring *buffer;
};
static OSStatus theRenderProc(void *inRefCon,
@@ -104,14 +103,14 @@ static OSStatus theRenderProc(void *inRefCon,
struct ao *ao = inRefCon;
struct priv *p = ao->priv;
- int buffered = ca_ringbuffer_buffered(p->buffer);
+ int buffered = mp_ring_buffered(p->buffer);
int requested = inNumFrames * p->packetSize;
if (buffered > requested)
buffered = requested;
if (buffered) {
- ca_ringbuffer_read(p->buffer,
+ mp_ring_read(p->buffer,
(unsigned char *)ioData->mBuffers[0].mData,
buffered);
} else {
@@ -622,8 +621,9 @@ static int init(struct ao *ao, char *params)
ao->bps = ao->samplerate * inDesc.mBytesPerFrame;
ao->buffersize = ao->bps;
- p->buffer = ca_ringbuffer_new2(p, ao->bps, maxFrames);
- ao->outburst = ca_ringbuffer_chunk_size(p->buffer);
+ int bufbytes = af_fmt_seconds_to_bytes(ao->format, 0.5, ao->channels.num);
+ p->buffer = mp_ring_new(p, bufbytes);
+ ao->outburst = maxFrames;
print_buffer(p->buffer);
@@ -859,8 +859,9 @@ static int OpenSPDIF(struct ao *ao)
/* For ac3/dts, just use packet size 6144 bytes as chunk size. */
int chunk_size = p->stream_format.mBytesPerPacket;
ao->buffersize = ao->bps;
- p->buffer = ca_ringbuffer_new2(p, ao->bps, chunk_size);
- ao->outburst = ca_ringbuffer_chunk_size(p->buffer);
+ int bufbytes = af_fmt_seconds_to_bytes(ao->format, 0.5, ao->channels.num);
+ p->buffer = mp_ring_new(p, bufbytes);
+ ao->outburst = chunk_size;
print_buffer(p->buffer);
@@ -1071,7 +1072,7 @@ static OSStatus RenderCallbackSPDIF(AudioDeviceID inDevice,
{
struct ao *ao = threadGlobals;
struct priv *p = ao->priv;
- int amt = ca_ringbuffer_buffered(p->buffer);
+ int amt = mp_ring_buffered(p->buffer);
AudioBuffer ca_buffer = outOutputData->mBuffers[p->i_stream_index];
int req = ca_buffer.mDataByteSize;
@@ -1079,9 +1080,9 @@ static OSStatus RenderCallbackSPDIF(AudioDeviceID inDevice,
amt = req;
if (amt) {
if (p->b_muted) {
- ca_ringbuffer_read(p->buffer, NULL, amt);
+ mp_ring_read(p->buffer, NULL, amt);
} else {
- ca_ringbuffer_read(p->buffer, (unsigned char *)ca_buffer.mData, amt);
+ mp_ring_read(p->buffer, (unsigned char *)ca_buffer.mData, amt);
}
}
@@ -1116,7 +1117,7 @@ static int play(struct ao *ao, void *output_samples, int num_bytes, int flags)
"Detected current stream does not support digital.\n");
}
- wrote = ca_ringbuffer_write(p->buffer, output_samples, num_bytes);
+ wrote = mp_ring_write(p->buffer, output_samples, num_bytes);
audio_resume(ao);
return wrote;
@@ -1127,7 +1128,7 @@ static void reset(struct ao *ao)
{
struct priv *p = ao->priv;
audio_pause(ao);
- ca_ringbuffer_reset(p->buffer);
+ mp_ring_reset(p->buffer);
}
@@ -1135,7 +1136,7 @@ static void reset(struct ao *ao)
static int get_space(struct ao *ao)
{
struct priv *p = ao->priv;
- return ca_ringbuffer_available(p->buffer);
+ return mp_ring_available(p->buffer);
}
@@ -1144,7 +1145,7 @@ static float get_delay(struct ao *ao)
{
// inaccurate, should also contain the data buffered e.g. by the OS
struct priv *p = ao->priv;
- return ca_ringbuffer_buffered(p->buffer) / (float)ao->bps;
+ return mp_ring_buffered(p->buffer) / (float)ao->bps;
}
static void uninit(struct ao *ao, bool immed)
@@ -1154,9 +1155,9 @@ static void uninit(struct ao *ao, bool immed)
if (!immed) {
long long timeleft =
- (1000000LL * ca_ringbuffer_buffered(p->buffer)) / ao->bps;
- ca_msg(MSGT_AO, MSGL_DBG2, "%d bytes left @%d bps (%d usec)\n", ca_ringbuffer_buffered(
- p->buffer), ao->bps, (int)timeleft);
+ (1000000LL * mp_ring_buffered(p->buffer)) / ao->bps;
+ ca_msg(MSGT_AO, MSGL_DBG2, "%d bytes left @%d bps (%d usec)\n",
+ mp_ring_buffered(p->buffer), ao->bps, (int)timeleft);
mp_sleep_us((int)timeleft);
}
diff --git a/audio/out/ao_coreaudio/ca_ringbuffer.c b/audio/out/ao_coreaudio/ca_ringbuffer.c
deleted file mode 100644
index 717466945d..0000000000
--- a/audio/out/ao_coreaudio/ca_ringbuffer.c
+++ /dev/null
@@ -1,106 +0,0 @@
-/*
- * This file is part of mpv.
- *
- * mpv is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * mpv is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with mpv. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#include <libavutil/fifo.h>
-#include "talloc.h"
-
-#include "ca_ringbuffer_internal.h"
-
-struct ca_ringbuffer {
- AVFifoBuffer *fifo;
- int len;
- int chunks;
- int chunk_size;
-};
-
-struct ca_ringbuffer *ca_ringbuffer_new(void *talloc_ctx, int chunks, int chunk_size)
-{
- struct ca_ringbuffer *buffer =
- talloc_zero(talloc_ctx, struct ca_ringbuffer);
-
- *buffer = (struct ca_ringbuffer) {
- .fifo = av_fifo_alloc(chunks * chunk_size),
- .len = chunks * chunk_size,
- .chunks = chunks,
- .chunk_size = chunk_size,
- };
-
- return buffer;
-}
-
-struct ca_ringbuffer *ca_ringbuffer_new2(void *talloc_ctx, int bps, int chunk_size)
-{
- int chunks = (bps + chunk_size - 1) / chunk_size;
- return ca_ringbuffer_new(talloc_ctx, chunks, chunk_size);
-}
-
-int ca_ringbuffer_read(struct ca_ringbuffer *buffer,
- unsigned char *data, int len)
-{
- int buffered = ca_ringbuffer_buffered(buffer);
- if (len > buffered)
- len = buffered;
- if (data)
- av_fifo_generic_read(buffer->fifo, data, len, NULL);
- else
- av_fifo_drain(buffer->fifo, len);
- return len;
-}
-
-int ca_ringbuffer_write(struct ca_ringbuffer *buffer,
- unsigned char *data, int len)
-{
- int free = buffer->len - av_fifo_size(buffer->fifo);
- if (len > free)
- len = free;
- return av_fifo_generic_write(buffer->fifo, data, len, NULL);
-}
-
-void ca_ringbuffer_reset(struct ca_ringbuffer *buffer)
-{
- av_fifo_reset(buffer->fifo);
-}
-
-int ca_ringbuffer_available(struct ca_ringbuffer *buffer)
-{
- return ca_ringbuffer_size(buffer) - ca_ringbuffer_buffered(buffer);
-}
-
-int ca_ringbuffer_size(struct ca_ringbuffer *buffer)
-{
- return buffer->len;
-}
-
-int ca_ringbuffer_buffered(struct ca_ringbuffer *buffer)
-{
- return av_fifo_size(buffer->fifo);
-}
-
-int ca_ringbuffer_chunk_size(struct ca_ringbuffer *buffer)
-{
- return buffer->chunk_size;
-}
-
-char *ca_ringbuffer_repr(struct ca_ringbuffer *buffer, void *talloc_ctx)
-{
- return talloc_asprintf(
- talloc_ctx,
- "Ringbuffer { .chunks = %d bytes, .chunk_size = %d bytes, .size = %d bytes }",
- buffer->chunks,
- ca_ringbuffer_chunk_size(buffer),
- ca_ringbuffer_size(buffer));
-}
diff --git a/audio/out/ao_coreaudio/ca_ringbuffer_internal.h b/audio/out/ao_coreaudio/ca_ringbuffer_internal.h
deleted file mode 100644
index 50b41871b9..0000000000
--- a/audio/out/ao_coreaudio/ca_ringbuffer_internal.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * This file is part of mpv.
- *
- * mpv is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * mpv is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with mpv. If not, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef MPV_AUDIO_OUT_CA_RINGBUFFER_H
-#define MPV_AUDIO_OUT_CA_RINGBUFFER_H
-
-struct ca_ringbuffer;
-
-struct ca_ringbuffer *ca_ringbuffer_new(void *talloc_ctx, int chunks, int chunk_size);
-struct ca_ringbuffer *ca_ringbuffer_new2(void *talloc_ctx, int bps, int chunk_size);
-int ca_ringbuffer_read(struct ca_ringbuffer *buffer, unsigned char *data, int len);
-int ca_ringbuffer_write(struct ca_ringbuffer *buffer, unsigned char *data, int len);
-
-void ca_ringbuffer_reset(struct ca_ringbuffer *buffer);
-
-int ca_ringbuffer_available(struct ca_ringbuffer *buffer);
-int ca_ringbuffer_size(struct ca_ringbuffer *buffer);
-int ca_ringbuffer_buffered(struct ca_ringbuffer *buffer);
-int ca_ringbuffer_chunk_size(struct ca_ringbuffer *buffer);
-
-char *ca_ringbuffer_repr(struct ca_ringbuffer *buffer, void *talloc_ctx);
-
-#endif