vqf: do not set bits_per_coded_sample for TwinVQ.
[libav.git] / libavformat / vqf.c
CommitLineData
616e899c
VS
1/*
2 * VQF demuxer
3 * Copyright (c) 2009 Vitor Sessak
4 *
2912e87a 5 * This file is part of Libav.
616e899c 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
616e899c
VS
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 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
616e899c
VS
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
2912e87a 18 * License along with Libav; if not, write to the Free Software
616e899c
VS
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include "avformat.h"
23#include "libavutil/intreadwrite.h"
d2d67e42 24#include "libavutil/dict.h"
0ebcdf5c 25#include "libavutil/mathematics.h"
616e899c
VS
26
27typedef struct VqfContext {
28 int frame_bit_len;
29 uint8_t last_frame_bits;
30 int remaining_bits;
31} VqfContext;
32
33static int vqf_probe(AVProbeData *probe_packet)
34{
35 if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
36 return 0;
37
38 if (!memcmp(probe_packet->buf + 4, "97012000", 8))
39 return AVPROBE_SCORE_MAX;
40
41 if (!memcmp(probe_packet->buf + 4, "00052200", 8))
42 return AVPROBE_SCORE_MAX;
43
44 return AVPROBE_SCORE_MAX/2;
45}
46
47static void add_metadata(AVFormatContext *s, const char *tag,
48 unsigned int tag_len, unsigned int remaining)
49{
12ad6671
MN
50 int len = FFMIN(tag_len, remaining);
51 char *buf;
616e899c 52
12ad6671
MN
53 if (len == UINT_MAX)
54 return;
616e899c 55
12ad6671
MN
56 buf = av_malloc(len+1);
57 if (!buf)
58 return;
b7effd4e 59 avio_read(s->pb, buf, len);
616e899c 60 buf[len] = 0;
d2d67e42 61 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
616e899c
VS
62}
63
64static int vqf_read_header(AVFormatContext *s, AVFormatParameters *ap)
65{
66 VqfContext *c = s->priv_data;
3b3bbdd3 67 AVStream *st = avformat_new_stream(s, NULL);
616e899c
VS
68 int chunk_tag;
69 int rate_flag = -1;
70 int header_size;
71 int read_bitrate = 0;
72 int size;
73
74 if (!st)
75 return AVERROR(ENOMEM);
76
45a8a02a 77 avio_skip(s->pb, 12);
616e899c 78
b7effd4e 79 header_size = avio_rb32(s->pb);
616e899c 80
72415b2a 81 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
616e899c
VS
82 st->codec->codec_id = CODEC_ID_TWINVQ;
83 st->start_time = 0;
84
85 do {
86 int len;
b7effd4e 87 chunk_tag = avio_rl32(s->pb);
616e899c
VS
88
89 if (chunk_tag == MKTAG('D','A','T','A'))
90 break;
91
b7effd4e 92 len = avio_rb32(s->pb);
616e899c
VS
93
94 if ((unsigned) len > INT_MAX/2) {
95 av_log(s, AV_LOG_ERROR, "Malformed header\n");
96 return -1;
97 }
98
99 header_size -= 8;
100
101 switch(chunk_tag){
102 case MKTAG('C','O','M','M'):
b7effd4e
AK
103 st->codec->channels = avio_rb32(s->pb) + 1;
104 read_bitrate = avio_rb32(s->pb);
105 rate_flag = avio_rb32(s->pb);
45a8a02a 106 avio_skip(s->pb, len-12);
616e899c
VS
107
108 st->codec->bit_rate = read_bitrate*1000;
616e899c
VS
109 break;
110 case MKTAG('N','A','M','E'):
111 add_metadata(s, "title" , len, header_size);
112 break;
113 case MKTAG('(','c',')',' '):
114 add_metadata(s, "copyright", len, header_size);
115 break;
116 case MKTAG('A','U','T','H'):
117 add_metadata(s, "author" , len, header_size);
118 break;
119 case MKTAG('A','L','B','M'):
120 add_metadata(s, "album" , len, header_size);
121 break;
122 case MKTAG('T','R','C','K'):
123 add_metadata(s, "track" , len, header_size);
124 break;
125 case MKTAG('C','O','M','T'):
126 add_metadata(s, "comment" , len, header_size);
127 break;
128 case MKTAG('F','I','L','E'):
129 add_metadata(s, "filename" , len, header_size);
130 break;
131 case MKTAG('D','S','I','Z'):
132 add_metadata(s, "size" , len, header_size);
133 break;
134 case MKTAG('D','A','T','E'):
135 add_metadata(s, "date" , len, header_size);
136 break;
137 case MKTAG('G','E','N','R'):
138 add_metadata(s, "genre" , len, header_size);
139 break;
140 default:
141 av_log(s, AV_LOG_ERROR, "Unknown chunk: %c%c%c%c\n",
142 ((char*)&chunk_tag)[0], ((char*)&chunk_tag)[1],
143 ((char*)&chunk_tag)[2], ((char*)&chunk_tag)[3]);
45a8a02a 144 avio_skip(s->pb, FFMIN(len, header_size));
616e899c
VS
145 break;
146 }
147
148 header_size -= len;
149
150 } while (header_size >= 0);
151
152 switch (rate_flag) {
153 case -1:
154 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
155 return -1;
156 case 44:
157 st->codec->sample_rate = 44100;
158 break;
159 case 22:
160 st->codec->sample_rate = 22050;
161 break;
162 case 11:
163 st->codec->sample_rate = 11025;
164 break;
165 default:
166 st->codec->sample_rate = rate_flag*1000;
167 break;
168 }
169
170 switch (((st->codec->sample_rate/1000) << 8) +
171 read_bitrate/st->codec->channels) {
172 case (11<<8) + 8 :
173 case (8 <<8) + 8 :
174 case (11<<8) + 10:
175 case (22<<8) + 32:
176 size = 512;
177 break;
178 case (16<<8) + 16:
179 case (22<<8) + 20:
180 case (22<<8) + 24:
181 size = 1024;
182 break;
183 case (44<<8) + 40:
184 case (44<<8) + 48:
185 size = 2048;
186 break;
187 default:
188 av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
189 st->codec->sample_rate, st->codec->bit_rate);
190 return -1;
191 }
192 c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
efd8b009 193 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
616e899c
VS
194
195 return 0;
196}
197
198static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
199{
200 VqfContext *c = s->priv_data;
201 int ret;
202 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
203
a2704c97 204 pkt->pos = avio_tell(s->pb);
616e899c
VS
205 pkt->stream_index = 0;
206
207 if (av_new_packet(pkt, size+2) < 0)
208 return AVERROR(EIO);
209
210 pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
211 pkt->data[1] = c->last_frame_bits;
b7effd4e 212 ret = avio_read(s->pb, pkt->data+2, size);
616e899c
VS
213
214 if (ret<=0) {
215 av_free_packet(pkt);
216 return AVERROR(EIO);
217 }
218
219 c->last_frame_bits = pkt->data[size+1];
220 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
221
222 return size+2;
223}
224
225static int vqf_read_seek(AVFormatContext *s,
226 int stream_index, int64_t timestamp, int flags)
227{
228 VqfContext *c = s->priv_data;
229 AVStream *st;
230 int ret;
231 int64_t pos;
232
233 st = s->streams[stream_index];
234 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
235 st->time_base.num,
236 st->time_base.den * (int64_t)c->frame_bit_len,
237 (flags & AVSEEK_FLAG_BACKWARD) ?
238 AV_ROUND_DOWN : AV_ROUND_UP);
239 pos *= c->frame_bit_len;
240
241 st->cur_dts = av_rescale(pos, st->time_base.den,
242 st->codec->bit_rate * (int64_t)st->time_base.num);
243
6b4aa5da 244 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
616e899c
VS
245 return ret;
246
247 c->remaining_bits = -7 - ((pos-7)&7);
248 return 0;
249}
250
c6610a21 251AVInputFormat ff_vqf_demuxer = {
dfc2c4d9
AK
252 .name = "vqf",
253 .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
254 .priv_data_size = sizeof(VqfContext),
255 .read_probe = vqf_probe,
256 .read_header = vqf_read_header,
257 .read_packet = vqf_read_packet,
258 .read_seek = vqf_read_seek,
616e899c
VS
259 .extensions = "vqf",
260};