25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/log.h"
27 #include "libavutil/opt.h"
31 #define SBG_SCALE (1 << 16)
32 #define DAY (24 * 60 * 60)
33 #define DAY_TS ((int64_t)DAY * AV_TIME_BASE)
158 int *
size,
int *max_size)
162 if (*size == *max_size) {
163 int m =
FFMAX(32,
FFMIN(*max_size, INT_MAX / 2) * 2);
171 ret = (
char *)*array + elsize * *size;
172 memset(ret, 0, elsize);
179 const char *cur = str;
184 if (*cur < '0' || *cur >
'9')
186 hours = strtol(cur, &end, 10);
187 if (end == cur || *end !=
':' || end[1] <
'0' || end[1] >
'9')
190 minutes = strtol(cur, &end, 10);
195 seconds =
strtod(cur + 1, &end);
199 *rtime = (hours * 3600 + minutes * 60 + seconds) *
AV_TIME_BASE;
205 return c ==
' ' || c ==
'\t' || c ==
'\r';
211 if (m < INT_MIN || m >= INT_MAX) {
283 if (s == p->
end || *s ==
'\n')
297 while (c < p->
end && ((*
c >=
'a' && *
c <=
'z') || (*
c >=
'A' && *
c <=
'Z')
298 || (*
c >=
'0' && *
c <=
'9') || *
c ==
'_' || *
c ==
'-'))
314 #define FORWARD_ERROR(c) \
318 return errcode ? errcode : AVERROR_INVALIDDATA; \
324 "immediate sequences not yet implemented");
331 "preprogrammed sequences not yet implemented");
339 "option '%c' requires an argument", o);
356 for (; ostr.
s < ostr.
e; ostr.
s++) {
374 if (oarg.
e != tptr) {
376 "syntax error for option -F");
384 if (oarg.
e != oarg.
s + r) {
386 "syntax error for option -L");
393 if (oarg.
e != oarg.
s + r) {
395 "syntax error for option -T");
404 memcpy(tptr, oarg.
s, oarg.
e - oarg.
s);
405 tptr[oarg.
e - oarg.
s] = 0;
412 if (oarg.
e != tptr) {
414 "syntax error for option -q");
419 "speed factor other than 1 not supported");
425 r = strtol(oarg.
s, &tptr, 10);
426 if (oarg.
e != tptr) {
428 "syntax error for option -r");
433 "invalid sample rate");
440 "unknown option: '%c'", *ostr.
s);
461 int64_t abs = 0, rel = 0, dt;
534 "relative time without previous absolute time");
569 "waveform definitions not yet implemented");
618 double carrierf, beatf;
619 int carrier, beat, vol;
684 double carrierf, beatf;
685 int carrier, beat, vol;
756 char *cursor_save = p->
cursor;
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');
797 .end = script + script_len,
804 .sample_rate = 44100,
835 const char *ctx = sp.
cursor;
838 int lctx = ectx - ctx;
839 const char *quote =
"\"";
840 if (lctx > 0 && ctx[lctx - 1] ==
'\r')
843 ctx =
"the end of line";
856 int size = 0, bufsize = 0,
r;
859 if (bufsize - size < 1024) {
860 bufsize =
FFMIN(
FFMAX(2 * bufsize, 8192), max_size);
861 if (bufsize - size < 2) {
889 int64_t now, cur_ts,
delta = 0;
891 for (i = 0; i < s->
nb_tseq; i++)
898 "Start time ignored in a purely relative script.\n");
911 "Scripts with mixed absolute and relative timestamps can give "
912 "unexpected results (pause, seeking, time zone change).\n");
915 tm = localtime(&now0);
916 now = tm ? tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec :
919 (
int)(now / 3600), (
int)(now / 60) % 60, (
int)now % 60);
921 for (i = 0; i < s->
nb_tseq; i++) {
933 for (i = 0; i < s->
nb_tseq; i++) {
934 if (s->
tseq[i].
ts.
t + delta < cur_ts)
954 for (i = 0; i < s->
nb_def; i++) {
965 if (def->
type ==
'B') {
986 int i,
r, nb_events_max = 0;
989 for (i = 0; i < s->
nb_tseq; i++) {
1011 ri = &inter->
inter[ref];
1014 ri->
f1 == ri->
f2 && ri->
f2 == f1 && f1 == f2 &&
1015 ri->
a1 == ri->
a2 && ri->
a2 == a1 && a1 == a2 &&
1033 i->
phi = ref >= 0 ? ref | 0x80000000 : 0;
1034 return i - inter->
inter;
1054 ts4 =
FFMIN(ts2, ts1 + cpoints[i][0] * dt);
1056 ts3, f, a, ts4, f, cpoints[i][1]);
1067 int64_t
ts1, int64_t
ts2,
1074 if (ts2 <= ts1 || (s1->
vol == 0 && s2->
vol == 0))
1080 if (s1->
beat == 0 && s2->
beat == 0) {
1104 if (transition == 2) {
1113 "using pink noise instead.\n");
1127 ts1, 0, s1->
vol - s1->
vol / 4,
1128 ts2, 0, s2->
vol - s2->
vol / 4);
1138 "Type %d is not implemented\n", s1->
type);
1180 int64_t tsmid = (ts1 >> 1) + (ts2 >> 1) + (ts1 & ts2 & 1);
1186 for (pass = 0; pass < 2; pass++) {
1194 for (i = 0; i < nb_elements; i++) {
1218 ts1, ts2, &s1mod, &s2mod, 3);
1229 ts1, tsmid, &s1mod, &smid, 1);
1236 tsmid, ts2, &smid, &s2mod, 2);
1272 period =
FFMAX(period, DAY_TS);
1280 : ev2->
ts + (ev1 < ev2 ? 0 : period);
1300 ev1 = i < 0 ? &ev0 : &s->
events[i];
1310 ev1 = i < 0 ? &ev0 : &s->
events[i];
1327 int i, edata_size = 4;
1330 for (i = 0; i < inter->
nb_inter; i++) {
1340 #define ADD_EDATA32(v) do { AV_WL32(edata, (v)); edata += 4; } while(0)
1341 #define ADD_EDATA64(v) do { AV_WL64(edata, (v)); edata += 8; } while(0)
1343 for (i = 0; i < inter->
nb_inter; i++) {
1362 if (edata != avc->
extradata + edata_size)
1402 "-m is ignored and mix channels will be silent.\n");
1456 packet->
dts = packet->
pts = ts;
1460 return packet->
size;
1464 int64_t min_ts, int64_t ts, int64_t max_ts,
int flags)
1466 if (flags || stream_index > 0)
1468 if (stream_index < 0)
1475 int64_t ts,
int flags)
1487 {
"max_file_size",
"", offsetof(
struct sbg_demuxer, max_file_size),
1509 .extensions =
"sbg",
static int parse_synth_channel_bell(struct sbg_parser *p, struct sbg_script_synth *synth)
#define AVERROR_PATCHWELCOME
static av_cold int sbg_read_header(AVFormatContext *avf)
static int generate_transition(void *log, struct sbg_script *s, struct ws_intervals *inter, struct sbg_script_event *ev1, struct sbg_script_event *ev2)
static av_cold int sbg_read_probe(AVProbeData *p)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
struct sbg_script_event * events
uint8_t opt_start_at_first
static int parse_immediate(struct sbg_parser *p)
AVCodecContext * codec
Codec context associated with this stream.
static int parse_timestamp(struct sbg_parser *p, struct sbg_timestamp *rts, int64_t *rrel)
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static const AVOption sbg_options[]
static int add_bell(struct ws_intervals *inter, struct sbg_script *s, int64_t ts1, int64_t ts2, int32_t f, int32_t a)
#define AV_CH_LAYOUT_STEREO
static int expand_tseq(void *log, struct sbg_script *s, int *nb_ev_max, int64_t t0, struct sbg_script_tseq *tseq)
static int parse_volume(struct sbg_parser *p, int *vol)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static void expand_timestamps(void *log, struct sbg_script *s)
static int parse_synth_channel(struct sbg_parser *p)
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
static int scale_double(void *log, double d, double m, int *r)
const char * av_default_item_name(void *ctx)
Return the context name.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static int sbg_read_seek(AVFormatContext *avf, int stream_index, int64_t ts, int flags)
static av_cold int end(AVCodecContext *avctx)
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
double strtod(const char *, char **)
static int parse_named_def(struct sbg_parser *p)
void parse_options(void *optctx, int argc, char **argv, const OptionDef *options, void(*parse_arg_function)(void *, const char *))
static int lex_wsword(struct sbg_parser *p, struct sbg_string *rs)
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
enum ws_interval_type type
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
static void free_script(struct sbg_script *s)
static void * av_x_if_null(const void *p, const void *x)
Return x default pointer in case p is NULL.
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq) av_const
Rescale a 64-bit integer by 2 rational numbers.
static const uint8_t frame_size[4]
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
void * priv_data
Format private data.
struct sbg_script_synth * synth
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
struct ws_interval * inter
static int lex_double(struct sbg_parser *p, double *r)
uint64_t channel_layout
Audio channel layout.
static int parse_synth_channel_pink(struct sbg_parser *p, struct sbg_script_synth *synth)
int buf_size
Size of buf except extra allocated bytes.
static int read_whole_file(AVIOContext *io, int max_size, char **rbuf)
#define FF_ARRAY_ELEMS(a)
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
struct sbg_script_definition * def
#define AV_TIME_BASE
Internal time base represented as integer.
static int parse_block_def(struct sbg_parser *p, struct sbg_script_definition *def)
static int parse_synth_channel_mix(struct sbg_parser *p, struct sbg_script_synth *synth)
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static void * alloc_array_elem(void **array, size_t elsize, int *size, int *max_size)
struct sbg_timestamp current_time
static int generate_intervals(void *log, struct sbg_script *s, int sample_rate, struct ws_intervals *inter)
static int sbg_read_packet(AVFormatContext *avf, AVPacket *packet)
static int parse_synth_def(struct sbg_parser *p, struct sbg_script_definition *def)
static int parse_wave_def(struct sbg_parser *p, int wavenum)
int frame_size
Number of samples per channel in an audio frame.
struct sbg_script_tseq * tseq
#define AV_LOG_INFO
Standard information.
enum AVMediaType codec_type
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
int sample_rate
samples per second
static int is_space(char c)
static int lex_char(struct sbg_parser *p, char c)
struct sbg_script_tseq * block_tseq
main external API structure.
static int lex_space(struct sbg_parser *p)
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)
void * av_realloc_f(void *ptr, size_t nelem, size_t elsize)
Allocate or reallocate a block of memory.
static void fade(AudioVectorScopeContext *p)
AVIOContext * pb
I/O context.
Describe the class of an AVClass context structure.
#define MKTAG(a, b, c, d)
static int encode_intervals(struct sbg_script *s, AVCodecContext *avc, struct ws_intervals *inter)
This structure contains the data a format has to probe a file.
static const AVClass sbg_demuxer_class
static int parse_fade(struct sbg_parser *p, struct sbg_fade *fr)
static int parse_preprogrammed(struct sbg_parser *p)
static int generate_plateau(void *log, struct sbg_script *s, struct ws_intervals *inter, struct sbg_script_event *ev1)
#define AV_OPT_FLAG_DECODING_PARAM
a generic parameter which can be set by the user for demuxing or decoding
int64_t duration
Decoding: duration of the stream, in stream time base.
static int parse_synth_channel_sine(struct sbg_parser *p, struct sbg_script_synth *synth)
static int parse_synth_channel_spin(struct sbg_parser *p, struct sbg_script_synth *synth)
static int parse_time_sequence(struct sbg_parser *p, int inblock)
static int sbg_read_seek2(AVFormatContext *avf, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
static int str_to_time(const char *str, int64_t *rtime)
static int lex_line_end(struct sbg_parser *p)
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base...
static int expand_script(void *log, struct sbg_script *s)
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)
static int parse_script(void *log, char *script, int script_len, struct sbg_script *rscript)
AVInputFormat ff_sbg_demuxer
#define AVERROR_INVALIDDATA
int channels
number of audio channels
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static int lex_name(struct sbg_parser *p, struct sbg_string *rs)
static int parse_optarg(struct sbg_parser *p, char o, struct sbg_string *r)
static int lex_fixed(struct sbg_parser *p, const char *t, int l)
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
struct sbg_script_synth::@158 ref
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
static int lex_time(struct sbg_parser *p, int64_t *rt)