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 BRP_MIN_BUFFER_SIZE FFMAX(FFMAX3(BRP_FILE_HEADER_SIZE, \
37 BRP_BLOCK_HEADER_SIZE, \
38 BRP_STREAM_HEADER_SIZE), \
41 #define BRP_CODEC_ID_BVID MKTAG('B', 'V', 'I', 'D')
42 #define BRP_CODEC_ID_BASF MKTAG('B', 'A', 'S', 'F')
44 typedef struct ArgoBRPFileHeader {
50 typedef struct ArgoBRPBlockHeader {
56 typedef struct ArgoBVIDHeader {
63 typedef struct ArgoBRPStreamHeader {
68 uint32_t extradata_size;
71 /* If codec_id == BRP_CODEC_ID_BVID */
73 /* If codec_id == BRP_CODEC_ID_BASF */
74 ArgoASFFileHeader basf;
76 } ArgoBRPStreamHeader;
78 typedef struct ArgoBRPDemuxContext {
79 ArgoBRPFileHeader fhdr;
80 ArgoBRPStreamHeader streams[BRP_MAX_STREAMS];
84 ArgoASFChunkHeader ckhdr;
86 } ArgoBRPDemuxContext;
88 static int argo_brp_probe(const AVProbeData *p)
90 if (AV_RL32(p->buf) != BRP_TAG)
93 return AVPROBE_SCORE_EXTENSION + 1;
96 static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
97 void *buf, size_t bufsz)
103 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
105 size = BVID_HEADER_SIZE;
106 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
108 size = ASF_FILE_HEADER_SIZE;
110 avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id);
112 if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0)
118 if (hdr->extradata_size != size) {
119 av_log(s, AV_LOG_ERROR, "Invalid %s extradata size %u, expected %u\n",
120 name, hdr->extradata_size, size);
121 return AVERROR_INVALIDDATA;
124 av_assert0(bufsz >= size);
126 if ((ret = avio_read(s->pb, buf, size)) < 0)
135 static int argo_brp_read_header(AVFormatContext *s)
138 AVIOContext *pb = s->pb;
139 ArgoBRPDemuxContext *brp = s->priv_data;
140 uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
142 if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0)
144 else if (ret != BRP_FILE_HEADER_SIZE)
147 brp->fhdr.magic = AV_RL32(buf + 0);
148 brp->fhdr.num_streams = AV_RL32(buf + 4);
149 brp->fhdr.byte_rate = AV_RL32(buf + 8);
151 if (brp->fhdr.magic != BRP_TAG)
152 return AVERROR_INVALIDDATA;
154 if (brp->fhdr.num_streams > BRP_MAX_STREAMS) {
155 avpriv_request_sample(s, ">%d streams", BRP_MAX_STREAMS);
156 return AVERROR_PATCHWELCOME;
159 /* Build the stream info. */
160 brp->basf.index = -1;
161 for (uint32_t i = 0; i < brp->fhdr.num_streams; i++) {
162 ArgoBRPStreamHeader *hdr = brp->streams + i;
165 if (!(st = avformat_new_stream(s, NULL)))
166 return AVERROR(ENOMEM);
168 if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0)
170 else if (ret != BRP_STREAM_HEADER_SIZE)
173 hdr->codec_id = AV_RL32(buf + 0);
174 hdr->id = AV_RL32(buf + 4);
175 hdr->duration_ms = AV_RL32(buf + 8);
176 hdr->byte_rate = AV_RL32(buf + 12);
177 hdr->extradata_size = AV_RL32(buf + 16);
179 /* This should always be the case. */
181 return AVERROR_INVALIDDATA;
183 /* Timestamps are in milliseconds. */
184 avpriv_set_pts_info(st, 64, 1, 1000);
185 st->duration = hdr->duration_ms;
186 st->codecpar->bit_rate = hdr->byte_rate * 8;
188 if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) {
190 } else if (ret > 0) {
191 st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
195 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
196 ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
198 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
200 /* No decoder for this yet. */
201 st->codecpar->codec_id = AV_CODEC_ID_NONE;
203 bvid->num_frames = AV_RL32(buf + 0);
204 bvid->width = AV_RL32(buf + 4);
205 bvid->height = AV_RL32(buf + 8);
206 bvid->depth = AV_RL32(buf + 12);
208 /* These are from 1990's games, sanity check this. */
209 if (bvid->width >= 65536 || bvid->height >= 65536 ||
210 bvid->depth > 24 || bvid->depth % 8 != 0) {
211 return AVERROR_INVALIDDATA;
214 st->codecpar->width = bvid->width;
215 st->codecpar->height = bvid->height;
217 if (bvid->depth == 8) {
218 st->codecpar->format = AV_PIX_FMT_PAL8;
219 } else if (bvid->depth == 24) {
220 st->codecpar->format = AV_PIX_FMT_RGB24;
222 avpriv_request_sample(s, "depth == %u", bvid->depth);
223 return AVERROR_PATCHWELCOME;
225 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
227 * It would make the demuxer significantly more complicated
228 * to support multiple BASF streams. I've never seen a file
229 * with more than one.
231 if (brp->basf.index >= 0) {
232 avpriv_request_sample(s, "Multiple BASF streams");
233 return AVERROR_PATCHWELCOME;
236 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
237 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO;
239 ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf);
241 if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
245 av_assert0(0); /* Caught above, should never happen. */
250 * This is nasty. BASF streams have their chunk header in each block,
251 * so the first one needs to be read to get the stream info. It should
252 * always be the first one.
254 if (brp->basf.index >= 0) {
255 AVStream *st = s->streams[brp->basf.index];
256 ArgoBRPStreamHeader *hdr = brp->streams + brp->basf.index;
257 ArgoBRPBlockHeader blk;
260 av_assert0(st->codecpar->codec_id == AV_CODEC_ID_ADPCM_ARGO);
261 av_assert0(brp->streams[brp->basf.index].extradata_size == ASF_FILE_HEADER_SIZE);
263 if ((ret = avio_tell(s->pb)) < 0)
268 if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
270 else if (ret != BRP_BLOCK_HEADER_SIZE)
273 blk.stream_id = AV_RL32(buf + 0);
274 blk.start_ms = AV_RL32(buf + 4);
275 blk.size = AV_RL32(buf + 8);
277 if (blk.stream_id != brp->basf.index) {
278 avpriv_request_sample(s, "first block not BASF");
279 return AVERROR_PATCHWELCOME;
282 if (blk.size < ASF_CHUNK_HEADER_SIZE)
283 return AVERROR_INVALIDDATA;
285 if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
287 else if (ret != ASF_CHUNK_HEADER_SIZE)
290 ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
292 if ((ret = ff_argo_asf_fill_stream(st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0)
295 /* Convert ms to samples. */
296 st->start_time = av_rescale_rnd(blk.start_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP);
297 st->duration = av_rescale_rnd(hdr->duration_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP);
299 if ((ret = avio_seek(s->pb, offset, SEEK_SET)) < 0)
305 static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
307 ArgoBRPDemuxContext *brp = s->priv_data;
308 ArgoBRPBlockHeader blk;
309 const ArgoBRPStreamHeader *shdr;
311 uint8_t buf[BRP_MIN_BUFFER_SIZE];
312 ArgoASFChunkHeader ckhdr;
315 if ((ret = avio_read(s->pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
317 else if (ret != BRP_BLOCK_HEADER_SIZE)
320 blk.stream_id = AV_RL32(buf + 0);
321 blk.start_ms = AV_RL32(buf + 4);
322 blk.size = AV_RL32(buf + 8);
324 if (blk.stream_id == -1)
327 if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams)
328 return AVERROR_INVALIDDATA;
330 st = s->streams[blk.stream_id];
331 shdr = brp->streams + blk.stream_id;
333 if (blk.stream_id == brp->basf.index) {
334 if (blk.size < ASF_CHUNK_HEADER_SIZE)
335 return AVERROR_INVALIDDATA;
337 if ((ret = avio_read(s->pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
340 ff_argo_asf_parse_chunk_header(&ckhdr, buf);
342 /* Ensure the chunk attributes are the same. */
343 if (ckhdr.sample_rate != brp->basf.ckhdr.sample_rate ||
344 ckhdr.flags != brp->basf.ckhdr.flags ||
345 ckhdr.unk1 != brp->basf.ckhdr.unk1 ||
346 ckhdr.unk2 != brp->basf.ckhdr.unk2)
347 return AVERROR_INVALIDDATA;
349 blk.size -= ASF_CHUNK_HEADER_SIZE;
351 if (blk.size % st->codecpar->block_align != 0)
352 return AVERROR_INVALIDDATA;
355 if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0)
357 else if (ret != blk.size)
358 return AVERROR_INVALIDDATA;
360 if (blk.stream_id == brp->basf.index) {
361 pkt->duration = ckhdr.num_samples * ckhdr.num_blocks;
362 pkt->pts = av_rescale_rnd(blk.start_ms, ckhdr.sample_rate, 1000, AV_ROUND_UP);
363 } else if (shdr->codec_id == BRP_CODEC_ID_BVID) {
364 pkt->duration = av_rescale_rnd(1, st->duration, shdr->extradata.bvid.num_frames, AV_ROUND_UP);
365 pkt->pts = blk.start_ms;
367 pkt->pts = blk.start_ms;
370 pkt->stream_index = blk.stream_id;
374 AVInputFormat ff_argo_brp_demuxer = {
376 .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games BRP"),
377 .priv_data_size = sizeof(ArgoBRPDemuxContext),
378 .read_probe = argo_brp_probe,
379 .read_header = argo_brp_read_header,
380 .read_packet = argo_brp_read_packet,