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