2010-11-22 22:09:26 +00:00
|
|
|
/*
|
|
|
|
* IEC 61937 demuxer
|
|
|
|
* Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* IEC 61937 demuxer, used for compressed data in S/PDIF
|
|
|
|
* @author Anssi Hannula
|
|
|
|
*/
|
|
|
|
|
2017-04-26 16:57:54 +00:00
|
|
|
#include "libavutil/bswap.h"
|
|
|
|
|
|
|
|
#include "libavcodec/ac3.h"
|
|
|
|
#include "libavcodec/adts_parser.h"
|
|
|
|
|
2010-11-22 22:09:26 +00:00
|
|
|
#include "avformat.h"
|
|
|
|
#include "spdif.h"
|
|
|
|
|
|
|
|
static int spdif_get_offset_and_codec(AVFormatContext *s,
|
2010-12-29 18:48:32 +00:00
|
|
|
enum IEC61937DataType data_type,
|
2010-11-22 22:09:26 +00:00
|
|
|
const char *buf, int *offset,
|
2012-08-05 09:11:04 +00:00
|
|
|
enum AVCodecID *codec)
|
2010-11-22 22:09:26 +00:00
|
|
|
{
|
2017-04-26 16:57:54 +00:00
|
|
|
uint32_t samples;
|
|
|
|
uint8_t frames;
|
|
|
|
int ret;
|
2010-11-22 22:09:26 +00:00
|
|
|
|
|
|
|
switch (data_type & 0xff) {
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_AC3:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = AC3_FRAME_SIZE << 2;
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_AC3;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_MPEG1_LAYER1:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = spdif_mpeg_pkt_offset[1][0];
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_MP1;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_MPEG1_LAYER23:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = spdif_mpeg_pkt_offset[1][0];
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_MP3;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_MPEG2_EXT:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = 4608;
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_MP3;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_MPEG2_AAC:
|
2017-04-26 16:57:54 +00:00
|
|
|
ret = av_adts_header_parse(buf, &samples, &frames);
|
|
|
|
if (ret < 0) {
|
2010-11-22 22:09:26 +00:00
|
|
|
if (s) /* be silent during a probe */
|
|
|
|
av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
|
2017-04-26 16:57:54 +00:00
|
|
|
return ret;
|
2010-11-22 22:09:26 +00:00
|
|
|
}
|
2017-04-26 16:57:54 +00:00
|
|
|
*offset = samples << 2;
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_AAC;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_MPEG2_LAYER1_LSF:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = spdif_mpeg_pkt_offset[0][0];
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_MP1;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_MPEG2_LAYER2_LSF:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = spdif_mpeg_pkt_offset[0][1];
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_MP2;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_MPEG2_LAYER3_LSF:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = spdif_mpeg_pkt_offset[0][2];
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_MP3;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_DTS1:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = 2048;
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_DTS;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_DTS2:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = 4096;
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_DTS;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
2010-12-29 18:48:32 +00:00
|
|
|
case IEC61937_DTS3:
|
2010-11-22 22:09:26 +00:00
|
|
|
*offset = 8192;
|
2012-08-05 09:11:04 +00:00
|
|
|
*codec = AV_CODEC_ID_DTS;
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (s) { /* be silent during a probe */
|
2013-02-25 22:54:28 +00:00
|
|
|
avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
|
|
|
|
data_type);
|
2010-11-22 22:09:26 +00:00
|
|
|
}
|
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Largest offset between bursts we currently handle, i.e. AAC with
|
2017-04-26 16:57:54 +00:00
|
|
|
samples = 4096 */
|
2010-11-22 22:09:26 +00:00
|
|
|
#define SPDIF_MAX_OFFSET 16384
|
|
|
|
|
2019-03-21 00:18:37 +00:00
|
|
|
static int spdif_probe(const AVProbeData *p)
|
2010-11-22 22:09:26 +00:00
|
|
|
{
|
2013-01-06 17:48:51 +00:00
|
|
|
enum AVCodecID codec;
|
|
|
|
return ff_spdif_probe (p->buf, p->buf_size, &codec);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
|
|
|
|
{
|
|
|
|
const uint8_t *buf = p_buf;
|
|
|
|
const uint8_t *probe_end = p_buf + FFMIN(2 * SPDIF_MAX_OFFSET, buf_size - 1);
|
2010-11-22 22:09:26 +00:00
|
|
|
const uint8_t *expected_code = buf + 7;
|
|
|
|
uint32_t state = 0;
|
|
|
|
int sync_codes = 0;
|
|
|
|
int consecutive_codes = 0;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
for (; buf < probe_end; buf++) {
|
|
|
|
state = (state << 8) | *buf;
|
|
|
|
|
|
|
|
if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
|
|
|
|
&& buf[1] < 0x37) {
|
|
|
|
sync_codes++;
|
|
|
|
|
|
|
|
if (buf == expected_code) {
|
|
|
|
if (++consecutive_codes >= 2)
|
|
|
|
return AVPROBE_SCORE_MAX;
|
|
|
|
} else
|
|
|
|
consecutive_codes = 0;
|
|
|
|
|
2017-10-30 21:56:45 +00:00
|
|
|
if (buf + 4 + AV_AAC_ADTS_HEADER_SIZE > p_buf + buf_size)
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* continue probing to find more sync codes */
|
2013-01-06 17:48:51 +00:00
|
|
|
probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p_buf + buf_size - 1);
|
2010-11-22 22:09:26 +00:00
|
|
|
|
|
|
|
/* skip directly to the next sync code */
|
|
|
|
if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
|
2013-01-06 17:48:51 +00:00
|
|
|
&buf[5], &offset, codec)) {
|
|
|
|
if (buf + offset >= p_buf + buf_size)
|
2010-11-22 22:09:26 +00:00
|
|
|
break;
|
|
|
|
expected_code = buf + offset;
|
|
|
|
buf = expected_code - 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sync_codes)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (sync_codes >= 6)
|
|
|
|
/* good amount of sync codes but with unexpected offsets */
|
2013-03-25 15:12:51 +00:00
|
|
|
return AVPROBE_SCORE_EXTENSION;
|
2010-11-22 22:09:26 +00:00
|
|
|
|
|
|
|
/* some sync codes were found */
|
2013-03-25 15:12:51 +00:00
|
|
|
return AVPROBE_SCORE_EXTENSION / 4;
|
2010-11-22 22:09:26 +00:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:20:36 +00:00
|
|
|
static int spdif_read_header(AVFormatContext *s)
|
2010-11-22 22:09:26 +00:00
|
|
|
{
|
|
|
|
s->ctx_flags |= AVFMTCTX_NOHEADER;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-01-06 17:48:51 +00:00
|
|
|
int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
|
2010-11-22 22:09:26 +00:00
|
|
|
{
|
2011-02-20 10:04:12 +00:00
|
|
|
AVIOContext *pb = s->pb;
|
2010-12-29 18:48:32 +00:00
|
|
|
enum IEC61937DataType data_type;
|
2012-08-05 09:11:04 +00:00
|
|
|
enum AVCodecID codec_id;
|
2010-11-22 22:09:26 +00:00
|
|
|
uint32_t state = 0;
|
|
|
|
int pkt_size_bits, offset, ret;
|
|
|
|
|
|
|
|
while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
|
2011-02-21 15:43:01 +00:00
|
|
|
state = (state << 8) | avio_r8(pb);
|
2014-08-07 20:12:41 +00:00
|
|
|
if (avio_feof(pb))
|
2010-11-22 22:09:26 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
data_type = avio_rl16(pb);
|
|
|
|
pkt_size_bits = avio_rl16(pb);
|
2010-11-22 22:09:26 +00:00
|
|
|
|
|
|
|
if (pkt_size_bits % 16)
|
2013-02-25 22:54:28 +00:00
|
|
|
avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
|
2010-11-22 22:09:26 +00:00
|
|
|
|
|
|
|
ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2011-03-03 19:11:45 +00:00
|
|
|
pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
|
2010-11-22 22:09:26 +00:00
|
|
|
|
2011-02-21 15:43:01 +00:00
|
|
|
if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
|
2010-11-22 22:09:26 +00:00
|
|
|
return AVERROR_EOF;
|
|
|
|
}
|
|
|
|
ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
|
|
|
|
|
|
|
|
ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
|
|
|
|
&offset, &codec_id);
|
2020-01-07 13:55:40 +00:00
|
|
|
if (ret < 0) {
|
2010-11-22 22:09:26 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* skip over the padding to the beginning of the next frame */
|
2011-03-15 08:14:38 +00:00
|
|
|
avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
|
2010-11-22 22:09:26 +00:00
|
|
|
|
|
|
|
if (!s->nb_streams) {
|
|
|
|
/* first packet, create a stream */
|
2011-06-18 09:43:24 +00:00
|
|
|
AVStream *st = avformat_new_stream(s, NULL);
|
2010-11-22 22:09:26 +00:00
|
|
|
if (!st) {
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
|
|
|
|
st->codecpar->codec_id = codec_id;
|
|
|
|
} else if (codec_id != s->streams[0]->codecpar->codec_id) {
|
2013-02-25 22:53:52 +00:00
|
|
|
avpriv_report_missing_feature(s, "Codec change in IEC 61937");
|
2010-11-22 22:09:26 +00:00
|
|
|
return AVERROR_PATCHWELCOME;
|
|
|
|
}
|
|
|
|
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
if (!s->bit_rate && s->streams[0]->codecpar->sample_rate)
|
2010-11-22 22:09:26 +00:00
|
|
|
/* stream bitrate matches 16-bit stereo PCM bitrate for currently
|
|
|
|
supported codecs */
|
lavf: replace AVStream.codec with AVStream.codecpar
Currently, AVStream contains an embedded AVCodecContext instance, which
is used by demuxers to export stream parameters to the caller and by
muxers to receive stream parameters from the caller. It is also used
internally as the codec context that is passed to parsers.
In addition, it is also widely used by the callers as the decoding (when
demuxer) or encoding (when muxing) context, though this has been
officially discouraged since Libav 11.
There are multiple important problems with this approach:
- the fields in AVCodecContext are in general one of
* stream parameters
* codec options
* codec state
However, it's not clear which ones are which. It is consequently
unclear which fields are a demuxer allowed to set or a muxer allowed to
read. This leads to erratic behaviour depending on whether decoding or
encoding is being performed or not (and whether it uses the AVStream
embedded codec context).
- various synchronization issues arising from the fact that the same
context is used by several different APIs (muxers/demuxers,
parsers, bitstream filters and encoders/decoders) simultaneously, with
there being no clear rules for who can modify what and the different
processes being typically delayed with respect to each other.
- avformat_find_stream_info() making it necessary to support opening
and closing a single codec context multiple times, thus
complicating the semantics of freeing various allocated objects in the
codec context.
Those problems are resolved by replacing the AVStream embedded codec
context with a newly added AVCodecParameters instance, which stores only
the stream parameters exported by the demuxers or read by the muxers.
2014-06-18 18:42:52 +00:00
|
|
|
s->bit_rate = 2 * 16 * s->streams[0]->codecpar->sample_rate;
|
2010-11-22 22:09:26 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-04-19 17:45:24 +00:00
|
|
|
const AVInputFormat ff_spdif_demuxer = {
|
2011-07-16 20:18:12 +00:00
|
|
|
.name = "spdif",
|
|
|
|
.long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
|
|
|
|
.read_probe = spdif_probe,
|
|
|
|
.read_header = spdif_read_header,
|
2013-01-06 17:48:51 +00:00
|
|
|
.read_packet = ff_spdif_read_packet,
|
2012-04-06 14:50:48 +00:00
|
|
|
.flags = AVFMT_GENERIC_INDEX,
|
2010-11-22 22:09:26 +00:00
|
|
|
};
|