FFmpeg  4.3.9
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(const 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 int 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  int ret;
58 
59  if (len == UINT_MAX)
60  return AVERROR_INVALIDDATA;
61 
62  buf = av_malloc(len+1);
63  if (!buf)
64  return AVERROR(ENOMEM);
65 
66  ret = avio_read(s->pb, buf, len);
67  if (ret < 0)
68  return ret;
69  if (len != ret)
70  return AVERROR_INVALIDDATA;
71  buf[len] = 0;
72  AV_WL32(key, tag);
73  return av_dict_set(&s->metadata, key, buf, AV_DICT_DONT_STRDUP_VAL);
74 }
75 
77  { "(c) ", "copyright" },
78  { "ARNG", "arranger" },
79  { "AUTH", "author" },
80  { "BAND", "band" },
81  { "CDCT", "conductor" },
82  { "COMT", "comment" },
83  { "FILE", "filename" },
84  { "GENR", "genre" },
85  { "LABL", "publisher" },
86  { "MUSC", "composer" },
87  { "NAME", "title" },
88  { "NOTE", "note" },
89  { "PROD", "producer" },
90  { "PRSN", "personnel" },
91  { "REMX", "remixer" },
92  { "SING", "singer" },
93  { "TRCK", "track" },
94  { "WORD", "words" },
95  { 0 },
96 };
97 
99 {
100  VqfContext *c = s->priv_data;
102  int chunk_tag;
103  int rate_flag = -1;
104  int header_size;
105  int read_bitrate = 0;
106  int size, ret;
107  uint8_t comm_chunk[12];
108 
109  if (!st)
110  return AVERROR(ENOMEM);
111 
112  avio_skip(s->pb, 12);
113 
114  header_size = avio_rb32(s->pb);
115 
116  if (header_size < 0)
117  return AVERROR_INVALIDDATA;
118 
121  st->start_time = 0;
122 
123  do {
124  int len;
125  chunk_tag = avio_rl32(s->pb);
126 
127  if (chunk_tag == MKTAG('D','A','T','A'))
128  break;
129 
130  len = avio_rb32(s->pb);
131 
132  if ((unsigned) len > INT_MAX/2 || header_size < 8) {
133  av_log(s, AV_LOG_ERROR, "Malformed header\n");
134  return -1;
135  }
136 
137  header_size -= 8;
138 
139  switch(chunk_tag){
140  case MKTAG('C','O','M','M'):
141  if (len < 12)
142  return AVERROR_INVALIDDATA;
143 
144  avio_read(s->pb, comm_chunk, 12);
145  st->codecpar->channels = AV_RB32(comm_chunk ) + 1;
146  read_bitrate = AV_RB32(comm_chunk + 4);
147  rate_flag = AV_RB32(comm_chunk + 8);
148  avio_skip(s->pb, len-12);
149 
150  if (st->codecpar->channels <= 0) {
151  av_log(s, AV_LOG_ERROR, "Invalid number of channels\n");
152  return AVERROR_INVALIDDATA;
153  }
154 
155  st->codecpar->bit_rate = (int64_t)read_bitrate * 1000;
156  break;
157  case MKTAG('D','S','I','Z'): // size of compressed data
158  {
159  av_dict_set_int(&s->metadata, "size", avio_rb32(s->pb), 0);
160  }
161  break;
162  case MKTAG('Y','E','A','R'): // recording date
163  case MKTAG('E','N','C','D'): // compression date
164  case MKTAG('E','X','T','R'): // reserved
165  case MKTAG('_','Y','M','H'): // reserved
166  case MKTAG('_','N','T','T'): // reserved
167  case MKTAG('_','I','D','3'): // reserved for ID3 tags
168  avio_skip(s->pb, FFMIN(len, header_size));
169  break;
170  default:
171  ret = add_metadata(s, chunk_tag, len, header_size);
172  if (ret < 0)
173  return ret;
174  break;
175  }
176 
177  header_size -= len;
178 
179  } while (header_size >= 0 && !avio_feof(s->pb));
180 
181  switch (rate_flag) {
182  case -1:
183  av_log(s, AV_LOG_ERROR, "COMM tag not found!\n");
184  return -1;
185  case 44:
186  st->codecpar->sample_rate = 44100;
187  break;
188  case 22:
189  st->codecpar->sample_rate = 22050;
190  break;
191  case 11:
192  st->codecpar->sample_rate = 11025;
193  break;
194  default:
195  if (rate_flag < 8 || rate_flag > 44) {
196  av_log(s, AV_LOG_ERROR, "Invalid rate flag %d\n", rate_flag);
197  return AVERROR_INVALIDDATA;
198  }
199  st->codecpar->sample_rate = rate_flag*1000;
200  break;
201  }
202 
203  if (read_bitrate / st->codecpar->channels < 8 ||
204  read_bitrate / st->codecpar->channels > 48) {
205  av_log(s, AV_LOG_ERROR, "Invalid bitrate per channel %d\n",
206  read_bitrate / st->codecpar->channels);
207  return AVERROR_INVALIDDATA;
208  }
209 
210  switch (((st->codecpar->sample_rate/1000) << 8) +
211  read_bitrate/st->codecpar->channels) {
212  case (11<<8) + 8 :
213  case (8 <<8) + 8 :
214  case (11<<8) + 10:
215  case (22<<8) + 32:
216  size = 512;
217  break;
218  case (16<<8) + 16:
219  case (22<<8) + 20:
220  case (22<<8) + 24:
221  size = 1024;
222  break;
223  case (44<<8) + 40:
224  case (44<<8) + 48:
225  size = 2048;
226  break;
227  default:
228  av_log(s, AV_LOG_ERROR, "Mode not supported: %d Hz, %"PRId64" kb/s.\n",
230  return -1;
231  }
232  c->frame_bit_len = st->codecpar->bit_rate*size/st->codecpar->sample_rate;
233  avpriv_set_pts_info(st, 64, size, st->codecpar->sample_rate);
234 
235  /* put first 12 bytes of COMM chunk in extradata */
236  if ((ret = ff_alloc_extradata(st->codecpar, 12)) < 0)
237  return ret;
238  memcpy(st->codecpar->extradata, comm_chunk, 12);
239 
240  ff_metadata_conv_ctx(s, NULL, vqf_metadata_conv);
241 
242  return 0;
243 }
244 
246 {
247  VqfContext *c = s->priv_data;
248  int ret;
249  int size = (c->frame_bit_len - c->remaining_bits + 7)>>3;
250 
251  if ((ret = av_new_packet(pkt, size + 2)) < 0)
252  return ret;
253 
254  pkt->pos = avio_tell(s->pb);
255  pkt->stream_index = 0;
256  pkt->duration = 1;
257 
258  pkt->data[0] = 8 - c->remaining_bits; // Number of bits to skip
259  pkt->data[1] = c->last_frame_bits;
260  ret = avio_read(s->pb, pkt->data+2, size);
261 
262  if (ret != size) {
263  return AVERROR(EIO);
264  }
265 
266  c->last_frame_bits = pkt->data[size+1];
267  c->remaining_bits = (size << 3) - c->frame_bit_len + c->remaining_bits;
268 
269  return size+2;
270 }
271 
273  int stream_index, int64_t timestamp, int flags)
274 {
275  VqfContext *c = s->priv_data;
276  AVStream *st;
277  int64_t ret;
278  int64_t pos;
279 
280  st = s->streams[stream_index];
281  pos = av_rescale_rnd(timestamp * st->codecpar->bit_rate,
282  st->time_base.num,
284  (flags & AVSEEK_FLAG_BACKWARD) ?
286  pos *= c->frame_bit_len;
287 
288  st->cur_dts = av_rescale(pos, st->time_base.den,
289  st->codecpar->bit_rate * (int64_t)st->time_base.num);
290 
291  if ((ret = avio_seek(s->pb, ((pos-7) >> 3) + s->internal->data_offset, SEEK_SET)) < 0)
292  return ret;
293 
294  c->remaining_bits = -7 - ((pos-7)&7);
295  return 0;
296 }
297 
299  .name = "vqf",
300  .long_name = NULL_IF_CONFIG_SMALL("Nippon Telegraph and Telephone Corporation (NTT) TwinVQ"),
301  .priv_data_size = sizeof(VqfContext),
306  .extensions = "vqf,vql,vqe",
307 };
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2512
static int vqf_read_seek(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: vqf.c:272
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
void ff_metadata_conv_ctx(AVFormatContext *ctx, const AVMetadataConv *d_conv, const AVMetadataConv *s_conv)
Definition: metadata.c:59
int size
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:375
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:4948
int64_t data_offset
offset of the first packet
Definition: internal.h:80
static int read_seek(AVFormatContext *ctx, int stream_index, int64_t timestamp, int flags)
Definition: libcdio.c:153
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
int num
Numerator.
Definition: rational.h:59
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:241
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1804
int remaining_bits
Definition: vqf.c:32
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:329
Definition: vqf.c:29
const char * key
static AVPacket pkt
Round toward +infinity.
Definition: mathematics.h:83
Format I/O context.
Definition: avformat.h:1351
int64_t cur_dts
Definition: avformat.h:1079
Public dictionary API.
int frame_bit_len
Definition: vqf.c:30
uint8_t
#define av_malloc(s)
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:778
#define AV_RB32
Definition: intreadwrite.h:130
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:373
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4526
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1419
uint8_t * data
Definition: packet.h:355
uint32_t tag
Definition: movenc.c:1532
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
#define av_log(a,...)
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:625
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:88
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1591
Round toward -infinity.
Definition: mathematics.h:82
static int vqf_probe(const AVProbeData *probe_packet)
Definition: vqf.c:35
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:747
#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:188
unsigned int pos
Definition: spdifenc.c:412
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
AVInputFormat ff_vqf_demuxer
Definition: vqf.c:298
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:3328
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
#define FFMIN(a, b)
Definition: common.h:96
#define AV_DICT_DONT_STRDUP_VAL
Take ownership of a value that&#39;s been allocated with av_malloc() or another memory allocation functio...
Definition: dict.h:76
uint8_t last_frame_bits
Definition: vqf.c:31
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
#define s(width, name)
Definition: cbs_vp9.c:257
static int vqf_read_header(AVFormatContext *s)
Definition: vqf.c:98
#define AV_RL32
Definition: intreadwrite.h:146
static const AVMetadataConv vqf_metadata_conv[]
Definition: vqf.c:76
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
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:545
Stream structure.
Definition: avformat.h:876
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
static int add_metadata(AVFormatContext *s, uint32_t tag, unsigned int tag_len, unsigned int remaining)
Definition: vqf.c:52
AVIOContext * pb
I/O context.
Definition: avformat.h:1393
long long int64_t
Definition: coverity.c:34
static int vqf_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: vqf.c:245
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:70
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition: avformat.h:451
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
#define flags(name, subs,...)
Definition: cbs_av1.c:576
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
Main libavformat public API header.
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:915
static double c[64]
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:147
int den
Denominator.
Definition: rational.h:60
int len
void * priv_data
Format private data.
Definition: avformat.h:1379
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
int channels
Audio only.
Definition: codec_par.h:166
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:650
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1023
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:356
int stream_index
Definition: packet.h:357
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:905
#define MKTAG(a, b, c, d)
Definition: common.h:406
This structure stores compressed data.
Definition: packet.h:332
#define AV_WL32(p, v)
Definition: intreadwrite.h:426