FFmpeg  2.1.1
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 "internal.h"
37 #include "video.h"
38 
39 enum ScanMode {
40  MODE_TFF = 0,
41  MODE_BFF = 1,
42 };
43 
44 enum FieldType {
47 };
48 
49 typedef struct {
50  const AVClass *class;
51  enum ScanMode scan; // top or bottom field first scanning
52  int lowpass; // enable or disable low pass filterning
53  AVFrame *cur, *next; // the two frames from which the new one is obtained
55 
56 #define OFFSET(x) offsetof(InterlaceContext, x)
57 #define V AV_OPT_FLAG_VIDEO_PARAM
58 static const AVOption interlace_options[] = {
59  { "scan", "scanning mode", OFFSET(scan),
60  AV_OPT_TYPE_INT, {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
61  { "tff", "top field first", 0,
62  AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
63  { "bff", "bottom field first", 0,
64  AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
65  { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
66  AV_OPT_TYPE_INT, {.i64 = 1 }, 0, 1, .flags = V },
67  { NULL }
68 };
69 
70 AVFILTER_DEFINE_CLASS(interlace);
71 
72 static const enum AVPixelFormat formats_supported[] = {
77 };
78 
80 {
82  return 0;
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  // same input size
104  outlink->w = inlink->w;
105  outlink->h = inlink->h;
106  outlink->time_base = inlink->time_base;
107  outlink->frame_rate = inlink->frame_rate;
108  // half framerate
109  outlink->time_base.num *= 2;
110  outlink->frame_rate.den *= 2;
111  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
112 
113  av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
114  s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
115 
116  return 0;
117 }
118 
119 static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame,
120  AVFilterLink *inlink, enum FieldType field_type,
121  int lowpass)
122 {
123  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
124  int vsub = desc->log2_chroma_h;
125  int plane, i, j;
126 
127  for (plane = 0; plane < desc->nb_components; plane++) {
128  int lines = (plane == 1 || plane == 2) ? FF_CEIL_RSHIFT(inlink->h, vsub) : inlink->h;
129  int linesize = av_image_get_linesize(inlink->format, inlink->w, plane);
130  uint8_t *dstp = dst_frame->data[plane];
131  const uint8_t *srcp = src_frame->data[plane];
132 
133  av_assert0(linesize >= 0);
134 
135  lines = (lines + (field_type == FIELD_UPPER)) / 2;
136  if (field_type == FIELD_LOWER)
137  srcp += src_frame->linesize[plane];
138  if (field_type == FIELD_LOWER)
139  dstp += dst_frame->linesize[plane];
140  if (lowpass) {
141  int srcp_linesize = src_frame->linesize[plane] * 2;
142  int dstp_linesize = dst_frame->linesize[plane] * 2;
143  for (j = lines; j > 0; j--) {
144  const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
145  const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
146  if (j == lines)
147  srcp_above = srcp; // there is no line above
148  if (j == 1)
149  srcp_below = srcp; // there is no line below
150  for (i = 0; i < linesize; i++) {
151  // this calculation is an integer representation of
152  // '0.5 * current + 0.25 * above + 0.25 * below'
153  // '1 +' is for rounding.
154  dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
155  }
156  dstp += dstp_linesize;
157  srcp += srcp_linesize;
158  }
159  } else {
160  av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
161  srcp, src_frame->linesize[plane] * 2,
162  linesize, lines);
163  }
164  }
165 }
166 
167 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
168 {
169  AVFilterContext *ctx = inlink->dst;
170  AVFilterLink *outlink = ctx->outputs[0];
171  InterlaceContext *s = ctx->priv;
172  AVFrame *out;
173  int tff, ret;
174 
175  av_frame_free(&s->cur);
176  s->cur = s->next;
177  s->next = buf;
178 
179  /* we need at least two frames */
180  if (!s->cur || !s->next)
181  return 0;
182 
183  if (s->cur->interlaced_frame) {
184  av_log(ctx, AV_LOG_WARNING,
185  "video is already interlaced, adjusting framerate only\n");
186  out = av_frame_clone(s->cur);
187  out->pts /= 2; // adjust pts to new framerate
188  ret = ff_filter_frame(outlink, out);
189  return ret;
190  }
191 
192  tff = (s->scan == MODE_TFF);
193  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
194  if (!out)
195  return AVERROR(ENOMEM);
196 
197  av_frame_copy_props(out, s->cur);
198  out->interlaced_frame = 1;
199  out->top_field_first = tff;
200  out->pts /= 2; // adjust pts to new framerate
201 
202  /* copy upper/lower field from cur */
203  copy_picture_field(s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
204  av_frame_free(&s->cur);
205 
206  /* copy lower/upper field from next */
207  copy_picture_field(s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
208  av_frame_free(&s->next);
209 
210  ret = ff_filter_frame(outlink, out);
211 
212  return ret;
213 }
214 
215 static const AVFilterPad inputs[] = {
216  {
217  .name = "default",
218  .type = AVMEDIA_TYPE_VIDEO,
219  .filter_frame = filter_frame,
220  },
221  { NULL }
222 };
223 
224 static const AVFilterPad outputs[] = {
225  {
226  .name = "default",
227  .type = AVMEDIA_TYPE_VIDEO,
228  .config_props = config_out_props,
229  },
230  { NULL }
231 };
232 
234  .name = "interlace",
235  .description = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
236  .uninit = uninit,
237  .priv_class = &interlace_class,
238  .priv_size = sizeof(InterlaceContext),
240  .inputs = inputs,
241  .outputs = outputs,
242 };
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:73
const char * s
Definition: avisynth_c.h:668
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVOption.
Definition: opt.h:253
const char * name
Filter name.
Definition: avfilter.h:468
static const AVFilterPad inputs[]
Definition: vf_interlace.c:215
void * priv
private data for use by the filter
Definition: avfilter.h:648
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
planar YUV 4:2:2, 16bpp, (1 Cr &amp; Cb sample per 2x1 Y samples)
Definition: avcodec.h:4538
int num
numerator
Definition: rational.h:44
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
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
Pixel format.
Definition: avcodec.h:4533
#define av_cold
Definition: avcodec.h:653
Y , 8bpp.
Definition: avcodec.h:4542
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
static enum AVPixelFormat formats_supported[]
Definition: vf_interlace.c:72
static int query_formats(AVFilterContext *ctx)
Definition: vf_interlace.c:79
BYTE int const BYTE * srcp
Definition: avisynth_c.h:713
const char * name
Pad name.
Definition: internal.h:66
static int config_out_props(AVFilterLink *outlink)
Definition: vf_interlace.c:93
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
static const AVFilterPad outputs[]
Definition: vf_interlace.c:224
uint8_t
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only &quot;metadata&quot; fields from src to dst.
Definition: frame.c:446
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:182
#define FF_CEIL_RSHIFT(a, b)
Definition: avcodec.h:916
#define AV_LOG_VERBOSE
Detailed information.
Definition: avcodec.h:4163
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:293
planar YUV 4:2:0, 20bpp, (1 Cr &amp; Cb sample per 2x2 Y &amp; A samples)
Definition: avcodec.h:4571
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:531
A filter pad used for either input or output.
Definition: internal.h:60
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
Frame requests may need to loop in order to be fulfilled.
Definition: internal.h:347
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:77
BYTE * dstp
Definition: avisynth_c.h:713
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
Definition: avcodec.h:4548
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
Definition: avcodec.h:4570
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:123
FieldType
Definition: vf_field.c:32
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
AVFrame * next
Definition: vf_interlace.c:53
#define OFFSET(x)
Definition: vf_interlace.c:56
#define V
Definition: options_table.h:35
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:59
ret
Definition: avfilter.c:961
Main libavfilter public API header.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1938
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
Definition: avcodec.h:4546
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
planar YUV 4:2:0, 12bpp, (1 Cr &amp; Cb sample per 2x2 Y samples)
Definition: avcodec.h:4534
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_interlace.c:85
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:57
void * buf
Definition: avisynth_c.h:594
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:464
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:635
planar YUV 4:4:4, 24bpp, (1 Cr &amp; Cb sample per 1x1 Y samples)
Definition: avcodec.h:4539
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:124
ScanMode
Definition: vf_interlace.c:39
AVFrame * av_frame_clone(AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:338
static void copy_picture_field(AVFrame *src_frame, AVFrame *dst_frame, AVFilterLink *inlink, enum FieldType field_type, int lowpass)
Definition: vf_interlace.c:119
static const AVOption interlace_options[]
Definition: vf_interlace.c:58
enum ScanMode scan
Definition: vf_interlace.c:51
int den
denominator
Definition: rational.h:45
#define AVERROR_INVALIDDATA
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:298
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
Definition: vf_interlace.c:167
#define AVERROR(e)
An instance of a filter.
Definition: avfilter.h:627
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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:242
AVFilter avfilter_vf_interlace
Definition: vf_interlace.c:233
internal API functions
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
Definition: avcodec.h:4547
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
planar YUV 4:1:0, 9bpp, (1 Cr &amp; Cb sample per 4x4 Y samples)
Definition: avcodec.h:4540