Commit graph

917 commits

Author SHA1 Message Date
Timo Rothenpieler
262d41c804 all: fix typos found by codespell 2025-08-03 13:48:47 +02:00
Andreas Rheinhardt
abc54c25be avcodec/mpegvideo: Move loop_filter to {H263Dec,MPVEnc,VC1}Context
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-07-03 20:35:32 +02:00
Andreas Rheinhardt
60f51bdaac avcodec/mpegvideo: Move partitioned_frame to {H263Dec,MPVEnc}Context
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-07-03 20:35:32 +02:00
Andreas Rheinhardt
3c06d87c93 avcodec/mpegvideo: Move fields to {H263Dec,MPVEnc}Context when possible
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-07-03 20:35:31 +02:00
Andreas Rheinhardt
532ae5dc7d avcodec/ituh263enc: Inline value of h263_flv
It is always two when we encode FLV1.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-07-03 20:35:31 +02:00
Andreas Rheinhardt
2089d39735 avcodec/mpegvideo: Move mb_skip_run to {RV34Dec,MPVEnc}Context
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-07-03 20:35:31 +02:00
Andreas Rheinhardt
273de7f60c avcodec/mpegvideo: Move unrestricted_mv to MotionEstContext
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-07-03 20:34:51 +02:00
Andreas Rheinhardt
05510dac6b avcodec/mpegvideo: Move flipflop_rounding to {MSMPEG4Dec,MPVEnc}Context
Forgotten in 9964212545.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-07-03 20:34:51 +02:00
Andreas Rheinhardt
3b87a480fe avcodec/mpegvideoenc: Allocate blocks as part of MPVEncContext
This avoids mpegvideo.c having to deal with the fact that
the encoders use two sets of blocks and is in preparation
for not allocating blocks at all.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-07-03 20:34:51 +02:00
Andreas Rheinhardt
56fd45d2a9 avcodec/{ituh263,mpeg4video}enc: Simplify out-of-slice DC prediction
Now that every slice (and therefore every thread) uses its own
DC array, one can simply set the out-of-slice DC values to its
expected values (this is not possible for the decoders due
to error resilience).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:52 +02:00
Andreas Rheinhardt
c84a03d752 avcodec/mpegvideo: Allocate dc_val for each encoder slice
This fixes data races (which are UB) in the MPEG-4 and H.263+
encoder when predicting DC values; these encoders unconditionally
read values from the line above the current line and only check
lateron (via first_slice_line) whether said prediction can be used
at all. It will also allow to remove said checks (by setting the
entries to 1024 upon opening a new slice).

The vsynth{1,2,3,_lena}-mpeg4-thread FATE tests were affected by this:
https://fate.ffmpeg.org/report.cgi?slot=x86_64-archlinux-gcc-tsan-slices&time=20250613002615

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:52 +02:00
Andreas Rheinhardt
81ab1106c8 avcodec/mpegvideo: Only keep MpegEncContext.dc_val[0]
The chroma dc_val pointers are mostly unused (accesses use
dc_val[0] and block_index), so remove them.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:52 +02:00
Andreas Rheinhardt
c4ebe55f94 avcodec/mpegvideo: Allocate ac_val jointly
They are currently allocated separately per slice; allocating them
jointly allows to avoid saving them in ff_update_duplicate_context().

The way it is done also avoids allocating ac_val for encoders that
don't need it (e.g. H.263 or H.263+ with AIC).

