]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_telecine.c
lavf/gif: trim unnecessarily long netscape ext code.
[ffmpeg] / libavfilter / vf_telecine.c
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;
38     int first_field;
39     char *pattern;
40     unsigned int pattern_pos;
41
42     AVRational pts;
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
52     AVFrame *frame[5];
53     AVFrame *temp;
54 } TelecineContext;
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
100 static int query_formats(AVFilterContext *ctx)
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
136     tc->nb_planes = av_pix_fmt_count_planes(inlink->format);
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
273 AVFilter avfilter_vf_telecine = {
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,
280     .query_formats = query_formats,
281     .inputs        = telecine_inputs,
282     .outputs       = telecine_outputs,
283 };