3 * Copyright (c) 2009 Vitor Sessak
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/mathematics.h"
29 typedef struct VqfContext {
31 uint8_t last_frame_bits;
35 static int vqf_probe(AVProbeData *probe_packet)
37 if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
40 if (!memcmp(probe_packet->buf + 4, "97012000", 8))
41 return AVPROBE_SCORE_MAX;
43 if (!memcmp(probe_packet->buf + 4, "00052200", 8))
44 return AVPROBE_SCORE_MAX;
46 return AVPROBE_SCORE_EXTENSION;
49 static void add_metadata(AVFormatContext *s, uint32_t tag,
50 unsigned int tag_len, unsigned int remaining)
52 int len = FFMIN(tag_len, remaining);
53 char *buf, key[5] = {0};
58 buf = av_malloc(len+1);
61 avio_read(s->pb, buf, len);
64 av_dict_set(&s->metadata, key, buf, AV_DICT_DONT_STRDUP_VAL);
67 static const AVMetadataConv vqf_metadata_conv[] = {
68 { "(c) ", "copyright" },
69 { "ARNG", "arranger" },
72 { "CDCT", "conductor" },
73 { "COMT", "comment" },
74 { "FILE", "filename" },
76 { "LABL", "publisher" },
77 { "MUSC", "composer" },
80 { "PROD", "producer" },
81 { "PRSN", "personnel" },
82 { "REMX", "remixer" },
89 static int vqf_read_header(AVFormatContext *s)
91 VqfContext *c = s->priv_data;
92 AVStream *st = avformat_new_stream(s, NULL);
98 uint8_t comm_chunk[12];
101 return AVERROR(ENOMEM);
103 avio_skip(s->pb, 12);
105 header_size = avio_rb32(s->pb);
107 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
108 st->codec->codec_id = AV_CODEC_ID_TWINVQ;
113 chunk_tag = avio_rl32(s->pb);
115 if (chunk_tag == MKTAG('D','A','T','A'))
118 len = avio_rb32(s->pb);
120 if ((unsigned) len > INT_MAX/2) {
121 av_log(s, AV_LOG_ERROR, "Malformed header\n");
128 case MKTAG('C','O','M','M'):
129 avio_read(s->pb, comm_chunk, 12);
130 st->codec->channels = AV_RB32(comm_chunk ) + 1;
131 read_bitrate = AV_RB32(comm_chunk + 4);
132 rate_flag = AV_RB32(comm_chunk + 8);
133 avio_skip(s->pb, len-12);
135 if (st->codec->channels <= 0) {
136 av_log(s, AV_LOG_ERROR, "Invalid number of channels\n");
137 return AVERROR_INVALIDDATA;
140 st->codec->bit_rate = read_bitrate*1000;
142 case MKTAG('D','S','I','Z'): // size of compressed data
145 int size = avio_rb32(s->pb);
147 snprintf(buf, sizeof(buf), "%d", size);
148 av_dict_set(&s->metadata, "size", buf, 0);
151 case MKTAG('Y','E','A','R'): // recording date
152 case MKTAG('E','N','C','D'): // compression date
153 case MKTAG('E','X','T','R'): // reserved
154 case MKTAG('_','Y','M','H'): // reserved
155 case MKTAG('_','N','T','T'): // reserved
156 case MKTAG('_','I','D','3'): // reserved for ID3 tags
157 avio_skip(s->pb, FFMIN(len, header_size));
160 add_metadata(s, chunk_tag, len, header_size);
166 } while (header_size >= 0 && !url_feof(s->pb));
170 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
173 st->codec->sample_rate = 44100;
176 st->codec->sample_rate = 22050;
179 st->codec->sample_rate = 11025;
182 if (rate_flag < 8 || rate_flag > 44) {
183 av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag);
184 return AVERROR_INVALIDDATA;
186 st->codec->sample_rate = rate_flag*1000;
190 if (read_bitrate / st->codec->channels < 8 ||
191 read_bitrate / st->codec->channels > 48) {
192 av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n",
193 read_bitrate / st->codec->channels);
194 return AVERROR_INVALIDDATA;
197 switch (((st->codec->sample_rate/1000) << 8) +
198 read_bitrate/st->codec->channels) {
215 av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
216 st->codec->sample_rate, st->codec->bit_rate);
219 c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
220 avpriv_set_pts_info(st, 64, size, st->codec->sample_rate);
222 /* put first 12 bytes of COMM chunk in extradata */
223 if (!(st->codec->extradata = av_malloc(12 + FF_INPUT_BUFFER_PADDING_SIZE)))
224 return AVERROR(ENOMEM);
225 st->codec->extradata_size = 12;
226 memcpy(st->codec->extradata, comm_chunk, 12);
228 ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv);
233 static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
235 VqfContext *c = s->priv_data;
237 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
239 if (av_new_packet(pkt, size+2) < 0)
242 pkt->pos = avio_tell(s->pb);
243 pkt->stream_index = 0;
246 pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
247 pkt->data[1] = c->last_frame_bits;
248 ret = avio_read(s->pb, pkt->data+2, size);
255 c->last_frame_bits = pkt->data[size+1];
256 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
261 static int vqf_read_seek(AVFormatContext *s,
262 int stream_index, int64_t timestamp, int flags)
264 VqfContext *c = s->priv_data;
269 st = s->streams[stream_index];
270 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
272 st->time_base.den * (int64_t)c->frame_bit_len,
273 (flags & AVSEEK_FLAG_BACKWARD) ?
274 AV_ROUND_DOWN : AV_ROUND_UP);
275 pos *= c->frame_bit_len;
277 st->cur_dts = av_rescale(pos, st->time_base.den,
278 st->codec->bit_rate * (int64_t)st->time_base.num);
280 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
283 c->remaining_bits = -7 - ((pos-7)&7);
287 AVInputFormat ff_vqf_demuxer = {
289 .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
290 .priv_data_size = sizeof(VqfContext),
291 .read_probe = vqf_probe,
292 .read_header = vqf_read_header,
293 .read_packet = vqf_read_packet,
294 .read_seek = vqf_read_seek,
295 .extensions = "vqf,vql,vqe",