mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2025-01-08 13:22:53 +02:00
vf_pad: switch to filter_frame
Based on patch by Anton Khirnov Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
This commit is contained in:
parent
a0958949eb
commit
6be0df5080
@ -88,7 +88,6 @@ typedef struct {
|
||||
uint8_t rgba_color[4]; ///< color for the padding area
|
||||
FFDrawContext draw;
|
||||
FFDrawColor color;
|
||||
int needs_copy;
|
||||
} PadContext;
|
||||
|
||||
static av_cold int init(AVFilterContext *ctx, const char *args)
|
||||
@ -263,129 +262,84 @@ static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, i
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
|
||||
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *in)
|
||||
{
|
||||
PadContext *pad = inlink->dst->priv;
|
||||
AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
|
||||
AVFilterBufferRef *for_next_filter;
|
||||
int plane, ret = 0;
|
||||
AVFilterBufferRef *out = avfilter_ref_buffer(in, ~0);
|
||||
int plane, needs_copy;
|
||||
|
||||
if (!outpicref)
|
||||
if (!out) {
|
||||
avfilter_unref_bufferp(&in);
|
||||
return AVERROR(ENOMEM);
|
||||
}
|
||||
|
||||
for (plane = 0; plane < 4 && outpicref->data[plane] && pad->draw.pixelstep[plane]; plane++) {
|
||||
for (plane = 0; plane < 4 && out->data[plane] && pad->draw.pixelstep[plane]; plane++) {
|
||||
int hsub = pad->draw.hsub[plane];
|
||||
int vsub = pad->draw.vsub[plane];
|
||||
|
||||
av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
|
||||
av_assert0(out->buf->w > 0 && out->buf->h > 0);
|
||||
|
||||
if(outpicref->format != outpicref->buf->format) //unsupported currently
|
||||
if (out->format != out->buf->format) //unsupported currently
|
||||
break;
|
||||
|
||||
outpicref->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane]
|
||||
+ (pad->y >> vsub) * outpicref->linesize[plane];
|
||||
out->data[plane] -= (pad->x >> hsub) * pad->draw.pixelstep[plane] +
|
||||
(pad->y >> vsub) * out->linesize[plane];
|
||||
|
||||
if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
|
||||
|| does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
|
||||
|| does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
|
||||
|| does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
|
||||
)
|
||||
if (does_clip(pad, out, plane, hsub, vsub, 0, 0) ||
|
||||
does_clip(pad, out, plane, hsub, vsub, 0, pad->h - 1) ||
|
||||
does_clip(pad, out, plane, hsub, vsub, pad->w - 1, 0) ||
|
||||
does_clip(pad, out, plane, hsub, vsub, pad->w - 1, pad->h - 1))
|
||||
break;
|
||||
}
|
||||
pad->needs_copy= plane < 4 && outpicref->data[plane] || !(outpicref->perms & AV_PERM_WRITE);
|
||||
if(pad->needs_copy){
|
||||
needs_copy = plane < 4 && out->data[plane] || !(out->perms & AV_PERM_WRITE);
|
||||
if (needs_copy) {
|
||||
av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
|
||||
avfilter_unref_buffer(outpicref);
|
||||
outpicref = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
|
||||
FFMAX(inlink->w, pad->w),
|
||||
FFMAX(inlink->h, pad->h));
|
||||
if (!outpicref)
|
||||
avfilter_unref_buffer(out);
|
||||
out = ff_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
|
||||
FFMAX(inlink->w, pad->w),
|
||||
FFMAX(inlink->h, pad->h));
|
||||
if (!out) {
|
||||
avfilter_unref_bufferp(&in);
|
||||
return AVERROR(ENOMEM);
|
||||
}
|
||||
|
||||
avfilter_copy_buffer_ref_props(outpicref, inpicref);
|
||||
avfilter_copy_buffer_ref_props(out, in);
|
||||
}
|
||||
|
||||
outpicref->video->w = pad->w;
|
||||
outpicref->video->h = pad->h;
|
||||
out->video->w = pad->w;
|
||||
out->video->h = pad->h;
|
||||
|
||||
for_next_filter = avfilter_ref_buffer(outpicref, ~0);
|
||||
if (!for_next_filter) {
|
||||
ret = AVERROR(ENOMEM);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
ret = ff_start_frame(inlink->dst->outputs[0], for_next_filter);
|
||||
if (ret < 0)
|
||||
goto fail;
|
||||
|
||||
inlink->dst->outputs[0]->out_buf = outpicref;
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
avfilter_unref_bufferp(&outpicref);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
|
||||
{
|
||||
PadContext *pad = link->dst->priv;
|
||||
int bar_y, bar_h = 0, ret = 0;
|
||||
|
||||
if (slice_dir * before_slice == 1 && y == pad->y) {
|
||||
/* top bar */
|
||||
bar_y = 0;
|
||||
bar_h = pad->y;
|
||||
} else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
|
||||
/* bottom bar */
|
||||
bar_y = pad->y + pad->in_h;
|
||||
bar_h = pad->h - pad->in_h - pad->y;
|
||||
}
|
||||
|
||||
if (bar_h) {
|
||||
/* top bar */
|
||||
if (pad->y) {
|
||||
ff_fill_rectangle(&pad->draw, &pad->color,
|
||||
link->dst->outputs[0]->out_buf->data,
|
||||
link->dst->outputs[0]->out_buf->linesize,
|
||||
0, bar_y, pad->w, bar_h);
|
||||
ret = ff_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
|
||||
out->data, out->linesize,
|
||||
0, 0, pad->w, pad->y);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
|
||||
{
|
||||
PadContext *pad = link->dst->priv;
|
||||
AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
|
||||
AVFilterBufferRef *inpic = link->cur_buf;
|
||||
int ret;
|
||||
|
||||
y += pad->y;
|
||||
|
||||
y = ff_draw_round_to_sub(&pad->draw, 1, -1, y);
|
||||
h = ff_draw_round_to_sub(&pad->draw, 1, -1, h);
|
||||
|
||||
if (!h)
|
||||
return 0;
|
||||
draw_send_bar_slice(link, y, h, slice_dir, 1);
|
||||
/* bottom bar */
|
||||
if (pad->h > pad->y + pad->in_h) {
|
||||
ff_fill_rectangle(&pad->draw, &pad->color,
|
||||
out->data, out->linesize,
|
||||
0, pad->y + pad->in_h, pad->w, pad->h - pad->y - pad->in_h);
|
||||
}
|
||||
|
||||
/* left border */
|
||||
ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
|
||||
0, y, pad->x, h);
|
||||
ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
|
||||
0, pad->y, pad->x, in->video->h);
|
||||
|
||||
if(pad->needs_copy){
|
||||
if (needs_copy) {
|
||||
ff_copy_rectangle2(&pad->draw,
|
||||
outpic->data, outpic->linesize,
|
||||
inpic ->data, inpic ->linesize,
|
||||
pad->x, y, 0, y - pad->y, inpic->video->w, h);
|
||||
out->data, out->linesize, in->data, in->linesize,
|
||||
pad->x, pad->y, 0, 0, in->video->w, in->video->h);
|
||||
}
|
||||
|
||||
/* right border */
|
||||
ff_fill_rectangle(&pad->draw, &pad->color, outpic->data, outpic->linesize,
|
||||
pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
|
||||
ret = ff_draw_slice(link->dst->outputs[0], y, h, slice_dir);
|
||||
if (ret < 0)
|
||||
return ret;
|
||||
ff_fill_rectangle(&pad->draw, &pad->color, out->data, out->linesize,
|
||||
pad->x + pad->in_w, pad->y, pad->w - pad->x - pad->in_w,
|
||||
in->video->h);
|
||||
|
||||
return draw_send_bar_slice(link, y, h, slice_dir, -1);
|
||||
avfilter_unref_bufferp(&in);
|
||||
return ff_filter_frame(inlink->dst->outputs[0], out);
|
||||
}
|
||||
|
||||
static const AVFilterPad avfilter_vf_pad_inputs[] = {
|
||||
@ -394,8 +348,7 @@ static const AVFilterPad avfilter_vf_pad_inputs[] = {
|
||||
.type = AVMEDIA_TYPE_VIDEO,
|
||||
.config_props = config_input,
|
||||
.get_video_buffer = get_video_buffer,
|
||||
.start_frame = start_frame,
|
||||
.draw_slice = draw_slice,
|
||||
.filter_frame = filter_frame,
|
||||
},
|
||||
{ NULL }
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user