2 * Copyright (c) 2003 Tobias Diedrich
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "libavutil/imgutils.h"
25 typedef struct RepeatFieldsContext {
32 } RepeatFieldsContext;
34 static av_cold void uninit(AVFilterContext *ctx)
36 RepeatFieldsContext *s = ctx->priv;
38 av_frame_free(&s->frame);
41 static int query_formats(AVFilterContext *ctx)
43 static const enum AVPixelFormat pixel_fmts_eq[] = {
53 AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_eq);
55 return AVERROR(ENOMEM);
56 return ff_set_common_formats(ctx, fmts_list);
59 static int config_input(AVFilterLink *inlink)
61 RepeatFieldsContext *s = inlink->dst->priv;
62 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
65 if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
68 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
69 s->planeheight[0] = s->planeheight[3] = inlink->h;
71 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
76 static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
78 if (av_cmp_q(link->frame_rate, (AVRational){30000, 1001}) == 0 &&
79 av_cmp_q(link->time_base, (AVRational){1001, 60000}) <= 0
81 f->pts = pts + av_rescale_q(fields, (AVRational){1001, 60000}, link->time_base);
83 f->pts = AV_NOPTS_VALUE;
86 static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
87 AVFilterContext *ctx = inlink->dst;
88 AVFilterLink *outlink = inlink->dst->outputs[0];
89 RepeatFieldsContext *s = ctx->priv;
95 s->frame = av_frame_clone(in);
97 return AVERROR(ENOMEM);
98 s->frame->pts = AV_NOPTS_VALUE;
103 if ((state == 0 && !in->top_field_first) ||
104 (state == 1 && in->top_field_first)) {
105 av_log(ctx, AV_LOG_WARNING, "Unexpected field flags: "
106 "state=%d top_field_first=%d repeat_first_field=%d\n",
107 state, in->top_field_first, in->repeat_pict);
114 new = av_frame_clone(in);
116 return AVERROR(ENOMEM);
118 ret = ff_filter_frame(outlink, new);
120 if (in->repeat_pict) {
121 av_frame_make_writable(out);
122 update_pts(outlink, out, in->pts, 2);
123 for (i = 0; i < s->nb_planes; i++) {
124 av_image_copy_plane(out->data[i], out->linesize[i] * 2,
125 in->data[i], in->linesize[i] * 2,
126 s->linesize[i], s->planeheight[i] / 2);
131 for (i = 0; i < s->nb_planes; i++) {
132 av_frame_make_writable(out);
133 av_image_copy_plane(out->data[i] + out->linesize[i], out->linesize[i] * 2,
134 in->data[i] + in->linesize[i], in->linesize[i] * 2,
135 s->linesize[i], s->planeheight[i] / 2);
138 ret = ff_filter_frame(outlink, av_frame_clone(out));
140 if (in->repeat_pict) {
143 new = av_frame_clone(in);
145 return AVERROR(ENOMEM);
147 ret = ff_filter_frame(outlink, new);
150 av_frame_make_writable(out);
151 update_pts(outlink, out, in->pts, 1);
152 for (i = 0; i < s->nb_planes; i++) {
153 av_image_copy_plane(out->data[i], out->linesize[i] * 2,
154 in->data[i], in->linesize[i] * 2,
155 s->linesize[i], s->planeheight[i] / 2);
166 static const AVFilterPad repeatfields_inputs[] = {
169 .type = AVMEDIA_TYPE_VIDEO,
170 .filter_frame = filter_frame,
171 .config_props = config_input,
176 static const AVFilterPad repeatfields_outputs[] = {
179 .type = AVMEDIA_TYPE_VIDEO,
184 AVFilter ff_vf_repeatfields = {
185 .name = "repeatfields",
186 .description = NULL_IF_CONFIG_SMALL("Hard repeat fields based on MPEG repeat field flag."),
187 .priv_size = sizeof(RepeatFieldsContext),
189 .inputs = repeatfields_inputs,
190 .outputs = repeatfields_outputs,
191 .query_formats = query_formats,