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