FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/vqf.c
Date: 2024-04-16 15:12:51
Exec Total Coverage
Lines: 84 145 57.9%
Functions: 4 5 80.0%
Branches: 26 61 42.6%

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