1 /*
2 * 3DO STR demuxer
3 * Copyright (c) 2015 Paul B Mahol
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 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25
threedostr_probe(const AVProbeData * p)26 static int threedostr_probe(const AVProbeData *p)
27 {
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);
31
32 if (size < 8 || p->buf_size - i < size)
33 return 0;
34 i += 8;
35 size -= 8;
36 switch (chunk) {
37 case MKTAG('C','T','R','L'):
38 break;
39 case MKTAG('S','N','D','S'):
40 if (size < 56)
41 return 0;
42 i += 8;
43 if (AV_RL32(p->buf + i) != MKTAG('S','H','D','R'))
44 return 0;
45 i += 28;
46
47 if (AV_RB32(p->buf + i) <= 0)
48 return 0;
49 i += 4;
50 if (AV_RB32(p->buf + i) <= 0)
51 return 0;
52 i += 4;
53 if (AV_RL32(p->buf + i) == MKTAG('S','D','X','2'))
54 return AVPROBE_SCORE_MAX;
55 else
56 return 0;
57 break;
58 case MKTAG('S','H','D','R'):
59 if (size > 0x78) {
60 i += 0x78;
61 size -= 0x78;
62 }
63 break;
64 default:
65 break;
66 }
67
68 i += size;
69 }
70
71 return 0;
72 }
73
threedostr_read_header(AVFormatContext * s)74 static int threedostr_read_header(AVFormatContext *s)
75 {
76 unsigned chunk, codec = 0, size, ctrl_size = -1, found_shdr = 0;
77 AVStream *st;
78
79 while (!avio_feof(s->pb) && !found_shdr) {
80 chunk = avio_rl32(s->pb);
81 size = avio_rb32(s->pb);
82
83 if (size < 8)
84 return AVERROR_INVALIDDATA;
85 size -= 8;
86
87 switch (chunk) {
88 case MKTAG('C','T','R','L'):
89 ctrl_size = size;
90 break;
91 case MKTAG('S','N','D','S'):
92 if (size < 56)
93 return AVERROR_INVALIDDATA;
94 avio_skip(s->pb, 8);
95 if (avio_rl32(s->pb) != MKTAG('S','H','D','R'))
96 return AVERROR_INVALIDDATA;
97 avio_skip(s->pb, 24);
98
99 st = avformat_new_stream(s, NULL);
100 if (!st)
101 return AVERROR(ENOMEM);
102
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);
109 avio_skip(s->pb, 4);
110 if (ctrl_size == 20 || ctrl_size == 3 || ctrl_size == -1)
111 st->duration = (avio_rb32(s->pb) - 1) / st->codecpar->channels;
112 else
113 st->duration = avio_rb32(s->pb) * 16 / st->codecpar->channels;
114 size -= 56;
115 found_shdr = 1;
116 break;
117 case MKTAG('S','H','D','R'):
118 if (size > 0x78) {
119 avio_skip(s->pb, 0x74);
120 size -= 0x78;
121 if (avio_rl32(s->pb) == MKTAG('C','T','R','L') && size > 4) {
122 ctrl_size = avio_rb32(s->pb);
123 size -= 4;
124 }
125 }
126 break;
127 default:
128 av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
129 break;
130 }
131
132 avio_skip(s->pb, size);
133 }
134
135 switch (codec) {
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;
139 break;
140 default:
141 avpriv_request_sample(s, "codec %X", codec);
142 return AVERROR_PATCHWELCOME;
143 }
144
145 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
146
147 return 0;
148 }
149
threedostr_read_packet(AVFormatContext * s,AVPacket * pkt)150 static int threedostr_read_packet(AVFormatContext *s, AVPacket *pkt)
151 {
152 unsigned chunk, size;
153 AVStream *st = s->streams[0];
154 int64_t pos;
155 int ret = 0;
156
157 while (!avio_feof(s->pb)) {
158 pos = avio_tell(s->pb);
159 chunk = avio_rl32(s->pb);
160 size = avio_rb32(s->pb);
161
162 if (!size)
163 continue;
164
165 if (size < 8)
166 return AVERROR_INVALIDDATA;
167 size -= 8;
168
169 switch (chunk) {
170 case MKTAG('S','N','D','S'):
171 if (size <= 16)
172 return AVERROR_INVALIDDATA;
173 avio_skip(s->pb, 8);
174 if (avio_rl32(s->pb) != MKTAG('S','S','M','P'))
175 return AVERROR_INVALIDDATA;
176 avio_skip(s->pb, 4);
177 size -= 16;
178 ret = av_get_packet(s->pb, pkt, size);
179 pkt->pos = pos;
180 pkt->stream_index = 0;
181 pkt->duration = size / st->codecpar->channels;
182 return ret;
183 default:
184 av_log(s, AV_LOG_DEBUG, "skipping unknown chunk: %X\n", chunk);
185 break;
186 }
187
188 avio_skip(s->pb, size);
189 }
190
191 return AVERROR_EOF;
192 }
193
194 AVInputFormat ff_threedostr_demuxer = {
195 .name = "3dostr",
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,
200 .extensions = "str",
201 .flags = AVFMT_GENERIC_INDEX,
202 };
203