Commit Graph

837 Commits

Author SHA1 Message Date
Anton Khirnov 8763b9cc36 fftools/ffmpeg_mux: make streamcopy_started private to muxing code
It is no longer used outside of ffmpeg_mux*
2023-04-17 12:01:40 +02:00
Anton Khirnov 83646146fe fftools/ffmpeg_mux: make ts_copy_start private to muxing code
It is no longer used outside of ffmpeg_mux*
2023-04-17 12:01:40 +02:00
Anton Khirnov 3f11582ca3 fftools/ffmpeg_mux: stop using filter_in_rescale_delta_last for streamcopy
That field was added to store timestamp conversion state for audio
decoding code. Later it started being used by streamcopy as well, but
that use is wrong because, for a given input stream, both decoding and
an arbitrary number of streamcopies may be performed simultaneously.
They would then all overwrite the same state variable.

Store this state in MuxStream instead.

This is the last use of InputStream in of_streamcopy(), so the ist
parameter can now be removed.
2023-04-17 12:01:40 +02:00
Anton Khirnov 150c992490 fftools/ffmpeg_mux: use output stream parameters in of_streamcopy()
They should always be the same as the input stream parameters, but this
reduces the need to access InputStream in muxing code.
2023-04-17 12:01:40 +02:00
Anton Khirnov 2178ff2162 fftools/ffmpeg: move do_streamcopy() to ffmpeg_mux
do_streamcopy() is muxing code, so this is a more appropriate place for
this. The last uses of InputStream in it will be removed in following
commits.
2023-04-17 12:01:40 +02:00
Anton Khirnov a34f483291 fftools/ffmpeg: add muxer-input codec parameters to OutputStream
It stores codec parameters of the stream submitted to the muxer, which
may be different from the codec parameters in AVStream due to bitstream
filtering.

This avoids the confusing back and forth synchronisation between the
encoder, bitstream filters, and the muxer, now information flows only in
one direction. It also reduces the need for non-muxing code to access
AVStream.
2023-04-17 12:01:40 +02:00
Anton Khirnov a064aed4c3 fftools/ffmpeg: store stream media type in OutputStream
Reduces access to a deeply nested muxer property
OutputStream.st->codecpar->codec_type for this fundamental and immutable
stream property.

Besides making the code shorter, this will allow making the AVStream
(OutputStream.st) private to the muxer in the future.
2023-04-17 12:01:40 +02:00
Anton Khirnov de38e17583 fftools/ffmpeg: initialize no-filter streams earlier
There is no reason to postpone it until transcode_init() anymore, it can
be done right at the end of of_open().
2023-04-17 12:01:40 +02:00
Anton Khirnov 2058402e00 fftools/ffmpeg: open decoders right after we know they are needed
Will allow initializing subtitle encoding earlier.
2023-04-17 12:01:40 +02:00
Anton Khirnov c8fa58430e fftools/ffmpeg_dec: drop useless abort_codec_experimental()
It does nothing beyond exit_program().
2023-04-17 12:01:40 +02:00
Anton Khirnov 503c705634 fftools/ffmpeg_dec: reindent after previous commit 2023-04-17 12:01:40 +02:00
Anton Khirnov 9ce1a041d8 fftools/ffmpeg: move opening decoders to a new file
All decoding code will be moved to this file in the future.
2023-04-17 12:01:40 +02:00
Anton Khirnov ff92ecad2f fftools/ffmpeg: stop setting InputStream fields from muxing/filtering code
Set InputStream.decoding_needed/discard/etc. only from
ist_{filter,output},add() functions. Reduces the knowledge of
InputStream internals in muxing/filtering code.
2023-04-17 12:01:40 +02:00
Anton Khirnov ae071c9e39 fftools/ffmpeg: add a function adding a destination filter for InputStream
This way filtering code does not directly mess with InputStream
internals. Will become more useful in following commits.
2023-04-17 12:01:40 +02:00
Anton Khirnov c7438e8737 fftools/ffmpeg_mux_init: consolidate input stream flagging code
Makes it easier to see where the input stream is modified from muxer
code.
2023-04-17 12:01:40 +02:00
Anton Khirnov 0c44db4646 fftools/ffmpeg: drop unnecessary indirection
init_input_stream() can print log messages directly, there is no need to
ship them to the caller.

