FFmpeg
aaxdec.c
Go to the documentation of this file.
1 /*
2  * AAX demuxer
3  * Copyright (c) 2020 Paul B Mahol
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 "libavutil/avassert.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avformat.h"
25 #include "internal.h"
26 
27 typedef struct AAXColumn {
30  const char *name;
31  uint32_t offset;
32  int size;
33 } AAXColumn;
34 
35 typedef struct AAXSegment {
36  int64_t start;
37  int64_t end;
38 } AAXSegment;
39 
40 typedef struct AAXContext {
41  int64_t table_size;
42  uint16_t version;
43  int64_t rows_offset;
44  int64_t strings_offset;
45  int64_t data_offset;
46  int64_t name_offset;
47  uint16_t columns;
48  uint16_t row_width;
49  uint32_t nb_segments;
50  int64_t schema_offset;
51  int64_t strings_size;
52  char *string_table;
53 
54  uint32_t current_segment;
55 
58 } AAXContext;
59 
60 static int aax_probe(const AVProbeData *p)
61 {
62  if (AV_RB32(p->buf) != MKBETAG('@','U','T','F'))
63  return 0;
64  if (AV_RB32(p->buf + 4) == 0)
65  return 0;
66  if (AV_RB16(p->buf + 8) > 1)
67  return 0;
68  if (AV_RB32(p->buf + 28) < 1)
69  return 0;
70 
71  return AVPROBE_SCORE_MAX;
72 }
73 
74 enum ColumnFlag {
78  COLUMN_FLAG_UNDEFINED = 0x8 /* shouldn't exist */
79 };
80 
81 enum ColumnType {
94  COLUMN_TYPE_UINT128 = 0x0c, /* for GUIDs */
96 };
97 
98 static int64_t get_pts(AVFormatContext *s, int64_t pos, int size)
99 {
100  AAXContext *a = s->priv_data;
101  int64_t pts = 0;
102 
103  for (int seg = 0; seg < a->current_segment; seg++)
104  pts += (a->segments[seg].end - a->segments[seg].start) / size;
105 
106  pts += ((pos - a->segments[a->current_segment].start) / size);
107 
108  return pts;
109 }
110 
112 {
113  AAXContext *a = s->priv_data;
114  AVIOContext *pb = s->pb;
115  AVCodecParameters *par;
116  AVStream *st;
117  int64_t column_offset = 0;
118  int ret, extradata_size;
119  char *codec;
120 
121  avio_skip(pb, 4);
122  a->table_size = avio_rb32(pb) + 8LL;
123  a->version = avio_rb16(pb);
124  a->rows_offset = avio_rb16(pb) + 8LL;
125  a->strings_offset = avio_rb32(pb) + 8LL;
126  a->data_offset = avio_rb32(pb) + 8LL;
127  a->name_offset = avio_rb32(pb);
128  a->columns = avio_rb16(pb);
129  a->row_width = avio_rb16(pb);
130  a->nb_segments = avio_rb32(pb);
131 
132  if (a->nb_segments < 1)
133  return AVERROR_INVALIDDATA;
134 
135  a->schema_offset = 0x20;
137 
138  if (a->rows_offset > a->table_size ||
139  a->strings_offset > a->table_size ||
140  a->data_offset > a->table_size)
141  return AVERROR_INVALIDDATA;
142  if (a->strings_size <= 0 || a->name_offset >= a->strings_size ||
143  a->strings_size > UINT16_MAX)
144  return AVERROR_INVALIDDATA;
145  if (a->columns <= 0)
146  return AVERROR_INVALIDDATA;
147 
148  a->segments = av_calloc(a->nb_segments, sizeof(*a->segments));
149  if (!a->segments)
150  return AVERROR(ENOMEM);
151 
152  a->xcolumns = av_calloc(a->columns, sizeof(*a->xcolumns));
153  if (!a->xcolumns) {
154  ret = AVERROR(ENOMEM);
155  goto fail;
156  }
157 
158  a->string_table = av_calloc(a->strings_size + 1, sizeof(*a->string_table));
159  if (!a->string_table) {
160  ret = AVERROR(ENOMEM);
161  goto fail;
162  }
163 
164  for (int c = 0; c < a->columns; c++) {
165  uint8_t info = avio_r8(pb);
166  uint32_t offset = avio_rb32(pb);
167  int value_size;
168 
169  if (offset >= a->strings_size) {
170  ret = AVERROR_INVALIDDATA;
171  goto fail;
172  }
173 
174  a->xcolumns[c].flag = info >> 4;
175  a->xcolumns[c].type = info & 0x0F;
176 
177  switch (a->xcolumns[c].type) {
178  case COLUMN_TYPE_UINT8:
179  case COLUMN_TYPE_SINT8:
180  value_size = 0x01;
181  break;
182  case COLUMN_TYPE_UINT16:
183  case COLUMN_TYPE_SINT16:
184  value_size = 0x02;
185  break;
186  case COLUMN_TYPE_UINT32:
187  case COLUMN_TYPE_SINT32:
188  case COLUMN_TYPE_FLOAT:
189  case COLUMN_TYPE_STRING:
190  value_size = 0x04;
191  break;
192  case COLUMN_TYPE_VLDATA:
193  value_size = 0x08;
194  break;
195  case COLUMN_TYPE_UINT128:
196  value_size = 0x10;
197  break;
198  default:
199  ret = AVERROR_INVALIDDATA;
200  goto fail;
201  }
202 
203  a->xcolumns[c].size = value_size;
204 
205  if (a->xcolumns[c].flag & COLUMN_FLAG_NAME)
206  a->xcolumns[c].name = a->string_table + offset;
207 
208  if (a->xcolumns[c].flag & COLUMN_FLAG_DEFAULT) {
209  /* data is found relative to columns start */
210  a->xcolumns[c].offset = avio_tell(pb) - a->schema_offset;
211  avio_skip(pb, value_size);
212  }
213 
214  if (a->xcolumns[c].flag & COLUMN_FLAG_ROW) {
215  /* data is found relative to row start */
216  a->xcolumns[c].offset = column_offset;
217  column_offset += value_size;
218  }
219  }
220 
221  avio_seek(pb, a->strings_offset, SEEK_SET);
222  ret = avio_read(pb, a->string_table, a->strings_size);
223  if (ret != a->strings_size) {
224  if (ret < 0)
225  goto fail;
226  ret = AVERROR(EIO);
227  goto fail;
228  }
229 
230  for (int c = 0; c < a->columns; c++) {
231  int64_t data_offset = 0;
232  int64_t col_offset;
233  int flag, type;
234 
235  if (strcmp(a->xcolumns[c].name, "data"))
236  continue;
237 
238  type = a->xcolumns[c].type;
239  flag = a->xcolumns[c].flag;
240  col_offset = a->xcolumns[c].offset;
241 
242  for (uint64_t r = 0; r < a->nb_segments; r++) {
243  if (flag & COLUMN_FLAG_DEFAULT) {
244  data_offset = a->schema_offset + col_offset;
245  } else if (flag & COLUMN_FLAG_ROW) {
246  data_offset = a->rows_offset + r * a->row_width + col_offset;
247  } else {
248  ret = AVERROR_INVALIDDATA;
249  goto fail;
250  }
251 
252  avio_seek(pb, data_offset, SEEK_SET);
253  if (type == COLUMN_TYPE_VLDATA) {
254  int64_t start, size;
255 
256  start = avio_rb32(pb);
257  size = avio_rb32(pb);
258  a->segments[r].start = start + a->data_offset;
259  a->segments[r].end = a->segments[r].start + size;
260  } else {
261  ret = AVERROR_INVALIDDATA;
262  goto fail;
263  }
264  }
265  }
266 
267  st = avformat_new_stream(s, NULL);
268  if (!st) {
269  ret = AVERROR(ENOMEM);
270  goto fail;
271  }
272  st->start_time = 0;
273  par = s->streams[0]->codecpar;
275 
276  codec = a->string_table + a->name_offset;
277  if (!strcmp(codec, "AAX")) {
279  avio_seek(pb, a->segments[0].start, SEEK_SET);
280  if (avio_rb16(pb) != 0x8000) {
281  ret = AVERROR_INVALIDDATA;
282  goto fail;
283  }
284  extradata_size = avio_rb16(pb) + 4;
285  if (extradata_size < 12) {
286  ret = AVERROR_INVALIDDATA;
287  goto fail;
288  }
289  avio_seek(pb, -4, SEEK_CUR);
290  ret = ff_get_extradata(s, par, pb, extradata_size);
291  if (ret < 0) {
292  goto fail;
293  }
294  par->channels = AV_RB8 (par->extradata + 7);
295  par->sample_rate = AV_RB32(par->extradata + 8);
296  if (!par->channels || !par->sample_rate) {
297  ret = AVERROR_INVALIDDATA;
298  goto fail;
299  }
300 
301  avpriv_set_pts_info(st, 64, 32, par->sample_rate);
302  /*} else if (!strcmp(codec, "HCA") ){
303  par->codec_id = AV_CODEC_ID_HCA;*/
304  } else {
305  ret = AVERROR_INVALIDDATA;
306  goto fail;
307  }
308 
309  return 0;
310 fail:
311  av_freep(&a->string_table);
312  av_freep(&a->xcolumns);
313  av_freep(&a->segments);
314 
315  return ret;
316 }
317 
319 {
320  AAXContext *a = s->priv_data;
321  AVCodecParameters *par = s->streams[0]->codecpar;
322  AVIOContext *pb = s->pb;
323  const int size = 18 * par->channels;
324  int ret, extradata_size = 0;
325  uint8_t *extradata = NULL;
326  int skip = 0;
327 
328  if (avio_feof(pb))
329  return AVERROR_EOF;
330 
331  pkt->pos = avio_tell(pb);
332 
333  for (uint32_t seg = 0; seg < a->nb_segments; seg++) {
334  int64_t start = a->segments[seg].start;
335  int64_t end = a->segments[seg].end;
336 
337  if (pkt->pos >= start && pkt->pos <= end) {
338  a->current_segment = seg;
339  if (par->codec_id == AV_CODEC_ID_ADPCM_ADX)
340  skip = (end - start) - ((end - start) / size) * size;
341  break;
342  }
343  }
344 
345  if (pkt->pos >= a->segments[a->current_segment].end - skip) {
346  if (a->current_segment + 1 == a->nb_segments)
347  return AVERROR_EOF;
348  a->current_segment++;
349  avio_seek(pb, a->segments[a->current_segment].start, SEEK_SET);
350 
351  if (par->codec_id == AV_CODEC_ID_ADPCM_ADX) {
352  if (avio_rb16(pb) != 0x8000)
353  return AVERROR_INVALIDDATA;
354  extradata_size = avio_rb16(pb) + 4;
355  avio_seek(pb, -4, SEEK_CUR);
356  if (extradata_size < 12)
357  return AVERROR_INVALIDDATA;
358  extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
359  if (!extradata)
360  return AVERROR(ENOMEM);
361  if (avio_read(pb, extradata, extradata_size) != extradata_size) {
362  av_free(extradata);
363  return AVERROR(EIO);
364  }
365  memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
366  }
367  }
368 
369  ret = av_get_packet(pb, pkt, size);
370  if (ret != size) {
371  av_free(extradata);
372  return ret < 0 ? ret : AVERROR(EIO);
373  }
374  pkt->duration = 1;
375  pkt->stream_index = 0;
376  pkt->pts = get_pts(s, pkt->pos, size);
377 
378  if (extradata) {
379  ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, extradata, extradata_size);
380  if (ret < 0) {
381  av_free(extradata);
382  return ret;
383  }
384  }
385 
386  return ret;
387 }
388 
390 {
391  AAXContext *a = s->priv_data;
392 
393  av_freep(&a->segments);
394  av_freep(&a->xcolumns);
395  av_freep(&a->string_table);
396 
397  return 0;
398 }
399 
401  .name = "aax",
402  .long_name = NULL_IF_CONFIG_SMALL("CRI AAX"),
403  .priv_data_size = sizeof(AAXContext),
408  .extensions = "aax",
410 };
char * string_table
Definition: aaxdec.c:52
ColumnType
Definition: aaxdec.c:81
#define NULL
Definition: coverity.c:32
Bytestream IO Context.
Definition: avio.h:161
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int64_t start
Definition: aaxdec.c:36
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:383
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:4871
AAXSegment * segments
Definition: aaxdec.c:57
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
uint16_t columns
Definition: aaxdec.c:47
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:341
static AVPacket pkt
unsigned int avio_rb16(AVIOContext *s)
Definition: aviobuf.c:768
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_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:91
static int aax_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: aaxdec.c:318
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
Format I/O context.
Definition: avformat.h:1239
uint8_t
#define av_malloc(s)
unsigned int avio_rb32(AVIOContext *s)
Definition: aviobuf.c:783
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:92
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:381
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
static int aax_read_close(AVFormatContext *s)
Definition: aaxdec.c:389
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4453
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:91
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1307
int64_t strings_size
Definition: aaxdec.c:51
AAXColumn * xcolumns
Definition: aaxdec.c:56
#define AVERROR_EOF
End of file.
Definition: error.h:55
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:145
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:305
ptrdiff_t size
Definition: opengl_enc.c:100
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:637
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
const char * r
Definition: vf_curves.c:114
int64_t rows_offset
Definition: aaxdec.c:43
unsigned int pos
Definition: spdifenc.c:410
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
simple assert() macros that are a bit more flexible than ISO C assert().
static int aax_probe(const AVProbeData *p)
Definition: aaxdec.c:60
#define fail()
Definition: checkasm.h:123
int avio_r8(AVIOContext *s)
Definition: aviobuf.c:628
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
int64_t strings_offset
Definition: aaxdec.c:44
MIPS optimizations info
Definition: mips.txt:2
uint32_t nb_segments
Definition: aaxdec.c:49
int64_t schema_offset
Definition: aaxdec.c:50
uint16_t row_width
Definition: aaxdec.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:527
Stream structure.
Definition: avformat.h:880
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_reading.c:42
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_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL AV_RB8
Definition: bytestream.h:91
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
int64_t table_size
Definition: aaxdec.c:41
AVIOContext * pb
I/O context.
Definition: avformat.h:1281
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
static int64_t get_pts(AVFormatContext *s, int64_t pos, int size)
Definition: aaxdec.c:98
ColumnFlag
Definition: aaxdec.c:74
FF_ENABLE_DEPRECATION_WARNINGS int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as a packet side data.
Definition: avpacket.c:298
const char * name
Definition: aaxdec.c:30
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
static int64_t pts
#define flags(name, subs,...)
Definition: cbs_av1.c:560
static int read_probe(const AVProbeData *pd)
Definition: jvdec.c:55
uint32_t current_segment
Definition: aaxdec.c:54
static int aax_read_header(AVFormatContext *s)
Definition: aaxdec.c:111
int sample_rate
Audio only.
Definition: codec_par.h:170
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:453
AVInputFormat ff_aax_demuxer
Definition: aaxdec.c:400
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:919
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:3278
int64_t name_offset
Definition: aaxdec.c:46
int size
Definition: aaxdec.c:32
int64_t data_offset
Definition: aaxdec.c:45
#define MKBETAG(a, b, c, d)
Definition: common.h:406
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:215
int64_t end
Definition: aaxdec.c:37
uint32_t offset
Definition: aaxdec.c:31
#define av_free(p)
void * priv_data
Format private data.
Definition: avformat.h:1267
uint8_t type
Definition: aaxdec.c:29
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
#define av_freep(p)
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:652
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1045
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:368
int stream_index
Definition: packet.h:365
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
uint16_t version
Definition: aaxdec.c:42
This structure stores compressed data.
Definition: packet.h:340
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:356
uint8_t flag
Definition: aaxdec.c:28