Commit Graph

139 Commits

Author SHA1 Message Date
Anton Khirnov 93e26a4db7 fftools/ffmpeg: handle -enc_time_base -1 during stream creation
There is no reason to postpone it until opening the encoder. Also, abort
when the input stream is unknown, rather than disregard an explicit
request from the user.
2023-06-05 16:15:04 +02:00
Anton Khirnov 58a64e3d54 fftools/ffmpeg_mux_init: only process -enc_time_base if the stream is encoded
It has no effect otherwise.
2023-06-05 16:15:04 +02:00
Anton Khirnov c8a85d1b2f fftools/ffmpeg_mux_init: do not overwrite OutputStream.frame_rate for streamcopy
The values currently written into it are not used after
streamcopy_init(), so it is better to confine them to that function.
2023-06-05 16:15:04 +02:00
Anton Khirnov 57021a68d9 fftools/ffmpeg_mux: set stream duration after the timebase is certainly known
Stop assuming the encoder knows the muxing timebase, which does not
always have to hold (e.g. due to bitstream filters).
2023-06-05 16:15:04 +02:00
Anton Khirnov 7520cd9f07 fftools/ffmpeg_mux_init: move OutputFilter setup code to ffmpeg_filter
That is a more appropriate place for it.
2023-05-31 16:15:47 +02:00
Anton Khirnov 87b576135e fftools/ffmpeg_filter: factor out binding an output stream to OutputFilter
While the new function is trivial for now, it will become more useful in
future commits.
2023-05-31 16:15:47 +02:00
Anton Khirnov 7ea10bfef3 fftools/ffmpeg_filter: store just the link label in OutputFilter
Not the entire AVFilterInOut. This is simpler.
2023-05-31 16:15:47 +02:00
Anton Khirnov da7ae627e1 fftools/ffmpeg_filter: always pass graph description to fg_create()
Currently NULL would be passed for simple filtergraphs, which would
make the filter code extract the graph description from the output
stream when needed. This is unnecessarily convoluted.
2023-05-31 16:15:47 +02:00
Anton Khirnov b56db2956b fftools/ffmpeg: add logging for creating output streams 2023-05-31 16:15:47 +02:00
Anton Khirnov eeb7d92ac7 fftools/ffmpeg_mux_init: merge ost_add_from_filter() to ost_add()
This way ost_add() knows about the complex filtergraph it is fed from,
which will become useful in future commits.
2023-05-31 16:15:47 +02:00
Anton Khirnov e9a868e9ee fftools/sync_queue: add debug logging 2023-05-28 10:47:59 +02:00
Anton Khirnov 6abb4a28ef fftools/ffmpeg: add InputStream.index
This allows to avoid access to the underlying AVStream in many places.
2023-05-28 10:47:59 +02:00
Anton Khirnov ec75b79fe1 fftools/ffmpeg: fail earlier on text/bitmap subtitles mismatch
Checking whether the user requested an unsupported conversion between
text and bitmap subtitles can be done immediately when creating the
output stream.
2023-05-28 10:47:59 +02:00
Anton Khirnov dfa29ba955 fftools/ffmpeg: return error codes from ist_*_add()
Will be useful in future commits.
2023-05-22 17:10:44 +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 0add05bd3a fftools/ffmpeg_mux_init: rename init_output_filter() to ost_add_from_filter()
The previous name is misleading, because the function does not actually
initialize any filters - it creates a new output stream and binds a
filtergraph output to it.
2023-04-24 16:56:06 +02:00
Anton Khirnov 4800b50258 fftools/ffmpeg: simplify init_output_filter() 2023-04-24 16:56:06 +02:00
Anton Khirnov ea4c42779b fftools/ffmpeg_mux_init: drop a redundant assignment
OutputFilter.format is initialized in ofilter_alloc().
2023-04-24 16:56:06 +02:00
Anton Khirnov 2104de13e9 fftools/ffmpeg: reindent after previous commit 2023-04-24 16:56:06 +02:00
Anton Khirnov 6cace17b0d fftools/ffmpeg_mux_init: drop useless new_stream_{data,unknown}
Their only function is checking that encoding was not specified for
data/unknown-type streams, but the check is broken because enc_ctx will
not be created in ost_add() unless a valid encoder can be found.

Add an actually working check for all types for which encoding is not
supported in choose_encoder().
2023-04-24 16:56:06 +02:00
Anton Khirnov ab0d2052a3 fftools/ffmpeg: remove unused function arguments 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 84e1e0fa73 fftools/ffmpeg_mux_init: move check for mixing simple/complex filters
Do it in ost_get_filters() together with other similar checks. Will be
useful in following commits.

