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
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/mathematics.h"
27 typedef struct VqfContext {
29 uint8_t last_frame_bits;
33 static int vqf_probe(AVProbeData *probe_packet)
35 if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
38 if (!memcmp(probe_packet->buf + 4, "97012000", 8))
39 return AVPROBE_SCORE_MAX;
41 if (!memcmp(probe_packet->buf + 4, "00052200", 8))
42 return AVPROBE_SCORE_MAX;
44 return AVPROBE_SCORE_MAX/2;
47 static void add_metadata(AVFormatContext *s, const char *tag,
48 unsigned int tag_len, unsigned int remaining)
50 int len = FFMIN(tag_len, remaining);
56 buf = av_malloc(len+1);
59 avio_read(s->pb, buf, len);
61 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
64 static int vqf_read_header(AVFormatContext *s, AVFormatParameters *ap)
66 VqfContext *c = s->priv_data;
67 AVStream *st = avformat_new_stream(s, NULL);
73 uint8_t comm_chunk[12];
76 return AVERROR(ENOMEM);
80 header_size = avio_rb32(s->pb);
82 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
83 st->codec->codec_id = CODEC_ID_TWINVQ;
88 chunk_tag = avio_rl32(s->pb);
90 if (chunk_tag == MKTAG('D','A','T','A'))
93 len = avio_rb32(s->pb);
95 if ((unsigned) len > INT_MAX/2) {
96 av_log(s, AV_LOG_ERROR, "Malformed header\n");
103 case MKTAG('C','O','M','M'):
104 avio_read(s->pb, comm_chunk, 12);
105 st->codec->channels = AV_RB32(comm_chunk ) + 1;
106 read_bitrate = AV_RB32(comm_chunk + 4);
107 rate_flag = AV_RB32(comm_chunk + 8);
108 avio_skip(s->pb, len-12);
110 st->codec->bit_rate = read_bitrate*1000;
112 case MKTAG('N','A','M','E'):
113 add_metadata(s, "title" , len, header_size);
115 case MKTAG('(','c',')',' '):
116 add_metadata(s, "copyright", len, header_size);
118 case MKTAG('A','U','T','H'):
119 add_metadata(s, "author" , len, header_size);
121 case MKTAG('A','L','B','M'):
122 add_metadata(s, "album" , len, header_size);
124 case MKTAG('T','R','C','K'):
125 add_metadata(s, "track" , len, header_size);
127 case MKTAG('C','O','M','T'):
128 add_metadata(s, "comment" , len, header_size);
130 case MKTAG('F','I','L','E'):
131 add_metadata(s, "filename" , len, header_size);
133 case MKTAG('D','S','I','Z'):
134 add_metadata(s, "size" , len, header_size);
136 case MKTAG('D','A','T','E'):
137 add_metadata(s, "date" , len, header_size);
139 case MKTAG('G','E','N','R'):
140 add_metadata(s, "genre" , len, header_size);
143 av_log(s, AV_LOG_ERROR, "Unknown chunk: %c%c%c%c\n",
144 ((char*)&chunk_tag)[0], ((char*)&chunk_tag)[1],
145 ((char*)&chunk_tag)[2], ((char*)&chunk_tag)[3]);
146 avio_skip(s->pb, FFMIN(len, header_size));
152 } while (header_size >= 0);
156 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
159 st->codec->sample_rate = 44100;
162 st->codec->sample_rate = 22050;
165 st->codec->sample_rate = 11025;
168 st->codec->sample_rate = rate_flag*1000;
172 switch (((st->codec->sample_rate/1000) << 8) +
173 read_bitrate/st->codec->channels) {
190 av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
191 st->codec->sample_rate, st->codec->bit_rate);
194 c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
195 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
197 /* put first 12 bytes of COMM chunk in extradata */
198 if (!(st->codec->extradata = av_malloc(12 + FF_INPUT_BUFFER_PADDING_SIZE)))
199 return AVERROR(ENOMEM);
200 st->codec->extradata_size = 12;
201 memcpy(st->codec->extradata, comm_chunk, 12);
206 static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
208 VqfContext *c = s->priv_data;
210 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
212 pkt->pos = avio_tell(s->pb);
213 pkt->stream_index = 0;
215 if (av_new_packet(pkt, size+2) < 0)
218 pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
219 pkt->data[1] = c->last_frame_bits;
220 ret = avio_read(s->pb, pkt->data+2, size);
227 c->last_frame_bits = pkt->data[size+1];
228 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
233 static int vqf_read_seek(AVFormatContext *s,
234 int stream_index, int64_t timestamp, int flags)
236 VqfContext *c = s->priv_data;
241 st = s->streams[stream_index];
242 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
244 st->time_base.den * (int64_t)c->frame_bit_len,
245 (flags & AVSEEK_FLAG_BACKWARD) ?
246 AV_ROUND_DOWN : AV_ROUND_UP);
247 pos *= c->frame_bit_len;
249 st->cur_dts = av_rescale(pos, st->time_base.den,
250 st->codec->bit_rate * (int64_t)st->time_base.num);
252 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
255 c->remaining_bits = -7 - ((pos-7)&7);
259 AVInputFormat ff_vqf_demuxer = {
261 .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
262 .priv_data_size = sizeof(VqfContext),
263 .read_probe = vqf_probe,
264 .read_header = vqf_read_header,
265 .read_packet = vqf_read_packet,
266 .read_seek = vqf_read_seek,