FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/sbgdec.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 246 864 28.5%
Functions: 27 48 56.2%
Branches: 147 638 23.0%

Line Branch Exec Source
1 /*
2 * SBG (SBaGen) file format decoder
3 * Copyright (c) 2011 Nicolas George
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 <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 #include "libavutil/bprint.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/intreadwrite.h"
28 #include "libavutil/log.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/time_internal.h"
31 #include "avformat.h"
32 #include "internal.h"
33
34 #define SBG_SCALE (1 << 16)
35 #define DAY (24 * 60 * 60)
36 #define DAY_TS ((int64_t)DAY * AV_TIME_BASE)
37
38 struct sbg_demuxer {
39 AVClass *class;
40 int sample_rate;
41 int frame_size;
42 int max_file_size;
43 };
44
45 struct sbg_string {
46 char *s;
47 char *e;
48 };
49
50 enum sbg_fade_type {
51 SBG_FADE_SILENCE = 0,
52 SBG_FADE_SAME = 1,
53 SBG_FADE_ADAPT = 3,
54 };
55
56 struct sbg_fade {
57 int8_t in, out, slide;
58 };
59
60 enum sbg_synth_type {
61 SBG_TYPE_NONE,
62 SBG_TYPE_SINE,
63 SBG_TYPE_NOISE,
64 SBG_TYPE_BELL,
65 SBG_TYPE_MIX,
66 SBG_TYPE_SPIN,
67 };
68
69 /* bell: freq constant, ampl decreases exponentially, can be approx lin */
70
71 struct sbg_timestamp {
72 int64_t t;
73 char type; /* 0 for relative, 'N' for now, 'T' for absolute */
74 };
75
76 struct sbg_script_definition {
77 char *name;
78 int name_len;
79 int elements, nb_elements;
80 char type; /* 'S' or 'B' */
81 };
82
83 struct sbg_script_synth {
84 int carrier;
85 int beat;
86 int vol;
87 enum sbg_synth_type type;
88 struct {
89 int l, r;
90 } ref;
91 };
92
93 struct sbg_script_tseq {
94 struct sbg_timestamp ts;
95 char *name;
96 int name_len;
97 int lock;
98 struct sbg_fade fade;
99 };
100
101 struct sbg_script_event {
102 int64_t ts;
103 int64_t ts_int, ts_trans, ts_next;
104 int elements, nb_elements;
105 struct sbg_fade fade;
106 };
107
108 struct sbg_script {
109 struct sbg_script_definition *def;
110 struct sbg_script_synth *synth;
111 struct sbg_script_tseq *tseq;
112 struct sbg_script_tseq *block_tseq;
113 struct sbg_script_event *events;
114 int nb_def;
115 int nb_tseq;
116 int nb_events;
117 int nb_synth;
118 int64_t start_ts;
119 int64_t end_ts;
120 int64_t opt_fade_time;
121 int64_t opt_duration;
122 char *opt_mix;
123 int sample_rate;
124 uint8_t opt_start_at_first;
125 uint8_t opt_end_at_last;
126 };
127
128 struct sbg_parser {
129 void *log;
130 char *script, *end;
131 char *cursor;
132 struct sbg_script scs;
133 struct sbg_timestamp current_time;
134 int nb_block_tseq;
135 int nb_def_max, nb_synth_max, nb_tseq_max, nb_block_tseq_max;
136 int line_no;
137 char err_msg[128];
138 };
139
140 enum ws_interval_type {
141 WS_SINE = MKTAG('S','I','N','E'),
142 WS_NOISE = MKTAG('N','O','I','S'),
143 };
144
145 struct ws_interval {
146 int64_t ts1, ts2;
147 enum ws_interval_type type;
148 uint32_t channels;
149 int32_t f1, f2;
150 int32_t a1, a2;
151 uint32_t phi;
152 };
153
154 struct ws_intervals {
155 struct ws_interval *inter;
156 int nb_inter;
157 int max_inter;
158 };
159
160 5 static void *alloc_array_elem(void **array, size_t elsize,
161 int *size, int *max_size)
162 {
163 void *ret;
164
165
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (*size == *max_size) {
166 5 int m = FFMAX(32, FFMIN(*max_size, INT_MAX / 2) * 2);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (*size >= m)
168 return NULL;
169 5 *array = av_realloc_f(*array, m, elsize);
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (!*array)
171 return NULL;
172 5 *max_size = m;
173 }
174 5 ret = (char *)*array + elsize * *size;
175 5 memset(ret, 0, elsize);
176 5 (*size)++;
177 5 return ret;
178 }
179
180 7023 static int str_to_time(const char *str, int64_t *rtime)
181 {
182 7023 const char *cur = str;
183 char *end;
184 int hours, minutes;
185 7023 double seconds = 0;
186 7023 int64_t ts = 0;
187
188
4/4
✓ Branch 0 taken 1843 times.
✓ Branch 1 taken 5180 times.
✓ Branch 2 taken 1780 times.
✓ Branch 3 taken 63 times.
7023 if (*cur < '0' || *cur > '9')
189 6960 return 0;
190 63 hours = strtol(cur, &end, 10);
191
5/8
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 59 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
63 if (end == cur || *end != ':' || end[1] < '0' || end[1] > '9')
192 59 return 0;
193 4 cur = end + 1;
194 4 minutes = strtol(cur, &end, 10);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (end == cur)
196 return 0;
197 4 cur = end;
198
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (*end == ':'){
199 4 seconds = strtod(cur + 1, &end);
200
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (end > cur + 1)
201 4 cur = end;
202 4 ts = av_clipd(seconds * AV_TIME_BASE, INT64_MIN/2, INT64_MAX/2);
203 }
204 4 *rtime = av_sat_add64((hours * 3600LL + minutes * 60LL) * AV_TIME_BASE, ts);
205 4 return cur - str;
206 }
207
208 7167 static inline int is_space(char c)
209 {
210
6/6
✓ Branch 0 taken 7160 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 7158 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 7157 times.
7167 return c == ' ' || c == '\t' || c == '\r';
211 }
212
213 static inline int scale_double(void *log, double d, double m, int *r)
214 {
215 m *= d * SBG_SCALE;
216 if (m < INT_MIN || m >= INT_MAX) {
217 if (log)
218 av_log(log, AV_LOG_ERROR, "%g is too large\n", d);
219 return AVERROR(EDOM);
220 }
221 *r = m;
222 return 0;
223 }
224
225 7150 static int lex_space(struct sbg_parser *p)
226 {
227 7150 char *c = p->cursor;
228
229
3/4
✓ Branch 0 taken 7159 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 7150 times.
7159 while (p->cursor < p->end && is_space(*p->cursor))
230 9 p->cursor++;
231 7150 return p->cursor > c;
232 }
233
234 8830 static int lex_char(struct sbg_parser *p, char c)
235 {
236
3/4
✓ Branch 0 taken 8830 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 8817 times.
8830 int r = p->cursor < p->end && *p->cursor == c;
237
238 8830 p->cursor += r;
239 8830 return r;
240 }
241
242 4 static int lex_double(struct sbg_parser *p, double *r)
243 {
244 double d;
245 char *end;
246
247
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
4 if (p->cursor == p->end || is_space(*p->cursor) || *p->cursor == '\n')
248 return 0;
249 4 d = strtod(p->cursor, &end);
250
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (end > p->cursor) {
251 2 *r = d;
252 2 p->cursor = end;
253 2 return 1;
254 }
255 2 return 0;
256 }
257
258 7032 static int lex_fixed(struct sbg_parser *p, const char *t, int l)
259 {
260
2/4
✓ Branch 0 taken 7032 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7032 times.
✗ Branch 3 not taken.
7032 if (p->end - p->cursor < l || memcmp(p->cursor, t, l))
261 7032 return 0;
262 p->cursor += l;
263 return 1;
264 }
265
266 14145 static int lex_line_end(struct sbg_parser *p)
267 {
268
3/4
✓ Branch 0 taken 14145 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 103 times.
✓ Branch 3 taken 14042 times.
14145 if (p->cursor < p->end && *p->cursor == '#') {
269 103 p->cursor++;
270
3/4
✓ Branch 0 taken 11220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11117 times.
✓ Branch 3 taken 103 times.
11220 while (p->cursor < p->end && *p->cursor != '\n')
271 11117 p->cursor++;
272 }
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14145 times.
14145 if (p->cursor == p->end)
274 /* simulate final LF for files lacking it */
275 return 1;
276
2/2
✓ Branch 0 taken 14039 times.
✓ Branch 1 taken 106 times.
14145 if (*p->cursor != '\n')
277 14039 return 0;
278 106 p->cursor++;
279 106 p->line_no++;
280 106 lex_space(p);
281 106 return 1;
282 }
283
284 1 static int lex_wsword(struct sbg_parser *p, struct sbg_string *rs)
285 {
286 1 char *s = p->cursor, *c = s;
287
288
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (s == p->end || *s == '\n')
289 return 0;
290
4/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 1 times.
4 while (c < p->end && *c != '\n' && !is_space(*c))
291 3 c++;
292 1 rs->s = s;
293 1 rs->e = p->cursor = c;
294 1 lex_space(p);
295 1 return 1;
296 }
297
298 7026 static int lex_name(struct sbg_parser *p, struct sbg_string *rs)
299 {
300 7026 char *s = p->cursor, *c = s;
301
302
9/10
✓ Branch 0 taken 14088 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1082 times.
✓ Branch 3 taken 13006 times.
✓ Branch 4 taken 984 times.
✓ Branch 5 taken 98 times.
✓ Branch 6 taken 6018 times.
✓ Branch 7 taken 7086 times.
✓ Branch 8 taken 5788 times.
✓ Branch 9 taken 230 times.
14088 while (c < p->end && ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')
303
8/8
✓ Branch 0 taken 593 times.
✓ Branch 1 taken 6723 times.
✓ Branch 2 taken 247 times.
✓ Branch 3 taken 346 times.
✓ Branch 4 taken 28 times.
✓ Branch 5 taken 7041 times.
✓ Branch 6 taken 15 times.
✓ Branch 7 taken 7026 times.
7316 || (*c >= '0' && *c <= '9') || *c == '_' || *c == '-'))
304 7062 c++;
305
2/2
✓ Branch 0 taken 5230 times.
✓ Branch 1 taken 1796 times.
7026 if (c == s)
306 5230 return 0;
307 1796 rs->s = s;
308 1796 rs->e = p->cursor = c;
309 1796 return 1;
310 }
311
312 7023 static int lex_time(struct sbg_parser *p, int64_t *rt)
313 {
314 7023 int r = str_to_time(p->cursor, rt);
315 7023 p->cursor += r;
316 7023 return r > 0;
317 }
318
319 #define FORWARD_ERROR(c) \
320 do { \
321 int errcode = c; \
322 if (errcode <= 0) \
323 return errcode ? errcode : AVERROR_INVALIDDATA; \
324 } while (0)
325
326 static int parse_immediate(struct sbg_parser *p)
327 {
328 snprintf(p->err_msg, sizeof(p->err_msg),
329 "immediate sequences not yet implemented");
330 return AVERROR_PATCHWELCOME;
331 }
332
333 static int parse_preprogrammed(struct sbg_parser *p)
334 {
335 snprintf(p->err_msg, sizeof(p->err_msg),
336 "preprogrammed sequences not yet implemented");
337 return AVERROR_PATCHWELCOME;
338 }
339
340 static int parse_optarg(struct sbg_parser *p, char o, struct sbg_string *r)
341 {
342 if (!lex_wsword(p, r)) {
343 snprintf(p->err_msg, sizeof(p->err_msg),
344 "option '%c' requires an argument", o);
345 return AVERROR_INVALIDDATA;
346 }
347 return 1;
348 }
349
350 7128 static int parse_options(struct sbg_parser *p)
351 {
352 struct sbg_string ostr, oarg;
353 7128 char mode = 0;
354 int r;
355 char *tptr;
356 double v;
357
358
3/4
✓ Branch 0 taken 7128 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7127 times.
✓ Branch 3 taken 1 times.
7128 if (p->cursor == p->end || *p->cursor != '-')
359 7127 return 0;
360
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 while (lex_char(p, '-') && lex_wsword(p, &ostr)) {
361
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (; ostr.s < ostr.e; ostr.s++) {
362 1 char opt = *ostr.s;
363
1/11
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
1 switch (opt) {
364 case 'S':
365 p->scs.opt_start_at_first = 1;
366 break;
367 case 'E':
368 p->scs.opt_end_at_last = 1;
369 break;
370 case 'i':
371 mode = 'i';
372 break;
373 case 'p':
374 mode = 'p';
375 break;
376 case 'F':
377 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
378 v = strtod(oarg.s, &tptr);
379 if (oarg.e != tptr) {
380 snprintf(p->err_msg, sizeof(p->err_msg),
381 "syntax error for option -F");
382 return AVERROR_INVALIDDATA;
383 }
384 p->scs.opt_fade_time = v * AV_TIME_BASE / 1000;
385 break;
386 case 'L':
387 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
388 r = str_to_time(oarg.s, &p->scs.opt_duration);
389 if (oarg.e != oarg.s + r) {
390 snprintf(p->err_msg, sizeof(p->err_msg),
391 "syntax error for option -L");
392 return AVERROR_INVALIDDATA;
393 }
394 break;
395 case 'T':
396 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
397 r = str_to_time(oarg.s, &p->scs.start_ts);
398 if (oarg.e != oarg.s + r) {
399 snprintf(p->err_msg, sizeof(p->err_msg),
400 "syntax error for option -T");
401 return AVERROR_INVALIDDATA;
402 }
403 break;
404 case 'm':
405 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
406 tptr = av_malloc(oarg.e - oarg.s + 1);
407 if (!tptr)
408 return AVERROR(ENOMEM);
409 memcpy(tptr, oarg.s, oarg.e - oarg.s);
410 tptr[oarg.e - oarg.s] = 0;
411 av_free(p->scs.opt_mix);
412 p->scs.opt_mix = tptr;
413 break;
414 case 'q':
415 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
416 v = strtod(oarg.s, &tptr);
417 if (oarg.e != tptr) {
418 snprintf(p->err_msg, sizeof(p->err_msg),
419 "syntax error for option -q");
420 return AVERROR_INVALIDDATA;
421 }
422 if (v != 1) {
423 snprintf(p->err_msg, sizeof(p->err_msg),
424 "speed factor other than 1 not supported");
425 return AVERROR_PATCHWELCOME;
426 }
427 break;
428 case 'r':
429 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
430 r = strtol(oarg.s, &tptr, 10);
431 if (oarg.e != tptr) {
432 snprintf(p->err_msg, sizeof(p->err_msg),
433 "syntax error for option -r");
434 return AVERROR_INVALIDDATA;
435 }
436 if (r < 40) {
437 snprintf(p->err_msg, sizeof(p->err_msg),
438 "invalid sample rate");
439 return AVERROR_PATCHWELCOME;
440 }
441 p->scs.sample_rate = r;
442 break;
443 1 default:
444 1 snprintf(p->err_msg, sizeof(p->err_msg),
445 1 "unknown option: '%c'", *ostr.s);
446 1 return AVERROR_INVALIDDATA;
447 }
448 }
449 }
450 switch (mode) {
451 case 'i':
452 return parse_immediate(p);
453 case 'p':
454 return parse_preprogrammed(p);
455 case 0:
456 if (!lex_line_end(p))
457 return AVERROR_INVALIDDATA;
458 return 1;
459 }
460 return AVERROR_BUG;
461 }
462
463 7021 static int parse_timestamp(struct sbg_parser *p,
464 struct sbg_timestamp *rts, int64_t *rrel)
465 {
466 7021 int64_t abs = 0, rel = 0, dt;
467 7021 char type = 0;
468 int r;
469
470
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7021 times.
7021 if (lex_fixed(p, "NOW", 3)) {
471 type = 'N';
472 r = 1;
473 } else {
474 7021 r = lex_time(p, &abs);
475
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7017 times.
7021 if (r)
476 4 type = 'T';
477 }
478
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 7019 times.
7021 while (lex_char(p, '+')) {
479
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!lex_time(p, &dt))
480 2 return AVERROR_INVALIDDATA;
481 if (av_sat_add64(rel, dt) - dt != rel)
482 return AVERROR_INVALIDDATA;
483 rel += dt;
484 r = 1;
485 }
486
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7015 times.
7019 if (r) {
487
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
4 if (!lex_space(p))
488 1 return AVERROR_INVALIDDATA;
489 3 rts->type = type;
490 3 rts->t = abs;
491 3 *rrel = rel;
492 }
493 7018 return r;
494 }
495
496 3 static int parse_fade(struct sbg_parser *p, struct sbg_fade *fr)
497 {
498 3 struct sbg_fade f = {0};
499
500
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (lex_char(p, '<'))
501 f.in = SBG_FADE_SILENCE;
502
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 else if (lex_char(p, '-'))
503 f.in = SBG_FADE_SAME;
504
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 else if (lex_char(p, '='))
505 f.in = SBG_FADE_ADAPT;
506 else
507 3 return 0;
508 if (lex_char(p, '>'))
509 f.out = SBG_FADE_SILENCE;
510 else if (lex_char(p, '-'))
511 f.out = SBG_FADE_SAME;
512 else if (lex_char(p, '='))
513 f.out = SBG_FADE_ADAPT;
514 else
515 return AVERROR_INVALIDDATA;
516 *fr = f;
517 return 1;
518 }
519
520 7021 static int parse_time_sequence(struct sbg_parser *p, int inblock)
521 {
522 struct sbg_timestamp ts;
523 int64_t rel_ts;
524 int r;
525 7021 struct sbg_fade fade = { SBG_FADE_SAME, SBG_FADE_SAME, 0 };
526 struct sbg_string name;
527 struct sbg_script_tseq *tseq;
528
529 7021 r = parse_timestamp(p, &ts, &rel_ts);
530
2/2
✓ Branch 0 taken 7015 times.
✓ Branch 1 taken 6 times.
7021 if (!r)
531 7015 return 0;
532
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (r < 0)
533 3 return r;
534
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ts.type) {
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (inblock)
536 return AVERROR_INVALIDDATA;
537 3 p->current_time.type = ts.type;
538 3 p->current_time.t = ts.t;
539 } else if(!inblock && !p->current_time.type) {
540 snprintf(p->err_msg, sizeof(p->err_msg),
541 "relative time without previous absolute time");
542 return AVERROR_INVALIDDATA;
543 }
544 3 ts.type = p->current_time.type;
545
546
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (av_sat_add64(p->current_time.t, rel_ts) != p->current_time.t + (uint64_t)rel_ts)
547 return AVERROR_INVALIDDATA;
548 3 ts.t = p->current_time.t + rel_ts;
549 3 r = parse_fade(p, &fade);
550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (r < 0)
551 return r;
552 3 lex_space(p);
553
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!lex_name(p, &name))
554 return AVERROR_INVALIDDATA;
555 3 lex_space(p);
556
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (lex_fixed(p, "->", 2)) {
557 fade.slide = SBG_FADE_ADAPT;
558 lex_space(p);
559 }
560
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 if (!lex_line_end(p))
561 2 return AVERROR_INVALIDDATA;
562
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 tseq = inblock ?
563 alloc_array_elem((void **)&p->scs.block_tseq, sizeof(*tseq),
564 &p->nb_block_tseq, &p->nb_block_tseq_max) :
565 1 alloc_array_elem((void **)&p->scs.tseq, sizeof(*tseq),
566 &p->scs.nb_tseq, &p->nb_tseq_max);
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!tseq)
568 return AVERROR(ENOMEM);
569 1 tseq->ts = ts;
570 1 tseq->name = name.s;
571 1 tseq->name_len = name.e - name.s;
572 1 tseq->fade = fade;
573 1 return 1;
574 }
575
576 static int parse_wave_def(struct sbg_parser *p, int wavenum)
577 {
578 snprintf(p->err_msg, sizeof(p->err_msg),
579 "waveform definitions not yet implemented");
580 return AVERROR_PATCHWELCOME;
581 }
582
583 static int parse_block_def(struct sbg_parser *p,
584 struct sbg_script_definition *def)
585 {
586 int r, tseq;
587
588 lex_space(p);
589 if (!lex_line_end(p))
590 return AVERROR_INVALIDDATA;
591 tseq = p->nb_block_tseq;
592 while (1) {
593 r = parse_time_sequence(p, 1);
594 if (r < 0)
595 return r;
596 if (!r)
597 break;
598 }
599 if (!lex_char(p, '}'))
600 return AVERROR_INVALIDDATA;
601 lex_space(p);
602 if (!lex_line_end(p))
603 return AVERROR_INVALIDDATA;
604 def->type = 'B';
605 def->elements = tseq;
606 def->nb_elements = p->nb_block_tseq - tseq;
607 if (!def->nb_elements)
608 return AVERROR_INVALIDDATA;
609 return 1;
610 }
611
612 2 static int parse_volume(struct sbg_parser *p, int *vol)
613 {
614 double v;
615
616
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!lex_char(p, '/'))
617 2 return 0;
618 if (!lex_double(p, &v))
619 return AVERROR_INVALIDDATA;
620 if (scale_double(p->log, v, 0.01, vol))
621 return AVERROR(ERANGE);
622 return 1;
623 }
624
625 2 static int parse_synth_channel_sine(struct sbg_parser *p,
626 struct sbg_script_synth *synth)
627 {
628 double carrierf, beatf;
629 int carrier, beat, vol;
630
631
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (!lex_double(p, &carrierf))
632 return 0;
633
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!lex_double(p, &beatf))
634 2 beatf = 0;
635
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 FORWARD_ERROR(parse_volume(p, &vol));
636 if (scale_double(p->log, carrierf, 1, &carrier) < 0 ||
637 scale_double(p->log, beatf, 1, &beat) < 0)
638 return AVERROR(EDOM);
639 synth->type = SBG_TYPE_SINE;
640 synth->carrier = carrier;
641 synth->beat = beat;
642 synth->vol = vol;
643 return 1;
644 }
645
646 2 static int parse_synth_channel_pink(struct sbg_parser *p,
647 struct sbg_script_synth *synth)
648 {
649 int vol;
650
651
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!lex_fixed(p, "pink", 4))
652 2 return 0;
653 FORWARD_ERROR(parse_volume(p, &vol));
654 synth->type = SBG_TYPE_NOISE;
655 synth->vol = vol;
656 return 1;
657 }
658
659 2 static int parse_synth_channel_bell(struct sbg_parser *p,
660 struct sbg_script_synth *synth)
661 {
662 double carrierf;
663 int carrier, vol;
664
665
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!lex_fixed(p, "bell", 4))
666 2 return 0;
667 if (!lex_double(p, &carrierf))
668 return AVERROR_INVALIDDATA;
669 FORWARD_ERROR(parse_volume(p, &vol));
670 if (scale_double(p->log, carrierf, 1, &carrier) < 0)
671 return AVERROR(EDOM);
672 synth->type = SBG_TYPE_BELL;
673 synth->carrier = carrier;
674 synth->vol = vol;
675 return 1;
676 }
677
678 2 static int parse_synth_channel_mix(struct sbg_parser *p,
679 struct sbg_script_synth *synth)
680 {
681 int vol;
682
683
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!lex_fixed(p, "mix", 3))
684 2 return 0;
685 FORWARD_ERROR(parse_volume(p, &vol));
686 synth->type = SBG_TYPE_MIX;
687 synth->vol = vol;
688 return 1;
689 }
690
691 2 static int parse_synth_channel_spin(struct sbg_parser *p,
692 struct sbg_script_synth *synth)
693 {
694 double carrierf, beatf;
695 int carrier, beat, vol;
696
697
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (!lex_fixed(p, "spin:", 5))
698 2 return 0;
699 if (!lex_double(p, &carrierf))
700 return AVERROR_INVALIDDATA;
701 if (!lex_double(p, &beatf))
702 return AVERROR_INVALIDDATA;
703 FORWARD_ERROR(parse_volume(p, &vol));
704 if (scale_double(p->log, carrierf, 1, &carrier) < 0 ||
705 scale_double(p->log, beatf, 1, &beat) < 0)
706 return AVERROR(EDOM);
707 synth->type = SBG_TYPE_SPIN;
708 synth->carrier = carrier;
709 synth->beat = beat;
710 synth->vol = vol;
711 return 1;
712 }
713
714 2 static int parse_synth_channel(struct sbg_parser *p)
715 {
716 int r;
717 struct sbg_script_synth *synth;
718
719 2 synth = alloc_array_elem((void **)&p->scs.synth, sizeof(*synth),
720 &p->scs.nb_synth, &p->nb_synth_max);
721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!synth)
722 return AVERROR(ENOMEM);
723 2 r = lex_char(p, '-');
724
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!r)
725 2 r = parse_synth_channel_pink(p, synth);
726
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!r)
727 2 r = parse_synth_channel_bell(p, synth);
728
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!r)
729 2 r = parse_synth_channel_mix(p, synth);
730
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!r)
731 2 r = parse_synth_channel_spin(p, synth);
732 /* Unimplemented: wave%d:%f%f/vol (carrier, beat) */
733
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!r)
734 2 r = parse_synth_channel_sine(p, synth);
735
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (r <= 0)
736 2 p->scs.nb_synth--;
737 2 return r;
738 }
739
740 2 static int parse_synth_def(struct sbg_parser *p,
741 struct sbg_script_definition *def)
742 {
743 int r, synth;
744
745 2 synth = p->scs.nb_synth;
746 while (1) {
747 2 r = parse_synth_channel(p);
748
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (r < 0)
749 2 return r;
750 if (!r || !lex_space(p))
751 break;
752 }
753 lex_space(p);
754 if (synth == p->scs.nb_synth)
755 return AVERROR_INVALIDDATA;
756 if (!lex_line_end(p))
757 return AVERROR_INVALIDDATA;
758 def->type = 'S';
759 def->elements = synth;
760 def->nb_elements = p->scs.nb_synth - synth;
761 return 1;
762 }
763
764 7023 static int parse_named_def(struct sbg_parser *p)
765 {
766 7023 char *cursor_save = p->cursor;
767 struct sbg_string name;
768 struct sbg_script_definition *def;
769
770
6/6
✓ Branch 1 taken 1793 times.
✓ Branch 2 taken 5230 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 1783 times.
✓ Branch 7 taken 8 times.
✓ Branch 8 taken 2 times.
7023 if (!lex_name(p, &name) || !lex_char(p, ':') || !lex_space(p)) {
771 7021 p->cursor = cursor_save;
772 7021 return 0;
773 }
774
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (name.e - name.s == 6 && !memcmp(name.s, "wave", 4) &&
775 name.s[4] >= '0' && name.s[4] <= '9' &&
776 name.s[5] >= '0' && name.s[5] <= '9') {
777 int wavenum = (name.s[4] - '0') * 10 + (name.s[5] - '0');
778 return parse_wave_def(p, wavenum);
779 }
780 2 def = alloc_array_elem((void **)&p->scs.def, sizeof(*def),
781 &p->scs.nb_def, &p->nb_def_max);
782
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!def)
783 return AVERROR(ENOMEM);
784 2 def->name = name.s;
785 2 def->name_len = name.e - name.s;
786
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (lex_char(p, '{'))
787 return parse_block_def(p, def);
788 2 return parse_synth_def(p, def);
789 }
790
791 14046 static void free_script(struct sbg_script *s)
792 {
793 14046 av_freep(&s->def);
794 14046 av_freep(&s->synth);
795 14046 av_freep(&s->tseq);
796 14046 av_freep(&s->block_tseq);
797 14046 av_freep(&s->events);
798 14046 av_freep(&s->opt_mix);
799 14046 }
800
801 7023 static int parse_script(void *log, char *script, int script_len,
802 struct sbg_script *rscript)
803 {
804 7023 struct sbg_parser sp = {
805 .log = log,
806 .script = script,
807 7023 .end = script + script_len,
808 .cursor = script,
809 .line_no = 1,
810 .err_msg = "",
811 .scs = {
812 /* default values */
813 .start_ts = AV_NOPTS_VALUE,
814 .sample_rate = 44100,
815 .opt_fade_time = 60 * AV_TIME_BASE,
816 },
817 };
818 int r;
819
820 7023 lex_space(&sp);
821
1/2
✓ Branch 0 taken 7128 times.
✗ Branch 1 not taken.
14151 while (sp.cursor < sp.end) {
822 7128 r = parse_options(&sp);
823
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7127 times.
7128 if (r < 0)
824 1 goto fail;
825
3/4
✓ Branch 0 taken 7127 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 7022 times.
✓ Branch 4 taken 105 times.
7127 if (!r && !lex_line_end(&sp))
826 7022 break;
827 }
828
1/2
✓ Branch 0 taken 7023 times.
✗ Branch 1 not taken.
7023 while (sp.cursor < sp.end) {
829 7023 r = parse_named_def(&sp);
830
2/2
✓ Branch 0 taken 7021 times.
✓ Branch 1 taken 2 times.
7023 if (!r)
831 7021 r = parse_time_sequence(&sp, 0);
832
2/2
✓ Branch 0 taken 7015 times.
✓ Branch 1 taken 8 times.
7023 if (!r)
833
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7015 times.
7015 r = lex_line_end(&sp) ? 1 : AVERROR_INVALIDDATA;
834
2/2
✓ Branch 0 taken 7022 times.
✓ Branch 1 taken 1 times.
7023 if (r < 0)
835 7022 goto fail;
836 }
837 *rscript = sp.scs;
838 return 1;
839 7023 fail:
840 7023 free_script(&sp.scs);
841
2/2
✓ Branch 0 taken 7022 times.
✓ Branch 1 taken 1 times.
7023 if (!*sp.err_msg)
842
1/2
✓ Branch 0 taken 7022 times.
✗ Branch 1 not taken.
7022 if (r == AVERROR_INVALIDDATA)
843 7022 snprintf(sp.err_msg, sizeof(sp.err_msg), "syntax error");
844
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7023 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7023 if (log && *sp.err_msg) {
845 const char *ctx = sp.cursor;
846 const char *ectx = av_x_if_null(memchr(ctx, '\n', sp.end - sp.cursor),
847 sp.end);
848 int lctx = ectx - ctx;
849 const char *quote = "\"";
850 if (lctx > 0 && ctx[lctx - 1] == '\r')
851 lctx--;
852 if (lctx == 0) {
853 ctx = "the end of line";
854 lctx = strlen(ctx);
855 quote = "";
856 }
857 av_log(log, AV_LOG_ERROR, "Error line %d: %s near %s%.*s%s.\n",
858 sp.line_no, sp.err_msg, quote, lctx, ctx, quote);
859 }
860 7023 return r;
861 }
862
863 static int read_whole_file(AVIOContext *io, int max_size, AVBPrint *rbuf)
864 {
865 int ret = avio_read_to_bprint(io, rbuf, max_size);
866 if (ret < 0)
867 return ret;
868 if (!av_bprint_is_complete(rbuf))
869 return AVERROR(ENOMEM);
870 /* Check if we have read the whole file. AVIOContext.eof_reached is only
871 * set after a read failed due to EOF, so this check is incorrect in case
872 * max_size equals the actual file size, but checking for that would
873 * require attempting to read beyond max_size. */
874 if (!io->eof_reached)
875 return AVERROR(EFBIG);
876 return 0;
877 }
878
879 static int expand_timestamps(void *log, struct sbg_script *s)
880 {
881 int i, nb_rel = 0;
882 int64_t now, cur_ts, delta = 0;
883
884 for (i = 0; i < s->nb_tseq; i++)
885 nb_rel += s->tseq[i].ts.type == 'N';
886 if (nb_rel == s->nb_tseq) {
887 /* All ts are relative to NOW: consider NOW = 0 */
888 now = 0;
889 if (s->start_ts != AV_NOPTS_VALUE)
890 av_log(log, AV_LOG_WARNING,
891 "Start time ignored in a purely relative script.\n");
892 } else if (nb_rel == 0 && s->start_ts != AV_NOPTS_VALUE ||
893 s->opt_start_at_first) {
894 /* All ts are absolute and start time is specified */
895 if (s->start_ts == AV_NOPTS_VALUE)
896 s->start_ts = s->tseq[0].ts.t;
897 now = s->start_ts;
898 } else {
899 /* Mixed relative/absolute ts: expand */
900 time_t now0;
901 struct tm *tm, tmpbuf;
902
903 av_log(log, AV_LOG_WARNING,
904 "Scripts with mixed absolute and relative timestamps can give "
905 "unexpected results (pause, seeking, time zone change).\n");
906 #undef time
907 time(&now0);
908 tm = localtime_r(&now0, &tmpbuf);
909 now = tm ? tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec :
910 now0 % DAY;
911 av_log(log, AV_LOG_INFO, "Using %02d:%02d:%02d as NOW.\n",
912 (int)(now / 3600), (int)(now / 60) % 60, (int)now % 60);
913 now *= AV_TIME_BASE;
914 for (i = 0; i < s->nb_tseq; i++) {
915 if (s->tseq[i].ts.type == 'N') {
916 s->tseq[i].ts.t += now;
917 s->tseq[i].ts.type = 'T'; /* not necessary */
918 }
919 }
920 }
921 if (s->start_ts == AV_NOPTS_VALUE)
922 s->start_ts = (s->opt_start_at_first && s->tseq) ? s->tseq[0].ts.t : now;
923 if (s->start_ts > INT64_MAX - s->opt_duration)
924 return AVERROR_INVALIDDATA;
925
926 s->end_ts = s->opt_duration ? s->start_ts + s->opt_duration :
927 AV_NOPTS_VALUE; /* may be overridden later by -E option */
928 cur_ts = now;
929 for (i = 0; i < s->nb_tseq; i++) {
930 if (av_sat_add64(s->tseq[i].ts.t, delta) != s->tseq[i].ts.t + (uint64_t)delta)
931 return AVERROR_INVALIDDATA;
932 if (s->tseq[i].ts.t + delta < cur_ts)
933 delta += DAY_TS;
934 cur_ts = s->tseq[i].ts.t += delta;
935 }
936 return 0;
937 }
938
939 static int expand_tseq(void *log, struct sbg_script *s, int *nb_ev_max,
940 int64_t t0, struct sbg_script_tseq *tseq)
941 {
942 int i, r;
943 struct sbg_script_definition *def;
944 struct sbg_script_tseq *be;
945 struct sbg_script_event *ev;
946
947 if (tseq->lock++) {
948 av_log(log, AV_LOG_ERROR, "Recursion loop on \"%.*s\"\n",
949 tseq->name_len, tseq->name);
950 return AVERROR(EINVAL);
951 }
952 if (t0 + (uint64_t)tseq->ts.t != av_sat_add64(t0, tseq->ts.t))
953 return AVERROR(EINVAL);
954
955 t0 += tseq->ts.t;
956 for (i = 0; i < s->nb_def; i++) {
957 if (s->def[i].name_len == tseq->name_len &&
958 !memcmp(s->def[i].name, tseq->name, tseq->name_len))
959 break;
960 }
961 if (i >= s->nb_def) {
962 av_log(log, AV_LOG_ERROR, "Tone-set \"%.*s\" not defined\n",
963 tseq->name_len, tseq->name);
964 return AVERROR(EINVAL);
965 }
966 def = &s->def[i];
967 if (def->type == 'B') {
968 be = s->block_tseq + def->elements;
969 for (i = 0; i < def->nb_elements; i++) {
970 r = expand_tseq(log, s, nb_ev_max, t0, &be[i]);
971 if (r < 0)
972 return r;
973 }
974 } else {
975 ev = alloc_array_elem((void **)&s->events, sizeof(*ev),
976 &s->nb_events, nb_ev_max);
977 if (!ev)
978 return AVERROR(ENOMEM);
979 ev->ts = tseq->ts.t;
980 ev->elements = def->elements;
981 ev->nb_elements = def->nb_elements;
982 ev->fade = tseq->fade;
983 }
984 tseq->lock--;
985 return 0;
986 }
987
988 static int expand_script(void *log, struct sbg_script *s)
989 {
990 int i, r, nb_events_max = 0;
991
992 r = expand_timestamps(log, s);
993 if (r < 0)
994 return r;
995 for (i = 0; i < s->nb_tseq; i++) {
996 r = expand_tseq(log, s, &nb_events_max, 0, &s->tseq[i]);
997 if (r < 0)
998 return r;
999 }
1000 if (!s->nb_events) {
1001 av_log(log, AV_LOG_ERROR, "No events in script\n");
1002 return AVERROR_INVALIDDATA;
1003 }
1004 if (s->opt_end_at_last)
1005 s->end_ts = s->events[s->nb_events - 1].ts;
1006 return 0;
1007 }
1008
1009 static int add_interval(struct ws_intervals *inter,
1010 enum ws_interval_type type, uint32_t channels, int ref,
1011 int64_t ts1, int32_t f1, int32_t a1,
1012 int64_t ts2, int32_t f2, int32_t a2)
1013 {
1014 struct ws_interval *i, *ri;
1015
1016 if (ref >= 0) {
1017 ri = &inter->inter[ref];
1018 /* ref and new intervals are constant, identical and adjacent */
1019 if (ri->type == type && ri->channels == channels &&
1020 ri->f1 == ri->f2 && ri->f2 == f1 && f1 == f2 &&
1021 ri->a1 == ri->a2 && ri->a2 == a1 && a1 == a2 &&
1022 ri->ts2 == ts1) {
1023 ri->ts2 = ts2;
1024 return ref;
1025 }
1026 }
1027 i = alloc_array_elem((void **)&inter->inter, sizeof(*i),
1028 &inter->nb_inter, &inter->max_inter);
1029 if (!i)
1030 return AVERROR(ENOMEM);
1031 i->ts1 = ts1;
1032 i->ts2 = ts2;
1033 i->type = type;
1034 i->channels = channels;
1035 i->f1 = f1;
1036 i->f2 = f2;
1037 i->a1 = a1;
1038 i->a2 = a2;
1039 i->phi = ref >= 0 ? ref | 0x80000000 : 0;
1040 return i - inter->inter;
1041 }
1042
1043 static int add_bell(struct ws_intervals *inter, struct sbg_script *s,
1044 int64_t ts1, int64_t ts2, int32_t f, int32_t a)
1045 {
1046 /* SBaGen uses an exponential decrease every 50ms.
1047 We approximate it with piecewise affine segments. */
1048 int32_t cpoints[][2] = {
1049 { 2, a },
1050 { 4, a - a / 4 },
1051 { 8, a / 2 },
1052 { 16, a / 4 },
1053 { 25, a / 10 },
1054 { 50, a / 80 },
1055 { 75, 0 },
1056 };
1057 int i, r;
1058 int64_t dt = s->sample_rate / 20, ts3 = ts1, ts4;
1059 for (i = 0; i < FF_ARRAY_ELEMS(cpoints); i++) {
1060 ts4 = FFMIN(ts2, ts1 + cpoints[i][0] * dt);
1061 r = add_interval(inter, WS_SINE, 3, -1,
1062 ts3, f, a, ts4, f, cpoints[i][1]);
1063 if (r < 0)
1064 return r;
1065 ts3 = ts4;
1066 a = cpoints[i][1];
1067 }
1068 return 0;
1069 }
1070
1071 static int generate_interval(void *log, struct sbg_script *s,
1072 struct ws_intervals *inter,
1073 int64_t ts1, int64_t ts2,
1074 struct sbg_script_synth *s1,
1075 struct sbg_script_synth *s2,
1076 int transition)
1077 {
1078 int r;
1079
1080 if (ts2 <= ts1 || (s1->vol == 0 && s2->vol == 0))
1081 return 0;
1082 switch (s1->type) {
1083 case SBG_TYPE_NONE:
1084 break;
1085 case SBG_TYPE_SINE:
1086 if (s1->beat == 0 && s2->beat == 0) {
1087 r = add_interval(inter, WS_SINE, 3, s1->ref.l,
1088 ts1, s1->carrier, s1->vol,
1089 ts2, s2->carrier, s2->vol);
1090 if (r < 0)
1091 return r;
1092 s2->ref.l = s2->ref.r = r;
1093 } else {
1094 r = add_interval(inter, WS_SINE, 1, s1->ref.l,
1095 ts1, s1->carrier + s1->beat / 2, s1->vol,
1096 ts2, s2->carrier + s2->beat / 2, s2->vol);
1097 if (r < 0)
1098 return r;
1099 s2->ref.l = r;
1100 r = add_interval(inter, WS_SINE, 2, s1->ref.r,
1101 ts1, s1->carrier - s1->beat / 2, s1->vol,
1102 ts2, s2->carrier - s2->beat / 2, s2->vol);
1103 if (r < 0)
1104 return r;
1105 s2->ref.r = r;
1106 }
1107 break;
1108
1109 case SBG_TYPE_BELL:
1110 if (transition == 2) {
1111 r = add_bell(inter, s, ts1, ts2, s1->carrier, s2->vol);
1112 if (r < 0)
1113 return r;
1114 }
1115 break;
1116
1117 case SBG_TYPE_SPIN:
1118 av_log(log, AV_LOG_WARNING, "Spinning noise not implemented, "
1119 "using pink noise instead.\n");
1120 /* fall through */
1121 case SBG_TYPE_NOISE:
1122 /* SBaGen's pink noise generator uses:
1123 - 1 band of white noise, mean square: 1/3;
1124 - 9 bands of subsampled white noise with linear
1125 interpolation, mean square: 2/3 each;
1126 with 1/10 weight each: the total mean square is 7/300.
1127 Our pink noise generator uses 8 bands of white noise with
1128 rectangular subsampling: the total mean square is 1/24.
1129 Therefore, to match SBaGen's volume, we must multiply vol by
1130 sqrt((7/300) / (1/24)) = sqrt(14/25) =~ 0.748
1131 */
1132 r = add_interval(inter, WS_NOISE, 3, s1->ref.l,
1133 ts1, 0, s1->vol - s1->vol / 4,
1134 ts2, 0, s2->vol - s2->vol / 4);
1135 if (r < 0)
1136 return r;
1137 s2->ref.l = s2->ref.r = r;
1138 break;
1139
1140 case SBG_TYPE_MIX:
1141 /* Unimplemented: silence; warning present elsewhere */
1142 default:
1143 av_log(log, AV_LOG_ERROR,
1144 "Type %d is not implemented\n", s1->type);
1145 return AVERROR_PATCHWELCOME;
1146 }
1147 return 0;
1148 }
1149
1150 static int generate_plateau(void *log, struct sbg_script *s,
1151 struct ws_intervals *inter,
1152 struct sbg_script_event *ev1)
1153 {
1154 int64_t ts1 = ev1->ts_int, ts2 = ev1->ts_trans;
1155 int i, r;
1156 struct sbg_script_synth *s1;
1157
1158 for (i = 0; i < ev1->nb_elements; i++) {
1159 s1 = &s->synth[ev1->elements + i];
1160 r = generate_interval(log, s, inter, ts1, ts2, s1, s1, 0);
1161 if (r < 0)
1162 return r;
1163 }
1164 return 0;
1165 }
1166
1167 /*
1168
1169 ts1 ts2 ts1 tsmid ts2
1170 | | | | |
1171 v v v | v
1172 ____ ____ v ____
1173 ''''.... ''.. ..''
1174 ''''....____ ''....''
1175
1176 compatible transition incompatible transition
1177 */
1178
1179 static int generate_transition(void *log, struct sbg_script *s,
1180 struct ws_intervals *inter,
1181 struct sbg_script_event *ev1,
1182 struct sbg_script_event *ev2)
1183 {
1184 int64_t ts1 = ev1->ts_trans, ts2 = ev1->ts_next;
1185 /* (ts1 + ts2) / 2 without overflow */
1186 int64_t tsmid = (ts1 >> 1) + (ts2 >> 1) + (ts1 & ts2 & 1);
1187 enum sbg_fade_type type = ev1->fade.slide | (ev1->fade.out & ev2->fade.in);
1188 int nb_elements = FFMAX(ev1->nb_elements, ev2->nb_elements);
1189 struct sbg_script_synth *s1, *s2, s1mod, s2mod, smid;
1190 int pass, i, r;
1191
1192 for (pass = 0; pass < 2; pass++) {
1193 /* pass = 0 -> compatible and first half of incompatible
1194 pass = 1 -> second half of incompatible
1195 Using two passes like that ensures that the intervals are generated
1196 in increasing order according to their start timestamp.
1197 Otherwise it would be necessary to sort them
1198 while keeping the mutual references.
1199 */
1200 for (i = 0; i < nb_elements; i++) {
1201 s1 = i < ev1->nb_elements ? &s->synth[ev1->elements + i] : &s1mod;
1202 s2 = i < ev2->nb_elements ? &s->synth[ev2->elements + i] : &s2mod;
1203 s1mod = s1 != &s1mod ? *s1 : (struct sbg_script_synth){ 0 };
1204 s2mod = s2 != &s2mod ? *s2 : (struct sbg_script_synth){ 0 };
1205 if (ev1->fade.slide) {
1206 /* for slides, and only for slides, silence ("-") is equivalent
1207 to anything with volume 0 */
1208 if (s1mod.type == SBG_TYPE_NONE) {
1209 s1mod = s2mod;
1210 s1mod.vol = 0;
1211 } else if (s2mod.type == SBG_TYPE_NONE) {
1212 s2mod = s1mod;
1213 s2mod.vol = 0;
1214 }
1215 }
1216 if (s1mod.type == s2mod.type &&
1217 s1mod.type != SBG_TYPE_BELL &&
1218 (type == SBG_FADE_ADAPT ||
1219 (s1mod.carrier == s2mod.carrier &&
1220 s1mod.beat == s2mod.beat))) {
1221 /* compatible: single transition */
1222 if (!pass) {
1223 r = generate_interval(log, s, inter,
1224 ts1, ts2, &s1mod, &s2mod, 3);
1225 if (r < 0)
1226 return r;
1227 s2->ref = s2mod.ref;
1228 }
1229 } else {
1230 /* incompatible: silence at midpoint */
1231 if (!pass) {
1232 smid = s1mod;
1233 smid.vol = 0;
1234 r = generate_interval(log, s, inter,
1235 ts1, tsmid, &s1mod, &smid, 1);
1236 if (r < 0)
1237 return r;
1238 } else {
1239 smid = s2mod;
1240 smid.vol = 0;
1241 r = generate_interval(log, s, inter,
1242 tsmid, ts2, &smid, &s2mod, 2);
1243 if (r < 0)
1244 return r;
1245 s2->ref = s2mod.ref;
1246 }
1247 }
1248 }
1249 }
1250 return 0;
1251 }
1252
1253 /*
1254 ev1 trats ev2 intts endts ev3
1255 | | | | | |
1256 v v v v v v
1257 ________________
1258 .... .... ....
1259 '''....________________....''' '''...._______________
1260
1261 \_________/\______________/\_________/\______________/\_________/\_____________/
1262 tr x->1 int1 tr 1->2 int2 tr 2->3 int3
1263 */
1264
1265 static int generate_intervals(void *log, struct sbg_script *s, int sample_rate,
1266 struct ws_intervals *inter)
1267 {
1268 int64_t trans_time = s->opt_fade_time / 2;
1269 struct sbg_script_event ev0, *ev1, *ev2;
1270 int64_t period;
1271 int i, r;
1272
1273 /* SBaGen handles the time before and after the extremal events,
1274 and the corresponding transitions, as if the sequence were cyclic
1275 with a 24-hours period. */
1276 period = s->events[s->nb_events - 1].ts - (uint64_t)s->events[0].ts;
1277 if (period < 0)
1278 return AVERROR_INVALIDDATA;
1279
1280 period = (period + (DAY_TS - 1)) / DAY_TS * DAY_TS;
1281 period = FFMAX(period, DAY_TS);
1282
1283 /* Prepare timestamps for transitions */
1284 for (i = 0; i < s->nb_events; i++) {
1285 ev1 = &s->events[i];
1286 ev2 = &s->events[(i + 1) % s->nb_events];
1287 ev1->ts_int = ev1->ts;
1288
1289 if (!ev1->fade.slide && ev1 >= ev2 && ev2->ts > INT64_MAX - period)
1290 return AVERROR_INVALIDDATA;
1291
1292 ev1->ts_trans = ev1->fade.slide ? ev1->ts
1293 : ev2->ts + (ev1 < ev2 ? 0 : period);
1294 }
1295 for (i = 0; i < s->nb_events; i++) {
1296 ev1 = &s->events[i];
1297 ev2 = &s->events[(i + 1) % s->nb_events];
1298 if (!ev1->fade.slide) {
1299 ev1->ts_trans = FFMAX(ev1->ts_int, ev1->ts_trans - trans_time);
1300 ev2->ts_int = FFMIN(ev2->ts_trans, ev2->ts_int + trans_time);
1301 }
1302 ev1->ts_next = ev2->ts_int + (ev1 < ev2 ? 0 : period);
1303 }
1304
1305 /* Pseudo event before the first one */
1306 ev0 = s->events[s->nb_events - 1];
1307 if (av_sat_sub64(ev0.ts_int, period) != (uint64_t)ev0.ts_int - period)
1308 return AVERROR_INVALIDDATA;
1309 ev0.ts_int -= period;
1310 ev0.ts_trans -= period;
1311 ev0.ts_next -= period;
1312
1313 /* Convert timestamps */
1314 for (i = -1; i < s->nb_events; i++) {
1315 ev1 = i < 0 ? &ev0 : &s->events[i];
1316 ev1->ts_int = av_rescale(ev1->ts_int, sample_rate, AV_TIME_BASE);
1317 ev1->ts_trans = av_rescale(ev1->ts_trans, sample_rate, AV_TIME_BASE);
1318 ev1->ts_next = av_rescale(ev1->ts_next, sample_rate, AV_TIME_BASE);
1319 }
1320
1321 /* Generate intervals */
1322 for (i = 0; i < s->nb_synth; i++)
1323 s->synth[i].ref.l = s->synth[i].ref.r = -1;
1324 for (i = -1; i < s->nb_events; i++) {
1325 ev1 = i < 0 ? &ev0 : &s->events[i];
1326 ev2 = &s->events[(i + 1) % s->nb_events];
1327 r = generate_plateau(log, s, inter, ev1);
1328 if (r < 0)
1329 return r;
1330 r = generate_transition(log, s, inter, ev1, ev2);
1331 if (r < 0)
1332 return r;
1333 }
1334 if (!inter->nb_inter)
1335 av_log(log, AV_LOG_WARNING, "Completely silent script.\n");
1336 return 0;
1337 }
1338
1339 static int encode_intervals(struct sbg_script *s, AVCodecParameters *par,
1340 struct ws_intervals *inter)
1341 {
1342 int i, edata_size = 4, ret;
1343 uint8_t *edata;
1344
1345 for (i = 0; i < inter->nb_inter; i++) {
1346 edata_size += inter->inter[i].type == WS_SINE ? 44 :
1347 inter->inter[i].type == WS_NOISE ? 32 : 0;
1348 if (edata_size < 0)
1349 return AVERROR(ENOMEM);
1350 }
1351 if ((ret = ff_alloc_extradata(par, edata_size)) < 0)
1352 return ret;
1353 edata = par->extradata;
1354
1355 #define ADD_EDATA32(v) do { AV_WL32(edata, (v)); edata += 4; } while(0)
1356 #define ADD_EDATA64(v) do { AV_WL64(edata, (v)); edata += 8; } while(0)
1357 ADD_EDATA32(inter->nb_inter);
1358 for (i = 0; i < inter->nb_inter; i++) {
1359 ADD_EDATA64(inter->inter[i].ts1);
1360 ADD_EDATA64(inter->inter[i].ts2);
1361 ADD_EDATA32(inter->inter[i].type);
1362 ADD_EDATA32(inter->inter[i].channels);
1363 switch (inter->inter[i].type) {
1364 case WS_SINE:
1365 ADD_EDATA32(inter->inter[i].f1);
1366 ADD_EDATA32(inter->inter[i].f2);
1367 ADD_EDATA32(inter->inter[i].a1);
1368 ADD_EDATA32(inter->inter[i].a2);
1369 ADD_EDATA32(inter->inter[i].phi);
1370 break;
1371 case WS_NOISE:
1372 ADD_EDATA32(inter->inter[i].a1);
1373 ADD_EDATA32(inter->inter[i].a2);
1374 break;
1375 }
1376 }
1377 if (edata != par->extradata + edata_size)
1378 return AVERROR_BUG;
1379 return 0;
1380 }
1381
1382 7023 static av_cold int sbg_read_probe(const AVProbeData *p)
1383 {
1384 int r, score;
1385 7023 struct sbg_script script = { 0 };
1386
1387 7023 r = parse_script(NULL, p->buf, p->buf_size, &script);
1388
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 7023 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
7023 score = r < 0 || !script.nb_def || !script.nb_tseq ? 0 :
1389 AVPROBE_SCORE_MAX / 3;
1390 7023 free_script(&script);
1391 7023 return score;
1392 }
1393
1394 static av_cold int sbg_read_header(AVFormatContext *avf)
1395 {
1396 struct sbg_demuxer *sbg = avf->priv_data;
1397 AVBPrint bprint;
1398 int r;
1399 struct sbg_script script = { 0 };
1400 AVStream *st;
1401 FFStream *sti;
1402 struct ws_intervals inter = { 0 };
1403
1404 av_bprint_init(&bprint, 0, sbg->max_file_size + 1U);
1405 r = read_whole_file(avf->pb, sbg->max_file_size, &bprint);
1406 if (r < 0)
1407 goto fail2;
1408
1409 r = parse_script(avf, bprint.str, bprint.len, &script);
1410 if (r < 0)
1411 goto fail2;
1412 if (!sbg->sample_rate)
1413 sbg->sample_rate = script.sample_rate;
1414 else
1415 script.sample_rate = sbg->sample_rate;
1416 if (!sbg->frame_size)
1417 sbg->frame_size = FFMAX(1, sbg->sample_rate / 10);
1418 if (script.opt_mix)
1419 av_log(avf, AV_LOG_WARNING, "Mix feature not implemented: "
1420 "-m is ignored and mix channels will be silent.\n");
1421 r = expand_script(avf, &script);
1422 if (r < 0)
1423 goto fail2;
1424 av_bprint_finalize(&bprint, NULL);
1425 r = generate_intervals(avf, &script, sbg->sample_rate, &inter);
1426 if (r < 0)
1427 goto fail;
1428
1429 if (script.end_ts != AV_NOPTS_VALUE && script.end_ts < script.start_ts) {
1430 r = AVERROR_INVALIDDATA;
1431 goto fail;
1432 }
1433
1434 st = avformat_new_stream(avf, NULL);
1435 if (!st)
1436 return AVERROR(ENOMEM);
1437 sti = ffstream(st);
1438 st->codecpar->codec_type = AVMEDIA_TYPE_AUDIO;
1439 st->codecpar->codec_id = AV_CODEC_ID_FFWAVESYNTH;
1440 st->codecpar->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
1441 st->codecpar->sample_rate = sbg->sample_rate;
1442 st->codecpar->frame_size = sbg->frame_size;
1443 avpriv_set_pts_info(st, 64, 1, st->codecpar->sample_rate);
1444 sti->probe_packets = 0;
1445 st->start_time = av_rescale(script.start_ts,
1446 sbg->sample_rate, AV_TIME_BASE);
1447 st->duration = script.end_ts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
1448 av_rescale(script.end_ts - script.start_ts,
1449 sbg->sample_rate, AV_TIME_BASE);
1450
1451 if (st->duration != AV_NOPTS_VALUE && (
1452 st->duration < 0 || st->start_time > INT64_MAX - st->duration)) {
1453 r = AVERROR_INVALIDDATA;
1454 goto fail;
1455 }
1456
1457 sti->cur_dts = st->start_time;
1458 r = encode_intervals(&script, st->codecpar, &inter);
1459 if (r < 0)
1460 goto fail;
1461
1462 av_free(inter.inter);
1463 free_script(&script);
1464 return 0;
1465
1466 fail2:
1467 av_bprint_finalize(&bprint, NULL);
1468 fail:
1469 av_free(inter.inter);
1470 free_script(&script);
1471 return r;
1472 }
1473
1474 static int sbg_read_packet(AVFormatContext *avf, AVPacket *packet)
1475 {
1476 int64_t ts, end_ts;
1477 int ret;
1478
1479 ts = ffstream(avf->streams[0])->cur_dts;
1480 end_ts = av_sat_add64(ts, avf->streams[0]->codecpar->frame_size);
1481 if (avf->streams[0]->duration != AV_NOPTS_VALUE)
1482 end_ts = FFMIN(avf->streams[0]->start_time + avf->streams[0]->duration,
1483 end_ts);
1484 if (end_ts <= ts)
1485 return AVERROR_EOF;
1486 if ((ret = av_new_packet(packet, 12)) < 0)
1487 return ret;
1488 packet->dts = packet->pts = ts;
1489 packet->duration = end_ts - ts;
1490 AV_WL64(packet->data + 0, ts);
1491 AV_WL32(packet->data + 8, packet->duration);
1492 return packet->size;
1493 }
1494
1495 static int sbg_read_seek2(AVFormatContext *avf, int stream_index,
1496 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
1497 {
1498 if (flags || stream_index > 0)
1499 return AVERROR(EINVAL);
1500 if (stream_index < 0)
1501 ts = av_rescale_q(ts, AV_TIME_BASE_Q, avf->streams[0]->time_base);
1502 ffstream(avf->streams[0])->cur_dts = ts;
1503 return 0;
1504 }
1505
1506 static int sbg_read_seek(AVFormatContext *avf, int stream_index,
1507 int64_t ts, int flags)
1508 {
1509 return sbg_read_seek2(avf, stream_index, ts, ts, ts, 0);
1510 }
1511
1512 static const AVOption sbg_options[] = {
1513 { "sample_rate", "", offsetof(struct sbg_demuxer, sample_rate),
1514 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
1515 AV_OPT_FLAG_DECODING_PARAM },
1516 { "frame_size", "", offsetof(struct sbg_demuxer, frame_size),
1517 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
1518 AV_OPT_FLAG_DECODING_PARAM },
1519 { "max_file_size", "", offsetof(struct sbg_demuxer, max_file_size),
1520 AV_OPT_TYPE_INT, { .i64 = 5000000 }, 0, INT_MAX,
1521 AV_OPT_FLAG_DECODING_PARAM },
1522 { NULL },
1523 };
1524
1525 static const AVClass sbg_demuxer_class = {
1526 .class_name = "sbg_demuxer",
1527 .item_name = av_default_item_name,
1528 .option = sbg_options,
1529 .version = LIBAVUTIL_VERSION_INT,
1530 };
1531
1532 const AVInputFormat ff_sbg_demuxer = {
1533 .name = "sbg",
1534 .long_name = NULL_IF_CONFIG_SMALL("SBaGen binaural beats script"),
1535 .priv_data_size = sizeof(struct sbg_demuxer),
1536 .read_probe = sbg_read_probe,
1537 .read_header = sbg_read_header,
1538 .read_packet = sbg_read_packet,
1539 .read_seek = sbg_read_seek,
1540 .read_seek2 = sbg_read_seek2,
1541 .extensions = "sbg",
1542 .priv_class = &sbg_demuxer_class,
1543 };
1544