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