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