Line | Branch | Exec | Source |
---|---|---|---|
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 | #include "video.h" | ||
26 | |||
27 | typedef struct SeparateFieldsContext { | ||
28 | int nb_planes; | ||
29 | AVFrame *second; | ||
30 | } SeparateFieldsContext; | ||
31 | |||
32 | 1 | static int config_props_output(AVFilterLink *outlink) | |
33 | { | ||
34 | 1 | AVFilterContext *ctx = outlink->src; | |
35 | 1 | SeparateFieldsContext *s = ctx->priv; | |
36 | 1 | AVFilterLink *inlink = ctx->inputs[0]; | |
37 | |||
38 | 1 | s->nb_planes = av_pix_fmt_count_planes(inlink->format); | |
39 | |||
40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (inlink->h & 1) { |
41 | ✗ | av_log(ctx, AV_LOG_ERROR, "height must be even\n"); | |
42 | ✗ | return AVERROR_INVALIDDATA; | |
43 | } | ||
44 | |||
45 | 1 | outlink->time_base.num = inlink->time_base.num; | |
46 | 1 | outlink->time_base.den = inlink->time_base.den * 2; | |
47 | 1 | outlink->frame_rate.num = inlink->frame_rate.num * 2; | |
48 | 1 | outlink->frame_rate.den = inlink->frame_rate.den; | |
49 | 1 | outlink->w = inlink->w; | |
50 | 1 | outlink->h = inlink->h / 2; | |
51 | |||
52 | 1 | return 0; | |
53 | } | ||
54 | |||
55 | 100 | static void extract_field(AVFrame *frame, int nb_planes, int type) | |
56 | { | ||
57 | int i; | ||
58 | |||
59 |
2/2✓ Branch 0 taken 300 times.
✓ Branch 1 taken 100 times.
|
400 | for (i = 0; i < nb_planes; i++) { |
60 |
2/2✓ Branch 0 taken 150 times.
✓ Branch 1 taken 150 times.
|
300 | if (type) |
61 | 150 | frame->data[i] = frame->data[i] + frame->linesize[i]; | |
62 | 300 | frame->linesize[i] *= 2; | |
63 | } | ||
64 | 100 | } | |
65 | |||
66 | 50 | static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref) | |
67 | { | ||
68 | 50 | AVFilterContext *ctx = inlink->dst; | |
69 | 50 | SeparateFieldsContext *s = ctx->priv; | |
70 | 50 | AVFilterLink *outlink = ctx->outputs[0]; | |
71 | int ret; | ||
72 | |||
73 | 50 | inpicref->height = outlink->h; | |
74 | #if FF_API_INTERLACED_FRAME | ||
75 | FF_DISABLE_DEPRECATION_WARNINGS | ||
76 | 50 | inpicref->interlaced_frame = 0; | |
77 | FF_ENABLE_DEPRECATION_WARNINGS | ||
78 | #endif | ||
79 | 50 | inpicref->flags &= ~AV_FRAME_FLAG_INTERLACED; | |
80 | |||
81 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 49 times.
|
50 | if (!s->second) { |
82 | 1 | goto clone; | |
83 | } else { | ||
84 | 49 | AVFrame *second = s->second; | |
85 | |||
86 | 49 | extract_field(second, s->nb_planes, !!(second->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)); | |
87 | |||
88 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | if (second->pts != AV_NOPTS_VALUE && |
89 |
1/2✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
|
49 | inpicref->pts != AV_NOPTS_VALUE) |
90 | 49 | second->pts += inpicref->pts; | |
91 | else | ||
92 | ✗ | second->pts = AV_NOPTS_VALUE; | |
93 | |||
94 | 49 | ret = ff_filter_frame(outlink, second); | |
95 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
|
49 | if (ret < 0) |
96 | ✗ | return ret; | |
97 | 49 | clone: | |
98 | 50 | s->second = av_frame_clone(inpicref); | |
99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
50 | if (!s->second) |
100 | ✗ | return AVERROR(ENOMEM); | |
101 | } | ||
102 | |||
103 | 50 | extract_field(inpicref, s->nb_planes, !(inpicref->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)); | |
104 | |||
105 |
1/2✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
|
50 | if (inpicref->pts != AV_NOPTS_VALUE) |
106 | 50 | inpicref->pts *= 2; | |
107 | |||
108 | 50 | return ff_filter_frame(outlink, inpicref); | |
109 | } | ||
110 | |||
111 | 1 | static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts) | |
112 | { | ||
113 | 1 | AVFilterContext *ctx = outlink->src; | |
114 | 1 | SeparateFieldsContext *s = ctx->priv; | |
115 | 1 | int ret = 0; | |
116 | |||
117 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->second) { |
118 | 1 | *out_pts = s->second->pts += pts; | |
119 | 1 | extract_field(s->second, s->nb_planes, !!(s->second->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST)); | |
120 | 1 | ret = ff_filter_frame(outlink, s->second); | |
121 | 1 | s->second = NULL; | |
122 | } | ||
123 | |||
124 | 1 | return ret; | |
125 | } | ||
126 | |||
127 | 101 | static int activate(AVFilterContext *ctx) | |
128 | { | ||
129 | 101 | AVFilterLink *inlink = ctx->inputs[0]; | |
130 | 101 | AVFilterLink *outlink = ctx->outputs[0]; | |
131 | AVFrame *in; | ||
132 | int64_t pts; | ||
133 | int ret, status; | ||
134 | |||
135 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
|
101 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
136 | |||
137 | 101 | ret = ff_inlink_consume_frame(inlink, &in); | |
138 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 101 times.
|
101 | if (ret < 0) |
139 | ✗ | return ret; | |
140 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 51 times.
|
101 | if (ret > 0) |
141 | 50 | return filter_frame(inlink, in); | |
142 | |||
143 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 50 times.
|
51 | if (ff_inlink_acknowledge_status(inlink, &status, &pts)) { |
144 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (status == AVERROR_EOF) { |
145 | 1 | int64_t out_pts = pts; | |
146 | |||
147 | 1 | ret = flush_frame(outlink, pts, &out_pts); | |
148 | 1 | ff_outlink_set_status(outlink, status, out_pts); | |
149 | 1 | return ret; | |
150 | } | ||
151 | } | ||
152 | |||
153 |
1/2✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
|
50 | FF_FILTER_FORWARD_WANTED(outlink, inlink); |
154 | |||
155 | ✗ | return FFERROR_NOT_READY; | |
156 | } | ||
157 | |||
158 | 2 | static av_cold void uninit(AVFilterContext *ctx) | |
159 | { | ||
160 | 2 | SeparateFieldsContext *s = ctx->priv; | |
161 | |||
162 | 2 | av_frame_free(&s->second); | |
163 | 2 | } | |
164 | |||
165 | static const AVFilterPad separatefields_outputs[] = { | ||
166 | { | ||
167 | .name = "default", | ||
168 | .type = AVMEDIA_TYPE_VIDEO, | ||
169 | .config_props = config_props_output, | ||
170 | }, | ||
171 | }; | ||
172 | |||
173 | const AVFilter ff_vf_separatefields = { | ||
174 | .name = "separatefields", | ||
175 | .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."), | ||
176 | .priv_size = sizeof(SeparateFieldsContext), | ||
177 | .activate = activate, | ||
178 | .uninit = uninit, | ||
179 | FILTER_INPUTS(ff_video_default_filterpad), | ||
180 | FILTER_OUTPUTS(separatefields_outputs), | ||
181 | }; | ||
182 |