Imported Upstream version 6.1
[platform/upstream/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 "internal.h"
35 #include "spdif.h"
36
37 static int spdif_get_offset_and_codec(AVFormatContext *s,
38                                       enum IEC61937DataType data_type,
39                                       const char *buf, int *offset,
40                                       enum AVCodecID *codec)
41 {
42     uint32_t samples;
43     uint8_t frames;
44     int ret;
45
46     switch (data_type & 0xff) {
47     case IEC61937_AC3:
48         *offset = AC3_FRAME_SIZE << 2;
49         *codec = AV_CODEC_ID_AC3;
50         break;
51     case IEC61937_MPEG1_LAYER1:
52         *offset = spdif_mpeg_pkt_offset[1][0];
53         *codec = AV_CODEC_ID_MP1;
54         break;
55     case IEC61937_MPEG1_LAYER23:
56         *offset = spdif_mpeg_pkt_offset[1][0];
57         *codec = AV_CODEC_ID_MP3;
58         break;
59     case IEC61937_MPEG2_EXT:
60         *offset = 4608;
61         *codec = AV_CODEC_ID_MP3;
62         break;
63     case IEC61937_MPEG2_AAC:
64         ret = av_adts_header_parse(buf, &samples, &frames);
65         if (ret < 0) {
66             if (s) /* be silent during a probe */
67                 av_log(s, AV_LOG_ERROR, "Invalid AAC packet in IEC 61937\n");
68             return ret;
69         }
70         *offset = samples << 2;
71         *codec = AV_CODEC_ID_AAC;
72         break;
73     case IEC61937_MPEG2_LAYER1_LSF:
74         *offset = spdif_mpeg_pkt_offset[0][0];
75         *codec = AV_CODEC_ID_MP1;
76         break;
77     case IEC61937_MPEG2_LAYER2_LSF:
78         *offset = spdif_mpeg_pkt_offset[0][1];
79         *codec = AV_CODEC_ID_MP2;
80         break;
81     case IEC61937_MPEG2_LAYER3_LSF:
82         *offset = spdif_mpeg_pkt_offset[0][2];
83         *codec = AV_CODEC_ID_MP3;
84         break;
85     case IEC61937_DTS1:
86         *offset = 2048;
87         *codec = AV_CODEC_ID_DTS;
88         break;
89     case IEC61937_DTS2:
90         *offset = 4096;
91         *codec = AV_CODEC_ID_DTS;
92         break;
93     case IEC61937_DTS3:
94         *offset = 8192;
95         *codec = AV_CODEC_ID_DTS;
96         break;
97     case IEC61937_EAC3:
98         *offset = 24576;
99         *codec = AV_CODEC_ID_EAC3;
100         break;
101     default:
102         if (s) { /* be silent during a probe */
103             avpriv_request_sample(s, "Data type 0x%04x in IEC 61937",
104                                   data_type);
105         }
106         return AVERROR_PATCHWELCOME;
107     }
108     return 0;
109 }
110
111 /* Largest offset between bursts we currently handle, i.e. AAC with
112    samples = 4096 */
113 #define SPDIF_MAX_OFFSET 16384
114
115 static int spdif_probe(const AVProbeData *p)
116 {
117     enum AVCodecID codec;
118     return ff_spdif_probe (p->buf, p->buf_size, &codec);
119 }
120
121 int ff_spdif_probe(const uint8_t *p_buf, int buf_size, enum AVCodecID *codec)
122 {
123     const uint8_t *buf = p_buf;
124     const uint8_t *probe_end = p_buf + FFMIN(2 * SPDIF_MAX_OFFSET, buf_size - 1);
125     const uint8_t *expected_code = buf + 7;
126     uint32_t state = 0;
127     int sync_codes = 0;
128     int consecutive_codes = 0;
129     int offset;
130
131     for (; buf < probe_end; buf++) {
132         state = (state << 8) | *buf;
133
134         if (state == (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))
135                 && buf[1] < 0x37) {
136             sync_codes++;
137
138             if (buf == expected_code) {
139                 if (++consecutive_codes >= 2)
140                     return AVPROBE_SCORE_MAX;
141             } else
142                 consecutive_codes = 0;
143
144             if (buf + 4 + AV_AAC_ADTS_HEADER_SIZE > p_buf + buf_size)
145                 break;
146
147             /* continue probing to find more sync codes */
148             probe_end = FFMIN(buf + SPDIF_MAX_OFFSET, p_buf + buf_size - 1);
149
150             /* skip directly to the next sync code */
151             if (!spdif_get_offset_and_codec(NULL, (buf[2] << 8) | buf[1],
152                                             &buf[5], &offset, codec)) {
153                 if (buf + offset >= p_buf + buf_size)
154                     break;
155                 expected_code = buf + offset;
156                 buf = expected_code - 7;
157             }
158         }
159     }
160
161     if (!sync_codes)
162         return 0;
163
164     if (sync_codes >= 6)
165         /* good amount of sync codes but with unexpected offsets */
166         return AVPROBE_SCORE_EXTENSION;
167
168     /* some sync codes were found */
169     return AVPROBE_SCORE_EXTENSION / 4;
170 }
171
172 static int spdif_read_header(AVFormatContext *s)
173 {
174     s->ctx_flags |= AVFMTCTX_NOHEADER;
175     return 0;
176 }
177
178 static int spdif_get_pkt_size_bits(int type, int code)
179 {
180     switch (type & 0xff) {
181     case IEC61937_EAC3:
182         return code << 3;
183     default:
184         return code;
185     }
186 }
187
188 int ff_spdif_read_packet(AVFormatContext *s, AVPacket *pkt)
189 {
190     AVIOContext *pb = s->pb;
191     enum IEC61937DataType data_type;
192     enum AVCodecID codec_id;
193     uint32_t state = 0;
194     int pkt_size_bits, offset, ret;
195
196     while (state != (AV_BSWAP16C(SYNCWORD1) << 16 | AV_BSWAP16C(SYNCWORD2))) {
197         state = (state << 8) | avio_r8(pb);
198         if (avio_feof(pb))
199             return AVERROR_EOF;
200     }
201
202     data_type = avio_rl16(pb);
203     pkt_size_bits = spdif_get_pkt_size_bits(data_type, avio_rl16(pb));
204
205     if (pkt_size_bits % 16)
206         avpriv_request_sample(s, "Packet not ending at a 16-bit boundary");
207
208     ret = av_new_packet(pkt, FFALIGN(pkt_size_bits, 16) >> 3);
209     if (ret)
210         return ret;
211
212     pkt->pos = avio_tell(pb) - BURST_HEADER_SIZE;
213
214     if (avio_read(pb, pkt->data, pkt->size) < pkt->size) {
215         return AVERROR_EOF;
216     }
217     ff_spdif_bswap_buf16((uint16_t *)pkt->data, (uint16_t *)pkt->data, pkt->size >> 1);
218
219     ret = spdif_get_offset_and_codec(s, data_type, pkt->data,
220                                      &offset, &codec_id);
221     if (ret < 0) {
222         return ret;
223     }
224
225     /* skip over the padding to the beginning of the next frame */
226     avio_skip(pb, offset - pkt->size - BURST_HEADER_SIZE);
227
228     if (!s->nb_streams) {
229         /* first packet, create a stream */
230         AVStream *st = avformat_new_stream(s, NULL);
231         if (!st) {
232             return AVERROR(ENOMEM);
233         }
234         st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
235         st->codecpar->codec_id = codec_id;
236         if (codec_id == AV_CODEC_ID_EAC3)
237             ffstream(st)->need_parsing = AVSTREAM_PARSE_FULL;
238     } else if (codec_id != s->streams[0]->codecpar->codec_id) {
239         avpriv_report_missing_feature(s, "Codec change in IEC 61937");
240         return AVERROR_PATCHWELCOME;
241     }
242
243     if (!s->bit_rate && s->streams[0]->codecpar->sample_rate)
244         /* stream bitrate matches 16-bit stereo PCM bitrate for currently
245            supported codecs */
246         s->bit_rate = 2 * 16LL * s->streams[0]->codecpar->sample_rate;
247
248     return 0;
249 }
250
251 const AVInputFormat ff_spdif_demuxer = {
252     .name           = "spdif",
253     .long_name      = NULL_IF_CONFIG_SMALL("IEC 61937 (compressed data in S/PDIF)"),
254     .read_probe     = spdif_probe,
255     .read_header    = spdif_read_header,
256     .read_packet    = ff_spdif_read_packet,
257     .flags          = AVFMT_GENERIC_INDEX,
258 };