[Libav-user] Getting resulting width and height from scale filter

Philippe Gorley philippe.gorley at savoirfairelinux.com
Thu Feb 21 19:13:01 EET 2019


On 2019-02-21 8:47 a.m., Strahinja Radman wrote:
> *From: *Michael Bachmann <mailto:Michael.Bachmann at picsystems.ch>
> *Sent: *21 February 2019 13:02
> *To: *libav-user at ffmpeg.org <mailto:libav-user at ffmpeg.org>
> *Subject: *[Libav-user] Getting resulting width and height from scale filter
> 
> Is there a way to get the resulting width and height from a scale 
> filter, so i can setup the Encoding-AVCodecContext correctly (with width 
> and height)?
> 
> 
> Here's my init filter method, which gets feeded with the filter 
> description "scale=-1:'min(720,iw)'":
> 
> 
> int FFMPEGCommands::init_filters(const char *filters_descr, int width, 
> int height, int streamFrameRate) {
>      char args[512];
>      int ret = 0;
>      const AVFilter *buffersrc = avfilter_get_by_name("buffer");
>      const AVFilter *buffersink = avfilter_get_by_name("buffersink");
>      AVFilterInOut *outputs = avfilter_inout_alloc();
>      AVFilterInOut *inputs = avfilter_inout_alloc();
>      AVRational time_base = { 1, streamFrameRate };
>      //AVRational time_base = 
> fmt_ctx->streams[video_stream_index]->time_base;
>      enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_YUV420P, 
> AV_PIX_FMT_NONE };
>      filter_graph = avfilter_graph_alloc();
>      if (!outputs || !inputs || !filter_graph) {
>          ret = AVERROR(ENOMEM);
>          goto end;
>      }
>      /* buffer video source: the decoded frames from the decoder will be 
> inserted here. */
>      snprintf(args, sizeof(args),
>          "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
>          width, height, STREAM_PIX_FMT,
>          time_base.num, time_base.den,
>          0, 1);
>      ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
>          args, NULL, filter_graph);
>      if (ret < 0) {
>          av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
>          goto end;
>      }
>      /* buffer video sink: to terminate the filter chain. */
>      ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
>          NULL, NULL, filter_graph);
>      if (ret < 0) {
>          av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
>          goto end;
>      }
>      ret = av_opt_set_int_list(buffersink_ctx, "pix_fmts", pix_fmts,
>          AV_PIX_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
>      if (ret < 0) {
>          av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
>          goto end;
>      }
>      /*
>       * Set the endpoints for the filter graph. The filter_graph will
>       * be linked to the graph described by filters_descr.
>       */
>       /*
>        * The buffer source output must be connected to the input pad of
>        * the first filter described by filters_descr; since the first
>        * filter input label is not specified, it is set to "in" by
>        * default.
>        */
>      outputs->name = av_strdup("in");
>      outputs->filter_ctx = buffersrc_ctx;
>      outputs->pad_idx = 0;
>      outputs->next = NULL;
>      /*
>       * The buffer sink input must be connected to the output pad of
>       * the last filter described by filters_descr; since the last
>       * filter output label is not specified, it is set to "out" by
>       * default.
>       */
>      inputs->name = av_strdup("out");
>      inputs->filter_ctx = buffersink_ctx; 
>      inputs->pad_idx = 0;
>      inputs->next = NULL;
>      if ((ret = avfilter_graph_parse_ptr(filter_graph, filters_descr,
>          &inputs, &outputs, NULL)) < 0)
>          goto end;
>      if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
>          goto end;
> end:
>      avfilter_inout_free(&inputs);
>      avfilter_inout_free(&outputs);
>      return ret;
> }
> 
> 
> The info you are seeking should be available here 
> filter_ctx.buffersink_ctx->inputs[0]->w  for width and 
> filter_ctx.buffersink_ctx->inputs[0]->h for height.

 From avfilter.h: "Applications must not normally access the link 
structure directly. Use the buffersrc and buffersink API instead."

The buffersink API (described in buffersink.h) shows that 
av_buffersink_get_w and av_buffersink_get_h can be used by passing in 
buffersink_ctx.

> 
> 
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/libav-user
> 
> To unsubscribe, visit link above, or email
> libav-user-request at ffmpeg.org with subject "unsubscribe".
> 

-- 
Philippe Gorley
Free Software Consultant | Montréal, Qc
Savoir-faire Linux


More information about the Libav-user mailing list