FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/ac3.h"
31 #include "libavcodec/adts_parser.h"
32 
33 #include "avformat.h"
34 #include "spdif.h"
35 
37  enum IEC61937DataType data_type,
38  const char *buf, int *offset,
39  enum AVCodecID *codec)
40 {
41  uint32_t samples;
43  int ret;
44 
45  switch (data_type & 0xff) {
46  case IEC61937_AC3:
47  *offset = AC3_FRAME_SIZE << 2;
48  *codec = AV_CODEC_ID_AC3;
49  break;
51  *offset = spdif_mpeg_pkt_offset[1][0];
52  *codec = AV_CODEC_ID_MP1;
53  break;
55  *offset = spdif_mpeg_pkt_offset[1][0];
56  *codec = AV_CODEC_ID_MP3;
57  break;
58  case IEC61937_MPEG2_EXT:
59  *offset = 4608;
60  *codec = AV_CODEC_ID_MP3;
61  break;
62  case IEC61937_MPEG2_AAC:
63  ret = av_adts_header_parse(buf, &samples, &frames);
64  if (ret < 0) {
65  if (s) /* be silent during a probe */
66  av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
67  return ret;
68  }
69  *offset = samples << 2;
70  *codec = AV_CODEC_ID_AAC;
71  break;
73  *offset = spdif_mpeg_pkt_offset[0][0];
74  *codec = AV_CODEC_ID_MP1;
75  break;
77  *offset = spdif_mpeg_pkt_offset[0][1];
78  *codec = AV_CODEC_ID_MP2;
79  break;
81  *offset = spdif_mpeg_pkt_offset[0][2];
82  *codec = AV_CODEC_ID_MP3;
83  break;
84  case IEC61937_DTS1:
85  *offset = 2048;
86  *codec = AV_CODEC_ID_DTS;
87  break;
88  case IEC61937_DTS2:
89  *offset = 4096;
90  *codec = AV_CODEC_ID_DTS;
91  break;
92  case IEC61937_DTS3:
93  *offset = 8192;
94  *codec = AV_CODEC_ID_DTS;
95  break;
96  default:
97  if (s) { /* be silent during a probe */
98  avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
99  data_type);
100  }
101  return AVERROR_PATCHWELCOME;
102  }
103  return 0;
104 }
105 
106 /* Largest offset between bursts we currently handle, i.e. AAC with
107  samples = 4096 */
108 #define SPDIF_MAX_OFFSET 16384
109 
110 static int spdif_probe(AVProbeData *p)
111 {
112  enum AVCodecID codec;
113  return ff_spdif_probe (p->buf, p->buf_size, &codec);
114 }
115 
116 int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
117 {
118  const uint8_t *buf = p_buf;
119  const uint8_t *probe_end = p_buf + FFMIN(2 * SPDIF_MAX_OFFSET, buf_size - 1);
120  const uint8_t *expected_code = buf + 7;
121  uint32_t state = 0;
122  int sync_codes = 0;
123  int consecutive_codes = 0;
124  int offset;
125 
126  for (; buf < probe_end; buf++) {
127  state = (state << 8) | *buf;
128 
129  if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
130  && buf[1] < 0x37) {
131  sync_codes++;
132 
133  if (buf == expected_code) {
134  if (++consecutive_codes >= 2)
135  return AVPROBE_SCORE_MAX;
136  } else
137  consecutive_codes = 0;
138 
139  if (buf + 4 + AV_AAC_ADTS_HEADER_SIZE > p_buf + buf_size)
140  break;
141 
142  /* continue probing to find more sync codes */
143  probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p_buf + buf_size - 1);
144 
145  /* skip directly to the next sync code */
146  if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
147  &buf[5], &offset, codec)) {
148  if (buf + offset >= p_buf + buf_size)
149  break;
150  expected_code = buf + offset;
151  buf = expected_code - 7;
152  }
153  }
154  }
155 
156  if (!sync_codes)
157  return 0;
158 
159  if (sync_codes >= 6)
160  /* good amount of sync codes but with unexpected offsets */
162 
163  /* some sync codes were found */
164  return AVPROBE_SCORE_EXTENSION / 4;
165 }
166 
168 {
170  return 0;
171 }
172 
174 {
175  AVIOContext *pb = s->pb;
176  enum IEC61937DataType data_type;
177  enum AVCodecID codec_id;
178  uint32_t state = 0;
179  int pkt_size_bits, offset, ret;
180 
181  while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
182  state = (state << 8) | avio_r8(pb);
183  if (avio_feof(pb))
184  return AVERROR_EOF;
185  }
186 
187  data_type = avio_rl16(pb);
188  pkt_size_bits = avio_rl16(pb);
189 
190  if (pkt_size_bits % 16)
191  avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
192 
193  ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
194  if (ret)
195  return ret;
196 
197  pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
198 
199  if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
200  av_packet_unref(pkt);
201  return AVERROR_EOF;
202  }
203  ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
204 
205  ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
206  &offset, &codec_id);
207  if (ret) {
208  av_packet_unref(pkt);
209  return ret;
210  }
211 
212  /* skip over the padding to the beginning of the next frame */
213  avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
214 
215  if (!s->nb_streams) {
216  /* first packet, create a stream */
218  if (!st) {
219  av_packet_unref(pkt);
220  return AVERROR(ENOMEM);
221  }
223  st->codecpar->codec_id = codec_id;
224  } else if (codec_id != s->streams[0]->codecpar->codec_id) {
225  avpriv_report_missing_feature(s, "Codec change in IEC 61937");
226  return AVERROR_PATCHWELCOME;
227  }
228 
229  if (!s->bit_rate && s->streams[0]->codecpar->sample_rate)
230  /* stream bitrate matches 16-bit stereo PCM bitrate for currently
231  supported codecs */
232  s->bit_rate = 2 * 16 * s->streams[0]->codecpar->sample_rate;
233 
234  return 0;
235 }
236 
238  .name = "spdif",
239  .long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
240  .read_probe = spdif_probe,
241  .read_header = spdif_read_header,
242  .read_packet = ff_spdif_read_packet,
243  .flags = AVFMT_GENERIC_INDEX,
244 };
static int spdif_get_offset_and_codec(AVFormatContext *s, enum IEC61937DataType data_type, const char *buf, int *offset, enum AVCodecID *codec)
Definition: spdifdec.c:36
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1465
#define BURST_HEADER_SIZE
Definition: spdif.h:30
MPEG-2, layer-1 low sampling frequency.
Definition: spdif.h:38
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3900
int size
Definition: avcodec.h:1446
static int spdif_read_header(AVFormatContext *s)
Definition: spdifdec.c:167
int64_t bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:1473
IEC61937DataType
Definition: spdif.h:32
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:331
static AVPacket pkt
void ff_spdif_bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
Definition: spdif.c:26
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1400
int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
Definition: spdifdec.c:116
Format I/O context.
Definition: avformat.h:1351
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:27
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:110
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1295
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4455
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
uint8_t * data
Definition: avcodec.h:1445
#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:557
#define FFALIGN(x, a)
Definition: macros.h:48
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:648
#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:86
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:215
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
DTS type III (2048 samples)
Definition: spdif.h:43
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:559
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3896
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
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
#define AV_AAC_ADTS_HEADER_SIZE
Definition: adts_parser.h:25
MPEG-1 layer 1.
Definition: spdif.h:34
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:639
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:451
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:450
static struct @303 state
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1407
#define FFMIN(a, b)
Definition: common.h:96
#define SPDIF_MAX_OFFSET
Definition: spdifdec.c:108
#define s(width, name)
Definition: cbs_vp9.c:257
enum AVCodecID codec_id
Definition: vaapi_decode.c:364
int frames
Definition: movenc.c:65
#define SYNCWORD2
Definition: spdif.h:29
Stream structure.
Definition: avformat.h:874
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:598
void * buf
Definition: avisynth_c.h:690
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:470
byte swapping routines
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:458
DTS type I (512 samples)
Definition: spdif.h:41
This structure contains the data a format has to probe a file.
Definition: avformat.h:448
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
MPEG-2, layer-2 low sampling frequency.
Definition: spdif.h:39
int sample_rate
Audio only.
Definition: avcodec.h:4010
#define AC3_FRAME_SIZE
Definition: ac3.h:38
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:460
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:754
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
AC-3 data.
Definition: spdif.h:33
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:647
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1021
int avio_feof(AVIOContext *s)
feof() equivalent for AVIOContext.
Definition: aviobuf.c:358
int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: spdifdec.c:173
This structure stores compressed data.
Definition: avcodec.h:1422
Common code between the AC-3 encoder and decoder.
AVInputFormat ff_spdif_demuxer
Definition: spdifdec.c:237