aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/mpegvideo.c
Commit message (Collapse)AuthorAgeFilesLines
* avcodec/mpegvideo: Move fields to {H263Dec,MPVEnc}Context when possibleAndreas Rheinhardt2025-07-031-2/+0
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpeg12dec: Don't use MPVContext.blockAndreas Rheinhardt2025-07-031-21/+2
| | | | | | | Instead add the necessary blocks directly into Mpeg12SliceContext. This allows to completely remove MPVContext.block. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Add missing headersAndreas Rheinhardt2025-07-031-0/+1
| | | | | | | These files currently rely on implicit inclusions of avassert.h and/or mathops.h via get_bits.h. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpeg12dec: Add Mpeg12SliceContextAndreas Rheinhardt2025-07-031-1/+1
| | | | | | | This is in preparation for removing the GetBitContext from MPVContext. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideoenc: Allocate blocks as part of MPVEncContextAndreas Rheinhardt2025-07-031-6/+6
| | | | | | | | 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/mpegvideo: Reduce stack usage when copying MpegEncContextAndreas Rheinhardt2025-06-211-16/+13
| | | | | | (GCC did not perform this on its own; Clang did.) Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Allocate dc_val for each encoder sliceAndreas Rheinhardt2025-06-211-3/+12
| | | | | | | | | | | | | | 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-4/+2
| | | | | | | 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: Zero-init mbintra_tableAndreas Rheinhardt2025-06-211-2/+1
| | | | | | | | | | Up until now, they are marked as dirty (filled with 1), meaning that the entries are in need of a reset via ff_clean_intra_table_entries(); but actually, the entries are initialized to the state that ff_clean_intra_table_entries() produces, so they can be marked as non-dirty (i.e. filled with 0). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Allocate ac_val jointlyAndreas Rheinhardt2025-06-211-32/+28
| | | | | | | | | | | | | | | 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-7/+3
| | | | | | 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: Combine storesAndreas Rheinhardt2025-06-211-4/+2
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Don't reset AC values of upper-left luma blockAndreas Rheinhardt2025-06-211-1/+3
| | | | | | | | Said block will only be referenced by blocks from the same macroblock, which will read the new AC values instead of the reset values from this function. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Provide alignment hint to compilerAndreas Rheinhardt2025-06-211-4/+6
| | | | | | | | | | The underlying element type has always a size of 32B, so that the pointer is always at least 16B aligned. Clang uses this to upgrade the (inlined) memset to aligned stores. GCC doesn't (it does it only when the alignment hint is provided via __builtin_assume_aligned). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Avoid {a,d}c_val[{1,2}]Andreas Rheinhardt2025-06-211-7/+8
| | | | | | | | These are already mostly unused; not using them here is in preparation for removing them altogether. Use block_index[4..5] instead for addressing. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Redo resetting intra table entryAndreas Rheinhardt2025-06-211-2/+0
| | | | | | | | | | | | | | 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: Defer init of enc slice ctxs in ff_mpv_common_init()Andreas Rheinhardt2025-06-211-3/+5
| | | | | | | | 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: Don't zero unnecessarilyAndreas Rheinhardt2025-06-211-1/+1
| | | | | | | The whole dc_val array is initialized (to 1024) immediately after the allocation. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Don't reset thread_context ptrs unnecessarilyAndreas Rheinhardt2025-06-211-1/+0
| | | | | | They are freed with av_freep(), so the pointers are already consistent. 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/+3
| | | | | | | | | 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 parent to MPVEncContextAndreas Rheinhardt2025-05-161-2/+0
| | | | | | | This is more type-safe and avoids having parent contexts when unnecessary. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move clear_context() to mpeg4videodec.cAndreas Rheinhardt2025-05-161-38/+0
| | | | | | | | | There is only one source of dirty MpegEncContexts: update_mpvctx() (which copies an MpegEncContext over another one in order to initialize it). So only clear these contexts. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Don't set [bf]_code for non-MPEG-4 decodersAndreas Rheinhardt2025-03-291-3/+0
| | | | | | It is only used by encoders and the MPEG-4 decoder. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Only keep the actually used unquantize funcsAndreas Rheinhardt2025-03-261-2/+0
| | | | | | | | | | | | | | 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: Move unquantize functions into a file of their ownAndreas Rheinhardt2025-03-261-235/+2
| | | | | | | This is in preparation for only keeping the actually used unquantize functions in MpegEncContext. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move MotionEstContext to MPVEncContextAndreas Rheinhardt2025-03-261-6/+0
| | | | | | | 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/mpegvideoenc: Add MPVEncContextAndreas Rheinhardt2025-03-261-3/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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: Support custom slice context sizesAndreas Rheinhardt2025-03-261-1/+2
| | | | | | | This is in preparation for adding a special slice context for the encoders and moving all the encoder-specific fields to it. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move allocating encoder buffers to mpegvideoenc.cAndreas Rheinhardt2025-03-261-17/+0
| | | | | | | | | | | | | | 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>
* avcodec/mpegvideo: Add pointer to main context to slice contextsAndreas Rheinhardt2025-03-261-0/+2
| | | | | | | | It is a pointer to const to allow the slice threads to inspect values without modifying them; also make it a simple cast for codecs that don't support slice threading. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Don't initialize [yc]_dc_scale_table by defaultAndreas Rheinhardt2025-03-261-2/+0
| | | | | | | | | Only the H.263-based decoders as well as the encoders need it; so move it to ff_h263_decode_init() as well as ff_mpv_encode_init(). Also for the latter make it only set these tables if none are already set. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo(_enc)?: Mark init, close functions as av_coldAndreas Rheinhardt2025-03-261-8/+8
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Mark ff_mpv_common_defaults() as av_coldAndreas Rheinhardt2025-03-071-1/+1
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Move bitstream_buffer to mpeg4videodecAndreas Rheinhardt2025-03-041-5/+0
| | | | | | | This is possible by moving the code using it to open a GetBitContext from h263dec.c to mpeg4videodec.c. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* lavc/refstruct: move to lavu and make publicAnton Khirnov2024-12-151-9/+9
| | | | It is highly versatile and generally useful.
* avcodec/mpegvideo: remove redundant workaround to recalculate last nonzero ↵Ramiro Polla2024-09-031-6/+3
| | | | | | | | | | | | | | | | | coefficient The x86 optimized dct_quantize only calculates the last nonzero coefficient correctly if the zigzag scan order is used. For the alternate scan order, this value is incorrect. To work around this, the dct_unquantize functions process the entire block if the alternate scan order is used. But a second workaround (bb198e198ab) was added that recalculates the last nonzero coefficient after dct_quantize is called if the alternate scan order is used. This commit removes the first workaround, which became redundant.
* avcodec/mpegvideo: Join loops when initializing ScanTableAndreas Rheinhardt2024-06-201-10/+3
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Set dct_unquantize earlierAndreas Rheinhardt2024-06-201-25/+25
| | | | | | | | Set them in ff_mpv_idct_init() so that they are already set in ff_mpv_decode_init(). This is in preparation for avoiding to set dct_unquantize in every ff_mpv_frame_start(). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Don't pretend dct_init can failAndreas Rheinhardt2024-06-201-3/+1
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* libavcodec/alpha: remove DSP & support codeSean McGovern2024-06-131-3/+1
| | | | | | | | | | | | | | | | | | Introduced in 1992, the Alpha was a 64-bit RISC processor designed to replace the VAX CISC machines sold by Digital Equipment Corporation. After Digital was acquired by Compaq in 1998 -- who themselves would be later purchased by Hewlett-Packard, the architecture was phased out over the following decade. It became effectively defunct in 2007, the last publicly available processor being the Alpha 21364. FFmpeg has not added any DSP code for this architecture since lowres2 was introduced in 2012, and it is more than unlikely someone still wishes to maintain it. Remove the DSP and support code. Signed-off-by: Vittorio Giovara <vittorio.giovara@gmail.com>
* avcodec/mpegvideo: Use enum for msmpeg4_versionAndreas Rheinhardt2024-06-121-3/+3
| | | | | | Improves readability. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Remove pblocksAndreas Rheinhardt2024-06-121-20/+2
| | | | | | | | | | | It has been added in a579db0c4fe026d49c71d1ff64a2d1d07c152d68 due to XvMC, but today it is only used to swap U and V for VCR2, a MPEG-2 variant with U and V swapped. This can be done in a simpler fashion, namely by simply swapping the U and V pointers of the corresponding MPVWorkPictures. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_dec: Don't sync encoder-only coded_picture_numberAndreas Rheinhardt2024-06-121-1/+0
| | | | Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo_enc: Unify initializing PutBitContextsAndreas Rheinhardt2024-06-121-1/+0
| | | | | | | This also rids us of the requirement to preserve the PutBitContext in ff_update_duplicate_context(). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegpicture: Avoid MotionEstContext in ff_mpeg_framesize_alloc()Andreas Rheinhardt2024-06-121-7/+3
| | | | | | | | | | Only set the ScratchpadContext and let the users that need it (i.e. encoders) set the MotionEstContext stuff themselves. Also add an explicit pointer to ScratchpadContext to point to the allocated buffer so that none of the other scratchpad pointers is singled out as being used for the allocations. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegpicture: Use union for b_scratchpad and rd_scratchpadAndreas Rheinhardt2024-06-121-1/+0
| | | | | | | These pointers point to the same buffers, so one can just use a union for them and avoid synchronising one of them. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegpicture: Make MPVPicture refcountedAndreas Rheinhardt2024-06-121-17/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Up until now, an initialized MpegEncContext had an array of MPVPictures (way more than were ever needed) and the MPVPicture* contained in the MPVWorkPictures as well as the input_picture and reordered_input_picture arrays (for the encoder) pointed into this array. Several of the pointers could point to the same slot and because there was no reference counting involved, one had to check for aliasing before unreferencing. Furthermore, given that these pointers were not ownership pointers the pointers were often simply reset without unreferencing the slot (happened e.g. for the RV30 and RV40 decoders) or there were moved without resetting the src pointer (happened for the encoders where the entries in the input_picture and reordered_input_picture arrays were not reset). Instead actually releasing these pictures was performed by looping over the whole array and checking which one of the entries needed to be kept. Given that the array had way too many slots (36), this meant that more than 30 MPVPictures have been unnecessarily unreferenced in every ff_mpv_frame_start(); something similar happened for the encoder. This commit changes this by making the MPVPictures refcounted via the RefStruct API. The MPVPictures itself are part of a pool so that this does not entail constant allocations; instead, the amount of allocations actually goes down, because the earlier code used such a large array of MPVPictures (36 entries) and allocated an AVFrame for every one of these on every ff_mpv_common_init(). In fact, the pool is only freed when closing the codec, so that reinitializations don't lead to new allocations (this avoids having to sync the pool in update_thread_context). Making MPVPictures refcounted also has another key benefit: It makes it possible to directly share them across threads (when using frame-threaded decoding), eliminating ugly code with underlying av_frame_ref()'s; sharing these pictures can't fail any more. The pool is allocated in ff_mpv_decode_init() for decoders, which therefore can fail now. This and the fact that the pool is not unreferenced in ff_mpv_common_end() also necessitated to mark several mpegvideo-decoders with the FF_CODEC_CAP_INIT_CLEANUP flag. *: This also means that there is no good reason any more for ff_mpv_common_frame_size_change() to exist. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegpicture: Split MPVPicture into WorkPicture and ordinary PicAndreas Rheinhardt2024-06-121-11/+0
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | There are two types of MPVPictures: Three (cur_pic, last_pic, next_pic) that are directly part of MpegEncContext and an array of MPVPictures that are separately allocated and are mostly accessed via pointers (cur|last|next)_pic_ptr; they are also used to store AVFrames in the encoder (necessary due to B-frames). As the name implies, each of the former is directly associated with one of the _ptr pointers: They actually share the same underlying buffers, but the ones that are part of the context can have their data pointers offset and their linesize doubled for field pictures. Up until now, each of these had their own references; in particular, there was an underlying av_frame_ref() to sync cur_pic and cur_pic_ptr etc. This is wasteful. This commit changes this relationship: cur_pic, last_pic and next_pic now become MPVWorkPictures; this structure does not have an AVFrame at all any more, but only the cached values of data and linesize. It also contains a pointer to the corresponding MPVPicture, establishing a more natural relationsship between the two. This already means that creating the context-pictures from the pointers can no longer fail. What has not been changed is the fact that the MPVPicture* pointers are not ownership pointers and that the MPVPictures are part of an array of MPVPictures that is owned by a single AVCodecContext. Doing so will be done in a latter commit. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegpicture: Cache AVFrame.data and linesize valuesAndreas Rheinhardt2024-06-121-5/+5
| | | | | | | This avoids an indirection and is in preparation for removing the AVFrame from MpegEncContext.(cur|last|next)_pic altogether. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
* avcodec/mpegvideo: Shorten variable namesAndreas Rheinhardt2024-06-121-17/+17
| | | | | | | | current_picture->cur_pic, last_picture->last_pic, similarly for new_picture and next_picture. Also rename the corresponding *_ptr fields. Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>