Browse code

Add QCP demuxer.

Originally committed as revision 18883 to svn://svn.ffmpeg.org/ffmpeg/trunk

Kenan Gillet authored on 2009/05/21 02:19:39
Showing 6 changed files
... ...
@@ -19,6 +19,7 @@ version <next>:
19 19
 - per-stream language-tags extraction in asfdec
20 20
 - V210 decoder and encoder
21 21
 - remaining GPL parts in AC-3 decoder converted to LGPL
22
+- QCP demuxer
22 23
 
23 24
 
24 25
 
... ...
@@ -139,6 +139,7 @@ library:
139 139
 @item Ogg                       @tab X @tab X
140 140
 @item TechnoTrend PVA           @tab   @tab X
141 141
     @tab Used by TechnoTrend DVB PCI boards.
142
+@item QCP                       @tab   @tab X
142 143
 @item raw ADTS (AAC)            @tab X @tab X
143 144
 @item raw AC-3                  @tab X @tab X
144 145
 @item raw Chinese AVS video     @tab   @tab X
... ...
@@ -177,6 +177,7 @@ OBJS-$(CONFIG_PCM_U8_DEMUXER)            += raw.o
177 177
 OBJS-$(CONFIG_PCM_U8_MUXER)              += raw.o
178 178
 OBJS-$(CONFIG_PSP_MUXER)                 += movenc.o riff.o isom.o avc.o
179 179
 OBJS-$(CONFIG_PVA_DEMUXER)               += pva.o
180
+OBJS-$(CONFIG_QCP_DEMUXER)               += qcp.o
180 181
 OBJS-$(CONFIG_R3D_DEMUXER)               += r3d.o
181 182
 OBJS-$(CONFIG_RAWVIDEO_DEMUXER)          += raw.o
182 183
 OBJS-$(CONFIG_RAWVIDEO_MUXER)            += raw.o
... ...
@@ -157,6 +157,7 @@ void av_register_all(void)
157 157
     REGISTER_MUXDEMUX (PCM_U8,    pcm_u8);
158 158
     REGISTER_MUXER    (PSP, psp);
159 159
     REGISTER_DEMUXER  (PVA, pva);
160
+    REGISTER_DEMUXER  (QCP, qcp);
160 161
     REGISTER_DEMUXER  (R3D, r3d);
161 162
     REGISTER_MUXDEMUX (RAWVIDEO, rawvideo);
162 163
     REGISTER_DEMUXER  (REDIR, redir);
... ...
@@ -22,7 +22,7 @@
22 22
 #define AVFORMAT_AVFORMAT_H
23 23
 
24 24
 #define LIBAVFORMAT_VERSION_MAJOR 52
25
-#define LIBAVFORMAT_VERSION_MINOR 32
25
+#define LIBAVFORMAT_VERSION_MINOR 33
26 26
 #define LIBAVFORMAT_VERSION_MICRO  0
27 27
 
