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 "internal.h"
32 #include "video.h"
33 
34 enum ScanMode {
35  MODE_TFF = 0,
36  MODE_BFF = 1,
37 };
38 
39 enum FieldType {
42 };
43 
44 typedef struct InterlaceContext {
45  const AVClass *class;
46  enum ScanMode scan; // top or bottom field first scanning
47  int lowpass; // enable or disable low pass filterning
48  AVFrame *cur, *next; // the two frames from which the new one is obtained
49  int got_output; // signal an output frame is reday to request_frame()
51 
52 #define OFFSET(x) offsetof(InterlaceContext, x)
53 #define V AV_OPT_FLAG_VIDEO_PARAM
54 static const AVOption options[] = {
55  { "scan", "scanning mode", OFFSET(scan),
56  AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
57  { "tff", "top field first", 0,
58  AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
59  { "bff", "bottom field first", 0,
60  AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
61  { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
62  AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
63  { NULL }
64 };
65 
66 static const AVClass class = {
67  .class_name = "interlace filter",
68  .item_name = av_default_item_name,
69  .option = options,
71 };
72 
73 
74 static const enum AVPixelFormat formats_supported[] = {
79 };
80 
82 {
84  return 0;
85 }
86 
87 static av_cold void uninit(AVFilterContext *ctx)
88 {
89  InterlaceContext *s = ctx->priv;
90 
91  av_frame_free(&s->cur);
92  av_frame_free(&s->next);
93 
94  av_opt_free(s);
95 }
96 
97 static int config_out_props(AVFilterLink *outlink)
98 {
99  AVFilterContext *ctx = outlink->src;
100  AVFilterLink *inlink = outlink->src->inputs[0];
101  InterlaceContext *s = ctx->priv;
102 
103  if (inlink->h < 2) {
104  av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
105  return AVERROR_INVALIDDATA;
106  }
107 
108  if (!s->lowpass)
109  av_log(ctx, AV_LOG_WARNING, "***warning*** Lowpass filter is disabled, "
110  "the resulting video will be aliased rather than interlaced.\n");
111 
112  // same input size
113  outlink->w = inlink->w;
114  outlink->h = inlink->h;
115  outlink->time_base = inlink->time_base;
116  // half framerate
117  outlink->time_base.num *= 2;
118 
119  av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
120  s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
121 
122  return 0;
123 }
124 
125 static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
126  AVFilterLink *inlink, enum FieldType field_type,
127  int lowpass)
128 {
129  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
130  int vsub = desc->log2_chroma_h;
131  int plane, i, j;
132 
133  for (plane = 0; plane < desc->nb_components; plane++) {
134  int lines = (plane == 1 || plane == 2) ? -(-inlink->h) >> vsub : inlink->h;
135  int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
136  uint8_t *dstp = dst_frame->data[plane];
137  const uint8_t *srcp = src_frame->data[plane];
138 
139  av_assert0(linesize >= 0);
140 
141  lines = (lines + (field_type == FIELD_UPPER)) / 2;
142  if (field_type == FIELD_LOWER)
143  srcp += src_frame->linesize[plane];
144  if (field_type == FIELD_LOWER)
145  dstp += dst_frame->linesize[plane];
146  if (lowpass) {
147  int srcp_linesize = src_frame->linesize[plane] * 2;
148  int dstp_linesize = dst_frame->linesize[plane] * 2;
149  for (j = lines; j > 0; j--) {
150  const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
151  const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
152  if (j == lines)
153  srcp_above = srcp; // there is no line above
154  if (j == 1)
155  srcp_below = srcp; // there is no line below
156  for (i = 0; i < linesize; i++) {
157  // this calculation is an integer representation of
158  // '0.5 * current + 0.25 * above + 0.25 * below'
159  // '1 +' is for rounding.
160  dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
161  }
162  dstp += dstp_linesize;
163  srcp += srcp_linesize;
164  }
165  } else {
166  av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
167  srcp, src_frame->linesize[plane] * 2,
168  linesize, lines);
169  }
170  }
171 }
172 
173 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
174 {
175  AVFilterContext *ctx = inlink->dst;
176  AVFilterLink *outlink = ctx->outputs[0];
177  InterlaceContext *s = ctx->priv;
178  AVFrame *out;
179  int tff, ret;
180 
181  av_frame_free(&s->cur);
182  s->cur = s->next;
183  s->next = buf;
184 
185  /* we need at least two frames */
186  if (!s->cur || !s->next)
187  return 0;
188 
189  if (s->cur->interlaced_frame) {
190  av_log(ctx, AV_LOG_WARNING,
191  "video is already interlaced, adjusting framerate only\n");
192  out = av_frame_clone(s->cur);
193  if (!out)
194  return AVERROR(ENOMEM);
195  out->pts /= 2; // adjust pts to new framerate
196  ret = ff_filter_frame(outlink, out);
197  s->got_output = 1;
198  return ret;
199  }
200 
201  tff = (s->scan == MODE_TFF);
202  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
203  if (!out)
204  return AVERROR(ENOMEM);
205 
206  av_frame_copy_props(out, s->cur);
207  out->interlaced_frame = 1;
208  out->top_field_first = tff;
209  out->pts /= 2; // adjust pts to new framerate
210 
211  /* copy upper/lower field from cur */
212  copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
213  av_frame_free(&s->cur);
214 
215  /* copy lower/upper field from next */
216  copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
217  av_frame_free(&s->next);
218 
219  ret = ff_filter_frame(outlink, out);
220  s->got_output = 1;
221 
222  return ret;
223 }
224 
225 static int request_frame(AVFilterLink *outlink)
226 {
227  AVFilterContext *ctx = outlink->src;
228  InterlaceContext *s = ctx->priv;
229  int ret = 0;
230 
231  s->got_output = 0;
232  while (ret >= 0 && !s->got_output)
233  ret = ff_request_frame(ctx->inputs[0]);
234 
235  return ret;
236 }
237 
238 static const AVFilterPad inputs[] = {
239  {
240  .name = "default",
241  .type = AVMEDIA_TYPE_VIDEO,
242  .filter_frame = filter_frame,
243  },
244  { NULL }
245 };
246 
247 static const AVFilterPad outputs[] = {
248  {
249  .name = "default",
250  .type = AVMEDIA_TYPE_VIDEO,
251  .config_props = config_out_props,
252  .request_frame = request_frame,
253  },
254  { NULL }
255 };
256 
258  .name = "interlace",
259  .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
260  .uninit = uninit,
261 
262  .priv_class = &class,
263  .priv_size = sizeof(InterlaceContext),
265 
266  .inputs = inputs,
267  .outputs = outputs,
268 };
269 
int av_image_get_linesize(enum AVPixelFormat pix_fmt, int width, int plane)
Compute the size of an image line with format pix_fmt and width width for the plane plane...
Definition: imgutils.c:50
#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:1599
This structure describes decoded (raw) audio or video data.
Definition: frame.h:135
AVOption.
Definition: opt.h:234
static const AVFilterPad inputs[]
Definition: vf_interlace.c:238
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:70
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:129
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:104
static const AVOption options[]
Definition: vf_interlace.c:54
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:74
static int query_formats(AVFilterContext *ctx)
Definition: vf_interlace.c:81
const char * name
Pad name.
Definition: internal.h:42
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:38
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:571
static int config_out_props(AVFilterLink *outlink)
Definition: vf_interlace.c:97
#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:733
static const AVFilterPad outputs[]
Definition: vf_interlace.c:247
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition: pixfmt.h:104
uint8_t
#define av_cold
Definition: attributes.h:66
static int request_frame(AVFilterLink *outlink)
Definition: vf_interlace.c:225
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 PIX_FMT_YUV440P and setting color_range ...
Definition: pixfmt.h:103
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: pixfmt.h:78
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:139
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:320
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:379
A filter pad used for either input or output.
Definition: internal.h:36
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
#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:69
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:145
void * priv
private data for use by the filter
Definition: avfilter.h:584
FieldType
Definition: vf_interlace.c:39
AVFilter ff_vf_interlace
Definition: vf_interlace.c:257
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
AVFrame * next
Definition: vf_interlace.c:48
#define OFFSET(x)
Definition: vf_interlace.c:52
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:69
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 PIX_FMT_YUV420P and setting color_...
Definition: pixfmt.h:77
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:270
NULL
Definition: eval.c:55
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_interlace.c:87
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:153
av_default_item_name
Definition: dnxhdenc.c:52
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:71
Describe the class of an AVClass context structure.
Definition: log.h:33
Filter definition.
Definition: avfilter.h:421
const char * name
Filter name.
Definition: avfilter.h:425
ScanMode
Definition: vf_interlace.c:34
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:578
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
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_dlog(ac->avr,"%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
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:65
Y , 8bpp.
Definition: pixfmt.h:73
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:659
static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame, AVFilterLink *inlink, enum FieldType field_type, int lowpass)
Definition: vf_interlace.c:125
common internal and external API header
#define V
Definition: vf_interlace.c:53
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: pixfmt.h:79
enum ScanMode scan
Definition: vf_interlace.c:46
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:325
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: vf_interlace.c:173
An instance of a filter.
Definition: avfilter.h:563
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:249
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:254
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:63
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:367