Libav
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 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 #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 
47 }
48 
49 static void add_metadata(AVFormatContext *s, uint32_t tag,
50  unsigned int tag_len, unsigned int remaining)
51 {
52  int len = FFMIN(tag_len, remaining);
53  char *buf, key[5] = {0};
54 
55  if (len == UINT_MAX)
56  return;
57 
58  buf = av_malloc(len+1);
59  if (!buf)
60  return;
61  avio_read(s->pb, buf, len);
62  buf[len] = 0;
63  AV_WL32(key, tag);
65 }
66 
68  { "(c) ", "copyright" },
69  { "ARNG", "arranger" },
70  { "AUTH", "author" },
71  { "BAND", "band" },
72  { "CDCT", "conductor" },
73  { "COMT", "comment" },
74  { "FILE", "filename" },
75  { "GENR", "genre" },
76  { "LABL", "publisher" },
77  { "MUSC", "composer" },
78  { "NAME", "title" },
79  { "NOTE", "note" },
80  { "PROD", "producer" },
81  { "PRSN", "personnel" },
82  { "REMX", "remixer" },
83  { "SING", "singer" },
84  { "TRCK", "track" },
85  { "WORD", "words" },
86  { 0 },
87 };
88 
90 {
91  VqfContext *c = s->priv_data;
93  int chunk_tag;
94  int rate_flag = -1;
95  int header_size;
96  int read_bitrate = 0;
97  int size;
98  uint8_t comm_chunk[12];
99 
100  if (!st)
101  return AVERROR(ENOMEM);
102 
103  avio_skip(s->pb, 12);
104 
105  header_size = avio_rb32(s->pb);
106 
109  st->start_time = 0;
110 
111  do {
112  int len;
113  chunk_tag = avio_rl32(s->pb);
114 
115  if (chunk_tag == MKTAG('D','A','T','A'))
116  break;
117 
118  len = avio_rb32(s->pb);
119 
120  if ((unsigned) len > INT_MAX/2) {
121  av_log(s, AV_LOG_ERROR, "Malformed header\n");
122  return -1;
123  }
124 
125  header_size -= 8;
126 
127  switch(chunk_tag){
128  case MKTAG('C','O','M','M'):
129  avio_read(s->pb, comm_chunk, 12);
130  st->codec->channels = AV_RB32(comm_chunk ) + 1;
131  read_bitrate = AV_RB32(comm_chunk + 4);
132  rate_flag = AV_RB32(comm_chunk + 8);
133  avio_skip(s->pb, len-12);
134 
135  st->codec->bit_rate = read_bitrate*1000;
136  break;
137  case MKTAG('D','S','I','Z'): // size of compressed data
138  {
139  char buf[8] = {0};
140  int size = avio_rb32(s->pb);
141 
142  snprintf(buf, sizeof(buf), "%d", size);
143  av_dict_set(&s->metadata, "size", buf, 0);
144  }
145  break;
146  case MKTAG('Y','E','A','R'): // recording date
147  case MKTAG('E','N','C','D'): // compression date
148  case MKTAG('E','X','T','R'): // reserved
149  case MKTAG('_','Y','M','H'): // reserved
150  case MKTAG('_','N','T','T'): // reserved
151  case MKTAG('_','I','D','3'): // reserved for ID3 tags
152  avio_skip(s->pb, FFMIN(len, header_size));
153  break;
154  default:
155  add_metadata(s, chunk_tag, len, header_size);
156  break;
157  }
158 
159  header_size -= len;
160 
161  } while (header_size >= 0);
162 
163  switch (rate_flag) {
164  case -1:
165  av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
166  return -1;
167  case 44:
168  st->codec->sample_rate = 44100;
169  break;
170  case 22:
171  st->codec->sample_rate = 22050;
172  break;
173  case 11:
174  st->codec->sample_rate = 11025;
175  break;
176  default:
177  if (rate_flag < 8 || rate_flag > 44) {
178  av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag);
179  return AVERROR_INVALIDDATA;
180  }
181  st->codec->sample_rate = rate_flag*1000;
182  break;
183  }
184 
185  if (read_bitrate / st->codec->channels < 8 ||
186  read_bitrate / st->codec->channels > 48) {
187  av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n",
188  read_bitrate / st->codec->channels);
189  return AVERROR_INVALIDDATA;
190  }
191 
192  switch (((st->codec->sample_rate/1000) << 8) +
193  read_bitrate/st->codec->channels) {
194  case (11<<8) + 8 :
195  case (8 <<8) + 8 :
196  case (11<<8) + 10:
197  case (22<<8) + 32:
198  size = 512;
199  break;
200  case (16<<8) + 16:
201  case (22<<8) + 20:
202  case (22<<8) + 24:
203  size = 1024;
204  break;
205  case (44<<8) + 40:
206  case (44<<8) + 48:
207  size = 2048;
208  break;
209  default:
210  av_log(s, AV_LOG_ERROR, "Mode not suported: %d Hz, %d kb/s.\n",
211  st->codec->sample_rate, st->codec->bit_rate);
212  return -1;
213  }
214  c->frame_bit_len = st->codec->bit_rate*size/st->codec->sample_rate;
215  avpriv_set_pts_info(st, 64, size, st->codec->sample_rate);
216 
217  /* put first 12 bytes of COMM chunk in extradata */
219  return AVERROR(ENOMEM);
220  st->codec->extradata_size = 12;
221  memcpy(st->codec->extradata, comm_chunk, 12);
222 
223  ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv);
224 
225  return 0;
226 }
227 
229 {
230  VqfContext *c = s->priv_data;
231  int ret;
232  int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
233 
234  if (av_new_packet(pkt, size+2) < 0)
235  return AVERROR(EIO);
236 
237  pkt->pos = avio_tell(s->pb);
238  pkt->stream_index = 0;
239  pkt->duration = 1;
240 
241  pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
242  pkt->data[1] = c->last_frame_bits;
243  ret = avio_read(s->pb, pkt->data+2, size);
244 
245  if (ret<=0) {
246  av_free_packet(pkt);
247  return AVERROR(EIO);
248  }
249 
250  c->last_frame_bits = pkt->data[size+1];
251  c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
252 
253  return size+2;
254 }
255 
257  int stream_index, int64_t timestamp, int flags)
258 {
259  VqfContext *c = s->priv_data;
260  AVStream *st;
261  int ret;
262  int64_t pos;
263 
264  st = s->streams[stream_index];
265  pos = av_rescale_rnd(timestamp * st->codec->bit_rate,
266  st->time_base.num,
267  st->time_base.den * (int64_t)c->frame_bit_len,
268  (flags & AVSEEK_FLAG_BACKWARD) ?
270  pos *= c->frame_bit_len;
271 
272  st->cur_dts = av_rescale(pos, st->time_base.den,
273  st->codec->bit_rate * (int64_t)st->time_base.num);
274 
275  if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->data_offset, SEEK_SET)) < 0)
276  return ret;
277 
278  c->remaining_bits = -7 - ((pos-7)&7);
279  return 0;
280 }
281 
283  .name = "vqf",
284  .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
285  .priv_data_size = sizeof(VqfContext),
290  .extensions = "vqf,vql,vqe",
291 };
static int vqf_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: vqf.c:256
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:243
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:525
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding) av_const
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:61
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:2821
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:718
int num
numerator
Definition: rational.h:44
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:564
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
int remaining_bits
Definition: vqf.c:32
Definition: vqf.c:29
int64_t data_offset
offset of the first packet
Definition: avformat.h:1220
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:595
static int vqf_probe(AVProbeData *probe_packet)
Definition: vqf.c:35
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1164
Format I/O context.
Definition: avformat.h:922
int64_t cur_dts
Definition: avformat.h:851
int frame_bit_len
Definition: vqf.c:30
uint8_t
Round toward +infinity.
Definition: mathematics.h:53
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:397
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:219
static int flags
Definition: log.c:44
uint32_t tag
Definition: movenc.c:844
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2518
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
#define AV_WL32(p, d)
Definition: intreadwrite.h:255
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
void * priv_data
Format private data.
Definition: avformat.h:950
#define AVERROR(e)
Definition: error.h:43
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:531
AVInputFormat ff_vqf_demuxer
Definition: vqf.c:282
int bit_rate
the average bitrate
Definition: avcodec.h:1114
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:116
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() and chilren.
Definition: dict.h:66
static int read_probe(AVProbeData *pd)
Definition: jvdec.c:55
uint8_t last_frame_bits
Definition: vqf.c:31
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:990
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:402
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
static int vqf_read_header(AVFormatContext *s)
Definition: vqf.c:89
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:186
static const AVMetadataConv vqf_metadata_conv[]
Definition: vqf.c:67
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:544
Stream structure.
Definition: avformat.h:699
NULL
Definition: eval.c:55
enum AVMediaType codec_type
Definition: avcodec.h:1058
enum AVCodecID codec_id
Definition: avcodec.h:1067
int sample_rate
samples per second
Definition: avcodec.h:1791
static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: vqf.c:228
AVIOContext * pb
I/O context.
Definition: avformat.h:964
int extradata_size
Definition: avcodec.h:1165
static int read_packet(AVFormatContext *ctx, AVPacket *pkt)
Definition: libcdio.c:114
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:68
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1609
uint8_t * data
Definition: avcodec.h:973
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1130
This structure contains the data a format has to probe a file.
Definition: avformat.h:395
Round toward -infinity.
Definition: mathematics.h:52
#define MKTAG(a, b, c, d)
Definition: common.h:238
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:404
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:452
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:749
Main libavformat public API header.
int den
denominator
Definition: rational.h:45
#define AV_RB32(x)
Definition: intreadwrite.h:232
#define AV_RL32(x)
Definition: intreadwrite.h:248
int len
int channels
number of audio channels
Definition: avcodec.h:1792
int stream_index
Definition: avcodec.h:975
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:741
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
This structure stores compressed data.
Definition: avcodec.h:950
static void add_metadata(AVFormatContext *s, uint32_t tag, unsigned int tag_len, unsigned int remaining)
Definition: vqf.c:49
#define FFMIN(a, b)
Definition: common.h:57