FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vqf.c
Go to the documentation of this file.
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 "internal.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/dict.h"
26 #include "libavutil/mathematics.h"
27 #include "riff.h"
28 
29 typedef struct VqfContext {
33 } VqfContext;
34 
35 static int vqf_probe(AVProbeData *probe_packet)
36 {
37  if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
38  return 0;
39 
40  if (!memcmp(probe_packet->buf + 4, "97012000", 8))
41  return AVPROBE_SCORE_MAX;
42 
43  if (!memcmp(probe_packet->buf + 4, "00052200", 8))
44  return AVPROBE_SCORE_MAX;
45 
46  if (AV_RL32(probe_packet->buf + 12) > (1<<27))
47  return AVPROBE_SCORE_EXTENSION/2;
48 
50 }
51 
52 static void add_metadata(AVFormatContext *s, uint32_t tag,
53  unsigned int tag_len, unsigned int remaining)
54 {
55  int len = FFMIN(tag_len, remaining);
56  char *buf, key[5] = {0};
57 
58  if (len == UINT_MAX)
59  return;
60 
61  buf = av_malloc(len+1);
62  if (!buf)
63  return;
64  avio_read(s->pb, buf, len);
65  buf[len] = 0;
66  AV_WL32(key, tag);
68 }
69 
71  { "(c) ", "copyright" },
72  { "ARNG", "arranger" },
73  { "AUTH", "author" },
74  { "BAND", "band" },
75  { "CDCT", "conductor" },
76  { "COMT", "comment" },
77  { "FILE", "filename" },
78  { "GENR", "genre" },
79  { "LABL", "publisher" },
80  { "MUSC", "composer" },
81  { "NAME", "title" },
82  { "NOTE", "note" },
83  { "PROD", "producer" },
84  { "PRSN", "personnel" },
85  { "REMX", "remixer" },
86  { "SING", "singer" },
87  { "TRCK", "track" },
88  { "WORD", "words" },
89  { 0 },
90 };
91 
93 {
94  VqfContext *c = s->priv_data;
95  AVStream *st = avformat_new_stream(s, NULL);
96  int chunk_tag;
97  int rate_flag = -1;
98  int header_size;
99  int read_bitrate = 0;
100  int size;
101  uint8_t comm_chunk[12];
102 
103  if (!st)
104  return AVERROR(ENOMEM);
105 
106  avio_skip(s->pb, 12);
107 
108  header_size = avio_rb32(s->pb);
109 
112  st->start_time = 0;
113 
114  do {
115  int len;
116  chunk_tag = avio_rl32(s->pb);
117 
118  if (chunk_tag == MKTAG('D','A','T','A'))
119  break;
120 
121  len = avio_rb32(s->pb);
122 
123  if ((unsigned) len > INT_MAX/2) {
124  av_log(s, AV_LOG_ERROR, "Malformed header\n");
125  return -1;
126  }
127 
128  header_size -= 8;
129 
130  switch(chunk_tag){
131  case MKTAG('C','O','M','M'):
132  avio_read(s->pb, comm_chunk, 12);
133  st->codec->channels = AV_RB32(comm_chunk ) + 1;
134  read_bitrate = AV_RB32(comm_chunk + 4);
135  rate_flag = AV_RB32(comm_chunk + 8);
136  avio_skip(s->pb, len-12);
137 
138  if (st->codec->channels <= 0) {
139  av_log(s, AV_LOG_ERROR, "Invalid number of channels\n");
140  return AVERROR_INVALIDDATA;
141  }
142 
143  st->codec->bit_rate = read_bitrate*1000;
144  break;
145  case MKTAG('D','S','I','Z'): // size of compressed data
146  {
147  char buf[8] = {0};
148  int size = avio_rb32(s->pb);
149 
150  snprintf(buf, sizeof(buf), "%d", size);
151  av_dict_set(&s->metadata, "size", buf, 0);
152  }
153  break;
154  case MKTAG('Y','E','A','R'): // recording date
155  case MKTAG('E','N','C','D'): // compression date
156  case MKTAG('E','X','T','R'): // reserved
157  case MKTAG('_','Y','M','H'): // reserved
158  case MKTAG('_','N','T','T'): // reserved
159  case MKTAG('_','I','D','3'): // reserved for ID3 tags
160  avio_skip(s->pb, FFMIN(len, header_size));
161  break;
162  default:
163  add_metadata(s, chunk_tag, len, header_size);
164  break;
165  }
166 
167  header_size -= len;
168 
169  } while (header_size >= 0 && !url_feof(s->pb));
170 
171  switch (rate_flag) {
172  case -1:
173  av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
174  return -1;
175  case 44:
176  st->codec->sample_rate = 44100;
177  break;
178  case 22:
179  st->codec->sample_rate = 22050;
180  break;
181  case 11:
182  st->codec->sample_rate = 11025;
183  break;
184  default:
185  if (rate_flag < 8 || rate_flag > 44) {
186  av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag);
187  return AVERROR_INVALIDDATA;
188  }
189  st->codec->sample_rate = rate_flag*1000;
190  break;
191  }
192 
193  if (read_bitrate / st->codec->channels < 8 ||
194  read_bitrate / st->codec->channels > 48) {
195  av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n",
196  read_bitrate / st->codec->channels);
197  return AVERROR_INVALIDDATA;
198  }
199 
200  switch (((st->codec->sample_rate/1000) << 8) +
201  read_bitrate/st->codec->channels) {
202  case (11<<8) + 8 :
203  case (8 <<8) + 8 :
204  case (11<<8) + 10:
205  case (22<<8) + 32:
206  size = 512;
207  break;
208  case (16<<8) + 16:
209  case (22<<8) + 20:
210  case (22<<8) + 24:
211  size = 1024;
212  break;
213  case (44<<8) + 40:
214  case (44<<8) + 48:
215  size = 2048;
216  break;
217  default:
218  av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
219  st->codec->sample_rate, st->codec->bit_rate);
220  return -1;
221  }
222  c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
223  avpriv_set_pts_info(st, 64, size, st->codec->sample_rate);
224 
225  /* put first 12 bytes of COMM chunk in extradata */
226  if (ff_alloc_extradata(st->codec, 12))
227  return AVERROR(ENOMEM);
228  memcpy(st->codec->extradata, comm_chunk, 12);
229 
230  ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv);
231 
232  return 0;
233 }
234 
236 {
237  VqfContext *c = s->priv_data;
238  int ret;
239  int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
240 
241  if (av_new_packet(pkt, size+2) < 0)
242  return AVERROR(EIO);
243 
244  pkt->pos = avio_tell(s->pb);
245  pkt->stream_index = 0;
246  pkt->duration = 1;
247 
248  pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
249  pkt->data[1] = c->last_frame_bits;
250  ret = avio_read(s->pb, pkt->data+2, size);
251 
252  if (ret != size) {
253  av_free_packet(pkt);
254  return AVERROR(EIO);
255  }
256 
257  c->last_frame_bits = pkt->data[size+1];
258  c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
259 
260  return size+2;
261 }
262 
264  int stream_index, int64_t timestamp, int flags)
265 {
266  VqfContext *c = s->priv_data;
267  AVStream *st;
268  int ret;
269  int64_t pos;
270 
271  st = s->streams[stream_index];
272  pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
273  st->time_base.num,
274  st->time_base.den * (int64_t)c->frame_bit_len,
275  (flags & AVSEEK_FLAG_BACKWARD) ?
277  pos *= c->frame_bit_len;
278 
279  st->cur_dts = av_rescale(pos, st->time_base.den,
280  st->codec->bit_rate * (int64_t)st->time_base.num);
281 
282  if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
283  return ret;
284 
285  c->remaining_bits = -7 - ((pos-7)&7);
286  return 0;
287 }
288 
290  .name = "vqf",
291  .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
292  .priv_data_size = sizeof(VqfContext),
297  .extensions = "vqf,vql,vqe",
298 };