Commit Graph

81 Commits

Author SHA1 Message Date
Andreas Rheinhardt 2135a40b1c avcodec/decode: Add new ProgressFrame API
Frame-threaded decoders with inter-frame dependencies
use the ThreadFrame API for syncing. It works as follows:

During init each thread allocates an AVFrame for every
ThreadFrame.

Thread A reads the header of its packet and allocates
a buffer for an AVFrame with ff_thread_get_ext_buffer()
(which also allocates a small structure that is shared
with other references to this frame) and sets its fields,
including side data. Then said thread calls ff_thread_finish_setup().
From that moment onward it is not allowed to change any
of the AVFrame fields at all any more, but it may change
fields which are an indirection away, like the content of
AVFrame.data or already existing side data.

After thread A has called ff_thread_finish_setup(),
another thread (the user one) calls the codec's update_thread_context
callback which in turn calls ff_thread_ref_frame() which
calls av_frame_ref() which reads every field of A's
AVFrame; hence the above restriction on modifications
of the AVFrame (as any modification of the AVFrame by A after
ff_thread_finish_setup() would be a data race). Of course,
this av_frame_ref() also incurs allocations and therefore
needs to be checked. ff_thread_ref_frame() also references
the small structure used for communicating progress.

This av_frame_ref() makes it awkward to propagate values that
only become known during decoding to later threads (in case of
frame reordering or other mechanisms of delayed output (like
show-existing-frames) it's not the decoding thread, but a later
thread that returns the AVFrame). E.g. for VP9 when exporting video
encoding parameters as side data the number of blocks only
becomes known during decoding, so one can't allocate the side data
before ff_thread_finish_setup(). It is currently being done afterwards
and this leads to a data race in the vp9-encparams test when using
frame-threading. Returning decode_error_flags is also complicated
by this.

To perform this exchange a buffer shared between the references
is needed (notice that simply giving the later threads a pointer
to the original AVFrame does not work, because said AVFrame will
be reused lateron when thread A decodes the next packet given to it).
One could extend the buffer already used for progress for this
or use a new one (requiring yet another allocation), yet both
of these approaches have the drawback of being unnatural, ugly
and requiring quite a lot of ad-hoc code. E.g. in case of the VP9
side data mentioned above one could not simply use the helper
that allocates and adds the side data to an AVFrame in one go.

The ProgressFrame API meanwhile offers a different solution to all
of this. It is based around the idea that the most natural
shared object for sharing information about an AVFrame between
decoding threads is the AVFrame itself. To actually implement this
the AVFrame needs to be reference counted. This is achieved by
putting a (ownership) pointer into a shared (and opaque) structure
that is managed by the RefStruct API and which also contains
the stuff necessary for progress reporting.
The users get a pointer to this AVFrame with the understanding
that the owner may set all the fields until it has indicated
that it has finished decoding this AVFrame; then the users are
allowed to read everything. Every decoder may of course employ
a different contract than the one outlined above.

Given that there is no underlying av_frame_ref(), creating
references to a ProgressFrame can't fail. Only
ff_thread_progress_get_buffer() can fail, but given that
it will replace calls to ff_thread_get_ext_buffer() it is
at places where errors are already expected and properly
taken care of.

The ProgressFrames are empty (i.e. the AVFrame pointer is NULL
and the AVFrames are not allocated during init at all)
while not being in use; ff_thread_progress_get_buffer() both
sets up the actual ProgressFrame and already calls
ff_thread_get_buffer(). So instead of checking for
ThreadFrame.f->data[0] or ThreadFrame.f->buf[0] being NULL
for "this reference frame is non-existing" one should check for
ProgressFrame.f.

This also implies that one can only set AVFrame properties
after having allocated the buffer. This restriction is not deep:
if it becomes onerous for any codec, ff_thread_progress_get_buffer()
can be broken up. The user would then have to get a buffer
himself.

In order to avoid unnecessary allocations, the shared structure
is pooled, so that both the structure as well as the AVFrame
itself are reused. This means that there won't be lots of
unnecessary allocations in case of non-frame-threaded decoding.
It might even turn out to have fewer than the current code
(the current code allocates AVFrames for every DPB slot, but
these are often excessively large and not completely used;
the new code allocates them on demand). Pooling relies on the
reset function of the RefStruct pool API, it would be impossible
to implement with the AVBufferPool API.

