]> git.sesse.net Git - ffmpeg/blob - libavformat/argo_brp.c
avformat/argo_brp: don't pass AVStream into avpriv_request_sample()
[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 BRP_MIN_BUFFER_SIZE     FFMAX(FFMAX3(BRP_FILE_HEADER_SIZE,    \
37                                              BRP_BLOCK_HEADER_SIZE,   \
38                                              BRP_STREAM_HEADER_SIZE), \
39                                       BVID_HEADER_SIZE)
40
41 #define BRP_CODEC_ID_BVID       MKTAG('B', 'V', 'I', 'D')
42 #define BRP_CODEC_ID_BASF       MKTAG('B', 'A', 'S', 'F')
43
44 typedef struct ArgoBRPFileHeader {
45     uint32_t magic;
46     uint32_t num_streams;
47     uint32_t byte_rate;
48 } ArgoBRPFileHeader;
49
50 typedef struct ArgoBRPBlockHeader {
51     int32_t  stream_id;
52     uint32_t start_ms;
53     uint32_t size;
54 } ArgoBRPBlockHeader;
55
56 typedef struct ArgoBVIDHeader {
57     uint32_t num_frames;
58     uint32_t width;
59     uint32_t height;
60     uint32_t depth;
61 } ArgoBVIDHeader;
62
63 typedef struct ArgoBRPStreamHeader {
64     uint32_t codec_id;
65     uint32_t id;
66     uint32_t duration_ms;
67     uint32_t byte_rate;
68     uint32_t extradata_size;
69     union
70     {
71         /* If codec_id == BRP_CODEC_ID_BVID */
72         ArgoBVIDHeader    bvid;
73         /* If codec_id == BRP_CODEC_ID_BASF */
74         ArgoASFFileHeader basf;
75     } extradata;
76 } ArgoBRPStreamHeader;
77
78 typedef struct ArgoBRPDemuxContext {
79     ArgoBRPFileHeader   fhdr;
80     ArgoBRPStreamHeader streams[BRP_MAX_STREAMS];
81
82     struct {
83         int                 index;
84         ArgoASFChunkHeader  ckhdr;
85     } basf;
86 } ArgoBRPDemuxContext;
87
88 static int argo_brp_probe(const AVProbeData *p)
89 {
90     if (AV_RL32(p->buf) != BRP_TAG)
91         return 0;
92
93     return AVPROBE_SCORE_EXTENSION + 1;
94 }
95
96 static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
97                           void *buf, size_t bufsz)
98 {
99     const char *name;
100     uint32_t size;
101     int64_t ret;
102
103     if (hdr->codec_id == BRP_CODEC_ID_BVID) {
104         name = "BVID";
105         size = BVID_HEADER_SIZE;
106     } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
107         name = "BASF";
108         size = ASF_FILE_HEADER_SIZE;
109     } else {
110         avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id);
111
112         if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0)
113             return ret;
114
115         return 1;
116     }
117
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;
122     }
123
124     av_assert0(bufsz >= size);
125
126     if ((ret = avio_read(s->pb, buf, size)) < 0)
127         return ret;
128
129     if (ret != size)
130         return AVERROR(EIO);
131
132     return 0;
133 }
134
135 static int argo_brp_read_header(AVFormatContext *s)
136 {
137     int64_t ret;
138     AVIOContext *pb = s->pb;
139     ArgoBRPDemuxContext *brp = s->priv_data;
140     uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
141
142     if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0)
143         return ret;
144     else if (ret != BRP_FILE_HEADER_SIZE)
145         return AVERROR(EIO);
146
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);
150
151     if (brp->fhdr.magic != BRP_TAG)
152         return AVERROR_INVALIDDATA;
153
154     if (brp->fhdr.num_streams > BRP_MAX_STREAMS) {
155         avpriv_request_sample(s, ">%d streams", BRP_MAX_STREAMS);
156         return AVERROR_PATCHWELCOME;
157     }
158
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;
163         AVStream *st;
164
165         if (!(st = avformat_new_stream(s, NULL)))
166             return AVERROR(ENOMEM);
167
168         if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0)
169             return ret;
170         else if (ret != BRP_STREAM_HEADER_SIZE)
171             return AVERROR(EIO);
172
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);
178
179         /* This should always be the case. */
180         if (hdr->id != i)
181             return AVERROR_INVALIDDATA;
182
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;
187
188         if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) {
189             return ret;
190         } else if (ret > 0) {
191             st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
192             continue;
193         }
194
195         if (hdr->codec_id == BRP_CODEC_ID_BVID) {
196             ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
197
198             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
199
200             /* No decoder for this yet. */
201             st->codecpar->codec_id   = AV_CODEC_ID_NONE;
202
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);
207
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;
212             }
213
214             st->codecpar->width  = bvid->width;
215             st->codecpar->height = bvid->height;
216
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;
221             } else {
222                 avpriv_request_sample(s, "depth == %u", bvid->depth);
223                 return AVERROR_PATCHWELCOME;
224             }
225         } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
226             /*
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.
230              */
231             if (brp->basf.index >= 0) {
232                 avpriv_request_sample(s, "Multiple BASF streams");
233                 return AVERROR_PATCHWELCOME;
234             }
235
236             st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
237             st->codecpar->codec_id   = AV_CODEC_ID_ADPCM_ARGO;
238             brp->basf.index          = i;
239             ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf);
240
241             if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
242                 return ret;
243
244         } else {
245             av_assert0(0); /* Caught above, should never happen. */
246         }
247     }
248
249     /*
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.
253      */
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;
258         int64_t offset;
259
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);
262
263         if ((ret = avio_tell(s->pb)) < 0)
264             return ret;
265
266         offset = ret;
267
268         if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
269             return ret;
270         else if (ret != BRP_BLOCK_HEADER_SIZE)
271             return AVERROR(EIO);
272
273         blk.stream_id = AV_RL32(buf + 0);
274         blk.start_ms  = AV_RL32(buf + 4);
275         blk.size      = AV_RL32(buf + 8);
276
277         if (blk.stream_id != brp->basf.index) {
278             avpriv_request_sample(s, "first block not BASF");
279             return AVERROR_PATCHWELCOME;
280         }
281
282         if (blk.size < ASF_CHUNK_HEADER_SIZE)
283             return AVERROR_INVALIDDATA;
284
285         if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
286             return ret;
287         else if (ret != ASF_CHUNK_HEADER_SIZE)
288             return AVERROR(EIO);
289
290         ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
291
292         if ((ret = ff_argo_asf_fill_stream(st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0)
293             return ret;
294
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);
298
299         if ((ret = avio_seek(s->pb, offset, SEEK_SET)) < 0)
300             return ret;
301     }
302     return 0;
303 }
304
305 static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
306 {
307     ArgoBRPDemuxContext *brp = s->priv_data;
308     ArgoBRPBlockHeader blk;
309     const ArgoBRPStreamHeader *shdr;
310     AVStream *st;
311     uint8_t buf[BRP_MIN_BUFFER_SIZE];
312     ArgoASFChunkHeader ckhdr;
313     int ret;
314
315     if ((ret = avio_read(s->pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
316         return ret;
317     else if (ret != BRP_BLOCK_HEADER_SIZE)
318         return AVERROR(EIO);
319
320     blk.stream_id = AV_RL32(buf + 0);
321     blk.start_ms  = AV_RL32(buf + 4);
322     blk.size      = AV_RL32(buf + 8);
323
324     if (blk.stream_id == -1)
325         return AVERROR_EOF;
326
327     if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams)
328         return AVERROR_INVALIDDATA;
329
330     st = s->streams[blk.stream_id];
331     shdr = brp->streams + blk.stream_id;
332
333     if (blk.stream_id == brp->basf.index) {
334         if (blk.size < ASF_CHUNK_HEADER_SIZE)
335             return AVERROR_INVALIDDATA;
336
337         if ((ret = avio_read(s->pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
338             return ret;
339
340         ff_argo_asf_parse_chunk_header(&ckhdr, buf);
341
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;
348
349         blk.size -= ASF_CHUNK_HEADER_SIZE;
350
351         if (blk.size % st->codecpar->block_align != 0)
352             return AVERROR_INVALIDDATA;
353     }
354
355     if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0)
356         return ret;
357     else if (ret != blk.size)
358         return AVERROR_INVALIDDATA;
359
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;
366     } else {
367         pkt->pts      = blk.start_ms;
368     }
369
370     pkt->stream_index = blk.stream_id;
371     return 0;
372 }
373
374 AVInputFormat ff_argo_brp_demuxer = {
375     .name           = "argo_brp",
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,
381 };