Also, log errors to the InputStream and avoid duplicate information in
the message.
2023-04-17 12:01:40 +02:00
Anton Khirnov 4358d4d8e7 fftools/ffmpeg: move a check to a more appropriate place
reap_filters() no longer needs to access the encoding context.
2023-04-17 12:01:40 +02:00
Anton Khirnov 4fc513b5e3 fftools/ffmpeg: move a misplaced assignment
Changing AVCodecContext.sample_aspect_ratio after the encoder was opened
is by itself questionable, but if anywhere it belongs in encoding rather
than filtering code.
2023-04-17 12:01:36 +02:00
Anton Khirnov a0452ee837 fftools/ffmpeg_mux_init: remove a redundant check 2023-04-17 12:00:06 +02:00
Anton Khirnov 12f3f41bbf fftools/ffmpeg: move init_output_stream_streamcopy() to ffmpeg_mux_init
Everything in it can be done immediately when creating the output
stream, there is no reason to postpone it.
2023-04-17 12:00:06 +02:00
Anton Khirnov 9bae55165d fftools/ffmpeg_mux_init: restructure output stream creation
Creating a new output stream of a given type is currently done by
calling new_<type>_stream(), which all start by calling
new_output_stream() to allocate the stream and do common init, followed
by type-specific init.

Reverse this structure - the caller now calls the common function
ost_add() with the type as a parameter, which then calls the
type-specific function internally. This will allow adding common code
that runs after type-specific code in future commits.
2023-04-17 12:00:06 +02:00
Anton Khirnov a7f26b1c63 fftools/ffmpeg_mux_init: move new_output_stream() lower in the file
Reduces the diff in the following commit.

Temporarily add a forward declaration for new_output_stream(), it will
be removed in the next commit.
2023-04-17 12:00:06 +02:00
Anton Khirnov 89c9a3ac35 fftools/ffmpeg: avoid possible invalid reads with short -tag values
Fixes #10319 and #10309.
2023-04-17 11:49:34 +02:00
Nongji Chen eb96cfbf57 ffmpeg: make timestamp discontinuity logging a warning
In most cases this should only occur once or so per stream in an
input, and in case the logic ends up in an eternal loop, it should
be visible to the end user instead of being completely invisible.

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
2023-04-14 15:06:41 +03:00
Anton Khirnov cea71b2139 fftools/ffmpeg: stop using fake dts for generating timestamps
When no packet dts values are available from the container, video
decoding code will currently use its own guessed values, which will then
be propagated to pkt_dts on decoded frames and used as pts in certain
cases. This is inaccurate, fragile, and unnecessarily convoluted.
Simply removing this allows the extrapolation code introduced in the
previous commit to do a better job.

Changes the results of numerous h264 and hevc FATE tests, where no
spurious timestamp gaps are generated anymore. Several tests no longer
need -vsync passthrough.
2023-04-13 15:34:07 +02:00
Anton Khirnov 5d407cb2d7 fftools/ffmpeg: change video decoding timestamp generation
When no timestamps are available from the container, the video decoding
code will currently use fake dts values - generated in
process_input_packet() based on a combination of information from the
decoder and the parser (obtained via the demuxer) - to generate
timestamps during decoder flushing. This is fragile, hard to follow, and
unnecessarily convoluted, since more reliable information can be
obtained directly from post-decoding values.

The new code keeps track of the last decoded frame pts and estimates its
duration based on a number of heuristics. Timestamps generated when both
pts and pkt_dts are missing are then simple pts+duration of the last frame.
The heuristics are somewhat complicated by the fact that lavf insists on
making up packet timestamps based on its highly incomplete information.
That should be removed in the future, allowing to further simplify this
code.

