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 "avformat.h"
29 #include "spdif.h"
30 #include "libavcodec/ac3.h"
31 #include "libavcodec/aacadtsdec.h"
32 
34  enum IEC61937DataType data_type,
35  const char *buf, int *offset,
36  enum AVCodecID *codec)
37 {
38  AACADTSHeaderInfo aac_hdr;
39  GetBitContext gbc;
40 
41  switch (data_type & 0xff) {
42  case IEC61937_AC3:
43  *offset = AC3_FRAME_SIZE << 2;
44  *codec = AV_CODEC_ID_AC3;
45  break;
47  *offset = spdif_mpeg_pkt_offset[1][0];
48  *codec = AV_CODEC_ID_MP1;
49  break;
51  *offset = spdif_mpeg_pkt_offset[1][0];
52  *codec = AV_CODEC_ID_MP3;
53  break;
54  case IEC61937_MPEG2_EXT:
55  *offset = 4608;
56  *codec = AV_CODEC_ID_MP3;
57  break;
58  case IEC61937_MPEG2_AAC:
59  init_get_bits(&gbc, buf, AAC_ADTS_HEADER_SIZE * 8);
60  if (avpriv_aac_parse_header(&gbc, &aac_hdr)) {
61  if (s) /* be silent during a probe */
62  av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
63  return AVERROR_INVALIDDATA;
64  }
65  *offset = aac_hdr.samples << 2;
66  *codec = AV_CODEC_ID_AAC;
67  break;
69  *offset = spdif_mpeg_pkt_offset[0][0];
70  *codec = AV_CODEC_ID_MP1;
71  break;
73  *offset = spdif_mpeg_pkt_offset[0][1];
74  *codec = AV_CODEC_ID_MP2;
75  break;
77  *offset = spdif_mpeg_pkt_offset[0][2];
78  *codec = AV_CODEC_ID_MP3;
79  break;
80  case IEC61937_DTS1:
81  *offset = 2048;
82  *codec = AV_CODEC_ID_DTS;
83  break;
84  case IEC61937_DTS2:
85  *offset = 4096;
86  *codec = AV_CODEC_ID_DTS;
87  break;
88  case IEC61937_DTS3:
89  *offset = 8192;
90  *codec = AV_CODEC_ID_DTS;
91  break;
92  default:
93  if (s) { /* be silent during a probe */
94  avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
95  data_type);
96  }
97  return AVERROR_PATCHWELCOME;
98  }
99  return 0;
100 }
101 
102 /* Largest offset between bursts we currently handle, i.e. AAC with
103  aac_hdr.samples = 4096 */
104 #define SPDIF_MAX_OFFSET 16384
105 
106 static int spdif_probe(AVProbeData *p)
107 {
108  enum AVCodecID codec;
109  return ff_spdif_probe (p->buf, p->buf_size, &codec);
110 }
111 
112 int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
113 {
114  const uint8_t *buf = p_buf;
115  const uint8_t *probe_end = p_buf + FFMIN(2 * SPDIF_MAX_OFFSET, buf_size - 1);
116  const uint8_t *expected_code = buf + 7;
117  uint32_t state = 0;
118  int sync_codes = 0;
119  int consecutive_codes = 0;
120  int offset;
121 
122  for (; buf < probe_end; buf++) {
123  state = (state << 8) | *buf;
124 
125  if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
126  && buf[1] < 0x37) {
127  sync_codes++;
128 
129  if (buf == expected_code) {
130  if (++consecutive_codes >= 2)
131  return AVPROBE_SCORE_MAX;
132  } else
133  consecutive_codes = 0;
134 
135  if (buf + 4 + AAC_ADTS_HEADER_SIZE > p_buf + buf_size)
136  break;
137 
138  /* continue probing to find more sync codes */
139  probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p_buf + buf_size - 1);
140 
141  /* skip directly to the next sync code */
142  if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
143  &buf[5], &offset, codec)) {
144  if (buf + offset >= p_buf + buf_size)
145  break;
146  expected_code = buf + offset;
147  buf = expected_code - 7;
148  }
149  }
150  }
151 
152  if (!sync_codes)
153  return 0;
154 
155  if (sync_codes >= 6)
156  /* good amount of sync codes but with unexpected offsets */
157  return AVPROBE_SCORE_MAX / 2;
158 
159  /* some sync codes were found */
160  return AVPROBE_SCORE_MAX / 8;
161 }
162 
164 {
166  return 0;
167 }
168 
170 {
171  AVIOContext *pb = s->pb;
172  enum IEC61937DataType data_type;
173  enum AVCodecID codec_id;
174  uint32_t state = 0;
175  int pkt_size_bits, offset, ret;
176 
177  while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
178  state = (state << 8) | avio_r8(pb);
179  if (url_feof(pb))
180  return AVERROR_EOF;
181  }
182 
183  data_type = avio_rl16(pb);
184  pkt_size_bits = avio_rl16(pb);
185 
186  if (pkt_size_bits % 16)
187  avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
188 
189  ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
190  if (ret)
191  return ret;
192 
193  pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
194 
195  if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
196  av_free_packet(pkt);
197  return AVERROR_EOF;
198  }
199  ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
200 
201  ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
202  &offset, &codec_id);
203  if (ret) {
204  av_free_packet(pkt);
205  return ret;
206  }
207 
208  /* skip over the padding to the beginning of the next frame */
209  avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
210 
211  if (!s->nb_streams) {
212  /* first packet, create a stream */
214  if (!st) {
215  av_free_packet(pkt);
216  return AVERROR(ENOMEM);
217  }
219  st->codec->codec_id = codec_id;
220  } else if (codec_id != s->streams[0]->codec->codec_id) {
221  avpriv_report_missing_feature(s, "Codec change in IEC 61937");
222  return AVERROR_PATCHWELCOME;
223  }
224 
225  if (!s->bit_rate && s->streams[0]->codec->sample_rate)
226  /* stream bitrate matches 16-bit stereo PCM bitrate for currently
227  supported codecs */
228  s->bit_rate = 2 * 16 * s->streams[0]->codec->sample_rate;
229 
230  return 0;
231 }
232 
234  .name = "spdif",
235  .long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
236  .read_probe = spdif_probe,
237  .read_header = spdif_read_header,
238  .read_packet = ff_spdif_read_packet,
239  .flags = AVFMT_GENERIC_INDEX,
240 };
static int spdif_get_offset_and_codec(AVFormatContext *s, enum IEC61937DataType data_type, const char *buf, int *offset, enum AVCodecID *codec)
Definition: spdifdec.c:33
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
int64_t pos
byte position in stream, -1 if unknown
#define BURST_HEADER_SIZE
Definition: spdif.h:30
MPEG-2, layer-1 low sampling frequency.
Definition: spdif.h:38
static int spdif_read_header(AVFormatContext *s)
Definition: spdifdec.c:163
IEC61937DataType
Definition: spdif.h:32
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:256
void ff_spdif_bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
Definition: spdif.c:26
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:980
#define FFALIGN(x, a)
Definition: common.h:63
int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
Definition: spdifdec.c:112
Format I/O context.
Definition: avformat.h:944
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
MPEG-2 AAC ADTS.
Definition: spdif.h:37
uint8_t
static int spdif_probe(AVProbeData *p)
Definition: spdifdec.c:106
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:914
static AVPacket pkt
Definition: demuxing.c:56
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
Definition: avformat.h:992
uint8_t * data
#define AVERROR_EOF
End of file.
Definition: error.h:55
DTS type II (1024 samples)
Definition: spdif.h:42
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:248
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
#define AV_BSWAP16C(x)
Definition: bswap.h:51
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
AVCodecID
Identify the syntax and semantics of the bitstream.
DTS type III (2048 samples)
Definition: spdif.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
preferred ID for decoding MPEG audio layer 1, 2 or 3
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
MPEG-1 layer 2 or 3 data or MPEG-2 without extension.
Definition: spdif.h:35
MPEG-2, layer-3 low sampling frequency.
Definition: spdif.h:40
enum AVCodecID codec_id
Definition: mov_chan.c:433
static const uint8_t offset[127][2]
Definition: vf_spp.c:70
MPEG-1 layer 1.
Definition: spdif.h:34
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:469
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:991
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
#define SPDIF_MAX_OFFSET
Definition: spdifdec.c:104
uint32_t samples
Definition: aacadtsdec.h:33
int url_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:280
#define SYNCWORD2
Definition: spdif.h:29
Stream structure.
Definition: avformat.h:643
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
NULL
Definition: eval.c:55
#define AAC_ADTS_HEADER_SIZE
Definition: aacadtsdec.h:29
enum AVMediaType codec_type
enum AVCodecID codec_id
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
int avpriv_aac_parse_header(GetBitContext *gbc, AACADTSHeaderInfo *hdr)
Parse AAC frame header.
Definition: aacadtsdec.c:29
void * buf
Definition: avisynth_c.h:594
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:353
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:379
DTS type I (512 samples)
Definition: spdif.h:41
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static uint32_t state
Definition: trasher.c:27
MPEG-2, layer-2 low sampling frequency.
Definition: spdif.h:39
#define AC3_FRAME_SIZE
Definition: ac3.h:37
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:563
Main libavformat public API header.
MPEG-2 data with extension.
Definition: spdif.h:36
static const uint16_t spdif_mpeg_pkt_offset[2][3]
Definition: spdif.h:55
#define SYNCWORD1
Definition: spdif.h:28
int bit_rate
Decoding: total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:1016
AC-3 data.
Definition: spdif.h:33
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: spdifdec.c:169
This structure stores compressed data.
Common code between the AC-3 encoder and decoder.
AVInputFormat ff_spdif_demuxer
Definition: spdifdec.c:233