FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/alp.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 63 88 71.6%
Functions: 5 6 83.3%
Branches: 27 50 54.0%

Line Branch Exec Source
1 /*
2 * LEGO Racers ALP (.tun & .pcm) (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 "libavutil/channel_layout.h"
26 #include "avformat.h"
27 #include "demux.h"
28 #include "internal.h"
29 #include "mux.h"
30 #include "rawenc.h"
31 #include "libavutil/intreadwrite.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/opt.h"
34
35 #define ALP_TAG MKTAG('A', 'L', 'P', ' ')
36 #define ALP_MAX_READ_SIZE 4096
37
38 typedef struct ALPHeader {
39 uint32_t magic; /*< Magic Number, {'A', 'L', 'P', ' '} */
40 uint32_t header_size; /*< Header size (after this). */
41 char adpcm[6]; /*< "ADPCM" */
42 uint8_t unk1; /*< Unknown */
43 uint8_t num_channels; /*< Channel Count. */
44 uint32_t sample_rate; /*< Sample rate, only if header_size >= 12. */
45 } ALPHeader;
46
47 typedef enum ALPType {
48 ALP_TYPE_AUTO = 0, /*< Autodetect based on file extension. */
49 ALP_TYPE_TUN = 1, /*< Force a .TUN file. */
50 ALP_TYPE_PCM = 2, /*< Force a .PCM file. */
51 } ALPType;
52
53 typedef struct ALPMuxContext {
54 const AVClass *class;
55 ALPType type;
56 } ALPMuxContext;
57
58 #if CONFIG_ALP_DEMUXER
59 7186 static int alp_probe(const AVProbeData *p)
60 {
61 uint32_t i;
62
63
2/2
✓ Branch 0 taken 7183 times.
✓ Branch 1 taken 3 times.
7186 if (AV_RL32(p->buf) != ALP_TAG)
64 7183 return 0;
65
66 /* Only allowed header sizes are 8 and 12. */
67 3 i = AV_RL32(p->buf + 4);
68
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
3 if (i != 8 && i != 12)
69 return 0;
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (strncmp("ADPCM", p->buf + 8, 6) != 0)
72 return 0;
73
74 3 return AVPROBE_SCORE_MAX - 1;
75 }
76
77 3 static int alp_read_header(AVFormatContext *s)
78 {
79 int ret;
80 AVStream *st;
81 3 ALPHeader *hdr = s->priv_data;
82 AVCodecParameters *par;
83
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((hdr->magic = avio_rl32(s->pb)) != ALP_TAG)
85 return AVERROR_INVALIDDATA;
86
87 3 hdr->header_size = avio_rl32(s->pb);
88
89
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
3 if (hdr->header_size != 8 && hdr->header_size != 12) {
90 return AVERROR_INVALIDDATA;
91 }
92
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if ((ret = avio_read(s->pb, hdr->adpcm, sizeof(hdr->adpcm))) < 0)
94 return ret;
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 else if (ret != sizeof(hdr->adpcm))
96 return AVERROR(EIO);
97
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (strncmp("ADPCM", hdr->adpcm, sizeof(hdr->adpcm)) != 0)
99 return AVERROR_INVALIDDATA;
100
101 3 hdr->unk1 = avio_r8(s->pb);
102 3 hdr->num_channels = avio_r8(s->pb);
103
104
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (hdr->header_size == 8) {
105 /* .TUN music file */
106 1 hdr->sample_rate = 22050;
107
108 } else {
109 /* .PCM sound file */
110 2 hdr->sample_rate = avio_rl32(s->pb);
111 }
112
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (hdr->sample_rate > 44100) {
114 avpriv_request_sample(s, "Sample Rate > 44100");
115 return AVERROR_PATCHWELCOME;
116 }
117
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!(st = avformat_new_stream(s, NULL)))
119 return AVERROR(ENOMEM);
120
121 3 par = st->codecpar;
122 3 par->codec_type = AVMEDIA_TYPE_AUDIO;
123 3 par->codec_id = AV_CODEC_ID_ADPCM_IMA_ALP;
124 3 par->format = AV_SAMPLE_FMT_S16;
125 3 par->sample_rate = hdr->sample_rate;
126
127
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 if (hdr->num_channels > 2 || hdr->num_channels == 0)
128 return AVERROR_INVALIDDATA;
129
130 3 av_channel_layout_default(&par->ch_layout, hdr->num_channels);
131 3 par->bits_per_coded_sample = 4;
132 3 par->block_align = 1;
133 3 par->bit_rate = par->ch_layout.nb_channels *
134 3 par->sample_rate *
135 3 par->bits_per_coded_sample;
136
137 3 avpriv_set_pts_info(st, 64, 1, par->sample_rate);
138 3 return 0;
139 }
140
141 90 static int alp_read_packet(AVFormatContext *s, AVPacket *pkt)
142 {
143 int ret;
144 90 AVCodecParameters *par = s->streams[0]->codecpar;
145
146
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 85 times.
90 if ((ret = av_get_packet(s->pb, pkt, ALP_MAX_READ_SIZE)) < 0)
147 5 return ret;
148
149 85 pkt->flags &= ~AV_PKT_FLAG_CORRUPT;
150 85 pkt->stream_index = 0;
151 85 pkt->duration = ret * 2 / par->ch_layout.nb_channels;
152
153 85 return 0;
154 }
155
156 static int alp_seek(AVFormatContext *s, int stream_index,
157 int64_t pts, int flags)
158 {
159 const ALPHeader *hdr = s->priv_data;
160
161 if (pts != 0)
162 return AVERROR(EINVAL);
163
164 return avio_seek(s->pb, hdr->header_size + 8, SEEK_SET);
165 }
166
167 const FFInputFormat ff_alp_demuxer = {
168 .p.name = "alp",
169 .p.long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
170 .priv_data_size = sizeof(ALPHeader),
171 .read_probe = alp_probe,
172 .read_header = alp_read_header,
173 .read_packet = alp_read_packet,
174 .read_seek = alp_seek,
175 };
176 #endif
177
178 #if CONFIG_ALP_MUXER
179
180 1 static int alp_write_init(AVFormatContext *s)
181 {
182 1 ALPMuxContext *alp = s->priv_data;
183 AVCodecParameters *par;
184
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (alp->type == ALP_TYPE_AUTO) {
186 if (av_match_ext(s->url, "pcm"))
187 alp->type = ALP_TYPE_PCM;
188 else
189 alp->type = ALP_TYPE_TUN;
190 }
191
192 1 par = s->streams[0]->codecpar;
193
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->ch_layout.nb_channels > 2) {
195 av_log(s, AV_LOG_ERROR, "A maximum of 2 channels are supported\n");
196 return AVERROR(EINVAL);
197 }
198
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (par->sample_rate > 44100) {
200 av_log(s, AV_LOG_ERROR, "Sample rate too large\n");
201 return AVERROR(EINVAL);
202 }
203
204
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (alp->type == ALP_TYPE_TUN && par->sample_rate != 22050) {
205 av_log(s, AV_LOG_ERROR, "Sample rate must be 22050 for TUN files\n");
206 return AVERROR(EINVAL);
207 }
208 1 return 0;
209 }
210
211 1 static int alp_write_header(AVFormatContext *s)
212 {
213 1 ALPMuxContext *alp = s->priv_data;
214 1 AVCodecParameters *par = s->streams[0]->codecpar;
215
216 1 avio_wl32(s->pb, ALP_TAG);
217
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 avio_wl32(s->pb, alp->type == ALP_TYPE_PCM ? 12 : 8);
218 1 avio_write(s->pb, "ADPCM", 6);
219 1 avio_w8(s->pb, 0);
220 1 avio_w8(s->pb, par->ch_layout.nb_channels);
221
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (alp->type == ALP_TYPE_PCM)
222 1 avio_wl32(s->pb, par->sample_rate);
223
224 1 return 0;
225 }
226
227 enum { AE = AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM };
228
229 static const AVOption alp_options[] = {
230 {
231 .name = "type",
232 .help = "set file type",
233 .offset = offsetof(ALPMuxContext, type),
234 .type = AV_OPT_TYPE_INT,
235 .default_val = {.i64 = ALP_TYPE_AUTO},
236 .min = ALP_TYPE_AUTO,
237 .max = ALP_TYPE_PCM,
238 .flags = AE,
239 .unit = "type",
240 },
241 {
242 .name = "auto",
243 .help = "autodetect based on file extension",
244 .offset = 0,
245 .type = AV_OPT_TYPE_CONST,
246 .default_val = {.i64 = ALP_TYPE_AUTO},
247 .min = 0,
248 .max = 0,
249 .flags = AE,
250 .unit = "type"
251 },
252 {
253 .name = "tun",
254 .help = "force .tun, used for music",
255 .offset = 0,
256 .type = AV_OPT_TYPE_CONST,
257 .default_val = {.i64 = ALP_TYPE_TUN},
258 .min = 0,
259 .max = 0,
260 .flags = AE,
261 .unit = "type"
262 },
263 {
264 .name = "pcm",
265 .help = "force .pcm, used for sfx",
266 .offset = 0,
267 .type = AV_OPT_TYPE_CONST,
268 .default_val = {.i64 = ALP_TYPE_PCM},
269 .min = 0,
270 .max = 0,
271 .flags = AE,
272 .unit = "type"
273 },
274 { NULL }
275 };
276
277 static const AVClass alp_muxer_class = {
278 .class_name = "alp",
279 .item_name = av_default_item_name,
280 .option = alp_options,
281 .version = LIBAVUTIL_VERSION_INT
282 };
283
284 const FFOutputFormat ff_alp_muxer = {
285 .p.name = "alp",
286 .p.long_name = NULL_IF_CONFIG_SMALL("LEGO Racers ALP"),
287 .p.extensions = "tun,pcm",
288 .p.audio_codec = AV_CODEC_ID_ADPCM_IMA_ALP,
289 .p.video_codec = AV_CODEC_ID_NONE,
290 .p.subtitle_codec = AV_CODEC_ID_NONE,
291 .p.priv_class = &alp_muxer_class,
292 .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
293 FF_OFMT_FLAG_ONLY_DEFAULT_CODECS,
294 .init = alp_write_init,
295 .write_header = alp_write_header,
296 .write_packet = ff_raw_write_packet,
297 .priv_data_size = sizeof(ALPMuxContext)
298 };
299 #endif
300