Commit Graph

257 Commits

Author SHA1 Message Date
Anton Khirnov 044d70736e fftools/ffmpeg: move some code from init_output_stream() to init_output_stream_encode()
The code is subtitle-encoding-specific, so this is a more appropriate
place for it.
2022-10-04 11:55:03 +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 3c0f532cbc fftools/ffmpeg: pass the timestamp to check_recording_time()
Stop setting OutputStream.sync_opts for subtitle encoding, as it is now
unused.
2022-10-04 11:55:03 +02:00
Anton Khirnov 69f770f77c fftools/ffmpeg: stop setting OutputStream.sync_opts for streamcopy
It is not used for anything.
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 5188aeaffb fftools/ffmpeg: cosmetics
Reindent after previous commit, apply some style fixes.
2022-10-04 11:55:03 +02:00
Anton Khirnov c75be06148 fftools/ffmpeg: move forced keyframe processing into its own function 2022-10-04 11:55:03 +02:00
Anton Khirnov 7a994c8701 fftools/ffmpeg: drop always-true conditions
in_picture->pts cannot be AV_NOPTS_VALUE, as it is set to ost->sync_opts
a few lines above. ost->sync_opts is never AV_NOPTS_VALUE.
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
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