FFmpeg
spdifdec.c
Go to the documentation of this file.
1 /*
2  * IEC 61937 demuxer
3  * Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * IEC 61937 demuxer, used for compressed data in S/PDIF
25  * @author Anssi Hannula
26  */
27 
28 #include "libavutil/bswap.h"
29 
30 #include "libavcodec/ac3defs.h"
31 #include "libavcodec/adts_parser.h"
32 
33 #include "avformat.h"
34 #include "demux.h"
35 #include "internal.h"
36 #include "spdif.h"
37 
39  enum IEC61937DataType data_type,
40  const char *buf, int *offset,
41  enum AVCodecID *codec)
42 {
43  uint32_t samples;
44  uint8_t frames;
45  int ret;
46 
47  switch (data_type & 0xff) {
48  case IEC61937_AC3:
49  *offset = AC3_FRAME_SIZE << 2;
50  *codec = AV_CODEC_ID_AC3;
51  break;
54  *codec = AV_CODEC_ID_MP1;
55  break;
58  *codec = AV_CODEC_ID_MP3;
59  break;
60  case IEC61937_MPEG2_EXT:
61  *offset = 4608;
62  *codec = AV_CODEC_ID_MP3;
63  break;
64  case IEC61937_MPEG2_AAC:
66  if (ret < 0) {
67  if (s) /* be silent during a probe */
68  av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
69  return ret;
70  }
71  *offset = samples << 2;
72  *codec = AV_CODEC_ID_AAC;
73  break;
76  *codec = AV_CODEC_ID_MP1;
77  break;
80  *codec = AV_CODEC_ID_MP2;
81  break;
84  *codec = AV_CODEC_ID_MP3;
85  break;
86  case IEC61937_DTS1:
87  *offset = 2048;
88  *codec = AV_CODEC_ID_DTS;
89  break;
90  case IEC61937_DTS2:
91  *offset = 4096;
92  *codec = AV_CODEC_ID_DTS;
93  break;
94  case IEC61937_DTS3:
95  *offset = 8192;
96  *codec = AV_CODEC_ID_DTS;
97  break;
98  case IEC61937_EAC3:
99  *offset = 24576;
100  *codec = AV_CODEC_ID_EAC3;
101  break;
102  default:
103  if (s) { /* be silent during a probe */
104  avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
105  data_type);
106  }
107  return AVERROR_PATCHWELCOME;
108  }
109  return 0;
110 }
111 
112 /* Largest offset between bursts we currently handle, i.e. AAC with
113  samples = 4096 */
114 #define SPDIF_MAX_OFFSET 16384
115 
116 static int spdif_probe(const AVProbeData *p)
117 {
118  enum AVCodecID codec;
119  return ff_spdif_probe (p->buf, p->buf_size, &codec);
120 }
121 
122 int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
123 {
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;
127  uint32_t state = 0;
128  int sync_codes = 0;
129  int consecutive_codes = 0;
130  int offset;
131 
132  for (; buf < probe_end; buf++) {
133  state = (state << 8) | *buf;
134 
135  if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
136  && buf[1] < 0x37) {
137  sync_codes++;
138 
139  if (buf == expected_code) {
140  if (++consecutive_codes >= 2)
141  return AVPROBE_SCORE_MAX;
142  } else
143  consecutive_codes = 0;
144 
145  if (buf + 4 + AV_AAC_ADTS_HEADER_SIZE > p_buf + buf_size)
146  break;
147 
148  /* continue probing to find more sync codes */
149  probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p_buf + buf_size - 1);
150 
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)
155  break;
156  expected_code = buf + offset;
157  buf = expected_code - 7;
158  }
159  }
160  }
161 
162  if (!sync_codes)
163  return 0;
164 
165  if (sync_codes >= 6)
166  /* good amount of sync codes but with unexpected offsets */
168 
169  /* some sync codes were found */
170  return AVPROBE_SCORE_EXTENSION / 4;
171 }
172 
174 {
175  s->ctx_flags |= AVFMTCTX_NOHEADER;
176  return 0;
177 }
178 
179 static int spdif_get_pkt_size_bits(int type, int code)
180 {
181  switch (type & 0xff) {
182  case IEC61937_EAC3:
183  return code << 3;
184  default:
185  return code;
186  }
187 }
188 
190 {
191  AVIOContext *pb = s->pb;
192  enum IEC61937DataType data_type;
193  enum AVCodecID codec_id;
194  uint32_t state = 0;
195  int pkt_size_bits, offset, ret;
196 
197  while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
198  state = (state << 8) | avio_r8(pb);
199  if (avio_feof(pb))
200  return AVERROR_EOF;
201  }
202 
203  data_type = avio_rl16(pb);
204  pkt_size_bits = spdif_get_pkt_size_bits(data_type, avio_rl16(pb));
205 
206  if (pkt_size_bits % 16)
207  avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
208 
209  ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
210  if (ret)
211  return ret;
212 
214 
215  if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
216  return AVERROR_EOF;
217  }
218  ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
219 
220  ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
221  &offset, &codec_id);
222  if (ret < 0) {
223  return ret;
224  }
225 
226  /* skip over the padding to the beginning of the next frame */
228 
229  if (!s->nb_streams) {
230  /* first packet, create a stream */
232  if (!st) {
233  return AVERROR(ENOMEM);
234  }
236  st->codecpar->codec_id = codec_id;
237  if (codec_id == AV_CODEC_ID_EAC3)
239  } else if (codec_id != s->streams[0]->codecpar->codec_id) {
240  avpriv_report_missing_feature(s, "Codec change in IEC 61937");
241  return AVERROR_PATCHWELCOME;
242  }
243 
244  if (!s->bit_rate && s->streams[0]->codecpar->sample_rate)
245  /* stream bitrate matches 16-bit stereo PCM bitrate for currently
246  supported codecs */
247  s->bit_rate = 2 * 16LL * s->streams[0]->codecpar->sample_rate;
248 
249  return 0;
250 }
251 
253  .p.name = "spdif",
254  .p.long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
255  .p.flags = AVFMT_GENERIC_INDEX,
256  .read_probe = spdif_probe,
257  .read_header = spdif_read_header,
258  .read_packet = ff_spdif_read_packet,
259 };
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:448
IEC61937DataType
IEC61937DataType
Definition: spdif.h:32
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_AAC_ADTS_HEADER_SIZE
#define AV_AAC_ADTS_HEADER_SIZE
Definition: adts_parser.h:25
AVPacket::data
uint8_t * data
Definition: packet.h:539
IEC61937_MPEG2_LAYER1_LSF
@ IEC61937_MPEG2_LAYER1_LSF
MPEG-2, layer-1 low sampling frequency.
Definition: spdif.h:38
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
av_adts_header_parse
int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, uint8_t *frames)
Extract the number of samples and frames from AAC data.
Definition: adts_parser.c:30
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
ff_spdif_read_packet
int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: spdifdec.c:189
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:358
frames
if it could not because there are no more frames
Definition: filter_design.txt:266
SPDIF_MAX_OFFSET
#define SPDIF_MAX_OFFSET
Definition: spdifdec.c:114
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:446
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
ff_spdif_bswap_buf16
void ff_spdif_bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
Definition: spdif.c:26
IEC61937_MPEG2_EXT
@ IEC61937_MPEG2_EXT
MPEG-2 data with extension.
Definition: spdif.h:36
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:445
AV_BSWAP16C
#define AV_BSWAP16C(x)
Definition: bswap.h:47
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:410
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:325
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
IEC61937_AC3
@ IEC61937_AC3
AC-3 data.
Definition: spdif.h:33
ac3defs.h
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
spdif_get_pkt_size_bits
static int spdif_get_pkt_size_bits(int type, int code)
Definition: spdifdec.c:179
NULL
#define NULL
Definition: coverity.c:32
AC3_FRAME_SIZE
#define AC3_FRAME_SIZE
Definition: ac3defs.h:32
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:64
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1251
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
ff_spdif_demuxer
const FFInputFormat ff_spdif_demuxer
Definition: spdifdec.c:252
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
IEC61937_DTS3
@ IEC61937_DTS3
DTS type III (2048 samples)
Definition: spdif.h:43
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:485
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:447
spdif_get_offset_and_codec
static int spdif_get_offset_and_codec(AVFormatContext *s, enum IEC61937DataType data_type, const char *buf, int *offset, enum AVCodecID *codec)
Definition: spdifdec.c:38
IEC61937_MPEG1_LAYER23
@ IEC61937_MPEG1_LAYER23
MPEG-1 layer 2 or 3 data or MPEG-2 without extension.
Definition: spdif.h:35
AVIOContext
Bytestream IO Context.
Definition: avio.h:160
AVPacket::size
int size
Definition: packet.h:540
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
IEC61937_DTS2
@ IEC61937_DTS2
DTS type II (1024 samples)
Definition: spdif.h:42
AV_CODEC_ID_DTS
@ AV_CODEC_ID_DTS
Definition: codec_id.h:449
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
avio_r8
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:603
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
IEC61937_DTS1
@ IEC61937_DTS1
DTS type I (512 samples)
Definition: spdif.h:41
ff_spdif_probe
int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
Definition: spdifdec.c:122
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
IEC61937_MPEG1_LAYER1
@ IEC61937_MPEG1_LAYER1
MPEG-1 layer 1.
Definition: spdif.h:34
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
demux.h
IEC61937_EAC3
@ IEC61937_EAC3
E-AC-3 data.
Definition: spdif.h:51
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
bswap.h
adts_parser.h
avformat.h
SYNCWORD2
#define SYNCWORD2
Definition: spdif.h:29
spdif_probe
static int spdif_probe(const AVProbeData *p)
Definition: spdifdec.c:116
BURST_HEADER_SIZE
#define BURST_HEADER_SIZE
Definition: spdif.h:30
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
state
static struct @463 state
IEC61937_MPEG2_LAYER2_LSF
@ IEC61937_MPEG2_LAYER2_LSF
MPEG-2, layer-2 low sampling frequency.
Definition: spdif.h:39
avpriv_request_sample
#define avpriv_request_sample(...)
Definition: tableprint_vlc.h:36
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
spdif_read_header
static int spdif_read_header(AVFormatContext *s)
Definition: spdifdec.c:173
FFInputFormat
Definition: demux.h:42
AVSTREAM_PARSE_FULL
@ AVSTREAM_PARSE_FULL
full parsing and repack
Definition: avformat.h:593
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
IEC61937_MPEG2_AAC
@ IEC61937_MPEG2_AAC
MPEG-2 AAC ADTS.
Definition: spdif.h:37
AV_CODEC_ID_MP1
@ AV_CODEC_ID_MP1
Definition: codec_id.h:487
spdif_mpeg_pkt_offset
static const uint16_t spdif_mpeg_pkt_offset[2][3]
Definition: spdif.h:55
IEC61937_MPEG2_LAYER3_LSF
@ IEC61937_MPEG2_LAYER3_LSF
MPEG-2, layer-3 low sampling frequency.
Definition: spdif.h:40
spdif.h
SYNCWORD1
#define SYNCWORD1
Definition: spdif.h:28
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346