Commit Graph

339 Commits

Author SHA1 Message Date
Anton Khirnov 2f24290c8e fftools/ffmpeg: disable and deprecate -qphist
This option adds a long string of numbers to the progress line, where
i-th number contains the base-2 logarithm of the number of times a frame
with this QP value was seen by print_report().

There are multiple problems with this feature:
* despite this existing since 2005, web search shows no indication
  that it was ever useful for any meaningful purpose;
* the format of what is printed is entirely undocumented, one has to
  find it out from the source code;
* QP values above 31 are silently ignored;
* it only works with one video stream;
* as it relies on global state, it is in conflict with ongoing
  architectural changes.

It then seems that the nontrivial cost of maintaining this option is not
worth its negligible (or possibly negative - since it pollutes the
already large option space) value.
Users who really need similar functionality can also implement it
themselves using -vstats.
2023-04-13 15:11:56 +02:00
Anton Khirnov 5d97ba5d9c fftools/ffmpeg: move printing verbose demuxing stats to ffmpeg_demux
This is a more appropriate place for this.
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 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 d99846d2f2 fftools/ffmpeg: move the hw_device_free_all() call to ffmpeg_cleanup()
Frees devices on failure as well as success.
2023-04-13 15:11:56 +02:00
Anton Khirnov f2c8dff906 fftools/ffmpeg: drop a useless goto
There is no cleanup in transcode(), can return an error directly.
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 2225134e7d fftools/ffmpeg: clean up #includes
Drop unneeded ctype.h and math.h.
Group all system headers together.
Sort unconditional includes alphabetically.
Group local includes by the library, sort alphabetically.
2023-04-09 15:47:45 +02:00
Anton Khirnov a2a09932f2 fftools/ffmpeg: stop including os_support.h
It was added for usleep(), which
- is not used in ffmpeg
- is not handled in os_support.h anymore
2023-04-09 15:47:45 +02:00
Anton Khirnov 44accfef41 fftools/ffmpeg: move audio/video encoding code to ffmpeg_enc.c 2023-04-09 15:47:45 +02:00
Anton Khirnov 9de5dc74fd fftools/ffmpeg: move subtitle encoding to ffmpeg_enc.c 2023-04-09 15:47:45 +02:00
Anton Khirnov b79448fa38 fftools/ffmpeg: replace ff_dlog() with av_log()
ff_dlog() is a lavu internal macro, and av_log() at trace level is just
as good here.
2023-04-09 15:47:45 +02:00
Anton Khirnov a996478e8c fftools/ffmpeg: simplify output stream initialization call graph
Several places in the code currently call init_output_stream_wrapper(),
which in turn calls init_output_stream(), which then calls either
enc_open() or init_output_stream_streamcopy(), followed by
of_stream_init(), which tells the muxer the stream is ready for muxing.

All except one of these callers are in the encoding code, which will be
moved to ffmpeg_enc.c. Keeping this structure would then necessitate
ffmpeg_enc.c calling back into the common code in ffmpeg.c, which would
then just call ffmpeg_mux, thus making the already convoluted call chain
even more so.

Simplify the situation by using separate paths for filter-fed output
streams (audio and video encoders) and others (subtitles, streamcopy,
data, attachments).
2023-04-09 15:47:45 +02:00
Anton Khirnov d96f2fbf76 fftools/ffmpeg: move initializing encoders to a new file
This file will contain more encoding-related code in the future.
2023-04-09 15:47:45 +02:00
Anton Khirnov 1dabd48519 fftools/ffmpeg: reindent after previous commit 2023-04-09 15:47:45 +02:00
Anton Khirnov 39291f19e2 fftools/ffmpeg: move encoder initialization to init_output_stream_encode
Encoder initialization is currently split rather arbitrarily between
init_output_stream_encode() and init_output_stream(). Move all of it to
init_output_stream_encode().
2023-04-09 15:47:45 +02:00
Anton Khirnov e1fe6c5577 fftools/ffmpeg: use stack variables to shorten code 2023-04-09 15:47:45 +02:00
Anton Khirnov e1e0c2c7f2 fftools/ffmpeg: drop unnecessary indirection
init_output_stream() can print log messages directly, there is no need
to ship them to the caller.
2023-04-09 15:47:45 +02:00
Anton Khirnov 237f9b9682 fftools/ffmpeg: stop handling AVMEDIA_TYPE_DATA in init_output_stream_encode()
We do not support data encoders, so this should never be reached.
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 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 87e9f5ad3c fftools/ffmpeg: move initializing next_[pd]ts to add_input_streams()
They are initialized to constants, so it makes most sense to do it as
soon as possible.
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
Anton Khirnov e1d12aaa45 fftools/ffmpeg: do not return finished streams from choose_output() 2023-04-09 15:47:45 +02:00
Anton Khirnov 416e2661ea fftools/ffmpeg: make sure non-lavfi streams are closed on input EOF 2023-04-09 15:47:45 +02:00
rcombs b68a6ba469 ffmpeg: send only one rect per packet when encoding ASS
The packet and rect formats are identical,
so there's no support for multiple rects per packet.
2023-03-21 14:24:31 -04:00
Marton Balint 6b6f7db819 avcodec: add AVCodecContext.frame_num as 64 bit variant to frame_number
Frame counters can overflow relatively easily (INT_MAX number of frames is
slightly more than 1 year for 60 fps content), so make sure we use 64 bit
values for them.

