Browse code

avformat: add musx demuxer

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2016/04/03 19:10:09
Showing 5 changed files
... ...
@@ -19,6 +19,7 @@ version <next>:
19 19
 - libdcadec removed
20 20
 - bitstream filter for extracting DTS core
21 21
 - ADPCM IMA DAT4 decoder
22
+- musx demuxer
22 23
 
23 24
 version 3.0:
24 25
 - Common Encryption (CENC) MP4 encoding and decoding support
... ...
@@ -296,6 +296,7 @@ OBJS-$(CONFIG_MSF_DEMUXER)               += msf.o
296 296
 OBJS-$(CONFIG_MPSUB_DEMUXER)             += mpsubdec.o subtitles.o
297 297
 OBJS-$(CONFIG_MSNWC_TCP_DEMUXER)         += msnwc_tcp.o
298 298
 OBJS-$(CONFIG_MTV_DEMUXER)               += mtv.o
299
+OBJS-$(CONFIG_MUSX_DEMUXER)              += musx.o
299 300
 OBJS-$(CONFIG_MV_DEMUXER)                += mvdec.o
300 301
 OBJS-$(CONFIG_MVI_DEMUXER)               += mvi.o
301 302
 OBJS-$(CONFIG_MXF_DEMUXER)               += mxfdec.o mxf.o
... ...
@@ -207,6 +207,7 @@ void av_register_all(void)
207 207
     REGISTER_DEMUXER (MSF,              msf);
208 208
     REGISTER_DEMUXER (MSNWC_TCP,        msnwc_tcp);
209 209
     REGISTER_DEMUXER (MTV,              mtv);
210
+    REGISTER_DEMUXER (MUSX,             musx);
210 211
     REGISTER_DEMUXER (MV,               mv);
211 212
     REGISTER_DEMUXER (MVI,              mvi);
212 213
     REGISTER_MUXDEMUX(MXF,              mxf);
