00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <time.h>
00025 #include "libavutil/intreadwrite.h"
00026 #include "libavutil/log.h"
00027 #include "libavutil/opt.h"
00028 #include "avformat.h"
00029 #include "internal.h"
00030
00031 #define SBG_SCALE (1 << 16)
00032 #define DAY (24 * 60 * 60)
00033 #define DAY_TS ((int64_t)DAY * AV_TIME_BASE)
00034
00035 struct sbg_demuxer {
00036 AVClass *class;
00037 int sample_rate;
00038 int frame_size;
00039 int max_file_size;
00040 };
00041
00042 struct sbg_string {
00043 char *s;
00044 char *e;
00045 };
00046
00047 enum sbg_fade_type {
00048 SBG_FADE_SILENCE = 0,
00049 SBG_FADE_SAME = 1,
00050 SBG_FADE_ADAPT = 3,
00051 };
00052
00053 struct sbg_fade {
00054 int8_t in, out, slide;
00055 };
00056
00057 enum sbg_synth_type {
00058 SBG_TYPE_NONE,
00059 SBG_TYPE_SINE,
00060 SBG_TYPE_NOISE,
00061 SBG_TYPE_BELL,
00062 SBG_TYPE_MIX,
00063 SBG_TYPE_SPIN,
00064 };
00065
00066
00067
00068 struct sbg_timestamp {
00069 int64_t t;
00070 char type;
00071 };
00072
00073 struct sbg_script_definition {
00074 char *name;
00075 int name_len;
00076 int elements, nb_elements;
00077 char type;
00078 };
00079
00080 struct sbg_script_synth {
00081 int carrier;
00082 int beat;
00083 int vol;
00084 enum sbg_synth_type type;
00085 struct {
00086 int l, r;
00087 } ref;
00088 };
00089
00090 struct sbg_script_tseq {
00091 struct sbg_timestamp ts;
00092 char *name;
00093 int name_len;
00094 int lock;
00095 struct sbg_fade fade;
00096 };
00097
00098 struct sbg_script_event {
00099 int64_t ts;
00100 int64_t ts_int, ts_trans, ts_next;
00101 int elements, nb_elements;
00102 struct sbg_fade fade;
00103 };
00104
00105 struct sbg_script {
00106 struct sbg_script_definition *def;
00107 struct sbg_script_synth *synth;
00108 struct sbg_script_tseq *tseq;
00109 struct sbg_script_tseq *block_tseq;
00110 struct sbg_script_event *events;
00111 int nb_def;
00112 int nb_tseq;
00113 int nb_events;
00114 int nb_synth;
00115 int64_t start_ts;
00116 int64_t end_ts;
00117 int64_t opt_fade_time;
00118 int64_t opt_duration;
00119 char *opt_mix;
00120 int sample_rate;
00121 uint8_t opt_start_at_first;
00122 uint8_t opt_end_at_last;
00123 };
00124
00125 struct sbg_parser {
00126 void *log;
00127 char *script, *end;
00128 char *cursor;
00129 struct sbg_script scs;
00130 struct sbg_timestamp current_time;
00131 int nb_block_tseq;
00132 int nb_def_max, nb_synth_max, nb_tseq_max, nb_block_tseq_max;
00133 int line_no;
00134 char err_msg[128];
00135 };
00136
00137 enum ws_interval_type {
00138 WS_SINE = MKTAG('S','I','N','E'),
00139 WS_NOISE = MKTAG('N','O','I','S'),
00140 };
00141
00142 struct ws_interval {
00143 int64_t ts1, ts2;
00144 enum ws_interval_type type;
00145 uint32_t channels;
00146 int32_t f1, f2;
00147 int32_t a1, a2;
00148 uint32_t phi;
00149 };
00150
00151 struct ws_intervals {
00152 struct ws_interval *inter;
00153 int nb_inter;
00154 int max_inter;
00155 };
00156
00157 static void *alloc_array_elem(void **array, size_t elsize,
00158 int *size, int *max_size)
00159 {
00160 void *ret;
00161
00162 if (*size == *max_size) {
00163 int m = FFMAX(32, FFMIN(*max_size, INT_MAX / 2) * 2);
00164 if (*size >= m)
00165 return NULL;
00166 *array = av_realloc_f(*array, m, elsize);
00167 if (!*array)
00168 return NULL;
00169 *max_size = m;
00170 }
00171 ret = (char *)*array + elsize * *size;
00172 memset(ret, 0, elsize);
00173 (*size)++;
00174 return ret;
00175 }
00176
00177 static int str_to_time(const char *str, int64_t *rtime)
00178 {
00179 const char *cur = str;
00180 char *end;
00181 int hours, minutes;
00182 double seconds = 0;
00183
00184 if (*cur < '0' || *cur > '9')
00185 return 0;
00186 hours = strtol(cur, &end, 10);
00187 if (end == cur || *end != ':' || end[1] < '0' || end[1] > '9')
00188 return 0;
00189 cur = end + 1;
00190 minutes = strtol(cur, &end, 10);
00191 if (end == cur)
00192 return 0;
00193 cur = end;
00194 if (*end == ':'){
00195 seconds = strtod(cur + 1, &end);
00196 if (end > cur + 1)
00197 cur = end;
00198 }
00199 *rtime = (hours * 3600 + minutes * 60 + seconds) * AV_TIME_BASE;
00200 return cur - str;
00201 }
00202
00203 static inline int is_space(char c)
00204 {
00205 return c == ' ' || c == '\t' || c == '\r';
00206 }
00207
00208 static inline int scale_double(void *log, double d, double m, int *r)
00209 {
00210 m *= d * SBG_SCALE;
00211 if (m < INT_MIN || m >= INT_MAX) {
00212 if (log)
00213 av_log(log, AV_LOG_ERROR, "%g is too large\n", d);
00214 return AVERROR(EDOM);
00215 }
00216 *r = m;
00217 return 0;
00218 }
00219
00220 static int lex_space(struct sbg_parser *p)
00221 {
00222 char *c = p->cursor;
00223
00224 while (p->cursor < p->end && is_space(*p->cursor))
00225 p->cursor++;
00226 return p->cursor > c;
00227 }
00228
00229 static int lex_char(struct sbg_parser *p, char c)
00230 {
00231 int r = p->cursor < p->end && *p->cursor == c;
00232
00233 p->cursor += r;
00234 return r;
00235 }
00236
00237 static int lex_double(struct sbg_parser *p, double *r)
00238 {
00239 double d;
00240 char *end;
00241
00242 if (p->cursor == p->end || is_space(*p->cursor) || *p->cursor == '\n')
00243 return 0;
00244 d = strtod(p->cursor, &end);
00245 if (end > p->cursor) {
00246 *r = d;
00247 p->cursor = end;
00248 return 1;
00249 }
00250 return 0;
00251 }
00252
00253 static int lex_fixed(struct sbg_parser *p, const char *t, int l)
00254 {
00255 if (p->end - p->cursor < l || memcmp(p->cursor, t, l))
00256 return 0;
00257 p->cursor += l;
00258 return 1;
00259 }
00260
00261 static int lex_line_end(struct sbg_parser *p)
00262 {
00263 if (p->cursor < p->end && *p->cursor == '#') {
00264 p->cursor++;
00265 while (p->cursor < p->end && *p->cursor != '\n')
00266 p->cursor++;
00267 }
00268 if (p->cursor == p->end)
00269
00270 return 1;
00271 if (*p->cursor != '\n')
00272 return 0;
00273 p->cursor++;
00274 p->line_no++;
00275 lex_space(p);
00276 return 1;
00277 }
00278
00279 static int lex_wsword(struct sbg_parser *p, struct sbg_string *rs)
00280 {
00281 char *s = p->cursor, *c = s;
00282
00283 if (s == p->end || *s == '\n')
00284 return 0;
00285 while (c < p->end && *c != '\n' && !is_space(*c))
00286 c++;
00287 rs->s = s;
00288 rs->e = p->cursor = c;
00289 lex_space(p);
00290 return 1;
00291 }
00292
00293 static int lex_name(struct sbg_parser *p, struct sbg_string *rs)
00294 {
00295 char *s = p->cursor, *c = s;
00296
00297 while (c < p->end && ((*c >= 'a' && *c <= 'z') || (*c >= 'A' && *c <= 'Z')
00298 || (*c >= '0' && *c <= '9') || *c == '_' || *c == '-'))
00299 c++;
00300 if (c == s)
00301 return 0;
00302 rs->s = s;
00303 rs->e = p->cursor = c;
00304 return 1;
00305 }
00306
00307 static int lex_time(struct sbg_parser *p, int64_t *rt)
00308 {
00309 int r = str_to_time(p->cursor, rt);
00310 p->cursor += r;
00311 return r > 0;
00312 }
00313
00314 #define FORWARD_ERROR(c) \
00315 do { \
00316 int errcode = c; \
00317 if (errcode <= 0) \
00318 return errcode ? errcode : AVERROR_INVALIDDATA; \
00319 } while(0);
00320
00321 static int parse_immediate(struct sbg_parser *p)
00322 {
00323 snprintf(p->err_msg, sizeof(p->err_msg),
00324 "immediate sequences not yet implemented");
00325 return AVERROR_PATCHWELCOME;
00326 }
00327
00328 static int parse_preprogrammed(struct sbg_parser *p)
00329 {
00330 snprintf(p->err_msg, sizeof(p->err_msg),
00331 "preprogrammed sequences not yet implemented");
00332 return AVERROR_PATCHWELCOME;
00333 }
00334
00335 static int parse_optarg(struct sbg_parser *p, char o, struct sbg_string *r)
00336 {
00337 if (!lex_wsword(p, r)) {
00338 snprintf(p->err_msg, sizeof(p->err_msg),
00339 "option '%c' requires an argument", o);
00340 return AVERROR_INVALIDDATA;
00341 }
00342 return 1;
00343 }
00344
00345 static int parse_options(struct sbg_parser *p)
00346 {
00347 struct sbg_string ostr, oarg;
00348 char mode = 0;
00349 int r;
00350 char *tptr;
00351 double v;
00352
00353 if (p->cursor == p->end || *p->cursor != '-')
00354 return 0;
00355 while (lex_char(p, '-') && lex_wsword(p, &ostr)) {
00356 for (; ostr.s < ostr.e; ostr.s++) {
00357 char opt = *ostr.s;
00358 switch (opt) {
00359 case 'S':
00360 p->scs.opt_start_at_first = 1;
00361 break;
00362 case 'E':
00363 p->scs.opt_end_at_last = 1;
00364 break;
00365 case 'i':
00366 mode = 'i';
00367 break;
00368 case 'p':
00369 mode = 'p';
00370 break;
00371 case 'F':
00372 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
00373 v = strtod(oarg.s, &tptr);
00374 if (oarg.e != tptr) {
00375 snprintf(p->err_msg, sizeof(p->err_msg),
00376 "syntax error for option -F");
00377 return AVERROR_INVALIDDATA;
00378 }
00379 p->scs.opt_fade_time = v * AV_TIME_BASE / 1000;
00380 break;
00381 case 'L':
00382 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
00383 r = str_to_time(oarg.s, &p->scs.opt_duration);
00384 if (oarg.e != oarg.s + r) {
00385 snprintf(p->err_msg, sizeof(p->err_msg),
00386 "syntax error for option -L");
00387 return AVERROR_INVALIDDATA;
00388 }
00389 break;
00390 case 'T':
00391 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
00392 r = str_to_time(oarg.s, &p->scs.start_ts);
00393 if (oarg.e != oarg.s + r) {
00394 snprintf(p->err_msg, sizeof(p->err_msg),
00395 "syntax error for option -T");
00396 return AVERROR_INVALIDDATA;
00397 }
00398 break;
00399 case 'm':
00400 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
00401 tptr = av_malloc(oarg.e - oarg.s + 1);
00402 if (!tptr)
00403 return AVERROR(ENOMEM);
00404 memcpy(tptr, oarg.s, oarg.e - oarg.s);
00405 tptr[oarg.e - oarg.s] = 0;
00406 av_free(p->scs.opt_mix);
00407 p->scs.opt_mix = tptr;
00408 break;
00409 case 'q':
00410 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
00411 v = strtod(oarg.s, &tptr);
00412 if (oarg.e != tptr) {
00413 snprintf(p->err_msg, sizeof(p->err_msg),
00414 "syntax error for option -q");
00415 return AVERROR_INVALIDDATA;
00416 }
00417 if (v != 1) {
00418 snprintf(p->err_msg, sizeof(p->err_msg),
00419 "speed factor other than 1 not supported");
00420 return AVERROR_PATCHWELCOME;
00421 }
00422 break;
00423 case 'r':
00424 FORWARD_ERROR(parse_optarg(p, opt, &oarg));
00425 r = strtol(oarg.s, &tptr, 10);
00426 if (oarg.e != tptr) {
00427 snprintf(p->err_msg, sizeof(p->err_msg),
00428 "syntax error for option -r");
00429 return AVERROR_INVALIDDATA;
00430 }
00431 if (r < 40) {
00432 snprintf(p->err_msg, sizeof(p->err_msg),
00433 "invalid sample rate");
00434 return AVERROR_PATCHWELCOME;
00435 }
00436 p->scs.sample_rate = r;
00437 break;
00438 default:
00439 snprintf(p->err_msg, sizeof(p->err_msg),
00440 "unknown option: '%c'", *ostr.s);
00441 return AVERROR_INVALIDDATA;
00442 }
00443 }
00444 }
00445 switch (mode) {
00446 case 'i':
00447 return parse_immediate(p);
00448 case 'p':
00449 return parse_preprogrammed(p);
00450 case 0:
00451 if (!lex_line_end(p))
00452 return AVERROR_INVALIDDATA;
00453 return 1;
00454 }
00455 return AVERROR_BUG;
00456 }
00457
00458 static int parse_timestamp(struct sbg_parser *p,
00459 struct sbg_timestamp *rts, int64_t *rrel)
00460 {
00461 int64_t abs = 0, rel = 0, dt;
00462 char type = 0;
00463 int r;
00464
00465 if (lex_fixed(p, "NOW", 3)) {
00466 type = 'N';
00467 r = 1;
00468 } else {
00469 r = lex_time(p, &abs);
00470 if (r)
00471 type = 'T';
00472 }
00473 while (lex_char(p, '+')) {
00474 if (!lex_time(p, &dt))
00475 return AVERROR_INVALIDDATA;
00476 rel += dt;
00477 r = 1;
00478 }
00479 if (r) {
00480 if (!lex_space(p))
00481 return AVERROR_INVALIDDATA;
00482 rts->type = type;
00483 rts->t = abs;
00484 *rrel = rel;
00485 }
00486 return r;
00487 }
00488
00489 static int parse_fade(struct sbg_parser *p, struct sbg_fade *fr)
00490 {
00491 struct sbg_fade f;
00492
00493 if (lex_char(p, '<'))
00494 f.in = SBG_FADE_SILENCE;
00495 else if (lex_char(p, '-'))
00496 f.in = SBG_FADE_SAME;
00497 else if (lex_char(p, '='))
00498 f.in = SBG_FADE_ADAPT;
00499 else
00500 return 0;
00501 if (lex_char(p, '>'))
00502 f.out = SBG_FADE_SILENCE;
00503 else if (lex_char(p, '-'))
00504 f.out = SBG_FADE_SAME;
00505 else if (lex_char(p, '='))
00506 f.out = SBG_FADE_ADAPT;
00507 else
00508 return AVERROR_INVALIDDATA;
00509 *fr = f;
00510 return 1;
00511 }
00512
00513 static int parse_time_sequence(struct sbg_parser *p, int inblock)
00514 {
00515 struct sbg_timestamp ts;
00516 int64_t rel_ts;
00517 int r;
00518 struct sbg_fade fade = { SBG_FADE_SAME, SBG_FADE_SAME, 0 };
00519 struct sbg_string name;
00520 struct sbg_script_tseq *tseq;
00521
00522 r = parse_timestamp(p, &ts, &rel_ts);
00523 if (!r)
00524 return 0;
00525 if (r < 0)
00526 return r;
00527 if (ts.type) {
00528 if (inblock)
00529 return AVERROR_INVALIDDATA;
00530 p->current_time.type = ts.type;
00531 p->current_time.t = ts.t;
00532 } else if(!inblock && !p->current_time.type) {
00533 snprintf(p->err_msg, sizeof(p->err_msg),
00534 "relative time without previous absolute time");
00535 return AVERROR_INVALIDDATA;
00536 }
00537 ts.type = p->current_time.type;
00538 ts.t = p->current_time.t + rel_ts;
00539 r = parse_fade(p, &fade);
00540 if (r < 0)
00541 return r;
00542 lex_space(p);
00543 if (!lex_name(p, &name))
00544 return AVERROR_INVALIDDATA;
00545 lex_space(p);
00546 if (lex_fixed(p, "->", 2)) {
00547 fade.slide = SBG_FADE_ADAPT;
00548 lex_space(p);
00549 }
00550 if (!lex_line_end(p))
00551 return AVERROR_INVALIDDATA;
00552 tseq = inblock ?
00553 alloc_array_elem((void **)&p->scs.block_tseq, sizeof(*tseq),
00554 &p->nb_block_tseq, &p->nb_block_tseq_max) :
00555 alloc_array_elem((void **)&p->scs.tseq, sizeof(*tseq),
00556 &p->scs.nb_tseq, &p->nb_tseq_max);
00557 if (!tseq)
00558 return AVERROR(ENOMEM);
00559 tseq->ts = ts;
00560 tseq->name = name.s;
00561 tseq->name_len = name.e - name.s;
00562 tseq->fade = fade;
00563 return 1;
00564 }
00565
00566 static int parse_wave_def(struct sbg_parser *p, int wavenum)
00567 {
00568 snprintf(p->err_msg, sizeof(p->err_msg),
00569 "waveform definitions not yet implemented");
00570 return AVERROR_PATCHWELCOME;
00571 }
00572
00573 static int parse_block_def(struct sbg_parser *p,
00574 struct sbg_script_definition *def)
00575 {
00576 int r, tseq;
00577
00578 lex_space(p);
00579 if (!lex_line_end(p))
00580 return AVERROR_INVALIDDATA;
00581 tseq = p->nb_block_tseq;
00582 while (1) {
00583 r = parse_time_sequence(p, 1);
00584 if (r < 0)
00585 return r;
00586 if (!r)
00587 break;
00588 }
00589 if (!lex_char(p, '}'))
00590 return AVERROR_INVALIDDATA;
00591 lex_space(p);
00592 if (!lex_line_end(p))
00593 return AVERROR_INVALIDDATA;
00594 def->type = 'B';
00595 def->elements = tseq;
00596 def->nb_elements = p->nb_block_tseq - tseq;
00597 if (!def->nb_elements)
00598 return AVERROR_INVALIDDATA;
00599 return 1;
00600 }
00601
00602 static int parse_volume(struct sbg_parser *p, int *vol)
00603 {
00604 double v;
00605
00606 if (!lex_char(p, '/'))
00607 return 0;
00608 if (!lex_double(p, &v))
00609 return AVERROR_INVALIDDATA;
00610 if (scale_double(p->log, v, 0.01, vol))
00611 return AVERROR(ERANGE);
00612 return 1;
00613 }
00614
00615 static int parse_synth_channel_sine(struct sbg_parser *p,
00616 struct sbg_script_synth *synth)
00617 {
00618 double carrierf, beatf;
00619 int carrier, beat, vol;
00620
00621 if (!lex_double(p, &carrierf))
00622 return 0;
00623 if (!lex_double(p, &beatf))
00624 beatf = 0;
00625 FORWARD_ERROR(parse_volume(p, &vol));
00626 if (scale_double(p->log, carrierf, 1, &carrier) < 0 ||
00627 scale_double(p->log, beatf, 1, &beat) < 0)
00628 return AVERROR(EDOM);
00629 synth->type = SBG_TYPE_SINE;
00630 synth->carrier = carrier;
00631 synth->beat = beat;
00632 synth->vol = vol;
00633 return 1;
00634 }
00635
00636 static int parse_synth_channel_pink(struct sbg_parser *p,
00637 struct sbg_script_synth *synth)
00638 {
00639 int vol;
00640
00641 if (!lex_fixed(p, "pink", 4))
00642 return 0;
00643 FORWARD_ERROR(parse_volume(p, &vol));
00644 synth->type = SBG_TYPE_NOISE;
00645 synth->vol = vol;
00646 return 1;
00647 }
00648
00649 static int parse_synth_channel_bell(struct sbg_parser *p,
00650 struct sbg_script_synth *synth)
00651 {
00652 double carrierf;
00653 int carrier, vol;
00654
00655 if (!lex_fixed(p, "bell", 4))
00656 return 0;
00657 if (!lex_double(p, &carrierf))
00658 return AVERROR_INVALIDDATA;
00659 FORWARD_ERROR(parse_volume(p, &vol));
00660 if (scale_double(p->log, carrierf, 1, &carrier) < 0)
00661 return AVERROR(EDOM);
00662 synth->type = SBG_TYPE_BELL;
00663 synth->carrier = carrier;
00664 synth->vol = vol;
00665 return 1;
00666 }
00667
00668 static int parse_synth_channel_mix(struct sbg_parser *p,
00669 struct sbg_script_synth *synth)
00670 {
00671 int vol;
00672
00673 if (!lex_fixed(p, "mix", 3))
00674 return 0;
00675 FORWARD_ERROR(parse_volume(p, &vol));
00676 synth->type = SBG_TYPE_MIX;
00677 synth->vol = vol;
00678 return 1;
00679 }
00680
00681 static int parse_synth_channel_spin(struct sbg_parser *p,
00682 struct sbg_script_synth *synth)
00683 {
00684 double carrierf, beatf;
00685 int carrier, beat, vol;
00686
00687 if (!lex_fixed(p, "spin:", 5))
00688 return 0;
00689 if (!lex_double(p, &carrierf))
00690 return AVERROR_INVALIDDATA;
00691 if (!lex_double(p, &beatf))
00692 return AVERROR_INVALIDDATA;
00693 FORWARD_ERROR(parse_volume(p, &vol));
00694 if (scale_double(p->log, carrierf, 1, &carrier) < 0 ||
00695 scale_double(p->log, beatf, 1, &beat) < 0)
00696 return AVERROR(EDOM);
00697 synth->type = SBG_TYPE_SPIN;
00698 synth->carrier = carrier;
00699 synth->beat = beat;
00700 synth->vol = vol;
00701 return 1;
00702 }
00703
00704 static int parse_synth_channel(struct sbg_parser *p)
00705 {
00706 int r;
00707 struct sbg_script_synth *synth;
00708
00709 synth = alloc_array_elem((void **)&p->scs.synth, sizeof(*synth),
00710 &p->scs.nb_synth, &p->nb_synth_max);
00711 if (!synth)
00712 return AVERROR(ENOMEM);
00713 r = lex_char(p, '-');
00714 if (!r)
00715 r = parse_synth_channel_pink(p, synth);
00716 if (!r)
00717 r = parse_synth_channel_bell(p, synth);
00718 if (!r)
00719 r = parse_synth_channel_mix(p, synth);
00720 if (!r)
00721 r = parse_synth_channel_spin(p, synth);
00722
00723 if (!r)
00724 r = parse_synth_channel_sine(p, synth);
00725 if (r <= 0)
00726 p->scs.nb_synth--;
00727 return r;
00728 }
00729
00730 static int parse_synth_def(struct sbg_parser *p,
00731 struct sbg_script_definition *def)
00732 {
00733 int r, synth;
00734
00735 synth = p->scs.nb_synth;
00736 while (1) {
00737 r = parse_synth_channel(p);
00738 if (r < 0)
00739 return r;
00740 if (!r || !lex_space(p))
00741 break;
00742 }
00743 lex_space(p);
00744 if (synth == p->scs.nb_synth)
00745 return AVERROR_INVALIDDATA;
00746 if (!lex_line_end(p))
00747 return AVERROR_INVALIDDATA;
00748 def->type = 'S';
00749 def->elements = synth;
00750 def->nb_elements = p->scs.nb_synth - synth;
00751 return 1;
00752 }
00753
00754 static int parse_named_def(struct sbg_parser *p)
00755 {
00756 char *cursor_save = p->cursor;
00757 struct sbg_string name;
00758 struct sbg_script_definition *def;
00759
00760 if (!lex_name(p, &name) || !lex_char(p, ':') || !lex_space(p)) {
00761 p->cursor = cursor_save;
00762 return 0;
00763 }
00764 if (name.e - name.s == 6 && !memcmp(name.s, "wave", 4) &&
00765 name.s[4] >= '0' && name.s[4] <= '9' &&
00766 name.s[5] >= '0' && name.s[5] <= '9') {
00767 int wavenum = (name.s[4] - '0') * 10 + (name.s[5] - '0');
00768 return parse_wave_def(p, wavenum);
00769 }
00770 def = alloc_array_elem((void **)&p->scs.def, sizeof(*def),
00771 &p->scs.nb_def, &p->nb_def_max);
00772 if (!def)
00773 return AVERROR(ENOMEM);
00774 def->name = name.s;
00775 def->name_len = name.e - name.s;
00776 if (lex_char(p, '{'))
00777 return parse_block_def(p, def);
00778 return parse_synth_def(p, def);
00779 }
00780
00781 static void free_script(struct sbg_script *s)
00782 {
00783 av_freep(&s->def);
00784 av_freep(&s->synth);
00785 av_freep(&s->tseq);
00786 av_freep(&s->block_tseq);
00787 av_freep(&s->events);
00788 av_freep(&s->opt_mix);
00789 }
00790
00791 static int parse_script(void *log, char *script, int script_len,
00792 struct sbg_script *rscript)
00793 {
00794 struct sbg_parser sp = {
00795 .log = log,
00796 .script = script,
00797 .end = script + script_len,
00798 .cursor = script,
00799 .line_no = 1,
00800 .err_msg = "",
00801 .scs = {
00802
00803 .start_ts = AV_NOPTS_VALUE,
00804 .sample_rate = 44100,
00805 .opt_fade_time = 60 * AV_TIME_BASE,
00806 },
00807 };
00808 int r;
00809
00810 lex_space(&sp);
00811 while (sp.cursor < sp.end) {
00812 r = parse_options(&sp);
00813 if (r < 0)
00814 goto fail;
00815 if (!r && !lex_line_end(&sp))
00816 break;
00817 }
00818 while (sp.cursor < sp.end) {
00819 r = parse_named_def(&sp);
00820 if (!r)
00821 r = parse_time_sequence(&sp, 0);
00822 if (!r)
00823 r = lex_line_end(&sp) ? 1 : AVERROR_INVALIDDATA;
00824 if (r < 0)
00825 goto fail;
00826 }
00827 *rscript = sp.scs;
00828 return 1;
00829 fail:
00830 free_script(&sp.scs);
00831 if (!*sp.err_msg)
00832 if (r == AVERROR_INVALIDDATA)
00833 snprintf(sp.err_msg, sizeof(sp.err_msg), "syntax error");
00834 if (log && *sp.err_msg) {
00835 const char *ctx = sp.cursor;
00836 const char *ectx = av_x_if_null(memchr(ctx, '\n', sp.end - sp.cursor),
00837 sp.end);
00838 int lctx = ectx - ctx;
00839 const char *quote = "\"";
00840 if (lctx > 0 && ctx[lctx - 1] == '\r')
00841 lctx--;
00842 if (lctx == 0) {
00843 ctx = "the end of line";
00844 lctx = strlen(ctx);
00845 quote = "";
00846 }
00847 av_log(log, AV_LOG_ERROR, "Error line %d: %s near %s%.*s%s.\n",
00848 sp.line_no, sp.err_msg, quote, lctx, ctx, quote);
00849 }
00850 return r;
00851 }
00852
00853 static int read_whole_file(AVIOContext *io, int max_size, char **rbuf)
00854 {
00855 char *buf = NULL;
00856 int size = 0, bufsize = 0, r;
00857
00858 while (1) {
00859 if (bufsize - size < 1024) {
00860 bufsize = FFMIN(FFMAX(2 * bufsize, 8192), max_size);
00861 if (bufsize - size < 2) {
00862 size = AVERROR(EFBIG);
00863 goto fail;
00864 }
00865 buf = av_realloc_f(buf, bufsize, 1);
00866 if (!buf) {
00867 size = AVERROR(ENOMEM);
00868 goto fail;
00869 }
00870 }
00871 r = avio_read(io, buf, bufsize - size - 1);
00872 if (r == AVERROR_EOF)
00873 break;
00874 if (r < 0)
00875 goto fail;
00876 size += r;
00877 }
00878 buf[size] = 0;
00879 *rbuf = buf;
00880 return size;
00881 fail:
00882 av_free(buf);
00883 return size;
00884 }
00885
00886 static void expand_timestamps(void *log, struct sbg_script *s)
00887 {
00888 int i, nb_rel = 0;
00889 int64_t now, cur_ts, delta = 0;
00890
00891 for (i = 0; i < s->nb_tseq; i++)
00892 nb_rel += s->tseq[i].ts.type == 'N';
00893 if (nb_rel == s->nb_tseq) {
00894
00895 now = 0;
00896 if (s->start_ts != AV_NOPTS_VALUE)
00897 av_log(log, AV_LOG_WARNING,
00898 "Start time ignored in a purely relative script.\n");
00899 } else if (nb_rel == 0 && s->start_ts != AV_NOPTS_VALUE ||
00900 s->opt_start_at_first) {
00901
00902 if (s->start_ts == AV_NOPTS_VALUE)
00903 s->start_ts = s->tseq[0].ts.t;
00904 now = s->start_ts;
00905 } else {
00906
00907 time_t now0;
00908 struct tm *tm;
00909
00910 av_log(log, AV_LOG_WARNING,
00911 "Scripts with mixed absolute and relative timestamps can give "
00912 "unexpected results (pause, seeking, time zone change).\n");
00913 #undef time
00914 time(&now0);
00915 tm = localtime(&now0);
00916 now = tm ? tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec :
00917 now0 % DAY;
00918 av_log(log, AV_LOG_INFO, "Using %02d:%02d:%02d as NOW.\n",
00919 (int)(now / 3600), (int)(now / 60) % 60, (int)now % 60);
00920 now *= AV_TIME_BASE;
00921 for (i = 0; i < s->nb_tseq; i++) {
00922 if (s->tseq[i].ts.type == 'N') {
00923 s->tseq[i].ts.t += now;
00924 s->tseq[i].ts.type = 'T';
00925 }
00926 }
00927 }
00928 if (s->start_ts == AV_NOPTS_VALUE)
00929 s->start_ts = s->opt_start_at_first ? s->tseq[0].ts.t : now;
00930 s->end_ts = s->opt_duration ? s->start_ts + s->opt_duration :
00931 AV_NOPTS_VALUE;
00932 cur_ts = now;
00933 for (i = 0; i < s->nb_tseq; i++) {
00934 if (s->tseq[i].ts.t + delta < cur_ts)
00935 delta += DAY_TS;
00936 cur_ts = s->tseq[i].ts.t += delta;
00937 }
00938 }
00939
00940 static int expand_tseq(void *log, struct sbg_script *s, int *nb_ev_max,
00941 int64_t t0, struct sbg_script_tseq *tseq)
00942 {
00943 int i, r;
00944 struct sbg_script_definition *def;
00945 struct sbg_script_tseq *be;
00946 struct sbg_script_event *ev;
00947
00948 if (tseq->lock++) {
00949 av_log(log, 16, "Recursion loop on \"%.*s\"\n",
00950 tseq->name_len, tseq->name);
00951 return AVERROR(EINVAL);
00952 }
00953 t0 += tseq->ts.t;
00954 for (i = 0; i < s->nb_def; i++) {
00955 if (s->def[i].name_len == tseq->name_len &&
00956 !memcmp(s->def[i].name, tseq->name, tseq->name_len))
00957 break;
00958 }
00959 if (i >= s->nb_def) {
00960 av_log(log, 16, "Tone-set \"%.*s\" not defined\n",
00961 tseq->name_len, tseq->name);
00962 return AVERROR(EINVAL);
00963 }
00964 def = &s->def[i];
00965 if (def->type == 'B') {
00966 be = s->block_tseq + def->elements;
00967 for (i = 0; i < def->nb_elements; i++) {
00968 r = expand_tseq(log, s, nb_ev_max, t0, &be[i]);
00969 if (r < 0)
00970 return r;
00971 }
00972 } else {
00973 ev = alloc_array_elem((void **)&s->events, sizeof(*ev),
00974 &s->nb_events, nb_ev_max);
00975 ev->ts = tseq->ts.t;
00976 ev->elements = def->elements;
00977 ev->nb_elements = def->nb_elements;
00978 ev->fade = tseq->fade;
00979 }
00980 tseq->lock--;
00981 return 0;
00982 }
00983
00984 static int expand_script(void *log, struct sbg_script *s)
00985 {
00986 int i, r, nb_events_max = 0;
00987
00988 expand_timestamps(log, s);
00989 for (i = 0; i < s->nb_tseq; i++) {
00990 r = expand_tseq(log, s, &nb_events_max, 0, &s->tseq[i]);
00991 if (r < 0)
00992 return r;
00993 }
00994 if (!s->nb_events) {
00995 av_log(log, AV_LOG_ERROR, "No events in script\n");
00996 return AVERROR_INVALIDDATA;
00997 }
00998 if (s->opt_end_at_last)
00999 s->end_ts = s->events[s->nb_events - 1].ts;
01000 return 0;
01001 }
01002
01003 static int add_interval(struct ws_intervals *inter,
01004 enum ws_interval_type type, uint32_t channels, int ref,
01005 int64_t ts1, int32_t f1, int32_t a1,
01006 int64_t ts2, int32_t f2, int32_t a2)
01007 {
01008 struct ws_interval *i, *ri;
01009
01010 if (ref >= 0) {
01011 ri = &inter->inter[ref];
01012
01013 if (ri->type == type && ri->channels == channels &&
01014 ri->f1 == ri->f2 && ri->f2 == f1 && f1 == f2 &&
01015 ri->a1 == ri->a2 && ri->a2 == a1 && a1 == a2 &&
01016 ri->ts2 == ts1) {
01017 ri->ts2 = ts2;
01018 return ref;
01019 }
01020 }
01021 i = alloc_array_elem((void **)&inter->inter, sizeof(*i),
01022 &inter->nb_inter, &inter->max_inter);
01023 if (!i)
01024 return AVERROR(ENOMEM);
01025 i->ts1 = ts1;
01026 i->ts2 = ts2;
01027 i->type = type;
01028 i->channels = channels;
01029 i->f1 = f1;
01030 i->f2 = f2;
01031 i->a1 = a1;
01032 i->a2 = a2;
01033 i->phi = ref >= 0 ? ref | 0x80000000 : 0;
01034 return i - inter->inter;
01035 }
01036
01037 static int add_bell(struct ws_intervals *inter, struct sbg_script *s,
01038 int64_t ts1, int64_t ts2, int32_t f, int32_t a)
01039 {
01040
01041
01042 int32_t cpoints[][2] = {
01043 { 2, a },
01044 { 4, a - a / 4 },
01045 { 8, a / 2 },
01046 { 16, a / 4 },
01047 { 25, a / 10 },
01048 { 50, a / 80 },
01049 { 75, 0 },
01050 };
01051 int i, r;
01052 int64_t dt = s->sample_rate / 20, ts3 = ts1, ts4;
01053 for (i = 0; i < FF_ARRAY_ELEMS(cpoints); i++) {
01054 ts4 = FFMIN(ts2, ts1 + cpoints[i][0] * dt);
01055 r = add_interval(inter, WS_SINE, 3, -1,
01056 ts3, f, a, ts4, f, cpoints[i][1]);
01057 if (r < 0)
01058 return r;
01059 ts3 = ts4;
01060 a = cpoints[i][1];
01061 }
01062 return 0;
01063 }
01064
01065 static int generate_interval(void *log, struct sbg_script *s,
01066 struct ws_intervals *inter,
01067 int64_t ts1, int64_t ts2,
01068 struct sbg_script_synth *s1,
01069 struct sbg_script_synth *s2,
01070 int transition)
01071 {
01072 int r;
01073
01074 if (ts2 <= ts1 || (s1->vol == 0 && s2->vol == 0))
01075 return 0;
01076 switch (s1->type) {
01077 case SBG_TYPE_NONE:
01078 break;
01079 case SBG_TYPE_SINE:
01080 if (s1->beat == 0 && s2->beat == 0) {
01081 r = add_interval(inter, WS_SINE, 3, s1->ref.l,
01082 ts1, s1->carrier, s1->vol,
01083 ts2, s2->carrier, s2->vol);
01084 if (r < 0)
01085 return r;
01086 s2->ref.l = s2->ref.r = r;
01087 } else {
01088 r = add_interval(inter, WS_SINE, 1, s1->ref.l,
01089 ts1, s1->carrier + s1->beat / 2, s1->vol,
01090 ts2, s2->carrier + s2->beat / 2, s2->vol);
01091 if (r < 0)
01092 return r;
01093 s2->ref.l = r;
01094 r = add_interval(inter, WS_SINE, 2, s1->ref.r,
01095 ts1, s1->carrier - s1->beat / 2, s1->vol,
01096 ts2, s2->carrier - s2->beat / 2, s2->vol);
01097 if (r < 0)
01098 return r;
01099 s2->ref.r = r;
01100 }
01101 break;
01102
01103 case SBG_TYPE_BELL:
01104 if (transition == 2) {
01105 r = add_bell(inter, s, ts1, ts2, s1->carrier, s2->vol);
01106 if (r < 0)
01107 return r;
01108 }
01109 break;
01110
01111 case SBG_TYPE_SPIN:
01112 av_log(log, AV_LOG_WARNING, "Spinning noise not implemented, "
01113 "using pink noise instead.\n");
01114
01115 case SBG_TYPE_NOISE:
01116
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126 r = add_interval(inter, WS_NOISE, 3, s1->ref.l,
01127 ts1, 0, s1->vol - s1->vol / 4,
01128 ts2, 0, s2->vol - s2->vol / 4);
01129 if (r < 0)
01130 return r;
01131 s2->ref.l = s2->ref.r = r;
01132 break;
01133
01134 case SBG_TYPE_MIX:
01135
01136 default:
01137 av_log(log, AV_LOG_ERROR,
01138 "Type %d is not implemented\n", s1->type);
01139 return AVERROR_PATCHWELCOME;
01140 }
01141 return 0;
01142 }
01143
01144 static int generate_plateau(void *log, struct sbg_script *s,
01145 struct ws_intervals *inter,
01146 struct sbg_script_event *ev1)
01147 {
01148 int64_t ts1 = ev1->ts_int, ts2 = ev1->ts_trans;
01149 int i, r;
01150 struct sbg_script_synth *s1;
01151
01152 for (i = 0; i < ev1->nb_elements; i++) {
01153 s1 = &s->synth[ev1->elements + i];
01154 r = generate_interval(log, s, inter, ts1, ts2, s1, s1, 0);
01155 if (r < 0)
01156 return r;
01157 }
01158 return 0;
01159 }
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173 static int generate_transition(void *log, struct sbg_script *s,
01174 struct ws_intervals *inter,
01175 struct sbg_script_event *ev1,
01176 struct sbg_script_event *ev2)
01177 {
01178 int64_t ts1 = ev1->ts_trans, ts2 = ev1->ts_next;
01179
01180 int64_t tsmid = (ts1 >> 1) + (ts2 >> 1) + (ts1 & ts2 & 1);
01181 enum sbg_fade_type type = ev1->fade.slide | (ev1->fade.out & ev2->fade.in);
01182 int nb_elements = FFMAX(ev1->nb_elements, ev2->nb_elements);
01183 struct sbg_script_synth *s1, *s2, s1mod, s2mod, smid;
01184 int pass, i, r;
01185
01186 for (pass = 0; pass < 2; pass++) {
01187
01188
01189
01190
01191
01192
01193
01194 for (i = 0; i < nb_elements; i++) {
01195 s1 = i < ev1->nb_elements ? &s->synth[ev1->elements + i] : &s1mod;
01196 s2 = i < ev2->nb_elements ? &s->synth[ev2->elements + i] : &s2mod;
01197 s1mod = s1 != &s1mod ? *s1 : (struct sbg_script_synth){ 0 };
01198 s2mod = s2 != &s2mod ? *s2 : (struct sbg_script_synth){ 0 };
01199 if (ev1->fade.slide) {
01200
01201
01202 if (s1mod.type == SBG_TYPE_NONE) {
01203 s1mod = s2mod;
01204 s1mod.vol = 0;
01205 } else if (s2mod.type == SBG_TYPE_NONE) {
01206 s2mod = s1mod;
01207 s2mod.vol = 0;
01208 }
01209 }
01210 if (s1mod.type == s2mod.type &&
01211 s1mod.type != SBG_TYPE_BELL &&
01212 (type == SBG_FADE_ADAPT ||
01213 (s1mod.carrier == s2mod.carrier &&
01214 s1mod.beat == s2mod.beat))) {
01215
01216 if (!pass) {
01217 r = generate_interval(log, s, inter,
01218 ts1, ts2, &s1mod, &s2mod, 3);
01219 if (r < 0)
01220 return r;
01221 s2->ref = s2mod.ref;
01222 }
01223 } else {
01224
01225 if (!pass) {
01226 smid = s1mod;
01227 smid.vol = 0;
01228 r = generate_interval(log, s, inter,
01229 ts1, tsmid, &s1mod, &smid, 1);
01230 if (r < 0)
01231 return r;
01232 } else {
01233 smid = s2mod;
01234 smid.vol = 0;
01235 r = generate_interval(log, s, inter,
01236 tsmid, ts2, &smid, &s2mod, 2);
01237 if (r < 0)
01238 return r;
01239 s2->ref = s2mod.ref;
01240 }
01241 }
01242 }
01243 }
01244 return 0;
01245 }
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259 static int generate_intervals(void *log, struct sbg_script *s, int sample_rate,
01260 struct ws_intervals *inter)
01261 {
01262 int64_t trans_time = s->opt_fade_time / 2;
01263 struct sbg_script_event ev0, *ev1, *ev2;
01264 int64_t period;
01265 int i, r;
01266
01267
01268
01269
01270 period = s->events[s->nb_events - 1].ts - s->events[0].ts;
01271 period = (period + (DAY_TS - 1)) / DAY_TS * DAY_TS;
01272 period = FFMAX(period, DAY_TS);
01273
01274
01275 for (i = 0; i < s->nb_events; i++) {
01276 ev1 = &s->events[i];
01277 ev2 = &s->events[(i + 1) % s->nb_events];
01278 ev1->ts_int = ev1->ts;
01279 ev1->ts_trans = ev1->fade.slide ? ev1->ts
01280 : ev2->ts + (ev1 < ev2 ? 0 : period);
01281 }
01282 for (i = 0; i < s->nb_events; i++) {
01283 ev1 = &s->events[i];
01284 ev2 = &s->events[(i + 1) % s->nb_events];
01285 if (!ev1->fade.slide) {
01286 ev1->ts_trans = FFMAX(ev1->ts_int, ev1->ts_trans - trans_time);
01287 ev2->ts_int = FFMIN(ev2->ts_trans, ev2->ts_int + trans_time);
01288 }
01289 ev1->ts_next = ev2->ts_int + (ev1 < ev2 ? 0 : period);
01290 }
01291
01292
01293 ev0 = s->events[s->nb_events - 1];
01294 ev0.ts_int -= period;
01295 ev0.ts_trans -= period;
01296 ev0.ts_next -= period;
01297
01298
01299 for (i = -1; i < s->nb_events; i++) {
01300 ev1 = i < 0 ? &ev0 : &s->events[i];
01301 ev1->ts_int = av_rescale(ev1->ts_int, sample_rate, AV_TIME_BASE);
01302 ev1->ts_trans = av_rescale(ev1->ts_trans, sample_rate, AV_TIME_BASE);
01303 ev1->ts_next = av_rescale(ev1->ts_next, sample_rate, AV_TIME_BASE);
01304 }
01305
01306
01307 for (i = 0; i < s->nb_synth; i++)
01308 s->synth[i].ref.l = s->synth[i].ref.r = -1;
01309 for (i = -1; i < s->nb_events; i++) {
01310 ev1 = i < 0 ? &ev0 : &s->events[i];
01311 ev2 = &s->events[(i + 1) % s->nb_events];
01312 r = generate_plateau(log, s, inter, ev1);
01313 if (r < 0)
01314 return r;
01315 r = generate_transition(log, s, inter, ev1, ev2);
01316 if (r < 0)
01317 return r;
01318 }
01319 if (!inter->nb_inter)
01320 av_log(log, AV_LOG_WARNING, "Completely silent script.\n");
01321 return 0;
01322 }
01323
01324 static int encode_intervals(struct sbg_script *s, AVCodecContext *avc,
01325 struct ws_intervals *inter)
01326 {
01327 int i, edata_size = 4;
01328 uint8_t *edata;
01329
01330 for (i = 0; i < inter->nb_inter; i++) {
01331 edata_size += inter->inter[i].type == WS_SINE ? 44 :
01332 inter->inter[i].type == WS_NOISE ? 32 : 0;
01333 if (edata_size < 0)
01334 return AVERROR(ENOMEM);
01335 }
01336 edata = av_malloc(edata_size);
01337 if (!edata)
01338 return AVERROR(ENOMEM);
01339 avc->extradata = edata;
01340 avc->extradata_size = edata_size;
01341
01342 #define ADD_EDATA32(v) do { AV_WL32(edata, (v)); edata += 4; } while(0)
01343 #define ADD_EDATA64(v) do { AV_WL64(edata, (v)); edata += 8; } while(0)
01344 ADD_EDATA32(inter->nb_inter);
01345 for (i = 0; i < inter->nb_inter; i++) {
01346 ADD_EDATA64(inter->inter[i].ts1);
01347 ADD_EDATA64(inter->inter[i].ts2);
01348 ADD_EDATA32(inter->inter[i].type);
01349 ADD_EDATA32(inter->inter[i].channels);
01350 switch (inter->inter[i].type) {
01351 case WS_SINE:
01352 ADD_EDATA32(inter->inter[i].f1);
01353 ADD_EDATA32(inter->inter[i].f2);
01354 ADD_EDATA32(inter->inter[i].a1);
01355 ADD_EDATA32(inter->inter[i].a2);
01356 ADD_EDATA32(inter->inter[i].phi);
01357 break;
01358 case WS_NOISE:
01359 ADD_EDATA32(inter->inter[i].a1);
01360 ADD_EDATA32(inter->inter[i].a2);
01361 break;
01362 }
01363 }
01364 if (edata != avc->extradata + edata_size)
01365 return AVERROR_BUG;
01366 return 0;
01367 }
01368
01369 static av_cold int sbg_read_probe(AVProbeData *p)
01370 {
01371 int r, score;
01372 struct sbg_script script = { 0 };
01373
01374 r = parse_script(NULL, p->buf, p->buf_size, &script);
01375 score = r < 0 || !script.nb_def || !script.nb_tseq ? 0 :
01376 AVPROBE_SCORE_MAX / 3;
01377 free_script(&script);
01378 return score;
01379 }
01380
01381 static av_cold int sbg_read_header(AVFormatContext *avf)
01382 {
01383 struct sbg_demuxer *sbg = avf->priv_data;
01384 int r;
01385 char *buf = NULL;
01386 struct sbg_script script = { 0 };
01387 AVStream *st;
01388 struct ws_intervals inter = { 0 };
01389
01390 r = read_whole_file(avf->pb, sbg->max_file_size, &buf);
01391 if (r < 0)
01392 goto fail;
01393 r = parse_script(avf, buf, r, &script);
01394 if (r < 0)
01395 goto fail;
01396 if (!sbg->sample_rate)
01397 sbg->sample_rate = script.sample_rate;
01398 else
01399 script.sample_rate = sbg->sample_rate;
01400 if (!sbg->frame_size)
01401 sbg->frame_size = FFMAX(1, sbg->sample_rate / 10);
01402 if (script.opt_mix)
01403 av_log(avf, AV_LOG_WARNING, "Mix feature not implemented: "
01404 "-m is ignored and mix channels will be silent.\n");
01405 r = expand_script(avf, &script);
01406 if (r < 0)
01407 goto fail;
01408 av_freep(&buf);
01409 r = generate_intervals(avf, &script, sbg->sample_rate, &inter);
01410 if (r < 0)
01411 goto fail;
01412
01413 st = avformat_new_stream(avf, NULL);
01414 if (!st)
01415 return AVERROR(ENOMEM);
01416 st->codec->codec_type = AVMEDIA_TYPE_AUDIO;
01417 st->codec->codec_id = AV_CODEC_ID_FFWAVESYNTH;
01418 st->codec->channels = 2;
01419 st->codec->channel_layout = AV_CH_LAYOUT_STEREO;
01420 st->codec->sample_rate = sbg->sample_rate;
01421 st->codec->frame_size = sbg->frame_size;
01422 avpriv_set_pts_info(st, 64, 1, st->codec->sample_rate);
01423 st->probe_packets = 0;
01424 st->start_time = av_rescale(script.start_ts,
01425 sbg->sample_rate, AV_TIME_BASE);
01426 st->duration = script.end_ts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
01427 av_rescale(script.end_ts - script.start_ts,
01428 sbg->sample_rate, AV_TIME_BASE);
01429 st->cur_dts = st->start_time;
01430 r = encode_intervals(&script, st->codec, &inter);
01431 if (r < 0)
01432 goto fail;
01433
01434 av_free(inter.inter);
01435 free_script(&script);
01436 return 0;
01437
01438 fail:
01439 av_free(inter.inter);
01440 free_script(&script);
01441 av_free(buf);
01442 return r;
01443 }
01444
01445 static int sbg_read_packet(AVFormatContext *avf, AVPacket *packet)
01446 {
01447 int64_t ts, end_ts;
01448
01449 ts = avf->streams[0]->cur_dts;
01450 end_ts = ts + avf->streams[0]->codec->frame_size;
01451 if (avf->streams[0]->duration != AV_NOPTS_VALUE)
01452 end_ts = FFMIN(avf->streams[0]->start_time + avf->streams[0]->duration,
01453 end_ts);
01454 if (end_ts <= ts)
01455 return AVERROR_EOF;
01456 if (av_new_packet(packet, 12) < 0)
01457 return AVERROR(ENOMEM);
01458 packet->dts = packet->pts = ts;
01459 packet->duration = end_ts - ts;
01460 AV_WL64(packet->data + 0, ts);
01461 AV_WL32(packet->data + 8, packet->duration);
01462 return packet->size;
01463 }
01464
01465 static int sbg_read_seek2(AVFormatContext *avf, int stream_index,
01466 int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
01467 {
01468 if (flags || stream_index > 0)
01469 return AVERROR(EINVAL);
01470 if (stream_index < 0)
01471 ts = av_rescale_q(ts, AV_TIME_BASE_Q, avf->streams[0]->time_base);
01472 avf->streams[0]->cur_dts = ts;
01473 return 0;
01474 }
01475
01476 static int sbg_read_seek(AVFormatContext *avf, int stream_index,
01477 int64_t ts, int flags)
01478 {
01479 return sbg_read_seek2(avf, stream_index, ts, ts, ts, 0);
01480 }
01481
01482 static const AVOption sbg_options[] = {
01483 { "sample_rate", "", offsetof(struct sbg_demuxer, sample_rate),
01484 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
01485 AV_OPT_FLAG_DECODING_PARAM },
01486 { "frame_size", "", offsetof(struct sbg_demuxer, frame_size),
01487 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX,
01488 AV_OPT_FLAG_DECODING_PARAM },
01489 { "max_file_size", "", offsetof(struct sbg_demuxer, max_file_size),
01490 AV_OPT_TYPE_INT, { .i64 = 5000000 }, 0, INT_MAX,
01491 AV_OPT_FLAG_DECODING_PARAM },
01492 { NULL },
01493 };
01494
01495 static const AVClass sbg_demuxer_class = {
01496 .class_name = "sbg_demuxer",
01497 .item_name = av_default_item_name,
01498 .option = sbg_options,
01499 .version = LIBAVUTIL_VERSION_INT,
01500 };
01501
01502 AVInputFormat ff_sbg_demuxer = {
01503 .name = "sbg",
01504 .long_name = NULL_IF_CONFIG_SMALL("SBaGen binaural beats script"),
01505 .priv_data_size = sizeof(struct sbg_demuxer),
01506 .read_probe = sbg_read_probe,
01507 .read_header = sbg_read_header,
01508 .read_packet = sbg_read_packet,
01509 .read_seek = sbg_read_seek,
01510 .read_seek2 = sbg_read_seek2,
01511 .extensions = "sbg",
01512 .priv_class = &sbg_demuxer_class,
01513 };