Finally, ProgressFrames have no notion of owner; they are built
on top of the ThreadProgress API which also lacks such a concept.
Instead every ThreadProgress and every ProgressFrame contains
its own mutex and condition variable, making it completely independent
of pthread_frame.c. Just like the ThreadFrame API it is simply
presumed that only the actual owner/producer of a frame reports
progress on said frame.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-04-19 13:18:04 +02:00
Anton Khirnov a3f4670943 lavc/decode: move sd_global_map to avcodec
It will be shared with encoding code.
2024-03-28 08:40:01 +01:00
James Almer 65ddc74988 avutil: remove deprecated FF_API_OLD_CHANNEL_LAYOUT
Signed-off-by: James Almer <jamrial@gmail.com>
2024-03-07 08:53:30 -03:00
James Almer 2717dcfb85 avcodec: remove deprecated FF_API_AVCTX_FRAME_NUMBER
Signed-off-by: James Almer <jamrial@gmail.com>
2024-03-07 08:53:30 -03:00
James Almer eb5b4e60c9 avcodec/avcodec: don't print coded dimensions if not set
The avctx passed to avcodec_string() may have unset coded dimensions, as is the
case when called by av_dump_format() where the streams had all the needed
information at the container level, and as such no frames were decoded
internally.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-02-13 20:32:48 -03:00
Anton Khirnov 1cc24d7495 lavc: deprecate avcodec_close()
Its use has been discouraged since 2016, but now is no longer used in
avformat, so there is no reason to keep it public.
2024-02-09 16:14:56 +01:00
Marton Balint 363b3ec98a all: use av_channel_layout_describe_bprint instead of av_channel_layout_describe in a few places
Where an AVBPrint buffer is used later anyway.

Signed-off-by: Marton Balint <cus@passwd.hu>
2024-01-07 22:47:22 +01:00
Andreas Rheinhardt 78c9ed26b6 avcodec/get_buffer: Use RefStruct API for FramePool
Avoids allocations and frees and error checks for said allocations;
also avoids a few indirections and casts.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-10-07 22:34:10 +02:00
Andreas Rheinhardt f58038d498 avcodec/avcodec: Avoid codec_desc.h, codec_par.h inclusions
Instead, use forward declarations; and in order not to affect
any user include these headers for them, but not internally.
This has the advantage of removing implicit inclusions of these
headers from almost all files providing codecs.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-10-03 01:59:07 +02:00
Andreas Rheinhardt f8503b4c33 avutil/internal: Don't auto-include emms.h
Instead include emms.h wherever it is needed.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-09-04 11:04:45 +02:00
Andreas Rheinhardt 866be3fa1e avcodec/internal: Move FF_MAX_EXTRADATA_SIZE to its only user
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2023-08-05 09:40:06 +02:00
Anton Khirnov f264204de9 lavc: deprecate AV_CODEC_FLAG_DROPCHANGED
This decoding flag makes decoders drop all frames after a parameter
change, but what exactly constitutes a parameter change is not well
defined and will typically depend on the exact use case.
This functionality then does not belong in libavcodec, but rather in
user code
2023-07-15 10:19:33 +02:00
Anton Khirnov 232700154c lavc: add generic-encode-layer private data
Move AVCodecInternal.intra_only_flag to it, should should not be visible
outside of encode.c.
2023-07-07 12:07:23 +02:00
Anton Khirnov f2fb882b3f lavc: add generic-decode-layer private data
Move AVCodecInternal.nb_draining_errors to it, should should not be
visible outside of decode.c.
2023-07-07 12:07:23 +02:00
Anton Khirnov 6ff27024b8 lavc/avcodec: split flushing into decode- and encode-specific functions
Will allow making some state private to encoding/decoding in the future.
2023-07-07 12:07:23 +02:00
Anton Khirnov 5e7b5b0090 lavc: add a header for internal generic-layer APIs
The goal is to distinguish between APIs provided by the generic layer to
individual codecs and APIs internal to the generic layer.

Start by moving ff_{decode,encode}_receive_frame() and
ff_{decode,encode}_preinit() into this new header, as those functions
are called from generic code and should not be visible to individual
codecs.
2023-07-07 12:07:23 +02:00
Anton Khirnov 3d2e1aa324
lavc/decode: stop duplicating code from hwaccel_uninit() 2023-05-29 00:41:27 +02:00
Zhao Zhili 36a56d3cc8 avcodec/avcodec: fix UB NULL+0
Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2023-05-04 12:10:11 +08: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 5f9e848e68 avcodec: remove FF_API_AVCTX_TIMEBASE
Signed-off-by: James Almer <jamrial@gmail.com>
2023-02-09 15:35:08 +01:00
James Almer b1fdb0b347 Revert "avcodec/decode: use a packet list to store packet properties"
The idea behind last_pkt_props was to store the properties of the last packet
fed to the decoder. Any sort of queueing required by CODEC_CAP_DELAY decoders
that consume several packets before they start outputting frames should be done
by the decoders in question. An example of this is libdav1d.

This is required for the following commits that will fix last_pkt_props in
frame threading scenarios, as well as maintain its contents during flush.

