Commit Graph

1143 Commits

Author SHA1 Message Date
Anton Khirnov 3a89e6d352 fftools/ffmpeg_filter: restrict reap_filters() to a single filtergraph
This is more natural, as all except one of its callers require
processing only one filtergraph.
2023-07-20 20:30:13 +02:00
Anton Khirnov 9d44eb8af5 fftools/ffmpeg_filter: stop accessing encoder from pixfmt selection
ffmpeg CLI pixel format selection for filtering currently special-cases
MJPEG encoding, where it will restrict the supported list of pixel
formats depending on the value of the -strict option. In order to get
that value it will apply it from the options dict into the encoder
context, which is a highly invasive action even now, and would become a
race once encoding is moved to its own thread.

The ugliness of this code can be much reduced by moving the special
handling of MJPEG into ofilter_bind_ost(), which is called from encoder
init and is thus synchronized with it. There is also no need to write
anything to the encoder context, we can evaluate the option into our
stack variable.

There is also no need to access AVCodec at all during pixel format
selection, as the pixel formats array is already stored in
OutputFilterPriv.
2023-07-20 20:30:13 +02:00
Anton Khirnov 037d364797 fftools/ffmpeg_filter: move "smart" pixfmt selection to ffmpeg_mux_init
This code works on encoder information and has no interaction with
filtering, so it does not belong in ffmpeg_filter.
2023-07-20 20:30:04 +02:00
Anton Khirnov 249bca763d fftools/ffmpeg_mux_init: handle pixel format endianness
When -pix_fmt designates a BE/LE pixel format, it gets translated into
the native one by av_get_pix_fmt(). This may not always be the best
choice, as the encoder might only support one endianness. In such a
case, explicitly choose the endianness supported by the encoder.

While this is currently redundant with choose_pixel_fmt() in
ffmpeg_filter.c, the latter code will be deprecated in following commits.
2023-07-20 20:30:04 +02:00
Andreas Rheinhardt 463a472426 fftools/ffmpeg_enc: Reindentate after the last commit
Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-07-17 16:18:12 +02:00
Andreas Rheinhardt bfe15b81b3 fftools/ffmpeg_enc: Remove always-true checks
frame is always != NULL for audio and video here
(this is checked by an assert and the frame is already dereferenced
before it reaches this code here).
Fixes Coverity issue #1538858.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-07-17 16:14:41 +02:00
Anton Khirnov 153cf85b24 fftools/ffmpeg_mux_init: fix an array declaration
map_func is supposed to be an array of const pointer to function
returning int, not an array of pointer to function returning const int.

Reported-By: Martin Storsjö
2023-07-16 11:10:00 +02:00
Anton Khirnov dff3a283cd fftools/ffmpeg: rework -enc_time_base handling
Read the timebase from FrameData rather than the input stream. This
should fix #10393 and generally be more reliable.

Replace the use of '-1' to indicate demuxing timebase with the string
'demux'. Also allow to request filter timebase with
'-enc_time_base filter'.
2023-07-15 11:02:11 +02:00
Anton Khirnov 4d06742b93 fftools/ffmpeg: add more structure to FrameData
It now contains data from multiple sources, so group those items that
always come from the decoder. Also, initialize them to invalid values,
so that frames that did not originate from a decoder can be
distinguished.
2023-07-15 11:02:11 +02:00
Anton Khirnov 75d0af388f fftools/ffmpeg_filter: make OutputFilter.filter private
It should not be accessed from outside of filtering code.
2023-07-15 11:02:11 +02:00
Anton Khirnov 9a97f9cf3a fftools/ffmpeg_enc: initialize audio/video encoders from frame parameters
This is possible now that enc_open() is always called with a non-NULL
frame for audio/video.

Previously the code would directly reach into the buffersink, which is a
layering violation.
2023-07-15 11:02:11 +02:00
Anton Khirnov b295ec31f3 fftools/ffmpeg_filter: only flush vsync code if encoding actually started
Otherwise this has no effect.

