2 * Argonaut Games ASF demuxer
4 * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/avassert.h"
27 #define ASF_TAG MKTAG('A', 'S', 'F', '\0')
28 #define ASF_FILE_HEADER_SIZE 24
29 #define ASF_CHUNK_HEADER_SIZE 20
31 typedef struct ArgoASFFileHeader {
32 uint32_t magic; /*< Magic Number, {'A', 'S', 'F', '\0'} */
33 uint16_t version_major; /*< File Major Version. */
34 uint16_t version_minor; /*< File Minor Version. */
35 uint32_t num_chunks; /*< No. chunks in the file. */
36 uint32_t chunk_offset; /*< Offset to the first chunk from the start of the file. */
37 int8_t name[8]; /*< Name. */
40 typedef struct ArgoASFChunkHeader {
41 uint32_t num_blocks; /*< No. blocks in the chunk. */
42 uint32_t num_samples; /*< No. samples per channel in a block. */
43 uint32_t unk1; /*< Unknown */
44 uint16_t sample_rate; /*< Sample rate. */
45 uint16_t unk2; /*< Unknown. */
46 uint32_t flags; /*< Stream flags. */
50 ASF_CF_BITS_PER_SAMPLE = (1 << 0), /*< 16-bit if set, 8 otherwise. */
51 ASF_CF_STEREO = (1 << 1), /*< Stereo if set, mono otherwise. */
52 ASF_CF_ALWAYS1_1 = (1 << 2), /*< Unknown, always seems to be set. */
53 ASF_CF_ALWAYS1_2 = (1 << 3), /*< Unknown, always seems to be set. */
55 ASF_CF_ALWAYS1 = ASF_CF_ALWAYS1_1 | ASF_CF_ALWAYS1_2,
56 ASF_CF_ALWAYS0 = ~(ASF_CF_BITS_PER_SAMPLE | ASF_CF_STEREO | ASF_CF_ALWAYS1)
59 typedef struct ArgoASFDemuxContext {
60 ArgoASFFileHeader fhdr;
61 ArgoASFChunkHeader ckhdr;
63 } ArgoASFDemuxContext;
65 static void argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf)
67 hdr->magic = AV_RL32(buf + 0);
68 hdr->version_major = AV_RL16(buf + 4);
69 hdr->version_minor = AV_RL16(buf + 6);
70 hdr->num_chunks = AV_RL32(buf + 8);
71 hdr->chunk_offset = AV_RL32(buf + 12);
72 for (int i = 0; i < FF_ARRAY_ELEMS(hdr->name); i++)
73 hdr->name[i] = AV_RL8(buf + 16 + i);
76 static void argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf)
78 hdr->num_blocks = AV_RL32(buf + 0);
79 hdr->num_samples = AV_RL32(buf + 4);
80 hdr->unk1 = AV_RL32(buf + 8);
81 hdr->sample_rate = AV_RL16(buf + 12);
82 hdr->unk2 = AV_RL16(buf + 14);
83 hdr->flags = AV_RL32(buf + 16);
88 * 1.1: The sample files in /game-formats/brender/part2.zip
89 * 1.2: Croc! Legend of the Gobbos
92 static int argo_asf_is_known_version(const ArgoASFFileHeader *hdr)
94 return (hdr->version_major == 1 && hdr->version_minor == 1) ||
95 (hdr->version_major == 1 && hdr->version_minor == 2) ||
96 (hdr->version_major == 2 && hdr->version_minor == 1);
99 static int argo_asf_probe(const AVProbeData *p)
101 ArgoASFFileHeader hdr;
103 av_assert0(AVPROBE_PADDING_SIZE >= ASF_FILE_HEADER_SIZE);
105 argo_asf_parse_file_header(&hdr, p->buf);
107 if (hdr.magic != ASF_TAG)
110 if (!argo_asf_is_known_version(&hdr))
111 return AVPROBE_SCORE_EXTENSION / 2;
113 return AVPROBE_SCORE_EXTENSION + 1;
116 static int argo_asf_read_header(AVFormatContext *s)
119 AVIOContext *pb = s->pb;
121 ArgoASFDemuxContext *asf = s->priv_data;
122 uint8_t buf[FFMAX(ASF_FILE_HEADER_SIZE, ASF_CHUNK_HEADER_SIZE)];
124 if (!(st = avformat_new_stream(s, NULL)))
125 return AVERROR(ENOMEM);
127 if ((ret = avio_read(pb, buf, ASF_FILE_HEADER_SIZE)) < 0)
129 else if (ret != ASF_FILE_HEADER_SIZE)
132 argo_asf_parse_file_header(&asf->fhdr, buf);
134 if (!argo_asf_is_known_version(&asf->fhdr)) {
135 avpriv_request_sample(s, "Version %hu.%hu",
136 asf->fhdr.version_major, asf->fhdr.version_minor
138 return AVERROR_PATCHWELCOME;
141 if (asf->fhdr.num_chunks == 0) {
142 return AVERROR_INVALIDDATA;
143 } else if (asf->fhdr.num_chunks > 1) {
144 avpriv_request_sample(s, ">1 chunk");
145 return AVERROR_PATCHWELCOME;
148 if (asf->fhdr.chunk_offset < ASF_FILE_HEADER_SIZE)
149 return AVERROR_INVALIDDATA;
151 if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 0)
154 if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
156 else if (ret != ASF_CHUNK_HEADER_SIZE)
159 argo_asf_parse_chunk_header(&asf->ckhdr, buf);
161 if ((asf->ckhdr.flags & ASF_CF_ALWAYS1) != ASF_CF_ALWAYS1 || (asf->ckhdr.flags & ASF_CF_ALWAYS0) != 0) {
162 avpriv_request_sample(s, "Nonstandard flags (0x%08X)", asf->ckhdr.flags);
163 return AVERROR_PATCHWELCOME;
166 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
167 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO;
168 st->codecpar->format = AV_SAMPLE_FMT_S16P;
170 if (asf->ckhdr.flags & ASF_CF_STEREO) {
171 st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
172 st->codecpar->channels = 2;
174 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
175 st->codecpar->channels = 1;
178 st->codecpar->sample_rate = asf->ckhdr.sample_rate;
180 st->codecpar->bits_per_coded_sample = 4;
182 if (asf->ckhdr.flags & ASF_CF_BITS_PER_SAMPLE)
183 st->codecpar->bits_per_raw_sample = 16;
185 st->codecpar->bits_per_raw_sample = 8;
187 if (st->codecpar->bits_per_raw_sample != 16) {
188 /* The header allows for these, but I've never seen any files with them. */
189 avpriv_request_sample(s, "Non 16-bit samples");
190 return AVERROR_PATCHWELCOME;
194 * (nchannel control bytes) + ((bytes_per_channel) * nchannel)
195 * For mono, this is 17. For stereo, this is 34.
197 st->codecpar->frame_size = st->codecpar->channels +
198 (asf->ckhdr.num_samples / 2) *
199 st->codecpar->channels;
201 st->codecpar->block_align = st->codecpar->frame_size;
203 st->codecpar->bit_rate = st->codecpar->channels *
204 st->codecpar->sample_rate *
205 st->codecpar->bits_per_coded_sample;
207 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
209 st->duration = asf->ckhdr.num_blocks * asf->ckhdr.num_samples;
210 st->nb_frames = asf->ckhdr.num_blocks;
214 static int argo_asf_read_packet(AVFormatContext *s, AVPacket *pkt)
216 ArgoASFDemuxContext *asf = s->priv_data;
218 AVStream *st = s->streams[0];
219 AVIOContext *pb = s->pb;
222 if (asf->blocks_read >= asf->ckhdr.num_blocks)
225 if ((ret = av_get_packet(pb, pkt, st->codecpar->frame_size)) < 0)
227 else if (ret != st->codecpar->frame_size)
228 return AVERROR_INVALIDDATA;
230 pkt->stream_index = st->index;
231 pkt->duration = asf->ckhdr.num_samples;
238 * Not actually sure what ASF stands for.
239 * - Argonaut Sound File?
240 * - Audio Stream File?
242 AVInputFormat ff_argo_asf_demuxer = {
244 .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
245 .priv_data_size = sizeof(ArgoASFDemuxContext),
246 .read_probe = argo_asf_probe,
247 .read_header = argo_asf_read_header,
248 .read_packet = argo_asf_read_packet