Commit Graph

300 Commits

Author SHA1 Message Date
Anton Khirnov 04f0c40c74 fftools/ffmpeg_filter: add InputFilter private data
Move InputFilter.frame_queue to it, which is not accessed outside of
ffmpeg_filter.
2023-05-02 10:59:24 +02:00
Anton Khirnov 87ced7aee0 fftools/ffmpeg_filter: make graph_desc private
It is not used outside of ffmpeg_filter.
2023-05-02 10:59:24 +02:00
Anton Khirnov 09dd9c5f41 fftools/ffmpeg_filter: add filtergraph private data
Start by moving OutputStream.filtered_frame to it, which really belongs
to the filtergraph rather than the output stream.
2023-05-02 10:59:24 +02:00
Anton Khirnov aee5b774bf fftools/ffmpeg_filter: make ifilter_parameters_from_frame() static
It is no longer used outside of this file.
2023-05-02 10:59:24 +02:00
Anton Khirnov 7e3eb24123 fftools/ffmpeg: move OutputStream.packets_encoded to Encoder
It is no longer used outside of ffmpeg_enc.
2023-05-02 10:59:24 +02:00
Anton Khirnov 6e487a50a1 fftools/ffmpeg: drop OutputStream.error
Only the first component is used in update_video_stats(), so make it a
stack variable in that function.
2023-05-02 10:59:24 +02:00
Anton Khirnov 52b632b65b fftools/ffmpeg: drop OutputStream.pict_type
It is no longer used outside of update_video_stats(), so make it a stack
variable in that function.
2023-05-02 10:59:24 +02:00
Anton Khirnov 5a04aae821 fftools/ffmpeg: deprecate -adrift_threshold
This option has had no effect since -async was removed in
3d86a13b47
2023-05-02 10:59:24 +02:00
Anton Khirnov cc7a8ddf56 fftools/ffmpeg: move freeing a filtergraph into a separate function 2023-04-24 16:56:06 +02:00
Anton Khirnov eb9ce9de3b fftools/ffmpeg: move filtering functions to ffmpeg_filter 2023-04-24 16:56:06 +02:00
Anton Khirnov 8b56480652 fftools/ffmpeg_filter: add a function for creating a filtergraph
Code creating a new filtergraph is currently duplicated in 3 places.
This commit unifies it and moves towards making filtergraphs more
self-contained.
2023-04-24 16:56:06 +02:00
Anton Khirnov ecb44ca877 fftools/ffmpeg_mux_init: drop OutputStream.filters[_script]
They are not needed outside of ost_get_filters(), so make them stack
variables there.
2023-04-24 16:56:06 +02:00
Anton Khirnov a664ec94e0 fftools/ffmpeg_filter: drop write-only FilterGraph.reconfiguration 2023-04-24 16:56:06 +02:00
Anton Khirnov 0b0fa5c2a8 fftools/ffmpeg_enc: make data_size_enc private to encoding code
It is no longer used outside of ffmpeg_enc.c
2023-04-17 12:01:40 +02:00
Anton Khirnov 30699c103f fftools/ffmpeg_mux: make copy_initial_nonkeyframes private to muxing code
It is no longer used outside of ffmpeg_mux*
2023-04-17 12:01:40 +02:00
Anton Khirnov 7f8827f28e fftools/ffmpeg_mux: make copy_prior_start private to muxing code
It is no longer used outside of ffmpeg_mux*
2023-04-17 12:01:40 +02:00
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 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 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 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 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 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 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 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 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 fd91ac11ed fftools/ffmpeg: move OutputStream.last_filter_pts to OutputFilter
This value is associated with the filtergraph output rather than the
output stream, so this is a more appropriate place for it.
2023-04-13 15:11:56 +02:00
Anton Khirnov 83da6d3f54 fftools/ffmpeg: move OutputStream.last_nb0_frames to Encoder
It is video encoding-only and does not need to be visible outside of
ffmpeg_enc.c

