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 BVID_HEADER_SIZE 16
36 #define MASK_HEADER_SIZE 12
37 #define BRP_MIN_BUFFER_SIZE FFMAX3(FFMAX3(BRP_FILE_HEADER_SIZE, \
38 BRP_BLOCK_HEADER_SIZE, \
39 BRP_STREAM_HEADER_SIZE), \
43 #define BRP_CODEC_ID_BVID MKTAG('B', 'V', 'I', 'D')
44 #define BRP_CODEC_ID_BASF MKTAG('B', 'A', 'S', 'F')
45 #define BRP_CODEC_ID_MASK MKTAG('M', 'A', 'S', 'K')
47 typedef struct ArgoBRPFileHeader {
53 typedef struct ArgoBRPBlockHeader {
59 typedef struct ArgoBVIDHeader {
66 typedef struct ArgoMASKHeader {
72 typedef struct ArgoBRPStreamHeader {
77 uint32_t extradata_size;
80 /* If codec_id == BRP_CODEC_ID_BVID */
82 /* If codec_id == BRP_CODEC_ID_BASF */
83 ArgoASFFileHeader basf;
84 /* If codec_id == BRP_CODEC_ID_MASK */
87 } ArgoBRPStreamHeader;
89 typedef struct ArgoBRPDemuxContext {
90 ArgoBRPFileHeader fhdr;
91 ArgoBRPStreamHeader streams[BRP_MAX_STREAMS];
95 ArgoASFChunkHeader ckhdr;
97 } ArgoBRPDemuxContext;
99 static int argo_brp_probe(const AVProbeData *p)
101 if (AV_RL32(p->buf) != BRP_TAG)
104 return AVPROBE_SCORE_EXTENSION + 1;
107 static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
108 void *buf, size_t bufsz)
114 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
116 size = BVID_HEADER_SIZE;
117 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
119 size = ASF_FILE_HEADER_SIZE;
120 } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
122 size = MASK_HEADER_SIZE;
124 avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id);
126 if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0)
132 if (hdr->extradata_size != size) {
133 av_log(s, AV_LOG_ERROR, "Invalid %s extradata size %u, expected %u\n",
134 name, hdr->extradata_size, size);
135 return AVERROR_INVALIDDATA;
138 av_assert0(bufsz >= size);
140 if ((ret = avio_read(s->pb, buf, size)) < 0)
149 static int argo_brp_read_header(AVFormatContext *s)
152 AVIOContext *pb = s->pb;
153 ArgoBRPDemuxContext *brp = s->priv_data;
154 uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
156 if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0)
158 else if (ret != BRP_FILE_HEADER_SIZE)
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);
165 if (brp->fhdr.magic != BRP_TAG)
166 return AVERROR_INVALIDDATA;
168 if (brp->fhdr.num_streams > BRP_MAX_STREAMS) {
169 avpriv_request_sample(s, ">%d streams", BRP_MAX_STREAMS);
170 return AVERROR_PATCHWELCOME;
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;
179 if (!(st = avformat_new_stream(s, NULL)))
180 return AVERROR(ENOMEM);
182 if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0)
184 else if (ret != BRP_STREAM_HEADER_SIZE)
187 hdr->codec_id = AV_RL32(buf + 0);
188 hdr->id = AV_RL32(buf + 4);
189 hdr->duration_ms = AV_RL32(buf + 8);
190 hdr->byte_rate = AV_RL32(buf + 12);
191 hdr->extradata_size = AV_RL32(buf + 16);
193 /* This should always be the case. */
195 return AVERROR_INVALIDDATA;
197 /* Timestamps are in milliseconds. */
198 avpriv_set_pts_info(st, 64, 1, 1000);
199 st->duration = hdr->duration_ms;
200 st->codecpar->bit_rate = hdr->byte_rate * 8;
202 if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) {
204 } else if (ret > 0) {
205 st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
209 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
210 ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
212 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
214 /* No decoder for this yet. */
215 st->codecpar->codec_id = AV_CODEC_ID_NONE;
217 bvid->num_frames = AV_RL32(buf + 0);
218 bvid->width = AV_RL32(buf + 4);
219 bvid->height = AV_RL32(buf + 8);
220 bvid->depth = AV_RL32(buf + 12);
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;
228 st->codecpar->width = bvid->width;
229 st->codecpar->height = bvid->height;
231 if (bvid->depth == 8) {
232 st->codecpar->format = AV_PIX_FMT_PAL8;
233 } else if (bvid->depth == 24) {
234 st->codecpar->format = AV_PIX_FMT_RGB24;
236 avpriv_request_sample(s, "depth == %u", bvid->depth);
237 return AVERROR_PATCHWELCOME;
239 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
241 * It would make the demuxer significantly more complicated
242 * to support multiple BASF streams. I've never seen a file
243 * with more than one.
245 if (brp->basf.index >= 0) {
246 avpriv_request_sample(s, "Multiple BASF streams");
247 return AVERROR_PATCHWELCOME;
250 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
251 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO;
253 ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf);
255 if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
257 } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
258 ArgoMASKHeader *mask = &hdr->extradata.mask;
260 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
262 mask->num_frames = AV_RL32(buf + 0);
263 mask->width = AV_RL32(buf + 4);
264 mask->height = AV_RL32(buf + 8);
266 st->nb_frames = mask->num_frames;
268 av_assert0(0); /* Caught above, should never happen. */
273 * This is nasty. BASF streams have their chunk header in each block,
274 * so the first one needs to be read to get the stream info. It should
275 * always be the first one.
277 if (brp->basf.index >= 0) {
278 AVStream *st = s->streams[brp->basf.index];
279 ArgoBRPStreamHeader *hdr = brp->streams + brp->basf.index;
280 ArgoBRPBlockHeader blk;
283 av_assert0(st->codecpar->codec_id == AV_CODEC_ID_ADPCM_ARGO);
284 av_assert0(brp->streams[brp->basf.index].extradata_size == ASF_FILE_HEADER_SIZE);
286 if ((ret = avio_tell(s->pb)) < 0)
291 if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
293 else if (ret != BRP_BLOCK_HEADER_SIZE)
296 blk.stream_id = AV_RL32(buf + 0);
297 blk.start_ms = AV_RL32(buf + 4);
298 blk.size = AV_RL32(buf + 8);
300 if (blk.stream_id != brp->basf.index) {
301 avpriv_request_sample(s, "first block not BASF");
302 return AVERROR_PATCHWELCOME;
305 if (blk.size < ASF_CHUNK_HEADER_SIZE)
306 return AVERROR_INVALIDDATA;
308 if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
310 else if (ret != ASF_CHUNK_HEADER_SIZE)
313 ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
315 if ((ret = ff_argo_asf_fill_stream(s, st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0)
318 /* Convert ms to samples. */
319 st->start_time = av_rescale_rnd(blk.start_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP);
320 st->duration = av_rescale_rnd(hdr->duration_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP);
322 if ((ret = avio_seek(s->pb, offset, SEEK_SET)) < 0)
328 static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
330 ArgoBRPDemuxContext *brp = s->priv_data;
331 ArgoBRPBlockHeader blk;
332 const ArgoBRPStreamHeader *shdr;
334 uint8_t buf[BRP_MIN_BUFFER_SIZE];
335 ArgoASFChunkHeader ckhdr;
338 if ((ret = avio_read(s->pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
340 else if (ret != BRP_BLOCK_HEADER_SIZE)
343 blk.stream_id = AV_RL32(buf + 0);
344 blk.start_ms = AV_RL32(buf + 4);
345 blk.size = AV_RL32(buf + 8);
347 if (blk.stream_id == -1)
350 if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams)
351 return AVERROR_INVALIDDATA;
353 st = s->streams[blk.stream_id];
354 shdr = brp->streams + blk.stream_id;
356 if (blk.stream_id == brp->basf.index) {
357 if (blk.size < ASF_CHUNK_HEADER_SIZE)
358 return AVERROR_INVALIDDATA;
360 if ((ret = avio_read(s->pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
363 ff_argo_asf_parse_chunk_header(&ckhdr, buf);
365 /* Ensure the chunk attributes are the same. */
366 if (ckhdr.sample_rate != brp->basf.ckhdr.sample_rate ||
367 ckhdr.flags != brp->basf.ckhdr.flags ||
368 ckhdr.unk1 != brp->basf.ckhdr.unk1 ||
369 ckhdr.unk2 != brp->basf.ckhdr.unk2)
370 return AVERROR_INVALIDDATA;
372 blk.size -= ASF_CHUNK_HEADER_SIZE;
374 if (blk.size % st->codecpar->block_align != 0)
375 return AVERROR_INVALIDDATA;
378 if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0)
380 else if (ret != blk.size)
381 return AVERROR_INVALIDDATA;
383 if (blk.stream_id == brp->basf.index) {
384 pkt->duration = ckhdr.num_samples * ckhdr.num_blocks;
385 pkt->pts = av_rescale_rnd(blk.start_ms, ckhdr.sample_rate, 1000, AV_ROUND_UP);
386 } else if (shdr->codec_id == BRP_CODEC_ID_BVID) {
387 pkt->duration = av_rescale_rnd(1, st->duration, shdr->extradata.bvid.num_frames, AV_ROUND_UP);
388 pkt->pts = blk.start_ms;
390 pkt->pts = blk.start_ms;
393 pkt->stream_index = blk.stream_id;
397 AVInputFormat ff_argo_brp_demuxer = {
399 .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games BRP"),
400 .priv_data_size = sizeof(ArgoBRPDemuxContext),
401 .read_probe = argo_brp_probe,
402 .read_header = argo_brp_read_header,
403 .read_packet = argo_brp_read_packet,