3 * Copyright (c) 2015 Paul B Mahol
5 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/intreadwrite.h"
26 static int threedostr_probe(const AVProbeData *p)
28 for (int i = 0; i < p->buf_size;) {
29 unsigned chunk = AV_RL32(p->buf + i);
30 unsigned size = AV_RB32(p->buf + i + 4);
32 if (size < 8 || p->buf_size - i < size)
37 case MKTAG('C','T','R','L'):
39 case MKTAG('S','N','D','S'):
43 if (AV_RL32(p->buf + i) != MKTAG('S','H','D','R'))
47 if (AV_RB32(p->buf + i) <= 0)
50 if (AV_RB32(p->buf + i) <= 0)
53 if (AV_RL32(p->buf + i) == MKTAG('S','D','X','2'))
54 return AVPROBE_SCORE_MAX;
58 case MKTAG('S','H','D','R'):
74 static int threedostr_read_header(AVFormatContext *s)
76 unsigned chunk, codec = 0, size, ctrl_size = -1, found_shdr = 0;
79 while (!avio_feof(s->pb) && !found_shdr) {
80 chunk = avio_rl32(s->pb);
81 size = avio_rb32(s->pb);
84 return AVERROR_INVALIDDATA;
88 case MKTAG('C','T','R','L'):
91 case MKTAG('S','N','D','S'):
93 return AVERROR_INVALIDDATA;
95 if (avio_rl32(s->pb) != MKTAG('S','H','D','R'))
96 return AVERROR_INVALIDDATA;
99 st = avformat_new_stream(s, NULL);
101 return AVERROR(ENOMEM);
103 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
104 st->codecpar->sample_rate = avio_rb32(s->pb);
105 st->codecpar->channels = avio_rb32(s->pb);
106 if (st->codecpar->channels <= 0 || st->codecpar->sample_rate <= 0)
107 return AVERROR_INVALIDDATA;
108 codec = avio_rl32(s->pb);
110 if (ctrl_size == 20 || ctrl_size == 3 || ctrl_size == -1)
111 st->duration = (avio_rb32(s->pb) - 1) / st->codecpar->channels;
113 st->duration = avio_rb32(s->pb) * 16 / st->codecpar->channels;
117 case MKTAG('S','H','D','R'):
119 avio_skip(s->pb, 0x74);
121 if (avio_rl32(s->pb) == MKTAG('C','T','R','L') && size > 4) {
122 ctrl_size = avio_rb32(s->pb);
128 av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
132 avio_skip(s->pb, size);
136 case MKTAG('S','D','X','2'):
137 st->codecpar->codec_id = AV_CODEC_ID_SDX2_DPCM;
138 st->codecpar->block_align = 1 * st->codecpar->channels;
141 avpriv_request_sample(s, "codec %X", codec);
142 return AVERROR_PATCHWELCOME;
145 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
150 static int threedostr_read_packet(AVFormatContext *s, AVPacket *pkt)
152 unsigned chunk, size;
153 AVStream *st = s->streams[0];
157 while (!avio_feof(s->pb)) {
158 pos = avio_tell(s->pb);
159 chunk = avio_rl32(s->pb);
160 size = avio_rb32(s->pb);
166 return AVERROR_INVALIDDATA;
170 case MKTAG('S','N','D','S'):
172 return AVERROR_INVALIDDATA;
174 if (avio_rl32(s->pb) != MKTAG('S','S','M','P'))
175 return AVERROR_INVALIDDATA;
178 ret = av_get_packet(s->pb, pkt, size);
180 pkt->stream_index = 0;
181 pkt->duration = size / st->codecpar->channels;
184 av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
188 avio_skip(s->pb, size);
194 AVInputFormat ff_threedostr_demuxer = {
196 .long_name = NULL_IF_CONFIG_SMALL("3DO STR"),
197 .read_probe = threedostr_probe,
198 .read_header = threedostr_read_header,
199 .read_packet = threedostr_read_packet,
201 .flags = AVFMT_GENERIC_INDEX,