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];
81 /* To know how much of a BASF to give. */
85 /* BASF-specific fields. */
88 ArgoASFChunkHeader ckhdr;
91 /* ms, not samples. */
94 } ArgoBRPDemuxContext;
96 static int argo_brp_probe(const AVProbeData *p)
98 if (AV_RL32(p->buf) != BRP_TAG)
101 return AVPROBE_SCORE_EXTENSION + 1;
104 static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
105 void *buf, size_t bufsz)
111 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
113 size = BVID_HEADER_SIZE;
114 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
116 size = ASF_FILE_HEADER_SIZE;
118 avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id);
120 if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0)
126 if (hdr->extradata_size != size) {
127 av_log(s, AV_LOG_ERROR, "Invalid %s extradata size %u, expected %u\n",
128 name, hdr->extradata_size, size);
129 return AVERROR_INVALIDDATA;
132 av_assert0(bufsz >= size);
134 if ((ret = avio_read(s->pb, buf, size)) < 0)
143 static int argo_brp_read_header(AVFormatContext *s)
146 AVIOContext *pb = s->pb;
147 ArgoBRPDemuxContext *brp = s->priv_data;
148 uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
150 if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0)
152 else if (ret != BRP_FILE_HEADER_SIZE)
155 brp->fhdr.magic = AV_RL32(buf + 0);
156 brp->fhdr.num_streams = AV_RL32(buf + 4);
157 brp->fhdr.byte_rate = AV_RL32(buf + 8);
159 if (brp->fhdr.magic != BRP_TAG)
160 return AVERROR_INVALIDDATA;
162 if (brp->fhdr.num_streams > BRP_MAX_STREAMS) {
163 avpriv_request_sample(s, ">%d streams", BRP_MAX_STREAMS);
164 return AVERROR_PATCHWELCOME;
167 /* Build the stream info. */
168 brp->basf.index = -1;
169 for (uint32_t i = 0; i < brp->fhdr.num_streams; i++) {
170 ArgoBRPStreamHeader *hdr = brp->streams + i;
173 if (!(st = avformat_new_stream(s, NULL)))
174 return AVERROR(ENOMEM);
176 if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0)
178 else if (ret != BRP_STREAM_HEADER_SIZE)
181 hdr->codec_id = AV_RL32(buf + 0);
182 hdr->id = AV_RL32(buf + 4);
183 hdr->duration_ms = AV_RL32(buf + 8);
184 hdr->byte_rate = AV_RL32(buf + 12);
185 hdr->extradata_size = AV_RL32(buf + 16);
187 /* Timestamps are in milliseconds. */
188 avpriv_set_pts_info(st, 64, 1, 1000);
189 st->duration = hdr->duration_ms;
190 st->codecpar->bit_rate = hdr->byte_rate * 8;
192 if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) {
194 } else if (ret > 0) {
195 st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
199 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
200 ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
202 st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
204 /* No decoder for this yet. */
205 st->codecpar->codec_id = AV_CODEC_ID_NONE;
207 bvid->num_frames = AV_RL32(buf + 0);
208 bvid->width = AV_RL32(buf + 4);
209 bvid->height = AV_RL32(buf + 8);
210 bvid->depth = AV_RL32(buf + 12);
212 /* These are from 1990's games, sanity check this. */
213 if (bvid->width >= 65536 || bvid->height >= 65536 ||
214 bvid->depth > 24 || bvid->depth % 8 != 0) {
215 return AVERROR_INVALIDDATA;
218 st->codecpar->width = bvid->width;
219 st->codecpar->height = bvid->height;
221 if (bvid->depth == 8) {
222 st->codecpar->format = AV_PIX_FMT_PAL8;
223 } else if (bvid->depth == 24) {
224 st->codecpar->format = AV_PIX_FMT_RGB24;
226 avpriv_request_sample(s, "depth == %u", bvid->depth);
227 return AVERROR_PATCHWELCOME;
229 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
231 * It would make the demuxer significantly more complicated
232 * to support multiple BASF streams. I've never seen a file
233 * with more than one.
235 if (brp->basf.index >= 0) {
236 avpriv_request_sample(s, "Multiple BASF streams");
237 return AVERROR_PATCHWELCOME;
240 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
241 st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO;
243 ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf);
245 if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
249 av_assert0(0); /* Caught above, should never happen. */
254 * This is nasty. BASF streams only have one (huge) block.
255 * It should be the first one. It contains the chunk header, so
256 * it needs to be read here.
258 if (brp->basf.index >= 0) {
259 AVStream *st = s->streams[brp->basf.index];
260 ArgoBRPStreamHeader *hdr = brp->streams + brp->basf.index;
261 ArgoBRPBlockHeader blk;
263 av_assert0(st->codecpar->codec_id == AV_CODEC_ID_ADPCM_ARGO);
264 av_assert0(brp->streams[brp->basf.index].extradata_size == ASF_FILE_HEADER_SIZE);
266 if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
268 else if (ret != BRP_BLOCK_HEADER_SIZE)
271 blk.stream_id = AV_RL32(buf + 0);
272 blk.start_ms = AV_RL32(buf + 4);
273 blk.size = AV_RL32(buf + 8);
275 if (blk.stream_id != brp->basf.index) {
276 avpriv_request_sample(st, "first block not BASF");
277 return AVERROR_PATCHWELCOME;
280 if (blk.size < ASF_CHUNK_HEADER_SIZE)
281 return AVERROR_INVALIDDATA;
283 if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
285 else if (ret != ASF_CHUNK_HEADER_SIZE)
288 ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
290 if ((ret = ff_argo_asf_fill_stream(st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0)
293 /* Convert ms to samples. */
294 st->start_time = (blk.start_ms * st->codecpar->sample_rate) / 1000;
296 if ((ret = avio_tell(s->pb)) < 0)
299 brp->basf.offset = ret;
301 if ((ret = avio_skip(s->pb, blk.size - ASF_CHUNK_HEADER_SIZE)) < 0)
307 static int argo_brp_read_basf(AVFormatContext *s, AVPacket *pkt,
308 ArgoBRPDemuxContext *brp, int ignorepts)
310 ArgoASFChunkHeader *ckhdr = &brp->basf.ckhdr;
311 AVCodecParameters *par;
314 if (brp->basf.index < 0)
317 par = s->streams[brp->basf.index]->codecpar;
319 if (brp->basf.blocks_read >= ckhdr->num_blocks)
322 if (!ignorepts && brp->lastpts < brp->basf.lastpts)
325 if ((ret = avio_tell(s->pb)) < 0)
330 if ((ret = avio_seek(s->pb, brp->basf.offset, SEEK_SET)) < 0)
332 else if (ret != brp->basf.offset)
335 if ((ret = av_get_packet(s->pb, pkt, par->block_align)) < 0)
338 if ((ret = avio_seek(s->pb, old, SEEK_SET)) < 0)
343 pkt->stream_index = brp->basf.index;
344 pkt->duration = ckhdr->num_samples;
346 brp->basf.offset += pkt->size;
347 brp->basf.blocks_read += 1;
348 /* Need the ceil() because ((32 * 1000) / 44100) < 1 */
349 brp->basf.lastpts += ceilf((ckhdr->num_samples * 1000.0f) / ckhdr->sample_rate);
353 static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
355 ArgoBRPDemuxContext *brp = s->priv_data;
356 ArgoBRPBlockHeader blk;
357 AVIOContext *pb = s->pb;
358 uint8_t buf[BRP_BLOCK_HEADER_SIZE];
362 * Special-case: send some more BASF content if we're running behind.
363 * Grr, why couldn't they just interleave it.
365 if ((ret = argo_brp_read_basf(s, pkt, brp, brp->hit_eof)) < 0)
373 if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
375 else if (ret != BRP_BLOCK_HEADER_SIZE)
378 blk.stream_id = AV_RL32(buf + 0);
379 blk.start_ms = AV_RL32(buf + 4);
380 blk.size = AV_RL32(buf + 8);
382 /* This is meant to be EOF, but there might be more BASF packets. */
383 if (blk.stream_id == -1) {
385 /* This is nasty, but safe. */
386 return argo_brp_read_packet(s, pkt);
389 if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams)
390 return AVERROR_INVALIDDATA;
392 /* Skip BASF blocks. */
393 if (blk.stream_id == brp->basf.index) {
394 if ((ret = avio_skip(s->pb, blk.size)) < 0)
397 if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0)
399 else if (ret != blk.size)
400 return AVERROR_INVALIDDATA;
403 pkt->stream_index = blk.stream_id;
404 pkt->pts = blk.start_ms;
405 brp->lastpts = FFMAX(brp->lastpts, blk.start_ms);
409 AVInputFormat ff_argo_brp_demuxer = {
411 .long_name = NULL_IF_CONFIG_SMALL("Argonaut Games BRP"),
412 .priv_data_size = sizeof(ArgoBRPDemuxContext),
413 .read_probe = argo_brp_probe,
414 .read_header = argo_brp_read_header,
415 .read_packet = argo_brp_read_packet,