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(const 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 if (AV_RL32(probe_packet->buf + 12) > (1<<27))
47 return AVPROBE_SCORE_EXTENSION/2;
49 return AVPROBE_SCORE_EXTENSION;
52 static void add_metadata(AVFormatContext *s, uint32_t tag,
53 unsigned int tag_len, unsigned int remaining)
55 int len = FFMIN(tag_len, remaining);
56 char *buf, key[5] = {0};
61 buf = av_malloc(len+1);
64 avio_read(s->pb, buf, len);
67 av_dict_set(&s->metadata, key, buf, AV_DICT_DONT_STRDUP_VAL);
70 static const AVMetadataConv vqf_metadata_conv[] = {
71 { "(c) ", "copyright" },
72 { "ARNG", "arranger" },
75 { "CDCT", "conductor" },
76 { "COMT", "comment" },
77 { "FILE", "filename" },
79 { "LABL", "publisher" },
80 { "MUSC", "composer" },
83 { "PROD", "producer" },
84 { "PRSN", "personnel" },
85 { "REMX", "remixer" },
92 static int vqf_read_header(AVFormatContext *s)
94 VqfContext *c = s->priv_data;
95 AVStream *st = avformat_new_stream(s, NULL);
101 uint8_t comm_chunk[12];
104 return AVERROR(ENOMEM);
106 avio_skip(s->pb, 12);
108 header_size = avio_rb32(s->pb);
111 return AVERROR_INVALIDDATA;
113 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
114 st->codecpar->codec_id = AV_CODEC_ID_TWINVQ;
119 chunk_tag = avio_rl32(s->pb);
121 if (chunk_tag == MKTAG('D','A','T','A'))
124 len = avio_rb32(s->pb);
126 if ((unsigned) len > INT_MAX/2 || header_size < 8) {
127 av_log(s, AV_LOG_ERROR, "Malformed header\n");
134 case MKTAG('C','O','M','M'):
135 avio_read(s->pb, comm_chunk, 12);
136 st->codecpar->channels = AV_RB32(comm_chunk ) + 1;
137 read_bitrate = AV_RB32(comm_chunk + 4);
138 rate_flag = AV_RB32(comm_chunk + 8);
139 avio_skip(s->pb, len-12);
141 if (st->codecpar->channels <= 0) {
142 av_log(s, AV_LOG_ERROR, "Invalid number of channels\n");
143 return AVERROR_INVALIDDATA;
146 st->codecpar->bit_rate = (int64_t)read_bitrate * 1000;
148 case MKTAG('D','S','I','Z'): // size of compressed data
150 av_dict_set_int(&s->metadata, "size", avio_rb32(s->pb), 0);
153 case MKTAG('Y','E','A','R'): // recording date
154 case MKTAG('E','N','C','D'): // compression date
155 case MKTAG('E','X','T','R'): // reserved
156 case MKTAG('_','Y','M','H'): // reserved
157 case MKTAG('_','N','T','T'): // reserved
158 case MKTAG('_','I','D','3'): // reserved for ID3 tags
159 avio_skip(s->pb, FFMIN(len, header_size));
162 add_metadata(s, chunk_tag, len, header_size);
168 } while (header_size >= 0 && !avio_feof(s->pb));
172 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
175 st->codecpar->sample_rate = 44100;
178 st->codecpar->sample_rate = 22050;
181 st->codecpar->sample_rate = 11025;
184 if (rate_flag < 8 || rate_flag > 44) {
185 av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag);
186 return AVERROR_INVALIDDATA;
188 st->codecpar->sample_rate = rate_flag*1000;
192 if (read_bitrate / st->codecpar->channels < 8 ||
193 read_bitrate / st->codecpar->channels > 48) {
194 av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n",
195 read_bitrate / st->codecpar->channels);
196 return AVERROR_INVALIDDATA;
199 switch (((st->codecpar->sample_rate/1000) << 8) +
200 read_bitrate/st->codecpar->channels) {
217 av_log(s, AV_LOG_ERROR, "Mode not supported: %d Hz, %"PRId64" kb/s.\n",
218 st->codecpar->sample_rate, st->codecpar->bit_rate);
221 c->frame_bit_len = st->codecpar->bit_rate*size/st->codecpar->sample_rate;
222 avpriv_set_pts_info(st, 64, size, st->codecpar->sample_rate);
224 /* put first 12 bytes of COMM chunk in extradata */
225 if ((ret = ff_alloc_extradata(st->codecpar, 12)) < 0)
227 memcpy(st->codecpar->extradata, comm_chunk, 12);
229 ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv);
234 static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
236 VqfContext *c = s->priv_data;
238 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
240 if ((ret = av_new_packet(pkt, size + 2)) < 0)
243 pkt->pos = avio_tell(s->pb);
244 pkt->stream_index = 0;
247 pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
248 pkt->data[1] = c->last_frame_bits;
249 ret = avio_read(s->pb, pkt->data+2, size);
252 av_packet_unref(pkt);
256 c->last_frame_bits = pkt->data[size+1];
257 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
262 static int vqf_read_seek(AVFormatContext *s,
263 int stream_index, int64_t timestamp, int flags)
265 VqfContext *c = s->priv_data;
270 st = s->streams[stream_index];
271 pos = av_rescale_rnd(timestamp * st->codecpar->bit_rate,
273 st->time_base.den * (int64_t)c->frame_bit_len,
274 (flags & AVSEEK_FLAG_BACKWARD) ?
275 AV_ROUND_DOWN : AV_ROUND_UP);
276 pos *= c->frame_bit_len;
278 st->cur_dts = av_rescale(pos, st->time_base.den,
279 st->codecpar->bit_rate * (int64_t)st->time_base.num);
281 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->internal->data_offset, SEEK_SET)) < 0)
284 c->remaining_bits = -7 - ((pos-7)&7);
288 AVInputFormat ff_vqf_demuxer = {
290 .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
291 .priv_data_size = sizeof(VqfContext),
292 .read_probe = vqf_probe,
293 .read_header = vqf_read_header,
294 .read_packet = vqf_read_packet,
295 .read_seek = vqf_read_seek,
296 .extensions = "vqf,vql,vqe",