FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/apm.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 98 128 76.6%
Functions: 7 7 100.0%
Branches: 30 54 55.6%

Line Branch Exec Source
1 /*
2 * Rayman 2 APM (De)muxer
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 "config_components.h"
24
25 #include "avformat.h"
26 #include "internal.h"
27 #include "mux.h"
28 #include "rawenc.h"
29 #include "libavutil/channel_layout.h"
30 #include "libavutil/internal.h"
31 #include "libavutil/intreadwrite.h"
32
33 #define APM_FILE_HEADER_SIZE 20
34 #define APM_FILE_EXTRADATA_SIZE 80
35 #define APM_EXTRADATA_SIZE 28
36
37 #define APM_MAX_READ_SIZE 4096
38
39 #define APM_TAG_CODEC 0x2000
40 #define APM_TAG_VS12 MKTAG('v', 's', '1', '2')
41 #define APM_TAG_DATA MKTAG('D', 'A', 'T', 'A')
42
43 typedef struct APMState {
44 int32_t has_saved;
45 int32_t predictor_r;
46 int32_t step_index_r;
47 int32_t saved_r;
48 int32_t predictor_l;
49 int32_t step_index_l;
50 int32_t saved_l;
51 } APMState;
52
53 typedef struct APMExtraData {
54 uint32_t magic;
55 uint32_t file_size;
56 uint32_t data_size;
57 uint32_t unk1;
58 uint32_t unk2;
59 APMState state;
60 uint32_t unk3[7];
61 uint32_t data;
62 } APMExtraData;
63
64 #if CONFIG_APM_DEMUXER
65 3 static void apm_parse_extradata(APMExtraData *ext, const uint8_t *buf)
66 {
67 3 ext->magic = AV_RL32(buf + 0);
68 3 ext->file_size = AV_RL32(buf + 4);
69 3 ext->data_size = AV_RL32(buf + 8);
70 3 ext->unk1 = AV_RL32(buf + 12);
71 3 ext->unk2 = AV_RL32(buf + 16);
72
73 3 ext->state.has_saved = AV_RL32(buf + 20);
74 3 ext->state.predictor_r = AV_RL32(buf + 24);
75 3 ext->state.step_index_r = AV_RL32(buf + 28);
76 3 ext->state.saved_r = AV_RL32(buf + 32);
77 3 ext->state.predictor_l = AV_RL32(buf + 36);
78 3 ext->state.step_index_l = AV_RL32(buf + 40);
79 3 ext->state.saved_l = AV_RL32(buf + 44);
80
81
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 3 times.
24 for (int i = 0; i < FF_ARRAY_ELEMS(ext->unk3); i++)
82 21 ext->unk3[i] = AV_RL32(buf + 48 + (i * 4));
83
84 3 ext->data = AV_RL32(buf + 76);
85 3 }
86
87 7023 static int apm_probe(const AVProbeData *p)
88 {
89
2/2
✓ Branch 0 taken 7020 times.
✓ Branch 1 taken 3 times.
7023 if (AV_RL16(p->buf) != APM_TAG_CODEC)
90 7020 return 0;
91
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (p->buf_size < 100)
93 return 0;
94
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (AV_RL32(p->buf + 20) != APM_TAG_VS12)
96 return 0;
97
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (AV_RL32(p->buf + 96) != APM_TAG_DATA)
99 return 0;
100
101 3 return AVPROBE_SCORE_MAX - 1;
102 }
103
104 3 static int apm_read_header(AVFormatContext *s)
105 {
106 int64_t ret;
107 AVStream *st;
108 APMExtraData extradata;
109 AVCodecParameters *par;
110 uint8_t buf[APM_FILE_EXTRADATA_SIZE];
111 int channels;
112
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(st = avformat_new_stream(s, NULL)))
114 return AVERROR(ENOMEM);
115
116 /*
117 * This is 98% a WAVEFORMATEX, but there's something screwy with the extradata
118 * that ff_get_wav_header() can't (and shouldn't) handle properly.
119 */
120
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_rl16(s->pb) != APM_TAG_CODEC)
121 return AVERROR_INVALIDDATA;
122
123 3 par = st->codecpar;
124 3 channels = avio_rl16(s->pb);
125 3 par->sample_rate = avio_rl32(s->pb);
126
127 /* Skip the bitrate, it's usually wrong anyway. */
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = avio_skip(s->pb, 4)) < 0)
129 return ret;
130
131 3 par->block_align = avio_rl16(s->pb);
132 3 par->bits_per_coded_sample = avio_rl16(s->pb);
133
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (avio_rl32(s->pb) != APM_FILE_EXTRADATA_SIZE)
135 return AVERROR_INVALIDDATA;
136
137 /* 8 = bits per sample * max channels */
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (par->sample_rate > (INT_MAX / 8))
139 return AVERROR_INVALIDDATA;
140
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (par->bits_per_coded_sample != 4)
142 return AVERROR_INVALIDDATA;
143
144
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (channels > 2 || channels == 0)
145 return AVERROR_INVALIDDATA;
146
147 3 av_channel_layout_default(&par->ch_layout, channels);
148 3 par->codec_type = AVMEDIA_TYPE_AUDIO;
149 3 par->codec_id = AV_CODEC_ID_ADPCM_IMA_APM;
150 3 par->format = AV_SAMPLE_FMT_S16;
151 3 par->bit_rate = par->ch_layout.nb_channels *
152 3 (int64_t)par->sample_rate *
153 3 par->bits_per_coded_sample;
154
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = avio_read(s->pb, buf, APM_FILE_EXTRADATA_SIZE)) < 0)
156 return ret;
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 else if (ret != APM_FILE_EXTRADATA_SIZE)
158 return AVERROR(EIO);
159
160 3 apm_parse_extradata(&extradata, buf);
161
162
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (extradata.magic != APM_TAG_VS12 || extradata.data != APM_TAG_DATA)
163 return AVERROR_INVALIDDATA;
164
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (extradata.state.has_saved) {
166 avpriv_request_sample(s, "Saved Samples");
167 return AVERROR_PATCHWELCOME;
168 }
169
170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = ff_alloc_extradata(par, APM_EXTRADATA_SIZE)) < 0)
171 return ret;
172
173 /* Use the entire state as extradata. */
174 3 memcpy(par->extradata, buf + 20, APM_EXTRADATA_SIZE);
175
176 3 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
177 3 st->start_time = 0;
178 3 st->duration = extradata.data_size *
179 3 (8 / par->bits_per_coded_sample) /
180 3 par->ch_layout.nb_channels;
181 3 return 0;
182 }
183
184 92 static int apm_read_packet(AVFormatContext *s, AVPacket *pkt)
185 {
186 int ret;
187 92 AVCodecParameters *par = s->streams[0]->codecpar;
188
189 /*
190 * For future reference: if files with the `has_saved` field set ever
191 * surface, `saved_l`, and `saved_r` will each contain 8 "saved" samples
192 * that should be sent to the decoder before the actual data.
193 */
194
195
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 87 times.
92 if ((ret = av_get_packet(s->pb, pkt, APM_MAX_READ_SIZE)) < 0)
196 5 return ret;
197
198 87 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
199 87 pkt->stream_index = 0;
200 87 pkt->duration = ret * (8 / par->bits_per_coded_sample) / par->ch_layout.nb_channels;
201
202 87 return 0;
203 }
204
205 const AVInputFormat ff_apm_demuxer = {
206 .name = "apm",
207 .long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
208 .read_probe = apm_probe,
209 .read_header = apm_read_header,
210 .read_packet = apm_read_packet
211 };
212 #endif
213
214 #if CONFIG_APM_MUXER
215 1 static int apm_write_init(AVFormatContext *s)
216 {
217 AVCodecParameters *par;
218
219
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (s->nb_streams != 1) {
220 av_log(s, AV_LOG_ERROR, "APM files have exactly one stream\n");
221 return AVERROR(EINVAL);
222 }
223
224 1 par = s->streams[0]->codecpar;
225
226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->codec_id != AV_CODEC_ID_ADPCM_IMA_APM) {
227 av_log(s, AV_LOG_ERROR, "%s codec not supported\n",
228 avcodec_get_name(par->codec_id));
229 return AVERROR(EINVAL);
230 }
231
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->ch_layout.nb_channels > 2) {
233 av_log(s, AV_LOG_ERROR, "APM files only support up to 2 channels\n");
234 return AVERROR(EINVAL);
235 }
236
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->sample_rate > (INT_MAX / 8)) {
238 av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
239 return AVERROR(EINVAL);
240 }
241
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->extradata_size != APM_EXTRADATA_SIZE) {
243 av_log(s, AV_LOG_ERROR, "Invalid/missing extradata\n");
244 return AVERROR(EINVAL);
245 }
246
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!(s->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
248 av_log(s, AV_LOG_ERROR, "Stream not seekable, unable to write output file\n");
249 return AVERROR(EINVAL);
250 }
251
252 1 return 0;
253 }
254
255 1 static int apm_write_header(AVFormatContext *s)
256 {
257 1 uint8_t buf[APM_FILE_EXTRADATA_SIZE] = { 0 };
258 1 AVCodecParameters *par = s->streams[0]->codecpar;
259
260 /*
261 * Bodge a WAVEFORMATEX manually, ff_put_wav_header() can't
262 * be used because of the extra 2 bytes.
263 */
264 1 avio_wl16(s->pb, APM_TAG_CODEC);
265 1 avio_wl16(s->pb, par->ch_layout.nb_channels);
266 1 avio_wl32(s->pb, par->sample_rate);
267 /* This is the wrong calculation, but it's what the orginal files have. */
268 1 avio_wl32(s->pb, par->sample_rate * par->ch_layout.nb_channels * 2);
269 1 avio_wl16(s->pb, par->block_align);
270 1 avio_wl16(s->pb, par->bits_per_coded_sample);
271 1 avio_wl32(s->pb, APM_FILE_EXTRADATA_SIZE);
272
273 /*
274 * Build the extradata. Assume the codec's given us correct data.
275 * File and data sizes are fixed later.
276 */
277 1 AV_WL32(buf + 0, APM_TAG_VS12); /* magic */
278 1 AV_WL32(buf + 12, 0xFFFFFFFF); /* unk1 */
279 1 memcpy( buf + 20, par->extradata, APM_EXTRADATA_SIZE);
280 1 AV_WL32(buf + 76, APM_TAG_DATA); /* data */
281
282 1 avio_write(s->pb, buf, APM_FILE_EXTRADATA_SIZE);
283 1 return 0;
284 }
285
286 1 static int apm_write_trailer(AVFormatContext *s)
287 {
288 int64_t file_size, data_size;
289
290 1 file_size = avio_tell(s->pb);
291 1 data_size = file_size - (APM_FILE_HEADER_SIZE + APM_FILE_EXTRADATA_SIZE);
292
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (file_size >= UINT32_MAX) {
294 av_log(s, AV_LOG_ERROR,
295 "Filesize %"PRId64" invalid for APM, output file will be broken\n",
296 file_size);
297 return AVERROR(ERANGE);
298 }
299
300 1 avio_seek(s->pb, 24, SEEK_SET);
301 1 avio_wl32(s->pb, (uint32_t)file_size);
302 1 avio_wl32(s->pb, (uint32_t)data_size);
303
304 1 return 0;
305 }
306
307 const FFOutputFormat ff_apm_muxer = {
308 .p.name = "apm",
309 .p.long_name = NULL_IF_CONFIG_SMALL("Ubisoft Rayman 2 APM"),
310 .p.extensions = "apm",
311 .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_APM,
312 .p.video_codec = AV_CODEC_ID_NONE,
313 .init = apm_write_init,
314 .write_header = apm_write_header,
315 .write_packet = ff_raw_write_packet,
316 .write_trailer = apm_write_trailer
317 };
318 #endif
319