Will be useful in following commits.
2023-07-15 11:02:11 +02:00
Anton Khirnov 0a6751a78a 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.
2023-07-15 11:02:11 +02:00
Anton Khirnov 20c42213ea fftools/ffmpeg_mux_init: drop an obsolete assignment
This line was added in c30a4489b4 along with
AVStream.sample_aspect_ratio. However, configuring SAR for video
encoding is now done after this code (specifically in enc_open(), which
is called when the first video frame to be encoded is obtained), so this
line cannot have any meaningful effect.
2023-07-15 11:02:11 +02:00
Anton Khirnov c328bff4da fftools/ffmpeg_enc: return errors from enc_subtitle() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 2e742a2c3c fftools/ffmpeg_mux: return errors from of_streamcopy() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 464a5e8e76 fftools/ffmpeg: handle error codes from process_input_packet()
None are returned for now, but that will change in future commits.
2023-07-15 11:02:11 +02:00
Anton Khirnov 5fe3914c39 fftools/ffmpeg: return an error instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 858b635613 fftools/ffmpeg_mux_init: replace all remaining aborts with returning error codes
Mainly concerns new_stream_*() and their callees.
2023-07-15 11:02:11 +02:00
Anton Khirnov 45473258ff fftools/ffmpeg_mux_init: return error codes from metadata processing instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov dd44871eb9 fftools/ffmpeg_mux_init: improve of_add_programs()
Replace duplicated(!) and broken* custom string parsing with
av_dict_parse_string(). Return error codes instead of aborting.

* e.g. it treats NULL returned from av_get_token() as "separator not
  found", when in fact av_get_token() only returns NULL on memory
  allocation failure
2023-07-15 11:02:11 +02:00
Anton Khirnov fd40197170 fftools/ffmpeg_mux_init: return error codes from validate_enc_avopt() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 4e3557aadb fftools/ffmpeg_mux_init: return error codes from parse_forced_key_frames() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 6691d131d8 fftools/ffmpeg_mux_init: return error codes from copy_meta() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 54cbe6f57d fftools/ffmpeg_mux_init: return error codes from ost_add() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 38a2fc2c29 fftools/ffmpeg_mux_init: move allocation out of prologue
ost_add() has a very large variable declaration prologue, performing
"active" actions that can fail in there is confusing.
2023-07-15 11:02:11 +02:00
Anton Khirnov 4e366100e9 fftools/ffmpeg_mux_init: return error codes from map_*() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov eee09cdf39 fftools/ffmpeg_mux_init: improve error handling in of_add_attachments()
* return error codes instead of aborting
* avoid leaking the AVIOContext on failure
* check the return code of avio_read()
2023-07-15 11:02:11 +02:00
Anton Khirnov b3eedca5e5 fftools/ffmpeg_mux_init: return errors from create_streams() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov c23cff5a8a fftools/ffmpeg_demux: return errors from ist_add() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov cce294638b fftools/ffmpeg: return errors from assert_file_overwrite() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 2d59873fc1 fftools/ffmpeg_demux: add logging for -dump_attachment
Makes it more clear what was written where.
2023-07-15 11:02:11 +02:00
Anton Khirnov 63e4e8e4f4 fftools/ffmpeg_demux: forward errors from dump_attachment() instead of aborting
Also, check the return code of avio_close().
2023-07-15 11:02:11 +02:00
Anton Khirnov ff0160cb37 fftools/ffmpeg_demux: drop a redundant avio_flush()
It is immediately followed by avio_close(), which is documented to flush
the buffers.
2023-07-15 11:02:11 +02:00
Anton Khirnov ad80857a97 fftools/ffmpeg_demux: return errors from ifile_open() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov c313cdd70c fftools/ffmpeg_mux_init: return errors from of_open() instead of aborting 2023-07-15 11:02:11 +02:00
Anton Khirnov 9a2335444b fftools/ffmpeg_mux: forward EOF from the sync queue
EOF from sq_receive() means no packets will ever be output by the sync
queue. Since the muxing sync queue is always used by all interleaved
(i.e. non-attachment) streams, this means no further packets can make
it to the muxer and we can terminate muxing now.
2023-07-13 13:08:56 +02:00
Anton Khirnov 6a9d3f46c7 fftools/ffmpeg: drop an obsolete debug log
The value it prints has not been cur_dts from lavf for a very long time,
so it's misleading.
2023-07-11 19:28:18 +02:00
Anton Khirnov 57a42a714b fftools/ffmpeg_filter: consolidate calling avfilter_graph_set_auto_convert()
Do not call it from choose_pix_fmts(), as that function is not supposed
to modify random filtergraph properties.
2023-07-11 19:28:18 +02:00
Anton Khirnov 432399780a fftools/ffmpeg_filter: make OutputFile.{formats,ch_layouts,sample_rates} private
They are not used outside of the filtering code.
2023-07-11 19:28:18 +02:00
Anton Khirnov a3ab5bf80d fftools/ffmpeg_filter: make OutputFile.width,height private
They are not used outside of the filtering code.
2023-07-11 19:28:18 +02:00
Anton Khirnov c19aa9c28f fftools/ffmpeg_filter: make OutputFile.ch_layout private
It is not used outside of the filtering code.
2023-07-11 19:28:18 +02:00
Anton Khirnov 42f3f54cf4 fftools/ffmpeg_filter: make OutputFile.format/sample_rate private
They are not used outside of the filtering code.
2023-07-11 19:28:18 +02:00
Anton Khirnov c9a99ffe51 fftools/ffmpeg_filter: make sure no input or output is bound twice 2023-07-11 19:28:18 +02:00
Marton Balint 7357012bb5 fftools/ffmpeg: fix negative timestamps at the beginning of the encoding
Also fix a couple of possible overflows while at it.

