]> git.sesse.net Git - ffmpeg/blob - libavformat/argo_brp.c
avformat/argo_brp: remove an allocation
[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     /* To know how much of a BASF to give. */
82     int64_t             lastpts;
83     int                 hit_eof;
84
85     /* BASF-specific fields. */
86     struct {
87         int                 index;
88         ArgoASFChunkHeader  ckhdr;
89         int64_t             blocks_read;
90         int64_t             offset;
91         /* ms, not samples. */
92         int64_t             lastpts;
93     } basf;
94 } ArgoBRPDemuxContext;
95
96 static int argo_brp_probe(const AVProbeData *p)
97 {
98     if (AV_RL32(p->buf) != BRP_TAG)
99         return 0;
100
101     return AVPROBE_SCORE_EXTENSION + 1;
102 }
103
104 static int read_extradata(AVFormatContext *s, const ArgoBRPStreamHeader *hdr,
105                           void *buf, size_t bufsz)
106 {
107     const char *name;
108     uint32_t size;
109     int64_t ret;
110
111     if (hdr->codec_id == BRP_CODEC_ID_BVID) {
112         name = "BVID";
113         size = BVID_HEADER_SIZE;
114     } else if (hdr->codec_id == BRP_CODEC_ID_BASF) {
115         name = "BASF";
116         size = ASF_FILE_HEADER_SIZE;
117     } else {
118         avpriv_request_sample(s, "BRP codec id 0x%x", hdr->codec_id);
119
120         if ((ret = avio_skip(s->pb, hdr->extradata_size)) < 0)
121             return ret;
122
123         return 1;
124     }
125
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;
130     }
131
132     av_assert0(bufsz >= size);
133
134     if ((ret = avio_read(s->pb, buf, size)) < 0)
135         return ret;
136
137     if (ret != size)
138         return AVERROR(EIO);
139
140     return 0;
141 }
142
143 static int argo_brp_read_header(AVFormatContext *s)
144 {
145     int64_t ret;
146     AVIOContext *pb = s->pb;
147     ArgoBRPDemuxContext *brp = s->priv_data;
148     uint8_t buf[FFMAX(BRP_MIN_BUFFER_SIZE, ASF_MIN_BUFFER_SIZE)];
149
150     if ((ret = avio_read(pb, buf, BRP_FILE_HEADER_SIZE)) < 0)
151         return ret;
152     else if (ret != BRP_FILE_HEADER_SIZE)
153         return AVERROR(EIO);
154
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);
158
159     if (brp->fhdr.magic != BRP_TAG)
160         return AVERROR_INVALIDDATA;
161
162     if (brp->fhdr.num_streams > BRP_MAX_STREAMS) {
163         avpriv_request_sample(s, ">%d streams", BRP_MAX_STREAMS);
164         return AVERROR_PATCHWELCOME;
165     }
166
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;
171         AVStream *st;
172
173         if (!(st = avformat_new_stream(s, NULL))) {
174             ret = AVERROR(ENOMEM);
175             goto fail;
176         }
177
178         if ((ret = avio_read(pb, buf, BRP_STREAM_HEADER_SIZE)) < 0) {
179             goto fail;
180         } else if (ret != BRP_STREAM_HEADER_SIZE) {
181             ret = AVERROR(EIO);
182             goto fail;
183         }
184
185         hdr->codec_id       = AV_RL32(buf + 0);
186         hdr->id             = AV_RL32(buf + 4);
187         hdr->duration_ms    = AV_RL32(buf + 8);
188         hdr->byte_rate      = AV_RL32(buf + 12);
189         hdr->extradata_size = AV_RL32(buf + 16);
190
191         /* Timestamps are in milliseconds. */
192         avpriv_set_pts_info(st, 64, 1, 1000);
193         st->duration           = hdr->duration_ms;
194         st->codecpar->bit_rate = hdr->byte_rate * 8;
195
196         if ((ret = read_extradata(s, hdr, buf, sizeof(buf))) < 0) {
197             goto fail;
198         } else if (ret > 0) {
199             st->codecpar->codec_type = AVMEDIA_TYPE_UNKNOWN;
200             continue;
201         }
202
203         if (hdr->codec_id == BRP_CODEC_ID_BVID) {
204             ArgoBVIDHeader *bvid = &hdr->extradata.bvid;
205
206             st->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
207
208             /* No decoder for this yet. */
209             st->codecpar->codec_id   = AV_CODEC_ID_NONE;
210
211             bvid->num_frames = AV_RL32(buf +  0);
212             bvid->width      = AV_RL32(buf +  4);
213             bvid->height     = AV_RL32(buf +  8);
214             bvid->depth      = AV_RL32(buf + 12);
215
216             /* These are from 1990's games, sanity check this. */
217             if (bvid->width >= 65536 || bvid->height >= 65536 ||
218                 bvid->depth > 24     || bvid->depth % 8 != 0) {
219                 ret = AVERROR_INVALIDDATA;
220                 goto fail;
221             }
222
223             st->codecpar->width  = bvid->width;
224             st->codecpar->height = bvid->height;
225
226             if (bvid->depth == 8) {
227                 st->codecpar->format = AV_PIX_FMT_PAL8;
228             } else if (bvid->depth == 24) {
229                 st->codecpar->format = AV_PIX_FMT_RGB24;
230             } else {
231                 avpriv_request_sample(s, "depth == %u", bvid->depth);
232                 ret = AVERROR_PATCHWELCOME;
233                 goto fail;
234             }
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                 ret = AVERROR_PATCHWELCOME;
244                 goto fail;
245             }
246
247             st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
248             st->codecpar->codec_id   = AV_CODEC_ID_ADPCM_ARGO;
249             brp->basf.index          = i;
250             ff_argo_asf_parse_file_header(&hdr->extradata.basf, buf);
251
252             if ((ret = ff_argo_asf_validate_file_header(s, &hdr->extradata.basf)) < 0)
253                 goto fail;
254
255         } else {
256             av_assert0(0); /* Caught above, should never happen. */
257         }
258     }
259
260     /*
261      * This is nasty. BASF streams only have one (huge) block.
262      * It should be the first one. It contains the chunk header, so
263      * it needs to be read here.
264      */
265     if (brp->basf.index >= 0) {
266         AVStream *st = s->streams[brp->basf.index];
267         ArgoBRPStreamHeader *hdr = brp->streams + brp->basf.index;
268         ArgoBRPBlockHeader blk;
269
270         av_assert0(st->codecpar->codec_id == AV_CODEC_ID_ADPCM_ARGO);
271         av_assert0(brp->streams[brp->basf.index].extradata_size == ASF_FILE_HEADER_SIZE);
272
273         if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
274             goto fail;
275         else if (ret != BRP_BLOCK_HEADER_SIZE) {
276             ret = AVERROR(EIO);
277             goto fail;
278         }
279
280         blk.stream_id = AV_RL32(buf + 0);
281         blk.start_ms  = AV_RL32(buf + 4);
282         blk.size      = AV_RL32(buf + 8);
283
284         if (blk.stream_id != brp->basf.index) {
285             avpriv_request_sample(st, "first block not BASF");
286             ret = AVERROR_PATCHWELCOME;
287             goto fail;
288         }
289
290         if (blk.size < ASF_CHUNK_HEADER_SIZE) {
291             ret = AVERROR_INVALIDDATA;
292             goto fail;
293         }
294
295         if ((ret = avio_read(pb, buf, ASF_CHUNK_HEADER_SIZE)) < 0)
296             goto fail;
297         else if (ret != ASF_CHUNK_HEADER_SIZE) {
298             ret = AVERROR(EIO);
299             goto fail;
300         }
301
302         ff_argo_asf_parse_chunk_header(&brp->basf.ckhdr, buf);
303
304         if ((ret = ff_argo_asf_fill_stream(st, &hdr->extradata.basf, &brp->basf.ckhdr)) < 0)
305             goto fail;
306
307         /* Convert ms to samples. */
308         st->start_time = (blk.start_ms * st->codecpar->sample_rate) / 1000;
309
310         if ((ret = avio_tell(s->pb)) < 0)
311             goto fail;
312
313         brp->basf.offset = ret;
314
315         if ((ret = avio_skip(s->pb, blk.size - ASF_CHUNK_HEADER_SIZE)) < 0)
316             goto fail;
317     }
318     return 0;
319
320 fail:
321     return ret;
322 }
323
324 static int argo_brp_read_basf(AVFormatContext *s, AVPacket *pkt,
325                               ArgoBRPDemuxContext *brp, int ignorepts)
326 {
327     ArgoASFChunkHeader *ckhdr = &brp->basf.ckhdr;
328     AVCodecParameters *par;
329     int64_t ret, old;
330
331     if (brp->basf.index < 0)
332         return 0;
333
334     par = s->streams[brp->basf.index]->codecpar;
335
336     if (brp->basf.blocks_read >= ckhdr->num_blocks)
337         return 0;
338
339     if (!ignorepts && brp->lastpts < brp->basf.lastpts)
340         return 0;
341
342     if ((ret = avio_tell(s->pb)) < 0)
343         return ret;
344
345     old = ret;
346
347     if ((ret = avio_seek(s->pb, brp->basf.offset, SEEK_SET)) < 0)
348         return ret;
349     else if (ret != brp->basf.offset)
350         return AVERROR(EIO);
351
352     if ((ret = av_get_packet(s->pb, pkt, par->block_align)) < 0)
353         return ret;
354
355     if ((ret = avio_seek(s->pb, old, SEEK_SET)) < 0)
356         return ret;
357     else if (ret != old)
358         return AVERROR(EIO);
359
360     pkt->stream_index      = brp->basf.index;
361     pkt->duration          = ckhdr->num_samples;
362
363     brp->basf.offset      += pkt->size;
364     brp->basf.blocks_read += 1;
365     /* Need the ceil() because ((32 * 1000) / 44100) < 1 */
366     brp->basf.lastpts     += ceilf((ckhdr->num_samples * 1000.0f) / ckhdr->sample_rate);
367     return 1;
368 }
369
370 static int argo_brp_read_packet(AVFormatContext *s, AVPacket *pkt)
371 {
372     ArgoBRPDemuxContext *brp = s->priv_data;
373     ArgoBRPBlockHeader blk;
374     AVIOContext *pb = s->pb;
375     uint8_t buf[BRP_BLOCK_HEADER_SIZE];
376     int ret;
377
378     /*
379      * Special-case: send some more BASF content if we're running behind.
380      * Grr, why couldn't they just interleave it.
381      */
382     if ((ret = argo_brp_read_basf(s, pkt, brp, brp->hit_eof)) < 0)
383         return ret;
384     else if (ret > 0)
385         return 0;
386
387     if (brp->hit_eof)
388         return AVERROR_EOF;
389
390     if ((ret = avio_read(pb, buf, BRP_BLOCK_HEADER_SIZE)) < 0)
391         return ret;
392     else if (ret != BRP_BLOCK_HEADER_SIZE)
393         return AVERROR(EIO);
394
395     blk.stream_id = AV_RL32(buf + 0);
396     blk.start_ms  = AV_RL32(buf + 4);
397     blk.size      = AV_RL32(buf + 8);
398
399     /* This is meant to be EOF, but there might be more BASF packets. */
400     if (blk.stream_id == -1) {
401         brp->hit_eof = 1;
402         /* This is nasty, but safe. */
403         return argo_brp_read_packet(s, pkt);
404     }
405
406     if (blk.stream_id < -1 || blk.stream_id >= s->nb_streams)
407         return AVERROR_INVALIDDATA;
408
409     /* Skip BASF blocks. */
410     if (blk.stream_id == brp->basf.index) {
411         if ((ret = avio_skip(s->pb, blk.size)) < 0)
412             return ret;
413     } else {
414         if ((ret = av_get_packet(s->pb, pkt, blk.size)) < 0)
415             return ret;
416         else if (ret != blk.size)
417             return AVERROR_INVALIDDATA;
418     }
419
420     pkt->stream_index = blk.stream_id;
421     pkt->pts          = blk.start_ms;
422     brp->lastpts      = FFMAX(brp->lastpts, blk.start_ms);
423     return 0;
424 }
425
426 AVInputFormat ff_argo_brp_demuxer = {
427     .name           = "argo_brp",
428     .long_name      = NULL_IF_CONFIG_SMALL("Argonaut Games BRP"),
429     .priv_data_size = sizeof(ArgoBRPDemuxContext),
430     .read_probe     = argo_brp_probe,
431     .read_header    = argo_brp_read_header,
432     .read_packet    = argo_brp_read_packet,
433 };