Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * VQF demuxer | ||
3 | * Copyright (c) 2009 Vitor Sessak | ||
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 | #include "avformat.h" | ||
23 | #include "avio_internal.h" | ||
24 | #include "demux.h" | ||
25 | #include "internal.h" | ||
26 | #include "libavutil/intreadwrite.h" | ||
27 | #include "libavutil/dict.h" | ||
28 | #include "libavutil/mathematics.h" | ||
29 | #include "libavutil/mem.h" | ||
30 | #include "metadata.h" | ||
31 | |||
32 | typedef struct VqfContext { | ||
33 | int frame_bit_len; | ||
34 | uint8_t last_frame_bits; | ||
35 | int remaining_bits; | ||
36 | } VqfContext; | ||
37 | |||
38 | 7211 | static int vqf_probe(const AVProbeData *probe_packet) | |
39 | { | ||
40 |
2/2✓ Branch 0 taken 7209 times.
✓ Branch 1 taken 2 times.
|
7211 | if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N')) |
41 | 7209 | return 0; | |
42 | |||
43 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (!memcmp(probe_packet->buf + 4, "97012000", 8)) |
44 | 2 | return AVPROBE_SCORE_MAX; | |
45 | |||
46 | ✗ | if (!memcmp(probe_packet->buf + 4, "00052200", 8)) | |
47 | ✗ | return AVPROBE_SCORE_MAX; | |
48 | |||
49 | ✗ | if (AV_RL32(probe_packet->buf + 12) > (1<<27)) | |
50 | ✗ | return AVPROBE_SCORE_EXTENSION/2; | |
51 | |||
52 | ✗ | return AVPROBE_SCORE_EXTENSION; | |
53 | } | ||
54 | |||
55 | 10 | static int add_metadata(AVFormatContext *s, uint32_t tag, | |
56 | unsigned int tag_len, unsigned int remaining) | ||
57 | { | ||
58 | 10 | int len = FFMIN(tag_len, remaining); | |
59 | 10 | char *buf, key[5] = {0}; | |
60 | int ret; | ||
61 | |||
62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (len == UINT_MAX) |
63 | ✗ | return AVERROR_INVALIDDATA; | |
64 | |||
65 | 10 | buf = av_malloc(len+1); | |
66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (!buf) |
67 | ✗ | return AVERROR(ENOMEM); | |
68 | |||
69 | 10 | ret = ffio_read_size(s->pb, buf, len); | |
70 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (ret < 0) { |
71 | ✗ | av_free(buf); | |
72 | ✗ | return ret; | |
73 | } | ||
74 | 10 | buf[len] = 0; | |
75 | 10 | AV_WL32(key, tag); | |
76 | 10 | return av_dict_set(&s->metadata, key, buf, AV_DICT_DONT_STRDUP_VAL); | |
77 | } | ||
78 | |||
79 | static const AVMetadataConv vqf_metadata_conv[] = { | ||
80 | { "(c) ", "copyright" }, | ||
81 | { "ARNG", "arranger" }, | ||
82 | { "AUTH", "author" }, | ||
83 | { "BAND", "band" }, | ||
84 | { "CDCT", "conductor" }, | ||
85 | { "COMT", "comment" }, | ||
86 | { "FILE", "filename" }, | ||
87 | { "GENR", "genre" }, | ||
88 | { "LABL", "publisher" }, | ||
89 | { "MUSC", "composer" }, | ||
90 | { "NAME", "title" }, | ||
91 | { "NOTE", "note" }, | ||
92 | { "PROD", "producer" }, | ||
93 | { "PRSN", "personnel" }, | ||
94 | { "REMX", "remixer" }, | ||
95 | { "SING", "singer" }, | ||
96 | { "TRCK", "track" }, | ||
97 | { "WORD", "words" }, | ||
98 | { 0 }, | ||
99 | }; | ||
100 | |||
101 | 2 | static int vqf_read_header(AVFormatContext *s) | |
102 | { | ||
103 | 2 | VqfContext *c = s->priv_data; | |
104 | 2 | AVStream *st = avformat_new_stream(s, NULL); | |
105 | int chunk_tag; | ||
106 | 2 | int rate_flag = -1; | |
107 | int header_size; | ||
108 | 2 | int read_bitrate = 0; | |
109 | int size, ret; | ||
110 | uint8_t comm_chunk[12]; | ||
111 | |||
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!st) |
113 | ✗ | return AVERROR(ENOMEM); | |
114 | |||
115 | 2 | avio_skip(s->pb, 12); | |
116 | |||
117 | 2 | header_size = avio_rb32(s->pb); | |
118 | |||
119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (header_size < 0) |
120 | ✗ | return AVERROR_INVALIDDATA; | |
121 | |||
122 | 2 | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
123 | 2 | st->codecpar->codec_id = AV_CODEC_ID_TWINVQ; | |
124 | 2 | st->start_time = 0; | |
125 | |||
126 | do { | ||
127 | int len; | ||
128 | 16 | chunk_tag = avio_rl32(s->pb); | |
129 | |||
130 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 14 times.
|
16 | if (chunk_tag == MKTAG('D','A','T','A')) |
131 | 2 | break; | |
132 | |||
133 | 14 | len = avio_rb32(s->pb); | |
134 | |||
135 |
2/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
|
14 | if ((unsigned) len > INT_MAX/2 || header_size < 8) { |
136 | ✗ | av_log(s, AV_LOG_ERROR, "Malformed header\n"); | |
137 | ✗ | return -1; | |
138 | } | ||
139 | |||
140 | 14 | header_size -= 8; | |
141 | |||
142 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
|
14 | switch(chunk_tag){ |
143 | 2 | case MKTAG('C','O','M','M'): | |
144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (len < 12) |
145 | ✗ | return AVERROR_INVALIDDATA; | |
146 | |||
147 | 2 | avio_read(s->pb, comm_chunk, 12); | |
148 | 2 | st->codecpar->ch_layout.nb_channels = AV_RB32(comm_chunk) + 1; | |
149 | 2 | read_bitrate = AV_RB32(comm_chunk + 4); | |
150 | 2 | rate_flag = AV_RB32(comm_chunk + 8); | |
151 | 2 | avio_skip(s->pb, len-12); | |
152 | |||
153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (st->codecpar->ch_layout.nb_channels <= 0) { |
154 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid number of channels\n"); | |
155 | ✗ | return AVERROR_INVALIDDATA; | |
156 | } | ||
157 | |||
158 | 2 | st->codecpar->bit_rate = (int64_t)read_bitrate * 1000; | |
159 | 2 | break; | |
160 | 2 | case MKTAG('D','S','I','Z'): // size of compressed data | |
161 | { | ||
162 | 2 | av_dict_set_int(&s->metadata, "size", avio_rb32(s->pb), 0); | |
163 | } | ||
164 | 2 | break; | |
165 | ✗ | case MKTAG('Y','E','A','R'): // recording date | |
166 | case MKTAG('E','N','C','D'): // compression date | ||
167 | case MKTAG('E','X','T','R'): // reserved | ||
168 | case MKTAG('_','Y','M','H'): // reserved | ||
169 | case MKTAG('_','N','T','T'): // reserved | ||
170 | case MKTAG('_','I','D','3'): // reserved for ID3 tags | ||
171 | ✗ | avio_skip(s->pb, FFMIN(len, header_size)); | |
172 | ✗ | break; | |
173 | 10 | default: | |
174 | 10 | ret = add_metadata(s, chunk_tag, len, header_size); | |
175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
10 | if (ret < 0) |
176 | ✗ | return ret; | |
177 | 10 | break; | |
178 | } | ||
179 | |||
180 | 14 | header_size -= len; | |
181 | |||
182 |
2/4✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
|
14 | } while (header_size >= 0 && !avio_feof(s->pb)); |
183 | |||
184 |
1/5✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
2 | switch (rate_flag) { |
185 | ✗ | case -1: | |
186 | ✗ | av_log(s, AV_LOG_ERROR, "COMM tag not found!\n"); | |
187 | ✗ | return -1; | |
188 | ✗ | case 44: | |
189 | ✗ | st->codecpar->sample_rate = 44100; | |
190 | ✗ | break; | |
191 | 2 | case 22: | |
192 | 2 | st->codecpar->sample_rate = 22050; | |
193 | 2 | break; | |
194 | ✗ | case 11: | |
195 | ✗ | st->codecpar->sample_rate = 11025; | |
196 | ✗ | break; | |
197 | ✗ | default: | |
198 | ✗ | if (rate_flag < 8 || rate_flag > 44) { | |
199 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag); | |
200 | ✗ | return AVERROR_INVALIDDATA; | |
201 | } | ||
202 | ✗ | st->codecpar->sample_rate = rate_flag*1000; | |
203 | ✗ | break; | |
204 | } | ||
205 | |||
206 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (read_bitrate / st->codecpar->ch_layout.nb_channels < 8 || |
207 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | read_bitrate / st->codecpar->ch_layout.nb_channels > 48) { |
208 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n", | |
209 | ✗ | read_bitrate / st->codecpar->ch_layout.nb_channels); | |
210 | ✗ | return AVERROR_INVALIDDATA; | |
211 | } | ||
212 | |||
213 | 2 | switch (((st->codecpar->sample_rate/1000) << 8) + | |
214 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | read_bitrate/st->codecpar->ch_layout.nb_channels) { |
215 | ✗ | case (11<<8) + 8 : | |
216 | case (8 <<8) + 8 : | ||
217 | case (11<<8) + 10: | ||
218 | case (22<<8) + 32: | ||
219 | ✗ | size = 512; | |
220 | ✗ | break; | |
221 | 2 | case (16<<8) + 16: | |
222 | case (22<<8) + 20: | ||
223 | case (22<<8) + 24: | ||
224 | 2 | size = 1024; | |
225 | 2 | break; | |
226 | ✗ | case (44<<8) + 40: | |
227 | case (44<<8) + 48: | ||
228 | ✗ | size = 2048; | |
229 | ✗ | break; | |
230 | ✗ | default: | |
231 | ✗ | av_log(s, AV_LOG_ERROR, "Mode not supported: %d Hz, %"PRId64" kb/s.\n", | |
232 | ✗ | st->codecpar->sample_rate, st->codecpar->bit_rate); | |
233 | ✗ | return -1; | |
234 | } | ||
235 | 2 | c->frame_bit_len = st->codecpar->bit_rate*size/st->codecpar->sample_rate; | |
236 | 2 | avpriv_set_pts_info(st, 64, size, st->codecpar->sample_rate); | |
237 | |||
238 | /* put first 12 bytes of COMM chunk in extradata */ | ||
239 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if ((ret = ff_alloc_extradata(st->codecpar, 12)) < 0) |
240 | ✗ | return ret; | |
241 | 2 | memcpy(st->codecpar->extradata, comm_chunk, 12); | |
242 | |||
243 | 2 | ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv); | |
244 | |||
245 | 2 | return 0; | |
246 | } | ||
247 | |||
248 | 5178 | static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt) | |
249 | { | ||
250 | 5178 | VqfContext *c = s->priv_data; | |
251 | int ret; | ||
252 | 5178 | int size = (c->frame_bit_len - c->remaining_bits + 7)>>3; | |
253 | |||
254 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 5178 times.
|
5178 | if ((ret = av_new_packet(pkt, size + 2)) < 0) |
255 | ✗ | return ret; | |
256 | |||
257 | 5178 | pkt->pos = avio_tell(s->pb); | |
258 | 5178 | pkt->stream_index = 0; | |
259 | 5178 | pkt->duration = 1; | |
260 | |||
261 | 5178 | pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip | |
262 | 5178 | pkt->data[1] = c->last_frame_bits; | |
263 | 5178 | ret = ffio_read_size(s->pb, pkt->data + 2, size); | |
264 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5176 times.
|
5178 | if (ret < 0) |
265 | 2 | return ret; | |
266 | |||
267 | 5176 | c->last_frame_bits = pkt->data[size+1]; | |
268 | 5176 | c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits; | |
269 | |||
270 | 5176 | return 0; | |
271 | } | ||
272 | |||
273 | ✗ | static int vqf_read_seek(AVFormatContext *s, | |
274 | int stream_index, int64_t timestamp, int flags) | ||
275 | { | ||
276 | ✗ | VqfContext *c = s->priv_data; | |
277 | AVStream *st; | ||
278 | int64_t ret; | ||
279 | int64_t pos; | ||
280 | |||
281 | ✗ | st = s->streams[stream_index]; | |
282 | ✗ | pos = av_rescale_rnd(timestamp * st->codecpar->bit_rate, | |
283 | ✗ | st->time_base.num, | |
284 | ✗ | st->time_base.den * (int64_t)c->frame_bit_len, | |
285 | ✗ | (flags & AVSEEK_FLAG_BACKWARD) ? | |
286 | AV_ROUND_DOWN : AV_ROUND_UP); | ||
287 | ✗ | pos *= c->frame_bit_len; | |
288 | |||
289 | ✗ | ffstream(st)->cur_dts = av_rescale(pos, st->time_base.den, | |
290 | ✗ | st->codecpar->bit_rate * (int64_t)st->time_base.num); | |
291 | |||
292 | ✗ | if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + ffformatcontext(s)->data_offset, SEEK_SET)) < 0) | |
293 | ✗ | return ret; | |
294 | |||
295 | ✗ | c->remaining_bits = -7 - ((pos-7)&7); | |
296 | ✗ | return 0; | |
297 | } | ||
298 | |||
299 | const FFInputFormat ff_vqf_demuxer = { | ||
300 | .p.name = "vqf", | ||
301 | .p.long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"), | ||
302 | .p.extensions = "vqf,vql,vqe", | ||
303 | .priv_data_size = sizeof(VqfContext), | ||
304 | .read_probe = vqf_probe, | ||
305 | .read_header = vqf_read_header, | ||
306 | .read_packet = vqf_read_packet, | ||
307 | .read_seek = vqf_read_seek, | ||
308 | }; | ||
309 |