FFmpeg
vf_weave.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/imgutils.h"
22 #include "libavutil/opt.h"
23 #include "libavutil/pixdesc.h"
24 #include "avfilter.h"
25 #include "filters.h"
26 #include "formats.h"
27 #include "video.h"
28 
29 typedef struct WeaveContext {
30  const AVClass *class;
33  int nb_planes;
34  int planeheight[4];
35  int outheight[4];
36  int linesize[4];
37 
39 } WeaveContext;
40 
41 #define OFFSET(x) offsetof(WeaveContext, x)
42 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
43 
44 static const AVOption weave_options[] = {
45  { "first_field", "set first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, .unit = "field"},
46  { "top", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "field"},
47  { "t", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "field"},
48  { "bottom", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "field"},
49  { "b", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "field"},
50  { NULL }
51 };
52 
53 AVFILTER_DEFINE_CLASS_EXT(weave, "(double)weave", weave_options);
54 
56 {
57  int reject_flags = AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_HWACCEL;
58 
59  return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
60 }
61 
62 static int config_props_output(AVFilterLink *outlink)
63 {
64  AVFilterContext *ctx = outlink->src;
65  WeaveContext *s = ctx->priv;
66  AVFilterLink *inlink = ctx->inputs[0];
68  int ret;
69 
70  if (!s->double_weave) {
72  FilterLink *ol = ff_filter_link(outlink);
73  outlink->time_base.num = inlink->time_base.num * 2;
74  outlink->time_base.den = inlink->time_base.den;
75  ol->frame_rate.num = il->frame_rate.num;
76  ol->frame_rate.den = il->frame_rate.den * 2;
77  }
78  outlink->w = inlink->w;
79  outlink->h = inlink->h * 2;
80 
81  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
82  return ret;
83 
84  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
85  s->planeheight[0] = s->planeheight[3] = inlink->h;
86 
87  s->outheight[1] = s->outheight[2] = AV_CEIL_RSHIFT(2*inlink->h, desc->log2_chroma_h);
88  s->outheight[0] = s->outheight[3] = 2*inlink->h;
89 
90  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
91 
92  return 0;
93 }
94 
95 typedef struct ThreadData {
96  AVFrame *in, *out;
97 } ThreadData;
98 
99 static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
100 {
101  AVFilterLink *inlink = ctx->inputs[0];
103  WeaveContext *s = ctx->priv;
104  ThreadData *td = arg;
105  AVFrame *in = td->in;
106  AVFrame *out = td->out;
107 
108  const int weave = (s->double_weave && !(inl->frame_count_out & 1));
109  const int field1 = weave ? s->first_field : (!s->first_field);
110  const int field2 = weave ? (!s->first_field) : s->first_field;
111 
112  for (int i = 0; i < s->nb_planes; i++) {
113  const int height = s->planeheight[i];
114  const int start = (height * jobnr) / nb_jobs;
115  const int end = (height * (jobnr+1)) / nb_jobs;
116  const int compensation = 2*end > s->outheight[i];
117 
118  av_image_copy_plane(out->data[i] + out->linesize[i] * field1 +
119  out->linesize[i] * start * 2,
120  out->linesize[i] * 2,
121  in->data[i] + start * in->linesize[i],
122  in->linesize[i],
123  s->linesize[i], end - start - compensation * field1);
124  av_image_copy_plane(out->data[i] + out->linesize[i] * field2 +
125  out->linesize[i] * start * 2,
126  out->linesize[i] * 2,
127  s->prev->data[i] + start * s->prev->linesize[i],
128  s->prev->linesize[i],
129  s->linesize[i], end - start - compensation * field2);
130  }
131 
132  return 0;
133 }
134 
136 {
137  AVFilterContext *ctx = inlink->dst;
138  WeaveContext *s = ctx->priv;
139  AVFilterLink *outlink = ctx->outputs[0];
140  ThreadData td;
141  AVFrame *out;
142 
143  if (!s->prev) {
144  s->prev = in;
145  return 0;
146  }
147 
148  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
149  if (!out) {
150  av_frame_free(&in);
151  av_frame_free(&s->prev);
152  return AVERROR(ENOMEM);
153  }
155 
156  td.out = out, td.in = in;
158  FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
159 
160  out->pts = s->double_weave ? s->prev->pts : in->pts / 2;
161 #if FF_API_INTERLACED_FRAME
163  out->interlaced_frame = 1;
164  out->top_field_first = !s->first_field;
166 #endif
167  out->flags |= AV_FRAME_FLAG_INTERLACED;
168  if (s->first_field)
170  else
172 
173  if (!s->double_weave)
174  av_frame_free(&in);
175  av_frame_free(&s->prev);
176  if (s->double_weave)
177  s->prev = in;
178  return ff_filter_frame(outlink, out);
179 }
180 
182 {
183  WeaveContext *s = ctx->priv;
184 
185  av_frame_free(&s->prev);
186 }
187 
188 static const AVFilterPad weave_inputs[] = {
189  {
190  .name = "default",
191  .type = AVMEDIA_TYPE_VIDEO,
192  .filter_frame = filter_frame,
193  },
194 };
195 
196 static const AVFilterPad weave_outputs[] = {
197  {
198  .name = "default",
199  .type = AVMEDIA_TYPE_VIDEO,
200  .config_props = config_props_output,
201  },
202 };
203 
205  .name = "weave",
206  .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
207  .priv_size = sizeof(WeaveContext),
208  .priv_class = &weave_class,
209  .uninit = uninit,
214 };
215 
217 {
218  WeaveContext *s = ctx->priv;
219 
220  if (!strcmp(ctx->filter->name, "doubleweave"))
221  s->double_weave = 1;
222 
223  return 0;
224 }
225 
227  .name = "doubleweave",
228  .description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
229  .priv_class = &weave_class,
230  .priv_size = sizeof(WeaveContext),
231  .init = init,
232  .uninit = uninit,
237 };
ff_get_video_buffer
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:116
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
out
FILE * out
Definition: movenc.c:55
WeaveContext::double_weave
int double_weave
Definition: vf_weave.c:32
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1025
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2965
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:162
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:501
AVOption
AVOption.
Definition: opt.h:429
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:205
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:526
weave_outputs
static const AVFilterPad weave_outputs[]
Definition: vf_weave.c:196
video.h
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:155
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:410
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
AV_FRAME_FLAG_TOP_FIELD_FIRST
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition: frame.h:653
formats.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3005
weave_inputs
static const AVFilterPad weave_inputs[]
Definition: vf_weave.c:188
AV_PIX_FMT_FLAG_HWACCEL
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:128
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: filters.h:38
WeaveContext::first_field
int first_field
Definition: vf_weave.c:31
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_weave.c:181
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:867
WeaveContext::prev
AVFrame * prev
Definition: vf_weave.c:38
av_image_fill_linesizes
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:89
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
WeaveContext::outheight
int outheight[4]
Definition: vf_weave.c:35
filters.h
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(weave, "(double)weave", weave_options)
ctx
AVFormatContext * ctx
Definition: movenc.c:49
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: filters.h:263
arg
const char * arg
Definition: jacosubdec.c:67
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:713
WeaveContext::planeheight
int planeheight[4]
Definition: vf_weave.c:34
ff_filter_link
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition: filters.h:197
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
height
#define height
Definition: dsp.h:85
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
ff_vf_doubleweave
const AVFilter ff_vf_doubleweave
Definition: vf_weave.c:226
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_weave.c:55
WeaveContext::nb_planes
int nb_planes
Definition: vf_weave.c:33
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_weave.c:135
ff_formats_pixdesc_filter
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition: formats.c:553
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:836
ThreadData
Used for passing data between threads.
Definition: dsddec.c:71
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
WeaveContext
Definition: vf_weave.c:29
AVFilterPad::name
const char * name
Pad name.
Definition: filters.h:44
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:648
AVFilter
Filter definition.
Definition: avfilter.h:201
ret
ret
Definition: filter_design.txt:187
config_props_output
static int config_props_output(AVFilterLink *outlink)
Definition: vf_weave.c:62
weave_options
static const AVOption weave_options[]
Definition: vf_weave.c:44
OFFSET
#define OFFSET(x)
Definition: vf_weave.c:41
ff_filter_execute
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: avfilter.c:1653
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
avfilter.h
weave_slice
static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_weave.c:99
AVFilterContext
An instance of a filter.
Definition: avfilter.h:457
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVFILTER_FLAG_SLICE_THREADS
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition: avfilter.h:152
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FLAGS
#define FLAGS
Definition: vf_weave.c:42
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:434
WeaveContext::linesize
int linesize[4]
Definition: vf_weave.c:36
first_field
static int first_field(const struct video_data *s)
Definition: v4l2.c:256
AV_PIX_FMT_FLAG_PAL
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:120
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_weave.c:216
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: filters.h:236
ff_vf_weave
const AVFilter ff_vf_weave
Definition: vf_weave.c:204