1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2025-11-23 21:54:53 +02:00
Commit Graph

267 Commits

Author SHA1 Message Date
James Almer
f5eb11a71d avcodec/decode: add missing syncing of side_data_pref_mask to worker threads
Signed-off-by: James Almer <jamrial@gmail.com>
2025-11-02 18:14:17 -03:00
Andreas Rheinhardt
8e90f150eb avcodec/decode: Optimize lcevc away if disabled
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-11-02 16:50:36 +01:00
Andreas Rheinhardt
63685709b9 avcodec/decode: Mark init,close functions as av_cold
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-11-02 16:49:26 +01:00
Andreas Rheinhardt
2786e5a9ad avcodec/decode: Put lcevc fields into structure of their own
Makes it easier to see that width and height in DecodeContext is
actually a lcevc field.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-11-02 16:00:06 +01:00
Andreas Rheinhardt
182b9c7a4a avcodec/decode: Don't allocate LCEVC context for non-video
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-11-02 15:29:59 +01:00
James Almer
26f4230082 avcodec/decode: sync initial_pict_type and intra_only_flag with thread worker's avctx
Regression since 5acbdd2264, which removed
setting both values from PerThreadContext.
Given the pthread code calls ff_decode_receive_frame_internal() on the frame,
any value set before it will be overwritten, so instead sync each thread's
DecodeContext and let ff_decode_receive_frame_internal() handle these values.

Fixes issue #20534.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-16 15:41:50 -03:00
Leo Izen
74e61981b5 avcodec/decode: treat orientation 1 as valid displaymatrix
Since 9dc79241d9 we now always pop the
orientation off of the IFD and use a display matrix instead. This means
we should not produce a warning and refuse if the orientation field
indicates a default orientation (i.e. 1).

Signed-off-by: Leo Izen <leo.izen@gmail.com>
Reported-by: Ramiro Polla <ramiro.polla@gmail.com>
2025-09-06 13:15:24 -04:00
Niklas Haas
ecebf9c693 avcodec/avcodec: add AVCodecContext.alpha_mode
Following in the footsteps of the previous commit, this commit adds the
new fields to AVCodecContext so we can start properly setting it on codecs,
as well as limiting the list of supported options to detect a format mismatch
during encode.

This commit also sets up the necessary infrastructure to start using the
newly added field in all codecs.
2025-09-02 17:06:24 +02:00
James Almer
9dc79241d9 avcodec/decode: always extract display matrix from Exif in frames
Should help ensure no conflicting values are propagated.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-01 12:08:04 -03:00
James Almer
6a15183e6a avcodec/decode: parse Exif packet side data before passing it to frames
Extract Orientation and export it as a display matrix if present, and set the
frame's metadata with the remaining Exif entries.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-01 12:07:59 -03:00
James Almer
fa1938c574 avcodec/decode: use ff_frame_new_side_data() to export Exif side data
Otherwise, the user requested priority of packet side data will be ignored.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-01 12:07:41 -03:00
James Almer
75bd5056a4 avcodec/decode: use av_exif_get_tag_id() where useful
Removes dependency on exif_internal.h

Signed-off-by: James Almer <jamrial@gmail.com>
2025-09-01 12:07:41 -03:00
James Almer
5ed1d72792 avcodec/decode: remove Exif Orientation tag after adding it as display matrix
Don't replace it with a conflicting value.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-08-21 22:48:16 +00:00
James Almer
5a9e5d8031 avcodec/exif: use ff_frame_new_side_data() to export display matrix
Otherwise, the user requested priority of packet side data will be ignored.
For this, move the relevant functions to decode.c, as they need access to an
AVCodecContext.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-08-21 22:48:16 +00:00
Andreas Rheinhardt
9102a90a95 Revert "avcodec/decode: Fix avcodec parameters when bsfs are enable by decoder"
This reverts commit 1c17061397.

The commit intended to provide certain codecs using *_mp4toannexb
bitstream filters with updated (annex B) extradata (even when
the user-supplied one was ISOBMFF), yet BSFs are allowed to change
way more. The media100_to_mjpegb BSF used by the media100 decoder
changes the codec id; the commit being reverted therefore changed
AVCodecContext.codec_id which is an API violation and broke
media100 decoding with the FFmpeg cli tool.

This commit also made changes from the internal BSF externally
visible. extradata is documented to be "owned by the codec and
freed in avcodec_free_context()" which does not include replacing
it with something else in avcodec_open2() and may surprise users
who think that AVCodecContext.extradata is immutable before
avcodec_free_context(). It also incurred a memdup which is completely
unnecessary for most decoders.

Therefore this commit is reverted. The problem it tried to solve
will be solved differently in the next commit.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-17 00:35:13 +02:00
James Almer
8eae65dc5c avcodec/lcevcdec: don't try to write to output frames directly
The buffer references may not be writable at this point, as the decoder
calls get_buffer2() with the AV_GET_BUFFER_FLAG_REF flag.

