FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/argo_brp.c
Date: 2025-04-25 22:50:00
Exec Total Coverage
Lines: 3 179 1.7%
Functions: 1 4 25.0%
Branches: 1 120 0.8%

Line Branch Exec Source
1 /*
2 * Argonaut Games BRP Demuxer
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 "avformat.h"
24 #include "avio_internal.h"
25 #include "demux.h"
26 #include "internal.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/avassert.h"
29 #include "libavutil/internal.h"
30 #include "argo_asf.h"
31
32 #define BRP_TAG MKTAG('B', 'R', 'P', 'P')
33 #define BRP_FILE_HEADER_SIZE 12
34 #define BRP_BLOCK_HEADER_SIZE 12
35 #define BRP_STREAM_HEADER_SIZE 20
36 #define BRP_MAX_STREAMS 32 /* Soft cap, but even this is overkill. */
37 #define BRP_BASF_LOOKAHEAD 10 /* How many blocks to search for the first BASF one. */
38 #define BVID_HEADER_SIZE 16
39 #define MASK_HEADER_SIZE 12
40 #define BRP_MIN_BUFFER_SIZE FFMAX3(FFMAX3(BRP_FILE_HEADER_SIZE, \
41 BRP_BLOCK_HEADER_SIZE, \
42 BRP_STREAM_HEADER_SIZE), \
43 BVID_HEADER_SIZE, \
44 MASK_HEADER_SIZE)
45
46 #define BRP_CODEC_ID_BVID MKTAG('B', 'V', 'I', 'D')
47 #define BRP_CODEC_ID_BASF MKTAG('B', 'A', 'S', 'F')
48 #define BRP_CODEC_ID_MASK MKTAG('M', 'A', 'S', 'K')
49
50 typedef struct ArgoBRPFileHeader {
51 uint32_t magic;
52 uint32_t num_streams;
53 uint32_t byte_rate;
54 } ArgoBRPFileHeader;
55
56 typedef struct ArgoBRPBlockHeader {
57 int32_t stream_id;
58 uint32_t start_ms;
59 uint32_t size;
60 } ArgoBRPBlockHeader;
61
62 typedef struct ArgoBVIDHeader {
63 uint32_t num_frames;
64 uint32_t width;
65 uint32_t height;
66 uint32_t depth;
67 } ArgoBVIDHeader;
68
69 typedef struct ArgoMASKHeader {
70 uint32_t num_frames;
71 uint32_t width;
72 uint32_t height;
73 } ArgoMASKHeader;
74
75 typedef struct ArgoBRPStreamHeader {
76 uint32_t codec_id;
77 uint32_t id;
78 uint32_t duration_ms;
79 uint32_t byte_rate;
80 uint32_t extradata_size;
81 union
82 {
83 /* If codec_id == BRP_CODEC_ID_BVID */
84 ArgoBVIDHeader bvid;
85 /* If codec_id == BRP_CODEC_ID_BASF */
86 ArgoASFFileHeader basf;
87 /* If codec_id == BRP_CODEC_ID_MASK */
88 ArgoMASKHeader mask;
89 } extradata;
90 } ArgoBRPStreamHeader;
91
92 typedef struct ArgoBRPDemuxContext {
93 ArgoBRPFileHeader fhdr;
94 ArgoBRPStreamHeader streams[BRP_MAX_STREAMS];
95
96 struct {
97 int index;
98 ArgoASFChunkHeader ckhdr;
99 } basf;
100 } ArgoBRPDemuxContext;
101
102 7211 static int argo_brp_probe(const AVProbeData *p)
103 {
104
1/2
✓ Branch 0 taken 7211 times.
✗ Branch 1 not taken.
7211 if (AV_RL32(p->buf) != BRP_TAG)
105 7211 return 0;
106
107 return AVPROBE_SCORE_EXTENSION + 1;
108 }
109
110 static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
111 void *buf, size_t bufsz)
112 {
113 const char *name;
114 uint32_t size;
115 int64_t ret;
116
117 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
118 name = "BVID";
119 size = BVID_HEADER_SIZE;
120 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
121 name = "BASF";
122 size = ASF_FILE_HEADER_SIZE;
123 } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
124 name = "MASK";
125 size = MASK_HEADER_SIZE;
126 } else {
127 avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id);
128
129 if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0)
130 return ret;
131
132 return 1;
133 }
134
135 if (hdr->extradata_size != size) {
136 av_log(s, AV_LOG_ERROR, "Invalid %s extradata size %u, expected %u\n",
137 name, hdr->extradata_size, size);
138 return AVERROR_INVALIDDATA;
139 }
140
141 av_assert0(bufsz >= size);
142
143 ret = ffio_read_size(s->pb, buf, size);
144 if (ret < 0)
145 return ret;
146
147 return 0;
148 }
149
150 static int argo_brp_read_header(AVFormatContext *s)
151 {
152 int64_t ret;
153 AVIOContext *pb = s->pb;
154 ArgoBRPDemuxContext *brp = s->priv_data;
155 uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
156
157 ret = ffio_read_size(pb, buf, BRP_FILE_HEADER_SIZE);
158 if (ret < 0)
159 return ret;
160
161 brp->fhdr.magic = AV_RL32(buf + 0);
162 brp->fhdr.num_streams = AV_RL32(buf + 4);
163 brp->fhdr.byte_rate = AV_RL32(buf + 8);
164
165 if (brp->fhdr.magic != BRP_TAG)
166 return AVERROR_INVALIDDATA;
167
168 if (brp->fhdr.num_streams > BRP_MAX_STREAMS) {
169 avpriv_request_sample(s, ">%d streams", BRP_MAX_STREAMS);
170 return AVERROR_PATCHWELCOME;
171 }
172
173 /* Build the stream info. */
174 brp->basf.index = -1;
175 for (uint32_t i = 0; i < brp->fhdr.num_streams; i++) {
176 ArgoBRPStreamHeader *hdr = brp->streams + i;
177 AVStream *st;
178
179 if (!(st = avformat_new_stream(s, NULL)))
180 return AVERROR(ENOMEM);
181
182 ret = ffio_read_size(pb, buf, BRP_STREAM_HEADER_SIZE);
183 if (ret < 0)
184 return ret;
185
186 hdr->codec_id = AV_RL32(buf + 0);
187 hdr->id = AV_RL32(buf + 4);
188 hdr->duration_ms = AV_RL32(buf + 8);
189 hdr->byte_rate = AV_RL32(buf + 12);
190 hdr->extradata_size = AV_RL32(buf + 16);
191
192 /* This should always be the case. */
193 if (hdr->id != i)
194 return AVERROR_INVALIDDATA;
195
196 /* Timestamps are in milliseconds. */
197 avpriv_set_pts_info(st, 64, 1, 1000);
198 st->duration = hdr->duration_ms;
199 st->codecpar->bit_rate = hdr->byte_rate * 8;
200
201 if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) {
202 return ret;
203 } else if (ret > 0) {
204 st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
205 continue;
206 }
207
208 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
209 ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
210
211 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
212 st->codecpar->codec_id = AV_CODEC_ID_ARGO;
213
214 bvid->num_frames = AV_RL32(buf + 0);
215 bvid->width = AV_RL32(buf + 4);
216 bvid->height = AV_RL32(buf + 8);
217 bvid->depth = AV_RL32(buf + 12);
218
219 if (bvid->num_frames == 0)
220 return AVERROR_INVALIDDATA;
221
222 /* These are from 1990's games, sanity check this. */
223 if (bvid->width >= 65536 || bvid->height >= 65536 ||
224 bvid->depth > 24 || bvid->depth % 8 != 0) {
225 return AVERROR_INVALIDDATA;
226 }
227
228 st->codecpar->width = bvid->width;
229 st->codecpar->height = bvid->height;
230 st->nb_frames = bvid->num_frames;
231 st->codecpar->bits_per_coded_sample = bvid->depth;
232 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
233 /*
234 * It would make the demuxer significantly more complicated
235 * to support multiple BASF streams. I've never seen a file
236 * with more than one.
237 */
238 if (brp->basf.index >= 0) {
239 avpriv_request_sample(s, "Multiple BASF streams");
240 return AVERROR_PATCHWELCOME;
241 }
242
243 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
244 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO;
245 brp->basf.index = i;
246 ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf);
247
248 if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
249 return ret;
250
251 st->nb_frames = hdr->extradata.basf.num_chunks;
252 } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
253 ArgoMASKHeader *mask = &hdr->extradata.mask;
254
255 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
256
257 mask->num_frames = AV_RL32(buf + 0);
258 mask->width = AV_RL32(buf + 4);
259 mask->height = AV_RL32(buf + 8);
260
261 st->nb_frames = mask->num_frames;
262 } else {
263 av_assert0(0); /* Caught above, should never happen. */
264 }
265 }
266
267 /* Try to find the first BASF chunk. */
268 if (brp->basf.index >= 0) {
269 AVStream *st = s->streams[brp->basf.index];
270 ArgoBRPStreamHeader *hdr = brp->streams + brp->basf.index;
271 ArgoBRPBlockHeader blk;
272 int64_t offset;
273 int i;
274
275 av_assert0(st->codecpar->codec_id == AV_CODEC_ID_ADPCM_ARGO);
276 av_assert0(brp->streams[brp->basf.index].extradata_size == ASF_FILE_HEADER_SIZE);
277
278 if ((ret = avio_tell(s->pb)) < 0)
279 return ret;
280
281 offset = ret;
282
283 av_log(s, AV_LOG_TRACE, "Searching %d blocks for BASF...", BRP_BASF_LOOKAHEAD);
284
285 for (i = 0; i < BRP_BASF_LOOKAHEAD; i++) {
286 ret = ffio_read_size(pb, buf, BRP_BLOCK_HEADER_SIZE);
287 if (ret < 0)
288 return ret;
289
290 blk.stream_id = AV_RL32(buf + 0);
291 blk.start_ms = AV_RL32(buf + 4);
292 blk.size = AV_RL32(buf + 8);
293
294 if (blk.stream_id == brp->basf.index || blk.stream_id == -1)
295 break;
296
297 if ((ret = avio_skip(pb, blk.size)) < 0)
298 return ret;
299 }
300
301 if (i == BRP_BASF_LOOKAHEAD || blk.stream_id == -1) {
302 /* Don't error here, as there may still be a valid video stream. */
303 av_log(s, AV_LOG_TRACE, "not found\n");
304 goto done;
305 }
306
307 av_log(s, AV_LOG_TRACE, "found at index %d\n", i);
308
309 if (blk.size < ASF_CHUNK_HEADER_SIZE)
310 return AVERROR_INVALIDDATA;
311
312 ret = ffio_read_size(pb, buf, BRP_BLOCK_HEADER_SIZE);
313 if (ret < 0)
314 return ret;
315
316 ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
317
318 /*
319 * Special Case Hack. It seems that in files where the BASF block isn't first,
320 * v1.1 streams are allowed to be non-22050...
321 * Bump the version to 1.2 so ff_argo_asf_fill_stream() doesn't "correct" it.
322 *
323 * Found in Alien Odyssey games files in:
324 * ./GRAPHICS/COMMBUNK/{{COMADD1,COMM2_{1,2,3E},COMM3_{2,3,4,5,6}},FADE{1,2}}.BRP
325 *
326 * Either this format really inconsistent, or FX Fighter and Croc just ignored the
327 * sample rate field...
328 */
329 if (i != 0 && hdr->extradata.basf.version_major == 1 && hdr->extradata.basf.version_minor == 1)
330 hdr->extradata.basf.version_minor = 2;
331
332 if ((ret = ff_argo_asf_fill_stream(s, st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0)
333 return ret;
334
335 /* Convert ms to samples. */
336 st->start_time = av_rescale_rnd(blk.start_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP);
337 st->duration = av_rescale_rnd(hdr->duration_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP);
338
339 done:
340 if ((ret = avio_seek(s->pb, offset, SEEK_SET)) < 0)
341 return ret;
342 }
343 return 0;
344 }
345
346 static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
347 {
348 ArgoBRPDemuxContext *brp = s->priv_data;
349 ArgoBRPBlockHeader blk;
350 const ArgoBRPStreamHeader *shdr;
351 AVStream *st;
352 uint8_t buf[BRP_MIN_BUFFER_SIZE];
353 ArgoASFChunkHeader ckhdr;
354 int ret;
355
356 ret = ffio_read_size(s->pb, buf, BRP_BLOCK_HEADER_SIZE);
357 if (ret < 0)
358 return ret;
359
360 blk.stream_id = AV_RL32(buf + 0);
361 blk.start_ms = AV_RL32(buf + 4);
362 blk.size = AV_RL32(buf + 8);
363
364 if (blk.stream_id == -1)
365 return AVERROR_EOF;
366
367 if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams)
368 return AVERROR_INVALIDDATA;
369
370 st = s->streams[blk.stream_id];
371 shdr = brp->streams + blk.stream_id;
372
373 if (blk.stream_id == brp->basf.index) {
374 if (blk.size < ASF_CHUNK_HEADER_SIZE)
375 return AVERROR_INVALIDDATA;
376
377 ret = ffio_read_size(s->pb, buf, ASF_CHUNK_HEADER_SIZE);
378 if (ret < 0)
379 return ret;
380
381 ff_argo_asf_parse_chunk_header(&ckhdr, buf);
382
383 /* Ensure the chunk attributes are the same. */
384 if (ckhdr.sample_rate != brp->basf.ckhdr.sample_rate ||
385 ckhdr.flags != brp->basf.ckhdr.flags ||
386 ckhdr.unk1 != brp->basf.ckhdr.unk1 ||
387 ckhdr.unk2 != brp->basf.ckhdr.unk2)
388 return AVERROR_INVALIDDATA;
389
390 blk.size -= ASF_CHUNK_HEADER_SIZE;
391 }
392
393 if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0)
394 return ret;
395 else if (ret != blk.size)
396 return AVERROR_INVALIDDATA;
397
398 if (blk.stream_id == brp->basf.index) {
399 pkt->duration = ckhdr.num_samples * ckhdr.num_blocks;
400 pkt->pts = av_rescale_rnd(blk.start_ms, ckhdr.sample_rate, 1000, AV_ROUND_UP);
401 } else if (shdr->codec_id == BRP_CODEC_ID_BVID) {
402 pkt->duration = av_rescale_rnd(1, st->duration, shdr->extradata.bvid.num_frames, AV_ROUND_UP);
403 pkt->pts = blk.start_ms;
404 } else {
405 pkt->pts = blk.start_ms;
406 }
407
408 pkt->stream_index = blk.stream_id;
409 return 0;
410 }
411
412 const FFInputFormat ff_argo_brp_demuxer = {
413 .p.name = "argo_brp",
414 .p.long_name = NULL_IF_CONFIG_SMALL("Argonaut Games BRP"),
415 .priv_data_size = sizeof(ArgoBRPDemuxContext),
416 .read_probe = argo_brp_probe,
417 .read_header = argo_brp_read_header,
418 .read_packet = argo_brp_read_packet,
419 };
420