ffmpeg / libavformat / qcp.c @ 56a10009
History | View | Annotate | Download (6.05 KB)
1 | cdce0fb8 | Kenan Gillet | /*
|
---|---|---|---|
2 | * QCP format (.qcp) demuxer
|
||
3 | * Copyright (c) 2009 Kenan Gillet
|
||
4 | *
|
||
5 | 2912e87a | Mans Rullgard | * This file is part of Libav.
|
6 | cdce0fb8 | Kenan Gillet | *
|
7 | 2912e87a | Mans Rullgard | * Libav is free software; you can redistribute it and/or
|
8 | cdce0fb8 | Kenan Gillet | * 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 | 2912e87a | Mans Rullgard | * Libav is distributed in the hope that it will be useful,
|
13 | cdce0fb8 | Kenan Gillet | * 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 | 2912e87a | Mans Rullgard | * License along with Libav; if not, write to the Free Software
|
19 | cdce0fb8 | Kenan Gillet | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
20 | */
|
||
21 | |||
22 | /**
|
||
23 | ba87f080 | Diego Biurrun | * @file
|
24 | cdce0fb8 | Kenan Gillet | * QCP format (.qcp) demuxer
|
25 | * @author Kenan Gillet
|
||
26 | * @sa RFC 3625: "The QCP File Format and Media Types for Speech Data"
|
||
27 | * http://tools.ietf.org/html/rfc3625
|
||
28 | */
|
||
29 | |||
30 | #include "libavutil/intreadwrite.h" |
||
31 | #include "avformat.h" |
||
32 | |||
33 | typedef struct { |
||
34 | uint32_t data_size; ///< size of data chunk
|
||
35 | |||
36 | #define QCP_MAX_MODE 4 |
||
37 | int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding |
||
38 | ///< to each mode, -1 if no size.
|
||
39 | } QCPContext; |
||
40 | |||
41 | /**
|
||
42 | * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file;
|
||
43 | * the first byte of the GUID can be either 0x41 or 0x42.
|
||
44 | */
|
||
45 | static const uint8_t guid_qcelp_13k_part[15] = { |
||
46 | 0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba, |
||
47 | 0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e |
||
48 | }; |
||
49 | |||
50 | /**
|
||
51 | * EVRC GUID as stored in the file
|
||
52 | */
|
||
53 | static const uint8_t guid_evrc[16] = { |
||
54 | 0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46, |
||
55 | 0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4 |
||
56 | }; |
||
57 | |||
58 | /**
|
||
59 | * SMV GUID as stored in the file
|
||
60 | */
|
||
61 | static const uint8_t guid_smv[16] = { |
||
62 | 0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed, |
||
63 | 0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84 |
||
64 | }; |
||
65 | |||
66 | /**
|
||
67 | * @param guid contains at least 16 bytes
|
||
68 | * @return 1 if the guid is a qcelp_13k guid, 0 otherwise
|
||
69 | */
|
||
70 | static int is_qcelp_13k_guid(const uint8_t *guid) { |
||
71 | return (guid[0] == 0x41 || guid[0] == 0x42) |
||
72 | && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part)); |
||
73 | } |
||
74 | |||
75 | static int qcp_probe(AVProbeData *pd) |
||
76 | { |
||
77 | if (AV_RL32(pd->buf ) == AV_RL32("RIFF") && |
||
78 | AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt ")) |
||
79 | return AVPROBE_SCORE_MAX;
|
||
80 | return 0; |
||
81 | } |
||
82 | |||
83 | static int qcp_read_header(AVFormatContext *s, AVFormatParameters *ap) |
||
84 | { |
||
85 | ae628ec1 | Anton Khirnov | AVIOContext *pb = s->pb; |
86 | cdce0fb8 | Kenan Gillet | QCPContext *c = s->priv_data; |
87 | AVStream *st = av_new_stream(s, 0);
|
||
88 | uint8_t buf[16];
|
||
89 | int i, nb_rates;
|
||
90 | |||
91 | if (!st)
|
||
92 | return AVERROR(ENOMEM);
|
||
93 | |||
94 | b7effd4e | Anton Khirnov | avio_rb32(pb); // "RIFF"
|
95 | s->file_size = avio_rl32(pb) + 8;
|
||
96 | 45a8a02a | Anton Khirnov | avio_skip(pb, 8 + 4 + 1 + 1); // "QLCMfmt " + chunk-size + major-version + minor-version |
97 | cdce0fb8 | Kenan Gillet | |
98 | 72415b2a | Stefano Sabatini | st->codec->codec_type = AVMEDIA_TYPE_AUDIO; |
99 | cdce0fb8 | Kenan Gillet | st->codec->channels = 1;
|
100 | b7effd4e | Anton Khirnov | avio_read(pb, buf, 16);
|
101 | cdce0fb8 | Kenan Gillet | if (is_qcelp_13k_guid(buf)) {
|
102 | st->codec->codec_id = CODEC_ID_QCELP; |
||
103 | } else if (!memcmp(buf, guid_evrc, 16)) { |
||
104 | av_log(s, AV_LOG_ERROR, "EVRC codec is not supported.\n");
|
||
105 | return AVERROR_PATCHWELCOME;
|
||
106 | } else if (!memcmp(buf, guid_smv, 16)) { |
||
107 | av_log(s, AV_LOG_ERROR, "SMV codec is not supported.\n");
|
||
108 | return AVERROR_PATCHWELCOME;
|
||
109 | } else {
|
||
110 | av_log(s, AV_LOG_ERROR, "Unknown codec GUID.\n");
|
||
111 | return AVERROR_INVALIDDATA;
|
||
112 | } |
||
113 | 45a8a02a | Anton Khirnov | avio_skip(pb, 2 + 80); // codec-version + codec-name |
114 | b7effd4e | Anton Khirnov | st->codec->bit_rate = avio_rl16(pb); |
115 | cdce0fb8 | Kenan Gillet | |
116 | b7effd4e | Anton Khirnov | s->packet_size = avio_rl16(pb); |
117 | 45a8a02a | Anton Khirnov | avio_skip(pb, 2); // block-size |
118 | b7effd4e | Anton Khirnov | st->codec->sample_rate = avio_rl16(pb); |
119 | 45a8a02a | Anton Khirnov | avio_skip(pb, 2); // sample-size |
120 | cdce0fb8 | Kenan Gillet | |
121 | memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode)); |
||
122 | b7effd4e | Anton Khirnov | nb_rates = avio_rl32(pb); |
123 | cdce0fb8 | Kenan Gillet | nb_rates = FFMIN(nb_rates, 8);
|
124 | for (i=0; i<nb_rates; i++) { |
||
125 | b7effd4e | Anton Khirnov | int size = avio_r8(pb);
|
126 | int mode = avio_r8(pb);
|
||
127 | cdce0fb8 | Kenan Gillet | if (mode > QCP_MAX_MODE) {
|
128 | av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size);
|
||
129 | } else
|
||
130 | c->rates_per_mode[mode] = size; |
||
131 | } |
||
132 | 45a8a02a | Anton Khirnov | avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved |
133 | cdce0fb8 | Kenan Gillet | |
134 | return 0; |
||
135 | } |
||
136 | |||
137 | static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt) |
||
138 | { |
||
139 | ae628ec1 | Anton Khirnov | AVIOContext *pb = s->pb; |
140 | cdce0fb8 | Kenan Gillet | QCPContext *c = s->priv_data; |
141 | unsigned int chunk_size, tag; |
||
142 | |||
143 | 66e5b1df | Anton Khirnov | while(!pb->eof_reached) {
|
144 | cdce0fb8 | Kenan Gillet | if (c->data_size) {
|
145 | b7effd4e | Anton Khirnov | int pkt_size, ret, mode = avio_r8(pb);
|
146 | cdce0fb8 | Kenan Gillet | |
147 | if (s->packet_size) {
|
||
148 | pkt_size = s->packet_size - 1;
|
||
149 | } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) { |
||
150 | c->data_size--; |
||
151 | continue;
|
||
152 | } |
||
153 | |||
154 | if (c->data_size <= pkt_size) {
|
||
155 | av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n");
|
||
156 | pkt_size = c->data_size - 1;
|
||
157 | } |
||
158 | |||
159 | if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) { |
||
160 | if (pkt_size != ret)
|
||
161 | av_log(s, AV_LOG_ERROR, "Packet size is too small.\n");
|
||
162 | |||
163 | c->data_size -= pkt_size + 1;
|
||
164 | } |
||
165 | return ret;
|
||
166 | } |
||
167 | |||
168 | a2704c97 | Anton Khirnov | if (avio_tell(pb) & 1 && avio_r8(pb)) |
169 | cdce0fb8 | Kenan Gillet | av_log(s, AV_LOG_WARNING, "Padding should be 0.\n");
|
170 | |||
171 | b7effd4e | Anton Khirnov | tag = avio_rl32(pb); |
172 | chunk_size = avio_rl32(pb); |
||
173 | cdce0fb8 | Kenan Gillet | switch (tag) {
|
174 | case MKTAG('v', 'r', 'a', 't'): |
||
175 | b7effd4e | Anton Khirnov | if (avio_rl32(pb)) // var-rate-flag |
176 | cdce0fb8 | Kenan Gillet | s->packet_size = 0;
|
177 | 45a8a02a | Anton Khirnov | avio_skip(pb, 4); // size-in-packets |
178 | cdce0fb8 | Kenan Gillet | break;
|
179 | case MKTAG('d', 'a', 't', 'a'): |
||
180 | c->data_size = chunk_size; |
||
181 | break;
|
||
182 | |||
183 | default:
|
||
184 | 45a8a02a | Anton Khirnov | avio_skip(pb, chunk_size); |
185 | cdce0fb8 | Kenan Gillet | } |
186 | } |
||
187 | return AVERROR_EOF;
|
||
188 | } |
||
189 | |||
190 | c6610a21 | Diego Elio Pettenò | AVInputFormat ff_qcp_demuxer = { |
191 | cdce0fb8 | Kenan Gillet | .name = "qcp",
|
192 | .long_name = NULL_IF_CONFIG_SMALL("QCP format"),
|
||
193 | .priv_data_size = sizeof(QCPContext),
|
||
194 | .read_probe = qcp_probe, |
||
195 | .read_header = qcp_read_header, |
||
196 | .read_packet = qcp_read_packet, |
||
197 | }; |