[PATCH] libavformat: add S/PDIF demuxer
Anssi Hannula
anssi.hannula
Fri Jul 23 04:52:03 CEST 2010
---
doc/general.texi | 2 +
libavformat/Makefile | 1 +
libavformat/allformats.c | 2 +-
libavformat/spdif.c | 196 +++++++++++++++++++++++++++++++++++++++++++++-
4 files changed, 199 insertions(+), 2 deletions(-)
diff --git a/doc/general.texi b/doc/general.texi
index 76c676f..eefbdb5 100644
--- a/doc/general.texi
+++ b/doc/general.texi
@@ -228,6 +228,8 @@ library:
@item Sony PlayStation STR @tab @tab X
@item Sony Wave64 (W64) @tab @tab X
@item SoX native format @tab X @tab X
+ at item S/PDIF / IEC958 (IEC-61937) @tab X @tab X
+ @tab Encoded data encapsulated in IEC-61937 as used in S/PDIF.
@item SUN AU format @tab X @tab X
@item Text files @tab @tab X
@item THP @tab @tab X
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 50a7b3a..5526d36 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -241,6 +241,7 @@ OBJS-$(CONFIG_SMACKER_DEMUXER) += smacker.o
OBJS-$(CONFIG_SOL_DEMUXER) += sol.o raw.o
OBJS-$(CONFIG_SOX_DEMUXER) += soxdec.o raw.o
OBJS-$(CONFIG_SOX_MUXER) += soxenc.o
+OBJS-$(CONFIG_SPDIF_DEMUXER) += spdif.o
OBJS-$(CONFIG_SPDIF_MUXER) += spdif.o
OBJS-$(CONFIG_STR_DEMUXER) += psxstr.o
OBJS-$(CONFIG_SWF_DEMUXER) += swfdec.o
diff --git a/libavformat/allformats.c b/libavformat/allformats.c
index ce0e90d..d7c657e 100644
--- a/libavformat/allformats.c
+++ b/libavformat/allformats.c
@@ -188,7 +188,7 @@ void av_register_all(void)
REGISTER_DEMUXER (SMACKER, smacker);
REGISTER_DEMUXER (SOL, sol);
REGISTER_MUXDEMUX (SOX, sox);
- REGISTER_MUXER (SPDIF, spdif);
+ REGISTER_MUXDEMUX (SPDIF, spdif);
REGISTER_DEMUXER (STR, str);
REGISTER_MUXDEMUX (SWF, swf);
REGISTER_MUXER (TG2, tg2);
diff --git a/libavformat/spdif.c b/libavformat/spdif.c
index ac84c50..5bc692a 100644
--- a/libavformat/spdif.c
+++ b/libavformat/spdif.c
@@ -1,6 +1,7 @@
/*
- * IEC958 muxer
+ * IEC958 muxer and demuxer
* Copyright (c) 2009 Bartlomiej Wolowiec
+ * Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
*
* This file is part of FFmpeg.
*
@@ -23,6 +24,7 @@
* @file
* IEC-61937 encapsulation of various formats, used by S/PDIF
* @author Bartlomiej Wolowiec
+ * @author Anssi Hannula
*/
/*
@@ -103,6 +105,7 @@ static void bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
dst[i + 0] = av_bswap16(src[i + 0]);
}
+#if CONFIG_SPDIF_MUXER
static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
{
IEC958Context *ctx = s->priv_data;
@@ -158,6 +161,7 @@ static const enum IEC958DataType mpeg_data_type[2][3] = {
{ IEC958_MPEG2_LAYER1_LSF, IEC958_MPEG2_LAYER2_LSF, IEC958_MPEG2_LAYER3_LSF }, //MPEG2 LSF
{ IEC958_MPEG1_LAYER1, IEC958_MPEG1_LAYER23, IEC958_MPEG1_LAYER23 }, //MPEG1
};
+#endif
static const uint16_t mpeg_pkt_offset[2][3] = {
//LAYER1 LAYER2 LAYER3
@@ -165,6 +169,7 @@ static const uint16_t mpeg_pkt_offset[2][3] = {
{ 1536, 4608, 4608 }, // MPEG1
};
+#if CONFIG_SPDIF_MUXER
static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
{
IEC958Context *ctx = s->priv_data;
@@ -311,3 +316,192 @@ AVOutputFormat spdif_muxer = {
spdif_write_packet,
spdif_write_trailer,
};
+#endif
+
+#if CONFIG_SPDIF_DEMUXER
+
+/* Largest offset between bursts we currently handle, i.e. AAC with
+ aac_hdr.samples = 4096 */
+#define SPDIF_MAX_OFFSET 16384
+
+static int spdif_probe(AVProbeData *p)
+{
+ const uint8_t *buf = p->buf;
+ const uint8_t *probe_end = p->buf + FFMIN(64, p->buf_size) - 1;
+ const uint8_t *prev_sync = NULL;
+ uint32_t state = 0;
+ int sync_codes = 0;
+
+ for (; buf < probe_end; buf++) {
+ state = (state << 8) | *buf;
+
+ if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
+ if (buf == p->buf + 7)
+ return AVPROBE_SCORE_MAX; /* sync word at the very beginning */
+ if (++sync_codes >= 4)
+ return AVPROBE_SCORE_MAX; /* 4 sync codes found */
+
+ prev_sync = buf;
+ if (sync_codes == 1)
+ /* continue probing to find more sync codes */
+ probe_end = p->buf + p->buf_size;
+ continue;
+ }
+ if (prev_sync && buf > prev_sync + SPDIF_MAX_OFFSET)
+ return 1; /* at least one sync code found, but there is a gap */
+ }
+
+ if (sync_codes) {
+ /* sync codes were found, but buffer ended before 4 sync codes were
+ encountered; we still return relatively high score so that
+ (ac3-in-)spdif-in-wav gets probed as spdif as chained demuxers are
+ not yet supported */
+ return AVPROBE_SCORE_MAX - 1;
+ }
+ return 0;
+}
+
+static int spdif_read_header(AVFormatContext *s, AVFormatParameters *ap)
+{
+ s->ctx_flags |= AVFMTCTX_NOHEADER;
+ return 0;
+}
+
+static int spdif_get_offset_and_codec(enum IEC958DataType data_type,
+ AVPacket *pkt, int *offset,
+ enum CodecID *codec,
+ AVFormatContext *s)
+{
+ AACADTSHeaderInfo aac_hdr;
+ GetBitContext gbc;
+
+ switch (data_type & 0xff) {
+ case IEC958_AC3:
+ *offset = AC3_FRAME_SIZE << 2;
+ *codec = CODEC_ID_AC3;
+ break;
+ case IEC958_MPEG1_LAYER1:
+ *offset = mpeg_pkt_offset[1][0];
+ *codec = CODEC_ID_MP1;
+ break;
+ case IEC958_MPEG1_LAYER23:
+ *offset = mpeg_pkt_offset[1][0];
+ *codec = CODEC_ID_MP3;
+ break;
+ case IEC958_MPEG2_EXT:
+ *offset = 4608;
+ *codec = CODEC_ID_MP3;
+ break;
+ case IEC958_MPEG2_AAC:
+ init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
+ if (ff_aac_parse_header(&gbc, &aac_hdr)) {
+ av_log(s, AV_LOG_ERROR, "Invalid AAC packet in S/PDIF\n");
+ return AVERROR(EINVAL);
+ }
+ *offset = aac_hdr.samples << 2;
+ *codec = CODEC_ID_AAC;
+ break;
+ case IEC958_MPEG2_LAYER1_LSF:
+ *offset = mpeg_pkt_offset[0][0];
+ *codec = CODEC_ID_MP1;
+ break;
+ case IEC958_MPEG2_LAYER2_LSF:
+ *offset = mpeg_pkt_offset[0][1];
+ *codec = CODEC_ID_MP2;
+ break;
+ case IEC958_MPEG2_LAYER3_LSF:
+ *offset = mpeg_pkt_offset[0][2];
+ *codec = CODEC_ID_MP3;
+ break;
+ case IEC958_DTS1:
+ *offset = 2048;
+ *codec = CODEC_ID_DTS;
+ break;
+ case IEC958_DTS2:
+ *offset = 4096;
+ *codec = CODEC_ID_DTS;
+ break;
+ case IEC958_DTS3:
+ *offset = 8192;
+ *codec = CODEC_ID_DTS;
+ break;
+ default:
+ av_log(s, AV_LOG_WARNING, "Data type 0x%04x", data_type);
+ av_log_missing_feature(s, " in S/PDIF is", 1);
+ return AVERROR(ENOSYS);
+ }
+ return 0;
+}
+
+static int spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+ ByteIOContext *pb = s->pb;
+ enum IEC958DataType data_type;
+ enum CodecID codec_id;
+ uint32_t state = 0;
+ int pkt_size_bits, offset, ret;
+
+ while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
+ state = (state << 8) | get_byte(pb);
+ if (url_feof(pb))
+ return AVERROR_EOF;
+ }
+
+ data_type = get_le16(pb);
+ pkt_size_bits = get_le16(pb);
+
+ if (pkt_size_bits % 16)
+ av_log_ask_for_sample(s, "Packet does not end to a 16-bit boundary.");
+
+ ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
+ if (ret)
+ return ret;
+
+ pkt->pos = url_ftell(pb) - BURST_HEADER_SIZE;
+
+ if (get_buffer(pb, pkt->data, pkt->size) < pkt->size) {
+ av_free_packet(pkt);
+ return AVERROR(EIO);
+ }
+ bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
+
+ ret = spdif_get_offset_and_codec(data_type, pkt, &offset, &codec_id, s);
+ if (ret) {
+ av_free_packet(pkt);
+ return ret;
+ }
+
+ /* skip over the padding to the beginning of the next frame */
+ url_fskip(pb, offset - pkt->size - BURST_HEADER_SIZE);
+
+ if (!s->nb_streams) {
+ /* first packet, create a stream */
+ AVStream *st = av_new_stream(s, 0);
+ if (!st) {
+ av_free_packet(pkt);
+ return AVERROR(ENOMEM);
+ }
+ st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
+ st->codec->codec_id = codec_id;
+ } else if (codec_id != s->streams[0]->codec->codec_id) {
+ av_log_ask_for_sample(s, "Codec changed in S/PDIF stream.");
+ return AVERROR(ENOSYS);
+ }
+
+ if (!s->bit_rate && s->streams[0]->codec->sample_rate)
+ /* stream bitrate matches 16-bit stereo PCM bitrate */
+ s->bit_rate = 2 * 16 * s->streams[0]->codec->sample_rate;
+
+ return 0;
+}
+
+AVInputFormat spdif_demuxer = {
+ "spdif",
+ NULL_IF_CONFIG_SMALL("IEC958 - S/PDIF (IEC-61937)"),
+ 0,
+ spdif_probe,
+ spdif_read_header,
+ spdif_read_packet,
+ .flags = AVFMT_GENERIC_INDEX,
+};
+#endif
--
1.7.1
--Boundary-00=_lHSSM85JTyJ0J23--
More information about the ffmpeg-devel
mailing list