-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvfilter.c
170 lines (137 loc) · 5.5 KB
/
vfilter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "vfilter.h"
typedef struct {
enum PixelFormat pix_fmt;
} FFSinkContext;
static int ffsink_init(AVFilterContext *ctx, const char *args, void *opaque)
{
FFSinkContext *priv = ctx->priv;
if (!opaque)
return AVERROR(EINVAL);
*priv = *(FFSinkContext *)opaque;
return 0;
}
static void null_end_frame(AVFilterLink *inlink) { }
static int ffsink_query_formats(AVFilterContext *ctx)
{
FFSinkContext *priv = ctx->priv;
enum PixelFormat pix_fmts[] = { priv->pix_fmt, PIX_FMT_NONE };
avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
return 0;
}
AVFilter ffsink = {
.name = "ffsink",
.priv_size = sizeof(FFSinkContext),
.init = ffsink_init,
.query_formats = ffsink_query_formats,
.inputs = (AVFilterPad[]) {{ .name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.end_frame = null_end_frame,
.min_perms = AV_PERM_READ, },
{ .name = NULL }},
.outputs = (AVFilterPad[]) {{ .name = NULL }},
};
int get_filtered_video_frame(AVFilterContext *ctx, AVFrame *frame,
AVFilterBufferRef **picref_ptr, AVRational *tb)
{
int ret;
AVFilterBufferRef *picref;
if ((ret = avfilter_request_frame(ctx->inputs[0])) < 0)
return ret;
if (!(picref = ctx->inputs[0]->cur_buf))
return AVERROR(ENOENT);
*picref_ptr = picref;
ctx->inputs[0]->cur_buf = NULL;
*tb = ctx->inputs[0]->time_base;
memcpy(frame->data, picref->data, sizeof(frame->data));
memcpy(frame->linesize, picref->linesize, sizeof(frame->linesize));
frame->interlaced_frame = picref->video->interlaced;
frame->top_field_first = picref->video->top_field_first;
return 1;
}
int configure_filters(AVFilterGraph *graph, char *vfilters, AVInputStream *ist, AVOutputStream *ost, struct CodecSettings *cs)
{
AVFilterContext *last_filter, *filter;
/** filter graph containing all filters including input & output */
AVCodecContext *codec = ost->st?ost->st->codec:NULL;
AVCodecContext *icodec = ist->st->codec;
FFSinkContext ffsink_ctx = { .pix_fmt = 0 };
char args[255];
int ret;
if (codec)
ffsink_ctx.pix_fmt = codec->pix_fmt ;
graph = av_mallocz(sizeof(AVFilterGraph));
if ((ret = avfilter_open(&ist->input_video_filter, avfilter_get_by_name("buffer"), "src")) < 0)
return ret;
if (( ret = avfilter_open(&ist->out_video_filter, &ffsink, "out")) < 0)
return ret;
snprintf(args, 255, "%d:%d:%d:%d:%d", ist->st->codec->width,
ist->st->codec->height, ist->st->codec->pix_fmt,
ist->st->time_base.num, ist->st->time_base.den);
if ((ret = avfilter_init_filter(ist->input_video_filter, args, NULL)) < 0)
return ret;
if (( ret = avfilter_init_filter(ist->out_video_filter, NULL, &ffsink_ctx)) < 0)
return ret;
/* add input and output filters to the overall graph */
avfilter_graph_add_filter(graph, ist->input_video_filter);
avfilter_graph_add_filter(graph, ist->out_video_filter);
last_filter = ist->input_video_filter;
if (ost->video_crop) {
snprintf(args, 255, "%d:%d:%d:%d",
icodec->width-(ost->leftBand+ost->frame_rightBand),
icodec->height-(ost->topBand+ost->frame_bottomBand),
ost->leftBand, ost->topBand
);
if ((ret = avfilter_open(&filter, avfilter_get_by_name("crop"), NULL)) < 0)
return ret;
if ((ret = avfilter_init_filter(filter, args, NULL)) < 0)
return ret;
if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
return ret;
last_filter = filter;
avfilter_graph_add_filter(graph, last_filter);
}
if((cs->w !=
icodec->width - (ost->frame_leftBand + ost->frame_rightBand)) ||
(cs->h != icodec->height - (ost->frame_topBand + ost->frame_bottomBand))) {
snprintf(args, 255, "%d:%d:flags=0x%X",
codec->width,
codec->height,
ost->sws_opts);
if ((ret = avfilter_open(&filter, avfilter_get_by_name("scale"), NULL)) < 0)
return ret;
if ((ret = avfilter_init_filter(filter, args, NULL)) < 0)
return ret;
if ((ret = avfilter_link(last_filter, 0, filter, 0)) < 0)
return ret;
last_filter = filter;
avfilter_graph_add_filter(graph, last_filter);
}
snprintf(args, sizeof(args), "flags=0x%X", ost->sws_opts);
graph->scale_sws_opts = av_strdup(args);
if (vfilters) {
AVFilterInOut *outputs = av_malloc(sizeof(AVFilterInOut));
AVFilterInOut *inputs = av_malloc(sizeof(AVFilterInOut));
outputs->name = av_strdup("in");
outputs->filter_ctx = last_filter;
outputs->pad_idx = 0;
outputs->next = NULL;
inputs->name = av_strdup("out");
inputs->filter_ctx = ist->out_video_filter;
inputs->pad_idx = 0;
inputs->next = NULL;
if (avfilter_graph_parse(graph, vfilters, inputs, outputs, NULL) < 0)
return -1;
av_freep(&vfilters);
} else {
if (avfilter_link(last_filter, 0, ist->out_video_filter, 0) < 0)
return -1;
}
/* configure all the filter links */
if (avfilter_graph_config(graph, NULL))
return -1;
if (codec) {
codec->width = ist->out_video_filter->inputs[0]->w;
codec->height = ist->out_video_filter->inputs[0]->h;
}
return 0;
}