You've already forked FFmpeg
mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-08-10 06:10:52 +02:00
avfilter/vf_zoompan: rewrite so it doesn't cache all output frames
Signed-off-by: Paul B Mahol <onemda@gmail.com>
This commit is contained in:
@@ -18,6 +18,7 @@
|
|||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "libavutil/avassert.h"
|
||||||
#include "libavutil/eval.h"
|
#include "libavutil/eval.h"
|
||||||
#include "libavutil/opt.h"
|
#include "libavutil/opt.h"
|
||||||
#include "libavutil/pixdesc.h"
|
#include "libavutil/pixdesc.h"
|
||||||
@@ -85,6 +86,12 @@ typedef struct ZPcontext {
|
|||||||
int prev_nb_frames;
|
int prev_nb_frames;
|
||||||
struct SwsContext *sws;
|
struct SwsContext *sws;
|
||||||
int64_t frame_count;
|
int64_t frame_count;
|
||||||
|
const AVPixFmtDescriptor *desc;
|
||||||
|
AVFrame *in;
|
||||||
|
double var_values[VARS_NB];
|
||||||
|
int nb_frames;
|
||||||
|
int current_frame;
|
||||||
|
int finished;
|
||||||
} ZPContext;
|
} ZPContext;
|
||||||
|
|
||||||
#define OFFSET(x) offsetof(ZPContext, x)
|
#define OFFSET(x) offsetof(ZPContext, x)
|
||||||
@@ -116,98 +123,68 @@ static int config_output(AVFilterLink *outlink)
|
|||||||
|
|
||||||
outlink->w = s->w;
|
outlink->w = s->w;
|
||||||
outlink->h = s->h;
|
outlink->h = s->h;
|
||||||
|
s->desc = av_pix_fmt_desc_get(outlink->format);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
static int output_single_frame(AVFilterContext *ctx, AVFrame *in, double *var_values, int i,
|
||||||
|
double *zoom, double *dx, double *dy)
|
||||||
{
|
{
|
||||||
AVFilterContext *ctx = inlink->dst;
|
|
||||||
AVFilterLink *outlink = ctx->outputs[0];
|
|
||||||
ZPContext *s = ctx->priv;
|
ZPContext *s = ctx->priv;
|
||||||
double var_values[VARS_NB], nb_frames, zoom, dx, dy;
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(in->format);
|
int64_t pts = av_rescale_q(in->pts, ctx->inputs[0]->time_base,
|
||||||
AVFrame *out = NULL;
|
|
||||||
int i, k, x, y, w, h, ret = 0;
|
|
||||||
|
|
||||||
var_values[VAR_IN_W] = var_values[VAR_IW] = in->width;
|
|
||||||
var_values[VAR_IN_H] = var_values[VAR_IH] = in->height;
|
|
||||||
var_values[VAR_OUT_W] = var_values[VAR_OW] = s->w;
|
|
||||||
var_values[VAR_OUT_H] = var_values[VAR_OH] = s->h;
|
|
||||||
var_values[VAR_IN] = inlink->frame_count + 1;
|
|
||||||
var_values[VAR_ON] = outlink->frame_count + 1;
|
|
||||||
var_values[VAR_PX] = s->x;
|
|
||||||
var_values[VAR_PY] = s->y;
|
|
||||||
var_values[VAR_X] = 0;
|
|
||||||
var_values[VAR_Y] = 0;
|
|
||||||
var_values[VAR_PZOOM] = s->prev_zoom;
|
|
||||||
var_values[VAR_ZOOM] = 1;
|
|
||||||
var_values[VAR_PDURATION] = s->prev_nb_frames;
|
|
||||||
var_values[VAR_A] = (double) in->width / in->height;
|
|
||||||
var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
|
|
||||||
(double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
|
|
||||||
var_values[VAR_DAR] = var_values[VAR_A] * var_values[VAR_SAR];
|
|
||||||
var_values[VAR_HSUB] = 1 << desc->log2_chroma_w;
|
|
||||||
var_values[VAR_VSUB] = 1 << desc->log2_chroma_h;
|
|
||||||
|
|
||||||
if ((ret = av_expr_parse_and_eval(&nb_frames, s->duration_expr_str,
|
|
||||||
var_names, var_values,
|
|
||||||
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
var_values[VAR_DURATION] = nb_frames;
|
|
||||||
for (i = 0; i < nb_frames; i++) {
|
|
||||||
int px[4];
|
|
||||||
int py[4];
|
|
||||||
uint8_t *input[4];
|
|
||||||
int64_t pts = av_rescale_q(in->pts, inlink->time_base,
|
|
||||||
outlink->time_base) + s->frame_count;
|
outlink->time_base) + s->frame_count;
|
||||||
|
int k, x, y, w, h, ret = 0;
|
||||||
|
uint8_t *input[4];
|
||||||
|
int px[4], py[4];
|
||||||
|
AVFrame *out;
|
||||||
|
|
||||||
var_values[VAR_TIME] = pts * av_q2d(outlink->time_base);
|
var_values[VAR_TIME] = pts * av_q2d(outlink->time_base);
|
||||||
var_values[VAR_FRAME] = i;
|
var_values[VAR_FRAME] = i;
|
||||||
var_values[VAR_ON] = outlink->frame_count + 1;
|
var_values[VAR_ON] = outlink->frame_count + 1;
|
||||||
if ((ret = av_expr_parse_and_eval(&zoom, s->zoom_expr_str,
|
if ((ret = av_expr_parse_and_eval(zoom, s->zoom_expr_str,
|
||||||
var_names, var_values,
|
var_names, var_values,
|
||||||
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
|
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
|
||||||
goto fail;
|
return ret;
|
||||||
|
|
||||||
zoom = av_clipd(zoom, 1, 10);
|
*zoom = av_clipd(*zoom, 1, 10);
|
||||||
var_values[VAR_ZOOM] = zoom;
|
var_values[VAR_ZOOM] = *zoom;
|
||||||
w = in->width * (1.0 / zoom);
|
w = in->width * (1.0 / *zoom);
|
||||||
h = in->height * (1.0 / zoom);
|
h = in->height * (1.0 / *zoom);
|
||||||
|
|
||||||
if ((ret = av_expr_parse_and_eval(&dx, s->x_expr_str,
|
if ((ret = av_expr_parse_and_eval(dx, s->x_expr_str,
|
||||||
var_names, var_values,
|
var_names, var_values,
|
||||||
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
|
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
|
||||||
goto fail;
|
return ret;
|
||||||
x = dx = av_clipd(dx, 0, FFMAX(in->width - w, 0));
|
x = *dx = av_clipd(*dx, 0, FFMAX(in->width - w, 0));
|
||||||
var_values[VAR_X] = dx;
|
var_values[VAR_X] = *dx;
|
||||||
x &= ~((1 << desc->log2_chroma_w) - 1);
|
x &= ~((1 << s->desc->log2_chroma_w) - 1);
|
||||||
|
|
||||||
if ((ret = av_expr_parse_and_eval(&dy, s->y_expr_str,
|
if ((ret = av_expr_parse_and_eval(dy, s->y_expr_str,
|
||||||
var_names, var_values,
|
var_names, var_values,
|
||||||
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
|
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0)
|
||||||
goto fail;
|
return ret;
|
||||||
y = dy = av_clipd(dy, 0, FFMAX(in->height - h, 0));
|
y = *dy = av_clipd(*dy, 0, FFMAX(in->height - h, 0));
|
||||||
var_values[VAR_Y] = dy;
|
var_values[VAR_Y] = *dy;
|
||||||
y &= ~((1 << desc->log2_chroma_h) - 1);
|
y &= ~((1 << s->desc->log2_chroma_h) - 1);
|
||||||
|
|
||||||
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
|
||||||
if (!out) {
|
if (!out) {
|
||||||
ret = AVERROR(ENOMEM);
|
ret = AVERROR(ENOMEM);
|
||||||
goto fail;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
px[1] = px[2] = FF_CEIL_RSHIFT(x, desc->log2_chroma_w);
|
px[1] = px[2] = FF_CEIL_RSHIFT(x, s->desc->log2_chroma_w);
|
||||||
px[0] = px[3] = x;
|
px[0] = px[3] = x;
|
||||||
|
|
||||||
py[1] = py[2] = FF_CEIL_RSHIFT(y, desc->log2_chroma_h);
|
py[1] = py[2] = FF_CEIL_RSHIFT(y, s->desc->log2_chroma_h);
|
||||||
py[0] = py[3] = y;
|
py[0] = py[3] = y;
|
||||||
|
|
||||||
s->sws = sws_alloc_context();
|
s->sws = sws_alloc_context();
|
||||||
if (!s->sws) {
|
if (!s->sws) {
|
||||||
ret = AVERROR(ENOMEM);
|
ret = AVERROR(ENOMEM);
|
||||||
goto fail;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (k = 0; in->data[k]; k++)
|
for (k = 0; in->data[k]; k++)
|
||||||
@@ -222,7 +199,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|||||||
av_opt_set_int(s->sws, "sws_flags", SWS_BICUBIC, 0);
|
av_opt_set_int(s->sws, "sws_flags", SWS_BICUBIC, 0);
|
||||||
|
|
||||||
if ((ret = sws_init_context(s->sws, NULL, NULL)) < 0)
|
if ((ret = sws_init_context(s->sws, NULL, NULL)) < 0)
|
||||||
goto fail;
|
return ret;
|
||||||
|
|
||||||
sws_scale(s->sws, (const uint8_t *const *)&input, in->linesize, 0, h, out->data, out->linesize);
|
sws_scale(s->sws, (const uint8_t *const *)&input, in->linesize, 0, h, out->data, out->linesize);
|
||||||
|
|
||||||
@@ -230,27 +207,100 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
|||||||
s->frame_count++;
|
s->frame_count++;
|
||||||
|
|
||||||
ret = ff_filter_frame(outlink, out);
|
ret = ff_filter_frame(outlink, out);
|
||||||
out = NULL;
|
|
||||||
if (ret < 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
sws_freeContext(s->sws);
|
sws_freeContext(s->sws);
|
||||||
s->sws = NULL;
|
s->sws = NULL;
|
||||||
|
s->current_frame++;
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
|
||||||
|
{
|
||||||
|
AVFilterContext *ctx = inlink->dst;
|
||||||
|
AVFilterLink *outlink = ctx->outputs[0];
|
||||||
|
ZPContext *s = ctx->priv;
|
||||||
|
double nb_frames;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (s->in) {
|
||||||
|
av_frame_free(&in);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->finished = 0;
|
||||||
|
s->var_values[VAR_IN_W] = s->var_values[VAR_IW] = in->width;
|
||||||
|
s->var_values[VAR_IN_H] = s->var_values[VAR_IH] = in->height;
|
||||||
|
s->var_values[VAR_OUT_W] = s->var_values[VAR_OW] = s->w;
|
||||||
|
s->var_values[VAR_OUT_H] = s->var_values[VAR_OH] = s->h;
|
||||||
|
s->var_values[VAR_IN] = inlink->frame_count + 1;
|
||||||
|
s->var_values[VAR_ON] = outlink->frame_count + 1;
|
||||||
|
s->var_values[VAR_PX] = s->x;
|
||||||
|
s->var_values[VAR_PY] = s->y;
|
||||||
|
s->var_values[VAR_X] = 0;
|
||||||
|
s->var_values[VAR_Y] = 0;
|
||||||
|
s->var_values[VAR_PZOOM] = s->prev_zoom;
|
||||||
|
s->var_values[VAR_ZOOM] = 1;
|
||||||
|
s->var_values[VAR_PDURATION] = s->prev_nb_frames;
|
||||||
|
s->var_values[VAR_A] = (double) in->width / in->height;
|
||||||
|
s->var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ?
|
||||||
|
(double) inlink->sample_aspect_ratio.num / inlink->sample_aspect_ratio.den : 1;
|
||||||
|
s->var_values[VAR_DAR] = s->var_values[VAR_A] * s->var_values[VAR_SAR];
|
||||||
|
s->var_values[VAR_HSUB] = 1 << s->desc->log2_chroma_w;
|
||||||
|
s->var_values[VAR_VSUB] = 1 << s->desc->log2_chroma_h;
|
||||||
|
|
||||||
|
if ((ret = av_expr_parse_and_eval(&nb_frames, s->duration_expr_str,
|
||||||
|
var_names, s->var_values,
|
||||||
|
NULL, NULL, NULL, NULL, NULL, 0, ctx)) < 0) {
|
||||||
|
av_frame_free(&in);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
s->var_values[VAR_DURATION] = s->nb_frames = nb_frames;
|
||||||
|
s->in = in;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int request_frame(AVFilterLink *outlink)
|
||||||
|
{
|
||||||
|
AVFilterContext *ctx = outlink->src;
|
||||||
|
ZPContext *s = ctx->priv;
|
||||||
|
AVFrame *in = s->in;
|
||||||
|
double zoom, dx, dy;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (in) {
|
||||||
|
ret = output_single_frame(ctx, in, s->var_values, s->current_frame,
|
||||||
|
&zoom, &dx, &dy);
|
||||||
|
if (ret < 0)
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s->current_frame >= s->nb_frames) {
|
||||||
s->x = dx;
|
s->x = dx;
|
||||||
s->y = dy;
|
s->y = dy;
|
||||||
s->prev_zoom = zoom;
|
s->prev_zoom = zoom;
|
||||||
s->prev_nb_frames = nb_frames;
|
s->prev_nb_frames = s->nb_frames;
|
||||||
|
s->nb_frames = 0;
|
||||||
|
s->current_frame = 0;
|
||||||
|
av_frame_free(&s->in);
|
||||||
|
ret = 0;
|
||||||
|
s->finished = 1;
|
||||||
|
ret = ff_request_frame(ctx->inputs[0]);
|
||||||
|
}
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
sws_freeContext(s->sws);
|
sws_freeContext(s->sws);
|
||||||
s->sws = NULL;
|
s->sws = NULL;
|
||||||
av_frame_free(&out);
|
|
||||||
av_frame_free(&in);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int poll_frame(AVFilterLink *link)
|
||||||
|
{
|
||||||
|
ZPContext *s = link->src->priv;
|
||||||
|
return s->nb_frames - s->current_frame;
|
||||||
|
}
|
||||||
|
|
||||||
static int query_formats(AVFilterContext *ctx)
|
static int query_formats(AVFilterContext *ctx)
|
||||||
{
|
{
|
||||||
static const enum AVPixelFormat pix_fmts[] = {
|
static const enum AVPixelFormat pix_fmts[] = {
|
||||||
@@ -295,6 +345,8 @@ static const AVFilterPad outputs[] = {
|
|||||||
.name = "default",
|
.name = "default",
|
||||||
.type = AVMEDIA_TYPE_VIDEO,
|
.type = AVMEDIA_TYPE_VIDEO,
|
||||||
.config_props = config_output,
|
.config_props = config_output,
|
||||||
|
.poll_frame = poll_frame,
|
||||||
|
.request_frame = request_frame,
|
||||||
},
|
},
|
||||||
{ NULL }
|
{ NULL }
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user