aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/argo_asf.c
blob: 94d3ae3e091a83e5ca9ba794ef156daf404a3bd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
 * Argonaut Games ASF (de)muxer
 *
 * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
#include "avformat.h"
#include "internal.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/avassert.h"

#define ASF_TAG                 MKTAG('A', 'S', 'F', '\0')
#define ASF_FILE_HEADER_SIZE    24
#define ASF_CHUNK_HEADER_SIZE   20
#define ASF_SAMPLE_COUNT        32

typedef struct ArgoASFFileHeader {
    uint32_t    magic;          /*< Magic Number, {'A', 'S', 'F', '\0'} */
    uint16_t    version_major;  /*< File Major Version. */
    uint16_t    version_minor;  /*< File Minor Version. */
    uint32_t    num_chunks;     /*< No. chunks in the file. */
    uint32_t    chunk_offset;   /*< Offset to the first chunk from the start of the file. */
    int8_t      name[8];        /*< Name. */
} ArgoASFFileHeader;

typedef struct ArgoASFChunkHeader {
    uint32_t    num_blocks;     /*< No. blocks in the chunk. */
    uint32_t    num_samples;    /*< No. samples per channel in a block. Always 32. */
    uint32_t    unk1;           /*< Unknown */
    uint16_t    sample_rate;    /*< Sample rate. */
    uint16_t    unk2;           /*< Unknown. */
    uint32_t    flags;          /*< Stream flags. */
} ArgoASFChunkHeader;

enum {
    ASF_CF_BITS_PER_SAMPLE  = (1 << 0), /*< 16-bit if set, 8 otherwise.      */
    ASF_CF_STEREO           = (1 << 1), /*< Stereo if set, mono otherwise.   */
    ASF_CF_ALWAYS1_1        = (1 << 2), /*< Unknown, always seems to be set. */
    ASF_CF_ALWAYS1_2        = (1 << 3), /*< Unknown, always seems to be set. */

    ASF_CF_ALWAYS1          = ASF_CF_ALWAYS1_1 | ASF_CF_ALWAYS1_2,
    ASF_CF_ALWAYS0          = ~(ASF_CF_BITS_PER_SAMPLE | ASF_CF_STEREO | ASF_CF_ALWAYS1)
};

typedef struct ArgoASFDemuxContext {
    ArgoASFFileHeader   fhdr;
    ArgoASFChunkHeader  ckhdr;
    uint32_t            blocks_read;
} ArgoASFDemuxContext;

#if CONFIG_ARGO_ASF_DEMUXER
static void argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf)
{
    hdr->magic          = AV_RL32(buf + 0);
    hdr->version_major  = AV_RL16(buf + 4);
    hdr->version_minor  = AV_RL16(buf + 6);
    hdr->num_chunks     = AV_RL32(buf + 8);
    hdr->chunk_offset   = AV_RL32(buf + 12);
    for (int i = 0; i < FF_ARRAY_ELEMS(hdr->name); i++)
        hdr->name[i]    = AV_RL8(buf + 16 + i);
}

static void argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf)
{
    hdr->num_blocks     = AV_RL32(buf + 0);
    hdr->num_samples    = AV_RL32(buf + 4);
    hdr->unk1           = AV_RL32(buf + 8);
    hdr->sample_rate    = AV_RL16(buf + 12);
    hdr->unk2           = AV_RL16(buf + 14);
    hdr->flags          = AV_RL32(buf + 16);
}

/*
 * Known versions:
 * 1.1: https://samples.ffmpeg.org/game-formats/brender/part2.zip
 *      FX Fighter
 * 1.2: Croc! Legend of the Gobbos
 * 2.1: Croc 2
 *      The Emperor's New Groove
 *      Disney's Aladdin in Nasira's Revenge
 */
static int argo_asf_is_known_version(const ArgoASFFileHeader *hdr)
{
    return (hdr->version_major == 1 && hdr->version_minor == 1) ||
           (hdr->version_major == 1 && hdr->version_minor == 2) ||
           (hdr->version_major == 2 && hdr->version_minor == 1);
}

static int argo_asf_probe(const AVProbeData *p)
{
    ArgoASFFileHeader hdr;

    av_assert0(AVPROBE_PADDING_SIZE >= ASF_FILE_HEADER_SIZE);

    argo_asf_parse_file_header(&hdr, p->buf);

    if (hdr.magic != ASF_TAG)
        return 0;

    if (!argo_asf_is_known_version(&hdr))
        return AVPROBE_SCORE_EXTENSION / 2;

    return AVPROBE_SCORE_EXTENSION + 1;
}

