FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/argo_asf.c
Date: 2026-04-24 19:58:39
Exec Total Coverage
Lines: 147 195 75.4%
Functions: 14 15 93.3%
Branches: 45 92 48.9%

Line Branch Exec Source
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
23 #include "config_components.h"
24
25 #include "libavutil/avstring.h"
26 #include "avformat.h"
27 #include "avio_internal.h"
28 #include "demux.h"
29 #include "internal.h"
30 #include "mux.h"
31 #include "libavutil/channel_layout.h"
32 #include "libavutil/intreadwrite.h"
33 #include "libavutil/avassert.h"
34 #include "libavutil/opt.h"
35 #include "argo_asf.h"
36
37 /* Maximum number of blocks to read at once. */
38 #define ASF_NB_BLOCKS 32
39
40 typedef struct ArgoASFDemuxContext {
41 ArgoASFFileHeader fhdr;
42 ArgoASFChunkHeader ckhdr;
43 uint32_t blocks_read;
44 } ArgoASFDemuxContext;
45
46 typedef struct ArgoASFMuxContext {
47 const AVClass *class;
48 int version_major;
49 int version_minor;
50 const char *name;
51 int64_t nb_blocks;
52 } ArgoASFMuxContext;
53
54 7483 void ff_argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf)
55 {
56 7483 hdr->magic = AV_RL32(buf + 0);
57 7483 hdr->version_major = AV_RL16(buf + 4);
58 7483 hdr->version_minor = AV_RL16(buf + 6);
59 7483 hdr->num_chunks = AV_RL32(buf + 8);
60 7483 hdr->chunk_offset = AV_RL32(buf + 12);
61 7483 memcpy(hdr->name, buf + 16, ASF_NAME_SIZE);
62 7483 hdr->name[ASF_NAME_SIZE] = '\0';
63 7483 }
64
65 3 int ff_argo_asf_validate_file_header(AVFormatContext *s, const ArgoASFFileHeader *hdr)
66 {
67
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (hdr->magic != ASF_TAG || hdr->num_chunks == 0)
68 return AVERROR_INVALIDDATA;
69
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (hdr->chunk_offset < ASF_FILE_HEADER_SIZE)
71 return AVERROR_INVALIDDATA;
72
73 3 return 0;
74 }
75
76 3 void ff_argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf)
77 {
78 3 hdr->num_blocks = AV_RL32(buf + 0);
79 3 hdr->num_samples = AV_RL32(buf + 4);
80 3 hdr->unk1 = AV_RL32(buf + 8);
81 3 hdr->sample_rate = AV_RL16(buf + 12);
82 3 hdr->unk2 = AV_RL16(buf + 14);
83 3 hdr->flags = AV_RL32(buf + 16);
84 3 }
85
86 3 int ff_argo_asf_fill_stream(AVFormatContext *s, AVStream *st, const ArgoASFFileHeader *fhdr,
87 const ArgoASFChunkHeader *ckhdr)
88 {
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (ckhdr->num_samples != ASF_SAMPLE_COUNT) {
90 av_log(s, AV_LOG_ERROR, "Invalid sample count. Got %u, expected %d\n",
91 ckhdr->num_samples, ASF_SAMPLE_COUNT);
92 return AVERROR_INVALIDDATA;
93 }
94
95
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if ((ckhdr->flags & ASF_CF_ALWAYS1) != ASF_CF_ALWAYS1 || (ckhdr->flags & ASF_CF_ALWAYS0) != 0) {
96 avpriv_request_sample(s, "Nonstandard flags (0x%08X)", ckhdr->flags);
97 return AVERROR_PATCHWELCOME;
98 }
99
100 3 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
101 3 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO;
102 3 st->codecpar->format = AV_SAMPLE_FMT_S16P;
103
104
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (ckhdr->flags & ASF_CF_STEREO) {
105 2 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
106 } else {
107 1 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
108 }
109
110 /* v1.1 files (FX Fighter) are all marked as 44100, but are actually 22050. */
111
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
3 if (fhdr->version_major == 1 && fhdr->version_minor == 1)
112 1 st->codecpar->sample_rate = 22050;
113 else
114 2 st->codecpar->sample_rate = ckhdr->sample_rate;
115
116 3 st->codecpar->bits_per_coded_sample = 4;
117
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!(ckhdr->flags & ASF_CF_BITS_PER_SAMPLE)) {
119 /* The header allows for these, but I've never seen any files with them. */
120 avpriv_request_sample(s, "Non 16-bit samples");
121 return AVERROR_PATCHWELCOME;
122 }
123
124 /*
125 * (nchannel control bytes) + ((bytes_per_channel) * nchannel)
126 * For mono, this is 17. For stereo, this is 34.
127 */
128 3 st->codecpar->block_align = st->codecpar->ch_layout.nb_channels +
129 3 (ckhdr->num_samples / 2) *
130 st->codecpar->ch_layout.nb_channels;
131
132 3 st->codecpar->bit_rate = st->codecpar->ch_layout.nb_channels *
133 3 st->codecpar->sample_rate *
134 3 st->codecpar->bits_per_coded_sample;
135
136 3 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
137 3 st->start_time = 0;
138
139
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (fhdr->num_chunks == 1) {
140 3 st->duration = ckhdr->num_blocks * ckhdr->num_samples;
141 3 st->nb_frames = ckhdr->num_blocks;
142 }
143
144 3 return 0;
145 }
146
147 #if CONFIG_ARGO_ASF_DEMUXER
148 /*
149 * Known versions:
150 * 1.1: https://samples.ffmpeg.org/game-formats/brender/part2.zip
151 * FX Fighter
152 * 1.2: Croc! Legend of the Gobbos
153 * 2.1: Croc 2
154 * The Emperor's New Groove
155 * Disney's Aladdin in Nasira's Revenge
156 */
157 3 static int argo_asf_is_known_version(const ArgoASFFileHeader *hdr)
158 {
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 return (hdr->version_major == 1 && hdr->version_minor == 1) ||
160
3/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6 (hdr->version_major == 1 && hdr->version_minor == 2) ||
161
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 (hdr->version_major == 2 && hdr->version_minor == 1);
162 }
163
164 7480 static int argo_asf_probe(const AVProbeData *p)
165 {
166 ArgoASFFileHeader hdr;
167
168 av_assert0(AVPROBE_PADDING_SIZE >= ASF_FILE_HEADER_SIZE);
169
170 7480 ff_argo_asf_parse_file_header(&hdr, p->buf);
171
172
2/2
✓ Branch 0 taken 7477 times.
✓ Branch 1 taken 3 times.
7480 if (hdr.magic != ASF_TAG)
173 7477 return 0;
174
175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!argo_asf_is_known_version(&hdr))
176 return AVPROBE_SCORE_EXTENSION / 2;
177
178 3 return AVPROBE_SCORE_EXTENSION + 1;
179 }
180
181 3 static int argo_asf_read_header(AVFormatContext *s)
182 {
183 int64_t ret;
184 3 AVIOContext *pb = s->pb;
185 AVStream *st;
186 3 ArgoASFDemuxContext *asf = s->priv_data;
187 uint8_t buf[ASF_MIN_BUFFER_SIZE];
188
189
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(st = avformat_new_stream(s, NULL)))
190 return AVERROR(ENOMEM);
191
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ffio_read_size(pb, buf, ASF_FILE_HEADER_SIZE)) < 0)
193 return ret;
194
195 3 ff_argo_asf_parse_file_header(&asf->fhdr, buf);
196
197
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ff_argo_asf_validate_file_header(s, &asf->fhdr)) < 0)
198 return ret;
199
200 /* This should only be 1 in ASF files. >1 is fine if in BRP. */
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (asf->fhdr.num_chunks != 1)
202 return AVERROR_INVALIDDATA;
203
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = avio_skip(pb, asf->fhdr.chunk_offset - ASF_FILE_HEADER_SIZE)) < 0)
205 return ret;
206
207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ffio_read_size(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
208 return ret;
209
210 3 ff_argo_asf_parse_chunk_header(&asf->ckhdr, buf);
211
212 3 av_dict_set(&s->metadata, "title", asf->fhdr.name, 0);
213
214 3 return ff_argo_asf_fill_stream(s, st, &asf->fhdr, &asf->ckhdr);
215 }
216
217 949 static int argo_asf_read_packet(AVFormatContext *s, AVPacket *pkt)
218 {
219 949 ArgoASFDemuxContext *asf = s->priv_data;
220
221 949 AVStream *st = s->streams[0];
222 949 AVIOContext *pb = s->pb;
223 int ret;
224
225
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 946 times.
949 if (asf->blocks_read >= asf->ckhdr.num_blocks)
226 3 return AVERROR_EOF;
227
228 946 ret = av_get_packet(pb, pkt, st->codecpar->block_align *
229 946 FFMIN(ASF_NB_BLOCKS, asf->ckhdr.num_blocks - asf->blocks_read));
230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 946 times.
946 if (ret < 0)
231 return ret;
232
233 /* Something real screwy is going on. */
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 946 times.
946 if (ret % st->codecpar->block_align != 0)
235 return AVERROR_INVALIDDATA;
236
237
238 946 pkt->stream_index = st->index;
239 946 pkt->duration = asf->ckhdr.num_samples * (ret / st->codecpar->block_align);
240 946 pkt->pts = asf->blocks_read * asf->ckhdr.num_samples;
241 946 asf->blocks_read += (ret / st->codecpar->block_align);
242
243 946 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
244 946 return 0;
245 }
246
247 static int argo_asf_seek(AVFormatContext *s, int stream_index,
248 int64_t pts, int flags)
249 {
250 ArgoASFDemuxContext *asf = s->priv_data;
251 AVStream *st = s->streams[stream_index];
252 int64_t offset;
253 uint32_t block = pts / asf->ckhdr.num_samples;
254
255 if (block >= asf->ckhdr.num_blocks)
256 return -1;
257
258 offset = asf->fhdr.chunk_offset + ASF_CHUNK_HEADER_SIZE +
259 block * (int64_t)st->codecpar->block_align;
260
261 if ((offset = avio_seek(s->pb, offset, SEEK_SET)) < 0)
262 return offset;
263
264 asf->blocks_read = block;
265 return 0;
266 }
267
268 /*
269 * Not actually sure what ASF stands for.
270 * - Argonaut Sound File?
271 * - Audio Stream File?
272 */
273 const FFInputFormat ff_argo_asf_demuxer = {
274 .p.name = "argo_asf",
275 .p.long_name = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
276 .priv_data_size = sizeof(ArgoASFDemuxContext),
277 .read_probe = argo_asf_probe,
278 .read_header = argo_asf_read_header,
279 .read_packet = argo_asf_read_packet,
280 .read_seek = argo_asf_seek,
281 };
282 #endif
283
284 #if CONFIG_ARGO_ASF_MUXER
285 1 static int argo_asf_write_init(AVFormatContext *s)
286 {
287 1 ArgoASFMuxContext *ctx = s->priv_data;
288 1 const AVCodecParameters *par = s->streams[0]->codecpar;
289
290
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 if (ctx->version_major == 1 && ctx->version_minor == 1 && par->sample_rate != 22050) {
291 av_log(s, AV_LOG_ERROR, "ASF v1.1 files only support a sample rate of 22050\n");
292 return AVERROR(EINVAL);
293 }
294
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->ch_layout.nb_channels > 2) {
296 av_log(s, AV_LOG_ERROR, "ASF files only support up to 2 channels\n");
297 return AVERROR(EINVAL);
298 }
299
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->block_align != 17 * par->ch_layout.nb_channels)
301 return AVERROR(EINVAL);
302
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->sample_rate > UINT16_MAX) {
304 av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
305 return AVERROR(EINVAL);
306 }
307
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
309 av_log(s, AV_LOG_ERROR, "Stream not seekable, unable to write output file\n");
310 return AVERROR(EINVAL);
311 }
312
313 1 return 0;
314 }
315
316 1 static void argo_asf_write_file_header(const ArgoASFFileHeader *fhdr, AVIOContext *pb)
317 {
318 1 avio_wl32( pb, fhdr->magic);
319 1 avio_wl16( pb, fhdr->version_major);
320 1 avio_wl16( pb, fhdr->version_minor);
321 1 avio_wl32( pb, fhdr->num_chunks);
322 1 avio_wl32( pb, fhdr->chunk_offset);
323 1 avio_write(pb, fhdr->name, ASF_NAME_SIZE);
324 1 }
325
326 1 static void argo_asf_write_chunk_header(const ArgoASFChunkHeader *ckhdr, AVIOContext *pb)
327 {
328 1 avio_wl32(pb, ckhdr->num_blocks);
329 1 avio_wl32(pb, ckhdr->num_samples);
330 1 avio_wl32(pb, ckhdr->unk1);
331 1 avio_wl16(pb, ckhdr->sample_rate);
332 1 avio_wl16(pb, ckhdr->unk2);
333 1 avio_wl32(pb, ckhdr->flags);
334 1 }
335
336 1 static int argo_asf_write_header(AVFormatContext *s)
337 {
338 1 const AVCodecParameters *par = s->streams[0]->codecpar;
339 1 ArgoASFMuxContext *ctx = s->priv_data;
340 ArgoASFChunkHeader chdr;
341 1 ArgoASFFileHeader fhdr = {
342 .magic = ASF_TAG,
343 1 .version_major = (uint16_t)ctx->version_major,
344 1 .version_minor = (uint16_t)ctx->version_minor,
345 .num_chunks = 1,
346 .chunk_offset = ASF_FILE_HEADER_SIZE
347 };
348 AVDictionaryEntry *t;
349 const char *name, *end;
350 size_t len;
351
352 /*
353 * If the user specified a name, use it as is. Otherwise,
354 * try to use metadata (if present), then fall back to the
355 * filename (minus extension).
356 */
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ctx->name) {
358 name = ctx->name;
359 len = strlen(ctx->name);
360
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 } else if ((t = av_dict_get(s->metadata, "title", NULL, 0))) {
361 name = t->value;
362 len = strlen(t->value);
363
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 } else if (!(end = strrchr((name = av_basename(s->url)), '.'))) {
364 len = strlen(name);
365 } else {
366 1 len = end - name;
367 }
368 1 memcpy(fhdr.name, name, FFMIN(len, ASF_NAME_SIZE));
369
370 1 chdr.num_blocks = 0;
371 1 chdr.num_samples = ASF_SAMPLE_COUNT;
372 1 chdr.unk1 = 0;
373
374
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (ctx->version_major == 1 && ctx->version_minor == 1)
375 chdr.sample_rate = 44100;
376 else
377 1 chdr.sample_rate = par->sample_rate;
378
379 1 chdr.unk2 = ~0;
380 1 chdr.flags = ASF_CF_BITS_PER_SAMPLE | ASF_CF_ALWAYS1;
381
382
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (par->ch_layout.nb_channels == 2)
383 1 chdr.flags |= ASF_CF_STEREO;
384
385 1 argo_asf_write_file_header(&fhdr, s->pb);
386 1 argo_asf_write_chunk_header(&chdr, s->pb);
387 1 return 0;
388 }
389
390 8269 static int argo_asf_write_packet(AVFormatContext *s, AVPacket *pkt)
391 {
392 8269 ArgoASFMuxContext *ctx = s->priv_data;
393 8269 AVCodecParameters *par = s->streams[0]->codecpar;
394 8269 int nb_blocks = pkt->size / par->block_align;
395
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8269 times.
8269 if (pkt->size % par->block_align != 0)
397 return AVERROR_INVALIDDATA;
398
399
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8269 times.
8269 if (ctx->nb_blocks + nb_blocks > UINT32_MAX)
400 return AVERROR_INVALIDDATA;
401
402 8269 avio_write(s->pb, pkt->data, pkt->size);
403
404 8269 ctx->nb_blocks += nb_blocks;
405 8269 return 0;
406 }
407
408 1 static int argo_asf_write_trailer(AVFormatContext *s)
409 {
410 1 ArgoASFMuxContext *ctx = s->priv_data;
411 int64_t ret;
412
413
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = avio_seek(s->pb, ASF_FILE_HEADER_SIZE, SEEK_SET)) < 0)
414 return ret;
415
416 1 avio_wl32(s->pb, (uint32_t)ctx->nb_blocks);
417 1 return 0;
418 }
419
420 static const AVOption argo_asf_options[] = {
421 {
422 .name = "version_major",
423 .help = "override file major version",
424 .offset = offsetof(ArgoASFMuxContext, version_major),
425 .type = AV_OPT_TYPE_INT,
426 .default_val = {.i64 = 2},
427 .min = 0,
428 .max = UINT16_MAX,
429 .flags = AV_OPT_FLAG_ENCODING_PARAM
430 },
431 {
432 .name = "version_minor",
433 .help = "override file minor version",
434 .offset = offsetof(ArgoASFMuxContext, version_minor),
435 .type = AV_OPT_TYPE_INT,
436 .default_val = {.i64 = 1},
437 .min = 0,
438 .max = UINT16_MAX,
439 .flags = AV_OPT_FLAG_ENCODING_PARAM
440 },
441 {
442 .name = "name",
443 .help = "embedded file name (max 8 characters)",
444 .offset = offsetof(ArgoASFMuxContext, name),
445 .type = AV_OPT_TYPE_STRING,
446 .default_val = {.str = NULL},
447 .flags = AV_OPT_FLAG_ENCODING_PARAM
448 },
449 { NULL }
450 };
451
452 static const AVClass argo_asf_muxer_class = {
453 .class_name = "argo_asf_muxer",
454 .item_name = av_default_item_name,
455 .option = argo_asf_options,
456 .version = LIBAVUTIL_VERSION_INT
457 };
458
459 const FFOutputFormat ff_argo_asf_muxer = {
460 .p.name = "argo_asf",
461 .p.long_name = NULL_IF_CONFIG_SMALL("Argonaut Games ASF"),
462 /*
463 * NB: Can't do this as it conflicts with the actual ASF format.
464 * .p.extensions = "asf",
465 */
466 .p.audio_codec = AV_CODEC_ID_ADPCM_ARGO,
467 .p.video_codec = AV_CODEC_ID_NONE,
468 .p.subtitle_codec = AV_CODEC_ID_NONE,
469 .p.priv_class = &argo_asf_muxer_class,
470 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
471 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
472 .init = argo_asf_write_init,
473 .write_header = argo_asf_write_header,
474 .write_packet = argo_asf_write_packet,
475 .write_trailer = argo_asf_write_trailer,
476 .priv_data_size = sizeof(ArgoASFMuxContext)
477 };
478 #endif
479