The results of the following tests change:
* h264-3386 now requires -fps_mode passthrough to avoid dropping frames
  at the end; this is a pathology of the interaction of the new and old
  code, and the fact that the sample switches from field to frame coding
  in the last packet, and will be fixed in following commits
* hevc-conformance-DELTAQP_A_BRCM_4 stops inventing an arbitrary
  timestamp gap at the end
* hevc-small422chroma - the single frame output by this test now has a
  timestamp of 0, rather than an arbitrary 7
2023-04-13 15:34:07 +02:00
Anton Khirnov 380db56928 fftools/ffmpeg: use InputStream.pts as last resort for decoded frame pts 2023-04-13 15:34:07 +02:00
Anton Khirnov 95fa4edbd6 fftools/ffmpeg: improve decoder -ts_debug line
* log to the input stream log context
* drop the now-duplicate index/type information
* show pkt_dts and frame duration
2023-04-13 15:34:07 +02:00
Anton Khirnov d56652fdc8 fftools/ffmpeg: stop using InputStream.pts for streamcopy
This field contains different values depending on whether the stream is
being decoded or not. When it is, InputStream.pts is set to the
timestamp of the last decoded frame. Otherwise, it is made equal to
InputStream.dts.

Since a given InputStream can be at the same time decoded and
streamcopied to any number of output streams, this use is incorrect, as
decoded frame timestamps can be delayed with respect to input packets by
an arbitrary amount (e.g. depending on the thread count when frame
threading is used).

Replace all uses of InputStream.pts for streamcopy with InputStream.dts,
which is its value when decoding is not performed. Stop setting
InputStream.pts for pure streamcopy.
Also, pass InputStream.dts as a parameter to do_streamcopy(), which
will allow that function to be decoupled from InputStream completely in
the future.
2023-04-13 15:32:56 +02:00
Anton Khirnov 106eb58ceb fftools/ffmpeg: move checking for input -t out of do_streamcopy()
This check is entirely about the properties of the input stream, while
do_streamcopy() should contain code specific to a given output stream.
2023-04-13 15:11:56 +02:00
Anton Khirnov 0feff04956 fftools/ffmpeg: only set InputStream.next_pts for decoding
It is write-only for streamcopy.
2023-04-13 15:11:56 +02:00
Anton Khirnov d867f9ab8c fftools/ffmpeg: use AVPacket.time_base to simplify do_streamcopy()
Besides making the code shorter, this also reduces the use of
InputStream in this function and will allow not accessing it at all in
the future.
2023-04-13 15:11:56 +02:00
Anton Khirnov d1cb31d7d8 fftools/ffmpeg_demux: set the timebase on demuxed packets
Simplifies tracking what timebase are the timestamps in. Will be useful
in following commits.
2023-04-13 15:11:56 +02:00
Anton Khirnov fdf29dcebb fftools/ffmpeg: inline check_output_constraints() into its only caller
Which is subtitle encoding. Also, check for AVSubtitle.pts rather than
InputStream.pts, since that is the more authoritative value and is
guaranteed to be valid.
2023-04-13 15:11:56 +02:00
Anton Khirnov ceb0275e45 fftools/ffmpeg: stop calling check_output_constraints() for streamcopy
That function only contains two checks now - whether the muxer returned
EOF and whether the packet timestamp is before requested output start
time.

The first check is unnecessary, since the packet will just be rejected
by the muxer. The second check is better combined with a related check
directly in do_streamcopy().
2023-04-13 15:11:56 +02:00
Anton Khirnov a85e7e5dea fftools/ffmpeg: track a list of non-lavfi outputs in InputStream
Currently, output streams where an input stream is sent directly (i.e.
not through lavfi) are determined by iterating over ALL the output
streams and skipping the irrelevant ones. This is awkward and
inefficient.
2023-04-13 15:11:56 +02:00
Anton Khirnov 798da60e6a fftools/ffmpeg_mux_init: print more meaningful error messages 2023-04-13 15:11:56 +02:00
Anton Khirnov 5297250920 fftools/ffmpeg_filter: stop setting encoder channel layout unnecessarily
The channel layout is set before opening the encoder, in enc_open().
Messing with it in configure_output_audio_filter() cannot accomplish
anything meaningful.
2023-04-13 15:11:56 +02:00
Anton Khirnov 2f24290c8e fftools/ffmpeg: disable and deprecate -qphist
This option adds a long string of numbers to the progress line, where
i-th number contains the base-2 logarithm of the number of times a frame
with this QP value was seen by print_report().

