]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_separatefields.c
74ebd35bd14d98b91fddd90c73ee14f698090b5d
[ffmpeg] / libavfilter / vf_separatefields.c
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "libavutil/pixdesc.h"
22 #include "avfilter.h"
23 #include "filters.h"
24 #include "internal.h"
25
26 typedef struct SeparateFieldsContext {
27     int nb_planes;
28     AVFrame *second;
29 } SeparateFieldsContext;
30
31 static int config_props_output(AVFilterLink *outlink)
32 {
33     AVFilterContext *ctx = outlink->src;
34     SeparateFieldsContext *s = ctx->priv;
35     AVFilterLink *inlink = ctx->inputs[0];
36
37     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
38
39     if (inlink->h & 1) {
40         av_log(ctx, AV_LOG_ERROR, "height must be even\n");
41         return AVERROR_INVALIDDATA;
42     }
43
44     outlink->time_base.num = inlink->time_base.num;
45     outlink->time_base.den = inlink->time_base.den * 2;
46     outlink->frame_rate.num = inlink->frame_rate.num * 2;
47     outlink->frame_rate.den = inlink->frame_rate.den;
48     outlink->w = inlink->w;
49     outlink->h = inlink->h / 2;
50
51     return 0;
52 }
53
54 static void extract_field(AVFrame *frame, int nb_planes, int type)
55 {
56     int i;
57
58     for (i = 0; i < nb_planes; i++) {
59         if (type)
60             frame->data[i] = frame->data[i] + frame->linesize[i];
61         frame->linesize[i] *= 2;
62     }
63 }
64
65 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
66 {
67     AVFilterContext *ctx = inlink->dst;
68     SeparateFieldsContext *s = ctx->priv;
69     AVFilterLink *outlink = ctx->outputs[0];
70     int ret;
71
72     inpicref->height = outlink->h;
73     inpicref->interlaced_frame = 0;
74
75     if (!s->second) {
76         goto clone;
77     } else {
78         AVFrame *second = s->second;
79
80         extract_field(second, s->nb_planes, second->top_field_first);
81
82         if (second->pts != AV_NOPTS_VALUE &&
83             inpicref->pts != AV_NOPTS_VALUE)
84             second->pts += inpicref->pts;
85         else
86             second->pts = AV_NOPTS_VALUE;
87
88         ret = ff_filter_frame(outlink, second);
89         if (ret < 0)
90             return ret;
91 clone:
92         s->second = av_frame_clone(inpicref);
93         if (!s->second)
94             return AVERROR(ENOMEM);
95     }
96
97     extract_field(inpicref, s->nb_planes, !inpicref->top_field_first);
98
99     if (inpicref->pts != AV_NOPTS_VALUE)
100         inpicref->pts *= 2;
101
102     return ff_filter_frame(outlink, inpicref);
103 }
104
105 static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
106 {
107     AVFilterContext *ctx = outlink->src;
108     SeparateFieldsContext *s = ctx->priv;
109     int ret = 0;
110
111     if (s->second) {
112         *out_pts = s->second->pts += pts;
113         extract_field(s->second, s->nb_planes, s->second->top_field_first);
114         ret = ff_filter_frame(outlink, s->second);
115         s->second = NULL;
116     }
117
118     return ret;
119 }
120
121 static int activate(AVFilterContext *ctx)
122 {
123     AVFilterLink *inlink = ctx->inputs[0];
124     AVFilterLink *outlink = ctx->outputs[0];
125     AVFrame *in;
126     int64_t pts;
127     int ret, status;
128
129     FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
130
131     ret = ff_inlink_consume_frame(inlink, &in);
132     if (ret < 0)
133         return ret;
134     if (ret > 0)
135         return filter_frame(inlink, in);
136
137     if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
138         if (status == AVERROR_EOF) {
139             int64_t out_pts = pts;
140
141             ret = flush_frame(outlink, pts, &out_pts);
142             ff_outlink_set_status(outlink, status, out_pts);
143             return ret;
144         }
145     }
146
147     FF_FILTER_FORWARD_WANTED(outlink, inlink);
148
149     return FFERROR_NOT_READY;
150 }
151
152 static av_cold void uninit(AVFilterContext *ctx)
153 {
154     SeparateFieldsContext *s = ctx->priv;
155
156     av_frame_free(&s->second);
157 }
158
159 static const AVFilterPad separatefields_inputs[] = {
160     {
161         .name         = "default",
162         .type         = AVMEDIA_TYPE_VIDEO,
163     },
164     { NULL }
165 };
166
167 static const AVFilterPad separatefields_outputs[] = {
168     {
169         .name          = "default",
170         .type          = AVMEDIA_TYPE_VIDEO,
171         .config_props  = config_props_output,
172     },
173     { NULL }
174 };
175
176 AVFilter ff_vf_separatefields = {
177     .name        = "separatefields",
178     .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
179     .priv_size   = sizeof(SeparateFieldsContext),
180     .activate    = activate,
181     .uninit      = uninit,
182     .inputs      = separatefields_inputs,
183     .outputs     = separatefields_outputs,
184 };