Fixes races as reported by tsan, producing correct output regardless of
threading choices.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-05-22 19:28:35 -03:00
Andreas Rheinhardt
3b2a9410ef avcodec/decode: Only use ff_progress_frame_get_buffer() with blank input
All users (namely HEVC) that use ff_progress_frame_alloc()
should just use ff_thread_get_buffer(). Using
ff_progress_frame_get_buffer() is not a must; it is merely
a convenience wrapper.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-04-14 10:15:08 +02:00
Zhao Zhili
1c17061397 avcodec/decode: Fix avcodec parameters when bsfs are enable by decoder
BSF can update extradata, e.g., vvc_mp4toannexb. If we don't copy
bsf->par_out back to avcodec context, decoder can get extradata in
mp4 format, while packets are in annexb format.
2025-04-05 00:28:20 +08:00
Andreas Rheinhardt
b306683d12 avutil/frame: Port AVFrame.private_ref to RefStruct API
This is possible without deprecation period, because said field
is documented as only for our libav* libraries and not the general
public.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Signed-off-by: James Almer <jamrial@gmail.com>
2025-03-28 14:33:08 -03:00
James Almer
1061689ad8 avutil: remove deprecated FF_API_FRAME_KEY
Deprecated since 2023-05-04.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-03-28 14:33:08 -03:00
James Almer
8af3331883 avutil: remove deprecated FF_API_INTERLACED_FRAME
Deprecated since 2023-05-04.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-03-28 14:33:08 -03:00
James Almer
ebd6d07ab9 avutil: remove deprecated FF_API_FRAME_PKT
Deprecated since 2023-03-20.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-03-28 14:33:08 -03:00
James Almer
5828624246 avcodec: remove deprecated FF_API_DROPCHANGED
Deprecated since 2023-07-15.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-03-28 14:33:07 -03:00
Andreas Rheinhardt
c8be309719 avcodec/codec_internal: Add inlined version of av_codec_is_(de|en)coder
These functions check whether the AVCodec* is NULL, but this
has already been checked at a lot of places in our codebase,
so that it boils down to checking the is_decoder flag.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-13 02:16:27 +01:00
Andreas Rheinhardt
3e19e5062c avcodec/decode: Move is_open check to avcodec_receive_frame()
It also applies to scenarios where ff_encode_receive_frame()
is used. Also remove the redundant av_codec_is_decoder().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-13 02:16:27 +01:00
James Almer
603334e86f avcodec/decode: inject missing global side data to output frames
ff_decode_frame_props() injects global side data passed by the caller (Usually
coming from the container) but ignores the global side data the decoder
gathered from the bitstream itself.
This commit amends this.

Signed-off-by: James Almer <jamrial@gmail.com>
2025-03-03 18:10:10 -03:00
Anton Khirnov
56ba57b672 lavc/refstruct: move to lavu and make public
It is highly versatile and generally useful.
2024-12-15 14:03:47 +01:00
Eugene Zemtsov
e9c3698ed2 avcodec/decode: Fix incorrect enum type used in side_data_map()
It's AVPacketSideDataType, not AVFrameSideDataType.

Reviewed-by: Ted (Chromium) Meyer <tmathmeyer@chromium.org>
Reviewed-by: Marth64 <marth64@proxyid.net>
Signed-off-by: Marth64 <marth64@proxyid.net>
2024-11-18 15:55:57 -06:00
Thomas Guillem
44a0a0c050 avcodec/decode: clean-up if get_hw_frames_parameters fails
Fixes the following assert:

[00007f1df83d17e0] vaapi generic error: avcodec_get_hw_frames_parameters failed: -22
Assertion p_dst->hwaccel_threadsafe || (!dst->hwaccel && !dst->internal->hwaccel_priv_data) failed at libavcodec/pthread_frame.c:349

Reproduced from VLC with VAAPI, when fallbacking from hw to sw.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2024-10-30 21:31:17 +08:00
Anton Khirnov
0fde9c609f lavc/decode: merge stereo3d information from decoder with packet side data
The HEVC decoder will start setting stereoscopic view position (left or
right) based on 3D Reference Displays Info SEI message in future
commits. This information should be merged with container-derived
stereo3D side data.
2024-09-23 17:12:17 +02:00
Anton Khirnov
327080c088 lavc/decode: make sure side data mapping does not produce duplicates
Also, deduplicate the code performing the mapping.
2024-09-23 17:12:15 +02:00
Anton Khirnov
bcbe999077 lavc/decode: clear side data in reget_buffer()
Otherwise it may accumulate when e.g. global side data is repeatedly
copied to the frame with in each subsequent reget_buffer() call.
2024-09-23 17:11:40 +02:00
Anton Khirnov
e19551d165 lavc/decode: do not clear the frame discard flag in ff_decode_frame_props_from_pkt()
Only do it in reget_buffer().