There are multiple problems with this feature:
* despite this existing since 2005, web search shows no indication
  that it was ever useful for any meaningful purpose;
* the format of what is printed is entirely undocumented, one has to
  find it out from the source code;
* QP values above 31 are silently ignored;
* it only works with one video stream;
* as it relies on global state, it is in conflict with ongoing
  architectural changes.

It then seems that the nontrivial cost of maintaining this option is not
worth its negligible (or possibly negative - since it pollutes the
already large option space) value.
Users who really need similar functionality can also implement it
themselves using -vstats.
2023-04-13 15:11:56 +02:00
Anton Khirnov 952110f974 fftools/ffmpeg_demux: log final stats to demuxer context 2023-04-13 15:11:56 +02:00
Anton Khirnov 28e258a809 fftools/ffmpeg_demux: reindent 2023-04-13 15:11:56 +02:00
Anton Khirnov 5d97ba5d9c fftools/ffmpeg: move printing verbose demuxing stats to ffmpeg_demux
This is a more appropriate place for this.
2023-04-13 15:11:56 +02:00
Anton Khirnov 0288951174 fftools/ffmpeg_mux: make data_size_mux private to ffmpeg_mux
It is no longer used outside of this file.
2023-04-13 15:11:56 +02:00
Anton Khirnov 37b118096a fftools/ffmpeg: rewrite printing the final output sizes
Current code in print_final_stats(), printing the final summary such as
  video:8851kB audio:548kB subtitle:0kB other streams:0kB global headers:20kB muxing overhead: 0.559521%
was written with a single output file in mind and makes very little
sense otherwise.

Print this information in mux_final_stats() instead, one line per output
file. Use the correct filesize, if available.
2023-04-13 15:11:56 +02:00
Anton Khirnov 6b2e222a45 fftools/ffmpeg_mux: log final stats to muxer context 2023-04-13 15:11:56 +02:00
Anton Khirnov c1764d067d fftools/ffmpeg_mux: reindent 2023-04-13 15:11:56 +02:00
Anton Khirnov 3b6b0d1afb fftools/ffmpeg: move printing verbose muxing stats to ffmpeg_mux
This is a more appropriate place for this.
2023-04-13 15:11:56 +02:00
Anton Khirnov 79e136f14b fftools/ffmpeg: factorize checking whether any output was written
This is currently done in two places:
* at the end of print_final_stats(), which merely prints a warning if
  the total size of all written packets is zero
* at the end of transcode() (under a misleading historical 'close each
  encoder' comment), which instead checks the packet count to implement
  -abort_on empty_output[_stream]

Consolidate both of these blocks into a single function called from
of_write_trailer(), which is a more appropriate place for this. Also,
return an error code rather than exit immediately, which ensures all
output files are properly closed.
2023-04-13 15:11:56 +02:00
Anton Khirnov 5cf81bed88 fftools/ffmpeg: eliminate the main_return_code global
Properly pass muxing return codes through the call stack instead.
Slightly changes behavior in case of errors:
* the output IO stream is closed even if writing the trailer returns an
  error, which should be more correct
* all files get properly closed with -xerror, even if one of them fails
2023-04-13 15:11:56 +02:00
Anton Khirnov d99846d2f2 fftools/ffmpeg: move the hw_device_free_all() call to ffmpeg_cleanup()
Frees devices on failure as well as success.
2023-04-13 15:11:56 +02:00