Also deprecate the old 32 bit frame_number attribute.

Signed-off-by: Marton Balint <cus@passwd.hu>
2023-02-13 00:36:46 +01:00
James Almer e0786a8eeb avcodec: remove FF_API_THREAD_SAFE_CALLBACKS
Signed-off-by: James Almer <jamrial@gmail.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2023-02-09 15:24:16 +01:00
Andreas Rheinhardt 868a31b42d avcodec: Make avcodec_decode_subtitle2 accept a const AVPacket*
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: Anton Khirnov <anton@khirnov.net>
2023-02-09 15:24:15 +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
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
Jan Ekström 746d27455b ffmpeg: move decoded frame counter from after post-processing to decode
This way we can call process_subtitles without causing the decoded
frame counter to get bumped.

Additionally, this now takes into mention all of the decoded
subtitle frames without fix_sub_duration latency/buffering, or filtering
out decoded reset/end subtitles without any rendered rectangles, which
matches the original intent in 4754345027
.

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
2023-02-03 12:21:02 +02:00
Jan Ekström fcc50674de ffmpeg: refactor post-decoding steps for subtitles into a function
This enables us to later call this when generating additional
subtitles for splitting purposes.

Co-authored-by: Andrzej Nadachowski <andrzej.nadachowski@24i.com>

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
2023-02-03 12:21:02 +02:00
Anton Khirnov 806ecace91 fftools/ffmpeg: support input frame params in encoding stats 2023-01-31 09:09:23 +01:00
Anton Khirnov 61afbc2376 fftools/ffmpeg: use correct IO context for -enc_stats_post 2023-01-31 09:09:11 +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 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 7655249f72 fftools/ffmpeg: reindent after previous commit 2023-01-10 11:49:28 +01:00
Anton Khirnov a848a5a223 fftools/ffmpeg: move video frame dup/drop logic into its own function 2023-01-10 11:49:28 +01:00
Anton Khirnov 5874ca3650 fftools/ffmpeg: rename a variable to be more descriptive 2023-01-10 11:49:28 +01:00
Anton Khirnov 3b21f10639 fftools/ffmpeg: fix stream id in an error message.
Broken in 7ef7a22251
2023-01-10 11:49:28 +01:00
Anton Khirnov c60941dfaf fftools/ffmpeg: stop using AVCodecContext.sample_rate in decode_audio()
Use the decoded frame's sample_rate instead, which is the authoritative
value.

Drop a now-obsolete check validating AVCodecContext.sample_rate.
2023-01-10 11:49:28 +01:00
Marvin Scholz f1907faab4 fftools: use av_dict_iterate
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-12-01 11:21:14 +01:00
Anton Khirnov becbb22eb0 fftools/ffmpeg: cosmetics
Reindent after previous commit and break/split some lines as
appropriate.
2022-11-28 10:28:14 +01:00
Anton Khirnov d04ec7efe3 fftools/ffmpeg: remove a useless inner block
adjust_frame_pts_to_encoder_tb() is so small that this serves no useful
purpose.
2022-11-28 10:28:14 +01:00
Anton Khirnov d60d6d819d fftools/ffmpeg: drop an always-false check 2022-11-28 10:28:14 +01:00
Anton Khirnov 8ee4365ad8 fftools/ffmpeg: only convert video frame pts if we have a frame
Calling adjust_frame_pts_to_encoder_tb() with a NULL frame does not
perform a meaningful action.
2022-11-28 10:28:14 +01:00