Commit Graph

248 Commits

Author SHA1 Message Date
Zhao Zhili af919cf780 fftools/ffmpeg: fix av_display_rotation_set() type cast
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2022-09-22 20:03:57 +08:00
Andreas Rheinhardt 601faaed92 fftools: Use report_error_then_exit_program() for allocation failures
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-01 14:26:00 +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 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 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 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 6ded80af92 fftools/ffmpeg: move packet timestamp processing to demuxer thread
Discontinuity detection/correction is left in the main thread, as it is
entangled with InputStream.next_dts and related variables, which may be
set by decoding code.

Fixes races e.g. in fate-ffmpeg-streamloop after
aae9de0cb2.
2022-08-13 12:41:05 +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 ca38fe927e fftools/ffmpeg: simplify conditions in ts_discontinuity_process 2022-08-13 12:41:05 +02:00
Anton Khirnov aa6d4a53e3 fftools/ffmpeg: move inter-stream ts discontinuity handling to ts_discontinuity_process() 2022-08-13 12:41:05 +02:00
Anton Khirnov e2d784a5b7 fftools/ffmpeg: move timestamp discontinuity correction out of process_input() 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 86e9cef77b fftools/ffmpeg: move stream-dependent starttime correction to transcode_init()
Currently this code is located in the discontinuity handling block,
where it does not belong.
2022-08-13 12:41:05 +02:00
Anton Khirnov 5d499d3250 fftools/ffmpeg: mark all encode sync queues as done before flushing encoders 2022-08-13 12:41:05 +02:00
Anton Khirnov a5e025e826 fftools/ffmpeg: move handling corrupt packets to the input thread 2022-08-08 16:20:58 +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 07da07ddb0 fftools/ffmpeg: report new streams from the input thread
This avoids a potential race with the demuxer adding new streams. It is
also more efficient, since we no longer do inter-thread transfers of
packets that will be just discarded.
2022-08-08 16:20:58 +02:00
Anton Khirnov 9c16310fe5 fftools/ffmpeg: handle dumping input packets in input_thread()
This is a more appropriate place for this.
2022-08-08 16:20:58 +02:00
Anton Khirnov b1f24afe7e fftools/ffmpeg: drop the 'h' key handling
This undocumented feature runtime-enables dumping input packets. I can
think of no reasonable real-world use case that cannot also be
accomplished in a different way. Keeping this functionality would
interfere with the following commit moving it to the input thread (then
setting the variable would require locking or atomics, which would be
unnecessarily complicated for a feature that probably nobody uses).
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 d8bf53710f fftools/ffmpeg: always read input in a thread
This will be required by the following architecture changes.
2022-08-08 16:20:58 +02:00
Anton Khirnov 9139ea4c8d fftools/ffmpeg: drop a superfluous stack variable 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 a1da77bd71 fftools/ffmpeg: stop accessing the decoder context unnecessarily
The same information is available from AVStream.codecpar. This will
allow to stop allocating a decoder unless decoding is actually
performed.
2022-08-08 16:20:58 +02:00
Anton Khirnov ecfdab20e4 fftools/ffmpeg: do not log to the decoder context
That should only be done from inside the decoder. Log to NULL instead,
as is the current convention in ffmpeg.
2022-07-28 16:37:16 +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 8384d8403f fftools/ffmpeg: stop allocating an encoder context when not encoding 2022-07-28 16:37:16 +02:00
Anton Khirnov 1eae0ee02e fftools/ffmpeg: do not use the encoder context for streamcopy
The streamcopy initialization code briefly needs an AVCodecContext to
apply AVOptions to. Allocate a temporary codec context, do not use the
encoding one.
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 8cbf229c94 fftools/ffmpeg: drop the -vol option
It has been deprecated in favor of the volume filter since 2012.
2022-07-28 16:37:16 +02:00
Anton Khirnov 1cd60d3b35 fftools/ffmpeg: remove an unnecessary avcodec_close() call
No encoders can possibly be opened at this point. And even if some were,
they would be closed in ffmpeg_cleanup().
2022-07-28 16:37:16 +02:00
Anton Khirnov c2f2dd0296 fftools/ffmpeg: stop accessing the encoder context unnecessarily
The same information is available from AVStream.codecpar. This will
allow to stop allocating an encoder unless encoding is actually
performed.
2022-07-28 16:37:16 +02:00
Anton Khirnov 34cbda3ee1 fftools/ffmpeg: replace AVFrame.pkt_duration with duration
Mistakenly reintroduced in 4740fea7dd.
2022-07-24 08:46:16 +02:00
Andreas Rheinhardt b0ae5d02f3 fftools/ffmpeg: Zero subtitle packet padding
Fixes uninitialized reads in the sub-lrc-remux test.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-07-23 19:01:44 +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 8e854cdd2e fftools/ffmpeg: stop using av_stream_get_end_pts()
It retrieves the muxer's internal timestamp with under-defined
semantics. Continuing to use this value would also require
synchronization once the muxer is moved to a separate thread.

Replace the value with last_mux_dts.
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 4cd19eaffa fftools/ffmpeg: stop using OutputStream.frame_number in print_report()
This field means different things when the video is encoded (number of
frames emitted to the encoding sync queue/encoder by the video sync
code) or copied (number of packets sent to the muxer sync queue).

Print the value of packets_written instead, which means the same thing
in both cases. It is also more accurate, since packets may be dropped by
the sync queue or bitstream filters.
2022-07-23 11:53:19 +02:00
Anton Khirnov 587081a179 fftools/ffmpeg: use the sync queues to handle -frames
Same issues apply to it as to -shortest.

Changes the results of the following tests:
- matroska-flac-extradata-update
  The test reencodes two input FLAC streams into three output FLAC
  streams. The last output stream is limited to 8 frames. The current
  code results in the first two output streams having 12 frames, after
  this commit all three streams have 8 frames and are the same length.
  This new result is better, since it is predictable.
- mkv-1242
  The test streamcopies one video and one audio stream, video is limited
  to 11 frames. The new result shortens the audio stream so that it is
  not longer than the video.
2022-07-23 11:53:19 +02:00