2 * Argonaut Games ASF (de)muxer
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
30 #define ASF_SAMPLE_COUNT 32
32 typedef struct ArgoASFFileHeader {
33 uint32_t magic; /*< Magic Number, {'A', 'S', 'F', '\0'} */
34 uint16_t version_major; /*< File Major Version. */
35 uint16_t version_minor; /*< File Minor Version. */
36 uint32_t num_chunks; /*< No. chunks in the file. */
37 uint32_t chunk_offset; /*< Offset to the first chunk from the start of the file. */
38 int8_t name[8]; /*< Name. */
41 typedef struct ArgoASFChunkHeader {
42 uint32_t num_blocks; /*< No. blocks in the chunk. */
43 uint32_t num_samples; /*< No. samples per channel in a block. Always 32. */
44 uint32_t unk1; /*< Unknown */
45 uint16_t sample_rate; /*< Sample rate. */
46 uint16_t unk2; /*< Unknown. */
47 uint32_t flags; /*< Stream flags. */
51 ASF_CF_BITS_PER_SAMPLE = (1 << 0), /*< 16-bit if set, 8 otherwise. */
52 ASF_CF_STEREO = (1 << 1), /*< Stereo if set, mono otherwise. */
53 ASF_CF_ALWAYS1_1 = (1 << 2), /*< Unknown, always seems to be set. */
54 ASF_CF_ALWAYS1_2 = (1 << 3), /*< Unknown, always seems to be set. */
56 ASF_CF_ALWAYS1 = ASF_CF_ALWAYS1_1 | ASF_CF_ALWAYS1_2,
57 ASF_CF_ALWAYS0 = ~(ASF_CF_BITS_PER_SAMPLE | ASF_CF_STEREO | ASF_CF_ALWAYS1)
60 typedef struct ArgoASFDemuxContext {
61 ArgoASFFileHeader fhdr;
62 ArgoASFChunkHeader ckhdr;
64 } ArgoASFDemuxContext;
66 #if CONFIG_ARGO_ASF_DEMUXER
67 static void argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf)
69 hdr->magic = AV_RL32(buf + 0);
70 hdr->version_major = AV_RL16(buf + 4);
71 hdr->version_minor = AV_RL16(buf + 6);
72 hdr->num_chunks = AV_RL32(buf + 8);
73 hdr->chunk_offset = AV_RL32(buf + 12);
74 for (int i = 0; i < FF_ARRAY_ELEMS(hdr->name); i++)
75 hdr->name[i] = AV_RL8(buf + 16 + i);
78 static void argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf)
80 hdr->num_blocks = AV_RL32(buf + 0);
81 hdr->num_samples = AV_RL32(buf + 4);
82 hdr->unk1 = AV_RL32(buf + 8);
83 hdr->sample_rate = AV_RL16(buf + 12);
84 hdr->unk2 = AV_RL16(buf + 14);
85 hdr->flags = AV_RL32(buf + 16);
90 * 1.1: https://samples.ffmpeg.org/game-formats/brender/part2.zip
92 * 1.2: Croc! Legend of the Gobbos
94 * The Emperor's New Groove
95 * Disney's Aladdin in Nasira's Revenge
97 static int argo_asf_is_known_version(const ArgoASFFileHeader *hdr)
99 return (hdr->version_major == 1 && hdr->version_minor == 1) ||
100 (hdr->version_major == 1 && hdr->version_minor == 2) ||
101 (hdr->version_major == 2 && hdr->version_minor == 1);
104 static int argo_asf_probe(const AVProbeData *p)
106 ArgoASFFileHeader hdr;
108 av_assert0(AVPROBE_PADDING_SIZE >= ASF_FILE_HEADER_SIZE);
110 argo_asf_parse_file_header(&hdr, p->buf);
112 if (hdr.magic != ASF_TAG)
115 if (!argo_asf_is_known_version(&hdr))
116 return AVPROBE_SCORE_EXTENSION / 2;
118 return AVPROBE_SCORE_EXTENSION + 1;
121 static int argo_asf_read_header(AVFormatContext *s)
124 AVIOContext *pb = s->pb;
126 ArgoASFDemuxContext *asf = s->priv_data;
127 uint8_t buf[FFMAX(ASF_FILE_HEADER_SIZE, ASF_CHUNK_HEADER_SIZE)];
129 if (!(st = avformat_new_stream(s, NULL)))
130 return AVERROR(ENOMEM);
132 if ((ret = avio_read(pb, buf, ASF_FILE_HEADER_SIZE)) < 0)
134 else if (ret != ASF_FILE_HEADER_SIZE)
137 argo_asf_parse_file_header(&asf->fhdr, buf);
139 if (asf->fhdr.num_chunks == 0) {
140 return AVERROR_INVALIDDATA;
141 } else if (asf->fhdr.num_chunks > 1) {
142 avpriv_request_sample(s, ">1 chunk");
143 return AVERROR_PATCHWELCOME;
146 if (asf->fhdr.chunk_offset < ASF_FILE_HEADER_SIZE)
147 return AVERROR_INVALIDDATA;
149 if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 0)
152 if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
154 else if (ret != ASF_CHUNK_HEADER_SIZE)
157 argo_asf_parse_chunk_header(&asf->ckhdr, buf);
159 if (asf->ckhdr.num_samples != ASF_SAMPLE_COUNT) {
160 av_log(s, AV_LOG_ERROR, "Invalid sample count. Got %u, expected %d\n",
161 asf->ckhdr.num_samples, ASF_SAMPLE_COUNT);
162 return AVERROR_INVALIDDATA;
165 if ((asf->ckhdr.flags & ASF_CF_ALWAYS1) != ASF_CF_ALWAYS1 || (asf->ckhdr.flags & ASF_CF_ALWAYS0) != 0) {
166 avpriv_request_sample(s, "Nonstandard flags (0x%08X)", asf->ckhdr.flags);
167 return AVERROR_PATCHWELCOME;
170 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
171 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO;
172 st->codecpar->format = AV_SAMPLE_FMT_S16P;
174 if (asf->ckhdr.flags & ASF_CF_STEREO) {
175 st->codecpar->channel_layout = AV_CH_LAYOUT_STEREO;
176 st->codecpar->channels = 2;
178 st->codecpar->channel_layout = AV_CH_LAYOUT_MONO;
179 st->codecpar->channels = 1;
182 st->codecpar->sample_rate = asf->ckhdr.sample_rate;
184 st->codecpar->bits_per_coded_sample = 4;
186 if (asf->ckhdr.flags & ASF_CF_BITS_PER_SAMPLE)
187 st->codecpar->bits_per_raw_sample = 16;
189 st->codecpar->bits_per_raw_sample = 8;
191 if (st->codecpar->bits_per_raw_sample != 16) {
192 /* The header allows for these, but I've never seen any files with them. */
193 avpriv_request_sample(s, "Non 16-bit samples");
194 return AVERROR_PATCHWELCOME;
198 * (nchannel control bytes) + ((bytes_per_channel) * nchannel)
199 * For mono, this is 17. For stereo, this is 34.
201 st->codecpar->frame_size = st->codecpar->channels +
202 (asf->ckhdr.num_samples / 2) *
203 st->codecpar->channels;
205 st->codecpar->block_align = st->codecpar->frame_size;
207 st->codecpar->bit_rate = st->codecpar->channels *
208 st->codecpar->sample_rate *
209 st->codecpar->bits_per_coded_sample;
211 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
213 st->duration = asf->ckhdr.num_blocks * asf->ckhdr.num_samples;
214 st->nb_frames = asf->ckhdr.num_blocks;
218 static int argo_asf_read_packet(AVFormatContext *s, AVPacket *pkt)
220 ArgoASFDemuxContext *asf = s->priv_data;
222 AVStream *st = s->streams[0];
223 AVIOContext *pb = s->pb;
226 if (asf->blocks_read >= asf->ckhdr.num_blocks)
229 if ((ret = av_get_packet(pb, pkt, st->codecpar->frame_size)) < 0)
231 else if (ret != st->codecpar->frame_size)
232 return AVERROR_INVALIDDATA;
234 pkt->stream_index = st->index;
235 pkt->duration = asf->ckhdr.num_samples;
242 * Not actually sure what ASF stands for.
243 * - Argonaut Sound File?
244 * - Audio Stream File?
246 AVInputFormat ff_argo_asf_demuxer = {
248 .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
249 .priv_data_size = sizeof(ArgoASFDemuxContext),
250 .read_probe = argo_asf_probe,
251 .read_header = argo_asf_read_header,
252 .read_packet = argo_asf_read_packet
256 #if CONFIG_ARGO_ASF_MUXER
257 static int argo_asf_write_init(AVFormatContext *s)
259 const AVCodecParameters *par;
261 if (s->nb_streams != 1) {
262 av_log(s, AV_LOG_ERROR, "ASF files have exactly one stream\n");
263 return AVERROR(EINVAL);
266 par = s->streams[0]->codecpar;
268 if (par->codec_id != AV_CODEC_ID_ADPCM_ARGO) {
269 av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
270 avcodec_get_name(par->codec_id));
271 return AVERROR(EINVAL);
274 if (par->channels > 2) {
275 av_log(s, AV_LOG_ERROR, "ASF files only support up to 2 channels\n");
276 return AVERROR(EINVAL);
279 if (par->sample_rate > UINT16_MAX) {
280 av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
281 return AVERROR(EINVAL);
284 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
285 av_log(s, AV_LOG_ERROR, "Stream not seekable, unable to write output file\n");
286 return AVERROR(EINVAL);
292 static void argo_asf_write_file_header(const ArgoASFFileHeader *fhdr, AVIOContext *pb)
294 avio_wl32( pb, fhdr->magic);
295 avio_wl16( pb, fhdr->version_major);
296 avio_wl16( pb, fhdr->version_minor);
297 avio_wl32( pb, fhdr->num_chunks);
298 avio_wl32( pb, fhdr->chunk_offset);
299 avio_write(pb, fhdr->name, sizeof(fhdr->name));
302 static void argo_asf_write_chunk_header(const ArgoASFChunkHeader *ckhdr, AVIOContext *pb)
304 avio_wl32(pb, ckhdr->num_blocks);
305 avio_wl32(pb, ckhdr->num_samples);
306 avio_wl32(pb, ckhdr->unk1);
307 avio_wl16(pb, ckhdr->sample_rate);
308 avio_wl16(pb, ckhdr->unk2);
309 avio_wl32(pb, ckhdr->flags);
312 static int argo_asf_write_header(AVFormatContext *s)
314 const AVCodecParameters *par = s->streams[0]->codecpar;
315 ArgoASFFileHeader fhdr;
316 ArgoASFChunkHeader chdr;
318 fhdr.magic = ASF_TAG;
319 fhdr.version_major = 2;
320 fhdr.version_minor = 1;
322 fhdr.chunk_offset = ASF_FILE_HEADER_SIZE;
323 strncpy(fhdr.name, av_basename(s->url), FF_ARRAY_ELEMS(fhdr.name));
326 chdr.num_samples = ASF_SAMPLE_COUNT;
328 chdr.sample_rate = par->sample_rate;
330 chdr.flags = ASF_CF_BITS_PER_SAMPLE | ASF_CF_ALWAYS1;
332 if (par->channels == 2)
333 chdr.flags |= ASF_CF_STEREO;
335 argo_asf_write_file_header(&fhdr, s->pb);
336 argo_asf_write_chunk_header(&chdr, s->pb);
340 static int argo_asf_write_packet(AVFormatContext *s, AVPacket *pkt)
342 if (pkt->size != 17 * s->streams[0]->codecpar->channels)
343 return AVERROR_INVALIDDATA;
345 if (s->streams[0]->nb_frames >= UINT32_MAX)
346 return AVERROR_INVALIDDATA;
348 avio_write(s->pb, pkt->data, pkt->size);
352 static int argo_asf_write_trailer(AVFormatContext *s)
356 if ((ret = avio_seek(s->pb, ASF_FILE_HEADER_SIZE, SEEK_SET) < 0))
359 avio_wl32(s->pb, (uint32_t)s->streams[0]->nb_frames);
363 AVOutputFormat ff_argo_asf_muxer = {
365 .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
367 * NB: Can't do this as it conflicts with the actual ASF format.
368 * .extensions = "asf",
370 .audio_codec = AV_CODEC_ID_ADPCM_ARGO,
371 .video_codec = AV_CODEC_ID_NONE,
372 .init = argo_asf_write_init,
373 .write_header = argo_asf_write_header,
374 .write_packet = argo_asf_write_packet,
375 .write_trailer = argo_asf_write_trailer