Fixes the negative initial timestamps in ticket #10358.

Signed-off-by: Marton Balint <cus@passwd.hu>
2023-07-09 19:50:15 +02:00
QiTong Li 79f41a0760 fftools/ffplay: fix typo in frame_queue_destory
Not sure if the function naming frame_queue_destory is intended because
"destory" is not really a word. Changing it to "destroy" makes more sense.

Signed-off-by: QiTong Li <liqitong@163.com>
Signed-off-by: Marton Balint <cus@passwd.hu>
2023-07-06 21:24:47 +02:00
James Almer 50f34172e0 fftools/ffplay: remove usage of internal AVInputFormat.read_seek field
It's an internal field, so it should not be touched.

Reviewed-by: Marton Balint <cus@passwd.hu>
Signed-off-by: James Almer <jamrial@gmail.com>
2023-07-02 19:04:25 -03:00
Anton Khirnov 1617d1a752 fftools/ffmpeg: pass subtitle decoder dimensions to sub2video
Restores behavior from before 20cacfe493.
Eventually this should be handled similarly to audio/video - with a
filtergraph reset or a manual scaler.
2023-06-19 09:48:56 +02:00
Anton Khirnov 88f80977eb fftools/ffmpeg: use AVFrame to pass subtitles from decoders to filters
Allows to use the same buffering code for all media types. Will also be
important for the following commit.
2023-06-19 09:48:56 +02:00
Anton Khirnov fa717baaa5 fftools/ffmpeg_enc: constify the subtitle passed to enc_subtitle() 2023-06-19 09:48:56 +02:00
Anton Khirnov e89a6d1089 fftools/ffmpeg_dec: move InputStream.prev_sub to Decoder
It does not need to be visible outside of decoding code.
2023-06-19 09:48:56 +02:00
Anton Khirnov a45b9d35b9 fftools/ffmpeg: move fix_sub_duration_heartbeat() to ffmpeg_dec
This way ffmpeg.c does not need to access InputStream.prev_sub and it
can be made private.
2023-06-19 09:48:55 +02:00
Anton Khirnov 01897c1788 fftools/ffmpeg_dec: move decoding to a separate thread
This is only a preparatory step to a fully threaded architecture and
does not yet make decoding truly parallel - the main thread will
currently submit a packet and wait until it has been fully processed by
the decoding thread before moving on. Decoder behavior as observed by
the rest of the program should remain unchanged. That will change in
future commits after encoders and filters are moved to threads and a
thread-aware scheduler is added.
2023-06-19 09:48:55 +02:00
Anton Khirnov 5293adb1a7 fftools/ffmpeg: attach bits_per_raw_sample information to frames
This way avoids encoders reaching into filters or decoders for this
information.
2023-06-19 09:48:55 +02:00
Anton Khirnov 7d4e00ccf0 fftools/ffmpeg_dec: stop using Decoder.pkt
It is only used for flushing the subtitle decoder, so allocate a
dedicated packet for that.

