Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / libavformat / spdifdec.c
blob6d37c94f7586a4857d6d48996ed8bdccf56061da
1 /*
2 * IEC 61937 demuxer
3 * Copyright (c) 2010 Anssi Hannula <anssi.hannula at iki.fi>
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 /**
23 * @file
24 * IEC 61937 demuxer, used for compressed data in S/PDIF
25 * @author Anssi Hannula
28 #include "avformat.h"
29 #include "spdif.h"
30 #include "libavcodec/ac3.h"
31 #include "libavcodec/aacadtsdec.h"
33 static int spdif_get_offset_and_codec(AVFormatContext *s,
34 enum IEC61937DataType data_type,
35 const char *buf, int *offset,
36 enum AVCodecID *codec)
38 AACADTSHeaderInfo aac_hdr;
39 GetBitContext gbc;
41 switch (data_type & 0xff) {
42 case IEC61937_AC3:
43 *offset = AC3_FRAME_SIZE << 2;
44 *codec = AV_CODEC_ID_AC3;
45 break;
46 case IEC61937_MPEG1_LAYER1:
47 *offset = spdif_mpeg_pkt_offset[1][0];
48 *codec = AV_CODEC_ID_MP1;
49 break;
50 case IEC61937_MPEG1_LAYER23:
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;
65 *offset = aac_hdr.samples << 2;
66 *codec = AV_CODEC_ID_AAC;
67 break;
68 case IEC61937_MPEG2_LAYER1_LSF:
69 *offset = spdif_mpeg_pkt_offset[0][0];
70 *codec = AV_CODEC_ID_MP1;
71 break;
72 case IEC61937_MPEG2_LAYER2_LSF:
73 *offset = spdif_mpeg_pkt_offset[0][1];
74 *codec = AV_CODEC_ID_MP2;
75 break;
76 case IEC61937_MPEG2_LAYER3_LSF:
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 av_log(s, AV_LOG_WARNING, "Data type 0x%04x", data_type);
95 av_log_missing_feature(s, " in IEC 61937", 1);
97 return AVERROR_PATCHWELCOME;
99 return 0;
102 /* Largest offset between bursts we currently handle, i.e. AAC with
103 aac_hdr.samples = 4096 */
104 #define SPDIF_MAX_OFFSET 16384
106 static int spdif_probe(AVProbeData *p)
108 const uint8_t *buf = p->buf;
109 const uint8_t *probe_end = p->buf + FFMIN(2 * SPDIF_MAX_OFFSET, p->buf_size - 1);
110 const uint8_t *expected_code = buf + 7;
111 uint32_t state = 0;
112 int sync_codes = 0;
113 int consecutive_codes = 0;
114 int offset;
115 enum AVCodecID codec;
117 for (; buf < probe_end; buf++) {
118 state = (state << 8) | *buf;
120 if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
121 && buf[1] < 0x37) {
122 sync_codes++;
124 if (buf == expected_code) {
125 if (++consecutive_codes >= 2)
126 return AVPROBE_SCORE_MAX;
127 } else
128 consecutive_codes = 0;
130 if (buf + 4 + AAC_ADTS_HEADER_SIZE > p->buf + p->buf_size)
131 break;
133 /* continue probing to find more sync codes */
134 probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p->buf + p->buf_size - 1);
136 /* skip directly to the next sync code */
137 if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
138 &buf[5], &offset, &codec)) {
139 if (buf + offset >= p->buf + p->buf_size)
140 break;
141 expected_code = buf + offset;
142 buf = expected_code - 7;
147 if (!sync_codes)
148 return 0;
150 if (sync_codes >= 6)
151 /* good amount of sync codes but with unexpected offsets */
152 return AVPROBE_SCORE_MAX / 2;
154 /* some sync codes were found */
155 return AVPROBE_SCORE_MAX / 8;
158 static int spdif_read_header(AVFormatContext *s)
160 s->ctx_flags |= AVFMTCTX_NOHEADER;
161 return 0;
164 static int spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
166 AVIOContext *pb = s->pb;
167 enum IEC61937DataType data_type;
168 enum AVCodecID codec_id;
169 uint32_t state = 0;
170 int pkt_size_bits, offset, ret;
172 while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
173 state = (state << 8) | avio_r8(pb);
174 if (pb->eof_reached)
175 return AVERROR_EOF;
178 data_type = avio_rl16(pb);
179 pkt_size_bits = avio_rl16(pb);
181 if (pkt_size_bits % 16)
182 av_log_ask_for_sample(s, "Packet does not end to a 16-bit boundary.");
184 ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
185 if (ret)
186 return ret;
188 pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
190 if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
191 av_free_packet(pkt);
192 return AVERROR_EOF;
194 ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
196 ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
197 &offset, &codec_id);
198 if (ret) {
199 av_free_packet(pkt);
200 return ret;
203 /* skip over the padding to the beginning of the next frame */
204 avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
206 if (!s->nb_streams) {
207 /* first packet, create a stream */
208 AVStream *st = avformat_new_stream(s, NULL);
209 if (!st) {
210 av_free_packet(pkt);
211 return AVERROR(ENOMEM);
213 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
214 st->codec->codec_id = codec_id;
215 } else if (codec_id != s->streams[0]->codec->codec_id) {
216 av_log_missing_feature(s, "Codec change in IEC 61937", 0);
217 return AVERROR_PATCHWELCOME;
220 if (!s->bit_rate && s->streams[0]->codec->sample_rate)
221 /* stream bitrate matches 16-bit stereo PCM bitrate for currently
222 supported codecs */
223 s->bit_rate = 2 * 16 * s->streams[0]->codec->sample_rate;
225 return 0;
228 AVInputFormat ff_spdif_demuxer = {
229 .name = "spdif",
230 .long_name = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
231 .read_probe = spdif_probe,
232 .read_header = spdif_read_header,
233 .read_packet = spdif_read_packet,
234 .flags = AVFMT_GENERIC_INDEX,