2012-08-02 09:24:02 +03:00
|
|
|
/*
|
2012-08-09 01:26:38 +03:00
|
|
|
* ffmpeg filter configuration
|
2012-08-02 09:24:02 +03:00
|
|
|
*
|
2012-08-09 01:26:38 +03:00
|
|
|
* This file is part of FFmpeg.
|
2012-08-02 09:24:02 +03:00
|
|
|
*
|
2012-08-09 01:26:38 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-08-02 09:24:02 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2012-08-09 01:26:38 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-08-02 09:24:02 +03:00
|
|
|
* 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
|
2012-08-09 01:26:38 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-08-02 09:24:02 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2013-11-23 23:32:55 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
#include "ffmpeg.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
#include "libavfilter/avfilter.h"
|
2012-08-09 01:26:38 +03:00
|
|
|
#include "libavfilter/buffersink.h"
|
2016-02-12 10:22:15 +02:00
|
|
|
#include "libavfilter/buffersrc.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
#include "libavutil/avassert.h"
|
2012-08-09 01:26:38 +03:00
|
|
|
#include "libavutil/avstring.h"
|
|
|
|
#include "libavutil/bprint.h"
|
2012-11-10 18:00:00 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2024-03-25 02:30:37 +02:00
|
|
|
#include "libavutil/mem.h"
|
2012-11-23 17:35:48 +03:00
|
|
|
#include "libavutil/opt.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
#include "libavutil/pixdesc.h"
|
|
|
|
#include "libavutil/pixfmt.h"
|
|
|
|
#include "libavutil/samplefmt.h"
|
2023-11-24 20:08:15 +02:00
|
|
|
#include "libavutil/time.h"
|
2023-04-18 15:24:46 +02:00
|
|
|
#include "libavutil/timestamp.h"
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
// FIXME private header, used for mid_pred()
|
|
|
|
#include "libavcodec/mathops.h"
|
|
|
|
|
2023-04-18 19:44:15 +02:00
|
|
|
typedef struct FilterGraphPriv {
|
2024-02-14 20:08:18 +02:00
|
|
|
FilterGraph fg;
|
2023-04-18 19:44:15 +02:00
|
|
|
|
2023-06-08 20:58:16 +02:00
|
|
|
// name used for logging
|
2024-02-14 20:08:18 +02:00
|
|
|
char log_name[32];
|
2023-06-08 20:58:16 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
int is_simple;
|
2023-05-27 17:38:36 +02:00
|
|
|
// true when the filtergraph contains only meta filters
|
|
|
|
// that do not modify the frame data
|
2024-02-14 20:08:18 +02:00
|
|
|
int is_meta;
|
2023-07-18 16:37:52 +02:00
|
|
|
// source filters are present in the graph
|
2024-02-14 20:08:18 +02:00
|
|
|
int have_sources;
|
|
|
|
int disable_conversions;
|
2023-05-21 19:09:01 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
unsigned nb_outputs_done;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
const char *graph_desc;
|
2023-04-27 15:47:04 +02:00
|
|
|
|
2024-04-04 11:30:18 +02:00
|
|
|
char *nb_threads;
|
|
|
|
|
2023-04-18 19:44:15 +02:00
|
|
|
// frame for temporarily holding output from the filtergraph
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFrame *frame;
|
2023-07-24 12:32:48 +02:00
|
|
|
// frame for sending output to the encoder
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFrame *frame_enc;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-05-18 16:56:15 +02:00
|
|
|
Scheduler *sch;
|
|
|
|
unsigned sch_idx;
|
2023-04-18 19:44:15 +02:00
|
|
|
} FilterGraphPriv;
|
|
|
|
|
|
|
|
static FilterGraphPriv *fgp_from_fg(FilterGraph *fg)
|
|
|
|
{
|
|
|
|
return (FilterGraphPriv*)fg;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:39:47 +02:00
|
|
|
static const FilterGraphPriv *cfgp_from_cfg(const FilterGraph *fg)
|
|
|
|
{
|
|
|
|
return (const FilterGraphPriv*)fg;
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
// data that is local to the filter thread and not visible outside of it
|
|
|
|
typedef struct FilterGraphThread {
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFilterGraph *graph;
|
2023-12-13 19:26:21 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFrame *frame;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
|
|
|
// Temporary buffer for output frames, since on filtergraph reset
|
|
|
|
// we cannot send them to encoders immediately.
|
|
|
|
// The output index is stored in frame opaque.
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFifo *frame_queue_out;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
// index of the next input to request from the scheduler
|
2024-02-14 20:08:18 +02:00
|
|
|
unsigned next_in;
|
2023-07-18 16:37:52 +02:00
|
|
|
// set to 1 after at least one frame passed through this output
|
2024-02-14 20:08:18 +02:00
|
|
|
int got_frame;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
|
|
|
// EOF status of each input/output, as received by the thread
|
2024-02-14 20:08:18 +02:00
|
|
|
uint8_t *eof_in;
|
|
|
|
uint8_t *eof_out;
|
2023-06-08 20:36:33 +02:00
|
|
|
} FilterGraphThread;
|
|
|
|
|
2023-04-25 23:44:49 +02:00
|
|
|
typedef struct InputFilterPriv {
|
2024-02-14 20:08:18 +02:00
|
|
|
InputFilter ifilter;
|
2023-04-25 23:44:49 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
InputFilterOptions opts;
|
2024-02-13 13:01:11 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
int index;
|
2023-11-03 16:24:36 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFilterContext *filter;
|
2023-05-24 12:35:50 +02:00
|
|
|
|
2023-05-10 09:59:13 +02:00
|
|
|
// used to hold submitted input
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFrame *frame;
|
2023-05-10 09:59:13 +02:00
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
/* for filters that are not yet bound to an input stream,
|
|
|
|
* this stores the input linklabel, if any */
|
2024-02-14 20:08:18 +02:00
|
|
|
uint8_t *linklabel;
|
2023-05-21 22:18:57 +02:00
|
|
|
|
2023-05-13 19:59:45 +02:00
|
|
|
// filter data type
|
2024-02-14 20:08:18 +02:00
|
|
|
enum AVMediaType type;
|
2023-05-13 19:59:45 +02:00
|
|
|
// source data type: AVMEDIA_TYPE_SUBTITLE for sub2video,
|
|
|
|
// same as type otherwise
|
2024-02-14 20:08:18 +02:00
|
|
|
enum AVMediaType type_src;
|
2023-05-13 10:29:01 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
int eof;
|
2024-02-13 17:44:32 +02:00
|
|
|
int bound;
|
2023-04-30 12:24:50 +02:00
|
|
|
|
2023-05-13 10:05:02 +02:00
|
|
|
// parameters configured for this input
|
2024-02-14 20:08:18 +02:00
|
|
|
int format;
|
2023-05-13 10:05:02 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
int width, height;
|
|
|
|
AVRational sample_aspect_ratio;
|
|
|
|
enum AVColorSpace color_space;
|
|
|
|
enum AVColorRange color_range;
|
2023-05-13 10:05:02 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
int sample_rate;
|
|
|
|
AVChannelLayout ch_layout;
|
2023-05-13 10:05:02 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
AVRational time_base;
|
2023-04-25 23:55:44 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFifo *frame_queue;
|
2023-04-30 12:17:16 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
AVBufferRef *hw_frames_ctx;
|
2023-04-30 12:26:51 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
int displaymatrix_present;
|
2024-03-22 21:01:06 +02:00
|
|
|
int displaymatrix_applied;
|
2024-02-14 20:08:18 +02:00
|
|
|
int32_t displaymatrix[9];
|
2023-04-30 12:26:51 +02:00
|
|
|
|
2023-05-23 13:07:57 +02:00
|
|
|
struct {
|
2023-05-24 10:08:33 +02:00
|
|
|
AVFrame *frame;
|
|
|
|
|
|
|
|
int64_t last_pts;
|
|
|
|
int64_t end_pts;
|
|
|
|
|
|
|
|
///< marks if sub2video_update should force an initialization
|
|
|
|
unsigned int initialize;
|
2023-05-23 13:07:57 +02:00
|
|
|
} sub2video;
|
2023-04-25 23:44:49 +02:00
|
|
|
} InputFilterPriv;
|
|
|
|
|
|
|
|
static InputFilterPriv *ifp_from_ifilter(InputFilter *ifilter)
|
|
|
|
{
|
|
|
|
return (InputFilterPriv*)ifilter;
|
|
|
|
}
|
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
typedef struct FPSConvContext {
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFrame *last_frame;
|
2023-07-24 12:32:48 +02:00
|
|
|
/* number of frames emitted by the video-encoding sync code */
|
2024-02-14 20:08:18 +02:00
|
|
|
int64_t frame_number;
|
2023-07-24 12:32:48 +02:00
|
|
|
/* history of nb_frames_prev, i.e. the number of times the
|
|
|
|
* previous frame was duplicated by vsync code in recent
|
|
|
|
* do_video_out() calls */
|
2024-02-14 20:08:18 +02:00
|
|
|
int64_t frames_prev_hist[3];
|
2023-07-24 12:32:48 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
uint64_t dup_warning;
|
2023-07-24 12:32:48 +02:00
|
|
|
|
|
|
|
int last_dropped;
|
|
|
|
int dropped_keyframe;
|
|
|
|
|
2024-04-01 06:29:16 +02:00
|
|
|
enum VideoSyncMethod vsync_method;
|
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
AVRational framerate;
|
|
|
|
AVRational framerate_max;
|
|
|
|
const AVRational *framerate_supported;
|
|
|
|
int framerate_clip;
|
|
|
|
} FPSConvContext;
|
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
typedef struct OutputFilterPriv {
|
2024-02-14 20:08:18 +02:00
|
|
|
OutputFilter ofilter;
|
2023-07-06 17:09:59 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
int index;
|
2023-11-03 16:24:36 +02:00
|
|
|
|
2024-04-02 13:40:42 +02:00
|
|
|
void *log_parent;
|
|
|
|
char log_name[32];
|
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
char *name;
|
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
AVFilterContext *filter;
|
2023-05-27 17:58:35 +02:00
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
/* desired output stream properties */
|
2024-02-14 20:08:18 +02:00
|
|
|
int format;
|
|
|
|
int width, height;
|
|
|
|
int sample_rate;
|
|
|
|
AVChannelLayout ch_layout;
|
2024-04-08 13:58:19 +02:00
|
|
|
enum AVColorSpace color_space;
|
|
|
|
enum AVColorRange color_range;
|
2023-07-06 17:09:59 +02:00
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
// time base in which the output is sent to our downstream
|
|
|
|
// does not need to match the filtersink's timebase
|
2024-02-14 20:08:18 +02:00
|
|
|
AVRational tb_out;
|
2023-07-24 12:32:48 +02:00
|
|
|
// at least one frame with the above timebase was sent
|
|
|
|
// to our downstream, so it cannot change anymore
|
2024-02-14 20:08:18 +02:00
|
|
|
int tb_out_locked;
|
2023-07-24 12:32:48 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
AVRational sample_aspect_ratio;
|
2023-07-06 21:31:49 +02:00
|
|
|
|
2024-04-02 13:58:59 +02:00
|
|
|
AVDictionary *sws_opts;
|
|
|
|
AVDictionary *swr_opts;
|
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
// those are only set if no format is specified and the encoder gives us multiple options
|
|
|
|
// They point directly to the relevant lists of the encoder.
|
2024-02-14 20:08:18 +02:00
|
|
|
const int *formats;
|
|
|
|
const AVChannelLayout *ch_layouts;
|
|
|
|
const int *sample_rates;
|
2024-04-08 13:58:19 +02:00
|
|
|
const enum AVColorSpace *color_spaces;
|
|
|
|
const enum AVColorRange *color_ranges;
|
2023-07-06 21:31:49 +02:00
|
|
|
|
2024-02-14 20:08:18 +02:00
|
|
|
AVRational enc_timebase;
|
2024-04-02 13:58:59 +02:00
|
|
|
int64_t trim_start_us;
|
|
|
|
int64_t trim_duration_us;
|
2023-07-24 12:32:48 +02:00
|
|
|
// offset for output timestamps, in AV_TIME_BASE_Q
|
2024-02-14 20:08:18 +02:00
|
|
|
int64_t ts_offset;
|
|
|
|
int64_t next_pts;
|
|
|
|
FPSConvContext fps;
|
2024-04-01 06:29:16 +02:00
|
|
|
|
|
|
|
unsigned flags;
|
2023-07-06 17:09:59 +02:00
|
|
|
} OutputFilterPriv;
|
|
|
|
|
|
|
|
static OutputFilterPriv *ofp_from_ofilter(OutputFilter *ofilter)
|
|
|
|
{
|
|
|
|
return (OutputFilterPriv*)ofilter;
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
typedef struct FilterCommand {
|
|
|
|
char *target;
|
|
|
|
char *command;
|
|
|
|
char *arg;
|
|
|
|
|
|
|
|
double time;
|
|
|
|
int all_filters;
|
|
|
|
} FilterCommand;
|
|
|
|
|
|
|
|
static void filter_command_free(void *opaque, uint8_t *data)
|
|
|
|
{
|
|
|
|
FilterCommand *fc = (FilterCommand*)data;
|
|
|
|
|
|
|
|
av_freep(&fc->target);
|
|
|
|
av_freep(&fc->command);
|
|
|
|
av_freep(&fc->arg);
|
|
|
|
|
|
|
|
av_free(data);
|
|
|
|
}
|
2023-06-09 19:16:29 +02:00
|
|
|
|
2023-05-24 10:08:33 +02:00
|
|
|
static int sub2video_get_blank_frame(InputFilterPriv *ifp)
|
|
|
|
{
|
|
|
|
AVFrame *frame = ifp->sub2video.frame;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
av_frame_unref(frame);
|
|
|
|
|
|
|
|
frame->width = ifp->width;
|
|
|
|
frame->height = ifp->height;
|
|
|
|
frame->format = ifp->format;
|
2023-10-20 17:14:20 +02:00
|
|
|
frame->colorspace = ifp->color_space;
|
|
|
|
frame->color_range = ifp->color_range;
|
2023-05-24 10:08:33 +02:00
|
|
|
|
|
|
|
ret = av_frame_get_buffer(frame, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
memset(frame->data[0], 0, frame->height * frame->linesize[0]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sub2video_copy_rect(uint8_t *dst, int dst_linesize, int w, int h,
|
|
|
|
AVSubtitleRect *r)
|
|
|
|
{
|
|
|
|
uint32_t *pal, *dst2;
|
|
|
|
uint8_t *src, *src2;
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
if (r->type != SUBTITLE_BITMAP) {
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "sub2video: non-bitmap subtitle\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (r->x < 0 || r->x + r->w > w || r->y < 0 || r->y + r->h > h) {
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "sub2video: rectangle (%d %d %d %d) overflowing %d %d\n",
|
|
|
|
r->x, r->y, r->w, r->h, w, h
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dst += r->y * dst_linesize + r->x * 4;
|
|
|
|
src = r->data[0];
|
|
|
|
pal = (uint32_t *)r->data[1];
|
|
|
|
for (y = 0; y < r->h; y++) {
|
|
|
|
dst2 = (uint32_t *)dst;
|
|
|
|
src2 = src;
|
|
|
|
for (x = 0; x < r->w; x++)
|
|
|
|
*(dst2++) = pal[*(src2++)];
|
|
|
|
dst += dst_linesize;
|
|
|
|
src += r->linesize[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sub2video_push_ref(InputFilterPriv *ifp, int64_t pts)
|
|
|
|
{
|
|
|
|
AVFrame *frame = ifp->sub2video.frame;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
av_assert1(frame->data[0]);
|
|
|
|
ifp->sub2video.last_pts = frame->pts = pts;
|
2023-05-24 12:35:50 +02:00
|
|
|
ret = av_buffersrc_add_frame_flags(ifp->filter, frame,
|
2023-05-24 10:08:33 +02:00
|
|
|
AV_BUFFERSRC_FLAG_KEEP_REF |
|
|
|
|
AV_BUFFERSRC_FLAG_PUSH);
|
|
|
|
if (ret != AVERROR_EOF && ret < 0)
|
|
|
|
av_log(NULL, AV_LOG_WARNING, "Error while add the frame to buffer source(%s).\n",
|
|
|
|
av_err2str(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sub2video_update(InputFilterPriv *ifp, int64_t heartbeat_pts,
|
|
|
|
const AVSubtitle *sub)
|
|
|
|
{
|
|
|
|
AVFrame *frame = ifp->sub2video.frame;
|
|
|
|
int8_t *dst;
|
|
|
|
int dst_linesize;
|
2024-02-19 11:43:24 +02:00
|
|
|
int num_rects;
|
2023-05-24 10:08:33 +02:00
|
|
|
int64_t pts, end_pts;
|
|
|
|
|
|
|
|
if (sub) {
|
|
|
|
pts = av_rescale_q(sub->pts + sub->start_display_time * 1000LL,
|
|
|
|
AV_TIME_BASE_Q, ifp->time_base);
|
|
|
|
end_pts = av_rescale_q(sub->pts + sub->end_display_time * 1000LL,
|
|
|
|
AV_TIME_BASE_Q, ifp->time_base);
|
|
|
|
num_rects = sub->num_rects;
|
|
|
|
} else {
|
|
|
|
/* If we are initializing the system, utilize current heartbeat
|
|
|
|
PTS as the start time, and show until the following subpicture
|
|
|
|
is received. Otherwise, utilize the previous subpicture's end time
|
|
|
|
as the fall-back value. */
|
|
|
|
pts = ifp->sub2video.initialize ?
|
|
|
|
heartbeat_pts : ifp->sub2video.end_pts;
|
|
|
|
end_pts = INT64_MAX;
|
|
|
|
num_rects = 0;
|
|
|
|
}
|
|
|
|
if (sub2video_get_blank_frame(ifp) < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Impossible to get a blank canvas.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dst = frame->data [0];
|
|
|
|
dst_linesize = frame->linesize[0];
|
2024-02-19 11:43:24 +02:00
|
|
|
for (int i = 0; i < num_rects; i++)
|
2023-05-24 10:08:33 +02:00
|
|
|
sub2video_copy_rect(dst, dst_linesize, frame->width, frame->height, sub->rects[i]);
|
|
|
|
sub2video_push_ref(ifp, pts);
|
|
|
|
ifp->sub2video.end_pts = end_pts;
|
|
|
|
ifp->sub2video.initialize = 0;
|
|
|
|
}
|
|
|
|
|
2021-04-06 05:26:31 +02:00
|
|
|
/* Define a function for appending a list of allowed formats
|
|
|
|
* to an AVBPrint. If nonempty, the list will have a header. */
|
|
|
|
#define DEF_CHOOSE_FORMAT(name, type, var, supported_list, none, printf_format, get_name) \
|
2023-07-06 17:09:59 +02:00
|
|
|
static void choose_ ## name (OutputFilterPriv *ofp, AVBPrint *bprint) \
|
2012-08-02 09:24:02 +03:00
|
|
|
{ \
|
2023-07-06 17:09:59 +02:00
|
|
|
if (ofp->var == none && !ofp->supported_list) \
|
2021-04-06 05:26:31 +02:00
|
|
|
return; \
|
|
|
|
av_bprintf(bprint, #name "="); \
|
2023-07-06 17:09:59 +02:00
|
|
|
if (ofp->var != none) { \
|
|
|
|
av_bprintf(bprint, printf_format, get_name(ofp->var)); \
|
2021-04-06 05:26:31 +02:00
|
|
|
} else { \
|
2012-08-02 09:24:02 +03:00
|
|
|
const type *p; \
|
|
|
|
\
|
2023-07-06 17:09:59 +02:00
|
|
|
for (p = ofp->supported_list; *p != none; p++) { \
|
2021-04-06 05:26:31 +02:00
|
|
|
av_bprintf(bprint, printf_format "|", get_name(*p)); \
|
2012-08-02 09:24:02 +03:00
|
|
|
} \
|
2021-04-06 05:26:31 +02:00
|
|
|
if (bprint->len > 0) \
|
|
|
|
bprint->str[--bprint->len] = '\0'; \
|
|
|
|
} \
|
|
|
|
av_bprint_chars(bprint, ':', 1); \
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2023-10-25 12:39:29 +02:00
|
|
|
DEF_CHOOSE_FORMAT(pix_fmts, enum AVPixelFormat, format, formats,
|
|
|
|
AV_PIX_FMT_NONE, "%s", av_get_pix_fmt_name)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
DEF_CHOOSE_FORMAT(sample_fmts, enum AVSampleFormat, format, formats,
|
2021-04-06 05:26:31 +02:00
|
|
|
AV_SAMPLE_FMT_NONE, "%s", av_get_sample_fmt_name)
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
DEF_CHOOSE_FORMAT(sample_rates, int, sample_rate, sample_rates, 0,
|
2021-04-06 05:26:31 +02:00
|
|
|
"%d", )
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2024-04-08 13:58:19 +02:00
|
|
|
DEF_CHOOSE_FORMAT(color_spaces, enum AVColorSpace, color_space, color_spaces,
|
|
|
|
AVCOL_SPC_UNSPECIFIED, "%s", av_color_space_name);
|
|
|
|
|
|
|
|
DEF_CHOOSE_FORMAT(color_ranges, enum AVColorRange, color_range, color_ranges,
|
|
|
|
AVCOL_RANGE_UNSPECIFIED, "%s", av_color_range_name);
|
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
static void choose_channel_layouts(OutputFilterPriv *ofp, AVBPrint *bprint)
|
2021-09-03 02:04:30 +02:00
|
|
|
{
|
2023-07-06 17:09:59 +02:00
|
|
|
if (av_channel_layout_check(&ofp->ch_layout)) {
|
2021-09-03 02:04:30 +02:00
|
|
|
av_bprintf(bprint, "channel_layouts=");
|
2023-07-06 17:09:59 +02:00
|
|
|
av_channel_layout_describe_bprint(&ofp->ch_layout, bprint);
|
2023-07-06 17:09:59 +02:00
|
|
|
} else if (ofp->ch_layouts) {
|
2021-09-03 02:04:30 +02:00
|
|
|
const AVChannelLayout *p;
|
|
|
|
|
|
|
|
av_bprintf(bprint, "channel_layouts=");
|
2023-07-06 17:09:59 +02:00
|
|
|
for (p = ofp->ch_layouts; p->nb_channels; p++) {
|
2021-09-03 02:04:30 +02:00
|
|
|
av_channel_layout_describe_bprint(p, bprint);
|
|
|
|
av_bprintf(bprint, "|");
|
|
|
|
}
|
|
|
|
if (bprint->len > 0)
|
|
|
|
bprint->str[--bprint->len] = '\0';
|
|
|
|
} else
|
|
|
|
return;
|
|
|
|
av_bprint_chars(bprint, ':', 1);
|
|
|
|
}
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-05-21 20:29:59 +02:00
|
|
|
static int read_binary(const char *path, uint8_t **data, int *len)
|
|
|
|
{
|
|
|
|
AVIOContext *io = NULL;
|
|
|
|
int64_t fsize;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*data = NULL;
|
|
|
|
*len = 0;
|
|
|
|
|
|
|
|
ret = avio_open2(&io, path, AVIO_FLAG_READ, &int_cb, NULL);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot open file '%s': %s\n",
|
|
|
|
path, av_err2str(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fsize = avio_size(io);
|
|
|
|
if (fsize < 0 || fsize > INT_MAX) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Cannot obtain size of file %s\n", path);
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data = av_malloc(fsize);
|
|
|
|
if (!*data) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avio_read(io, *data, fsize);
|
|
|
|
if (ret != fsize) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error reading file %s\n", path);
|
|
|
|
ret = ret < 0 ? ret : AVERROR(EIO);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = fsize;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
avio_close(io);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(data);
|
|
|
|
*len = 0;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int filter_opt_apply(AVFilterContext *f, const char *key, const char *val)
|
|
|
|
{
|
|
|
|
const AVOption *o = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = av_opt_set(f, key, val, AV_OPT_SEARCH_CHILDREN);
|
|
|
|
if (ret >= 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ret == AVERROR_OPTION_NOT_FOUND && key[0] == '/')
|
|
|
|
o = av_opt_find(f, key + 1, NULL, 0, AV_OPT_SEARCH_CHILDREN);
|
|
|
|
if (!o)
|
|
|
|
goto err_apply;
|
|
|
|
|
|
|
|
// key is a valid option name prefixed with '/'
|
|
|
|
// interpret value as a path from which to load the actual option value
|
|
|
|
key++;
|
|
|
|
|
|
|
|
if (o->type == AV_OPT_TYPE_BINARY) {
|
|
|
|
uint8_t *data;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
ret = read_binary(val, &data, &len);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err_load;
|
|
|
|
|
|
|
|
ret = av_opt_set_bin(f, key, data, len, AV_OPT_SEARCH_CHILDREN);
|
|
|
|
av_freep(&data);
|
|
|
|
} else {
|
|
|
|
char *data = file_read(val);
|
|
|
|
if (!data) {
|
|
|
|
ret = AVERROR(EIO);
|
|
|
|
goto err_load;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = av_opt_set(f, key, data, AV_OPT_SEARCH_CHILDREN);
|
|
|
|
av_freep(&data);
|
|
|
|
}
|
|
|
|
if (ret < 0)
|
|
|
|
goto err_apply;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_apply:
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Error applying option '%s' to filter '%s': %s\n",
|
|
|
|
key, f->filter->name, av_err2str(ret));
|
|
|
|
return ret;
|
|
|
|
err_load:
|
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
|
|
|
"Error loading value for option '%s' from file '%s'\n",
|
|
|
|
key, val);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int graph_opts_apply(AVFilterGraphSegment *seg)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < seg->nb_chains; i++) {
|
|
|
|
AVFilterChain *ch = seg->chains[i];
|
|
|
|
|
|
|
|
for (size_t j = 0; j < ch->nb_filters; j++) {
|
|
|
|
AVFilterParams *p = ch->filters[j];
|
|
|
|
const AVDictionaryEntry *e = NULL;
|
|
|
|
|
|
|
|
av_assert0(p->filter);
|
|
|
|
|
|
|
|
while ((e = av_dict_iterate(p->opts, e))) {
|
|
|
|
int ret = filter_opt_apply(p->filter, e->key, e->value);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
av_dict_free(&p->opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int graph_parse(AVFilterGraph *graph, const char *desc,
|
|
|
|
AVFilterInOut **inputs, AVFilterInOut **outputs,
|
|
|
|
AVBufferRef *hw_device)
|
|
|
|
{
|
|
|
|
AVFilterGraphSegment *seg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
*inputs = NULL;
|
|
|
|
*outputs = NULL;
|
|
|
|
|
|
|
|
ret = avfilter_graph_segment_parse(graph, desc, 0, &seg);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_graph_segment_create_filters(seg, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (hw_device) {
|
|
|
|
for (int i = 0; i < graph->nb_filters; i++) {
|
|
|
|
AVFilterContext *f = graph->filters[i];
|
|
|
|
|
|
|
|
if (!(f->filter->flags & AVFILTER_FLAG_HWDEVICE))
|
|
|
|
continue;
|
|
|
|
f->hw_device_ctx = av_buffer_ref(hw_device);
|
|
|
|
if (!f->hw_device_ctx) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = graph_opts_apply(seg);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ret = avfilter_graph_segment_apply(seg, 0, inputs, outputs);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
avfilter_graph_segment_free(&seg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-22 09:58:31 +02:00
|
|
|
// Filters can be configured only if the formats of all inputs are known.
|
|
|
|
static int ifilter_has_all_input_formats(FilterGraph *fg)
|
|
|
|
{
|
2024-02-19 11:43:24 +02:00
|
|
|
for (int i = 0; i < fg->nb_inputs; i++) {
|
2023-05-22 09:58:31 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
|
|
|
|
if (ifp->format < 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:02:21 +02:00
|
|
|
static int filter_thread(void *arg);
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-05-21 20:29:59 +02:00
|
|
|
static char *describe_filter_link(FilterGraph *fg, AVFilterInOut *inout, int in)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inout->filter_ctx;
|
|
|
|
AVFilterPad *pads = in ? ctx->input_pads : ctx->output_pads;
|
|
|
|
int nb_pads = in ? ctx->nb_inputs : ctx->nb_outputs;
|
|
|
|
|
|
|
|
if (nb_pads > 1)
|
2023-07-13 15:11:07 +02:00
|
|
|
return av_strdup(ctx->filter->name);
|
|
|
|
return av_asprintf("%s:%s", ctx->filter->name,
|
|
|
|
avfilter_pad_get_name(pads, inout->pad_idx));
|
2023-05-21 20:29:59 +02:00
|
|
|
}
|
|
|
|
|
2024-04-02 13:40:42 +02:00
|
|
|
static const char *ofilter_item_name(void *obj)
|
|
|
|
{
|
|
|
|
OutputFilterPriv *ofp = obj;
|
|
|
|
return ofp->log_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass ofilter_class = {
|
|
|
|
.class_name = "OutputFilter",
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.item_name = ofilter_item_name,
|
|
|
|
.parent_log_context_offset = offsetof(OutputFilterPriv, log_parent),
|
|
|
|
.category = AV_CLASS_CATEGORY_FILTER,
|
|
|
|
};
|
|
|
|
|
|
|
|
static OutputFilter *ofilter_alloc(FilterGraph *fg, enum AVMediaType type)
|
2023-03-27 08:12:14 +02:00
|
|
|
{
|
2023-07-06 17:09:59 +02:00
|
|
|
OutputFilterPriv *ofp;
|
2023-03-27 08:12:14 +02:00
|
|
|
OutputFilter *ofilter;
|
|
|
|
|
2023-07-13 17:50:16 +02:00
|
|
|
ofp = allocate_array_elem(&fg->outputs, sizeof(*ofp), &fg->nb_outputs);
|
|
|
|
if (!ofp)
|
|
|
|
return NULL;
|
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
ofilter = &ofp->ofilter;
|
2024-04-02 13:40:42 +02:00
|
|
|
ofilter->class = &ofilter_class;
|
|
|
|
ofp->log_parent = fg;
|
2023-03-27 08:12:14 +02:00
|
|
|
ofilter->graph = fg;
|
2024-04-02 13:40:42 +02:00
|
|
|
ofilter->type = type;
|
2023-07-06 17:09:59 +02:00
|
|
|
ofp->format = -1;
|
2024-04-08 13:58:19 +02:00
|
|
|
ofp->color_space = AVCOL_SPC_UNSPECIFIED;
|
|
|
|
ofp->color_range = AVCOL_RANGE_UNSPECIFIED;
|
2023-11-03 16:24:36 +02:00
|
|
|
ofp->index = fg->nb_outputs - 1;
|
2023-03-27 08:12:14 +02:00
|
|
|
|
2024-04-02 13:40:42 +02:00
|
|
|
snprintf(ofp->log_name, sizeof(ofp->log_name), "%co%d",
|
|
|
|
av_get_media_type_string(type)[0], ofp->index);
|
|
|
|
|
2023-03-27 08:12:14 +02:00
|
|
|
return ofilter;
|
|
|
|
}
|
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist,
|
|
|
|
const ViewSpecifier *vs)
|
2023-05-21 21:11:09 +02:00
|
|
|
{
|
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
2023-06-08 20:36:33 +02:00
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(ifilter->graph);
|
2024-08-10 18:36:49 +02:00
|
|
|
SchedulerNode src;
|
|
|
|
int ret;
|
2023-05-21 21:11:09 +02:00
|
|
|
|
2024-02-13 17:44:32 +02:00
|
|
|
av_assert0(!ifp->bound);
|
|
|
|
ifp->bound = 1;
|
2023-06-08 16:19:14 +02:00
|
|
|
|
2024-01-10 13:31:58 +02:00
|
|
|
if (ifp->type != ist->par->codec_type &&
|
|
|
|
!(ifp->type == AVMEDIA_TYPE_VIDEO && ist->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {
|
|
|
|
av_log(fgp, AV_LOG_ERROR, "Tried to connect %s stream to %s filtergraph input\n",
|
|
|
|
av_get_media_type_string(ist->par->codec_type), av_get_media_type_string(ifp->type));
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2023-05-23 22:54:23 +02:00
|
|
|
ifp->type_src = ist->st->codecpar->codec_type;
|
|
|
|
|
2024-02-22 14:24:38 +02:00
|
|
|
ifp->opts.fallback = av_frame_alloc();
|
|
|
|
if (!ifp->opts.fallback)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
ret = ist_filter_add(ist, ifilter, filtergraph_is_simple(ifilter->graph),
|
|
|
|
vs, &ifp->opts, &src);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2023-05-18 16:56:15 +02:00
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
ret = sch_connect(fgp->sch,
|
|
|
|
src, SCH_FILTER_IN(fgp->sch_idx, ifp->index));
|
2023-05-21 21:11:09 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2023-05-24 10:08:33 +02:00
|
|
|
if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
|
|
|
|
ifp->sub2video.frame = av_frame_alloc();
|
|
|
|
if (!ifp->sub2video.frame)
|
|
|
|
return AVERROR(ENOMEM);
|
2024-02-13 13:18:27 +02:00
|
|
|
|
|
|
|
ifp->width = ifp->opts.sub2video_width;
|
|
|
|
ifp->height = ifp->opts.sub2video_height;
|
|
|
|
|
|
|
|
/* rectangles are AV_PIX_FMT_PAL8, but we have no guarantee that the
|
|
|
|
palettes for all rectangles are identical or compatible */
|
|
|
|
ifp->format = AV_PIX_FMT_RGB32;
|
|
|
|
|
2024-02-13 17:35:21 +02:00
|
|
|
ifp->time_base = AV_TIME_BASE_Q;
|
|
|
|
|
2024-02-13 13:18:27 +02:00
|
|
|
av_log(fgp, AV_LOG_VERBOSE, "sub2video: using %dx%d canvas\n",
|
|
|
|
ifp->width, ifp->height);
|
2023-05-24 10:08:33 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
return 0;
|
2023-05-21 21:11:09 +02:00
|
|
|
}
|
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
static int ifilter_bind_dec(InputFilterPriv *ifp, Decoder *dec,
|
|
|
|
const ViewSpecifier *vs)
|
2024-02-19 11:27:44 +02:00
|
|
|
{
|
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph);
|
2024-08-10 18:36:49 +02:00
|
|
|
SchedulerNode src;
|
|
|
|
int ret;
|
2024-02-19 11:27:44 +02:00
|
|
|
|
|
|
|
av_assert0(!ifp->bound);
|
|
|
|
ifp->bound = 1;
|
|
|
|
|
|
|
|
if (ifp->type != dec->type) {
|
|
|
|
av_log(fgp, AV_LOG_ERROR, "Tried to connect %s decoder to %s filtergraph input\n",
|
|
|
|
av_get_media_type_string(dec->type), av_get_media_type_string(ifp->type));
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ifp->type_src = ifp->type;
|
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
ret = dec_filter_add(dec, &ifp->ifilter, &ifp->opts, vs, &src);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2024-02-19 11:27:44 +02:00
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
ret = sch_connect(fgp->sch, src, SCH_FILTER_IN(fgp->sch_idx, ifp->index));
|
2024-02-19 11:27:44 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-03-29 11:15:49 +02:00
|
|
|
static int set_channel_layout(OutputFilterPriv *f, const AVChannelLayout *layouts_allowed,
|
|
|
|
const AVChannelLayout *layout_requested)
|
2023-05-21 22:46:59 +02:00
|
|
|
{
|
|
|
|
int i, err;
|
|
|
|
|
2024-03-29 11:15:49 +02:00
|
|
|
if (layout_requested->order != AV_CHANNEL_ORDER_UNSPEC) {
|
2023-05-21 22:46:59 +02:00
|
|
|
/* Pass the layout through for all orders but UNSPEC */
|
2024-03-29 11:15:49 +02:00
|
|
|
err = av_channel_layout_copy(&f->ch_layout, layout_requested);
|
2023-05-21 22:46:59 +02:00
|
|
|
if (err < 0)
|
2023-07-13 15:11:07 +02:00
|
|
|
return err;
|
|
|
|
return 0;
|
2023-05-21 22:46:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Requested layout is of order UNSPEC */
|
2024-03-29 11:15:49 +02:00
|
|
|
if (!layouts_allowed) {
|
2023-05-21 22:46:59 +02:00
|
|
|
/* Use the default native layout for the requested amount of channels when the
|
|
|
|
encoder doesn't have a list of supported layouts */
|
2024-03-29 11:15:49 +02:00
|
|
|
av_channel_layout_default(&f->ch_layout, layout_requested->nb_channels);
|
2023-07-13 15:11:07 +02:00
|
|
|
return 0;
|
2023-05-21 22:46:59 +02:00
|
|
|
}
|
|
|
|
/* Encoder has a list of supported layouts. Pick the first layout in it with the
|
|
|
|
same amount of channels as the requested layout */
|
2024-03-29 11:15:49 +02:00
|
|
|
for (i = 0; layouts_allowed[i].nb_channels; i++) {
|
|
|
|
if (layouts_allowed[i].nb_channels == layout_requested->nb_channels)
|
2023-05-21 22:46:59 +02:00
|
|
|
break;
|
|
|
|
}
|
2024-03-29 11:15:49 +02:00
|
|
|
if (layouts_allowed[i].nb_channels) {
|
2023-05-21 22:46:59 +02:00
|
|
|
/* Use it if one is found */
|
2024-03-29 11:15:49 +02:00
|
|
|
err = av_channel_layout_copy(&f->ch_layout, &layouts_allowed[i]);
|
2023-05-21 22:46:59 +02:00
|
|
|
if (err < 0)
|
2023-07-13 15:11:07 +02:00
|
|
|
return err;
|
|
|
|
return 0;
|
2023-05-21 22:46:59 +02:00
|
|
|
}
|
|
|
|
/* If no layout for the amount of channels requested was found, use the default
|
|
|
|
native layout for it. */
|
2024-03-29 11:15:49 +02:00
|
|
|
av_channel_layout_default(&f->ch_layout, layout_requested->nb_channels);
|
2023-07-13 15:11:07 +02:00
|
|
|
|
|
|
|
return 0;
|
2023-05-21 22:46:59 +02:00
|
|
|
}
|
|
|
|
|
2024-09-12 18:25:36 +02:00
|
|
|
int ofilter_bind_enc(OutputFilter *ofilter, unsigned sched_idx_enc,
|
2024-04-01 06:07:45 +02:00
|
|
|
const OutputFilterOptions *opts)
|
2023-05-21 22:33:13 +02:00
|
|
|
{
|
2023-07-06 17:09:59 +02:00
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
|
2023-05-21 23:18:29 +02:00
|
|
|
FilterGraph *fg = ofilter->graph;
|
2023-07-06 17:35:19 +02:00
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(fg);
|
2023-05-18 16:56:15 +02:00
|
|
|
int ret;
|
2023-05-21 22:46:59 +02:00
|
|
|
|
2024-04-04 11:35:26 +02:00
|
|
|
av_assert0(!ofilter->bound);
|
2024-09-12 18:25:36 +02:00
|
|
|
av_assert0(!opts->enc ||
|
|
|
|
ofilter->type == opts->enc->type);
|
2023-06-08 16:19:14 +02:00
|
|
|
|
2024-04-04 11:35:26 +02:00
|
|
|
ofilter->bound = 1;
|
2023-05-21 22:33:13 +02:00
|
|
|
av_freep(&ofilter->linklabel);
|
2023-05-21 22:46:59 +02:00
|
|
|
|
2024-04-01 06:29:16 +02:00
|
|
|
ofp->flags = opts->flags;
|
2024-04-01 06:29:16 +02:00
|
|
|
ofp->ts_offset = opts->ts_offset;
|
2024-04-01 06:29:16 +02:00
|
|
|
ofp->enc_timebase = opts->output_tb;
|
2023-07-24 12:32:48 +02:00
|
|
|
|
2024-04-02 13:58:59 +02:00
|
|
|
ofp->trim_start_us = opts->trim_start_us;
|
|
|
|
ofp->trim_duration_us = opts->trim_duration_us;
|
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
ofp->name = av_strdup(opts->name);
|
|
|
|
if (!ofp->name)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2024-04-02 13:58:59 +02:00
|
|
|
ret = av_dict_copy(&ofp->sws_opts, opts->sws_opts, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = av_dict_copy(&ofp->swr_opts, opts->swr_opts, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (opts->flags & OFILTER_FLAG_AUDIO_24BIT)
|
|
|
|
av_dict_set(&ofp->swr_opts, "output_sample_bits", "24", 0);
|
|
|
|
|
2024-04-02 13:40:42 +02:00
|
|
|
if (fgp->is_simple) {
|
|
|
|
// for simple filtergraph there is just one output,
|
|
|
|
// so use only graph-level information for logging
|
|
|
|
ofp->log_parent = NULL;
|
|
|
|
av_strlcpy(ofp->log_name, fgp->log_name, sizeof(ofp->log_name));
|
|
|
|
} else
|
|
|
|
av_strlcatf(ofp->log_name, sizeof(ofp->log_name), "->%s", ofp->name);
|
|
|
|
|
2024-04-01 06:29:16 +02:00
|
|
|
switch (ofilter->type) {
|
2023-05-21 22:46:59 +02:00
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2024-04-01 06:29:16 +02:00
|
|
|
ofp->width = opts->width;
|
|
|
|
ofp->height = opts->height;
|
|
|
|
if (opts->format != AV_PIX_FMT_NONE) {
|
|
|
|
ofp->format = opts->format;
|
2024-04-05 20:47:48 +02:00
|
|
|
} else
|
|
|
|
ofp->formats = opts->formats;
|
2023-07-06 19:19:51 +02:00
|
|
|
|
2024-04-08 13:58:19 +02:00
|
|
|
if (opts->color_space != AVCOL_SPC_UNSPECIFIED)
|
|
|
|
ofp->color_space = opts->color_space;
|
|
|
|
else
|
|
|
|
ofp->color_spaces = opts->color_spaces;
|
|
|
|
|
|
|
|
if (opts->color_range != AVCOL_RANGE_UNSPECIFIED)
|
|
|
|
ofp->color_range = opts->color_range;
|
|
|
|
else
|
|
|
|
ofp->color_ranges = opts->color_ranges;
|
|
|
|
|
2024-04-01 06:29:16 +02:00
|
|
|
fgp->disable_conversions |= !!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT);
|
2023-07-06 17:35:19 +02:00
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
ofp->fps.last_frame = av_frame_alloc();
|
|
|
|
if (!ofp->fps.last_frame)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2024-04-01 06:29:16 +02:00
|
|
|
ofp->fps.vsync_method = opts->vsync_method;
|
2024-09-12 18:17:30 +02:00
|
|
|
ofp->fps.framerate = opts->frame_rate;
|
|
|
|
ofp->fps.framerate_max = opts->max_frame_rate;
|
2024-09-12 18:09:58 +02:00
|
|
|
ofp->fps.framerate_supported = opts->frame_rates;
|
2023-07-24 12:32:48 +02:00
|
|
|
|
|
|
|
// reduce frame rate for mpeg4 to be within the spec limits
|
2024-04-01 06:07:45 +02:00
|
|
|
if (opts->enc && opts->enc->id == AV_CODEC_ID_MPEG4)
|
2023-07-24 12:32:48 +02:00
|
|
|
ofp->fps.framerate_clip = 65535;
|
|
|
|
|
|
|
|
ofp->fps.dup_warning = 1000;
|
|
|
|
|
2023-05-21 22:46:59 +02:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2024-04-01 06:29:16 +02:00
|
|
|
if (opts->format != AV_SAMPLE_FMT_NONE) {
|
|
|
|
ofp->format = opts->format;
|
2024-04-05 20:47:48 +02:00
|
|
|
} else {
|
|
|
|
ofp->formats = opts->formats;
|
2023-05-21 22:46:59 +02:00
|
|
|
}
|
2024-04-01 06:29:16 +02:00
|
|
|
if (opts->sample_rate) {
|
|
|
|
ofp->sample_rate = opts->sample_rate;
|
2024-04-05 20:47:48 +02:00
|
|
|
} else
|
|
|
|
ofp->sample_rates = opts->sample_rates;
|
2024-04-01 06:29:16 +02:00
|
|
|
if (opts->ch_layout.nb_channels) {
|
2024-04-05 20:47:48 +02:00
|
|
|
int ret = set_channel_layout(ofp, opts->ch_layouts, &opts->ch_layout);
|
2023-07-13 15:11:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2024-04-05 20:47:48 +02:00
|
|
|
} else {
|
|
|
|
ofp->ch_layouts = opts->ch_layouts;
|
2023-05-21 22:46:59 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2023-05-21 23:18:29 +02:00
|
|
|
|
2023-05-18 16:56:15 +02:00
|
|
|
ret = sch_connect(fgp->sch, SCH_FILTER_OUT(fgp->sch_idx, ofp->index),
|
|
|
|
SCH_ENC(sched_idx_enc));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
return 0;
|
2023-05-21 22:33:13 +02:00
|
|
|
}
|
|
|
|
|
2024-04-04 11:46:52 +02:00
|
|
|
static int ofilter_bind_ifilter(OutputFilter *ofilter, InputFilterPriv *ifp,
|
|
|
|
const OutputFilterOptions *opts)
|
|
|
|
{
|
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
|
|
|
|
|
|
|
|
av_assert0(!ofilter->bound);
|
|
|
|
av_assert0(ofilter->type == ifp->type);
|
|
|
|
|
|
|
|
ofilter->bound = 1;
|
|
|
|
av_freep(&ofilter->linklabel);
|
|
|
|
|
|
|
|
ofp->name = av_strdup(opts->name);
|
|
|
|
if (!ofp->name)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
av_strlcatf(ofp->log_name, sizeof(ofp->log_name), "->%s", ofp->name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ifilter_bind_fg(InputFilterPriv *ifp, FilterGraph *fg_src, int out_idx)
|
|
|
|
{
|
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(ifp->ifilter.graph);
|
|
|
|
OutputFilter *ofilter_src = fg_src->outputs[out_idx];
|
|
|
|
OutputFilterOptions opts;
|
|
|
|
char name[32];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
av_assert0(!ifp->bound);
|
|
|
|
ifp->bound = 1;
|
|
|
|
|
|
|
|
if (ifp->type != ofilter_src->type) {
|
|
|
|
av_log(fgp, AV_LOG_ERROR, "Tried to connect %s output to %s input\n",
|
|
|
|
av_get_media_type_string(ofilter_src->type),
|
|
|
|
av_get_media_type_string(ifp->type));
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
ifp->type_src = ifp->type;
|
|
|
|
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
|
|
|
|
|
|
|
snprintf(name, sizeof(name), "fg:%d:%d", fgp->fg.index, ifp->index);
|
|
|
|
opts.name = name;
|
|
|
|
|
|
|
|
ret = ofilter_bind_ifilter(ofilter_src, ifp, &opts);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = sch_connect(fgp->sch, SCH_FILTER_OUT(fg_src->index, out_idx),
|
|
|
|
SCH_FILTER_IN(fgp->sch_idx, ifp->index));
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-21 21:11:09 +02:00
|
|
|
static InputFilter *ifilter_alloc(FilterGraph *fg)
|
2023-04-25 23:32:04 +02:00
|
|
|
{
|
2023-07-13 17:50:16 +02:00
|
|
|
InputFilterPriv *ifp;
|
|
|
|
InputFilter *ifilter;
|
2023-04-25 23:32:04 +02:00
|
|
|
|
2023-07-13 17:50:16 +02:00
|
|
|
ifp = allocate_array_elem(&fg->inputs, sizeof(*ifp), &fg->nb_inputs);
|
|
|
|
if (!ifp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ifilter = &ifp->ifilter;
|
2023-04-25 23:32:04 +02:00
|
|
|
ifilter->graph = fg;
|
|
|
|
|
2023-05-10 09:59:13 +02:00
|
|
|
ifp->frame = av_frame_alloc();
|
|
|
|
if (!ifp->frame)
|
2023-07-13 15:11:07 +02:00
|
|
|
return NULL;
|
2023-05-10 09:59:13 +02:00
|
|
|
|
2023-11-03 16:24:36 +02:00
|
|
|
ifp->index = fg->nb_inputs - 1;
|
2023-05-13 10:05:02 +02:00
|
|
|
ifp->format = -1;
|
2024-02-22 14:24:38 +02:00
|
|
|
ifp->color_space = AVCOL_SPC_UNSPECIFIED;
|
|
|
|
ifp->color_range = AVCOL_RANGE_UNSPECIFIED;
|
2023-04-30 12:17:16 +02:00
|
|
|
|
2023-04-25 23:44:49 +02:00
|
|
|
ifp->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
|
|
|
|
if (!ifp->frame_queue)
|
2023-07-13 15:11:07 +02:00
|
|
|
return NULL;
|
2023-04-25 23:32:04 +02:00
|
|
|
|
|
|
|
return ifilter;
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:41:48 +02:00
|
|
|
void fg_free(FilterGraph **pfg)
|
|
|
|
{
|
|
|
|
FilterGraph *fg = *pfg;
|
2023-04-18 19:44:15 +02:00
|
|
|
FilterGraphPriv *fgp;
|
2023-04-18 19:41:48 +02:00
|
|
|
|
|
|
|
if (!fg)
|
|
|
|
return;
|
2023-04-18 19:44:15 +02:00
|
|
|
fgp = fgp_from_fg(fg);
|
2023-04-18 19:41:48 +02:00
|
|
|
|
2023-04-18 19:43:14 +02:00
|
|
|
for (int j = 0; j < fg->nb_inputs; j++) {
|
|
|
|
InputFilter *ifilter = fg->inputs[j];
|
2023-04-25 23:44:49 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
2023-04-18 19:43:14 +02:00
|
|
|
|
2023-04-25 23:44:49 +02:00
|
|
|
if (ifp->frame_queue) {
|
2023-04-18 19:43:14 +02:00
|
|
|
AVFrame *frame;
|
2023-04-25 23:44:49 +02:00
|
|
|
while (av_fifo_read(ifp->frame_queue, &frame, 1) >= 0)
|
2023-04-18 19:43:14 +02:00
|
|
|
av_frame_free(&frame);
|
2023-04-25 23:44:49 +02:00
|
|
|
av_fifo_freep2(&ifp->frame_queue);
|
2023-04-18 19:41:48 +02:00
|
|
|
}
|
2023-05-24 10:08:33 +02:00
|
|
|
av_frame_free(&ifp->sub2video.frame);
|
2023-04-30 12:17:16 +02:00
|
|
|
|
2023-05-10 09:59:13 +02:00
|
|
|
av_frame_free(&ifp->frame);
|
2024-02-22 14:24:38 +02:00
|
|
|
av_frame_free(&ifp->opts.fallback);
|
2023-05-10 09:59:13 +02:00
|
|
|
|
2023-04-30 12:26:51 +02:00
|
|
|
av_buffer_unref(&ifp->hw_frames_ctx);
|
2023-05-21 22:18:57 +02:00
|
|
|
av_freep(&ifp->linklabel);
|
2024-02-13 13:18:27 +02:00
|
|
|
av_freep(&ifp->opts.name);
|
2023-04-18 19:43:14 +02:00
|
|
|
av_freep(&ifilter->name);
|
|
|
|
av_freep(&fg->inputs[j]);
|
|
|
|
}
|
|
|
|
av_freep(&fg->inputs);
|
|
|
|
for (int j = 0; j < fg->nb_outputs; j++) {
|
|
|
|
OutputFilter *ofilter = fg->outputs[j];
|
2023-07-06 17:09:59 +02:00
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
|
2023-04-18 19:43:14 +02:00
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
av_frame_free(&ofp->fps.last_frame);
|
2024-04-02 13:58:59 +02:00
|
|
|
av_dict_free(&ofp->sws_opts);
|
|
|
|
av_dict_free(&ofp->swr_opts);
|
2023-07-24 12:32:48 +02:00
|
|
|
|
2023-05-21 20:17:19 +02:00
|
|
|
av_freep(&ofilter->linklabel);
|
2023-04-18 19:43:14 +02:00
|
|
|
av_freep(&ofilter->name);
|
2024-04-04 10:51:53 +02:00
|
|
|
av_freep(&ofilter->apad);
|
2024-04-02 11:40:41 +02:00
|
|
|
av_freep(&ofp->name);
|
2023-07-06 17:09:59 +02:00
|
|
|
av_channel_layout_uninit(&ofp->ch_layout);
|
2023-04-18 19:43:14 +02:00
|
|
|
av_freep(&fg->outputs[j]);
|
|
|
|
}
|
|
|
|
av_freep(&fg->outputs);
|
2023-04-27 15:47:04 +02:00
|
|
|
av_freep(&fgp->graph_desc);
|
2024-04-04 11:30:18 +02:00
|
|
|
av_freep(&fgp->nb_threads);
|
2023-04-18 19:41:48 +02:00
|
|
|
|
2023-04-18 19:44:15 +02:00
|
|
|
av_frame_free(&fgp->frame);
|
2023-07-24 12:32:48 +02:00
|
|
|
av_frame_free(&fgp->frame_enc);
|
2023-04-18 19:44:15 +02:00
|
|
|
|
2023-04-18 19:41:48 +02:00
|
|
|
av_freep(pfg);
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:58:16 +02:00
|
|
|
static const char *fg_item_name(void *obj)
|
|
|
|
{
|
|
|
|
const FilterGraphPriv *fgp = obj;
|
|
|
|
|
|
|
|
return fgp->log_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AVClass fg_class = {
|
|
|
|
.class_name = "FilterGraph",
|
|
|
|
.version = LIBAVUTIL_VERSION_INT,
|
|
|
|
.item_name = fg_item_name,
|
|
|
|
.category = AV_CLASS_CATEGORY_FILTER,
|
|
|
|
};
|
|
|
|
|
2023-05-18 16:56:15 +02:00
|
|
|
int fg_create(FilterGraph **pfg, char *graph_desc, Scheduler *sch)
|
2023-04-18 14:11:03 +02:00
|
|
|
{
|
2023-07-13 17:50:16 +02:00
|
|
|
FilterGraphPriv *fgp;
|
|
|
|
FilterGraph *fg;
|
2023-04-18 14:11:03 +02:00
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
AVFilterInOut *inputs, *outputs;
|
|
|
|
AVFilterGraph *graph;
|
|
|
|
int ret = 0;
|
|
|
|
|
2024-04-04 12:49:55 +02:00
|
|
|
fgp = av_mallocz(sizeof(*fgp));
|
2023-07-13 17:50:16 +02:00
|
|
|
if (!fgp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
fg = &fgp->fg;
|
|
|
|
|
2024-04-04 12:49:55 +02:00
|
|
|
if (pfg) {
|
2023-07-13 15:11:07 +02:00
|
|
|
*pfg = fg;
|
2024-04-04 12:49:55 +02:00
|
|
|
fg->index = -1;
|
|
|
|
} else {
|
|
|
|
ret = av_dynarray_add_nofree(&filtergraphs, &nb_filtergraphs, fgp);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(&fgp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fg->index = nb_filtergraphs - 1;
|
|
|
|
}
|
2023-07-13 15:11:07 +02:00
|
|
|
|
2023-06-08 20:58:16 +02:00
|
|
|
fg->class = &fg_class;
|
2023-04-27 15:47:04 +02:00
|
|
|
fgp->graph_desc = graph_desc;
|
2023-07-06 17:35:19 +02:00
|
|
|
fgp->disable_conversions = !auto_conversion_filters;
|
2023-05-18 16:56:15 +02:00
|
|
|
fgp->sch = sch;
|
2023-04-18 14:11:03 +02:00
|
|
|
|
2023-06-08 20:58:16 +02:00
|
|
|
snprintf(fgp->log_name, sizeof(fgp->log_name), "fc#%d", fg->index);
|
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
fgp->frame = av_frame_alloc();
|
|
|
|
fgp->frame_enc = av_frame_alloc();
|
|
|
|
if (!fgp->frame || !fgp->frame_enc)
|
2023-07-13 15:11:07 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2023-04-18 19:44:15 +02:00
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
/* this graph is only used for determining the kinds of inputs
|
|
|
|
* and outputs we have, and is discarded on exit from this function */
|
|
|
|
graph = avfilter_graph_alloc();
|
|
|
|
if (!graph)
|
2023-07-13 15:11:07 +02:00
|
|
|
return AVERROR(ENOMEM);;
|
2023-05-21 22:18:57 +02:00
|
|
|
graph->nb_threads = 1;
|
|
|
|
|
|
|
|
ret = graph_parse(graph, fgp->graph_desc, &inputs, &outputs, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
for (unsigned i = 0; i < graph->nb_filters; i++) {
|
|
|
|
const AVFilter *f = graph->filters[i]->filter;
|
|
|
|
if (!avfilter_filter_pad_count(f, 0) &&
|
|
|
|
!(f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)) {
|
|
|
|
fgp->have_sources = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
for (AVFilterInOut *cur = inputs; cur; cur = cur->next) {
|
|
|
|
InputFilter *const ifilter = ifilter_alloc(fg);
|
2023-11-04 08:53:40 +02:00
|
|
|
InputFilterPriv *ifp;
|
2023-05-21 22:18:57 +02:00
|
|
|
|
2023-11-04 08:53:40 +02:00
|
|
|
if (!ifilter) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ifp = ifp_from_ifilter(ifilter);
|
2023-05-21 22:18:57 +02:00
|
|
|
ifp->linklabel = cur->name;
|
|
|
|
cur->name = NULL;
|
|
|
|
|
|
|
|
ifp->type = avfilter_pad_get_type(cur->filter_ctx->input_pads,
|
|
|
|
cur->pad_idx);
|
2024-02-21 16:43:25 +02:00
|
|
|
|
|
|
|
if (ifp->type != AVMEDIA_TYPE_VIDEO && ifp->type != AVMEDIA_TYPE_AUDIO) {
|
|
|
|
av_log(fg, AV_LOG_FATAL, "Only video and audio filters supported "
|
|
|
|
"currently.\n");
|
|
|
|
ret = AVERROR(ENOSYS);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
ifilter->name = describe_filter_link(fg, cur, 1);
|
2023-07-13 15:11:07 +02:00
|
|
|
if (!ifilter->name) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2023-05-21 22:18:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (AVFilterInOut *cur = outputs; cur; cur = cur->next) {
|
2024-04-02 13:40:42 +02:00
|
|
|
const enum AVMediaType type = avfilter_pad_get_type(cur->filter_ctx->output_pads,
|
|
|
|
cur->pad_idx);
|
|
|
|
OutputFilter *const ofilter = ofilter_alloc(fg, type);
|
2023-05-21 22:18:57 +02:00
|
|
|
|
2023-11-04 08:54:10 +02:00
|
|
|
if (!ofilter) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
2023-07-13 17:50:16 +02:00
|
|
|
goto fail;
|
2023-11-04 08:54:10 +02:00
|
|
|
}
|
2023-07-13 17:50:16 +02:00
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
ofilter->linklabel = cur->name;
|
|
|
|
cur->name = NULL;
|
|
|
|
|
|
|
|
ofilter->name = describe_filter_link(fg, cur, 0);
|
2023-07-13 15:11:07 +02:00
|
|
|
if (!ofilter->name) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
2023-05-21 22:18:57 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 21:33:44 +02:00
|
|
|
if (!fg->nb_outputs) {
|
|
|
|
av_log(fg, AV_LOG_FATAL, "A filtergraph has zero outputs, this is not supported\n");
|
|
|
|
ret = AVERROR(ENOSYS);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2023-05-18 16:56:15 +02:00
|
|
|
ret = sch_add_filtergraph(sch, fg->nb_inputs, fg->nb_outputs,
|
|
|
|
filter_thread, fgp);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
|
|
|
fgp->sch_idx = ret;
|
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
fail:
|
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
avfilter_inout_free(&outputs);
|
|
|
|
avfilter_graph_free(&graph);
|
|
|
|
|
|
|
|
if (ret < 0)
|
2023-07-13 15:11:07 +02:00
|
|
|
return ret;
|
2023-05-21 22:18:57 +02:00
|
|
|
|
2023-07-13 15:11:07 +02:00
|
|
|
return 0;
|
2023-04-18 14:11:03 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 19:16:39 +02:00
|
|
|
int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
|
2023-05-18 16:56:15 +02:00
|
|
|
char *graph_desc,
|
2024-04-01 06:07:45 +02:00
|
|
|
Scheduler *sch, unsigned sched_idx_enc,
|
|
|
|
const OutputFilterOptions *opts)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2023-04-18 14:11:03 +02:00
|
|
|
FilterGraph *fg;
|
2023-05-21 19:09:01 +02:00
|
|
|
FilterGraphPriv *fgp;
|
2023-05-11 10:51:13 +02:00
|
|
|
int ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2024-05-17 20:46:36 +02:00
|
|
|
ret = fg_create(&ost->fg_simple, graph_desc, sch);
|
2023-07-13 15:11:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2024-05-17 20:46:36 +02:00
|
|
|
fg = ost->fg_simple;
|
2023-05-21 19:09:01 +02:00
|
|
|
fgp = fgp_from_fg(fg);
|
|
|
|
|
|
|
|
fgp->is_simple = 1;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
snprintf(fgp->log_name, sizeof(fgp->log_name), "%cf%s",
|
|
|
|
av_get_media_type_string(ost->type)[0], opts->name);
|
2023-06-08 20:58:16 +02:00
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
if (fg->nb_inputs != 1 || fg->nb_outputs != 1) {
|
2023-06-08 20:58:16 +02:00
|
|
|
av_log(fg, AV_LOG_ERROR, "Simple filtergraph '%s' was expected "
|
2023-05-21 22:18:57 +02:00
|
|
|
"to have exactly 1 input and 1 output. "
|
|
|
|
"However, it had %d input(s) and %d output(s). Please adjust, "
|
|
|
|
"or use a complex filtergraph (-filter_complex) instead.\n",
|
|
|
|
graph_desc, fg->nb_inputs, fg->nb_outputs);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2024-04-01 06:23:58 +02:00
|
|
|
if (fg->outputs[0]->type != ost->type) {
|
|
|
|
av_log(fg, AV_LOG_ERROR, "Filtergraph has a %s output, cannot connect "
|
|
|
|
"it to %s output stream\n",
|
|
|
|
av_get_media_type_string(fg->outputs[0]->type),
|
|
|
|
av_get_media_type_string(ost->type));
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
ost->filter = fg->outputs[0];
|
2016-05-27 12:14:33 +02:00
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
ret = ifilter_bind_ist(fg->inputs[0], ist, opts->vs);
|
2023-05-11 10:51:13 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2023-04-11 12:57:30 +02:00
|
|
|
|
2024-09-12 18:25:36 +02:00
|
|
|
ret = ofilter_bind_enc(fg->outputs[0], sched_idx_enc, opts);
|
2023-07-13 15:11:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2023-05-21 22:33:13 +02:00
|
|
|
|
2024-04-04 11:30:18 +02:00
|
|
|
if (opts->nb_threads) {
|
|
|
|
av_freep(&fgp->nb_threads);
|
|
|
|
fgp->nb_threads = av_strdup(opts->nb_threads);
|
|
|
|
if (!fgp->nb_threads)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2016-05-22 19:57:36 +02:00
|
|
|
return 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2024-02-21 16:28:01 +02:00
|
|
|
static int fg_complex_bind_input(FilterGraph *fg, InputFilter *ifilter)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2023-04-27 15:47:04 +02:00
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(fg);
|
2023-05-21 22:18:57 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
2012-08-02 09:24:02 +03:00
|
|
|
InputStream *ist = NULL;
|
2023-05-21 22:18:57 +02:00
|
|
|
enum AVMediaType type = ifp->type;
|
2024-08-10 18:36:49 +02:00
|
|
|
ViewSpecifier vs = { .type = VIEW_SPECIFIER_TYPE_NONE };
|
|
|
|
const char *spec;
|
|
|
|
char *p;
|
2023-05-11 10:51:13 +02:00
|
|
|
int i, ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2024-02-19 11:27:44 +02:00
|
|
|
if (ifp->linklabel && !strncmp(ifp->linklabel, "dec:", 4)) {
|
|
|
|
// bind to a standalone decoder
|
|
|
|
int dec_idx;
|
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
dec_idx = strtol(ifp->linklabel + 4, &p, 0);
|
2024-02-19 11:27:44 +02:00
|
|
|
if (dec_idx < 0 || dec_idx >= nb_decoders) {
|
|
|
|
av_log(fg, AV_LOG_ERROR, "Invalid decoder index %d in filtergraph description %s\n",
|
|
|
|
dec_idx, fgp->graph_desc);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
if (type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
spec = *p == ':' ? p + 1 : p;
|
|
|
|
ret = view_specifier_parse(&spec, &vs);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ifilter_bind_dec(ifp, decoders[dec_idx], &vs);
|
2024-02-19 11:27:44 +02:00
|
|
|
if (ret < 0)
|
|
|
|
av_log(fg, AV_LOG_ERROR, "Error binding a decoder to filtergraph input %s\n",
|
|
|
|
ifilter->name);
|
|
|
|
return ret;
|
|
|
|
} else if (ifp->linklabel) {
|
2024-08-10 18:36:49 +02:00
|
|
|
StreamSpecifier ss;
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFormatContext *s;
|
|
|
|
AVStream *st = NULL;
|
2024-04-04 11:46:52 +02:00
|
|
|
int file_idx;
|
|
|
|
|
|
|
|
// try finding an unbound filtergraph output with this label
|
|
|
|
for (int i = 0; i < nb_filtergraphs; i++) {
|
|
|
|
FilterGraph *fg_src = filtergraphs[i];
|
|
|
|
|
|
|
|
if (fg == fg_src)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int j = 0; j < fg_src->nb_outputs; j++) {
|
|
|
|
OutputFilter *ofilter = fg_src->outputs[j];
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2024-04-04 11:46:52 +02:00
|
|
|
if (!ofilter->bound && ofilter->linklabel &&
|
|
|
|
!strcmp(ofilter->linklabel, ifp->linklabel)) {
|
|
|
|
av_log(fg, AV_LOG_VERBOSE,
|
|
|
|
"Binding input with label '%s' to filtergraph output %d:%d\n",
|
|
|
|
ifp->linklabel, i, j);
|
|
|
|
|
|
|
|
ret = ifilter_bind_fg(ifp, fg_src, j);
|
|
|
|
if (ret < 0)
|
|
|
|
av_log(fg, AV_LOG_ERROR, "Error binding filtergraph input %s\n",
|
|
|
|
ifp->linklabel);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// bind to an explicitly specified demuxer stream
|
|
|
|
file_idx = strtol(ifp->linklabel, &p, 0);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (file_idx < 0 || file_idx >= nb_input_files) {
|
2023-06-08 20:58:16 +02:00
|
|
|
av_log(fg, AV_LOG_FATAL, "Invalid file index %d in filtergraph description %s.\n",
|
2023-04-27 15:47:04 +02:00
|
|
|
file_idx, fgp->graph_desc);
|
2023-07-13 15:11:07 +02:00
|
|
|
return AVERROR(EINVAL);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
s = input_files[file_idx]->ctx;
|
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
ret = stream_specifier_parse(&ss, *p == ':' ? p + 1 : p, 1, fg);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(fg, AV_LOG_ERROR, "Invalid stream specifier: %s\n", p);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
spec = ss.remainder ? ss.remainder : "";
|
|
|
|
ret = view_specifier_parse(&spec, &vs);
|
|
|
|
if (ret < 0) {
|
|
|
|
stream_specifier_uninit(&ss);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
for (i = 0; i < s->nb_streams; i++) {
|
2016-09-01 16:48:45 +02:00
|
|
|
enum AVMediaType stream_type = s->streams[i]->codecpar->codec_type;
|
2012-08-09 01:26:38 +03:00
|
|
|
if (stream_type != type &&
|
|
|
|
!(stream_type == AVMEDIA_TYPE_SUBTITLE &&
|
|
|
|
type == AVMEDIA_TYPE_VIDEO /* sub2video hack */))
|
2012-08-02 09:24:02 +03:00
|
|
|
continue;
|
2024-08-10 18:36:49 +02:00
|
|
|
if (stream_specifier_match(&ss, s, s->streams[i], fg)) {
|
2012-08-02 09:24:02 +03:00
|
|
|
st = s->streams[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-08-10 18:36:49 +02:00
|
|
|
stream_specifier_uninit(&ss);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (!st) {
|
2023-06-08 20:58:16 +02:00
|
|
|
av_log(fg, AV_LOG_FATAL, "Stream specifier '%s' in filtergraph description %s "
|
2023-04-27 15:47:04 +02:00
|
|
|
"matches no streams.\n", p, fgp->graph_desc);
|
2023-07-13 15:11:07 +02:00
|
|
|
return AVERROR(EINVAL);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
2022-11-17 10:49:27 +02:00
|
|
|
ist = input_files[file_idx]->streams[st->index];
|
2024-02-21 16:55:30 +02:00
|
|
|
|
|
|
|
av_log(fg, AV_LOG_VERBOSE,
|
|
|
|
"Binding input with label '%s' to input stream %d:%d\n",
|
|
|
|
ifp->linklabel, ist->file->index, ist->index);
|
2012-08-02 09:24:02 +03:00
|
|
|
} else {
|
2023-05-11 10:40:58 +02:00
|
|
|
ist = ist_find_unused(type);
|
2022-11-17 10:49:27 +02:00
|
|
|
if (!ist) {
|
2024-09-11 12:33:18 +02:00
|
|
|
av_log(fg, AV_LOG_FATAL,
|
|
|
|
"Cannot find an unused %s input stream to feed the "
|
|
|
|
"unlabeled input pad %s.\n",
|
|
|
|
av_get_media_type_string(type), ifilter->name);
|
2023-07-13 15:11:07 +02:00
|
|
|
return AVERROR(EINVAL);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
2024-02-21 16:55:30 +02:00
|
|
|
|
|
|
|
av_log(fg, AV_LOG_VERBOSE,
|
|
|
|
"Binding unlabeled input %d to input stream %d:%d\n",
|
|
|
|
ifp->index, ist->file->index, ist->index);
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
av_assert0(ist);
|
|
|
|
|
2024-08-10 18:36:49 +02:00
|
|
|
ret = ifilter_bind_ist(ifilter, ist, &vs);
|
2023-05-11 10:51:13 +02:00
|
|
|
if (ret < 0) {
|
2023-06-08 20:58:16 +02:00
|
|
|
av_log(fg, AV_LOG_ERROR,
|
2023-05-11 10:51:13 +02:00
|
|
|
"Error binding an input stream to complex filtergraph input %s.\n",
|
2023-05-21 22:18:57 +02:00
|
|
|
ifilter->name);
|
2023-07-13 15:11:07 +02:00
|
|
|
return ret;
|
2023-05-11 10:51:13 +02:00
|
|
|
}
|
2023-07-13 15:11:07 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2024-04-05 12:10:21 +02:00
|
|
|
static int bind_inputs(FilterGraph *fg)
|
2015-07-15 19:49:24 +02:00
|
|
|
{
|
2024-04-04 11:46:52 +02:00
|
|
|
// bind filtergraph inputs to input streams or other filtergraphs
|
2023-07-13 15:11:07 +02:00
|
|
|
for (int i = 0; i < fg->nb_inputs; i++) {
|
2024-02-21 16:28:01 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (ifp->bound)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = fg_complex_bind_input(fg, &ifp->ifilter);
|
2023-07-13 15:11:07 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2024-02-21 16:28:01 +02:00
|
|
|
|
2024-04-05 12:10:21 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fg_finalise_bindings(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (int i = 0; i < nb_filtergraphs; i++) {
|
|
|
|
ret = bind_inputs(filtergraphs[i]);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that all outputs were bound
|
|
|
|
for (int i = 0; i < nb_filtergraphs; i++) {
|
|
|
|
FilterGraph *fg = filtergraphs[i];
|
|
|
|
|
|
|
|
for (int j = 0; j < fg->nb_outputs; j++) {
|
|
|
|
OutputFilter *output = fg->outputs[j];
|
|
|
|
if (!output->bound) {
|
|
|
|
av_log(filtergraphs[j], AV_LOG_FATAL,
|
|
|
|
"Filter %s has an unconnected output\n", output->name);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
2024-02-21 16:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-05 12:10:21 +02:00
|
|
|
|
2023-05-21 22:18:57 +02:00
|
|
|
return 0;
|
2015-07-15 19:49:24 +02:00
|
|
|
}
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
static int insert_trim(int64_t start_time, int64_t duration,
|
|
|
|
AVFilterContext **last_filter, int *pad_idx,
|
|
|
|
const char *filter_name)
|
2013-04-10 16:02:01 +03:00
|
|
|
{
|
|
|
|
AVFilterGraph *graph = (*last_filter)->graph;
|
|
|
|
AVFilterContext *ctx;
|
|
|
|
const AVFilter *trim;
|
2013-06-15 10:59:40 +03:00
|
|
|
enum AVMediaType type = avfilter_pad_get_type((*last_filter)->output_pads, *pad_idx);
|
|
|
|
const char *name = (type == AVMEDIA_TYPE_VIDEO) ? "trim" : "atrim";
|
2013-04-10 16:02:01 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
if (duration == INT64_MAX && start_time == AV_NOPTS_VALUE)
|
2013-04-10 16:02:01 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
trim = avfilter_get_by_name(name);
|
|
|
|
if (!trim) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "%s filter not present, cannot limit "
|
|
|
|
"recording time.\n", name);
|
|
|
|
return AVERROR_FILTER_NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = avfilter_graph_alloc_filter(graph, trim, filter_name);
|
|
|
|
if (!ctx)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2013-06-15 10:59:40 +03:00
|
|
|
if (duration != INT64_MAX) {
|
2013-07-13 00:40:58 +03:00
|
|
|
ret = av_opt_set_int(ctx, "durationi", duration,
|
2013-04-10 16:02:01 +03:00
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
}
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret >= 0 && start_time != AV_NOPTS_VALUE) {
|
2013-07-13 00:40:58 +03:00
|
|
|
ret = av_opt_set_int(ctx, "starti", start_time,
|
2013-04-10 16:02:01 +03:00
|
|
|
AV_OPT_SEARCH_CHILDREN);
|
|
|
|
}
|
2013-04-10 16:02:01 +03:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Error configuring the %s filter", name);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = avfilter_init_str(ctx, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*last_filter = ctx;
|
|
|
|
*pad_idx = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-29 16:59:01 +02:00
|
|
|
static int insert_filter(AVFilterContext **last_filter, int *pad_idx,
|
|
|
|
const char *filter_name, const char *args)
|
|
|
|
{
|
|
|
|
AVFilterGraph *graph = (*last_filter)->graph;
|
2024-09-20 15:29:54 +02:00
|
|
|
const AVFilter *filter = avfilter_get_by_name(filter_name);
|
2015-04-29 16:59:01 +02:00
|
|
|
AVFilterContext *ctx;
|
|
|
|
int ret;
|
|
|
|
|
2024-09-20 15:29:54 +02:00
|
|
|
if (!filter)
|
|
|
|
return AVERROR_BUG;
|
|
|
|
|
2015-04-29 16:59:01 +02:00
|
|
|
ret = avfilter_graph_create_filter(&ctx,
|
2024-09-20 15:29:54 +02:00
|
|
|
filter,
|
2015-04-29 16:59:01 +02:00
|
|
|
filter_name, args, NULL, graph);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = avfilter_link(*last_filter, *pad_idx, ctx, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
*last_filter = ctx;
|
|
|
|
*pad_idx = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int configure_output_video_filter(FilterGraph *fg, AVFilterGraph *graph,
|
|
|
|
OutputFilter *ofilter, AVFilterInOut *out)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2023-07-06 17:09:59 +02:00
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *last_filter = out->filter_ctx;
|
2021-12-01 18:13:32 +02:00
|
|
|
AVBPrint bprint;
|
2012-08-02 09:24:02 +03:00
|
|
|
int pad_idx = out->pad_idx;
|
|
|
|
int ret;
|
|
|
|
char name[255];
|
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
snprintf(name, sizeof(name), "out_%s", ofp->name);
|
2023-05-27 17:58:35 +02:00
|
|
|
ret = avfilter_graph_create_filter(&ofp->filter,
|
2013-03-11 23:49:04 +03:00
|
|
|
avfilter_get_by_name("buffersink"),
|
2023-12-13 19:26:21 +02:00
|
|
|
name, NULL, NULL, graph);
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2024-04-02 13:58:59 +02:00
|
|
|
if ((ofp->width || ofp->height) && (ofp->flags & OFILTER_FLAG_AUTOSCALE)) {
|
2012-08-02 09:24:02 +03:00
|
|
|
char args[255];
|
|
|
|
AVFilterContext *filter;
|
2021-10-19 00:27:01 +02:00
|
|
|
const AVDictionaryEntry *e = NULL;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2015-08-08 13:33:46 +02:00
|
|
|
snprintf(args, sizeof(args), "%d:%d",
|
2023-07-06 17:09:59 +02:00
|
|
|
ofp->width, ofp->height);
|
2015-08-08 13:33:46 +02:00
|
|
|
|
2024-04-02 13:58:59 +02:00
|
|
|
while ((e = av_dict_iterate(ofp->sws_opts, e))) {
|
2015-08-08 13:33:46 +02:00
|
|
|
av_strlcatf(args, sizeof(args), ":%s=%s", e->key, e->value);
|
|
|
|
}
|
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
snprintf(name, sizeof(name), "scaler_out_%s", ofp->name);
|
2012-08-02 09:24:02 +03:00
|
|
|
if ((ret = avfilter_graph_create_filter(&filter, avfilter_get_by_name("scale"),
|
2023-12-13 19:26:21 +02:00
|
|
|
name, args, NULL, graph)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
last_filter = filter;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
2023-10-25 12:39:29 +02:00
|
|
|
av_assert0(!(ofp->flags & OFILTER_FLAG_DISABLE_CONVERT) ||
|
|
|
|
ofp->format != AV_PIX_FMT_NONE || !ofp->formats);
|
2021-12-01 18:13:32 +02:00
|
|
|
av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED);
|
2023-10-25 12:39:29 +02:00
|
|
|
choose_pix_fmts(ofp, &bprint);
|
2024-04-08 13:58:19 +02:00
|
|
|
choose_color_spaces(ofp, &bprint);
|
|
|
|
choose_color_ranges(ofp, &bprint);
|
2023-10-25 12:39:29 +02:00
|
|
|
if (!av_bprint_is_complete(&bprint))
|
|
|
|
return AVERROR(ENOMEM);
|
2023-07-13 15:11:07 +02:00
|
|
|
|
2023-10-25 12:39:29 +02:00
|
|
|
if (bprint.len) {
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *filter;
|
2021-04-06 06:21:22 +02:00
|
|
|
|
2013-04-21 12:59:34 +03:00
|
|
|
ret = avfilter_graph_create_filter(&filter,
|
2014-10-14 18:46:44 +03:00
|
|
|
avfilter_get_by_name("format"),
|
2023-10-25 12:39:29 +02:00
|
|
|
"format", bprint.str, NULL, graph);
|
2021-12-01 18:13:32 +02:00
|
|
|
av_bprint_finalize(&bprint, NULL);
|
2013-04-21 12:59:34 +03:00
|
|
|
if (ret < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, filter, 0)) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
last_filter = filter;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
snprintf(name, sizeof(name), "trim_out_%s", ofp->name);
|
2024-04-02 13:58:59 +02:00
|
|
|
ret = insert_trim(ofp->trim_start_us, ofp->trim_duration_us,
|
2013-06-15 10:59:40 +03:00
|
|
|
&last_filter, &pad_idx, name);
|
2013-04-10 16:02:01 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
2023-05-27 17:58:35 +02:00
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, ofp->filter, 0)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int configure_output_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
|
|
|
|
OutputFilter *ofilter, AVFilterInOut *out)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2023-07-06 17:09:59 +02:00
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *last_filter = out->filter_ctx;
|
|
|
|
int pad_idx = out->pad_idx;
|
2021-04-06 05:26:31 +02:00
|
|
|
AVBPrint args;
|
2012-08-02 09:24:02 +03:00
|
|
|
char name[255];
|
|
|
|
int ret;
|
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
snprintf(name, sizeof(name), "out_%s", ofp->name);
|
2023-05-27 17:58:35 +02:00
|
|
|
ret = avfilter_graph_create_filter(&ofp->filter,
|
2013-03-11 23:49:04 +03:00
|
|
|
avfilter_get_by_name("abuffersink"),
|
2023-12-13 19:26:21 +02:00
|
|
|
name, NULL, NULL, graph);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2023-05-27 17:58:35 +02:00
|
|
|
if ((ret = av_opt_set_int(ofp->filter, "all_channel_counts", 1, AV_OPT_SEARCH_CHILDREN)) < 0)
|
2013-04-11 15:58:08 +03:00
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2012-08-09 01:26:38 +03:00
|
|
|
#define AUTO_INSERT_FILTER(opt_name, filter_name, arg) do { \
|
|
|
|
AVFilterContext *filt_ctx; \
|
|
|
|
\
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofilter, AV_LOG_INFO, opt_name " is forwarded to lavfi " \
|
2012-08-09 01:26:38 +03:00
|
|
|
"similarly to -af " filter_name "=%s.\n", arg); \
|
|
|
|
\
|
|
|
|
ret = avfilter_graph_create_filter(&filt_ctx, \
|
|
|
|
avfilter_get_by_name(filter_name), \
|
2023-12-13 19:26:21 +02:00
|
|
|
filter_name, arg, NULL, graph); \
|
2012-08-09 01:26:38 +03:00
|
|
|
if (ret < 0) \
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail; \
|
2012-08-09 01:26:38 +03:00
|
|
|
\
|
|
|
|
ret = avfilter_link(last_filter, pad_idx, filt_ctx, 0); \
|
|
|
|
if (ret < 0) \
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail; \
|
2012-08-09 01:26:38 +03:00
|
|
|
\
|
|
|
|
last_filter = filt_ctx; \
|
|
|
|
pad_idx = 0; \
|
|
|
|
} while (0)
|
2021-04-06 05:26:31 +02:00
|
|
|
av_bprint_init(&args, 0, AV_BPRINT_SIZE_UNLIMITED);
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
choose_sample_fmts(ofp, &args);
|
|
|
|
choose_sample_rates(ofp, &args);
|
|
|
|
choose_channel_layouts(ofp, &args);
|
2021-04-06 05:26:31 +02:00
|
|
|
if (!av_bprint_is_complete(&args)) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (args.len) {
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterContext *format;
|
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
snprintf(name, sizeof(name), "format_out_%s", ofp->name);
|
2012-08-02 09:24:02 +03:00
|
|
|
ret = avfilter_graph_create_filter(&format,
|
|
|
|
avfilter_get_by_name("aformat"),
|
2023-12-13 19:26:21 +02:00
|
|
|
name, args.str, NULL, graph);
|
2012-08-02 09:24:02 +03:00
|
|
|
if (ret < 0)
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
ret = avfilter_link(last_filter, pad_idx, format, 0);
|
|
|
|
if (ret < 0)
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
|
|
|
last_filter = format;
|
|
|
|
pad_idx = 0;
|
|
|
|
}
|
|
|
|
|
2024-04-04 10:51:53 +02:00
|
|
|
if (ofilter->apad)
|
|
|
|
AUTO_INSERT_FILTER("-apad", "apad", ofilter->apad);
|
2013-05-09 00:43:19 +03:00
|
|
|
|
2024-04-02 11:40:41 +02:00
|
|
|
snprintf(name, sizeof(name), "trim for output %s", ofp->name);
|
2024-04-02 13:58:59 +02:00
|
|
|
ret = insert_trim(ofp->trim_start_us, ofp->trim_duration_us,
|
2013-06-15 10:59:40 +03:00
|
|
|
&last_filter, &pad_idx, name);
|
2013-04-10 16:02:01 +03:00
|
|
|
if (ret < 0)
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail;
|
2013-04-10 16:02:01 +03:00
|
|
|
|
2023-05-27 17:58:35 +02:00
|
|
|
if ((ret = avfilter_link(last_filter, pad_idx, ofp->filter, 0)) < 0)
|
2021-04-06 05:26:31 +02:00
|
|
|
goto fail;
|
|
|
|
fail:
|
|
|
|
av_bprint_finalize(&args, NULL);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2021-04-06 05:26:31 +02:00
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int configure_output_filter(FilterGraph *fg, AVFilterGraph *graph,
|
|
|
|
OutputFilter *ofilter, AVFilterInOut *out)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2024-04-02 12:25:33 +02:00
|
|
|
switch (ofilter->type) {
|
2023-12-13 19:26:21 +02:00
|
|
|
case AVMEDIA_TYPE_VIDEO: return configure_output_video_filter(fg, graph, ofilter, out);
|
|
|
|
case AVMEDIA_TYPE_AUDIO: return configure_output_audio_filter(fg, graph, ofilter, out);
|
2021-08-06 15:28:35 +02:00
|
|
|
default: av_assert0(0); return 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-24 10:08:33 +02:00
|
|
|
static void sub2video_prepare(InputFilterPriv *ifp)
|
2012-08-09 01:26:38 +03:00
|
|
|
{
|
2023-05-24 10:08:33 +02:00
|
|
|
ifp->sub2video.last_pts = INT64_MIN;
|
|
|
|
ifp->sub2video.end_pts = INT64_MIN;
|
2019-02-23 01:48:08 +02:00
|
|
|
|
|
|
|
/* sub2video structure has been (re-)initialized.
|
|
|
|
Mark it as such so that the system will be
|
|
|
|
initialized with the first received heartbeat. */
|
2023-05-24 10:08:33 +02:00
|
|
|
ifp->sub2video.initialize = 1;
|
2012-08-09 01:26:38 +03:00
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
|
|
|
|
InputFilter *ifilter, AVFilterInOut *in)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2023-04-25 23:55:44 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
AVFilterContext *last_filter;
|
2013-06-14 19:12:23 +03:00
|
|
|
const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
|
2021-09-21 14:29:15 +02:00
|
|
|
const AVPixFmtDescriptor *desc;
|
2024-02-13 13:18:27 +02:00
|
|
|
AVRational fr = ifp->opts.framerate;
|
2012-08-02 09:24:02 +03:00
|
|
|
AVRational sar;
|
2012-08-09 01:26:38 +03:00
|
|
|
AVBPrint args;
|
|
|
|
char name[255];
|
2013-06-15 10:59:40 +03:00
|
|
|
int ret, pad_idx = 0;
|
2016-05-08 23:39:20 +02:00
|
|
|
AVBufferSrcParameters *par = av_buffersrc_parameters_alloc();
|
2016-02-12 10:22:15 +02:00
|
|
|
if (!par)
|
|
|
|
return AVERROR(ENOMEM);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-05-24 10:08:33 +02:00
|
|
|
if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE)
|
|
|
|
sub2video_prepare(ifp);
|
2012-08-09 01:26:38 +03:00
|
|
|
|
2023-05-13 10:05:02 +02:00
|
|
|
sar = ifp->sample_aspect_ratio;
|
2012-08-09 01:26:38 +03:00
|
|
|
if(!sar.den)
|
|
|
|
sar = (AVRational){0,1};
|
2018-06-10 09:57:26 +02:00
|
|
|
av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
|
2012-08-09 01:26:38 +03:00
|
|
|
av_bprintf(&args,
|
|
|
|
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:"
|
2024-01-10 11:05:47 +02:00
|
|
|
"pixel_aspect=%d/%d:colorspace=%d:range=%d",
|
2023-05-13 10:05:02 +02:00
|
|
|
ifp->width, ifp->height, ifp->format,
|
2023-10-20 17:14:20 +02:00
|
|
|
ifp->time_base.num, ifp->time_base.den, sar.num, sar.den,
|
2024-01-10 11:05:47 +02:00
|
|
|
ifp->color_space, ifp->color_range);
|
2012-08-09 01:26:38 +03:00
|
|
|
if (fr.num && fr.den)
|
|
|
|
av_bprintf(&args, ":frame_rate=%d/%d", fr.num, fr.den);
|
2024-02-13 13:18:27 +02:00
|
|
|
snprintf(name, sizeof(name), "graph %d input from stream %s", fg->index,
|
|
|
|
ifp->opts.name);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2016-05-08 23:39:20 +02:00
|
|
|
|
2023-05-24 12:35:50 +02:00
|
|
|
if ((ret = avfilter_graph_create_filter(&ifp->filter, buffer_filt, name,
|
2023-12-13 19:26:21 +02:00
|
|
|
args.str, NULL, graph)) < 0)
|
2016-05-19 00:10:27 +02:00
|
|
|
goto fail;
|
2023-04-30 12:26:51 +02:00
|
|
|
par->hw_frames_ctx = ifp->hw_frames_ctx;
|
2023-05-24 12:35:50 +02:00
|
|
|
ret = av_buffersrc_parameters_set(ifp->filter, par);
|
2016-02-12 10:22:15 +02:00
|
|
|
if (ret < 0)
|
2016-05-19 00:10:27 +02:00
|
|
|
goto fail;
|
2016-05-08 23:39:20 +02:00
|
|
|
av_freep(&par);
|
2023-05-24 12:35:50 +02:00
|
|
|
last_filter = ifp->filter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-05-13 10:05:02 +02:00
|
|
|
desc = av_pix_fmt_desc_get(ifp->format);
|
2021-09-21 14:29:15 +02:00
|
|
|
av_assert0(desc);
|
|
|
|
|
2024-05-28 21:37:44 +02:00
|
|
|
if ((ifp->opts.flags & IFILTER_FLAG_CROP)) {
|
|
|
|
char crop_buf[64];
|
2024-07-18 03:57:46 +02:00
|
|
|
snprintf(crop_buf, sizeof(crop_buf), "w=iw-%u-%u:h=ih-%u-%u:x=%u:y=%u",
|
2024-05-28 21:37:44 +02:00
|
|
|
ifp->opts.crop_left, ifp->opts.crop_right,
|
|
|
|
ifp->opts.crop_top, ifp->opts.crop_bottom,
|
|
|
|
ifp->opts.crop_left, ifp->opts.crop_top);
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "crop", crop_buf);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-09-21 14:29:15 +02:00
|
|
|
// TODO: insert hwaccel enabled filters like transpose_vaapi into the graph
|
2024-03-22 21:01:06 +02:00
|
|
|
ifp->displaymatrix_applied = 0;
|
2024-02-13 13:18:27 +02:00
|
|
|
if ((ifp->opts.flags & IFILTER_FLAG_AUTOROTATE) &&
|
|
|
|
!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL)) {
|
2023-04-30 12:26:51 +02:00
|
|
|
int32_t *displaymatrix = ifp->displaymatrix;
|
2021-09-08 20:34:25 +02:00
|
|
|
double theta;
|
|
|
|
|
|
|
|
theta = get_rotation(displaymatrix);
|
2015-05-03 23:53:32 +02:00
|
|
|
|
|
|
|
if (fabs(theta - 90) < 1.0) {
|
2021-12-19 00:19:05 +02:00
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "transpose",
|
|
|
|
displaymatrix[3] > 0 ? "cclock_flip" : "clock");
|
2015-05-03 23:53:32 +02:00
|
|
|
} else if (fabs(theta - 180) < 1.0) {
|
2021-09-08 20:34:26 +02:00
|
|
|
if (displaymatrix[0] < 0) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "hflip", NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (displaymatrix[4] < 0) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
|
|
|
|
}
|
2015-05-03 23:53:32 +02:00
|
|
|
} else if (fabs(theta - 270) < 1.0) {
|
2021-12-19 00:19:05 +02:00
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "transpose",
|
|
|
|
displaymatrix[3] < 0 ? "clock_flip" : "cclock");
|
2015-05-03 23:53:32 +02:00
|
|
|
} else if (fabs(theta) > 1.0) {
|
|
|
|
char rotate_buf[64];
|
|
|
|
snprintf(rotate_buf, sizeof(rotate_buf), "%f*PI/180", theta);
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "rotate", rotate_buf);
|
2021-09-08 20:34:26 +02:00
|
|
|
} else if (fabs(theta) < 1.0) {
|
|
|
|
if (displaymatrix && displaymatrix[4] < 0) {
|
|
|
|
ret = insert_filter(&last_filter, &pad_idx, "vflip", NULL);
|
|
|
|
}
|
2015-04-29 16:59:01 +02:00
|
|
|
}
|
2015-05-03 23:53:32 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2024-03-22 21:01:06 +02:00
|
|
|
|
|
|
|
ifp->displaymatrix_applied = 1;
|
2015-04-29 16:59:01 +02:00
|
|
|
}
|
|
|
|
|
2024-02-13 13:18:27 +02:00
|
|
|
snprintf(name, sizeof(name), "trim_in_%s", ifp->opts.name);
|
2024-02-13 13:01:11 +02:00
|
|
|
ret = insert_trim(ifp->opts.trim_start_us, ifp->opts.trim_end_us,
|
2014-11-24 07:31:20 +02:00
|
|
|
&last_filter, &pad_idx, name);
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
return 0;
|
2016-05-19 00:10:27 +02:00
|
|
|
fail:
|
|
|
|
av_freep(&par);
|
|
|
|
|
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
|
|
|
|
InputFilter *ifilter, AVFilterInOut *in)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2023-04-25 23:55:44 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
2013-06-14 19:25:30 +03:00
|
|
|
AVFilterContext *last_filter;
|
2013-06-14 19:12:23 +03:00
|
|
|
const AVFilter *abuffer_filt = avfilter_get_by_name("abuffer");
|
2012-12-26 18:53:02 +03:00
|
|
|
AVBPrint args;
|
|
|
|
char name[255];
|
2013-06-15 10:59:40 +03:00
|
|
|
int ret, pad_idx = 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2012-12-26 18:53:02 +03:00
|
|
|
av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
|
|
|
|
av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
|
2023-04-25 23:55:44 +02:00
|
|
|
ifp->time_base.num, ifp->time_base.den,
|
2023-05-13 10:05:02 +02:00
|
|
|
ifp->sample_rate,
|
|
|
|
av_get_sample_fmt_name(ifp->format));
|
|
|
|
if (av_channel_layout_check(&ifp->ch_layout) &&
|
|
|
|
ifp->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
|
2021-09-03 02:04:30 +02:00
|
|
|
av_bprintf(&args, ":channel_layout=");
|
2023-05-13 10:05:02 +02:00
|
|
|
av_channel_layout_describe_bprint(&ifp->ch_layout, &args);
|
2021-09-03 02:04:30 +02:00
|
|
|
} else
|
2023-05-13 10:05:02 +02:00
|
|
|
av_bprintf(&args, ":channels=%d", ifp->ch_layout.nb_channels);
|
2024-02-13 13:18:27 +02:00
|
|
|
snprintf(name, sizeof(name), "graph_%d_in_%s", fg->index, ifp->opts.name);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-05-24 12:35:50 +02:00
|
|
|
if ((ret = avfilter_graph_create_filter(&ifp->filter, abuffer_filt,
|
2012-12-26 18:53:02 +03:00
|
|
|
name, args.str, NULL,
|
2023-12-13 19:26:21 +02:00
|
|
|
graph)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
2023-05-24 12:35:50 +02:00
|
|
|
last_filter = ifp->filter;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2024-02-13 13:18:27 +02:00
|
|
|
snprintf(name, sizeof(name), "trim for input stream %s", ifp->opts.name);
|
2024-02-13 13:01:11 +02:00
|
|
|
ret = insert_trim(ifp->opts.trim_start_us, ifp->opts.trim_end_us,
|
2014-11-24 07:31:20 +02:00
|
|
|
&last_filter, &pad_idx, name);
|
2013-06-15 10:59:40 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-06-14 19:25:30 +03:00
|
|
|
if ((ret = avfilter_link(last_filter, 0, in->filter_ctx, in->pad_idx)) < 0)
|
2012-08-02 09:24:02 +03:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int configure_input_filter(FilterGraph *fg, AVFilterGraph *graph,
|
|
|
|
InputFilter *ifilter, AVFilterInOut *in)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2023-05-13 19:59:45 +02:00
|
|
|
switch (ifp_from_ifilter(ifilter)->type) {
|
2023-12-13 19:26:21 +02:00
|
|
|
case AVMEDIA_TYPE_VIDEO: return configure_input_video_filter(fg, graph, ifilter, in);
|
|
|
|
case AVMEDIA_TYPE_AUDIO: return configure_input_audio_filter(fg, graph, ifilter, in);
|
2021-08-06 15:28:35 +02:00
|
|
|
default: av_assert0(0); return 0;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static void cleanup_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
|
2017-03-02 20:14:58 +02:00
|
|
|
{
|
2024-02-19 11:43:24 +02:00
|
|
|
for (int i = 0; i < fg->nb_outputs; i++)
|
2023-05-27 17:58:35 +02:00
|
|
|
ofp_from_ofilter(fg->outputs[i])->filter = NULL;
|
2024-02-19 11:43:24 +02:00
|
|
|
for (int i = 0; i < fg->nb_inputs; i++)
|
2023-05-24 12:35:50 +02:00
|
|
|
ifp_from_ifilter(fg->inputs[i])->filter = NULL;
|
2023-12-13 19:26:21 +02:00
|
|
|
avfilter_graph_free(&fgt->graph);
|
2017-03-02 20:14:58 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 12:22:57 +02:00
|
|
|
static int filter_is_buffersrc(const AVFilterContext *f)
|
|
|
|
{
|
|
|
|
return f->nb_inputs == 0 &&
|
2022-01-21 10:11:50 +02:00
|
|
|
(!strcmp(f->filter->name, "buffer") ||
|
|
|
|
!strcmp(f->filter->name, "abuffer"));
|
2021-11-23 12:22:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int graph_is_meta(AVFilterGraph *graph)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < graph->nb_filters; i++) {
|
|
|
|
const AVFilterContext *f = graph->filters[i];
|
|
|
|
|
|
|
|
/* in addition to filters flagged as meta, also
|
|
|
|
* disregard sinks and buffersources (but not other sources,
|
|
|
|
* since they introduce data we are not aware of)
|
|
|
|
*/
|
|
|
|
if (!((f->filter->flags & AVFILTER_FLAG_METADATA_ONLY) ||
|
|
|
|
f->nb_outputs == 0 ||
|
|
|
|
filter_is_buffersrc(f)))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int sub2video_frame(InputFilter *ifilter, AVFrame *frame, int buffer);
|
2023-11-03 16:54:29 +02:00
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int configure_filtergraph(FilterGraph *fg, FilterGraphThread *fgt)
|
2012-08-02 09:24:02 +03:00
|
|
|
{
|
2023-04-27 15:47:04 +02:00
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(fg);
|
2023-03-15 20:09:11 +02:00
|
|
|
AVBufferRef *hw_device;
|
2012-08-02 09:24:02 +03:00
|
|
|
AVFilterInOut *inputs, *outputs, *cur;
|
2016-05-22 17:57:59 +02:00
|
|
|
int ret, i, simple = filtergraph_is_simple(fg);
|
2023-07-18 16:37:52 +02:00
|
|
|
int have_input_eof = 0;
|
2023-05-21 19:16:39 +02:00
|
|
|
const char *graph_desc = fgp->graph_desc;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
cleanup_filtergraph(fg, fgt);
|
|
|
|
fgt->graph = avfilter_graph_alloc();
|
|
|
|
if (!fgt->graph)
|
2012-08-02 09:24:02 +03:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (simple) {
|
2024-04-02 13:58:59 +02:00
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[0]);
|
2012-12-19 05:47:28 +03:00
|
|
|
|
2021-09-03 15:30:32 +02:00
|
|
|
if (filter_nbthreads) {
|
2023-12-13 19:26:21 +02:00
|
|
|
ret = av_opt_set(fgt->graph, "threads", filter_nbthreads, 0);
|
2021-09-03 15:30:32 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2024-04-04 11:30:18 +02:00
|
|
|
} else if (fgp->nb_threads) {
|
|
|
|
ret = av_opt_set(fgt->graph, "threads", fgp->nb_threads, 0);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2021-08-08 11:42:20 +02:00
|
|
|
}
|
2016-10-30 15:25:39 +02:00
|
|
|
|
2024-04-02 13:58:59 +02:00
|
|
|
if (av_dict_count(ofp->sws_opts)) {
|
|
|
|
ret = av_dict_get_string(ofp->sws_opts,
|
2023-12-13 19:26:21 +02:00
|
|
|
&fgt->graph->scale_sws_opts,
|
2022-11-26 16:46:19 +02:00
|
|
|
'=', ':');
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2021-08-01 12:58:19 +02:00
|
|
|
}
|
2012-11-23 17:35:48 +03:00
|
|
|
|
2024-04-02 13:58:59 +02:00
|
|
|
if (av_dict_count(ofp->swr_opts)) {
|
2022-11-26 16:46:19 +02:00
|
|
|
char *args;
|
2024-04-02 13:58:59 +02:00
|
|
|
ret = av_dict_get_string(ofp->swr_opts, &args, '=', ':');
|
2022-11-26 16:46:19 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2023-12-13 19:26:21 +02:00
|
|
|
av_opt_set(fgt->graph, "aresample_swr_opts", args, 0);
|
2022-11-26 16:46:19 +02:00
|
|
|
av_free(args);
|
2013-02-24 21:07:42 +03:00
|
|
|
}
|
2016-10-30 15:25:39 +02:00
|
|
|
} else {
|
2023-12-13 19:26:21 +02:00
|
|
|
fgt->graph->nb_threads = filter_complex_nbthreads;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2023-03-15 20:09:11 +02:00
|
|
|
hw_device = hw_device_for_filter();
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
if ((ret = graph_parse(fgt->graph, graph_desc, &inputs, &outputs, hw_device)) < 0)
|
2020-04-13 17:33:19 +02:00
|
|
|
goto fail;
|
2016-03-24 01:48:13 +02:00
|
|
|
|
2012-08-02 09:24:02 +03:00
|
|
|
for (cur = inputs, i = 0; cur; cur = cur->next, i++)
|
2023-12-13 19:26:21 +02:00
|
|
|
if ((ret = configure_input_filter(fg, fgt->graph, fg->inputs[i], cur)) < 0) {
|
2014-08-30 13:27:50 +03:00
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
avfilter_inout_free(&outputs);
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2014-08-30 13:27:50 +03:00
|
|
|
}
|
2012-08-02 09:24:02 +03:00
|
|
|
avfilter_inout_free(&inputs);
|
|
|
|
|
2023-07-13 15:11:07 +02:00
|
|
|
for (cur = outputs, i = 0; cur; cur = cur->next, i++) {
|
2023-12-13 19:26:21 +02:00
|
|
|
ret = configure_output_filter(fg, fgt->graph, fg->outputs[i], cur);
|
2023-07-13 15:11:07 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
avfilter_inout_free(&outputs);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
2015-07-15 19:49:24 +02:00
|
|
|
avfilter_inout_free(&outputs);
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-07-06 17:35:19 +02:00
|
|
|
if (fgp->disable_conversions)
|
2023-12-13 19:26:21 +02:00
|
|
|
avfilter_graph_set_auto_convert(fgt->graph, AVFILTER_AUTO_CONVERT_NONE);
|
|
|
|
if ((ret = avfilter_graph_config(fgt->graph, NULL)) < 0)
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2012-08-02 09:24:02 +03:00
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
fgp->is_meta = graph_is_meta(fgt->graph);
|
2021-11-23 12:22:57 +02:00
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
/* limit the lists of allowed formats to the ones selected, to
|
|
|
|
* make sure they stay the same if the filtergraph is reconfigured later */
|
2024-02-19 11:43:24 +02:00
|
|
|
for (int i = 0; i < fg->nb_outputs; i++) {
|
2016-05-23 14:09:08 +02:00
|
|
|
OutputFilter *ofilter = fg->outputs[i];
|
2023-07-06 17:09:59 +02:00
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
|
2023-05-27 17:58:35 +02:00
|
|
|
AVFilterContext *sink = ofp->filter;
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
ofp->format = av_buffersink_get_format(sink);
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
ofp->width = av_buffersink_get_w(sink);
|
|
|
|
ofp->height = av_buffersink_get_h(sink);
|
2024-04-08 13:58:19 +02:00
|
|
|
ofp->color_space = av_buffersink_get_colorspace(sink);
|
|
|
|
ofp->color_range = av_buffersink_get_color_range(sink);
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
// If the timing parameters are not locked yet, get the tentative values
|
|
|
|
// here but don't lock them. They will only be used if no output frames
|
|
|
|
// are ever produced.
|
|
|
|
if (!ofp->tb_out_locked) {
|
|
|
|
AVRational fr = av_buffersink_get_frame_rate(sink);
|
|
|
|
if (ofp->fps.framerate.num <= 0 && ofp->fps.framerate.den <= 0 &&
|
|
|
|
fr.num > 0 && fr.den > 0)
|
|
|
|
ofp->fps.framerate = fr;
|
|
|
|
ofp->tb_out = av_buffersink_get_time_base(sink);
|
|
|
|
}
|
2023-07-06 21:31:49 +02:00
|
|
|
ofp->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink);
|
|
|
|
|
2023-07-06 17:09:59 +02:00
|
|
|
ofp->sample_rate = av_buffersink_get_sample_rate(sink);
|
2023-07-06 17:09:59 +02:00
|
|
|
av_channel_layout_uninit(&ofp->ch_layout);
|
|
|
|
ret = av_buffersink_get_ch_layout(sink, &ofp->ch_layout);
|
2021-09-03 02:04:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2016-05-23 14:09:08 +02:00
|
|
|
}
|
|
|
|
|
2024-02-19 11:43:24 +02:00
|
|
|
for (int i = 0; i < fg->nb_inputs; i++) {
|
2023-04-25 23:44:49 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
|
2022-01-10 12:04:36 +02:00
|
|
|
AVFrame *tmp;
|
2023-04-25 23:44:49 +02:00
|
|
|
while (av_fifo_read(ifp->frame_queue, &tmp, 1) >= 0) {
|
2023-06-14 18:08:10 +02:00
|
|
|
if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
|
2023-12-13 19:26:21 +02:00
|
|
|
sub2video_frame(&ifp->ifilter, tmp, !fgt->graph);
|
2023-06-14 18:08:10 +02:00
|
|
|
} else {
|
|
|
|
ret = av_buffersrc_add_frame(ifp->filter, tmp);
|
|
|
|
}
|
2016-06-27 18:59:23 +02:00
|
|
|
av_frame_free(&tmp);
|
|
|
|
if (ret < 0)
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2016-06-27 19:03:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send the EOFs for the finished inputs */
|
2024-02-19 11:43:24 +02:00
|
|
|
for (int i = 0; i < fg->nb_inputs; i++) {
|
2023-04-30 12:24:50 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
|
2023-06-08 20:36:33 +02:00
|
|
|
if (fgt->eof_in[i]) {
|
2023-05-24 12:35:50 +02:00
|
|
|
ret = av_buffersrc_add_frame(ifp->filter, NULL);
|
2016-06-27 19:03:42 +02:00
|
|
|
if (ret < 0)
|
2017-03-02 20:14:58 +02:00
|
|
|
goto fail;
|
2023-07-18 16:37:52 +02:00
|
|
|
have_input_eof = 1;
|
2016-06-27 18:59:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
if (have_input_eof) {
|
|
|
|
// make sure the EOF propagates to the end of the graph
|
2023-12-13 19:26:21 +02:00
|
|
|
ret = avfilter_graph_request_oldest(fgt->graph);
|
2023-07-18 16:37:52 +02:00
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-03-02 20:14:58 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
return 0;
|
2017-03-02 20:14:58 +02:00
|
|
|
fail:
|
2023-12-13 19:26:21 +02:00
|
|
|
cleanup_filtergraph(fg, fgt);
|
2017-03-02 20:14:58 +02:00
|
|
|
return ret;
|
2012-08-02 09:24:02 +03:00
|
|
|
}
|
|
|
|
|
2023-04-18 20:15:07 +02:00
|
|
|
static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *frame)
|
2016-05-23 14:09:08 +02:00
|
|
|
{
|
2023-04-30 12:26:51 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
2021-09-08 20:34:25 +02:00
|
|
|
AVFrameSideData *sd;
|
2021-09-03 02:04:30 +02:00
|
|
|
int ret;
|
2021-09-08 20:34:25 +02:00
|
|
|
|
2023-04-30 12:32:32 +02:00
|
|
|
ret = av_buffer_replace(&ifp->hw_frames_ctx, frame->hw_frames_ctx);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2024-02-13 17:35:21 +02:00
|
|
|
ifp->time_base = (ifp->type == AVMEDIA_TYPE_AUDIO) ? (AVRational){ 1, frame->sample_rate } :
|
|
|
|
(ifp->opts.flags & IFILTER_FLAG_CFR) ? av_inv_q(ifp->opts.framerate) :
|
|
|
|
frame->time_base;
|
|
|
|
|
2023-05-13 10:05:02 +02:00
|
|
|
ifp->format = frame->format;
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2023-05-13 10:05:02 +02:00
|
|
|
ifp->width = frame->width;
|
|
|
|
ifp->height = frame->height;
|
|
|
|
ifp->sample_aspect_ratio = frame->sample_aspect_ratio;
|
2023-10-20 17:14:20 +02:00
|
|
|
ifp->color_space = frame->colorspace;
|
|
|
|
ifp->color_range = frame->color_range;
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2023-05-13 10:05:02 +02:00
|
|
|
ifp->sample_rate = frame->sample_rate;
|
|
|
|
ret = av_channel_layout_copy(&ifp->ch_layout, &frame->ch_layout);
|
2021-09-03 02:04:30 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2016-05-23 14:09:08 +02:00
|
|
|
|
2021-09-08 20:34:25 +02:00
|
|
|
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
|
|
|
|
if (sd)
|
2023-05-13 13:36:40 +02:00
|
|
|
memcpy(ifp->displaymatrix, sd->data, sizeof(ifp->displaymatrix));
|
|
|
|
ifp->displaymatrix_present = !!sd;
|
2021-09-08 20:34:25 +02:00
|
|
|
|
2016-05-23 14:09:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-24 12:39:47 +02:00
|
|
|
int filtergraph_is_simple(const FilterGraph *fg)
|
2016-05-22 17:57:59 +02:00
|
|
|
{
|
2023-05-24 12:39:47 +02:00
|
|
|
const FilterGraphPriv *fgp = cfgp_from_cfg(fg);
|
2023-05-21 19:09:01 +02:00
|
|
|
return fgp->is_simple;
|
2016-05-22 17:57:59 +02:00
|
|
|
}
|
2023-04-18 15:24:46 +02:00
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static void send_command(FilterGraph *fg, AVFilterGraph *graph,
|
|
|
|
double time, const char *target,
|
2023-06-08 20:36:33 +02:00
|
|
|
const char *command, const char *arg, int all_filters)
|
2023-07-16 12:38:24 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
if (!graph)
|
2023-07-16 12:38:24 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (time < 0) {
|
|
|
|
char response[4096];
|
2023-12-13 19:26:21 +02:00
|
|
|
ret = avfilter_graph_send_command(graph, target, command, arg,
|
2023-07-16 12:38:24 +02:00
|
|
|
response, sizeof(response),
|
|
|
|
all_filters ? 0 : AVFILTER_CMD_FLAG_ONE);
|
|
|
|
fprintf(stderr, "Command reply for stream %d: ret:%d res:\n%s",
|
|
|
|
fg->index, ret, response);
|
|
|
|
} else if (!all_filters) {
|
|
|
|
fprintf(stderr, "Queuing commands only on filters supporting the specific command is unsupported\n");
|
|
|
|
} else {
|
2023-12-13 19:26:21 +02:00
|
|
|
ret = avfilter_graph_queue_command(graph, target, command, arg, 0, time);
|
2023-07-16 12:38:24 +02:00
|
|
|
if (ret < 0)
|
|
|
|
fprintf(stderr, "Queuing command failed with error %s\n", av_err2str(ret));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
static int choose_input(const FilterGraph *fg, const FilterGraphThread *fgt)
|
|
|
|
{
|
2024-03-07 16:21:30 +02:00
|
|
|
int nb_requests, nb_requests_max = -1;
|
2023-06-08 20:36:33 +02:00
|
|
|
int best_input = -1;
|
|
|
|
|
|
|
|
for (int i = 0; i < fg->nb_inputs; i++) {
|
|
|
|
InputFilter *ifilter = fg->inputs[i];
|
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
|
|
|
|
2023-07-19 19:29:18 +02:00
|
|
|
if (fgt->eof_in[i])
|
2023-06-08 20:36:33 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
nb_requests = av_buffersrc_get_nb_failed_requests(ifp->filter);
|
|
|
|
if (nb_requests > nb_requests_max) {
|
|
|
|
nb_requests_max = nb_requests;
|
|
|
|
best_input = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 19:29:18 +02:00
|
|
|
av_assert0(best_input >= 0);
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
return best_input;
|
|
|
|
}
|
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
static int choose_out_timebase(OutputFilterPriv *ofp, AVFrame *frame)
|
|
|
|
{
|
|
|
|
OutputFilter *ofilter = &ofp->ofilter;
|
|
|
|
FPSConvContext *fps = &ofp->fps;
|
|
|
|
AVRational tb = (AVRational){ 0, 0 };
|
|
|
|
AVRational fr;
|
2023-11-13 10:35:44 +02:00
|
|
|
const FrameData *fd;
|
2023-07-24 12:32:48 +02:00
|
|
|
|
2023-11-13 10:35:44 +02:00
|
|
|
fd = frame_data_c(frame);
|
2023-07-24 12:32:48 +02:00
|
|
|
|
|
|
|
// apply -enc_time_base
|
|
|
|
if (ofp->enc_timebase.num == ENC_TIME_BASE_DEMUX &&
|
|
|
|
(fd->dec.tb.num <= 0 || fd->dec.tb.den <= 0)) {
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_ERROR,
|
2023-07-24 12:32:48 +02:00
|
|
|
"Demuxing timebase not available - cannot use it for encoding\n");
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ofp->enc_timebase.num) {
|
|
|
|
case 0: break;
|
|
|
|
case ENC_TIME_BASE_DEMUX: tb = fd->dec.tb; break;
|
|
|
|
case ENC_TIME_BASE_FILTER: tb = frame->time_base; break;
|
|
|
|
default: tb = ofp->enc_timebase; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ofilter->type == AVMEDIA_TYPE_AUDIO) {
|
|
|
|
tb = tb.num ? tb : (AVRational){ 1, frame->sample_rate };
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
fr = fps->framerate;
|
|
|
|
if (!fr.num) {
|
|
|
|
AVRational fr_sink = av_buffersink_get_frame_rate(ofp->filter);
|
|
|
|
if (fr_sink.num > 0 && fr_sink.den > 0)
|
|
|
|
fr = fr_sink;
|
|
|
|
}
|
|
|
|
|
2024-04-01 06:29:16 +02:00
|
|
|
if (fps->vsync_method == VSYNC_CFR || fps->vsync_method == VSYNC_VSCFR) {
|
2023-07-24 12:32:48 +02:00
|
|
|
if (!fr.num && !fps->framerate_max.num) {
|
|
|
|
fr = (AVRational){25, 1};
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_WARNING,
|
2023-07-24 12:32:48 +02:00
|
|
|
"No information "
|
|
|
|
"about the input framerate is available. Falling "
|
|
|
|
"back to a default value of 25fps. Use the -r option "
|
|
|
|
"if you want a different framerate.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fps->framerate_max.num &&
|
|
|
|
(av_q2d(fr) > av_q2d(fps->framerate_max) ||
|
|
|
|
!fr.den))
|
|
|
|
fr = fps->framerate_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fr.num > 0) {
|
|
|
|
if (fps->framerate_supported) {
|
|
|
|
int idx = av_find_nearest_q_idx(fr, fps->framerate_supported);
|
|
|
|
fr = fps->framerate_supported[idx];
|
|
|
|
}
|
|
|
|
if (fps->framerate_clip) {
|
|
|
|
av_reduce(&fr.num, &fr.den,
|
|
|
|
fr.num, fr.den, fps->framerate_clip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(tb.num > 0 && tb.den > 0))
|
|
|
|
tb = av_inv_q(fr);
|
|
|
|
if (!(tb.num > 0 && tb.den > 0))
|
|
|
|
tb = frame->time_base;
|
|
|
|
|
2023-12-15 03:25:41 +02:00
|
|
|
fps->framerate = fr;
|
2023-07-24 12:32:48 +02:00
|
|
|
finish:
|
|
|
|
ofp->tb_out = tb;
|
|
|
|
ofp->tb_out_locked = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double adjust_frame_pts_to_encoder_tb(AVFrame *frame, AVRational tb_dst,
|
|
|
|
int64_t start_time)
|
|
|
|
{
|
|
|
|
double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
|
|
|
|
|
|
|
|
AVRational tb = tb_dst;
|
|
|
|
AVRational filter_tb = frame->time_base;
|
|
|
|
const int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
|
|
|
|
|
|
|
|
if (frame->pts == AV_NOPTS_VALUE)
|
|
|
|
goto early_exit;
|
|
|
|
|
|
|
|
tb.den <<= extra_bits;
|
|
|
|
float_pts = av_rescale_q(frame->pts, filter_tb, tb) -
|
|
|
|
av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
|
|
|
|
float_pts /= 1 << extra_bits;
|
|
|
|
// when float_pts is not exactly an integer,
|
|
|
|
// avoid exact midpoints to reduce the chance of rounding differences, this
|
|
|
|
// can be removed in case the fps code is changed to work with integers
|
|
|
|
if (float_pts != llrint(float_pts))
|
|
|
|
float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
|
|
|
|
|
|
|
|
frame->pts = av_rescale_q(frame->pts, filter_tb, tb_dst) -
|
|
|
|
av_rescale_q(start_time, AV_TIME_BASE_Q, tb_dst);
|
|
|
|
frame->time_base = tb_dst;
|
|
|
|
|
|
|
|
early_exit:
|
|
|
|
|
|
|
|
if (debug_ts) {
|
|
|
|
av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
|
|
|
|
frame ? av_ts2str(frame->pts) : "NULL",
|
|
|
|
av_ts2timestr(frame->pts, &tb_dst),
|
|
|
|
float_pts, tb_dst.num, tb_dst.den);
|
|
|
|
}
|
|
|
|
|
|
|
|
return float_pts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert frame timestamps to the encoder timebase and decide how many times
|
|
|
|
* should this (and possibly previous) frame be repeated in order to conform to
|
|
|
|
* desired target framerate (if any).
|
|
|
|
*/
|
|
|
|
static void video_sync_process(OutputFilterPriv *ofp, AVFrame *frame,
|
|
|
|
int64_t *nb_frames, int64_t *nb_frames_prev)
|
|
|
|
{
|
|
|
|
OutputFilter *ofilter = &ofp->ofilter;
|
|
|
|
FPSConvContext *fps = &ofp->fps;
|
|
|
|
double delta0, delta, sync_ipts, duration;
|
|
|
|
|
|
|
|
if (!frame) {
|
|
|
|
*nb_frames_prev = *nb_frames = mid_pred(fps->frames_prev_hist[0],
|
|
|
|
fps->frames_prev_hist[1],
|
|
|
|
fps->frames_prev_hist[2]);
|
|
|
|
|
|
|
|
if (!*nb_frames && fps->last_dropped) {
|
2023-07-18 16:37:52 +02:00
|
|
|
atomic_fetch_add(&ofilter->nb_frames_drop, 1);
|
2023-07-24 12:32:48 +02:00
|
|
|
fps->last_dropped++;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
duration = frame->duration * av_q2d(frame->time_base) / av_q2d(ofp->tb_out);
|
|
|
|
|
|
|
|
sync_ipts = adjust_frame_pts_to_encoder_tb(frame, ofp->tb_out, ofp->ts_offset);
|
|
|
|
/* delta0 is the "drift" between the input frame and
|
|
|
|
* where it would fall in the output. */
|
|
|
|
delta0 = sync_ipts - ofp->next_pts;
|
|
|
|
delta = delta0 + duration;
|
|
|
|
|
|
|
|
// tracks the number of times the PREVIOUS frame should be duplicated,
|
|
|
|
// mostly for variable framerate (VFR)
|
|
|
|
*nb_frames_prev = 0;
|
|
|
|
/* by default, we output a single frame */
|
|
|
|
*nb_frames = 1;
|
|
|
|
|
|
|
|
if (delta0 < 0 &&
|
|
|
|
delta > 0 &&
|
2024-04-01 06:29:16 +02:00
|
|
|
fps->vsync_method != VSYNC_PASSTHROUGH
|
2023-12-14 14:42:17 +02:00
|
|
|
#if FFMPEG_OPT_VSYNC_DROP
|
2024-04-01 06:29:16 +02:00
|
|
|
&& fps->vsync_method != VSYNC_DROP
|
2023-12-14 14:42:17 +02:00
|
|
|
#endif
|
|
|
|
) {
|
2023-07-24 12:32:48 +02:00
|
|
|
if (delta0 < -0.6) {
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_VERBOSE, "Past duration %f too large\n", -delta0);
|
2023-07-24 12:32:48 +02:00
|
|
|
} else
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_DEBUG, "Clipping frame in rate conversion by %f\n", -delta0);
|
2023-07-24 12:32:48 +02:00
|
|
|
sync_ipts = ofp->next_pts;
|
|
|
|
duration += delta0;
|
|
|
|
delta0 = 0;
|
|
|
|
}
|
|
|
|
|
2024-04-01 06:29:16 +02:00
|
|
|
switch (fps->vsync_method) {
|
2023-07-24 12:32:48 +02:00
|
|
|
case VSYNC_VSCFR:
|
|
|
|
if (fps->frame_number == 0 && delta0 >= 0.5) {
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_DEBUG, "Not duplicating %d initial frames\n", (int)lrintf(delta0));
|
2023-07-24 12:32:48 +02:00
|
|
|
delta = duration;
|
|
|
|
delta0 = 0;
|
|
|
|
ofp->next_pts = llrint(sync_ipts);
|
|
|
|
}
|
|
|
|
case VSYNC_CFR:
|
|
|
|
// FIXME set to 0.5 after we fix some dts/pts bugs like in avidec.c
|
|
|
|
if (frame_drop_threshold && delta < frame_drop_threshold && fps->frame_number) {
|
|
|
|
*nb_frames = 0;
|
|
|
|
} else if (delta < -1.1)
|
|
|
|
*nb_frames = 0;
|
|
|
|
else if (delta > 1.1) {
|
|
|
|
*nb_frames = llrintf(delta);
|
|
|
|
if (delta0 > 1.1)
|
|
|
|
*nb_frames_prev = llrintf(delta0 - 0.6);
|
|
|
|
}
|
|
|
|
frame->duration = 1;
|
|
|
|
break;
|
|
|
|
case VSYNC_VFR:
|
|
|
|
if (delta <= -0.6)
|
|
|
|
*nb_frames = 0;
|
|
|
|
else if (delta > 0.6)
|
|
|
|
ofp->next_pts = llrint(sync_ipts);
|
|
|
|
frame->duration = llrint(duration);
|
|
|
|
break;
|
2023-12-14 14:42:17 +02:00
|
|
|
#if FFMPEG_OPT_VSYNC_DROP
|
2023-07-24 12:32:48 +02:00
|
|
|
case VSYNC_DROP:
|
2023-12-14 14:42:17 +02:00
|
|
|
#endif
|
2023-07-24 12:32:48 +02:00
|
|
|
case VSYNC_PASSTHROUGH:
|
|
|
|
ofp->next_pts = llrint(sync_ipts);
|
|
|
|
frame->duration = llrint(duration);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
av_assert0(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
|
|
|
memmove(fps->frames_prev_hist + 1,
|
|
|
|
fps->frames_prev_hist,
|
|
|
|
sizeof(fps->frames_prev_hist[0]) * (FF_ARRAY_ELEMS(fps->frames_prev_hist) - 1));
|
|
|
|
fps->frames_prev_hist[0] = *nb_frames_prev;
|
|
|
|
|
|
|
|
if (*nb_frames_prev == 0 && fps->last_dropped) {
|
2023-07-18 16:37:52 +02:00
|
|
|
atomic_fetch_add(&ofilter->nb_frames_drop, 1);
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_VERBOSE,
|
2023-07-24 12:32:48 +02:00
|
|
|
"*** dropping frame %"PRId64" at ts %"PRId64"\n",
|
|
|
|
fps->frame_number, fps->last_frame->pts);
|
|
|
|
}
|
|
|
|
if (*nb_frames > (*nb_frames_prev && fps->last_dropped) + (*nb_frames > *nb_frames_prev)) {
|
2023-07-18 16:37:52 +02:00
|
|
|
uint64_t nb_frames_dup;
|
2023-07-24 12:32:48 +02:00
|
|
|
if (*nb_frames > dts_error_threshold * 30) {
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_ERROR, "%"PRId64" frame duplication too large, skipping\n", *nb_frames - 1);
|
2023-07-18 16:37:52 +02:00
|
|
|
atomic_fetch_add(&ofilter->nb_frames_drop, 1);
|
2023-07-24 12:32:48 +02:00
|
|
|
*nb_frames = 0;
|
|
|
|
return;
|
|
|
|
}
|
2023-07-18 16:37:52 +02:00
|
|
|
nb_frames_dup = atomic_fetch_add(&ofilter->nb_frames_dup,
|
|
|
|
*nb_frames - (*nb_frames_prev && fps->last_dropped) - (*nb_frames > *nb_frames_prev));
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_VERBOSE, "*** %"PRId64" dup!\n", *nb_frames - 1);
|
2023-07-18 16:37:52 +02:00
|
|
|
if (nb_frames_dup > fps->dup_warning) {
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_WARNING, "More than %"PRIu64" frames duplicated\n", fps->dup_warning);
|
2023-07-24 12:32:48 +02:00
|
|
|
fps->dup_warning *= 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fps->last_dropped = *nb_frames == *nb_frames_prev && frame;
|
|
|
|
fps->dropped_keyframe |= fps->last_dropped && (frame->flags & AV_FRAME_FLAG_KEY);
|
|
|
|
}
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
static int close_output(OutputFilterPriv *ofp, FilterGraphThread *fgt)
|
|
|
|
{
|
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
// we are finished and no frames were ever seen at this output,
|
|
|
|
// at least initialize the encoder with a dummy frame
|
|
|
|
if (!fgt->got_frame) {
|
|
|
|
AVFrame *frame = fgt->frame;
|
|
|
|
FrameData *fd;
|
|
|
|
|
|
|
|
frame->time_base = ofp->tb_out;
|
|
|
|
frame->format = ofp->format;
|
|
|
|
|
|
|
|
frame->width = ofp->width;
|
|
|
|
frame->height = ofp->height;
|
|
|
|
frame->sample_aspect_ratio = ofp->sample_aspect_ratio;
|
|
|
|
|
|
|
|
frame->sample_rate = ofp->sample_rate;
|
|
|
|
if (ofp->ch_layout.nb_channels) {
|
|
|
|
ret = av_channel_layout_copy(&frame->ch_layout, &ofp->ch_layout);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = frame_data(frame);
|
|
|
|
if (!fd)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
fd->frame_rate_filter = ofp->fps.framerate;
|
|
|
|
|
|
|
|
av_assert0(!frame->buf[0]);
|
|
|
|
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_WARNING,
|
2023-07-18 16:37:52 +02:00
|
|
|
"No filtered frames for output stream, trying to "
|
|
|
|
"initialize anyway.\n");
|
|
|
|
|
|
|
|
ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->index, frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_unref(frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fgt->eof_out[ofp->index] = 1;
|
|
|
|
|
2024-03-09 20:53:11 +02:00
|
|
|
ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->index, NULL);
|
|
|
|
return (ret == AVERROR_EOF) ? 0 : ret;
|
2023-07-18 16:37:52 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
static int fg_output_frame(OutputFilterPriv *ofp, FilterGraphThread *fgt,
|
2023-07-18 16:37:52 +02:00
|
|
|
AVFrame *frame)
|
2023-07-24 12:32:48 +02:00
|
|
|
{
|
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
|
|
|
|
AVFrame *frame_prev = ofp->fps.last_frame;
|
|
|
|
enum AVMediaType type = ofp->ofilter.type;
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
int64_t nb_frames = !!frame, nb_frames_prev = 0;
|
2023-07-24 12:32:48 +02:00
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
if (type == AVMEDIA_TYPE_VIDEO && (frame || fgt->got_frame))
|
2023-07-24 12:32:48 +02:00
|
|
|
video_sync_process(ofp, frame, &nb_frames, &nb_frames_prev);
|
|
|
|
|
|
|
|
for (int64_t i = 0; i < nb_frames; i++) {
|
|
|
|
AVFrame *frame_out;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
AVFrame *frame_in = (i < nb_frames_prev && frame_prev->buf[0]) ?
|
|
|
|
frame_prev : frame;
|
|
|
|
if (!frame_in)
|
|
|
|
break;
|
|
|
|
|
|
|
|
frame_out = fgp->frame_enc;
|
|
|
|
ret = av_frame_ref(frame_out, frame_in);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
frame_out->pts = ofp->next_pts;
|
|
|
|
|
|
|
|
if (ofp->fps.dropped_keyframe) {
|
|
|
|
frame_out->flags |= AV_FRAME_FLAG_KEY;
|
|
|
|
ofp->fps.dropped_keyframe = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
frame->pts = (frame->pts == AV_NOPTS_VALUE) ? ofp->next_pts :
|
|
|
|
av_rescale_q(frame->pts, frame->time_base, ofp->tb_out) -
|
|
|
|
av_rescale_q(ofp->ts_offset, AV_TIME_BASE_Q, ofp->tb_out);
|
|
|
|
|
|
|
|
frame->time_base = ofp->tb_out;
|
|
|
|
frame->duration = av_rescale_q(frame->nb_samples,
|
|
|
|
(AVRational){ 1, frame->sample_rate },
|
|
|
|
ofp->tb_out);
|
|
|
|
|
|
|
|
ofp->next_pts = frame->pts + frame->duration;
|
|
|
|
|
|
|
|
frame_out = frame;
|
|
|
|
}
|
|
|
|
|
2024-03-09 20:58:46 +02:00
|
|
|
// send the frame to consumers
|
|
|
|
ret = sch_filter_send(fgp->sch, fgp->sch_idx, ofp->index, frame_out);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_unref(frame_out);
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2024-03-09 20:58:46 +02:00
|
|
|
if (!fgt->eof_out[ofp->index]) {
|
|
|
|
fgt->eof_out[ofp->index] = 1;
|
|
|
|
fgp->nb_outputs_done++;
|
2023-06-08 20:36:33 +02:00
|
|
|
}
|
2024-03-09 20:58:46 +02:00
|
|
|
|
|
|
|
return ret == AVERROR_EOF ? 0 : ret;
|
2023-06-08 20:36:33 +02:00
|
|
|
}
|
2023-07-24 12:32:48 +02:00
|
|
|
|
|
|
|
if (type == AVMEDIA_TYPE_VIDEO) {
|
|
|
|
ofp->fps.frame_number++;
|
|
|
|
ofp->next_pts++;
|
|
|
|
|
|
|
|
if (i == nb_frames_prev && frame)
|
|
|
|
frame->flags &= ~AV_FRAME_FLAG_KEY;
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
fgt->got_frame = 1;
|
2023-07-24 12:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (frame && frame_prev) {
|
|
|
|
av_frame_unref(frame_prev);
|
|
|
|
av_frame_move_ref(frame_prev, frame);
|
|
|
|
}
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
if (!frame)
|
|
|
|
return close_output(ofp, fgt);
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
static int fg_output_step(OutputFilterPriv *ofp, FilterGraphThread *fgt,
|
2023-07-18 16:37:52 +02:00
|
|
|
AVFrame *frame)
|
2023-04-18 15:24:46 +02:00
|
|
|
{
|
2023-07-23 10:19:08 +02:00
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(ofp->ofilter.graph);
|
2023-07-23 10:20:20 +02:00
|
|
|
AVFilterContext *filter = ofp->filter;
|
|
|
|
FrameData *fd;
|
2023-07-23 10:19:08 +02:00
|
|
|
int ret;
|
2023-05-27 17:38:36 +02:00
|
|
|
|
2023-07-23 10:35:08 +02:00
|
|
|
ret = av_buffersink_get_frame_flags(filter, frame,
|
|
|
|
AV_BUFFERSINK_FLAG_NO_REQUEST);
|
2023-07-18 16:37:52 +02:00
|
|
|
if (ret == AVERROR_EOF && !fgt->eof_out[ofp->index]) {
|
|
|
|
ret = fg_output_frame(ofp, fgt, NULL);
|
2023-09-12 11:58:12 +02:00
|
|
|
return (ret < 0) ? ret : 1;
|
|
|
|
} else if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
|
2023-07-23 10:20:20 +02:00
|
|
|
return 1;
|
2023-09-12 11:58:12 +02:00
|
|
|
} else if (ret < 0) {
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_WARNING,
|
2023-09-12 11:58:12 +02:00
|
|
|
"Error in retrieving a frame from the filtergraph: %s\n",
|
|
|
|
av_err2str(ret));
|
|
|
|
return ret;
|
2023-07-23 10:20:20 +02:00
|
|
|
}
|
2023-06-08 20:36:33 +02:00
|
|
|
|
|
|
|
if (fgt->eof_out[ofp->index]) {
|
2023-07-23 10:35:08 +02:00
|
|
|
av_frame_unref(frame);
|
2023-07-23 10:20:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2023-04-18 15:24:46 +02:00
|
|
|
|
2023-07-24 12:32:48 +02:00
|
|
|
frame->time_base = av_buffersink_get_time_base(filter);
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
if (debug_ts)
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_INFO, "filter_raw -> pts:%s pts_time:%s time_base:%d/%d\n",
|
2023-06-08 20:36:33 +02:00
|
|
|
av_ts2str(frame->pts), av_ts2timestr(frame->pts, &frame->time_base),
|
|
|
|
frame->time_base.num, frame->time_base.den);
|
2023-07-24 12:32:48 +02:00
|
|
|
|
|
|
|
// Choose the output timebase the first time we get a frame.
|
|
|
|
if (!ofp->tb_out_locked) {
|
|
|
|
ret = choose_out_timebase(ofp, frame);
|
|
|
|
if (ret < 0) {
|
2024-04-02 13:40:42 +02:00
|
|
|
av_log(ofp, AV_LOG_ERROR, "Could not choose an output time base\n");
|
2023-07-24 12:32:48 +02:00
|
|
|
av_frame_unref(frame);
|
|
|
|
return ret;
|
|
|
|
}
|
2023-07-23 10:20:20 +02:00
|
|
|
}
|
2023-04-18 15:24:46 +02:00
|
|
|
|
2023-07-23 10:35:08 +02:00
|
|
|
fd = frame_data(frame);
|
2023-07-23 10:20:20 +02:00
|
|
|
if (!fd) {
|
2023-07-23 10:35:08 +02:00
|
|
|
av_frame_unref(frame);
|
2023-07-23 10:20:20 +02:00
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
2023-05-27 17:38:36 +02:00
|
|
|
|
2023-11-24 20:08:15 +02:00
|
|
|
fd->wallclock[LATENCY_PROBE_FILTER_POST] = av_gettime_relative();
|
|
|
|
|
2023-07-23 10:20:20 +02:00
|
|
|
// only use bits_per_raw_sample passed through from the decoder
|
|
|
|
// if the filtergraph did not touch the frame data
|
|
|
|
if (!fgp->is_meta)
|
|
|
|
fd->bits_per_raw_sample = 0;
|
2023-05-27 12:32:38 +02:00
|
|
|
|
2024-04-02 13:44:22 +02:00
|
|
|
if (ofp->ofilter.type == AVMEDIA_TYPE_VIDEO) {
|
2023-07-24 12:32:48 +02:00
|
|
|
if (!frame->duration) {
|
|
|
|
AVRational fr = av_buffersink_get_frame_rate(filter);
|
|
|
|
if (fr.num > 0 && fr.den > 0)
|
2023-07-21 18:39:08 +02:00
|
|
|
frame->duration = av_rescale_q(1, av_inv_q(fr), frame->time_base);
|
|
|
|
}
|
2023-07-24 12:32:48 +02:00
|
|
|
|
|
|
|
fd->frame_rate_filter = ofp->fps.framerate;
|
2023-07-23 10:20:20 +02:00
|
|
|
}
|
2023-05-27 12:32:38 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
ret = fg_output_frame(ofp, fgt, frame);
|
2023-07-23 10:35:08 +02:00
|
|
|
av_frame_unref(frame);
|
2023-07-23 10:20:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2023-07-13 13:34:32 +02:00
|
|
|
|
2023-07-23 10:19:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
/* retrieve all frames available at filtergraph outputs
|
|
|
|
* and send them to consumers */
|
2023-06-08 20:36:33 +02:00
|
|
|
static int read_frames(FilterGraph *fg, FilterGraphThread *fgt,
|
2023-07-18 16:37:52 +02:00
|
|
|
AVFrame *frame)
|
2023-07-23 10:19:08 +02:00
|
|
|
{
|
2023-06-08 20:36:33 +02:00
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(fg);
|
2023-07-18 16:37:52 +02:00
|
|
|
int did_step = 0;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
// graph not configured, just select the input to request
|
2023-12-13 19:26:21 +02:00
|
|
|
if (!fgt->graph) {
|
2023-07-18 16:37:52 +02:00
|
|
|
for (int i = 0; i < fg->nb_inputs; i++) {
|
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(fg->inputs[i]);
|
|
|
|
if (ifp->format < 0 && !fgt->eof_in[i]) {
|
|
|
|
fgt->next_in = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2023-07-23 10:19:08 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
// This state - graph is not configured, but all inputs are either
|
|
|
|
// initialized or EOF - should be unreachable because sending EOF to a
|
|
|
|
// filter without even a fallback format should fail
|
|
|
|
av_assert0(0);
|
|
|
|
return AVERROR_BUG;
|
|
|
|
}
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
while (fgp->nb_outputs_done < fg->nb_outputs) {
|
|
|
|
int ret;
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
ret = avfilter_graph_request_oldest(fgt->graph);
|
2023-07-18 16:37:52 +02:00
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
fgt->next_in = choose_input(fg, fgt);
|
|
|
|
break;
|
|
|
|
} else if (ret < 0) {
|
|
|
|
if (ret == AVERROR_EOF)
|
|
|
|
av_log(fg, AV_LOG_VERBOSE, "Filtergraph returned EOF, finishing\n");
|
|
|
|
else
|
|
|
|
av_log(fg, AV_LOG_ERROR,
|
|
|
|
"Error requesting a frame from the filtergraph: %s\n",
|
|
|
|
av_err2str(ret));
|
|
|
|
return ret;
|
2023-06-08 20:36:33 +02:00
|
|
|
}
|
2023-07-18 16:37:52 +02:00
|
|
|
fgt->next_in = fg->nb_inputs;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
// return after one iteration, so that scheduler can rate-control us
|
|
|
|
if (did_step && fgp->have_sources)
|
|
|
|
return 0;
|
2023-07-23 10:19:08 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
/* Reap all buffers present in the buffer sinks */
|
|
|
|
for (int i = 0; i < fg->nb_outputs; i++) {
|
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
while (!ret) {
|
|
|
|
ret = fg_output_step(ofp, fgt, frame);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
2023-04-18 15:24:46 +02:00
|
|
|
}
|
2023-07-18 16:37:52 +02:00
|
|
|
did_step = 1;
|
2023-12-19 17:37:02 +02:00
|
|
|
}
|
2023-04-18 15:24:46 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
return (fgp->nb_outputs_done == fg->nb_outputs) ? AVERROR_EOF : 0;
|
2023-04-18 15:24:46 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
static void sub2video_heartbeat(InputFilter *ifilter, int64_t pts, AVRational tb)
|
2023-05-24 10:08:33 +02:00
|
|
|
{
|
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
|
|
|
int64_t pts2;
|
|
|
|
|
|
|
|
/* subtitles seem to be usually muxed ahead of other streams;
|
|
|
|
if not, subtracting a larger time here is necessary */
|
|
|
|
pts2 = av_rescale_q(pts, tb, ifp->time_base) - 1;
|
|
|
|
|
|
|
|
/* do not send the heartbeat frame if the subtitle is already ahead */
|
|
|
|
if (pts2 <= ifp->sub2video.last_pts)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (pts2 >= ifp->sub2video.end_pts || ifp->sub2video.initialize)
|
|
|
|
/* if we have hit the end of the current displayed subpicture,
|
|
|
|
or if we need to initialize the system, update the
|
|
|
|
overlayed subpicture and its start/end times */
|
|
|
|
sub2video_update(ifp, pts2 + 1, NULL);
|
2023-11-02 20:57:59 +02:00
|
|
|
else
|
2023-05-24 10:08:33 +02:00
|
|
|
sub2video_push_ref(ifp, pts2);
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
static int sub2video_frame(InputFilter *ifilter, AVFrame *frame, int buffer)
|
2023-05-23 13:00:12 +02:00
|
|
|
{
|
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
|
|
|
int ret;
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
if (buffer) {
|
2023-11-03 16:54:29 +02:00
|
|
|
AVFrame *tmp;
|
|
|
|
|
|
|
|
if (!frame)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
tmp = av_frame_alloc();
|
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
av_frame_move_ref(tmp, frame);
|
|
|
|
|
|
|
|
ret = av_fifo_write(ifp->frame_queue, &tmp, 1);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&tmp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
// heartbeat frame
|
|
|
|
if (frame && !frame->buf[0]) {
|
|
|
|
sub2video_heartbeat(ifilter, frame->pts, frame->time_base);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-03 16:56:51 +02:00
|
|
|
if (!frame) {
|
|
|
|
if (ifp->sub2video.end_pts < INT64_MAX)
|
|
|
|
sub2video_update(ifp, INT64_MAX, NULL);
|
2023-05-24 10:08:33 +02:00
|
|
|
|
2023-11-03 16:56:51 +02:00
|
|
|
return av_buffersrc_add_frame(ifp->filter, NULL);
|
|
|
|
}
|
2023-05-24 10:08:33 +02:00
|
|
|
|
2023-11-03 16:56:51 +02:00
|
|
|
ifp->width = frame->width ? frame->width : ifp->width;
|
|
|
|
ifp->height = frame->height ? frame->height : ifp->height;
|
2023-06-14 18:34:57 +02:00
|
|
|
|
2023-11-03 16:56:51 +02:00
|
|
|
sub2video_update(ifp, INT64_MIN, (const AVSubtitle*)frame->buf[0]->data);
|
2023-05-23 13:00:12 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
static int send_eof(FilterGraphThread *fgt, InputFilter *ifilter,
|
|
|
|
int64_t pts, AVRational tb)
|
2023-04-18 15:24:46 +02:00
|
|
|
{
|
2023-04-26 00:00:02 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
2023-04-18 15:24:46 +02:00
|
|
|
int ret;
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
if (fgt->eof_in[ifp->index])
|
|
|
|
return 0;
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
fgt->eof_in[ifp->index] = 1;
|
2023-04-18 15:24:46 +02:00
|
|
|
|
2023-05-24 12:35:50 +02:00
|
|
|
if (ifp->filter) {
|
2023-04-26 00:00:02 +02:00
|
|
|
pts = av_rescale_q_rnd(pts, tb, ifp->time_base,
|
|
|
|
AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX);
|
|
|
|
|
2023-05-24 12:35:50 +02:00
|
|
|
ret = av_buffersrc_close(ifp->filter, pts, AV_BUFFERSRC_FLAG_PUSH);
|
2023-04-18 15:24:46 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
} else {
|
2023-05-13 10:05:02 +02:00
|
|
|
if (ifp->format < 0) {
|
2023-05-11 11:59:44 +02:00
|
|
|
// the filtergraph was never configured, use the fallback parameters
|
2024-02-22 14:24:38 +02:00
|
|
|
ifp->format = ifp->opts.fallback->format;
|
|
|
|
ifp->sample_rate = ifp->opts.fallback->sample_rate;
|
|
|
|
ifp->width = ifp->opts.fallback->width;
|
|
|
|
ifp->height = ifp->opts.fallback->height;
|
|
|
|
ifp->sample_aspect_ratio = ifp->opts.fallback->sample_aspect_ratio;
|
|
|
|
ifp->color_space = ifp->opts.fallback->colorspace;
|
|
|
|
ifp->color_range = ifp->opts.fallback->color_range;
|
|
|
|
ifp->time_base = ifp->opts.fallback->time_base;
|
2023-05-11 11:59:44 +02:00
|
|
|
|
2023-05-13 10:05:02 +02:00
|
|
|
ret = av_channel_layout_copy(&ifp->ch_layout,
|
2024-02-22 14:24:38 +02:00
|
|
|
&ifp->opts.fallback->ch_layout);
|
2023-05-11 11:59:44 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2023-05-11 12:02:49 +02:00
|
|
|
|
|
|
|
if (ifilter_has_all_input_formats(ifilter->graph)) {
|
2023-06-08 20:36:33 +02:00
|
|
|
ret = configure_filtergraph(ifilter->graph, fgt);
|
2023-05-11 12:02:49 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
av_log(NULL, AV_LOG_ERROR, "Error initializing filters!\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
2023-05-11 11:59:44 +02:00
|
|
|
}
|
2023-04-30 12:17:16 +02:00
|
|
|
|
2023-05-23 22:54:23 +02:00
|
|
|
if (ifp->format < 0) {
|
2023-05-13 10:29:01 +02:00
|
|
|
av_log(NULL, AV_LOG_ERROR,
|
2024-02-13 13:18:27 +02:00
|
|
|
"Cannot determine format of input %s after EOF\n",
|
|
|
|
ifp->opts.name);
|
2023-04-18 15:24:46 +02:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-29 15:12:12 +02:00
|
|
|
enum ReinitReason {
|
|
|
|
VIDEO_CHANGED = (1 << 0),
|
|
|
|
AUDIO_CHANGED = (1 << 1),
|
|
|
|
MATRIX_CHANGED = (1 << 2),
|
|
|
|
HWACCEL_CHANGED = (1 << 3)
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *unknown_if_null(const char *str)
|
|
|
|
{
|
|
|
|
return str ? str : "unknown";
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
static int send_frame(FilterGraph *fg, FilterGraphThread *fgt,
|
|
|
|
InputFilter *ifilter, AVFrame *frame)
|
2023-04-18 15:24:46 +02:00
|
|
|
{
|
2023-04-25 23:44:49 +02:00
|
|
|
InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
|
2023-11-24 20:08:15 +02:00
|
|
|
FrameData *fd;
|
2023-04-18 15:24:46 +02:00
|
|
|
AVFrameSideData *sd;
|
2023-12-29 15:12:12 +02:00
|
|
|
int need_reinit = 0, ret;
|
2023-04-18 15:24:46 +02:00
|
|
|
|
|
|
|
/* determine if the parameters for this input changed */
|
2023-05-13 19:59:45 +02:00
|
|
|
switch (ifp->type) {
|
2023-04-18 15:24:46 +02:00
|
|
|
case AVMEDIA_TYPE_AUDIO:
|
2023-12-29 15:12:12 +02:00
|
|
|
if (ifp->format != frame->format ||
|
|
|
|
ifp->sample_rate != frame->sample_rate ||
|
|
|
|
av_channel_layout_compare(&ifp->ch_layout, &frame->ch_layout))
|
|
|
|
need_reinit |= AUDIO_CHANGED;
|
2023-04-18 15:24:46 +02:00
|
|
|
break;
|
|
|
|
case AVMEDIA_TYPE_VIDEO:
|
2023-12-29 15:12:12 +02:00
|
|
|
if (ifp->format != frame->format ||
|
|
|
|
ifp->width != frame->width ||
|
|
|
|
ifp->height != frame->height ||
|
|
|
|
ifp->color_space != frame->colorspace ||
|
|
|
|
ifp->color_range != frame->color_range)
|
|
|
|
need_reinit |= VIDEO_CHANGED;
|
2023-04-18 15:24:46 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX)) {
|
2023-05-13 13:36:40 +02:00
|
|
|
if (!ifp->displaymatrix_present ||
|
|
|
|
memcmp(sd->data, ifp->displaymatrix, sizeof(ifp->displaymatrix)))
|
2023-12-29 15:12:12 +02:00
|
|
|
need_reinit |= MATRIX_CHANGED;
|
2023-05-13 13:36:40 +02:00
|
|
|
} else if (ifp->displaymatrix_present)
|
2023-12-29 15:12:12 +02:00
|
|
|
need_reinit |= MATRIX_CHANGED;
|
2023-04-18 15:24:46 +02:00
|
|
|
|
2024-02-13 13:18:27 +02:00
|
|
|
if (!(ifp->opts.flags & IFILTER_FLAG_REINIT) && fgt->graph)
|
2023-12-29 17:33:30 +02:00
|
|
|
need_reinit = 0;
|
|
|
|
|
|
|
|
if (!!ifp->hw_frames_ctx != !!frame->hw_frames_ctx ||
|
|
|
|
(ifp->hw_frames_ctx && ifp->hw_frames_ctx->data != frame->hw_frames_ctx->data))
|
2023-12-29 15:12:12 +02:00
|
|
|
need_reinit |= HWACCEL_CHANGED;
|
2023-12-29 17:33:30 +02:00
|
|
|
|
2023-04-18 15:24:46 +02:00
|
|
|
if (need_reinit) {
|
|
|
|
ret = ifilter_parameters_from_frame(ifilter, frame);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (re)init the graph if possible, otherwise buffer the frame and return */
|
2023-12-13 19:26:21 +02:00
|
|
|
if (need_reinit || !fgt->graph) {
|
2023-06-08 20:36:33 +02:00
|
|
|
AVFrame *tmp = av_frame_alloc();
|
|
|
|
|
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
2023-04-18 15:24:46 +02:00
|
|
|
if (!ifilter_has_all_input_formats(fg)) {
|
2023-06-08 20:36:33 +02:00
|
|
|
av_frame_move_ref(tmp, frame);
|
2023-04-18 15:24:46 +02:00
|
|
|
|
2023-04-25 23:44:49 +02:00
|
|
|
ret = av_fifo_write(ifp->frame_queue, &tmp, 1);
|
2023-04-18 15:24:46 +02:00
|
|
|
if (ret < 0)
|
|
|
|
av_frame_free(&tmp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
ret = fgt->graph ? read_frames(fg, fgt, tmp) : 0;
|
2023-06-08 20:36:33 +02:00
|
|
|
av_frame_free(&tmp);
|
|
|
|
if (ret < 0)
|
2023-04-18 15:24:46 +02:00
|
|
|
return ret;
|
|
|
|
|
2023-12-29 15:12:12 +02:00
|
|
|
if (fgt->graph) {
|
|
|
|
AVBPrint reason;
|
|
|
|
av_bprint_init(&reason, 0, AV_BPRINT_SIZE_AUTOMATIC);
|
|
|
|
if (need_reinit & AUDIO_CHANGED) {
|
|
|
|
const char *sample_format_name = av_get_sample_fmt_name(frame->format);
|
|
|
|
av_bprintf(&reason, "audio parameters changed to %d Hz, ", frame->sample_rate);
|
|
|
|
av_channel_layout_describe_bprint(&frame->ch_layout, &reason);
|
|
|
|
av_bprintf(&reason, ", %s, ", unknown_if_null(sample_format_name));
|
|
|
|
}
|
|
|
|
if (need_reinit & VIDEO_CHANGED) {
|
|
|
|
const char *pixel_format_name = av_get_pix_fmt_name(frame->format);
|
|
|
|
const char *color_space_name = av_color_space_name(frame->colorspace);
|
|
|
|
const char *color_range_name = av_color_range_name(frame->color_range);
|
|
|
|
av_bprintf(&reason, "video parameters changed to %s(%s, %s), %dx%d, ",
|
|
|
|
unknown_if_null(pixel_format_name), unknown_if_null(color_range_name),
|
|
|
|
unknown_if_null(color_space_name), frame->width, frame->height);
|
|
|
|
}
|
|
|
|
if (need_reinit & MATRIX_CHANGED)
|
|
|
|
av_bprintf(&reason, "display matrix changed, ");
|
|
|
|
if (need_reinit & HWACCEL_CHANGED)
|
|
|
|
av_bprintf(&reason, "hwaccel changed, ");
|
|
|
|
if (reason.len > 1)
|
|
|
|
reason.str[reason.len - 2] = '\0'; // remove last comma
|
|
|
|
av_log(fg, AV_LOG_INFO, "Reconfiguring filter graph%s%s\n", reason.len ? " because " : "", reason.str);
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
ret = configure_filtergraph(fg, fgt);
|
2023-04-18 15:24:46 +02:00
|
|
|
if (ret < 0) {
|
2023-06-08 20:58:16 +02:00
|
|
|
av_log(fg, AV_LOG_ERROR, "Error reinitializing filters!\n");
|
2023-04-18 15:24:46 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 09:59:13 +02:00
|
|
|
frame->pts = av_rescale_q(frame->pts, frame->time_base, ifp->time_base);
|
|
|
|
frame->duration = av_rescale_q(frame->duration, frame->time_base, ifp->time_base);
|
|
|
|
frame->time_base = ifp->time_base;
|
|
|
|
|
2024-03-22 21:01:06 +02:00
|
|
|
if (ifp->displaymatrix_applied)
|
|
|
|
av_frame_remove_side_data(frame, AV_FRAME_DATA_DISPLAYMATRIX);
|
|
|
|
|
2023-11-24 20:08:15 +02:00
|
|
|
fd = frame_data(frame);
|
|
|
|
if (!fd)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
fd->wallclock[LATENCY_PROBE_FILTER_PRE] = av_gettime_relative();
|
|
|
|
|
2023-05-24 12:35:50 +02:00
|
|
|
ret = av_buffersrc_add_frame_flags(ifp->filter, frame,
|
2023-05-10 09:59:13 +02:00
|
|
|
AV_BUFFERSRC_FLAG_PUSH);
|
2023-04-18 15:24:46 +02:00
|
|
|
if (ret < 0) {
|
2023-05-10 09:59:13 +02:00
|
|
|
av_frame_unref(frame);
|
2023-04-18 15:24:46 +02:00
|
|
|
if (ret != AVERROR_EOF)
|
2023-06-08 20:58:16 +02:00
|
|
|
av_log(fg, AV_LOG_ERROR, "Error while filtering: %s\n", av_err2str(ret));
|
2023-04-18 15:24:46 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
static void fg_thread_set_name(const FilterGraph *fg)
|
|
|
|
{
|
|
|
|
char name[16];
|
|
|
|
if (filtergraph_is_simple(fg)) {
|
2024-04-02 11:40:41 +02:00
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[0]);
|
|
|
|
snprintf(name, sizeof(name), "%cf%s",
|
|
|
|
av_get_media_type_string(ofp->ofilter.type)[0],
|
|
|
|
ofp->name);
|
2023-06-08 20:36:33 +02:00
|
|
|
} else {
|
|
|
|
snprintf(name, sizeof(name), "fc%d", fg->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
ff_thread_setname(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fg_thread_uninit(FilterGraphThread *fgt)
|
|
|
|
{
|
|
|
|
if (fgt->frame_queue_out) {
|
|
|
|
AVFrame *frame;
|
|
|
|
while (av_fifo_read(fgt->frame_queue_out, &frame, 1) >= 0)
|
|
|
|
av_frame_free(&frame);
|
|
|
|
av_fifo_freep2(&fgt->frame_queue_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&fgt->frame);
|
|
|
|
av_freep(&fgt->eof_in);
|
|
|
|
av_freep(&fgt->eof_out);
|
|
|
|
|
2023-12-13 19:26:21 +02:00
|
|
|
avfilter_graph_free(&fgt->graph);
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
memset(fgt, 0, sizeof(*fgt));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fg_thread_init(FilterGraphThread *fgt, const FilterGraph *fg)
|
|
|
|
{
|
|
|
|
memset(fgt, 0, sizeof(*fgt));
|
|
|
|
|
|
|
|
fgt->frame = av_frame_alloc();
|
|
|
|
if (!fgt->frame)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
fgt->eof_in = av_calloc(fg->nb_inputs, sizeof(*fgt->eof_in));
|
|
|
|
if (!fgt->eof_in)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
fgt->eof_out = av_calloc(fg->nb_outputs, sizeof(*fgt->eof_out));
|
|
|
|
if (!fgt->eof_out)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
fgt->frame_queue_out = av_fifo_alloc2(1, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
|
|
|
|
if (!fgt->frame_queue_out)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
fg_thread_uninit(fgt);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2024-02-22 17:02:21 +02:00
|
|
|
static int filter_thread(void *arg)
|
2023-06-08 20:36:33 +02:00
|
|
|
{
|
|
|
|
FilterGraphPriv *fgp = arg;
|
|
|
|
FilterGraph *fg = &fgp->fg;
|
|
|
|
|
|
|
|
FilterGraphThread fgt;
|
|
|
|
int ret = 0, input_status = 0;
|
|
|
|
|
|
|
|
ret = fg_thread_init(&fgt, fg);
|
|
|
|
if (ret < 0)
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
fg_thread_set_name(fg);
|
|
|
|
|
|
|
|
// if we have all input parameters the graph can now be configured
|
|
|
|
if (ifilter_has_all_input_formats(fg)) {
|
|
|
|
ret = configure_filtergraph(fg, &fgt);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_log(fg, AV_LOG_ERROR, "Error configuring filter graph: %s\n",
|
|
|
|
av_err2str(ret));
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
InputFilter *ifilter;
|
|
|
|
InputFilterPriv *ifp;
|
|
|
|
enum FrameOpaque o;
|
2023-07-18 16:37:52 +02:00
|
|
|
unsigned input_idx = fgt.next_in;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
input_status = sch_filter_receive(fgp->sch, fgp->sch_idx,
|
|
|
|
&input_idx, fgt.frame);
|
|
|
|
if (input_status == AVERROR_EOF) {
|
2023-06-08 20:36:33 +02:00
|
|
|
av_log(fg, AV_LOG_VERBOSE, "Filtering thread received EOF\n");
|
|
|
|
break;
|
2023-07-18 16:37:52 +02:00
|
|
|
} else if (input_status == AVERROR(EAGAIN)) {
|
|
|
|
// should only happen when we didn't request any input
|
|
|
|
av_assert0(input_idx == fg->nb_inputs);
|
|
|
|
goto read_frames;
|
2023-06-08 20:36:33 +02:00
|
|
|
}
|
2023-07-18 16:37:52 +02:00
|
|
|
av_assert0(input_status >= 0);
|
|
|
|
|
|
|
|
o = (intptr_t)fgt.frame->opaque;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
|
|
|
o = (intptr_t)fgt.frame->opaque;
|
|
|
|
|
|
|
|
// message on the control stream
|
|
|
|
if (input_idx == fg->nb_inputs) {
|
2023-07-18 16:37:52 +02:00
|
|
|
FilterCommand *fc;
|
|
|
|
|
|
|
|
av_assert0(o == FRAME_OPAQUE_SEND_COMMAND && fgt.frame->buf[0]);
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
fc = (FilterCommand*)fgt.frame->buf[0]->data;
|
2023-12-13 19:26:21 +02:00
|
|
|
send_command(fg, fgt.graph, fc->time, fc->target, fc->command, fc->arg,
|
2023-07-18 16:37:52 +02:00
|
|
|
fc->all_filters);
|
|
|
|
av_frame_unref(fgt.frame);
|
2023-06-08 20:36:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we received an input frame or EOF
|
|
|
|
ifilter = fg->inputs[input_idx];
|
|
|
|
ifp = ifp_from_ifilter(ifilter);
|
2023-07-18 16:37:52 +02:00
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE) {
|
|
|
|
int hb_frame = input_status >= 0 && o == FRAME_OPAQUE_SUB_HEARTBEAT;
|
2023-12-13 19:26:21 +02:00
|
|
|
ret = sub2video_frame(ifilter, (fgt.frame->buf[0] || hb_frame) ? fgt.frame : NULL,
|
|
|
|
!fgt.graph);
|
2023-07-18 16:37:52 +02:00
|
|
|
} else if (fgt.frame->buf[0]) {
|
2023-06-08 20:36:33 +02:00
|
|
|
ret = send_frame(fg, &fgt, ifilter, fgt.frame);
|
|
|
|
} else {
|
2023-07-18 16:37:52 +02:00
|
|
|
av_assert1(o == FRAME_OPAQUE_EOF);
|
|
|
|
ret = send_eof(&fgt, ifilter, fgt.frame->pts, fgt.frame->time_base);
|
2023-06-08 20:36:33 +02:00
|
|
|
}
|
|
|
|
av_frame_unref(fgt.frame);
|
2024-01-24 11:42:13 +02:00
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
av_log(fg, AV_LOG_VERBOSE, "Input %u no longer accepts new data\n",
|
|
|
|
input_idx);
|
|
|
|
sch_filter_receive_finish(fgp->sch, fgp->sch_idx, input_idx);
|
|
|
|
continue;
|
|
|
|
}
|
2023-06-08 20:36:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto finish;
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
read_frames:
|
|
|
|
// retrieve all newly avalable frames
|
|
|
|
ret = read_frames(fg, &fgt, fgt.frame);
|
|
|
|
if (ret == AVERROR_EOF) {
|
|
|
|
av_log(fg, AV_LOG_VERBOSE, "All consumers returned EOF\n");
|
2023-06-08 20:36:33 +02:00
|
|
|
break;
|
2023-07-18 16:37:52 +02:00
|
|
|
} else if (ret < 0) {
|
|
|
|
av_log(fg, AV_LOG_ERROR, "Error sending frames to consumers: %s\n",
|
|
|
|
av_err2str(ret));
|
|
|
|
goto finish;
|
2023-06-08 20:36:33 +02:00
|
|
|
}
|
2023-07-18 16:37:52 +02:00
|
|
|
}
|
2023-04-30 23:05:55 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
for (unsigned i = 0; i < fg->nb_outputs; i++) {
|
|
|
|
OutputFilterPriv *ofp = ofp_from_ofilter(fg->outputs[i]);
|
2023-06-08 20:36:33 +02:00
|
|
|
|
fftools/ffmpeg_filter: fix NULL pointer dereference
In close_output(), a dummy frame is created with format NONE passed
to enc_open(), which isn't prepared for it. The NULL pointer
dereference happened at
av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth.
When fgt.graph is NULL, skip fg_output_frame() since there is
nothing to output.
frame #0: 0x0000005555bc34a4 ffmpeg_g`enc_open(opaque=0xb400007efe2db690, frame=0xb400007efe2d9f70) at ffmpeg_enc.c:235:44
frame #1: 0x0000005555bef250 ffmpeg_g`enc_open(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1462:11
frame #2: 0x0000005555bee094 ffmpeg_g`send_to_enc(sch=0xb400007dde2d4090, enc=0xb400007e4e2daad0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:1571:19
frame #3: 0x0000005555bee01c ffmpeg_g`sch_filter_send(sch=0xb400007dde2d4090, fg_idx=0, out_idx=0, frame=0xb400007efe2d9f70) at ffmpeg_sched.c:2154:12
frame #4: 0x0000005555bcf124 ffmpeg_g`close_output(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08) at ffmpeg_filter.c:2225:15
frame #5: 0x0000005555bcb000 ffmpeg_g`fg_output_frame(ofp=0xb400007e4e2d85b0, fgt=0x0000007d1790eb08, frame=0x0000000000000000) at ffmpeg_filter.c:2317:16
frame #6: 0x0000005555bc7e48 ffmpeg_g`filter_thread(arg=0xb400007eae2ce7a0) at ffmpeg_filter.c:2836:15
frame #7: 0x0000005555bee568 ffmpeg_g`task_wrapper(arg=0xb400007d8e2db478) at ffmpeg_sched.c:2200:21
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2023-12-20 12:26:22 +02:00
|
|
|
if (fgt.eof_out[i] || !fgt.graph)
|
2023-04-18 15:24:46 +02:00
|
|
|
continue;
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
ret = fg_output_frame(ofp, &fgt, NULL);
|
2023-06-08 20:36:33 +02:00
|
|
|
if (ret < 0)
|
|
|
|
goto finish;
|
2023-04-18 15:24:46 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 20:36:33 +02:00
|
|
|
finish:
|
2023-07-18 16:37:52 +02:00
|
|
|
// EOF is normal termination
|
|
|
|
if (ret == AVERROR_EOF)
|
|
|
|
ret = 0;
|
2023-04-18 15:24:46 +02:00
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
fg_thread_uninit(&fgt);
|
2023-06-08 20:36:33 +02:00
|
|
|
|
2024-02-22 17:02:21 +02:00
|
|
|
return ret;
|
2023-06-08 20:36:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void fg_send_command(FilterGraph *fg, double time, const char *target,
|
|
|
|
const char *command, const char *arg, int all_filters)
|
|
|
|
{
|
|
|
|
FilterGraphPriv *fgp = fgp_from_fg(fg);
|
|
|
|
AVBufferRef *buf;
|
|
|
|
FilterCommand *fc;
|
|
|
|
|
|
|
|
fc = av_mallocz(sizeof(*fc));
|
|
|
|
if (!fc)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buf = av_buffer_create((uint8_t*)fc, sizeof(*fc), filter_command_free, NULL, 0);
|
|
|
|
if (!buf) {
|
|
|
|
av_freep(&fc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fc->target = av_strdup(target);
|
|
|
|
fc->command = av_strdup(command);
|
|
|
|
fc->arg = av_strdup(arg);
|
|
|
|
if (!fc->target || !fc->command || !fc->arg) {
|
|
|
|
av_buffer_unref(&buf);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fc->time = time;
|
|
|
|
fc->all_filters = all_filters;
|
|
|
|
|
|
|
|
fgp->frame->buf[0] = buf;
|
|
|
|
fgp->frame->opaque = (void*)(intptr_t)FRAME_OPAQUE_SEND_COMMAND;
|
|
|
|
|
2023-07-18 16:37:52 +02:00
|
|
|
sch_filter_command(fgp->sch, fgp->sch_idx, fgp->frame);
|
2023-04-18 15:24:46 +02:00
|
|
|
}
|