OSDN Git Service

rtpdec: Don't pass non-const pointers to fmtp attribute parsing functions
[android-x86/external-ffmpeg.git] / libavfilter / vf_interlace.c
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with Libav; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 /**
20  * @file
21  * progressive to interlaced content filter, inspired by heavy debugging of tinterlace filter
22  */
23
24 #include "libavutil/common.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/avassert.h"
28
29 #include "formats.h"
30 #include "avfilter.h"
31 #include "interlace.h"
32 #include "internal.h"
33 #include "video.h"
34
35 #define OFFSET(x) offsetof(InterlaceContext, x)
36 #define V AV_OPT_FLAG_VIDEO_PARAM
37 static const AVOption options[] = {
38     { "scan", "scanning mode", OFFSET(scan),
39         AV_OPT_TYPE_INT,   {.i64 = MODE_TFF }, 0, 1, .flags = V, .unit = "scan" },
40     { "tff", "top field first", 0,
41         AV_OPT_TYPE_CONST, {.i64 = MODE_TFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
42     { "bff", "bottom field first", 0,
43         AV_OPT_TYPE_CONST, {.i64 = MODE_BFF }, INT_MIN, INT_MAX, .flags = V, .unit = "scan" },
44     { "lowpass", "enable vertical low-pass filter", OFFSET(lowpass),
45         AV_OPT_TYPE_INT,   {.i64 = 1 },        0, 1, .flags = V },
46     { NULL }
47 };
48
49 static const AVClass class = {
50     .class_name = "interlace filter",
51     .item_name  = av_default_item_name,
52     .option     = options,
53     .version    = LIBAVUTIL_VERSION_INT,
54 };
55
56 static void lowpass_line_c(uint8_t *dstp, ptrdiff_t linesize,
57                            const uint8_t *srcp,
58                            const uint8_t *srcp_above,
59                            const uint8_t *srcp_below)
60 {
61     int i;
62     for (i = 0; i < linesize; i++) {
63         // this calculation is an integer representation of
64         // '0.5 * current + 0.25 * above + 0.25 * below'
65         // '1 +' is for rounding.
66         dstp[i] = (1 + srcp[i] + srcp[i] + srcp_above[i] + srcp_below[i]) >> 2;
67     }
68 }
69
70 static const enum AVPixelFormat formats_supported[] = {
71     AV_PIX_FMT_YUV420P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV444P,
72     AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUVA420P,
73     AV_PIX_FMT_GRAY8,    AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
74     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
75 };
76
77 static int query_formats(AVFilterContext *ctx)
78 {
79     ff_set_common_formats(ctx, ff_make_format_list(formats_supported));
80     return 0;
81 }
82
83 static av_cold void uninit(AVFilterContext *ctx)
84 {
85     InterlaceContext *s = ctx->priv;
86
87     av_frame_free(&s->cur);
88     av_frame_free(&s->next);
89
90     av_opt_free(s);
91 }
92
93 static int config_out_props(AVFilterLink *outlink)
94 {
95     AVFilterContext *ctx = outlink->src;
96     AVFilterLink *inlink = outlink->src->inputs[0];
97     InterlaceContext *s = ctx->priv;
98
99     if (inlink->h < 2) {
100         av_log(ctx, AV_LOG_ERROR, "input video height is too small\n");
101         return AVERROR_INVALIDDATA;
102     }
103
104     if (!s->lowpass)
105         av_log(ctx, AV_LOG_WARNING, "Lowpass filter is disabled, "
106                "the resulting video will be aliased rather than interlaced.\n");
107
108     // same input size
109     outlink->w = inlink->w;
110     outlink->h = inlink->h;
111     outlink->time_base = inlink->time_base;
112     // half framerate
113     outlink->time_base.num *= 2;
114
115
116     if (s->lowpass) {
117         s->lowpass_line = lowpass_line_c;
118         if (ARCH_X86)
119             ff_interlace_init_x86(s);
120     }
121
122     av_log(ctx, AV_LOG_VERBOSE, "%s interlacing %s lowpass filter\n",
123            s->scan == MODE_TFF ? "tff" : "bff", (s->lowpass) ? "with" : "without");
124
125     return 0;
126 }
127
128 static void copy_picture_field(InterlaceContext *s,
129                                AVFrame *src_frame, AVFrame *dst_frame,
130                                AVFilterLink *inlink, enum FieldType field_type,
131                                int lowpass)
132 {
133     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
134     int hsub = desc->log2_chroma_w;
135     int vsub = desc->log2_chroma_h;
136     int plane, j;
137
138     for (plane = 0; plane < desc->nb_components; plane++) {
139         int cols  = (plane == 1 || plane == 2) ? -(-inlink->w) >> hsub : inlink->w;
140         int lines = (plane == 1 || plane == 2) ? -(-inlink->h) >> vsub : inlink->h;
141         uint8_t *dstp = dst_frame->data[plane];
142         const uint8_t *srcp = src_frame->data[plane];
143
144         av_assert0(cols >= 0 || lines >= 0);
145
146         lines = (lines + (field_type == FIELD_UPPER)) / 2;
147         if (field_type == FIELD_LOWER) {
148             srcp += src_frame->linesize[plane];
149             dstp += dst_frame->linesize[plane];
150         }
151         if (lowpass) {
152             int srcp_linesize = src_frame->linesize[plane] * 2;
153             int dstp_linesize = dst_frame->linesize[plane] * 2;
154             for (j = lines; j > 0; j--) {
155                 const uint8_t *srcp_above = srcp - src_frame->linesize[plane];
156                 const uint8_t *srcp_below = srcp + src_frame->linesize[plane];
157                 if (j == lines)
158                     srcp_above = srcp; // there is no line above
159                 if (j == 1)
160                     srcp_below = srcp; // there is no line below
161                 s->lowpass_line(dstp, cols, srcp, srcp_above, srcp_below);
162                 dstp += dstp_linesize;
163                 srcp += srcp_linesize;
164             }
165         } else {
166             av_image_copy_plane(dstp, dst_frame->linesize[plane] * 2,
167                                 srcp, src_frame->linesize[plane] * 2,
168                                 cols, lines);
169         }
170     }
171 }
172
173 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
174 {
175     AVFilterContext *ctx = inlink->dst;
176     AVFilterLink *outlink = ctx->outputs[0];
177     InterlaceContext *s = ctx->priv;
178     AVFrame *out;
179     int tff, ret;
180
181     av_frame_free(&s->cur);
182     s->cur  = s->next;
183     s->next = buf;
184
185     /* we need at least two frames */
186     if (!s->cur || !s->next)
187         return 0;
188
189     if (s->cur->interlaced_frame) {
190         av_log(ctx, AV_LOG_WARNING,
191                "video is already interlaced, adjusting framerate only\n");
192         out = av_frame_clone(s->cur);
193         if (!out)
194             return AVERROR(ENOMEM);
195         out->pts /= 2;  // adjust pts to new framerate
196         ret = ff_filter_frame(outlink, out);
197         s->got_output = 1;
198         return ret;
199     }
200
201     tff = (s->scan == MODE_TFF);
202     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
203     if (!out)
204         return AVERROR(ENOMEM);
205
206     av_frame_copy_props(out, s->cur);
207     out->interlaced_frame = 1;
208     out->top_field_first  = tff;
209     out->pts             /= 2;  // adjust pts to new framerate
210
211     /* copy upper/lower field from cur */
212     copy_picture_field(s, s->cur, out, inlink, tff ? FIELD_UPPER : FIELD_LOWER, s->lowpass);
213     av_frame_free(&s->cur);
214
215     /* copy lower/upper field from next */
216     copy_picture_field(s, s->next, out, inlink, tff ? FIELD_LOWER : FIELD_UPPER, s->lowpass);
217     av_frame_free(&s->next);
218
219     ret = ff_filter_frame(outlink, out);
220     s->got_output = 1;
221
222     return ret;
223 }
224
225 static int request_frame(AVFilterLink *outlink)
226 {
227     AVFilterContext *ctx = outlink->src;
228     InterlaceContext *s  = ctx->priv;
229     int ret = 0;
230
231     s->got_output = 0;
232     while (ret >= 0 && !s->got_output)
233         ret = ff_request_frame(ctx->inputs[0]);
234
235     return ret;
236 }
237
238 static const AVFilterPad inputs[] = {
239     {
240         .name         = "default",
241         .type         = AVMEDIA_TYPE_VIDEO,
242         .filter_frame = filter_frame,
243     },
244     { NULL }
245 };
246
247 static const AVFilterPad outputs[] = {
248     {
249         .name          = "default",
250         .type          = AVMEDIA_TYPE_VIDEO,
251         .config_props  = config_out_props,
252         .request_frame = request_frame,
253     },
254     { NULL }
255 };
256
257 AVFilter ff_vf_interlace = {
258     .name          = "interlace",
259     .description   = NULL_IF_CONFIG_SMALL("Convert progressive video into interlaced."),
260     .uninit        = uninit,
261
262     .priv_class    = &class,
263     .priv_size     = sizeof(InterlaceContext),
264     .query_formats = query_formats,
265
266     .inputs        = inputs,
267     .outputs       = outputs,
268 };
269