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