FFmpeg
Loading...
Searching...
No Matches
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
29typedef struct WeaveContext {
30 const AVClass *class;
35 int outheight[4];
36 int linesize[4];
37
40
41#define OFFSET(x) offsetof(WeaveContext, x)
42#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
43
44static 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
54
56 AVFilterFormatsConfig **cfg_in,
57 AVFilterFormatsConfig **cfg_out)
58{
60
61 return ff_set_common_formats2(ctx, cfg_in, cfg_out,
62 ff_formats_pixdesc_filter(0, reject_flags));
63}
64
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) {
74 FilterLink *il = ff_filter_link(inlink);
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
98typedef struct ThreadData {
99 AVFrame *in, *out;
100} ThreadData;
101
102static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
103{
104 AVFilterLink *inlink = ctx->inputs[0];
105 FilterLink *inl = ff_filter_link(inlink);
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 = ff_slice_pos(height, jobnr, nb_jobs);
118 const int end = ff_slice_pos(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
138static int filter_frame(AVFilterLink *inlink, AVFrame *in)
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;
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
185static const AVFilterPad weave_inputs[] = {
186 {
187 .name = "default",
188 .type = AVMEDIA_TYPE_VIDEO,
189 .filter_frame = filter_frame,
190 },
191};
192
193static const AVFilterPad weave_outputs[] = {
194 {
195 .name = "default",
196 .type = AVMEDIA_TYPE_VIDEO,
197 .config_props = config_props_output,
198 },
199};
200
202 .p.name = "weave",
203 .p.description = NULL_IF_CONFIG_SMALL("Weave input video fields into frames."),
204 .p.priv_class = &weave_class,
206 .priv_size = sizeof(WeaveContext),
207 .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 .p.name = "doubleweave",
225 .p.description = NULL_IF_CONFIG_SMALL("Weave input video fields into double number of frames."),
226 .p.priv_class = &weave_class,
228 .priv_size = sizeof(WeaveContext),
229 .init = init,
230 .uninit = uninit,
234};
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition aeval.c:246
const FFFilter ff_vf_doubleweave
Definition vf_weave.c:223
const FFFilter ff_vf_weave
Definition vf_weave.c:201
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1696
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:846
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define NULL
Definition coverity.c:32
static int filter_frame(DBEDecodeContext *s, AVFrame *frame)
Definition dolby_e.c:1067
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_set_common_formats2(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out, AVFilterFormats *formats)
Definition formats.c:1137
AVFilterFormats * ff_formats_pixdesc_filter(unsigned want, unsigned rej)
Construct a formats list containing all pixel formats with certain properties.
Definition formats.c:620
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:166
#define AVERROR(e)
Definition error.h:45
#define AV_FRAME_FLAG_INTERLACED
A flag to mark frames whose content is interlaced.
Definition frame.h:695
#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:700
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:599
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
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
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
misc image utilities
static av_cold void uninit(AVBitStreamFilterContext *ctx)
const char * arg
Definition jacosubdec.c:65
#define FILTER_INPUTS(array)
Definition filters.h:264
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:763
#define AVFILTER_DEFINE_CLASS_EXT(name, desc, options)
Definition filters.h:470
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_QUERY_FUNC2(func)
Definition filters.h:241
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
const char * desc
Definition libsvtav1.c:83
#define FFMIN(a, b)
Definition macros.h:49
AVOptions.
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3500
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition pixdesc.h:128
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition pixdesc.h:120
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
Lists of formats / etc.
Definition avfilter.h:120
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
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:517
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Used for passing data between threads.
Definition dsddec.c:71
AVFrame * out
int nb_planes
Definition vf_weave.c:33
int first_field
Definition vf_weave.c:31
int outheight[4]
Definition vf_weave.c:35
int planeheight[4]
Definition vf_weave.c:34
int linesize[4]
Definition vf_weave.c:36
int double_weave
Definition vf_weave.c:32
AVFrame * prev
Definition vf_weave.c:38
static FILE * out
Definition movenc.c:55
static AVFormatContext * ctx
Definition movenc.c:49
#define height
Definition dsp.h:89
static int first_field(const struct video_data *s)
Definition v4l2.c:260
static int config_props_output(AVFilterLink *outlink)
static const AVFilterPad weave_outputs[]
Definition vf_weave.c:193
static const AVOption weave_options[]
Definition vf_weave.c:44
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition vf_weave.c:138
static int query_formats(const AVFilterContext *ctx, AVFilterFormatsConfig **cfg_in, AVFilterFormatsConfig **cfg_out)
Definition vf_weave.c:55
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_weave.c:178
static int config_props_output(AVFilterLink *outlink)
Definition vf_weave.c:65
#define OFFSET(x)
Definition vf_weave.c:41
static int weave_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_weave.c:102
static const AVFilterPad weave_inputs[]
Definition vf_weave.c:185
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89