Also, rename the variable to frames_prev_hist to be consistent with
the naming in do_video_out().
2023-04-13 15:11:56 +02:00
Anton Khirnov 87ae84e4af fftools/ffmpeg: move OutputStream.sq_frame to Encoder
It is audio/video encoding-only and does not need to be visible outside
of ffmpeg_enc.c
2023-04-13 15:11:56 +02:00
Anton Khirnov 710da200fc fftools/ffmpeg: move OutputStream.next_pts to Encoder
It is audio/video encoding-only and does not need to be visible outside
of ffmpeg_enc.c
2023-04-13 15:11:56 +02:00
Anton Khirnov 45202556cb fftools/ffmpeg: move OutputStream.vsync_frame_number to Encoder
It is video encoding-only and does not need to be visible outside of
ffmpeg_enc.c
2023-04-13 15:11:56 +02:00
Anton Khirnov f30b620e98 fftools/ffmpeg: add encoder private data
Start by moving OutputStream.last_frame to it. In the future it will
hold other encoder-internal state.
2023-04-09 15:47:45 +02:00
Anton Khirnov 44accfef41 fftools/ffmpeg: move audio/video encoding code to ffmpeg_enc.c 2023-04-09 15:47:45 +02:00
Anton Khirnov 9de5dc74fd fftools/ffmpeg: move subtitle encoding to ffmpeg_enc.c 2023-04-09 15:47:45 +02:00
Anton Khirnov d96f2fbf76 fftools/ffmpeg: move initializing encoders to a new file
This file will contain more encoding-related code in the future.
2023-04-09 15:47:45 +02:00
Anton Khirnov 8e23a62eff fftools/ffmpeg: drop InputStream.processing_needed
It is equivalent to !InputStream.discard.
2023-04-09 15:47:45 +02:00
Anton Khirnov 3f63685c35 fftools/ffmpeg: supply hw_device_ctx to filters before initializing them
This is more correct, but was not possible before the recently-added
filtergraph parsing API.

Also, only pass hw devices to filters that are flagged as capable of
using them.

Tested-by: Niklas Haas
2023-03-24 10:23:52 +01:00
Anton Khirnov 156ca86569 fftools/ffmpeg: move ts_scale to DemuxStream
It is not needed outside of ffmpeg_demux.
2023-02-20 19:22:22 +01:00
Anton Khirnov 66c1e956aa fftools/ffmpeg_demux: add an AVClass to DemuxStream/InputStream
Use it for logging. This makes log messages related to this input stream
more consistent.
2023-02-20 19:22:22 +01:00
Anton Khirnov d9079f6700 fftools/ffmpeg_demux: move InputStream.guess_layout_max to stack
It is only needed while processing the stream in add_input_streams(), no
reason to store it in the context.
2023-02-20 19:22:22 +01:00
Anton Khirnov 8a7554a574 fftools/ffmpeg_demux: add InputStream private data
Move {min,max}_pts to it, which is not used outside of ffmpeg_demux.
2023-02-20 19:22:22 +01:00
Anton Khirnov cd4cd95d5e fftools/ffmpeg_demux: add an AVClass to Demuxer/InputFile
Use it for logging. This makes log messages related to this input file
more consistent.
2023-02-20 19:22:22 +01:00
Anton Khirnov 42a0dd6e7e fftools/ffmpeg: add an option for writing pre-muxing stats
Analogous to -enc_stats*, but happens right before muxing. Useful
because bitstream filters and the sync queue can modify packets after
encoding and before muxing. Also has access to the muxing timebase.
2023-02-09 15:24:15 +01:00
Jan Ekström 9a820ec8b1 ffmpeg: add video heartbeat capability to fix_sub_duration
Splits the currently handled subtitle at random access point
packets that can be configured to follow a specific output stream.
Currently only subtitle streams which are directly mapped into the
same output in which the heartbeat stream resides are affected.

This way the subtitle - which is known to be shown at this time
can be split and passed to muxer before its full duration is
yet known. This is also a drawback, as this essentially outputs
multiple subtitles from a single input subtitle that continues
over multiple random access points. Thus this feature should not
be utilized in cases where subtitle output latency does not matter.

Co-authored-by: Andrzej Nadachowski <andrzej.nadachowski@24i.com>
Co-authored-by: Bernard Boulay <bernard.boulay@24i.com>

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
2023-02-03 16:17:29 +02:00
Anton Khirnov 806ecace91 fftools/ffmpeg: support input frame params in encoding stats 2023-01-31 09:09:23 +01:00
Anton Khirnov 9b5036fabd fftools/ffmpeg: add an AVClass to MuxStream/OutputStream
Use it for logging. This makes log messages related to this output
stream more consistent.
2023-01-29 09:12:22 +01:00
Anton Khirnov d2c983c213 fftools/ffmpeg: add an AVClass to Muxer/OutputFile
Use it for logging. This makes log messages related to this output file
more consistent.
2023-01-29 09:10:57 +01:00
Anton Khirnov 425b2c4a56 fftools/ffmpeg: add options for writing encoding stats
Similar to -vstats, but more flexible:
- works for audio as well as video
- frame and/or packet information
- user-specifiable format
2023-01-29 09:09:59 +01:00
Anton Khirnov 2fa2e146cc fftools/ffmpeg: avoid storing full forced keyframe spec
It is not needed after the spec is parsed. Also avoids ugly string
comparisons for each video frame.
2022-11-28 10:28:14 +01:00
Anton Khirnov 334e52e094 fftools/ffmpeg: parse forced keyframes in of_open()
Allows to remove the ugly of_get_chapters() wrapper.
2022-11-28 10:28:14 +01:00
Anton Khirnov efe4423627 fftools/ffmpeg: store forced keyframe pts in AV_TIME_BASE_Q
Rather than the encoder timebase. Since the times are parsed as
microseconds, this will not reduce precision, except possibly when
chapter times are used and the chapter timebase happens to be better
aligned with the encoder timebase, which is unlikely.

