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