Browse code

lavfi: add inverse telecine filter

Approved-by: Paul B Mahol <onemda@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Himangi Saraogi authored on 2015/03/27 23:06:05
Showing 5 changed files
... ...
@@ -9,6 +9,7 @@ version <next>:
9 9
 - libdcadec wrapper
10 10
 - Drop support for nvenc API before 5.0
11 11
 - nvenc H265 encoder
12
+- Detelecine filter
12 13
 
13 14
 
14 15
 version 2.6:
... ...
@@ -3728,6 +3728,33 @@ FFmpeg was configured with @code{--enable-opencl}. Default value is 0.
3728 3728
 
3729 3729
 @end table
3730 3730
 
3731
+@section detelecine
3732
+
3733
+Apply an exact inverse of the telecine operation. It requires a predefined
3734
+pattern specified using the pattern option which must be the same as that passed
3735
+to the telecine filter.
3736
+
3737
+This filter accepts the following options:
3738
+
3739
+@table @option
3740
+@item first_field
3741
+@table @samp
3742
+@item top, t
3743
+top field first
3744
+@item bottom, b
3745
+bottom field first
3746
+The default value is @code{top}.
3747
+@end table
3748
+
3749
+@item pattern
3750
+A string of numbers representing the pulldown pattern you wish to apply.
3751
+The default value is @code{23}.
3752
+
3753
+@item start_frame
3754
+A number representing position of the first frame with respect to the telecine
3755
+pattern. This is to be used if the stream is cut. The default value is @code{0}.
3756
+@end table
3757
+
3731 3758
 @section drawbox
3732 3759
 
3733 3760
 Draw a colored box on the input image.
... ...
@@ -112,6 +112,7 @@ OBJS-$(CONFIG_DECIMATE_FILTER)               += vf_decimate.o
112 112
 OBJS-$(CONFIG_DEJUDDER_FILTER)               += vf_dejudder.o
113 113
 OBJS-$(CONFIG_DELOGO_FILTER)                 += vf_delogo.o
114 114
 OBJS-$(CONFIG_DESHAKE_FILTER)                += vf_deshake.o
115
+OBJS-$(CONFIG_DETELECINE_FILTER)             += vf_detelecine.o
115 116
 OBJS-$(CONFIG_DRAWBOX_FILTER)                += vf_drawbox.o
116 117
 OBJS-$(CONFIG_DRAWGRID_FILTER)               += vf_drawbox.o
117 118
 OBJS-$(CONFIG_DRAWTEXT_FILTER)               += vf_drawtext.o
... ...
@@ -128,6 +128,7 @@ void avfilter_register_all(void)
128 128
     REGISTER_FILTER(DEJUDDER,       dejudder,       vf);
129 129
     REGISTER_FILTER(DELOGO,         delogo,         vf);
130 130
     REGISTER_FILTER(DESHAKE,        deshake,        vf);
131
+    REGISTER_FILTER(DETELECINE,     detelecine,     vf);
131 132
     REGISTER_FILTER(DRAWBOX,        drawbox,        vf);
132 133
     REGISTER_FILTER(DRAWGRID,       drawgrid,       vf);
133 134
     REGISTER_FILTER(DRAWTEXT,       drawtext,       vf);