This will allow parsing the keyframe times earlier (before encoder
timebase is known) in future commits.
2022-11-28 10:28:14 +01:00
Anton Khirnov b1143330c8 fftools/ffmpeg: move force-keyframe-related vars to a separate struct
There are 8 of them and they are typically used together. Allows to pass
just this struct to forced_kf_apply(), which makes it clear that the
rest of the OutputStream is not accessed there.
2022-11-28 10:28:14 +01:00
Anton Khirnov 95af0bcc34 fftools/ffmpeg_mux_init: postpone matching -disposition to streams
Do it in set_dispositions() rather than during stream creation.

Since at this point all other stream information is known, this allows
setting disposition based on metadata, which implements #10015. This
also avoids an extra allocated string in OutputStream that was unused
after of_open().
2022-11-23 10:36:23 +01:00
Anton Khirnov 3da5c60aa9 fftools/ffmpeg: _-prefix variables in MATCH_PER_STREAM_OPT()
Avoids conflicts, e.g. when 'i' is used in the outvar expression.
2022-11-23 10:36:23 +01:00
Anton Khirnov fd8bf8d3b5 fftools/ffmpeg: remove the input_streams global
Replace it with an array of streams in each InputFile. This is a more
accurate reflection of the actual relationship between InputStream and
InputFile.

Analogous to what was previously done to output streams in
7ef7a22251.
2022-11-23 10:36:23 +01:00
Anton Khirnov 4c40581614 fftools/ffmpeg: replace OutputStream.source_index with a pointer to InputStream
This is simpler. The indirection via an index exists for historical
reasons that longer apply.
2022-11-23 10:36:23 +01:00
Anton Khirnov b58264217b fftools/ffmpeg_mux_init: drop an always-false check
It cannot be true since 1959351aec. Effectively reverts 6a3833e141.
2022-11-17 10:52:58 +01:00
Anton Khirnov 52380a055b fftools/ffmpeg_mux_init: move validating codec avoptions to a separate function 2022-11-17 10:52:58 +01:00
Anton Khirnov 0fb7d111e8 fftools/ffmpeg: move OutputStream.max_frames to MuxStream
It no longer needs to be visible outside of the muxing code.
2022-11-17 10:52:58 +01:00
Anton Khirnov c15eb2773a fftools/ffmpeg_[de]mux: constify all uses of OptionsContext 2022-11-16 08:41:14 +01:00
Anton Khirnov 4119480681 fftools/ffmpeg_mux_init: stop using OptionsContext as storage in copy_metadata()
It should be input-only to this code. Will allow making it const in
future commits.
2022-11-16 08:41:14 +01:00
Gyan Doshi 5ccd4d3060 ffmpeg: fix implementation of updated input start time
The current adjustment of input start times just adjusts the tsoffset.
And it does so, by resetting the tsoffset to nullify the new start time.
This leads to breakage of -copyts, ignoring of input_ts_offset, breaking
of -isync as well as breaking wrap correction.

Fixed by taking cognizance of these parameters, and by correcting start times
just before sync offsets are applied.
2022-11-03 14:38:42 +05:30
Anton Khirnov 5c1a096d02 fftools/ffmpeg: move nb_streams_warn from InputFile to Demuxer
It is private to the demuxer and do not need to be visible outside of
it.
2022-10-25 11:04:42 +02:00
Anton Khirnov 541104f3a3 fftools/ffmpeg: move duration/time_base from InputFile to Demuxer
They are private to the demuxer and do not need to be visible outside of
it.
2022-10-25 11:04:42 +02:00
Anton Khirnov 5bc1f177d3 fftools/ffmpeg: move threading fields from InputFile to Demuxer
They are private to the demuxer and do not need to be visible outside of
it.
2022-10-25 11:04:42 +02:00
Anton Khirnov c20977c4e0 fftools/ffmpeg: drop free_input_threads()
Stop demuxer threads in ifile_close() instead. Simplifies the demuxer
API.
2022-10-25 11:04:42 +02:00
Anton Khirnov 295848bacb fftools/ffmpeg: move closing the input file into a separate function
For now this is just closing the format context and freeing InputFile,
but will contain more in the future.
2022-10-25 11:04:42 +02:00
Anton Khirnov 09cd147dcc fftools/ffmpeg: drop init_input_threads()
Start threads implicitly when ifile_get_packet() is called. Simplifies
the demuxer API.
2022-10-25 11:04:42 +02:00
Anton Khirnov 6975320506 fftools/ffmpeg_demux: add demuxer private data
Move InputFile.loop into it.
2022-10-25 11:04:42 +02:00
Anton Khirnov 78efefa9a5 fftools/ffmpeg_opt: move opening input files to ffmpeg_demux.c
This is similar to what was done before for output files and will allow
introducing demuxer-private state in future commits

