lavf: make av_set_pts_info private.
[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 "internal.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/mathematics.h"
27
28 typedef struct VqfContext {
29 int frame_bit_len;
30 uint8_t last_frame_bits;
31 int remaining_bits;
32 } VqfContext;
33
34 static int vqf_probe(AVProbeData *probe_packet)
35 {
36 if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
37 return 0;
38
39 if (!memcmp(probe_packet->buf + 4, "97012000", 8))
40 return AVPROBE_SCORE_MAX;
41
42 if (!memcmp(probe_packet->buf + 4, "00052200", 8))
43 return AVPROBE_SCORE_MAX;
44
45 return AVPROBE_SCORE_MAX/2;
46 }
47
48 static void add_metadata(AVFormatContext *s, const char *tag,
49 unsigned int tag_len, unsigned int remaining)
50 {
51 int len = FFMIN(tag_len, remaining);
52 char *buf;
53
54 if (len == UINT_MAX)
55 return;
56
57 buf = av_malloc(len+1);
58 if (!buf)
59 return;
60 avio_read(s->pb, buf, len);
61 buf[len] = 0;
62 av_dict_set(&s->metadata, tag, buf, AV_DICT_DONT_STRDUP_VAL);
63 }
64
65 static int vqf_read_header(AVFormatContext *s, AVFormatParameters *ap)
66 {
67 VqfContext *c = s->priv_data;
68 AVStream *st = avformat_new_stream(s, NULL);
69 int chunk_tag;
70 int rate_flag = -1;
71 int header_size;
72 int read_bitrate = 0;
73 int size;
74 uint8_t comm_chunk[12];
75
76 if (!st)
77 return AVERROR(ENOMEM);
78
79 avio_skip(s->pb, 12);
80
81 header_size = avio_rb32(s->pb);
82
83 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
84 st->codec->codec_id = CODEC_ID_TWINVQ;
85 st->start_time = 0;
86
87 do {
88 int len;
89 chunk_tag = avio_rl32(s->pb);
90
91 if (chunk_tag == MKTAG('D','A','T','A'))
92 break;
93
94 len = avio_rb32(s->pb);
95
96 if ((unsigned) len > INT_MAX/2) {
97 av_log(s, AV_LOG_ERROR, "Malformed header\n");
98 return -1;
99 }
100
101 header_size -= 8;
102
103 switch(chunk_tag){
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);
110
111 st->codec->bit_rate = read_bitrate*1000;
112 break;
113 case MKTAG('N','A','M','E'):
114 add_metadata(s, "title" , len, header_size);
115 break;
116 case MKTAG('(','c',')',' '):
117 add_metadata(s, "copyright", len, header_size);
118 break;
119 case MKTAG('A','U','T','H'):
120 add_metadata(s, "author" , len, header_size);
121 break;
122 case MKTAG('A','L','B','M'):
123 add_metadata(s, "album" , len, header_size);
124 break;
125 case MKTAG('T','R','C','K'):
126 add_metadata(s, "track" , len, header_size);
127 break;
128 case MKTAG('C','O','M','T'):
129 add_metadata(s, "comment" , len, header_size);
130 break;
131 case MKTAG('F','I','L','E'):
132 add_metadata(s, "filename" , len, header_size);
133 break;
134 case MKTAG('D','S','I','Z'):
135 add_metadata(s, "size" , len, header_size);
136 break;
137 case MKTAG('D','A','T','E'):
138 add_metadata(s, "date" , len, header_size);
139 break;
140 case MKTAG('G','E','N','R'):
141 add_metadata(s, "genre" , len, header_size);
142 break;
143 default:
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));
148 break;
149 }
150
151 header_size -= len;
152
153 } while (header_size >= 0);
154
155 switch (rate_flag) {
156 case -1:
157 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
158 return -1;
159 case 44:
160 st->codec->sample_rate = 44100;
161 break;
162 case 22:
163 st->codec->sample_rate = 22050;
164 break;
165 case 11:
166 st->codec->sample_rate = 11025;
167 break;
168 default:
169 st->codec->sample_rate = rate_flag*1000;
170 break;
171 }
172
173 switch (((st->codec->sample_rate/1000) << 8) +
174 read_bitrate/st->codec->channels) {
175 case (11<<8) + 8 :
176 case (8 <<8) + 8 :
177 case (11<<8) + 10:
178 case (22<<8) + 32:
179 size = 512;
180 break;
181 case (16<<8) + 16:
182 case (22<<8) + 20:
183 case (22<<8) + 24:
184 size = 1024;
185 break;
186 case (44<<8) + 40:
187 case (44<<8) + 48:
188 size = 2048;
189 break;
190 default:
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);
193 return -1;
194 }
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);
197
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);
203
204 return 0;
205 }
206
207 static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
208 {
209 VqfContext *c = s->priv_data;
210 int ret;
211 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
212
213 pkt->pos = avio_tell(s->pb);
214 pkt->stream_index = 0;
215
216 if (av_new_packet(pkt, size+2) < 0)
217 return AVERROR(EIO);
218
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);
222
223 if (ret<=0) {
224 av_free_packet(pkt);
225 return AVERROR(EIO);
226 }
227
228 c->last_frame_bits = pkt->data[size+1];
229 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
230
231 return size+2;
232 }
233
234 static int vqf_read_seek(AVFormatContext *s,
235 int stream_index, int64_t timestamp, int flags)
236 {
237 VqfContext *c = s->priv_data;
238 AVStream *st;
239 int ret;
240 int64_t pos;
241
242 st = s->streams[stream_index];
243 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
244 st->time_base.num,
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;
249
250 st->cur_dts = av_rescale(pos, st->time_base.den,
251 st->codec->bit_rate * (int64_t)st->time_base.num);
252
253 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
254 return ret;
255
256 c->remaining_bits = -7 - ((pos-7)&7);
257 return 0;
258 }
259
260 AVInputFormat ff_vqf_demuxer = {
261 .name = "vqf",
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,
268 .extensions = "vqf",
269 };