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 "internal.h"
26 
27 typedef struct WeaveContext {
28  const AVClass *class;
31  int nb_planes;
32  int planeheight[4];
33  int linesize[4];
34 
36 } WeaveContext;
37 
38 #define OFFSET(x) offsetof(WeaveContext, x)
39 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
40 
41 static const AVOption weave_options[] = {
42  { "first_field", "set first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
43  { "top", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
44  { "t", "set top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
45  { "bottom", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
46  { "b", "set bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
47  { NULL }
48 };
49 
50 AVFILTER_DEFINE_CLASS_EXT(weave, "(double)weave", weave_options);
51 
53 {
54  int reject_flags = AV_PIX_FMT_FLAG_PAL | AV_PIX_FMT_FLAG_HWACCEL;
55 
56  return ff_set_common_formats(ctx, ff_formats_pixdesc_filter(0, reject_flags));
57 }
58 
59 static int config_props_output(AVFilterLink *outlink)
60 {
61  AVFilterContext *ctx = outlink->src;
62  WeaveContext *s = ctx->priv;
63  AVFilterLink *inlink = ctx->inputs[0];
65  int ret;
66 
67  if (!s->double_weave) {
68  outlink->time_base.num = inlink->time_base.num * 2;
69  outlink->time_base.den = inlink->time_base.den;
70  outlink->frame_rate.num = inlink->frame_rate.num;
71  outlink->frame_rate.den = inlink->frame_rate.den * 2;
72  }
73  outlink->w = inlink->w;
74  outlink->h = inlink->h * 2;
75 
76  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
77  return ret;
78 
79  s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
80  s->planeheight[0] = s->planeheight[3] = inlink->h;
81 
82  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
83 
84  return 0;
85 }
86 
87 typedef struct ThreadData {
88  AVFrame *in, *out;
89 } ThreadData;
90 
91 static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
92 {
93  AVFilterLink *inlink = ctx->inputs[0];
94  WeaveContext *s = ctx->priv;
95  ThreadData *td = arg;
96  AVFrame *in = td->in;
97  AVFrame *out = td->out;
98 
99  const int weave = (s->double_weave && !(inlink->frame_count_out & 1));
100  const int field1 = weave ? s->first_field : (!s->first_field);
101  const int field2 = weave ? (!s->first_field) : s->first_field;
102 
103  for (int i = 0; i < s->nb_planes; i++) {
104  const int height = s->planeheight[i];
105  const int start = (height * jobnr) / nb_jobs;
106  const int end = (height * (jobnr+1)) / nb_jobs;
107 
108  av_image_copy_plane(out->data[i] + out->linesize[i] * field1 +
109  out->linesize[i] * start * 2,
110  out->linesize[i] * 2,
111  in->data[i] + start * in->linesize[i],
112  in->linesize[i],
113  s->linesize[i], end - start);
114  av_image_copy_plane(out->data[i] + out->linesize[i] * field2 +
115  out->linesize[i] * start * 2,
116  out->linesize[i] * 2,
117  s->prev->data[i] + start * s->prev->linesize[i],
118  s->prev->linesize[i],
119  s->linesize[i], end - start);
120  }
121 
122  return 0;
123 }
124 
126 {
127  AVFilterContext *ctx = inlink->dst;
128  WeaveContext *s = ctx->priv;
129  AVFilterLink *outlink = ctx->outputs[0];
130  ThreadData td;
131  AVFrame *out;
132 
133  if (!s->prev) {
134  s->prev = in;
135  return 0;
136  }
137 
138  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
139  if (!out) {
140  av_frame_free(&in);
141  av_frame_free(&s->prev);
142  return AVERROR(ENOMEM);
143  }
145 
146  td.out = out, td.in = in;
148  FFMIN(s->planeheight[1], ff_filter_get_nb_threads(ctx)));
149 
150  out->pts = s->double_weave ? s->prev->pts : in->pts / 2;
151  out->interlaced_frame = 1;
152  out->top_field_first = !s->first_field;
153 
154  if (!s->double_weave)
155  av_frame_free(&in);
156  av_frame_free(&s->prev);
157  if (s->double_weave)
158  s->prev = in;
159  return ff_filter_frame(outlink, out);
160 }
161 
163 {
164  WeaveContext *s = ctx->priv;
165 
166  av_frame_free(&s->prev);
167 }
168 
169 static const AVFilterPad weave_inputs[] = {
170  {
171  .name = "default",
172  .type = AVMEDIA_TYPE_VIDEO,
173  .filter_frame = filter_frame,
174  },
175 };
176 
177 static const AVFilterPad weave_outputs[] = {
178  {
179  .name = "default",
180  .type = AVMEDIA_TYPE_VIDEO,
181  .config_props = config_props_output,
182  },
183 };
184 
186  .name = "weave",
187  .description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
188  .priv_size = sizeof(WeaveContext),
189  .priv_class = &weave_class,
190  .uninit = uninit,
195 };
196 
198 {
199  WeaveContext *s = ctx->priv;
200 
201  if (!strcmp(ctx->filter->name, "doubleweave"))
202  s->double_weave = 1;
203 
204  return 0;
205 }
206 
208  .name = "doubleweave",
209  .description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
210  .priv_class = &weave_class,
211  .priv_size = sizeof(WeaveContext),
212  .init = init,
213  .uninit = uninit,
218 };
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:98
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:30
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:2660
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:109
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:317
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:424
AVOption
AVOption.
Definition: opt.h:247
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:168
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:169
ThreadData::out
AVFrame * out
Definition: af_adeclick.c:473
weave_outputs
static const AVFilterPad weave_outputs[]
Definition: vf_weave.c:177
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:338
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_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2700
weave_inputs
static const AVFilterPad weave_inputs[]
Definition: vf_weave.c:169
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:50
WeaveContext::first_field
int first_field
Definition: vf_weave.c:29
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_weave.c:162
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:699
WeaveContext::prev
AVFrame * prev
Definition: vf_weave.c:35
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:257
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:51
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:191
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:537
WeaveContext::planeheight
int planeheight[4]
Definition: vf_weave.c:32
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
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:117
ff_vf_doubleweave
const AVFilter ff_vf_doubleweave
Definition: vf_weave.c:207
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_weave.c:52
WeaveContext::nb_planes
int nb_planes
Definition: vf_weave.c:31
height
#define height
internal.h
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_weave.c:125
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:457
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
ff_filter_get_nb_threads
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition: avfilter.c:803
ThreadData
Used for passing data between threads.
Definition: dsddec.c:67
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
WeaveContext
Definition: vf_weave.c:27
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:56
AVFilter
Filter definition.
Definition: avfilter.h:165
ret
ret
Definition: filter_design.txt:187
config_props_output
static int config_props_output(AVFilterLink *outlink)
Definition: vf_weave.c:59
weave_options
static const AVOption weave_options[]
Definition: vf_weave.c:41
OFFSET
#define OFFSET(x)
Definition: vf_weave.c:38
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
avfilter.h
weave_slice
static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition: vf_weave.c:91
AVFilterContext
An instance of a filter.
Definition: avfilter.h:402
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:121
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ThreadData::in
AVFrame * in
Definition: af_adecorrelate.c:154
FLAGS
#define FLAGS
Definition: vf_weave.c:39
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:192
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:362
WeaveContext::linesize
int linesize[4]
Definition: vf_weave.c:33
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:143
first_field
static int first_field(const struct video_data *s)
Definition: v4l2.c:236
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:233
init
static av_cold int init(AVFilterContext *ctx)
Definition: vf_weave.c:197
ff_vf_weave
const AVFilter ff_vf_weave
Definition: vf_weave.c:185