forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbgdec.c
1545 lines (1417 loc) · 46.1 KB
/
sbgdec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SBG (SBaGen) file format decoder
* Copyright (c) 2011 Nicolas George
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "libavutil/channel_layout.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/log.h"
#include "libavutil/opt.h"
#include "libavutil/time_internal.h"
#include "avformat.h"
#include "internal.h"
#define SBG_SCALE (1 << 16)
#define DAY (24 * 60 * 60)
#define DAY_TS ((int64_t)DAY * AV_TIME_BASE)
struct sbg_demuxer {
AVClass *class;
int sample_rate;
int frame_size;
int max_file_size;
};
struct sbg_string {
char *s;
char *e;
};
enum sbg_fade_type {
SBG_FADE_SILENCE = 0,
SBG_FADE_SAME = 1,
SBG_FADE_ADAPT = 3,
};
struct sbg_fade {
int8_t in, out, slide;
};
enum sbg_synth_type {
SBG_TYPE_NONE,
SBG_TYPE_SINE,
SBG_TYPE_NOISE,
SBG_TYPE_BELL,
SBG_TYPE_MIX,
SBG_TYPE_SPIN,
};
/* bell: freq constant, ampl decreases exponentially, can be approx lin */
struct sbg_timestamp {
int64_t t;
char type; /* 0 for relative, 'N' for now, 'T' for absolute */
};
struct sbg_script_definition {
char *name;
int name_len;
int elements, nb_elements;
char type; /* 'S' or 'B' */
};
struct sbg_script_synth {
int carrier;
int beat;
int vol;
enum sbg_synth_type type;
struct {
int l, r;
} ref;
};
struct sbg_script_tseq {
struct sbg_timestamp ts;
char *name;
int name_len;
int lock;
struct sbg_fade fade;
};
struct sbg_script_event {
int64_t ts;
int64_t ts_int, ts_trans, ts_next;
int elements, nb_elements;
struct sbg_fade fade;
};
struct sbg_script {
struct sbg_script_definition *def;
struct sbg_script_synth *synth;
struct sbg_script_tseq *tseq;
struct sbg_script_tseq *block_tseq;
struct sbg_script_event *events;
int nb_def;
int nb_tseq;
int nb_events;
int nb_synth;
int64_t start_ts;
int64_t end_ts;
int64_t opt_fade_time;
int64_t opt_duration;
char *opt_mix;
int sample_rate;
uint8_t opt_start_at_first;
uint8_t opt_end_at_last;
};
struct sbg_parser {
void *log;
char *script, *end;
char *cursor;
struct sbg_script scs;
struct sbg_timestamp current_time;
int nb_block_tseq;
int nb_def_max, nb_synth_max, nb_tseq_max, nb_block_tseq_max;
int line_no;
char err_msg[128];
};
enum ws_interval_type {
WS_SINE = MKTAG('S','I','N','E'),
WS_NOISE = MKTAG('N','O','I','S'),
};
struct ws_interval {
int64_t ts1, ts2;
enum ws_interval_type type;
uint32_t channels;
int32_t f1, f2;
int32_t a1, a2;
uint32_t phi;
};
struct ws_intervals {
struct ws_interval *inter;
int nb_inter;
int max_inter;
};
static void *alloc_array_elem(void **array, size_t elsize,
int *size, int *max_size)
{
void *ret;
if (*size == *max_size) {
int m = FFMAX(32, FFMIN(*max_size, INT_MAX / 2) * 2);
if (*size >= m)
return NULL;
*array = av_realloc_f(*array, m, elsize);
if (!*array)
return NULL;
*max_size = m;
}
ret = (char *)*array + elsize * *size;
memset(ret, 0, elsize);
(*size)++;
return ret;
}
static int str_to_time(const char *str, int64_t *rtime)
{
const char *cur = str;
char *end;
int hours, minutes;
double seconds = 0;
int64_t ts = 0;
if (*cur < '0' || *cur > '9')
return 0;
hours = strtol(cur, &end, 10);
if (end == cur || *end != ':' || end[1] < '0' || end[1] > '9')
return 0;
cur = end + 1;
minutes = strtol(cur, &end, 10);
if (end == cur)
return 0;
cur = end;
if (*end == ':'){
seconds = strtod(cur + 1, &end);
if (end > cur + 1)
cur = end;
ts = av_clipd(seconds * AV_TIME_BASE, INT64_MIN/2, INT64_MAX/2);
}
*rtime = av_sat_add64((hours * 3600LL + minutes * 60LL) * AV_TIME_BASE, ts);
return cur - str;
}
static inline int is_space(char c)
{
return c == ' ' || c == '\t' || c == '\r';
}
static inline int scale_double(void *log, double d, double m, int *r)
{
m *= d * SBG_SCALE;
if (m < INT_MIN || m >= INT_MAX) {
if (log)
av_log(log, AV_LOG_ERROR, "%g is too large\n", d);
return AVERROR(EDOM);
}
*r = m;
return 0;
}
static int lex_space(struct sbg_parser *p)
{
char *c = p->cursor;
while (p->cursor < p->end && is_space(*p->cursor))
p->cursor++;
return p->cursor > c;
}
static int lex_char(struct sbg_parser *p, char c)
{
int r = p->cursor < p->end && *p->cursor == c;
p->cursor += r;
return r;
}
static int lex_double(struct sbg_parser *p, double *r)
{
double d;
char *end;
if (p->cursor == p->end || is_space(*p->cursor) || *p->cursor == '\n')
return 0;
d = strtod(p->cursor, &end);
if (end > p->cursor) {
*r = d;
p->cursor = end;
return 1;
}
return 0;
}
static int lex_fixed(struct sbg_parser *p, const char *t, int l)
{
if (p->end - p->cursor < l || memcmp(p->cursor, t, l))
return 0;
p->cursor += l;
return 1;
}
static int lex_line_end(struct sbg_parser *p)
{
if (p->cursor < p->end && *p->cursor == '#') {
p->cursor++;
while (p->cursor < p->end && *p->cursor != '\n')
p->cursor++;
}
if (p->cursor == p->end)
/* simulate final LF for files lacking it */
return 1;
if (*p->cursor != '\n')
return 0;
p->cursor++;
p->line_no++;
lex_space(p);
return 1;
}
static int lex_wsword(struct sbg_parser *p, struct sbg_string *rs)
{
char *s = p->cursor, *c = s;
if (s == p->end || *s == '\n')
return 0;
while (c < p->end && *c != '\n' && !is_space(*c))
c++;
rs->s = s;
rs->e = p->cursor = c;
lex_space(p);
return 1;
}
static int lex_name(struct sbg_parser *p, struct sbg_string *rs)
{
char *s = p->cursor, *c = s;
while (c < p->end && ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')
|| (*c >= '0' && *c <= '9') || *c == '_' || *c == '-'))
c++;
if (c == s)
return 0;
rs->s = s;
rs->e = p->cursor = c;
return 1;
}
static int lex_time(struct sbg_parser *p, int64_t *rt)
{
int r = str_to_time(p->cursor, rt);
p->cursor += r;
return r > 0;
}
#define FORWARD_ERROR(c) \
do { \
int errcode = c; \
if (errcode <= 0) \
return errcode ? errcode : AVERROR_INVALIDDATA; \
} while (0)
static int parse_immediate(struct sbg_parser *p)
{
snprintf(p->err_msg, sizeof(p->err_msg),
"immediate sequences not yet implemented");
return AVERROR_PATCHWELCOME;
}
static int parse_preprogrammed(struct sbg_parser *p)
{
snprintf(p->err_msg, sizeof(p->err_msg),
"preprogrammed sequences not yet implemented");
return AVERROR_PATCHWELCOME;
}
static int parse_optarg(struct sbg_parser *p, char o, struct sbg_string *r)
{
if (!lex_wsword(p, r)) {
snprintf(p->err_msg, sizeof(p->err_msg),
"option '%c' requires an argument", o);
return AVERROR_INVALIDDATA;
}
return 1;
}
static int parse_options(struct sbg_parser *p)
{
struct sbg_string ostr, oarg;
char mode = 0;
int r;
char *tptr;
double v;
if (p->cursor == p->end || *p->cursor != '-')
return 0;
while (lex_char(p, '-') && lex_wsword(p, &ostr)) {
for (; ostr.s < ostr.e; ostr.s++) {
char opt = *ostr.s;
switch (opt) {
case 'S':
p->scs.opt_start_at_first = 1;
break;
case 'E':
p->scs.opt_end_at_last = 1;
break;
case 'i':
mode = 'i';
break;
case 'p':
mode = 'p';
break;
case 'F':
FORWARD_ERROR(parse_optarg(p, opt, &oarg));
v = strtod(oarg.s, &tptr);
if (oarg.e != tptr) {
snprintf(p->err_msg, sizeof(p->err_msg),
"syntax error for option -F");
return AVERROR_INVALIDDATA;
}
p->scs.opt_fade_time = v * AV_TIME_BASE / 1000;
break;
case 'L':
FORWARD_ERROR(parse_optarg(p, opt, &oarg));
r = str_to_time(oarg.s, &p->scs.opt_duration);
if (oarg.e != oarg.s + r) {
snprintf(p->err_msg, sizeof(p->err_msg),
"syntax error for option -L");
return AVERROR_INVALIDDATA;
}
break;
case 'T':
FORWARD_ERROR(parse_optarg(p, opt, &oarg));
r = str_to_time(oarg.s, &p->scs.start_ts);
if (oarg.e != oarg.s + r) {
snprintf(p->err_msg, sizeof(p->err_msg),
"syntax error for option -T");
return AVERROR_INVALIDDATA;
}
break;
case 'm':
FORWARD_ERROR(parse_optarg(p, opt, &oarg));
tptr = av_malloc(oarg.e - oarg.s + 1);
if (!tptr)
return AVERROR(ENOMEM);
memcpy(tptr, oarg.s, oarg.e - oarg.s);
tptr[oarg.e - oarg.s] = 0;
av_free(p->scs.opt_mix);
p->scs.opt_mix = tptr;
break;
case 'q':
FORWARD_ERROR(parse_optarg(p, opt, &oarg));
v = strtod(oarg.s, &tptr);
if (oarg.e != tptr) {
snprintf(p->err_msg, sizeof(p->err_msg),
"syntax error for option -q");
return AVERROR_INVALIDDATA;
}
if (v != 1) {
snprintf(p->err_msg, sizeof(p->err_msg),
"speed factor other than 1 not supported");
return AVERROR_PATCHWELCOME;
}
break;
case 'r':
FORWARD_ERROR(parse_optarg(p, opt, &oarg));
r = strtol(oarg.s, &tptr, 10);
if (oarg.e != tptr) {
snprintf(p->err_msg, sizeof(p->err_msg),
"syntax error for option -r");
return AVERROR_INVALIDDATA;
}
if (r < 40) {
snprintf(p->err_msg, sizeof(p->err_msg),
"invalid sample rate");
return AVERROR_PATCHWELCOME;
}
p->scs.sample_rate = r;
break;
default:
snprintf(p->err_msg, sizeof(p->err_msg),
"unknown option: '%c'", *ostr.s);
return AVERROR_INVALIDDATA;
}
}
}
switch (mode) {
case 'i':
return parse_immediate(p);
case 'p':
return parse_preprogrammed(p);
case 0:
if (!lex_line_end(p))
return AVERROR_INVALIDDATA;
return 1;
}
return AVERROR_BUG;
}
static int parse_timestamp(struct sbg_parser *p,
struct sbg_timestamp *rts, int64_t *rrel)
{
int64_t abs = 0, rel = 0, dt;
char type = 0;
int r;
if (lex_fixed(p, "NOW", 3)) {
type = 'N';
r = 1;
} else {
r = lex_time(p, &abs);
if (r)
type = 'T';
}
while (lex_char(p, '+')) {
if (!lex_time(p, &dt))
return AVERROR_INVALIDDATA;
if (av_sat_add64(rel, dt) - dt != rel)
return AVERROR_INVALIDDATA;
rel += dt;
r = 1;
}
if (r) {
if (!lex_space(p))
return AVERROR_INVALIDDATA;
rts->type = type;
rts->t = abs;
*rrel = rel;
}
return r;
}
static int parse_fade(struct sbg_parser *p, struct sbg_fade *fr)
{
struct sbg_fade f = {0};
if (lex_char(p, '<'))
f.in = SBG_FADE_SILENCE;
else if (lex_char(p, '-'))
f.in = SBG_FADE_SAME;
else if (lex_char(p, '='))
f.in = SBG_FADE_ADAPT;
else
return 0;
if (lex_char(p, '>'))
f.out = SBG_FADE_SILENCE;
else if (lex_char(p, '-'))
f.out = SBG_FADE_SAME;
else if (lex_char(p, '='))
f.out = SBG_FADE_ADAPT;
else
return AVERROR_INVALIDDATA;
*fr = f;
return 1;
}
static int parse_time_sequence(struct sbg_parser *p, int inblock)
{
struct sbg_timestamp ts;
int64_t rel_ts;
int r;
struct sbg_fade fade = { SBG_FADE_SAME, SBG_FADE_SAME, 0 };
struct sbg_string name;
struct sbg_script_tseq *tseq;
r = parse_timestamp(p, &ts, &rel_ts);
if (!r)
return 0;
if (r < 0)
return r;
if (ts.type) {
if (inblock)
return AVERROR_INVALIDDATA;
p->current_time.type = ts.type;
p->current_time.t = ts.t;
} else if(!inblock && !p->current_time.type) {
snprintf(p->err_msg, sizeof(p->err_msg),
"relative time without previous absolute time");
return AVERROR_INVALIDDATA;
}
ts.type = p->current_time.type;
if (av_sat_add64(p->current_time.t, rel_ts) != p->current_time.t + (uint64_t)rel_ts)
return AVERROR_INVALIDDATA;
ts.t = p->current_time.t + rel_ts;
r = parse_fade(p, &fade);
if (r < 0)
return r;
lex_space(p);
if (!lex_name(p, &name))
return AVERROR_INVALIDDATA;
lex_space(p);
if (lex_fixed(p, "->", 2)) {
fade.slide = SBG_FADE_ADAPT;
lex_space(p);
}
if (!lex_line_end(p))
return AVERROR_INVALIDDATA;
tseq = inblock ?
alloc_array_elem((void **)&p->scs.block_tseq, sizeof(*tseq),
&p->nb_block_tseq, &p->nb_block_tseq_max) :
alloc_array_elem((void **)&p->scs.tseq, sizeof(*tseq),
&p->scs.nb_tseq, &p->nb_tseq_max);
if (!tseq)
return AVERROR(ENOMEM);
tseq->ts = ts;
tseq->name = name.s;
tseq->name_len = name.e - name.s;
tseq->fade = fade;
return 1;
}
static int parse_wave_def(struct sbg_parser *p, int wavenum)
{
snprintf(p->err_msg, sizeof(p->err_msg),
"waveform definitions not yet implemented");
return AVERROR_PATCHWELCOME;
}
static int parse_block_def(struct sbg_parser *p,
struct sbg_script_definition *def)
{
int r, tseq;
lex_space(p);
if (!lex_line_end(p))
return AVERROR_INVALIDDATA;
tseq = p->nb_block_tseq;
while (1) {
r = parse_time_sequence(p, 1);
if (r < 0)
return r;
if (!r)
break;
}
if (!lex_char(p, '}'))
return AVERROR_INVALIDDATA;
lex_space(p);
if (!lex_line_end(p))
return AVERROR_INVALIDDATA;
def->type = 'B';
def->elements = tseq;
def->nb_elements = p->nb_block_tseq - tseq;
if (!def->nb_elements)
return AVERROR_INVALIDDATA;
return 1;
}
static int parse_volume(struct sbg_parser *p, int *vol)
{
double v;
if (!lex_char(p, '/'))
return 0;
if (!lex_double(p, &v))
return AVERROR_INVALIDDATA;
if (scale_double(p->log, v, 0.01, vol))
return AVERROR(ERANGE);
return 1;
}
static int parse_synth_channel_sine(struct sbg_parser *p,
struct sbg_script_synth *synth)
{
double carrierf, beatf;
int carrier, beat, vol;
if (!lex_double(p, &carrierf))
return 0;
if (!lex_double(p, &beatf))
beatf = 0;
FORWARD_ERROR(parse_volume(p, &vol));
if (scale_double(p->log, carrierf, 1, &carrier) < 0 ||
scale_double(p->log, beatf, 1, &beat) < 0)
return AVERROR(EDOM);
synth->type = SBG_TYPE_SINE;
synth->carrier = carrier;
synth->beat = beat;
synth->vol = vol;
return 1;
}
static int parse_synth_channel_pink(struct sbg_parser *p,
struct sbg_script_synth *synth)
{
int vol;
if (!lex_fixed(p, "pink", 4))
return 0;
FORWARD_ERROR(parse_volume(p, &vol));
synth->type = SBG_TYPE_NOISE;
synth->vol = vol;
return 1;
}
static int parse_synth_channel_bell(struct sbg_parser *p,
struct sbg_script_synth *synth)
{
double carrierf;
int carrier, vol;
if (!lex_fixed(p, "bell", 4))
return 0;
if (!lex_double(p, &carrierf))
return AVERROR_INVALIDDATA;
FORWARD_ERROR(parse_volume(p, &vol));
if (scale_double(p->log, carrierf, 1, &carrier) < 0)
return AVERROR(EDOM);
synth->type = SBG_TYPE_BELL;
synth->carrier = carrier;
synth->vol = vol;
return 1;
}
static int parse_synth_channel_mix(struct sbg_parser *p,
struct sbg_script_synth *synth)
{
int vol;
if (!lex_fixed(p, "mix", 3))
return 0;
FORWARD_ERROR(parse_volume(p, &vol));
synth->type = SBG_TYPE_MIX;
synth->vol = vol;
return 1;
}
static int parse_synth_channel_spin(struct sbg_parser *p,
struct sbg_script_synth *synth)
{
double carrierf, beatf;
int carrier, beat, vol;
if (!lex_fixed(p, "spin:", 5))
return 0;
if (!lex_double(p, &carrierf))
return AVERROR_INVALIDDATA;
if (!lex_double(p, &beatf))
return AVERROR_INVALIDDATA;
FORWARD_ERROR(parse_volume(p, &vol));
if (scale_double(p->log, carrierf, 1, &carrier) < 0 ||
scale_double(p->log, beatf, 1, &beat) < 0)
return AVERROR(EDOM);
synth->type = SBG_TYPE_SPIN;
synth->carrier = carrier;
synth->beat = beat;
synth->vol = vol;
return 1;
}
static int parse_synth_channel(struct sbg_parser *p)
{
int r;
struct sbg_script_synth *synth;
synth = alloc_array_elem((void **)&p->scs.synth, sizeof(*synth),
&p->scs.nb_synth, &p->nb_synth_max);
if (!synth)
return AVERROR(ENOMEM);
r = lex_char(p, '-');
if (!r)
r = parse_synth_channel_pink(p, synth);
if (!r)
r = parse_synth_channel_bell(p, synth);
if (!r)
r = parse_synth_channel_mix(p, synth);
if (!r)
r = parse_synth_channel_spin(p, synth);
/* Unimplemented: wave%d:%f%f/vol (carrier, beat) */
if (!r)
r = parse_synth_channel_sine(p, synth);
if (r <= 0)
p->scs.nb_synth--;
return r;
}
static int parse_synth_def(struct sbg_parser *p,
struct sbg_script_definition *def)
{
int r, synth;
synth = p->scs.nb_synth;
while (1) {
r = parse_synth_channel(p);
if (r < 0)
return r;
if (!r || !lex_space(p))
break;
}
lex_space(p);
if (synth == p->scs.nb_synth)
return AVERROR_INVALIDDATA;
if (!lex_line_end(p))
return AVERROR_INVALIDDATA;
def->type = 'S';
def->elements = synth;
def->nb_elements = p->scs.nb_synth - synth;
return 1;
}
static int parse_named_def(struct sbg_parser *p)
{
char *cursor_save = p->cursor;
struct sbg_string name;
struct sbg_script_definition *def;
if (!lex_name(p, &name) || !lex_char(p, ':') || !lex_space(p)) {
p->cursor = cursor_save;
return 0;
}
if (name.e - name.s == 6 && !memcmp(name.s, "wave", 4) &&
name.s[4] >= '0' && name.s[4] <= '9' &&
name.s[5] >= '0' && name.s[5] <= '9') {
int wavenum = (name.s[4] - '0') * 10 + (name.s[5] - '0');
return parse_wave_def(p, wavenum);
}
def = alloc_array_elem((void **)&p->scs.def, sizeof(*def),
&p->scs.nb_def, &p->nb_def_max);
if (!def)
return AVERROR(ENOMEM);
def->name = name.s;
def->name_len = name.e - name.s;
if (lex_char(p, '{'))
return parse_block_def(p, def);
return parse_synth_def(p, def);
}
static void free_script(struct sbg_script *s)
{
av_freep(&s->def);
av_freep(&s->synth);
av_freep(&s->tseq);
av_freep(&s->block_tseq);
av_freep(&s->events);
av_freep(&s->opt_mix);
}
static int parse_script(void *log, char *script, int script_len,
struct sbg_script *rscript)
{
struct sbg_parser sp = {
.log = log,
.script = script,
.end = script + script_len,
.cursor = script,
.line_no = 1,
.err_msg = "",
.scs = {
/* default values */
.start_ts = AV_NOPTS_VALUE,
.sample_rate = 44100,
.opt_fade_time = 60 * AV_TIME_BASE,
},
};
int r;
lex_space(&sp);
while (sp.cursor < sp.end) {
r = parse_options(&sp);
if (r < 0)
goto fail;
if (!r && !lex_line_end(&sp))
break;
}
while (sp.cursor < sp.end) {
r = parse_named_def(&sp);
if (!r)
r = parse_time_sequence(&sp, 0);
if (!r)
r = lex_line_end(&sp) ? 1 : AVERROR_INVALIDDATA;
if (r < 0)
goto fail;
}
*rscript = sp.scs;
return 1;
fail:
free_script(&sp.scs);
if (!*sp.err_msg)
if (r == AVERROR_INVALIDDATA)
snprintf(sp.err_msg, sizeof(sp.err_msg), "syntax error");
if (log && *sp.err_msg) {
const char *ctx = sp.cursor;
const char *ectx = av_x_if_null(memchr(ctx, '\n', sp.end - sp.cursor),
sp.end);
int lctx = ectx - ctx;
const char *quote = "\"";
if (lctx > 0 && ctx[lctx - 1] == '\r')
lctx--;
if (lctx == 0) {
ctx = "the end of line";
lctx = strlen(ctx);
quote = "";
}
av_log(log, AV_LOG_ERROR, "Error line %d: %s near %s%.*s%s.\n",
sp.line_no, sp.err_msg, quote, lctx, ctx, quote);
}
return r;
}
static int read_whole_file(AVIOContext *io, int max_size, char **rbuf)
{
char *buf = NULL;
int size = 0, bufsize = 0, r;
while (1) {
if (bufsize - size < 1024) {
bufsize = FFMIN(FFMAX(2 * bufsize, 8192), max_size);
if (bufsize - size < 2) {
size = AVERROR(EFBIG);
goto fail;
}
buf = av_realloc_f(buf, bufsize, 1);
if (!buf) {
size = AVERROR(ENOMEM);
goto fail;
}
}
r = avio_read(io, buf, bufsize - size - 1);
if (r == AVERROR_EOF)
break;
if (r < 0)
goto fail;
size += r;
}
buf[size] = 0;
*rbuf = buf;
return size;
fail:
av_free(buf);
return size;
}
static int expand_timestamps(void *log, struct sbg_script *s)
{
int i, nb_rel = 0;
int64_t now, cur_ts, delta = 0;
for (i = 0; i < s->nb_tseq; i++)
nb_rel += s->tseq[i].ts.type == 'N';
if (nb_rel == s->nb_tseq) {
/* All ts are relative to NOW: consider NOW = 0 */
now = 0;
if (s->start_ts != AV_NOPTS_VALUE)
av_log(log, AV_LOG_WARNING,
"Start time ignored in a purely relative script.\n");
} else if (nb_rel == 0 && s->start_ts != AV_NOPTS_VALUE ||
s->opt_start_at_first) {
/* All ts are absolute and start time is specified */
if (s->start_ts == AV_NOPTS_VALUE)
s->start_ts = s->tseq[0].ts.t;
now = s->start_ts;
} else {
/* Mixed relative/absolute ts: expand */
time_t now0;
struct tm *tm, tmpbuf;
av_log(log, AV_LOG_WARNING,
"Scripts with mixed absolute and relative timestamps can give "
"unexpected results (pause, seeking, time zone change).\n");
#undef time
time(&now0);
tm = localtime_r(&now0, &tmpbuf);
now = tm ? tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec :
now0 % DAY;
av_log(log, AV_LOG_INFO, "Using %02d:%02d:%02d as NOW.\n",
(int)(now / 3600), (int)(now / 60) % 60, (int)now % 60);
now *= AV_TIME_BASE;
for (i = 0; i < s->nb_tseq; i++) {
if (s->tseq[i].ts.type == 'N') {
s->tseq[i].ts.t += now;
s->tseq[i].ts.type = 'T'; /* not necessary */
}
}
}
if (s->start_ts == AV_NOPTS_VALUE)
s->start_ts = (s->opt_start_at_first && s->tseq) ? s->tseq[0].ts.t : now;
if (s->start_ts > INT64_MAX - s->opt_duration)
return AVERROR_INVALIDDATA;
s->end_ts = s->opt_duration ? s->start_ts + s->opt_duration :
AV_NOPTS_VALUE; /* may be overridden later by -E option */
cur_ts = now;
for (i = 0; i < s->nb_tseq; i++) {
if (av_sat_add64(s->tseq[i].ts.t, delta) != s->tseq[i].ts.t + (uint64_t)delta)
return AVERROR_INVALIDDATA;
if (s->tseq[i].ts.t + delta < cur_ts)
delta += DAY_TS;
cur_ts = s->tseq[i].ts.t += delta;
}
return 0;
}
static int expand_tseq(void *log, struct sbg_script *s, int *nb_ev_max,
int64_t t0, struct sbg_script_tseq *tseq)
{
int i, r;
struct sbg_script_definition *def;
struct sbg_script_tseq *be;
struct sbg_script_event *ev;
if (tseq->lock++) {
av_log(log, AV_LOG_ERROR, "Recursion loop on \"%.*s\"\n",
tseq->name_len, tseq->name);
return AVERROR(EINVAL);
}
if (t0 + (uint64_t)tseq->ts.t != av_sat_add64(t0, tseq->ts.t))
return AVERROR(EINVAL);
t0 += tseq->ts.t;
for (i = 0; i < s->nb_def; i++) {
if (s->def[i].name_len == tseq->name_len &&
!memcmp(s->def[i].name, tseq->name, tseq->name_len))
break;
}
if (i >= s->nb_def) {
av_log(log, AV_LOG_ERROR, "Tone-set \"%.*s\" not defined\n",
tseq->name_len, tseq->name);
return AVERROR(EINVAL);
}
def = &s->def[i];
if (def->type == 'B') {
be = s->block_tseq + def->elements;
for (i = 0; i < def->nb_elements; i++) {
r = expand_tseq(log, s, nb_ev_max, t0, &be[i]);
if (r < 0)
return r;
}
} else {
ev = alloc_array_elem((void **)&s->events, sizeof(*ev),
&s->nb_events, nb_ev_max);
if (!ev)
return AVERROR(ENOMEM);
ev->ts = tseq->ts.t;
ev->elements = def->elements;
ev->nb_elements = def->nb_elements;
ev->fade = tseq->fade;
}
tseq->lock--;