FFmpeg
Loading...
Searching...
No Matches
argo_brp.c
Go to the documentation of this file.
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"
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
50typedef struct ArgoBRPFileHeader {
51 uint32_t magic;
52 uint32_t num_streams;
53 uint32_t byte_rate;
55
61
62typedef struct ArgoBVIDHeader {
63 uint32_t num_frames;
64 uint32_t width;
65 uint32_t height;
66 uint32_t depth;
68
69typedef struct ArgoMASKHeader {
70 uint32_t num_frames;
71 uint32_t width;
72 uint32_t height;
74
75typedef struct ArgoBRPStreamHeader {
76 uint32_t codec_id;
77 uint32_t id;
78 uint32_t duration_ms;
79 uint32_t byte_rate;
81 union
82 {
83 /* If codec_id == BRP_CODEC_ID_BVID */
85 /* If codec_id == BRP_CODEC_ID_BASF */
87 /* If codec_id == BRP_CODEC_ID_MASK */
91
101
102static int argo_brp_probe(const AVProbeData *p)
103{
104 if (AV_RL32(p->buf) != BRP_TAG)
105 return 0;
106
107 return AVPROBE_SCORE_EXTENSION + 1;
108}
109
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";
120 } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
121 name = "BASF";
123 } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
124 name = "MASK";
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
151{
152 int64_t ret;
153 AVIOContext *pb = s->pb;
154 ArgoBRPDemuxContext *brp = s->priv_data;
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);
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
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) {
205 continue;
206 }
207
208 if (hdr->codec_id == BRP_CODEC_ID_BVID) {
209 ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
210
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;
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");
241 }
242
245 brp->basf.index = i;
247
248 if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
249 return ret;
250
252 } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
254
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;
273 int i;
274
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++) {
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
313 if (ret < 0)
314 return ret;
315
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)
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);
338
339done:
340 if ((ret = avio_seek(s->pb, offset, SEEK_SET)) < 0)
341 return ret;
342 }
343 return 0;
344}
345
347{
348 ArgoBRPDemuxContext *brp = s->priv_data;
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
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
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
413 .p.name = "argo_brp",
414 .p.long_name = NULL_IF_CONFIG_SMALL("Argonaut Games BRP"),
415 .priv_data_size = sizeof(ArgoBRPDemuxContext),
419};
const FFInputFormat ff_argo_brp_demuxer
Definition argo_brp.c:412
int ff_argo_asf_validate_file_header(AVFormatContext *s, const ArgoASFFileHeader *hdr)
Definition argo_asf.c:65
void ff_argo_asf_parse_file_header(ArgoASFFileHeader *hdr, const uint8_t *buf)
Definition argo_asf.c:54
int ff_argo_asf_fill_stream(AVFormatContext *s, AVStream *st, const ArgoASFFileHeader *fhdr, const ArgoASFChunkHeader *ckhdr)
Definition argo_asf.c:86
void ff_argo_asf_parse_chunk_header(ArgoASFChunkHeader *hdr, const uint8_t *buf)
Definition argo_asf.c:76
#define ASF_CHUNK_HEADER_SIZE
Definition argo_asf.h:33
#define ASF_FILE_HEADER_SIZE
Definition argo_asf.h:32
#define ASF_MIN_BUFFER_SIZE
Definition argo_asf.h:35
#define BRP_CODEC_ID_MASK
Definition argo_brp.c:48
#define BRP_STREAM_HEADER_SIZE
Definition argo_brp.c:35
#define BRP_CODEC_ID_BASF
Definition argo_brp.c:47
#define BRP_TAG
Definition argo_brp.c:32
#define BRP_MAX_STREAMS
Definition argo_brp.c:36
#define BRP_CODEC_ID_BVID
Definition argo_brp.c:46
#define BVID_HEADER_SIZE
Definition argo_brp.c:38
static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr, void *buf, size_t bufsz)
Definition argo_brp.c:110
#define BRP_FILE_HEADER_SIZE
Definition argo_brp.c:33
static int argo_brp_read_header(AVFormatContext *s)
Definition argo_brp.c:150
#define BRP_BLOCK_HEADER_SIZE
Definition argo_brp.c:34
static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition argo_brp.c:346
static int argo_brp_probe(const AVProbeData *p)
Definition argo_brp.c:102
#define BRP_BASF_LOOKAHEAD
Definition argo_brp.c:37
#define MASK_HEADER_SIZE
Definition argo_brp.c:39
#define BRP_MIN_BUFFER_SIZE
Definition argo_brp.c:40
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition avformat.c:834
Main libavformat public API header.
#define AVPROBE_SCORE_EXTENSION
score for file extension
Definition avformat.h:481
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition utils.c:98
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition avio.h:494
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition aviobuf.c:321
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:665
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
static int read_probe(const AVProbeData *p)
Definition cdg.c:30
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int read_header(FFV1Context *f, RangeCoder *c)
Definition ffv1dec.c:578
@ AV_CODEC_ID_ADPCM_ARGO
Definition codec_id.h:412
@ AV_CODEC_ID_ARGO
Definition codec_id.h:302
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition error.h:64
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition log.h:236
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition mathematics.c:58
@ AV_ROUND_UP
Round toward +infinity.
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition avutil.h:202
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition avutil.h:199
#define AV_RL32(p)
unsigned offset
Definition libaomenc.c:763
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMAX(a, b)
Definition macros.h:47
const char * name
Definition qsvenc.c:142
#define blk(i)
Definition sha.c:55
int height
The height of the video frame in pixels.
Definition codec_par.h:150
int bits_per_coded_sample
The number of bits per sample in the codedwords.
Definition codec_par.h:113
int width
The width of the video frame in pixels.
Definition codec_par.h:143
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition codec_par.h:99
enum AVMediaType codec_type
General type of the encoded data.
Definition codec_par.h:53
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition codec_par.h:57
int sample_rate
The number of audio samples per second.
Definition codec_par.h:213
Format I/O context.
Definition avformat.h:1333
Bytestream IO Context.
Definition avio.h:160
This structure stores compressed data.
Definition packet.h:580
This structure contains the data a format has to probe a file.
Definition avformat.h:471
Stream structure.
Definition avformat.h:766
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition avformat.h:789
int64_t nb_frames
number of frames in this stream if known or 0
Definition avformat.h:827
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition avformat.h:825
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition avformat.h:815
uint32_t num_blocks
Definition argo_asf.h:48
uint16_t sample_rate
Definition argo_asf.h:51
uint32_t num_samples
Definition argo_asf.h:49
uint32_t num_chunks
Definition argo_asf.h:42
uint16_t version_minor
Definition argo_asf.h:41
uint16_t version_major
Definition argo_asf.h:40
uint32_t start_ms
Definition argo_brp.c:58
int32_t stream_id
Definition argo_brp.c:57
ArgoBRPStreamHeader streams[BRP_MAX_STREAMS]
Definition argo_brp.c:94
ArgoBRPFileHeader fhdr
Definition argo_brp.c:93
struct ArgoBRPDemuxContext::@346012062342021133222157011302015224044156247160 basf
ArgoASFChunkHeader ckhdr
Definition argo_brp.c:98
uint32_t num_streams
Definition argo_brp.c:52
uint32_t byte_rate
Definition argo_brp.c:53
uint32_t magic
Definition argo_brp.c:51
uint32_t codec_id
Definition argo_brp.c:76
ArgoASFFileHeader basf
Definition argo_brp.c:86
ArgoBVIDHeader bvid
Definition argo_brp.c:84
union ArgoBRPStreamHeader::@025021377356220343137257004240256234020123134072 extradata
uint32_t extradata_size
Definition argo_brp.c:80
uint32_t duration_ms
Definition argo_brp.c:78
ArgoMASKHeader mask
Definition argo_brp.c:88
uint32_t byte_rate
Definition argo_brp.c:79
uint32_t width
Definition argo_brp.c:64
uint32_t depth
Definition argo_brp.c:66
uint32_t height
Definition argo_brp.c:65
uint32_t num_frames
Definition argo_brp.c:63
uint32_t num_frames
Definition argo_brp.c:70
uint32_t height
Definition argo_brp.c:72
uint32_t width
Definition argo_brp.c:71
#define avpriv_request_sample(...)
#define av_log(a,...)
int size