From d0f767f81f7b6c4e9a3d29a9fd622ab519461213 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Thu, 25 Aug 2022 10:01:19 +0200 Subject: [PATCH] fftools/ffmpeg: drop OutputStream.enc 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 | 54 +++++++++++++++++++++++------------------ 5 files changed, 55 insertions(+), 46 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index ff74534ad0..0ca07873b2 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); @@ -2869,13 +2869,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); @@ -2884,7 +2885,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); } @@ -3011,9 +3012,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) { @@ -3154,7 +3155,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; @@ -3183,7 +3184,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); @@ -3210,8 +3211,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); @@ -3423,7 +3424,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; } @@ -3434,7 +3435,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 = "?"; @@ -3830,7 +3831,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 c461e93ff9..937a65b8e8 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -515,7 +515,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 8128dcf9fb..296751e19d 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) { @@ -2869,6 +2875,7 @@ static void of_add_metadata(AVFormatContext *oc, const OptionsContext *o) } static void set_channel_layout(OutputFilter *f, OutputStream *ost) { + const AVCodec *c = ost->enc_ctx->codec; int i, err; if (ost->enc_ctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) { @@ -2880,7 +2887,7 @@ static void set_channel_layout(OutputFilter *f, OutputStream *ost) } /* Requested layout is of order UNSPEC */ - if (!ost->enc->ch_layouts) { + if (!c->ch_layouts) { /* Use the default native layout for the requested amount of channels when the encoder doesn't have a list of supported layouts */ av_channel_layout_default(&f->ch_layout, ost->enc_ctx->ch_layout.nb_channels); @@ -2888,13 +2895,13 @@ static void set_channel_layout(OutputFilter *f, OutputStream *ost) } /* Encoder has a list of supported layouts. Pick the first layout in it with the same amount of channels as the requested layout */ - for (i = 0; ost->enc->ch_layouts[i].nb_channels; i++) { - if (ost->enc->ch_layouts[i].nb_channels == ost->enc_ctx->ch_layout.nb_channels) + for (i = 0; c->ch_layouts[i].nb_channels; i++) { + if (c->ch_layouts[i].nb_channels == ost->enc_ctx->ch_layout.nb_channels) break; } - if (ost->enc->ch_layouts[i].nb_channels) { + if (c->ch_layouts[i].nb_channels) { /* Use it if one is found */ - err = av_channel_layout_copy(&f->ch_layout, &ost->enc->ch_layouts[i]); + err = av_channel_layout_copy(&f->ch_layout, &c->ch_layouts[i]); if (err < 0) exit_program(1); return; @@ -3069,6 +3076,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: @@ -3078,24 +3086,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) { set_channel_layout(f, ost); - } 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; }