wav: remove a use of deprecated AV_METADATA_ macro
[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;
67 AVStream *st = av_new_stream(s, 0);
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;
109 st->codec->bits_per_coded_sample = 16;
110 break;
111 case MKTAG('N','A','M','E'):
112 add_metadata(s, "title" , len, header_size);
113 break;
114 case MKTAG('(','c',')',' '):
115 add_metadata(s, "copyright", len, header_size);
116 break;
117 case MKTAG('A','U','T','H'):
118 add_metadata(s, "author" , len, header_size);
119 break;
120 case MKTAG('A','L','B','M'):
121 add_metadata(s, "album" , len, header_size);
122 break;
123 case MKTAG('T','R','C','K'):
124 add_metadata(s, "track" , len, header_size);
125 break;
126 case MKTAG('C','O','M','T'):
127 add_metadata(s, "comment" , len, header_size);
128 break;
129 case MKTAG('F','I','L','E'):
130 add_metadata(s, "filename" , len, header_size);
131 break;
132 case MKTAG('D','S','I','Z'):
133 add_metadata(s, "size" , len, header_size);
134 break;
135 case MKTAG('D','A','T','E'):
136 add_metadata(s, "date" , len, header_size);
137 break;
138 case MKTAG('G','E','N','R'):
139 add_metadata(s, "genre" , len, header_size);
140 break;
141 default:
142 av_log(s, AV_LOG_ERROR, "Unknown chunk: %c%c%c%c\n",
143 ((char*)&chunk_tag)[0], ((char*)&chunk_tag)[1],
144 ((char*)&chunk_tag)[2], ((char*)&chunk_tag)[3]);
45a8a02a 145 avio_skip(s->pb, FFMIN(len, header_size));
616e899c
VS
146 break;
147 }
148
149 header_size -= len;
150
151 } while (header_size >= 0);
152
153 switch (rate_flag) {
154 case -1:
155 av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
156 return -1;
157 case 44:
158 st->codec->sample_rate = 44100;
159 break;
160 case 22:
161 st->codec->sample_rate = 22050;
162 break;
163 case 11:
164 st->codec->sample_rate = 11025;
165 break;
166 default:
167 st->codec->sample_rate = rate_flag*1000;
168 break;
169 }
170
171 switch (((st->codec->sample_rate/1000) << 8) +
172 read_bitrate/st->codec->channels) {
173 case (11<<8) + 8 :
174 case (8 <<8) + 8 :
175 case (11<<8) + 10:
176 case (22<<8) + 32:
177 size = 512;
178 break;
179 case (16<<8) + 16:
180 case (22<<8) + 20:
181 case (22<<8) + 24:
182 size = 1024;
183 break;
184 case (44<<8) + 40:
185 case (44<<8) + 48:
186 size = 2048;
187 break;
188 default:
189 av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
190 st->codec->sample_rate, st->codec->bit_rate);
191 return -1;
192 }
193 c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
efd8b009 194 av_set_pts_info(st, 64, 1, st->codec->sample_rate);
616e899c
VS
195
196 return 0;
197}
198
199static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
200{
201 VqfContext *c = s->priv_data;
202 int ret;
203 int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
204
a2704c97 205 pkt->pos = avio_tell(s->pb);
616e899c
VS
206 pkt->stream_index = 0;
207
208 if (av_new_packet(pkt, size+2) < 0)
209 return AVERROR(EIO);
210
211 pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
212 pkt->data[1] = c->last_frame_bits;
b7effd4e 213 ret = avio_read(s->pb, pkt->data+2, size);
616e899c
VS
214
215 if (ret<=0) {
216 av_free_packet(pkt);
217 return AVERROR(EIO);
218 }
219
220 c->last_frame_bits = pkt->data[size+1];
221 c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
222
223 return size+2;
224}
225
226static int vqf_read_seek(AVFormatContext *s,
227 int stream_index, int64_t timestamp, int flags)
228{
229 VqfContext *c = s->priv_data;
230 AVStream *st;
231 int ret;
232 int64_t pos;
233
234 st = s->streams[stream_index];
235 pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
236 st->time_base.num,
237 st->time_base.den * (int64_t)c->frame_bit_len,
238 (flags & AVSEEK_FLAG_BACKWARD) ?
239 AV_ROUND_DOWN : AV_ROUND_UP);
240 pos *= c->frame_bit_len;
241
242 st->cur_dts = av_rescale(pos, st->time_base.den,
243 st->codec->bit_rate * (int64_t)st->time_base.num);
244
6b4aa5da 245 if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
616e899c
VS
246 return ret;
247
248 c->remaining_bits = -7 - ((pos-7)&7);
249 return 0;
250}
251
c6610a21 252AVInputFormat ff_vqf_demuxer = {
616e899c
VS
253 "vqf",
254 NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
255 sizeof(VqfContext),
256 vqf_probe,
257 vqf_read_header,
258 vqf_read_packet,
259 NULL,
260 vqf_read_seek,
261 .extensions = "vqf",
262};