Unlike for muxing, the code is moved to existing ffmpeg_demux.c rather
than to a new file. The reason is just file size - the demuxing code is
much smaller than muxing.
2022-10-25 11:04:42 +02:00
Anton Khirnov 4cfffdd551 fftools/ffmpeg: rename read_file() to avoid conflict with libass
libass defines a non-static read_file() symbol, which causes conflicts
with static linking.
2022-10-21 10:13:16 +02:00
Jan Ekström b9058765d7 ffmpeg: Deprecate display rotation override with a metadata key
Now that we have proper options for defining display matrix
overrides, this should no longer be required.

fftools does not have its own versioning, so for now the define is
just set to 1 and disables the functionality if set to zero.
2022-10-19 11:53:52 +02:00
Jan Ekström c889248647 ffmpeg: Add display_{rotation, hflip, vflip} options
This enables overriding the rotation as well as horizontal/vertical
flip state of a specific video stream on the input side.

Additionally, switch the singular test that was utilizing the rotation
metadata to instead override the input display rotation, thus leading
to the same result.
2022-10-19 11:53:52 +02:00
Andreas Rheinhardt ff2c37d449 fftools/ffmpeg_opt: Move stuff only used by ffmpeg_mux_init to it
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-10-18 14:19:11 +02:00
Anton Khirnov c5d7b6f49b fftools/ffmpeg_mux: move muxing queue fields from OutputStream to MuxStream
They are private to the muxer and do not need to be visible outside of
it.
2022-10-18 14:19:11 +02:00
Anton Khirnov f0cd68eea0 fftools/ffmpeg_mux: move bsf_ctx from OutputStream to MuxStream
It is private to the muxer and does not need to be visible outside of
it.
2022-10-18 14:19:11 +02:00
Anton Khirnov 7ef7a22251 fftools/ffmpeg: remove the output_streams global
Replace it with an array of streams in each OutputFile. This is a more
accurate reflection of the actual relationship between OutputStream and
OutputFile. This is easier to handle and will allow further
simplifications in future commits.
2022-10-18 13:57:43 +02:00
Anton Khirnov ee0a900e58 fftools/ffmpeg_mux: move sq_mux from OutputFile to Muxer
It is internal to ffmpeg_mux* and does not need to be visible to other
code.
2022-10-18 13:57:43 +02:00
Anton Khirnov 36ce335d46 fftools/ffmpeg_mux: inline of_muxer_init() into of_open()
A separate muxer init is no longer necessary, now that of_open() has
access to Muxer.
2022-10-18 13:57:43 +02:00
Anton Khirnov 65d106933a fftools/ffmpeg_mux: embed OutputFile in a Muxer
This is now possible since the code allocating OutputFile can see
sizeof(Muxer). Avoids the overhead and extra complexity of allocating
two objects instead of one.

Similar to what is done e.g. for AVStream/FFStream in lavf.
2022-10-18 13:57:43 +02:00
Anton Khirnov 18d6c07267 fftools/ffmpeg_opt: move opening output files into a new file
ffmpeg_opt.c currently contains code for
- parsing the options provided on the command line
- opening and initializing input files based on these options
- opening and initializing output files based on these options

The code dealing with each of these is for the most part disjoint, so it
makes sense to move them to separate files. Beyond reducing the quite
considerable size of ffmpeg_opt.c, this will also allow exposing muxer
internals (currently private to ffmpeg_mux.c) to the initialization
code, thus removing the awkward separation currently in place.
2022-10-18 13:57:42 +02:00
Anton Khirnov 965bff37b6 fftools/ffmpeg: move some stream initialization code to ffmpeg_mux
The code in question is muxing-specific and so belongs there. This will
allow make some objects private to the muxer in future commits.
2022-10-18 13:57:42 +02:00
Anton Khirnov d579a70291 fftools/ffmpeg: move output_packet() to ffmpeg_mux
This function is common to both transcoding and streamcopy, so it
properly belongs into the muxing code.
2022-10-18 13:57:42 +02:00
Marvin Scholz 3ce1616106 ffmpeg: Make find_stream_info behave like a normal per-file option
Currently it would essentially change the find_stream_info setting for
the file it was specified for and all following files, which is unusual
and somewhat unexpected behaviour for a per-file option and not even
documented to behave like this.

