Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2011 Mark Himsley | ||
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 | /** | ||
22 | * @file | ||
23 | * video field order filter, heavily influenced by vf_pad.c | ||
24 | */ | ||
25 | |||
26 | #include "libavutil/imgutils.h" | ||
27 | #include "libavutil/internal.h" | ||
28 | #include "libavutil/opt.h" | ||
29 | #include "libavutil/pixdesc.h" | ||
30 | #include "avfilter.h" | ||
31 | #include "filters.h" | ||
32 | #include "formats.h" | ||
33 | #include "video.h" | ||
34 | |||
35 | typedef struct FieldOrderContext { | ||
36 | const AVClass *class; | ||
37 | int dst_tff; ///< output bff/tff | ||
38 | int line_size[4]; ///< bytes of pixel data per line for each plane | ||
39 | } FieldOrderContext; | ||
40 | |||
41 | 158 | static int query_formats(const AVFilterContext *ctx, | |
42 | AVFilterFormatsConfig **cfg_in, | ||
43 | AVFilterFormatsConfig **cfg_out) | ||
44 | { | ||
45 | 158 | const AVPixFmtDescriptor *desc = NULL; | |
46 | AVFilterFormats *formats; | ||
47 | int ret; | ||
48 | |||
49 | /** accept any input pixel format that is not hardware accelerated, not | ||
50 | * a bitstream format, and does not have vertically sub-sampled chroma */ | ||
51 | 158 | formats = NULL; | |
52 |
2/2✓ Branch 1 taken 38394 times.
✓ Branch 2 taken 158 times.
|
38552 | while ((desc = av_pix_fmt_desc_next(desc))) { |
53 | 38394 | enum AVPixelFormat pix_fmt = av_pix_fmt_desc_get_id(desc); | |
54 |
2/2✓ Branch 0 taken 36182 times.
✓ Branch 1 taken 2212 times.
|
38394 | if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL || |
55 |
2/2✓ Branch 0 taken 36024 times.
✓ Branch 1 taken 158 times.
|
36182 | desc->flags & AV_PIX_FMT_FLAG_PAL || |
56 |
2/2✓ Branch 0 taken 35076 times.
✓ Branch 1 taken 948 times.
|
36024 | desc->flags & AV_PIX_FMT_FLAG_BITSTREAM) && |
57 |
4/6✓ Branch 0 taken 35076 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29704 times.
✓ Branch 3 taken 5372 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 29704 times.
|
64780 | desc->nb_components && !desc->log2_chroma_h && |
58 | 29704 | (ret = ff_add_format(&formats, pix_fmt)) < 0) | |
59 | ✗ | return ret; | |
60 | } | ||
61 | 158 | return ff_set_common_formats2(ctx, cfg_in, cfg_out, formats); | |
62 | } | ||
63 | |||
64 | 157 | static int config_input(AVFilterLink *inlink) | |
65 | { | ||
66 | 157 | AVFilterContext *ctx = inlink->dst; | |
67 | 157 | FieldOrderContext *s = ctx->priv; | |
68 | |||
69 | 157 | return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w); | |
70 | } | ||
71 | |||
72 | 181 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
73 | { | ||
74 | 181 | AVFilterContext *ctx = inlink->dst; | |
75 | 181 | FieldOrderContext *s = ctx->priv; | |
76 | 181 | AVFilterLink *outlink = ctx->outputs[0]; | |
77 | int h, plane, src_line_step, dst_line_step, line_size, line; | ||
78 | uint8_t *dst, *src; | ||
79 | AVFrame *out; | ||
80 | |||
81 |
1/2✓ Branch 0 taken 181 times.
✗ Branch 1 not taken.
|
181 | if (!(frame->flags & AV_FRAME_FLAG_INTERLACED) || |
82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 181 times.
|
181 | !!(frame->flags & AV_FRAME_FLAG_TOP_FIELD_FIRST) == s->dst_tff) { |
83 | ✗ | av_log(ctx, AV_LOG_VERBOSE, | |
84 | "Skipping %s.\n", | ||
85 | ✗ | (frame->flags & AV_FRAME_FLAG_INTERLACED) ? | |
86 | "frame with same field order" : "progressive frame"); | ||
87 | ✗ | return ff_filter_frame(outlink, frame); | |
88 | } | ||
89 | |||
90 |
1/2✓ Branch 1 taken 181 times.
✗ Branch 2 not taken.
|
181 | if (av_frame_is_writable(frame)) { |
91 | 181 | out = frame; | |
92 | } else { | ||
93 | ✗ | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); | |
94 | ✗ | if (!out) { | |
95 | ✗ | av_frame_free(&frame); | |
96 | ✗ | return AVERROR(ENOMEM); | |
97 | } | ||
98 | ✗ | av_frame_copy_props(out, frame); | |
99 | } | ||
100 | |||
101 | 181 | av_log(ctx, AV_LOG_TRACE, | |
102 | "picture will move %s one line\n", | ||
103 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 25 times.
|
181 | s->dst_tff ? "up" : "down"); |
104 | 181 | h = frame->height; | |
105 |
5/6✓ Branch 0 taken 513 times.
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 361 times.
✓ Branch 3 taken 152 times.
✓ Branch 4 taken 361 times.
✗ Branch 5 not taken.
|
542 | for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) { |
106 | 361 | dst_line_step = out->linesize[plane] * (h > 2); | |
107 | 361 | src_line_step = frame->linesize[plane] * (h > 2); | |
108 | 361 | line_size = s->line_size[plane]; | |
109 | 361 | dst = out->data[plane]; | |
110 | 361 | src = frame->data[plane]; | |
111 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 25 times.
|
361 | if (s->dst_tff) { |
112 | /** Move every line up one line, working from | ||
113 | * the top to the bottom of the frame. | ||
114 | * The original top line is lost. | ||
115 | * The new last line is created as a copy of the | ||
116 | * penultimate line from that field. */ | ||
117 |
2/2✓ Branch 0 taken 96768 times.
✓ Branch 1 taken 336 times.
|
97104 | for (line = 0; line < h; line++) { |
118 |
2/2✓ Branch 0 taken 96432 times.
✓ Branch 1 taken 336 times.
|
96768 | if (1 + line < frame->height) { |
119 | 96432 | memcpy(dst, src + src_line_step, line_size); | |
120 | } else { | ||
121 | 336 | memcpy(dst, src - 2 * src_line_step, line_size); | |
122 | } | ||
123 | 96768 | dst += dst_line_step; | |
124 | 96768 | src += src_line_step; | |
125 | } | ||
126 | } else { | ||
127 | /** Move every line down one line, working from | ||
128 | * the bottom to the top of the frame. | ||
129 | * The original bottom line is lost. | ||
130 | * The new first line is created as a copy of the | ||
131 | * second line from that field. */ | ||
132 | 25 | dst += (h - 1) * dst_line_step; | |
133 | 25 | src += (h - 1) * src_line_step; | |
134 |
2/2✓ Branch 0 taken 7200 times.
✓ Branch 1 taken 25 times.
|
7225 | for (line = h - 1; line >= 0 ; line--) { |
135 |
2/2✓ Branch 0 taken 7175 times.
✓ Branch 1 taken 25 times.
|
7200 | if (line > 0) { |
136 | 7175 | memcpy(dst, src - src_line_step, line_size); | |
137 | } else { | ||
138 | 25 | memcpy(dst, src + 2 * src_line_step, line_size); | |
139 | } | ||
140 | 7200 | dst -= dst_line_step; | |
141 | 7200 | src -= src_line_step; | |
142 | } | ||
143 | } | ||
144 | } | ||
145 | #if FF_API_INTERLACED_FRAME | ||
146 | FF_DISABLE_DEPRECATION_WARNINGS | ||
147 | 181 | out->top_field_first = s->dst_tff; | |
148 | FF_ENABLE_DEPRECATION_WARNINGS | ||
149 | #endif | ||
150 |
2/2✓ Branch 0 taken 156 times.
✓ Branch 1 taken 25 times.
|
181 | if (s->dst_tff) |
151 | 156 | out->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
152 | else | ||
153 | 25 | out->flags &= ~AV_FRAME_FLAG_TOP_FIELD_FIRST; | |
154 | |||
155 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 181 times.
|
181 | if (frame != out) |
156 | ✗ | av_frame_free(&frame); | |
157 | 181 | return ff_filter_frame(outlink, out); | |
158 | } | ||
159 | |||
160 | #define OFFSET(x) offsetof(FieldOrderContext, x) | ||
161 | #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM | ||
162 | |||
163 | static const AVOption fieldorder_options[] = { | ||
164 | { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, .unit = "order" }, | ||
165 | { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" }, | ||
166 | { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" }, | ||
167 | { NULL } | ||
168 | }; | ||
169 | |||
170 | AVFILTER_DEFINE_CLASS(fieldorder); | ||
171 | |||
172 | static const AVFilterPad avfilter_vf_fieldorder_inputs[] = { | ||
173 | { | ||
174 | .name = "default", | ||
175 | .type = AVMEDIA_TYPE_VIDEO, | ||
176 | .config_props = config_input, | ||
177 | .filter_frame = filter_frame, | ||
178 | }, | ||
179 | }; | ||
180 | |||
181 | const AVFilter ff_vf_fieldorder = { | ||
182 | .name = "fieldorder", | ||
183 | .description = NULL_IF_CONFIG_SMALL("Set the field order."), | ||
184 | .priv_size = sizeof(FieldOrderContext), | ||
185 | .priv_class = &fieldorder_class, | ||
186 | FILTER_INPUTS(avfilter_vf_fieldorder_inputs), | ||
187 | FILTER_OUTPUTS(ff_video_default_filterpad), | ||
188 | FILTER_QUERY_FUNC2(query_formats), | ||
189 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, | ||
190 | }; | ||
191 |