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>
32
33 #define SBG_SCALE (1 << 16)
34 #define DAY (24 * 60 * 60)
35 #define DAY_TS ((int64_t)DAY * AV_TIME_BASE)
36
42 };
43
47 };
48
53 };
54
57 };
58
66 };
67
68 /* bell: freq constant, ampl decreases exponentially, can be approx lin */
69
72 char type;
/* 0 for relative, 'N' for now, 'T' for absolute */
73 };
74
79 char type;
/* 'S' or 'B' */
80 };
81
87 struct {
90 };
91
98 };
99
105 };
106
125 };
126
137 };
138
142 };
143
151 };
152
157 };
158
160 int *
size,
int *max_size)
161 {
163
164 if (*
size == *max_size) {
165 int m =
FFMAX(32,
FFMIN(*max_size, INT_MAX / 2) * 2);
171 *max_size = m;
172 }
174 memset(
ret, 0, elsize);
175 (*size)++;
177 }
178
180 {
181 const char *cur =
str;
182 char *end;
183 int hours, minutes;
184 double seconds = 0;
185 int64_t ts = 0;
186
187 if (*cur < '0' || *cur > '9')
188 return 0;
189 hours = strtol(cur, &end, 10);
190 if (end == cur || *end != ':' || end[1] < '0' || end[1] > '9')
191 return 0;
192 cur = end + 1;
193 minutes = strtol(cur, &end, 10);
194 if (end == cur)
195 return 0;
196 cur = end;
197 if (*end == ':'){
198 seconds =
strtod(cur + 1, &end);
199 if (end > cur + 1)
200 cur = end;
202 }
205 }
206
208 {
209 return c ==
' ' ||
c ==
'\t' ||
c ==
'\r';
210 }
211
213 {
215 if (m < INT_MIN || m >= INT_MAX) {
216 if (log)
219 }
221 return 0;
222 }
223
225 {
227
231 }
232
234 {
236
239 }
240
242 {
244 char *end;
245
247 return 0;
252 return 1;
253 }
254 return 0;
255 }
256
258 {
260 return 0;
262 return 1;
263 }
264
266 {
271 }
273 /* simulate final LF for files lacking it */
274 return 1;
276 return 0;
280 return 1;
281 }
282
284 {
286
287 if (
s == p->
end || *
s ==
'\n')
288 return 0;
289 while (c < p->end && *
c !=
'\n' && !
is_space(*
c))
294 return 1;
295 }
296
298 {
300
301 while (c < p->end && ((*
c >=
'a' && *
c <=
'z') || (*
c >=
'A' && *
c <=
'Z')
302 || (*
c >=
'0' && *
c <=
'9') || *
c ==
'_' || *
c ==
'-'))
305 return 0;
308 return 1;
309 }
310
312 {
316 }
317
318 #define FORWARD_ERROR(c) \
319 do { \
320 int errcode = c; \
321 if (errcode <= 0) \
322 return errcode ? errcode : AVERROR_INVALIDDATA; \
323 } while (0)
324
326 {
328 "immediate sequences not yet implemented");
330 }
331
333 {
335 "preprogrammed sequences not yet implemented");
337 }
338
340 {
343 "option '%c' requires an argument", o);
345 }
346 return 1;
347 }
348
350 {
354 char *tptr;
355 double v;
356
358 return 0;
360 for (; ostr.
s < ostr.
e; ostr.
s++) {
362 switch (opt) {
363 case 'S':
365 break;
366 case 'E':
368 break;
369 case 'i':
371 break;
372 case 'p':
374 break;
375 case 'F':
378 if (oarg.
e != tptr) {
380 "syntax error for option -F");
382 }
384 break;
385 case 'L':
388 if (oarg.
e != oarg.
s +
r) {
390 "syntax error for option -L");
392 }
393 break;
394 case 'T':
397 if (oarg.
e != oarg.
s +
r) {
399 "syntax error for option -T");
401 }
402 break;
403 case 'm':
406 if (!tptr)
408 memcpy(tptr, oarg.
s, oarg.
e - oarg.
s);
409 tptr[oarg.
e - oarg.
s] = 0;
412 break;
413 case 'q':
416 if (oarg.
e != tptr) {
418 "syntax error for option -q");
420 }
421 if (v != 1) {
423 "speed factor other than 1 not supported");
425 }
426 break;
427 case 'r':
429 r = strtol(oarg.
s, &tptr, 10);
430 if (oarg.
e != tptr) {
432 "syntax error for option -r");
434 }
437 "invalid sample rate");
439 }
441 break;
442 default:
444 "unknown option: '%c'", *ostr.
s);
446 }
447 }
448 }
450 case 'i':
452 case 'p':
454 case 0:
457 return 1;
458 }
460 }
461
464 {
465 int64_t
abs = 0, rel = 0, dt;
468
472 } else {
476 }
482 rel += dt;
484 }
490 *rrel = rel;
491 }
493 }
494
496 {
498
505 else
506 return 0;
513 else
516 return 1;
517 }
518
520 {
522 int64_t rel_ts;
527
530 return 0;
534 if (inblock)
540 "relative time without previous absolute time");
542 }
544
558 }
561 tseq = inblock ?
566 if (!tseq)
572 return 1;
573 }
574
576 {
578 "waveform definitions not yet implemented");
580 }
581
584 {
586
591 while (1) {
596 break;
597 }
608 return 1;
609 }
610
612 {
613 double v;
614
616 return 0;
621 return 1;
622 }
623
626 {
627 double carrierf, beatf;
628 int carrier, beat, vol;
629
631 return 0;
633 beatf = 0;
642 return 1;
643 }
644
647 {
648 int vol;
649
651 return 0;
655 return 1;
656 }
657
660 {
661 double carrierf;
662 int carrier, vol;
663
665 return 0;
674 return 1;
675 }
676
679 {
680 int vol;
681
683 return 0;
687 return 1;
688 }
689
692 {
693 double carrierf, beatf;
694 int carrier, beat, vol;
695
697 return 0;
710 return 1;
711 }
712
714 {
717
720 if (!synth)
731 /* Unimplemented: wave%d:%f%f/vol (carrier, beat) */
737 }
738
741 {
743
745 while (1) {
750 break;
751 }
760 return 1;
761 }
762
764 {
765 char *cursor_save = p->
cursor;
768
771 return 0;
772 }
773 if (
name.e -
name.s == 6 && !memcmp(
name.s,
"wave", 4) &&
774 name.s[4] >=
'0' &&
name.s[4] <=
'9' &&
775 name.s[5] >=
'0' &&
name.s[5] <=
'9') {
776 int wavenum = (
name.s[4] -
'0') * 10 + (
name.s[5] -
'0');
778 }
781 if (!def)
788 }
789
791 {
798 }
799
802 {
806 .end =
script + script_len,
808 .line_no = 1,
809 .err_msg = "",
810 .scs = {
811 /* default values */
813 .sample_rate = 44100,
815 },
816 };
818
820 while (
sp.cursor <
sp.end) {
825 break;
826 }
827 while (
sp.cursor <
sp.end) {
835 }
837 return 1;
842 snprintf(
sp.err_msg,
sizeof(
sp.err_msg),
"syntax error");
843 if (
log && *
sp.err_msg) {
844 const char *
ctx =
sp.cursor;
847 int lctx = ectx -
ctx;
848 const char *quote = "\"";
849 if (lctx > 0 &&
ctx[lctx - 1] ==
'\r')
850 lctx--;
851 if (lctx == 0) {
852 ctx =
"the end of line";
854 quote = "";
855 }
857 sp.line_no,
sp.err_msg, quote, lctx,
ctx, quote);
858 }
860 }
861
863 {
865 int size = 0, bufsize = 0,
r;
866
867 while (1) {
868 if (bufsize -
size < 1024) {
869 bufsize =
FFMIN(
FFMAX(2 * bufsize, 8192), max_size);
870 if (bufsize -
size < 2) {
873 }
875 if (!buf) {
878 }
879 }
882 break;
886 }
888 *rbuf = buf;
893 }
894
896 {
898 int64_t now, cur_ts,
delta = 0;
899
900 for (
i = 0;
i <
s->nb_tseq;
i++)
901 nb_rel +=
s->tseq[
i].ts.type ==
'N';
902 if (nb_rel ==
s->nb_tseq) {
903 /* All ts are relative to NOW: consider NOW = 0 */
904 now = 0;
907 "Start time ignored in a purely relative script.\n");
909 s->opt_start_at_first) {
910 /* All ts are absolute and start time is specified */
912 s->start_ts =
s->tseq[0].ts.t;
914 } else {
915 /* Mixed relative/absolute ts: expand */
916 time_t now0;
917 struct tm *tm, tmpbuf;
918
920 "Scripts with mixed absolute and relative timestamps can give "
921 "unexpected results (pause, seeking, time zone change).\n");
922 #undef time
923 time(&now0);
925 now = tm ? tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec :
928 (int)(now / 3600), (int)(now / 60) % 60, (int)now % 60);
930 for (
i = 0;
i <
s->nb_tseq;
i++) {
931 if (
s->tseq[
i].ts.type ==
'N') {
932 s->tseq[
i].ts.t += now;
933 s->tseq[
i].ts.type =
'T';
/* not necessary */
934 }
935 }
936 }
938 s->start_ts = (
s->opt_start_at_first &&
s->tseq) ?
s->tseq[0].ts.t : now;
939 if (
s->start_ts > INT64_MAX -
s->opt_duration)
941
942 s->end_ts =
s->opt_duration ?
s->start_ts +
s->opt_duration :
944 cur_ts = now;
945 for (
i = 0;
i <
s->nb_tseq;
i++) {
948 if (
s->tseq[
i].ts.t +
delta < cur_ts)
950 cur_ts =
s->tseq[
i].ts.t +=
delta;
951 }
952 return 0;
953 }
954
957 {
962
967 }
970
972 for (
i = 0;
i <
s->nb_def;
i++) {
975 break;
976 }
977 if (
i >=
s->nb_def) {
981 }
983 if (def->
type ==
'B') {
989 }
990 } else {
992 &
s->nb_events, nb_ev_max);
993 if (!ev)
999 }
1001 return 0;
1002 }
1003
1005 {
1006 int i,
r, nb_events_max = 0;
1007
1011 for (
i = 0;
i <
s->nb_tseq;
i++) {
1015 }
1016 if (!
s->nb_events) {
1019 }
1020 if (
s->opt_end_at_last)
1021 s->end_ts =
s->events[
s->nb_events - 1].ts;
1022 return 0;
1023 }
1024
1029 {
1031
1034 /* ref and new intervals are constant, identical and adjacent */
1041 }
1042 }
1055 i->phi =
ref >= 0 ?
ref | 0x80000000 : 0;
1057 }
1058
1061 {
1062 /* SBaGen uses an exponential decrease every 50ms.
1063 We approximate it with piecewise affine segments. */
1071 { 75, 0 },
1072 };
1074 int64_t dt =
s->sample_rate / 20, ts3 =
ts1, ts4;
1078 ts3,
f,
a, ts4,
f, cpoints[
i][1]);
1081 ts3 = ts4;
1083 }
1084 return 0;
1085 }
1086
1089 int64_t
ts1, int64_t
ts2,
1092 int transition)
1093 {
1095
1096 if (
ts2 <=
ts1 || (
s1->vol == 0 &&
s2->vol == 0))
1097 return 0;
1100 break;
1102 if (
s1->beat == 0 &&
s2->beat == 0) {
1108 s2->ref.l =
s2->ref.r =
r;
1109 } else {
1111 ts1,
s1->carrier +
s1->beat / 2,
s1->vol,
1112 ts2,
s2->carrier +
s2->beat / 2,
s2->vol);
1117 ts1,
s1->carrier -
s1->beat / 2,
s1->vol,
1118 ts2,
s2->carrier -
s2->beat / 2,
s2->vol);
1122 }
1123 break;
1124
1126 if (transition == 2) {
1130 }
1131 break;
1132
1135 "using pink noise instead.\n");
1136 /* fall through */
1138 /* SBaGen's pink noise generator uses:
1139 - 1 band of white noise, mean square: 1/3;
1140 - 9 bands of subsampled white noise with linear
1141 interpolation, mean square: 2/3 each;
1142 with 1/10 weight each: the total mean square is 7/300.
1143 Our pink noise generator uses 8 bands of white noise with
1144 rectangular subsampling: the total mean square is 1/24.
1145 Therefore, to match SBaGen's volume, we must multiply vol by
1146 sqrt((7/300) / (1/24)) = sqrt(14/25) =~ 0.748
1147 */
1149 ts1, 0,
s1->vol -
s1->vol / 4,
1150 ts2, 0,
s2->vol -
s2->vol / 4);
1153 s2->ref.l =
s2->ref.r =
r;
1154 break;
1155
1157 /* Unimplemented: silence; warning present elsewhere */
1158 default:
1160 "Type %d is not implemented\n",
s1->type);
1162 }
1163 return 0;
1164 }
1165
1169 {
1173
1179 }
1180 return 0;
1181 }
1182
1183 /*
1184
1185 ts1 ts2 ts1 tsmid ts2
1186 | | | | |
1187 v v v | v
1188 ____ ____ v ____
1189 ''''.... ''.. ..''
1190 ''''....____ ''....''
1191
1192 compatible transition incompatible transition
1193 */
1194
1199 {
1201 /* (ts1 + ts2) / 2 without overflow */
1202 int64_t tsmid = (ts1 >> 1) + (ts2 >> 1) + (ts1 & ts2 & 1);
1207
1209 /* pass = 0 -> compatible and first half of incompatible
1210 pass = 1 -> second half of incompatible
1211 Using two passes like that ensures that the intervals are generated
1212 in increasing order according to their start timestamp.
1213 Otherwise it would be necessary to sort them
1214 while keeping the mutual references.
1215 */
1216 for (
i = 0;
i < nb_elements;
i++) {
1222 /* for slides, and only for slides, silence ("-") is equivalent
1223 to anything with volume 0 */
1225 s1mod = s2mod;
1228 s2mod = s1mod;
1230 }
1231 }
1237 /* compatible: single transition */
1240 ts1, ts2, &s1mod, &s2mod, 3);
1243 s2->ref = s2mod.
ref;
1244 }
1245 } else {
1246 /* incompatible: silence at midpoint */
1248 smid = s1mod;
1251 ts1, tsmid, &s1mod, &smid, 1);
1254 } else {
1255 smid = s2mod;
1258 tsmid, ts2, &smid, &s2mod, 2);
1261 s2->ref = s2mod.
ref;
1262 }
1263 }
1264 }
1265 }
1266 return 0;
1267 }
1268
1269 /*
1270 ev1 trats ev2 intts endts ev3
1271 | | | | | |
1272 v v v v v v
1273 ________________
1274 .... .... ....
1275 '''....________________....''' '''...._______________
1276
1277 \_________/\______________/\_________/\______________/\_________/\_____________/
1278 tr x->1 int1 tr 1->2 int2 tr 2->3 int3
1279 */
1280
1283 {
1284 int64_t trans_time =
s->opt_fade_time / 2;
1288
1289 /* SBaGen handles the time before and after the extremal events,
1290 and the corresponding transitions, as if the sequence were cyclic
1291 with a 24-hours period. */
1292 period =
s->events[
s->nb_events - 1].ts -
s->events[0].ts;
1295
1296 /* Prepare timestamps for transitions */
1297 for (
i = 0;
i <
s->nb_events;
i++) {
1298 ev1 = &
s->events[
i];
1299 ev2 = &
s->events[(
i + 1) %
s->nb_events];
1301
1304
1306 : ev2->
ts + (ev1 < ev2 ? 0 :
period);
1307 }
1308 for (
i = 0;
i <
s->nb_events;
i++) {
1309 ev1 = &
s->events[
i];
1310 ev2 = &
s->events[(
i + 1) %
s->nb_events];
1314 }
1316 }
1317
1318 /* Pseudo event before the first one */
1319 ev0 =
s->events[
s->nb_events - 1];
1323
1324 /* Convert timestamps */
1325 for (
i = -1;
i <
s->nb_events;
i++) {
1326 ev1 =
i < 0 ? &ev0 : &
s->events[
i];
1330 }
1331
1332 /* Generate intervals */
1333 for (
i = 0;
i <
s->nb_synth;
i++)
1334 s->synth[
i].ref.l =
s->synth[
i].ref.r = -1;
1335 for (
i = -1;
i <
s->nb_events;
i++) {
1336 ev1 =
i < 0 ? &ev0 : &
s->events[
i];
1337 ev2 = &
s->events[(
i + 1) %
s->nb_events];
1344 }
1347 return 0;
1348 }
1349
1352 {
1353 int i, edata_size = 4,
ret;
1354 uint8_t *edata;
1355
1359 if (edata_size < 0)
1361 }
1365
1366 #define ADD_EDATA32(v) do { AV_WL32(edata, (v)); edata += 4; } while(0)
1367 #define ADD_EDATA64(v) do { AV_WL64(edata, (v)); edata += 8; } while(0)
1381 break;
1385 break;
1386 }
1387 }
1388 if (edata != par->
extradata + edata_size)
1390 return 0;
1391 }
1392
1394 {
1397
1402 return score;
1403 }
1404
1406 {
1414
1423 else
1429 "-m is ignored and mix channels will be silent.\n");
1437
1441 }
1442
1444 if (!st)
1463
1466 return 0;
1467
1473 }
1474
1476 {
1477 int64_t ts, end_ts;
1479
1484 end_ts);
1485 if (end_ts <= ts)
1489 packet->
dts = packet->
pts = ts;
1493 return packet->
size;
1494 }
1495
1497 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
1498 {
1499 if (
flags || stream_index > 0)
1501 if (stream_index < 0)
1504 return 0;
1505 }
1506
1508 int64_t ts,
int flags)
1509 {
1511 }
1512
1520 {
"max_file_size",
"", offsetof(
struct sbg_demuxer, max_file_size),
1524 };
1525
1531 };
1532
1542 .extensions = "sbg",
1544 };