vqf/twinvq: pass vqf COMM chunk info in extradata
[libav.git] / libavformat / vqf.c
1 /*
2 * VQF demuxer
3 * Copyright (c) 2009 Vitor Sessak
4 *
5 * This file is part of Libav.
6 *
7 * Libav 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.
11 *
12 * Libav 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.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "avformat.h"
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/dict.h"
25 #include "libavutil/mathematics.h"
26
27 typedef struct VqfContext {
28 int frame_bit_len;
29 uint8_t last_frame_bits;
30 int remaining_bits;
31 } VqfContext;
32
33 static 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
47 static void add_metadata(AVFormatContext *s, const char *tag,
48 unsigned int tag_len, unsigned int remaining)
49 {
50 int len = FFMIN(tag_len, remaining);
51 char *buf;
52
53 if (len == UINT_MAX)
54 return;
55
56 buf = av_malloc(len+1);
57 if (!buf)
58 return;
59 avio_read(s->pb, buf, len);
60 buf[len] = 0;
61 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
62 }
63
64 static int vqf_read_header(AVFormatContext *s, AVFormatParameters *ap)
65 {
66 VqfContext *c = s->priv_data;
67 AVStream *st = avformat_new_stream(s, NULL);
68 int chunk_tag;
69 int rate_flag = -1;
70 int header_size;
71 int read_bitrate = 0;
72 int size;
73 uint8_t comm_chunk[12];
74
75 if (!st)
76 return AVERROR(ENOMEM);
77
78 avio_skip(s->pb, 12);
79
80 header_size = avio_rb32(s->pb);
81
82 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
83 st->codec->codec_id = CODEC_ID_TWINVQ;
84 st->start_time = 0;
85
86 do {
87 int len;
88 chunk_tag = avio_rl32(s->pb);
89
90 if (chunk_tag == MKTAG('D','A','T','A'))
91 break;
92
93 len = avio_rb32(s->pb);
94
95 if ((unsigned) len > INT_MAX/2) {
96 av_log(s, AV_LOG_ERROR, "Malformed header\n");
97 return -1;
98 }
99
100 header_size -= 8;
101
102 switch(chunk_tag){
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);
109
110 st->codec->bit_rate = read_bitrate*1000;
111 break;
112 case MKTAG('N','A','M','E'):
113 add_metadata(s, "title" , len, header_size);
114 break;
115 case MKTAG('(','c',')',' '):
116 add_metadata(s, "copyright", len, header_size);
117 break;
118 case MKTAG('A','U','T','H'):
119 add_metadata(s, "author" , len, header_size);
120 break;
121 case MKTAG('A','L','B','M'):
122 add_metadata(s, "album" , len, header_size);
123 break;
124 case MKTAG('T','R','C','K'):
125 add_metadata(s, "track" , len, header_size);
126 break;
127 case MKTAG('C','O','M','T'):
128 add_metadata(s, "comment" , len, header_size);
129 break;
130 case MKTAG('F','I','L','E'):
131 add_metadata(s, "filename" , len, header_size);
132 break;
133 case MKTAG('D','S','I','Z'):
134 add_metadata(s, "size" , len, header_size);
135 break;
136 case MKTAG('D','A','T','E'):
137 add_metadata(s, "date" , len, header_size);
138 break;
139 case MKTAG('G','E','N','R'):
140 add_metadata(s, "genre" , len, header_size);
141 break;
142 default:
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));
147 break;
148 }
149
150 header_size -= len;
151
152 } while (header_size >= 0);
153
154 switch (rate_flag) {
155 case -1:
156 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
157 return -1;
158 case 44:
159 st->codec->sample_rate = 44100;
160 break;
161 case 22:
162 st->codec->sample_rate = 22050;
163 break;
164 case 11:
165 st->codec->sample_rate = 11025;
166 break;
167 default:
168 st->codec->sample_rate = rate_flag*1000;
169 break;
170 }
171
172 switch (((st->codec->sample_rate/1000) << 8) +
173 read_bitrate/st->codec->channels) {
174 case (11<<8) + 8 :
175 case (8 <<8) + 8 :
176 case (11<<8) + 10:
177 case (22<<8) + 32:
178 size = 512;
179 break;
180 case (16<<8) + 16:
181 case (22<<8) + 20:
182 case (22<<8) + 24:
183 size = 1024;
184 break;
185 case (44<<8) + 40:
186 case (44<<8) + 48:
187 size = 2048;
188 break;
189 default:
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);
192 return -1;
193 }
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);
196
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);
202
203 return 0;
204 }
205
206 static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
207 {
208 VqfContext *c = s->priv_data;
209 int ret;
210 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
211
212 pkt->pos = avio_tell(s->pb);
213 pkt->stream_index = 0;
214
215 if (av_new_packet(pkt, size+2) < 0)
216 return AVERROR(EIO);
217
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);
221
222 if (ret<=0) {
223 av_free_packet(pkt);
224 return AVERROR(EIO);
225 }
226
227 c->last_frame_bits = pkt->data[size+1];
228 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
229
230 return size+2;
231 }
232
233 static int vqf_read_seek(AVFormatContext *s,
234 int stream_index, int64_t timestamp, int flags)
235 {
236 VqfContext *c = s->priv_data;
237 AVStream *st;
238 int ret;
239 int64_t pos;
240
241 st = s->streams[stream_index];
242 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
243 st->time_base.num,
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;
248
249 st->cur_dts = av_rescale(pos, st->time_base.den,
250 st->codec->bit_rate * (int64_t)st->time_base.num);
251
252 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
253 return ret;
254
255 c->remaining_bits = -7 - ((pos-7)&7);
256 return 0;
257 }
258
259 AVInputFormat ff_vqf_demuxer = {
260 .name = "vqf",
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,
267 .extensions = "vqf",
268 };