]> git.sesse.net Git - ffmpeg/blob - libavformat/apm.c
avformat/apm: fix variable/structure names and cosmetics
[ffmpeg] / libavformat / apm.c
1 /*
2  * Rayman 2 APM 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 #include "avformat.h"
23 #include "internal.h"
24 #include "libavutil/internal.h"
25 #include "libavutil/intreadwrite.h"
26
27 #define APM_FILE_HEADER_SIZE    18
28 #define APM_FILE_EXTRADATA_SIZE 80
29 #define APM_EXTRADATA_SIZE      28
30
31 #define APM_MAX_READ_SIZE       4096
32
33 #define APM_TAG_CODEC           0x2000
34 #define APM_TAG_VS12            MKTAG('v', 's', '1', '2')
35 #define APM_TAG_DATA            MKTAG('D', 'A', 'T', 'A')
36
37 typedef struct APMState {
38     int32_t     has_saved;
39     int32_t     predictor_r;
40     int32_t     step_index_r;
41     int32_t     saved_r;
42     int32_t     predictor_l;
43     int32_t     step_index_l;
44     int32_t     saved_l;
45 } APMState;
46
47 typedef struct APMExtraData {
48     uint32_t    magic;
49     uint32_t    file_size;
50     uint32_t    data_size;
51     uint32_t    unk1;
52     uint32_t    unk2;
53     APMState    state;
54     uint32_t    unk3[7];
55     uint32_t    data;
56 } APMExtraData;
57
58 static void apm_parse_extradata(APMExtraData *ext, const uint8_t *buf)
59 {
60     ext->magic              = AV_RL32(buf + 0);
61     ext->file_size          = AV_RL32(buf + 4);
62     ext->data_size          = AV_RL32(buf + 8);
63     ext->unk1               = AV_RL32(buf + 12);
64     ext->unk2               = AV_RL32(buf + 16);
65
66     ext->state.has_saved    = AV_RL32(buf + 20);
67     ext->state.predictor_r  = AV_RL32(buf + 24);
68     ext->state.step_index_r = AV_RL32(buf + 28);
69     ext->state.saved_r      = AV_RL32(buf + 32);
70     ext->state.predictor_l  = AV_RL32(buf + 36);
71     ext->state.step_index_l = AV_RL32(buf + 40);
72     ext->state.saved_l      = AV_RL32(buf + 44);
73
74     for (int i = 0; i < FF_ARRAY_ELEMS(ext->unk3); i++)
75         ext->unk3[i]        = AV_RL32(buf + 48 + (i * 4));
76
77     ext->data               = AV_RL32(buf + 76);
78 }
79
80 static int apm_probe(const AVProbeData *p)
81 {
82     if (AV_RL16(p->buf) != APM_TAG_CODEC)
83         return 0;
84
85     if (p->buf_size < 100)
86         return 0;
87
88     if (AV_RL32(p->buf + 20) != APM_TAG_VS12)
89         return 0;
90
91     if (AV_RL32(p->buf + 96) != APM_TAG_DATA)
92         return 0;
93
94     return AVPROBE_SCORE_MAX - 1;
95 }
96
97 static int apm_read_header(AVFormatContext *s)
98 {
99     int64_t ret;
100     AVStream *st;
101     APMExtraData extradata;
102     AVCodecParameters *par;
103     uint8_t buf[APM_FILE_EXTRADATA_SIZE];
104
105     if (!(st = avformat_new_stream(s, NULL)))
106         return AVERROR(ENOMEM);
107
108     /*
109      * This is 98% a WAVEFORMATEX, but there's something screwy with the extradata
110      * that ff_get_wav_header() can't (and shouldn't) handle properly.
111      */
112     if (avio_rl16(s->pb) != APM_TAG_CODEC)
113         return AVERROR_INVALIDDATA;
114
115     par = st->codecpar;
116     par->channels              = avio_rl16(s->pb);
117     par->sample_rate           = avio_rl32(s->pb);
118
119     /* Skip the bitrate, it's usually wrong anyway. */
120     if ((ret = avio_skip(s->pb, 4)) < 0)
121         return ret;
122
123     par->block_align           = avio_rl16(s->pb);
124     par->bits_per_coded_sample = avio_rl16(s->pb);
125
126     if (avio_rl32(s->pb) != APM_FILE_EXTRADATA_SIZE)
127         return AVERROR_INVALIDDATA;
128
129     /* I've never seen files greater than this. */
130     if (par->sample_rate > 44100)
131         return AVERROR_INVALIDDATA;
132
133     if (par->bits_per_coded_sample != 4)
134         return AVERROR_INVALIDDATA;
135
136     if (par->channels == 2)
137         par->channel_layout    = AV_CH_LAYOUT_STEREO;
138     else if (par->channels == 1)
139         par->channel_layout    = AV_CH_LAYOUT_MONO;
140     else
141         return AVERROR_INVALIDDATA;
142
143     par->codec_type            = AVMEDIA_TYPE_AUDIO;
144     par->codec_id              = AV_CODEC_ID_ADPCM_IMA_APM;
145     par->format                = AV_SAMPLE_FMT_S16;
146     par->bits_per_raw_sample   = 16;
147     par->bit_rate              = par->channels *
148                                  par->sample_rate *
149                                  par->bits_per_coded_sample;
150
151     if ((ret = avio_read(s->pb, buf, APM_FILE_EXTRADATA_SIZE)) < 0)
152         return ret;
153     else if (ret != APM_FILE_EXTRADATA_SIZE)
154         return AVERROR(EIO);
155
156     apm_parse_extradata(&extradata, buf);
157
158     if (extradata.magic != APM_TAG_VS12 || extradata.data != APM_TAG_DATA)
159         return AVERROR_INVALIDDATA;
160
161     if (extradata.state.has_saved) {
162         avpriv_request_sample(s, "Saved Samples");
163         return AVERROR_PATCHWELCOME;
164     }
165
166     if ((ret = ff_alloc_extradata(par, APM_EXTRADATA_SIZE)) < 0)
167         return ret;
168
169     /* Use the entire state as extradata. */
170     memcpy(par->extradata, buf + 20, APM_EXTRADATA_SIZE);
171
172     avpriv_set_pts_info(st, 64, 1, par->sample_rate);
173     st->start_time  = 0;
174     st->duration    = extradata.data_size *
175                       (8 / par->bits_per_coded_sample) /
176                       par->channels;
177     return 0;
178 }
179
180 static int apm_read_packet(AVFormatContext *s, AVPacket *pkt)
181 {
182     int ret;
183     AVCodecParameters *par = s->streams[0]->codecpar;
184
185     /*
186      * For future reference: if files with the `has_saved` field set ever
187      * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples
188      * that should be sent to the decoder before the actual data.
189      */
190
191     if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0)
192         return ret;
193
194     pkt->flags          &= ~AV_PKT_FLAG_CORRUPT;
195     pkt->stream_index   = 0;
196     pkt->duration       = ret * (8 / par->bits_per_coded_sample) / par->channels;
197
198     return 0;
199 }
200
201 AVInputFormat ff_apm_demuxer = {
202     .name           = "apm",
203     .long_name      = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
204     .read_probe     = apm_probe,
205     .read_header    = apm_read_header,
206     .read_packet    = apm_read_packet
207 };