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