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