Signed-off-by: Anton Khirnov <anton@khirnov.net>
2022-10-13 10:19:13 +02:00
Anton Khirnov 29188f9c62 fftools/ffmpeg: rename OutputStream.sync_opts to next_pts
The current name is confusing.
2022-10-04 11:55:03 +02:00
Anton Khirnov 76678b7137 fftools/ffmpeg: drop never-set OutputStream.first_pts 2022-10-04 11:55:03 +02:00
Anton Khirnov 3d86a13b47 fftools/ffmpeg: drop the -async option
It has been deprecated in favor of the aresample filter for almost 10
years.

Another thing this option can do is drop audio timestamps and have them
generated by the encoding code or the muxer, but
- for encoding, this can already be done with the setpts filter
- for muxing this should almost never be done as timestamp generation by
  the muxer is deprecated, but people who really want to do this can use
  the setts bitstream filter
2022-10-04 11:55:03 +02:00
Marvin Scholz cc48454561 fftools/ffmpeg: Remove unused frame_bits_per_raw_sample variable
Unused since the bits_per_raw_sample was made a per-output-stream
option in 4258893961

Signed-off-by: Anton Khirnov <anton@khirnov.net>
2022-10-02 11:59:25 +02:00
Marvin Scholz 793953f44a fftools/ffmpeg: Remove unused do_deinterlace variable
Unused since removal of the -deinterlace option in
d013453caa

Signed-off-by: Anton Khirnov <anton@khirnov.net>
2022-10-02 11:59:25 +02:00
Marvin Scholz 1c8e95bd99 fftools: Remove unused videotoolbox_init declaration
The code that defined videotoolbox_init was removed in
4b54818981

Signed-off-by: Anton Khirnov <anton@khirnov.net>
2022-10-02 11:59:25 +02:00
Marvin Scholz d2cf6d1665 fftools: Remove unused qsv declarations
The code that uses these was removed in
ecee3b07cd

Signed-off-by: Anton Khirnov <anton@khirnov.net>
2022-10-02 11:59:25 +02:00
Anton Khirnov 4fce3bab64 fftools/ffmpeg: use a separate counter for encoded packet data size
update_video_stats() currently uses OutputStream.data_size to print the
total size of the encoded stream so far and the average bitrate.
However, that field is updated in the muxer thread, right before the
packet is sent to the muxer. Not only is this racy, but the numbers may
not match even if muxing was in the main thread due to bitstream
filters, filesize limiting, etc.

Introduce a new counter, data_size_enc, for total size of the packets
received from the encoder and use that in update_video_stats(). Rename
data_size to data_size_mux to indicate its semantics more clearly.

No synchronization is needed for data_size_mux, because it is only read
in the main thread in print_final_stats(), which runs after the muxer
threads are terminated.
2022-08-29 15:42:11 +02:00
Anton Khirnov 0dd7347963 fftools/ffmpeg: drop OutputStream.fps_mode
It is only used within new_video_stream(), so make it a local variable
there.
2022-08-29 15:42:11 +02:00
Anton Khirnov d0f767f81f fftools/ffmpeg: drop OutputStream.enc
It is either equal to OutputStream.enc_ctx->codec, or NULL when enc_ctx
is NULL. Replace the use of enc with enc_ctx->codec, or the equivalent
enc_ctx->codec_* fields where more convenient.
2022-08-29 15:42:11 +02:00
Anton Khirnov 7c8737548f fftools/ffmpeg: remove a stale extern declaration
Forgotten in 8cbf229c94
2022-08-29 15:42:11 +02:00
Anton Khirnov 8d26a21bf6 fftools/ffmpeg: stop accessing av_stream_get_parser() from the main thread
It races with the demuxing thread. Instead, send the information along
with the demuxed packets.

Ideally, the code should stop using the stream-internal parsing
completely, but that requires considerably more effort.

Fixes races, e.g. in:
- fate-h264-brokensps-2580
- fate-h264-extradata-reload
- fate-iv8-demux
- fate-m4v-cfr
- fate-m4v
2022-08-29 15:42:11 +02:00
Anton Khirnov f23e3ce858 fftools/ffmpeg: call av_guess_frame_rate() when opening the file
It is currently called when configuring the filter, which races with the
demuxer thread.
2022-08-22 11:36:43 +02:00
Anton Khirnov ab31473830 fftools/ffmpeg: store a separate copy of input codec parameters
Use it instead of AVStream.codecpar in the main thread. While
AVStream.codecpar is documented to only be updated when the stream is
added or avformat_find_stream_info(), it is actually updated during
demuxing. Accessing it from a different thread then constitutes a race.

