From: Anton Khirnov <anton@khirnov.net> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 18/22] fftools/ffmpeg_enc: initialize audio/video encoders from frame parameters Date: Fri, 7 Jul 2023 11:48:43 +0200 Message-ID: <20230707094847.25324-18-anton@khirnov.net> (raw) In-Reply-To: <20230707094847.25324-1-anton@khirnov.net> This is possible now that enc_open() is always called with a non-NULL frame for audio/video. Previously the code would directly reach into the buffersink, which is a layering violation. --- fftools/ffmpeg_enc.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/fftools/ffmpeg_enc.c b/fftools/ffmpeg_enc.c index dd056e42f5..1347493f9f 100644 --- a/fftools/ffmpeg_enc.c +++ b/fftools/ffmpeg_enc.c @@ -34,8 +34,6 @@ #include "libavutil/rational.h" #include "libavutil/timestamp.h" -#include "libavfilter/buffersink.h" - #include "libavcodec/avcodec.h" // FIXME private header, used for mid_pred() @@ -198,12 +196,21 @@ int enc_open(OutputStream *ost, AVFrame *frame) AVCodecContext *dec_ctx = NULL; const AVCodec *enc = enc_ctx->codec; OutputFile *of = output_files[ost->file_index]; - FrameData *fd = frame ? frame_data(frame) : NULL; + FrameData *fd; int ret; if (e->opened) return 0; + // frame is always non-NULL for audio and video + av_assert0(frame || (enc->type != AVMEDIA_TYPE_VIDEO && enc->type != AVMEDIA_TYPE_AUDIO)); + + if (frame) { + fd = frame_data(frame); + if (!fd) + return AVERROR(ENOMEM); + } + set_encoder_id(output_files[ost->file_index], ost); if (ist) { @@ -212,15 +219,15 @@ int enc_open(OutputStream *ost, AVFrame *frame) switch (enc_ctx->codec_type) { case AVMEDIA_TYPE_AUDIO: - enc_ctx->sample_fmt = av_buffersink_get_format(ost->filter->filter); - enc_ctx->sample_rate = av_buffersink_get_sample_rate(ost->filter->filter); - ret = av_buffersink_get_ch_layout(ost->filter->filter, &enc_ctx->ch_layout); + enc_ctx->sample_fmt = frame->format; + enc_ctx->sample_rate = frame->sample_rate; + ret = av_channel_layout_copy(&enc_ctx->ch_layout, &frame->ch_layout); if (ret < 0) return ret; if (ost->bits_per_raw_sample) enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; - else if (fd) + else enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample, av_get_bytes_per_sample(enc_ctx->sample_fmt) << 3); @@ -231,7 +238,7 @@ int enc_open(OutputStream *ost, AVFrame *frame) case AVMEDIA_TYPE_VIDEO: { AVRational fr = ost->frame_rate; - if (!fr.num && fd) + if (!fr.num) fr = fd->frame_rate_filter; if (!fr.num && !ost->max_frame_rate.num) { fr = (AVRational){25, 1}; @@ -261,7 +268,7 @@ int enc_open(OutputStream *ost, AVFrame *frame) av_inv_q(fr); if (!(enc_ctx->time_base.num && enc_ctx->time_base.den)) - enc_ctx->time_base = av_buffersink_get_time_base(ost->filter->filter); + enc_ctx->time_base = frame->time_base; if ( av_q2d(enc_ctx->time_base) < 0.001 && ost->vsync_method != VSYNC_PASSTHROUGH && (ost->vsync_method == VSYNC_CFR || ost->vsync_method == VSYNC_VSCFR || (ost->vsync_method == VSYNC_AUTO && !(of->format->flags & AVFMT_VARIABLE_FPS)))){ @@ -270,18 +277,18 @@ int enc_open(OutputStream *ost, AVFrame *frame) "setting vsync/fps_mode to vfr\n"); } - enc_ctx->width = av_buffersink_get_w(ost->filter->filter); - enc_ctx->height = av_buffersink_get_h(ost->filter->filter); + enc_ctx->width = frame->width; + enc_ctx->height = frame->height; enc_ctx->sample_aspect_ratio = ost->st->sample_aspect_ratio = ost->frame_aspect_ratio.num ? // overridden by the -aspect cli option av_mul_q(ost->frame_aspect_ratio, (AVRational){ enc_ctx->height, enc_ctx->width }) : - av_buffersink_get_sample_aspect_ratio(ost->filter->filter); + frame->sample_aspect_ratio; - enc_ctx->pix_fmt = av_buffersink_get_format(ost->filter->filter); + enc_ctx->pix_fmt = frame->format; if (ost->bits_per_raw_sample) enc_ctx->bits_per_raw_sample = ost->bits_per_raw_sample; - else if (fd) + else enc_ctx->bits_per_raw_sample = FFMIN(fd->bits_per_raw_sample, av_pix_fmt_desc_get(enc_ctx->pix_fmt)->comp[0].depth); -- 2.40.1 _______________________________________________ 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:[~2023-07-07 9:51 UTC|newest] Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-07-07 9:48 [FFmpeg-devel] [PATCH 01/22] lavc/encode: print separate messages for unknown and unsupported formats Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 02/22] lavc/encode: shorten code by using a local variable Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 03/22] lavc/encoder: always print an error on an unsupported channel layout Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 04/22] lavc/encode: improve input sample rate validation Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 05/22] lavc/encode: improve unsupported-format error messages Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 06/22] lavfi: make sure frame SAR matches the link value Anton Khirnov 2023-07-15 21:10 ` Michael Niedermayer 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 07/22] fftools/ffmpeg_filter: make sure no input or output is bound twice Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 08/22] fftools/ffmpeg_filter: make OutputFile.format/sample_rate private Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 09/22] fftools/ffmpeg_filter: make OutputFile.ch_layout private Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 10/22] fftools/ffmpeg_filter: make OutputFile.width, height private Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 11/22] fftools/ffmpeg_filter: make OutputFile.{formats, ch_layouts, sample_rates} private Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 12/22] fftools/ffmpeg_filter: consolidate calling avfilter_graph_set_auto_convert() Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 13/22] fftools/ffmpeg_filter: stop disregarding user-specified pixel format Anton Khirnov 2023-07-08 22:15 ` Michael Niedermayer 2023-07-09 2:18 ` Anton Khirnov 2023-07-08 22:51 ` Michael Niedermayer 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 14/22] fftools/ffmpeg_filter: stop accessing encoder from pixfmt selection Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 15/22] fftools/ffmpeg: drop an obsolete debug log Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 16/22] fftools/ffmpeg: rework initializing encoders with no frames Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 17/22] fftools/ffmpeg_filter: only flush vsync code if encoding actually started Anton Khirnov 2023-07-07 9:48 ` Anton Khirnov [this message] 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 19/22] fftools/ffmpeg_filter: make OutputFilter.filter private Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 20/22] fftools/ffmpeg: add more structure to FrameData Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 21/22] fftools/ffmpeg: rework -enc_time_base handling Anton Khirnov 2023-07-07 9:48 ` [FFmpeg-devel] [PATCH 22/22] doc/ffmpeg: fix -enc_time_base documentation Anton Khirnov
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=20230707094847.25324-18-anton@khirnov.net \ --to=anton@khirnov.net \ --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