X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavfilter%2Fbuffersrc.c;h=9294811d365ea4f9d9182e655994f727d843dd28;hb=0ba844a053da5e409a36a8159adb16aea5b5dd71;hp=3920b048db928fafca5569795b8461e43b8d9ebc;hpb=c63da6e91630b72fa5916314910cb35e82331f70;p=ffmpeg diff --git a/libavfilter/buffersrc.c b/libavfilter/buffersrc.c index 3920b048db9..9294811d365 100644 --- a/libavfilter/buffersrc.c +++ b/libavfilter/buffersrc.c @@ -54,6 +54,8 @@ typedef struct BufferSourceContext { AVRational pixel_aspect; char *sws_param; + AVBufferRef *hw_frames_ctx; + /* audio only */ int sample_rate; enum AVSampleFormat sample_fmt; @@ -61,6 +63,7 @@ typedef struct BufferSourceContext { uint64_t channel_layout; char *channel_layout_str; + int got_format_from_params; int eof; } BufferSourceContext; @@ -76,6 +79,62 @@ typedef struct BufferSourceContext { return AVERROR(EINVAL);\ } +AVBufferSrcParameters *av_buffersrc_parameters_alloc(void) +{ + AVBufferSrcParameters *par = av_mallocz(sizeof(*par)); + if (!par) + return NULL; + + par->format = -1; + + return par; +} + +int av_buffersrc_parameters_set(AVFilterContext *ctx, AVBufferSrcParameters *param) +{ + BufferSourceContext *s = ctx->priv; + + if (param->time_base.num > 0 && param->time_base.den > 0) + s->time_base = param->time_base; + + switch (ctx->filter->outputs[0].type) { + case AVMEDIA_TYPE_VIDEO: + if (param->format != AV_PIX_FMT_NONE) { + s->got_format_from_params = 1; + s->pix_fmt = param->format; + } + if (param->width > 0) + s->w = param->width; + if (param->height > 0) + s->h = param->height; + if (param->sample_aspect_ratio.num > 0 && param->sample_aspect_ratio.den > 0) + s->pixel_aspect = param->sample_aspect_ratio; + if (param->frame_rate.num > 0 && param->frame_rate.den > 0) + s->frame_rate = param->frame_rate; + if (param->hw_frames_ctx) { + av_buffer_unref(&s->hw_frames_ctx); + s->hw_frames_ctx = av_buffer_ref(param->hw_frames_ctx); + if (!s->hw_frames_ctx) + return AVERROR(ENOMEM); + } + break; + case AVMEDIA_TYPE_AUDIO: + if (param->format != AV_SAMPLE_FMT_NONE) { + s->got_format_from_params = 1; + s->sample_fmt = param->format; + } + if (param->sample_rate > 0) + s->sample_rate = param->sample_rate; + if (param->channel_layout) + s->channel_layout = param->channel_layout; + break; + default: + return AVERROR_BUG; + } + + return 0; +} + int attribute_align_arg av_buffersrc_write_frame(AVFilterContext *ctx, const AVFrame *frame) { return av_buffersrc_add_frame_flags(ctx, (AVFrame *)frame, @@ -187,7 +246,8 @@ static av_cold int init_video(AVFilterContext *ctx) { BufferSourceContext *c = ctx->priv; - if (c->pix_fmt == AV_PIX_FMT_NONE || !c->w || !c->h || av_q2d(c->time_base) <= 0) { + if (!(c->pix_fmt != AV_PIX_FMT_NONE || c->got_format_from_params) || !c->w || !c->h || + av_q2d(c->time_base) <= 0) { av_log(ctx, AV_LOG_ERROR, "Invalid parameters provided.\n"); return AVERROR(EINVAL); } @@ -225,8 +285,8 @@ static const AVOption buffer_options[] = { { "sar_num", "deprecated, do not use", OFFSET(pixel_aspect.num), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V }, { "sar_den", "deprecated, do not use", OFFSET(pixel_aspect.den), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, V }, #endif - { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V }, - { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 1 }, 0, DBL_MAX, V }, + { "sar", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V }, + { "pixel_aspect", "sample aspect ratio", OFFSET(pixel_aspect), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V }, { "time_base", NULL, OFFSET(time_base), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V }, { "frame_rate", NULL, OFFSET(frame_rate), AV_OPT_TYPE_RATIONAL, { .dbl = 0 }, 0, DBL_MAX, V }, { "sws_param", NULL, OFFSET(sws_param), AV_OPT_TYPE_STRING, .flags = V }, @@ -251,7 +311,7 @@ static av_cold int init_audio(AVFilterContext *ctx) BufferSourceContext *s = ctx->priv; int ret = 0; - if (s->sample_fmt == AV_SAMPLE_FMT_NONE) { + if (!(s->sample_fmt != AV_SAMPLE_FMT_NONE || s->got_format_from_params)) { av_log(ctx, AV_LOG_ERROR, "Sample format was not set or was invalid\n"); return AVERROR(EINVAL); } @@ -305,6 +365,7 @@ static av_cold void uninit(AVFilterContext *ctx) av_fifo_generic_read(s->fifo, &frame, sizeof(frame), NULL); av_frame_free(&frame); } + av_buffer_unref(&s->hw_frames_ctx); av_fifo_freep(&s->fifo); } @@ -352,6 +413,12 @@ static int config_props(AVFilterLink *link) link->w = c->w; link->h = c->h; link->sample_aspect_ratio = c->pixel_aspect; + + if (c->hw_frames_ctx) { + link->hw_frames_ctx = av_buffer_ref(c->hw_frames_ctx); + if (!link->hw_frames_ctx) + return AVERROR(ENOMEM); + } break; case AVMEDIA_TYPE_AUDIO: if (!c->channel_layout) @@ -370,6 +437,7 @@ static int request_frame(AVFilterLink *link) { BufferSourceContext *c = link->src->priv; AVFrame *frame; + int ret; if (!av_fifo_size(c->fifo)) { if (c->eof) @@ -379,7 +447,9 @@ static int request_frame(AVFilterLink *link) } av_fifo_generic_read(c->fifo, &frame, sizeof(frame), NULL); - return ff_filter_frame(link, frame); + ret = ff_filter_frame(link, frame); + + return ret; } static int poll_frame(AVFilterLink *link)