Ideally, some mechanism should eventually be provided for signalling
parameter updates to the user. Then the demuxing thread could pick up
the changes and propagate them to the decoder.
2022-08-16 11:09:09 +02:00
Anton Khirnov 3b2beceae1 fftools/ffmpeg: use a separate variable for discontinuity offset
This will allow to move normal offset handling to demuxer thread, since
discontinuities currently have to be processed in the main thread, as
the code uses some decoder-produced values.
2022-08-13 12:41:05 +02:00
Anton Khirnov 274c8d5882 fftools/ffmpeg: pre-compute the streamcopy start pts before transcoding starts
InputFile.ts_offset can change during transcoding, due to discontinuity
correction. This should not affect the streamcopy starting timestamp.

Cf. bf2590aed3
2022-08-13 12:41:05 +02:00
Anton Khirnov 61d9f34c70 fftools/ffmpeg_demux: do not store demux packet in the context
Its use is local to input_thread().
2022-08-08 16:20:58 +02:00
Anton Khirnov aae9de0cb2 fftools/ffmpeg: move -stream_loop handling to the demuxer thread
-stream_loop is currently handled by destroying the demuxer thread,
seeking, then recreating it anew. This is very messy and conflicts with
the future goal of moving each major ffmpeg component into its own
thread.

Handle -stream_loop directly in the demuxer thread. Looping requires the
demuxer to know the duration of the file, which takes into account the
duration of the last decoded audio frame (if any). Use a thread message
queue to communicate this information from the main thread to the
demuxer thread.
2022-08-08 16:20:58 +02:00
Anton Khirnov b99462cd27 fftools/ffmpeg: move seek_to_start() to ffmpeg_demux.c
Reduces the diff in the following commit.
2022-08-08 16:20:58 +02:00
Anton Khirnov 57d75ca031 fftools/ffmpeg: move get_input_packet() to ffmpeg_demux.c
Also rename it to use the ifile_* namespace.
2022-08-08 16:20:58 +02:00
Anton Khirnov 7b6622705e fftools/ffmpeg: move the input thread into its own file
It will contain more demuxing-specific code in the future.
2022-08-08 16:20:58 +02:00
Anton Khirnov c3b8d59d2f fftools/ffmpeg: drop a write-only variable 2022-08-08 16:20:58 +02:00
Anton Khirnov cc2b7f4625 fftools/ffmpeg: store the input file index in InputFile
Use it to simplify some code and fix two off-by-one errors.

Similar to what was previously done for OutputFile.
2022-08-08 16:20:58 +02:00
Anton Khirnov fee249b30a fftools/ffmpeg: deprecate specifying a sync stream with -map
It has not had any effect whatsoever for over 10 years.
2022-08-08 16:20:58 +02:00
Anton Khirnov 49123dd058 fftools/ffmpeg: remove OutputStream.sync_ist
It is not actually used for anything.
2022-08-08 16:20:58 +02:00
Anton Khirnov 8e092c3eac fftools/ffmpeg: remove OutputStream.encoding_needed
It is unnecessary, as it is always exactly equivalent to !!ost->enc_ctx
2022-08-08 16:20:58 +02:00
Anton Khirnov 80e7feb48b fftools/ffmpeg: remove OutputStream.stream_copy
There are currently three possible modes for an output stream:
1) The stream is produced by encoding output from some filtergraph. This
   is true when ost->enc_ctx != NULL, or equivalently when
   ost->encoding_needed != 0.
2) The stream is produced by copying some input stream's packets. This
   is true when ost->enc_ctx == NULL && ost->source_index >= 0.
3) The stream is produced by attaching some file directly. This is true
   when ost->enc_ctx == NULL && ost->source_index < 0.

OutputStream.stream_copy is currently used to identify case 2), and
sometimes to confusingly (or even incorrectly) identify case 1). Remove
it, replacing its usage with checking enc_ctx/source_index values.
2022-08-08 16:20:58 +02:00
Anton Khirnov 98ece428e3 fftools/ffmpeg: deprecate the -map_channel option
It is now entirely redundant with audio filters, and is in fact
implemented by setting up a 'pan' filter instance.
2022-07-28 16:37:16 +02:00
Anton Khirnov 4f91782fcd fftools/ffmpeg: move guess_input_channel_layout() to ffmpeg_opt.c
That is the only place where it is used. Also make it static.
2022-07-28 16:37:16 +02:00
Anton Khirnov 6353b28042 fftools/ffmpeg: drop unused hwaccel variables 2022-07-28 16:37:16 +02:00
Anton Khirnov 1a378b8274 fftools/ffmpeg: deprecate -psnr
It is entirely redundant with -flags +psnr.
2022-07-28 16:37:16 +02:00
Anton Khirnov 61e42e04ed fftools/ffmpeg: drop OutputStream.ref_par
It serves no purpose, codec parameters can be written directly to
AVStream.codecpar with the same effect.
2022-07-28 16:37:16 +02:00
Anton Khirnov 2d924b3a63 fftools/ffmpeg: move each muxer to a separate thread 2022-07-23 11:53:19 +02:00
Anton Khirnov 760ce4bc0b fftools/ffmpeg: depend on threads
ffmpeg will be switched to a fully threaded architecture, starting with
muxers.
2022-07-23 11:53:19 +02:00
Anton Khirnov 52bc8a842e fftools/ffmpeg_mux: return errors from of_submit_packet()
Do not call exit_program(), as that would conflict with moving this code
into a separate thread.
2022-07-23 11:53:19 +02:00
Anton Khirnov ff593c6c88 fftools/ffmpeg: make the muxer AVFormatContext private to ffmpeg_mux.c
Since the muxer will operate in a separate thread in the future, the
muxer context should not be accessed from the outside.
2022-07-23 11:53:19 +02:00
Anton Khirnov 4403851ca9 fftools/ffmpeg: only set OutputStream.frame_number for video encoding
It is unused otherwise.

