Libav
vf_interlace.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with Libav; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/avassert.h"
28 
29 #include "formats.h"
30 #include "avfilter.h"
31 #include "interlace.h"
32 #include "internal.h"
33 #include "video.h"
34 
35 #define OFFSET(x) offsetof(InterlaceContext, x)
36 #define V AV_OPT_FLAG_VIDEO_PARAM
37 static const AVOption options[] = {
38  { "scan", "scanning mode", OFFSET(scan),
39  AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
40  { "tff", "top field first", 0,
41  AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
42  { "bff", "bottom field first", 0,
43  AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
44  { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
45  AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
46  { NULL }
47 };
48 
49 static const AVClass class = {
50  .class_name = "interlace filter",
51  .item_name = av_default_item_name,
52  .option = options,
54 };
55 
56 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
57  const uint8_t *srcp,
58  const uint8_t *srcp_above,
59  const uint8_t *srcp_below)
60 {
61  int i;
62  for (i = 0; i < linesize; i++) {
63  // this calculation is an integer representation of
64  // '0.5 * current + 0.25 * above + 0.25 * below'
65  // '1 +' is for rounding.
66  dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
67  }
68 }
69 
70 static const enum AVPixelFormat formats_supported[] = {
75 };
76 
78 {
80  return 0;
81 }
82 
84 {
85  InterlaceContext *s = ctx->priv;
86 
87  av_frame_free(&s->cur);
88  av_frame_free(&s->next);
89 
90  av_opt_free(s);
91 }
92 
93 static int config_out_props(AVFilterLink *outlink)
94 {
95  AVFilterContext *ctx = outlink->src;
96  AVFilterLink *inlink = outlink->src->inputs[0];
97  InterlaceContext *s = ctx->priv;
98 
99  if (inlink->h < 2) {
100  av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
101  return AVERROR_INVALIDDATA;
102  }
103 
104  if (!s->lowpass)
105  av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
106  "the resulting video will be aliased rather than interlaced.\n");
107 
108  // same input size
109  outlink->w = inlink->w;
110  outlink->h = inlink->h;
111  outlink->time_base = inlink->time_base;
112  outlink->frame_rate = inlink->frame_rate;
113  // half framerate
114  outlink->time_base.num *= 2;
115  outlink->frame_rate.den *= 2;
116 
117 
118  if (s->lowpass) {
120  if (ARCH_X86)
122  }
123 
124  av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
125  s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
126 
127  return 0;
128 }
129 
131  AVFrame *src_frame, AVFrame *dst_frame,
132  AVFilterLink *inlink, enum FieldType field_type,
133  int lowpass)
134 {
136  int hsub = desc->log2_chroma_w;
137  int vsub = desc->log2_chroma_h;
138  int plane, j;
139 
140  for (plane = 0; plane < desc->nb_components; plane++) {
141  int cols = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->w, hsub)
142  : inlink->w;
143  int lines = (plane == 1 || plane == 2) ? AV_CEIL_RSHIFT(inlink->h, vsub)
144  : inlink->h;
145  uint8_t *dstp = dst_frame->data[plane];
146  const uint8_t *srcp = src_frame->data[plane];
147 
148  av_assert0(cols >= 0 || lines >= 0);
149 
150  lines = (lines + (field_type == FIELD_UPPER)) / 2;
151  if (field_type == FIELD_LOWER) {
152  srcp += src_frame->linesize[plane];
153  dstp += dst_frame->linesize[plane];
154  }
155  if (lowpass) {
156  int srcp_linesize = src_frame->linesize[plane] * 2;
157  int dstp_linesize = dst_frame->linesize[plane] * 2;
158  for (j = lines; j > 0; j--) {
159  const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
160  const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
161  if (j == lines)
162  srcp_above = srcp; // there is no line above
163  if (j == 1)
164  srcp_below = srcp; // there is no line below
165  s->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
166  dstp += dstp_linesize;
167  srcp += srcp_linesize;
168  }
169  } else {
170  av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
171  srcp, src_frame->linesize[plane] * 2,
172  cols, lines);
173  }
174  }
175 }
176 
177 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
178 {
179  AVFilterContext *ctx = inlink->dst;
180  AVFilterLink *outlink = ctx->outputs[0];
181  InterlaceContext *s = ctx->priv;
182  AVFrame *out;
183  int tff, ret;
184 
185  av_frame_free(&s->cur);
186  s->cur = s->next;
187  s->next = buf;
188 
189  /* we need at least two frames */
190  if (!s->cur || !s->next)
191  return 0;
192 
193  if (s->cur->interlaced_frame) {
194  av_log(ctx, AV_LOG_WARNING,
195  "video is already interlaced, adjusting framerate only\n");
196  out = av_frame_clone(s->cur);
197  if (!out)
198  return AVERROR(ENOMEM);
199  out->pts /= 2; // adjust pts to new framerate
200  ret = ff_filter_frame(outlink, out);
201  s->got_output = 1;
202  return ret;
203  }
204 
205  tff = (s->scan == MODE_TFF);
206  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
207  if (!out)
208  return AVERROR(ENOMEM);
209 
210  av_frame_copy_props(out, s->cur);
211  out->interlaced_frame = 1;
212  out->top_field_first = tff;
213  out->pts /= 2; // adjust pts to new framerate
214 
215  /* copy upper/lower field from cur */
216  copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
217  av_frame_free(&s->cur);
218 
219  /* copy lower/upper field from next */
220  copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
221  av_frame_free(&s->next);
222 
223  ret = ff_filter_frame(outlink, out);
224  s->got_output = 1;
225 
226  return ret;
227 }
228 
229 static int request_frame(AVFilterLink *outlink)
230 {
231  AVFilterContext *ctx = outlink->src;
232  InterlaceContext *s = ctx->priv;
233  int ret = 0;
234 
235  s->got_output = 0;
236  while (ret >= 0 && !s->got_output)
237  ret = ff_request_frame(ctx->inputs[0]);
238 
239  return ret;
240 }
241 
242 static const AVFilterPad inputs[] = {
243  {
244  .name = "default",
245  .type = AVMEDIA_TYPE_VIDEO,
246  .filter_frame = filter_frame,
247  },
248  { NULL }
249 };
250 
251 static const AVFilterPad outputs[] = {
252  {
253  .name = "default",
254  .type = AVMEDIA_TYPE_VIDEO,
255  .config_props = config_out_props,
256  .request_frame = request_frame,
257  },
258  { NULL }
259 };
260 
262  .name = "interlace",
263  .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
264  .uninit = uninit,
265 
266  .priv_class = &class,
267  .priv_size = sizeof(InterlaceContext),
269 
270  .inputs = inputs,
271  .outputs = outputs,
272 };
273 
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1768
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVOption.
Definition: opt.h:234
static const AVFilterPad inputs[]
Definition: vf_interlace.c:242
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:64
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
void(* lowpass_line)(uint8_t *dstp, ptrdiff_t linesize, const uint8_t *srcp, const uint8_t *srcp_above, const uint8_t *srcp_below)
Definition: interlace.h:53
Main libavfilter public API header.
const char * desc
Definition: nvenc.c:101
int num
numerator
Definition: rational.h:44
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:57
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:91
static const AVOption options[]
Definition: vf_interlace.c:37
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:165
static enum AVPixelFormat formats_supported[]
Definition: vf_interlace.c:70
static int query_formats(AVFilterContext *ctx)
Definition: vf_interlace.c:77
const char * name
Pad name.
Definition: internal.h:41
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:270
static int config_out_props(AVFilterLink *outlink)
Definition: vf_interlace.c:93
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:747
static const AVFilterPad outputs[]
Definition: vf_interlace.c:251
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:98
uint8_t
#define av_cold
Definition: attributes.h:66
static int request_frame(AVFilterLink *outlink)
Definition: vf_interlace.c:229
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:211
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:97
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:72
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:140
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:263
void 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:389
A filter pad used for either input or output.
Definition: internal.h:35
FieldType
Definition: interlace.h:42
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
void ff_interlace_init_x86(InterlaceContext *interlace)
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:100
#define ARCH_X86
Definition: config.h:33
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:148
void * priv
private data for use by the filter
Definition: avfilter.h:277
AVFilter ff_vf_interlace
Definition: vf_interlace.c:261
simple assert() macros that are a bit more flexible than ISO C assert().
AVFrame * next
Definition: interlace.h:51
#define OFFSET(x)
Definition: vf_interlace.c:35
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:63
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:82
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:71
AVFormatContext * ctx
Definition: movenc.c:48
progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter...
static av_always_inline uint32_t lowpass(int prev, int cur, int16_t *coef, int depth)
Definition: vf_hqdn3d.c:51
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:289
NULL
Definition: eval.c:55
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_interlace.c:83
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:158
av_default_item_name
Definition: dnxhdenc.c:55
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:80
static void copy_picture_field(InterlaceContext *s, AVFrame *src_frame, AVFrame *dst_frame, AVFilterLink *inlink, enum FieldType field_type, int lowpass)
Definition: vf_interlace.c:130
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:65
Describe the class of an AVClass context structure.
Definition: log.h:34
Filter definition.
Definition: avfilter.h:120
const char * name
Filter name.
Definition: avfilter.h:124
AVFrame * cur
Definition: interlace.h:51
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:274
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:146
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
Y , 8bpp.
Definition: pixfmt.h:67
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:715
common internal and external API header
#define V
Definition: vf_interlace.c:36
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:73
enum ScanMode scan
Definition: interlace.h:49
int den
denominator
Definition: rational.h:45
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:268
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: vf_interlace.c:177
An instance of a filter.
Definition: avfilter.h:262
FILE * out
Definition: movenc.c:54
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:264
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:255
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:386
#define AV_CEIL_RSHIFT(a, b)
Fast a / (1 << b) rounded toward +inf, assuming a >= 0 and b >= 0.
Definition: common.h:57
static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize, const uint8_t *srcp, const uint8_t *srcp_above, const uint8_t *srcp_below)
Definition: vf_interlace.c:56