summaryrefslogtreecommitdiffstats
path: root/audio/decode/ad_spdif.c
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2013-11-12 22:27:44 +0100
committerwm4 <wm4@nowhere>2013-11-12 23:39:09 +0100
commit22b3f522cacfbdba76d311c86efd6091512eb089 (patch)
tree1105af44a9403bde554cd4b6041d05ceea4fb39a /audio/decode/ad_spdif.c
parent5388a0cd4062ba24f5382f025552422fb6430906 (diff)
downloadmpv-22b3f522cacfbdba76d311c86efd6091512eb089.tar.bz2
mpv-22b3f522cacfbdba76d311c86efd6091512eb089.tar.xz
audio: add support for using non-interleaved audio from decoders directly
Most libavcodec decoders output non-interleaved audio. Add direct support for this, and remove the hack that repacked non-interleaved audio back to packed audio. Remove the minlen argument from the decoder callback. Instead of forcing every decoder to have its own decode loop to fill the buffer until minlen is reached, leave this to the caller. So if a decoder doesn't return enough data, it's simply called again. (In future, I even want to change it so that decoders don't read packets directly, but instead the caller has to pass packets to the decoders. This fits well with this change, because now the decoder callback typically decodes at most one packet.) ad_mpg123.c receives some heavy refactoring. The main problem is that it wanted to handle format changes when there was no data in the decode output buffer yet. This sounds reasonable, but actually it would write data into a buffer prepared for old data, since the caller doesn't know about the format change yet. (I.e. the best place for a format change would be _after_ writing the last sample to the output buffer.) It's possible that this code was not perfectly sane before this commit, and perhaps lost one frame of data after a format change, but I didn't confirm this. Trying to fix this, I ended up rewriting the decoding and also the probing.
Diffstat (limited to 'audio/decode/ad_spdif.c')
-rw-r--r--audio/decode/ad_spdif.c55
1 files changed, 31 insertions, 24 deletions
diff --git a/audio/decode/ad_spdif.c b/audio/decode/ad_spdif.c
index f03041d6a6..a233286c19 100644
--- a/audio/decode/ad_spdif.c
+++ b/audio/decode/ad_spdif.c
@@ -19,6 +19,7 @@
*/
#include <string.h>
+#include <assert.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
@@ -184,37 +185,43 @@ fail:
return 0;
}
-static int decode_audio(sh_audio_t *sh, unsigned char *buf,
- int minlen, int maxlen)
+static int decode_audio(sh_audio_t *sh, struct mp_audio *buffer, int maxlen)
{
struct spdifContext *spdif_ctx = sh->context;
AVFormatContext *lavf_ctx = spdif_ctx->lavf_ctx;
+ int sstride = 2 * sh->channels.num;
+ assert(sstride == buffer->sstride);
+
+ if (maxlen < spdif_ctx->iec61937_packet_size)
+ return 0;
+
spdif_ctx->out_buffer_len = 0;
spdif_ctx->out_buffer_size = maxlen;
- spdif_ctx->out_buffer = buf;
- while (spdif_ctx->out_buffer_len + spdif_ctx->iec61937_packet_size < maxlen
- && spdif_ctx->out_buffer_len < minlen) {
- struct demux_packet *mpkt = demux_read_packet(sh->gsh);
- if (!mpkt)
- break;
- AVPacket pkt;
- mp_set_av_packet(&pkt, mpkt);
- pkt.pts = pkt.dts = 0;
- mp_msg(MSGT_DECAUDIO, MSGL_V, "spdif packet, size=%d\n", pkt.size);
- if (mpkt->pts != MP_NOPTS_VALUE) {
- sh->pts = mpkt->pts;
- sh->pts_bytes = 0;
- }
- int out_len = spdif_ctx->out_buffer_len;
- int ret = av_write_frame(lavf_ctx, &pkt);
- avio_flush(lavf_ctx->pb);
- sh->pts_bytes += spdif_ctx->out_buffer_len - out_len;
- talloc_free(mpkt);
- if (ret < 0)
- break;
+ spdif_ctx->out_buffer = buffer->planes[0];
+
+ struct demux_packet *mpkt = demux_read_packet(sh->gsh);
+ if (!mpkt)
+ return 0;
+
+ AVPacket pkt;
+ mp_set_av_packet(&pkt, mpkt);
+ pkt.pts = pkt.dts = 0;
+ mp_msg(MSGT_DECAUDIO, MSGL_V, "spdif packet, size=%d\n", pkt.size);
+ if (mpkt->pts != MP_NOPTS_VALUE) {
+ sh->pts = mpkt->pts;
+ sh->pts_offset = 0;
}
- return spdif_ctx->out_buffer_len;
+ int out_len = spdif_ctx->out_buffer_len;
+ int ret = av_write_frame(lavf_ctx, &pkt);
+ avio_flush(lavf_ctx->pb);
+ sh->pts_offset += (spdif_ctx->out_buffer_len - out_len) / sstride;
+ talloc_free(mpkt);
+ if (ret < 0)
+ return -1;
+
+ buffer->samples = spdif_ctx->out_buffer_len / sstride;
+ return 0;
}
static int control(sh_audio_t *sh, int cmd, void *arg)