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