/src/ffmpeg/libavformat/musx.c
Line | Count | Source |
1 | | /* |
2 | | * MUSX demuxer |
3 | | * Copyright (c) 2016 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/avassert.h" |
23 | | #include "libavutil/intreadwrite.h" |
24 | | #include "avformat.h" |
25 | | #include "demux.h" |
26 | | #include "internal.h" |
27 | | |
28 | | static int musx_probe(const AVProbeData *p) |
29 | 964k | { |
30 | 964k | unsigned version; |
31 | | |
32 | 964k | if (AV_RB32(p->buf) != MKBETAG('M','U','S','X')) |
33 | 963k | return 0; |
34 | | |
35 | 1.19k | version = AV_RL32(p->buf + 8); |
36 | 1.19k | if (version != 10 && |
37 | 1.02k | version != 6 && |
38 | 888 | version != 5 && |
39 | 695 | version != 4 && |
40 | 548 | version != 201) |
41 | 486 | return 0; |
42 | | |
43 | 707 | return AVPROBE_SCORE_MAX / 5 * 2; |
44 | 1.19k | } |
45 | | |
46 | | static int musx_read_header(AVFormatContext *s) |
47 | 2.40k | { |
48 | 2.40k | unsigned type, version, coding, offset; |
49 | 2.40k | AVStream *st; |
50 | | |
51 | 2.40k | avio_skip(s->pb, 8); |
52 | 2.40k | version = avio_rl32(s->pb); |
53 | 2.40k | if (version != 10 && |
54 | 731 | version != 6 && |
55 | 596 | version != 5 && |
56 | 495 | version != 4 && |
57 | 475 | version != 201) { |
58 | 219 | avpriv_request_sample(s, "Unsupported version: %d", version); |
59 | 219 | return AVERROR_PATCHWELCOME; |
60 | 219 | } |
61 | 2.18k | avio_skip(s->pb, 4); |
62 | | |
63 | 2.18k | st = avformat_new_stream(s, NULL); |
64 | 2.18k | if (!st) |
65 | 0 | return AVERROR(ENOMEM); |
66 | | |
67 | 2.18k | if (version == 201) { |
68 | 256 | avio_skip(s->pb, 8); |
69 | 256 | offset = avio_rl32(s->pb); |
70 | 256 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
71 | 256 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX; |
72 | 256 | st->codecpar->ch_layout.nb_channels = 2; |
73 | 256 | st->codecpar->sample_rate = 32000; |
74 | 256 | st->codecpar->block_align = 0x80 * st->codecpar->ch_layout.nb_channels; |
75 | 1.92k | } else if (version == 10) { |
76 | 1.67k | type = avio_rl32(s->pb); |
77 | 1.67k | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
78 | 1.67k | offset = 0x800; |
79 | 1.67k | switch (type) { |
80 | 399 | case MKTAG('P', 'S', '3', '_'): |
81 | 399 | st->codecpar->ch_layout.nb_channels = 2; |
82 | 399 | st->codecpar->sample_rate = 44100; |
83 | 399 | avio_skip(s->pb, 44); |
84 | 399 | coding = avio_rl32(s->pb); |
85 | 399 | if (coding == MKTAG('D', 'A', 'T', '4') || |
86 | 309 | coding == MKTAG('D', 'A', 'T', '8')) { |
87 | 309 | avio_skip(s->pb, 4); |
88 | 309 | st->codecpar->ch_layout.nb_channels = avio_rl32(s->pb); |
89 | 309 | if (st->codecpar->ch_layout.nb_channels <= 0 || |
90 | 276 | st->codecpar->ch_layout.nb_channels > INT_MAX / 0x20) |
91 | 40 | return AVERROR_INVALIDDATA; |
92 | 269 | st->codecpar->sample_rate = avio_rl32(s->pb); |
93 | 269 | } |
94 | 359 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_DAT4; |
95 | 359 | st->codecpar->block_align = 0x20 * st->codecpar->ch_layout.nb_channels; |
96 | 359 | break; |
97 | 1.03k | case MKTAG('W', 'I', 'I', '_'): |
98 | 1.03k | avio_skip(s->pb, 44); |
99 | 1.03k | coding = avio_rl32(s->pb); |
100 | 1.03k | if (coding != MKTAG('D', 'A', 'T', '4') && |
101 | 260 | coding != MKTAG('D', 'A', 'T', '8')) { |
102 | 86 | avpriv_request_sample(s, "Unsupported coding: %X", coding); |
103 | 86 | return AVERROR_PATCHWELCOME; |
104 | 86 | } |
105 | 948 | avio_skip(s->pb, 4); |
106 | 948 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_DAT4; |
107 | 948 | st->codecpar->ch_layout.nb_channels = avio_rl32(s->pb); |
108 | 948 | if (st->codecpar->ch_layout.nb_channels <= 0 || |
109 | 913 | st->codecpar->ch_layout.nb_channels > INT_MAX / 0x20) |
110 | 42 | return AVERROR_INVALIDDATA; |
111 | 906 | st->codecpar->sample_rate = avio_rl32(s->pb); |
112 | 906 | st->codecpar->block_align = 0x20 * st->codecpar->ch_layout.nb_channels; |
113 | 906 | break; |
114 | 6 | case MKTAG('X', 'E', '_', '_'): |
115 | 6 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_DAT4; |
116 | 6 | st->codecpar->ch_layout.nb_channels = 2; |
117 | 6 | st->codecpar->sample_rate = 32000; |
118 | 6 | st->codecpar->block_align = 0x20 * st->codecpar->ch_layout.nb_channels; |
119 | 6 | break; |
120 | 2 | case MKTAG('P', 'S', 'P', '_'): |
121 | 2 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX; |
122 | 2 | st->codecpar->ch_layout.nb_channels = 2; |
123 | 2 | st->codecpar->sample_rate = 32768; |
124 | 2 | st->codecpar->block_align = 0x80 * st->codecpar->ch_layout.nb_channels; |
125 | 2 | break; |
126 | 1 | case MKTAG('P', 'S', '2', '_'): |
127 | 1 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX; |
128 | 1 | st->codecpar->ch_layout.nb_channels = 2; |
129 | 1 | st->codecpar->sample_rate = 32000; |
130 | 1 | st->codecpar->block_align = 0x80 * st->codecpar->ch_layout.nb_channels; |
131 | 1 | break; |
132 | 229 | default: |
133 | 229 | avpriv_request_sample(s, "Unsupported type: %X", type); |
134 | 229 | return AVERROR_PATCHWELCOME; |
135 | 1.67k | } |
136 | 1.67k | } else if (version == 6 || version == 5 || version == 4) { |
137 | 256 | type = avio_rl32(s->pb); |
138 | 256 | avio_skip(s->pb, 20); |
139 | 256 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; |
140 | 256 | st->codecpar->ch_layout.nb_channels = 2; |
141 | 256 | switch (type) { |
142 | 69 | case MKTAG('G', 'C', '_', '_'): |
143 | 69 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_DAT4; |
144 | 69 | st->codecpar->block_align = 0x20 * st->codecpar->ch_layout.nb_channels; |
145 | 69 | st->codecpar->sample_rate = 32000; |
146 | 69 | offset = avio_rb32(s->pb); |
147 | 69 | break; |
148 | 1 | case MKTAG('P', 'S', '2', '_'): |
149 | 1 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_PSX; |
150 | 1 | st->codecpar->block_align = 0x80 * st->codecpar->ch_layout.nb_channels; |
151 | 1 | st->codecpar->sample_rate = 32000; |
152 | 1 | offset = avio_rl32(s->pb); |
153 | 1 | break; |
154 | 35 | case MKTAG('X', 'B', '_', '_'): |
155 | 35 | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_IMA_DAT4; |
156 | 35 | st->codecpar->block_align = 0x20 * st->codecpar->ch_layout.nb_channels; |
157 | 35 | st->codecpar->sample_rate = 44100; |
158 | 35 | offset = avio_rl32(s->pb); |
159 | 35 | break; |
160 | 151 | default: |
161 | 151 | avpriv_request_sample(s, "Unsupported type: %X", type); |
162 | 151 | return AVERROR_PATCHWELCOME; |
163 | 256 | } |
164 | 256 | } else { |
165 | 0 | av_assert0(0); |
166 | 0 | } |
167 | | |
168 | 1.63k | avio_seek(s->pb, offset, SEEK_SET); |
169 | | |
170 | 1.63k | avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate); |
171 | | |
172 | 1.63k | return 0; |
173 | 2.18k | } |
174 | | |
175 | | static int musx_read_packet(AVFormatContext *s, AVPacket *pkt) |
176 | 223k | { |
177 | 223k | AVCodecParameters *par = s->streams[0]->codecpar; |
178 | | |
179 | 223k | return av_get_packet(s->pb, pkt, par->block_align); |
180 | 223k | } |
181 | | |
182 | | const FFInputFormat ff_musx_demuxer = { |
183 | | .p.name = "musx", |
184 | | .p.long_name = NULL_IF_CONFIG_SMALL("Eurocom MUSX"), |
185 | | .p.extensions = "musx", |
186 | | .read_probe = musx_probe, |
187 | | .read_header = musx_read_header, |
188 | | .read_packet = musx_read_packet, |
189 | | }; |