28 28
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
29 29
new file mode 100644
... ...
@@ -0,0 +1,197 @@
0
+/*
1
+ * QCP format (.qcp) demuxer
2
+ * Copyright (c) 2009 Kenan Gillet
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
+/**
22
+ * @file libavformat/qcp.c
23
+ * QCP format (.qcp) demuxer
24
+ * @author Kenan Gillet
25
+ * @sa RFC 3625: "The QCP File Format and Media Types for Speech Data"
26
+ *     http://tools.ietf.org/html/rfc3625
27
+ */
28
+
29
+#include "libavutil/intreadwrite.h"
30
+#include "avformat.h"
31
+
32
+typedef struct {
33
+    uint32_t data_size;                     ///< size of data chunk
34
+
35
+#define QCP_MAX_MODE 4
36
+    int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding
37
+                                            ///< to each mode, -1 if no size.
38
+} QCPContext;
39
+
40
+/**
41
+ * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
42
+ * the first byte of the GUID can be either 0x41 or 0x42.
43
+ */
44
+static const uint8_t guid_qcelp_13k_part[15] = {
45
+    0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba,
46
+    0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e
47
+};
48
+
49
+/**
50
+ * EVRC GUID as stored in the file
51
+ */
52
+static const uint8_t guid_evrc[16] = {
53
+    0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46,
54
+    0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4
55
+};
56
+
57
+/**
58
+ * SMV GUID as stored in the file
59
+ */
60
+static const uint8_t guid_smv[16] = {
61
+    0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed,
62
+    0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84
63
+};
64
+
65
+/**
66
+ * @param guid contains at least 16 bytes
67
+ * @return 1 if the guid is a qcelp_13k guid, 0 otherwise
68
+ */
69
+static int is_qcelp_13k_guid(const uint8_t *guid) {
70
+    return (guid[0] == 0x41 || guid[0] == 0x42)
71
+        && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part));
72
+}
73
+
74
+static int qcp_probe(AVProbeData *pd)
75
+{
76
+    if (AV_RL32(pd->buf  ) == AV_RL32("RIFF") &&
77
+        AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt "))
78
+        return AVPROBE_SCORE_MAX;
79
+    return 0;
80
+}
81
+
82
+static int qcp_read_header(AVFormatContext *s, AVFormatParameters *ap)
83
+{
84
+    ByteIOContext *pb = s->pb;
85
+    QCPContext    *c  = s->priv_data;
86
+    AVStream      *st = av_new_stream(s, 0);
87
+    uint8_t       buf[16];
88
+    int           i, nb_rates;
89
+
90
+    if (!st)
91
+        return AVERROR(ENOMEM);
92
+
93
+    get_be32(pb);                    // "RIFF"
94
+    s->file_size = get_le32(pb) + 8;
95
+    url_fskip(pb, 8 + 4 + 1 + 1);    // "QLCMfmt " + chunk-size + major-version + minor-version
96
+
97
+    st->codec->codec_type = CODEC_TYPE_AUDIO;
98
+    st->codec->channels   = 1;
99
+    get_buffer(pb, buf, 16);
100
+    if (is_qcelp_13k_guid(buf)) {
101
+        st->codec->codec_id = CODEC_ID_QCELP;
102
+    } else if (!memcmp(buf, guid_evrc, 16)) {
103
+        av_log(s, AV_LOG_ERROR, "EVRC codec is not supported.\n");
104
+        return AVERROR_PATCHWELCOME;
105
+    } else if (!memcmp(buf, guid_smv, 16)) {
106
+        av_log(s, AV_LOG_ERROR, "SMV codec is not supported.\n");
107
+        return AVERROR_PATCHWELCOME;
108
+    } else {
109
+        av_log(s, AV_LOG_ERROR, "Unknown codec GUID.\n");
110
+        return AVERROR_INVALIDDATA;
111
+    }
112
+    url_fskip(pb, 2 + 80); // codec-version + codec-name
113
+    st->codec->bit_rate = get_le16(pb);
114
+
115
+    s->packet_size = get_le16(pb);
116
+    url_fskip(pb, 2); // block-size
117
+    st->codec->sample_rate = get_le16(pb);
118
+    url_fskip(pb, 2); // sample-size
119
+
120
+    memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode));
121
+    nb_rates = get_le32(pb);
122
+    nb_rates = FFMIN(nb_rates, 8);
123
+    for (i=0; i<nb_rates; i++) {
124
+        int size = get_byte(pb);
125
+        int mode = get_byte(pb);
126
+        if (mode > QCP_MAX_MODE) {
127
+            av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
128
+        } else
129
+            c->rates_per_mode[mode] = size;
130
+    }
131
+    url_fskip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved
132
+
133
+    return 0;
134
+}
135
+
136
+static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt)
137
+{
138
+    ByteIOContext *pb = s->pb;
139
+    QCPContext    *c  = s->priv_data;
140
+    unsigned int  chunk_size, tag;
141
+
142
+    while(!url_feof(pb)) {
143
+        if (c->data_size) {
144
+            int pkt_size, ret, mode = get_byte(pb);
145
+
146
+            if (s->packet_size) {
147
+                pkt_size = s->packet_size - 1;
148
+            } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) {
149
+                c->data_size--;
150
+                continue;
151
+            }
152
+
153
+            if (c->data_size <= pkt_size) {
154
+                av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
155
+                pkt_size = c->data_size - 1;
156
+            }
157
+
158
+            if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) {
159
+                if (pkt_size != ret)
160
+                    av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
161
+
162
+                c->data_size -= pkt_size + 1;
163
+            }
164
+            return ret;
165
+        }
166
+
167
+        if (url_ftell(pb) & 1 && get_byte(pb))
168
+            av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
169
+
170
+        tag        = get_le32(pb);
171
+        chunk_size = get_le32(pb);
172
+        switch (tag) {
173
+        case MKTAG('v', 'r', 'a', 't'):
174
+            if (get_le32(pb)) // var-rate-flag
175
+                s->packet_size = 0;
176
+            url_fskip(pb, 4); // size-in-packets
177
+            break;
178
+        case MKTAG('d', 'a', 't', 'a'):
179
+            c->data_size = chunk_size;
180
+            break;
181
+
182
+        default:
183
+            url_fskip(pb, chunk_size);
184
+        }
185
+    }
186
+    return AVERROR_EOF;
187
+}
188
+
189
+AVInputFormat qcp_demuxer = {
190
+    .name           = "qcp",
191
+    .long_name      = NULL_IF_CONFIG_SMALL("QCP format"),
192
+    .priv_data_size = sizeof(QCPContext),
193
+    .read_probe     = qcp_probe,
194
+    .read_header    = qcp_read_header,
195
+    .read_packet    = qcp_read_packet,
196
+};