Rename the field to vsync_frame_number to better reflect its current
purpose.
2022-07-23 11:53:19 +02:00
Anton Khirnov 4740fea7dd fftools/ffmpeg: rework -shortest implementation
The -shortest option (which finishes the output file at the time the
shortest stream ends) is currently implemented by faking the -t option
when an output stream ends. This approach is fragile, since it depends
on the frames/packets being processed in a specific order. E.g. there
are currently some situations in which the output file length will
depend unpredictably on unrelated factors like encoder delay. More
importantly, the present work aiming at splitting various ffmpeg
components into different threads will make this approach completely
unworkable, since the frames/packets will arrive in effectively random
order.

This commit introduces a "sync queue", which is essentially a collection
of FIFOs, one per stream. Frames/packets are submitted to these FIFOs
and are then released for further processing (encoding or muxing) when
it is ensured that the frame in question will not cause its stream to
get ahead of the other streams (the logic is similar to libavformat's
interleaving queue).

These sync queues are then used for encoding and/or muxing when the
-shortest option is specified.

A new option – -shortest_buf_duration – controls the maximum number of
queued packets, to avoid runaway memory usage.

This commit changes the results of the following tests:
- copy-shortest[12]: the last audio frame is now gone. This is
  correct, since it actually outlasts the last video frame.
- shortest-sub: the video packets following the last subtitle packet are
  now gone. This is also correct.
2022-07-23 11:53:19 +02:00
Anton Khirnov d02ae31fb2 fftools/ffmpeg: use pre-BSF DTS for choosing next output
The following commits will add a new buffering stage after bitstream
filters, which should not be taken into account for choosing next
output.

OutputStream.last_mux_dts is also used by the muxing code to make up
missing DTS values - that field is now moved to the muxer-private
MuxStream object.
2022-07-23 11:53:19 +02:00
Anton Khirnov b2b9e9ccee fftools/ffmpeg: use last filter output pts to choose next output stream
This will be needed in following commits that will add new buffering
stages after encoding and bitstream filtering.
2022-07-23 11:53:19 +02:00
Anton Khirnov 52fee96ae9 fftools/ffmpeg: move output file opts into private context
It is private to the muxer, no reason to access it from outside.
2022-07-23 11:53:19 +02:00
Anton Khirnov ec00b005f9 fftools/ffmpeg_mux: split of_write_packet()
It is currently called from two places:
- output_packet() in ffmpeg.c, which submits the newly available output
  packet to the muxer
- from of_check_init() in ffmpeg_mux.c after the header has been
  written, to flush the muxing queue

Some packets will thus be processed by this function twice, so it
requires an extra parameter to indicate the place it is called from and
avoid modifying some state twice.

This is fragile and hard to follow, so split this function into two.
Also rename of_write_packet() to of_submit_packet() to better reflect
its new purpose.
2022-07-23 11:53:19 +02:00
Anton Khirnov 9c2b800203 fftools/ffmpeg: move the mux queue into muxer private data
The muxing queue currently lives in OutputStream, which is a very large
struct storing the state for both encoding and muxing. The muxing queue
is only used by the code in ffmpeg_mux, so it makes sense to restrict it
to that file.

This makes the first step towards reducing the scope of OutputStream.
2022-07-23 11:53:19 +02:00
Anton Khirnov 48989efb76 fftools/ffmpeg: access output file chapters through a wrapper
Avoid accessing the muxer context directly, as this will become
forbidden in future commits.
2022-07-23 11:53:19 +02:00
Anton Khirnov 4877842bb5 fftools/ffmpeg: refactor the code checking for bitexact output
Figure out earlier whether the output stream/file should be bitexact and
store this information in a flag in OutputFile/OutputStream.

