vf_telecine.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Rudolf Polzer
3  * Copyright (c) 2013 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 /**
23  * @file telecine filter, heavily based from mpv-player:TOOLS/vf_dlopen/telecine.c by
24  * Rudolf Polzer.
25  */
26 
27 #include "libavutil/avstring.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35 
36 typedef struct {
37  const AVClass *class;
39  char *pattern;
40  unsigned int pattern_pos;
41 
43  double ts_unit;
44  int out_cnt;
45  int occupied;
46  int64_t frame_count;
47 
48  int nb_planes;
49  int planeheight[4];
50  int stride[4];
51 
55 
56 #define OFFSET(x) offsetof(TelecineContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
58 
59 static const AVOption telecine_options[] = {
60  {"first_field", "select first field", OFFSET(first_field), AV_OPT_TYPE_INT, {.i64=0}, 0, 1, FLAGS, "field"},
61  {"top", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
62  {"t", "select top field first", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
63  {"bottom", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
64  {"b", "select bottom field first", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
65  {"pattern", "pattern that describe for how many fields a frame is to be displayed", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str="23"}, 0, 0, FLAGS},
66  {NULL}
67 };
68 
69 AVFILTER_DEFINE_CLASS(telecine);
70 
71 static av_cold int init(AVFilterContext *ctx)
72 {
73  TelecineContext *tc = ctx->priv;
74  const char *p;
75  int max = 0;
76 
77  if (!strlen(tc->pattern)) {
78  av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
79  return AVERROR_INVALIDDATA;
80  }
81 
82  for (p = tc->pattern; *p; p++) {
83  if (!av_isdigit(*p)) {
84  av_log(ctx, AV_LOG_ERROR, "Provided pattern includes non-numeric characters.\n");
85  return AVERROR_INVALIDDATA;
86  }
87 
88  max = FFMAX(*p - '0', max);
89  tc->pts.num += 2;
90  tc->pts.den += *p - '0';
91  }
92 
93  tc->out_cnt = (max + 1) / 2;
94  av_log(ctx, AV_LOG_INFO, "Telecine pattern %s yields up to %d frames per frame, pts advance factor: %d/%d\n",
95  tc->pattern, tc->out_cnt, tc->pts.num, tc->pts.den);
96 
97  return 0;
98 }
99 
101 {
102  AVFilterFormats *pix_fmts = NULL;
103  int fmt;
104 
105  for (fmt = 0; fmt < AV_PIX_FMT_NB; fmt++) {
106  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
107  if (!(desc->flags & PIX_FMT_HWACCEL))
108  ff_add_format(&pix_fmts, fmt);
109  }
110 
111  ff_set_common_formats(ctx, pix_fmts);
112  return 0;
113 }
114 
115 static int config_input(AVFilterLink *inlink)
116 {
117  TelecineContext *tc = inlink->dst->priv;
118  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
119  int i, ret;
120 
121  tc->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
122  if (!tc->temp)
123  return AVERROR(ENOMEM);
124  for (i = 0; i < tc->out_cnt; i++) {
125  tc->frame[i] = ff_get_video_buffer(inlink, inlink->w, inlink->h);
126  if (!tc->frame[i])
127  return AVERROR(ENOMEM);
128  }
129 
130  if ((ret = av_image_fill_linesizes(tc->stride, inlink->format, inlink->w)) < 0)
131  return ret;
132 
133  tc->planeheight[1] = tc->planeheight[2] = inlink->h >> desc->log2_chroma_h;
134  tc->planeheight[0] = tc->planeheight[3] = inlink->h;
135 
137 
138  return 0;
139 }
140 
141 static int config_output(AVFilterLink *outlink)
142 {
143  AVFilterContext *ctx = outlink->src;
144  TelecineContext *tc = ctx->priv;
145  const AVFilterLink *inlink = ctx->inputs[0];
146  AVRational fps = inlink->frame_rate;
147 
148  if (!fps.num || !fps.den) {
149  av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
150  "current rate of %d/%d is invalid\n", fps.num, fps.den);
151  return AVERROR(EINVAL);
152  }
153  fps = av_mul_q(fps, av_inv_q(tc->pts));
154  av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
155  inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
156 
157  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
158  outlink->frame_rate = fps;
159  outlink->time_base = av_mul_q(inlink->time_base, tc->pts);
160 
161  tc->ts_unit = av_q2d(av_inv_q(av_mul_q(fps, outlink->time_base)));
162 
163  return 0;
164 }
165 
166 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
167 {
168  AVFilterContext *ctx = inlink->dst;
169  AVFilterLink *outlink = ctx->outputs[0];
170  TelecineContext *tc = ctx->priv;
171  int i, len, ret = 0, nout = 0;
172 
173  len = tc->pattern[tc->pattern_pos] - '0';
174 
175  tc->pattern_pos++;
176  if (!tc->pattern[tc->pattern_pos])
177  tc->pattern_pos = 0;
178 
179  if (!len) { // do not output any field from this frame
180  av_frame_free(&inpicref);
181  return 0;
182  }
183 
184  if (tc->occupied) {
185  for (i = 0; i < tc->nb_planes; i++) {
186  // fill in the EARLIER field from the buffered pic
187  av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * tc->first_field,
188  tc->frame[nout]->linesize[i] * 2,
189  tc->temp->data[i] + tc->temp->linesize[i] * tc->first_field,
190  tc->temp->linesize[i] * 2,
191  tc->stride[i],
192  (tc->planeheight[i] - tc->first_field + 1) / 2);
193  // fill in the LATER field from the new pic
194  av_image_copy_plane(tc->frame[nout]->data[i] + tc->frame[nout]->linesize[i] * !tc->first_field,
195  tc->frame[nout]->linesize[i] * 2,
196  inpicref->data[i] + inpicref->linesize[i] * !tc->first_field,
197  inpicref->linesize[i] * 2,
198  tc->stride[i],
199  (tc->planeheight[i] - !tc->first_field + 1) / 2);
200  }
201  nout++;
202  len--;
203  tc->occupied = 0;
204  }
205 
206  while (len >= 2) {
207  // output THIS image as-is
208  for (i = 0; i < tc->nb_planes; i++)
209  av_image_copy_plane(tc->frame[nout]->data[i], tc->frame[nout]->linesize[i],
210  inpicref->data[i], inpicref->linesize[i],
211  tc->stride[i],
212  tc->planeheight[i]);
213  nout++;
214  len -= 2;
215  }
216 
217  if (len >= 1) {
218  // copy THIS image to the buffer, we need it later
219  for (i = 0; i < tc->nb_planes; i++)
220  av_image_copy_plane(tc->temp->data[i], tc->temp->linesize[i],
221  inpicref->data[i], inpicref->linesize[i],
222  tc->stride[i],
223  tc->planeheight[i]);
224  tc->occupied = 1;
225  }
226 
227  for (i = 0; i < nout; i++) {
228  AVFrame *frame = av_frame_clone(tc->frame[i]);
229 
230  if (!frame) {
231  av_frame_free(&inpicref);
232  return AVERROR(ENOMEM);
233  }
234 
235  av_frame_copy_props(frame, inpicref);
236  frame->pts = tc->frame_count++ * tc->ts_unit;
237  ret = ff_filter_frame(outlink, frame);
238  }
239  av_frame_free(&inpicref);
240 
241  return ret;
242 }
243 
244 static av_cold void uninit(AVFilterContext *ctx)
245 {
246  TelecineContext *tc = ctx->priv;
247  int i;
248 
249  av_frame_free(&tc->temp);
250  for (i = 0; i < tc->out_cnt; i++)
251  av_frame_free(&tc->frame[i]);
252 }
253 
254 static const AVFilterPad telecine_inputs[] = {
255  {
256  .name = "default",
257  .type = AVMEDIA_TYPE_VIDEO,
258  .filter_frame = filter_frame,
259  .config_props = config_input,
260  },
261  { NULL }
262 };
263 
264 static const AVFilterPad telecine_outputs[] = {
265  {
266  .name = "default",
267  .type = AVMEDIA_TYPE_VIDEO,
268  .config_props = config_output,
269  },
270  { NULL }
271 };
272 
274  .name = "telecine",
275  .description = NULL_IF_CONFIG_SMALL("Apply a telecine pattern."),
276  .priv_size = sizeof(TelecineContext),
277  .priv_class = &telecine_class,
278  .init = init,
279  .uninit = uninit,
281  .inputs = telecine_inputs,
282  .outputs = telecine_outputs,
283 };
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:424
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1778
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
AVOption.
Definition: opt.h:251
const char * fmt
Definition: avisynth_c.h:669
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:1818
external API header
if max(w)>1 w=0.9 *w/max(w)
int num
numerator
Definition: rational.h:44
#define tc
Definition: regdef.h:69
int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.c:288
static int config_input(AVFilterLink *inlink)
Definition: vf_telecine.c:115
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:143
int stride
Definition: mace.c:144
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
int64_t frame_count
Definition: vf_telecine.c:46
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:545
int planeheight[4]
Definition: vf_telecine.c:49
static const AVFilterPad telecine_outputs[]
Definition: vf_telecine.c:264
frame
Definition: stft.m:14
A filter pad used for either input or output.
AVRational pts
Definition: vf_telecine.c:42
static int config_output(AVFilterLink *outlink)
Definition: vf_telecine.c:141
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:75
static av_cold int init(AVFilterContext *ctx)
Definition: vf_telecine.c:71
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
#define OFFSET(x)
Definition: vf_telecine.c:56
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:344
#define FFMAX(a, b)
Definition: common.h:56
#define AV_LOG_VERBOSE
Definition: log.h:157
ret
Definition: avfilter.c:821
#define FLAGS
Definition: vf_telecine.c:57
AVFrame * temp
Definition: vf_telecine.c:53
unsigned int pattern_pos
Definition: vf_telecine.c:40
AVFILTER_DEFINE_CLASS(telecine)
AVFrame * av_frame_clone(AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:317
#define PIX_FMT_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:92
NULL
Definition: eval.c:55
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:101
uint8_t flags
Definition: pixdesc.h:76
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
Describe the class of an AVClass context structure.
Definition: log.h:50
Filter definition.
Definition: avfilter.h:436
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:86
synthesis window for stochastic i
rational number numerator/denominator
Definition: rational.h:43
const char * name
filter name
Definition: avfilter.h:437
AVFrame * frame[5]
Definition: vf_telecine.c:52
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:122
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
static const AVOption telecine_options[]
Definition: vf_telecine.c:59
static const AVFilterPad telecine_inputs[]
Definition: vf_telecine.c:254
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
int den
denominator
Definition: rational.h:45
int len
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_telecine.c:244
A list of supported formats for one end of a filter link.
Definition: formats.h:64
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_telecine.c:166
An instance of a filter.
Definition: avfilter.h:524
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:237
#define AV_LOG_INFO
Definition: log.h:156
AVFilter avfilter_vf_telecine
Definition: vf_telecine.c:273
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
static int first_field(int fd)
Definition: v4l2.c:262
internal API functions
static int query_formats(AVFilterContext *ctx)
Definition: vf_telecine.c:100
Frame requests may need to loop in order to be fulfilled.
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs