aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/mpegvideo_enc.c
Commit message (Collapse)AuthorAgeFilesLines
* all: fix typos found by codespellTimo Rothenpieler4 days1-1/+1
|
* avcodec/mpegvideo: Move loop_filter to {H263Dec,MPVEnc,VC1}ContextAndreas Rheinhardt2025-07-031-5/+5
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move partitioned_frame to {H263Dec,MPVEnc}ContextAndreas Rheinhardt2025-07-031-5/+5
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move fields to {H263Dec,MPVEnc}Context when possibleAndreas Rheinhardt2025-07-031-20/+20
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/ituh263enc: Inline value of h263_flvAndreas Rheinhardt2025-07-031-1/+0
| | | | | | It is always two when we encode FLV1. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move mb_skip_run to {RV34Dec,MPVEnc}ContextAndreas Rheinhardt2025-07-031-5/+6
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move unrestricted_mv to MotionEstContextAndreas Rheinhardt2025-07-031-9/+9
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move flipflop_rounding to {MSMPEG4Dec,MPVEnc}ContextAndreas Rheinhardt2025-07-031-7/+7
| | | | | | Forgotten in 9964212545554d9ae1afaabd23a7f9a361997d01. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideoenc: Allocate blocks as part of MPVEncContextAndreas Rheinhardt2025-07-031-38/+40
| | | | | | | | 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>
* avcodec/{ituh263,mpeg4video}enc: Simplify out-of-slice DC predictionAndreas Rheinhardt2025-06-211-1/+7
| | | | | | | | | 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>
* avcodec/mpegvideo: Allocate dc_val for each encoder sliceAndreas Rheinhardt2025-06-211-0/+1
| | | | | | | | | | | | | | 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>
* avcodec/mpegvideo: Only keep MpegEncContext.dc_val[0]Andreas Rheinhardt2025-06-211-3/+3
| | | | | | | 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>
* avcodec/mpegvideo: Allocate ac_val jointlyAndreas Rheinhardt2025-06-211-12/+24
| | | | | | | | | | | | | | | 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>
* avcodec/mpegvideo: Only use a single ac_val pointerAndreas Rheinhardt2025-06-211-2/+2
| | | | | | The chroma pointers ac_val[1] and ac_val[2] are no longer used anywhere. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Redo resetting intra table entryAndreas Rheinhardt2025-06-211-2/+5
| | | | | | | | | | | | | | 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>
* avcodec/mpegvideo_enc: Simplify allocating non-slice buffersAndreas Rheinhardt2025-06-211-75/+73
| | | | | | | 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>
* avcodec/mpegvideo: Defer init of enc slice ctxs in ff_mpv_common_init()Andreas Rheinhardt2025-06-211-3/+6
| | | | | | | | 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>
* avcodec/mpegvideo_enc: Remove always-true branchAndreas Rheinhardt2025-06-211-4/+2
| | | | | | | Always-true since 1c40a179222f638e88c8e7b1a374753a00b0f979 which made 4863671d888273392e9dcc2429f3852c00330498 superfluous. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/pixblockdsp: Pass bits_per_raw_sample directlyAndreas Rheinhardt2025-05-311-1/+1
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/ituh263enc: Don't use h263_plus fieldAndreas Rheinhardt2025-05-261-1/+0
| | | | | | It is equal to codec_id == AV_CODEC_ID_H263P, check for that instead. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Check h263_aic, not h263_plus for allocating dc_valAndreas Rheinhardt2025-05-261-1/+2
| | | | | | | | | 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>
* avcodec/mpegvideo: Move mpeg_quant to {Mpeg4Dec,MPVEnc}ContextAndreas Rheinhardt2025-05-261-7/+8
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_enc: Reduce stack usageAndreas Rheinhardt2025-05-261-4/+5
| | | | | | | | | | | | | | 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>
* avcodec/mpegvideo_enc: Set gob_index once during initAndreas Rheinhardt2025-05-261-13/+5
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_enc: Use av_unreachable() for unreachable codeAndreas Rheinhardt2025-05-261-3/+7
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move parent to MPVEncContextAndreas Rheinhardt2025-05-161-0/+1
| | | | | | | This is more type-safe and avoids having parent contexts when unnecessary. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move [fb]_code to Mpeg4Dec and MPVEncContextAndreas Rheinhardt2025-05-161-15/+15
| | | | | | | | 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>
* avcodec/mpegvideo: Don't set [bf]_code for non-MPEG-4 decodersAndreas Rheinhardt2025-03-291-0/+3
| | | | | | It is only used by encoders and the MPEG-4 decoder. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/motion_est: Put map, me_map into MotionEstContextAndreas Rheinhardt2025-03-291-13/+2
| | | | | | | | 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>
* avcodec/mpegvideo_enc: Don't use unnecessarily much stackAndreas Rheinhardt2025-03-291-69/+89
| | | | | | | | | | | | | | 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>
* avcodec/mpegvideo_enc: Pass data_partitioning directlyAndreas Rheinhardt2025-03-291-6/+5
| | | | | | This avoids having to store it in the backup MPVEncContext. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_enc: Set b-frame chain length properlyAndreas Rheinhardt2025-03-271-2/+3
| | | | | | | Fixes a regression caused by my desire to use loop-scope for iterators in 72bf3d3c12ae562fd408603483bc59058757b1a1. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Only keep the actually used unquantize funcsAndreas Rheinhardt2025-03-261-11/+21
| | | | | | | | | | | | | | 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>
* avcodec/mpegvideo_enc: Use better variable nameAndreas Rheinhardt2025-03-261-5/+5
| | | | | | Also fixes shadowing. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_enc: Defer initialization of mb-pos dependent varsAndreas Rheinhardt2025-03-261-6/+4
| | | | | | | 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>
* avcodec/motion_est: Reset scene_change score, MB variance statsAndreas Rheinhardt2025-03-261-9/+3
| | | | | | | | | 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>
* avcodec/mpegvideo_enc: Move code to initialize variables immediatelyAndreas Rheinhardt2025-03-261-8/+5
| | | | | | | | 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>
* avcodec/mpegvideo: Move MotionEstContext to MPVEncContextAndreas Rheinhardt2025-03-261-21/+20
| | | | | | | 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>
* avcodec/mpegvideo_enc: Don't reset statistics twiceAndreas Rheinhardt2025-03-261-8/+9
| | | | | | | | 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>
* avcodec/mpegvideo_enc: Move lambda, lambda2 to MPVEncContextAndreas Rheinhardt2025-03-261-20/+22
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideoenc: Add MPVEncContextAndreas Rheinhardt2025-03-261-1052/+1061
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* avcodec/mpegvideo: Move me_pre, me_penalty_compensation to MPVMainEncCtxAndreas Rheinhardt2025-03-261-4/+4
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_enc: Call ff_mpv_common_init() laterAndreas Rheinhardt2025-03-261-14/+17
| | | | | | | | | | 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>
* avcodec/mpegvideo: Move last-pic information to MPVMainEncContextAndreas Rheinhardt2025-03-261-8/+8
| | | | | | | 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>
* avcodec/mpegvideo: Move noise_reduction to MPVMainEncContextAndreas Rheinhardt2025-03-261-8/+11
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_enc: Move allocating remaining buffers to init_buffersAndreas Rheinhardt2025-03-261-19/+19
| | | | | | | 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>
* avcodec/mpegvideo_enc: Reindent after the previous commitAndreas Rheinhardt2025-03-261-11/+11
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_enc: Don't allocate B-frame bufs without B framesAndreas Rheinhardt2025-03-261-3/+7
| | | | | | | | (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>
* avcodec/mpegvideo: Move encoder-only base arrays to MPVMainEncContextAndreas Rheinhardt2025-03-261-51/+41
| | | | | | | | 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>
* avcodec/mpegvideo: Move allocating encoder buffers to mpegvideoenc.cAndreas Rheinhardt2025-03-261-0/+54
| | | | | | | | | | | | | | dct_error_sum and me.map are allocated per slice-context and therefore their allocation has not been moved to mpegvideoenc.c in 0154fb43e328b13da8943e66b38dc1b5ab9315af. 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>