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 "formats.h"
26 #include "internal.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) {
71  outlink->time_base.num = inlink->time_base.num * 2;
72  outlink->time_base.den = inlink->time_base.den;
73  outlink->frame_rate.num = inlink->frame_rate.num;
74  outlink->frame_rate.den = inlink->frame_rate.den * 2;
75  }
76  outlink->w = inlink->w;
77  outlink->h = inlink->h * 2;
78 
79  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
80  return ret;
81 
82  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
83  s->planeheight[0] = s->planeheight[3] = inlink->h;
84 
85  s->outheight[1] = s->outheight[2] = AV_CEIL_RSHIFT(2*inlink->h, desc->log2_chroma_h);
86  s->outheight[0] = s->outheight[3] = 2*inlink->h;
87 
88  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
89 
90  return 0;
91 }
92 
93 typedef struct ThreadData {
94  AVFrame *in, *out;
95 } ThreadData;
96 
97 static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
98 {
99  AVFilterLink *inlink = ctx->inputs[0];
100  WeaveContext *s = ctx->priv;
101  ThreadData *td = arg;
102  AVFrame *in = td->in;
103  AVFrame *out = td->out;
104 
105  const int weave = (s->double_weave && !(inlink->frame_count_out & 1));
106  const int field1 = weave ? s->first_field : (!s->first_field);
107  const int field2 = weave ? (!s->first_field) : s->first_field;
108 
109  for (int i = 0; i < s->nb_planes; i++) {
110  const int height = s->planeheight[i];
111  const int start = (height * jobnr) / nb_jobs;
112  const int end = (height * (jobnr+1)) / nb_jobs;
113  const int compensation = 2*end > s->outheight[i];
114 
115  av_image_copy_plane(out->data[i] + out->linesize[i] * field1 +
116  out->linesize[i] * start * 2,
117  out->linesize[i] * 2,
118  in->data[i] + start * in->linesize[i],
119  in->linesize[i],
120  s->linesize[i], end - start - compensation * field1);
121  av_image_copy_plane(out->data[i] + out->linesize[i] * field2 +
122  out->linesize[i] * start * 2,
123  out->linesize[i] * 2,
124  s->prev->data[i] + start * s->prev->linesize[i],
125  s->prev->linesize[i],
126  s->linesize[i], end - start - compensation * field2);
127  }
128 
129  return 0;
130 }
131 
133 {
134  AVFilterContext *ctx = inlink->dst;
135  WeaveContext *s = ctx->priv;
136  AVFilterLink *outlink = ctx->outputs[0];
137  ThreadData td;
138  AVFrame *out;
139 
140  if (!s->prev) {
141  s->prev = in;
142  return 0;
143  }
144 
145  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
146  if (!out) {
147  av_frame_free(&in);
148  av_frame_free(&s->prev);
149  return AVERROR(ENOMEM);
150  }
152 
153  td.out = out, td.in = in;
155  FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
156 
157  out->pts = s->double_weave ? s->prev->pts : in->pts / 2;
158 #if FF_API_INTERLACED_FRAME
160  out->interlaced_frame = 1;
161  out->top_field_first = !s->first_field;
163 #endif
164  out->flags |= AV_FRAME_FLAG_INTERLACED;
165  if (s->first_field)
167  else
169 
170  if (!s->double_weave)
171  av_frame_free(&in);
172  av_frame_free(&s->prev);
173  if (s->double_weave)
174  s->prev = in;
175  return ff_filter_frame(outlink, out);
176 }
177 
179 {
180  WeaveContext *s = ctx->priv;
181 
182  av_frame_free(&s->prev);
183 }
184 
185 static const AVFilterPad weave_inputs[] = {
186  {
187  .name = "default",
188  .type = AVMEDIA_TYPE_VIDEO,
189  .filter_frame = filter_frame,
190  },
191 };
192 
193 static const AVFilterPad weave_outputs[] = {
194  {
195  .name = "default",
196  .type = AVMEDIA_TYPE_VIDEO,
197  .config_props = config_props_output,
198  },
199 };
200 
202  .name = "weave",
203  .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
204  .priv_size = sizeof(WeaveContext),
205  .priv_class = &weave_class,
206  .uninit = uninit,
211 };
212 
214 {
215  WeaveContext *s = ctx->priv;
216 
217  if (!strcmp(ctx->filter->name, "doubleweave"))
218  s->double_weave = 1;
219 
220  return 0;
221 }
222 
224  .name = "doubleweave",
225  .description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
226  .priv_class = &weave_class,
227  .priv_size = sizeof(WeaveContext),
228  .init = init,
229  .uninit = uninit,
234 };
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:112
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:73
td
#define td
Definition: regdef.h:70
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:54
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:1018
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
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:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:456
AVOption
AVOption.
Definition: opt.h:346
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:159
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:526
weave_outputs
static const AVFilterPad weave_outputs[]
Definition: vf_weave.c:193
video.h
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:153
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
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:608
formats.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3002
weave_inputs
static const AVFilterPad weave_inputs[]
Definition: vf_weave.c:185
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: internal.h:33
WeaveContext::first_field
int first_field
Definition: vf_weave.c:31
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_weave.c:178
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:58
WeaveContext::outheight
int outheight[4]
Definition: vf_weave.c:35
AVFILTER_DEFINE_CLASS_EXT
AVFILTER_DEFINE_CLASS_EXT(weave, "(double)weave", weave_options)
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
arg
const char * arg
Definition: jacosubdec.c:67
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
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:679
WeaveContext::planeheight
int planeheight[4]
Definition: vf_weave.c:34
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:106
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
ff_vf_doubleweave
const AVFilter ff_vf_doubleweave
Definition: vf_weave.c:223
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_weave.c:55
WeaveContext::nb_planes
int nb_planes
Definition: vf_weave.c:33
height
#define height
internal.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_weave.c:132
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:255
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:825
ThreadData
Used for passing data between threads.
Definition: dsddec.c:69
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
WeaveContext
Definition: vf_weave.c:29
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
AV_FRAME_FLAG_INTERLACED
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition: frame.h:603
AVFilter
Filter definition.
Definition: avfilter.h:166
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
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:235
avfilter.h
weave_slice
static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_weave.c:97
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
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:117
desc
const char * desc
Definition: libsvtav1.c:75
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
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
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:389
WeaveContext::linesize
int linesize[4]
Definition: vf_weave.c:36
ff_filter_execute
static av_always_inline int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: internal.h:134
first_field
static int first_field(const struct video_data *s)
Definition: v4l2.c:246
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
Definition: opt.h:244
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_weave.c:213
ff_vf_weave
const AVFilter ff_vf_weave
Definition: vf_weave.c:201