3b39e273 |
/*
* 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
*/
#include "avformat.h"
#include "spdif.h"
#include "libavcodec/ac3.h"
#include "libavcodec/aacadtsdec.h"
static int spdif_get_offset_and_codec(AVFormatContext *s, |
d8e481bb |
enum IEC61937DataType data_type, |
3b39e273 |
const char *buf, int *offset, |
36ef5369 |
enum AVCodecID *codec) |
3b39e273 |
{
AACADTSHeaderInfo aac_hdr;
GetBitContext gbc;
switch (data_type & 0xff) { |
d8e481bb |
case IEC61937_AC3: |
3b39e273 |
*offset = AC3_FRAME_SIZE << 2; |
36ef5369 |
*codec = AV_CODEC_ID_AC3; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_MPEG1_LAYER1: |
3b39e273 |
*offset = spdif_mpeg_pkt_offset[1][0]; |
36ef5369 |
*codec = AV_CODEC_ID_MP1; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_MPEG1_LAYER23: |
3b39e273 |
*offset = spdif_mpeg_pkt_offset[1][0]; |
36ef5369 |
*codec = AV_CODEC_ID_MP3; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_MPEG2_EXT: |
3b39e273 |
*offset = 4608; |
36ef5369 |
*codec = AV_CODEC_ID_MP3; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_MPEG2_AAC: |
3b39e273 |
init_get_bits(&gbc, buf, AAC_ADTS_HEADER_SIZE * 8); |
f86387b6 |
if (avpriv_aac_parse_header(&gbc, &aac_hdr) < 0) { |
3b39e273 |
if (s) /* be silent during a probe */
av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
return AVERROR_INVALIDDATA;
}
*offset = aac_hdr.samples << 2; |
36ef5369 |
*codec = AV_CODEC_ID_AAC; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_MPEG2_LAYER1_LSF: |
3b39e273 |
*offset = spdif_mpeg_pkt_offset[0][0]; |
36ef5369 |
*codec = AV_CODEC_ID_MP1; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_MPEG2_LAYER2_LSF: |
3b39e273 |
*offset = spdif_mpeg_pkt_offset[0][1]; |
36ef5369 |
*codec = AV_CODEC_ID_MP2; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_MPEG2_LAYER3_LSF: |
3b39e273 |
*offset = spdif_mpeg_pkt_offset[0][2]; |
36ef5369 |
*codec = AV_CODEC_ID_MP3; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_DTS1: |
3b39e273 |
*offset = 2048; |
36ef5369 |
*codec = AV_CODEC_ID_DTS; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_DTS2: |
3b39e273 |
*offset = 4096; |
36ef5369 |
*codec = AV_CODEC_ID_DTS; |
3b39e273 |
break; |
d8e481bb |
case IEC61937_DTS3: |
3b39e273 |
*offset = 8192; |
36ef5369 |
*codec = AV_CODEC_ID_DTS; |
3b39e273 |
break;
default:
if (s) { /* be silent during a probe */ |
1ecdf891 |
avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
data_type); |
3b39e273 |
}
return AVERROR_PATCHWELCOME;
}
return 0;
}
/* 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)
{ |
1ae9d282 |
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); |
3b39e273 |
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;
|
1ae9d282 |
if (buf + 4 + AAC_ADTS_HEADER_SIZE > p_buf + buf_size) |
3b39e273 |
break;
/* continue probing to find more sync codes */ |
1ae9d282 |
probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p_buf + buf_size - 1); |
3b39e273 |
/* skip directly to the next sync code */
if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1], |
1ae9d282 |
&buf[5], &offset, codec)) {
if (buf + offset >= p_buf + buf_size) |
3b39e273 |
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 */ |
e0f8be64 |
return AVPROBE_SCORE_EXTENSION; |
3b39e273 |
/* some sync codes were found */ |
e0f8be64 |
return AVPROBE_SCORE_EXTENSION / 4; |
3b39e273 |
}
|
6e9651d1 |
static int spdif_read_header(AVFormatContext *s) |
3b39e273 |
{
s->ctx_flags |= AVFMTCTX_NOHEADER;
return 0;
}
|
1ae9d282 |
int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt) |
3b39e273 |
{ |
471fe57e |
AVIOContext *pb = s->pb; |
d8e481bb |
enum IEC61937DataType data_type; |
36ef5369 |
enum AVCodecID codec_id; |
3b39e273 |
uint32_t state = 0;
int pkt_size_bits, offset, ret;
while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) { |
e63a3628 |
state = (state << 8) | avio_r8(pb); |
d34ec64a |
if (avio_feof(pb)) |
3b39e273 |
return AVERROR_EOF;
}
|
e63a3628 |
data_type = avio_rl16(pb);
pkt_size_bits = avio_rl16(pb); |
3b39e273 |
if (pkt_size_bits % 16) |
1ecdf891 |
avpriv_request_sample(s, "Packet not ending at a 16-bit boundary"); |
3b39e273 |
ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
if (ret)
return ret;
|
384c9c2f |
pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE; |
3b39e273 |
|
e63a3628 |
if (avio_read(pb, pkt->data, pkt->size) < pkt->size) { |
ce70f28a |
av_packet_unref(pkt); |
3b39e273 |
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);
if (ret) { |
ce70f28a |
av_packet_unref(pkt); |
3b39e273 |
return ret;
}
/* skip over the padding to the beginning of the next frame */ |
45a8a02a |
avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE); |
3b39e273 |
if (!s->nb_streams) {
/* first packet, create a stream */ |
3b3bbdd3 |
AVStream *st = avformat_new_stream(s, NULL); |
3b39e273 |
if (!st) { |
ce70f28a |
av_packet_unref(pkt); |
3b39e273 |
return AVERROR(ENOMEM);
} |
9200514a |
st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
st->codecpar->codec_id = codec_id;
} else if (codec_id != s->streams[0]->codecpar->codec_id) { |
63d744e2 |
avpriv_report_missing_feature(s, "Codec change in IEC 61937"); |
3b39e273 |
return AVERROR_PATCHWELCOME;
}
|
9200514a |
if (!s->bit_rate && s->streams[0]->codecpar->sample_rate) |
3b39e273 |
/* stream bitrate matches 16-bit stereo PCM bitrate for currently
supported codecs */ |
9200514a |
s->bit_rate = 2 * 16 * s->streams[0]->codecpar->sample_rate; |
3b39e273 |
return 0;
}
|
66355be3 |
AVInputFormat ff_spdif_demuxer = { |
dfc2c4d9 |
.name = "spdif",
.long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
.read_probe = spdif_probe,
.read_header = spdif_read_header, |
1ae9d282 |
.read_packet = ff_spdif_read_packet, |
20234a4b |
.flags = AVFMT_GENERIC_INDEX, |
3b39e273 |
}; |