FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_interlace.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2003 Michael Zucchi <notzed@ximian.com>
3  * Copyright (c) 2010 Baptiste Coudurier
4  * Copyright (c) 2011 Stefano Sabatini
5  * Copyright (c) 2013 Vittorio Giovara <vittorio.giovara@gmail.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23 
24 /**
25  * @file
26  * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
27  */
28 
29 #include "libavutil/common.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/imgutils.h"
32 #include "libavutil/avassert.h"
33 
34 #include "formats.h"
35 #include "avfilter.h"
36 #include "interlace.h"
37 #include "internal.h"
38 #include "video.h"
39 
40 #define OFFSET(x) offsetof(InterlaceContext, x)
41 #define V AV_OPT_FLAG_VIDEO_PARAM
42 static const AVOption interlace_options[] = {
43  { "scan", "scanning mode", OFFSET(scan),
44  AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
45  { "tff", "top field first", 0,
46  AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
47  { "bff", "bottom field first", 0,
48  AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
49  { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
50  AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
51  { NULL }
52 };
53 
54 AVFILTER_DEFINE_CLASS(interlace);
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  if (!fmts_list)
81  return AVERROR(ENOMEM);
82  return ff_set_common_formats(ctx, fmts_list);
83 }
84 
85 static av_cold void uninit(AVFilterContext *ctx)
86 {
87  InterlaceContext *s = ctx->priv;
88 
89  av_frame_free(&s->cur);
90  av_frame_free(&s->next);
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  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
117 
118 
119  if (s->lowpass) {
121  if (ARCH_X86)
123  }
124 
125  av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
126  s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
127 
128  return 0;
129 }
130 
132  AVFrame *src_frame, AVFrame *dst_frame,
133  AVFilterLink *inlink, enum FieldType field_type,
134  int lowpass)
135 {
136  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
137  int hsub = desc->log2_chroma_w;
138  int vsub = desc->log2_chroma_h;
139  int plane, j;
140 
141  for (plane = 0; plane < desc->nb_components; plane++) {
142  int cols = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
143  int lines = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
144  uint8_t *dstp = dst_frame->data[plane];
145  const uint8_t *srcp = src_frame->data[plane];
146 
147  av_assert0(cols >= 0 || lines >= 0);
148 
149  lines = (lines + (field_type == FIELD_UPPER)) / 2;
150  if (field_type == FIELD_LOWER) {
151  srcp += src_frame->linesize[plane];
152  dstp += dst_frame->linesize[plane];
153  }
154  if (lowpass) {
155  int srcp_linesize = src_frame->linesize[plane] * 2;
156  int dstp_linesize = dst_frame->linesize[plane] * 2;
157  for (j = lines; j > 0; j--) {
158  const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
159  const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
160  if (j == lines)
161  srcp_above = srcp; // there is no line above
162  if (j == 1)
163  srcp_below = srcp; // there is no line below
164  s->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
165  dstp += dstp_linesize;
166  srcp += srcp_linesize;
167  }
168  } else {
169  av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
170  srcp, src_frame->linesize[plane] * 2,
171  cols, lines);
172  }
173  }
174 }
175 
176 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
177 {
178  AVFilterContext *ctx = inlink->dst;
179  AVFilterLink *outlink = ctx->outputs[0];
180  InterlaceContext *s = ctx->priv;
181  AVFrame *out;
182  int tff, ret;
183 
184  av_frame_free(&s->cur);
185  s->cur = s->next;
186  s->next = buf;
187 
188  /* we need at least two frames */
189  if (!s->cur || !s->next)
190  return 0;
191 
192  if (s->cur->interlaced_frame) {
193  av_log(ctx, AV_LOG_WARNING,
194  "video is already interlaced, adjusting framerate only\n");
195  out = av_frame_clone(s->cur);
196  if (!out)
197  return AVERROR(ENOMEM);
198  out->pts /= 2; // adjust pts to new framerate
199  ret = ff_filter_frame(outlink, out);
200  return ret;
201  }
202 
203  tff = (s->scan == MODE_TFF);
204  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
205  if (!out)
206  return AVERROR(ENOMEM);
207 
208  av_frame_copy_props(out, s->cur);
209  out->interlaced_frame = 1;
210  out->top_field_first = tff;
211  out->pts /= 2; // adjust pts to new framerate
212 
213  /* copy upper/lower field from cur */
214  copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
215  av_frame_free(&s->cur);
216 
217  /* copy lower/upper field from next */
218  copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
219  av_frame_free(&s->next);
220 
221  ret = ff_filter_frame(outlink, out);
222 
223  return ret;
224 }
225 
226 static const AVFilterPad inputs[] = {
227  {
228  .name = "default",
229  .type = AVMEDIA_TYPE_VIDEO,
230  .filter_frame = filter_frame,
231  },
232  { NULL }
233 };
234 
235 static const AVFilterPad outputs[] = {
236  {
237  .name = "default",
238  .type = AVMEDIA_TYPE_VIDEO,
239  .config_props = config_out_props,
240  },
241  { NULL }
242 };
243 
245  .name = "interlace",
246  .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
247  .uninit = uninit,
248  .priv_class = &interlace_class,
249  .priv_size = sizeof(InterlaceContext),
251  .inputs = inputs,
252  .outputs = outputs,
253 };
int plane
Definition: avisynth_c.h:291
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2090
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
AVOption.
Definition: opt.h:255
static const AVFilterPad inputs[]
Definition: vf_interlace.c:226
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
Main libavfilter public API header.
int num
numerator
Definition: rational.h:44
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
Definition: pixdesc.h:80
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:283
static enum AVPixelFormat formats_supported[]
Definition: vf_interlace.c:70
static int query_formats(AVFilterContext *ctx)
Definition: vf_interlace.c:77
BYTE int const BYTE * srcp
Definition: avisynth_c.h:676
const char * name
Pad name.
Definition: internal.h:67
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
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:1145
static const AVFilterPad outputs[]
Definition: vf_interlace.c:235
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:103
uint8_t
#define av_cold
Definition: attributes.h:74
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:257
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
Definition: pixfmt.h:102
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:76
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:367
#define av_log(a,...)
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:52
A filter pad used for either input or output.
Definition: internal.h:61
FieldType
Definition: interlace.h:42
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:542
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:89
BYTE * dstp
Definition: avisynth_c.h:676
#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:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
AVFilter ff_vf_interlace
Definition: vf_interlace.c:244
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:40
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
ret
Definition: avfilter.c:974
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter...
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:449
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_interlace.c:85
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
AVFILTER_DEFINE_CLASS(interlace)
void * buf
Definition: avisynth_c.h:553
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:131
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:69
Filter definition.
Definition: avfilter.h:470
const char * name
Filter name.
Definition: avfilter.h:474
AVFrame * cur
Definition: interlace.h:51
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
Frame requests may need to loop in order to be fulfilled.
Definition: internal.h:359
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
Y , 8bpp.
Definition: pixfmt.h:71
static const AVOption interlace_options[]
Definition: vf_interlace.c:42
common internal and external API header
#define V
Definition: vf_interlace.c:41
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:77
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:372
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: vf_interlace.c:176
A list of supported formats for one end of a filter link.
Definition: formats.h:64
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
An instance of a filter.
Definition: avfilter.h:633
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:273
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:548
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