Keep Decoder.pkt unused for now, it will be repurposed in future
commits.
2023-06-19 09:48:55 +02:00
Anton Khirnov 1bdd53e2f9 fftools/ffmpeg_filter: make configure_filtergraph() static
It is no longer used outside of ffmpeg_filter.
2023-06-19 09:48:55 +02:00
Anton Khirnov b1a213ab5d fftools/ffmpeg_filter: reject filtergraphs with zero outputs
Nothing useful can be done with them currently.
2023-06-19 09:48:55 +02:00
Anton Khirnov a7aa05c599 fftools/ffmpeg_filter: add an AVClass to FilterGraph
Use it for logging.
2023-06-19 09:48:55 +02:00
Anton Khirnov a934392db0 fftools/ffmpeg_enc: move dup_warning global variable to Encoder 2023-06-19 09:48:55 +02:00
Anton Khirnov 1adad44fc7 fftools/ffmpeg_dec: move InputStream.hwaccel_pix_fmt to Decoder
It is purely decoder-internal state.
2023-06-19 09:48:55 +02:00
Anton Khirnov 174cb3accf fftools/ffmpeg_dec: remove pointless InputStream.hwaccel_retrieve_data
It is always set to hwaccel_retrieve_data() from ffmpeg_hw.c, so that
function can just be called directly instead.
2023-06-19 09:48:55 +02:00
Anton Khirnov 25d96ab6c0 fftools/ffmpeg_hw: inline hwaccel_decode_init() into its caller
The function is now trivial and cannot fail, so all error handling in
its caller can be removed.
2023-06-19 09:48:55 +02:00
Anton Khirnov 6c9cbf7507 fftools/ffmpeg_demux: reindent after previous commit 2023-06-19 09:48:55 +02:00
Anton Khirnov 98766dbbd7 fftools/ffmpeg_demux: move the loop out of add_input_streams()
Make the function process just one input stream at a time and save an
indentation level. Also rename it to ist_add() to be consistent with an
analogous function in ffmpeg_mux_init.
2023-06-19 09:48:55 +02:00
Anton Khirnov df81fb46ee fftools/ffmpeg_dec: simplify process_subtitle()
Its got_output argument always points to 1.
2023-06-19 09:48:55 +02:00
Anton Khirnov e9eb44ed88 fftools/ffmpeg_dec: drop always-0 InputStream.prev_sub.ret 2023-06-19 09:48:55 +02:00
Anton Khirnov 141d11cb3c fftools/ffmpeg_mux: make OutputStream.pkt private
It is no longer used outside of muxing code.
2023-06-05 16:16:13 +02:00
Anton Khirnov 730a2221af fftools/ffmpeg_enc: use a private AVPacket instance for encoding
The code currently uses OutputStream.pkt, which complicates its
ownership semantics.
2023-06-05 16:16:12 +02:00
Anton Khirnov f94957e8f4 fftools/ffmpeg_mux: simplify calling of_output_packet()
Use NULL packets to signal EOF instead of a separate variable. This is
made possible by the previous commit.
2023-06-05 16:16:12 +02:00
Anton Khirnov 96e1325d91 fftools/ffmpeg_mux: use a dedicated packet for BSF output
Currently of_output_packet() reuses the input packet, which requires its
callers to submit blank packets even on EOF, which makes the code more
complex.
2023-06-05 16:16:12 +02:00
Anton Khirnov c803b36b8f fftools/ffmpeg_enc: stop using OutputStream.initialized
It is set by the muxing code, which will not be synchronized with
encoding code after upcoming threading changes. Use an encoder-private
variable instead.
2023-06-05 16:15:04 +02:00
Anton Khirnov 568d414074 fftools/ffmpeg: attach filter framerate to frames
This way the encoder does not need to reach backward into the filter.
2023-06-05 16:15:04 +02:00
Anton Khirnov 9630341073 fftools/ffmpeg: factor out attaching FrameData to a frame
Will be useful in following commits.
2023-06-05 16:15:04 +02:00
Anton Khirnov d7781cfb95 fftools/ffmpeg: convert timestamps inside the muxer
Packets submitted to the muxer now have their timebase attached to them,
so the muxer can do conversion to muxing timebase and avoid exposing it
to callers.
2023-06-05 16:15:04 +02:00
Anton Khirnov cf121592c5 fftools/ffmpeg_mux_init: do not overwrite OutputStream.frame_rate for encoding
The values currently written into it are not used after
enc_open(), so it is better to confine them to that function.
2023-06-05 16:15:04 +02:00
Anton Khirnov 5bea8daa78 fftools/ffmpeg_enc: do not guess frame durations from output framerate
There is no reason to expect input frame durations to match output
framerate.
2023-06-05 16:15:04 +02:00
Anton Khirnov 2f92650c5d fftools/ffmpeg_enc: inline init_encoder_time_base() into its callers
The function now reduces to a ternary operator, so it is shorter and
clearer to eliminate it.
2023-06-05 16:15:04 +02:00
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 2baae6434b fftools/ffmpeg_enc: merge two adjacent video-specific blocks
There is no meaningful reason for them to be separated.
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 ccf219e361 fftools/ffmpeg_enc: avoid breaking exactly integer timestamps in vsync code
The code will currently add a small offset to avoid exact midpoints, but
this can cause inexact results when a float timestamp is exactly
representable as an integer.