static int argo_asf_read_header(AVFormatContext *s)
{
    int64_t ret;
    AVIOContext *pb = s->pb;
    AVStream *st;
    ArgoASFDemuxContext *asf = s->priv_data;
    uint8_t buf[FFMAX(ASF_FILE_HEADER_SIZE, ASF_CHUNK_HEADER_SIZE)];

    if (!(st = avformat_new_stream(s, NULL)))
        return AVERROR(ENOMEM);

    if ((ret = avio_read(pb, buf, ASF_FILE_HEADER_SIZE)) < 0)
        return ret;
    else if (ret != ASF_FILE_HEADER_SIZE)
        return AVERROR(EIO);

    argo_asf_parse_file_header(&asf->fhdr, buf);

    if (!argo_asf_is_known_version(&asf->fhdr)) {
        avpriv_request_sample(s, "Version %hu.%hu",
            asf->fhdr.version_major, asf->fhdr.version_minor
        );
        return AVERROR_PATCHWELCOME;
    }

    if (asf->fhdr.num_chunks == 0) {
        return AVERROR_INVALIDDATA;
    } else if (asf->fhdr.num_chunks > 1) {
        avpriv_request_sample(s, ">1 chunk");
        return AVERROR_PATCHWELCOME;
    }

    if (asf->fhdr.chunk_offset < ASF_FILE_HEADER_SIZE)
        return AVERROR_INVALIDDATA;

    if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 0)
        return ret;

    if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
        return ret;
    else if (ret != ASF_CHUNK_HEADER_SIZE)
        return AVERROR(EIO);

    argo_asf_parse_chunk_header(&asf->ckhdr, buf);

    if (asf->ckhdr.num_samples != ASF_SAMPLE_COUNT) {
        av_log(s, AV_LOG_ERROR, "Invalid sample count. Got %u, expected %d\n",
               asf->ckhdr.num_samples, ASF_SAMPLE_COUNT);
        return AVERROR_INVALIDDATA;
    }

    if ((asf->ckhdr.flags & ASF_CF_ALWAYS1) != ASF_CF_ALWAYS1 || (asf->ckhdr.flags & ASF_CF_ALWAYS0) != 0) {
        avpriv_request_sample(s, "Nonstandard flags (0x%08X)", asf->ckhdr.flags);
        return AVERROR_PATCHWELCOME;
    }

    st->codecpar->codec_type                = AVMEDIA_TYPE_AUDIO;
    st->codecpar->codec_id                  = AV_CODEC_ID_ADPCM_ARGO;
    st->codecpar->format                    = AV_SAMPLE_FMT_S16P;

    if (asf->ckhdr.flags & ASF_CF_STEREO) {
        st->codecpar->channel_layout        = AV_CH_LAYOUT_STEREO;
        st->codecpar->channels              = 2;
    } else {
        st->codecpar->channel_layout        = AV_CH_LAYOUT_MONO;
        st->codecpar->channels              = 1;
    }

    st->codecpar->sample_rate               = asf->ckhdr.sample_rate;

    st->codecpar->bits_per_coded_sample     = 4;

    if (asf->ckhdr.flags & ASF_CF_BITS_PER_SAMPLE)
        st->codecpar->bits_per_raw_sample   = 16;
    else
        st->codecpar->bits_per_raw_sample   = 8;

    if (st->codecpar->bits_per_raw_sample != 16) {
        /* The header allows for these, but I've never seen any files with them. */
        avpriv_request_sample(s, "Non 16-bit samples");
        return AVERROR_PATCHWELCOME;
    }

    /*
     * (nchannel control bytes) + ((bytes_per_channel) * nchannel)
     * For mono, this is 17. For stereo, this is 34.
     */
    st->codecpar->frame_size            = st->codecpar->channels +
                                          (asf->ckhdr.num_samples / 2) *
                                          st->codecpar->channels;

    st->codecpar->block_align           = st->codecpar->frame_size;

    st->codecpar->bit_rate              = st->codecpar->channels *
                                          st->codecpar->sample_rate *
                                          st->codecpar->bits_per_coded_sample;

    avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
    st->start_time      = 0;
    st->duration        = asf->ckhdr.num_blocks * asf->ckhdr.num_samples;
    st->nb_frames       = asf->ckhdr.num_blocks;
    return 0;
}

static int argo_asf_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    ArgoASFDemuxContext *asf = s->priv_data;

    AVStream *st = s->streams[0];
    AVIOContext *pb = s->pb;
    int ret;

    if (asf->blocks_read >= asf->ckhdr.num_blocks)
        return AVERROR_EOF;

    if ((ret = av_get_packet(pb, pkt, st->codecpar->frame_size)) < 0)
        return ret;
    else if (ret != st->codecpar->frame_size)
        return AVERROR_INVALIDDATA;

    pkt->stream_index   = st->index;
    pkt->duration       = asf->ckhdr.num_samples;

    ++asf->blocks_read;
    return 0;
}

/*
 * Not actually sure what ASF stands for.
 * - Argonaut Sound File?
 * - Audio Stream File?
 */
