Browse code

avformat: add aix demuxer

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

Paul B Mahol authored on 2016/04/05 21:05:10
Showing 5 changed files
... ...
@@ -20,6 +20,7 @@ version <next>:
20 20
 - bitstream filter for extracting DTS core
21 21
 - ADPCM IMA DAT4 decoder
22 22
 - musx demuxer
23
+- aix demuxer
23 24
 
24 25
 version 3.0:
25 26
 - Common Encryption (CENC) MP4 encoding and decoding support
... ...
@@ -82,6 +82,7 @@ OBJS-$(CONFIG_AFC_DEMUXER)               += afc.o
82 82
 OBJS-$(CONFIG_AIFF_DEMUXER)              += aiffdec.o pcm.o isom.o \
83 83
                                             mov_chan.o
84 84
 OBJS-$(CONFIG_AIFF_MUXER)                += aiffenc.o isom.o id3v2enc.o
85
+OBJS-$(CONFIG_AIX_DEMUXER)               += aixdec.o
85 86
 OBJS-$(CONFIG_AMR_DEMUXER)               += amr.o
86 87
 OBJS-$(CONFIG_AMR_MUXER)                 += amr.o
87 88
 OBJS-$(CONFIG_ANM_DEMUXER)               += anm.o
88 89
new file mode 100644
... ...
@@ -0,0 +1,140 @@
0
+/*
1
+ * AIX 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/intreadwrite.h"
22
+#include "avformat.h"
23
+#include "internal.h"
24
+
25
+static int aix_probe(AVProbeData *p)
26
+{
27
+    if (AV_RL32(p->buf) != MKTAG('A','I','X','F') ||
28
+        AV_RB32(p->buf +  8) != 0x01000014 ||
29
+        AV_RB32(p->buf + 12) != 0x00000800)
30
+        return 0;
31
+
32
+    return AVPROBE_SCORE_MAX;
33
+}
34
+
35
+static int aix_read_header(AVFormatContext *s)
36
+{
37
+    unsigned nb_streams, first_offset, nb_segments;
38
+    unsigned stream_list_offset;
39
+    unsigned segment_list_offset = 0x20;
40
+    unsigned segment_list_entry_size = 0x10;
41
+    unsigned size;
42
+    int i;
43
+
44
+    avio_skip(s->pb, 4);
45
+    first_offset = avio_rb32(s->pb) + 8;
46
+    avio_skip(s->pb, 16);
47
+    nb_segments = avio_rb16(s->pb);
48
+    if (nb_segments == 0)
49
+        return AVERROR_INVALIDDATA;
50
+    stream_list_offset = segment_list_offset + segment_list_entry_size * nb_segments + 0x10;
51
+    if (stream_list_offset >= first_offset)
52
+        return AVERROR_INVALIDDATA;
53
+    avio_seek(s->pb, stream_list_offset, SEEK_SET);
54
+    nb_streams = avio_r8(s->pb);
55
+    if (nb_streams == 0)
56
+        return AVERROR_INVALIDDATA;
57
+    avio_skip(s->pb, 7);
58
+    for (i = 0; i < nb_streams; i++) {
59
+        AVStream *st = avformat_new_stream(s, NULL);
60
+
61
+        if (!st)
62
+            return AVERROR(ENOMEM);
63
+        st->codec->codec_type  = AVMEDIA_TYPE_AUDIO;
64
+        st->codec->codec_id    = AV_CODEC_ID_ADPCM_ADX;
65
+        st->codec->sample_rate = avio_rb32(s->pb);
66
+        st->codec->channels    = avio_r8(s->pb);
67
+        avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
68
+        avio_skip(s->pb, 3);
69
+    }
70
+
71
+    avio_seek(s->pb, first_offset, SEEK_SET);
72
+    for (i = 0; i < nb_streams; i++) {
73
+        if (avio_rl32(s->pb) != MKTAG('A','I','X','P'))
74
+            return AVERROR_INVALIDDATA;
75
+        size = avio_rb32(s->pb);
76
+        if (size <= 8)
77
+            return AVERROR_INVALIDDATA;
78
+        avio_skip(s->pb, 8);
79
+        ff_get_extradata(s->streams[i]->codec, s->pb, size - 8);
80
+    }
81
+
82
+    return 0;
83
+}
84
+
85
+static int aix_read_packet(AVFormatContext *s, AVPacket *pkt)
86
+{
87
+    unsigned size, index, duration, chunk;
88
+    int64_t pos;
89
+    int sequence, ret, i;
90
+
91
+    pos = avio_tell(s->pb);
92
+    if (avio_feof(s->pb))
93
+        return AVERROR_EOF;
94
+    chunk = avio_rl32(s->pb);
95
+    size = avio_rb32(s->pb);
96
+    if (chunk == MKTAG('A','I','X','E')) {
97
+        avio_skip(s->pb, size);
98
+        for (i = 0; i < s->nb_streams; i++) {
99
+            if (avio_feof(s->pb))
100
+                return AVERROR_EOF;
101
+            chunk = avio_rl32(s->pb);
102
+            size = avio_rb32(s->pb);
103
+            avio_skip(s->pb, size);
104
+        }
105
+        pos = avio_tell(s->pb);
106
+        chunk = avio_rl32(s->pb);
107
+        size = avio_rb32(s->pb);
108
+    }
109
+
110
+    if (chunk != MKTAG('A','I','X','P'))
111
+        return AVERROR_INVALIDDATA;
112
+    if (size <= 8)
113
+        return AVERROR_INVALIDDATA;
114
+    index = avio_r8(s->pb);
115
+    if (avio_r8(s->pb) != s->nb_streams || index >= s->nb_streams)
116
+        return AVERROR_INVALIDDATA;
117
+    duration = avio_rb16(s->pb);
118
+    sequence = avio_rb32(s->pb);
119
+    if (sequence < 0) {
120
+        avio_skip(s->pb, size - 8);
121
+        return 0;
122
+    }
123
+
124
+    ret = av_get_packet(s->pb, pkt, size - 8);
125
+    pkt->stream_index = index;
126
+    pkt->duration = duration;
127
+    pkt->pos = pos;
128
+    return ret;
129
+}
130
+
131
+AVInputFormat ff_aix_demuxer = {
132
+    .name        = "aix",
133
+    .long_name   = NULL_IF_CONFIG_SMALL("CRI AIX"),
134
+    .read_probe  = aix_probe,
135
+    .read_header = aix_read_header,
136
+    .read_packet = aix_read_packet,
137
+    .extensions  = "aix",
138
+    .flags       = AVFMT_GENERIC_INDEX,
139
+};
... ...
@@ -66,6 +66,7 @@ void av_register_all(void)
66 66
     REGISTER_DEMUXER (AEA,              aea);
67 67
     REGISTER_DEMUXER (AFC,              afc);
68 68
     REGISTER_MUXDEMUX(AIFF,             aiff);
69
+    REGISTER_DEMUXER (AIX,              aix);
69 70
     REGISTER_MUXDEMUX(AMR,              amr);
70 71
     REGISTER_DEMUXER (ANM,              anm);
71 72
     REGISTER_DEMUXER (APC,              apc);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFORMAT_VERSION_MAJOR  57
33
-#define LIBAVFORMAT_VERSION_MINOR  30
33
+#define LIBAVFORMAT_VERSION_MINOR  31
34 34
 #define LIBAVFORMAT_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \