tools/sofa2wavs: fix build on Windows
[ffmpeg.git] / libavformat / spdifdec.c
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
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)
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;
52 case IEC61937_MPEG1_LAYER1:
53 *offset = spdif_mpeg_pkt_offset[1][0];
54 *codec = AV_CODEC_ID_MP1;
55 break;
56 case IEC61937_MPEG1_LAYER23:
57 *offset = spdif_mpeg_pkt_offset[1][0];
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:
65 ret = av_adts_header_parse(buf, &samples, &frames);
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;
74 case IEC61937_MPEG2_LAYER1_LSF:
75 *offset = spdif_mpeg_pkt_offset[0][0];
76 *codec = AV_CODEC_ID_MP1;
77 break;
78 case IEC61937_MPEG2_LAYER2_LSF:
79 *offset = spdif_mpeg_pkt_offset[0][1];
80 *codec = AV_CODEC_ID_MP2;
81 break;
82 case IEC61937_MPEG2_LAYER3_LSF:
83 *offset = spdif_mpeg_pkt_offset[0][2];
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 */
167 return AVPROBE_SCORE_EXTENSION;
168
169 /* some sync codes were found */
170 return AVPROBE_SCORE_EXTENSION / 4;
171 }
172
173 static int spdif_read_header(AVFormatContext *s)
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
189 int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
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
213 pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
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 */
227 avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
228
229 if (!s->nb_streams) {
230 /* first packet, create a stream */
231 AVStream *st = avformat_new_stream(s, NULL);
232 if (!st) {
233 return AVERROR(ENOMEM);
234 }
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;
239 else
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;
244 }
245
246 if (!s->bit_rate && s->streams[0]->codecpar->sample_rate)
247 /* stream bitrate matches 16-bit stereo PCM bitrate for currently
248 supported codecs */
249 s->bit_rate = 2 * 16LL * s->streams[0]->codecpar->sample_rate;
250
251 return 0;
252 }
253
254 const FFInputFormat ff_spdif_demuxer = {
255 .p.name = "spdif",
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,
261 };