Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * QCP format (.qcp) demuxer | ||
3 | * Copyright (c) 2009 Kenan Gillet | ||
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 | /** | ||
23 | * @file | ||
24 | * QCP format (.qcp) demuxer | ||
25 | * @author Kenan Gillet | ||
26 | * @see RFC 3625: "The QCP File Format and Media Types for Speech Data" | ||
27 | * http://tools.ietf.org/html/rfc3625 | ||
28 | */ | ||
29 | |||
30 | #include "libavutil/channel_layout.h" | ||
31 | #include "libavutil/intreadwrite.h" | ||
32 | #include "avformat.h" | ||
33 | #include "demux.h" | ||
34 | #include "riff.h" | ||
35 | |||
36 | typedef struct QCPContext { | ||
37 | uint32_t data_size; ///< size of data chunk | ||
38 | |||
39 | #define QCP_MAX_MODE 4 | ||
40 | int16_t rates_per_mode[QCP_MAX_MODE+1]; ///< contains the packet size corresponding | ||
41 | ///< to each mode, -1 if no size. | ||
42 | } QCPContext; | ||
43 | |||
44 | /** | ||
45 | * Last 15 out of 16 bytes of QCELP-13K GUID, as stored in the file; | ||
46 | * the first byte of the GUID can be either 0x41 or 0x42. | ||
47 | */ | ||
48 | static const uint8_t guid_qcelp_13k_part[15] = { | ||
49 | 0x6d, 0x7f, 0x5e, 0x15, 0xb1, 0xd0, 0x11, 0xba, | ||
50 | 0x91, 0x00, 0x80, 0x5f, 0xb4, 0xb9, 0x7e | ||
51 | }; | ||
52 | |||
53 | /** | ||
54 | * EVRC GUID as stored in the file | ||
55 | */ | ||
56 | static const uint8_t guid_evrc[16] = { | ||
57 | 0x8d, 0xd4, 0x89, 0xe6, 0x76, 0x90, 0xb5, 0x46, | ||
58 | 0x91, 0xef, 0x73, 0x6a, 0x51, 0x00, 0xce, 0xb4 | ||
59 | }; | ||
60 | |||
61 | static const uint8_t guid_4gv[16] = { | ||
62 | 0xca, 0x29, 0xfd, 0x3c, 0x53, 0xf6, 0xf5, 0x4e, | ||
63 | 0x90, 0xe9, 0xf4, 0x23, 0x6d, 0x59, 0x9b, 0x61 | ||
64 | }; | ||
65 | |||
66 | /** | ||
67 | * SMV GUID as stored in the file | ||
68 | */ | ||
69 | static const uint8_t guid_smv[16] = { | ||
70 | 0x75, 0x2b, 0x7c, 0x8d, 0x97, 0xa7, 0x49, 0xed, | ||
71 | 0x98, 0x5e, 0xd5, 0x3c, 0x8c, 0xc7, 0x5f, 0x84 | ||
72 | }; | ||
73 | |||
74 | /** | ||
75 | * @param guid contains at least 16 bytes | ||
76 | * @return 1 if the guid is a qcelp_13k guid, 0 otherwise | ||
77 | */ | ||
78 | 2 | static int is_qcelp_13k_guid(const uint8_t *guid) { | |
79 | ✗ | return (guid[0] == 0x41 || guid[0] == 0x42) | |
80 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | && !memcmp(guid+1, guid_qcelp_13k_part, sizeof(guid_qcelp_13k_part)); |
81 | } | ||
82 | |||
83 | 7186 | static int qcp_probe(const AVProbeData *pd) | |
84 | { | ||
85 |
2/2✓ Branch 0 taken 960 times.
✓ Branch 1 taken 6226 times.
|
7186 | if (AV_RL32(pd->buf ) == AV_RL32("RIFF") && |
86 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 958 times.
|
960 | AV_RL64(pd->buf+8) == AV_RL64("QLCMfmt ")) |
87 | 2 | return AVPROBE_SCORE_MAX; | |
88 | 7184 | return 0; | |
89 | } | ||
90 | |||
91 | 2 | static int qcp_read_header(AVFormatContext *s) | |
92 | { | ||
93 | 2 | AVIOContext *pb = s->pb; | |
94 | 2 | QCPContext *c = s->priv_data; | |
95 | 2 | AVStream *st = avformat_new_stream(s, NULL); | |
96 | uint8_t buf[16]; | ||
97 | int i; | ||
98 | unsigned nb_rates; | ||
99 | |||
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) |
101 | ✗ | return AVERROR(ENOMEM); | |
102 | |||
103 | 2 | avio_rb32(pb); // "RIFF" | |
104 | 2 | avio_skip(pb, 4 + 8 + 4 + 1 + 1); // filesize + "QLCMfmt " + chunk-size + major-version + minor-version | |
105 | |||
106 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
107 | 2 | st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO; | |
108 | 2 | avio_read(pb, buf, 16); | |
109 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if (is_qcelp_13k_guid(buf)) { |
110 | 2 | st->codecpar->codec_id = AV_CODEC_ID_QCELP; | |
111 | ✗ | } else if (!memcmp(buf, guid_evrc, 16)) { | |
112 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_EVRC; | |
113 | ✗ | } else if (!memcmp(buf, guid_smv, 16)) { | |
114 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_SMV; | |
115 | ✗ | } else if (!memcmp(buf, guid_4gv, 16)) { | |
116 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_4GV; | |
117 | } else { | ||
118 | ✗ | av_log(s, AV_LOG_ERROR, "Unknown codec GUID "FF_PRI_GUID".\n", | |
119 | ✗ | FF_ARG_GUID(buf)); | |
120 | ✗ | return AVERROR_INVALIDDATA; | |
121 | } | ||
122 | 2 | avio_skip(pb, 2 + 80); // codec-version + codec-name | |
123 | 2 | st->codecpar->bit_rate = avio_rl16(pb); | |
124 | |||
125 | 2 | s->packet_size = avio_rl16(pb); | |
126 | 2 | avio_skip(pb, 2); // block-size | |
127 | 2 | st->codecpar->sample_rate = avio_rl16(pb); | |
128 | 2 | avio_skip(pb, 2); // sample-size | |
129 | |||
130 | 2 | memset(c->rates_per_mode, -1, sizeof(c->rates_per_mode)); | |
131 | 2 | nb_rates = avio_rl32(pb); | |
132 | 2 | nb_rates = FFMIN(nb_rates, 8); | |
133 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 2 times.
|
12 | for (i=0; i<nb_rates; i++) { |
134 | 10 | int size = avio_r8(pb); | |
135 | 10 | int mode = avio_r8(pb); | |
136 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (mode > QCP_MAX_MODE) { |
137 | ✗ | av_log(s, AV_LOG_WARNING, "Unknown entry %d=>%d in rate-map-table.\n ", mode, size); | |
138 | } else | ||
139 | 10 | c->rates_per_mode[mode] = size; | |
140 | } | ||
141 | 2 | avio_skip(pb, 16 - 2*nb_rates + 20); // empty entries of rate-map-table + reserved | |
142 | |||
143 | 2 | return 0; | |
144 | } | ||
145 | |||
146 | 992 | static int qcp_read_packet(AVFormatContext *s, AVPacket *pkt) | |
147 | { | ||
148 | 992 | AVIOContext *pb = s->pb; | |
149 | 992 | QCPContext *c = s->priv_data; | |
150 | unsigned int chunk_size, tag; | ||
151 | |||
152 |
2/2✓ Branch 1 taken 1000 times.
✓ Branch 2 taken 2 times.
|
1002 | while(!avio_feof(pb)) { |
153 |
2/2✓ Branch 0 taken 990 times.
✓ Branch 1 taken 10 times.
|
1000 | if (c->data_size) { |
154 | 990 | int pkt_size, ret, mode = avio_r8(pb); | |
155 | |||
156 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 990 times.
|
990 | if (s->packet_size) { |
157 | ✗ | pkt_size = s->packet_size - 1; | |
158 |
2/4✓ Branch 0 taken 990 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 990 times.
|
990 | } else if (mode > QCP_MAX_MODE || (pkt_size = c->rates_per_mode[mode]) < 0) { |
159 | ✗ | c->data_size--; | |
160 | ✗ | continue; | |
161 | } | ||
162 | |||
163 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 990 times.
|
990 | if (c->data_size <= pkt_size) { |
164 | ✗ | av_log(s, AV_LOG_WARNING, "Data chunk is too small.\n"); | |
165 | ✗ | pkt_size = c->data_size - 1; | |
166 | } | ||
167 | |||
168 |
1/2✓ Branch 1 taken 990 times.
✗ Branch 2 not taken.
|
990 | if ((ret = av_get_packet(pb, pkt, pkt_size)) >= 0) { |
169 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 990 times.
|
990 | if (pkt_size != ret) |
170 | ✗ | av_log(s, AV_LOG_ERROR, "Packet size is too small.\n"); | |
171 | |||
172 | 990 | c->data_size -= pkt_size + 1; | |
173 | } | ||
174 | 990 | return ret; | |
175 | } | ||
176 | |||
177 |
3/4✓ Branch 1 taken 4 times.
✓ Branch 2 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
|
10 | if (avio_tell(pb) & 1 && avio_r8(pb)) |
178 | ✗ | av_log(s, AV_LOG_WARNING, "Padding should be 0.\n"); | |
179 | |||
180 | 10 | tag = avio_rl32(pb); | |
181 | 10 | chunk_size = avio_rl32(pb); | |
182 |
3/3✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 6 times.
|
10 | switch (tag) { |
183 | 2 | case MKTAG('v', 'r', 'a', 't'): | |
184 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if (avio_rl32(pb)) // var-rate-flag |
185 | 2 | s->packet_size = 0; | |
186 | 2 | avio_skip(pb, 4); // size-in-packets | |
187 | 2 | break; | |
188 | 2 | case MKTAG('d', 'a', 't', 'a'): | |
189 | 2 | c->data_size = chunk_size; | |
190 | 2 | break; | |
191 | |||
192 | 6 | default: | |
193 | 6 | avio_skip(pb, chunk_size); | |
194 | } | ||
195 | } | ||
196 | 2 | return AVERROR_EOF; | |
197 | } | ||
198 | |||
199 | const FFInputFormat ff_qcp_demuxer = { | ||
200 | .p.name = "qcp", | ||
201 | .p.long_name = NULL_IF_CONFIG_SMALL("QCP"), | ||
202 | .priv_data_size = sizeof(QCPContext), | ||
203 | .read_probe = qcp_probe, | ||
204 | .read_header = qcp_read_header, | ||
205 | .read_packet = qcp_read_packet, | ||
206 | }; | ||
207 |