Also, improve the log message.
2023-04-24 16:56:06 +02:00
Anton Khirnov 0fe5eaf69d fftools/ffmpeg_mux_init: consolidate handling -filter for audio/video 2023-04-24 16:56:06 +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 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 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 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 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 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
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 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 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 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 923c6ab170 fftools/ffmpeg: use sync queues for enforcing audio frame size
The code currently uses lavfi for this, which creates a sort of
configuration dependency loop - the encoder should be ideally
initialized with information from the first audio frame, but to get this
frame one needs to first open the encoder to know the frame size. This
necessitates an awkward workaround, which causes audio handling to be
different from video.

With this change, audio encoder initialization is congruent with video.
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
Thilo Borgmann 653ee3f159 fftools/ffmpeg_mux_init: Use all metadata selectors if none is specified.
Fixes regression from 3c7dd5ed37.
Fixes ticket #10157.
2023-03-25 20:28:41 +01:00
Anton Khirnov 1e406692e5 fftools/ffmpeg_mux_init: avoid invalid reads in forced keyframe parsing
Fixes #10243
2023-03-20 10:53:03 +01:00
James Almer 6d6ae3595b avformat: remove FF_API_AVSTREAM_CLASS
Signed-off-by: James Almer <jamrial@gmail.com>
2023-02-09 15:35:14 +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 260f391893 fftools/ffmpeg: always generate CFR output when -r is used
Current code may, depending on the muxer, decide to use VSYNC_VFR tagged
with the specified framerate, without actually performing framerate
conversion. This is clearly wrong and against the documentation, which
states unambiguously that -r should produce CFR output for video
encoding.

FATE test changes:
* nuv-rtjpeg: replace -r with '-enc_time_base -1', which keeps the
  original timebase. Output frames are now produced with proper
  durations.
* filter-mpdecimate: just drop the -r option, it is unnecessary
* filter-fps-r: remove, this test makes no sense and actually
  produces broken VFR output (with incorrect frame durations).
2023-01-10 11:49:28 +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 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 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 86e2ffedfe fftools/ffmpeg_mux_init: simplify inner loop in map_auto_{video,audio}
Skip unusable streams early and do not compute any scores for them.
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 13947aff3f fftools/ffmpeg_mux_init: use av_dict_iterate() where appropriate 2022-11-17 10:52:58 +01:00
Anton Khirnov 19e192eae6 fftools/ffmpeg_mux_init: do not call av{codec,format}_get_class() repeatedly 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 6a8145a4b1 fftools/ffmpeg_mux_init: move more code from of_open() to create_streams()
Specificaly, the of_add_attachments() call (which can add attachment
streams to the output) and the check whether the output file contains
any streams. They both logically belong in create_streams().
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
Anton Khirnov 3c7dd5ed37 fftools/ffmpeg_mux_init: drop a duplicated block in copy_metadata()
It does the same thing as the block right below it.
2022-11-16 08:41:14 +01:00
Anton Khirnov 0d821edb40 fftools/ffmpeg_mux_init: stop modifying OptionsContext.*_disable
The current code will override the *_disable fields (set by -vn/-an
options) when creating output streams for unlabeled complex filtergraph
outputs, in order to disable automatic mapping for the corresponding
media type.

However, this will apply not only to automatic mappings, but to manual
ones as well, which should not happen. Avoid this by adding local
variables that are used only for automatic mappings.
2022-10-25 11:12:22 +02:00
Anton Khirnov 69da53ade9 fftools/ffmpeg_mux_init: move code creating streams into a new function
Makes it easy to see where all the streams are created. Will also be
useful in the following commit.
2022-10-25 11:12:05 +02:00
Anton Khirnov 006df0b6fe fftools/ffmpeg_mux_init: stop modifying some OptionsContext fields
Specifically recording_time and stop_time - use local variables instead.
OptionsContext should be input-only to this code. Will allow making it
const in future commits.
2022-10-25 11:11:35 +02:00
Anton Khirnov ccab823559 fftools/ffmpeg_mux_init: constify metadata specifier arguments 2022-10-25 11:04:42 +02:00
Anton Khirnov aa0ce91f57 fftools/ffmpeg_mux_init: avoid modifying OptionsContext.chapters_input_file
Use a local variable instead. This will allow making OptionsContext
const in future commits.
2022-10-25 11:04:42 +02:00
Anton Khirnov 5ccc151bf2 fftools/ffmpeg: factor out copying metadata/chapters from of_open()
This code shares variables like OptionsContext.metadata_*_manual, so it
makes sense to group it together.
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
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 2266e04834 fftools/ffmpeg_mux: embed OutputStream in a MuxStream
This is now possible since OutputStream is a child of OutputFile and the
code allocating it can access MuxStream. Avoids the overhead and extra
complexity of allocating two objects instead of one.

Similar to what was previously done for OutputFile/Muxer.
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 0baed07f74 fftools/ffmpeg_mux_init: pass Muxer to new_output_stream()
And intermediate functions. Will be useful in the following commit.
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 a55ca682e2 fftools/ffmpeg_mux: allocate sq_pkt in setup_sync_queues()
This is now possible since setup_sync_queues() can interact with 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