39 #define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name) \
40 static char *choose_ ## var ## s(OutputStream *ost) \
42 if (ost->st->codec->var != none) { \
43 get_name(ost->st->codec->var); \
44 return av_strdup(name); \
45 } else if (ost->enc && ost->enc->supported_list) { \
47 AVIOContext *s = NULL; \
51 if (avio_open_dyn_buf(&s) < 0) \
54 for (p = ost->enc->supported_list; *p != none; p++) { \
56 avio_printf(s, "%s|", name); \
58 len = avio_close_dyn_buf(s, &ret); \
100 ist->filters[ist->nb_filters - 1] = fg->
inputs[0];
125 int file_idx = strtol(in->
name, &p, 0);
155 if (i == nb_input_streams) {
157 "unlabeled input pad %d on filter %s\n", in->
pad_idx,
195 "recording time.\n", name);
203 if (duration != INT64_MAX) {
240 snprintf(name,
sizeof(name),
"output stream %d:%d", ost->
file_index, ost->
index);
251 snprintf(args,
sizeof(args),
"%d:%d:0x%X",
255 snprintf(name,
sizeof(name),
"scaler for output stream %d:%d",
260 if ((ret =
avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
267 if ((pix_fmts = choose_pix_fmts(ost))) {
269 snprintf(name,
sizeof(name),
"pixel format for output stream %d:%d",
273 "format", pix_fmts,
NULL,
276 if ((ret =
avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
288 snprintf(args,
sizeof(args),
"fps=%d/%d", ost->
frame_rate.
num,
290 snprintf(name,
sizeof(name),
"fps for output stream %d:%d",
304 snprintf(name,
sizeof(name),
"trim for output stream %d:%d",
307 &last_filter, &pad_idx, name);
325 char *
sample_fmts, *sample_rates, *channel_layouts;
330 snprintf(name,
sizeof(name),
"output stream %d:%d", ost->
file_index, ost->
index);
340 sample_fmts = choose_sample_fmts(ost);
341 sample_rates = choose_sample_rates(ost);
342 channel_layouts = choose_channel_layouts(ost);
343 if (sample_fmts || sample_rates || channel_layouts) {
349 len += snprintf(args + len,
sizeof(args) - len,
"sample_fmts=%s:",
352 len += snprintf(args + len,
sizeof(args) - len,
"sample_rates=%s:",
355 len += snprintf(args + len,
sizeof(args) - len,
"channel_layouts=%s:",
363 snprintf(name,
sizeof(name),
"audio format for output stream %d:%d",
375 last_filter = format;
379 snprintf(name,
sizeof(name),
"trim for output stream %d:%d",
382 &last_filter, &pad_idx, name);
392 #define DESCRIBE_FILTER_LINK(f, inout, in) \
394 AVFilterContext *ctx = inout->filter_ctx; \
395 AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads; \
396 int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs; \
399 if (avio_open_dyn_buf(&pb) < 0) \
402 avio_printf(pb, "%s", ctx->filter->name); \
404 avio_printf(pb, ":%s", avfilter_pad_get_name(pads, inout->pad_idx));\
406 avio_close_dyn_buf(pb, &f->name); \
431 char args[255],
name[255];
432 int ret, pad_idx = 0;
437 snprintf(args,
sizeof(args),
"%d:%d:%d:%d:%d:%d:%d", ist->
st->
codec->
width,
441 snprintf(name,
sizeof(name),
"graph %d input from stream %d:%d", fg->
index,
447 last_filter = ifilter->
filter;
452 snprintf(name,
sizeof(name),
"force CFR for input from stream %d:%d",
463 last_filter = setpts;
466 snprintf(name,
sizeof(name),
"trim for input stream %d:%d",
485 char args[255],
name[255];
486 int ret, pad_idx = 0;
488 snprintf(args,
sizeof(args),
"time_base=%d/%d:sample_rate=%d:sample_fmt=%s"
489 ":channel_layout=0x%"PRIx64,
494 snprintf(name,
sizeof(name),
"graph %d input from stream %d:%d", fg->
index,
501 last_filter = ifilter->
filter;
508 "asyncts audio filter instead.\n");
511 len += snprintf(args + len,
sizeof(args) - len,
"compensate=1:"
513 snprintf(args + len,
sizeof(args) - len,
"min_delta=%f",
516 snprintf(name,
sizeof(name),
"graph %d audio sync for input stream %d:%d",
534 "audio filter instead.\n");
536 snprintf(args,
sizeof(args),
"volume=%f",
audio_volume / 256.0);
538 snprintf(name,
sizeof(name),
"graph %d volume for input stream %d:%d",
550 last_filter = volume;
553 snprintf(name,
sizeof(name),
"trim for input stream %d:%d",
595 snprintf(args,
sizeof(args),
"flags=0x%X", (
unsigned)ost->
sws_flags);
604 args[strlen(args) - 1] =
'\0';
611 if (simple && (!inputs || inputs->
next || !outputs || outputs->
next)) {
613 "exactly one input and output.\n", graph_desc);
617 for (cur = inputs; !simple && init && cur; cur = cur->
next)
620 for (cur = inputs, i = 0; cur; cur = cur->
next, i++)
625 if (!init || simple) {
628 for (cur = outputs, i = 0; cur; cur = cur->
next, i++)
636 for (cur = outputs; cur;) {
uint64_t av_get_default_channel_layout(int nb_channels)
Return default channel layout for a given number of channels.
#define DEF_CHOOSE_FORMAT(type, var, supported_list, none, get_name)
static const char * filter_name(void *p)
AVFilterGraph * avfilter_graph_alloc(void)
Allocate a filter graph.
#define AV_LOG_WARNING
Something somehow does not look correct.
static const AVFilterPad outputs[]
Main libavfilter public API header.
int avfilter_graph_config(AVFilterGraph *graphctx, void *log_ctx)
Check validity and configure all the links and formats in the graph.
void avfilter_inout_free(AVFilterInOut **inout)
Free the supplied list of AVFilterInOut and set *inout to NULL.
struct AVFilterInOut * next
next input/input in the list, NULL if this is the last
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the given stream matches a stream specifier.
enum AVMediaType avfilter_pad_get_type(const AVFilterPad *pads, int pad_idx)
Get the type of an AVFilterPad.
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
int index
stream index in AVFormatContext
static int configure_input_audio_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
void avfilter_graph_free(AVFilterGraph **graph)
Free a graph, destroy its links, and set *graph to NULL.
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
FilterGraph * init_simple_filtergraph(InputStream *ist, OutputStream *ost)
FilterGraph ** filtergraphs
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
char * scale_sws_opts
sws options to use for the auto-inserted scale filters
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
int configure_filtergraph(FilterGraph *fg)
char * name
name of this filter instance
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int avfilter_link(AVFilterContext *src, unsigned srcpad, AVFilterContext *dst, unsigned dstpad)
Link two filters together.
AVFilterPad * output_pads
array of output pads
enum AVSampleFormat sample_fmt
audio sample format
float audio_drift_threshold
#define DESCRIBE_FILTER_LINK(f, inout, in)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
AVStream ** streams
A list of all streams in the file.
int av_opt_set_double(void *obj, const char *name, double val, int search_flags)
int avfilter_graph_create_filter(AVFilterContext **filt_ctx, const AVFilter *filt, const char *name, const char *args, void *opaque, AVFilterGraph *graph_ctx)
Create and add a filter instance into an existing graph.
char * resample_lavr_opts
libavresample options to use for the auto-inserted resample filters
AVDictionary * resample_opts
int avfilter_graph_parse2(AVFilterGraph *graph, const char *filters, AVFilterInOut **inputs, AVFilterInOut **outputs)
Add a graph described by a string to a graph.
AVFilterPad * input_pads
array of input pads
static int64_t start_time
static int configure_input_video_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define GET_CH_LAYOUT_NAME(ch_layout)
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_dlog(ac->avr,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
static int configure_output_audio_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
uint64_t channel_layout
Audio channel layout.
AVCodecContext * codec
Codec context associated with this stream.
const AVFilter * avfilter_get_by_name(const char *name)
Get a filter definition matching the given name.
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
audio channel layout utility functions
static int insert_trim(int64_t start_time, int64_t duration, AVFilterContext **last_filter, int *pad_idx, const char *filter_name)
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
struct OutputStream * ost
int width
picture width / height.
#define GET_PIX_FMT_NAME(pix_fmt)
enum AVPixelFormat pix_fmt
static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
#define GET_SAMPLE_FMT_NAME(sample_fmt)
AVFilterContext * filter_ctx
filter context associated to this input/output
int avfilter_init_str(AVFilterContext *filter, const char *args)
Initialize a filter with the supplied parameters.
A linked-list of the inputs/outputs of the filter chain.
#define GET_SAMPLE_RATE_NAME(rate)
OutputFile ** output_files
enum AVMediaType codec_type
char * av_strdup(const char *s)
Duplicate the string s.
int sample_rate
samples per second
main external API structure.
static const AVFilterPad inputs[]
int pad_idx
index of the filt_ctx pad to use for linking
rational number numerator/denominator
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
static int configure_input_filter(FilterGraph *fg, InputFilter *ifilter, AVFilterInOut *in)
char * name
unique name for this input/output in the list
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
#define AVERROR_FILTER_NOT_FOUND
Filter not found.
int configure_output_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
struct FilterGraph * graph
InputStream ** input_streams
AVSampleFormat
Audio Sample Formats.
static int configure_output_video_filter(FilterGraph *fg, OutputFilter *ofilter, AVFilterInOut *out)
static av_cold int init(AVCodecParserContext *s)
#define GROW_ARRAY(array, nb_elems)
AVFilterContext * avfilter_graph_alloc_filter(AVFilterGraph *graph, const AVFilter *filter, const char *name)
Create a new filter instance in a filter graph.
int channels
number of audio channels
#define AV_DICT_IGNORE_SUFFIX
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
AVPixelFormat
Pixel format.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define AV_NOPTS_VALUE
Undefined timestamp value.
int ist_in_filtergraph(FilterGraph *fg, InputStream *ist)