This entailed moving setting nb_slices to ff_mpv_init_context_frame()
which is called from ff_mpv_common_frame_size_change(). The resultant
nb_slices will always be one when called from a decoder using
ff_mpv_common_frame_size_change().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:52 +02:00
Andreas Rheinhardt
dba1061ba4 avcodec/mpegvideo: Only use a single ac_val pointer
The chroma pointers ac_val[1] and ac_val[2] are no longer used anywhere.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:52 +02:00
Andreas Rheinhardt
13527b39bb avcodec/mpegvideo: Redo resetting intra table entry
All callers check the corresponding entry of MpegEncContext.mbintra_table
and if set (indicating that the intra tables might have been written to
when decodeing a intra MB, so that they are "dirty"), call
ff_clean_intra_table_entries(), which resets them to default values
and resets the mbintra_table entry. Move resetting to the callers
(via an inline function that also performs the checks). This currently
has the advantage that the additional load of the mbintra_table ptr
can be avoided. It will also allow to simplify
ff_clean_intra_table_entries() (by using block_index[4] and [5]).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:52 +02:00
Andreas Rheinhardt
822be72f0a avcodec/mpegvideo_enc: Simplify allocating non-slice buffers
Allocate them before the slice contexts, so that they are automatically
copied to the slice contexts. This avoids having to set them in a loop.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:52 +02:00
Andreas Rheinhardt
8f92885b1f avcodec/mpegvideo: Defer init of enc slice ctxs in ff_mpv_common_init()
This will allow to perform initializations between ff_mpv_common_init()
and ff_mpv_init_duplicate_contexts() that will be automatically
copied to the slice contexts.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:52 +02:00
Andreas Rheinhardt
5b1e805653 avcodec/mpegvideo_enc: Remove always-true branch
Always-true since 1c40a17922
which made 4863671d88 superfluous.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-06-21 22:08:51 +02:00
Andreas Rheinhardt
17d5f30dd5 avcodec/pixblockdsp: Pass bits_per_raw_sample directly
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-31 01:27:09 +02:00
Andreas Rheinhardt
da5c2a419b avcodec/ituh263enc: Don't use h263_plus field
It is equal to codec_id == AV_CODEC_ID_H263P, check for that instead.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-26 05:03:15 +02:00
Andreas Rheinhardt
8987c29390 avcodec/mpegvideo: Check h263_aic, not h263_plus for allocating dc_val
This means that these buffers won't be allocated any more
for H.263+ with AIC disabled.
Also remove setting h263_plus for the RV20 encoder,
as it has only been done to force allocating dc_val.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-26 04:59:11 +02:00
Andreas Rheinhardt
ed73675832 avcodec/mpegvideo: Move mpeg_quant to {Mpeg4Dec,MPVEnc}Context
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-26 04:58:42 +02:00
Andreas Rheinhardt
295abb1fdc avcodec/mpegvideo_enc: Reduce stack usage
Multiple PutBitContexts are used when encoding partitioned
frames. When there are multiple candidates for macroblock types,
multiple states (namely the state before encoding the current MB,
the best state among the ones already tried and the current one)
need to be kept; duplicates of the PutBitContexts are among this
state. The temporary buffers for them are kept on the stack
in encode_thread() and their size is quite generous (MAX_MB_SIZE
- 3000 bytes). This commit uses tighter bounds, bringing the
size of the pb2 buffer down to 15 bytes.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-26 04:02:58 +02:00
Andreas Rheinhardt
9918741967 avcodec/mpegvideo_enc: Set gob_index once during init
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-26 04:00:08 +02:00
Andreas Rheinhardt
ef95a8d7b5 avcodec/mpegvideo_enc: Use av_unreachable() for unreachable code
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-26 03:38:37 +02:00
Andreas Rheinhardt
505510acda avcodec/mpegvideo: Move parent to MPVEncContext
This is more type-safe and avoids having parent contexts
when unnecessary.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-16 01:37:35 +02:00
Andreas Rheinhardt
f82a75333a avcodec/mpegvideo: Move [fb]_code to Mpeg4Dec and MPVEncContext
It is only used by the MPEG-4 decoder and the encoders.
Notice that this field is a per-frame property and therefore
does not need to by synced in mpeg4_update_thread_context().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-05-16 01:37:35 +02:00
Andreas Rheinhardt
116fd73143 avcodec/mpegvideo: Don't set [bf]_code for non-MPEG-4 decoders
It is only used by encoders and the MPEG-4 decoder.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-29 01:44:22 +01:00
Andreas Rheinhardt
f80a939a2e avcodec/motion_est: Put map, me_map into MotionEstContext
They have a fixed size and given that nowadays
MotionEstContext is no longer in any decoder's private context,
it is not wasteful to just put it into there.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-29 01:41:19 +01:00
Andreas Rheinhardt
074d8343d6 avcodec/mpegvideo_enc: Don't use unnecessarily much stack
encode_thread() puts two MPVEncContexts (2*6516B here)
on the stack and zeroes one of them in order to
temporarily store the variables that get changed
during encoding a macroblock (when there is more than
one candidate type for a macroblock). This is wasteful
and therefore this commit adds a small (328B here) structure
to store exactly the fields that actually need to be backed
up. Then one can extend MPVEncContext without fearing
too use up to much stack.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-29 01:38:30 +01:00
Andreas Rheinhardt
f8360ed629 avcodec/mpegvideo_enc: Pass data_partitioning directly
This avoids having to store it in the backup MPVEncContext.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-29 01:37:04 +01:00
Andreas Rheinhardt
114fccc4a5 avcodec/mpegvideo_enc: Set b-frame chain length properly
Fixes a regression caused by my desire to use loop-scope
for iterators in 72bf3d3c12.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-27 03:16:18 +01:00
Andreas Rheinhardt
e5a33c898a avcodec/mpegvideo: Only keep the actually used unquantize funcs
For all encoders and all decoders except MPEG-4 the unquantize
functions to use don't change at all and therefore needn't be
kept in the context. So discard them after setting them;
for MPEG-4, the functions get assigned on a per-frame basis.

