FFmpeg
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 "demux.h"
24 #include "internal.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/dict.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 #include "metadata.h"
30 
31 typedef struct VqfContext {
33  uint8_t last_frame_bits;
35 } VqfContext;
36 
37 static int vqf_probe(const AVProbeData *probe_packet)
38 {
39  if (AV_RL32(probe_packet->buf) != MKTAG('T','W','I','N'))
40  return 0;
41 
42  if (!memcmp(probe_packet->buf + 4, "97012000", 8))
43  return AVPROBE_SCORE_MAX;
44 
45  if (!memcmp(probe_packet->buf + 4, "00052200", 8))
46  return AVPROBE_SCORE_MAX;
47 
48  if (AV_RL32(probe_packet->buf + 12) > (1<<27))
49  return AVPROBE_SCORE_EXTENSION/2;
50 
52 }
53 
54 static void add_metadata(AVFormatContext *s, uint32_t tag,
55  unsigned int tag_len, unsigned int remaining)
56 {
57  int len = FFMIN(tag_len, remaining);
58  char *buf, key[5] = {0};
59 
60  if (len == UINT_MAX)
61  return;
62 
63  buf = av_malloc(len+1);
64  if (!buf)
65  return;
66  avio_read(s->pb, buf, len);
67  buf[len] = 0;
68  AV_WL32(key, tag);
69  av_dict_set(&s->metadata, key, buf, AV_DICT_DONT_STRDUP_VAL);
70 }
71 
73  { "(c) ", "copyright" },
74  { "ARNG", "arranger" },
75  { "AUTH", "author" },
76  { "BAND", "band" },
77  { "CDCT", "conductor" },
78  { "COMT", "comment" },
79  { "FILE", "filename" },
80  { "GENR", "genre" },
81  { "LABL", "publisher" },
82  { "MUSC", "composer" },
83  { "NAME", "title" },
84  { "NOTE", "note" },
85  { "PROD", "producer" },
86  { "PRSN", "personnel" },
87  { "REMX", "remixer" },
88  { "SING", "singer" },
89  { "TRCK", "track" },
90  { "WORD", "words" },
91  { 0 },
92 };
93 
95 {
96  VqfContext *c = s->priv_data;
98  int chunk_tag;
99  int rate_flag = -1;
100  int header_size;
101  int read_bitrate = 0;
102  int size, ret;
103  uint8_t comm_chunk[12];
104 
105  if (!st)
106  return AVERROR(ENOMEM);
107 
108  avio_skip(s->pb, 12);
109 
110  header_size = avio_rb32(s->pb);
111 
112  if (header_size < 0)
113  return AVERROR_INVALIDDATA;
114 
117  st->start_time = 0;
118 
119  do {
120  int len;
121  chunk_tag = avio_rl32(s->pb);
122 
123  if (chunk_tag == MKTAG('D','A','T','A'))
124  break;
125 
126  len = avio_rb32(s->pb);
127 
128  if ((unsigned) len > INT_MAX/2 || header_size < 8) {
129  av_log(s, AV_LOG_ERROR, "Malformed header\n");
130  return -1;
131  }
132 
133  header_size -= 8;
134 
135  switch(chunk_tag){
136  case MKTAG('C','O','M','M'):
137  if (len < 12)
138  return AVERROR_INVALIDDATA;
139 
140  avio_read(s->pb, comm_chunk, 12);
141  st->codecpar->ch_layout.nb_channels = AV_RB32(comm_chunk) + 1;
142  read_bitrate = AV_RB32(comm_chunk + 4);
143  rate_flag = AV_RB32(comm_chunk + 8);
144  avio_skip(s->pb, len-12);
145 
146  if (st->codecpar->ch_layout.nb_channels <= 0) {
147  av_log(s, AV_LOG_ERROR, "Invalid number of channels\n");
148  return AVERROR_INVALIDDATA;
149  }
150 
151  st->codecpar->bit_rate = (int64_t)read_bitrate * 1000;
152  break;
153  case MKTAG('D','S','I','Z'): // size of compressed data
154  {
155  av_dict_set_int(&s->metadata, "size", avio_rb32(s->pb), 0);
156  }
157  break;
158  case MKTAG('Y','E','A','R'): // recording date
159  case MKTAG('E','N','C','D'): // compression date
160  case MKTAG('E','X','T','R'): // reserved
161  case MKTAG('_','Y','M','H'): // reserved
162  case MKTAG('_','N','T','T'): // reserved
163  case MKTAG('_','I','D','3'): // reserved for ID3 tags
164  avio_skip(s->pb, FFMIN(len, header_size));
165  break;
166  default:
167  add_metadata(s, chunk_tag, len, header_size);
168  break;
169  }
170 
171  header_size -= len;
172 
173  } while (header_size >= 0 && !avio_feof(s->pb));
174 
175  switch (rate_flag) {
176  case -1:
177  av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
178  return -1;
179  case 44:
180  st->codecpar->sample_rate = 44100;
181  break;
182  case 22:
183  st->codecpar->sample_rate = 22050;
184  break;
185  case 11:
186  st->codecpar->sample_rate = 11025;
187  break;
188  default:
189  if (rate_flag < 8 || rate_flag > 44) {
190  av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag);
191  return AVERROR_INVALIDDATA;
192  }
193  st->codecpar->sample_rate = rate_flag*1000;
194  break;
195  }
196 
197  if (read_bitrate / st->codecpar->ch_layout.nb_channels < 8 ||
198  read_bitrate / st->codecpar->ch_layout.nb_channels > 48) {
199  av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n",
200  read_bitrate / st->codecpar->ch_layout.nb_channels);
201  return AVERROR_INVALIDDATA;
202  }
203 
204  switch (((st->codecpar->sample_rate/1000) << 8) +
205  read_bitrate/st->codecpar->ch_layout.nb_channels) {
206  case (11<<8) + 8 :
207  case (8 <<8) + 8 :
208  case (11<<8) + 10:
209  case (22<<8) + 32:
210  size = 512;
211  break;
212  case (16<<8) + 16:
213  case (22<<8) + 20:
214  case (22<<8) + 24:
215  size = 1024;
216  break;
217  case (44<<8) + 40:
218  case (44<<8) + 48:
219  size = 2048;
220  break;
221  default:
222  av_log(s, AV_LOG_ERROR, "Mode not supported: %d Hz, %"PRId64" kb/s.\n",
224  return -1;
225  }
226  c->frame_bit_len = st->codecpar->bit_rate*size/st->codecpar->sample_rate;
228 
229  /* put first 12 bytes of COMM chunk in extradata */
230  if ((ret = ff_alloc_extradata(st->codecpar, 12)) < 0)
231  return ret;
232  memcpy(st->codecpar->extradata, comm_chunk, 12);
233 
235 
236  return 0;
237 }
238 
240 {
241  VqfContext *c = s->priv_data;
242  int ret;
243  int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
244 
245  if ((ret = av_new_packet(pkt, size + 2)) < 0)
246  return ret;
247 
248  pkt->pos = avio_tell(s->pb);
249  pkt->stream_index = 0;
250  pkt->duration = 1;
251 
252  pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
253  pkt->data[1] = c->last_frame_bits;
254  ret = avio_read(s->pb, pkt->data+2, size);
255 
256  if (ret != size) {
257  return AVERROR(EIO);
258  }
259 
260  c->last_frame_bits = pkt->data[size+1];
261  c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
262 
263  return 0;
264 }
265 
267  int stream_index, int64_t timestamp, int flags)
268 {
269  VqfContext *c = s->priv_data;
270  AVStream *st;
271  int64_t ret;
272  int64_t pos;
273 
274  st = s->streams[stream_index];
275  pos = av_rescale_rnd(timestamp * st->codecpar->bit_rate,
276  st->time_base.num,
277  st->time_base.den * (int64_t)c->frame_bit_len,
280  pos *= c->frame_bit_len;
281 
283  st->codecpar->bit_rate * (int64_t)st->time_base.num);
284 
285  if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + ffformatcontext(s)->data_offset, SEEK_SET)) < 0)
286  return ret;
287 
288  c->remaining_bits = -7 - ((pos-7)&7);
289  return 0;
290 }
291 
293  .p.name = "vqf",
294  .p.long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
295  .p.extensions = "vqf,vql,vqe",
296  .priv_data_size = sizeof(VqfContext),
301 };
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:134
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:69
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
vqf_probe
static int vqf_probe(const AVProbeData *probe_packet)
Definition: vqf.c:37
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:422
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:127
vqf_read_seek
static int vqf_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: vqf.c:266
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int64_t
long long int64_t
Definition: coverity.c:34
add_metadata
static void add_metadata(AVFormatContext *s, uint32_t tag, unsigned int tag_len, unsigned int remaining)
Definition: vqf.c:54
VqfContext::remaining_bits
int remaining_bits
Definition: vqf.c:34
AVPacket::data
uint8_t * data
Definition: packet.h:539
VqfContext
Definition: vqf.c:31
AVMetadataConv
Definition: metadata.h:34
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:557
mathematics.h
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVPROBE_SCORE_MAX
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:463
VqfContext::last_frame_bits
uint8_t last_frame_bits
Definition: vqf.c:33
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:867
vqf_metadata_conv
static const AVMetadataConv vqf_metadata_conv[]
Definition: vqf.c:72
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:358
read_seek
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:151
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
AVRational::num
int num
Numerator.
Definition: rational.h:59
AV_DICT_DONT_STRDUP_VAL
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that's been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:79
avio_rb32
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:761
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
av_new_packet
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: packet.c:98
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
key
const char * key
Definition: hwcontext_opencl.c:189
vqf_read_packet
static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: vqf.c:239
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2498
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:787
NULL
#define NULL
Definition: coverity.c:32
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:180
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AVPROBE_SCORE_EXTENSION
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:461
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:184
AV_ROUND_DOWN
@ AV_ROUND_DOWN
Round toward -infinity.
Definition: mathematics.h:133
av_rescale_rnd
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:730
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
size
int size
Definition: twinvq_data.h:10344
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
vqf_read_header
static int vqf_read_header(AVFormatContext *s)
Definition: vqf.c:94
ff_vqf_demuxer
const FFInputFormat ff_vqf_demuxer
Definition: vqf.c:292
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
demux.h
len
int len
Definition: vorbis_enc_data.h:426
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
tag
uint32_t tag
Definition: movenc.c:1876
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:231
metadata.h
pos
unsigned int pos
Definition: spdifenc.c:414
avformat.h
dict.h
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AVRational::den
int den
Denominator.
Definition: rational.h:60
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:612
AVPacket::stream_index
int stream_index
Definition: packet.h:541
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:318
av_dict_set_int
int av_dict_set_int(AVDictionary **pm, const char *key, int64_t value, int flags)
Convenience wrapper for av_dict_set() that converts the value to a string and stores it.
Definition: dict.c:167
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
mem.h
VqfContext::frame_bit_len
int frame_bit_len
Definition: vqf.c:32
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
av_dict_set
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:88
FFStream::cur_dts
int64_t cur_dts
Definition: internal.h:349
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:97
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:797
AV_CODEC_ID_TWINVQ
@ AV_CODEC_ID_TWINVQ
Definition: codec_id.h:489
ff_metadata_conv_ctx
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:227
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346