From patchwork Thu Aug 25 08:59:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 37471 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:139a:b0:8f:1db5:eae2 with SMTP id w26csp201249pzh; Thu, 25 Aug 2022 02:02:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR4wSibiwUAfXMvkAJHG7qIADgVjO5K8S9Cn0H2BEChdvcQrJ1A3TToIvlxhfyYJaLTHzwIS X-Received: by 2002:a50:9f4f:0:b0:447:8aff:d312 with SMTP id b73-20020a509f4f000000b004478affd312mr2335419edf.110.1661418125187; Thu, 25 Aug 2022 02:02:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661418125; cv=none; d=google.com; s=arc-20160816; b=D1jgublaBkJHRZBfZCQVGayyuCP4ir4ULw24vnQGlNbqGcaAywArw0C9qB97M4IK8f HU1uBqVOMBAx0SVtF7y94jFu85qP4yeSZu7rZFfwSYp3zdBOj7CqaqtwNjKL9rYj2oR7 3NHe9zznQQx6mY5tzg2MIjZbnI5k6i0GrgupYPj89a5ozls9WprjuaS3Np1GEqPxwOnd YL6Wh2Fe6KjCjprOWAx4KbhlLPgsGZR5BHTapd7p+jv+4PM8zuwajKE8r2luSUv3UwcI 4o5tYZ/ySZnzOudoIvpL/bsR77M02mnouylddmMaWavGoVwnTTrBU8I1sMh5dMdR4pgl YE/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=Inn3wDn+m0EpkSZdKf6+1UEhTVvVTilQtftIpfB8d3s=; b=DDUJClgNV4LWrzl7t6YNv0kHsHPMhFDBaYEJHtpez+xI4pRXSXCkCRh1KlSg9UKSgh 3Tmn6kj8ENAbmb3yVO3wHeYMugT7A/l1Fjx+bVYZIN3fSztsAAf6nkF0ekiuQS99ODbB lwCkyzfrtOLASbmq0Dn8jfAqbgbAGBXhtXpykRlRi12EwUCx0gSJrtuWH4bae3p/YweA bn+cKynu6oxqYXTc4x+uSchwGPX81Z+6dJ25Gt7visOc7BOAwYzo/nh5BD4WPcteBk/6 jmb0PmjvWhq6oGCbwwTfTFtyACG+2UL4At7fMXIZifRF8X3BBPitgBtKg2CXzVHsNL+t 7SRw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ga26-20020a1709070c1a00b0072eda634546si3801002ejc.560.2022.08.25.02.02.04; Thu, 25 Aug 2022 02:02:05 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E656268B9F2; Thu, 25 Aug 2022 12:01:22 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A543D68B9C8 for ; Thu, 25 Aug 2022 12:01:13 +0300 (EEST) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 66BD4240D1C for ; Thu, 25 Aug 2022 11:01:13 +0200 (CEST) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id 1uT6C7iw8rPL for ; Thu, 25 Aug 2022 11:01:12 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail0.khirnov.net (Postfix) with ESMTPS id 0C369240D1A for ; Thu, 25 Aug 2022 11:01:11 +0200 (CEST) Received: by libav.khirnov.net (Postfix, from userid 1000) id 834733A0F7F; Thu, 25 Aug 2022 11:01:04 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Thu, 25 Aug 2022 10:59:54 +0200 Message-Id: <20220825085956.4790-4-anton@khirnov.net> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220825085956.4790-1-anton@khirnov.net> References: <20220825085956.4790-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 4/6] fftools/ffmpeg: drop OutputStream.enc X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: u+CecZ7CXKq3 It is either equal to OutputStream.enc_ctx->codec, or NULL when enc_ctx is NULL. Replace the use of enc with enc_ctx->codec, or the equivalent enc_ctx->codec_* fields where more convenient. --- fftools/ffmpeg.c | 27 +++++++++++++------------- fftools/ffmpeg.h | 1 - fftools/ffmpeg_filter.c | 13 +++++++------ fftools/ffmpeg_hw.c | 6 +++--- fftools/ffmpeg_opt.c | 43 ++++++++++++++++++++++++----------------- 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index ef7177fc33..4a825aa6b5 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -2362,7 +2362,7 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output, OutputStream *ost = output_streams[i]; if (!check_output_constraints(ist, ost) || !ost->enc_ctx - || ost->enc->type != AVMEDIA_TYPE_SUBTITLE) + || ost->enc_ctx->codec_type != AVMEDIA_TYPE_SUBTITLE) continue; do_subtitle_out(output_files[ost->file_index], ost, &subtitle); @@ -2865,13 +2865,14 @@ static int init_output_stream_streamcopy(OutputStream *ost) static void set_encoder_id(OutputFile *of, OutputStream *ost) { + const char *cname = ost->enc_ctx->codec->name; uint8_t *encoder_string; int encoder_string_len; if (av_dict_get(ost->st->metadata, "encoder", NULL, 0)) return; - encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2; + encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(cname) + 2; encoder_string = av_mallocz(encoder_string_len); if (!encoder_string) exit_program(1); @@ -2880,7 +2881,7 @@ static void set_encoder_id(OutputFile *of, OutputStream *ost) av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len); else av_strlcpy(encoder_string, "Lavc ", encoder_string_len); - av_strlcat(encoder_string, ost->enc->name, encoder_string_len); + av_strlcat(encoder_string, cname, encoder_string_len); av_dict_set(&ost->st->metadata, "encoder", encoder_string, AV_DICT_DONT_STRDUP_VAL | AV_DICT_DONT_OVERWRITE); } @@ -3007,9 +3008,9 @@ static int init_output_stream_encode(OutputStream *ost, AVFrame *frame) !ost->frame_rate.den)) ost->frame_rate = ost->max_frame_rate; - if (ost->enc->supported_framerates && !ost->force_fps) { - int idx = av_find_nearest_q_idx(ost->frame_rate, ost->enc->supported_framerates); - ost->frame_rate = ost->enc->supported_framerates[idx]; + if (enc_ctx->codec->supported_framerates && !ost->force_fps) { + int idx = av_find_nearest_q_idx(ost->frame_rate, enc_ctx->codec->supported_framerates); + ost->frame_rate = enc_ctx->codec->supported_framerates[idx]; } // reduce frame rate for mpeg4 to be within the spec limits if (enc_ctx->codec_id == AV_CODEC_ID_MPEG4) { @@ -3150,7 +3151,7 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, int ret = 0; if (ost->enc_ctx) { - const AVCodec *codec = ost->enc; + const AVCodec *codec = ost->enc_ctx->codec; AVCodecContext *dec = NULL; InputStream *ist; @@ -3179,7 +3180,7 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, return ret; } - if (ist && ist->dec->type == AVMEDIA_TYPE_SUBTITLE && ost->enc->type == AVMEDIA_TYPE_SUBTITLE) { + if (ist && ist->dec->type == AVMEDIA_TYPE_SUBTITLE && codec->type == AVMEDIA_TYPE_SUBTITLE) { int input_props = 0, output_props = 0; AVCodecDescriptor const *input_descriptor = avcodec_descriptor_get(dec->codec_id); @@ -3206,8 +3207,8 @@ static int init_output_stream(OutputStream *ost, AVFrame *frame, ost->file_index, ost->index); return ret; } - if (ost->enc->type == AVMEDIA_TYPE_AUDIO && - !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) + if (codec->type == AVMEDIA_TYPE_AUDIO && + !(codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) av_buffersink_set_frame_size(ost->filter->filter, ost->enc_ctx->frame_size); assert_avoptions(ost->encoder_opts); @@ -3419,7 +3420,7 @@ static int transcode_init(void) av_log(NULL, AV_LOG_INFO, " (graph %d)", ost->filter->graph->index); av_log(NULL, AV_LOG_INFO, " -> Stream #%d:%d (%s)\n", ost->file_index, - ost->index, ost->enc ? ost->enc->name : "?"); + ost->index, ost->enc_ctx->codec->name); continue; } @@ -3430,7 +3431,7 @@ static int transcode_init(void) ost->index); if (ost->enc_ctx) { const AVCodec *in_codec = input_streams[ost->source_index]->dec; - const AVCodec *out_codec = ost->enc; + const AVCodec *out_codec = ost->enc_ctx->codec; const char *decoder_name = "?"; const char *in_codec_name = "?"; const char *encoder_name = "?"; @@ -3826,7 +3827,7 @@ static int process_input(int file_index) OutputStream *ost = output_streams[j]; if (ost->source_index == ifile->ist_index + i && - (!ost->enc_ctx || ost->enc->type == AVMEDIA_TYPE_SUBTITLE)) { + (!ost->enc_ctx || ost->enc_ctx->codec_type == AVMEDIA_TYPE_SUBTITLE)) { OutputFile *of = output_files[ost->file_index]; output_packet(of, ost->pkt, ost, 1); } diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 50654bf8db..68f24bd1d3 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -509,7 +509,6 @@ typedef struct OutputStream { AVBSFContext *bsf_ctx; AVCodecContext *enc_ctx; - const AVCodec *enc; int64_t max_frames; AVFrame *filtered_frame; AVFrame *last_frame; diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c index e9479018e4..ac8d81c8aa 100644 --- a/fftools/ffmpeg_filter.c +++ b/fftools/ffmpeg_filter.c @@ -90,6 +90,7 @@ choose_pixel_fmt(const AVCodec *codec, enum AVPixelFormat target, static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint) { OutputStream *ost = ofilter->ost; + AVCodecContext *enc = ost->enc_ctx; const AVDictionaryEntry *strict_dict = av_dict_get(ost->encoder_opts, "strict", NULL, 0); if (strict_dict) // used by choose_pixel_fmt() and below @@ -103,14 +104,14 @@ static const char *choose_pix_fmts(OutputFilter *ofilter, AVBPrint *bprint) return av_get_pix_fmt_name(ost->enc_ctx->pix_fmt); } if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) { - return av_get_pix_fmt_name(choose_pixel_fmt(ost->enc, ost->enc_ctx->pix_fmt, + return av_get_pix_fmt_name(choose_pixel_fmt(enc->codec, enc->pix_fmt, ost->enc_ctx->strict_std_compliance)); - } else if (ost->enc->pix_fmts) { + } else if (enc->codec->pix_fmts) { const enum AVPixelFormat *p; - p = ost->enc->pix_fmts; + p = enc->codec->pix_fmts; if (ost->enc_ctx->strict_std_compliance > FF_COMPLIANCE_UNOFFICIAL) { - p = get_compliance_normal_pix_fmts(ost->enc, p); + p = get_compliance_normal_pix_fmts(enc->codec, p); } for (; *p != AV_PIX_FMT_NONE; p++) { @@ -1095,8 +1096,8 @@ int configure_filtergraph(FilterGraph *fg) for (i = 0; i < fg->nb_outputs; i++) { OutputStream *ost = fg->outputs[i]->ost; - if (ost->enc->type == AVMEDIA_TYPE_AUDIO && - !(ost->enc->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) + if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_AUDIO && + !(ost->enc_ctx->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)) av_buffersink_set_frame_size(ost->filter->filter, ost->enc_ctx->frame_size); } diff --git a/fftools/ffmpeg_hw.c b/fftools/ffmpeg_hw.c index 8acfeaf08f..88fa782470 100644 --- a/fftools/ffmpeg_hw.c +++ b/fftools/ffmpeg_hw.c @@ -461,7 +461,7 @@ int hw_device_setup_for_encode(OutputStream *ost) } for (i = 0;; i++) { - config = avcodec_get_hw_config(ost->enc, i); + config = avcodec_get_hw_config(ost->enc_ctx->codec, i); if (!config) break; @@ -472,7 +472,7 @@ int hw_device_setup_for_encode(OutputStream *ost) av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using input " "frames context (format %s) with %s encoder.\n", av_get_pix_fmt_name(ost->enc_ctx->pix_fmt), - ost->enc->name); + ost->enc_ctx->codec->name); ost->enc_ctx->hw_frames_ctx = av_buffer_ref(frames_ref); if (!ost->enc_ctx->hw_frames_ctx) return AVERROR(ENOMEM); @@ -487,7 +487,7 @@ int hw_device_setup_for_encode(OutputStream *ost) if (dev) { av_log(ost->enc_ctx, AV_LOG_VERBOSE, "Using device %s " "(type %s) with %s encoder.\n", dev->name, - av_hwdevice_get_type_name(dev->type), ost->enc->name); + av_hwdevice_get_type_name(dev->type), ost->enc_ctx->codec->name); ost->enc_ctx->hw_device_ctx = av_buffer_ref(dev->device_ref); if (!ost->enc_ctx->hw_device_ctx) return AVERROR(ENOMEM); diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 1ba7950bc1..1659cf55ba 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -1513,18 +1513,21 @@ static int get_preset_file_2(const char *preset_name, const char *codec_name, AV return ret; } -static int choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *ost) +static int choose_encoder(OptionsContext *o, AVFormatContext *s, + OutputStream *ost, const AVCodec **enc) { enum AVMediaType type = ost->st->codecpar->codec_type; char *codec_name = NULL; + *enc = NULL; + if (type == AVMEDIA_TYPE_VIDEO || type == AVMEDIA_TYPE_AUDIO || type == AVMEDIA_TYPE_SUBTITLE) { MATCH_PER_STREAM_OPT(codec_names, str, codec_name, s, ost->st); if (!codec_name) { ost->st->codecpar->codec_id = av_guess_codec(s->oformat, NULL, s->url, NULL, ost->st->codecpar->codec_type); - ost->enc = avcodec_find_encoder(ost->st->codecpar->codec_id); - if (!ost->enc) { + *enc = avcodec_find_encoder(ost->st->codecpar->codec_id); + if (!*enc) { av_log(NULL, AV_LOG_FATAL, "Automatic encoder selection failed for " "output stream #%d:%d. Default encoder for format %s (codec %s) is " "probably disabled. Please choose an encoder manually.\n", @@ -1533,8 +1536,8 @@ static int choose_encoder(OptionsContext *o, AVFormatContext *s, OutputStream *o return AVERROR_ENCODER_NOT_FOUND; } } else if (strcmp(codec_name, "copy")) { - ost->enc = find_codec_or_die(codec_name, ost->st->codecpar->codec_type, 1); - ost->st->codecpar->codec_id = ost->enc->id; + *enc = find_codec_or_die(codec_name, ost->st->codecpar->codec_type, 1); + ost->st->codecpar->codec_id = (*enc)->id; } } @@ -1560,6 +1563,7 @@ static int check_opt_bitexact(void *ctx, const AVDictionary *opts, static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, enum AVMediaType type, int source_index) { OutputStream *ost; + const AVCodec *enc; AVStream *st = avformat_new_stream(oc, NULL); int idx = oc->nb_streams - 1, ret = 0; const char *bsfs = NULL, *time_base = NULL; @@ -1583,15 +1587,15 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e ost->forced_kf_ref_pts = AV_NOPTS_VALUE; st->codecpar->codec_type = type; - ret = choose_encoder(o, oc, ost); + ret = choose_encoder(o, oc, ost, &enc); if (ret < 0) { av_log(NULL, AV_LOG_FATAL, "Error selecting an encoder for stream " "%d:%d\n", ost->file_index, ost->index); exit_program(1); } - if (ost->enc) { - ost->enc_ctx = avcodec_alloc_context3(ost->enc); + if (enc) { + ost->enc_ctx = avcodec_alloc_context3(enc); if (!ost->enc_ctx) { av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding context.\n"); exit_program(1); @@ -1606,16 +1610,18 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e if (!ost->pkt) exit_program(1); - if (ost->enc) { + if (ost->enc_ctx) { + AVCodecContext *enc = ost->enc_ctx; AVIOContext *s = NULL; char *buf = NULL, *arg = NULL, *preset = NULL; - ost->encoder_opts = filter_codec_opts(o->g->codec_opts, ost->enc->id, oc, st, ost->enc); + ost->encoder_opts = filter_codec_opts(o->g->codec_opts, enc->codec_id, + oc, st, enc->codec); MATCH_PER_STREAM_OPT(presets, str, preset, oc, st); ost->autoscale = 1; MATCH_PER_STREAM_OPT(autoscale, i, ost->autoscale, oc, st); - if (preset && (!(ret = get_preset_file_2(preset, ost->enc->name, &s)))) { + if (preset && (!(ret = get_preset_file_2(preset, enc->codec->name, &s)))) { AVBPrint bprint; av_bprint_init(&bprint, 0, AV_BPRINT_SIZE_UNLIMITED); do { @@ -1729,7 +1735,7 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e av_dict_copy(&ost->sws_dict, o->g->sws_dict, 0); av_dict_copy(&ost->swr_opts, o->g->swr_opts, 0); - if (ost->enc && av_get_exact_bits_per_sample(ost->enc->id) == 24) + if (ost->enc_ctx && av_get_exact_bits_per_sample(ost->enc_ctx->codec_id) == 24) av_dict_set(&ost->swr_opts, "output_sample_bits", "24", 0); ost->source_index = source_index; @@ -1985,7 +1991,7 @@ static OutputStream *new_video_stream(OptionsContext *o, AVFormatContext *oc, in ost->logfile_prefix ? ost->logfile_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX, nb_output_streams - 1); - if (!strcmp(ost->enc->name, "libx264")) { + if (!strcmp(ost->enc_ctx->codec->name, "libx264")) { av_dict_set(&ost->encoder_opts, "stats", logfilename, AV_DICT_DONT_OVERWRITE); } else { if (video_enc->flags & AV_CODEC_FLAG_PASS2) { @@ -3033,6 +3039,7 @@ static int open_output_file(OptionsContext *o, const char *filename) /* set the filter output constraints */ if (ost->filter) { + const AVCodec *c = ost->enc_ctx->codec; OutputFilter *f = ost->filter; switch (ost->enc_ctx->codec_type) { case AVMEDIA_TYPE_VIDEO: @@ -3042,24 +3049,24 @@ static int open_output_file(OptionsContext *o, const char *filename) if (ost->enc_ctx->pix_fmt != AV_PIX_FMT_NONE) { f->format = ost->enc_ctx->pix_fmt; } else { - f->formats = ost->enc->pix_fmts; + f->formats = c->pix_fmts; } break; case AVMEDIA_TYPE_AUDIO: if (ost->enc_ctx->sample_fmt != AV_SAMPLE_FMT_NONE) { f->format = ost->enc_ctx->sample_fmt; } else { - f->formats = ost->enc->sample_fmts; + f->formats = c->sample_fmts; } if (ost->enc_ctx->sample_rate) { f->sample_rate = ost->enc_ctx->sample_rate; } else { - f->sample_rates = ost->enc->supported_samplerates; + f->sample_rates = c->supported_samplerates; } if (ost->enc_ctx->ch_layout.nb_channels) { av_channel_layout_default(&f->ch_layout, ost->enc_ctx->ch_layout.nb_channels); - } else if (ost->enc->ch_layouts) { - f->ch_layouts = ost->enc->ch_layouts; + } else if (c->ch_layouts) { + f->ch_layouts = c->ch_layouts; } break; }