This commit is contained in:
2025-08-27 15:09:05 +09:00
parent 57961d84de
commit a13f66d917
9896 changed files with 2193048 additions and 730 deletions
+95
View File
@@ -0,0 +1,95 @@
AVPROGS-$(CONFIG_FFMPEG) += ffmpeg
AVPROGS-$(CONFIG_FFPLAY) += ffplay
AVPROGS-$(CONFIG_FFPROBE) += ffprobe
AVPROGS := $(AVPROGS-yes:%=%$(PROGSSUF)$(EXESUF))
PROGS += $(AVPROGS)
AVBASENAMES = ffmpeg ffplay ffprobe
ALLAVPROGS = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF))
ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF))
include $(SRC_PATH)/fftools/resources/Makefile
OBJS-ffmpeg += \
fftools/ffmpeg_dec.o \
fftools/ffmpeg_demux.o \
fftools/ffmpeg_enc.o \
fftools/ffmpeg_filter.o \
fftools/ffmpeg_hw.o \
fftools/ffmpeg_mux.o \
fftools/ffmpeg_mux_init.o \
fftools/ffmpeg_opt.o \
fftools/ffmpeg_sched.o \
fftools/graph/graphprint.o \
fftools/sync_queue.o \
fftools/thread_queue.o \
fftools/textformat/avtextformat.o \
fftools/textformat/tf_compact.o \
fftools/textformat/tf_default.o \
fftools/textformat/tf_flat.o \
fftools/textformat/tf_ini.o \
fftools/textformat/tf_json.o \
fftools/textformat/tf_mermaid.o \
fftools/textformat/tf_xml.o \
fftools/textformat/tw_avio.o \
fftools/textformat/tw_buffer.o \
fftools/textformat/tw_stdout.o \
$(OBJS-resman) \
OBJS-ffprobe += \
fftools/textformat/avtextformat.o \
fftools/textformat/tf_compact.o \
fftools/textformat/tf_default.o \
fftools/textformat/tf_flat.o \
fftools/textformat/tf_ini.o \
fftools/textformat/tf_json.o \
fftools/textformat/tf_mermaid.o \
fftools/textformat/tf_xml.o \
fftools/textformat/tw_avio.o \
fftools/textformat/tw_buffer.o \
fftools/textformat/tw_stdout.o \
OBJS-ffplay += fftools/ffplay_renderer.o
define DOFFTOOL
OBJS-$(1) += fftools/cmdutils.o fftools/opt_common.o fftools/$(1).o $(OBJS-$(1)-yes)
ifdef HAVE_GNU_WINDRES
OBJS-$(1) += fftools/fftoolsres.o
endif
$(1)$(PROGSSUF)_g$(EXESUF): $$(OBJS-$(1))
$$(OBJS-$(1)): | fftools fftools/textformat fftools/resources fftools/graph
$$(OBJS-$(1)): CFLAGS += $(CFLAGS-$(1))
$(1)$(PROGSSUF)_g$(EXESUF): LDFLAGS += $(LDFLAGS-$(1))
$(1)$(PROGSSUF)_g$(EXESUF): FF_EXTRALIBS += $(EXTRALIBS-$(1))
-include $$(OBJS-$(1):.o=.d)
endef
$(foreach P,$(AVPROGS-yes),$(eval $(call DOFFTOOL,$(P))))
all: $(AVPROGS)
fftools/ffprobe.o fftools/cmdutils.o: libavutil/ffversion.h | fftools
OUTDIRS += fftools
OUTDIRS += fftools/textformat
OUTDIRS += fftools/resources
OUTDIRS += fftools/graph
ifdef AVPROGS
install: install-progs install-data
endif
install-progs-yes:
install-progs-$(CONFIG_SHARED): install-libs
install-progs: install-progs-yes $(AVPROGS)
$(Q)mkdir -p "$(BINDIR)"
$(INSTALL) -c -m 755 $(AVPROGS) "$(BINDIR)"
uninstall: uninstall-progs
uninstall-progs:
$(RM) $(addprefix "$(BINDIR)/", $(ALLAVPROGS))
clean::
$(RM) $(ALLAVPROGS) $(ALLAVPROGS_G) $(CLEANSUFFIXES:%=fftools/%) $(CLEANSUFFIXES:%=fftools/graph/%) $(CLEANSUFFIXES:%=fftools/textformat/%)
File diff suppressed because it is too large Load Diff
+548
View File
@@ -0,0 +1,548 @@
/*
* Various utilities for command line tools
* copyright (c) 2003 Fabrice Bellard
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_CMDUTILS_H
#define FFTOOLS_CMDUTILS_H
#include <stdint.h>
#include "config.h"
#include "libavcodec/avcodec.h"
#include "libavfilter/avfilter.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#ifdef _WIN32
#undef main /* We don't want SDL to override our main() */
#endif
/**
* program name, defined by the program for show_version().
*/
extern const char program_name[];
/**
* program birth year, defined by the program for show_banner()
*/
extern const int program_birth_year;
extern AVDictionary *sws_dict;
extern AVDictionary *swr_opts;
extern AVDictionary *format_opts, *codec_opts;
extern int hide_banner;
/**
* Initialize dynamic library loading
*/
void init_dynload(void);
/**
* Uninitialize the cmdutils option system, in particular
* free the *_opts contexts and their contents.
*/
void uninit_opts(void);
/**
* Trivial log callback.
* Only suitable for opt_help and similar since it lacks prefix handling.
*/
void log_callback_help(void* ptr, int level, const char* fmt, va_list vl);
/**
* Fallback for options that are not explicitly handled, these will be
* parsed through AVOptions.
*/
int opt_default(void *optctx, const char *opt, const char *arg);
/**
* Limit the execution time.
*/
int opt_timelimit(void *optctx, const char *opt, const char *arg);
enum OptionType {
OPT_TYPE_FUNC,
OPT_TYPE_BOOL,
OPT_TYPE_STRING,
OPT_TYPE_INT,
OPT_TYPE_INT64,
OPT_TYPE_FLOAT,
OPT_TYPE_DOUBLE,
OPT_TYPE_TIME,
};
/**
* Parse a string and return its corresponding value as a double.
*
* @param context the context of the value to be set (e.g. the
* corresponding command line option name)
* @param numstr the string to be parsed
* @param type the type (OPT_INT64 or OPT_FLOAT) as which the
* string should be parsed
* @param min the minimum valid accepted value
* @param max the maximum valid accepted value
*/
int parse_number(const char *context, const char *numstr, enum OptionType type,
double min, double max, double *dst);
enum StreamList {
STREAM_LIST_ALL,
STREAM_LIST_STREAM_ID,
STREAM_LIST_PROGRAM,
STREAM_LIST_GROUP_ID,
STREAM_LIST_GROUP_IDX,
};
typedef struct StreamSpecifier {
// trailing stream index - pick idx-th stream that matches
// all the other constraints; -1 when not present
int idx;
// which stream list to consider
enum StreamList stream_list;
// STREAM_LIST_STREAM_ID: stream ID
// STREAM_LIST_GROUP_IDX: group index
// STREAM_LIST_GROUP_ID: group ID
// STREAM_LIST_PROGRAM: program ID
int64_t list_id;
// when not AVMEDIA_TYPE_UNKNOWN, consider only streams of this type
enum AVMediaType media_type;
uint8_t no_apic;
uint8_t usable_only;
int disposition;
char *meta_key;
char *meta_val;
char *remainder;
} StreamSpecifier;
/**
* Parse a stream specifier string into a form suitable for matching.
*
* @param ss Parsed specifier will be stored here; must be uninitialized
* with stream_specifier_uninit() when no longer needed.
* @param spec String containing the stream specifier to be parsed.
* @param allow_remainder When 1, the part of spec that is left after parsing
* the stream specifier is stored into ss->remainder.
* When 0, any remainder will cause parsing to fail.
*/
int stream_specifier_parse(StreamSpecifier *ss, const char *spec,
int allow_remainder, void *logctx);
/**
* @return 1 if st matches the parsed specifier, 0 if it does not
*/
unsigned stream_specifier_match(const StreamSpecifier *ss,
const AVFormatContext *s, const AVStream *st,
void *logctx);
void stream_specifier_uninit(StreamSpecifier *ss);
typedef struct SpecifierOpt {
// original specifier or empty string
char *specifier;
// parsed specifier for OPT_FLAG_PERSTREAM options
StreamSpecifier stream_spec;
union {
uint8_t *str;
int i;
int64_t i64;
uint64_t ui64;
float f;
double dbl;
} u;
} SpecifierOpt;
typedef struct SpecifierOptList {
SpecifierOpt *opt;
int nb_opt;
/* Canonical option definition that was parsed into this list. */
const struct OptionDef *opt_canon;
/* Type corresponding to the field that should be used from SpecifierOpt.u.
* May not match the option type, e.g. OPT_TYPE_BOOL options are stored as
* int, so this field would be OPT_TYPE_INT for them */
enum OptionType type;
} SpecifierOptList;
typedef struct OptionDef {
const char *name;
enum OptionType type;
int flags;
/* The OPT_TYPE_FUNC option takes an argument.
* Must not be used with other option types, as for those it holds:
* - OPT_TYPE_BOOL do not take an argument
* - all other types do
*/
#define OPT_FUNC_ARG (1 << 0)
/* Program will immediately exit after processing this option */
#define OPT_EXIT (1 << 1)
/* Option is intended for advanced users. Only affects
* help output.
*/
#define OPT_EXPERT (1 << 2)
#define OPT_VIDEO (1 << 3)
#define OPT_AUDIO (1 << 4)
#define OPT_SUBTITLE (1 << 5)
#define OPT_DATA (1 << 6)
/* The option is per-file (currently ffmpeg-only). At least one of OPT_INPUT,
* OPT_OUTPUT, OPT_DECODER must be set when this flag is in use.
*/
#define OPT_PERFILE (1 << 7)
/* Option is specified as an offset in a passed optctx.
* Always use as OPT_OFFSET in option definitions. */
#define OPT_FLAG_OFFSET (1 << 8)
#define OPT_OFFSET (OPT_FLAG_OFFSET | OPT_PERFILE)
/* Option is to be stored in a SpecifierOptList.
Always use as OPT_SPEC in option definitions. */
#define OPT_FLAG_SPEC (1 << 9)
#define OPT_SPEC (OPT_FLAG_SPEC | OPT_OFFSET)
/* Option applies per-stream (implies OPT_SPEC). */
#define OPT_FLAG_PERSTREAM (1 << 10)
#define OPT_PERSTREAM (OPT_FLAG_PERSTREAM | OPT_SPEC)
/* ffmpeg-only - specifies whether an OPT_PERFILE option applies to input,
* output, or both. */
#define OPT_INPUT (1 << 11)
#define OPT_OUTPUT (1 << 12)
/* This option is a "canonical" form, to which one or more alternatives
* exist. These alternatives are listed in u1.names_alt. */
#define OPT_HAS_ALT (1 << 13)
/* This option is an alternative form of some other option, whose
* name is stored in u1.name_canon */
#define OPT_HAS_CANON (1 << 14)
/* ffmpeg-only - OPT_PERFILE may apply to standalone decoders */
#define OPT_DECODER (1 << 15)
union {
void *dst_ptr;
int (*func_arg)(void *, const char *, const char *);
size_t off;
} u;
const char *help;
const char *argname;
union {
/* Name of the canonical form of this option.
* Is valid when OPT_HAS_CANON is set. */
const char *name_canon;
/* A NULL-terminated list of alternate forms of this option.
* Is valid when OPT_HAS_ALT is set. */
const char * const *names_alt;
} u1;
} OptionDef;
/**
* Print help for all options matching specified flags.
*
* @param options a list of options
* @param msg title of this group. Only printed if at least one option matches.
* @param req_flags print only options which have all those flags set.
* @param rej_flags don't print options which have any of those flags set.
*/
void show_help_options(const OptionDef *options, const char *msg, int req_flags,
int rej_flags);
/**
* Show help for all options with given flags in class and all its
* children.
*/
void show_help_children(const AVClass *class, int flags);
/**
* Per-fftool specific help handler. Implemented in each
* fftool, called by show_help().
*/
void show_help_default(const char *opt, const char *arg);
/**
* Parse the command line arguments.
*
* @param optctx an opaque options context
* @param argc number of command line arguments
* @param argv values of command line arguments
* @param options Array with the definitions required to interpret every
* option of the form: -option_name [argument]
* @param parse_arg_function Name of the function called to process every
* argument without a leading option name flag. NULL if such arguments do
* not have to be processed.
*/
int parse_options(void *optctx, int argc, char **argv, const OptionDef *options,
int (* parse_arg_function)(void *optctx, const char*));
/**
* Parse one given option.
*
* @return on success 1 if arg was consumed, 0 otherwise; negative number on error
*/
int parse_option(void *optctx, const char *opt, const char *arg,
const OptionDef *options);
/**
* An option extracted from the commandline.
* Cannot use AVDictionary because of options like -map which can be
* used multiple times.
*/
typedef struct Option {
const OptionDef *opt;
const char *key;
const char *val;
} Option;
typedef struct OptionGroupDef {
/** group name */
const char *name;
/**
* Option to be used as group separator. Can be NULL for groups which
* are terminated by a non-option argument (e.g. ffmpeg output files)
*/
const char *sep;
/**
* Option flags that must be set on each option that is
* applied to this group
*/
int flags;
} OptionGroupDef;
typedef struct OptionGroup {
const OptionGroupDef *group_def;
const char *arg;
Option *opts;
int nb_opts;
AVDictionary *codec_opts;
AVDictionary *format_opts;
AVDictionary *sws_dict;
AVDictionary *swr_opts;
} OptionGroup;
/**
* A list of option groups that all have the same group type
* (e.g. input files or output files)
*/
typedef struct OptionGroupList {
const OptionGroupDef *group_def;
OptionGroup *groups;
int nb_groups;
} OptionGroupList;
typedef struct OptionParseContext {
OptionGroup global_opts;
OptionGroupList *groups;
int nb_groups;
/* parsing state */
OptionGroup cur_group;
} OptionParseContext;
/**
* Parse an options group and write results into optctx.
*
* @param optctx an app-specific options context. NULL for global options group
*/
int parse_optgroup(void *optctx, OptionGroup *g, const OptionDef *defs);
/**
* Split the commandline into an intermediate form convenient for further
* processing.
*
* The commandline is assumed to be composed of options which either belong to a
* group (those with OPT_SPEC, OPT_OFFSET or OPT_PERFILE) or are global
* (everything else).
*
* A group (defined by an OptionGroupDef struct) is a sequence of options
* terminated by either a group separator option (e.g. -i) or a parameter that
* is not an option (doesn't start with -). A group without a separator option
* must always be first in the supplied groups list.
*
* All options within the same group are stored in one OptionGroup struct in an
* OptionGroupList, all groups with the same group definition are stored in one
* OptionGroupList in OptionParseContext.groups. The order of group lists is the
* same as the order of group definitions.
*/
int split_commandline(OptionParseContext *octx, int argc, char *argv[],
const OptionDef *options,
const OptionGroupDef *groups, int nb_groups);
/**
* Free all allocated memory in an OptionParseContext.
*/
void uninit_parse_context(OptionParseContext *octx);
/**
* Find the '-loglevel' option in the command line args and apply it.
*/
void parse_loglevel(int argc, char **argv, const OptionDef *options);
/**
* Return index of option opt in argv or 0 if not found.
*/
int locate_option(int argc, char **argv, const OptionDef *options,
const char *optname);
/**
* Check if the given stream matches a stream specifier.
*
* @param s Corresponding format context.
* @param st Stream from s to be checked.
* @param spec A stream specifier of the [v|a|s|d]:[\<stream index\>] form.
*
* @return 1 if the stream matches, 0 if it doesn't, <0 on error
*/
int check_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec);
/**
* Filter out options for given codec.
*
* Create a new options dictionary containing only the options from
* opts which apply to the codec with ID codec_id.
*
* @param opts dictionary to place options in
* @param codec_id ID of the codec that should be filtered for
* @param s Corresponding format context.
* @param st A stream from s for which the options should be filtered.
* @param codec The particular codec for which the options should be filtered.
* If null, the default one is looked up according to the codec id.
* @param dst a pointer to the created dictionary
* @param opts_used if non-NULL, every option stored in dst is also stored here,
* with specifiers preserved
* @return a non-negative number on success, a negative error code on failure
*/
int filter_codec_opts(const AVDictionary *opts, enum AVCodecID codec_id,
AVFormatContext *s, AVStream *st, const AVCodec *codec,
AVDictionary **dst, AVDictionary **opts_used);
/**
* Setup AVCodecContext options for avformat_find_stream_info().
*
* Create an array of dictionaries, one dictionary for each stream
* contained in s.
* Each dictionary will contain the options from codec_opts which can
* be applied to the corresponding stream codec context.
*/
int setup_find_stream_info_opts(AVFormatContext *s,
AVDictionary *codec_opts,
AVDictionary ***dst);
/**
* Print an error message to stderr, indicating filename and a human
* readable description of the error code err.
*
* If strerror_r() is not available the use of this function in a
* multithreaded application may be unsafe.
*
* @see av_strerror()
*/
static inline void print_error(const char *filename, int err)
{
av_log(NULL, AV_LOG_ERROR, "%s: %s\n", filename, av_err2str(err));
}
/**
* Print the program banner to stderr. The banner contents depend on the
* current version of the repository and of the libav* libraries used by
* the program.
*/
void show_banner(int argc, char **argv, const OptionDef *options);
/**
* Return a positive value if a line read from standard input
* starts with [yY], otherwise return 0.
*/
int read_yesno(void);
/**
* Get a file corresponding to a preset file.
*
* If is_path is non-zero, look for the file in the path preset_name.
* Otherwise search for a file named arg.ffpreset in the directories
* $FFMPEG_DATADIR (if set), $HOME/.ffmpeg, and in the datadir defined
* at configuration time or in a "ffpresets" folder along the executable
* on win32, in that order. If no such file is found and
* codec_name is defined, then search for a file named
* codec_name-preset_name.avpreset in the above-mentioned directories.
*
* @param filename buffer where the name of the found filename is written
* @param filename_size size in bytes of the filename buffer
* @param preset_name name of the preset to search
* @param is_path tell if preset_name is a filename path
* @param codec_name name of the codec for which to look for the
* preset, may be NULL
*/
FILE *get_preset_file(char *filename, size_t filename_size,
const char *preset_name, int is_path, const char *codec_name);
/**
* Realloc array to hold new_size elements of elem_size.
*
* @param array pointer to the array to reallocate, will be updated
* with a new pointer on success
* @param elem_size size in bytes of each element
* @param size new element count will be written here
* @param new_size number of elements to place in reallocated array
* @return a non-negative number on success, a negative error code on failure
*/
int grow_array(void **array, int elem_size, int *size, int new_size);
/**
* Atomically add a new element to an array of pointers, i.e. allocate
* a new entry, reallocate the array of pointers and make the new last
* member of this array point to the newly allocated buffer.
*
* @param array array of pointers to reallocate
* @param elem_size size of the new element to allocate
* @param nb_elems pointer to the number of elements of the array array;
* *nb_elems will be incremented by one by this function.
* @return pointer to the newly allocated entry or NULL on failure
*/
void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
#define GROW_ARRAY(array, nb_elems)\
grow_array((void**)&array, sizeof(*array), &nb_elems, nb_elems + 1)
double get_rotation(const int32_t *displaymatrix);
/* read file contents into a string */
char *file_read(const char *filename);
/* Remove keys in dictionary b from dictionary a */
void remove_avoptions(AVDictionary **a, AVDictionary *b);
/* Check if any keys exist in dictionary m */
int check_avoptions(AVDictionary *m);
int cmdutils_isalnum(char c);
#endif /* FFTOOLS_CMDUTILS_H */
File diff suppressed because it is too large Load Diff
+935
View File
@@ -0,0 +1,935 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_FFMPEG_H
#define FFTOOLS_FFMPEG_H
#include "config.h"
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <signal.h>
#include "cmdutils.h"
#include "ffmpeg_sched.h"
#include "sync_queue.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/bsf.h"
#include "libavfilter/avfilter.h"
#include "libavutil/avutil.h"
#include "libavutil/bprint.h"
#include "libavutil/dict.h"
#include "libavutil/eval.h"
#include "libavutil/fifo.h"
#include "libavutil/hwcontext.h"
#include "libavutil/pixfmt.h"
#include "libavutil/rational.h"
#include "libavutil/thread.h"
#include "libavutil/threadmessage.h"
#include "libswresample/swresample.h"
// deprecated features
#define FFMPEG_OPT_QPHIST 1
#define FFMPEG_OPT_ADRIFT_THRESHOLD 1
#define FFMPEG_OPT_ENC_TIME_BASE_NUM 1
#define FFMPEG_OPT_TOP 1
#define FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP 1
#define FFMPEG_OPT_VSYNC_DROP 1
#define FFMPEG_OPT_VSYNC 1
#define FFMPEG_OPT_FILTER_SCRIPT 1
#define FFMPEG_ERROR_RATE_EXCEEDED FFERRTAG('E', 'R', 'E', 'D')
enum VideoSyncMethod {
VSYNC_AUTO = -1,
VSYNC_PASSTHROUGH,
VSYNC_CFR,
VSYNC_VFR,
VSYNC_VSCFR,
#if FFMPEG_OPT_VSYNC_DROP
VSYNC_DROP,
#endif
};
enum EncTimeBase {
ENC_TIME_BASE_DEMUX = -1,
ENC_TIME_BASE_FILTER = -2,
};
enum HWAccelID {
HWACCEL_NONE = 0,
HWACCEL_AUTO,
HWACCEL_GENERIC,
};
enum FrameOpaque {
FRAME_OPAQUE_SUB_HEARTBEAT = 1,
FRAME_OPAQUE_EOF,
FRAME_OPAQUE_SEND_COMMAND,
};
enum PacketOpaque {
PKT_OPAQUE_SUB_HEARTBEAT = 1,
PKT_OPAQUE_FIX_SUB_DURATION,
};
enum LatencyProbe {
LATENCY_PROBE_DEMUX,
LATENCY_PROBE_DEC_PRE,
LATENCY_PROBE_DEC_POST,
LATENCY_PROBE_FILTER_PRE,
LATENCY_PROBE_FILTER_POST,
LATENCY_PROBE_ENC_PRE,
LATENCY_PROBE_ENC_POST,
LATENCY_PROBE_NB,
};
typedef struct HWDevice {
const char *name;
enum AVHWDeviceType type;
AVBufferRef *device_ref;
} HWDevice;
enum ViewSpecifierType {
// no specifier given
VIEW_SPECIFIER_TYPE_NONE = 0,
// val is view index
VIEW_SPECIFIER_TYPE_IDX,
// val is view ID
VIEW_SPECIFIER_TYPE_ID,
// specify view by its position, val is AV_STEREO3D_VIEW_LEFT/RIGHT
VIEW_SPECIFIER_TYPE_POS,
// use all views, val is ignored
VIEW_SPECIFIER_TYPE_ALL,
};
typedef struct ViewSpecifier {
enum ViewSpecifierType type;
unsigned val;
} ViewSpecifier;
/* select an input stream for an output stream */
typedef struct StreamMap {
int disabled; /* 1 is this mapping is disabled by a negative map */
int file_index;
int stream_index;
char *linklabel; /* name of an output link, for mapping lavfi outputs */
ViewSpecifier vs;
} StreamMap;
typedef struct OptionsContext {
OptionGroup *g;
/* input/output options */
int64_t start_time;
int64_t start_time_eof;
int seek_timestamp;
const char *format;
SpecifierOptList codec_names;
SpecifierOptList audio_ch_layouts;
SpecifierOptList audio_channels;
SpecifierOptList audio_sample_rate;
SpecifierOptList frame_rates;
SpecifierOptList max_frame_rates;
SpecifierOptList frame_sizes;
SpecifierOptList frame_pix_fmts;
/* input options */
int64_t input_ts_offset;
int loop;
int rate_emu;
float readrate;
float readrate_catchup;
double readrate_initial_burst;
int accurate_seek;
int thread_queue_size;
int input_sync_ref;
int find_stream_info;
SpecifierOptList ts_scale;
SpecifierOptList dump_attachment;
SpecifierOptList hwaccels;
SpecifierOptList hwaccel_devices;
SpecifierOptList hwaccel_output_formats;
SpecifierOptList autorotate;
SpecifierOptList apply_cropping;
/* output options */
StreamMap *stream_maps;
int nb_stream_maps;
const char **attachments;
int nb_attachments;
int chapters_input_file;
int64_t recording_time;
int64_t stop_time;
int64_t limit_filesize;
float mux_preload;
float mux_max_delay;
float shortest_buf_duration;
int shortest;
int bitexact;
int video_disable;
int audio_disable;
int subtitle_disable;
int data_disable;
// keys are stream indices
AVDictionary *streamid;
SpecifierOptList metadata;
SpecifierOptList max_frames;
SpecifierOptList bitstream_filters;
SpecifierOptList codec_tags;
SpecifierOptList sample_fmts;
SpecifierOptList qscale;
SpecifierOptList forced_key_frames;
SpecifierOptList fps_mode;
SpecifierOptList force_fps;
SpecifierOptList frame_aspect_ratios;
SpecifierOptList display_rotations;
SpecifierOptList display_hflips;
SpecifierOptList display_vflips;
SpecifierOptList rc_overrides;
SpecifierOptList intra_matrices;
SpecifierOptList inter_matrices;
SpecifierOptList chroma_intra_matrices;
#if FFMPEG_OPT_TOP
SpecifierOptList top_field_first;
#endif
SpecifierOptList metadata_map;
SpecifierOptList presets;
SpecifierOptList copy_initial_nonkeyframes;
SpecifierOptList copy_prior_start;
SpecifierOptList filters;
#if FFMPEG_OPT_FILTER_SCRIPT
SpecifierOptList filter_scripts;
#endif
SpecifierOptList reinit_filters;
SpecifierOptList drop_changed;
SpecifierOptList fix_sub_duration;
SpecifierOptList fix_sub_duration_heartbeat;
SpecifierOptList canvas_sizes;
SpecifierOptList pass;
SpecifierOptList passlogfiles;
SpecifierOptList max_muxing_queue_size;
SpecifierOptList muxing_queue_data_threshold;
SpecifierOptList guess_layout_max;
SpecifierOptList apad;
SpecifierOptList discard;
SpecifierOptList disposition;
SpecifierOptList program;
SpecifierOptList stream_groups;
SpecifierOptList time_bases;
SpecifierOptList enc_time_bases;
SpecifierOptList autoscale;
SpecifierOptList bits_per_raw_sample;
SpecifierOptList enc_stats_pre;
SpecifierOptList enc_stats_post;
SpecifierOptList mux_stats;
SpecifierOptList enc_stats_pre_fmt;
SpecifierOptList enc_stats_post_fmt;
SpecifierOptList mux_stats_fmt;
} OptionsContext;
enum IFilterFlags {
IFILTER_FLAG_AUTOROTATE = (1 << 0),
IFILTER_FLAG_REINIT = (1 << 1),
IFILTER_FLAG_CFR = (1 << 2),
IFILTER_FLAG_CROP = (1 << 3),
IFILTER_FLAG_DROPCHANGED = (1 << 4),
};
typedef struct InputFilterOptions {
int64_t trim_start_us;
int64_t trim_end_us;
uint8_t *name;
/* When IFILTER_FLAG_CFR is set, the stream is guaranteed to be CFR with
* this framerate.
*
* Otherwise, this is an estimate that should not be relied upon to be
* accurate */
AVRational framerate;
unsigned crop_top;
unsigned crop_bottom;
unsigned crop_left;
unsigned crop_right;
int sub2video_width;
int sub2video_height;
// a combination of IFILTER_FLAG_*
unsigned flags;
AVFrame *fallback;
} InputFilterOptions;
enum OFilterFlags {
OFILTER_FLAG_DISABLE_CONVERT = (1 << 0),
// produce 24-bit audio
OFILTER_FLAG_AUDIO_24BIT = (1 << 1),
OFILTER_FLAG_AUTOSCALE = (1 << 2),
};
typedef struct OutputFilterOptions {
// Caller-provided name for this output
char *name;
// Codec used for encoding, may be NULL
const AVCodec *enc;
int64_t trim_start_us;
int64_t trim_duration_us;
int64_t ts_offset;
/* Desired output timebase.
* Numerator can be one of EncTimeBase values, or 0 when no preference.
*/
AVRational output_tb;
AVDictionary *sws_opts;
AVDictionary *swr_opts;
int64_t nb_threads;
// A combination of OFilterFlags.
unsigned flags;
int format;
int width;
int height;
enum AVColorSpace color_space;
enum AVColorRange color_range;
enum VideoSyncMethod vsync_method;
AVRational frame_rate;
AVRational max_frame_rate;
int sample_rate;
AVChannelLayout ch_layout;
const int *formats;
const int *sample_rates;
const AVChannelLayout *ch_layouts;
const AVRational *frame_rates;
const enum AVColorSpace *color_spaces;
const enum AVColorRange *color_ranges;
// for simple filtergraphs only, view specifier passed
// along to the decoder
const ViewSpecifier *vs;
} OutputFilterOptions;
typedef struct InputFilter {
struct FilterGraph *graph;
uint8_t *name;
int index;
// filter data type
enum AVMediaType type;
AVFilterContext *filter;
char *input_name;
/* for filters that are not yet bound to an input stream,
* this stores the input linklabel, if any */
uint8_t *linklabel;
} InputFilter;
typedef struct OutputFilter {
const AVClass *class;
struct FilterGraph *graph;
uint8_t *name;
int index;
AVFilterContext *filter;
char *output_name;
/* for filters that are not yet bound to an output stream,
* this stores the output linklabel, if any */
int bound;
uint8_t *linklabel;
char *apad;
enum AVMediaType type;
atomic_uint_least64_t nb_frames_dup;
atomic_uint_least64_t nb_frames_drop;
} OutputFilter;
typedef struct FilterGraph {
const AVClass *class;
int index;
InputFilter **inputs;
int nb_inputs;
OutputFilter **outputs;
int nb_outputs;
const char *graph_desc;
struct AVBPrint graph_print_buf;
} FilterGraph;
enum DecoderFlags {
DECODER_FLAG_FIX_SUB_DURATION = (1 << 0),
// input timestamps are unreliable (guessed by demuxer)
DECODER_FLAG_TS_UNRELIABLE = (1 << 1),
// decoder should override timestamps by fixed framerate
// from DecoderOpts.framerate
DECODER_FLAG_FRAMERATE_FORCED = (1 << 2),
#if FFMPEG_OPT_TOP
DECODER_FLAG_TOP_FIELD_FIRST = (1 << 3),
#endif
DECODER_FLAG_SEND_END_TS = (1 << 4),
// force bitexact decoding
DECODER_FLAG_BITEXACT = (1 << 5),
};
typedef struct DecoderOpts {
int flags;
char *name;
void *log_parent;
const AVCodec *codec;
const AVCodecParameters *par;
/* hwaccel options */
enum HWAccelID hwaccel_id;
enum AVHWDeviceType hwaccel_device_type;
char *hwaccel_device;
enum AVPixelFormat hwaccel_output_format;
AVRational time_base;
// Either forced (when DECODER_FLAG_FRAMERATE_FORCED is set) or
// estimated (otherwise) video framerate.
AVRational framerate;
} DecoderOpts;
typedef struct Decoder {
const AVClass *class;
enum AVMediaType type;
const uint8_t *subtitle_header;
int subtitle_header_size;
// number of frames/samples retrieved from the decoder
uint64_t frames_decoded;
uint64_t samples_decoded;
uint64_t decode_errors;
} Decoder;
typedef struct InputStream {
const AVClass *class;
/* parent source */
struct InputFile *file;
int index;
AVStream *st;
int user_set_discard;
/**
* Codec parameters - to be used by the decoding/streamcopy code.
* st->codecpar should not be accessed, because it may be modified
* concurrently by the demuxing thread.
*/
AVCodecParameters *par;
Decoder *decoder;
const AVCodec *dec;
/* framerate forced with -r */
AVRational framerate;
#if FFMPEG_OPT_TOP
int top_field_first;
#endif
int fix_sub_duration;
/* decoded data from this stream goes into all those filters
* currently video and audio only */
InputFilter **filters;
int nb_filters;
} InputStream;
typedef struct InputFile {
const AVClass *class;
int index;
AVFormatContext *ctx;
int64_t input_ts_offset;
int input_sync_ref;
/**
* Effective format start time based on enabled streams.
*/
int64_t start_time_effective;
int64_t ts_offset;
/* user-specified start time in AV_TIME_BASE or AV_NOPTS_VALUE */
int64_t start_time;
/* streams that ffmpeg is aware of;
* there may be extra streams in ctx that are not mapped to an InputStream
* if new streams appear dynamically during demuxing */
InputStream **streams;
int nb_streams;
} InputFile;
enum forced_keyframes_const {
FKF_N,
FKF_N_FORCED,
FKF_PREV_FORCED_N,
FKF_PREV_FORCED_T,
FKF_T,
FKF_NB
};
#define ABORT_ON_FLAG_EMPTY_OUTPUT (1 << 0)
#define ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM (1 << 1)
enum EncStatsType {
ENC_STATS_LITERAL = 0,
ENC_STATS_FILE_IDX,
ENC_STATS_STREAM_IDX,
ENC_STATS_FRAME_NUM,
ENC_STATS_FRAME_NUM_IN,
ENC_STATS_TIMEBASE,
ENC_STATS_TIMEBASE_IN,
ENC_STATS_PTS,
ENC_STATS_PTS_TIME,
ENC_STATS_PTS_IN,
ENC_STATS_PTS_TIME_IN,
ENC_STATS_DTS,
ENC_STATS_DTS_TIME,
ENC_STATS_SAMPLE_NUM,
ENC_STATS_NB_SAMPLES,
ENC_STATS_PKT_SIZE,
ENC_STATS_BITRATE,
ENC_STATS_AVG_BITRATE,
ENC_STATS_KEYFRAME,
};
typedef struct EncStatsComponent {
enum EncStatsType type;
uint8_t *str;
size_t str_len;
} EncStatsComponent;
typedef struct EncStats {
EncStatsComponent *components;
int nb_components;
AVIOContext *io;
pthread_mutex_t lock;
int lock_initialized;
} EncStats;
enum {
KF_FORCE_SOURCE = 1,
#if FFMPEG_OPT_FORCE_KF_SOURCE_NO_DROP
KF_FORCE_SOURCE_NO_DROP = 2,
#endif
};
typedef struct KeyframeForceCtx {
int type;
int64_t ref_pts;
// timestamps of the forced keyframes, in AV_TIME_BASE_Q
int64_t *pts;
int nb_pts;
int index;
AVExpr *pexpr;
double expr_const_values[FKF_NB];
int dropped_keyframe;
} KeyframeForceCtx;
typedef struct Encoder {
const AVClass *class;
AVCodecContext *enc_ctx;
// number of frames/samples sent to the encoder
uint64_t frames_encoded;
uint64_t samples_encoded;
} Encoder;
enum CroppingType {
CROP_DISABLED = 0,
CROP_ALL,
CROP_CODEC,
CROP_CONTAINER,
};
typedef struct OutputStream {
const AVClass *class;
enum AVMediaType type;
/* parent muxer */
struct OutputFile *file;
int index; /* stream index in the output file */
/* input stream that is the source for this output stream;
* may be NULL for streams with no well-defined source, e.g.
* attachments or outputs from complex filtergraphs */
InputStream *ist;
AVStream *st; /* stream in the output file */
Encoder *enc;
/* video only */
#if FFMPEG_OPT_TOP
int top_field_first;
#endif
int bitexact;
int bits_per_raw_sample;
AVRational frame_aspect_ratio;
KeyframeForceCtx kf;
const char *logfile_prefix;
FILE *logfile;
// simple filtergraph feeding this stream, if any
FilterGraph *fg_simple;
OutputFilter *filter;
char *attachment_filename;
/* stats */
// number of packets send to the muxer
atomic_uint_least64_t packets_written;
/* packet quality factor */
atomic_int quality;
EncStats enc_stats_pre;
EncStats enc_stats_post;
/*
* bool on whether this stream should be utilized for splitting
* subtitles utilizing fix_sub_duration at random access points.
*/
unsigned int fix_sub_duration_heartbeat;
} OutputStream;
typedef struct OutputFile {
const AVClass *class;
int index;
const char *url;
OutputStream **streams;
int nb_streams;
int64_t recording_time; ///< desired length of the resulting file in microseconds == AV_TIME_BASE units
int64_t start_time; ///< start time in microseconds == AV_TIME_BASE units
int bitexact;
} OutputFile;
// optionally attached as opaque_ref to decoded AVFrames
typedef struct FrameData {
// demuxer-estimated dts in AV_TIME_BASE_Q,
// to be used when real dts is missing
int64_t dts_est;
// properties that come from the decoder
struct {
uint64_t frame_num;
int64_t pts;
AVRational tb;
} dec;
AVRational frame_rate_filter;
int bits_per_raw_sample;
int64_t wallclock[LATENCY_PROBE_NB];
AVCodecParameters *par_enc;
} FrameData;
extern InputFile **input_files;
extern int nb_input_files;
extern OutputFile **output_files;
extern int nb_output_files;
// complex filtergraphs
extern FilterGraph **filtergraphs;
extern int nb_filtergraphs;
// standalone decoders (not tied to demuxed streams)
extern Decoder **decoders;
extern int nb_decoders;
extern char *vstats_filename;
extern float dts_delta_threshold;
extern float dts_error_threshold;
extern enum VideoSyncMethod video_sync_method;
extern float frame_drop_threshold;
extern int do_benchmark;
extern int do_benchmark_all;
extern int do_hex_dump;
extern int do_pkt_dump;
extern int copy_ts;
extern int start_at_zero;
extern int copy_tb;
extern int debug_ts;
extern int exit_on_error;
extern int abort_on_flags;
extern int print_stats;
extern int64_t stats_period;
extern int stdin_interaction;
extern AVIOContext *progress_avio;
extern float max_error_rate;
extern char *filter_nbthreads;
extern int filter_complex_nbthreads;
extern int filter_buffered_frames;
extern int vstats_version;
extern int print_graphs;
extern char *print_graphs_file;
extern char *print_graphs_format;
extern int auto_conversion_filters;
extern const AVIOInterruptCB int_cb;
extern const OptionDef options[];
extern HWDevice *filter_hw_device;
extern atomic_uint nb_output_dumped;
extern int ignore_unknown_streams;
extern int copy_unknown_streams;
extern int recast_media;
extern FILE *vstats_file;
void term_init(void);
void term_exit(void);
void show_usage(void);
int check_avoptions_used(const AVDictionary *opts, const AVDictionary *opts_used,
void *logctx, int decode);
int assert_file_overwrite(const char *filename);
int find_codec(void *logctx, const char *name,
enum AVMediaType type, int encoder, const AVCodec **codec);
int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_idx, int is_global);
int filtergraph_is_simple(const FilterGraph *fg);
int fg_create_simple(FilterGraph **pfg,
InputStream *ist,
char *graph_desc,
Scheduler *sch, unsigned sched_idx_enc,
const OutputFilterOptions *opts);
int fg_finalise_bindings(void);
/**
* Get our axiliary frame data attached to the frame, allocating it
* if needed.
*/
FrameData *frame_data(AVFrame *frame);
const FrameData *frame_data_c(AVFrame *frame);
FrameData *packet_data (AVPacket *pkt);
const FrameData *packet_data_c(AVPacket *pkt);
int ofilter_bind_enc(OutputFilter *ofilter,
unsigned sched_idx_enc,
const OutputFilterOptions *opts);
/**
* Create a new filtergraph in the global filtergraph list.
*
* @param graph_desc Graph description; an av_malloc()ed string, filtergraph
* takes ownership of it.
*/
int fg_create(FilterGraph **pfg, char *graph_desc, Scheduler *sch);
void fg_free(FilterGraph **pfg);
void fg_send_command(FilterGraph *fg, double time, const char *target,
const char *command, const char *arg, int all_filters);
int ffmpeg_parse_options(int argc, char **argv, Scheduler *sch);
void enc_stats_write(OutputStream *ost, EncStats *es,
const AVFrame *frame, const AVPacket *pkt,
uint64_t frame_num);
HWDevice *hw_device_get_by_name(const char *name);
HWDevice *hw_device_get_by_type(enum AVHWDeviceType type);
int hw_device_init_from_string(const char *arg, HWDevice **dev);
int hw_device_init_from_type(enum AVHWDeviceType type,
const char *device,
HWDevice **dev_out);
void hw_device_free_all(void);
/**
* Get a hardware device to be used with this filtergraph.
* The returned reference is owned by the callee, the caller
* must ref it explicitly for long-term use.
*/
AVBufferRef *hw_device_for_filter(void);
/**
* Create a standalone decoder.
*/
int dec_create(const OptionsContext *o, const char *arg, Scheduler *sch);
/**
* @param dec_opts Dictionary filled with decoder options. Its ownership
* is transferred to the decoder.
* @param param_out If non-NULL, media properties after opening the decoder
* are written here.
*
* @retval ">=0" non-negative scheduler index on success
* @retval "<0" an error code on failure
*/
int dec_init(Decoder **pdec, Scheduler *sch,
AVDictionary **dec_opts, const DecoderOpts *o,
AVFrame *param_out);
void dec_free(Decoder **pdec);
/*
* Called by filters to connect decoder's output to given filtergraph input.
*
* @param opts filtergraph input options, to be filled by this function
*/
int dec_filter_add(Decoder *dec, InputFilter *ifilter, InputFilterOptions *opts,
const ViewSpecifier *vs, SchedulerNode *src);
/*
* For multiview video, request output of the view(s) determined by vs.
* May be called multiple times.
*
* If this function is never called, only the base view is output. If it is
* called at least once, only the views requested are output.
*
* @param src scheduler node from which the frames corresponding vs
* will originate
*/
int dec_request_view(Decoder *dec, const ViewSpecifier *vs,
SchedulerNode *src);
int enc_alloc(Encoder **penc, const AVCodec *codec,
Scheduler *sch, unsigned sch_idx, void *log_parent);
void enc_free(Encoder **penc);
int enc_open(void *opaque, const AVFrame *frame);
int enc_loopback(Encoder *enc);
/*
* Initialize muxing state for the given stream, should be called
* after the codec/streamcopy setup has been done.
*
* Open the muxer once all the streams have been initialized.
*/
int of_stream_init(OutputFile *of, OutputStream *ost,
const AVCodecContext *enc_ctx);
int of_write_trailer(OutputFile *of);
int of_open(const OptionsContext *o, const char *filename, Scheduler *sch);
void of_free(OutputFile **pof);
void of_enc_stats_close(void);
int64_t of_filesize(OutputFile *of);
int ifile_open(const OptionsContext *o, const char *filename, Scheduler *sch);
void ifile_close(InputFile **f);
int ist_use(InputStream *ist, int decoding_needed,
const ViewSpecifier *vs, SchedulerNode *src);
int ist_filter_add(InputStream *ist, InputFilter *ifilter, int is_simple,
const ViewSpecifier *vs, InputFilterOptions *opts,
SchedulerNode *src);
/**
* Find an unused input stream of given type.
*/
InputStream *ist_find_unused(enum AVMediaType type);
/* iterate over all input streams in all input files;
* pass NULL to start iteration */
InputStream *ist_iter(InputStream *prev);
/* iterate over all output streams in all output files;
* pass NULL to start iteration */
OutputStream *ost_iter(OutputStream *prev);
void update_benchmark(const char *fmt, ...);
const char *opt_match_per_type_str(const SpecifierOptList *sol,
char mediatype);
void opt_match_per_stream_str(void *logctx, const SpecifierOptList *sol,
AVFormatContext *fc, AVStream *st, const char **out);
void opt_match_per_stream_int(void *logctx, const SpecifierOptList *sol,
AVFormatContext *fc, AVStream *st, int *out);
void opt_match_per_stream_int64(void *logctx, const SpecifierOptList *sol,
AVFormatContext *fc, AVStream *st, int64_t *out);
void opt_match_per_stream_dbl(void *logctx, const SpecifierOptList *sol,
AVFormatContext *fc, AVStream *st, double *out);
int view_specifier_parse(const char **pspec, ViewSpecifier *vs);
int muxer_thread(void *arg);
int encoder_thread(void *arg);
#endif /* FFTOOLS_FFMPEG_H */
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+937
View File
@@ -0,0 +1,937 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <math.h>
#include <stdint.h>
#include "ffmpeg.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/avutil.h"
#include "libavutil/dict.h"
#include "libavutil/display.h"
#include "libavutil/eval.h"
#include "libavutil/frame.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/log.h"
#include "libavutil/mem.h"
#include "libavutil/pixdesc.h"
#include "libavutil/rational.h"
#include "libavutil/time.h"
#include "libavutil/timestamp.h"
#include "libavcodec/avcodec.h"
typedef struct EncoderPriv {
Encoder e;
void *log_parent;
char log_name[32];
// combined size of all the packets received from the encoder
uint64_t data_size;
// number of packets received from the encoder
uint64_t packets_encoded;
int opened;
int attach_par;
Scheduler *sch;
unsigned sch_idx;
} EncoderPriv;
static EncoderPriv *ep_from_enc(Encoder *enc)
{
return (EncoderPriv*)enc;
}
// data that is local to the decoder thread and not visible outside of it
typedef struct EncoderThread {
AVFrame *frame;
AVPacket *pkt;
} EncoderThread;
void enc_free(Encoder **penc)
{
Encoder *enc = *penc;
if (!enc)
return;
if (enc->enc_ctx)
av_freep(&enc->enc_ctx->stats_in);
avcodec_free_context(&enc->enc_ctx);
av_freep(penc);
}
static const char *enc_item_name(void *obj)
{
const EncoderPriv *ep = obj;
return ep->log_name;
}
static const AVClass enc_class = {
.class_name = "Encoder",
.version = LIBAVUTIL_VERSION_INT,
.parent_log_context_offset = offsetof(EncoderPriv, log_parent),
.item_name = enc_item_name,
};
int enc_alloc(Encoder **penc, const AVCodec *codec,
Scheduler *sch, unsigned sch_idx, void *log_parent)
{
EncoderPriv *ep;
int ret = 0;
*penc = NULL;
ep = av_mallocz(sizeof(*ep));
if (!ep)
return AVERROR(ENOMEM);
ep->e.class = &enc_class;
ep->log_parent = log_parent;
ep->sch = sch;
ep->sch_idx = sch_idx;
snprintf(ep->log_name, sizeof(ep->log_name), "enc:%s", codec->name);
ep->e.enc_ctx = avcodec_alloc_context3(codec);
if (!ep->e.enc_ctx) {
ret = AVERROR(ENOMEM);
goto fail;
}
*penc = &ep->e;
return 0;
fail:
enc_free((Encoder**)&ep);
return ret;
}
static int hw_device_setup_for_encode(Encoder *e, AVCodecContext *enc_ctx,
AVBufferRef *frames_ref)
{
const AVCodecHWConfig *config;
HWDevice *dev = NULL;
if (frames_ref &&
((AVHWFramesContext*)frames_ref->data)->format ==
enc_ctx->pix_fmt) {
// Matching format, will try to use hw_frames_ctx.
} else {
frames_ref = NULL;
}
for (int i = 0;; i++) {
config = avcodec_get_hw_config(enc_ctx->codec, i);
if (!config)
break;
if (frames_ref &&
config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_FRAMES_CTX &&
(config->pix_fmt == AV_PIX_FMT_NONE ||
config->pix_fmt == enc_ctx->pix_fmt)) {
av_log(e, AV_LOG_VERBOSE, "Using input "
"frames context (format %s) with %s encoder.\n",
av_get_pix_fmt_name(enc_ctx->pix_fmt),
enc_ctx->codec->name);
enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref);
if (!enc_ctx->hw_frames_ctx)
return AVERROR(ENOMEM);
return 0;
}
if (!dev &&
config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)
dev = hw_device_get_by_type(config->device_type);
}
if (dev) {
av_log(e, AV_LOG_VERBOSE, "Using device %s "
"(type %s) with %s encoder.\n", dev->name,
av_hwdevice_get_type_name(dev->type), enc_ctx->codec->name);
enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref);
if (!enc_ctx->hw_device_ctx)
return AVERROR(ENOMEM);
} else {
// No device required, or no device available.
}
return 0;
}
int enc_open(void *opaque, const AVFrame *frame)
{
OutputStream *ost = opaque;
InputStream *ist = ost->ist;
Encoder *e = ost->enc;
EncoderPriv *ep = ep_from_enc(e);
AVCodecContext *enc_ctx = e->enc_ctx;
Decoder *dec = NULL;
const AVCodec *enc = enc_ctx->codec;
OutputFile *of = ost->file;
FrameData *fd;
int frame_samples = 0;
int ret;
if (ep->opened)
return 0;
// frame is always non-NULL for audio and video
av_assert0(frame || (enc->type != AVMEDIA_TYPE_VIDEO && enc->type != AVMEDIA_TYPE_AUDIO));
if (frame) {
av_assert0(frame->opaque_ref);
fd = (FrameData*)frame->opaque_ref->data;
for (int i = 0; i < frame->nb_side_data; i++) {
const AVSideDataDescriptor *desc = av_frame_side_data_desc(frame->side_data[i]->type);
if (!(desc->props & AV_SIDE_DATA_PROP_GLOBAL))
continue;
ret = av_frame_side_data_clone(&enc_ctx->decoded_side_data,
&enc_ctx->nb_decoded_side_data,
frame->side_data[i],
AV_FRAME_SIDE_DATA_FLAG_UNIQUE);
if (ret < 0)
return ret;
}
}
if (ist)
dec = ist->decoder;
// the timebase is chosen by filtering code
if (ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO) {
enc_ctx->time_base = frame->time_base;
enc_ctx->framerate = fd->frame_rate_filter;
}
switch (enc_ctx->codec_type) {
case AVMEDIA_TYPE_AUDIO:
av_assert0(frame->format != AV_SAMPLE_FMT_NONE &&
frame->sample_rate > 0 &&
frame->ch_layout.nb_channels > 0);
enc_ctx->sample_fmt = frame->format;
enc_ctx->sample_rate = frame->sample_rate;
ret = av_channel_layout_copy(&enc_ctx->ch_layout, &frame->ch_layout);
if (ret < 0)
return ret;
if (ost->bits_per_raw_sample)
enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
else
enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3);
break;
case AVMEDIA_TYPE_VIDEO: {
av_assert0(frame->format != AV_PIX_FMT_NONE &&
frame->width > 0 &&
frame->height > 0);
enc_ctx->width = frame->width;
enc_ctx->height = frame->height;
enc_ctx->sample_aspect_ratio =
ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option
av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) :
frame->sample_aspect_ratio;
enc_ctx->pix_fmt = frame->format;
if (ost->bits_per_raw_sample)
enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample;
else
enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample,
av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth);
enc_ctx->color_range = frame->color_range;
enc_ctx->color_primaries = frame->color_primaries;
enc_ctx->color_trc = frame->color_trc;
enc_ctx->colorspace = frame->colorspace;
enc_ctx->chroma_sample_location = frame->chroma_location;
if (enc_ctx->flags & (AV_CODEC_FLAG_INTERLACED_DCT | AV_CODEC_FLAG_INTERLACED_ME) ||
(frame->flags & AV_FRAME_FLAG_INTERLACED)
#if FFMPEG_OPT_TOP
|| ost->top_field_first >= 0
#endif
) {
int top_field_first =
#if FFMPEG_OPT_TOP
ost->top_field_first >= 0 ?
ost->top_field_first :
#endif
!!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST);
if (enc->id == AV_CODEC_ID_MJPEG)
enc_ctx->field_order = top_field_first ? AV_FIELD_TT : AV_FIELD_BB;
else
enc_ctx->field_order = top_field_first ? AV_FIELD_TB : AV_FIELD_BT;
} else
enc_ctx->field_order = AV_FIELD_PROGRESSIVE;
break;
}
case AVMEDIA_TYPE_SUBTITLE:
enc_ctx->time_base = AV_TIME_BASE_Q;
if (!enc_ctx->width) {
enc_ctx->width = ost->ist->par->width;
enc_ctx->height = ost->ist->par->height;
}
av_assert0(dec);
if (dec->subtitle_header) {
/* ASS code assumes this buffer is null terminated so add extra byte. */
enc_ctx->subtitle_header = av_mallocz(dec->subtitle_header_size + 1);
if (!enc_ctx->subtitle_header)
return AVERROR(ENOMEM);
memcpy(enc_ctx->subtitle_header, dec->subtitle_header,
dec->subtitle_header_size);
enc_ctx->subtitle_header_size = dec->subtitle_header_size;
}
break;
default:
av_assert0(0);
break;
}
if (ost->bitexact)
enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
if (enc->capabilities & AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE)
enc_ctx->flags |= AV_CODEC_FLAG_COPY_OPAQUE;
enc_ctx->flags |= AV_CODEC_FLAG_FRAME_DURATION;
ret = hw_device_setup_for_encode(e, enc_ctx, frame ? frame->hw_frames_ctx : NULL);
if (ret < 0) {
av_log(e, AV_LOG_ERROR,
"Encoding hardware device setup failed: %s\n", av_err2str(ret));
return ret;
}
if ((ret = avcodec_open2(enc_ctx, enc, NULL)) < 0) {
if (ret != AVERROR_EXPERIMENTAL)
av_log(e, AV_LOG_ERROR, "Error while opening encoder - maybe "
"incorrect parameters such as bit_rate, rate, width or height.\n");
return ret;
}
ep->opened = 1;
if (enc_ctx->frame_size)
frame_samples = enc_ctx->frame_size;
if (enc_ctx->bit_rate && enc_ctx->bit_rate < 1000 &&
enc_ctx->codec_id != AV_CODEC_ID_CODEC2 /* don't complain about 700 bit/s modes */)
av_log(e, AV_LOG_WARNING, "The bitrate parameter is set too low."
" It takes bits/s as argument, not kbits/s\n");
ret = of_stream_init(of, ost, enc_ctx);
if (ret < 0)
return ret;
return frame_samples;
}
static int check_recording_time(OutputStream *ost, int64_t ts, AVRational tb)
{
OutputFile *of = ost->file;
if (of->recording_time != INT64_MAX &&
av_compare_ts(ts, tb, of->recording_time, AV_TIME_BASE_Q) >= 0) {
return 0;
}
return 1;
}
static int do_subtitle_out(OutputFile *of, OutputStream *ost, const AVSubtitle *sub,
AVPacket *pkt)
{
Encoder *e = ost->enc;
EncoderPriv *ep = ep_from_enc(e);
int subtitle_out_max_size = 1024 * 1024;
int subtitle_out_size, nb, i, ret;
AVCodecContext *enc;
int64_t pts;
if (sub->pts == AV_NOPTS_VALUE) {
av_log(e, AV_LOG_ERROR, "Subtitle packets must have a pts\n");
return exit_on_error ? AVERROR(EINVAL) : 0;
}
if ((of->start_time != AV_NOPTS_VALUE && sub->pts < of->start_time))
return 0;
enc = e->enc_ctx;
/* Note: DVB subtitle need one packet to draw them and one other
packet to clear them */
/* XXX: signal it in the codec context ? */
if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE)
nb = 2;
else if (enc->codec_id == AV_CODEC_ID_ASS)
nb = FFMAX(sub->num_rects, 1);
else
nb = 1;
/* shift timestamp to honor -ss and make check_recording_time() work with -t */
pts = sub->pts;
if (of->start_time != AV_NOPTS_VALUE)
pts -= of->start_time;
for (i = 0; i < nb; i++) {
AVSubtitle local_sub = *sub;
if (!check_recording_time(ost, pts, AV_TIME_BASE_Q))
return AVERROR_EOF;
ret = av_new_packet(pkt, subtitle_out_max_size);
if (ret < 0)
return AVERROR(ENOMEM);
local_sub.pts = pts;
// start_display_time is required to be 0
local_sub.pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, AV_TIME_BASE_Q);
local_sub.end_display_time -= sub->start_display_time;
local_sub.start_display_time = 0;
if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE && i == 1)
local_sub.num_rects = 0;
else if (enc->codec_id == AV_CODEC_ID_ASS && sub->num_rects > 0) {
local_sub.num_rects = 1;
local_sub.rects += i;
}
e->frames_encoded++;
subtitle_out_size = avcodec_encode_subtitle(enc, pkt->data, pkt->size, &local_sub);
if (subtitle_out_size < 0) {
av_log(e, AV_LOG_FATAL, "Subtitle encoding failed\n");
return subtitle_out_size;
}
av_shrink_packet(pkt, subtitle_out_size);
pkt->time_base = AV_TIME_BASE_Q;
pkt->pts = sub->pts;
pkt->duration = av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
if (enc->codec_id == AV_CODEC_ID_DVB_SUBTITLE) {
/* XXX: the pts correction is handled here. Maybe handling
it in the codec would be better */
if (i == 0)
pkt->pts += av_rescale_q(sub->start_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
else
pkt->pts += av_rescale_q(sub->end_display_time, (AVRational){ 1, 1000 }, pkt->time_base);
}
pkt->dts = pkt->pts;
ret = sch_enc_send(ep->sch, ep->sch_idx, pkt);
if (ret < 0) {
av_packet_unref(pkt);
return ret;
}
}
return 0;
}
void enc_stats_write(OutputStream *ost, EncStats *es,
const AVFrame *frame, const AVPacket *pkt,
uint64_t frame_num)
{
Encoder *e = ost->enc;
EncoderPriv *ep = ep_from_enc(e);
AVIOContext *io = es->io;
AVRational tb = frame ? frame->time_base : pkt->time_base;
int64_t pts = frame ? frame->pts : pkt->pts;
AVRational tbi = (AVRational){ 0, 1};
int64_t ptsi = INT64_MAX;
const FrameData *fd = NULL;
if (frame ? frame->opaque_ref : pkt->opaque_ref) {
fd = (const FrameData*)(frame ? frame->opaque_ref->data : pkt->opaque_ref->data);
tbi = fd->dec.tb;
ptsi = fd->dec.pts;
}
pthread_mutex_lock(&es->lock);
for (size_t i = 0; i < es->nb_components; i++) {
const EncStatsComponent *c = &es->components[i];
switch (c->type) {
case ENC_STATS_LITERAL: avio_write (io, c->str, c->str_len); continue;
case ENC_STATS_FILE_IDX: avio_printf(io, "%d", ost->file->index); continue;
case ENC_STATS_STREAM_IDX: avio_printf(io, "%d", ost->index); continue;
case ENC_STATS_TIMEBASE: avio_printf(io, "%d/%d", tb.num, tb.den); continue;
case ENC_STATS_TIMEBASE_IN: avio_printf(io, "%d/%d", tbi.num, tbi.den); continue;
case ENC_STATS_PTS: avio_printf(io, "%"PRId64, pts); continue;
case ENC_STATS_PTS_IN: avio_printf(io, "%"PRId64, ptsi); continue;
case ENC_STATS_PTS_TIME: avio_printf(io, "%g", pts * av_q2d(tb)); continue;
case ENC_STATS_PTS_TIME_IN: avio_printf(io, "%g", ptsi == INT64_MAX ?
INFINITY : ptsi * av_q2d(tbi)); continue;
case ENC_STATS_FRAME_NUM: avio_printf(io, "%"PRIu64, frame_num); continue;
case ENC_STATS_FRAME_NUM_IN: avio_printf(io, "%"PRIu64, fd ? fd->dec.frame_num : -1); continue;
}
if (frame) {
switch (c->type) {
case ENC_STATS_SAMPLE_NUM: avio_printf(io, "%"PRIu64, e->samples_encoded); continue;
case ENC_STATS_NB_SAMPLES: avio_printf(io, "%d", frame->nb_samples); continue;
default: av_assert0(0);
}
} else {
switch (c->type) {
case ENC_STATS_DTS: avio_printf(io, "%"PRId64, pkt->dts); continue;
case ENC_STATS_DTS_TIME: avio_printf(io, "%g", pkt->dts * av_q2d(tb)); continue;
case ENC_STATS_PKT_SIZE: avio_printf(io, "%d", pkt->size); continue;
case ENC_STATS_KEYFRAME: avio_write(io, (pkt->flags & AV_PKT_FLAG_KEY) ?
"K" : "N", 1); continue;
case ENC_STATS_BITRATE: {
double duration = FFMAX(pkt->duration, 1) * av_q2d(tb);
avio_printf(io, "%g", 8.0 * pkt->size / duration);
continue;
}
case ENC_STATS_AVG_BITRATE: {
double duration = pkt->dts * av_q2d(tb);
avio_printf(io, "%g", duration > 0 ? 8.0 * ep->data_size / duration : -1.);
continue;
}
default: av_assert0(0);
}
}
}
avio_w8(io, '\n');
avio_flush(io);
pthread_mutex_unlock(&es->lock);
}
static inline double psnr(double d)
{
return -10.0 * log10(d);
}
static int update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
{
Encoder *e = ost->enc;
EncoderPriv *ep = ep_from_enc(e);
const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
NULL);
AVCodecContext *enc = e->enc_ctx;
enum AVPictureType pict_type;
int64_t frame_number;
double ti1, bitrate, avg_bitrate;
double psnr_val = -1;
int quality;
quality = sd ? AV_RL32(sd) : -1;
pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
atomic_store(&ost->quality, quality);
if ((enc->flags & AV_CODEC_FLAG_PSNR) && sd && sd[5]) {
// FIXME the scaling assumes 8bit
double error = AV_RL64(sd + 8) / (enc->width * enc->height * 255.0 * 255.0);
if (error >= 0 && error <= 1)
psnr_val = psnr(error);
}
if (!write_vstats)
return 0;
/* this is executed just the first time update_video_stats is called */
if (!vstats_file) {
vstats_file = fopen(vstats_filename, "w");
if (!vstats_file) {
perror("fopen");
return AVERROR(errno);
}
}
frame_number = ep->packets_encoded;
if (vstats_version <= 1) {
fprintf(vstats_file, "frame= %5"PRId64" q= %2.1f ", frame_number,
quality / (float)FF_QP2LAMBDA);
} else {
fprintf(vstats_file, "out= %2d st= %2d frame= %5"PRId64" q= %2.1f ",
ost->file->index, ost->index, frame_number,
quality / (float)FF_QP2LAMBDA);
}
if (psnr_val >= 0)
fprintf(vstats_file, "PSNR= %6.2f ", psnr_val);
fprintf(vstats_file,"f_size= %6d ", pkt->size);
/* compute pts value */
ti1 = pkt->dts * av_q2d(pkt->time_base);
if (ti1 < 0.01)
ti1 = 0.01;
bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
avg_bitrate = (double)(ep->data_size * 8) / ti1 / 1000.0;
fprintf(vstats_file, "s_size= %8.0fKiB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
(double)ep->data_size / 1024, ti1, bitrate, avg_bitrate);
fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(pict_type));
return 0;
}
static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame,
AVPacket *pkt)
{
Encoder *e = ost->enc;
EncoderPriv *ep = ep_from_enc(e);
AVCodecContext *enc = e->enc_ctx;
const char *type_desc = av_get_media_type_string(enc->codec_type);
const char *action = frame ? "encode" : "flush";
int ret;
if (frame) {
FrameData *fd = frame_data(frame);
if (!fd)
return AVERROR(ENOMEM);
fd->wallclock[LATENCY_PROBE_ENC_PRE] = av_gettime_relative();
if (ost->enc_stats_pre.io)
enc_stats_write(ost, &ost->enc_stats_pre, frame, NULL,
e->frames_encoded);
e->frames_encoded++;
e->samples_encoded += frame->nb_samples;
if (debug_ts) {
av_log(e, AV_LOG_INFO, "encoder <- type:%s "
"frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
type_desc,
av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
enc->time_base.num, enc->time_base.den);
}
if (frame->sample_aspect_ratio.num && !ost->frame_aspect_ratio.num)
enc->sample_aspect_ratio = frame->sample_aspect_ratio;
}
update_benchmark(NULL);
ret = avcodec_send_frame(enc, frame);
if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
av_log(e, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
type_desc);
return ret;
}
while (1) {
FrameData *fd;
av_packet_unref(pkt);
ret = avcodec_receive_packet(enc, pkt);
update_benchmark("%s_%s %d.%d", action, type_desc,
of->index, ost->index);
pkt->time_base = enc->time_base;
/* if two pass, output log on success and EOF */
if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
fprintf(ost->logfile, "%s", enc->stats_out);
if (ret == AVERROR(EAGAIN)) {
av_assert0(frame); // should never happen during flushing
return 0;
} else if (ret < 0) {
if (ret != AVERROR_EOF)
av_log(e, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
return ret;
}
fd = packet_data(pkt);
if (!fd)
return AVERROR(ENOMEM);
fd->wallclock[LATENCY_PROBE_ENC_POST] = av_gettime_relative();
// attach stream parameters to first packet if requested
avcodec_parameters_free(&fd->par_enc);
if (ep->attach_par && !ep->packets_encoded) {
fd->par_enc = avcodec_parameters_alloc();
if (!fd->par_enc)
return AVERROR(ENOMEM);
ret = avcodec_parameters_from_context(fd->par_enc, enc);
if (ret < 0)
return ret;
}
pkt->flags |= AV_PKT_FLAG_TRUSTED;
if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
ret = update_video_stats(ost, pkt, !!vstats_filename);
if (ret < 0)
return ret;
}
if (ost->enc_stats_post.io)
enc_stats_write(ost, &ost->enc_stats_post, NULL, pkt,
ep->packets_encoded);
if (debug_ts) {
av_log(e, AV_LOG_INFO, "encoder -> type:%s "
"pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
"duration:%s duration_time:%s\n",
type_desc,
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
}
ep->data_size += pkt->size;
ep->packets_encoded++;
ret = sch_enc_send(ep->sch, ep->sch_idx, pkt);
if (ret < 0) {
av_packet_unref(pkt);
return ret;
}
}
av_assert0(0);
}
static enum AVPictureType forced_kf_apply(void *logctx, KeyframeForceCtx *kf,
const AVFrame *frame)
{
double pts_time;
if (kf->ref_pts == AV_NOPTS_VALUE)
kf->ref_pts = frame->pts;
pts_time = (frame->pts - kf->ref_pts) * av_q2d(frame->time_base);
if (kf->index < kf->nb_pts &&
av_compare_ts(frame->pts, frame->time_base, kf->pts[kf->index], AV_TIME_BASE_Q) >= 0) {
kf->index++;
goto force_keyframe;
} else if (kf->pexpr) {
double res;
kf->expr_const_values[FKF_T] = pts_time;
res = av_expr_eval(kf->pexpr,
kf->expr_const_values, NULL);
av_log(logctx, AV_LOG_TRACE,
"force_key_frame: n:%f n_forced:%f prev_forced_n:%f t:%f prev_forced_t:%f -> res:%f\n",
kf->expr_const_values[FKF_N],
kf->expr_const_values[FKF_N_FORCED],
kf->expr_const_values[FKF_PREV_FORCED_N],
kf->expr_const_values[FKF_T],
kf->expr_const_values[FKF_PREV_FORCED_T],
res);
kf->expr_const_values[FKF_N] += 1;
if (res) {
kf->expr_const_values[FKF_PREV_FORCED_N] = kf->expr_const_values[FKF_N] - 1;
kf->expr_const_values[FKF_PREV_FORCED_T] = kf->expr_const_values[FKF_T];
kf->expr_const_values[FKF_N_FORCED] += 1;
goto force_keyframe;
}
} else if (kf->type == KF_FORCE_SOURCE && (frame->flags & AV_FRAME_FLAG_KEY)) {
goto force_keyframe;
}
return AV_PICTURE_TYPE_NONE;
force_keyframe:
av_log(logctx, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
return AV_PICTURE_TYPE_I;
}
static int frame_encode(OutputStream *ost, AVFrame *frame, AVPacket *pkt)
{
Encoder *e = ost->enc;
OutputFile *of = ost->file;
enum AVMediaType type = ost->type;
if (type == AVMEDIA_TYPE_SUBTITLE) {
const AVSubtitle *subtitle = frame && frame->buf[0] ?
(AVSubtitle*)frame->buf[0]->data : NULL;
// no flushing for subtitles
return subtitle && subtitle->num_rects ?
do_subtitle_out(of, ost, subtitle, pkt) : 0;
}
if (frame) {
if (!check_recording_time(ost, frame->pts, frame->time_base))
return AVERROR_EOF;
if (type == AVMEDIA_TYPE_VIDEO) {
frame->quality = e->enc_ctx->global_quality;
frame->pict_type = forced_kf_apply(e, &ost->kf, frame);
#if FFMPEG_OPT_TOP
if (ost->top_field_first >= 0) {
frame->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST;
frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST * (!!ost->top_field_first);
}
#endif
} else {
if (!(e->enc_ctx->codec->capabilities & AV_CODEC_CAP_PARAM_CHANGE) &&
e->enc_ctx->ch_layout.nb_channels != frame->ch_layout.nb_channels) {
av_log(e, AV_LOG_ERROR,
"Audio channel count changed and encoder does not support parameter changes\n");
return 0;
}
}
}
return encode_frame(of, ost, frame, pkt);
}
static void enc_thread_set_name(const OutputStream *ost)
{
char name[16];
snprintf(name, sizeof(name), "enc%d:%d:%s", ost->file->index, ost->index,
ost->enc->enc_ctx->codec->name);
ff_thread_setname(name);
}
static void enc_thread_uninit(EncoderThread *et)
{
av_packet_free(&et->pkt);
av_frame_free(&et->frame);
memset(et, 0, sizeof(*et));
}
static int enc_thread_init(EncoderThread *et)
{
memset(et, 0, sizeof(*et));
et->frame = av_frame_alloc();
if (!et->frame)
goto fail;
et->pkt = av_packet_alloc();
if (!et->pkt)
goto fail;
return 0;
fail:
enc_thread_uninit(et);
return AVERROR(ENOMEM);
}
int encoder_thread(void *arg)
{
OutputStream *ost = arg;
Encoder *e = ost->enc;
EncoderPriv *ep = ep_from_enc(e);
EncoderThread et;
int ret = 0, input_status = 0;
int name_set = 0;
ret = enc_thread_init(&et);
if (ret < 0)
goto finish;
/* Open the subtitle encoders immediately. AVFrame-based encoders
* are opened through a callback from the scheduler once they get
* their first frame
*
* N.B.: because the callback is called from a different thread,
* enc_ctx MUST NOT be accessed before sch_enc_receive() returns
* for the first time for audio/video. */
if (ost->type != AVMEDIA_TYPE_VIDEO && ost->type != AVMEDIA_TYPE_AUDIO) {
ret = enc_open(ost, NULL);
if (ret < 0)
goto finish;
}
while (!input_status) {
input_status = sch_enc_receive(ep->sch, ep->sch_idx, et.frame);
if (input_status < 0) {
if (input_status == AVERROR_EOF) {
av_log(e, AV_LOG_VERBOSE, "Encoder thread received EOF\n");
if (ep->opened)
break;
av_log(e, AV_LOG_ERROR, "Could not open encoder before EOF\n");
ret = AVERROR(EINVAL);
} else {
av_log(e, AV_LOG_ERROR, "Error receiving a frame for encoding: %s\n",
av_err2str(ret));
ret = input_status;
}
goto finish;
}
if (!name_set) {
enc_thread_set_name(ost);
name_set = 1;
}
ret = frame_encode(ost, et.frame, et.pkt);
av_packet_unref(et.pkt);
av_frame_unref(et.frame);
if (ret < 0) {
if (ret == AVERROR_EOF)
av_log(e, AV_LOG_VERBOSE, "Encoder returned EOF, finishing\n");
else
av_log(e, AV_LOG_ERROR, "Error encoding a frame: %s\n",
av_err2str(ret));
break;
}
}
// flush the encoder
if (ret == 0 || ret == AVERROR_EOF) {
ret = frame_encode(ost, NULL, et.pkt);
if (ret < 0 && ret != AVERROR_EOF)
av_log(e, AV_LOG_ERROR, "Error flushing encoder: %s\n",
av_err2str(ret));
}
// EOF is normal thread termination
if (ret == AVERROR_EOF)
ret = 0;
finish:
enc_thread_uninit(&et);
return ret;
}
int enc_loopback(Encoder *enc)
{
EncoderPriv *ep = ep_from_enc(enc);
ep->attach_par = 1;
return ep->sch_idx;
}
File diff suppressed because it is too large Load Diff
+319
View File
@@ -0,0 +1,319 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <string.h>
#include "libavutil/mem.h"
#include "ffmpeg.h"
static int nb_hw_devices;
static HWDevice **hw_devices;
HWDevice *hw_device_get_by_type(enum AVHWDeviceType type)
{
HWDevice *found = NULL;
int i;
for (i = 0; i < nb_hw_devices; i++) {
if (hw_devices[i]->type == type) {
if (found)
return NULL;
found = hw_devices[i];
}
}
return found;
}
HWDevice *hw_device_get_by_name(const char *name)
{
int i;
for (i = 0; i < nb_hw_devices; i++) {
if (!strcmp(hw_devices[i]->name, name))
return hw_devices[i];
}
return NULL;
}
static HWDevice *hw_device_add(void)
{
int err;
err = av_reallocp_array(&hw_devices, nb_hw_devices + 1,
sizeof(*hw_devices));
if (err) {
nb_hw_devices = 0;
return NULL;
}
hw_devices[nb_hw_devices] = av_mallocz(sizeof(HWDevice));
if (!hw_devices[nb_hw_devices])
return NULL;
return hw_devices[nb_hw_devices++];
}
static char *hw_device_default_name(enum AVHWDeviceType type)
{
// Make an automatic name of the form "type%d". We arbitrarily
// limit at 1000 anonymous devices of the same type - there is
// probably something else very wrong if you get to this limit.
const char *type_name = av_hwdevice_get_type_name(type);
char *name;
size_t index_pos;
int index, index_limit = 1000;
index_pos = strlen(type_name);
name = av_malloc(index_pos + 4);
if (!name)
return NULL;
for (index = 0; index < index_limit; index++) {
snprintf(name, index_pos + 4, "%s%d", type_name, index);
if (!hw_device_get_by_name(name))
break;
}
if (index >= index_limit) {
av_freep(&name);
return NULL;
}
return name;
}
int hw_device_init_from_string(const char *arg, HWDevice **dev_out)
{
// "type=name"
// "type=name,key=value,key2=value2"
// "type=name:device,key=value,key2=value2"
// "type:device,key=value,key2=value2"
// -> av_hwdevice_ctx_create()
// "type=name@name"
// "type@name"
// -> av_hwdevice_ctx_create_derived()
AVDictionary *options = NULL;
const char *type_name = NULL, *name = NULL, *device = NULL;
enum AVHWDeviceType type;
HWDevice *dev, *src;
AVBufferRef *device_ref = NULL;
int err;
const char *errmsg, *p, *q;
size_t k;
k = strcspn(arg, ":=@");
p = arg + k;
type_name = av_strndup(arg, k);
if (!type_name) {
err = AVERROR(ENOMEM);
goto fail;
}
type = av_hwdevice_find_type_by_name(type_name);
if (type == AV_HWDEVICE_TYPE_NONE) {
errmsg = "unknown device type";
goto invalid;
}
if (*p == '=') {
k = strcspn(p + 1, ":@,");
name = av_strndup(p + 1, k);
if (!name) {
err = AVERROR(ENOMEM);
goto fail;
}
if (hw_device_get_by_name(name)) {
errmsg = "named device already exists";
goto invalid;
}
p += 1 + k;
} else {
name = hw_device_default_name(type);
if (!name) {
err = AVERROR(ENOMEM);
goto fail;
}
}
if (!*p) {
// New device with no parameters.
err = av_hwdevice_ctx_create(&device_ref, type,
NULL, NULL, 0);
if (err < 0)
goto fail;
} else if (*p == ':') {
// New device with some parameters.
++p;
q = strchr(p, ',');
if (q) {
if (q - p > 0) {
device = av_strndup(p, q - p);
if (!device) {
err = AVERROR(ENOMEM);
goto fail;
}
}
err = av_dict_parse_string(&options, q + 1, "=", ",", 0);
if (err < 0) {
errmsg = "failed to parse options";
goto invalid;
}
}
err = av_hwdevice_ctx_create(&device_ref, type,
q ? device : p[0] ? p : NULL,
options, 0);
if (err < 0)
goto fail;
} else if (*p == '@') {
// Derive from existing device.
src = hw_device_get_by_name(p + 1);
if (!src) {
errmsg = "invalid source device name";
goto invalid;
}
err = av_hwdevice_ctx_create_derived(&device_ref, type,
src->device_ref, 0);
if (err < 0)
goto fail;
} else if (*p == ',') {
err = av_dict_parse_string(&options, p + 1, "=", ",", 0);
if (err < 0) {
errmsg = "failed to parse options";
goto invalid;
}
err = av_hwdevice_ctx_create(&device_ref, type,
NULL, options, 0);
if (err < 0)
goto fail;
} else {
errmsg = "parse error";
goto invalid;
}
dev = hw_device_add();
if (!dev) {
err = AVERROR(ENOMEM);
goto fail;
}
dev->name = name;
dev->type = type;
dev->device_ref = device_ref;
if (dev_out)
*dev_out = dev;
name = NULL;
err = 0;
done:
av_freep(&type_name);
av_freep(&name);
av_freep(&device);
av_dict_free(&options);
return err;
invalid:
av_log(NULL, AV_LOG_ERROR,
"Invalid device specification \"%s\": %s\n", arg, errmsg);
err = AVERROR(EINVAL);
goto done;
fail:
av_log(NULL, AV_LOG_ERROR,
"Device creation failed: %d.\n", err);
av_buffer_unref(&device_ref);
goto done;
}
int hw_device_init_from_type(enum AVHWDeviceType type,
const char *device,
HWDevice **dev_out)
{
AVBufferRef *device_ref = NULL;
HWDevice *dev;
char *name;
int err;
name = hw_device_default_name(type);
if (!name) {
err = AVERROR(ENOMEM);
goto fail;
}
err = av_hwdevice_ctx_create(&device_ref, type, device, NULL, 0);
if (err < 0) {
av_log(NULL, AV_LOG_ERROR,
"Device creation failed: %d.\n", err);
goto fail;
}
dev = hw_device_add();
if (!dev) {
err = AVERROR(ENOMEM);
goto fail;
}
dev->name = name;
dev->type = type;
dev->device_ref = device_ref;
if (dev_out)
*dev_out = dev;
return 0;
fail:
av_freep(&name);
av_buffer_unref(&device_ref);
return err;
}
void hw_device_free_all(void)
{
int i;
for (i = 0; i < nb_hw_devices; i++) {
av_freep(&hw_devices[i]->name);
av_buffer_unref(&hw_devices[i]->device_ref);
av_freep(&hw_devices[i]);
}
av_freep(&hw_devices);
nb_hw_devices = 0;
}
AVBufferRef *hw_device_for_filter(void)
{
// Pick the last hardware device if the user doesn't pick the device for
// filters explicitly with the filter_hw_device option.
if (filter_hw_device)
return filter_hw_device->device_ref;
else if (nb_hw_devices > 0) {
HWDevice *dev = hw_devices[nb_hw_devices - 1];
if (nb_hw_devices > 1)
av_log(NULL, AV_LOG_WARNING, "There are %d hardware devices. device "
"%s of type %s is picked for filters by default. Set hardware "
"device explicitly with the filter_hw_device option if device "
"%s is not usable for filters.\n",
nb_hw_devices, dev->name,
av_hwdevice_get_type_name(dev->type), dev->name);
return dev->device_ref;
}
return NULL;
}
+892
View File
@@ -0,0 +1,892 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdatomic.h>
#include <stdio.h>
#include <string.h>
#include "ffmpeg.h"
#include "ffmpeg_mux.h"
#include "ffmpeg_utils.h"
#include "sync_queue.h"
#include "libavutil/avstring.h"
#include "libavutil/fifo.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/log.h"
#include "libavutil/mem.h"
#include "libavutil/time.h"
#include "libavutil/timestamp.h"
#include "libavcodec/packet.h"
#include "libavformat/avformat.h"
#include "libavformat/avio.h"
typedef struct MuxThreadContext {
AVPacket *pkt;
AVPacket *fix_sub_duration_pkt;
} MuxThreadContext;
static Muxer *mux_from_of(OutputFile *of)
{
return (Muxer*)of;
}
static int64_t filesize(AVIOContext *pb)
{
int64_t ret = -1;
if (pb) {
ret = avio_size(pb);
if (ret <= 0) // FIXME improve avio_size() so it works with non seekable output too
ret = avio_tell(pb);
}
return ret;
}
static void mux_log_debug_ts(OutputStream *ost, const AVPacket *pkt)
{
static const char *desc[] = {
[LATENCY_PROBE_DEMUX] = "demux",
[LATENCY_PROBE_DEC_PRE] = "decode",
[LATENCY_PROBE_DEC_POST] = "decode",
[LATENCY_PROBE_FILTER_PRE] = "filter",
[LATENCY_PROBE_FILTER_POST] = "filter",
[LATENCY_PROBE_ENC_PRE] = "encode",
[LATENCY_PROBE_ENC_POST] = "encode",
[LATENCY_PROBE_NB] = "mux",
};
char latency[512];
*latency = 0;
if (pkt->opaque_ref) {
const FrameData *fd = (FrameData*)pkt->opaque_ref->data;
int64_t now = av_gettime_relative();
int64_t total = INT64_MIN;
int next;
for (unsigned i = 0; i < FF_ARRAY_ELEMS(fd->wallclock); i = next) {
int64_t val = fd->wallclock[i];
next = i + 1;
if (val == INT64_MIN)
continue;
if (total == INT64_MIN) {
total = now - val;
snprintf(latency, sizeof(latency), "total:%gms", total / 1e3);
}
// find the next valid entry
for (; next <= FF_ARRAY_ELEMS(fd->wallclock); next++) {
int64_t val_next = (next == FF_ARRAY_ELEMS(fd->wallclock)) ?
now : fd->wallclock[next];
int64_t diff;
if (val_next == INT64_MIN)
continue;
diff = val_next - val;
// print those stages that take at least 5% of total
if (100. * diff > 5. * total) {
av_strlcat(latency, ", ", sizeof(latency));
if (!strcmp(desc[i], desc[next]))
av_strlcat(latency, desc[i], sizeof(latency));
else
av_strlcatf(latency, sizeof(latency), "%s-%s:",
desc[i], desc[next]);
av_strlcatf(latency, sizeof(latency), " %gms/%d%%",
diff / 1e3, (int)(100. * diff / total));
}
break;
}
}
}
av_log(ost, AV_LOG_INFO, "muxer <- pts:%s pts_time:%s dts:%s dts_time:%s "
"duration:%s duration_time:%s size:%d latency(%s)\n",
av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
pkt->size, *latency ? latency : "N/A");
}
static int mux_fixup_ts(Muxer *mux, MuxStream *ms, AVPacket *pkt)
{
OutputStream *ost = &ms->ost;
#if FFMPEG_OPT_VSYNC_DROP
if (ost->type == AVMEDIA_TYPE_VIDEO && ms->ts_drop)
pkt->pts = pkt->dts = AV_NOPTS_VALUE;
#endif
// rescale timestamps to the stream timebase
if (ost->type == AVMEDIA_TYPE_AUDIO && !ost->enc) {
// use av_rescale_delta() for streamcopying audio, to preserve
// accuracy with coarse input timebases
int duration = av_get_audio_frame_duration2(ost->st->codecpar, pkt->size);
if (!duration)
duration = ost->st->codecpar->frame_size;
pkt->dts = av_rescale_delta(pkt->time_base, pkt->dts,
(AVRational){1, ost->st->codecpar->sample_rate}, duration,
&ms->ts_rescale_delta_last, ost->st->time_base);
pkt->pts = pkt->dts;
pkt->duration = av_rescale_q(pkt->duration, pkt->time_base, ost->st->time_base);
} else
av_packet_rescale_ts(pkt, pkt->time_base, ost->st->time_base);
pkt->time_base = ost->st->time_base;
if (!(mux->fc->oformat->flags & AVFMT_NOTIMESTAMPS)) {
if (pkt->dts != AV_NOPTS_VALUE &&
pkt->pts != AV_NOPTS_VALUE &&
pkt->dts > pkt->pts) {
av_log(ost, AV_LOG_WARNING, "Invalid DTS: %"PRId64" PTS: %"PRId64", replacing by guess\n",
pkt->dts, pkt->pts);
pkt->pts =
pkt->dts = pkt->pts + pkt->dts + ms->last_mux_dts + 1
- FFMIN3(pkt->pts, pkt->dts, ms->last_mux_dts + 1)
- FFMAX3(pkt->pts, pkt->dts, ms->last_mux_dts + 1);
}
if ((ost->type == AVMEDIA_TYPE_AUDIO || ost->type == AVMEDIA_TYPE_VIDEO || ost->type == AVMEDIA_TYPE_SUBTITLE) &&
pkt->dts != AV_NOPTS_VALUE &&
ms->last_mux_dts != AV_NOPTS_VALUE) {
int64_t max = ms->last_mux_dts + !(mux->fc->oformat->flags & AVFMT_TS_NONSTRICT);
if (pkt->dts < max) {
int loglevel = max - pkt->dts > 2 || ost->type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
if (exit_on_error)
loglevel = AV_LOG_ERROR;
av_log(ost, loglevel, "Non-monotonic DTS; "
"previous: %"PRId64", current: %"PRId64"; ",
ms->last_mux_dts, pkt->dts);
if (exit_on_error) {
return AVERROR(EINVAL);
}
av_log(ost, loglevel, "changing to %"PRId64". This may result "
"in incorrect timestamps in the output file.\n",
max);
if (pkt->pts >= pkt->dts)
pkt->pts = FFMAX(pkt->pts, max);
pkt->dts = max;
}
}
}
ms->last_mux_dts = pkt->dts;
if (debug_ts)
mux_log_debug_ts(ost, pkt);
return 0;
}
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
{
MuxStream *ms = ms_from_ost(ost);
AVFormatContext *s = mux->fc;
int64_t fs;
uint64_t frame_num;
int ret;
fs = filesize(s->pb);
atomic_store(&mux->last_filesize, fs);
if (fs >= mux->limit_filesize) {
ret = AVERROR_EOF;
goto fail;
}
ret = mux_fixup_ts(mux, ms, pkt);
if (ret < 0)
goto fail;
ms->data_size_mux += pkt->size;
frame_num = atomic_fetch_add(&ost->packets_written, 1);
pkt->stream_index = ost->index;
if (ms->stats.io)
enc_stats_write(ost, &ms->stats, NULL, pkt, frame_num);
ret = av_interleaved_write_frame(s, pkt);
if (ret < 0) {
av_log(ost, AV_LOG_ERROR,
"Error submitting a packet to the muxer: %s\n",
av_err2str(ret));
goto fail;
}
return 0;
fail:
av_packet_unref(pkt);
return ret;
}
static int sync_queue_process(Muxer *mux, MuxStream *ms, AVPacket *pkt, int *stream_eof)
{
OutputFile *of = &mux->of;
if (ms->sq_idx_mux >= 0) {
int ret = sq_send(mux->sq_mux, ms->sq_idx_mux, SQPKT(pkt));
if (ret < 0) {
if (ret == AVERROR_EOF)
*stream_eof = 1;
return ret;
}
while (1) {
ret = sq_receive(mux->sq_mux, -1, SQPKT(mux->sq_pkt));
if (ret < 0) {
/* n.b.: We forward EOF from the sync queue, terminating muxing.
* This assumes that if a muxing sync queue is present, then all
* the streams use it. That is true currently, but may change in
* the future, then this code needs to be revisited.
*/
return ret == AVERROR(EAGAIN) ? 0 : ret;
}
ret = write_packet(mux, of->streams[ret],
mux->sq_pkt);
if (ret < 0)
return ret;
}
} else if (pkt)
return write_packet(mux, &ms->ost, pkt);
return 0;
}
static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt);
/* apply the output bitstream filters */
static int mux_packet_filter(Muxer *mux, MuxThreadContext *mt,
OutputStream *ost, AVPacket *pkt, int *stream_eof)
{
MuxStream *ms = ms_from_ost(ost);
const char *err_msg;
int ret;
if (pkt && !ost->enc) {
ret = of_streamcopy(&mux->of, ost, pkt);
if (ret == AVERROR(EAGAIN))
return 0;
else if (ret == AVERROR_EOF) {
av_packet_unref(pkt);
pkt = NULL;
*stream_eof = 1;
} else if (ret < 0)
goto fail;
}
// emit heartbeat for -fix_sub_duration;
// we are only interested in heartbeats on on random access points.
if (pkt && (pkt->flags & AV_PKT_FLAG_KEY)) {
mt->fix_sub_duration_pkt->opaque = (void*)(intptr_t)PKT_OPAQUE_FIX_SUB_DURATION;
mt->fix_sub_duration_pkt->pts = pkt->pts;
mt->fix_sub_duration_pkt->time_base = pkt->time_base;
ret = sch_mux_sub_heartbeat(mux->sch, mux->sch_idx, ms->sch_idx,
mt->fix_sub_duration_pkt);
if (ret < 0)
goto fail;
}
if (ms->bsf_ctx) {
int bsf_eof = 0;
if (pkt)
av_packet_rescale_ts(pkt, pkt->time_base, ms->bsf_ctx->time_base_in);
ret = av_bsf_send_packet(ms->bsf_ctx, pkt);
if (ret < 0) {
err_msg = "submitting a packet for bitstream filtering";
goto fail;
}
while (!bsf_eof) {
ret = av_bsf_receive_packet(ms->bsf_ctx, ms->bsf_pkt);
if (ret == AVERROR(EAGAIN))
return 0;
else if (ret == AVERROR_EOF)
bsf_eof = 1;
else if (ret < 0) {
av_log(ost, AV_LOG_ERROR,
"Error applying bitstream filters to a packet: %s",
av_err2str(ret));
if (exit_on_error)
return ret;
continue;
}
if (!bsf_eof)
ms->bsf_pkt->time_base = ms->bsf_ctx->time_base_out;
ret = sync_queue_process(mux, ms, bsf_eof ? NULL : ms->bsf_pkt, stream_eof);
if (ret < 0)
goto mux_fail;
}
*stream_eof = 1;
} else {
ret = sync_queue_process(mux, ms, pkt, stream_eof);
if (ret < 0)
goto mux_fail;
}
return *stream_eof ? AVERROR_EOF : 0;
mux_fail:
err_msg = "submitting a packet to the muxer";
fail:
if (ret != AVERROR_EOF)
av_log(ost, AV_LOG_ERROR, "Error %s: %s\n", err_msg, av_err2str(ret));
return ret;
}
static void thread_set_name(Muxer *mux)
{
char name[16];
snprintf(name, sizeof(name), "mux%d:%s",
mux->of.index, mux->fc->oformat->name);
ff_thread_setname(name);
}
static void mux_thread_uninit(MuxThreadContext *mt)
{
av_packet_free(&mt->pkt);
av_packet_free(&mt->fix_sub_duration_pkt);
memset(mt, 0, sizeof(*mt));
}
static int mux_thread_init(MuxThreadContext *mt)
{
memset(mt, 0, sizeof(*mt));
mt->pkt = av_packet_alloc();
if (!mt->pkt)
goto fail;
mt->fix_sub_duration_pkt = av_packet_alloc();
if (!mt->fix_sub_duration_pkt)
goto fail;
return 0;
fail:
mux_thread_uninit(mt);
return AVERROR(ENOMEM);
}
int muxer_thread(void *arg)
{
Muxer *mux = arg;
OutputFile *of = &mux->of;
MuxThreadContext mt;
int ret = 0;
ret = mux_thread_init(&mt);
if (ret < 0)
goto finish;
thread_set_name(mux);
while (1) {
OutputStream *ost;
int stream_idx, stream_eof = 0;
ret = sch_mux_receive(mux->sch, of->index, mt.pkt);
stream_idx = mt.pkt->stream_index;
if (stream_idx < 0) {
av_log(mux, AV_LOG_VERBOSE, "All streams finished\n");
ret = 0;
break;
}
ost = of->streams[mux->sch_stream_idx[stream_idx]];
mt.pkt->stream_index = ost->index;
mt.pkt->flags &= ~AV_PKT_FLAG_TRUSTED;
ret = mux_packet_filter(mux, &mt, ost, ret < 0 ? NULL : mt.pkt, &stream_eof);
av_packet_unref(mt.pkt);
if (ret == AVERROR_EOF) {
if (stream_eof) {
sch_mux_receive_finish(mux->sch, of->index, stream_idx);
} else {
av_log(mux, AV_LOG_VERBOSE, "Muxer returned EOF\n");
ret = 0;
break;
}
} else if (ret < 0) {
av_log(mux, AV_LOG_ERROR, "Error muxing a packet\n");
break;
}
}
finish:
mux_thread_uninit(&mt);
return ret;
}
static int of_streamcopy(OutputFile *of, OutputStream *ost, AVPacket *pkt)
{
MuxStream *ms = ms_from_ost(ost);
FrameData *fd = pkt->opaque_ref ? (FrameData*)pkt->opaque_ref->data : NULL;
int64_t dts = fd ? fd->dts_est : AV_NOPTS_VALUE;
int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
int64_t ts_offset;
if (of->recording_time != INT64_MAX &&
dts >= of->recording_time + start_time)
return AVERROR_EOF;
if (!ms->streamcopy_started && !(pkt->flags & AV_PKT_FLAG_KEY) &&
!ms->copy_initial_nonkeyframes)
return AVERROR(EAGAIN);
if (!ms->streamcopy_started) {
if (!ms->copy_prior_start &&
(pkt->pts == AV_NOPTS_VALUE ?
dts < ms->ts_copy_start :
pkt->pts < av_rescale_q(ms->ts_copy_start, AV_TIME_BASE_Q, pkt->time_base)))
return AVERROR(EAGAIN);
if (of->start_time != AV_NOPTS_VALUE && dts < of->start_time)
return AVERROR(EAGAIN);
}
ts_offset = av_rescale_q(start_time, AV_TIME_BASE_Q, pkt->time_base);
if (pkt->pts != AV_NOPTS_VALUE)
pkt->pts -= ts_offset;
if (pkt->dts == AV_NOPTS_VALUE) {
pkt->dts = av_rescale_q(dts, AV_TIME_BASE_Q, pkt->time_base);
} else if (ost->st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
pkt->pts = pkt->dts - ts_offset;
}
pkt->dts -= ts_offset;
ms->streamcopy_started = 1;
return 0;
}
int print_sdp(const char *filename);
int print_sdp(const char *filename)
{
char sdp[16384];
int j = 0, ret;
AVIOContext *sdp_pb;
AVFormatContext **avc;
avc = av_malloc_array(nb_output_files, sizeof(*avc));
if (!avc)
return AVERROR(ENOMEM);
for (int i = 0; i < nb_output_files; i++) {
Muxer *mux = mux_from_of(output_files[i]);
if (!strcmp(mux->fc->oformat->name, "rtp")) {
avc[j] = mux->fc;
j++;
}
}
if (!j) {
av_log(NULL, AV_LOG_ERROR, "No output streams in the SDP.\n");
ret = AVERROR(EINVAL);
goto fail;
}
ret = av_sdp_create(avc, j, sdp, sizeof(sdp));
if (ret < 0)
goto fail;
if (!filename) {
printf("SDP:\n%s\n", sdp);
fflush(stdout);
} else {
ret = avio_open2(&sdp_pb, filename, AVIO_FLAG_WRITE, &int_cb, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to open sdp file '%s'\n", filename);
goto fail;
}
avio_print(sdp_pb, sdp);
avio_closep(&sdp_pb);
}
fail:
av_freep(&avc);
return ret;
}
int mux_check_init(void *arg)
{
Muxer *mux = arg;
OutputFile *of = &mux->of;
AVFormatContext *fc = mux->fc;
int ret;
ret = avformat_write_header(fc, &mux->opts);
if (ret < 0) {
av_log(mux, AV_LOG_ERROR, "Could not write header (incorrect codec "
"parameters ?): %s\n", av_err2str(ret));
return ret;
}
//assert_avoptions(of->opts);
mux->header_written = 1;
av_dump_format(fc, of->index, fc->url, 1);
atomic_fetch_add(&nb_output_dumped, 1);
return 0;
}
static int bsf_init(MuxStream *ms)
{
OutputStream *ost = &ms->ost;
AVBSFContext *ctx = ms->bsf_ctx;
int ret;
if (!ctx)
return avcodec_parameters_copy(ost->st->codecpar, ms->par_in);
ret = avcodec_parameters_copy(ctx->par_in, ms->par_in);
if (ret < 0)
return ret;
ctx->time_base_in = ost->st->time_base;
ret = av_bsf_init(ctx);
if (ret < 0) {
av_log(ms, AV_LOG_ERROR, "Error initializing bitstream filter: %s\n",
ctx->filter->name);
return ret;
}
ret = avcodec_parameters_copy(ost->st->codecpar, ctx->par_out);
if (ret < 0)
return ret;
ost->st->time_base = ctx->time_base_out;
ms->bsf_pkt = av_packet_alloc();
if (!ms->bsf_pkt)
return AVERROR(ENOMEM);
return 0;
}
int of_stream_init(OutputFile *of, OutputStream *ost,
const AVCodecContext *enc_ctx)
{
Muxer *mux = mux_from_of(of);
MuxStream *ms = ms_from_ost(ost);
int ret;
if (enc_ctx) {
// use upstream time base unless it has been overridden previously
if (ost->st->time_base.num <= 0 || ost->st->time_base.den <= 0)
ost->st->time_base = av_add_q(enc_ctx->time_base, (AVRational){0, 1});
ost->st->avg_frame_rate = enc_ctx->framerate;
ost->st->sample_aspect_ratio = enc_ctx->sample_aspect_ratio;
ret = avcodec_parameters_from_context(ms->par_in, enc_ctx);
if (ret < 0) {
av_log(ost, AV_LOG_FATAL,
"Error initializing the output stream codec parameters.\n");
return ret;
}
}
/* initialize bitstream filters for the output stream
* needs to be done here, because the codec id for streamcopy is not
* known until now */
ret = bsf_init(ms);
if (ret < 0)
return ret;
if (ms->stream_duration) {
ost->st->duration = av_rescale_q(ms->stream_duration, ms->stream_duration_tb,
ost->st->time_base);
}
if (ms->sch_idx >= 0)
return sch_mux_stream_ready(mux->sch, of->index, ms->sch_idx);
return 0;
}
static int check_written(OutputFile *of)
{
int64_t total_packets_written = 0;
int pass1_used = 1;
int ret = 0;
for (int i = 0; i < of->nb_streams; i++) {
OutputStream *ost = of->streams[i];
uint64_t packets_written = atomic_load(&ost->packets_written);
total_packets_written += packets_written;
if (ost->enc &&
(ost->enc->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
!= AV_CODEC_FLAG_PASS1)
pass1_used = 0;
if (!packets_written &&
(abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) {
av_log(ost, AV_LOG_FATAL, "Empty output stream\n");
ret = err_merge(ret, AVERROR(EINVAL));
}
}
if (!total_packets_written) {
int level = AV_LOG_WARNING;
if (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT) {
ret = err_merge(ret, AVERROR(EINVAL));
level = AV_LOG_FATAL;
}
av_log(of, level, "Output file is empty, nothing was encoded%s\n",
pass1_used ? "" : "(check -ss / -t / -frames parameters if used)");
}
return ret;
}
static void mux_final_stats(Muxer *mux)
{
OutputFile *of = &mux->of;
uint64_t total_packets = 0, total_size = 0;
uint64_t video_size = 0, audio_size = 0, subtitle_size = 0,
extra_size = 0, other_size = 0;
uint8_t overhead[16] = "unknown";
int64_t file_size = of_filesize(of);
av_log(of, AV_LOG_VERBOSE, "Output file #%d (%s):\n",
of->index, of->url);
for (int j = 0; j < of->nb_streams; j++) {
OutputStream *ost = of->streams[j];
MuxStream *ms = ms_from_ost(ost);
const AVCodecParameters *par = ost->st->codecpar;
const enum AVMediaType type = par->codec_type;
const uint64_t s = ms->data_size_mux;
switch (type) {
case AVMEDIA_TYPE_VIDEO: video_size += s; break;
case AVMEDIA_TYPE_AUDIO: audio_size += s; break;
case AVMEDIA_TYPE_SUBTITLE: subtitle_size += s; break;
default: other_size += s; break;
}
extra_size += par->extradata_size;
total_size += s;
total_packets += atomic_load(&ost->packets_written);
av_log(of, AV_LOG_VERBOSE, " Output stream #%d:%d (%s): ",
of->index, j, av_get_media_type_string(type));
if (ost->enc) {
av_log(of, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
ost->enc->frames_encoded);
if (type == AVMEDIA_TYPE_AUDIO)
av_log(of, AV_LOG_VERBOSE, " (%"PRIu64" samples)", ost->enc->samples_encoded);
av_log(of, AV_LOG_VERBOSE, "; ");
}
av_log(of, AV_LOG_VERBOSE, "%"PRIu64" packets muxed (%"PRIu64" bytes); ",
atomic_load(&ost->packets_written), s);
av_log(of, AV_LOG_VERBOSE, "\n");
}
av_log(of, AV_LOG_VERBOSE, " Total: %"PRIu64" packets (%"PRIu64" bytes) muxed\n",
total_packets, total_size);
if (total_size && file_size > 0 && file_size >= total_size) {
snprintf(overhead, sizeof(overhead), "%f%%",
100.0 * (file_size - total_size) / total_size);
}
av_log(of, AV_LOG_INFO,
"video:%1.0fKiB audio:%1.0fKiB subtitle:%1.0fKiB other streams:%1.0fKiB "
"global headers:%1.0fKiB muxing overhead: %s\n",
video_size / 1024.0,
audio_size / 1024.0,
subtitle_size / 1024.0,
other_size / 1024.0,
extra_size / 1024.0,
overhead);
}
int of_write_trailer(OutputFile *of)
{
Muxer *mux = mux_from_of(of);
AVFormatContext *fc = mux->fc;
int ret, mux_result = 0;
if (!mux->header_written) {
av_log(mux, AV_LOG_ERROR,
"Nothing was written into output file, because "
"at least one of its streams received no packets.\n");
return AVERROR(EINVAL);
}
ret = av_write_trailer(fc);
if (ret < 0) {
av_log(mux, AV_LOG_ERROR, "Error writing trailer: %s\n", av_err2str(ret));
mux_result = err_merge(mux_result, ret);
}
mux->last_filesize = filesize(fc->pb);
if (!(fc->oformat->flags & AVFMT_NOFILE)) {
ret = avio_closep(&fc->pb);
if (ret < 0) {
av_log(mux, AV_LOG_ERROR, "Error closing file: %s\n", av_err2str(ret));
mux_result = err_merge(mux_result, ret);
}
}
mux_final_stats(mux);
// check whether anything was actually written
ret = check_written(of);
mux_result = err_merge(mux_result, ret);
return mux_result;
}
static void enc_stats_uninit(EncStats *es)
{
for (int i = 0; i < es->nb_components; i++)
av_freep(&es->components[i].str);
av_freep(&es->components);
if (es->lock_initialized)
pthread_mutex_destroy(&es->lock);
es->lock_initialized = 0;
}
static void ost_free(OutputStream **post)
{
OutputStream *ost = *post;
MuxStream *ms;
if (!ost)
return;
ms = ms_from_ost(ost);
enc_free(&ost->enc);
fg_free(&ost->fg_simple);
if (ost->logfile) {
if (fclose(ost->logfile))
av_log(ms, AV_LOG_ERROR,
"Error closing logfile, loss of information possible: %s\n",
av_err2str(AVERROR(errno)));
ost->logfile = NULL;
}
avcodec_parameters_free(&ms->par_in);
av_bsf_free(&ms->bsf_ctx);
av_packet_free(&ms->bsf_pkt);
av_packet_free(&ms->pkt);
av_freep(&ost->kf.pts);
av_expr_free(ost->kf.pexpr);
av_freep(&ost->logfile_prefix);
av_freep(&ost->attachment_filename);
enc_stats_uninit(&ost->enc_stats_pre);
enc_stats_uninit(&ost->enc_stats_post);
enc_stats_uninit(&ms->stats);
av_freep(post);
}
static void fc_close(AVFormatContext **pfc)
{
AVFormatContext *fc = *pfc;
if (!fc)
return;
if (!(fc->oformat->flags & AVFMT_NOFILE))
avio_closep(&fc->pb);
avformat_free_context(fc);
*pfc = NULL;
}
void of_free(OutputFile **pof)
{
OutputFile *of = *pof;
Muxer *mux;
if (!of)
return;
mux = mux_from_of(of);
sq_free(&mux->sq_mux);
for (int i = 0; i < of->nb_streams; i++)
ost_free(&of->streams[i]);
av_freep(&of->streams);
av_freep(&mux->sch_stream_idx);
av_dict_free(&mux->opts);
av_dict_free(&mux->enc_opts_used);
av_packet_free(&mux->sq_pkt);
fc_close(&mux->fc);
av_freep(pof);
}
int64_t of_filesize(OutputFile *of)
{
Muxer *mux = mux_from_of(of);
return atomic_load(&mux->last_filesize);
}
+131
View File
@@ -0,0 +1,131 @@
/*
* Muxer internal APIs - should not be included outside of ffmpeg_mux*
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_FFMPEG_MUX_H
#define FFTOOLS_FFMPEG_MUX_H
#include <stdatomic.h>
#include <stdint.h>
#include "ffmpeg_sched.h"
#include "libavformat/avformat.h"
#include "libavcodec/packet.h"
#include "libavutil/dict.h"
#include "libavutil/fifo.h"
typedef struct MuxStream {
OutputStream ost;
/**
* Codec parameters for packets submitted to the muxer (i.e. before
* bitstream filtering, if any).
*/
AVCodecParameters *par_in;
// name used for logging
char log_name[32];
AVBSFContext *bsf_ctx;
AVPacket *bsf_pkt;
AVPacket *pkt;
EncStats stats;
int sch_idx;
int sch_idx_enc;
int sch_idx_src;
int sq_idx_mux;
int64_t max_frames;
// timestamp from which the streamcopied streams should start,
// in AV_TIME_BASE_Q;
// everything before it should be discarded
int64_t ts_copy_start;
/* dts of the last packet sent to the muxer, in the stream timebase
* used for making up missing dts values */
int64_t last_mux_dts;
int64_t stream_duration;
AVRational stream_duration_tb;
// state for av_rescale_delta() call for audio in write_packet()
int64_t ts_rescale_delta_last;
// combined size of all the packets sent to the muxer
uint64_t data_size_mux;
int copy_initial_nonkeyframes;
int copy_prior_start;
int streamcopy_started;
#if FFMPEG_OPT_VSYNC_DROP
int ts_drop;
#endif
AVRational frame_rate;
AVRational max_frame_rate;
int force_fps;
const char *apad;
} MuxStream;
typedef struct Muxer {
OutputFile of;
// name used for logging
char log_name[32];
AVFormatContext *fc;
Scheduler *sch;
unsigned sch_idx;
// OutputStream indices indexed by scheduler stream indices
int *sch_stream_idx;
int nb_sch_stream_idx;
AVDictionary *opts;
// used to validate that all encoder avoptions have been actually used
AVDictionary *enc_opts_used;
/* filesize limit expressed in bytes */
int64_t limit_filesize;
atomic_int_least64_t last_filesize;
int header_written;
SyncQueue *sq_mux;
AVPacket *sq_pkt;
} Muxer;
int mux_check_init(void *arg);
static inline MuxStream *ms_from_ost(OutputStream *ost)
{
return (MuxStream*)ost;
}
#endif /* FFTOOLS_FFMPEG_MUX_H */
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+504
View File
@@ -0,0 +1,504 @@
/*
* Inter-thread scheduling/synchronization.
* Copyright (c) 2023 Anton Khirnov
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_FFMPEG_SCHED_H
#define FFTOOLS_FFMPEG_SCHED_H
#include <stddef.h>
#include <stdint.h>
#include "ffmpeg_utils.h"
/*
* This file contains the API for the transcode scheduler.
*
* Overall architecture of the transcoding process involves instances of the
* following components:
* - demuxers, each containing any number of demuxed streams; demuxed packets
* belonging to some stream are sent to any number of decoders (transcoding)
* and/or muxers (streamcopy);
* - decoders, which receive encoded packets from some demuxed stream or
* encoder, decode them, and send decoded frames to any number of filtergraph
* inputs (audio/video) or encoders (subtitles);
* - filtergraphs, each containing zero or more inputs (0 in case the
* filtergraph contains a lavfi source filter), and one or more outputs; the
* inputs and outputs need not have matching media types;
* each filtergraph input receives decoded frames from some decoder or another
* filtergraph output;
* filtered frames from each output are sent to some encoder;
* - encoders, which receive decoded frames from some decoder (subtitles) or
* some filtergraph output (audio/video), encode them, and send encoded
* packets to any number of muxed streams or decoders;
* - muxers, each containing any number of muxed streams; each muxed stream
* receives encoded packets from some demuxed stream (streamcopy) or some
* encoder (transcoding); those packets are interleaved and written out by the
* muxer.
*
* The structure formed by the above components is a directed acyclic graph
* (absence of cycles is checked at startup).
*
* There must be at least one muxer instance, otherwise the transcode produces
* no output and is meaningless. Otherwise, in a generic transcoding scenario
* there may be arbitrary number of instances of any of the above components,
* interconnected in various ways.
*
* The code tries to keep all the output streams across all the muxers in sync
* (i.e. at the same DTS), which is accomplished by varying the rates at which
* packets are read from different demuxers and lavfi sources. Note that the
* degree of control we have over synchronization is fundamentally limited - if
* some demuxed streams in the same input are interleaved at different rates
* than that at which they are to be muxed (e.g. because an input file is badly
* interleaved, or the user changed their speed by mismatching amounts), then
* there will be increasing amounts of buffering followed by eventual
* transcoding failure.
*
* N.B. 1: there are meaningful transcode scenarios with no demuxers, e.g.
* - encoding and muxing output from filtergraph(s) that have no inputs;
* - creating a file that contains nothing but attachments and/or metadata.
*
* N.B. 2: a filtergraph output could, in principle, feed multiple encoders, but
* this is unnecessary because the (a)split filter provides the same
* functionality.
*
* The scheduler, in the above model, is the master object that oversees and
* facilitates the transcoding process. The basic idea is that all instances
* of the abovementioned components communicate only with the scheduler and not
* with each other. The scheduler is then the single place containing the
* knowledge about the whole transcoding pipeline.
*/
struct AVFrame;
struct AVPacket;
typedef struct Scheduler Scheduler;
enum SchedulerNodeType {
SCH_NODE_TYPE_NONE = 0,
SCH_NODE_TYPE_DEMUX,
SCH_NODE_TYPE_MUX,
SCH_NODE_TYPE_DEC,
SCH_NODE_TYPE_ENC,
SCH_NODE_TYPE_FILTER_IN,
SCH_NODE_TYPE_FILTER_OUT,
};
typedef struct SchedulerNode {
enum SchedulerNodeType type;
unsigned idx;
unsigned idx_stream;
} SchedulerNode;
typedef int (*SchThreadFunc)(void *arg);
#define SCH_DSTREAM(file, stream) \
(SchedulerNode){ .type = SCH_NODE_TYPE_DEMUX, \
.idx = file, .idx_stream = stream }
#define SCH_MSTREAM(file, stream) \
(SchedulerNode){ .type = SCH_NODE_TYPE_MUX, \
.idx = file, .idx_stream = stream }
#define SCH_DEC_IN(decoder) \
(SchedulerNode){ .type = SCH_NODE_TYPE_DEC, \
.idx = decoder }
#define SCH_DEC_OUT(decoder, out_idx) \
(SchedulerNode){ .type = SCH_NODE_TYPE_DEC, \
.idx = decoder, .idx_stream = out_idx }
#define SCH_ENC(encoder) \
(SchedulerNode){ .type = SCH_NODE_TYPE_ENC, \
.idx = encoder }
#define SCH_FILTER_IN(filter, input) \
(SchedulerNode){ .type = SCH_NODE_TYPE_FILTER_IN, \
.idx = filter, .idx_stream = input }
#define SCH_FILTER_OUT(filter, output) \
(SchedulerNode){ .type = SCH_NODE_TYPE_FILTER_OUT, \
.idx = filter, .idx_stream = output }
Scheduler *sch_alloc(void);
void sch_free(Scheduler **sch);
int sch_start(Scheduler *sch);
int sch_stop(Scheduler *sch, int64_t *finish_ts);
/**
* Wait until transcoding terminates or the specified timeout elapses.
*
* @param timeout_us Amount of time in microseconds after which this function
* will timeout.
* @param transcode_ts Current transcode timestamp in AV_TIME_BASE_Q, for
* informational purposes only.
*
* @retval 0 waiting timed out, transcoding is not finished
* @retval 1 transcoding is finished
*/
int sch_wait(Scheduler *sch, uint64_t timeout_us, int64_t *transcode_ts);
/**
* Add a demuxer to the scheduler.
*
* @param func Function executed as the demuxer task.
* @param ctx Demuxer state; will be passed to func and used for logging.
*
* @retval ">=0" Index of the newly-created demuxer.
* @retval "<0" Error code.
*/
int sch_add_demux(Scheduler *sch, SchThreadFunc func, void *ctx);
/**
* Add a demuxed stream for a previously added demuxer.
*
* @param demux_idx index previously returned by sch_add_demux()
*
* @retval ">=0" Index of the newly-created demuxed stream.
* @retval "<0" Error code.
*/
int sch_add_demux_stream(Scheduler *sch, unsigned demux_idx);
/**
* Add a decoder to the scheduler.
*
* @param func Function executed as the decoder task.
* @param ctx Decoder state; will be passed to func and used for logging.
* @param send_end_ts The decoder will return an end timestamp after flush packets
* are delivered to it. See documentation for
* sch_dec_receive() for more details.
*
* @retval ">=0" Index of the newly-created decoder.
* @retval "<0" Error code.
*/
int sch_add_dec(Scheduler *sch, SchThreadFunc func, void *ctx, int send_end_ts);
/**
* Add another output to decoder (e.g. for multiview video).
*
* @retval ">=0" Index of the newly-added decoder output.
* @retval "<0" Error code.
*/
int sch_add_dec_output(Scheduler *sch, unsigned dec_idx);
/**
* Add a filtergraph to the scheduler.
*
* @param nb_inputs Number of filtergraph inputs.
* @param nb_outputs number of filtergraph outputs
* @param func Function executed as the filtering task.
* @param ctx Filter state; will be passed to func and used for logging.
*
* @retval ">=0" Index of the newly-created filtergraph.
* @retval "<0" Error code.
*/
int sch_add_filtergraph(Scheduler *sch, unsigned nb_inputs, unsigned nb_outputs,
SchThreadFunc func, void *ctx);
/**
* Add a muxer to the scheduler.
*
* Note that muxer thread startup is more complicated than for other components,
* because
* - muxer streams fed by audio/video encoders become initialized dynamically at
* runtime, after those encoders receive their first frame and initialize
* themselves, followed by calling sch_mux_stream_ready()
* - the header can be written after all the streams for a muxer are initialized
* - we may need to write an SDP, which must happen
* - AFTER all the headers are written
* - BEFORE any packets are written by any muxer
* - with all the muxers quiescent
* To avoid complicated muxer-thread synchronization dances, we postpone
* starting the muxer threads until after the SDP is written. The sequence of
* events is then as follows:
* - After sch_mux_stream_ready() is called for all the streams in a given muxer,
* the header for that muxer is written (care is taken that headers for
* different muxers are not written concurrently, since they write file
* information to stderr). If SDP is not wanted, the muxer thread then starts
* and muxing begins.
* - When SDP _is_ wanted, no muxer threads start until the header for the last
* muxer is written. After that, the SDP is written, after which all the muxer
* threads are started at once.
*
* In order for the above to work, the scheduler needs to be able to invoke
* just writing the header, which is the reason the init parameter exists.
*
* @param func Function executed as the muxing task.
* @param init Callback that is called to initialize the muxer and write the
* header. Called after sch_mux_stream_ready() is called for all the
* streams in the muxer.
* @param ctx Muxer state; will be passed to func/init and used for logging.
* @param sdp_auto Determines automatic SDP writing - see sch_sdp_filename().
* @param thread_queue_size number of packets that can be buffered before
* sending to the muxer blocks
*
* @retval ">=0" Index of the newly-created muxer.
* @retval "<0" Error code.
*/
int sch_add_mux(Scheduler *sch, SchThreadFunc func, int (*init)(void *),
void *ctx, int sdp_auto, unsigned thread_queue_size);
/**
* Default size of a packet thread queue. For muxing this can be overridden by
* the thread_queue_size option as passed to a call to sch_add_mux().
*/
#define DEFAULT_PACKET_THREAD_QUEUE_SIZE 8
/**
* Default size of a frame thread queue.
*/
#define DEFAULT_FRAME_THREAD_QUEUE_SIZE 8
/**
* Add a muxed stream for a previously added muxer.
*
* @param mux_idx index previously returned by sch_add_mux()
*
* @retval ">=0" Index of the newly-created muxed stream.
* @retval "<0" Error code.
*/
int sch_add_mux_stream(Scheduler *sch, unsigned mux_idx);
/**
* Configure limits on packet buffering performed before the muxer task is
* started.
*
* @param mux_idx index previously returned by sch_add_mux()
* @param stream_idx_idx index previously returned by sch_add_mux_stream()
* @param data_threshold Total size of the buffered packets' data after which
* max_packets applies.
* @param max_packets maximum Maximum number of buffered packets after
* data_threshold is reached.
*/
void sch_mux_stream_buffering(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
size_t data_threshold, int max_packets);
/**
* Signal to the scheduler that the specified muxed stream is initialized and
* ready. Muxing is started once all the streams are ready.
*/
int sch_mux_stream_ready(Scheduler *sch, unsigned mux_idx, unsigned stream_idx);
/**
* Set the file path for the SDP.
*
* The SDP is written when either of the following is true:
* - this function is called at least once
* - sdp_auto=1 is passed to EVERY call of sch_add_mux()
*/
int sch_sdp_filename(Scheduler *sch, const char *sdp_filename);
/**
* Add an encoder to the scheduler.
*
* @param func Function executed as the encoding task.
* @param ctx Encoder state; will be passed to func and used for logging.
* @param open_cb This callback, if specified, will be called when the first
* frame is obtained for this encoder. For audio encoders with a
* fixed frame size (which use a sync queue in the scheduler to
* rechunk frames), it must return that frame size on success.
* Otherwise (non-audio, variable frame size) it should return 0.
*
* @retval ">=0" Index of the newly-created encoder.
* @retval "<0" Error code.
*/
int sch_add_enc(Scheduler *sch, SchThreadFunc func, void *ctx,
int (*open_cb)(void *func_arg, const struct AVFrame *frame));
/**
* Add an pre-encoding sync queue to the scheduler.
*
* @param buf_size_us Sync queue buffering size, passed to sq_alloc().
* @param logctx Logging context for the sync queue. passed to sq_alloc().
*
* @retval ">=0" Index of the newly-created sync queue.
* @retval "<0" Error code.
*/
int sch_add_sq_enc(Scheduler *sch, uint64_t buf_size_us, void *logctx);
int sch_sq_add_enc(Scheduler *sch, unsigned sq_idx, unsigned enc_idx,
int limiting, uint64_t max_frames);
int sch_connect(Scheduler *sch, SchedulerNode src, SchedulerNode dst);
enum DemuxSendFlags {
/**
* Treat the packet as an EOF for SCH_NODE_TYPE_MUX destinations
* send normally to other types.
*/
DEMUX_SEND_STREAMCOPY_EOF = (1 << 0),
};
/**
* Called by demuxer tasks to communicate with their downstreams. The following
* may be sent:
* - a demuxed packet for the stream identified by pkt->stream_index;
* - demuxer discontinuity/reset (e.g. after a seek) - this is signalled by an
* empty packet with stream_index=-1.
*
* @param demux_idx demuxer index
* @param pkt A demuxed packet to send.
* When flushing (i.e. pkt->stream_index=-1 on entry to this
* function), on successful return pkt->pts/pkt->time_base will be
* set to the maximum end timestamp of any decoded audio stream, or
* AV_NOPTS_VALUE if no decoded audio streams are present.
*
* @retval "non-negative value" success
* @retval AVERROR_EOF all consumers for the stream are done
* @retval AVERROR_EXIT all consumers are done, should terminate demuxing
* @retval "another negative error code" other failure
*/
int sch_demux_send(Scheduler *sch, unsigned demux_idx, struct AVPacket *pkt,
unsigned flags);
/**
* Called by decoder tasks to receive a packet for decoding.
*
* @param dec_idx decoder index
* @param pkt Input packet will be written here on success.
*
* An empty packet signals that the decoder should be flushed, but
* more packets will follow (e.g. after seeking). When a decoder
* created with send_end_ts=1 receives a flush packet, it must write
* the end timestamp of the stream after flushing to
* pkt->pts/time_base on the next call to this function (if any).
*
* @retval "non-negative value" success
* @retval AVERROR_EOF no more packets will arrive, should terminate decoding
* @retval "another negative error code" other failure
*/
int sch_dec_receive(Scheduler *sch, unsigned dec_idx, struct AVPacket *pkt);
/**
* Called by decoder tasks to send a decoded frame downstream.
*
* @param dec_idx Decoder index previously returned by sch_add_dec().
* @param frame Decoded frame; on success it is consumed and cleared by this
* function
*
* @retval ">=0" success
* @retval AVERROR_EOF all consumers are done, should terminate decoding
* @retval "another negative error code" other failure
*/
int sch_dec_send(Scheduler *sch, unsigned dec_idx,
unsigned out_idx, struct AVFrame *frame);
/**
* Called by filtergraph tasks to obtain frames for filtering. Will wait for a
* frame to become available and return it in frame.
*
* Filtergraphs that contain lavfi sources and do not currently require new
* input frames should call this function as a means of rate control - then
* in_idx should be set equal to nb_inputs on entry to this function.
*
* @param fg_idx Filtergraph index previously returned by sch_add_filtergraph().
* @param[in,out] in_idx On input contains the index of the input on which a frame
* is most desired. May be set to nb_inputs to signal that
* the filtergraph does not need more input currently.
*
* On success, will be replaced with the input index of
* the actually returned frame or EOF timestamp.
*
* @retval ">=0" Frame data or EOF timestamp was delivered into frame, in_idx
* contains the index of the input it belongs to.
* @retval AVERROR(EAGAIN) No frame was returned, the filtergraph should
* resume filtering. May only be returned when
* in_idx=nb_inputs on entry to this function.
* @retval AVERROR_EOF No more frames will arrive, should terminate filtering.
*/
int sch_filter_receive(Scheduler *sch, unsigned fg_idx,
unsigned *in_idx, struct AVFrame *frame);
/**
* Called by filter tasks to signal that a filter input will no longer accept input.
*
* @param fg_idx Filtergraph index previously returned from sch_add_filtergraph().
* @param in_idx Index of the input to finish.
*/
void sch_filter_receive_finish(Scheduler *sch, unsigned fg_idx, unsigned in_idx);
/**
* Called by filtergraph tasks to send a filtered frame or EOF to consumers.
*
* @param fg_idx Filtergraph index previously returned by sch_add_filtergraph().
* @param out_idx Index of the output which produced the frame.
* @param frame The frame to send to consumers. When NULL, signals that no more
* frames will be produced for the specified output. When non-NULL,
* the frame is consumed and cleared by this function on success.
*
* @retval "non-negative value" success
* @retval AVERROR_EOF all consumers are done
* @retval "another negative error code" other failure
*/
int sch_filter_send(Scheduler *sch, unsigned fg_idx, unsigned out_idx,
struct AVFrame *frame);
int sch_filter_command(Scheduler *sch, unsigned fg_idx, struct AVFrame *frame);
/**
* Called by encoder tasks to obtain frames for encoding. Will wait for a frame
* to become available and return it in frame.
*
* @param enc_idx Encoder index previously returned by sch_add_enc().
* @param frame Newly-received frame will be stored here on success. Must be
* clean on entrance to this function.
*
* @retval 0 A frame was successfully delivered into frame.
* @retval AVERROR_EOF No more frames will be delivered, the encoder should
* flush everything and terminate.
*
*/
int sch_enc_receive(Scheduler *sch, unsigned enc_idx, struct AVFrame *frame);
/**
* Called by encoder tasks to send encoded packets downstream.
*
* @param enc_idx Encoder index previously returned by sch_add_enc().
* @param pkt An encoded packet; it will be consumed and cleared by this
* function on success.
*
* @retval 0 success
* @retval "<0" Error code.
*/
int sch_enc_send (Scheduler *sch, unsigned enc_idx, struct AVPacket *pkt);
/**
* Called by muxer tasks to obtain packets for muxing. Will wait for a packet
* for any muxed stream to become available and return it in pkt.
*
* @param mux_idx Muxer index previously returned by sch_add_mux().
* @param pkt Newly-received packet will be stored here on success. Must be
* clean on entrance to this function.
*
* @retval 0 A packet was successfully delivered into pkt. Its stream_index
* corresponds to a stream index previously returned from
* sch_add_mux_stream().
* @retval AVERROR_EOF When pkt->stream_index is non-negative, this signals that
* no more packets will be delivered for this stream index.
* Otherwise this indicates that no more packets will be
* delivered for any stream and the muxer should therefore
* flush everything and terminate.
*/
int sch_mux_receive(Scheduler *sch, unsigned mux_idx, struct AVPacket *pkt);
/**
* Called by muxer tasks to signal that a stream will no longer accept input.
*
* @param stream_idx Stream index previously returned from sch_add_mux_stream().
*/
void sch_mux_receive_finish(Scheduler *sch, unsigned mux_idx, unsigned stream_idx);
int sch_mux_sub_heartbeat_add(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
unsigned dec_idx);
int sch_mux_sub_heartbeat(Scheduler *sch, unsigned mux_idx, unsigned stream_idx,
const AVPacket *pkt);
#endif /* FFTOOLS_FFMPEG_SCHED_H */
+63
View File
@@ -0,0 +1,63 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_FFMPEG_UTILS_H
#define FFTOOLS_FFMPEG_UTILS_H
#include <stdint.h>
#include "libavutil/common.h"
#include "libavutil/frame.h"
#include "libavutil/rational.h"
#include "libavcodec/packet.h"
typedef struct Timestamp {
int64_t ts;
AVRational tb;
} Timestamp;
/**
* Merge two return codes - return one of the error codes if at least one of
* them was negative, 0 otherwise.
*/
static inline int err_merge(int err0, int err1)
{
// prefer "real" errors over EOF
if ((err0 >= 0 || err0 == AVERROR_EOF) && err1 < 0)
return err1;
return (err0 < 0) ? err0 : FFMIN(err1, 0);
}
/**
* Wrapper calling av_frame_side_data_clone() in a loop for all source entries.
* It does not clear dst beforehand. */
static inline int clone_side_data(AVFrameSideData ***dst, int *nb_dst,
AVFrameSideData * const *src, int nb_src,
unsigned int flags)
{
for (int i = 0; i < nb_src; i++) {
int ret = av_frame_side_data_clone(dst, nb_dst, src[i], flags);
if (ret < 0)
return ret;
}
return 0;
}
#endif // FFTOOLS_FFMPEG_UTILS_H
File diff suppressed because it is too large Load Diff
+874
View File
@@ -0,0 +1,874 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define VK_NO_PROTOTYPES
#define VK_ENABLE_BETA_EXTENSIONS
#include "config.h"
#include "ffplay_renderer.h"
#if (SDL_VERSION_ATLEAST(2, 0, 6) && CONFIG_LIBPLACEBO)
/* Get PL_API_VER */
#include <libplacebo/config.h>
#define HAVE_VULKAN_RENDERER (PL_API_VER >= 278)
#else
#define HAVE_VULKAN_RENDERER 0
#endif
#if HAVE_VULKAN_RENDERER
#if defined(_WIN32) && !defined(VK_USE_PLATFORM_WIN32_KHR)
#define VK_USE_PLATFORM_WIN32_KHR
#endif
#include <libplacebo/vulkan.h>
#include <libplacebo/utils/frame_queue.h>
#include <libplacebo/utils/libav.h>
#include <SDL_vulkan.h>
#include "libavutil/bprint.h"
#include "libavutil/mem.h"
#endif
struct VkRenderer {
const AVClass *class;
int (*create)(VkRenderer *renderer, SDL_Window *window, AVDictionary *dict);
int (*get_hw_dev)(VkRenderer *renderer, AVBufferRef **dev);
int (*display)(VkRenderer *renderer, AVFrame *frame);
int (*resize)(VkRenderer *renderer, int width, int height);
void (*destroy)(VkRenderer *renderer);
};
#if HAVE_VULKAN_RENDERER
typedef struct RendererContext {
VkRenderer api;
// Can be NULL when vulkan instance is created by avutil
pl_vk_inst placebo_instance;
pl_vulkan placebo_vulkan;
pl_swapchain swapchain;
VkSurfaceKHR vk_surface;
pl_renderer renderer;
pl_tex tex[4];
pl_log vk_log;
AVBufferRef *hw_device_ref;
AVBufferRef *hw_frame_ref;
enum AVPixelFormat *transfer_formats;
AVHWFramesConstraints *constraints;
PFN_vkGetInstanceProcAddr get_proc_addr;
// This field is a copy from pl_vk_inst->instance or hw_device_ref instance.
VkInstance inst;
AVFrame *vk_frame;
} RendererContext;
static void vk_log_cb(void *log_priv, enum pl_log_level level,
const char *msg)
{
static const int level_map[] = {
AV_LOG_QUIET,
AV_LOG_FATAL,
AV_LOG_ERROR,
AV_LOG_WARNING,
AV_LOG_INFO,
AV_LOG_DEBUG,
AV_LOG_TRACE,
};
if (level > 0 && level < FF_ARRAY_ELEMS(level_map))
av_log(log_priv, level_map[level], "%s\n", msg);
}
// Should keep sync with optional_device_exts inside hwcontext_vulkan.c
static const char *optional_device_exts[] = {
/* Misc or required by other extensions */
VK_KHR_PORTABILITY_SUBSET_EXTENSION_NAME,
VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME,
VK_KHR_SAMPLER_YCBCR_CONVERSION_EXTENSION_NAME,
VK_EXT_DESCRIPTOR_BUFFER_EXTENSION_NAME,
VK_EXT_PHYSICAL_DEVICE_DRM_EXTENSION_NAME,
VK_EXT_SHADER_ATOMIC_FLOAT_EXTENSION_NAME,
VK_KHR_COOPERATIVE_MATRIX_EXTENSION_NAME,
/* Imports/exports */
VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
VK_EXT_EXTERNAL_MEMORY_DMA_BUF_EXTENSION_NAME,
VK_EXT_IMAGE_DRM_FORMAT_MODIFIER_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_FD_EXTENSION_NAME,
VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME,
#ifdef _WIN32
VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME,
VK_KHR_EXTERNAL_SEMAPHORE_WIN32_EXTENSION_NAME,
#endif
/* Video encoding/decoding */
VK_KHR_VIDEO_QUEUE_EXTENSION_NAME,
VK_KHR_VIDEO_DECODE_QUEUE_EXTENSION_NAME,
VK_KHR_VIDEO_DECODE_H264_EXTENSION_NAME,
VK_KHR_VIDEO_DECODE_H265_EXTENSION_NAME,
"VK_MESA_video_decode_av1",
};
static inline int enable_debug(const AVDictionary *opt)
{
AVDictionaryEntry *entry = av_dict_get(opt, "debug", NULL, 0);
int debug = entry && strtol(entry->value, NULL, 10);
return debug;
}
static void hwctx_lock_queue(void *priv, uint32_t qf, uint32_t qidx)
{
AVHWDeviceContext *avhwctx = priv;
const AVVulkanDeviceContext *hwctx = avhwctx->hwctx;
hwctx->lock_queue(avhwctx, qf, qidx);
}
static void hwctx_unlock_queue(void *priv, uint32_t qf, uint32_t qidx)
{
AVHWDeviceContext *avhwctx = priv;
const AVVulkanDeviceContext *hwctx = avhwctx->hwctx;
hwctx->unlock_queue(avhwctx, qf, qidx);
}
static int add_instance_extension(const char **ext, unsigned num_ext,
const AVDictionary *opt,
AVDictionary **dict)
{
const char *inst_ext_key = "instance_extensions";
AVDictionaryEntry *entry;
AVBPrint buf;
char *ext_list = NULL;
int ret;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
for (int i = 0; i < num_ext; i++) {
if (i)
av_bprintf(&buf, "+%s", ext[i]);
else
av_bprintf(&buf, "%s", ext[i]);
}
entry = av_dict_get(opt, inst_ext_key, NULL, 0);
if (entry && entry->value && entry->value[0]) {
if (num_ext)
av_bprintf(&buf, "+");
av_bprintf(&buf, "%s", entry->value);
}
ret = av_bprint_finalize(&buf, &ext_list);
if (ret < 0)
return ret;
return av_dict_set(dict, inst_ext_key, ext_list, AV_DICT_DONT_STRDUP_VAL);
}
static int add_device_extension(const AVDictionary *opt,
AVDictionary **dict)
{
const char *dev_ext_key = "device_extensions";
AVDictionaryEntry *entry;
AVBPrint buf;
char *ext_list = NULL;
int ret;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
av_bprintf(&buf, "%s", VK_KHR_SWAPCHAIN_EXTENSION_NAME);
for (int i = 0; i < pl_vulkan_num_recommended_extensions; i++)
av_bprintf(&buf, "+%s", pl_vulkan_recommended_extensions[i]);
entry = av_dict_get(opt, dev_ext_key, NULL, 0);
if (entry && entry->value && entry->value[0])
av_bprintf(&buf, "+%s", entry->value);
ret = av_bprint_finalize(&buf, &ext_list);
if (ret < 0)
return ret;
return av_dict_set(dict, dev_ext_key, ext_list, AV_DICT_DONT_STRDUP_VAL);
}
static const char *select_device(const AVDictionary *opt)
{
const AVDictionaryEntry *entry;
entry = av_dict_get(opt, "device", NULL, 0);
if (entry)
return entry->value;
return NULL;
}
static int create_vk_by_hwcontext(VkRenderer *renderer,
const char **ext, unsigned num_ext,
const AVDictionary *opt)
{
RendererContext *ctx = (RendererContext *) renderer;
AVHWDeviceContext *dev;
AVVulkanDeviceContext *hwctx;
AVDictionary *dict = NULL;
int ret;
ret = add_instance_extension(ext, num_ext, opt, &dict);
if (ret < 0)
return ret;
ret = add_device_extension(opt, &dict);
if (ret) {
av_dict_free(&dict);
return ret;
}
ret = av_hwdevice_ctx_create(&ctx->hw_device_ref, AV_HWDEVICE_TYPE_VULKAN,
select_device(opt), dict, 0);
av_dict_free(&dict);
if (ret < 0)
return ret;
dev = (AVHWDeviceContext *) ctx->hw_device_ref->data;
hwctx = dev->hwctx;
// There is no way to pass SDL GetInstanceProcAddr to hwdevice.
// Check the result and return error if they don't match.
if (hwctx->get_proc_addr != SDL_Vulkan_GetVkGetInstanceProcAddr()) {
av_log(renderer, AV_LOG_ERROR,
"hwdevice and SDL use different get_proc_addr. "
"Try -vulkan_params create_by_placebo=1\n");
return AVERROR_PATCHWELCOME;
}
ctx->get_proc_addr = hwctx->get_proc_addr;
ctx->inst = hwctx->inst;
struct pl_vulkan_import_params import_params = {
.instance = hwctx->inst,
.get_proc_addr = hwctx->get_proc_addr,
.phys_device = hwctx->phys_dev,
.device = hwctx->act_dev,
.extensions = hwctx->enabled_dev_extensions,
.num_extensions = hwctx->nb_enabled_dev_extensions,
.features = &hwctx->device_features,
.lock_queue = hwctx_lock_queue,
.unlock_queue = hwctx_unlock_queue,
.queue_ctx = dev,
.queue_graphics = {
.index = VK_QUEUE_FAMILY_IGNORED,
.count = 0,
},
.queue_compute = {
.index = VK_QUEUE_FAMILY_IGNORED,
.count = 0,
},
.queue_transfer = {
.index = VK_QUEUE_FAMILY_IGNORED,
.count = 0,
},
};
for (int i = 0; i < hwctx->nb_qf; i++) {
const AVVulkanDeviceQueueFamily *qf = &hwctx->qf[i];
if (qf->flags & VK_QUEUE_GRAPHICS_BIT) {
import_params.queue_graphics.index = qf->idx;
import_params.queue_graphics.count = qf->num;
}
if (qf->flags & VK_QUEUE_COMPUTE_BIT) {
import_params.queue_compute.index = qf->idx;
import_params.queue_compute.count = qf->num;
}
if (qf->flags & VK_QUEUE_TRANSFER_BIT) {
import_params.queue_transfer.index = qf->idx;
import_params.queue_transfer.count = qf->num;
}
}
ctx->placebo_vulkan = pl_vulkan_import(ctx->vk_log, &import_params);
if (!ctx->placebo_vulkan)
return AVERROR_EXTERNAL;
return 0;
}
static void placebo_lock_queue(struct AVHWDeviceContext *dev_ctx,
uint32_t queue_family, uint32_t index)
{
RendererContext *ctx = dev_ctx->user_opaque;
pl_vulkan vk = ctx->placebo_vulkan;
vk->lock_queue(vk, queue_family, index);
}
static void placebo_unlock_queue(struct AVHWDeviceContext *dev_ctx,
uint32_t queue_family,
uint32_t index)
{
RendererContext *ctx = dev_ctx->user_opaque;
pl_vulkan vk = ctx->placebo_vulkan;
vk->unlock_queue(vk, queue_family, index);
}
static int get_decode_queue(VkRenderer *renderer, int *index, int *count)
{
RendererContext *ctx = (RendererContext *) renderer;
VkQueueFamilyProperties *queue_family_prop = NULL;
uint32_t num_queue_family_prop = 0;
PFN_vkGetPhysicalDeviceQueueFamilyProperties get_queue_family_prop;
PFN_vkGetInstanceProcAddr get_proc_addr = ctx->get_proc_addr;
*index = -1;
*count = 0;
get_queue_family_prop = (PFN_vkGetPhysicalDeviceQueueFamilyProperties)
get_proc_addr(ctx->placebo_instance->instance,
"vkGetPhysicalDeviceQueueFamilyProperties");
get_queue_family_prop(ctx->placebo_vulkan->phys_device,
&num_queue_family_prop, NULL);
if (!num_queue_family_prop)
return AVERROR_EXTERNAL;
queue_family_prop = av_calloc(num_queue_family_prop,
sizeof(*queue_family_prop));
if (!queue_family_prop)
return AVERROR(ENOMEM);
get_queue_family_prop(ctx->placebo_vulkan->phys_device,
&num_queue_family_prop,
queue_family_prop);
for (int i = 0; i < num_queue_family_prop; i++) {
if (queue_family_prop[i].queueFlags & VK_QUEUE_VIDEO_DECODE_BIT_KHR) {
*index = i;
*count = queue_family_prop[i].queueCount;
break;
}
}
av_free(queue_family_prop);
return 0;
}
static int create_vk_by_placebo(VkRenderer *renderer,
const char **ext, unsigned num_ext,
const AVDictionary *opt)
{
RendererContext *ctx = (RendererContext *) renderer;
AVHWDeviceContext *device_ctx;
AVVulkanDeviceContext *vk_dev_ctx;
int decode_index;
int decode_count;
int ret;
ctx->get_proc_addr = SDL_Vulkan_GetVkGetInstanceProcAddr();
ctx->placebo_instance = pl_vk_inst_create(ctx->vk_log, pl_vk_inst_params(
.get_proc_addr = ctx->get_proc_addr,
.debug = enable_debug(opt),
.extensions = ext,
.num_extensions = num_ext
));
if (!ctx->placebo_instance) {
return AVERROR_EXTERNAL;
}
ctx->inst = ctx->placebo_instance->instance;
ctx->placebo_vulkan = pl_vulkan_create(ctx->vk_log, pl_vulkan_params(
.instance = ctx->placebo_instance->instance,
.get_proc_addr = ctx->placebo_instance->get_proc_addr,
.surface = ctx->vk_surface,
.allow_software = false,
.opt_extensions = optional_device_exts,
.num_opt_extensions = FF_ARRAY_ELEMS(optional_device_exts),
.extra_queues = VK_QUEUE_VIDEO_DECODE_BIT_KHR,
.device_name = select_device(opt),
));
if (!ctx->placebo_vulkan)
return AVERROR_EXTERNAL;
ctx->hw_device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VULKAN);
if (!ctx->hw_device_ref) {
return AVERROR(ENOMEM);
}
device_ctx = (AVHWDeviceContext *) ctx->hw_device_ref->data;
device_ctx->user_opaque = ctx;
vk_dev_ctx = device_ctx->hwctx;
vk_dev_ctx->lock_queue = placebo_lock_queue;
vk_dev_ctx->unlock_queue = placebo_unlock_queue;
vk_dev_ctx->get_proc_addr = ctx->placebo_instance->get_proc_addr;
vk_dev_ctx->inst = ctx->placebo_instance->instance;
vk_dev_ctx->phys_dev = ctx->placebo_vulkan->phys_device;
vk_dev_ctx->act_dev = ctx->placebo_vulkan->device;
vk_dev_ctx->device_features = *ctx->placebo_vulkan->features;
vk_dev_ctx->enabled_inst_extensions = ctx->placebo_instance->extensions;
vk_dev_ctx->nb_enabled_inst_extensions = ctx->placebo_instance->num_extensions;
vk_dev_ctx->enabled_dev_extensions = ctx->placebo_vulkan->extensions;
vk_dev_ctx->nb_enabled_dev_extensions = ctx->placebo_vulkan->num_extensions;
int nb_qf = 0;
vk_dev_ctx->qf[nb_qf] = (AVVulkanDeviceQueueFamily) {
.idx = ctx->placebo_vulkan->queue_graphics.index,
.num = ctx->placebo_vulkan->queue_graphics.count,
.flags = VK_QUEUE_GRAPHICS_BIT,
};
nb_qf++;
vk_dev_ctx->qf[nb_qf] = (AVVulkanDeviceQueueFamily) {
.idx = ctx->placebo_vulkan->queue_transfer.index,
.num = ctx->placebo_vulkan->queue_transfer.count,
.flags = VK_QUEUE_TRANSFER_BIT,
};
nb_qf++;
vk_dev_ctx->qf[nb_qf] = (AVVulkanDeviceQueueFamily) {
.idx = ctx->placebo_vulkan->queue_compute.index,
.num = ctx->placebo_vulkan->queue_compute.count,
.flags = VK_QUEUE_COMPUTE_BIT,
};
nb_qf++;
ret = get_decode_queue(renderer, &decode_index, &decode_count);
if (ret < 0)
return ret;
vk_dev_ctx->qf[nb_qf] = (AVVulkanDeviceQueueFamily) {
.idx = decode_index,
.num = decode_count,
.flags = VK_QUEUE_VIDEO_DECODE_BIT_KHR,
};
nb_qf++;
vk_dev_ctx->nb_qf = nb_qf;
ret = av_hwdevice_ctx_init(ctx->hw_device_ref);
if (ret < 0)
return ret;
return 0;
}
static int create(VkRenderer *renderer, SDL_Window *window, AVDictionary *opt)
{
int ret = 0;
unsigned num_ext = 0;
const char **ext = NULL;
int w, h;
struct pl_log_params vk_log_params = {
.log_cb = vk_log_cb,
.log_level = PL_LOG_DEBUG,
.log_priv = renderer,
};
RendererContext *ctx = (RendererContext *) renderer;
AVDictionaryEntry *entry;
ctx->vk_log = pl_log_create(PL_API_VER, &vk_log_params);
if (!SDL_Vulkan_GetInstanceExtensions(window, &num_ext, NULL)) {
av_log(NULL, AV_LOG_FATAL, "Failed to get vulkan extensions: %s\n",
SDL_GetError());
return AVERROR_EXTERNAL;
}
ext = av_calloc(num_ext, sizeof(*ext));
if (!ext) {
ret = AVERROR(ENOMEM);
goto out;
}
SDL_Vulkan_GetInstanceExtensions(window, &num_ext, ext);
entry = av_dict_get(opt, "create_by_placebo", NULL, 0);
if (entry && strtol(entry->value, NULL, 10))
ret = create_vk_by_placebo(renderer, ext, num_ext, opt);
else
ret = create_vk_by_hwcontext(renderer, ext, num_ext, opt);
if (ret < 0)
goto out;
if (!SDL_Vulkan_CreateSurface(window, ctx->inst, &ctx->vk_surface)) {
ret = AVERROR_EXTERNAL;
goto out;
}
ctx->swapchain = pl_vulkan_create_swapchain(
ctx->placebo_vulkan,
pl_vulkan_swapchain_params(
.surface = ctx->vk_surface,
.present_mode = VK_PRESENT_MODE_FIFO_KHR));
if (!ctx->swapchain) {
ret = AVERROR_EXTERNAL;
goto out;
}
SDL_Vulkan_GetDrawableSize(window, &w, &h);
pl_swapchain_resize(ctx->swapchain, &w, &h);
ctx->renderer = pl_renderer_create(ctx->vk_log, ctx->placebo_vulkan->gpu);
if (!ctx->renderer) {
ret = AVERROR_EXTERNAL;
goto out;
}
ctx->vk_frame = av_frame_alloc();
if (!ctx->vk_frame) {
ret = AVERROR(ENOMEM);
goto out;
}
ret = 0;
out:
av_free(ext);
return ret;
}
static int get_hw_dev(VkRenderer *renderer, AVBufferRef **dev)
{
RendererContext *ctx = (RendererContext *) renderer;
*dev = ctx->hw_device_ref;
return 0;
}
static int create_hw_frame(VkRenderer *renderer, AVFrame *frame)
{
RendererContext *ctx = (RendererContext *) renderer;
AVHWFramesContext *src_hw_frame = (AVHWFramesContext *)
frame->hw_frames_ctx->data;
AVHWFramesContext *hw_frame;
AVVulkanFramesContext *vk_frame_ctx;
int ret;
if (ctx->hw_frame_ref) {
hw_frame = (AVHWFramesContext *) ctx->hw_frame_ref->data;
if (hw_frame->width == frame->width &&
hw_frame->height == frame->height &&
hw_frame->sw_format == src_hw_frame->sw_format)
return 0;
av_buffer_unref(&ctx->hw_frame_ref);
}
if (!ctx->constraints) {
ctx->constraints = av_hwdevice_get_hwframe_constraints(
ctx->hw_device_ref, NULL);
if (!ctx->constraints)
return AVERROR(ENOMEM);
}
// Check constraints and skip create hwframe. Don't take it as error since
// we can fallback to memory copy from GPU to CPU.
if ((ctx->constraints->max_width &&
ctx->constraints->max_width < frame->width) ||
(ctx->constraints->max_height &&
ctx->constraints->max_height < frame->height) ||
(ctx->constraints->min_width &&
ctx->constraints->min_width > frame->width) ||
(ctx->constraints->min_height &&
ctx->constraints->min_height > frame->height))
return 0;
if (ctx->constraints->valid_sw_formats) {
enum AVPixelFormat *sw_formats = ctx->constraints->valid_sw_formats;
while (*sw_formats != AV_PIX_FMT_NONE) {
if (*sw_formats == src_hw_frame->sw_format)
break;
sw_formats++;
}
if (*sw_formats == AV_PIX_FMT_NONE)
return 0;
}
ctx->hw_frame_ref = av_hwframe_ctx_alloc(ctx->hw_device_ref);
if (!ctx->hw_frame_ref)
return AVERROR(ENOMEM);
hw_frame = (AVHWFramesContext *) ctx->hw_frame_ref->data;
hw_frame->format = AV_PIX_FMT_VULKAN;
hw_frame->sw_format = src_hw_frame->sw_format;
hw_frame->width = frame->width;
hw_frame->height = frame->height;
if (frame->format == AV_PIX_FMT_CUDA) {
vk_frame_ctx = hw_frame->hwctx;
vk_frame_ctx->flags = AV_VK_FRAME_FLAG_DISABLE_MULTIPLANE;
}
ret = av_hwframe_ctx_init(ctx->hw_frame_ref);
if (ret < 0) {
av_log(renderer, AV_LOG_ERROR, "Create hwframe context failed, %s\n",
av_err2str(ret));
return ret;
}
av_hwframe_transfer_get_formats(ctx->hw_frame_ref,
AV_HWFRAME_TRANSFER_DIRECTION_TO,
&ctx->transfer_formats, 0);
return 0;
}
static inline int check_hw_transfer(RendererContext *ctx, AVFrame *frame)
{
if (!ctx->hw_frame_ref || !ctx->transfer_formats)
return 0;
for (int i = 0; ctx->transfer_formats[i] != AV_PIX_FMT_NONE; i++)
if (ctx->transfer_formats[i] == frame->format)
return 1;
return 0;
}
static inline int move_to_output_frame(RendererContext *ctx, AVFrame *frame)
{
int ret = av_frame_copy_props(ctx->vk_frame, frame);
if (ret < 0)
return ret;
av_frame_unref(frame);
av_frame_move_ref(frame, ctx->vk_frame);
return 0;
}
static int map_frame(VkRenderer *renderer, AVFrame *frame, int use_hw_frame)
{
RendererContext *ctx = (RendererContext *) renderer;
int ret;
if (use_hw_frame && !ctx->hw_frame_ref)
return AVERROR(ENOSYS);
// Try map data first
av_frame_unref(ctx->vk_frame);
if (use_hw_frame) {
ctx->vk_frame->hw_frames_ctx = av_buffer_ref(ctx->hw_frame_ref);
ctx->vk_frame->format = AV_PIX_FMT_VULKAN;
}
ret = av_hwframe_map(ctx->vk_frame, frame, 0);
if (!ret)
return move_to_output_frame(ctx, frame);
if (ret != AVERROR(ENOSYS))
av_log(NULL, AV_LOG_FATAL, "Map frame failed: %s\n", av_err2str(ret));
return ret;
}
static int transfer_frame(VkRenderer *renderer, AVFrame *frame, int use_hw_frame)
{
RendererContext *ctx = (RendererContext *) renderer;
int ret;
if (use_hw_frame && !check_hw_transfer(ctx, frame))
return AVERROR(ENOSYS);
av_frame_unref(ctx->vk_frame);
if (use_hw_frame)
av_hwframe_get_buffer(ctx->hw_frame_ref, ctx->vk_frame, 0);
ret = av_hwframe_transfer_data(ctx->vk_frame, frame, 1);
if (!ret)
return move_to_output_frame(ctx, frame);
if (ret != AVERROR(ENOSYS))
av_log(NULL, AV_LOG_FATAL, "Transfer frame failed: %s\n",
av_err2str(ret));
return ret;
}
static int convert_frame(VkRenderer *renderer, AVFrame *frame)
{
int ret;
if (!frame->hw_frames_ctx)
return 0;
if (frame->format == AV_PIX_FMT_VULKAN)
return 0;
ret = create_hw_frame(renderer, frame);
if (ret < 0)
return ret;
for (int use_hw = 1; use_hw >=0; use_hw--) {
ret = map_frame(renderer, frame, use_hw);
if (!ret)
return 0;
if (ret != AVERROR(ENOSYS))
return ret;
ret = transfer_frame(renderer, frame, use_hw);
if (!ret)
return 0;
if (ret != AVERROR(ENOSYS))
return ret;
}
return ret;
}
static int display(VkRenderer *renderer, AVFrame *frame)
{
struct pl_swapchain_frame swap_frame = {0};
struct pl_frame pl_frame = {0};
struct pl_frame target = {0};
RendererContext *ctx = (RendererContext *) renderer;
int ret = 0;
struct pl_color_space hint = {0};
ret = convert_frame(renderer, frame);
if (ret < 0)
return ret;
if (!pl_map_avframe_ex(ctx->placebo_vulkan->gpu, &pl_frame, pl_avframe_params(
.frame = frame,
.tex = ctx->tex))) {
av_log(NULL, AV_LOG_ERROR, "pl_map_avframe_ex failed\n");
return AVERROR_EXTERNAL;
}
pl_color_space_from_avframe(&hint, frame);
pl_swapchain_colorspace_hint(ctx->swapchain, &hint);
if (!pl_swapchain_start_frame(ctx->swapchain, &swap_frame)) {
av_log(NULL, AV_LOG_ERROR, "start frame failed\n");
ret = AVERROR_EXTERNAL;
goto out;
}
pl_frame_from_swapchain(&target, &swap_frame);
if (!pl_render_image(ctx->renderer, &pl_frame, &target,
&pl_render_default_params)) {
av_log(NULL, AV_LOG_ERROR, "pl_render_image failed\n");
ret = AVERROR_EXTERNAL;
goto out;
}
if (!pl_swapchain_submit_frame(ctx->swapchain)) {
av_log(NULL, AV_LOG_ERROR, "pl_swapchain_submit_frame failed\n");
ret = AVERROR_EXTERNAL;
goto out;
}
pl_swapchain_swap_buffers(ctx->swapchain);
out:
pl_unmap_avframe(ctx->placebo_vulkan->gpu, &pl_frame);
return ret;
}
static int resize(VkRenderer *renderer, int width, int height)
{
RendererContext *ctx = (RendererContext *) renderer;
if (!pl_swapchain_resize(ctx->swapchain, &width, &height))
return AVERROR_EXTERNAL;
return 0;
}
static void destroy(VkRenderer *renderer)
{
RendererContext *ctx = (RendererContext *) renderer;
PFN_vkDestroySurfaceKHR vkDestroySurfaceKHR;
av_frame_free(&ctx->vk_frame);
av_freep(&ctx->transfer_formats);
av_hwframe_constraints_free(&ctx->constraints);
av_buffer_unref(&ctx->hw_frame_ref);
if (ctx->placebo_vulkan) {
for (int i = 0; i < FF_ARRAY_ELEMS(ctx->tex); i++)
pl_tex_destroy(ctx->placebo_vulkan->gpu, &ctx->tex[i]);
pl_renderer_destroy(&ctx->renderer);
pl_swapchain_destroy(&ctx->swapchain);
pl_vulkan_destroy(&ctx->placebo_vulkan);
}
if (ctx->vk_surface) {
vkDestroySurfaceKHR = (PFN_vkDestroySurfaceKHR)
ctx->get_proc_addr(ctx->inst, "vkDestroySurfaceKHR");
vkDestroySurfaceKHR(ctx->inst, ctx->vk_surface, NULL);
ctx->vk_surface = VK_NULL_HANDLE;
}
av_buffer_unref(&ctx->hw_device_ref);
pl_vk_inst_destroy(&ctx->placebo_instance);
pl_log_destroy(&ctx->vk_log);
}
static const AVClass vulkan_renderer_class = {
.class_name = "Vulkan Renderer",
.item_name = av_default_item_name,
.version = LIBAVUTIL_VERSION_INT,
};
VkRenderer *vk_get_renderer(void)
{
RendererContext *ctx = av_mallocz(sizeof(*ctx));
VkRenderer *renderer;
if (!ctx)
return NULL;
renderer = &ctx->api;
renderer->class = &vulkan_renderer_class;
renderer->get_hw_dev = get_hw_dev;
renderer->create = create;
renderer->display = display;
renderer->resize = resize;
renderer->destroy = destroy;
return renderer;
}
#else
VkRenderer *vk_get_renderer(void)
{
return NULL;
}
#endif
int vk_renderer_create(VkRenderer *renderer, SDL_Window *window,
AVDictionary *opt)
{
return renderer->create(renderer, window, opt);
}
int vk_renderer_get_hw_dev(VkRenderer *renderer, AVBufferRef **dev)
{
return renderer->get_hw_dev(renderer, dev);
}
int vk_renderer_display(VkRenderer *renderer, AVFrame *frame)
{
return renderer->display(renderer, frame);
}
int vk_renderer_resize(VkRenderer *renderer, int width, int height)
{
return renderer->resize(renderer, width, height);
}
void vk_renderer_destroy(VkRenderer *renderer)
{
renderer->destroy(renderer);
}
+41
View File
@@ -0,0 +1,41 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_FFPLAY_RENDERER_H
#define FFTOOLS_FFPLAY_RENDERER_H
#include <SDL.h>
#include "libavutil/frame.h"
typedef struct VkRenderer VkRenderer;
VkRenderer *vk_get_renderer(void);
int vk_renderer_create(VkRenderer *renderer, SDL_Window *window,
AVDictionary *opt);
int vk_renderer_get_hw_dev(VkRenderer *renderer, AVBufferRef **dev);
int vk_renderer_display(VkRenderer *renderer, AVFrame *frame);
int vk_renderer_resize(VkRenderer *renderer, int width, int height);
void vk_renderer_destroy(VkRenderer *renderer);
#endif /* FFTOOLS_FFPLAY_RENDERER_H */
File diff suppressed because it is too large Load Diff
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<asmv3:application>
<asmv3:windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2</dpiAwareness>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
+2
View File
@@ -0,0 +1,2 @@
#include <windows.h>
1 RT_MANIFEST fftools.manifest
+72
View File
@@ -0,0 +1,72 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_FOPEN_UTF8_H
#define FFTOOLS_FOPEN_UTF8_H
#include <stdio.h>
/* The fopen_utf8 function here is essentially equivalent to avpriv_fopen_utf8,
* except that it doesn't set O_CLOEXEC, and that it isn't exported
* from a different library. (On Windows, each DLL might use a different
* CRT, and FILE* handles can't be shared across them.) */
#ifdef _WIN32
#include "libavutil/mem.h"
#include "libavutil/wchar_filename.h"
static inline FILE *fopen_utf8(const char *path_utf8, const char *mode)
{
wchar_t *path_w, *mode_w;
FILE *f;
/* convert UTF-8 to wide chars */
if (get_extended_win32_path(path_utf8, &path_w)) /* This sets errno on error. */
return NULL;
if (!path_w)
goto fallback;
if (utf8towchar(mode, &mode_w))
return NULL;
if (!mode_w) {
/* If failing to interpret the mode string as utf8, it is an invalid
* parameter. */
av_freep(&path_w);
errno = EINVAL;
return NULL;
}
f = _wfopen(path_w, mode_w);
av_freep(&path_w);
av_freep(&mode_w);
return f;
fallback:
/* path may be in CP_ACP */
return fopen(path_utf8, mode);
}
#else
static inline FILE *fopen_utf8(const char *path, const char *mode)
{
return fopen(path, mode);
}
#endif
#endif /* FFTOOLS_FOPEN_UTF8_H */
File diff suppressed because it is too large Load Diff
+30
View File
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2018-2025 - softworkz
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_GRAPH_GRAPHPRINT_H
#define FFTOOLS_GRAPH_GRAPHPRINT_H
#include "fftools/ffmpeg.h"
int print_filtergraphs(FilterGraph **graphs, int nb_graphs, InputFile **ifiles, int nb_ifiles, OutputFile **ofiles, int nb_ofiles);
int print_filtergraph(FilterGraph *fg, AVFilterGraph *graph);
#endif /* FFTOOLS_GRAPH_GRAPHPRINT_H */
File diff suppressed because it is too large Load Diff
+231
View File
@@ -0,0 +1,231 @@
/*
* Option handlers shared between the tools.
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_OPT_COMMON_H
#define FFTOOLS_OPT_COMMON_H
#include "config.h"
#include "cmdutils.h"
#if CONFIG_AVDEVICE
/**
* Print a listing containing autodetected sinks of the output device.
* Device name with options may be passed as an argument to limit results.
*/
int show_sinks(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing autodetected sources of the input device.
* Device name with options may be passed as an argument to limit results.
*/
int show_sources(void *optctx, const char *opt, const char *arg);
#endif
#if CONFIG_AVDEVICE
#define CMDUTILS_COMMON_OPTIONS_AVDEVICE \
{ "sources" , OPT_TYPE_FUNC, OPT_EXIT | OPT_FUNC_ARG | OPT_EXPERT, { .func_arg = show_sources }, \
"list sources of the input device", "device" }, \
{ "sinks" , OPT_TYPE_FUNC, OPT_EXIT | OPT_FUNC_ARG | OPT_EXPERT, { .func_arg = show_sinks }, \
"list sinks of the output device", "device" }, \
#else
#define CMDUTILS_COMMON_OPTIONS_AVDEVICE
#endif
/**
* Print the license of the program to stdout. The license depends on
* the license of the libraries compiled into the program.
* This option processing function does not utilize the arguments.
*/
int show_license(void *optctx, const char *opt, const char *arg);
/**
* Generic -h handler common to all fftools.
*/
int show_help(void *optctx, const char *opt, const char *arg);
/**
* Print the version of the program to stdout. The version message
* depends on the current versions of the repository and of the libav*
* libraries.
* This option processing function does not utilize the arguments.
*/
int show_version(void *optctx, const char *opt, const char *arg);
/**
* Print the build configuration of the program to stdout. The contents
* depend on the definition of FFMPEG_CONFIGURATION.
* This option processing function does not utilize the arguments.
*/
int show_buildconf(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the formats supported by the
* program (including devices).
* This option processing function does not utilize the arguments.
*/
int show_formats(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the muxers supported by the
* program (including devices).
* This option processing function does not utilize the arguments.
*/
int show_muxers(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the demuxer supported by the
* program (including devices).
* This option processing function does not utilize the arguments.
*/
int show_demuxers(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the devices supported by the
* program.
* This option processing function does not utilize the arguments.
*/
int show_devices(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the codecs supported by the
* program.
* This option processing function does not utilize the arguments.
*/
int show_codecs(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the decoders supported by the
* program.
*/
int show_decoders(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the encoders supported by the
* program.
*/
int show_encoders(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the bit stream filters supported by the
* program.
* This option processing function does not utilize the arguments.
*/
int show_bsfs(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the protocols supported by the
* program.
* This option processing function does not utilize the arguments.
*/
int show_protocols(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the filters supported by the
* program.
* This option processing function does not utilize the arguments.
*/
int show_filters(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the pixel formats supported by the
* program.
* This option processing function does not utilize the arguments.
*/
int show_pix_fmts(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the standard channel layouts supported by
* the program.
* This option processing function does not utilize the arguments.
*/
int show_layouts(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the sample formats supported by the
* program.
*/
int show_sample_fmts(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all supported stream dispositions.
*/
int show_dispositions(void *optctx, const char *opt, const char *arg);
/**
* Print a listing containing all the color names and values recognized
* by the program.
*/
int show_colors(void *optctx, const char *opt, const char *arg);
/**
* Set the libav* libraries log level.
*/
int opt_loglevel(void *optctx, const char *opt, const char *arg);
int opt_report(void *optctx, const char *opt, const char *arg);
int init_report(const char *env, FILE **file);
int opt_max_alloc(void *optctx, const char *opt, const char *arg);
/**
* Override the cpuflags.
*/
int opt_cpuflags(void *optctx, const char *opt, const char *arg);
/**
* Override the cpucount.
*/
int opt_cpucount(void *optctx, const char *opt, const char *arg);
#define CMDUTILS_COMMON_OPTIONS \
{ "L", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_license }, "show license" }, \
{ "h", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_help }, "show help", "topic" }, \
{ "?", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_help }, "show help", "topic" }, \
{ "help", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_help }, "show help", "topic" }, \
{ "-help", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_help }, "show help", "topic" }, \
{ "version", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_version }, "show version" }, \
{ "buildconf", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_buildconf }, "show build configuration" }, \
{ "formats", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_formats }, "show available formats" }, \
{ "muxers", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_muxers }, "show available muxers" }, \
{ "demuxers", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_demuxers }, "show available demuxers" }, \
{ "devices", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_devices }, "show available devices" }, \
{ "codecs", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_codecs }, "show available codecs" }, \
{ "decoders", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_decoders }, "show available decoders" }, \
{ "encoders", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_encoders }, "show available encoders" }, \
{ "bsfs", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_bsfs }, "show available bit stream filters" }, \
{ "protocols", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_protocols }, "show available protocols" }, \
{ "filters", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_filters }, "show available filters" }, \
{ "pix_fmts", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_pix_fmts }, "show available pixel formats" }, \
{ "layouts", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_layouts }, "show standard channel layouts" }, \
{ "sample_fmts", OPT_TYPE_FUNC, OPT_EXIT, { .func_arg = show_sample_fmts }, "show available audio sample formats" }, \
{ "dispositions", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_dispositions}, "show available stream dispositions" }, \
{ "colors", OPT_TYPE_FUNC, OPT_EXIT | OPT_EXPERT, { .func_arg = show_colors }, "show available color names" }, \
{ "loglevel", OPT_TYPE_FUNC, OPT_FUNC_ARG | OPT_EXPERT, { .func_arg = opt_loglevel }, "set logging level", "loglevel" }, \
{ "v", OPT_TYPE_FUNC, OPT_FUNC_ARG, { .func_arg = opt_loglevel }, "set logging level", "loglevel" }, \
{ "report", OPT_TYPE_FUNC, OPT_EXPERT, { .func_arg = opt_report }, "generate a report" }, \
{ "max_alloc", OPT_TYPE_FUNC, OPT_FUNC_ARG | OPT_EXPERT, { .func_arg = opt_max_alloc }, "set maximum size of a single allocated block", "bytes" }, \
{ "cpuflags", OPT_TYPE_FUNC, OPT_FUNC_ARG | OPT_EXPERT, { .func_arg = opt_cpuflags }, "force specific cpu flags", "flags" }, \
{ "cpucount", OPT_TYPE_FUNC, OPT_FUNC_ARG | OPT_EXPERT, { .func_arg = opt_cpucount }, "force specific cpu count", "count" }, \
{ "hide_banner", OPT_TYPE_BOOL, OPT_EXPERT, {&hide_banner}, "do not show program banner", "hide_banner" }, \
CMDUTILS_COMMON_OPTIONS_AVDEVICE \
#endif /* FFTOOLS_OPT_COMMON_H */
+13
View File
@@ -0,0 +1,13 @@
clean::
$(RM) $(CLEANSUFFIXES:%=fftools/resources/%)
vpath %.html $(SRC_PATH)
vpath %.css $(SRC_PATH)
# Uncomment to prevent deletion during build
#.PRECIOUS: %.css.c %.css.min %.css.gz %.css.min.gz %.html.gz %.html.c
OBJS-resman += \
fftools/resources/resman.o \
fftools/resources/graph.html.o \
fftools/resources/graph.css.o \
+353
View File
@@ -0,0 +1,353 @@
/* Variables */
.root {
--ff-colvideo: #6eaa7b;
--ff-colaudio: #477fb3;
--ff-colsubtitle: #ad76ab;
--ff-coltext: #666;
}
/* Common & Misc */
.ff-inputfiles rect, .ff-outputfiles rect, .ff-inputstreams rect, .ff-outputstreams rect, .ff-decoders rect, .ff-encoders rect {
stroke-width: 0;
stroke: transparent;
filter: none !important;
fill: transparent !important;
display: none !important;
}
.cluster span {
color: var(--ff-coltext);
}
.cluster rect {
stroke: #dfdfdf !important;
transform: translateY(-2.3rem);
filter: drop-shadow(1px 2px 2px rgba(185,185,185,0.2)) !important;
rx: 8;
ry: 8;
}
.cluster-label {
font-size: 1.1rem;
}
.cluster-label .nodeLabel {
display: block;
font-weight: 500;
color: var(--ff-coltext);
}
.cluster-label div {
max-width: unset !important;
padding: 3px;
}
.cluster-label foreignObject {
transform: translateY(-0.7rem);
}
/* Input and output files */
.node.ff-inputfile .label foreignObject, .node.ff-outputfile .label foreignObject {
overflow: visible;
}
.cluster.ff-inputfile .cluster-label foreignObject div:not(foreignObject div div), .cluster.ff-outputfile .cluster-label foreignObject div:not(foreignObject div div) {
display: table !important;
}
.nodeLabel div.ff-inputfile, .nodeLabel div.ff-outputfile {
font-size: 1.1rem;
font-weight: 500;
min-width: 14rem;
width: 100%;
display: flex;
color: var(--ff-coltext);
margin-top: 0.1rem;
line-height: 1.35;
padding-bottom: 1.9rem;
}
.nodeLabel div.ff-outputfile {
flex-direction: row-reverse;
}
.ff-inputfile .index, .ff-outputfile .index {
order: 2;
color: var(--ff-coltext);
text-align: center;
border-radius: 0.45rem;
border: 0.18em solid #666666db;
font-weight: 600;
padding: 0 0.3em;
opacity: 0.8;
}
.ff-inputfile .index::before {
content: 'In ';
}
.ff-outputfile .index::before {
content: 'Out ';
}
.ff-inputfile .demuxer_name, .ff-outputfile .muxer_name {
flex: 1;
order: 1;
font-size: 0.9rem;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
text-align: center;
max-width: 8rem;
align-content: center;
margin: 0.2rem 0.4rem 0 0.4rem;
}
.ff-inputfile .file_extension, .ff-outputfile .file_extension {
order: 0;
background-color: #888;
color: white;
text-align: center;
border-radius: 0.45rem;
font-weight: 600;
padding: 0 0.4em;
align-content: center;
opacity: 0.8;
}
.ff-inputfile .url, .ff-outputfile .url {
order: 4;
text-align: center;
position: absolute;
left: 0;
right: 0;
bottom: 0.75rem;
font-size: 0.7rem;
font-weight: 400;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin: 0 0.3rem;
direction: rtl;
color: #999;
}
.cluster.ff-inputfile rect, .cluster.ff-outputfile rect {
transform: translateY(-1.8rem);
fill: url(#ff-radgradient);
}
/* Input and output streams */
.node.ff-inputstream rect, .node.ff-outputstream rect {
padding: 0 !important;
margin: 0 !important;
border: none !important;
fill: white;
stroke: #e5e5e5 !important;
height: 2.7rem;
transform: translateY(0.2rem);
filter: none;
rx: 3;
ry: 3;
}
.node.ff-inputstream .label foreignObject, .node.ff-outputstream .label foreignObject {
transform: translateY(-0.2%);
overflow: visible;
}
.node.ff-inputstream .label foreignObject div:not(foreignObject div div), .node.ff-outputstream .label foreignObject div:not(foreignObject div div) {
display: block !important;
line-height: 1.5 !important;
}
.nodeLabel div.ff-inputstream, .nodeLabel div.ff-outputstream {
font-size: 1.0rem;
font-weight: 500;
min-width: 12rem;
width: 100%;
display: flex;
}
.nodeLabel div.ff-outputstream {
flex-direction: row-reverse;
}
.ff-inputstream .name, .ff-outputstream .name {
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
text-align: left;
align-content: center;
margin-bottom: -0.15rem;
}
.ff-inputstream .index, .ff-outputstream .index {
flex: 0 0 1.4rem;
background-color: #888;
color: white;
text-align: center;
border-radius: 0.3rem;
font-weight: 600;
margin-right: -0.3rem;
margin-left: 0.4rem;
opacity: 0.8;
}
.ff-outputstream .index {
margin-right: 0.6rem;
margin-left: -0.4rem;
}
.ff-inputstream::before, .ff-outputstream::before {
font-variant-emoji: text;
flex: 0 0 2rem;
margin-left: -0.8rem;
margin-right: 0.2rem;
}
.ff-outputstream::before {
margin-left: 0.2rem;
margin-right: -0.6rem;
}
.ff-inputstream.video::before, .ff-outputstream.video::before {
content: '\239A';
color: var(--ff-colvideo);
font-size: 2.25rem;
line-height: 0.5;
font-weight: bold;
}
.ff-inputstream.audio::before, .ff-outputstream.audio::before {
content: '\1F39D';
color: var(--ff-colaudio);
font-size: 1.75rem;
line-height: 0.9;
}
.ff-inputstream.subtitle::before, .ff-outputstream.subtitle::before {
content: '\1AC';
color: var(--ff-colsubtitle);
font-size: 1.2rem;
line-height: 1.1;
transform: scaleX(1.5);
margin-top: 0.050rem;
}
.ff-inputstream.attachment::before, .ff-outputstream.attachment::before {
content: '\1F4CE';
font-size: 1.3rem;
line-height: 1.15;
}
.ff-inputstream.data::before, .ff-outputstream.data::before {
content: '\27E8\2219\2219\2219\27E9';
font-size: 1.15rem;
line-height: 1.17;
letter-spacing: -0.3px;
}
/* Filter Graphs */
.cluster.ff-filters rect {
stroke-dasharray: 6 !important;
stroke-width: 1.3px;
stroke: #d1d1d1 !important;
filter: none !important;
}
.cluster.ff-filters div.ff-filters .id {
display: none;
}
.cluster.ff-filters div.ff-filters .name {
margin-right: 0.5rem;
font-size: 0.9rem;
}
.cluster.ff-filters div.ff-filters .description {
font-weight: 400;
font-size: 0.75rem;
vertical-align: middle;
color: #777;
font-family: Cascadia Code, Lucida Console, monospace;
}
/* Filter Shapes */
.node.ff-filter rect {
rx: 10;
ry: 10;
stroke-width: 1px;
stroke: #d3d3d3;
fill: url(#ff-filtergradient);
filter: drop-shadow(1px 1px 2px rgba(0, 0, 0, 0.1));
}
.node.ff-filter .label foreignObject {
transform: translateY(-0.4rem);
overflow: visible;
}
.nodeLabel div.ff-filter {
font-size: 1.0rem;
font-weight: 500;
text-transform: uppercase;
min-width: 5.5rem;
margin-bottom: 0.5rem;
}
.nodeLabel div.ff-filter span {
color: inherit;
}
/* Decoders & Encoders */
.node.ff-decoder rect, .node.ff-encoder rect {
stroke-width: 1px;
stroke: #d3d3d3;
fill: url(#ff-filtergradient);
filter: drop-shadow(1px 1px 2px rgba(0, 0, 0, 0.1));
}
.nodeLabel div.ff-decoder, .nodeLabel div.ff-encoder {
font-size: 0.85rem;
font-weight: 500;
min-width: 3.5rem;
}
/* Links and Arrows */
path.flowchart-link[id|='video'] {
stroke: var(--ff-colvideo);
}
path.flowchart-link[id|='audio'] {
stroke: var(--ff-colaudio);
}
path.flowchart-link[id|='subtitle'] {
stroke: var(--ff-colsubtitle);
}
marker.marker path {
fill: context-stroke;
}
.edgeLabel foreignObject {
transform: translateY(-1rem);
}
.edgeLabel p {
background: transparent;
white-space: nowrap;
margin: 1rem 0.5rem !important;
font-weight: 500;
color: var(--ff-coltext);
}
.edgeLabel, .labelBkg {
background: transparent;
}
.edgeLabels .edgeLabel * {
font-size: 0.8rem;
}
+86
View File
@@ -0,0 +1,86 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>FFmpeg Graph</title>
</head>
<body>
<style>
html {
color: #666;
font-family: Roboto;
height: 100%;
}
body {
background-color: #f9f9f9;
box-sizing: border-box;
display: flex;
flex-direction: column;
height: 100%;
margin: 0;
padding: 1.7rem 1.7rem 3.5rem 1.7rem;
}
div#banner {
align-items: center;
display: flex;
flex-direction: row;
margin-bottom: 1.5rem;
margin-left: 0.6vw;
}
div#header {
aspect-ratio: 1/1;
background-image: url(https://trac.ffmpeg.org/ffmpeg-logo.png);
background-size: cover;
width: 1.6rem;
}
h1 {
font-size: 1.2rem;
margin: 0 0.5rem;
}
pre.mermaid {
align-items: center;
background-color: white;
box-shadow: 2px 2px 25px 0px #00000010;
color: transparent;
display: flex;
flex: 1;
justify-content: center;
margin: 0;
overflow: overlay;
}
pre.mermaid svg {
height: auto;
margin: 0;
max-width: unset !important;
width: auto;
}
pre.mermaid svg * {
user-select: none;
}
</style>
<div id="banner">
<div id="header"></div>
<h1>FFmpeg Execution Graph</h1>
</div>
<pre class="mermaid">
__###__
</pre>
<script type="module">
import vanillaJsWheelZoom from 'https://cdn.jsdelivr.net/npm/vanilla-js-wheel-zoom@9.0.4/+esm';
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
function initViewer() {
var element = document.querySelector('.mermaid svg')
vanillaJsWheelZoom.create('pre.mermaid svg', { type: 'html', smoothTimeDrag: 0, width: element.clientWidth, height: element.clientHeight, maxScale: 3 });
}
mermaid.initialize({ startOnLoad: false });
document.fonts.ready.then(() => { mermaid.run({ querySelector: '.mermaid', postRenderCallback: initViewer }); });
</script>
</body>
</html>
+196
View File
@@ -0,0 +1,196 @@
/*
* Copyright (c) 2025 - softworkz
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* output writers for filtergraph details
*/
#include "config.h"
#include <string.h>
#if CONFIG_RESOURCE_COMPRESSION
#include <zlib.h>
#endif
#include "resman.h"
#include "libavutil/avassert.h"
#include "libavutil/pixdesc.h"
#include "libavutil/dict.h"
#include "libavutil/common.h"
extern const unsigned char ff_graph_html_data[];
extern const unsigned int ff_graph_html_len;
extern const unsigned char ff_graph_css_data[];
extern const unsigned ff_graph_css_len;
static const FFResourceDefinition resource_definitions[] = {
[FF_RESOURCE_GRAPH_CSS] = { FF_RESOURCE_GRAPH_CSS, "graph.css", &ff_graph_css_data[0], &ff_graph_css_len },
[FF_RESOURCE_GRAPH_HTML] = { FF_RESOURCE_GRAPH_HTML, "graph.html", &ff_graph_html_data[0], &ff_graph_html_len },
};
static const AVClass resman_class = {
.class_name = "ResourceManager",
};
typedef struct ResourceManagerContext {
const AVClass *class;
AVDictionary *resource_dic;
} ResourceManagerContext;
static AVMutex mutex = AV_MUTEX_INITIALIZER;
static ResourceManagerContext resman_ctx = { .class = &resman_class };
#if CONFIG_RESOURCE_COMPRESSION
static int decompress_gzip(ResourceManagerContext *ctx, uint8_t *in, unsigned in_len, char **out, size_t *out_len)
{
z_stream strm;
unsigned chunk = 65534;
int ret;
uint8_t *buf;
*out = NULL;
memset(&strm, 0, sizeof(strm));
// Allocate output buffer with extra byte for null termination
buf = (uint8_t *)av_mallocz(chunk + 1);
if (!buf) {
av_log(ctx, AV_LOG_ERROR, "Failed to allocate decompression buffer\n");
return AVERROR(ENOMEM);
}
// 15 + 16 tells zlib to detect GZIP or zlib automatically
ret = inflateInit2(&strm, 15 + 16);
if (ret != Z_OK) {
av_log(ctx, AV_LOG_ERROR, "Error during zlib initialization: %s\n", strm.msg);
av_free(buf);
return AVERROR(ENOSYS);
}
strm.avail_in = in_len;
strm.next_in = in;
strm.avail_out = chunk;
strm.next_out = buf;
ret = inflate(&strm, Z_FINISH);
if (ret != Z_OK && ret != Z_STREAM_END) {
av_log(ctx, AV_LOG_ERROR, "Inflate failed: %d, %s\n", ret, strm.msg);
inflateEnd(&strm);
av_free(buf);
return (ret == Z_STREAM_END) ? Z_OK : ((ret == Z_OK) ? Z_BUF_ERROR : ret);
}
if (strm.avail_out == 0) {
// TODO: Error or loop decoding?
av_log(ctx, AV_LOG_WARNING, "Decompression buffer may be too small\n");
}
*out_len = chunk - strm.avail_out;
buf[*out_len] = 0; // Ensure null termination
inflateEnd(&strm);
*out = (char *)buf;
return Z_OK;
}
#endif
void ff_resman_uninit(void)
{
ff_mutex_lock(&mutex);
av_dict_free(&resman_ctx.resource_dic);
ff_mutex_unlock(&mutex);
}
char *ff_resman_get_string(FFResourceId resource_id)
{
ResourceManagerContext *ctx = &resman_ctx;
FFResourceDefinition resource_definition = { 0 };
AVDictionaryEntry *dic_entry;
char *res = NULL;
for (unsigned i = 0; i < FF_ARRAY_ELEMS(resource_definitions); ++i) {
FFResourceDefinition def = resource_definitions[i];
if (def.resource_id == resource_id) {
resource_definition = def;
break;
}
}
av_assert1(resource_definition.name);
ff_mutex_lock(&mutex);
dic_entry = av_dict_get(ctx->resource_dic, resource_definition.name, NULL, 0);
if (!dic_entry) {
int dict_ret;
#if CONFIG_RESOURCE_COMPRESSION
char *out = NULL;
size_t out_len;
int ret = decompress_gzip(ctx, (uint8_t *)resource_definition.data, *resource_definition.data_len, &out, &out_len);
if (ret) {
av_log(ctx, AV_LOG_ERROR, "Unable to decompress the resource with ID %d\n", resource_id);
goto end;
}
dict_ret = av_dict_set(&ctx->resource_dic, resource_definition.name, out, 0);
if (dict_ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Failed to store decompressed resource in dictionary: %d\n", dict_ret);
av_freep(&out);
goto end;
}
av_freep(&out);
#else
dict_ret = av_dict_set(&ctx->resource_dic, resource_definition.name, (const char *)resource_definition.data, 0);
if (dict_ret < 0) {
av_log(ctx, AV_LOG_ERROR, "Failed to store resource in dictionary: %d\n", dict_ret);
goto end;
}
#endif
dic_entry = av_dict_get(ctx->resource_dic, resource_definition.name, NULL, 0);
if (!dic_entry) {
av_log(ctx, AV_LOG_ERROR, "Failed to retrieve resource from dictionary after storing it\n");
goto end;
}
}
res = dic_entry->value;
end:
ff_mutex_unlock(&mutex);
return res;
}
+50
View File
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2025 - softworkz
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_RESOURCES_RESMAN_H
#define FFTOOLS_RESOURCES_RESMAN_H
#include <stdint.h>
#include "config.h"
#include "fftools/ffmpeg.h"
#include "libavutil/avutil.h"
#include "libavutil/bprint.h"
#include "fftools/textformat/avtextformat.h"
typedef enum {
FF_RESOURCE_GRAPH_CSS,
FF_RESOURCE_GRAPH_HTML,
} FFResourceId;
typedef struct FFResourceDefinition {
FFResourceId resource_id;
const char *name;
const unsigned char *data;
const unsigned *data_len;
} FFResourceDefinition;
void ff_resman_uninit(void);
char *ff_resman_get_string(FFResourceId resource_id);
#endif /* FFTOOLS_RESOURCES_RESMAN_H */
+684
View File
@@ -0,0 +1,684 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <string.h>
#include "libavutil/avassert.h"
#include "libavutil/container_fifo.h"
#include "libavutil/channel_layout.h"
#include "libavutil/cpu.h"
#include "libavutil/error.h"
#include "libavutil/mathematics.h"
#include "libavutil/mem.h"
#include "libavutil/samplefmt.h"
#include "libavutil/timestamp.h"
#include "sync_queue.h"
/*
* How this works:
* --------------
* time: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
* -------------------------------------------------------------------
* | | | | | | | | | | | | | |
* |
* stream 0| d=1 d=2 d=1 d=3
* |
*
* stream 1d=1 d=5
*
* |
* stream 2| d=1d=1d=1d=1 <- stream 2 is the head stream of the queue
* |
* ^ ^
* [stream 2 tail] [stream 2 head]
*
* We have N streams (N=3 in the diagram), each stream is a FIFO. The *tail* of
* each FIFO is the frame with smallest end time, the *head* is the frame with
* the largest end time. Frames submitted to the queue with sq_send() are placed
* after the head, frames returned to the caller with sq_receive() are taken
* from the tail.
*
* The head stream of the whole queue (SyncQueue.head_stream) is the limiting
* stream with the *smallest* head timestamp, i.e. the stream whose source lags
* furthest behind all other streams. It determines which frames can be output
* from the queue.
*
* In the diagram, the head stream is 2, because it head time is t=5, while
* streams 0 and 1 end at t=8 and t=9 respectively. All frames that _end_ at
* or before t=5 can be output, i.e. the first 3 frames from stream 0, first
* frame from stream 1, and all 4 frames from stream 2.
*/
#define SQPTR(sq, frame) ((sq->type == SYNC_QUEUE_FRAMES) ? \
(void*)frame.f : (void*)frame.p)
typedef struct SyncQueueStream {
AVContainerFifo *fifo;
AVRational tb;
/* number of audio samples in fifo */
uint64_t samples_queued;
/* stream head: largest timestamp seen */
int64_t head_ts;
int limiting;
/* no more frames will be sent for this stream */
int finished;
uint64_t frames_sent;
uint64_t samples_sent;
uint64_t frames_max;
int frame_samples;
} SyncQueueStream;
struct SyncQueue {
enum SyncQueueType type;
void *logctx;
/* no more frames will be sent for any stream */
int finished;
/* sync head: the stream with the _smallest_ head timestamp
* this stream determines which frames can be output */
int head_stream;
/* the finished stream with the smallest finish timestamp or -1 */
int head_finished_stream;
// maximum buffering duration in microseconds
int64_t buf_size_us;
SyncQueueStream *streams;
unsigned int nb_streams;
int have_limiting;
uintptr_t align_mask;
};
/**
* Compute the end timestamp of a frame. If nb_samples is provided, consider
* the frame to have this number of audio samples, otherwise use frame duration.
*/
static int64_t frame_end(const SyncQueue *sq, SyncQueueFrame frame, int nb_samples)
{
if (nb_samples) {
int64_t d = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate},
frame.f->time_base);
return frame.f->pts + d;
}
return (sq->type == SYNC_QUEUE_PACKETS) ?
frame.p->pts + frame.p->duration :
frame.f->pts + frame.f->duration;
}
static int frame_samples(const SyncQueue *sq, SyncQueueFrame frame)
{
return (sq->type == SYNC_QUEUE_PACKETS) ? 0 : frame.f->nb_samples;
}
static int frame_null(const SyncQueue *sq, SyncQueueFrame frame)
{
return (sq->type == SYNC_QUEUE_PACKETS) ? (frame.p == NULL) : (frame.f == NULL);
}
static void tb_update(const SyncQueue *sq, SyncQueueStream *st,
const SyncQueueFrame frame)
{
AVRational tb = (sq->type == SYNC_QUEUE_PACKETS) ?
frame.p->time_base : frame.f->time_base;
av_assert0(tb.num > 0 && tb.den > 0);
if (tb.num == st->tb.num && tb.den == st->tb.den)
return;
// timebase should not change after the first frame
av_assert0(!av_container_fifo_can_read(st->fifo));
if (st->head_ts != AV_NOPTS_VALUE)
st->head_ts = av_rescale_q(st->head_ts, st->tb, tb);
st->tb = tb;
}
static void finish_stream(SyncQueue *sq, unsigned int stream_idx)
{
SyncQueueStream *st = &sq->streams[stream_idx];
if (!st->finished)
av_log(sq->logctx, AV_LOG_DEBUG,
"sq: finish %u; head ts %s\n", stream_idx,
av_ts2timestr(st->head_ts, &st->tb));
st->finished = 1;
if (st->limiting && st->head_ts != AV_NOPTS_VALUE) {
/* check if this stream is the new finished head */
if (sq->head_finished_stream < 0 ||
av_compare_ts(st->head_ts, st->tb,
sq->streams[sq->head_finished_stream].head_ts,
sq->streams[sq->head_finished_stream].tb) < 0) {
sq->head_finished_stream = stream_idx;
}
/* mark as finished all streams that should no longer receive new frames,
* due to them being ahead of some finished stream */
st = &sq->streams[sq->head_finished_stream];
for (unsigned int i = 0; i < sq->nb_streams; i++) {
SyncQueueStream *st1 = &sq->streams[i];
if (st != st1 && st1->head_ts != AV_NOPTS_VALUE &&
av_compare_ts(st->head_ts, st->tb, st1->head_ts, st1->tb) <= 0) {
if (!st1->finished)
av_log(sq->logctx, AV_LOG_DEBUG,
"sq: finish secondary %u; head ts %s\n", i,
av_ts2timestr(st1->head_ts, &st1->tb));
st1->finished = 1;
}
}
}
/* mark the whole queue as finished if all streams are finished */
for (unsigned int i = 0; i < sq->nb_streams; i++) {
if (!sq->streams[i].finished)
return;
}
sq->finished = 1;
av_log(sq->logctx, AV_LOG_DEBUG, "sq: finish queue\n");
}
static void queue_head_update(SyncQueue *sq)
{
av_assert0(sq->have_limiting);
if (sq->head_stream < 0) {
unsigned first_limiting = UINT_MAX;
/* wait for one timestamp in each stream before determining
* the queue head */
for (unsigned int i = 0; i < sq->nb_streams; i++) {
SyncQueueStream *st = &sq->streams[i];
if (!st->limiting)
continue;
if (st->head_ts == AV_NOPTS_VALUE)
return;
if (first_limiting == UINT_MAX)
first_limiting = i;
}
// placeholder value, correct one will be found below
av_assert0(first_limiting < UINT_MAX);
sq->head_stream = first_limiting;
}
for (unsigned int i = 0; i < sq->nb_streams; i++) {
SyncQueueStream *st_head = &sq->streams[sq->head_stream];
SyncQueueStream *st_other = &sq->streams[i];
if (st_other->limiting && st_other->head_ts != AV_NOPTS_VALUE &&
av_compare_ts(st_other->head_ts, st_other->tb,
st_head->head_ts, st_head->tb) < 0)
sq->head_stream = i;
}
}
/* update this stream's head timestamp */
static void stream_update_ts(SyncQueue *sq, unsigned int stream_idx, int64_t ts)
{
SyncQueueStream *st = &sq->streams[stream_idx];
if (ts == AV_NOPTS_VALUE ||
(st->head_ts != AV_NOPTS_VALUE && st->head_ts >= ts))
return;
st->head_ts = ts;
/* if this stream is now ahead of some finished stream, then
* this stream is also finished */
if (sq->head_finished_stream >= 0 &&
av_compare_ts(sq->streams[sq->head_finished_stream].head_ts,
sq->streams[sq->head_finished_stream].tb,
ts, st->tb) <= 0)
finish_stream(sq, stream_idx);
/* update the overall head timestamp if it could have changed */
if (st->limiting &&
(sq->head_stream < 0 || sq->head_stream == stream_idx))
queue_head_update(sq);
}
/* If the queue for the given stream (or all streams when stream_idx=-1)
* is overflowing, trigger a fake heartbeat on lagging streams.
*
* @return 1 if heartbeat triggered, 0 otherwise
*/
static int overflow_heartbeat(SyncQueue *sq, int stream_idx)
{
SyncQueueStream *st;
SyncQueueFrame frame;
int64_t tail_ts = AV_NOPTS_VALUE;
/* if no stream specified, pick the one that is most ahead */
if (stream_idx < 0) {
int64_t ts = AV_NOPTS_VALUE;
for (int i = 0; i < sq->nb_streams; i++) {
st = &sq->streams[i];
if (st->head_ts != AV_NOPTS_VALUE &&
(ts == AV_NOPTS_VALUE ||
av_compare_ts(ts, sq->streams[stream_idx].tb,
st->head_ts, st->tb) < 0)) {
ts = st->head_ts;
stream_idx = i;
}
}
/* no stream has a timestamp yet -> nothing to do */
if (stream_idx < 0)
return 0;
}
st = &sq->streams[stream_idx];
/* get the chosen stream's tail timestamp */
for (size_t i = 0; tail_ts == AV_NOPTS_VALUE &&
av_container_fifo_peek(st->fifo, (void**)&frame, i) >= 0; i++)
tail_ts = frame_end(sq, frame, 0);
/* overflow triggers when the tail is over specified duration behind the head */
if (tail_ts == AV_NOPTS_VALUE || tail_ts >= st->head_ts ||
av_rescale_q(st->head_ts - tail_ts, st->tb, AV_TIME_BASE_Q) < sq->buf_size_us)
return 0;
/* signal a fake timestamp for all streams that prevent tail_ts from being output */
tail_ts++;
for (unsigned int i = 0; i < sq->nb_streams; i++) {
const SyncQueueStream *st1 = &sq->streams[i];
int64_t ts;
if (st == st1 || st1->finished ||
(st1->head_ts != AV_NOPTS_VALUE &&
av_compare_ts(tail_ts, st->tb, st1->head_ts, st1->tb) <= 0))
continue;
ts = av_rescale_q(tail_ts, st->tb, st1->tb);
if (st1->head_ts != AV_NOPTS_VALUE)
ts = FFMAX(st1->head_ts + 1, ts);
av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u overflow heardbeat %s -> %s\n",
i, av_ts2timestr(st1->head_ts, &st1->tb), av_ts2timestr(ts, &st1->tb));
stream_update_ts(sq, i, ts);
}
return 1;
}
int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame)
{
SyncQueueStream *st;
int64_t ts;
int ret, nb_samples;
av_assert0(stream_idx < sq->nb_streams);
st = &sq->streams[stream_idx];
if (frame_null(sq, frame)) {
av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u EOF\n", stream_idx);
finish_stream(sq, stream_idx);
return 0;
}
if (st->finished)
return AVERROR_EOF;
tb_update(sq, st, frame);
nb_samples = frame_samples(sq, frame);
// make sure frame duration is consistent with sample count
if (nb_samples) {
av_assert0(frame.f->sample_rate > 0);
frame.f->duration = av_rescale_q(nb_samples, (AVRational){ 1, frame.f->sample_rate },
frame.f->time_base);
}
ts = frame_end(sq, frame, 0);
av_log(sq->logctx, AV_LOG_DEBUG, "sq: send %u ts %s\n", stream_idx,
av_ts2timestr(ts, &st->tb));
ret = av_container_fifo_write(st->fifo, SQPTR(sq, frame), 0);
if (ret < 0)
return ret;
stream_update_ts(sq, stream_idx, ts);
st->samples_queued += nb_samples;
st->samples_sent += nb_samples;
if (st->frame_samples)
st->frames_sent = st->samples_sent / st->frame_samples;
else
st->frames_sent++;
if (st->frames_sent >= st->frames_max) {
av_log(sq->logctx, AV_LOG_DEBUG, "sq: %u frames_max %"PRIu64" reached\n",
stream_idx, st->frames_max);
finish_stream(sq, stream_idx);
}
return 0;
}
static void offset_audio(AVFrame *f, int nb_samples)
{
const int planar = av_sample_fmt_is_planar(f->format);
const int planes = planar ? f->ch_layout.nb_channels : 1;
const int bps = av_get_bytes_per_sample(f->format);
const int offset = nb_samples * bps * (planar ? 1 : f->ch_layout.nb_channels);
av_assert0(bps > 0);
av_assert0(nb_samples < f->nb_samples);
for (int i = 0; i < planes; i++) {
f->extended_data[i] += offset;
if (i < FF_ARRAY_ELEMS(f->data))
f->data[i] = f->extended_data[i];
}
f->linesize[0] -= offset;
f->nb_samples -= nb_samples;
f->duration = av_rescale_q(f->nb_samples, (AVRational){ 1, f->sample_rate },
f->time_base);
f->pts += av_rescale_q(nb_samples, (AVRational){ 1, f->sample_rate },
f->time_base);
}
static int frame_is_aligned(const SyncQueue *sq, const AVFrame *frame)
{
// only checks linesize[0], so only works for audio
av_assert0(frame->nb_samples > 0);
av_assert0(sq->align_mask);
// only check data[0], because we always offset all data pointers
// by the same offset, so if one is aligned, all are
if (!((uintptr_t)frame->data[0] & sq->align_mask) &&
!(frame->linesize[0] & sq->align_mask) &&
frame->linesize[0] > sq->align_mask)
return 1;
return 0;
}
static int receive_samples(SyncQueue *sq, SyncQueueStream *st,
AVFrame *dst, int nb_samples)
{
SyncQueueFrame src;
int ret;
av_assert0(st->samples_queued >= nb_samples);
ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
av_assert0(ret >= 0);
// peeked frame has enough samples and its data is aligned
// -> we can just make a reference and limit its sample count
if (src.f->nb_samples > nb_samples && frame_is_aligned(sq, src.f)) {
ret = av_frame_ref(dst, src.f);
if (ret < 0)
return ret;
dst->nb_samples = nb_samples;
offset_audio(src.f, nb_samples);
st->samples_queued -= nb_samples;
goto finish;
}
// otherwise allocate a new frame and copy the data
ret = av_channel_layout_copy(&dst->ch_layout, &src.f->ch_layout);
if (ret < 0)
return ret;
dst->format = src.f->format;
dst->nb_samples = nb_samples;
ret = av_frame_get_buffer(dst, 0);
if (ret < 0)
goto fail;
ret = av_frame_copy_props(dst, src.f);
if (ret < 0)
goto fail;
dst->nb_samples = 0;
while (dst->nb_samples < nb_samples) {
int to_copy;
ret = av_container_fifo_peek(st->fifo, (void**)&src, 0);
av_assert0(ret >= 0);
to_copy = FFMIN(nb_samples - dst->nb_samples, src.f->nb_samples);
av_samples_copy(dst->extended_data, src.f->extended_data, dst->nb_samples,
0, to_copy, dst->ch_layout.nb_channels, dst->format);
if (to_copy < src.f->nb_samples)
offset_audio(src.f, to_copy);
else
av_container_fifo_drain(st->fifo, 1);
st->samples_queued -= to_copy;
dst->nb_samples += to_copy;
}
finish:
dst->duration = av_rescale_q(nb_samples, (AVRational){ 1, dst->sample_rate },
dst->time_base);
return 0;
fail:
av_frame_unref(dst);
return ret;
}
static int receive_for_stream(SyncQueue *sq, unsigned int stream_idx,
SyncQueueFrame frame)
{
const SyncQueueStream *st_head = sq->head_stream >= 0 ?
&sq->streams[sq->head_stream] : NULL;
SyncQueueStream *st;
av_assert0(stream_idx < sq->nb_streams);
st = &sq->streams[stream_idx];
if (av_container_fifo_can_read(st->fifo) &&
(st->frame_samples <= st->samples_queued || st->finished)) {
int nb_samples = st->frame_samples;
SyncQueueFrame peek;
int64_t ts;
int cmp = 1;
if (st->finished)
nb_samples = FFMIN(nb_samples, st->samples_queued);
av_container_fifo_peek(st->fifo, (void**)&peek, 0);
ts = frame_end(sq, peek, nb_samples);
/* check if this stream's tail timestamp does not overtake
* the overall queue head */
if (ts != AV_NOPTS_VALUE && st_head)
cmp = av_compare_ts(ts, st->tb, st_head->head_ts, st_head->tb);
/* We can release frames that do not end after the queue head.
* Frames with no timestamps are just passed through with no conditions.
* Frames are also passed through when there are no limiting streams.
*/
if (cmp <= 0 || ts == AV_NOPTS_VALUE || !sq->have_limiting) {
if (nb_samples &&
(nb_samples != peek.f->nb_samples || !frame_is_aligned(sq, peek.f))) {
int ret = receive_samples(sq, st, frame.f, nb_samples);
if (ret < 0)
return ret;
} else {
int ret = av_container_fifo_read(st->fifo, SQPTR(sq, frame), 0);
av_assert0(ret >= 0);
av_assert0(st->samples_queued >= frame_samples(sq, frame));
st->samples_queued -= frame_samples(sq, frame);
}
av_log(sq->logctx, AV_LOG_DEBUG,
"sq: receive %u ts %s queue head %d ts %s\n", stream_idx,
av_ts2timestr(frame_end(sq, frame, 0), &st->tb),
sq->head_stream,
st_head ? av_ts2timestr(st_head->head_ts, &st_head->tb) : "N/A");
return 0;
}
}
return (sq->finished || (st->finished && !av_container_fifo_can_read(st->fifo))) ?
AVERROR_EOF : AVERROR(EAGAIN);
}
static int receive_internal(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
{
int nb_eof = 0;
int ret;
/* read a frame for a specific stream */
if (stream_idx >= 0) {
ret = receive_for_stream(sq, stream_idx, frame);
return (ret < 0) ? ret : stream_idx;
}
/* read a frame for any stream with available output */
for (unsigned int i = 0; i < sq->nb_streams; i++) {
ret = receive_for_stream(sq, i, frame);
if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) {
nb_eof += (ret == AVERROR_EOF);
continue;
}
return (ret < 0) ? ret : i;
}
return (nb_eof == sq->nb_streams) ? AVERROR_EOF : AVERROR(EAGAIN);
}
int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame)
{
int ret = receive_internal(sq, stream_idx, frame);
/* try again if the queue overflowed and triggered a fake heartbeat
* for lagging streams */
if (ret == AVERROR(EAGAIN) && overflow_heartbeat(sq, stream_idx))
ret = receive_internal(sq, stream_idx, frame);
return ret;
}
int sq_add_stream(SyncQueue *sq, int limiting)
{
SyncQueueStream *tmp, *st;
tmp = av_realloc_array(sq->streams, sq->nb_streams + 1, sizeof(*sq->streams));
if (!tmp)
return AVERROR(ENOMEM);
sq->streams = tmp;
st = &sq->streams[sq->nb_streams];
memset(st, 0, sizeof(*st));
st->fifo = (sq->type == SYNC_QUEUE_FRAMES) ?
av_container_fifo_alloc_avframe(0) : av_container_fifo_alloc_avpacket(0);
if (!st->fifo)
return AVERROR(ENOMEM);
/* we set a valid default, so that a pathological stream that never
* receives even a real timebase (and no frames) won't stall all other
* streams forever; cf. overflow_heartbeat() */
st->tb = (AVRational){ 1, 1 };
st->head_ts = AV_NOPTS_VALUE;
st->frames_max = UINT64_MAX;
st->limiting = limiting;
sq->have_limiting |= limiting;
return sq->nb_streams++;
}
void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx, uint64_t frames)
{
SyncQueueStream *st;
av_assert0(stream_idx < sq->nb_streams);
st = &sq->streams[stream_idx];
st->frames_max = frames;
if (st->frames_sent >= st->frames_max)
finish_stream(sq, stream_idx);
}
void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
int frame_samples)
{
SyncQueueStream *st;
av_assert0(sq->type == SYNC_QUEUE_FRAMES);
av_assert0(stream_idx < sq->nb_streams);
st = &sq->streams[stream_idx];
st->frame_samples = frame_samples;
sq->align_mask = av_cpu_max_align() - 1;
}
SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx)
{
SyncQueue *sq = av_mallocz(sizeof(*sq));
if (!sq)
return NULL;
sq->type = type;
sq->buf_size_us = buf_size_us;
sq->logctx = logctx;
sq->head_stream = -1;
sq->head_finished_stream = -1;
return sq;
}
void sq_free(SyncQueue **psq)
{
SyncQueue *sq = *psq;
if (!sq)
return;
for (unsigned int i = 0; i < sq->nb_streams; i++)
av_container_fifo_free(&sq->streams[i].fifo);
av_freep(&sq->streams);
av_freep(psq);
}
+118
View File
@@ -0,0 +1,118 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_SYNC_QUEUE_H
#define FFTOOLS_SYNC_QUEUE_H
#include <stdint.h>
#include "libavcodec/packet.h"
#include "libavutil/frame.h"
enum SyncQueueType {
SYNC_QUEUE_PACKETS,
SYNC_QUEUE_FRAMES,
};
typedef union SyncQueueFrame {
AVFrame *f;
AVPacket *p;
} SyncQueueFrame;
#define SQFRAME(frame) ((SyncQueueFrame){ .f = (frame) })
#define SQPKT(pkt) ((SyncQueueFrame){ .p = (pkt) })
/**
* A sync queue provides timestamp synchronization between multiple streams.
* Some of these streams are marked as "limiting", then the queue ensures no
* stream gets ahead of any of the limiting streams.
*/
typedef struct SyncQueue SyncQueue;
/**
* Allocate a sync queue of the given type.
*
* @param buf_size_us maximum duration that will be buffered in microseconds
*/
SyncQueue *sq_alloc(enum SyncQueueType type, int64_t buf_size_us, void *logctx);
void sq_free(SyncQueue **sq);
/**
* Add a new stream to the sync queue.
*
* @param limiting whether the stream is limiting, i.e. no other stream can be
* longer than this one
* @return
* - a non-negative stream index on success
* - a negative error code on error
*/
int sq_add_stream(SyncQueue *sq, int limiting);
/**
* Limit the number of output frames for stream with index stream_idx
* to max_frames.
*/
void sq_limit_frames(SyncQueue *sq, unsigned int stream_idx,
uint64_t max_frames);
/**
* Set a constant output audio frame size, in samples. Can only be used with
* SYNC_QUEUE_FRAMES queues and audio streams.
*
* All output frames will have exactly frame_samples audio samples, except
* possibly for the last one, which may have fewer.
*/
void sq_frame_samples(SyncQueue *sq, unsigned int stream_idx,
int frame_samples);
/**
* Submit a frame for the stream with index stream_idx.
*
* On success, the sync queue takes ownership of the frame and will reset the
* contents of the supplied frame. On failure, the frame remains owned by the
* caller.
*
* Sending a frame with NULL contents marks the stream as finished.
*
* @return
* - 0 on success
* - AVERROR_EOF when no more frames should be submitted for this stream
* - another a negative error code on failure
*/
int sq_send(SyncQueue *sq, unsigned int stream_idx, SyncQueueFrame frame);
/**
* Read a frame from the queue.
*
* @param stream_idx index of the stream to read a frame for. May be -1, then
* try to read a frame from any stream that is ready for
* output.
* @param frame output frame will be written here on success. The frame is owned
* by the caller.
*
* @return
* - a non-negative index of the stream to which the returned frame belongs
* - AVERROR(EAGAIN) when more frames need to be submitted to the queue
* - AVERROR_EOF when no more frames will be available for this stream (for any
* stream if stream_idx is -1)
* - another negative error code on failure
*/
int sq_receive(SyncQueue *sq, int stream_idx, SyncQueueFrame frame);
#endif // FFTOOLS_SYNC_QUEUE_H
+702
View File
@@ -0,0 +1,702 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "libavutil/mem.h"
#include "libavutil/avassert.h"
#include "libavutil/bprint.h"
#include "libavutil/error.h"
#include "libavutil/hash.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/macros.h"
#include "libavutil/opt.h"
#include "avtextformat.h"
#define SECTION_ID_NONE (-1)
#define SHOW_OPTIONAL_FIELDS_AUTO (-1)
#define SHOW_OPTIONAL_FIELDS_NEVER 0
#define SHOW_OPTIONAL_FIELDS_ALWAYS 1
static const struct {
double bin_val;
double dec_val;
char bin_str[4];
char dec_str[4];
} si_prefixes[] = {
{ 1.0, 1.0, "", "" },
{ 1.024e3, 1e3, "Ki", "K" },
{ 1.048576e6, 1e6, "Mi", "M" },
{ 1.073741824e9, 1e9, "Gi", "G" },
{ 1.099511627776e12, 1e12, "Ti", "T" },
{ 1.125899906842624e15, 1e15, "Pi", "P" },
};
static const char *textcontext_get_formatter_name(void *p)
{
AVTextFormatContext *tctx = p;
return tctx->formatter->name;
}
#define OFFSET(x) offsetof(AVTextFormatContext, x)
static const AVOption textcontext_options[] = {
{ "string_validation", "set string validation mode",
OFFSET(string_validation), AV_OPT_TYPE_INT, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, 0, AV_TEXTFORMAT_STRING_VALIDATION_NB - 1, .unit = "sv" },
{ "sv", "set string validation mode",
OFFSET(string_validation), AV_OPT_TYPE_INT, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, 0, AV_TEXTFORMAT_STRING_VALIDATION_NB - 1, .unit = "sv" },
{ "ignore", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_IGNORE }, .unit = "sv" },
{ "replace", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_REPLACE }, .unit = "sv" },
{ "fail", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_TEXTFORMAT_STRING_VALIDATION_FAIL }, .unit = "sv" },
{ "string_validation_replacement", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, { .str = "" } },
{ "svr", "set string validation replacement string", OFFSET(string_validation_replacement), AV_OPT_TYPE_STRING, { .str = "\xEF\xBF\xBD" } },
{ NULL }
};
static void *textcontext_child_next(void *obj, void *prev)
{
AVTextFormatContext *ctx = obj;
if (!prev && ctx->formatter && ctx->formatter->priv_class && ctx->priv)
return ctx->priv;
return NULL;
}
static const AVClass textcontext_class = {
.class_name = "AVTextContext",
.item_name = textcontext_get_formatter_name,
.option = textcontext_options,
.version = LIBAVUTIL_VERSION_INT,
.child_next = textcontext_child_next,
};
static void bprint_bytes(AVBPrint *bp, const uint8_t *ubuf, size_t ubuf_size)
{
av_bprintf(bp, "0X");
for (unsigned i = 0; i < ubuf_size; i++)
av_bprintf(bp, "%02X", ubuf[i]);
}
int avtext_context_close(AVTextFormatContext **ptctx)
{
AVTextFormatContext *tctx = *ptctx;
int ret = 0;
if (!tctx)
return AVERROR(EINVAL);
av_hash_freep(&tctx->hash);
if (tctx->formatter) {
if (tctx->formatter->uninit)
ret = tctx->formatter->uninit(tctx);
if (tctx->formatter->priv_class)
av_opt_free(tctx->priv);
}
for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++)
av_bprint_finalize(&tctx->section_pbuf[i], NULL);
av_freep(&tctx->priv);
av_opt_free(tctx);
av_freep(ptctx);
return ret;
}
int avtext_context_open(AVTextFormatContext **ptctx, const AVTextFormatter *formatter, AVTextWriterContext *writer_context, const char *args,
const AVTextFormatSection *sections, int nb_sections, AVTextFormatOptions options, char *show_data_hash)
{
AVTextFormatContext *tctx;
int ret = 0;
av_assert0(ptctx && formatter);
if (!(tctx = av_mallocz(sizeof(AVTextFormatContext)))) {
ret = AVERROR(ENOMEM);
goto fail;
}
for (int i = 0; i < SECTION_MAX_NB_LEVELS; i++)
av_bprint_init(&tctx->section_pbuf[i], 1, AV_BPRINT_SIZE_UNLIMITED);
tctx->class = &textcontext_class;
av_opt_set_defaults(tctx);
if (!(tctx->priv = av_mallocz(formatter->priv_size))) {
ret = AVERROR(ENOMEM);
goto fail;
}
tctx->show_value_unit = options.show_value_unit;
tctx->use_value_prefix = options.use_value_prefix;
tctx->use_byte_value_binary_prefix = options.use_byte_value_binary_prefix;
tctx->use_value_sexagesimal_format = options.use_value_sexagesimal_format;
tctx->show_optional_fields = options.show_optional_fields;
if (nb_sections > SECTION_MAX_NB_SECTIONS) {
av_log(tctx, AV_LOG_ERROR, "The number of section definitions (%d) is larger than the maximum allowed (%d)\n", nb_sections, SECTION_MAX_NB_SECTIONS);
ret = AVERROR(EINVAL);
goto fail;
}
tctx->formatter = formatter;
tctx->level = -1;
tctx->sections = sections;
tctx->nb_sections = nb_sections;
tctx->writer = writer_context;
if (formatter->priv_class) {
void *priv_ctx = tctx->priv;
*(const AVClass **)priv_ctx = formatter->priv_class;
av_opt_set_defaults(priv_ctx);
}
/* convert options to dictionary */
if (args) {
AVDictionary *opts = NULL;
const AVDictionaryEntry *opt = NULL;
if ((ret = av_dict_parse_string(&opts, args, "=", ":", 0)) < 0) {
av_log(tctx, AV_LOG_ERROR, "Failed to parse option string '%s' provided to textformat context\n", args);
av_dict_free(&opts);
goto fail;
}
while ((opt = av_dict_iterate(opts, opt))) {
if ((ret = av_opt_set(tctx, opt->key, opt->value, AV_OPT_SEARCH_CHILDREN)) < 0) {
av_log(tctx, AV_LOG_ERROR, "Failed to set option '%s' with value '%s' provided to textformat context\n",
opt->key, opt->value);
av_dict_free(&opts);
goto fail;
}
}
av_dict_free(&opts);
}
if (show_data_hash) {
if ((ret = av_hash_alloc(&tctx->hash, show_data_hash)) < 0) {
if (ret == AVERROR(EINVAL)) {
const char *n;
av_log(NULL, AV_LOG_ERROR, "Unknown hash algorithm '%s'\nKnown algorithms:", show_data_hash);
for (unsigned i = 0; (n = av_hash_names(i)); i++)
av_log(NULL, AV_LOG_ERROR, " %s", n);
av_log(NULL, AV_LOG_ERROR, "\n");
}
goto fail;
}
}
/* validate replace string */
{
const uint8_t *p = (uint8_t *)tctx->string_validation_replacement;
const uint8_t *endp = p + strlen((const char *)p);
while (*p) {
const uint8_t *p0 = p;
int32_t code;
ret = av_utf8_decode(&code, &p, endp, tctx->string_validation_utf8_flags);
if (ret < 0) {
AVBPrint bp;
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_AUTOMATIC);
bprint_bytes(&bp, p0, p - p0);
av_log(tctx, AV_LOG_ERROR,
"Invalid UTF8 sequence %s found in string validation replace '%s'\n",
bp.str, tctx->string_validation_replacement);
goto fail;
}
}
}
if (tctx->formatter->init)
ret = tctx->formatter->init(tctx);
if (ret < 0)
goto fail;
*ptctx = tctx;
return 0;
fail:
avtext_context_close(&tctx);
return ret;
}
/* Temporary definitions during refactoring */
static const char unit_second_str[] = "s";
static const char unit_hertz_str[] = "Hz";
static const char unit_byte_str[] = "byte";
static const char unit_bit_per_second_str[] = "bit/s";
void avtext_print_section_header(AVTextFormatContext *tctx, const void *data, int section_id)
{
if (section_id < 0 || section_id >= tctx->nb_sections) {
av_log(tctx, AV_LOG_ERROR, "Invalid section_id for section_header: %d\n", section_id);
return;
}
tctx->level++;
av_assert0(tctx->level < SECTION_MAX_NB_LEVELS);
tctx->nb_item[tctx->level] = 0;
memset(tctx->nb_item_type[tctx->level], 0, sizeof(tctx->nb_item_type[tctx->level]));
tctx->section[tctx->level] = &tctx->sections[section_id];
if (tctx->formatter->print_section_header)
tctx->formatter->print_section_header(tctx, data);
}
void avtext_print_section_footer(AVTextFormatContext *tctx)
{
if (tctx->level < 0 || tctx->level >= SECTION_MAX_NB_LEVELS) {
av_log(tctx, AV_LOG_ERROR, "Invalid level for section_footer: %d\n", tctx->level);
return;
}
int section_id = tctx->section[tctx->level]->id;
int parent_section_id = tctx->level ?
tctx->section[tctx->level - 1]->id : SECTION_ID_NONE;
if (parent_section_id != SECTION_ID_NONE) {
tctx->nb_item[tctx->level - 1]++;
tctx->nb_item_type[tctx->level - 1][section_id]++;
}
if (tctx->formatter->print_section_footer)
tctx->formatter->print_section_footer(tctx);
tctx->level--;
}
void avtext_print_integer(AVTextFormatContext *tctx, const char *key, int64_t val, int flags)
{
const AVTextFormatSection *section;
av_assert0(tctx);
if (tctx->show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
return;
if (tctx->show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
&& (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL)
&& !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS))
return;
av_assert0(key && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS);
section = tctx->section[tctx->level];
if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
tctx->formatter->print_integer(tctx, key, val);
tctx->nb_item[tctx->level]++;
}
}
static inline int validate_string(AVTextFormatContext *tctx, char **dstp, const char *src)
{
const uint8_t *p, *endp, *srcp = (const uint8_t *)src;
AVBPrint dstbuf;
AVBPrint invalid_seq;
int invalid_chars_nb = 0, ret = 0;
*dstp = NULL;
av_bprint_init(&dstbuf, 0, AV_BPRINT_SIZE_UNLIMITED);
av_bprint_init(&invalid_seq, 0, AV_BPRINT_SIZE_UNLIMITED);
endp = srcp + strlen(src);
for (p = srcp; *p;) {
int32_t code;
int invalid = 0;
const uint8_t *p0 = p;
if (av_utf8_decode(&code, &p, endp, tctx->string_validation_utf8_flags) < 0) {
av_bprint_clear(&invalid_seq);
bprint_bytes(&invalid_seq, p0, p - p0);
av_log(tctx, AV_LOG_DEBUG, "Invalid UTF-8 sequence '%s' found in string '%s'\n", invalid_seq.str, src);
invalid = 1;
}
if (invalid) {
invalid_chars_nb++;
switch (tctx->string_validation) {
case AV_TEXTFORMAT_STRING_VALIDATION_FAIL:
av_log(tctx, AV_LOG_ERROR, "Invalid UTF-8 sequence found in string '%s'\n", src);
ret = AVERROR_INVALIDDATA;
goto end;
case AV_TEXTFORMAT_STRING_VALIDATION_REPLACE:
av_bprintf(&dstbuf, "%s", tctx->string_validation_replacement);
break;
}
}
if (!invalid || tctx->string_validation == AV_TEXTFORMAT_STRING_VALIDATION_IGNORE)
av_bprint_append_data(&dstbuf, p0, p-p0);
}
if (invalid_chars_nb && tctx->string_validation == AV_TEXTFORMAT_STRING_VALIDATION_REPLACE)
av_log(tctx, AV_LOG_WARNING,
"%d invalid UTF-8 sequence(s) found in string '%s', replaced with '%s'\n",
invalid_chars_nb, src, tctx->string_validation_replacement);
end:
av_bprint_finalize(&dstbuf, dstp);
av_bprint_finalize(&invalid_seq, NULL);
return ret;
}
struct unit_value {
union {
double d;
int64_t i;
} val;
const char *unit;
};
static char *value_string(const AVTextFormatContext *tctx, char *buf, int buf_size, struct unit_value uv)
{
double vald;
int64_t vali = 0;
int show_float = 0;
if (uv.unit == unit_second_str) {
vald = uv.val.d;
show_float = 1;
} else {
vald = (double)uv.val.i;
vali = uv.val.i;
}
if (uv.unit == unit_second_str && tctx->use_value_sexagesimal_format) {
double secs;
int hours, mins;
secs = vald;
mins = (int)secs / 60;
secs = secs - mins * 60;
hours = mins / 60;
mins %= 60;
snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
} else {
const char *prefix_string = "";
if (tctx->use_value_prefix && vald > 1) {
int64_t index;
if (uv.unit == unit_byte_str && tctx->use_byte_value_binary_prefix) {
index = (int64_t)(log2(vald) / 10);
index = av_clip64(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
vald /= si_prefixes[index].bin_val;
prefix_string = si_prefixes[index].bin_str;
} else {
index = (int64_t)(log10(vald) / 3);
index = av_clip64(index, 0, FF_ARRAY_ELEMS(si_prefixes) - 1);
vald /= si_prefixes[index].dec_val;
prefix_string = si_prefixes[index].dec_str;
}
vali = (int64_t)vald;
}
if (show_float || (tctx->use_value_prefix && vald != (int64_t)vald))
snprintf(buf, buf_size, "%f", vald);
else
snprintf(buf, buf_size, "%"PRId64, vali);
av_strlcatf(buf, buf_size, "%s%s%s", *prefix_string || tctx->show_value_unit ? " " : "",
prefix_string, tctx->show_value_unit ? uv.unit : "");
}
return buf;
}
void avtext_print_unit_integer(AVTextFormatContext *tctx, const char *key, int64_t val, const char *unit)
{
char val_str[128];
struct unit_value uv;
uv.val.i = val;
uv.unit = unit;
avtext_print_string(tctx, key, value_string(tctx, val_str, sizeof(val_str), uv), 0);
}
int avtext_print_string(AVTextFormatContext *tctx, const char *key, const char *val, int flags)
{
const AVTextFormatSection *section;
int ret = 0;
av_assert0(key && val && tctx->level >= 0 && tctx->level < SECTION_MAX_NB_LEVELS);
section = tctx->section[tctx->level];
if (tctx->show_optional_fields == SHOW_OPTIONAL_FIELDS_NEVER)
return 0;
if (tctx->show_optional_fields == SHOW_OPTIONAL_FIELDS_AUTO
&& (flags & AV_TEXTFORMAT_PRINT_STRING_OPTIONAL)
&& !(tctx->formatter->flags & AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS))
return 0;
if (section->show_all_entries || av_dict_get(section->entries_to_show, key, NULL, 0)) {
if (flags & AV_TEXTFORMAT_PRINT_STRING_VALIDATE) {
char *key1 = NULL, *val1 = NULL;
ret = validate_string(tctx, &key1, key);
if (ret < 0) goto end;
ret = validate_string(tctx, &val1, val);
if (ret < 0) goto end;
tctx->formatter->print_string(tctx, key1, val1);
end:
if (ret < 0)
av_log(tctx, AV_LOG_ERROR,
"Invalid key=value string combination %s=%s in section %s\n",
key, val, section->unique_name);
av_free(key1);
av_free(val1);
} else {
tctx->formatter->print_string(tctx, key, val);
}
tctx->nb_item[tctx->level]++;
}
return ret;
}
void avtext_print_rational(AVTextFormatContext *tctx, const char *key, AVRational q, char sep)
{
char buf[44];
snprintf(buf, sizeof(buf), "%d%c%d", q.num, sep, q.den);
avtext_print_string(tctx, key, buf, 0);
}
void avtext_print_time(AVTextFormatContext *tctx, const char *key,
int64_t ts, const AVRational *time_base, int is_duration)
{
if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0)) {
avtext_print_string(tctx, key, "N/A", AV_TEXTFORMAT_PRINT_STRING_OPTIONAL);
} else {
char buf[128];
double d = av_q2d(*time_base) * ts;
struct unit_value uv;
uv.val.d = d;
uv.unit = unit_second_str;
value_string(tctx, buf, sizeof(buf), uv);
avtext_print_string(tctx, key, buf, 0);
}
}
void avtext_print_ts(AVTextFormatContext *tctx, const char *key, int64_t ts, int is_duration)
{
if ((!is_duration && ts == AV_NOPTS_VALUE) || (is_duration && ts == 0))
avtext_print_string(tctx, key, "N/A", AV_TEXTFORMAT_PRINT_STRING_OPTIONAL);
else
avtext_print_integer(tctx, key, ts, 0);
}
void avtext_print_data(AVTextFormatContext *tctx, const char *key,
const uint8_t *data, int size)
{
AVBPrint bp;
unsigned offset = 0;
int i;
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
av_bprintf(&bp, "\n");
while (size) {
av_bprintf(&bp, "%08x: ", offset);
int l = FFMIN(size, 16);
for (i = 0; i < l; i++) {
av_bprintf(&bp, "%02x", data[i]);
if (i & 1)
av_bprintf(&bp, " ");
}
av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
for (i = 0; i < l; i++)
av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
av_bprintf(&bp, "\n");
offset += l;
data += l;
size -= l;
}
avtext_print_string(tctx, key, bp.str, 0);
av_bprint_finalize(&bp, NULL);
}
void avtext_print_data_hash(AVTextFormatContext *tctx, const char *key,
const uint8_t *data, int size)
{
char buf[AV_HASH_MAX_SIZE * 2 + 64] = { 0 };
int len;
if (!tctx->hash)
return;
av_hash_init(tctx->hash);
av_hash_update(tctx->hash, data, size);
len = snprintf(buf, sizeof(buf), "%s:", av_hash_get_name(tctx->hash));
av_hash_final_hex(tctx->hash, (uint8_t *)&buf[len], (int)sizeof(buf) - len);
avtext_print_string(tctx, key, buf, 0);
}
void avtext_print_integers(AVTextFormatContext *tctx, const char *key,
uint8_t *data, int size, const char *format,
int columns, int bytes, int offset_add)
{
AVBPrint bp;
unsigned offset = 0;
if (!key || !data || !format || columns <= 0 || bytes <= 0)
return;
av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
av_bprintf(&bp, "\n");
while (size) {
av_bprintf(&bp, "%08x: ", offset);
for (int i = 0, l = FFMIN(size, columns); i < l; i++) {
if (bytes == 1) av_bprintf(&bp, format, *data);
else if (bytes == 2) av_bprintf(&bp, format, AV_RN16(data));
else if (bytes == 4) av_bprintf(&bp, format, AV_RN32(data));
data += bytes;
size--;
}
av_bprintf(&bp, "\n");
offset += offset_add;
}
avtext_print_string(tctx, key, bp.str, 0);
av_bprint_finalize(&bp, NULL);
}
static const char *writercontext_get_writer_name(void *p)
{
AVTextWriterContext *wctx = p;
return wctx->writer->name;
}
static void *writercontext_child_next(void *obj, void *prev)
{
AVTextFormatContext *ctx = obj;
if (!prev && ctx->formatter && ctx->formatter->priv_class && ctx->priv)
return ctx->priv;
return NULL;
}
static const AVClass textwriter_class = {
.class_name = "AVTextWriterContext",
.item_name = writercontext_get_writer_name,
.version = LIBAVUTIL_VERSION_INT,
.child_next = writercontext_child_next,
};
int avtextwriter_context_close(AVTextWriterContext **pwctx)
{
AVTextWriterContext *wctx = *pwctx;
int ret = 0;
if (!wctx)
return AVERROR(EINVAL);
if (wctx->writer) {
if (wctx->writer->uninit)
ret = wctx->writer->uninit(wctx);
if (wctx->writer->priv_class)
av_opt_free(wctx->priv);
}
av_freep(&wctx->priv);
av_freep(pwctx);
return ret;
}
int avtextwriter_context_open(AVTextWriterContext **pwctx, const AVTextWriter *writer)
{
AVTextWriterContext *wctx;
int ret = 0;
if (!pwctx || !writer)
return AVERROR(EINVAL);
if (!((wctx = av_mallocz(sizeof(AVTextWriterContext))))) {
ret = AVERROR(ENOMEM);
goto fail;
}
if (writer->priv_size && !((wctx->priv = av_mallocz(writer->priv_size)))) {
ret = AVERROR(ENOMEM);
goto fail;
}
if (writer->priv_class) {
void *priv_ctx = wctx->priv;
*(const AVClass **)priv_ctx = writer->priv_class;
av_opt_set_defaults(priv_ctx);
}
wctx->class = &textwriter_class;
wctx->writer = writer;
av_opt_set_defaults(wctx);
if (wctx->writer->init)
ret = wctx->writer->init(wctx);
if (ret < 0)
goto fail;
*pwctx = wctx;
return 0;
fail:
avtextwriter_context_close(&wctx);
return ret;
}
static const AVTextFormatter *const registered_formatters[] =
{
&avtextformatter_default,
&avtextformatter_compact,
&avtextformatter_csv,
&avtextformatter_flat,
&avtextformatter_ini,
&avtextformatter_json,
&avtextformatter_xml,
&avtextformatter_mermaid,
&avtextformatter_mermaidhtml,
NULL
};
const AVTextFormatter *avtext_get_formatter_by_name(const char *name)
{
for (int i = 0; registered_formatters[i]; i++) {
const char *end;
if (av_strstart(name, registered_formatters[i]->name, &end) &&
(*end == '\0' || *end == '='))
return registered_formatters[i];
}
return NULL;
}
+199
View File
@@ -0,0 +1,199 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_TEXTFORMAT_AVTEXTFORMAT_H
#define FFTOOLS_TEXTFORMAT_AVTEXTFORMAT_H
#include <stdint.h>
#include "libavutil/dict.h"
#include "libavformat/avio.h"
#include "libavutil/bprint.h"
#include "libavutil/rational.h"
#include "libavutil/hash.h"
#include "avtextwriters.h"
#define SECTION_MAX_NB_CHILDREN 11
typedef struct AVTextFormatSectionContext {
char *context_id;
const char *context_type;
int context_flags;
} AVTextFormatSectionContext;
typedef struct AVTextFormatSection {
int id; ///< unique id identifying a section
const char *name;
#define AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER 1 ///< the section only contains other sections, but has no data at its own level
#define AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY 2 ///< the section contains an array of elements of the same type
#define AV_TEXTFORMAT_SECTION_FLAG_HAS_VARIABLE_FIELDS 4 ///< the section may contain a variable number of fields with variable keys.
/// For these sections the element_name field is mandatory.
#define AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE 8 ///< the section contains a type to distinguish multiple nested elements
#define AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE 16 ///< the items in this array section should be numbered individually by type
#define AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE 32 ///< ...
#define AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS 64 ///< ...
#define AV_TEXTFORMAT_SECTION_PRINT_TAGS 128 ///< ...
#define AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH 256 ///< ...
int flags;
const int children_ids[SECTION_MAX_NB_CHILDREN + 1]; ///< list of children section IDS, terminated by -1
const char *element_name; ///< name of the contained element, if provided
const char *unique_name; ///< unique section name, in case the name is ambiguous
AVDictionary *entries_to_show;
const char *(*get_type)(const void *data); ///< function returning a type if defined, must be defined when SECTION_FLAG_HAS_TYPE is defined
int show_all_entries;
const char *id_key; ///< name of the key to be used as the id
const char *src_id_key; ///< name of the key to be used as the source id for diagram connections
const char *dest_id_key; ///< name of the key to be used as the target id for diagram connections
const char *linktype_key; ///< name of the key to be used as the link type for diagram connections (AVTextFormatLinkType)
} AVTextFormatSection;
typedef struct AVTextFormatContext AVTextFormatContext;
#define AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS 1
#define AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT 2
#define AV_TEXTFORMAT_FLAG_IS_DIAGRAM_FORMATTER 4
typedef enum {
AV_TEXTFORMAT_STRING_VALIDATION_FAIL,
AV_TEXTFORMAT_STRING_VALIDATION_REPLACE,
AV_TEXTFORMAT_STRING_VALIDATION_IGNORE,
AV_TEXTFORMAT_STRING_VALIDATION_NB
} StringValidation;
typedef enum {
AV_TEXTFORMAT_LINKTYPE_SRCDEST,
AV_TEXTFORMAT_LINKTYPE_DESTSRC,
AV_TEXTFORMAT_LINKTYPE_BIDIR,
AV_TEXTFORMAT_LINKTYPE_NONDIR,
AV_TEXTFORMAT_LINKTYPE_HIDDEN,
AV_TEXTFORMAT_LINKTYPE_ONETOMANY = AV_TEXTFORMAT_LINKTYPE_SRCDEST,
AV_TEXTFORMAT_LINKTYPE_MANYTOONE = AV_TEXTFORMAT_LINKTYPE_DESTSRC,
AV_TEXTFORMAT_LINKTYPE_ONETOONE = AV_TEXTFORMAT_LINKTYPE_BIDIR,
AV_TEXTFORMAT_LINKTYPE_MANYTOMANY = AV_TEXTFORMAT_LINKTYPE_NONDIR,
} AVTextFormatLinkType;
typedef struct AVTextFormatter {
const AVClass *priv_class; ///< private class of the formatter, if any
int priv_size; ///< private size for the formatter context
const char *name;
int (*init) (AVTextFormatContext *tctx);
int (*uninit)(AVTextFormatContext *tctx);
void (*print_section_header)(AVTextFormatContext *tctx, const void *data);
void (*print_section_footer)(AVTextFormatContext *tctx);
void (*print_integer) (AVTextFormatContext *tctx, const char *, int64_t);
void (*print_string) (AVTextFormatContext *tctx, const char *, const char *);
int flags; ///< a combination or AV_TEXTFORMAT__FLAG_*
} AVTextFormatter;
#define SECTION_MAX_NB_LEVELS 12
#define SECTION_MAX_NB_SECTIONS 100
struct AVTextFormatContext {
const AVClass *class; ///< class of the formatter
const AVTextFormatter *formatter; ///< the AVTextFormatter of which this is an instance
AVTextWriterContext *writer; ///< the AVTextWriterContext
char *name; ///< name of this formatter instance
void *priv; ///< private data for use by the filter
const AVTextFormatSection *sections; ///< array containing all sections
int nb_sections; ///< number of sections
int level; ///< current level, starting from 0
/** number of the item printed in the given section, starting from 0 */
unsigned int nb_item[SECTION_MAX_NB_LEVELS];
unsigned int nb_item_type[SECTION_MAX_NB_LEVELS][SECTION_MAX_NB_SECTIONS];
/** section per each level */
const AVTextFormatSection *section[SECTION_MAX_NB_LEVELS];
AVBPrint section_pbuf[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
/// used by various formatters
int show_optional_fields;
int show_value_unit;
int use_value_prefix;
int use_byte_value_binary_prefix;
int use_value_sexagesimal_format;
struct AVHashContext *hash;
int string_validation;
char *string_validation_replacement;
unsigned int string_validation_utf8_flags;
};
typedef struct AVTextFormatOptions {
int show_optional_fields;
int show_value_unit;
int use_value_prefix;
int use_byte_value_binary_prefix;
int use_value_sexagesimal_format;
} AVTextFormatOptions;
#define AV_TEXTFORMAT_PRINT_STRING_OPTIONAL 1
#define AV_TEXTFORMAT_PRINT_STRING_VALIDATE 2
int avtext_context_open(AVTextFormatContext **ptctx, const AVTextFormatter *formatter, AVTextWriterContext *writer_context, const char *args,
const AVTextFormatSection *sections, int nb_sections, AVTextFormatOptions options, char *show_data_hash);
int avtext_context_close(AVTextFormatContext **tctx);
void avtext_print_section_header(AVTextFormatContext *tctx, const void *data, int section_id);
void avtext_print_section_footer(AVTextFormatContext *tctx);
void avtext_print_integer(AVTextFormatContext *tctx, const char *key, int64_t val, int flags);
int avtext_print_string(AVTextFormatContext *tctx, const char *key, const char *val, int flags);
void avtext_print_unit_integer(AVTextFormatContext *tctx, const char *key, int64_t val, const char *unit);
void avtext_print_rational(AVTextFormatContext *tctx, const char *key, AVRational q, char sep);
void avtext_print_time(AVTextFormatContext *tctx, const char *key, int64_t ts, const AVRational *time_base, int is_duration);
void avtext_print_ts(AVTextFormatContext *tctx, const char *key, int64_t ts, int is_duration);
void avtext_print_data(AVTextFormatContext *tctx, const char *key, const uint8_t *data, int size);
void avtext_print_data_hash(AVTextFormatContext *tctx, const char *key, const uint8_t *data, int size);
void avtext_print_integers(AVTextFormatContext *tctx, const char *key, uint8_t *data, int size,
const char *format, int columns, int bytes, int offset_add);
const AVTextFormatter *avtext_get_formatter_by_name(const char *name);
extern const AVTextFormatter avtextformatter_default;
extern const AVTextFormatter avtextformatter_compact;
extern const AVTextFormatter avtextformatter_csv;
extern const AVTextFormatter avtextformatter_flat;
extern const AVTextFormatter avtextformatter_ini;
extern const AVTextFormatter avtextformatter_json;
extern const AVTextFormatter avtextformatter_xml;
extern const AVTextFormatter avtextformatter_mermaid;
extern const AVTextFormatter avtextformatter_mermaidhtml;
#endif /* FFTOOLS_TEXTFORMAT_AVTEXTFORMAT_H */
+62
View File
@@ -0,0 +1,62 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_TEXTFORMAT_AVTEXTWRITERS_H
#define FFTOOLS_TEXTFORMAT_AVTEXTWRITERS_H
#include <stdint.h>
#include "libavformat/avio.h"
#include "libavutil/bprint.h"
typedef struct AVTextWriterContext AVTextWriterContext;
typedef struct AVTextWriter {
const AVClass *priv_class; ///< private class of the writer, if any
int priv_size; ///< private size for the writer private class
const char *name;
int (*init)(AVTextWriterContext *wctx);
int (*uninit)(AVTextWriterContext *wctx);
void (*writer_w8)(AVTextWriterContext *wctx, int b);
void (*writer_put_str)(AVTextWriterContext *wctx, const char *str);
void (*writer_vprintf)(AVTextWriterContext *wctx, const char *fmt, va_list vl);
} AVTextWriter;
typedef struct AVTextWriterContext {
const AVClass *class; ///< class of the writer
const AVTextWriter *writer;
const char *name;
void *priv; ///< private data for use by the writer
} AVTextWriterContext;
int avtextwriter_context_open(AVTextWriterContext **pwctx, const AVTextWriter *writer);
int avtextwriter_context_close(AVTextWriterContext **pwctx);
int avtextwriter_create_stdout(AVTextWriterContext **pwctx);
int avtextwriter_create_avio(AVTextWriterContext **pwctx, AVIOContext *avio_ctx, int close_on_uninit);
int avtextwriter_create_file(AVTextWriterContext **pwctx, const char *output_filename);
int avtextwriter_create_buffer(AVTextWriterContext **pwctx, AVBPrint *buffer);
#endif /* FFTOOLS_TEXTFORMAT_AVTEXTWRITERS_H */
+280
View File
@@ -0,0 +1,280 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "avtextformat.h"
#include "libavutil/bprint.h"
#include "libavutil/error.h"
#include "libavutil/opt.h"
#include "tf_internal.h"
/* Compact output */
/**
* Apply C-language-like string escaping.
*/
static const char *c_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
{
const char *p;
for (p = src; *p; p++) {
switch (*p) {
case '\b': av_bprintf(dst, "%s", "\\b"); break;
case '\f': av_bprintf(dst, "%s", "\\f"); break;
case '\n': av_bprintf(dst, "%s", "\\n"); break;
case '\r': av_bprintf(dst, "%s", "\\r"); break;
case '\\': av_bprintf(dst, "%s", "\\\\"); break;
default:
if (*p == sep)
av_bprint_chars(dst, '\\', 1);
av_bprint_chars(dst, *p, 1);
}
}
return dst->str;
}
/**
* Quote fields containing special characters, check RFC4180.
*/
static const char *csv_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
{
char meta_chars[] = { sep, '"', '\n', '\r', '\0' };
int needs_quoting = !!src[strcspn(src, meta_chars)];
if (needs_quoting)
av_bprint_chars(dst, '"', 1);
for (; *src; src++) {
if (*src == '"')
av_bprint_chars(dst, '"', 1);
av_bprint_chars(dst, *src, 1);
}
if (needs_quoting)
av_bprint_chars(dst, '"', 1);
return dst->str;
}
static const char *none_escape_str(AVBPrint *dst, const char *src, const char sep, void *log_ctx)
{
return src;
}
typedef struct CompactContext {
const AVClass *class;
char *item_sep_str;
char item_sep;
int nokey;
int print_section;
char *escape_mode_str;
const char * (*escape_str)(AVBPrint *dst, const char *src, const char sep, void *log_ctx);
int nested_section[SECTION_MAX_NB_LEVELS];
int has_nested_elems[SECTION_MAX_NB_LEVELS];
int terminate_line[SECTION_MAX_NB_LEVELS];
} CompactContext;
#undef OFFSET
#define OFFSET(x) offsetof(CompactContext, x)
static const AVOption compact_options[] = {
{ "item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, { .str = "|" }, 0, 0 },
{ "s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, { .str = "|" }, 0, 0 },
{ "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, { .str = "c" }, 0, 0 },
{ "e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, { .str = "c" }, 0, 0 },
{ "print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ "p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ NULL },
};
DEFINE_FORMATTER_CLASS(compact);
static av_cold int compact_init(AVTextFormatContext *wctx)
{
CompactContext *compact = wctx->priv;
if (strlen(compact->item_sep_str) != 1) {
av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
compact->item_sep_str);
return AVERROR(EINVAL);
}
compact->item_sep = compact->item_sep_str[0];
if (!strcmp(compact->escape_mode_str, "none")) {
compact->escape_str = none_escape_str;
} else if (!strcmp(compact->escape_mode_str, "c" )) {
compact->escape_str = c_escape_str;
} else if (!strcmp(compact->escape_mode_str, "csv" )) {
compact->escape_str = csv_escape_str;
} else {
av_log(wctx, AV_LOG_ERROR, "Unknown escape mode '%s'\n", compact->escape_mode_str);
return AVERROR(EINVAL);
}
return 0;
}
static void compact_print_section_header(AVTextFormatContext *wctx, const void *data)
{
CompactContext *compact = wctx->priv;
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
if (!section)
return;
compact->terminate_line[wctx->level] = 1;
compact->has_nested_elems[wctx->level] = 0;
av_bprint_clear(&wctx->section_pbuf[wctx->level]);
if (parent_section &&
(section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE ||
(!(section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) &&
!(parent_section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY))))) {
/* define a prefix for elements not contained in an array or
in a wrapper, or for array elements with a type */
const char *element_name = (char *)av_x_if_null(section->element_name, section->name);
AVBPrint *section_pbuf = &wctx->section_pbuf[wctx->level];
compact->nested_section[wctx->level] = 1;
compact->has_nested_elems[wctx->level - 1] = 1;
av_bprintf(section_pbuf, "%s%s",
wctx->section_pbuf[wctx->level - 1].str, element_name);
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE) {
// add /TYPE to prefix
av_bprint_chars(section_pbuf, '/', 1);
// normalize section type, replace special characters and lower case
for (const char *p = section->get_type(data); *p; p++) {
char c =
(*p >= '0' && *p <= '9') ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ? av_tolower(*p) : '_';
av_bprint_chars(section_pbuf, c, 1);
}
}
av_bprint_chars(section_pbuf, ':', 1);
wctx->nb_item[wctx->level] = wctx->nb_item[wctx->level - 1];
} else {
if (parent_section && !(parent_section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY)) &&
wctx->level && wctx->nb_item[wctx->level - 1])
writer_w8(wctx, compact->item_sep);
if (compact->print_section &&
!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY)))
writer_printf(wctx, "%s%c", section->name, compact->item_sep);
}
}
static void compact_print_section_footer(AVTextFormatContext *wctx)
{
CompactContext *compact = wctx->priv;
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
if (!section)
return;
if (!compact->nested_section[wctx->level] &&
compact->terminate_line[wctx->level] &&
!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY)))
writer_w8(wctx, '\n');
}
static void compact_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
{
CompactContext *compact = wctx->priv;
AVBPrint buf;
if (wctx->nb_item[wctx->level])
writer_w8(wctx, compact->item_sep);
if (!compact->nokey)
writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
writer_put_str(wctx, compact->escape_str(&buf, value, compact->item_sep, wctx));
av_bprint_finalize(&buf, NULL);
}
static void compact_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
{
CompactContext *compact = wctx->priv;
if (wctx->nb_item[wctx->level])
writer_w8(wctx, compact->item_sep);
if (!compact->nokey)
writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
writer_printf(wctx, "%"PRId64, value);
}
const AVTextFormatter avtextformatter_compact = {
.name = "compact",
.priv_size = sizeof(CompactContext),
.init = compact_init,
.print_section_header = compact_print_section_header,
.print_section_footer = compact_print_section_footer,
.print_integer = compact_print_int,
.print_string = compact_print_str,
.flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS,
.priv_class = &compact_class,
};
/* CSV output */
#undef OFFSET
#define OFFSET(x) offsetof(CompactContext, x)
static const AVOption csv_options[] = {
{ "item_sep", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, { .str = "," }, 0, 0 },
{ "s", "set item separator", OFFSET(item_sep_str), AV_OPT_TYPE_STRING, { .str = "," }, 0, 0 },
{ "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ "escape", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, { .str = "csv" }, 0, 0 },
{ "e", "set escape mode", OFFSET(escape_mode_str), AV_OPT_TYPE_STRING, { .str = "csv" }, 0, 0 },
{ "print_section", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ "p", "print section name", OFFSET(print_section), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ NULL },
};
DEFINE_FORMATTER_CLASS(csv);
const AVTextFormatter avtextformatter_csv = {
.name = "csv",
.priv_size = sizeof(CompactContext),
.init = compact_init,
.print_section_header = compact_print_section_header,
.print_section_footer = compact_print_section_footer,
.print_integer = compact_print_int,
.print_string = compact_print_str,
.flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS,
.priv_class = &csv_class,
};
+136
View File
@@ -0,0 +1,136 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "avtextformat.h"
#include "libavutil/bprint.h"
#include "libavutil/opt.h"
#include "tf_internal.h"
/* Default output */
typedef struct DefaultContext {
const AVClass *class;
int nokey;
int noprint_wrappers;
int nested_section[SECTION_MAX_NB_LEVELS];
} DefaultContext;
#undef OFFSET
#define OFFSET(x) offsetof(DefaultContext, x)
static const AVOption default_options[] = {
{ "noprint_wrappers", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "nw", "do not print headers and footers", OFFSET(noprint_wrappers), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "nokey", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "nk", "force no key printing", OFFSET(nokey), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ NULL },
};
DEFINE_FORMATTER_CLASS(default);
/* lame uppercasing routine, assumes the string is lower case ASCII */
static inline char *upcase_string(char *dst, size_t dst_size, const char *src)
{
unsigned i;
for (i = 0; src[i] && i < dst_size - 1; i++)
dst[i] = (char)av_toupper(src[i]);
dst[i] = 0;
return dst;
}
static void default_print_section_header(AVTextFormatContext *wctx, const void *data)
{
DefaultContext *def = wctx->priv;
char buf[32];
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
if (!section)
return;
av_bprint_clear(&wctx->section_pbuf[wctx->level]);
if (parent_section &&
!(parent_section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY))) {
def->nested_section[wctx->level] = 1;
av_bprintf(&wctx->section_pbuf[wctx->level], "%s%s:",
wctx->section_pbuf[wctx->level - 1].str,
upcase_string(buf, sizeof(buf),
av_x_if_null(section->element_name, section->name)));
}
if (def->noprint_wrappers || def->nested_section[wctx->level])
return;
if (!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY)))
writer_printf(wctx, "[%s]\n", upcase_string(buf, sizeof(buf), section->name));
}
static void default_print_section_footer(AVTextFormatContext *wctx)
{
DefaultContext *def = wctx->priv;
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
char buf[32];
if (!section)
return;
if (def->noprint_wrappers || def->nested_section[wctx->level])
return;
if (!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER | AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY)))
writer_printf(wctx, "[/%s]\n", upcase_string(buf, sizeof(buf), section->name));
}
static void default_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
{
DefaultContext *def = wctx->priv;
if (!def->nokey)
writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
writer_printf(wctx, "%s\n", value);
}
static void default_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
{
DefaultContext *def = wctx->priv;
if (!def->nokey)
writer_printf(wctx, "%s%s=", wctx->section_pbuf[wctx->level].str, key);
writer_printf(wctx, "%"PRId64"\n", value);
}
const AVTextFormatter avtextformatter_default = {
.name = "default",
.priv_size = sizeof(DefaultContext),
.print_section_header = default_print_section_header,
.print_section_footer = default_print_section_footer,
.print_integer = default_print_int,
.print_string = default_print_str,
.flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS,
.priv_class = &default_class,
};
+160
View File
@@ -0,0 +1,160 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "avtextformat.h"
#include "libavutil/bprint.h"
#include "libavutil/error.h"
#include "libavutil/opt.h"
#include "tf_internal.h"
/* Flat output */
typedef struct FlatContext {
const AVClass *class;
const char *sep_str;
char sep;
int hierarchical;
} FlatContext;
#undef OFFSET
#define OFFSET(x) offsetof(FlatContext, x)
static const AVOption flat_options[] = {
{ "sep_char", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, { .str = "." }, 0, 0 },
{ "s", "set separator", OFFSET(sep_str), AV_OPT_TYPE_STRING, { .str = "." }, 0, 0 },
{ "hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ "h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ NULL },
};
DEFINE_FORMATTER_CLASS(flat);
static av_cold int flat_init(AVTextFormatContext *wctx)
{
FlatContext *flat = wctx->priv;
if (strlen(flat->sep_str) != 1) {
av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
flat->sep_str);
return AVERROR(EINVAL);
}
flat->sep = flat->sep_str[0];
return 0;
}
static const char *flat_escape_key_str(AVBPrint *dst, const char *src, const char sep)
{
const char *p;
for (p = src; *p; p++) {
if (!((*p >= '0' && *p <= '9') ||
(*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z')))
av_bprint_chars(dst, '_', 1);
else
av_bprint_chars(dst, *p, 1);
}
return dst->str;
}
static const char *flat_escape_value_str(AVBPrint *dst, const char *src)
{
const char *p;
for (p = src; *p; p++) {
switch (*p) {
case '\n': av_bprintf(dst, "%s", "\\n"); break;
case '\r': av_bprintf(dst, "%s", "\\r"); break;
case '\\': av_bprintf(dst, "%s", "\\\\"); break;
case '"': av_bprintf(dst, "%s", "\\\""); break;
case '`': av_bprintf(dst, "%s", "\\`"); break;
case '$': av_bprintf(dst, "%s", "\\$"); break;
default: av_bprint_chars(dst, *p, 1); break;
}
}
return dst->str;
}
static void flat_print_section_header(AVTextFormatContext *wctx, const void *data)
{
FlatContext *flat = wctx->priv;
AVBPrint *buf = &wctx->section_pbuf[wctx->level];
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
if (!section)
return;
/* build section header */
av_bprint_clear(buf);
if (!parent_section)
return;
av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level - 1].str);
if (flat->hierarchical ||
!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER))) {
av_bprintf(buf, "%s%s", wctx->section[wctx->level]->name, flat->sep_str);
if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
int n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
? wctx->nb_item_type[wctx->level - 1][section->id]
: wctx->nb_item[wctx->level - 1];
av_bprintf(buf, "%d%s", n, flat->sep_str);
}
}
}
static void flat_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
{
writer_printf(wctx, "%s%s=%"PRId64"\n", wctx->section_pbuf[wctx->level].str, key, value);
}
static void flat_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
{
FlatContext *flat = wctx->priv;
AVBPrint buf;
writer_put_str(wctx, wctx->section_pbuf[wctx->level].str);
av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
writer_printf(wctx, "%s=", flat_escape_key_str(&buf, key, flat->sep));
av_bprint_clear(&buf);
writer_printf(wctx, "\"%s\"\n", flat_escape_value_str(&buf, value));
av_bprint_finalize(&buf, NULL);
}
const AVTextFormatter avtextformatter_flat = {
.name = "flat",
.priv_size = sizeof(FlatContext),
.init = flat_init,
.print_section_header = flat_print_section_header,
.print_integer = flat_print_int,
.print_string = flat_print_str,
.flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS | AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT,
.priv_class = &flat_class,
};
+149
View File
@@ -0,0 +1,149 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "avtextformat.h"
#include "libavutil/bprint.h"
#include "libavutil/opt.h"
#include "tf_internal.h"
/* Default output */
typedef struct DefaultContext {
const AVClass *class;
int nokey;
int noprint_wrappers;
int nested_section[SECTION_MAX_NB_LEVELS];
} DefaultContext;
/* INI format output */
typedef struct INIContext {
const AVClass *class;
int hierarchical;
} INIContext;
#undef OFFSET
#define OFFSET(x) offsetof(INIContext, x)
static const AVOption ini_options[] = {
{ "hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ "h", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
{ NULL },
};
DEFINE_FORMATTER_CLASS(ini);
static char *ini_escape_str(AVBPrint *dst, const char *src)
{
int i = 0;
char c;
while ((c = src[i++])) {
switch (c) {
case '\b': av_bprintf(dst, "%s", "\\b"); break;
case '\f': av_bprintf(dst, "%s", "\\f"); break;
case '\n': av_bprintf(dst, "%s", "\\n"); break;
case '\r': av_bprintf(dst, "%s", "\\r"); break;
case '\t': av_bprintf(dst, "%s", "\\t"); break;
case '\\':
case '#':
case '=':
case ':':
av_bprint_chars(dst, '\\', 1);
/* fallthrough */
default:
if ((unsigned char)c < 32)
av_bprintf(dst, "\\x00%02x", (unsigned char)c);
else
av_bprint_chars(dst, c, 1);
break;
}
}
return dst->str;
}
static void ini_print_section_header(AVTextFormatContext *wctx, const void *data)
{
INIContext *ini = wctx->priv;
AVBPrint *buf = &wctx->section_pbuf[wctx->level];
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
if (!section)
return;
av_bprint_clear(buf);
if (!parent_section) {
writer_put_str(wctx, "# ffprobe output\n\n");
return;
}
if (wctx->nb_item[wctx->level - 1])
writer_w8(wctx, '\n');
av_bprintf(buf, "%s", wctx->section_pbuf[wctx->level - 1].str);
if (ini->hierarchical ||
!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER))) {
av_bprintf(buf, "%s%s", buf->str[0] ? "." : "", wctx->section[wctx->level]->name);
if (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
unsigned n = parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE
? wctx->nb_item_type[wctx->level - 1][section->id]
: wctx->nb_item[wctx->level - 1];
av_bprintf(buf, ".%u", n);
}
}
if (!(section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER)))
writer_printf(wctx, "[%s]\n", buf->str);
}
static void ini_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
{
AVBPrint buf;
av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
writer_printf(wctx, "%s=", ini_escape_str(&buf, key));
av_bprint_clear(&buf);
writer_printf(wctx, "%s\n", ini_escape_str(&buf, value));
av_bprint_finalize(&buf, NULL);
}
static void ini_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
{
writer_printf(wctx, "%s=%"PRId64"\n", key, value);
}
const AVTextFormatter avtextformatter_ini = {
.name = "ini",
.priv_size = sizeof(INIContext),
.print_section_header = ini_print_section_header,
.print_integer = ini_print_int,
.print_string = ini_print_str,
.flags = AV_TEXTFORMAT_FLAG_SUPPORTS_OPTIONAL_FIELDS | AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT,
.priv_class = &ini_class,
};
+81
View File
@@ -0,0 +1,81 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* Internal utilities for text formatters.
*/
#ifndef FFTOOLS_TEXTFORMAT_TF_INTERNAL_H
#define FFTOOLS_TEXTFORMAT_TF_INTERNAL_H
#include "avtextformat.h"
#define DEFINE_FORMATTER_CLASS(name) \
static const AVClass name##_class = { \
.class_name = #name, \
.item_name = av_default_item_name, \
.option = name##_options \
}
/**
* Safely validate and access a section at a given level
*/
static inline const AVTextFormatSection *tf_get_section(AVTextFormatContext *tfc, int level)
{
if (!tfc || level < 0 || level >= SECTION_MAX_NB_LEVELS || !tfc->section[level]) {
if (tfc)
av_log(tfc, AV_LOG_ERROR, "Invalid section access at level %d\n", level);
return NULL;
}
return tfc->section[level];
}
/**
* Safely access the parent section
*/
static inline const AVTextFormatSection *tf_get_parent_section(AVTextFormatContext *tfc, int level)
{
if (level <= 0)
return NULL;
return tf_get_section(tfc, level - 1);
}
static inline void writer_w8(AVTextFormatContext *wctx, int b)
{
wctx->writer->writer->writer_w8(wctx->writer, b);
}
static inline void writer_put_str(AVTextFormatContext *wctx, const char *str)
{
wctx->writer->writer->writer_put_str(wctx->writer, str);
}
static inline void writer_printf(AVTextFormatContext *wctx, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
wctx->writer->writer->writer_vprintf(wctx->writer, fmt, args);
va_end(args);
}
#endif /* FFTOOLS_TEXTFORMAT_TF_INTERNAL_H */
+213
View File
@@ -0,0 +1,213 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "avtextformat.h"
#include "libavutil/bprint.h"
#include "libavutil/opt.h"
#include "tf_internal.h"
/* JSON output */
typedef struct JSONContext {
const AVClass *class;
int indent_level;
int compact;
const char *item_sep, *item_start_end;
} JSONContext;
#undef OFFSET
#define OFFSET(x) offsetof(JSONContext, x)
static const AVOption json_options[] = {
{ "compact", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "c", "enable compact output", OFFSET(compact), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ NULL }
};
DEFINE_FORMATTER_CLASS(json);
static av_cold int json_init(AVTextFormatContext *wctx)
{
JSONContext *json = wctx->priv;
json->item_sep = json->compact ? ", " : ",\n";
json->item_start_end = json->compact ? " " : "\n";
return 0;
}
static const char *json_escape_str(AVBPrint *dst, const char *src, void *log_ctx)
{
static const char json_escape[] = { '"', '\\', '\b', '\f', '\n', '\r', '\t', 0 };
static const char json_subst[] = { '"', '\\', 'b', 'f', 'n', 'r', 't', 0 };
const char *p;
if (!src) {
av_log(log_ctx, AV_LOG_WARNING, "Cannot escape NULL string, returning NULL\n");
return NULL;
}
for (p = src; *p; p++) {
char *s = strchr(json_escape, *p);
if (s) {
av_bprint_chars(dst, '\\', 1);
av_bprint_chars(dst, json_subst[s - json_escape], 1);
} else if ((unsigned char)*p < 32) {
av_bprintf(dst, "\\u00%02x", (unsigned char)*p);
} else {
av_bprint_chars(dst, *p, 1);
}
}
return dst->str;
}
#define JSON_INDENT() writer_printf(wctx, "%*c", json->indent_level * 4, ' ')
static void json_print_section_header(AVTextFormatContext *wctx, const void *data)
{
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
JSONContext *json = wctx->priv;
AVBPrint buf;
if (!section)
return;
if (wctx->level && wctx->nb_item[wctx->level - 1])
writer_put_str(wctx, ",\n");
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER) {
writer_put_str(wctx, "{\n");
json->indent_level++;
} else {
av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
json_escape_str(&buf, section->name, wctx);
JSON_INDENT();
json->indent_level++;
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
writer_printf(wctx, "\"%s\": [\n", buf.str);
} else if (parent_section && !(parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY)) {
writer_printf(wctx, "\"%s\": {%s", buf.str, json->item_start_end);
} else {
writer_printf(wctx, "{%s", json->item_start_end);
/* this is required so the parser can distinguish between packets and frames */
if (parent_section && parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE) {
if (!json->compact)
JSON_INDENT();
writer_printf(wctx, "\"type\": \"%s\"", section->name);
wctx->nb_item[wctx->level]++;
}
}
av_bprint_finalize(&buf, NULL);
}
}
static void json_print_section_footer(AVTextFormatContext *wctx)
{
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
JSONContext *json = wctx->priv;
if (!section)
return;
if (wctx->level == 0) {
json->indent_level--;
writer_put_str(wctx, "\n}\n");
} else if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY) {
writer_w8(wctx, '\n');
json->indent_level--;
JSON_INDENT();
writer_w8(wctx, ']');
} else {
writer_put_str(wctx, json->item_start_end);
json->indent_level--;
if (!json->compact)
JSON_INDENT();
writer_w8(wctx, '}');
}
}
static inline void json_print_item_str(AVTextFormatContext *wctx,
const char *key, const char *value)
{
AVBPrint buf;
av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
writer_printf(wctx, "\"%s\":", json_escape_str(&buf, key, wctx));
av_bprint_clear(&buf);
writer_printf(wctx, " \"%s\"", json_escape_str(&buf, value, wctx));
av_bprint_finalize(&buf, NULL);
}
static void json_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
{
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
JSONContext *json = wctx->priv;
if (!section)
return;
if (wctx->nb_item[wctx->level] || (parent_section && parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE))
writer_put_str(wctx, json->item_sep);
if (!json->compact)
JSON_INDENT();
json_print_item_str(wctx, key, value);
}
static void json_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
{
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
JSONContext *json = wctx->priv;
AVBPrint buf;
if (!section)
return;
if (wctx->nb_item[wctx->level] || (parent_section && parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_NUMBERING_BY_TYPE))
writer_put_str(wctx, json->item_sep);
if (!json->compact)
JSON_INDENT();
av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
writer_printf(wctx, "\"%s\": %"PRId64, json_escape_str(&buf, key, wctx), value);
av_bprint_finalize(&buf, NULL);
}
const AVTextFormatter avtextformatter_json = {
.name = "json",
.priv_size = sizeof(JSONContext),
.init = json_init,
.print_section_header = json_print_section_header,
.print_section_footer = json_print_section_footer,
.print_integer = json_print_int,
.print_string = json_print_str,
.flags = AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT,
.priv_class = &json_class,
};
+673
View File
@@ -0,0 +1,673 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "avtextformat.h"
#include "tf_internal.h"
#include "tf_mermaid.h"
#include <libavutil/mem.h>
#include <libavutil/avassert.h>
#include <libavutil/bprint.h>
#include <libavutil/opt.h>
static const char *init_directive = ""
"%%{init: {"
"\"theme\": \"base\","
"\"curve\": \"monotoneX\","
"\"rankSpacing\": 10,"
"\"nodeSpacing\": 10,"
"\"themeCSS\": \"__###__\","
"\"fontFamily\": \"Roboto,Segoe UI,sans-serif\","
"\"themeVariables\": { "
"\"clusterBkg\": \"white\", "
"\"primaryBorderColor\": \"gray\", "
"\"lineColor\": \"gray\", "
"\"secondaryTextColor\": \"gray\", "
"\"tertiaryBorderColor\": \"gray\", "
"\"primaryTextColor\": \"#666\", "
"\"secondaryTextColor\": \"red\" "
"},"
"\"flowchart\": { "
"\"subGraphTitleMargin\": { \"top\": -15, \"bottom\": 20 }, "
"\"diagramPadding\": 20, "
"\"curve\": \"monotoneX\" "
"}"
" }}%%\n\n";
static const char* init_directive_er = ""
"%%{init: {"
"\"theme\": \"base\","
"\"layout\": \"elk\","
"\"curve\": \"monotoneX\","
"\"rankSpacing\": 65,"
"\"nodeSpacing\": 60,"
"\"themeCSS\": \"__###__\","
"\"fontFamily\": \"Roboto,Segoe UI,sans-serif\","
"\"themeVariables\": { "
"\"clusterBkg\": \"white\", "
"\"primaryBorderColor\": \"gray\", "
"\"lineColor\": \"gray\", "
"\"secondaryTextColor\": \"gray\", "
"\"tertiaryBorderColor\": \"gray\", "
"\"primaryTextColor\": \"#666\", "
"\"secondaryTextColor\": \"red\" "
"},"
"\"er\": { "
"\"diagramPadding\": 12, "
"\"entityPadding\": 4, "
"\"minEntityWidth\": 150, "
"\"minEntityHeight\": 20, "
"\"curve\": \"monotoneX\" "
"}"
" }}%%\n\n";
static const char *theme_css_er = ""
// Variables
".root { "
"--ff-colvideo: #6eaa7b; "
"--ff-colaudio: #477fb3; "
"--ff-colsubtitle: #ad76ab; "
"--ff-coltext: #666; "
"} "
" g.nodes g.node.default rect.basic.label-container, "
" g.nodes g.node.default path { "
" rx: 1; "
" ry: 1; "
" stroke-width: 1px !important; "
" stroke: #e9e9e9 !important; "
" fill: url(#ff-filtergradient) !important; "
" filter: drop-shadow(0px 0px 5.5px rgba(0, 0, 0, 0.05)); "
" fill: white !important; "
" } "
" "
" .relationshipLine { "
" stroke: gray; "
" stroke-width: 1; "
" fill: none; "
" filter: drop-shadow(0px 0px 3px rgba(0, 0, 0, 0.2)); "
" } "
" "
" g.node.default g.label.name foreignObject > div > span > p, "
" g.nodes g.node.default g.label:not(.attribute-name, .attribute-keys, .attribute-type, .attribute-comment) foreignObject > div > span > p { "
" font-size: 0.95rem; "
" font-weight: 500; "
" text-transform: uppercase; "
" min-width: 5.5rem; "
" margin-bottom: 0.5rem; "
" "
" } "
" "
" .edgePaths path { "
" marker-end: none; "
" marker-start: none; "
" "
"} ";
/* Mermaid Graph output */
typedef struct MermaidContext {
const AVClass *class;
AVDiagramConfig *diagram_config;
int subgraph_count;
int within_tag;
int indent_level;
int create_html;
// Options
int enable_link_colors; // Requires Mermaid 11.5
struct section_data {
const char *section_id;
const char *section_type;
const char *src_id;
const char *dest_id;
AVTextFormatLinkType link_type;
int current_is_textblock;
int current_is_stadium;
int subgraph_start_incomplete;
} section_data[SECTION_MAX_NB_LEVELS];
unsigned nb_link_captions[SECTION_MAX_NB_LEVELS]; ///< generic print buffer dedicated to each section,
AVBPrint link_buf; ///< print buffer for writing diagram links
AVDictionary *link_dict;
} MermaidContext;
#undef OFFSET
#define OFFSET(x) offsetof(MermaidContext, x)
static const AVOption mermaid_options[] = {
{ "link_coloring", "enable colored links (requires Mermaid >= 11.5)", OFFSET(enable_link_colors), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1 },
////{"diagram_css", "CSS for the diagram", OFFSET(diagram_css), AV_OPT_TYPE_STRING, {.i64=0}, 0, 1 },
////{"html_template", "Template HTML", OFFSET(html_template), AV_OPT_TYPE_STRING, {.i64=0}, 0, 1 },
{ NULL },
};
DEFINE_FORMATTER_CLASS(mermaid);
void av_diagram_init(AVTextFormatContext *tfc, AVDiagramConfig *diagram_config)
{
MermaidContext *mmc = tfc->priv;
mmc->diagram_config = diagram_config;
}
static av_cold int has_link_pair(const AVTextFormatContext *tfc, const char *src, const char *dest)
{
MermaidContext *mmc = tfc->priv;
AVBPrint buf;
av_bprint_init(&buf, 0, AV_BPRINT_SIZE_UNLIMITED);
av_bprintf(&buf, "%s--%s", src, dest);
if (mmc->link_dict && av_dict_get(mmc->link_dict, buf.str, NULL, 0))
return 1;
av_dict_set(&mmc->link_dict, buf.str, buf.str, 0);
return 0;
}
static av_cold int mermaid_init(AVTextFormatContext *tfc)
{
MermaidContext *mmc = tfc->priv;
av_bprint_init(&mmc->link_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
////mmc->enable_link_colors = 1; // Requires Mermaid 11.5
return 0;
}
static av_cold int mermaid_init_html(AVTextFormatContext *tfc)
{
MermaidContext *mmc = tfc->priv;
int ret = mermaid_init(tfc);
if (ret < 0)
return ret;
mmc->create_html = 1;
return 0;
}
static av_cold int mermaid_uninit(AVTextFormatContext *tfc)
{
MermaidContext *mmc = tfc->priv;
av_bprint_finalize(&mmc->link_buf, NULL);
av_dict_free(&mmc->link_dict);
for (unsigned i = 0; i < SECTION_MAX_NB_LEVELS; i++) {
av_freep(&mmc->section_data[i].dest_id);
av_freep(&mmc->section_data[i].section_id);
av_freep(&mmc->section_data[i].src_id);
av_freep(&mmc->section_data[i].section_type);
}
return 0;
}
static void set_str(const char **dst, const char *src)
{
if (*dst)
av_freep(dst);
if (src)
*dst = av_strdup(src);
}
#define MM_INDENT() writer_printf(tfc, "%*c", mmc->indent_level * 2, ' ')
static void mermaid_print_section_header(AVTextFormatContext *tfc, const void *data)
{
const AVTextFormatSection *section = tf_get_section(tfc, tfc->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(tfc, tfc->level);
if (!section)
return;
AVBPrint *buf = &tfc->section_pbuf[tfc->level];
MermaidContext *mmc = tfc->priv;
const AVTextFormatSectionContext *sec_ctx = data;
if (tfc->level == 0) {
char *directive;
AVBPrint css_buf;
const char *diag_directive = mmc->diagram_config->diagram_type == AV_DIAGRAMTYPE_ENTITYRELATIONSHIP ? init_directive_er : init_directive;
char *single_line_css = av_strireplace(mmc->diagram_config->diagram_css, "\n", " ");
(void)theme_css_er;
////char *single_line_css = av_strireplace(theme_css_er, "\n", " ");
av_bprint_init(&css_buf, 0, AV_BPRINT_SIZE_UNLIMITED);
av_bprint_escape(&css_buf, single_line_css, "'\\", AV_ESCAPE_MODE_BACKSLASH, AV_ESCAPE_FLAG_STRICT);
av_freep(&single_line_css);
directive = av_strireplace(diag_directive, "__###__", css_buf.str);
if (mmc->create_html) {
uint64_t length;
char *token_pos = av_stristr(mmc->diagram_config->html_template, "__###__");
if (!token_pos) {
av_log(tfc, AV_LOG_ERROR, "Unable to locate the required token (__###__) in the html template.");
return;
}
length = token_pos - mmc->diagram_config->html_template;
for (uint64_t i = 0; i < length; i++)
writer_w8(tfc, mmc->diagram_config->html_template[i]);
}
writer_put_str(tfc, directive);
switch (mmc->diagram_config->diagram_type) {
case AV_DIAGRAMTYPE_GRAPH:
writer_put_str(tfc, "flowchart LR\n");
////writer_put_str(tfc, " gradient_def@{ shape: text, label: \"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1\" height=\"1\"><defs><linearGradient id=\"ff-filtergradient\" x1=\"0%\" y1=\"0%\" x2=\"0%\" y2=\"100%\"><stop offset=\"0%\" style=\"stop-color:hsla(0, 0%, 30%, 0.02);\"/><stop offset=\"50%\" style=\"stop-color:hsla(0, 0%, 30%, 0);\"/><stop offset=\"100%\" style=\"stop-color:hsla(0, 0%, 30%, 0.05);\"/></linearGradient></defs></svg>\" }\n");
writer_put_str(tfc, " gradient_def@{ shape: text, label: \"<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"1\" height=\"1\"><defs><linearGradient id=\"ff-filtergradient\" x1=\"0%\" y1=\"0%\" x2=\"0%\" y2=\"100%\"><stop offset=\"0%\" style=\"stop-color:hsl(0, 0%, 98.6%); \"/><stop offset=\"50%\" style=\"stop-color:hsl(0, 0%, 100%); \"/><stop offset=\"100%\" style=\"stop-color:hsl(0, 0%, 96.5%); \"/></linearGradient><radialGradient id=\"ff-radgradient\" cx=\"50%\" cy=\"50%\" r=\"100%\" fx=\"45%\" fy=\"40%\"><stop offset=\"25%\" stop-color=\"hsl(0, 0%, 100%)\" /><stop offset=\"100%\" stop-color=\"hsl(0, 0%, 96%)\" /></radialGradient></defs></svg>\" }\n");
break;
case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
writer_put_str(tfc, "erDiagram\n");
break;
}
av_bprint_finalize(&css_buf, NULL);
av_freep(&directive);
return;
}
if (parent_section && parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH) {
struct section_data parent_sec_data = mmc->section_data[tfc->level - 1];
AVBPrint *parent_buf = &tfc->section_pbuf[tfc->level - 1];
if (parent_sec_data.subgraph_start_incomplete) {
if (parent_buf->len > 0)
writer_printf(tfc, "%s", parent_buf->str);
writer_put_str(tfc, "</div>\"]\n");
mmc->section_data[tfc->level - 1].subgraph_start_incomplete = 0;
}
}
av_freep(&mmc->section_data[tfc->level].section_id);
av_freep(&mmc->section_data[tfc->level].section_type);
av_freep(&mmc->section_data[tfc->level].src_id);
av_freep(&mmc->section_data[tfc->level].dest_id);
mmc->section_data[tfc->level].current_is_textblock = 0;
mmc->section_data[tfc->level].current_is_stadium = 0;
mmc->section_data[tfc->level].subgraph_start_incomplete = 0;
mmc->section_data[tfc->level].link_type = AV_TEXTFORMAT_LINKTYPE_SRCDEST;
// NOTE: av_strdup() allocations aren't checked
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH) {
av_bprint_clear(buf);
writer_put_str(tfc, "\n");
mmc->indent_level++;
if (sec_ctx->context_id) {
MM_INDENT();
writer_printf(tfc, "subgraph %s[\"<div class=\"ff-%s\">", sec_ctx->context_id, section->name);
} else {
av_log(tfc, AV_LOG_ERROR, "Unable to write subgraph start. Missing id field. Section: %s", section->name);
}
mmc->section_data[tfc->level].subgraph_start_incomplete = 1;
set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id);
}
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE) {
av_bprint_clear(buf);
writer_put_str(tfc, "\n");
mmc->indent_level++;
if (sec_ctx->context_id) {
set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id);
switch (mmc->diagram_config->diagram_type) {
case AV_DIAGRAMTYPE_GRAPH:
if (sec_ctx->context_flags & 1) {
MM_INDENT();
writer_printf(tfc, "%s@{ shape: text, label: \"", sec_ctx->context_id);
mmc->section_data[tfc->level].current_is_textblock = 1;
} else if (sec_ctx->context_flags & 2) {
MM_INDENT();
writer_printf(tfc, "%s([\"", sec_ctx->context_id);
mmc->section_data[tfc->level].current_is_stadium = 1;
} else {
MM_INDENT();
writer_printf(tfc, "%s(\"", sec_ctx->context_id);
}
break;
case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
MM_INDENT();
writer_printf(tfc, "%s {\n", sec_ctx->context_id);
break;
}
} else {
av_log(tfc, AV_LOG_ERROR, "Unable to write shape start. Missing id field. Section: %s", section->name);
}
set_str(&mmc->section_data[tfc->level].section_id, sec_ctx->context_id);
}
if (section->flags & AV_TEXTFORMAT_SECTION_PRINT_TAGS) {
if (sec_ctx && sec_ctx->context_type)
writer_printf(tfc, "<div class=\"ff-%s %s\">", section->name, sec_ctx->context_type);
else
writer_printf(tfc, "<div class=\"ff-%s\">", section->name);
}
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS) {
av_bprint_clear(buf);
mmc->nb_link_captions[tfc->level] = 0;
if (sec_ctx && sec_ctx->context_type)
set_str(&mmc->section_data[tfc->level].section_type, sec_ctx->context_type);
////if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE) {
//// AVBPrint buf;
//// av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
//// av_bprint_escape(&buf, section->get_type(data), NULL,
//// AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
//// writer_printf(tfc, " type=\"%s\"", buf.str);
}
}
static void mermaid_print_section_footer(AVTextFormatContext *tfc)
{
MermaidContext *mmc = tfc->priv;
const AVTextFormatSection *section = tf_get_section(tfc, tfc->level);
if (!section)
return;
AVBPrint *buf = &tfc->section_pbuf[tfc->level];
struct section_data sec_data = mmc->section_data[tfc->level];
if (section->flags & AV_TEXTFORMAT_SECTION_PRINT_TAGS)
writer_put_str(tfc, "</div>");
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE) {
switch (mmc->diagram_config->diagram_type) {
case AV_DIAGRAMTYPE_GRAPH:
if (sec_data.current_is_textblock) {
writer_printf(tfc, "\"}\n", section->name);
if (sec_data.section_id) {
MM_INDENT();
writer_put_str(tfc, "class ");
writer_put_str(tfc, sec_data.section_id);
writer_put_str(tfc, " ff-");
writer_put_str(tfc, section->name);
writer_put_str(tfc, "\n");
}
} else if (sec_data.current_is_stadium) {
writer_printf(tfc, "\"]):::ff-%s\n", section->name);
} else {
writer_printf(tfc, "\"):::ff-%s\n", section->name);
}
break;
case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
MM_INDENT();
writer_put_str(tfc, "}\n\n");
break;
}
mmc->indent_level--;
} else if ((section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH)) {
MM_INDENT();
writer_put_str(tfc, "end\n");
if (sec_data.section_id) {
MM_INDENT();
writer_put_str(tfc, "class ");
writer_put_str(tfc, sec_data.section_id);
writer_put_str(tfc, " ff-");
writer_put_str(tfc, section->name);
writer_put_str(tfc, "\n");
}
mmc->indent_level--;
}
if ((section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS))
if (sec_data.src_id && sec_data.dest_id
&& !has_link_pair(tfc, sec_data.src_id, sec_data.dest_id))
switch (mmc->diagram_config->diagram_type) {
case AV_DIAGRAMTYPE_GRAPH:
if (sec_data.section_type && mmc->enable_link_colors)
av_bprintf(&mmc->link_buf, "\n %s %s-%s-%s@==", sec_data.src_id, sec_data.section_type, sec_data.src_id, sec_data.dest_id);
else
av_bprintf(&mmc->link_buf, "\n %s ==", sec_data.src_id);
if (buf->len > 0) {
av_bprintf(&mmc->link_buf, " \"%s", buf->str);
for (unsigned i = 0; i < mmc->nb_link_captions[tfc->level]; i++)
av_bprintf(&mmc->link_buf, "<br>&nbsp;");
av_bprintf(&mmc->link_buf, "\" ==");
}
av_bprintf(&mmc->link_buf, "> %s", sec_data.dest_id);
break;
case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
av_bprintf(&mmc->link_buf, "\n %s", sec_data.src_id);
switch (sec_data.link_type) {
case AV_TEXTFORMAT_LINKTYPE_ONETOMANY:
av_bprintf(&mmc->link_buf, "%s", " ||--o{ ");
break;
case AV_TEXTFORMAT_LINKTYPE_MANYTOONE:
av_bprintf(&mmc->link_buf, "%s", " }o--|| ");
break;
case AV_TEXTFORMAT_LINKTYPE_ONETOONE:
av_bprintf(&mmc->link_buf, "%s", " ||--|| ");
break;
case AV_TEXTFORMAT_LINKTYPE_MANYTOMANY:
av_bprintf(&mmc->link_buf, "%s", " }o--o{ ");
break;
default:
av_bprintf(&mmc->link_buf, "%s", " ||--|| ");
break;
}
av_bprintf(&mmc->link_buf, "%s : \"\"", sec_data.dest_id);
break;
}
if (tfc->level == 0) {
writer_put_str(tfc, "\n");
if (mmc->create_html) {
char *token_pos = av_stristr(mmc->diagram_config->html_template, "__###__");
if (!token_pos) {
av_log(tfc, AV_LOG_ERROR, "Unable to locate the required token (__###__) in the html template.");
return;
}
token_pos += strlen("__###__");
writer_put_str(tfc, token_pos);
}
}
if (tfc->level == 1) {
if (mmc->link_buf.len > 0) {
writer_put_str(tfc, mmc->link_buf.str);
av_bprint_clear(&mmc->link_buf);
}
writer_put_str(tfc, "\n");
}
}
static void mermaid_print_value(AVTextFormatContext *tfc, const char *key,
const char *str, int64_t num, const int is_int)
{
MermaidContext *mmc = tfc->priv;
const AVTextFormatSection *section = tf_get_section(tfc, tfc->level);
if (!section)
return;
AVBPrint *buf = &tfc->section_pbuf[tfc->level];
struct section_data sec_data = mmc->section_data[tfc->level];
int exit = 0;
if (section->id_key && !strcmp(section->id_key, key)) {
set_str(&mmc->section_data[tfc->level].section_id, str);
exit = 1;
}
if (section->dest_id_key && !strcmp(section->dest_id_key, key)) {
set_str(&mmc->section_data[tfc->level].dest_id, str);
exit = 1;
}
if (section->src_id_key && !strcmp(section->src_id_key, key)) {
set_str(&mmc->section_data[tfc->level].src_id, str);
exit = 1;
}
if (section->linktype_key && !strcmp(section->linktype_key, key)) {
mmc->section_data[tfc->level].link_type = (AVTextFormatLinkType)num;
exit = 1;
}
if (exit)
return;
if ((section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_SHAPE | AV_TEXTFORMAT_SECTION_PRINT_TAGS))
|| (section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_SUBGRAPH && sec_data.subgraph_start_incomplete)) {
switch (mmc->diagram_config->diagram_type) {
case AV_DIAGRAMTYPE_GRAPH:
if (is_int) {
writer_printf(tfc, "<span class=\"%s\">%s: %"PRId64"</span>", key, key, num);
} else {
const char *tmp = av_strireplace(str, "\"", "'");
writer_printf(tfc, "<span class=\"%s\">%s</span>", key, tmp);
av_freep(&tmp);
}
break;
case AV_DIAGRAMTYPE_ENTITYRELATIONSHIP:
if (!is_int && str)
{
const char *col_type;
if (key[0] == '_')
return;
if (sec_data.section_id && !strcmp(str, sec_data.section_id))
col_type = "PK";
else if (sec_data.dest_id && !strcmp(str, sec_data.dest_id))
col_type = "FK";
else if (sec_data.src_id && !strcmp(str, sec_data.src_id))
col_type = "FK";
else
col_type = "";
MM_INDENT();
writer_printf(tfc, " %s %s %s\n", key, str, col_type);
}
break;
}
} else if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_LINKS) {
if (buf->len > 0)
av_bprintf(buf, "%s", "<br>");
av_bprintf(buf, "");
if (is_int)
av_bprintf(buf, "<span>%s: %"PRId64"</span>", key, num);
else
av_bprintf(buf, "<span>%s</span>", str);
mmc->nb_link_captions[tfc->level]++;
}
}
static inline void mermaid_print_str(AVTextFormatContext *tfc, const char *key, const char *value)
{
mermaid_print_value(tfc, key, value, 0, 0);
}
static void mermaid_print_int(AVTextFormatContext *tfc, const char *key, int64_t value)
{
mermaid_print_value(tfc, key, NULL, value, 1);
}
const AVTextFormatter avtextformatter_mermaid = {
.name = "mermaid",
.priv_size = sizeof(MermaidContext),
.init = mermaid_init,
.uninit = mermaid_uninit,
.print_section_header = mermaid_print_section_header,
.print_section_footer = mermaid_print_section_footer,
.print_integer = mermaid_print_int,
.print_string = mermaid_print_str,
.flags = AV_TEXTFORMAT_FLAG_IS_DIAGRAM_FORMATTER,
.priv_class = &mermaid_class,
};
const AVTextFormatter avtextformatter_mermaidhtml = {
.name = "mermaidhtml",
.priv_size = sizeof(MermaidContext),
.init = mermaid_init_html,
.uninit = mermaid_uninit,
.print_section_header = mermaid_print_section_header,
.print_section_footer = mermaid_print_section_footer,
.print_integer = mermaid_print_int,
.print_string = mermaid_print_str,
.flags = AV_TEXTFORMAT_FLAG_IS_DIAGRAM_FORMATTER,
.priv_class = &mermaid_class,
};
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_TEXTFORMAT_TF_MERMAID_H
#define FFTOOLS_TEXTFORMAT_TF_MERMAID_H
typedef enum {
AV_DIAGRAMTYPE_GRAPH,
AV_DIAGRAMTYPE_ENTITYRELATIONSHIP,
} AVDiagramType;
typedef struct AVDiagramConfig {
AVDiagramType diagram_type;
const char *diagram_css;
const char *html_template;
} AVDiagramConfig;
void av_diagram_init(AVTextFormatContext *tfc, AVDiagramConfig *diagram_config);
void av_mermaid_set_html_template(AVTextFormatContext *tfc, const char *html_template);
#endif /* FFTOOLS_TEXTFORMAT_TF_MERMAID_H */
+212
View File
@@ -0,0 +1,212 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <string.h>
#include "avtextformat.h"
#include "libavutil/bprint.h"
#include "libavutil/error.h"
#include "libavutil/opt.h"
#include "tf_internal.h"
/* XML output */
typedef struct XMLContext {
const AVClass *class;
int within_tag;
int indent_level;
int fully_qualified;
int xsd_strict;
} XMLContext;
#undef OFFSET
#define OFFSET(x) offsetof(XMLContext, x)
static const AVOption xml_options[] = {
{ "fully_qualified", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "q", "specify if the output should be fully qualified", OFFSET(fully_qualified), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "xsd_strict", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ "x", "ensure that the output is XSD compliant", OFFSET(xsd_strict), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1 },
{ NULL },
};
DEFINE_FORMATTER_CLASS(xml);
static av_cold int xml_init(AVTextFormatContext *wctx)
{
XMLContext *xml = wctx->priv;
if (xml->xsd_strict) {
xml->fully_qualified = 1;
#define CHECK_COMPLIANCE(opt, opt_name) \
if (opt) { \
av_log(wctx, AV_LOG_ERROR, \
"XSD-compliant output selected but option '%s' was selected, XML output may be non-compliant.\n" \
"You need to disable such option with '-no%s'\n", opt_name, opt_name); \
return AVERROR(EINVAL); \
}
////CHECK_COMPLIANCE(show_private_data, "private");
CHECK_COMPLIANCE(wctx->show_value_unit, "unit");
CHECK_COMPLIANCE(wctx->use_value_prefix, "prefix");
}
return 0;
}
#define XML_INDENT() writer_printf(wctx, "%*c", xml->indent_level * 4, ' ')
static void xml_print_section_header(AVTextFormatContext *wctx, const void *data)
{
XMLContext *xml = wctx->priv;
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
const AVTextFormatSection *parent_section = tf_get_parent_section(wctx, wctx->level);
if (!section)
return;
if (wctx->level == 0) {
const char *qual = " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
"xmlns:ffprobe=\"http://www.ffmpeg.org/schema/ffprobe\" "
"xsi:schemaLocation=\"http://www.ffmpeg.org/schema/ffprobe ffprobe.xsd\"";
writer_put_str(wctx, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writer_printf(wctx, "<%sffprobe%s>\n",
xml->fully_qualified ? "ffprobe:" : "",
xml->fully_qualified ? qual : "");
return;
}
if (xml->within_tag) {
xml->within_tag = 0;
writer_put_str(wctx, ">\n");
}
if (parent_section && (parent_section->flags & AV_TEXTFORMAT_SECTION_FLAG_IS_WRAPPER) &&
wctx->level && wctx->nb_item[wctx->level - 1])
writer_w8(wctx, '\n');
xml->indent_level++;
if (section->flags & (AV_TEXTFORMAT_SECTION_FLAG_IS_ARRAY | AV_TEXTFORMAT_SECTION_FLAG_HAS_VARIABLE_FIELDS)) {
XML_INDENT();
writer_printf(wctx, "<%s", section->name);
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_TYPE) {
AVBPrint buf;
av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
av_bprint_escape(&buf, section->get_type(data), NULL,
AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
writer_printf(wctx, " type=\"%s\"", buf.str);
}
writer_printf(wctx, ">\n", section->name);
} else {
XML_INDENT();
writer_printf(wctx, "<%s ", section->name);
xml->within_tag = 1;
}
}
static void xml_print_section_footer(AVTextFormatContext *wctx)
{
XMLContext *xml = wctx->priv;
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
if (!section)
return;
if (wctx->level == 0) {
writer_printf(wctx, "</%sffprobe>\n", xml->fully_qualified ? "ffprobe:" : "");
} else if (xml->within_tag) {
xml->within_tag = 0;
writer_put_str(wctx, "/>\n");
xml->indent_level--;
} else {
XML_INDENT();
writer_printf(wctx, "</%s>\n", section->name);
xml->indent_level--;
}
}
static void xml_print_value(AVTextFormatContext *wctx, const char *key,
const char *str, int64_t num, const int is_int)
{
AVBPrint buf;
XMLContext *xml = wctx->priv;
const AVTextFormatSection *section = tf_get_section(wctx, wctx->level);
if (!section)
return;
av_bprint_init(&buf, 1, AV_BPRINT_SIZE_UNLIMITED);
if (section->flags & AV_TEXTFORMAT_SECTION_FLAG_HAS_VARIABLE_FIELDS) {
xml->indent_level++;
XML_INDENT();
av_bprint_escape(&buf, key, NULL,
AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
writer_printf(wctx, "<%s key=\"%s\"",
section->element_name, buf.str);
av_bprint_clear(&buf);
if (is_int) {
writer_printf(wctx, " value=\"%"PRId64"\"/>\n", num);
} else {
av_bprint_escape(&buf, str, NULL,
AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
writer_printf(wctx, " value=\"%s\"/>\n", buf.str);
}
xml->indent_level--;
} else {
if (wctx->nb_item[wctx->level])
writer_w8(wctx, ' ');
if (is_int) {
writer_printf(wctx, "%s=\"%"PRId64"\"", key, num);
} else {
av_bprint_escape(&buf, str, NULL,
AV_ESCAPE_MODE_XML, AV_ESCAPE_FLAG_XML_DOUBLE_QUOTES);
writer_printf(wctx, "%s=\"%s\"", key, buf.str);
}
}
av_bprint_finalize(&buf, NULL);
}
static inline void xml_print_str(AVTextFormatContext *wctx, const char *key, const char *value)
{
xml_print_value(wctx, key, value, 0, 0);
}
static void xml_print_int(AVTextFormatContext *wctx, const char *key, int64_t value)
{
xml_print_value(wctx, key, NULL, value, 1);
}
const AVTextFormatter avtextformatter_xml = {
.name = "xml",
.priv_size = sizeof(XMLContext),
.init = xml_init,
.print_section_header = xml_print_section_header,
.print_section_footer = xml_print_section_footer,
.print_integer = xml_print_int,
.print_string = xml_print_str,
.flags = AV_TEXTFORMAT_FLAG_SUPPORTS_MIXED_ARRAY_CONTENT,
.priv_class = &xml_class,
};
+124
View File
@@ -0,0 +1,124 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include <string.h>
#include "avtextwriters.h"
#include "libavutil/avassert.h"
#include "libavutil/error.h"
/* AVIO Writer */
# define WRITER_NAME "aviowriter"
typedef struct IOWriterContext {
const AVClass *class;
AVIOContext *avio_context;
int close_on_uninit;
} IOWriterContext;
static av_cold int iowriter_uninit(AVTextWriterContext *wctx)
{
IOWriterContext *ctx = wctx->priv;
int ret = 0;
if (ctx->close_on_uninit)
ret = avio_closep(&ctx->avio_context);
return ret;
}
static void io_w8(AVTextWriterContext *wctx, int b)
{
IOWriterContext *ctx = wctx->priv;
avio_w8(ctx->avio_context, b);
}
static void io_put_str(AVTextWriterContext *wctx, const char *str)
{
IOWriterContext *ctx = wctx->priv;
avio_write(ctx->avio_context, (const unsigned char *)str, (int)strlen(str));
}
static void io_vprintf(AVTextWriterContext *wctx, const char *fmt, va_list vl)
{
IOWriterContext *ctx = wctx->priv;
avio_vprintf(ctx->avio_context, fmt, vl);
}
const AVTextWriter avtextwriter_avio = {
.name = WRITER_NAME,
.priv_size = sizeof(IOWriterContext),
.uninit = iowriter_uninit,
.writer_put_str = io_put_str,
.writer_vprintf = io_vprintf,
.writer_w8 = io_w8
};
int avtextwriter_create_file(AVTextWriterContext **pwctx, const char *output_filename)
{
IOWriterContext *ctx;
int ret;
if (!output_filename || !output_filename[0]) {
av_log(NULL, AV_LOG_ERROR, "The output_filename cannot be NULL or empty\n");
return AVERROR(EINVAL);
}
ret = avtextwriter_context_open(pwctx, &avtextwriter_avio);
if (ret < 0)
return ret;
ctx = (*pwctx)->priv;
if ((ret = avio_open(&ctx->avio_context, output_filename, AVIO_FLAG_WRITE)) < 0) {
av_log(ctx, AV_LOG_ERROR,
"Failed to open output '%s' with error: %s\n", output_filename, av_err2str(ret));
avtextwriter_context_close(pwctx);
return ret;
}
ctx->close_on_uninit = 1;
return ret;
}
int avtextwriter_create_avio(AVTextWriterContext **pwctx, AVIOContext *avio_ctx, int close_on_uninit)
{
IOWriterContext *ctx;
int ret;
av_assert0(avio_ctx);
ret = avtextwriter_context_open(pwctx, &avtextwriter_avio);
if (ret < 0)
return ret;
ctx = (*pwctx)->priv;
ctx->avio_context = avio_ctx;
ctx->close_on_uninit = close_on_uninit;
return ret;
}
+89
View File
@@ -0,0 +1,89 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <limits.h>
#include <stdarg.h>
#include "avtextwriters.h"
#include "libavutil/opt.h"
#include "libavutil/bprint.h"
/* Buffer Writer */
# define WRITER_NAME "bufferwriter"
typedef struct BufferWriterContext {
const AVClass *class;
AVBPrint *buffer;
} BufferWriterContext;
static const char *bufferwriter_get_name(void *ctx)
{
return WRITER_NAME;
}
static const AVClass bufferwriter_class = {
.class_name = WRITER_NAME,
.item_name = bufferwriter_get_name,
};
static void buffer_w8(AVTextWriterContext *wctx, int b)
{
BufferWriterContext *ctx = wctx->priv;
av_bprintf(ctx->buffer, "%c", b);
}
static void buffer_put_str(AVTextWriterContext *wctx, const char *str)
{
BufferWriterContext *ctx = wctx->priv;
av_bprintf(ctx->buffer, "%s", str);
}
static void buffer_vprintf(AVTextWriterContext *wctx, const char *fmt, va_list vl)
{
BufferWriterContext *ctx = wctx->priv;
av_vbprintf(ctx->buffer, fmt, vl);
}
const AVTextWriter avtextwriter_buffer = {
.name = WRITER_NAME,
.priv_size = sizeof(BufferWriterContext),
.priv_class = &bufferwriter_class,
.writer_put_str = buffer_put_str,
.writer_vprintf = buffer_vprintf,
.writer_w8 = buffer_w8
};
int avtextwriter_create_buffer(AVTextWriterContext **pwctx, AVBPrint *buffer)
{
BufferWriterContext *ctx;
int ret;
ret = avtextwriter_context_open(pwctx, &avtextwriter_buffer);
if (ret < 0)
return ret;
ctx = (*pwctx)->priv;
ctx->buffer = buffer;
return ret;
}
+78
View File
@@ -0,0 +1,78 @@
/*
* Copyright (c) The FFmpeg developers
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "avtextwriters.h"
#include "libavutil/opt.h"
/* STDOUT Writer */
# define WRITER_NAME "stdoutwriter"
typedef struct StdOutWriterContext {
const AVClass *class;
} StdOutWriterContext;
static const char *stdoutwriter_get_name(void *ctx)
{
return WRITER_NAME;
}
static const AVClass stdoutwriter_class = {
.class_name = WRITER_NAME,
.item_name = stdoutwriter_get_name,
};
static inline void stdout_w8(AVTextWriterContext *wctx, int b)
{
printf("%c", b);
}
static inline void stdout_put_str(AVTextWriterContext *wctx, const char *str)
{
printf("%s", str);
}
static inline void stdout_vprintf(AVTextWriterContext *wctx, const char *fmt, va_list vl)
{
vprintf(fmt, vl);
}
static const AVTextWriter avtextwriter_stdout = {
.name = WRITER_NAME,
.priv_size = sizeof(StdOutWriterContext),
.priv_class = &stdoutwriter_class,
.writer_put_str = stdout_put_str,
.writer_vprintf = stdout_vprintf,
.writer_w8 = stdout_w8
};
int avtextwriter_create_stdout(AVTextWriterContext **pwctx)
{
int ret;
ret = avtextwriter_context_open(pwctx, &avtextwriter_stdout);
return ret;
}
+251
View File
@@ -0,0 +1,251 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <string.h>
#include "libavutil/avassert.h"
#include "libavutil/container_fifo.h"
#include "libavutil/error.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
#include "libavutil/thread.h"
#include "libavcodec/packet.h"
#include "thread_queue.h"
enum {
FINISHED_SEND = (1 << 0),
FINISHED_RECV = (1 << 1),
};
struct ThreadQueue {
int *finished;
unsigned int nb_streams;
enum ThreadQueueType type;
AVContainerFifo *fifo;
AVFifo *fifo_stream_index;
pthread_mutex_t lock;
pthread_cond_t cond;
};
void tq_free(ThreadQueue **ptq)
{
ThreadQueue *tq = *ptq;
if (!tq)
return;
av_container_fifo_free(&tq->fifo);
av_fifo_freep2(&tq->fifo_stream_index);
av_freep(&tq->finished);
pthread_cond_destroy(&tq->cond);
pthread_mutex_destroy(&tq->lock);
av_freep(ptq);
}
ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
enum ThreadQueueType type)
{
ThreadQueue *tq;
int ret;
tq = av_mallocz(sizeof(*tq));
if (!tq)
return NULL;
ret = pthread_cond_init(&tq->cond, NULL);
if (ret) {
av_freep(&tq);
return NULL;
}
ret = pthread_mutex_init(&tq->lock, NULL);
if (ret) {
pthread_cond_destroy(&tq->cond);
av_freep(&tq);
return NULL;
}
tq->finished = av_calloc(nb_streams, sizeof(*tq->finished));
if (!tq->finished)
goto fail;
tq->nb_streams = nb_streams;
tq->type = type;
tq->fifo = (type == THREAD_QUEUE_FRAMES) ?
av_container_fifo_alloc_avframe(0) : av_container_fifo_alloc_avpacket(0);
if (!tq->fifo)
goto fail;
tq->fifo_stream_index = av_fifo_alloc2(queue_size, sizeof(unsigned), 0);
if (!tq->fifo_stream_index)
goto fail;
return tq;
fail:
tq_free(&tq);
return NULL;
}
int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data)
{
int *finished;
int ret;
av_assert0(stream_idx < tq->nb_streams);
finished = &tq->finished[stream_idx];
pthread_mutex_lock(&tq->lock);
if (*finished & FINISHED_SEND) {
ret = AVERROR(EINVAL);
goto finish;
}
while (!(*finished & FINISHED_RECV) && !av_fifo_can_write(tq->fifo_stream_index))
pthread_cond_wait(&tq->cond, &tq->lock);
if (*finished & FINISHED_RECV) {
ret = AVERROR_EOF;
*finished |= FINISHED_SEND;
} else {
ret = av_fifo_write(tq->fifo_stream_index, &stream_idx, 1);
if (ret < 0)
goto finish;
ret = av_container_fifo_write(tq->fifo, data, 0);
if (ret < 0)
goto finish;
pthread_cond_broadcast(&tq->cond);
}
finish:
pthread_mutex_unlock(&tq->lock);
return ret;
}
static int receive_locked(ThreadQueue *tq, int *stream_idx,
void *data)
{
unsigned int nb_finished = 0;
while (av_container_fifo_read(tq->fifo, data, 0) >= 0) {
unsigned idx;
int ret;
ret = av_fifo_read(tq->fifo_stream_index, &idx, 1);
av_assert0(ret >= 0);
if (tq->finished[idx] & FINISHED_RECV) {
(tq->type == THREAD_QUEUE_FRAMES) ?
av_frame_unref(data) : av_packet_unref(data);
continue;
}
*stream_idx = idx;
return 0;
}
for (unsigned int i = 0; i < tq->nb_streams; i++) {
if (!tq->finished[i])
continue;
/* return EOF to the consumer at most once for each stream */
if (!(tq->finished[i] & FINISHED_RECV)) {
tq->finished[i] |= FINISHED_RECV;
*stream_idx = i;
return AVERROR_EOF;
}
nb_finished++;
}
return nb_finished == tq->nb_streams ? AVERROR_EOF : AVERROR(EAGAIN);
}
int tq_receive(ThreadQueue *tq, int *stream_idx, void *data)
{
int ret;
*stream_idx = -1;
pthread_mutex_lock(&tq->lock);
while (1) {
size_t can_read = av_container_fifo_can_read(tq->fifo);
ret = receive_locked(tq, stream_idx, data);
// signal other threads if the fifo state changed
if (can_read != av_container_fifo_can_read(tq->fifo))
pthread_cond_broadcast(&tq->cond);
if (ret == AVERROR(EAGAIN)) {
pthread_cond_wait(&tq->cond, &tq->lock);
continue;
}
break;
}
pthread_mutex_unlock(&tq->lock);
return ret;
}
void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx)
{
av_assert0(stream_idx < tq->nb_streams);
pthread_mutex_lock(&tq->lock);
/* mark the stream as send-finished;
* next time the consumer thread tries to read this stream it will get
* an EOF and recv-finished flag will be set */
tq->finished[stream_idx] |= FINISHED_SEND;
pthread_cond_broadcast(&tq->cond);
pthread_mutex_unlock(&tq->lock);
}
void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx)
{
av_assert0(stream_idx < tq->nb_streams);
pthread_mutex_lock(&tq->lock);
/* mark the stream as recv-finished;
* next time the producer thread tries to send for this stream, it will
* get an EOF and send-finished flag will be set */
tq->finished[stream_idx] |= FINISHED_RECV;
pthread_cond_broadcast(&tq->cond);
pthread_mutex_unlock(&tq->lock);
}
+81
View File
@@ -0,0 +1,81 @@
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef FFTOOLS_THREAD_QUEUE_H
#define FFTOOLS_THREAD_QUEUE_H
#include <string.h>
enum ThreadQueueType {
THREAD_QUEUE_FRAMES,
THREAD_QUEUE_PACKETS,
};
typedef struct ThreadQueue ThreadQueue;
/**
* Allocate a queue for sending data between threads.
*
* @param nb_streams number of streams for which a distinct EOF state is
* maintained
* @param queue_size number of items that can be stored in the queue without
* blocking
*/
ThreadQueue *tq_alloc(unsigned int nb_streams, size_t queue_size,
enum ThreadQueueType type);
void tq_free(ThreadQueue **tq);
/**
* Send an item for the given stream to the queue.
*
* @param data the item to send, its contents will be moved using the callback
* provided to tq_alloc(); on failure the item will be left
* untouched
* @return
* - 0 the item was successfully sent
* - AVERROR(ENOMEM) could not allocate an item for writing to the FIFO
* - AVERROR(EINVAL) the sending side has previously been marked as finished
* - AVERROR_EOF the receiving side has marked the given stream as finished
*/
int tq_send(ThreadQueue *tq, unsigned int stream_idx, void *data);
/**
* Mark the given stream finished from the sending side.
*/
void tq_send_finish(ThreadQueue *tq, unsigned int stream_idx);
/**
* Read the next item from the queue.
*
* @param stream_idx the index of the stream that was processed or -1 will be
* written here
* @param data the data item will be written here on success using the
* callback provided to tq_alloc()
* @return
* - 0 a data item was successfully read; *stream_idx contains a non-negative
* stream index
* - AVERROR_EOF When *stream_idx is non-negative, this signals that the sending
* side has marked the given stream as finished. This will happen at most once
* for each stream. When *stream_idx is -1, all streams are done.
*/
int tq_receive(ThreadQueue *tq, int *stream_idx, void *data);
/**
* Mark the given stream finished from the receiving side.
*/
void tq_receive_finish(ThreadQueue *tq, unsigned int stream_idx);
#endif // FFTOOLS_THREAD_QUEUE_H