From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> To: ffmpeg-devel@ffmpeg.org Cc: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> Subject: [FFmpeg-devel] [PATCH 35/41] avcodec/mpegvideo: Allocate encoder-only tables in mpegvideo_enc.c Date: Sun, 30 Jan 2022 07:27:43 +0100 Message-ID: <AM7PR03MB6660615D3F5D44FEEF440E978F249@AM7PR03MB6660.eurprd03.prod.outlook.com> (raw) In-Reply-To: <AM7PR03MB666068B09E9D0014E1CC65DD8F249@AM7PR03MB6660.eurprd03.prod.outlook.com> This commit moves the encoder-only allocations of the tables owned solely by the main encoder context to mpegvideo_enc.c. This avoids checks in mpegvideo.c for whether we are dealing with an encoder; it also improves modularity (if encoders are disabled, this code will no longer be included in the binary). And it also avoids having to reset these pointers at the beginning of ff_mpv_common_init() (in case the dst context is uninitialized, ff_mpeg_update_thread_context() simply copies the src context into dst which therefore may contain pointers not owned by it, but this does not happen for encoders at all). Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com> --- libavcodec/mpegvideo.c | 118 +++---------------------------------- libavcodec/mpegvideo_enc.c | 68 ++++++++++++++++++++- 2 files changed, 74 insertions(+), 112 deletions(-) diff --git a/libavcodec/mpegvideo.c b/libavcodec/mpegvideo.c index 500f8af19d..9836000ad9 100644 --- a/libavcodec/mpegvideo.c +++ b/libavcodec/mpegvideo.c @@ -557,61 +557,13 @@ int ff_mpv_init_context_frame(MPVContext *s) s->mb_index2xy[s->mb_height * s->mb_width] = (s->mb_height - 1) * s->mb_stride + s->mb_width; // FIXME really needed? - if (s->encoding) { - /* Allocate MV tables */ - if (!FF_ALLOCZ_TYPED_ARRAY(s->p_mv_table_base, mv_table_size) || - !FF_ALLOCZ_TYPED_ARRAY(s->b_forw_mv_table_base, mv_table_size) || - !FF_ALLOCZ_TYPED_ARRAY(s->b_back_mv_table_base, mv_table_size) || - !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_forw_mv_table_base, mv_table_size) || - !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_back_mv_table_base, mv_table_size) || - !FF_ALLOCZ_TYPED_ARRAY(s->b_direct_mv_table_base, mv_table_size)) - return AVERROR(ENOMEM); - s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1; - s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1; - s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1; - s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1; - s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1; - s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1; - - /* Allocate MB type table */ - if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_type, mb_array_size) || - !FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size) || - !FF_ALLOC_TYPED_ARRAY (s->cplx_tab, mb_array_size) || - !FF_ALLOC_TYPED_ARRAY (s->bits_tab, mb_array_size)) - return AVERROR(ENOMEM); - -#define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p)))) - if (s->codec_id == AV_CODEC_ID_MPEG4 || - (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) { - int16_t (*tmp1)[2]; - uint8_t *tmp2; - if (!(tmp1 = ALLOCZ_ARRAYS(s->b_field_mv_table_base, 8, mv_table_size)) || - !(tmp2 = ALLOCZ_ARRAYS(s->b_field_select_table[0][0], 2 * 4, mv_table_size)) || - !ALLOCZ_ARRAYS(s->p_field_select_table[0], 2 * 2, mv_table_size)) - return AVERROR(ENOMEM); - - s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size; - tmp1 += s->mb_stride + 1; - - for (int i = 0; i < 2; i++) { - for (int j = 0; j < 2; j++) { - for (int k = 0; k < 2; k++) { - s->b_field_mv_table[i][j][k] = tmp1; - tmp1 += mv_table_size; - } - s->b_field_select_table[i][j] = tmp2; - tmp2 += 2 * mv_table_size; - } - } - } - } - if (s->codec_id == AV_CODEC_ID_MPEG4 || (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) { - int16_t (*tmp)[2]; /* interlaced direct mode decoding tables */ - if (!(tmp = ALLOCZ_ARRAYS(s->p_field_mv_table_base, 4, mv_table_size))) + int16_t (*tmp)[2] = av_calloc(mv_table_size, 4 * sizeof(*tmp)); + if (!tmp) return AVERROR(ENOMEM); + s->p_field_mv_table_base = tmp; tmp += s->mb_stride + 1; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { @@ -654,8 +606,6 @@ int ff_mpv_init_context_frame(MPVContext *s) static void clear_context(MPVMainContext *s) { - int i, j, k; - memset(&s->next_picture, 0, sizeof(s->next_picture)); memset(&s->last_picture, 0, sizeof(s->last_picture)); memset(&s->current_picture, 0, sizeof(s->current_picture)); @@ -684,31 +634,10 @@ static void clear_context(MPVMainContext *s) s->bitstream_buffer = NULL; s->allocated_bitstream_buffer_size = 0; s->picture = NULL; - s->mb_type = NULL; - s->p_mv_table_base = NULL; - s->b_forw_mv_table_base = NULL; - s->b_back_mv_table_base = NULL; - s->b_bidir_forw_mv_table_base = NULL; - s->b_bidir_back_mv_table_base = NULL; - s->b_direct_mv_table_base = NULL; - s->p_mv_table = NULL; - s->b_forw_mv_table = NULL; - s->b_back_mv_table = NULL; - s->b_bidir_forw_mv_table = NULL; - s->b_bidir_back_mv_table = NULL; - s->b_direct_mv_table = NULL; - s->b_field_mv_table_base = NULL; s->p_field_mv_table_base = NULL; - for (i = 0; i < 2; i++) { - for (j = 0; j < 2; j++) { - for (k = 0; k < 2; k++) { - s->b_field_mv_table[i][j][k] = NULL; - } - s->b_field_select_table[i][j] = NULL; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) s->p_field_mv_table[i][j] = NULL; - } - s->p_field_select_table[i] = NULL; - } s->dc_val_base = NULL; s->coded_block_base = NULL; @@ -721,10 +650,6 @@ static void clear_context(MPVMainContext *s) s->er.error_status_table = NULL; s->er.er_temp_buffer = NULL; s->mb_index2xy = NULL; - s->lambda_table = NULL; - - s->cplx_tab = NULL; - s->bits_tab = NULL; } /** @@ -820,37 +745,12 @@ av_cold int ff_mpv_common_init(MPVMainContext *s) void ff_mpv_free_context_frame(MPVMainContext *s) { - int i, j, k; - free_duplicate_contexts(s); - av_freep(&s->mb_type); - av_freep(&s->p_mv_table_base); - av_freep(&s->b_forw_mv_table_base); - av_freep(&s->b_back_mv_table_base); - av_freep(&s->b_bidir_forw_mv_table_base); - av_freep(&s->b_bidir_back_mv_table_base); - av_freep(&s->b_direct_mv_table_base); - s->p_mv_table = NULL; - s->b_forw_mv_table = NULL; - s->b_back_mv_table = NULL; - s->b_bidir_forw_mv_table = NULL; - s->b_bidir_back_mv_table = NULL; - s->b_direct_mv_table = NULL; - av_freep(&s->b_field_mv_table_base); - av_freep(&s->b_field_select_table[0][0]); av_freep(&s->p_field_mv_table_base); - av_freep(&s->p_field_select_table[0]); - for (i = 0; i < 2; i++) { - for (j = 0; j < 2; j++) { - for (k = 0; k < 2; k++) { - s->b_field_mv_table[i][j][k] = NULL; - } - s->b_field_select_table[i][j] = NULL; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) s->p_field_mv_table[i][j] = NULL; - } - s->p_field_select_table[i] = NULL; - } av_freep(&s->dc_val_base); av_freep(&s->coded_block_base); @@ -863,10 +763,6 @@ void ff_mpv_free_context_frame(MPVMainContext *s) av_freep(&s->er.error_status_table); av_freep(&s->er.er_temp_buffer); av_freep(&s->mb_index2xy); - av_freep(&s->lambda_table); - - av_freep(&s->cplx_tab); - av_freep(&s->bits_tab); s->linesize = s->uvlinesize = 0; } diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c index fd82681869..0204b4dacd 100644 --- a/libavcodec/mpegvideo_enc.c +++ b/libavcodec/mpegvideo_enc.c @@ -317,7 +317,7 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) MPVMainEncContext *const m = avctx->priv_data; MPVMainContext *const s = &m->common; AVCPBProperties *cpb_props; - int i, ret; + int i, ret, mv_table_size, mb_array_size; mpv_encode_defaults(m); @@ -818,6 +818,56 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) ff_pixblockdsp_init(&s->pdsp, avctx); ff_qpeldsp_init(&s->qdsp); + /* Allocate MV tables; the MV and MB tables will be copied + * to slice contexts by ff_update_duplicate_context(). */ + mv_table_size = (s->mb_height + 2) * s->mb_stride + 1; + if (!FF_ALLOCZ_TYPED_ARRAY(s->p_mv_table_base, mv_table_size) || + !FF_ALLOCZ_TYPED_ARRAY(s->b_forw_mv_table_base, mv_table_size) || + !FF_ALLOCZ_TYPED_ARRAY(s->b_back_mv_table_base, mv_table_size) || + !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_forw_mv_table_base, mv_table_size) || + !FF_ALLOCZ_TYPED_ARRAY(s->b_bidir_back_mv_table_base, mv_table_size) || + !FF_ALLOCZ_TYPED_ARRAY(s->b_direct_mv_table_base, mv_table_size)) + return AVERROR(ENOMEM); + s->p_mv_table = s->p_mv_table_base + s->mb_stride + 1; + s->b_forw_mv_table = s->b_forw_mv_table_base + s->mb_stride + 1; + s->b_back_mv_table = s->b_back_mv_table_base + s->mb_stride + 1; + s->b_bidir_forw_mv_table = s->b_bidir_forw_mv_table_base + s->mb_stride + 1; + s->b_bidir_back_mv_table = s->b_bidir_back_mv_table_base + s->mb_stride + 1; + s->b_direct_mv_table = s->b_direct_mv_table_base + s->mb_stride + 1; + + /* Allocate MB type table */ + mb_array_size = s->mb_height * s->mb_stride; + if (!FF_ALLOCZ_TYPED_ARRAY(s->mb_type, mb_array_size) || + !FF_ALLOCZ_TYPED_ARRAY(s->lambda_table, mb_array_size) || + !FF_ALLOC_TYPED_ARRAY (s->cplx_tab, mb_array_size) || + !FF_ALLOC_TYPED_ARRAY (s->bits_tab, mb_array_size)) + return AVERROR(ENOMEM); + +#define ALLOCZ_ARRAYS(p, mult, numb) ((p) = av_calloc(numb, mult * sizeof(*(p)))) + if (s->codec_id == AV_CODEC_ID_MPEG4 || + (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)) { + int16_t (*tmp1)[2]; + uint8_t *tmp2; + if (!(tmp1 = ALLOCZ_ARRAYS(s->b_field_mv_table_base, 8, mv_table_size)) || + !(tmp2 = ALLOCZ_ARRAYS(s->b_field_select_table[0][0], 2 * 4, mv_table_size)) || + !ALLOCZ_ARRAYS(s->p_field_select_table[0], 2 * 2, mv_table_size)) + return AVERROR(ENOMEM); + + s->p_field_select_table[1] = s->p_field_select_table[0] + 2 * mv_table_size; + tmp1 += s->mb_stride + 1; + + for (int i = 0; i < 2; i++) { + for (int j = 0; j < 2; j++) { + for (int k = 0; k < 2; k++) { + s->b_field_mv_table[i][j][k] = tmp1; + tmp1 += mv_table_size; + } + s->b_field_select_table[i][j] = tmp2; + tmp2 += 2 * mv_table_size; + } + } + } + if (!(avctx->stats_out = av_mallocz(256)) || !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix, 32) || !FF_ALLOCZ_TYPED_ARRAY(s->q_chroma_intra_matrix, 32) || @@ -943,6 +993,22 @@ av_cold int ff_mpv_encode_end(AVCodecContext *avctx) av_freep(&avctx->stats_out); + av_freep(&s->p_mv_table_base); + av_freep(&s->b_forw_mv_table_base); + av_freep(&s->b_back_mv_table_base); + av_freep(&s->b_bidir_forw_mv_table_base); + av_freep(&s->b_bidir_back_mv_table_base); + av_freep(&s->b_direct_mv_table_base); + av_freep(&s->b_field_mv_table_base); + av_freep(&s->b_field_select_table[0][0]); + av_freep(&s->p_field_select_table[0]); + + av_freep(&s->mb_type); + av_freep(&s->lambda_table); + + av_freep(&s->cplx_tab); + av_freep(&s->bits_tab); + if(s->q_chroma_intra_matrix != s->q_intra_matrix ) av_freep(&s->q_chroma_intra_matrix); if(s->q_chroma_intra_matrix16 != s->q_intra_matrix16) av_freep(&s->q_chroma_intra_matrix16); s->q_chroma_intra_matrix= NULL; -- 2.32.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2022-01-30 6:33 UTC|newest] Thread overview: 47+ messages / expand[flat|nested] mbox.gz Atom feed top 2022-01-30 6:08 [FFmpeg-devel] [PATCH 01/41] avcodec/mpegvideo_enc: Allow slices only for slice-thread-able codecs Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 02/41] avcodec/mjpegenc: Remove nonsense assert Andreas Rheinhardt 2022-01-30 13:15 ` Michael Niedermayer 2022-01-30 17:07 ` Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 03/41] avcodec/mjpegenc: Fix files with slices > 1, but threads == 1 Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 04/41] avcodec/mpegvideo: Enable private contexts Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 05/41] avcodec/h261: Separate decode and encode contexts Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 06/41] avcodec/h261enc: Pass PutBitContext directly in h261_encode_motion() Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 07/41] avcodec/idctdsp: Constify the permutation parameter of ff_init_scantable Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 08/41] avcodec/wmv2: Move initializing abt_scantables to the decoder Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 09/41] avcodec/wmv2: Split Wmv2Context into decoder and encoder context Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 10/41] avcodec/msmpeg4.h: Move encoder-only stuff to a new header Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 11/41] avcodec/msmpegenc: Add MSMPEG4EncContext and move ac_stats to it Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 12/41] avcodec/h263.h: Move encoder-only stuff to a new header h263enc.h Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 13/41] avcodec/mpegvideo: Move encoder-only stuff to a new header Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 14/41] avcodec/avcodec: Avoid MpegEncContext in AVHWAccel.decode_mb Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 15/41] avcodec/speedhqenc: Add SpeedHQEncContext and move slice_start to it Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 16/41] avcodec/mpegvideo: Use typedefs for MPV(Main)?(Dec|Enc)?Context Andreas Rheinhardt 2022-01-30 11:40 ` Michael Niedermayer 2022-01-30 23:05 ` Andreas Rheinhardt 2022-01-31 15:37 ` Michael Niedermayer 2022-01-30 11:43 ` Michael Niedermayer 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 17/41] avcodec/mpegvideo_enc: Don't find encoder by ID Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 18/41] avcodec/mpegvideoenc: Add proper MPVMainEncContext Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 19/41] avcodec/mpegvideoenc: Move tmp bframes to MPVMainEncContext Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 20/41] avcodec/mpegvideoenc: Move ratecontrol " Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 21/41] avcodec/mpegvideo: Move me_pre and me_penalty_compensation to enc-ctx Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 22/41] avcodec/mpegvideo: Move gop_size to MPVMainEncContext Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 23/41] avcodec/mpegvideo_enc: Don't set picture_in_gop_number for slice threads Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 24/41] avcodec/mpegvideo: Move picture_in_gop_number to MPVMainEncContext Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 25/41] avcodec/mpegvideo: Move pts and dts fields " Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 26/41] avcodec/mpegvideo: Move input_picture list " Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 27/41] avcodec/mpegvideo: Remove write-only [fb]_code Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 28/41] avcodec/mpegvideo: Move last-pic information to MPVMainEncContext Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 29/41] avcodec/mpegvideo: Move header_bits " Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 30/41] avcodec/mpegvideo_enc: Remove unused function parameters Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 31/41] avcodec/mpegvideo_enc: Remove unused parameter from encode_mb_hq() Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 32/41] avcodec/mpegvideo: Move vbv_delay to Mpeg1Context Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 33/41] avcodec/mpegvideo: Move brd_scale to MPVMainEncContext Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 34/41] avcodec/mpegvideo: Move ratecontrol parameters " Andreas Rheinhardt 2022-01-30 6:27 ` Andreas Rheinhardt [this message] 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 36/41] avcodec/mpegvideo: Move encoder-only base-arrays " Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 37/41] avcodec/mpegvideo_enc: Initialize non-JPEG q-matrices only once Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 38/41] avcodec/mpegvideo_enc: Avoid allocations for q_int(er|ra)_matrix tables Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 39/41] avcodec/mpegvideo: Move scenechange_threshold to MPVMainEncContext Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 40/41] avcodec/mpegvideo: Move dummy dst for depr. opts " Andreas Rheinhardt 2022-01-30 6:27 ` [FFmpeg-devel] [PATCH 41/41] avcodec/mpegvideo: Move frame_skip_(exp|cmp) " Andreas Rheinhardt
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=AM7PR03MB6660615D3F5D44FEEF440E978F249@AM7PR03MB6660.eurprd03.prod.outlook.com \ --to=andreas.rheinhardt@outlook.com \ --cc=ffmpeg-devel@ffmpeg.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git