Decoders not using any unquantize functions (H.261, MPEG-1/2)
as well as decoders that only call ff_mpv_reconstruct_mb()
through error resilience (RV30/40, the VC-1 family) don't have
the remaining pointers set at all.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:17:00 +01:00
Andreas Rheinhardt
46a2d03b75 avcodec/mpegvideo_enc: Use better variable name
Also fixes shadowing.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:13:24 +01:00
Andreas Rheinhardt
d5fba4aef9 avcodec/mpegvideo_enc: Defer initialization of mb-pos dependent vars
Only set them after mb_x and mb_y are known which happens
only after the call to ff_h261_reorder_mb_index().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:12:58 +01:00
Andreas Rheinhardt
854a3ed547 avcodec/motion_est: Reset scene_change score, MB variance stats
Reset them in ff_me_init_pic(). It is the appropriate place for it
and allows to remove resetting code from multiple places
(e.g. mpegvideo_enc.c resetted it in two different places
(one for the main slice context, one for all the others)).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:12:30 +01:00
Andreas Rheinhardt
479dac17f8 avcodec/mpegvideo_enc: Move code to initialize variables immediately
Also avoid casts and parentheses.
(This is only possible now because ff_update_duplicate_context()
no longer touches the PutBitContext.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:12:04 +01:00
Andreas Rheinhardt
6e45f7f535 avcodec/mpegvideo: Move MotionEstContext to MPVEncContext
All that is necessary to do so is perform ff_me_init_pic()
on every slice.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:11:32 +01:00
Andreas Rheinhardt
8b15979a4b avcodec/mpegvideo_enc: Don't reset statistics twice
This happens currently for the non-main slice contexts.
But these variables get reset at the start of encode_thread()
anyway for all slices, so this is unnecessary.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:10:06 +01:00
Andreas Rheinhardt
d66e9cb0d2 avcodec/mpegvideo_enc: Move lambda, lambda2 to MPVEncContext
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:09:33 +01:00
Andreas Rheinhardt
a064d34a32 avcodec/mpegvideoenc: Add MPVEncContext
Many of the fields of MpegEncContext (which is also used by decoders)
are actually only used by encoders. Therefore this commit adds
a new encoder-only structure and moves all of the encoder-only
fields to it except for those which require more explicit
synchronisation between the main slice context and the other
slice contexts. This synchronisation is currently mainly provided
by ff_update_thread_context() which simply copies most of
the main slice context over the other slice contexts. Fields
which are moved to the new MPVEncContext no longer participate
in this (which is desired, because it is horrible and for the
fields b) below wasteful) which means that some fields can only
be moved when explicit synchronisation code is added in later commits.

