]> git.sesse.net Git - ffmpeg/blob - libavformat/argo_asf.c
avformat/argo_asf: don't check file version
[ffmpeg] / libavformat / argo_asf.c
1 /*
2  * Argonaut Games ASF (de)muxer
3  *
4  * Copyright (C) 2020 Zane van Iperen (zane@zanevaniperen.com)
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 #include "avformat.h"
23 #include "internal.h"
24 #include "libavutil/intreadwrite.h"
25 #include "libavutil/avassert.h"
26
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
31
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. */
39 } ArgoASFFileHeader;
40
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. */
48 } ArgoASFChunkHeader;
49
50 enum {
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. */
55
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)
58 };
59
60 typedef struct ArgoASFDemuxContext {
61     ArgoASFFileHeader   fhdr;
62     ArgoASFChunkHeader  ckhdr;
63     uint32_t            blocks_read;
64 } ArgoASFDemuxContext;
65
66 #if CONFIG_ARGO_ASF_DEMUXER
67 static void argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf)
68 {
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);
76 }
77
78 static void argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf)
79 {
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);
86 }
87
88 /*
89  * Known versions:
90  * 1.1: https://samples.ffmpeg.org/game-formats/brender/part2.zip
91  *      FX Fighter
92  * 1.2: Croc! Legend of the Gobbos
93  * 2.1: Croc 2
94  *      The Emperor's New Groove
95  *      Disney's Aladdin in Nasira's Revenge
96  */
97 static int argo_asf_is_known_version(const ArgoASFFileHeader *hdr)
98 {
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);
102 }
103
104 static int argo_asf_probe(const AVProbeData *p)
105 {
106     ArgoASFFileHeader hdr;
107
108     av_assert0(AVPROBE_PADDING_SIZE >= ASF_FILE_HEADER_SIZE);
109
110     argo_asf_parse_file_header(&hdr, p->buf);
111
112     if (hdr.magic != ASF_TAG)
113         return 0;
114
115     if (!argo_asf_is_known_version(&hdr))
116         return AVPROBE_SCORE_EXTENSION / 2;
117
118     return AVPROBE_SCORE_EXTENSION + 1;
119 }
120
121 static int argo_asf_read_header(AVFormatContext *s)
122 {
123     int64_t ret;
124     AVIOContext *pb = s->pb;
125     AVStream *st;
126     ArgoASFDemuxContext *asf = s->priv_data;
127     uint8_t buf[FFMAX(ASF_FILE_HEADER_SIZE, ASF_CHUNK_HEADER_SIZE)];
128
129     if (!(st = avformat_new_stream(s, NULL)))
130         return AVERROR(ENOMEM);
131
132     if ((ret = avio_read(pb, buf, ASF_FILE_HEADER_SIZE)) < 0)
133         return ret;
134     else if (ret != ASF_FILE_HEADER_SIZE)
135         return AVERROR(EIO);
136
137     argo_asf_parse_file_header(&asf->fhdr, buf);
138
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;
144     }
145
146     if (asf->fhdr.chunk_offset < ASF_FILE_HEADER_SIZE)
147         return AVERROR_INVALIDDATA;
148
149     if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 0)
150         return ret;
151
152     if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
153         return ret;
154     else if (ret != ASF_CHUNK_HEADER_SIZE)
155         return AVERROR(EIO);
156
157     argo_asf_parse_chunk_header(&asf->ckhdr, buf);
158
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;
163     }
164
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;
168     }
169
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;
173
174     if (asf->ckhdr.flags & ASF_CF_STEREO) {
175         st->codecpar->channel_layout        = AV_CH_LAYOUT_STEREO;
176         st->codecpar->channels              = 2;
177     } else {
178         st->codecpar->channel_layout        = AV_CH_LAYOUT_MONO;
179         st->codecpar->channels              = 1;
180     }
181
182     st->codecpar->sample_rate               = asf->ckhdr.sample_rate;
183
184     st->codecpar->bits_per_coded_sample     = 4;
185
186     if (asf->ckhdr.flags & ASF_CF_BITS_PER_SAMPLE)
187         st->codecpar->bits_per_raw_sample   = 16;
188     else
189         st->codecpar->bits_per_raw_sample   = 8;
190
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;
195     }
196
197     /*
198      * (nchannel control bytes) + ((bytes_per_channel) * nchannel)
199      * For mono, this is 17. For stereo, this is 34.
200      */
201     st->codecpar->frame_size            = st->codecpar->channels +
202                                           (asf->ckhdr.num_samples / 2) *
203                                           st->codecpar->channels;
204
205     st->codecpar->block_align           = st->codecpar->frame_size;
206
207     st->codecpar->bit_rate              = st->codecpar->channels *
208                                           st->codecpar->sample_rate *
209                                           st->codecpar->bits_per_coded_sample;
210
211     avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
212     st->start_time      = 0;
213     st->duration        = asf->ckhdr.num_blocks * asf->ckhdr.num_samples;
214     st->nb_frames       = asf->ckhdr.num_blocks;
215     return 0;
216 }
217
218 static int argo_asf_read_packet(AVFormatContext *s, AVPacket *pkt)
219 {
220     ArgoASFDemuxContext *asf = s->priv_data;
221
222     AVStream *st = s->streams[0];
223     AVIOContext *pb = s->pb;
224     int ret;
225
226     if (asf->blocks_read >= asf->ckhdr.num_blocks)
227         return AVERROR_EOF;
228
229     if ((ret = av_get_packet(pb, pkt, st->codecpar->frame_size)) < 0)
230         return ret;
231     else if (ret != st->codecpar->frame_size)
232         return AVERROR_INVALIDDATA;
233
234     pkt->stream_index   = st->index;
235     pkt->duration       = asf->ckhdr.num_samples;
236
237     ++asf->blocks_read;
238     return 0;
239 }
240
241 /*
242  * Not actually sure what ASF stands for.
243  * - Argonaut Sound File?
244  * - Audio Stream File?
245  */
246 AVInputFormat ff_argo_asf_demuxer = {
247     .name           = "argo_asf",
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
253 };
254 #endif
255
256 #if CONFIG_ARGO_ASF_MUXER
257 static int argo_asf_write_init(AVFormatContext *s)
258 {
259     const AVCodecParameters *par;
260
261     if (s->nb_streams != 1) {
262         av_log(s, AV_LOG_ERROR, "ASF files have exactly one stream\n");
263         return AVERROR(EINVAL);
264     }
265
266     par = s->streams[0]->codecpar;
267
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);
272     }
273
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);
277     }
278
279     if (par->sample_rate > UINT16_MAX) {
280         av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
281         return AVERROR(EINVAL);
282     }
283
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);
287     }
288
289     return 0;
290 }
291
292 static void argo_asf_write_file_header(const ArgoASFFileHeader *fhdr, AVIOContext *pb)
293 {
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));
300 }
301
302 static void argo_asf_write_chunk_header(const ArgoASFChunkHeader *ckhdr, AVIOContext *pb)
303 {
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);
310 }
311
312 static int argo_asf_write_header(AVFormatContext *s)
313 {
314     const AVCodecParameters  *par = s->streams[0]->codecpar;
315     ArgoASFFileHeader  fhdr;
316     ArgoASFChunkHeader chdr;
317
318     fhdr.magic         = ASF_TAG;
319     fhdr.version_major = 2;
320     fhdr.version_minor = 1;
321     fhdr.num_chunks    = 1;
322     fhdr.chunk_offset  = ASF_FILE_HEADER_SIZE;
323     strncpy(fhdr.name, av_basename(s->url), FF_ARRAY_ELEMS(fhdr.name));
324
325     chdr.num_blocks    = 0;
326     chdr.num_samples   = ASF_SAMPLE_COUNT;
327     chdr.unk1          = 0;
328     chdr.sample_rate   = par->sample_rate;
329     chdr.unk2          = ~0;
330     chdr.flags         = ASF_CF_BITS_PER_SAMPLE | ASF_CF_ALWAYS1;
331
332     if (par->channels == 2)
333         chdr.flags |= ASF_CF_STEREO;
334
335     argo_asf_write_file_header(&fhdr, s->pb);
336     argo_asf_write_chunk_header(&chdr, s->pb);
337     return 0;
338 }
339
340 static int argo_asf_write_packet(AVFormatContext *s, AVPacket *pkt)
341 {
342     if (pkt->size != 17 * s->streams[0]->codecpar->channels)
343         return AVERROR_INVALIDDATA;
344
345     if (s->streams[0]->nb_frames >= UINT32_MAX)
346         return AVERROR_INVALIDDATA;
347
348     avio_write(s->pb, pkt->data, pkt->size);
349     return 0;
350 }
351
352 static int argo_asf_write_trailer(AVFormatContext *s)
353 {
354     int64_t ret;
355
356     if ((ret = avio_seek(s->pb, ASF_FILE_HEADER_SIZE, SEEK_SET) < 0))
357         return ret;
358
359     avio_wl32(s->pb, (uint32_t)s->streams[0]->nb_frames);
360     return 0;
361 }
362
363 AVOutputFormat ff_argo_asf_muxer = {
364     .name           = "argo_asf",
365     .long_name      = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
366     /*
367      * NB: Can't do this as it conflicts with the actual ASF format.
368      * .extensions  = "asf",
369      */
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
376 };
377 #endif