2 * Argonaut Games BRP 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
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/internal.h"
30 #define BRP_TAG MKTAG('B', 'R', 'P', 'P')
31 #define BRP_FILE_HEADER_SIZE 12
32 #define BRP_BLOCK_HEADER_SIZE 12
33 #define BRP_STREAM_HEADER_SIZE 20
34 #define BRP_MAX_STREAMS 32 /* Soft cap, but even this is overkill. */
35 #define BRP_BASF_LOOKAHEAD 10 /* How many blocks to search for the first BASF one. */
36 #define BVID_HEADER_SIZE 16
37 #define MASK_HEADER_SIZE 12
38 #define BRP_MIN_BUFFER_SIZE FFMAX3(FFMAX3(BRP_FILE_HEADER_SIZE, \
39 BRP_BLOCK_HEADER_SIZE, \
40 BRP_STREAM_HEADER_SIZE), \
44 #define BRP_CODEC_ID_BVID MKTAG('B', 'V', 'I', 'D')
45 #define BRP_CODEC_ID_BASF MKTAG('B', 'A', 'S', 'F')
46 #define BRP_CODEC_ID_MASK MKTAG('M', 'A', 'S', 'K')
48 typedef struct ArgoBRPFileHeader {
54 typedef struct ArgoBRPBlockHeader {
60 typedef struct ArgoBVIDHeader {
67 typedef struct ArgoMASKHeader {
73 typedef struct ArgoBRPStreamHeader {
78 uint32_t extradata_size;
81 /* If codec_id == BRP_CODEC_ID_BVID */
83 /* If codec_id == BRP_CODEC_ID_BASF */
84 ArgoASFFileHeader basf;
85 /* If codec_id == BRP_CODEC_ID_MASK */
88 } ArgoBRPStreamHeader;
90 typedef struct ArgoBRPDemuxContext {
91 ArgoBRPFileHeader fhdr;
92 ArgoBRPStreamHeader streams[BRP_MAX_STREAMS];
96 ArgoASFChunkHeader ckhdr;
98 } ArgoBRPDemuxContext;
100 static int argo_brp_probe(const AVProbeData *p)
102 if (AV_RL32(p->buf) != BRP_TAG)
105 return AVPROBE_SCORE_EXTENSION + 1;
108 static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
109 void *buf, size_t bufsz)
115 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
117 size = BVID_HEADER_SIZE;
118 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
120 size = ASF_FILE_HEADER_SIZE;
121 } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
123 size = MASK_HEADER_SIZE;
125 avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id);
127 if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0)
133 if (hdr->extradata_size != size) {
134 av_log(s, AV_LOG_ERROR, "Invalid %s extradata size %u, expected %u\n",
135 name, hdr->extradata_size, size);
136 return AVERROR_INVALIDDATA;
139 av_assert0(bufsz >= size);
141 if ((ret = avio_read(s->pb, buf, size)) < 0)
150 static int argo_brp_read_header(AVFormatContext *s)
153 AVIOContext *pb = s->pb;
154 ArgoBRPDemuxContext *brp = s->priv_data;
155 uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
157 if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0)
159 else if (ret != BRP_FILE_HEADER_SIZE)
162 brp->fhdr.magic = AV_RL32(buf + 0);
163 brp->fhdr.num_streams = AV_RL32(buf + 4);
164 brp->fhdr.byte_rate = AV_RL32(buf + 8);
166 if (brp->fhdr.magic != BRP_TAG)
167 return AVERROR_INVALIDDATA;
169 if (brp->fhdr.num_streams > BRP_MAX_STREAMS) {
170 avpriv_request_sample(s, ">%d streams", BRP_MAX_STREAMS);
171 return AVERROR_PATCHWELCOME;
174 /* Build the stream info. */
175 brp->basf.index = -1;
176 for (uint32_t i = 0; i < brp->fhdr.num_streams; i++) {
177 ArgoBRPStreamHeader *hdr = brp->streams + i;
180 if (!(st = avformat_new_stream(s, NULL)))
181 return AVERROR(ENOMEM);
183 if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0)
185 else if (ret != BRP_STREAM_HEADER_SIZE)
188 hdr->codec_id = AV_RL32(buf + 0);
189 hdr->id = AV_RL32(buf + 4);
190 hdr->duration_ms = AV_RL32(buf + 8);
191 hdr->byte_rate = AV_RL32(buf + 12);
192 hdr->extradata_size = AV_RL32(buf + 16);
194 /* This should always be the case. */
196 return AVERROR_INVALIDDATA;
198 /* Timestamps are in milliseconds. */
199 avpriv_set_pts_info(st, 64, 1, 1000);
200 st->duration = hdr->duration_ms;
201 st->codecpar->bit_rate = hdr->byte_rate * 8;
203 if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) {
205 } else if (ret > 0) {
206 st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
210 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
211 ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
213 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
214 st->codecpar->codec_id = AV_CODEC_ID_ARGO;
216 bvid->num_frames = AV_RL32(buf + 0);
217 bvid->width = AV_RL32(buf + 4);
218 bvid->height = AV_RL32(buf + 8);
219 bvid->depth = AV_RL32(buf + 12);
221 /* These are from 1990's games, sanity check this. */
222 if (bvid->width >= 65536 || bvid->height >= 65536 ||
223 bvid->depth > 24 || bvid->depth % 8 != 0) {
224 return AVERROR_INVALIDDATA;
227 st->codecpar->width = bvid->width;
228 st->codecpar->height = bvid->height;
229 st->nb_frames = bvid->num_frames;
230 st->codecpar->bits_per_raw_sample = bvid->depth;
231 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
233 * It would make the demuxer significantly more complicated
234 * to support multiple BASF streams. I've never seen a file
235 * with more than one.
237 if (brp->basf.index >= 0) {
238 avpriv_request_sample(s, "Multiple BASF streams");
239 return AVERROR_PATCHWELCOME;
242 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
243 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO;
245 ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf);
247 if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
250 st->nb_frames = hdr->extradata.basf.num_chunks;
251 } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
252 ArgoMASKHeader *mask = &hdr->extradata.mask;
254 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
256 mask->num_frames = AV_RL32(buf + 0);
257 mask->width = AV_RL32(buf + 4);
258 mask->height = AV_RL32(buf + 8);
260 st->nb_frames = mask->num_frames;
262 av_assert0(0); /* Caught above, should never happen. */
266 /* Try to find the first BASF chunk. */
267 if (brp->basf.index >= 0) {
268 AVStream *st = s->streams[brp->basf.index];
269 ArgoBRPStreamHeader *hdr = brp->streams + brp->basf.index;
270 ArgoBRPBlockHeader blk;
274 av_assert0(st->codecpar->codec_id == AV_CODEC_ID_ADPCM_ARGO);
275 av_assert0(brp->streams[brp->basf.index].extradata_size == ASF_FILE_HEADER_SIZE);
277 if ((ret = avio_tell(s->pb)) < 0)
282 av_log(s, AV_LOG_TRACE, "Searching %d blocks for BASF...", BRP_BASF_LOOKAHEAD);
284 for (i = 0; i < BRP_BASF_LOOKAHEAD; i++) {
285 if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
287 else if (ret != BRP_BLOCK_HEADER_SIZE)
290 blk.stream_id = AV_RL32(buf + 0);
291 blk.start_ms = AV_RL32(buf + 4);
292 blk.size = AV_RL32(buf + 8);
294 if (blk.stream_id == brp->basf.index || blk.stream_id == -1)
297 if ((ret = avio_skip(pb, blk.size)) < 0)
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");
307 av_log(s, AV_LOG_TRACE, "found at index %d\n", i);
309 if (blk.size < ASF_CHUNK_HEADER_SIZE)
310 return AVERROR_INVALIDDATA;
312 if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
314 else if (ret != ASF_CHUNK_HEADER_SIZE)
317 ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
320 * Special Case Hack. It seems that in files where the BASF block isn't first,
321 * v1.1 streams are allowed to be non-22050...
322 * Bump the version to 1.2 so ff_argo_asf_fill_stream() doesn't "correct" it.
324 * Found in Alien Odyssey games files in:
325 * ./GRAPHICS/COMMBUNK/{{COMADD1,COMM2_{1,2,3E},COMM3_{2,3,4,5,6}},FADE{1,2}}.BRP
327 * Either this format really inconsistent, or FX Fighter and Croc just ignored the
328 * sample rate field...
330 if (i != 0 && hdr->extradata.basf.version_major == 1 && hdr->extradata.basf.version_minor == 1)
331 hdr->extradata.basf.version_minor = 2;
333 if ((ret = ff_argo_asf_fill_stream(s, st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0)
336 /* Convert ms to samples. */
337 st->start_time = av_rescale_rnd(blk.start_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP);
338 st->duration = av_rescale_rnd(hdr->duration_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP);
341 if ((ret = avio_seek(s->pb, offset, SEEK_SET)) < 0)
347 static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
349 ArgoBRPDemuxContext *brp = s->priv_data;
350 ArgoBRPBlockHeader blk;
351 const ArgoBRPStreamHeader *shdr;
353 uint8_t buf[BRP_MIN_BUFFER_SIZE];
354 ArgoASFChunkHeader ckhdr;
357 if ((ret = avio_read(s->pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
359 else if (ret != BRP_BLOCK_HEADER_SIZE)
362 blk.stream_id = AV_RL32(buf + 0);
363 blk.start_ms = AV_RL32(buf + 4);
364 blk.size = AV_RL32(buf + 8);
366 if (blk.stream_id == -1)
369 if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams)
370 return AVERROR_INVALIDDATA;
372 st = s->streams[blk.stream_id];
373 shdr = brp->streams + blk.stream_id;
375 if (blk.stream_id == brp->basf.index) {
376 if (blk.size < ASF_CHUNK_HEADER_SIZE)
377 return AVERROR_INVALIDDATA;
379 if ((ret = avio_read(s->pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
382 ff_argo_asf_parse_chunk_header(&ckhdr, buf);
384 /* Ensure the chunk attributes are the same. */
385 if (ckhdr.sample_rate != brp->basf.ckhdr.sample_rate ||
386 ckhdr.flags != brp->basf.ckhdr.flags ||
387 ckhdr.unk1 != brp->basf.ckhdr.unk1 ||
388 ckhdr.unk2 != brp->basf.ckhdr.unk2)
389 return AVERROR_INVALIDDATA;
391 blk.size -= ASF_CHUNK_HEADER_SIZE;
393 if (blk.size % st->codecpar->block_align != 0)
394 return AVERROR_INVALIDDATA;
397 if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0)
399 else if (ret != blk.size)
400 return AVERROR_INVALIDDATA;
402 if (blk.stream_id == brp->basf.index) {
403 pkt->duration = ckhdr.num_samples * ckhdr.num_blocks;
404 pkt->pts = av_rescale_rnd(blk.start_ms, ckhdr.sample_rate, 1000, AV_ROUND_UP);
405 } else if (shdr->codec_id == BRP_CODEC_ID_BVID) {
406 pkt->duration = av_rescale_rnd(1, st->duration, shdr->extradata.bvid.num_frames, AV_ROUND_UP);
407 pkt->pts = blk.start_ms;
409 pkt->pts = blk.start_ms;
412 pkt->stream_index = blk.stream_id;
416 AVInputFormat ff_argo_brp_demuxer = {
418 .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games BRP"),
419 .priv_data_size = sizeof(ArgoBRPDemuxContext),
420 .read_probe = argo_brp_probe,
421 .read_header = argo_brp_read_header,
422 .read_packet = argo_brp_read_packet,