]> git.sesse.net Git - ffmpeg/blob - libavformat/argo_brp.c
avformat/argo_brp: support MASK streams
[ffmpeg] / libavformat / argo_brp.c
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 "internal.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/avassert.h"
27 #include "libavutil/internal.h"
28 #include "argo_asf.h"
29
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), \
40                                       BVID_HEADER_SIZE,                \
41                                       MASK_HEADER_SIZE)
42
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')
46
47 typedef struct ArgoBRPFileHeader {
48     uint32_t magic;
49     uint32_t num_streams;
50     uint32_t byte_rate;
51 } ArgoBRPFileHeader;
52
53 typedef struct ArgoBRPBlockHeader {
54     int32_t  stream_id;
55     uint32_t start_ms;
56     uint32_t size;
57 } ArgoBRPBlockHeader;
58
59 typedef struct ArgoBVIDHeader {
60     uint32_t num_frames;
61     uint32_t width;
62     uint32_t height;
63     uint32_t depth;
64 } ArgoBVIDHeader;
65
66 typedef struct ArgoMASKHeader {
67     uint32_t num_frames;
68     uint32_t width;
69     uint32_t height;
70 } ArgoMASKHeader;
71
72 typedef struct ArgoBRPStreamHeader {
73     uint32_t codec_id;
74     uint32_t id;
75     uint32_t duration_ms;
76     uint32_t byte_rate;
77     uint32_t extradata_size;
78     union
79     {
80         /* If codec_id == BRP_CODEC_ID_BVID */
81         ArgoBVIDHeader    bvid;
82         /* If codec_id == BRP_CODEC_ID_BASF */
83         ArgoASFFileHeader basf;
84         /* If codec_id == BRP_CODEC_ID_MASK */
85         ArgoMASKHeader    mask;
86     } extradata;
87 } ArgoBRPStreamHeader;
88
89 typedef struct ArgoBRPDemuxContext {
90     ArgoBRPFileHeader   fhdr;
91     ArgoBRPStreamHeader streams[BRP_MAX_STREAMS];
92
93     struct {
94         int                 index;
95         ArgoASFChunkHeader  ckhdr;
96     } basf;
97 } ArgoBRPDemuxContext;
98
99 static int argo_brp_probe(const AVProbeData *p)
100 {
101     if (AV_RL32(p->buf) != BRP_TAG)
102         return 0;
103
104     return AVPROBE_SCORE_EXTENSION + 1;
105 }
106
107 static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
108                           void *buf, size_t bufsz)
109 {
110     const char *name;
111     uint32_t size;
112     int64_t ret;
113
114     if (hdr->codec_id == BRP_CODEC_ID_BVID) {
115         name = "BVID";
116         size = BVID_HEADER_SIZE;
117     } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
118         name = "BASF";
119         size = ASF_FILE_HEADER_SIZE;
120     } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
121         name = "MASK";
122         size = MASK_HEADER_SIZE;
123     } else {
124         avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id);
125
126         if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0)
127             return ret;
128
129         return 1;
130     }
131
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;
136     }
137
138     av_assert0(bufsz >= size);
139
140     if ((ret = avio_read(s->pb, buf, size)) < 0)
141         return ret;
142
143     if (ret != size)
144         return AVERROR(EIO);
145
146     return 0;
147 }
148
149 static int argo_brp_read_header(AVFormatContext *s)
150 {
151     int64_t ret;
152     AVIOContext *pb = s->pb;
153     ArgoBRPDemuxContext *brp = s->priv_data;
154     uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
155
156     if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0)
157         return ret;
158     else if (ret != BRP_FILE_HEADER_SIZE)
159         return AVERROR(EIO);
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);
170         return AVERROR_PATCHWELCOME;
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
182         if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0)
183             return ret;
184         else if (ret != BRP_STREAM_HEADER_SIZE)
185             return AVERROR(EIO);
186
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);
192
193         /* This should always be the case. */
194         if (hdr->id != i)
195             return AVERROR_INVALIDDATA;
196
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;
201
202         if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) {
203             return ret;
204         } else if (ret > 0) {
205             st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
206             continue;
207         }
208
209         if (hdr->codec_id == BRP_CODEC_ID_BVID) {
210             ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
211
212             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
213
214             /* No decoder for this yet. */
215             st->codecpar->codec_id   = AV_CODEC_ID_NONE;
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             /* 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
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;
235             } else {
236                 avpriv_request_sample(s, "depth == %u", bvid->depth);
237                 return AVERROR_PATCHWELCOME;
238             }
239         } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
240             /*
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.
244              */
245             if (brp->basf.index >= 0) {
246                 avpriv_request_sample(s, "Multiple BASF streams");
247                 return AVERROR_PATCHWELCOME;
248             }
249
250             st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
251             st->codecpar->codec_id   = AV_CODEC_ID_ADPCM_ARGO;
252             brp->basf.index          = i;
253             ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf);
254
255             if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
256                 return ret;
257         } else if (hdr->codec_id == BRP_CODEC_ID_MASK) {
258             ArgoMASKHeader *mask = &hdr->extradata.mask;
259
260             st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
261
262             mask->num_frames = AV_RL32(buf + 0);
263             mask->width      = AV_RL32(buf + 4);
264             mask->height     = AV_RL32(buf + 8);
265
266             st->nb_frames    = mask->num_frames;
267         } else {
268             av_assert0(0); /* Caught above, should never happen. */
269         }
270     }
271
272     /*
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.
276      */
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;
281         int64_t offset;
282
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);
285
286         if ((ret = avio_tell(s->pb)) < 0)
287             return ret;
288
289         offset = ret;
290
291         if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
292             return ret;
293         else if (ret != BRP_BLOCK_HEADER_SIZE)
294             return AVERROR(EIO);
295
296         blk.stream_id = AV_RL32(buf + 0);
297         blk.start_ms  = AV_RL32(buf + 4);
298         blk.size      = AV_RL32(buf + 8);
299
300         if (blk.stream_id != brp->basf.index) {
301             avpriv_request_sample(s, "first block not BASF");
302             return AVERROR_PATCHWELCOME;
303         }
304
305         if (blk.size < ASF_CHUNK_HEADER_SIZE)
306             return AVERROR_INVALIDDATA;
307
308         if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
309             return ret;
310         else if (ret != ASF_CHUNK_HEADER_SIZE)
311             return AVERROR(EIO);
312
313         ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
314
315         if ((ret = ff_argo_asf_fill_stream(s, st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0)
316             return ret;
317
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);
321
322         if ((ret = avio_seek(s->pb, offset, SEEK_SET)) < 0)
323             return ret;
324     }
325     return 0;
326 }
327
328 static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
329 {
330     ArgoBRPDemuxContext *brp = s->priv_data;
331     ArgoBRPBlockHeader blk;
332     const ArgoBRPStreamHeader *shdr;
333     AVStream *st;
334     uint8_t buf[BRP_MIN_BUFFER_SIZE];
335     ArgoASFChunkHeader ckhdr;
336     int ret;
337
338     if ((ret = avio_read(s->pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
339         return ret;
340     else if (ret != BRP_BLOCK_HEADER_SIZE)
341         return AVERROR(EIO);
342
343     blk.stream_id = AV_RL32(buf + 0);
344     blk.start_ms  = AV_RL32(buf + 4);
345     blk.size      = AV_RL32(buf + 8);
346
347     if (blk.stream_id == -1)
348         return AVERROR_EOF;
349
350     if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams)
351         return AVERROR_INVALIDDATA;
352
353     st = s->streams[blk.stream_id];
354     shdr = brp->streams + blk.stream_id;
355
356     if (blk.stream_id == brp->basf.index) {
357         if (blk.size < ASF_CHUNK_HEADER_SIZE)
358             return AVERROR_INVALIDDATA;
359
360         if ((ret = avio_read(s->pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
361             return ret;
362
363         ff_argo_asf_parse_chunk_header(&ckhdr, buf);
364
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;
371
372         blk.size -= ASF_CHUNK_HEADER_SIZE;
373
374         if (blk.size % st->codecpar->block_align != 0)
375             return AVERROR_INVALIDDATA;
376     }
377
378     if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0)
379         return ret;
380     else if (ret != blk.size)
381         return AVERROR_INVALIDDATA;
382
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;
389     } else {
390         pkt->pts      = blk.start_ms;
391     }
392
393     pkt->stream_index = blk.stream_id;
394     return 0;
395 }
396
397 AVInputFormat ff_argo_brp_demuxer = {
398     .name           = "argo_brp",
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,
404 };