More explicitly, this commit moves the following fields:
a) Fields not copied by ff_update_duplicate_context():
dct_error_sum and dct_count; the former does not need synchronisation,
the latter is synchronised in merge_context_after_encode().
b) Fields which do not change after initialisation (these fields
could also be put into MPVMainEncContext at the cost of
an indirection to access them): lambda_table, adaptive_quant,
{luma,chroma}_elim_threshold, new_pic, fdsp, mpvencdsp, pdsp,
{p,b_forw,b_back,b_bidir_forw,b_bidir_back,b_direct,b_field}_mv_table,
[pb]_field_select_table, mb_{type,var,mean}, mc_mb_var, {min,max}_qcoeff,
{inter,intra}_quant_bias, ac_esc_length, the *_vlc_length fields,
the q_{intra,inter,chroma_intra}_matrix{,16}, dct_offset, mb_info,
mjpeg_ctx, rtp_mode, rtp_payload_size, encode_mb, all function
pointers, mpv_flags, quantizer_noise_shaping,
frame_reconstruction_bitfield, error_rate and intra_penalty.
c) Fields which are already (re)set explicitly: The PutBitContexts
pb, tex_pb, pb2; dquant, skipdct, encoding_error, the statistics
fields {mv,i_tex,p_tex,misc,last}_bits and i_count; last_mv_dir,
esc_pos (reset when writing the header).
d) Fields which are only used by encoders not supporting slice
threading for which synchronisation doesn't matter: esc3_level_length
and the remaining mb_info fields.
e) coded_score: This field is only really used when FF_MPV_FLAG_CBP_RD
is set (which implies trellis) and even then it is only used for
non-intra blocks. For these blocks dct_quantize_trellis_c() either
sets coded_score[n] or returns a last_non_zero value of -1
in which case coded_score will be reset in encode_mb_internal().
Therefore no old values are ever used.

The MotionEstContext has not been moved yet.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 04:08:33 +01:00
Andreas Rheinhardt
913322a267 avcodec/mpegvideo: Move me_pre, me_penalty_compensation to MPVMainEncCtx
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:46:49 +01:00
Andreas Rheinhardt
8397351221 avcodec/mpegvideo_enc: Call ff_mpv_common_init() later
Namely after the main slice context has already been initialized,
so that this initialized state is directly copied to the newly
created slice contexts without relying on it being copied
by ff_update_duplicate_context(). This is in preparation for further
commits.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:45:57 +01:00
Andreas Rheinhardt
fa630b481c avcodec/mpegvideo: Move last-pic information to MPVMainEncContext
last_pict_type, last_non_b_pict_type and last_lambda_for
are only used by the encoder's main thread.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:44:30 +01:00
Andreas Rheinhardt
1b537de8d4 avcodec/mpegvideo: Move noise_reduction to MPVMainEncContext
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:43:25 +01:00
Andreas Rheinhardt
f2ba0676b6 avcodec/mpegvideo_enc: Move allocating remaining buffers to init_buffers
Also allocate mb_type, mc_mb_var, mb_var and mb_mean jointly
while just at it.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:41:52 +01:00
Andreas Rheinhardt
a1f18b195e avcodec/mpegvideo_enc: Reindent after the previous commit
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:41:26 +01:00
Andreas Rheinhardt
9f43de06fa avcodec/mpegvideo_enc: Don't allocate B-frame bufs without B frames
(Checking in the same way for intra_only is not straightforward,
because at least p_mv_table is written to even in intra_only
mode.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:41:00 +01:00
Andreas Rheinhardt
7b5fb2e4ff avcodec/mpegvideo: Move encoder-only base arrays to MPVMainEncContext
Also allocate them jointly (thereby reducing the number of base
arrays considerably) and already set them on all slice contexts;
don't rely on ff_update_duplicate_context().

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:40:27 +01:00
Andreas Rheinhardt
a5a2fc04ee avcodec/mpegvideo: Move allocating encoder buffers to mpegvideoenc.c
dct_error_sum and me.map are allocated per slice-context
and therefore their allocation has not been moved to
mpegvideoenc.c in 0154fb43e3.

This commit changes this by allocating them jointly and
moving said allocations to mpegvideo_enc.c like the other
encoder-only buffers.
The buffers are suitably aligned to ensure that no false
sharing occurs.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
2025-03-26 03:39:58 +01:00