213 214
new file mode 100644
... ...
@@ -0,0 +1,177 @@
0
+/*
1
+ * MUSX demuxer
2
+ * Copyright (c) 2016 Paul B Mahol
3
+ *
4
+ * This file is part of FFmpeg.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or
7
+ * modify it under the terms of the GNU Lesser General Public
8
+ * License as published by the Free Software Foundation; either
9
+ * version 2.1 of the License, or (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
+ * Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public
17
+ * License along with FFmpeg; if not, write to the Free Software
18
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
+ */
20
+
21
+#include "libavutil/avassert.h"
22
+#include "avformat.h"
23
+#include "internal.h"
24
+
25
+static int musx_probe(AVProbeData *p)
26
+{
27
+    if (memcmp(p->buf, "MUSX", 4))
28
+        return 0;
29
+
30
+    return AVPROBE_SCORE_MAX / 5 * 2;
31
+}
32
+
33
+static int musx_read_header(AVFormatContext *s)
34
+{
35
+    unsigned type, version, coding, offset;
36
+    AVStream *st;
37
+
38
+    avio_skip(s->pb, 8);
39
+    version = avio_rl32(s->pb);
40
+    if (version != 10 &&
41
+        version != 6 &&
42
+        version != 5 &&
43
+        version != 4 &&
44
+        version != 201) {
45
+        avpriv_request_sample(s, "Unsupported version: %d", version);
46
+        return AVERROR_PATCHWELCOME;
47
+    }
48
+    avio_skip(s->pb, 4);
49
+
50
+    st = avformat_new_stream(s, NULL);
51
+    if (!st)
52
+        return AVERROR(ENOMEM);
53
+
54
+    if (version == 201) {
55
+        avio_skip(s->pb, 8);
56
+        offset = avio_rl32(s->pb);
57
+        st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
58
+        st->codec->codec_id    = AV_CODEC_ID_ADPCM_PSX;
59
+        st->codec->channels    = 2;
60
+        st->codec->sample_rate = 32000;
61
+        st->codec->block_align = 0x80 * st->codec->channels;
62
+    }  else if (version == 10) {
63
+        type = avio_rl32(s->pb);
64
+        st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
65
+        offset = 0x800;
66
+        switch (type) {
67
+        case MKTAG('P', 'S', '3', '_'):
68
+            st->codec->channels    = 2;
69
+            st->codec->sample_rate = 44100;
70
+            avio_skip(s->pb, 44);
71
+            coding = avio_rl32(s->pb);
72
+            if (coding == MKTAG('D', 'A', 'T', '4') ||
73
+                coding == MKTAG('D', 'A', 'T', '8')) {
74
+                avio_skip(s->pb, 4);
75
+                st->codec->channels   = avio_rl32(s->pb);
76
+                if (st->codec->channels <= 0 ||
77
+                    st->codec->channels > INT_MAX / 0x20)
78
+                    return AVERROR_INVALIDDATA;
79
+                st->codec->sample_rate = avio_rl32(s->pb);
80
+            }
81
+            st->codec->codec_id   = AV_CODEC_ID_ADPCM_IMA_DAT4;
82
+            st->codec->block_align = 0x20 * st->codec->channels;
83
+            break;
84
+        case MKTAG('W', 'I', 'I', '_'):
85
+            avio_skip(s->pb, 44);
86
+            coding = avio_rl32(s->pb);
87
+            if (coding != MKTAG('D', 'A', 'T', '4') &&
88
+                coding != MKTAG('D', 'A', 'T', '8')) {
89
+                avpriv_request_sample(s, "Unsupported coding: %X", coding);
90
+                return AVERROR_PATCHWELCOME;
91
+            }
92
+            avio_skip(s->pb, 4);
93
+            st->codec->codec_id   = AV_CODEC_ID_ADPCM_IMA_DAT4;
94
+            st->codec->channels   = avio_rl32(s->pb);
95
+            if (st->codec->channels <= 0 ||
96
+                st->codec->channels > INT_MAX / 0x20)
97
+                return AVERROR_INVALIDDATA;
98
+            st->codec->sample_rate = avio_rl32(s->pb);
99
+            st->codec->block_align = 0x20 * st->codec->channels;
100
+            break;
101
+        case MKTAG('X', 'E', '_', '_'):
102
+            st->codec->codec_id    = AV_CODEC_ID_ADPCM_IMA_DAT4;
103
+            st->codec->channels    = 2;
104
+            st->codec->sample_rate = 32000;
105
+            st->codec->block_align = 0x20 * st->codec->channels;
106
+            break;
107
+        case MKTAG('P', 'S', 'P', '_'):
108
+            st->codec->codec_id    = AV_CODEC_ID_ADPCM_PSX;
109
+            st->codec->channels    = 2;
110
+            st->codec->sample_rate = 32768;
111
+            st->codec->block_align = 0x80 * st->codec->channels;
112
+            break;
113
+        case MKTAG('P', 'S', '2', '_'):
114
+            st->codec->codec_id    = AV_CODEC_ID_ADPCM_PSX;
115
+            st->codec->channels    = 2;
116
+            st->codec->sample_rate = 32000;
117
+            st->codec->block_align = 0x80 * st->codec->channels;
118
+            break;
119
+        default:
120
+            avpriv_request_sample(s, "Unsupported type: %X", type);
121
+            return AVERROR_PATCHWELCOME;
122
+        }
123
+    } else if (version == 6 || version == 5 || version == 4) {
124
+        type = avio_rl32(s->pb);
125
+        avio_skip(s->pb, 20);
126
+        st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
127
+        st->codec->channels    = 2;
128
+        switch (type) {
129
+        case MKTAG('G', 'C', '_', '_'):
130
+            st->codec->codec_id    = AV_CODEC_ID_ADPCM_IMA_DAT4;
131
+            st->codec->block_align = 0x20 * st->codec->channels;
132
+            st->codec->sample_rate = 32000;
133
+            offset = avio_rb32(s->pb);
134
+            break;
135
+        case MKTAG('P', 'S', '2', '_'):
136
+            st->codec->codec_id    = AV_CODEC_ID_ADPCM_PSX;
137
+            st->codec->block_align = 0x80 * st->codec->channels;
138
+            st->codec->sample_rate = 32000;
139
+            offset = avio_rl32(s->pb);
140
+            break;
141
+        case MKTAG('X', 'B', '_', '_'):
142
+            st->codec->codec_id    = AV_CODEC_ID_ADPCM_IMA_DAT4;
143
+            st->codec->block_align = 0x20 * st->codec->channels;
144
+            st->codec->sample_rate = 44100;
145
+            offset = avio_rl32(s->pb);
146
+            break;
147
+        default:
148
+            avpriv_request_sample(s, "Unsupported type: %X", type);
149
+            return AVERROR_PATCHWELCOME;
150
+        }
151
+    } else {
152
+        av_assert0(0);
153
+    }
154
+
155
+    avio_seek(s->pb, offset, SEEK_SET);
156
+
157
+    avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
158
+
159
+    return 0;
160
+}
161
+
162
+static int musx_read_packet(AVFormatContext *s, AVPacket *pkt)
163
+{
164
+    AVCodecContext *codec = s->streams[0]->codec;
165
+
166
+    return av_get_packet(s->pb, pkt, codec->block_align);
167
+}
168
+
169
+AVInputFormat ff_musx_demuxer = {
170
+    .name           = "musx",
171
+    .long_name      = NULL_IF_CONFIG_SMALL("Eurocom MUSX"),
172
+    .read_probe     = musx_probe,
173
+    .read_header    = musx_read_header,
174
+    .read_packet    = musx_read_packet,
175
+    .extensions     = "musx",
176
+};
... ...
@@ -30,8 +30,8 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR  57
33
-#define LIBAVFORMAT_VERSION_MINOR  29
34
-#define LIBAVFORMAT_VERSION_MICRO 101
33
+#define LIBAVFORMAT_VERSION_MINOR  30
34
+#define LIBAVFORMAT_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
37 37
                                                LIBAVFORMAT_VERSION_MINOR, \