Line | Branch | Exec | Source |
---|---|---|---|
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 "demux.h" | ||
25 | #include "internal.h" | ||
26 | #include "libavutil/intreadwrite.h" | ||
27 | #include "libavutil/avassert.h" | ||
28 | #include "libavutil/internal.h" | ||
29 | #include "argo_asf.h" | ||
30 | |||
31 | #define BRP_TAG MKTAG('B', 'R', 'P', 'P') | ||
32 | #define BRP_FILE_HEADER_SIZE 12 | ||
33 | #define BRP_BLOCK_HEADER_SIZE 12 | ||
34 | #define BRP_STREAM_HEADER_SIZE 20 | ||
35 | #define BRP_MAX_STREAMS 32 /* Soft cap, but even this is overkill. */ | ||
36 | #define BRP_BASF_LOOKAHEAD 10 /* How many blocks to search for the first BASF one. */ | ||
37 | #define BVID_HEADER_SIZE 16 | ||
38 | #define MASK_HEADER_SIZE 12 | ||
39 | #define BRP_MIN_BUFFER_SIZE FFMAX3(FFMAX3(BRP_FILE_HEADER_SIZE, \ | ||
40 | BRP_BLOCK_HEADER_SIZE, \ | ||
41 | BRP_STREAM_HEADER_SIZE), \ | ||
42 | BVID_HEADER_SIZE, \ | ||
43 | MASK_HEADER_SIZE) | ||
44 | |||
45 | #define BRP_CODEC_ID_BVID MKTAG('B', 'V', 'I', 'D') | ||
46 | #define BRP_CODEC_ID_BASF MKTAG('B', 'A', 'S', 'F') | ||
47 | #define BRP_CODEC_ID_MASK MKTAG('M', 'A', 'S', 'K') | ||
48 | |||
49 | typedef struct ArgoBRPFileHeader { | ||
50 | uint32_t magic; | ||
51 | uint32_t num_streams; | ||
52 | uint32_t byte_rate; | ||
53 | } ArgoBRPFileHeader; | ||
54 | |||
55 | typedef struct ArgoBRPBlockHeader { | ||
56 | int32_t stream_id; | ||
57 | uint32_t start_ms; | ||
58 | uint32_t size; | ||
59 | } ArgoBRPBlockHeader; | ||
60 | |||
61 | typedef struct ArgoBVIDHeader { | ||
62 | uint32_t num_frames; | ||
63 | uint32_t width; | ||
64 | uint32_t height; | ||
65 | uint32_t depth; | ||
66 | } ArgoBVIDHeader; | ||
67 | |||
68 | typedef struct ArgoMASKHeader { | ||
69 | uint32_t num_frames; | ||
70 | uint32_t width; | ||
71 | uint32_t height; | ||
72 | } ArgoMASKHeader; | ||
73 | |||
74 | typedef struct ArgoBRPStreamHeader { | ||
75 | uint32_t codec_id; | ||
76 | uint32_t id; | ||
77 | uint32_t duration_ms; | ||
78 | uint32_t byte_rate; | ||
79 | uint32_t extradata_size; | ||
80 | union | ||
81 | { | ||
82 | /* If codec_id == BRP_CODEC_ID_BVID */ | ||
83 | ArgoBVIDHeader bvid; | ||
84 | /* If codec_id == BRP_CODEC_ID_BASF */ | ||
85 | ArgoASFFileHeader basf; | ||
86 | /* If codec_id == BRP_CODEC_ID_MASK */ | ||
87 | ArgoMASKHeader mask; | ||
88 | } extradata; | ||
89 | } ArgoBRPStreamHeader; | ||
90 | |||
91 | typedef struct ArgoBRPDemuxContext { | ||
92 | ArgoBRPFileHeader fhdr; | ||
93 | ArgoBRPStreamHeader streams[BRP_MAX_STREAMS]; | ||
94 | |||
95 | struct { | ||
96 | int index; | ||
97 | ArgoASFChunkHeader ckhdr; | ||
98 | } basf; | ||
99 | } ArgoBRPDemuxContext; | ||
100 | |||
101 | 7186 | static int argo_brp_probe(const AVProbeData *p) | |
102 | { | ||
103 |
1/2✓ Branch 0 taken 7186 times.
✗ Branch 1 not taken.
|
7186 | if (AV_RL32(p->buf) != BRP_TAG) |
104 | 7186 | return 0; | |
105 | |||
106 | ✗ | return AVPROBE_SCORE_EXTENSION + 1; | |
107 | } | ||
108 | |||
109 | ✗ | static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr, | |
110 | void *buf, size_t bufsz) | ||
111 | { | ||
112 | const char *name; | ||
113 | uint32_t size; | ||
114 | int64_t ret; | ||
115 | |||
116 | ✗ | if (hdr->codec_id == BRP_CODEC_ID_BVID) { | |
117 | ✗ | name = "BVID"; | |
118 | ✗ | size = BVID_HEADER_SIZE; | |
119 | ✗ | } else if (hdr->codec_id == BRP_CODEC_ID_BASF) { | |
120 | ✗ | name = "BASF"; | |
121 | ✗ | size = ASF_FILE_HEADER_SIZE; | |
122 | ✗ | } else if (hdr->codec_id == BRP_CODEC_ID_MASK) { | |
123 | ✗ | name = "MASK"; | |
124 | ✗ | size = MASK_HEADER_SIZE; | |
125 | } else { | ||
126 | ✗ | avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id); | |
127 | |||
128 | ✗ | if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0) | |
129 | ✗ | return ret; | |
130 | |||
131 | ✗ | return 1; | |
132 | } | ||
133 | |||
134 | ✗ | if (hdr->extradata_size != size) { | |
135 | ✗ | av_log(s, AV_LOG_ERROR, "Invalid %s extradata size %u, expected %u\n", | |
136 | ✗ | name, hdr->extradata_size, size); | |
137 | ✗ | return AVERROR_INVALIDDATA; | |
138 | } | ||
139 | |||
140 | ✗ | av_assert0(bufsz >= size); | |
141 | |||
142 | ✗ | if ((ret = avio_read(s->pb, buf, size)) < 0) | |
143 | ✗ | return ret; | |
144 | |||
145 | ✗ | if (ret != size) | |
146 | ✗ | return AVERROR(EIO); | |
147 | |||
148 | ✗ | return 0; | |
149 | } | ||
150 | |||
151 | ✗ | static int argo_brp_read_header(AVFormatContext *s) | |
152 | { | ||
153 | int64_t ret; | ||
154 | ✗ | AVIOContext *pb = s->pb; | |
155 | ✗ | ArgoBRPDemuxContext *brp = s->priv_data; | |
156 | uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)]; | ||
157 | |||
158 | ✗ | if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0) | |
159 | ✗ | return ret; | |
160 | ✗ | else if (ret != BRP_FILE_HEADER_SIZE) | |
161 | ✗ | return AVERROR(EIO); | |
162 | |||
163 | ✗ | brp->fhdr.magic = AV_RL32(buf + 0); | |
164 | ✗ | brp->fhdr.num_streams = AV_RL32(buf + 4); | |
165 | ✗ | brp->fhdr.byte_rate = AV_RL32(buf + 8); | |
166 | |||
167 | ✗ | if (brp->fhdr.magic != BRP_TAG) | |
168 | ✗ | return AVERROR_INVALIDDATA; | |
169 | |||
170 | ✗ | if (brp->fhdr.num_streams > BRP_MAX_STREAMS) { | |
171 | ✗ | avpriv_request_sample(s, ">%d streams", BRP_MAX_STREAMS); | |
172 | ✗ | return AVERROR_PATCHWELCOME; | |
173 | } | ||
174 | |||
175 | /* Build the stream info. */ | ||
176 | ✗ | brp->basf.index = -1; | |
177 | ✗ | for (uint32_t i = 0; i < brp->fhdr.num_streams; i++) { | |
178 | ✗ | ArgoBRPStreamHeader *hdr = brp->streams + i; | |
179 | AVStream *st; | ||
180 | |||
181 | ✗ | if (!(st = avformat_new_stream(s, NULL))) | |
182 | ✗ | return AVERROR(ENOMEM); | |
183 | |||
184 | ✗ | if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0) | |
185 | ✗ | return ret; | |
186 | ✗ | else if (ret != BRP_STREAM_HEADER_SIZE) | |
187 | ✗ | return AVERROR(EIO); | |
188 | |||
189 | ✗ | hdr->codec_id = AV_RL32(buf + 0); | |
190 | ✗ | hdr->id = AV_RL32(buf + 4); | |
191 | ✗ | hdr->duration_ms = AV_RL32(buf + 8); | |
192 | ✗ | hdr->byte_rate = AV_RL32(buf + 12); | |
193 | ✗ | hdr->extradata_size = AV_RL32(buf + 16); | |
194 | |||
195 | /* This should always be the case. */ | ||
196 | ✗ | if (hdr->id != i) | |
197 | ✗ | return AVERROR_INVALIDDATA; | |
198 | |||
199 | /* Timestamps are in milliseconds. */ | ||
200 | ✗ | avpriv_set_pts_info(st, 64, 1, 1000); | |
201 | ✗ | st->duration = hdr->duration_ms; | |
202 | ✗ | st->codecpar->bit_rate = hdr->byte_rate * 8; | |
203 | |||
204 | ✗ | if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) { | |
205 | ✗ | return ret; | |
206 | ✗ | } else if (ret > 0) { | |
207 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN; | |
208 | ✗ | continue; | |
209 | } | ||
210 | |||
211 | ✗ | if (hdr->codec_id == BRP_CODEC_ID_BVID) { | |
212 | ✗ | ArgoBVIDHeader *bvid = &hdr->extradata.bvid; | |
213 | |||
214 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO; | |
215 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_ARGO; | |
216 | |||
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); | |
221 | |||
222 | ✗ | if (bvid->num_frames == 0) | |
223 | ✗ | return AVERROR_INVALIDDATA; | |
224 | |||
225 | /* These are from 1990's games, sanity check this. */ | ||
226 | ✗ | if (bvid->width >= 65536 || bvid->height >= 65536 || | |
227 | ✗ | bvid->depth > 24 || bvid->depth % 8 != 0) { | |
228 | ✗ | return AVERROR_INVALIDDATA; | |
229 | } | ||
230 | |||
231 | ✗ | st->codecpar->width = bvid->width; | |
232 | ✗ | st->codecpar->height = bvid->height; | |
233 | ✗ | st->nb_frames = bvid->num_frames; | |
234 | ✗ | st->codecpar->bits_per_coded_sample = bvid->depth; | |
235 | ✗ | } else if (hdr->codec_id == BRP_CODEC_ID_BASF) { | |
236 | /* | ||
237 | * It would make the demuxer significantly more complicated | ||
238 | * to support multiple BASF streams. I've never seen a file | ||
239 | * with more than one. | ||
240 | */ | ||
241 | ✗ | if (brp->basf.index >= 0) { | |
242 | ✗ | avpriv_request_sample(s, "Multiple BASF streams"); | |
243 | ✗ | return AVERROR_PATCHWELCOME; | |
244 | } | ||
245 | |||
246 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO; | |
247 | ✗ | st->codecpar->codec_id = AV_CODEC_ID_ADPCM_ARGO; | |
248 | ✗ | brp->basf.index = i; | |
249 | ✗ | ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf); | |
250 | |||
251 | ✗ | if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0) | |
252 | ✗ | return ret; | |
253 | |||
254 | ✗ | st->nb_frames = hdr->extradata.basf.num_chunks; | |
255 | ✗ | } else if (hdr->codec_id == BRP_CODEC_ID_MASK) { | |
256 | ✗ | ArgoMASKHeader *mask = &hdr->extradata.mask; | |
257 | |||
258 | ✗ | st->codecpar->codec_type = AVMEDIA_TYPE_DATA; | |
259 | |||
260 | ✗ | mask->num_frames = AV_RL32(buf + 0); | |
261 | ✗ | mask->width = AV_RL32(buf + 4); | |
262 | ✗ | mask->height = AV_RL32(buf + 8); | |
263 | |||
264 | ✗ | st->nb_frames = mask->num_frames; | |
265 | } else { | ||
266 | ✗ | av_assert0(0); /* Caught above, should never happen. */ | |
267 | } | ||
268 | } | ||
269 | |||
270 | /* Try to find the first BASF chunk. */ | ||
271 | ✗ | if (brp->basf.index >= 0) { | |
272 | ✗ | AVStream *st = s->streams[brp->basf.index]; | |
273 | ✗ | ArgoBRPStreamHeader *hdr = brp->streams + brp->basf.index; | |
274 | ArgoBRPBlockHeader blk; | ||
275 | int64_t offset; | ||
276 | int i; | ||
277 | |||
278 | ✗ | av_assert0(st->codecpar->codec_id == AV_CODEC_ID_ADPCM_ARGO); | |
279 | ✗ | av_assert0(brp->streams[brp->basf.index].extradata_size == ASF_FILE_HEADER_SIZE); | |
280 | |||
281 | ✗ | if ((ret = avio_tell(s->pb)) < 0) | |
282 | ✗ | return ret; | |
283 | |||
284 | ✗ | offset = ret; | |
285 | |||
286 | ✗ | av_log(s, AV_LOG_TRACE, "Searching %d blocks for BASF...", BRP_BASF_LOOKAHEAD); | |
287 | |||
288 | ✗ | for (i = 0; i < BRP_BASF_LOOKAHEAD; i++) { | |
289 | ✗ | if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0) | |
290 | ✗ | return ret; | |
291 | ✗ | else if (ret != BRP_BLOCK_HEADER_SIZE) | |
292 | ✗ | return AVERROR(EIO); | |
293 | |||
294 | ✗ | blk.stream_id = AV_RL32(buf + 0); | |
295 | ✗ | blk.start_ms = AV_RL32(buf + 4); | |
296 | ✗ | blk.size = AV_RL32(buf + 8); | |
297 | |||
298 | ✗ | if (blk.stream_id == brp->basf.index || blk.stream_id == -1) | |
299 | break; | ||
300 | |||
301 | ✗ | if ((ret = avio_skip(pb, blk.size)) < 0) | |
302 | ✗ | return ret; | |
303 | } | ||
304 | |||
305 | ✗ | if (i == BRP_BASF_LOOKAHEAD || blk.stream_id == -1) { | |
306 | /* Don't error here, as there may still be a valid video stream. */ | ||
307 | ✗ | av_log(s, AV_LOG_TRACE, "not found\n"); | |
308 | ✗ | goto done; | |
309 | } | ||
310 | |||
311 | ✗ | av_log(s, AV_LOG_TRACE, "found at index %d\n", i); | |
312 | |||
313 | ✗ | if (blk.size < ASF_CHUNK_HEADER_SIZE) | |
314 | ✗ | return AVERROR_INVALIDDATA; | |
315 | |||
316 | ✗ | if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0) | |
317 | ✗ | return ret; | |
318 | ✗ | else if (ret != ASF_CHUNK_HEADER_SIZE) | |
319 | ✗ | return AVERROR(EIO); | |
320 | |||
321 | ✗ | ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf); | |
322 | |||
323 | /* | ||
324 | * Special Case Hack. It seems that in files where the BASF block isn't first, | ||
325 | * v1.1 streams are allowed to be non-22050... | ||
326 | * Bump the version to 1.2 so ff_argo_asf_fill_stream() doesn't "correct" it. | ||
327 | * | ||
328 | * Found in Alien Odyssey games files in: | ||
329 | * ./GRAPHICS/COMMBUNK/{{COMADD1,COMM2_{1,2,3E},COMM3_{2,3,4,5,6}},FADE{1,2}}.BRP | ||
330 | * | ||
331 | * Either this format really inconsistent, or FX Fighter and Croc just ignored the | ||
332 | * sample rate field... | ||
333 | */ | ||
334 | ✗ | if (i != 0 && hdr->extradata.basf.version_major == 1 && hdr->extradata.basf.version_minor == 1) | |
335 | ✗ | hdr->extradata.basf.version_minor = 2; | |
336 | |||
337 | ✗ | if ((ret = ff_argo_asf_fill_stream(s, st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0) | |
338 | ✗ | return ret; | |
339 | |||
340 | /* Convert ms to samples. */ | ||
341 | ✗ | st->start_time = av_rescale_rnd(blk.start_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP); | |
342 | ✗ | st->duration = av_rescale_rnd(hdr->duration_ms, st->codecpar->sample_rate, 1000, AV_ROUND_UP); | |
343 | |||
344 | ✗ | done: | |
345 | ✗ | if ((ret = avio_seek(s->pb, offset, SEEK_SET)) < 0) | |
346 | ✗ | return ret; | |
347 | } | ||
348 | ✗ | return 0; | |
349 | } | ||
350 | |||
351 | ✗ | static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt) | |
352 | { | ||
353 | ✗ | ArgoBRPDemuxContext *brp = s->priv_data; | |
354 | ArgoBRPBlockHeader blk; | ||
355 | const ArgoBRPStreamHeader *shdr; | ||
356 | AVStream *st; | ||
357 | uint8_t buf[BRP_MIN_BUFFER_SIZE]; | ||
358 | ArgoASFChunkHeader ckhdr; | ||
359 | int ret; | ||
360 | |||
361 | ✗ | if ((ret = avio_read(s->pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0) | |
362 | ✗ | return ret; | |
363 | ✗ | else if (ret != BRP_BLOCK_HEADER_SIZE) | |
364 | ✗ | return AVERROR(EIO); | |
365 | |||
366 | ✗ | blk.stream_id = AV_RL32(buf + 0); | |
367 | ✗ | blk.start_ms = AV_RL32(buf + 4); | |
368 | ✗ | blk.size = AV_RL32(buf + 8); | |
369 | |||
370 | ✗ | if (blk.stream_id == -1) | |
371 | ✗ | return AVERROR_EOF; | |
372 | |||
373 | ✗ | if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams) | |
374 | ✗ | return AVERROR_INVALIDDATA; | |
375 | |||
376 | ✗ | st = s->streams[blk.stream_id]; | |
377 | ✗ | shdr = brp->streams + blk.stream_id; | |
378 | |||
379 | ✗ | if (blk.stream_id == brp->basf.index) { | |
380 | ✗ | if (blk.size < ASF_CHUNK_HEADER_SIZE) | |
381 | ✗ | return AVERROR_INVALIDDATA; | |
382 | |||
383 | ✗ | if (avio_read(s->pb, buf, ASF_CHUNK_HEADER_SIZE) != ASF_CHUNK_HEADER_SIZE) | |
384 | ✗ | return AVERROR_INVALIDDATA; | |
385 | |||
386 | ✗ | ff_argo_asf_parse_chunk_header(&ckhdr, buf); | |
387 | |||
388 | /* Ensure the chunk attributes are the same. */ | ||
389 | ✗ | if (ckhdr.sample_rate != brp->basf.ckhdr.sample_rate || | |
390 | ✗ | ckhdr.flags != brp->basf.ckhdr.flags || | |
391 | ✗ | ckhdr.unk1 != brp->basf.ckhdr.unk1 || | |
392 | ✗ | ckhdr.unk2 != brp->basf.ckhdr.unk2) | |
393 | ✗ | return AVERROR_INVALIDDATA; | |
394 | |||
395 | ✗ | blk.size -= ASF_CHUNK_HEADER_SIZE; | |
396 | } | ||
397 | |||
398 | ✗ | if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0) | |
399 | ✗ | return ret; | |
400 | ✗ | else if (ret != blk.size) | |
401 | ✗ | return AVERROR_INVALIDDATA; | |
402 | |||
403 | ✗ | if (blk.stream_id == brp->basf.index) { | |
404 | ✗ | pkt->duration = ckhdr.num_samples * ckhdr.num_blocks; | |
405 | ✗ | pkt->pts = av_rescale_rnd(blk.start_ms, ckhdr.sample_rate, 1000, AV_ROUND_UP); | |
406 | ✗ | } else if (shdr->codec_id == BRP_CODEC_ID_BVID) { | |
407 | ✗ | pkt->duration = av_rescale_rnd(1, st->duration, shdr->extradata.bvid.num_frames, AV_ROUND_UP); | |
408 | ✗ | pkt->pts = blk.start_ms; | |
409 | } else { | ||
410 | ✗ | pkt->pts = blk.start_ms; | |
411 | } | ||
412 | |||
413 | ✗ | pkt->stream_index = blk.stream_id; | |
414 | ✗ | return 0; | |
415 | } | ||
416 | |||
417 | const FFInputFormat ff_argo_brp_demuxer = { | ||
418 | .p.name = "argo_brp", | ||
419 | .p.long_name = NULL_IF_CONFIG_SMALL("Argonaut Games BRP"), | ||
420 | .priv_data_size = sizeof(ArgoBRPDemuxContext), | ||
421 | .read_probe = argo_brp_probe, | ||
422 | .read_header = argo_brp_read_header, | ||
423 | .read_packet = argo_brp_read_packet, | ||
424 | }; | ||
425 |