Stop accessing the muxer in set_encoder_id(), which will become
forbidden in future commits.
2022-07-23 11:53:19 +02:00
Anton Khirnov 12e9e50219 fftools/ffmpeg: set want_sdp when initializing the muxer
Allows making the variable local to ffmpeg_mux.
2022-07-23 11:53:19 +02:00
Anton Khirnov d8e944c238 fftools/ffmpeg: refactor limiting output file size with -fs
Move the file size checking code to ffmpeg_mux. Use the recently
introduced of_filesize(), making this code consistent with the size
shown by print_report().
2022-07-23 11:53:19 +02:00
Anton Khirnov 81af4dec27 fftools/ffmpeg: fix the type of limit_filesize
The option is parsed as INT64 (signed). It is also compared to the
output of avio_tell(), which is also int64_t.
2022-07-23 11:53:19 +02:00
Anton Khirnov 9fe62a545f fftools/ffmpeg: add a helper function to access output file size
Stop accessing muxer internals from outside of ffmpeg_mux.
2022-07-23 11:53:19 +02:00
Anton Khirnov 6a23be92d2 fftools/ffmpeg_mux: add private muxer context
Move header_written into it, which is not (and should not be) used by
any code outside of ffmpeg_mux.

In the future this context will contain more muxer-private state that
should not be visible to other code.
2022-07-23 11:53:19 +02:00
Gyan Doshi 882aac99d2 ffmpeg: add option -isync
This is a per-file input option that adjusts an input's timestamps
with reference to another input, so that emitted packet timestamps
account for the difference between the start times of the two inputs.

Typical use case is to sync two or more live inputs such as from capture
devices. Both the target and reference input source timestamps should be
based on the same clock source.

If either input lacks starting timestamps, then no sync adjustment is made.
2022-07-14 15:48:24 +05:30
Marton Balint 20e2aa940c fftools/ffmpeg: change frame counters to 64 bit
Frame counters can overflow relatively easily (INT_MAX number of frames is
slightly more than 1 year for 60 fps content), so make sure we are always
using 64 bit values for them.

A live stream can easily run for more than a year and the framedup logic breaks
on an overflow.

Signed-off-by: Marton Balint <cus@passwd.hu>
2022-06-27 20:37:43 +02:00
Gyan Doshi 09c53a04c5 ffmpeg: add option fps_mode
fps_mode sets video sync per output stream. Overrides vsync for matching
streams.

vsync is deprecated.
2022-06-11 09:47:27 +05:30
Anton Khirnov 3d4d355375 fftools/ffmpeg: stop using AVStream.nb_frames in do_video_stats()
Its use for muxing is not documented, in practice it is incremented per
each packet successfully passed to the muxer's write_packet(). Since
there is a lot of indirection between ffmpeg receiving a packet from the
encoder and it actually being written (e.g. bitstream filters, the
interleaving queue), using nb_frames here is incorrect.

Add a new counter for packets received from encoder instead.
2022-05-24 13:46:57 +02:00
Anton Khirnov 4a7c8bb3d8 fftools/ffmpeg: store output format separately from the muxer context
Allows accessing it without going through the muxer context. This will
be useful in the following commits, where the muxer context will be
hidden.
2022-04-13 12:07:54 +02:00
Anton Khirnov 5c66ee6351 fftools/ffmpeg: move freeing the output file to ffmpeg_mux.c 2022-04-13 12:07:54 +02:00
Anton Khirnov 5bc644ea8a fftools/ffmpeg: move writing the trailer to ffmpeg_mux.c 2022-04-13 12:07:54 +02:00
Anton Khirnov 288aa37387 fftools/ffmpeg: move some muxing-related code into a separate file
This is a first step towards making muxers more independent from the
rest of the code.
2022-04-13 12:07:54 +02:00
Anton Khirnov b1a984cb49 fftools/ffmpeg: store the output file index in OutputFile
Use it to simplify check_init_output_file(). Will allow further
simplifications in the following commits.
2022-04-13 12:07:54 +02:00
Anton Khirnov 662364b2ef fftools/ffmpeg: stop using OutputStream.frame_number for streamcopy
This field is currently used by checks
- skipping packets before the first keyframe
- skipping packets before start time
to test whether any packets have been output already. But since
frame_number is incremented after the bitstream filters are applied
(which may involve delay), this use is incorrect. The keyframe check
works around this by adding an extra flag, the start-time check does
not.

Simplify both checks by replacing the seen_kf flag with a flag tracking
whether any packets have been output by do_streamcopy().
2022-04-13 12:07:54 +02:00
James Almer bc99e3a9d6 ffmpeg: replace custom channel_layout code with an SpecifierOpt based one
This is cleaner and allows fine tuning which stream the option is applied to.

Signed-off-by: James Almer <jamrial@gmail.com>
2022-03-22 08:45:38 -03:00