Fixes off-by-one in the first frame duration in multiple FATE tests.
2023-06-05 16:15:04 +02:00
Anton Khirnov c7a05ac117 fftools/ffmpeg_demux: do not set AVCodecContext.framerate
For decoding, this field is used by the decoder to export information
to the caller; it does not make sense for the caller to set it.
2023-06-05 16:15:04 +02:00
Anton Khirnov d8c61ba723 fftools/ffmpeg_filter: drop a block disabled since 2012 2023-06-05 16:15:04 +02:00
Anton Khirnov ad14bdbcfd fftools/ffmpeg_filter: drop a write-only variable 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 ba1141d8a9 fftools/ffmpeg_enc: move nb_frames{dup,drop} globals into OutputStream 2023-06-05 16:15:04 +02:00
Anton Khirnov ba2c791627 fftools/ffmpeg_filter: do not flush encoders on parameter change
It makes no sense to do so.
2023-05-31 16:20:19 +02:00
Anton Khirnov 106167374c fftools/ffmpeg_mux: flush bsfs immediately on exceeding recoding time
Current code marks the output stream as finished and waits for a flush
packet, but that is both unnecessary and suspect, as in theory nothing
should be sent to a finished stream - not even flush packets.
2023-05-31 16:20:19 +02:00
Anton Khirnov 2674532eee fftools/ffmpeg_filter: constify the argument of filtergraph_is_simple() 2023-05-31 16:20:19 +02:00
Anton Khirnov 3bfc2c589a fftools/ffmpeg_filter: make InputStream.filter private
It is no longer accessed outside of ffmpeg_filter.
2023-05-31 16:20:19 +02:00
Anton Khirnov 5924b70075 fftools/ffmpeg_filter: make ifilter_has_all_input_formats() static
It is no longer used outside ffmpeg_filter.
2023-05-31 16:20:19 +02:00
Anton Khirnov 2262df5e8a fftools/ffmpeg_filter: drop unreachable code
Filtergraphs with no inputs are initialized as soon as all their outputs
are bound, so this code should not be reachable.
2023-05-31 16:20:19 +02:00
Anton Khirnov f3e0a83e8e fftools/ffmpeg_enc: stop configuring filters from encoder flush
There is no way for a filtergraph to have all input parameters, yet not
be configured, so this code should not be reachable.
2023-05-31 16:20:19 +02:00
Anton Khirnov f8abab673c fftools/ffmpeg: move sub2video handling to ffmpeg_filter
Make all relevant state per-filtergraph input, rather than per-input
stream. Refactor the code to make it work and avoid leaking memory when
a single subtitle stream is sent to multiple filters.
2023-05-31 16:20:19 +02:00
Anton Khirnov 20cacfe493 fftools/ffmpeg: rework setting sub2video parameters
Set them in ifilter_parameters_from_dec(), similarly to audio/video
streams. This reduces the extent to which sub2video filters need to be
treated specially.
2023-05-31 16:20:19 +02:00
Anton Khirnov ea5c39cef6 fftools/ffmpeg: tweak sub2video_heartbeat() arguments
This function should not take an InputStream, as it only uses it to get
the InputFile and the timebase. Pass those directly instead and avoid
confusion over dealing with multiple InputStreams.
2023-05-31 16:20:19 +02:00
Anton Khirnov a6d67b11f5 fftools/ffmpeg_filter: move sub2video subtitle queue to InputFilterPriv
This queue should be associated with a specific filtergraph input - if
a subtitle stream is sent to multiple filters then each should have its
own queue.
2023-05-31 16:20:19 +02:00
Anton Khirnov 5d530e3a72 fftools/ffmpeg_dec: move sub2video submission to ffmpeg_filter
This code is a sub2video analogue of ifilter_send_frame(), so it
properly belongs to the filtering code.

Note that using sub2video with more than one target for a given input
subtitle stream is currently broken and this commit does not change
that. It will be addressed in following commits.
2023-05-31 16:19:49 +02:00
Anton Khirnov 3d35b73b2a fftools/ffmpeg: constify AVSubtitle parameters as appropriate 2023-05-31 16:15:47 +02:00