FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/lafdec.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 3 152 2.0%
Functions: 1 4 25.0%
Branches: 1 86 1.2%

Line Branch Exec Source
1 /*
2 * Limitless Audio Format demuxer
3 * Copyright (c) 2022 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "libavutil/intreadwrite.h"
23 #include "avformat.h"
24 #include "internal.h"
25
26 #define MAX_STREAMS 4096
27
28 typedef struct StreamParams {
29 AVChannelLayout layout;
30 float horizontal;
31 float vertical;
32 int lfe;
33 int stored;
34 } StreamParams;
35
36 typedef struct LAFContext {
37 uint8_t *data;
38 unsigned nb_stored;
39 unsigned stored_index;
40 unsigned index;
41 unsigned bpp;
42
43 StreamParams p[MAX_STREAMS];
44
45 int header_len;
46 uint8_t header[(MAX_STREAMS + 7) / 8];
47 } LAFContext;
48
49 6782 static int laf_probe(const AVProbeData *p)
50 {
51
1/2
✓ Branch 0 taken 6782 times.
✗ Branch 1 not taken.
6782 if (memcmp(p->buf, "LIMITLESS", 9))
52 6782 return 0;
53 if (memcmp(p->buf + 9, "HEAD", 4))
54 return 0;
55 return AVPROBE_SCORE_MAX;
56 }
57
58 static int laf_read_header(AVFormatContext *ctx)
59 {
60 LAFContext *s = ctx->priv_data;
61 AVIOContext *pb = ctx->pb;
62 unsigned st_count, mode;
63 unsigned sample_rate;
64 int64_t duration;
65 int codec_id;
66 int quality;
67 int bpp;
68
69 avio_skip(pb, 9);
70 if (avio_rb32(pb) != MKBETAG('H','E','A','D'))
71 return AVERROR_INVALIDDATA;
72
73 quality = avio_r8(pb);
74 if (quality > 3)
75 return AVERROR_INVALIDDATA;
76 mode = avio_r8(pb);
77 if (mode > 1)
78 return AVERROR_INVALIDDATA;
79 st_count = avio_rl32(pb);
80 if (st_count == 0 || st_count > MAX_STREAMS)
81 return AVERROR_INVALIDDATA;
82
83 for (int i = 0; i < st_count; i++) {
84 StreamParams *stp = &s->p[i];
85
86 stp->vertical = av_int2float(avio_rl32(pb));
87 stp->horizontal = av_int2float(avio_rl32(pb));
88 stp->lfe = avio_r8(pb);
89 if (stp->lfe) {
90 stp->layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MASK(1, (AV_CH_LOW_FREQUENCY));
91 } else if (stp->vertical == 0.f &&
92 stp->horizontal == 0.f) {
93 stp->layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MASK(1, (AV_CH_FRONT_CENTER));
94 } else if (stp->vertical == 0.f &&
95 stp->horizontal == -30.f) {
96 stp->layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MASK(1, (AV_CH_FRONT_LEFT));
97 } else if (stp->vertical == 0.f &&
98 stp->horizontal == 30.f) {
99 stp->layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MASK(1, (AV_CH_FRONT_RIGHT));
100 } else if (stp->vertical == 0.f &&
101 stp->horizontal == -110.f) {
102 stp->layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MASK(1, (AV_CH_SIDE_LEFT));
103 } else if (stp->vertical == 0.f &&
104 stp->horizontal == 110.f) {
105 stp->layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MASK(1, (AV_CH_SIDE_RIGHT));
106 } else {
107 stp->layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
108 }
109 }
110
111 sample_rate = avio_rl32(pb);
112 duration = avio_rl64(pb) / st_count;
113
114 switch (quality) {
115 case 0:
116 codec_id = AV_CODEC_ID_PCM_U8;
117 bpp = 1;
118 break;
119 case 1:
120 codec_id = AV_CODEC_ID_PCM_S16LE;
121 bpp = 2;
122 break;
123 case 2:
124 codec_id = AV_CODEC_ID_PCM_F32LE;
125 bpp = 4;
126 break;
127 case 3:
128 codec_id = AV_CODEC_ID_PCM_S24LE;
129 bpp = 3;
130 break;
131 }
132
133 s->index = 0;
134 s->stored_index = 0;
135 s->bpp = bpp;
136 if ((int64_t)bpp * st_count * (int64_t)sample_rate >= INT32_MAX)
137 return AVERROR_INVALIDDATA;
138 s->data = av_calloc(st_count * sample_rate, bpp);
139 if (!s->data)
140 return AVERROR(ENOMEM);
141
142 for (int st = 0; st < st_count; st++) {
143 StreamParams *stp = &s->p[st];
144 AVCodecParameters *par;
145 AVStream *st = avformat_new_stream(ctx, NULL);
146 if (!st)
147 return AVERROR(ENOMEM);
148
149 par = st->codecpar;
150 par->codec_id = codec_id;
151 par->codec_type = AVMEDIA_TYPE_AUDIO;
152 par->ch_layout.nb_channels = 1;
153 par->ch_layout = stp->layout;
154 par->sample_rate = sample_rate;
155 st->duration = duration;
156
157 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
158 }
159
160 s->header_len = (ctx->nb_streams + 7) / 8;
161
162 return 0;
163 }
164
165 static int laf_read_packet(AVFormatContext *ctx, AVPacket *pkt)
166 {
167 AVIOContext *pb = ctx->pb;
168 LAFContext *s = ctx->priv_data;
169 AVStream *st = ctx->streams[0];
170 const int bpp = s->bpp;
171 StreamParams *stp;
172 int64_t pos;
173 int ret;
174
175 pos = avio_tell(pb);
176
177 again:
178 if (avio_feof(pb))
179 return AVERROR_EOF;
180
181 if (s->index >= ctx->nb_streams) {
182 int cur_st = 0, st_count = 0, st_index = 0;
183
184 avio_read(pb, s->header, s->header_len);
185 for (int i = 0; i < s->header_len; i++) {
186 uint8_t val = s->header[i];
187
188 for (int j = 0; j < 8 && cur_st < ctx->nb_streams; j++, cur_st++) {
189 StreamParams *stp = &s->p[st_index];
190
191 stp->stored = 0;
192 if (val & 1) {
193 stp->stored = 1;
194 st_count++;
195 }
196 val >>= 1;
197 st_index++;
198 }
199 }
200
201 s->index = s->stored_index = 0;
202 s->nb_stored = st_count;
203 if (!st_count)
204 return AVERROR_INVALIDDATA;
205 ret = avio_read(pb, s->data, st_count * st->codecpar->sample_rate * bpp);
206 if (ret < 0)
207 return ret;
208 }
209
210 st = ctx->streams[s->index];
211 stp = &s->p[s->index];
212 while (!stp->stored) {
213 s->index++;
214 if (s->index >= ctx->nb_streams)
215 goto again;
216 stp = &s->p[s->index];
217 }
218 st = ctx->streams[s->index];
219
220 ret = av_new_packet(pkt, st->codecpar->sample_rate * bpp);
221 if (ret < 0)
222 return ret;
223
224 switch (bpp) {
225 case 1:
226 for (int n = 0; n < st->codecpar->sample_rate; n++)
227 pkt->data[n] = s->data[n * s->nb_stored + s->stored_index];
228 break;
229 case 2:
230 for (int n = 0; n < st->codecpar->sample_rate; n++)
231 AV_WN16(pkt->data + n * 2, AV_RN16(s->data + n * s->nb_stored * 2 + s->stored_index * 2));
232 break;
233 case 3:
234 for (int n = 0; n < st->codecpar->sample_rate; n++)
235 AV_WL24(pkt->data + n * 3, AV_RL24(s->data + n * s->nb_stored * 3 + s->stored_index * 3));
236 break;
237 case 4:
238 for (int n = 0; n < st->codecpar->sample_rate; n++)
239 AV_WN32(pkt->data + n * 4, AV_RN32(s->data + n * s->nb_stored * 4 + s->stored_index * 4));
240 break;
241 }
242
243 pkt->stream_index = s->index;
244 pkt->pos = pos;
245 s->index++;
246 s->stored_index++;
247
248 return 0;
249 }
250
251 static int laf_read_seek(AVFormatContext *ctx, int stream_index,
252 int64_t timestamp, int flags)
253 {
254 LAFContext *s = ctx->priv_data;
255
256 s->stored_index = s->index = s->nb_stored = 0;
257
258 return -1;
259 }
260
261 const AVInputFormat ff_laf_demuxer = {
262 .name = "laf",
263 .long_name = NULL_IF_CONFIG_SMALL("LAF (Limitless Audio Format)"),
264 .priv_data_size = sizeof(LAFContext),
265 .read_probe = laf_probe,
266 .read_header = laf_read_header,
267 .read_packet = laf_read_packet,
268 .read_seek = laf_read_seek,
269 .extensions = "laf",
270 .flags = AVFMT_GENERIC_INDEX,
271 };
272