FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/vqf.c
Date: 2025-03-08 20:38:41
Exec Total Coverage
Lines: 86 150 57.3%
Functions: 4 5 80.0%
Branches: 29 69 42.0%

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