aboutsummaryrefslogtreecommitdiffstats
path: root/fftools/ffmpeg_filter.c
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2023-07-06 21:31:49 +0200
committerAnton Khirnov <anton@khirnov.net>2023-07-15 11:02:11 +0200
commit0a6751a78ab8a894eceaade6bf7578af6c0cd38b (patch)
tree222a664705ff6a61c5d94d0c8fdf2bed4a2c8e00 /fftools/ffmpeg_filter.c
parent20c42213eaf0a9cfd446f5c3ab5afcfe9ec01f26 (diff)
downloadffmpeg-0a6751a78ab8a894eceaade6bf7578af6c0cd38b.tar.gz
fftools/ffmpeg: rework initializing encoders with no frames
When no frames were passed from a filtergraph to an encoder, but the filtergraph is configured (i.e. has output parameters), encoder flush code will use those parameters to initialize the encoder in a last-ditch effort to produce some useful output. Rework this process so that it is triggered by the filtergraph, which now sends a dummy frame with parameters, but no data, to the encoder, rather than the encoder reaching backwards into the filter. This approach is more in line with the natural data flow from filters to encoders and will allow to reduce encoder-filter interactions in following commits. This code is tested by fate-adpcm-ima-cunning-trunc-t2-track1, which (as confirmed by Zane) is supposed to produce empty output.
Diffstat (limited to 'fftools/ffmpeg_filter.c')
-rw-r--r--fftools/ffmpeg_filter.c54
1 files changed, 50 insertions, 4 deletions
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index caf85194c5..1b51ed55bd 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -143,11 +143,17 @@ typedef struct OutputFilterPriv {
int sample_rate;
AVChannelLayout ch_layout;
+ AVRational time_base;
+ AVRational sample_aspect_ratio;
+
// 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.
const int *formats;
const AVChannelLayout *ch_layouts;
const int *sample_rates;
+
+ // set to 1 after at least one frame passed through this output
+ int got_frame;
} OutputFilterPriv;
static OutputFilterPriv *ofp_from_ofilter(OutputFilter *ofilter)
@@ -1610,6 +1616,9 @@ static int configure_filtergraph(FilterGraph *fg)
ofp->width = av_buffersink_get_w(sink);
ofp->height = av_buffersink_get_h(sink);
+ ofp->time_base = av_buffersink_get_time_base(sink);
+ ofp->sample_aspect_ratio = av_buffersink_get_sample_aspect_ratio(sink);
+
ofp->sample_rate = av_buffersink_get_sample_rate(sink);
av_channel_layout_uninit(&ofp->ch_layout);
ret = av_buffersink_get_ch_layout(sink, &ofp->ch_layout);
@@ -1722,6 +1731,7 @@ int reap_filters(int flush)
{
/* Reap all buffers present in the buffer sinks */
for (OutputStream *ost = ost_iter(NULL); ost; ost = ost_iter(ost)) {
+ OutputFilterPriv *ofp;
FilterGraphPriv *fgp;
AVFrame *filtered_frame;
AVFilterContext *filter;
@@ -1731,6 +1741,7 @@ int reap_filters(int flush)
continue;
filter = ost->filter->filter;
fgp = fgp_from_fg(ost->filter->graph);
+ ofp = ofp_from_ofilter(ost->filter);
filtered_frame = fgp->frame;
@@ -1783,6 +1794,7 @@ int reap_filters(int flush)
enc_frame(ost, filtered_frame);
av_frame_unref(filtered_frame);
+ ofp->got_frame = 1;
}
}
@@ -1995,6 +2007,7 @@ int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_reference)
int fg_transcode_step(FilterGraph *graph, InputStream **best_ist)
{
+ FilterGraphPriv *fgp = fgp_from_fg(graph);
int i, ret;
int nb_requests, nb_requests_max = 0;
InputStream *ist;
@@ -2022,10 +2035,43 @@ int fg_transcode_step(FilterGraph *graph, InputStream **best_ist)
return reap_filters(0);
if (ret == AVERROR_EOF) {
- ret = reap_filters(1);
- for (i = 0; i < graph->nb_outputs; i++)
- close_output_stream(graph->outputs[i]->ost);
- return ret;
+ reap_filters(1);
+ for (int i = 0; i < graph->nb_outputs; i++) {
+ OutputFilter *ofilter = graph->outputs[i];
+ OutputFilterPriv *ofp = ofp_from_ofilter(ofilter);
+
+ // we are finished and no frames were ever seen at this output,
+ // at least initialize the encoder with a dummy frame
+ if (!ofp->got_frame) {
+ AVFrame *frame = fgp->frame;
+
+ frame->time_base = ofp->time_base;
+ 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;
+ }
+
+ av_assert0(!frame->buf[0]);
+
+ av_log(ofilter->ost, AV_LOG_WARNING,
+ "No filtered frames for output stream, trying to "
+ "initialize anyway.\n");
+
+ enc_open(ofilter->ost, frame);
+ av_frame_unref(frame);
+ }
+
+ close_output_stream(ofilter->ost);
+ }
+ return 0;
}
if (ret != AVERROR(EAGAIN))
return ret;