This revers commit 022a12b306.

Signed-off-by: James Almer <jamrial@gmail.com>
2022-12-07 08:55:33 -03:00
Andreas Rheinhardt 90edbd3185 avcodec/avcodec: Always use old channel count/layout if set
This ensures that if AVCodecContext.channels or
AVCodecContext.channel_layout are set, AVCodecContext.ch_layout
has the equivalent values after this block.

(In case these values are set inconsistently, the consistency check
for ch_layout below will error out.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-22 01:05:59 +02:00
Andreas Rheinhardt a06a2d8943 avcodec/avcodec: Check for more invalid channel layouts
In particular, check the provided channel layout for encoders
without AVCodec.ch_layouts set. This fixes an infinite loop
in the WavPack encoder (and maybe other issues in other encoders
as well) in case the channel count is zero.

Reviewed-by: James Almer <jamrial@gmail.com>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-22 01:03:20 +02:00
Andreas Rheinhardt e2e3181519 avcodec/avcodec: Uninitialize AVChannelLayout before overwriting it
Otherwise, there might be leaks.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-09-22 00:30:32 +02:00
James Almer 33cdf51a06 avcodec/avcodec: Use the new API fields to validate the layout returned by decoders
This block was scheduled for removal, which means that no validation would have
taken place after the old API was removed.
It was algo going to mistakenly remove an unrelated bits_per_coded_sample
check.

Signed-off-by: James Almer <jamrial@gmail.com>
2022-09-19 11:59:15 -03:00
Andreas Rheinhardt 70f3035482 avcodec/avcodec: Remove redundant check
At this point active_thread_type is set iff active_thread_type
is set to FF_THREAD_FRAME iff AVCodecInternal.frame_thread_encoder
is set.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-08-15 18:10:31 +02:00
Andreas Rheinhardt 3040876833 avcodec/avcodec: Move initializing frame-thrd encoder to encode_preinit
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-08-15 18:10:31 +02:00
Anton Khirnov e3838b856f lavc: add API for exporting reconstructed frames from encoders 2022-08-02 10:46:11 +02:00
Niklas Haas c688ddc067 avcodec: add common fflcms2 boilerplate
Handling this in general code makes more sense than handling it in
individual codec files, because it would be a lot of unnecessary code
duplication for the plenty of formats that support exporting ICC
profiles (jpg, png, tiff, webp, jxl, ...).

encode.c and decode.c will be in charge of initializing this state as
needed, so we merely need to make sure to uninit it afterwards from the
common destructor path.

Signed-off-by: Niklas Haas <git@haasn.dev>
2022-07-30 11:42:06 +02:00
James Almer 5114ce1e2a avcodec/aacdec: remove skip samples multiplier
The amount of padding samples reported by containers take into account the
extended samplerate in HE-AAC.

Fixes ticket #9671.

Signed-off-by: James Almer <jamrial@gmail.com>
2022-07-22 09:19:11 -03:00
Andreas Rheinhardt 21b23ceab3 avcodec: Make init-threadsafety the default
and remove FF_CODEC_CAP_INIT_THREADSAFE
All our native codecs are already init-threadsafe
(only wrappers for external libraries and hwaccels
are typically not marked as init-threadsafe yet),
so it is only natural for this to also be the default state.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-07-18 20:04:59 +02:00
Andreas Rheinhardt c19797bf59 avcodec/internal: Hide stuff only used by the core decode API
The general decoding API uses bitstream filters and an AVFifo
and therefore AVCodecInternal contains pointers to an AVBSFContext
and to an AVFifo and lavc/internal.h includes lavc/bsf.h and
lavu/fifo.h.
Yet actually, only two files are supposed to use these, namely
avcodec.c and (mainly) decode.c. For all the other files,
it should be an opaque type that they should not touch and that
they need not know anything about. This can be achieved by not
including these headers and using the structs instead of the
corresponding typedefs.
This also forces translation units that really use the BSF
and the FIFO APIs themselves to include the relevant headers
directly instead of relying on indirect inclusions (up until now,
even avcodec.c and decode.c relied on fifo.h to be included
by internal.h).
Of course, it also avoids unnecessary rebuilds when bsf.h or fifo.h
change.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-05-12 08:52:59 +02:00
Anton Khirnov 8016219472 lavc: drop a confusing message about "thread emulation"
There is no such thing.
2022-05-11 10:37:26 +02:00
Anton Khirnov 2cb86cd00c lavc/avcodec: only allocate decoding packets for decoders 2022-05-11 10:37:26 +02:00
Andreas Rheinhardt f2b79c5b85 lib*/version: Move library version functions into files of their own
This avoids having to rebuild big files every time FFMPEG_VERSION
changes (which it does with every commit).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-05-10 06:49:32 +02:00
Andreas Rheinhardt fa3f9f2f6a avcodec/avcodec: Don't reset decoder-fields for encoders when flushing
Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-04-13 18:04:25 +02:00
Andreas Rheinhardt f6e167998f avcodec/avcodec: Simplify accessing AVSubtitleRect via dedicated pointer
Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-04-13 18:03:40 +02:00
Andreas Rheinhardt 3649051dc4 avcodec/avcodec: Avoid av_frame_unref(NULL)
It works, but it is not documented to work.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-04-13 18:03:25 +02:00
Anton Khirnov 32413600e8 lavc/encode: drop EncodeSimpleContext
It has only a single member.
2022-04-13 12:14:30 +02:00
Anton Khirnov 1c01dca144 lavc/avcodec: only allocate the encoding frame for encoders
And only when needed, i.e. for encoders using the simple API.
2022-04-13 12:14:08 +02:00
Anton Khirnov 7efa6418b8 lavc/avcodec: simplify codec id/type validity checking
On entry to avcodec_open2(), the AVCodecContext type and id either have
to be UNKNOWN/NONE or have to match the codec to be used.
2022-04-13 12:13:15 +02:00
Andreas Rheinhardt 20f9727018 avcodec/codec_internal: Add FFCodec, hide internal part of AVCodec
Up until now, codec.h contains both public and private parts
of AVCodec. This exposes the internals of AVCodec to users
and leads them into the temptation of actually using them
and forces us to forward-declare structures and types that
users can't use at all.

This commit changes this by adding a new structure FFCodec to
codec_internal.h that extends AVCodec, i.e. contains the public
AVCodec as first member; the private fields of AVCodec are moved
to this structure, leaving codec.h clean.

Reviewed-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-03-21 01:33:09 +01:00
Andreas Rheinhardt a688f3c13c avcodec/internal: Move FF_CODEC_CAP_* to a new header codec_internal.h
Also move FF_CODEC_TAGS_END as well as struct AVCodecDefault.
This reduces the amount of files that have to include internal.h
(which comes with quite a lot of indirect inclusions), as e.g.
most encoders don't need it. It is furthemore in preparation
for moving the private part of AVCodec out of the public codec.h.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-03-21 01:33:09 +01:00
Martin Storsjö f2da2e1458 libavcodec: Split version.h
This avoids including version.h in all source files, avoiding
unnecessary rebuilds when the version number is bumped. Only
version_major.h is included by the main header, which defines
availability of e.g. FF_API_* macros, and which is bumped much
less often.

This isn't done for libavutil/version.h, because that header needs
to be included essentially everywhere due to LIBAVUTIL_VERSION_INT
being used wherever an AVClass is constructed.

Signed-off-by: Martin Storsjö <martin@martin.st>
2022-03-16 14:04:35 +02:00
Anton Khirnov 5636972c7a lavc: drop temporary compat wrappers for channel layout API change
Signed-off-by: James Almer <jamrial@gmail.com>
2022-03-15 09:42:46 -03:00
Vittorio Giovara 548aeb9383 lavc: switch to the new channel layout API
Since the request_channel_layout is used only by a handful of codecs,
move the option to codec private contexts.

Signed-off-by: Anton Khirnov <anton@khirnov.net>
Signed-off-by: James Almer <jamrial@gmail.com>
2022-03-15 09:42:39 -03:00
Andreas Rheinhardt 23b17f96f7 avcodec/avcodec: Decrease the amount of time while holding the lock
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2022-02-18 20:28:57 +01:00
Anton Khirnov 37e70d4802 lavc/avcodec: switch to new FIFO API 2022-02-07 00:31:23 +01:00
Andreas Rheinhardt ffdda740fe avcodec/internal: Allow receive_frame codecs to use decode_simple pkt
Decoders implementing the receive_frame API currently mostly use
stack packets to temporarily hold the packet they receive from
ff_decode_get_packet(). This role directly parallels the role of
in_pkt, the spare packet used in decode_simple_internal for the
decoders implementing the traditional decoding API. Said packet
is unused by the generic code for the decoders implementing the
receive_frame API, so allow them to use it to fulfill the function
it already fulfills for the traditional API for both APIs.

There is only one caveat in this: The packet is automatically
unreferenced in avcodec_flush_buffers(). But this is actually
positive as it means the decoders don't have to do this themselves
(in case the packet is preserved between receive_frame calls).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-11-07 17:12:58 +01:00
Andreas Rheinhardt b9fd9bce73 avcodec/avcodec: Simplify check for flushing of bsf
Just check for the existence of the bsf. This is equivalent to
the old criterion of the AVCodecContext being a decoder.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2021-09-26 13:29:29 +02:00