3 * Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * IEC 61937 demuxer, used for compressed data in S/PDIF
25 * @author Anssi Hannula
28 #include "libavutil/bswap.h"
30 #include "libavcodec/ac3defs.h"
31 #include "libavcodec/adts_parser.h"
38 static int spdif_get_offset_and_codec(AVFormatContext
*s
,
39 enum IEC61937DataType data_type
,
40 const char *buf
, int *offset
,
41 enum AVCodecID
*codec
)
47 switch (data_type
& 0xff) {
49 *offset
= AC3_FRAME_SIZE
<< 2;
50 *codec
= AV_CODEC_ID_AC3
;
52 case IEC61937_MPEG1_LAYER1
:
53 *offset
= spdif_mpeg_pkt_offset
[1][0];
54 *codec
= AV_CODEC_ID_MP1
;
56 case IEC61937_MPEG1_LAYER23
:
57 *offset
= spdif_mpeg_pkt_offset
[1][0];
58 *codec
= AV_CODEC_ID_MP3
;
60 case IEC61937_MPEG2_EXT
:
62 *codec
= AV_CODEC_ID_MP3
;
64 case IEC61937_MPEG2_AAC
:
65 ret
= av_adts_header_parse(buf
, &samples
, &frames
);
67 if (s
) /* be silent during a probe */
68 av_log(s
, AV_LOG_ERROR
, "Invalid AAC packet in IEC 61937\n");
71 *offset
= samples
<< 2;
72 *codec
= AV_CODEC_ID_AAC
;
74 case IEC61937_MPEG2_LAYER1_LSF
:
75 *offset
= spdif_mpeg_pkt_offset
[0][0];
76 *codec
= AV_CODEC_ID_MP1
;
78 case IEC61937_MPEG2_LAYER2_LSF
:
79 *offset
= spdif_mpeg_pkt_offset
[0][1];
80 *codec
= AV_CODEC_ID_MP2
;
82 case IEC61937_MPEG2_LAYER3_LSF
:
83 *offset
= spdif_mpeg_pkt_offset
[0][2];
84 *codec
= AV_CODEC_ID_MP3
;
88 *codec
= AV_CODEC_ID_DTS
;
92 *codec
= AV_CODEC_ID_DTS
;
96 *codec
= AV_CODEC_ID_DTS
;
100 *codec
= AV_CODEC_ID_EAC3
;
103 if (s
) { /* be silent during a probe */
104 avpriv_request_sample(s
, "Data type 0x%04x in IEC 61937",
107 return AVERROR_PATCHWELCOME
;
112 /* Largest offset between bursts we currently handle, i.e. AAC with
114 #define SPDIF_MAX_OFFSET 16384
116 static int spdif_probe(const AVProbeData
*p
)
118 enum AVCodecID codec
;
119 return ff_spdif_probe (p
->buf
, p
->buf_size
, &codec
);
122 int ff_spdif_probe(const uint8_t *p_buf
, int buf_size
, enum AVCodecID
*codec
)
124 const uint8_t *buf
= p_buf
;
125 const uint8_t *probe_end
= p_buf
+ FFMIN(2 * SPDIF_MAX_OFFSET
, buf_size
- 1);
126 const uint8_t *expected_code
= buf
+ 7;
129 int consecutive_codes
= 0;
132 for (; buf
< probe_end
; buf
++) {
133 state
= (state
<< 8) | *buf
;
135 if (state
== (AV_BSWAP16C(SYNCWORD1
) << 16 | AV_BSWAP16C(SYNCWORD2
))
139 if (buf
== expected_code
) {
140 if (++consecutive_codes
>= 2)
141 return AVPROBE_SCORE_MAX
;
143 consecutive_codes
= 0;
145 if (buf
+ 4 + AV_AAC_ADTS_HEADER_SIZE
> p_buf
+ buf_size
)
148 /* continue probing to find more sync codes */
149 probe_end
= FFMIN(buf
+ SPDIF_MAX_OFFSET
, p_buf
+ buf_size
- 1);
151 /* skip directly to the next sync code */
152 if (!spdif_get_offset_and_codec(NULL
, (buf
[2] << 8) | buf
[1],
153 &buf
[5], &offset
, codec
)) {
154 if (buf
+ offset
>= p_buf
+ buf_size
)
156 expected_code
= buf
+ offset
;
157 buf
= expected_code
- 7;
166 /* good amount of sync codes but with unexpected offsets */
167 return AVPROBE_SCORE_EXTENSION
;
169 /* some sync codes were found */
170 return AVPROBE_SCORE_EXTENSION
/ 4;
173 static int spdif_read_header(AVFormatContext
*s
)
175 s
->ctx_flags
|= AVFMTCTX_NOHEADER
;
179 static int spdif_get_pkt_size_bits(int type
, int code
)
181 switch (type
& 0xff) {
189 int ff_spdif_read_packet(AVFormatContext
*s
, AVPacket
*pkt
)
191 AVIOContext
*pb
= s
->pb
;
192 enum IEC61937DataType data_type
;
193 enum AVCodecID codec_id
;
195 int pkt_size_bits
, offset
, ret
;
197 while (state
!= (AV_BSWAP16C(SYNCWORD1
) << 16 | AV_BSWAP16C(SYNCWORD2
))) {
198 state
= (state
<< 8) | avio_r8(pb
);
203 data_type
= avio_rl16(pb
);
204 pkt_size_bits
= spdif_get_pkt_size_bits(data_type
, avio_rl16(pb
));
206 if (pkt_size_bits
% 16)
207 avpriv_request_sample(s
, "Packet not ending at a 16-bit boundary");
209 ret
= av_new_packet(pkt
, FFALIGN(pkt_size_bits
, 16) >> 3);
213 pkt
->pos
= avio_tell(pb
) - BURST_HEADER_SIZE
;
215 if (avio_read(pb
, pkt
->data
, pkt
->size
) < pkt
->size
) {
218 ff_spdif_bswap_buf16((uint16_t *)pkt
->data
, (uint16_t *)pkt
->data
, pkt
->size
>> 1);
220 ret
= spdif_get_offset_and_codec(s
, data_type
, pkt
->data
,
226 /* skip over the padding to the beginning of the next frame */
227 avio_skip(pb
, offset
- pkt
->size
- BURST_HEADER_SIZE
);
229 if (!s
->nb_streams
) {
230 /* first packet, create a stream */
231 AVStream
*st
= avformat_new_stream(s
, NULL
);
233 return AVERROR(ENOMEM
);
235 st
->codecpar
->codec_type
= AVMEDIA_TYPE_AUDIO
;
236 st
->codecpar
->codec_id
= codec_id
;
237 if (codec_id
== AV_CODEC_ID_EAC3
)
238 ffstream(st
)->need_parsing
= AVSTREAM_PARSE_FULL
;
240 ffstream(st
)->need_parsing
= AVSTREAM_PARSE_HEADERS
;
241 } else if (codec_id
!= s
->streams
[0]->codecpar
->codec_id
) {
242 avpriv_report_missing_feature(s
, "Codec change in IEC 61937");
243 return AVERROR_PATCHWELCOME
;
246 if (!s
->bit_rate
&& s
->streams
[0]->codecpar
->sample_rate
)
247 /* stream bitrate matches 16-bit stereo PCM bitrate for currently
249 s
->bit_rate
= 2 * 16LL * s
->streams
[0]->codecpar
->sample_rate
;
254 const FFInputFormat ff_spdif_demuxer
= {
256 .p
.long_name
= NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
257 .p
.flags
= AVFMT_GENERIC_INDEX
,
258 .read_probe
= spdif_probe
,
259 .read_header
= spdif_read_header
,
260 .read_packet
= ff_spdif_read_packet
,