FFmpeg
Loading...
Searching...
No Matches
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
23#include "libavutil/mem.h"
24#include "avformat.h"
25#include "avio_internal.h"
26#include "demux.h"
27#include "internal.h"
28
29typedef struct AAXColumn {
30 uint8_t flag;
31 uint8_t type;
32 const char *name;
33 uint32_t offset;
34 int size;
35} AAXColumn;
36
41
61
62static int aax_probe(const AVProbeData *p)
63{
64 if (AV_RB32(p->buf) != MKBETAG('@','U','T','F'))
65 return 0;
66 if (AV_RB32(p->buf + 4) == 0)
67 return 0;
68 if (AV_RB16(p->buf + 8) > 1)
69 return 0;
70 if (AV_RB32(p->buf + 28) < 1)
71 return 0;
72
73 return AVPROBE_SCORE_MAX;
74}
75
80 COLUMN_FLAG_UNDEFINED = 0x8 /* shouldn't exist */
81};
82
99
101{
102 AAXContext *a = s->priv_data;
103 int64_t pts = 0;
104
105 for (int seg = 0; seg < a->current_segment; seg++)
106 pts += (a->segments[seg].end - a->segments[seg].start) / size;
107
108 pts += ((pos - a->segments[a->current_segment].start) / size);
109
110 return pts;
111}
112
114{
115 AAXContext *a = s->priv_data;
116 AVIOContext *pb = s->pb;
118 AVStream *st;
119 int64_t column_offset = 0;
120 int ret, extradata_size;
121 char *codec;
122 int64_t ret64;
123
124 avio_skip(pb, 4);
125 a->table_size = avio_rb32(pb) + 8LL;
126 a->version = avio_rb16(pb);
127 a->rows_offset = avio_rb16(pb) + 8LL;
128 a->strings_offset = avio_rb32(pb) + 8LL;
129 a->data_offset = avio_rb32(pb) + 8LL;
130 a->name_offset = avio_rb32(pb);
131 a->columns = avio_rb16(pb);
132 a->row_width = avio_rb16(pb);
133 a->nb_segments = avio_rb32(pb);
134
135 if (a->nb_segments < 1)
136 return AVERROR_INVALIDDATA;
137
138 a->schema_offset = 0x20;
139 a->strings_size = a->data_offset - a->strings_offset;
140
141 if (a->rows_offset > a->table_size ||
142 a->strings_offset > a->table_size ||
143 a->data_offset > a->table_size)
144 return AVERROR_INVALIDDATA;
145 if (a->strings_size <= 0 || a->name_offset >= a->strings_size ||
146 a->strings_size > UINT16_MAX)
147 return AVERROR_INVALIDDATA;
148 if (a->columns <= 0)
149 return AVERROR_INVALIDDATA;
150
151 a->segments = av_calloc(a->nb_segments, sizeof(*a->segments));
152 if (!a->segments)
153 return AVERROR(ENOMEM);
154
155 a->xcolumns = av_calloc(a->columns, sizeof(*a->xcolumns));
156 if (!a->xcolumns)
157 return AVERROR(ENOMEM);
158
159 a->string_table = av_calloc(a->strings_size + 1, sizeof(*a->string_table));
160 if (!a->string_table)
161 return AVERROR(ENOMEM);
162
163 for (int c = 0; c < a->columns; c++) {
164 uint8_t info = avio_r8(pb);
165 uint32_t offset = avio_rb32(pb);
166 int value_size;
167
168 if (offset >= a->strings_size)
169 return AVERROR_INVALIDDATA;
170
171 a->xcolumns[c].flag = info >> 4;
172 a->xcolumns[c].type = info & 0x0F;
173
174 switch (a->xcolumns[c].type) {
177 value_size = 0x01;
178 break;
181 value_size = 0x02;
182 break;
187 value_size = 0x04;
188 break;
190 value_size = 0x08;
191 break;
193 value_size = 0x10;
194 break;
195 default:
196 return AVERROR_INVALIDDATA;
197 }
198
199 a->xcolumns[c].size = value_size;
200
201 if (a->xcolumns[c].flag & COLUMN_FLAG_NAME)
202 a->xcolumns[c].name = a->string_table + offset;
203
204 if (a->xcolumns[c].flag & COLUMN_FLAG_DEFAULT) {
205 /* data is found relative to columns start */
206 a->xcolumns[c].offset = avio_tell(pb) - a->schema_offset;
207 avio_skip(pb, value_size);
208 }
209
210 if (a->xcolumns[c].flag & COLUMN_FLAG_ROW) {
211 /* data is found relative to row start */
212 a->xcolumns[c].offset = column_offset;
213 column_offset += value_size;
214 }
215 }
216
217 ret = ret64 = avio_seek(pb, a->strings_offset, SEEK_SET);
218 if (ret64 < 0)
219 return ret;
220
221 ret = ffio_read_size(pb, a->string_table, a->strings_size);
222 if (ret < 0)
223 return ret;
224
225 for (int c = 0; c < a->columns; c++) {
226 int64_t data_offset = 0;
227 int64_t col_offset;
228 int flag, type;
229
230 if (!a->xcolumns[c].name || strcmp(a->xcolumns[c].name, "data"))
231 continue;
232
233 type = a->xcolumns[c].type;
234 flag = a->xcolumns[c].flag;
235 col_offset = a->xcolumns[c].offset;
236
237 for (uint64_t r = 0; r < a->nb_segments; r++) {
239 data_offset = a->schema_offset + col_offset;
240 } else if (flag & COLUMN_FLAG_ROW) {
241 data_offset = a->rows_offset + r * a->row_width + col_offset;
242 } else
243 return AVERROR_INVALIDDATA;
244
245 ret = ret64 = avio_seek(pb, data_offset, SEEK_SET);
246 if (ret64 < 0)
247 return ret;
248
249 if (type == COLUMN_TYPE_VLDATA) {
250 int64_t start, size;
251
252 start = avio_rb32(pb);
253 size = avio_rb32(pb);
254 if (!size)
255 return AVERROR_INVALIDDATA;
256 a->segments[r].start = start + a->data_offset;
257 a->segments[r].end = a->segments[r].start + size;
258 if (r &&
259 a->segments[r].start < a->segments[r-1].end &&
260 a->segments[r].end > a->segments[r-1].start)
261 return AVERROR_INVALIDDATA;
262 } else
263 return AVERROR_INVALIDDATA;
264 }
265 }
266
267 if (!a->segments[0].end)
268 return AVERROR_INVALIDDATA;
269
271 if (!st)
272 return AVERROR(ENOMEM);
273 st->start_time = 0;
274 par = s->streams[0]->codecpar;
276
277 codec = a->string_table + a->name_offset;
278 if (!strcmp(codec, "AAX")) {
280 ret64 = avio_seek(pb, a->segments[0].start, SEEK_SET);
281 if (ret64 < 0 || avio_rb16(pb) != 0x8000)
282 return AVERROR_INVALIDDATA;
283 extradata_size = avio_rb16(pb) + 4;
284 if (extradata_size < 12)
285 return AVERROR_INVALIDDATA;
286 avio_seek(pb, -4, SEEK_CUR);
287 ret = ff_get_extradata(s, par, pb, extradata_size);
288 if (ret < 0)
289 return ret;
290 par->ch_layout.nb_channels = AV_RB8 (par->extradata + 7);
291 par->sample_rate = AV_RB32(par->extradata + 8);
292 if (!par->ch_layout.nb_channels || !par->sample_rate)
293 return AVERROR_INVALIDDATA;
294
295 avpriv_set_pts_info(st, 64, 32, par->sample_rate);
296 /*} else if (!strcmp(codec, "HCA") ){
297 par->codec_id = AV_CODEC_ID_HCA;*/
298 } else {
299 return AVERROR_INVALIDDATA;
300 }
301
302 return 0;
303}
304
306{
307 AAXContext *a = s->priv_data;
308 AVCodecParameters *par = s->streams[0]->codecpar;
309 AVIOContext *pb = s->pb;
310 const int size = 18 * par->ch_layout.nb_channels;
311 int ret, extradata_size = 0;
312 uint8_t *extradata = NULL;
313 int skip = 0;
314
315 if (avio_feof(pb))
316 return AVERROR_EOF;
317
318 pkt->pos = avio_tell(pb);
319
320 for (uint32_t seg = 0; seg < a->nb_segments; seg++) {
321 int64_t start = a->segments[seg].start;
322 int64_t end = a->segments[seg].end;
323
324 if (pkt->pos >= start && pkt->pos <= end) {
325 a->current_segment = seg;
327 skip = (end - start) - ((end - start) / size) * size;
328 break;
329 }
330 }
331
332 if (pkt->pos >= a->segments[a->current_segment].end - skip) {
333 if (a->current_segment + 1 == a->nb_segments)
334 return AVERROR_EOF;
335 a->current_segment++;
336 avio_seek(pb, a->segments[a->current_segment].start, SEEK_SET);
337
338 if (par->codec_id == AV_CODEC_ID_ADPCM_ADX) {
339 if (avio_rb16(pb) != 0x8000)
340 return AVERROR_INVALIDDATA;
341 extradata_size = avio_rb16(pb) + 4;
342 avio_seek(pb, -4, SEEK_CUR);
343 if (extradata_size < 12)
344 return AVERROR_INVALIDDATA;
345 extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
346 if (!extradata)
347 return AVERROR(ENOMEM);
348 ret = ffio_read_size(pb, extradata, extradata_size);
349 if (ret < 0) {
350 av_free(extradata);
351 return ret;
352 }
353 memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
354 }
355 }
356
357 ret = av_get_packet(pb, pkt, size);
358 if (ret != size) {
359 av_free(extradata);
360 return ret < 0 ? ret : AVERROR_INVALIDDATA;
361 }
362 pkt->duration = 1;
363 pkt->stream_index = 0;
364 pkt->pts = get_pts(s, pkt->pos, size);
365
366 if (extradata) {
367 ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, extradata, extradata_size);
368 if (ret < 0) {
369 av_free(extradata);
370 return ret;
371 }
372 }
373
374 return ret;
375}
376
378{
379 AAXContext *a = s->priv_data;
380
381 av_freep(&a->segments);
382 av_freep(&a->xcolumns);
383 av_freep(&a->string_table);
384
385 return 0;
386}
387
389 .p.name = "aax",
390 .p.long_name = NULL_IF_CONFIG_SMALL("CRI AAX"),
391 .p.extensions = "aax",
392 .p.flags = AVFMT_GENERIC_INDEX,
393 .priv_data_size = sizeof(AAXContext),
394 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
399};
ColumnFlag
Definition aaxdec.c:76
@ COLUMN_FLAG_NAME
Definition aaxdec.c:77
@ COLUMN_FLAG_ROW
Definition aaxdec.c:79
@ COLUMN_FLAG_UNDEFINED
Definition aaxdec.c:80
@ COLUMN_FLAG_DEFAULT
Definition aaxdec.c:78
ColumnType
Definition aaxdec.c:83
@ COLUMN_TYPE_SINT32
Definition aaxdec.c:89
@ COLUMN_TYPE_DOUBLE
Definition aaxdec.c:93
@ COLUMN_TYPE_UINT128
Definition aaxdec.c:96
@ COLUMN_TYPE_UINT16
Definition aaxdec.c:86
@ COLUMN_TYPE_UINT8
Definition aaxdec.c:84
@ COLUMN_TYPE_SINT64
Definition aaxdec.c:91
@ COLUMN_TYPE_UINT32
Definition aaxdec.c:88
@ COLUMN_TYPE_STRING
Definition aaxdec.c:94
@ COLUMN_TYPE_VLDATA
Definition aaxdec.c:95
@ COLUMN_TYPE_UNDEFINED
Definition aaxdec.c:97
@ COLUMN_TYPE_SINT16
Definition aaxdec.c:87
@ COLUMN_TYPE_UINT64
Definition aaxdec.c:90
@ COLUMN_TYPE_FLOAT
Definition aaxdec.c:92
@ COLUMN_TYPE_SINT8
Definition aaxdec.c:85
static int aax_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition aaxdec.c:305
static int aax_read_close(AVFormatContext *s)
Definition aaxdec.c:377
static int aax_probe(const AVProbeData *p)
Definition aaxdec.c:62
static int64_t get_pts(AVFormatContext *s, int64_t pos, int size)
Definition aaxdec.c:100
const FFInputFormat ff_aax_demuxer
Definition aaxdec.c:388
static int aax_read_header(AVFormatContext *s)
Definition aaxdec.c:113
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:834
Main libavformat public API header.
#define AVPROBE_SCORE_MAX
maximum score
Definition avformat.h:483
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:98
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition avformat.h:499
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
unsigned int avio_rb16(AVIOContext *s)
Definition aviobuf.c:749
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition aviobuf.c:349
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
unsigned int avio_rb32(AVIOContext *s)
Definition aviobuf.c:764
int avio_r8(AVIOContext *s)
Definition aviobuf.c:606
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
static void BS_FUNC skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
#define flag(name)
Definition cbs_h264.c:60
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition demux.h:35
int ff_get_extradata(void *logctx, 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...
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_ADPCM_ADX
Definition codec_id.h:379
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition packet.h:56
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 packet.c:197
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
int a
cl_device_type type
#define r
Definition input.c:42
#define AV_RB8(x)
#define AV_RB32(p)
#define AV_RB16(p)
unsigned offset
Definition libaomenc.c:763
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_cold int read_close(AVFormatContext *ctx)
Definition libcdio.c:143
#define MKBETAG(a, b, c, d)
Definition macros.h:56
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
unsigned int pos
Definition spdifenc.c:431
uint8_t flag
Definition aaxdec.c:30
uint32_t offset
Definition aaxdec.c:33
uint8_t type
Definition aaxdec.c:31
int size
Definition aaxdec.c:34
const char * name
Definition aaxdec.c:32
int64_t rows_offset
Definition aaxdec.c:45
uint16_t columns
Definition aaxdec.c:49
uint16_t version
Definition aaxdec.c:44
AAXColumn * xcolumns
Definition aaxdec.c:58
uint32_t current_segment
Definition aaxdec.c:56
char * string_table
Definition aaxdec.c:54
int64_t strings_size
Definition aaxdec.c:53
int64_t strings_offset
Definition aaxdec.c:46
uint32_t nb_segments
Definition aaxdec.c:51
int64_t data_offset
Definition aaxdec.c:47
int64_t table_size
Definition aaxdec.c:43
int64_t schema_offset
Definition aaxdec.c:52
int64_t name_offset
Definition aaxdec.c:48
uint16_t row_width
Definition aaxdec.c:50
AAXSegment * segments
Definition aaxdec.c:59
int64_t end
Definition aaxdec.c:39
int64_t start
Definition aaxdec.c:38
int nb_channels
Number of channels in this layout.
This struct describes the properties of an encoded stream.
Definition codec_par.h:49
AVChannelLayout ch_layout
The channel layout and number of channels.
Definition codec_par.h:207
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition codec_par.h:71
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
#define av_free(p)
#define av_freep(p)
static int64_t pts
int size
static double c[64]