Commit Graph

95 Commits

Author SHA1 Message Date
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 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 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 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 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 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 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 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 a1002bc39c fftools/ffmpeg: replace print_error() by more meaningful messages 2023-05-15 13:32:01 +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 f4a60b8ddc fftools/ffmpeg: reduce access to OutputStream.enc_ctx
It will be made private to Encoder in the future.
2023-05-02 10:59:24 +02:00
Anton Khirnov ab0d2052a3 fftools/ffmpeg: remove unused function arguments 2023-04-24 16:56:06 +02:00
Zhao Zhili b56aca9b42 fftools/ffmpeg_mux: fix reporting muxer EOF as error
Fix #10327.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2023-04-23 21:23:11 +08:00
Anton Khirnov c17e33c058 fftools/ffmpeg: propagate frame durations to packets when encoding
Remove now-obsolete code setting packet durations pre-muxing for CFR
encoded video.

Changes output in the following FATE tests:
* numerous adpcm tests
* ffmpeg-filter_complex_audio
* lavf-asf
* lavf-mkv
* lavf-mkv_attachment
* matroska-encoding-delay
  All of these change due to the fact that the output duration is now
  the actual input data duration and does not include padding added by
  the encoder.

* apng-osample: less wrong packet durations are now passed to the muxer.
  They are not entirely correct, because the first frame duration should
  be 3 rather than 2. This is caused by the vsync code and should be
  addressed later, but this change is a step in the right direction.
* tscc2-mov: last output frame has a duration of 11 rather than 1 - this
  corresponds to the duration actually returned by the demuxer.
* film-cvid: video frame durations are now 2 rather than 1 - this
  corresponds to durations actually returned by the demuxer and matches
  the timestamps.
* mpeg2-ticket6677: durations of some video frames are now 2 rather than
  1 - this matches the timestamps.
2023-04-19 21:12:03 +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 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 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 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 090950f832 fftools/sync_queue: use timebase from input frames/packets
They are always properly set now. Avoid a separate timebase-setting
call, which duplicates the knowledge of the timebase being used.
2023-04-09 15:47:45 +02:00
Anton Khirnov b40856c905 fftools/ffmpeg_mux: distinguish between sync queue and muxer EOF
Individual streams should be terminated in the former case, the whole
muxing process in the latter.

Reported-by: Gyan Doshi
2023-02-10 11:01:11 +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
Anton Khirnov 6d4f3ae116 fftools/ffmpeg: store output packet timebases in the packet
Useful to keep track of what timebase the packet's timestamps are in.
2023-02-09 15:24:15 +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 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 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 874a6f2090 fftools/ffmpeg: set thread names 2022-10-24 02:00:31 +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 709b47f8a4 fftools/ffmpeg: free output streams in of_close()
Output streams are now children of OutputFile, so it makes more sense to
free them there.
2022-10-18 14:19:11 +02:00