AVInputFormat ff_argo_asf_demuxer = {
    .name           = "argo_asf",
    .long_name      = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
    .priv_data_size = sizeof(ArgoASFDemuxContext),
    .read_probe     = argo_asf_probe,
    .read_header    = argo_asf_read_header,
    .read_packet    = argo_asf_read_packet
};
#endif

#if CONFIG_ARGO_ASF_MUXER
static int argo_asf_write_init(AVFormatContext *s)
{
    const AVCodecParameters *par;

    if (s->nb_streams != 1) {
        av_log(s, AV_LOG_ERROR, "ASF files have exactly one stream\n");
        return AVERROR(EINVAL);
    }

    par = s->streams[0]->codecpar;

    if (par->codec_id != AV_CODEC_ID_ADPCM_ARGO) {
        av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
               avcodec_get_name(par->codec_id));
        return AVERROR(EINVAL);
    }

    if (par->channels > 2) {
        av_log(s, AV_LOG_ERROR, "ASF files only support up to 2 channels\n");
        return AVERROR(EINVAL);
    }

    if (par->sample_rate > UINT16_MAX) {
        av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
        return AVERROR(EINVAL);
    }

    if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
        av_log(s, AV_LOG_ERROR, "Stream not seekable, unable to write output file\n");
        return AVERROR(EINVAL);
    }

    return 0;
}

static void argo_asf_write_file_header(const ArgoASFFileHeader *fhdr, AVIOContext *pb)
{
    avio_wl32( pb, fhdr->magic);
    avio_wl16( pb, fhdr->version_major);
    avio_wl16( pb, fhdr->version_minor);
    avio_wl32( pb, fhdr->num_chunks);
    avio_wl32( pb, fhdr->chunk_offset);
    avio_write(pb, fhdr->name, sizeof(fhdr->name));
}

static void argo_asf_write_chunk_header(const ArgoASFChunkHeader *ckhdr, AVIOContext *pb)
{
    avio_wl32(pb, ckhdr->num_blocks);
    avio_wl32(pb, ckhdr->num_samples);
    avio_wl32(pb, ckhdr->unk1);
    avio_wl16(pb, ckhdr->sample_rate);
    avio_wl16(pb, ckhdr->unk2);
    avio_wl32(pb, ckhdr->flags);
}

static int argo_asf_write_header(AVFormatContext *s)
{
    const AVCodecParameters  *par = s->streams[0]->codecpar;
    ArgoASFFileHeader  fhdr;
    ArgoASFChunkHeader chdr;

    fhdr.magic         = ASF_TAG;
    fhdr.version_major = 2;
    fhdr.version_minor = 1;
    fhdr.num_chunks    = 1;
    fhdr.chunk_offset  = ASF_FILE_HEADER_SIZE;
    strncpy(fhdr.name, av_basename(s->url), FF_ARRAY_ELEMS(fhdr.name));

    chdr.num_blocks    = 0;
    chdr.num_samples   = ASF_SAMPLE_COUNT;
    chdr.unk1          = 0;
    chdr.sample_rate   = par->sample_rate;
    chdr.unk2          = ~0;
    chdr.flags         = ASF_CF_BITS_PER_SAMPLE | ASF_CF_ALWAYS1;

    if (par->channels == 2)
        chdr.flags |= ASF_CF_STEREO;

    argo_asf_write_file_header(&fhdr, s->pb);
    argo_asf_write_chunk_header(&chdr, s->pb);
    return 0;
}

static int argo_asf_write_packet(AVFormatContext *s, AVPacket *pkt)
{
    if (pkt->size != 17 * s->streams[0]->codecpar->channels)
        return AVERROR_INVALIDDATA;

    if (s->streams[0]->nb_frames >= UINT32_MAX)
        return AVERROR_INVALIDDATA;

    avio_write(s->pb, pkt->data, pkt->size);
    return 0;
}

static int argo_asf_write_trailer(AVFormatContext *s)
{
    int64_t ret;

    if ((ret = avio_seek(s->pb, ASF_FILE_HEADER_SIZE, SEEK_SET) < 0))
        return ret;

    avio_wl32(s->pb, (uint32_t)s->streams[0]->nb_frames);
    return 0;
}

AVOutputFormat ff_argo_asf_muxer = {
    .name           = "argo_asf",
    .long_name      = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
    /*
     * NB: Can't do this as it conflicts with the actual ASF format.
     * .extensions  = "asf",
     */
    .audio_codec    = AV_CODEC_ID_ADPCM_ARGO,
    .video_codec    = AV_CODEC_ID_NONE,
    .init           = argo_asf_write_init,
    .write_header   = argo_asf_write_header,
    .write_packet   = argo_asf_write_packet,
    .write_trailer  = argo_asf_write_trailer
};
#endif