The purpose of this clearing this flag is to prevent it for
unintentionally persisting across multiple invocations of this function
on one frame, however that is only a problem if the frame is not
unreffed between uses, which is only the case with reget_buffer().

In other cases the caller may legitimately want to set the discard flag
and should have the option of doing so.
2024-09-23 17:11:40 +02:00
James Almer
660e7e6a0e avcodec: add LCEVC decoding support via LCEVCdec
Signed-off-by: James Almer <jamrial@gmail.com>
2024-09-23 10:20:47 -03:00
James Almer
dbbf9a5ff7 avcodec/decode: split ProgressFrame allocator into two functions
Signed-off-by: James Almer <jamrial@gmail.com>
2024-09-23 10:05:34 -03:00
James Almer
df609af8e4 avcodec/packet: add an LCEVC enhancement data payload side data type
Signed-off-by: James Almer <jamrial@gmail.com>
2024-09-19 10:01:26 -03:00
Anton Khirnov
deee00e2eb lavc/decode: reindent after previous commit 2024-08-12 14:42:20 +02:00
Anton Khirnov
5acbdd2264 lavc: convert frame threading to the receive_frame() pattern
Reorganize the code such that the frame threading code does not call the
decoders directly, but instead calls back into the generic decoding
code. This avoids duplicating the logic that wraps the decoder
invocation and allows receive_frame()-based decoders to use frame
threading.

Further work by Timo Rothenpieler <timo@rothenpieler.org>.
2024-08-12 14:42:20 +02:00
Anton Khirnov
4d209dada7 lavc/decode: reindent 2024-08-12 14:42:20 +02:00
Anton Khirnov
048e978e8e lavc/decode: wrap AV_FRAME_FLAG_DISCARD handling in a loop
Makes sure discarded frames do not cause EAGAIN to be returned during
flushing, which is forbidden.
2024-08-12 14:42:20 +02:00
Anton Khirnov
64743b45b5 lavc/internal: document the precise meaning of AVCodecInternal.draining
Also, set draining=1 in case a bitstream filter returns an
internally-triggered EOF. While no bitstream filters currently inserted
by decoders will do that, that may change in the future and it is better
to cover this case.
2024-08-12 14:42:20 +02:00
James Almer
afb06aef7e avcodec/decode: remove unused argument from ff_frame_new_side_data_from_buf()
Signed-off-by: James Almer <jamrial@gmail.com>
2024-07-29 14:00:48 -03:00
Andreas Rheinhardt
eee88ba0dc avcodec/decode: Set KEY flag+pict_type generically for intra-only codecs
This commit is the analog of 3f11eac757
for decoding: It sets the AV_FRAME_FLAG_KEY and (for video decoders)
also pict_type to AV_PICTURE_TYPE_I. It furthermore stops setting
audio frames as always being key frames -- it is wrong for e.g.
TrueHD/MLP. The latter also affects TAK and DFPWM.

The change already improves output for several decoders where
it has been forgotten to set e.g. pict_type like speedhq, wnv1
or tiff. The latter is the reason for the change to the exif-image-tiff
FATE test reference file.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-05-19 11:39:45 +02:00
Michael Niedermayer
e9bb586543 avcodec/decode: decode_simple_internal() only implements audio and video
Fixes: CID1538861 Uninitialized scalar variable

Sponsored-by: Sovereign Tech Fund
Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
2024-05-13 03:40:18 +02:00
Andreas Rheinhardt
9dc8f8839e avcodec/pthread_frame: Add API to share RefStruct refs just once
This is useful when the lifetime of the object to be shared
is the whole decoding process as it allows to avoid having
to sync them every time in update_thread_context.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-04-19 13:18:04 +02:00
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
James Almer
6def8e3923 avcodec/decode: add AVFrameSideData helper wrappers that don't depend on frames
They will be useful to fill arrays stored in other structs.

Signed-off-by: James Almer <jamrial@gmail.com>
2024-04-11 11:46:23 -03:00
Andreas Rheinhardt
790f793844 avutil/common: Don't auto-include mem.h
There are lots of files that don't need it: The number of object
files that actually need it went down from 2011 to 884 here.

Keep it for external users in order to not cause breakages.

Also improve the other headers a bit while just at it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2024-03-31 00:08:43 +01: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
Zhao Zhili
c775163a8c avcodec/decode: log hwaccel name
Many users mistakenly think that hwaccel is an instance of a decoder,
and cannot find the corresponding decoder name in the logs. Log
hwaccel name so user know hwaccel has taken effect.

Signed-off-by: Zhao Zhili <zhilizhao@tencent.com>
2024-03-22 20:26:53 +08:00