Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavformat/aaxdec.c
Line
Count
Source (jump to first uncovered line)
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/intreadwrite.h"
23
#include "libavutil/mem.h"
24
#include "avformat.h"
25
#include "avio_internal.h"
26
#include "demux.h"
27
#include "internal.h"
28
29
typedef struct AAXColumn {
30
    uint8_t flag;
31
    uint8_t type;
32
    const char *name;
33
    uint32_t offset;
34
    int size;
35
} AAXColumn;
36
37
typedef struct AAXSegment {
38
    int64_t start;
39
    int64_t end;
40
} AAXSegment;
41
42
typedef struct AAXContext {
43
    int64_t table_size;
44
    uint16_t version;
45
    int64_t rows_offset;
46
    int64_t strings_offset;
47
    int64_t data_offset;
48
    int64_t name_offset;
49
    uint16_t columns;
50
    uint16_t row_width;
51
    uint32_t nb_segments;
52
    int64_t schema_offset;
53
    int64_t strings_size;
54
    char *string_table;
55
56
    uint32_t current_segment;
57
58
    AAXColumn *xcolumns;
59
    AAXSegment *segments;
60
} AAXContext;
61
62
static int aax_probe(const AVProbeData *p)
63
358k
{
64
358k
    if (AV_RB32(p->buf) != MKBETAG('@','U','T','F'))
65
358k
        return 0;
66
408
    if (AV_RB32(p->buf + 4) == 0)
67
76
        return 0;
68
332
    if (AV_RB16(p->buf + 8) > 1)
69
104
        return 0;
70
228
    if (AV_RB32(p->buf + 28) < 1)
71
143
        return 0;
72
73
85
    return AVPROBE_SCORE_MAX;
74
228
}
75
76
enum ColumnFlag {
77
    COLUMN_FLAG_NAME            = 0x1,
78
    COLUMN_FLAG_DEFAULT         = 0x2,
79
    COLUMN_FLAG_ROW             = 0x4,
80
    COLUMN_FLAG_UNDEFINED       = 0x8 /* shouldn't exist */
81
};
82
83
enum ColumnType {
84
    COLUMN_TYPE_UINT8           = 0x00,
85
    COLUMN_TYPE_SINT8           = 0x01,
86
    COLUMN_TYPE_UINT16          = 0x02,
87
    COLUMN_TYPE_SINT16          = 0x03,
88
    COLUMN_TYPE_UINT32          = 0x04,
89
    COLUMN_TYPE_SINT32          = 0x05,
90
    COLUMN_TYPE_UINT64          = 0x06,
91
    COLUMN_TYPE_SINT64          = 0x07,
92
    COLUMN_TYPE_FLOAT           = 0x08,
93
    COLUMN_TYPE_DOUBLE          = 0x09,
94
    COLUMN_TYPE_STRING          = 0x0a,
95
    COLUMN_TYPE_VLDATA          = 0x0b,
96
    COLUMN_TYPE_UINT128         = 0x0c, /* for GUIDs */
97
    COLUMN_TYPE_UNDEFINED       = -1
98
};
99
100
static int64_t get_pts(AVFormatContext *s, int64_t pos, int size)
101
79.3k
{
102
79.3k
    AAXContext *a = s->priv_data;
103
79.3k
    int64_t pts = 0;
104
105
79.3k
    for (int seg = 0; seg < a->current_segment; seg++)
106
0
        pts += (a->segments[seg].end - a->segments[seg].start) / size;
107
108
79.3k
    pts += ((pos - a->segments[a->current_segment].start) / size);
109
110
79.3k
    return pts;
111
79.3k
}
112
113
static int aax_read_header(AVFormatContext *s)
114
1.87k
{
115
1.87k
    AAXContext *a = s->priv_data;
116
1.87k
    AVIOContext *pb = s->pb;
117
1.87k
    AVCodecParameters *par;
118
1.87k
    AVStream *st;
119
1.87k
    int64_t column_offset = 0;
120
1.87k
    int ret, extradata_size;
121
1.87k
    char *codec;
122
1.87k
    int64_t ret64;
123
124
1.87k
    avio_skip(pb, 4);
125
1.87k
    a->table_size      = avio_rb32(pb) + 8LL;
126
1.87k
    a->version         = avio_rb16(pb);
127
1.87k
    a->rows_offset     = avio_rb16(pb) + 8LL;
128
1.87k
    a->strings_offset  = avio_rb32(pb) + 8LL;
129
1.87k
    a->data_offset     = avio_rb32(pb) + 8LL;
130
1.87k
    a->name_offset     = avio_rb32(pb);
131
1.87k
    a->columns         = avio_rb16(pb);
132
1.87k
    a->row_width       = avio_rb16(pb);
133
1.87k
    a->nb_segments     = avio_rb32(pb);
134
135
1.87k
    if (a->nb_segments < 1)
136
1.26k
        return AVERROR_INVALIDDATA;
137
138
613
    a->schema_offset   = 0x20;
139
613
    a->strings_size    = a->data_offset - a->strings_offset;
140
141
613
    if (a->rows_offset > a->table_size ||
142
613
        a->strings_offset > a->table_size ||
143
613
        a->data_offset > a->table_size)
144
79
        return AVERROR_INVALIDDATA;
145
534
    if (a->strings_size <= 0 || a->name_offset >= a->strings_size ||
146
534
        a->strings_size > UINT16_MAX)
147
62
        return AVERROR_INVALIDDATA;
148
472
    if (a->columns <= 0)
149
1
        return AVERROR_INVALIDDATA;
150
151
471
    a->segments = av_calloc(a->nb_segments, sizeof(*a->segments));
152
471
    if (!a->segments)
153
0
        return AVERROR(ENOMEM);
154
155
471
    a->xcolumns = av_calloc(a->columns, sizeof(*a->xcolumns));
156
471
    if (!a->xcolumns)
157
0
        return AVERROR(ENOMEM);
158
159
471
    a->string_table = av_calloc(a->strings_size + 1, sizeof(*a->string_table));
160
471
    if (!a->string_table)
161
0
        return AVERROR(ENOMEM);
162
163
280k
    for (int c = 0; c < a->columns; c++) {
164
280k
        uint8_t info = avio_r8(pb);
165
280k
        uint32_t offset = avio_rb32(pb);
166
280k
        int value_size;
167
168
280k
        if (offset >= a->strings_size)
169
2
            return AVERROR_INVALIDDATA;
170
171
280k
        a->xcolumns[c].flag = info >>   4;
172
280k
        a->xcolumns[c].type = info & 0x0F;
173
174
280k
        switch (a->xcolumns[c].type) {
175
279k
        case COLUMN_TYPE_UINT8:
176
279k
        case COLUMN_TYPE_SINT8:
177
279k
            value_size = 0x01;
178
279k
            break;
179
2
        case COLUMN_TYPE_UINT16:
180
201
        case COLUMN_TYPE_SINT16:
181
201
            value_size = 0x02;
182
201
            break;
183
3
        case COLUMN_TYPE_UINT32:
184
3
        case COLUMN_TYPE_SINT32:
185
4
        case COLUMN_TYPE_FLOAT:
186
4
        case COLUMN_TYPE_STRING:
187
4
            value_size = 0x04;
188
4
            break;
189
455
        case COLUMN_TYPE_VLDATA:
190
455
            value_size = 0x08;
191
455
            break;
192
72
        case COLUMN_TYPE_UINT128:
193
72
            value_size = 0x10;
194
72
            break;
195
0
        default:
196
0
            return AVERROR_INVALIDDATA;
197
280k
        }
198
199
280k
        a->xcolumns[c].size = value_size;
200
201
280k
        if (a->xcolumns[c].flag & COLUMN_FLAG_NAME)
202
1.09k
            a->xcolumns[c].name = a->string_table + offset;
203
204
280k
        if (a->xcolumns[c].flag & COLUMN_FLAG_DEFAULT) {
205
            /* data is found relative to columns start */
206
853
            a->xcolumns[c].offset = avio_tell(pb) - a->schema_offset;
207
853
            avio_skip(pb, value_size);
208
853
        }
209
210
280k
        if (a->xcolumns[c].flag & COLUMN_FLAG_ROW) {
211
            /* data is found relative to row start */
212
1.12k
            a->xcolumns[c].offset = column_offset;
213
1.12k
            column_offset += value_size;
214
1.12k
        }
215
280k
    }
216
217
469
    ret = ret64 = avio_seek(pb, a->strings_offset, SEEK_SET);
218
469
    if (ret64 < 0)
219
9
        return ret;
220
221
460
    ret = ffio_read_size(pb, a->string_table, a->strings_size);
222
460
    if (ret < 0)
223
7
        return ret;
224
225
45.3k
    for (int c = 0; c < a->columns; c++) {
226
44.9k
        int64_t data_offset = 0;
227
44.9k
        int64_t col_offset;
228
44.9k
        int flag, type;
229
230
44.9k
        if (!a->xcolumns[c].name || strcmp(a->xcolumns[c].name, "data"))
231
44.5k
            continue;
232
233
444
        type = a->xcolumns[c].type;
234
444
        flag = a->xcolumns[c].flag;
235
444
        col_offset = a->xcolumns[c].offset;
236
237
1.58k
        for (uint64_t r = 0; r < a->nb_segments; r++) {
238
1.18k
            if (flag & COLUMN_FLAG_DEFAULT) {
239
405
                data_offset = a->schema_offset + col_offset;
240
784
            } else if (flag & COLUMN_FLAG_ROW) {
241
784
                data_offset = a->rows_offset + r * a->row_width + col_offset;
242
784
            } else
243
0
                return AVERROR_INVALIDDATA;
244
245
1.18k
            ret = ret64 = avio_seek(pb, data_offset, SEEK_SET);
246
1.18k
            if (ret64 < 0)
247
30
                return ret;
248
249
1.15k
            if (type == COLUMN_TYPE_VLDATA) {
250
1.15k
                int64_t start, size;
251
252
1.15k
                start = avio_rb32(pb);
253
1.15k
                size  = avio_rb32(pb);
254
1.15k
                if (!size)
255
6
                    return AVERROR_INVALIDDATA;
256
1.15k
                a->segments[r].start = start + a->data_offset;
257
1.15k
                a->segments[r].end   = a->segments[r].start + size;
258
1.15k
                if (r &&
259
1.15k
                    a->segments[r].start < a->segments[r-1].end &&
260
1.15k
                    a->segments[r].end   > a->segments[r-1].start)
261
11
                    return AVERROR_INVALIDDATA;
262
1.15k
            } else
263
1
                return AVERROR_INVALIDDATA;
264
1.15k
        }
265
444
    }
266
267
405
    if (!a->segments[0].end)
268
9
        return AVERROR_INVALIDDATA;
269
270
396
    st = avformat_new_stream(s, NULL);
271
396
    if (!st)
272
0
        return AVERROR(ENOMEM);
273
396
    st->start_time = 0;
274
396
    par = s->streams[0]->codecpar;
275
396
    par->codec_type = AVMEDIA_TYPE_AUDIO;
276
277
396
    codec = a->string_table + a->name_offset;
278
396
    if (!strcmp(codec, "AAX")) {
279
389
        par->codec_id = AV_CODEC_ID_ADPCM_ADX;
280
389
        ret64 = avio_seek(pb, a->segments[0].start, SEEK_SET);
281
389
        if (ret64 < 0 || avio_rb16(pb) != 0x8000)
282
22
            return AVERROR_INVALIDDATA;
283
367
        extradata_size = avio_rb16(pb) + 4;
284
367
        if (extradata_size < 12)
285
2
            return AVERROR_INVALIDDATA;
286
365
        avio_seek(pb, -4, SEEK_CUR);
287
365
        ret = ff_get_extradata(s, par, pb, extradata_size);
288
365
        if (ret < 0)
289
20
            return ret;
290
345
        par->ch_layout.nb_channels = AV_RB8 (par->extradata + 7);
291
345
        par->sample_rate = AV_RB32(par->extradata + 8);
292
345
        if (!par->ch_layout.nb_channels || !par->sample_rate)
293
2
            return AVERROR_INVALIDDATA;
294
295
343
        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
343
    } else {
299
7
        return AVERROR_INVALIDDATA;
300
7
    }
301
302
343
    return 0;
303
396
}
304
305
static int aax_read_packet(AVFormatContext *s, AVPacket *pkt)
306
79.8k
{
307
79.8k
    AAXContext *a = s->priv_data;
308
79.8k
    AVCodecParameters *par = s->streams[0]->codecpar;
309
79.8k
    AVIOContext *pb = s->pb;
310
79.8k
    const int size = 18 * par->ch_layout.nb_channels;
311
79.8k
    int ret, extradata_size = 0;
312
79.8k
    uint8_t *extradata = NULL;
313
79.8k
    int skip = 0;
314
315
79.8k
    if (avio_feof(pb))
316
105
        return AVERROR_EOF;
317
318
79.7k
    pkt->pos = avio_tell(pb);
319
320
79.8k
    for (uint32_t seg = 0; seg < a->nb_segments; seg++) {
321
79.7k
        int64_t start = a->segments[seg].start;
322
79.7k
        int64_t end   = a->segments[seg].end;
323
324
79.7k
        if (pkt->pos >= start && pkt->pos <= end) {
325
79.6k
            a->current_segment = seg;
326
79.6k
            if (par->codec_id == AV_CODEC_ID_ADPCM_ADX)
327
79.6k
                skip = (end - start) - ((end - start) / size) * size;
328
79.6k
            break;
329
79.6k
        }
330
79.7k
    }
331
332
79.7k
    if (pkt->pos >= a->segments[a->current_segment].end - skip) {
333
81
        if (a->current_segment + 1 == a->nb_segments)
334
81
            return AVERROR_EOF;
335
0
        a->current_segment++;
336
0
        avio_seek(pb, a->segments[a->current_segment].start, SEEK_SET);
337
338
0
        if (par->codec_id == AV_CODEC_ID_ADPCM_ADX) {
339
0
            if (avio_rb16(pb) != 0x8000)
340
0
                return AVERROR_INVALIDDATA;
341
0
            extradata_size = avio_rb16(pb) + 4;
342
0
            avio_seek(pb, -4, SEEK_CUR);
343
0
            if (extradata_size < 12)
344
0
                return AVERROR_INVALIDDATA;
345
0
            extradata = av_malloc(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
346
0
            if (!extradata)
347
0
                return AVERROR(ENOMEM);
348
0
            if (avio_read(pb, extradata, extradata_size) != extradata_size) {
349
0
                av_free(extradata);
350
0
                return AVERROR(EIO);
351
0
            }
352
0
            memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
353
0
        }
354
0
    }
355
356
79.6k
    ret = av_get_packet(pb, pkt, size);
357
79.6k
    if (ret != size) {
358
298
        av_free(extradata);
359
298
        return ret < 0 ? ret : AVERROR(EIO);
360
298
    }
361
79.3k
    pkt->duration = 1;
362
79.3k
    pkt->stream_index = 0;
363
79.3k
    pkt->pts = get_pts(s, pkt->pos, size);
364
365
79.3k
    if (extradata) {
366
0
        ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, extradata, extradata_size);
367
0
        if (ret < 0) {
368
0
            av_free(extradata);
369
0
            return ret;
370
0
        }
371
0
    }
372
373
79.3k
    return ret;
374
79.3k
}
375
376
static int aax_read_close(AVFormatContext *s)
377
1.87k
{
378
1.87k
    AAXContext *a = s->priv_data;
379
380
1.87k
    av_freep(&a->segments);
381
1.87k
    av_freep(&a->xcolumns);
382
1.87k
    av_freep(&a->string_table);
383
384
1.87k
    return 0;
385
1.87k
}
386
387
const FFInputFormat ff_aax_demuxer = {
388
    .p.name         = "aax",
389
    .p.long_name    = NULL_IF_CONFIG_SMALL("CRI AAX"),
390
    .p.extensions   = "aax",
391
    .p.flags        = AVFMT_GENERIC_INDEX,
392
    .priv_data_size = sizeof(AAXContext),
393
    .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
394
    .read_probe     = aax_probe,
395
    .read_header    = aax_read_header,
396
    .read_packet    = aax_read_packet,
397
    .read_close     = aax_read_close,
398
};