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;
76  int elements, nb_elements;
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;
100  int64_t ts_int, ts_trans, ts_next;
101  int elements, nb_elements;
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;
132  int nb_def_max, nb_synth_max, nb_tseq_max, nb_block_tseq_max;
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,
1488  AV_OPT_FLAG_DECODING_PARAM },
1489  { "max_file_size", "", offsetof(struct sbg_demuxer, max_file_size),
1490  AV_OPT_TYPE_INT, { .i64 = 5000000 }, 0, INT_MAX,
1491  AV_OPT_FLAG_DECODING_PARAM },
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 };
int64_t ts_trans
Definition: sbgdec.c:100
char * cursor
Definition: sbgdec.c:128
char * opt_mix
Definition: sbgdec.c:119
float v
const char * s
Definition: avisynth_c.h:668
Bytestream IO Context.
Definition: avio.h:68
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int sample_rate
Definition: sbgdec.c:120
static int parse_synth_channel_bell(struct sbg_parser *p, struct sbg_script_synth *synth)
Definition: sbgdec.c:649
static av_cold int sbg_read_header(AVFormatContext *avf)
Definition: sbgdec.c:1381
FIXME Range Coding of cr are ref
Definition: snow.txt:367
AVOption.
Definition: opt.h:251
av_default_item_name
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:1173
static av_cold int sbg_read_probe(AVProbeData *p)
Definition: sbgdec.c:1369
void * av_realloc_f(void *ptr, size_t nelem, size_t elsize)
Allocate or reallocate a block of memory.
Definition: mem.c:168
int64_t end_ts
Definition: sbgdec.c:116
struct sbg_script_event * events
Definition: sbgdec.c:110
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
uint32_t channels
Definition: ffwavesynth.c:87
uint8_t opt_start_at_first
Definition: sbgdec.c:121
int probe_packets
Definition: avformat.h:793
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
static int parse_immediate(struct sbg_parser *p)
Definition: sbgdec.c:321
ws_interval_type
Definition: ffwavesynth.c:77
int nb_synth_max
Definition: sbgdec.c:132
static int parse_timestamp(struct sbg_parser *p, struct sbg_timestamp *rts, int64_t *rrel)
Definition: sbgdec.c:458
Sinusoidal phase f
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
#define pass
Definition: fft.c:335
static const AVOption sbg_options[]
Definition: sbgdec.c:1482
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:1037
char type
Definition: sbgdec.c:70
#define a1
Definition: regdef.h:47
#define FF_ARRAY_ELEMS(a)
#define AV_CH_LAYOUT_STEREO
char * script
Definition: sbgdec.c:127
static int expand_tseq(void *log, struct sbg_script *s, int *nb_ev_max, int64_t t0, struct sbg_script_tseq *tseq)
Definition: sbgdec.c:940
static int parse_volume(struct sbg_parser *p, int *vol)
Definition: sbgdec.c:602
int8_t in
Definition: sbgdec.c:54
#define DAY
Definition: sbgdec.c:32
static void expand_timestamps(void *log, struct sbg_script *s)
Definition: sbgdec.c:886
static int parse_synth_channel(struct sbg_parser *p)
Definition: sbgdec.c:704
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
set threshold d
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
Format I/O context.
Definition: avformat.h:944
static int scale_double(void *log, double d, double m, int *r)
Definition: sbgdec.c:208
#define AV_WL32(p, darg)
Definition: intreadwrite.h:282
int64_t cur_dts
Definition: avformat.h:785
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:55
#define SBG_SCALE
Definition: sbgdec.c:31
uint8_t
#define av_cold
Definition: attributes.h:78
float delta
mode
Definition: f_perms.c:27
AVOptions.
window constants for m
int32_t f1
Definition: sbgdec.c:146
static int sbg_read_seek(AVFormatContext *avf, int stream_index, int64_t ts, int flags)
Definition: sbgdec.c:1476
int nb_events
Definition: sbgdec.c:113
int64_t ts1
Definition: sbgdec.c:143
end end
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
AVStream ** streams
Definition: avformat.h:992
int nb_tseq
Definition: sbgdec.c:112
#define t0
Definition: regdef.h:28
double strtod(const char *, char **)
static int parse_named_def(struct sbg_parser *p)
Definition: sbgdec.c:754
uint8_t * data
#define AVERROR_EOF
End of file.
Definition: error.h:55
int64_t opt_duration
Definition: sbgdec.c:118
static int lex_wsword(struct sbg_parser *p, struct sbg_string *rs)
Definition: sbgdec.c:279
int nb_block_tseq
Definition: sbgdec.c:131
enum ws_interval_type type
Definition: ffwavesynth.c:88
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
sbg_synth_type
Definition: sbgdec.c:57
int nb_def
Definition: sbgdec.c:111
static void free_script(struct sbg_script *s)
Definition: sbgdec.c:781
f2
Definition: genspecsines3.m:4
struct sbg_timestamp ts
Definition: sbgdec.c:91
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
Definition: avutil.h:250
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:130
struct sbg_fade fade
Definition: sbgdec.c:102
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:73
int line_no
Definition: sbgdec.c:133
int nb_synth
Definition: sbgdec.c:114
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
#define s2
Definition: regdef.h:39
int frame_size
Definition: sbgdec.c:38
struct sbg_script_synth * synth
Definition: sbgdec.c:107
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const char * r
Definition: vf_curves.c:94
struct ws_interval * inter
Definition: sbgdec.c:152
static int lex_double(struct sbg_parser *p, double *r)
Definition: sbgdec.c:237
int nb_block_tseq_max
Definition: sbgdec.c:132
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int max_inter
Definition: sbgdec.c:154
int32_t a1
Definition: sbgdec.c:147
int64_t start_ts
Definition: sbgdec.c:115
#define FFMAX(a, b)
Definition: common.h:56
int size
uint64_t channel_layout
Audio channel layout.
static int parse_synth_channel_pink(struct sbg_parser *p, struct sbg_script_synth *synth)
Definition: sbgdec.c:636
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:662
FFmpeg Automated Testing Environment ************************************Table of Contents *****************FFmpeg Automated Testing Environment Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass target exec to configure or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script tests fate sh from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at doc fate_config sh template Create a configuration that suits your based on the configuration template The slot configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern< arch >< os >< compiler >< compiler version > The configuration file itself will be sourced in a shell script
Definition: fate.txt:34
int64_t ts
Definition: sbgdec.c:99
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:337
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:336
int64_t opt_fade_time
Definition: sbgdec.c:117
static int read_whole_file(AVIOContext *io, int max_size, char **rbuf)
Definition: sbgdec.c:853
uint64_t phi
Definition: ffwavesynth.c:86
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:118
struct sbg_script_definition * def
Definition: sbgdec.c:106
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:196
#define FFMIN(a, b)
Definition: common.h:58
static int parse_block_def(struct sbg_parser *p, struct sbg_script_definition *def)
Definition: sbgdec.c:573
ret
Definition: avfilter.c:821
static int parse_synth_channel_mix(struct sbg_parser *p, struct sbg_script_synth *synth)
Definition: sbgdec.c:668
struct sbg_fade fade
Definition: sbgdec.c:95
t
Definition: genspecsines3.m:6
int32_t
#define a2
Definition: regdef.h:48
static void * alloc_array_elem(void **array, size_t elsize, int *size, int *max_size)
Definition: sbgdec.c:157
enum sbg_synth_type type
Definition: sbgdec.c:84
struct sbg_timestamp current_time
Definition: sbgdec.c:130
#define DAY_TS
Definition: sbgdec.c:33
char * end
Definition: sbgdec.c:127
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
static int generate_intervals(void *log, struct sbg_script *s, int sample_rate, struct ws_intervals *inter)
Definition: sbgdec.c:1259
Stream structure.
Definition: avformat.h:643
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
static int sbg_read_packet(AVFormatContext *avf, AVPacket *packet)
Definition: sbgdec.c:1445
static int parse_synth_def(struct sbg_parser *p, struct sbg_script_definition *def)
Definition: sbgdec.c:730
static int parse_wave_def(struct sbg_parser *p, int wavenum)
Definition: sbgdec.c:566
int frame_size
Number of samples per channel in an audio frame.
struct sbg_script_tseq * tseq
Definition: sbgdec.c:108
NULL
Definition: eval.c:55
enum AVMediaType codec_type
enum AVCodecID codec_id
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:202
int sample_rate
samples per second
AVIOContext * pb
I/O context.
Definition: avformat.h:977
static int is_space(char c)
Definition: sbgdec.c:203
int nb_tseq_max
Definition: sbgdec.c:132
char * s
Definition: sbgdec.c:43
static int lex_char(struct sbg_parser *p, char c)
Definition: sbgdec.c:229
struct sbg_script_tseq * block_tseq
Definition: sbgdec.c:109
main external API structure.
static int lex_space(struct sbg_parser *p)
Definition: sbgdec.c:220
int max_file_size
Definition: sbgdec.c:39
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:1003
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
void * buf
Definition: avisynth_c.h:594
int64_t ts_next
Definition: sbgdec.c:100
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition: error.h:50
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
Describe the class of an AVClass context structure.
Definition: log.h:50
synthesis window for stochastic i
static int current_time
Definition: ffmpeg.c:133
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
Definition: opt.h:282
#define FORWARD_ERROR(c)
Definition: sbgdec.c:314
int sample_rate
Definition: sbgdec.c:37
static int encode_intervals(struct sbg_script *s, AVCodecContext *avc, struct ws_intervals *inter)
Definition: sbgdec.c:1324
#define s1
Definition: regdef.h:38
#define snprintf
Definition: snprintf.h:34
int8_t out
Definition: sbgdec.c:54
int64_t t
Definition: sbgdec.c:69
This structure contains the data a format has to probe a file.
Definition: avformat.h:334
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
#define type
int nb_def_max
Definition: sbgdec.c:132
static const AVClass sbg_demuxer_class
Definition: sbgdec.c:1495
static int parse_fade(struct sbg_parser *p, struct sbg_fade *fr)
Definition: sbgdec.c:489
char err_msg[128]
Definition: sbgdec.c:134
static int parse_preprogrammed(struct sbg_parser *p)
Definition: sbgdec.c:328
static int flags
Definition: cpu.c:23
static int generate_plateau(void *log, struct sbg_script *s, struct ws_intervals *inter, struct sbg_script_event *ev1)
Definition: sbgdec.c:1144
uint32_t phi
Definition: sbgdec.c:148
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:696
static int parse_synth_channel_sine(struct sbg_parser *p, struct sbg_script_synth *synth)
Definition: sbgdec.c:615
#define ADD_EDATA32(v)
struct sbg_script scs
Definition: sbgdec.c:129
#define AVPROBE_SCORE_MAX
maximum score, half of that is used for file-extension-based detection
Definition: avformat.h:340
static int parse_synth_channel_spin(struct sbg_parser *p, struct sbg_script_synth *synth)
Definition: sbgdec.c:681
int name_len
Definition: sbgdec.c:93
static int parse_time_sequence(struct sbg_parser *p, int inblock)
Definition: sbgdec.c:513
Main libavformat public API header.
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:1465
static int str_to_time(const char *str, int64_t *rtime)
Definition: sbgdec.c:177
static int lex_line_end(struct sbg_parser *p)
Definition: sbgdec.c:261
#define AV_WL64(p, darg)
Definition: intreadwrite.h:328
int nb_inter
Definition: sbgdec.c:153
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
Definition: avformat.h:689
static double c[64]
static int expand_script(void *log, struct sbg_script *s)
Definition: sbgdec.c:984
sbg_fade_type
Definition: sbgdec.c:47
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:1065
void * log
Definition: sbgdec.c:126
static int parse_script(void *log, char *script, int script_len, struct sbg_script *rscript)
Definition: sbgdec.c:791
#define ADD_EDATA64(v)
AVInputFormat ff_sbg_demuxer
Definition: sbgdec.c:1502
int32_t f2
Definition: sbgdec.c:146
int64_t ts_int
Definition: sbgdec.c:100
int channels
number of audio channels
uint8_t opt_end_at_last
Definition: sbgdec.c:122
void * priv_data
Format private data.
Definition: avformat.h:964
static int parse_options(struct sbg_parser *p)
Definition: sbgdec.c:345
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
int32_t a2
Definition: sbgdec.c:147
#define AV_LOG_INFO
Definition: log.h:156
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:461
static int lex_name(struct sbg_parser *p, struct sbg_string *rs)
Definition: sbgdec.c:293
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
static int parse_optarg(struct sbg_parser *p, char o, struct sbg_string *r)
Definition: sbgdec.c:335
struct sbg_script_synth::@155 ref
static int lex_fixed(struct sbg_parser *p, const char *t, int l)
Definition: sbgdec.c:253
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:679
int64_t ts2
Definition: sbgdec.c:143
#define MKTAG(a, b, c, d)
Definition: common.h:282
This structure stores compressed data.
char * name
Definition: sbgdec.c:92
f1
Definition: genspecsines3.m:3
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
for(j=16;j >0;--j)
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190
static int lex_time(struct sbg_parser *p, int64_t *rt)
Definition: sbgdec.c:307
int8_t slide
Definition: sbgdec.c:54
char * e
Definition: sbgdec.c:44