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