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"
28 typedef struct VqfContext {
30 uint8_t last_frame_bits;
34 static int vqf_probe(AVProbeData *probe_packet)
36 if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
39 if (!memcmp(probe_packet->buf + 4, "97012000", 8))
40 return AVPROBE_SCORE_MAX;
42 if (!memcmp(probe_packet->buf + 4, "00052200", 8))
43 return AVPROBE_SCORE_MAX;
45 return AVPROBE_SCORE_MAX/2;
48 static void add_metadata(AVFormatContext *s, const char *tag,
49 unsigned int tag_len, unsigned int remaining)
51 int len = FFMIN(tag_len, remaining);
57 buf = av_malloc(len+1);
60 avio_read(s->pb, buf, len);
62 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
65 static int vqf_read_header(AVFormatContext *s, AVFormatParameters *ap)
67 VqfContext *c = s->priv_data;
68 AVStream *st = avformat_new_stream(s, NULL);
74 uint8_t comm_chunk[12];
77 return AVERROR(ENOMEM);
81 header_size = avio_rb32(s->pb);
83 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
84 st->codec->codec_id = CODEC_ID_TWINVQ;
89 chunk_tag = avio_rl32(s->pb);
91 if (chunk_tag == MKTAG('D','A','T','A'))
94 len = avio_rb32(s->pb);
96 if ((unsigned) len > INT_MAX/2) {
97 av_log(s, AV_LOG_ERROR, "Malformed header\n");
104 case MKTAG('C','O','M','M'):
105 avio_read(s->pb, comm_chunk, 12);
106 st->codec->channels = AV_RB32(comm_chunk ) + 1;
107 read_bitrate = AV_RB32(comm_chunk + 4);
108 rate_flag = AV_RB32(comm_chunk + 8);
109 avio_skip(s->pb, len-12);
111 st->codec->bit_rate = read_bitrate*1000;
113 case MKTAG('N','A','M','E'):
114 add_metadata(s, "title" , len, header_size);
116 case MKTAG('(','c',')',' '):
117 add_metadata(s, "copyright", len, header_size);
119 case MKTAG('A','U','T','H'):
120 add_metadata(s, "author" , len, header_size);
122 case MKTAG('A','L','B','M'):
123 add_metadata(s, "album" , len, header_size);
125 case MKTAG('T','R','C','K'):
126 add_metadata(s, "track" , len, header_size);
128 case MKTAG('C','O','M','T'):
129 add_metadata(s, "comment" , len, header_size);
131 case MKTAG('F','I','L','E'):
132 add_metadata(s, "filename" , len, header_size);
134 case MKTAG('D','S','I','Z'):
135 add_metadata(s, "size" , len, header_size);
137 case MKTAG('D','A','T','E'):
138 add_metadata(s, "date" , len, header_size);
140 case MKTAG('G','E','N','R'):
141 add_metadata(s, "genre" , len, header_size);
144 av_log(s, AV_LOG_ERROR, "Unknown chunk: %c%c%c%c\n",
145 ((char*)&chunk_tag)[0], ((char*)&chunk_tag)[1],
146 ((char*)&chunk_tag)[2], ((char*)&chunk_tag)[3]);
147 avio_skip(s->pb, FFMIN(len, header_size));
153 } while (header_size >= 0);
157 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
160 st->codec->sample_rate = 44100;
163 st->codec->sample_rate = 22050;
166 st->codec->sample_rate = 11025;
169 st->codec->sample_rate = rate_flag*1000;
173 switch (((st->codec->sample_rate/1000) << 8) +
174 read_bitrate/st->codec->channels) {
191 av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
192 st->codec->sample_rate, st->codec->bit_rate);
195 c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
196 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
198 /* put first 12 bytes of COMM chunk in extradata */
199 if (!(st->codec->extradata = av_malloc(12 + FF_INPUT_BUFFER_PADDING_SIZE)))
200 return AVERROR(ENOMEM);
201 st->codec->extradata_size = 12;
202 memcpy(st->codec->extradata, comm_chunk, 12);
207 static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
209 VqfContext *c = s->priv_data;
211 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
213 pkt->pos = avio_tell(s->pb);
214 pkt->stream_index = 0;
216 if (av_new_packet(pkt, size+2) < 0)
219 pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
220 pkt->data[1] = c->last_frame_bits;
221 ret = avio_read(s->pb, pkt->data+2, size);
228 c->last_frame_bits = pkt->data[size+1];
229 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
234 static int vqf_read_seek(AVFormatContext *s,
235 int stream_index, int64_t timestamp, int flags)
237 VqfContext *c = s->priv_data;
242 st = s->streams[stream_index];
243 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
245 st->time_base.den * (int64_t)c->frame_bit_len,
246 (flags & AVSEEK_FLAG_BACKWARD) ?
247 AV_ROUND_DOWN : AV_ROUND_UP);
248 pos *= c->frame_bit_len;
250 st->cur_dts = av_rescale(pos, st->time_base.den,
251 st->codec->bit_rate * (int64_t)st->time_base.num);
253 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
256 c->remaining_bits = -7 - ((pos-7)&7);
260 AVInputFormat ff_vqf_demuxer = {
262 .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
263 .priv_data_size = sizeof(VqfContext),
264 .read_probe = vqf_probe,
265 .read_header = vqf_read_header,
266 .read_packet = vqf_read_packet,
267 .read_seek = vqf_read_seek,