| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | 7279 | static int aax_probe(const AVProbeData *p) | |
| 63 | { | ||
| 64 |
1/2✓ Branch 0 taken 7279 times.
✗ Branch 1 not taken.
|
7279 | if (AV_RB32(p->buf) != MKBETAG('@','U','T','F')) |
| 65 | 7279 | 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 | |||
| 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 | { | ||
| 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 | |||
| 113 | ✗ | static int aax_read_header(AVFormatContext *s) | |
| 114 | { | ||
| 115 | ✗ | AAXContext *a = s->priv_data; | |
| 116 | ✗ | AVIOContext *pb = s->pb; | |
| 117 | AVCodecParameters *par; | ||
| 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) { | |
| 175 | ✗ | case COLUMN_TYPE_UINT8: | |
| 176 | case COLUMN_TYPE_SINT8: | ||
| 177 | ✗ | value_size = 0x01; | |
| 178 | ✗ | break; | |
| 179 | ✗ | case COLUMN_TYPE_UINT16: | |
| 180 | case COLUMN_TYPE_SINT16: | ||
| 181 | ✗ | value_size = 0x02; | |
| 182 | ✗ | break; | |
| 183 | ✗ | case COLUMN_TYPE_UINT32: | |
| 184 | case COLUMN_TYPE_SINT32: | ||
| 185 | case COLUMN_TYPE_FLOAT: | ||
| 186 | case COLUMN_TYPE_STRING: | ||
| 187 | ✗ | value_size = 0x04; | |
| 188 | ✗ | break; | |
| 189 | ✗ | case COLUMN_TYPE_VLDATA: | |
| 190 | ✗ | value_size = 0x08; | |
| 191 | ✗ | break; | |
| 192 | ✗ | case COLUMN_TYPE_UINT128: | |
| 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++) { | |
| 238 | ✗ | if (flag & COLUMN_FLAG_DEFAULT) { | |
| 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 | |||
| 270 | ✗ | st = avformat_new_stream(s, NULL); | |
| 271 | ✗ | if (!st) | |
| 272 | ✗ | return AVERROR(ENOMEM); | |
| 273 | ✗ | st->start_time = 0; | |
| 274 | ✗ | par = s->streams[0]->codecpar; | |
| 275 | ✗ | par->codec_type = AVMEDIA_TYPE_AUDIO; | |
| 276 | |||
| 277 | ✗ | codec = a->string_table + a->name_offset; | |
| 278 | ✗ | if (!strcmp(codec, "AAX")) { | |
| 279 | ✗ | par->codec_id = AV_CODEC_ID_ADPCM_ADX; | |
| 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 | |||
| 305 | ✗ | static int aax_read_packet(AVFormatContext *s, AVPacket *pkt) | |
| 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; | |
| 326 | ✗ | if (par->codec_id == AV_CODEC_ID_ADPCM_ADX) | |
| 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 | ✗ | if (avio_read(pb, extradata, extradata_size) != extradata_size) { | |
| 349 | ✗ | av_free(extradata); | |
| 350 | ✗ | return AVERROR(EIO); | |
| 351 | } | ||
| 352 | ✗ | memset(extradata + extradata_size, 0, AV_INPUT_BUFFER_PADDING_SIZE); | |
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | ✗ | ret = av_get_packet(pb, pkt, size); | |
| 357 | ✗ | if (ret != size) { | |
| 358 | ✗ | av_free(extradata); | |
| 359 | ✗ | return ret < 0 ? ret : AVERROR(EIO); | |
| 360 | } | ||
| 361 | ✗ | pkt->duration = 1; | |
| 362 | ✗ | pkt->stream_index = 0; | |
| 363 | ✗ | pkt->pts = get_pts(s, pkt->pos, size); | |
| 364 | |||
| 365 | ✗ | if (extradata) { | |
| 366 | ✗ | ret = av_packet_add_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, extradata, extradata_size); | |
| 367 | ✗ | if (ret < 0) { | |
| 368 | ✗ | av_free(extradata); | |
| 369 | ✗ | return ret; | |
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 373 | ✗ | return ret; | |
| 374 | } | ||
| 375 | |||
| 376 | ✗ | static int aax_read_close(AVFormatContext *s) | |
| 377 | { | ||
| 378 | ✗ | AAXContext *a = s->priv_data; | |
| 379 | |||
| 380 | ✗ | av_freep(&a->segments); | |
| 381 | ✗ | av_freep(&a->xcolumns); | |
| 382 | ✗ | av_freep(&a->string_table); | |
| 383 | |||
| 384 | ✗ | return 0; | |
| 385 | } | ||
| 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 | }; | ||
| 399 |