134 135
new file mode 100644
... ...
@@ -0,0 +1,346 @@
0
+/*
1
+ * Copyright (c) 2015 Himangi Saraogi <himangi774@gmail.com>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file detelecine filter.
22
+ */
23
+
24
+
25
+#include "libavutil/avstring.h"
26
+#include "libavutil/imgutils.h"
27
+#include "libavutil/opt.h"
28
+#include "libavutil/pixdesc.h"
29
+#include "avfilter.h"
30
+#include "formats.h"
31
+#include "internal.h"
32
+#include "video.h"
33
+
34
+typedef struct {
35
+    const AVClass *class;
36
+    int first_field;
37
+    char *pattern;
38
+    int start_frame;
39
+    unsigned int pattern_pos;
40
+    unsigned int nskip_fields;
41
+    int64_t start_time;
42
+
43
+    AVRational pts;
44
+    AVRational ts_unit;
45
+    int occupied;
46
+
47
+    int nb_planes;
48
+    int planeheight[4];
49
+    int stride[4];
50
+
51
+    AVFrame *frame;
52
+    AVFrame *temp;
53
+} DetelecineContext;
54
+
55
+#define OFFSET(x) offsetof(DetelecineContext, x)
56
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
57
+
58
+static const AVOption detelecine_options[] = {
59
+    {"first_field", "select first field", OFFSET(first_field), AV_OPT_TYPE_INT,   {.i64=0}, 0, 1, FLAGS, "field"},
60
+        {"top",    "select top field first",                0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
61
+        {"t",      "select top field first",                0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, "field"},
62
+        {"bottom", "select bottom field first",             0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
63
+        {"b",      "select bottom field first",             0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, "field"},
64
+    {"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},
65
+    {"start_frame", "position of first frame with respect to the pattern if stream is cut", OFFSET(start_frame), AV_OPT_TYPE_INT, {.i64=0}, 0, 13, FLAGS},
66
+    {NULL}
67
+};
68
+
69
+AVFILTER_DEFINE_CLASS(detelecine);
70
+
71
+static av_cold int init(AVFilterContext *ctx)
72
+{
73
+    DetelecineContext *s = ctx->priv;
74
+    const char *p;
75
+    int max = 0;
76
+
77
+    if (!strlen(s->pattern)) {
78
+        av_log(ctx, AV_LOG_ERROR, "No pattern provided.\n");
79
+        return AVERROR_INVALIDDATA;
80
+    }
81
+
82
+    for (p = s->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
+        s->pts.num += *p - '0';
90
+        s->pts.den += 2;
91
+    }
92
+
93
+    s->nskip_fields = 0;
94
+    s->pattern_pos = 0;
95
+    s->start_time = AV_NOPTS_VALUE;
96
+
97
+    if (s->start_frame != 0) {
98
+        int nfields = 0;
99
+        for (p = s->pattern; *p; p++) {
100
+            nfields += *p - '0';
101
+            s->pattern_pos++;
102
+            if (nfields >= 2*s->start_frame) {
103
+                s->nskip_fields = nfields - 2*s->start_frame;
104
+                break;
105
+            }
106
+        }
107
+    }
108
+
109
+    av_log(ctx, AV_LOG_INFO, "Detelecine pattern %s removes up to %d frames per frame, pts advance factor: %d/%d\n",
110
+           s->pattern, (max + 1) / 2, s->pts.num, s->pts.den);
111
+
112
+    return 0;
113
+}
114
+
115
+static int query_formats(AVFilterContext *ctx)
116
+{
117
+    AVFilterFormats *pix_fmts = NULL;
118
+    int fmt;
119
+
120
+    for (fmt = 0; av_pix_fmt_desc_get(fmt); fmt++) {
121
+        const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
122
+        if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
123
+              desc->flags & AV_PIX_FMT_FLAG_PAL     ||
124
+              desc->flags & AV_PIX_FMT_FLAG_BITSTREAM))
125
+            ff_add_format(&pix_fmts, fmt);
126
+    }
127
+
128
+    ff_set_common_formats(ctx, pix_fmts);
129
+    return 0;
130
+}
131
+
132
+static int config_input(AVFilterLink *inlink)
133
+{
134
+    DetelecineContext *s = inlink->dst->priv;
135
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
136
+    int ret;
137
+
138
+    s->temp = ff_get_video_buffer(inlink, inlink->w, inlink->h);
139
+    if (!s->temp)
140
+        return AVERROR(ENOMEM);
141
+
142
+    s->frame = ff_get_video_buffer(inlink, inlink->w, inlink->h);
143
+    if (!s->frame)
144
+        return AVERROR(ENOMEM);
145
+
146
+    if ((ret = av_image_fill_linesizes(s->stride, inlink->format, inlink->w)) < 0)
147
+        return ret;
148
+
149
+    s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
150
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
151
+
152
+    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
153
+
154
+    return 0;
155
+}
156
+
157
+static int config_output(AVFilterLink *outlink)
158
+{
159
+    AVFilterContext *ctx = outlink->src;
160
+    DetelecineContext *s = ctx->priv;
161
+    const AVFilterLink *inlink = ctx->inputs[0];
162
+    AVRational fps = inlink->frame_rate;
163
+
164
+    if (!fps.num || !fps.den) {
165
+        av_log(ctx, AV_LOG_ERROR, "The input needs a constant frame rate; "
166
+               "current rate of %d/%d is invalid\n", fps.num, fps.den);
167
+        return AVERROR(EINVAL);
168
+    }
169
+    fps = av_mul_q(fps, av_inv_q(s->pts));
170
+    av_log(ctx, AV_LOG_VERBOSE, "FPS: %d/%d -> %d/%d\n",
171
+           inlink->frame_rate.num, inlink->frame_rate.den, fps.num, fps.den);
172
+
173
+    outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
174
+    outlink->frame_rate = fps;
175
+    outlink->time_base = av_mul_q(inlink->time_base, s->pts);
176
+    av_log(ctx, AV_LOG_VERBOSE, "TB: %d/%d -> %d/%d\n",
177
+           inlink->time_base.num, inlink->time_base.den, outlink->time_base.num, outlink->time_base.den);
178
+
179
+    s->ts_unit = av_inv_q(av_mul_q(fps, outlink->time_base));
180
+
181
+    return 0;
182
+}
183
+
184
+static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
185
+{
186
+    AVFilterContext *ctx = inlink->dst;
187
+    AVFilterLink *outlink = ctx->outputs[0];
188
+    DetelecineContext *s = ctx->priv;
189
+    int i, len = 0, ret = 0, out = 0;
190
+
191
+    if (s->start_time == AV_NOPTS_VALUE)
192
+        s->start_time = inpicref->pts;
193
+
194
+    if (s->nskip_fields >= 2) {
195
+        s->nskip_fields -= 2;
196
+        return 0;
197
+    } else if (s->nskip_fields >= 1) {
198
+        if (s->occupied) {
199
+            s->occupied = 0;
200
+            s->nskip_fields--;
201
+        }
202
+        else {
203
+            for (i = 0; i < s->nb_planes; i++) {
204
+                av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
205
+                                    inpicref->data[i], inpicref->linesize[i],
206
+                                    s->stride[i],
207
+                                    s->planeheight[i]);
208
+            }
209
+            s->occupied = 1;
210
+            s->nskip_fields--;
211
+            return 0;
212
+        }
213
+    }
214
+
215
+    if (s->nskip_fields == 0) {
216
+        while(!len && s->pattern[s->pattern_pos]) {
217
+            len = s->pattern[s->pattern_pos] - '0';
218
+            s->pattern_pos++;
219
+        }
220
+
221
+        if (!s->pattern[s->pattern_pos])
222
+            s->pattern_pos = 0;
223
+
224
+        if(!len) { // do not output any field as the entire pattern is zero
225
+            av_frame_free(&inpicref);
226
+            return 0;
227
+        }
228
+
229
+        if (s->occupied) {
230
+            for (i = 0; i < s->nb_planes; i++) {
231
+                // fill in the EARLIER field from the new pic
232
+                av_image_copy_plane(s->frame->data[i] + s->frame->linesize[i] * s->first_field,
233
+                                    s->frame->linesize[i] * 2,
234
+                                    inpicref->data[i] + inpicref->linesize[i] * s->first_field,
235
+                                    inpicref->linesize[i] * 2,
236
+                                    s->stride[i],
237
+                                    (s->planeheight[i] - s->first_field + 1) / 2);
238
+                // fill in the LATER field from the buffered pic
239
+                av_image_copy_plane(s->frame->data[i] + s->frame->linesize[i] * !s->first_field,
240
+                                    s->frame->linesize[i] * 2,
241
+                                    s->temp->data[i] + s->temp->linesize[i] * !s->first_field,
242
+                                    s->temp->linesize[i] * 2,
243
+                                    s->stride[i],
244
+                                    (s->planeheight[i] - !s->first_field + 1) / 2);
245
+            }
246
+            len -= 2;
247
+            for (i = 0; i < s->nb_planes; i++) {
248
+                av_image_copy_plane(s->temp->data[i], s->temp->linesize[i],
249
+                                    inpicref->data[i], inpicref->linesize[i],
250
+                                    s->stride[i],
251
+                                    s->planeheight[i]);
252
+            }
253
+            s->occupied = 1;
254
+            out = 1;
255
+        } else {
256
+            if (len >= 2) {
257
+                // output THIS image as-is
258
+                for (i = 0; i < s->nb_planes; i++)
259
+                    av_image_copy_plane(s->frame->data[i], s->frame->linesize[i],
260
+                                        inpicref->data[i], inpicref->linesize[i],
261
+                                        s->stride[i],
262
+                                        s->planeheight[i]);
263
+                len -= 2;
264
+                out = 1;
265
+            } else if (len == 1) {
266
+                // fill in the EARLIER field from the new pic
267
+                av_image_copy_plane(s->frame->data[i] + s->frame->linesize[i] * s->first_field,
268
+                                    s->frame->linesize[i] * 2,
269
+                                    inpicref->data[i] + inpicref->linesize[i] * s->first_field,
270
+                                    inpicref->linesize[i] * 2,
271
+                                    s->stride[i],
272
+                                    (s->planeheight[i] - s->first_field + 1) / 2);
273
+                // TODO: not sure about the other field
274
+
275
+                len--;
276
+                out = 1;
277
+            }
278
+        }
279
+
280
+        if (len == 1 && s->occupied)
281
+        {
282
+            len--;
283
+            s->occupied = 0;
284
+        }
285
+    }
286
+    s->nskip_fields = len;
287
+
288
+    if (out) {
289
+        AVFrame *frame = av_frame_clone(s->frame);
290
+
291
+        if (!frame) {
292
+            av_frame_free(&inpicref);
293
+            return AVERROR(ENOMEM);
294
+        }
295
+
296
+        av_frame_copy_props(frame, inpicref);
297
+        frame->pts = ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time) +
298
+                     av_rescale(outlink->frame_count, s->ts_unit.num,
299
+                                s->ts_unit.den);
300
+        ret = ff_filter_frame(outlink, frame);
301
+    }
302
+
303
+    av_frame_free(&inpicref);
304
+
305
+    return ret;
306
+}
307
+
308
+static av_cold void uninit(AVFilterContext *ctx)
309
+{
310
+    DetelecineContext *s = ctx->priv;
311
+
312
+    av_frame_free(&s->temp);
313
+    av_frame_free(&s->frame);
314
+}
315
+
316
+static const AVFilterPad detelecine_inputs[] = {
317
+    {
318
+        .name          = "default",
319
+        .type          = AVMEDIA_TYPE_VIDEO,
320
+        .filter_frame  = filter_frame,
321
+        .config_props  = config_input,
322
+    },
323
+    { NULL }
324
+};
325
+
326
+static const AVFilterPad detelecine_outputs[] = {
327
+    {
328
+        .name          = "default",
329
+        .type          = AVMEDIA_TYPE_VIDEO,
330
+        .config_props  = config_output,
331
+    },
332
+    { NULL }
333
+};
334
+
335
+AVFilter ff_vf_detelecine = {
336
+    .name          = "detelecine",
337
+    .description   = NULL_IF_CONFIG_SMALL("Apply an inverse telecine pattern."),
338
+    .priv_size     = sizeof(DetelecineContext),
339
+    .priv_class    = &detelecine_class,
340
+    .init          = init,
341
+    .uninit        = uninit,
342
+    .query_formats = query_formats,
343
+    .inputs        = detelecine_inputs,
344
+    .outputs       = detelecine_outputs,
345
+};