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 
55 static int query_formats(const AVFilterContext *ctx,
56  AVFilterFormatsConfig **cfg_in,
57  AVFilterFormatsConfig **cfg_out)
58 {
59  int reject_flags = AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_HWACCEL;
60 
61  return ff_set_common_formats2(ctx, cfg_in, cfg_out,
62  ff_formats_pixdesc_filter(0, reject_flags));
63 }
64 
65 static int config_props_output(AVFilterLink *outlink)
66 {
67  AVFilterContext *ctx = outlink->src;
68  WeaveContext *s = ctx->priv;
69  AVFilterLink *inlink = ctx->inputs[0];
71  int ret;
72 
73  if (!s->double_weave) {
75  FilterLink *ol = ff_filter_link(outlink);
76  outlink->time_base.num = inlink->time_base.num * 2;
77  outlink->time_base.den = inlink->time_base.den;
78  ol->frame_rate.num = il->frame_rate.num;
79  ol->frame_rate.den = il->frame_rate.den * 2;
80  }
81  outlink->w = inlink->w;
82  outlink->h = inlink->h * 2;
83 
84  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
85  return ret;
86 
87  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
88  s->planeheight[0] = s->planeheight[3] = inlink->h;
89 
90  s->outheight[1] = s->outheight[2] = AV_CEIL_RSHIFT(2*inlink->h, desc->log2_chroma_h);
91  s->outheight[0] = s->outheight[3] = 2*inlink->h;
92 
93  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
94 
95  return 0;
96 }
97 
98 typedef struct ThreadData {
99  AVFrame *in, *out;
100 } ThreadData;
101 
102 static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
103 {
104  AVFilterLink *inlink = ctx->inputs[0];
106  WeaveContext *s = ctx->priv;
107  ThreadData *td = arg;
108  AVFrame *in = td->in;
109  AVFrame *out = td->out;
110 
111  const int weave = (s->double_weave && !(inl->frame_count_out & 1));
112  const int field1 = weave ? s->first_field : (!s->first_field);
113  const int field2 = weave ? (!s->first_field) : s->first_field;
114 
115  for (int i = 0; i < s->nb_planes; i++) {
116  const int height = s->planeheight[i];
117  const int start = (height * jobnr) / nb_jobs;
118  const int end = (height * (jobnr+1)) / nb_jobs;
119  const int compensation = 2*end > s->outheight[i];
120 
121  av_image_copy_plane(out->data[i] + out->linesize[i] * field1 +
122  out->linesize[i] * start * 2,
123  out->linesize[i] * 2,
124  in->data[i] + start * in->linesize[i],
125  in->linesize[i],
126  s->linesize[i], end - start - compensation * field1);
127  av_image_copy_plane(out->data[i] + out->linesize[i] * field2 +
128  out->linesize[i] * start * 2,
129  out->linesize[i] * 2,
130  s->prev->data[i] + start * s->prev->linesize[i],
131  s->prev->linesize[i],
132  s->linesize[i], end - start - compensation * field2);
133  }
134 
135  return 0;
136 }
137 
139 {
140  AVFilterContext *ctx = inlink->dst;
141  WeaveContext *s = ctx->priv;
142  AVFilterLink *outlink = ctx->outputs[0];
143  ThreadData td;
144  AVFrame *out;
145 
146  if (!s->prev) {
147  s->prev = in;
148  return 0;
149  }
150 
151  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
152  if (!out) {
153  av_frame_free(&in);
154  av_frame_free(&s->prev);
155  return AVERROR(ENOMEM);
156  }
158 
159  td.out = out, td.in = in;
161  FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
162 
163  out->pts = s->double_weave ? s->prev->pts : in->pts / 2;
164 #if FF_API_INTERLACED_FRAME
166  out->interlaced_frame = 1;
167  out->top_field_first = !s->first_field;
169 #endif
170  out->flags |= AV_FRAME_FLAG_INTERLACED;
171  if (s->first_field)
173  else
175 
176  if (!s->double_weave)
177  av_frame_free(&in);
178  av_frame_free(&s->prev);
179  if (s->double_weave)
180  s->prev = in;
181  return ff_filter_frame(outlink, out);
182 }
183 
185 {
186  WeaveContext *s = ctx->priv;
187 
188  av_frame_free(&s->prev);
189 }
190 
191 static const AVFilterPad weave_inputs[] = {
192  {
193  .name = "default",
194  .type = AVMEDIA_TYPE_VIDEO,
195  .filter_frame = filter_frame,
196  },
197 };
198 
199 static const AVFilterPad weave_outputs[] = {
200  {
201  .name = "default",
202  .type = AVMEDIA_TYPE_VIDEO,
203  .config_props = config_props_output,
204  },
205 };
206 
208  .name = "weave",
209  .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
210  .priv_size = sizeof(WeaveContext),
211  .priv_class = &weave_class,
212  .uninit = uninit,
217 };
218 
220 {
221  WeaveContext *s = ctx->priv;
222 
223  if (!strcmp(ctx->filter->name, "doubleweave"))
224  s->double_weave = 1;
225 
226  return 0;
227 }
228 
230  .name = "doubleweave",
231  .description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
232  .priv_class = &weave_class,
233  .priv_size = sizeof(WeaveContext),
234  .init = init,
235  .uninit = uninit,
240 };
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:1061
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3170
ff_set_common_formats2
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition: formats.c:1007
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:163
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: filters.h:262
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:403
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:515
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:199
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:424
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:667
formats.h
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3210
weave_inputs
static const AVFilterPad weave_inputs[]
Definition: vf_weave.c:191
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:184
av_cold
#define av_cold
Definition: attributes.h:90
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:726
WeaveContext::planeheight
int planeheight[4]
Definition: vf_weave.c:34
AVFilterFormatsConfig
Lists of formats / etc.
Definition: avfilter.h:111
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:229
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:138
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:840
ThreadData
Used for passing data between threads.
Definition: dsddec.c:71
FILTER_QUERY_FUNC2
#define FILTER_QUERY_FUNC2(func)
Definition: filters.h:239
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:662
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:65
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:1666
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:102
query_formats
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition: vf_weave.c:55
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:448
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:219
ff_vf_weave
const AVFilter ff_vf_weave
Definition: vf_weave.c:207