• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2003 Tobias Diedrich
3  *
4  * This file is part of FFmpeg.
5  *
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.
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
14  * GNU General Public License for more details.
15  *
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.
19  */
20 
21 #include "libavutil/imgutils.h"
22 #include "avfilter.h"
23 #include "internal.h"
24 
25 typedef struct RepeatFieldsContext {
26     const AVClass *class;
27     int state;
28     int nb_planes;
29     int linesize[4];
30     int planeheight[4];
31     AVFrame *frame;
32 } RepeatFieldsContext;
33 
uninit(AVFilterContext * ctx)34 static av_cold void uninit(AVFilterContext *ctx)
35 {
36     RepeatFieldsContext *s = ctx->priv;
37 
38     av_frame_free(&s->frame);
39 }
40 
41 static const enum AVPixelFormat pixel_fmts_eq[] = {
42     AV_PIX_FMT_GRAY8,
43     AV_PIX_FMT_YUV410P,
44     AV_PIX_FMT_YUV411P,
45     AV_PIX_FMT_YUV420P,
46     AV_PIX_FMT_YUV422P,
47     AV_PIX_FMT_YUV444P,
48     AV_PIX_FMT_NONE
49 };
50 
config_input(AVFilterLink * inlink)51 static int config_input(AVFilterLink *inlink)
52 {
53     RepeatFieldsContext *s = inlink->dst->priv;
54     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
55     int ret;
56 
57     if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
58         return ret;
59 
60     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
61     s->planeheight[0] = s->planeheight[3] = inlink->h;
62 
63     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
64 
65     return 0;
66 }
67 
update_pts(AVFilterLink * link,AVFrame * f,int64_t pts,int fields)68 static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
69 {
70     if (av_cmp_q(link->frame_rate, (AVRational){30000, 1001}) == 0 &&
71          av_cmp_q(link->time_base, (AVRational){1001, 60000}) <= 0
72     ) {
73         f->pts = pts + av_rescale_q(fields, (AVRational){1001, 60000}, link->time_base);
74     } else
75         f->pts = AV_NOPTS_VALUE;
76 }
77 
filter_frame(AVFilterLink * inlink,AVFrame * in)78 static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
79     AVFilterContext *ctx = inlink->dst;
80     AVFilterLink *outlink = inlink->dst->outputs[0];
81     RepeatFieldsContext *s = ctx->priv;
82     AVFrame *out;
83     int ret, i;
84     int state = s->state;
85 
86     if (!s->frame) {
87         s->frame = av_frame_clone(in);
88         if (!s->frame)
89             return AVERROR(ENOMEM);
90         s->frame->pts = AV_NOPTS_VALUE;
91     }
92 
93     out = s->frame;
94 
95     if ((state == 0 && !in->top_field_first) ||
96         (state == 1 &&  in->top_field_first)) {
97         av_log(ctx, AV_LOG_WARNING, "Unexpected field flags: "
98                                     "state=%d top_field_first=%d repeat_first_field=%d\n",
99                                     state, in->top_field_first, in->repeat_pict);
100         state ^= 1;
101     }
102 
103     if (state == 0) {
104         AVFrame *new;
105 
106         new = av_frame_clone(in);
107         if (!new)
108             return AVERROR(ENOMEM);
109 
110         ret = ff_filter_frame(outlink, new);
111 
112         if (in->repeat_pict) {
113             av_frame_make_writable(out);
114             update_pts(outlink, out, in->pts, 2);
115             for (i = 0; i < s->nb_planes; i++) {
116                 av_image_copy_plane(out->data[i], out->linesize[i] * 2,
117                                     in->data[i], in->linesize[i] * 2,
118                                     s->linesize[i], s->planeheight[i] / 2);
119             }
120             state = 1;
121         }
122     } else {
123         for (i = 0; i < s->nb_planes; i++) {
124             av_frame_make_writable(out);
125             av_image_copy_plane(out->data[i] + out->linesize[i], out->linesize[i] * 2,
126                                 in->data[i] + in->linesize[i], in->linesize[i] * 2,
127                                 s->linesize[i], s->planeheight[i] / 2);
128         }
129 
130         ret = ff_filter_frame(outlink, av_frame_clone(out));
131 
132         if (in->repeat_pict) {
133             AVFrame *new;
134 
135             new = av_frame_clone(in);
136             if (!new)
137                 return AVERROR(ENOMEM);
138 
139             ret = ff_filter_frame(outlink, new);
140             state = 0;
141         } else {
142             av_frame_make_writable(out);
143             update_pts(outlink, out, in->pts, 1);
144             for (i = 0; i < s->nb_planes; i++) {
145                 av_image_copy_plane(out->data[i], out->linesize[i] * 2,
146                                     in->data[i], in->linesize[i] * 2,
147                                     s->linesize[i], s->planeheight[i] / 2);
148             }
149         }
150     }
151 
152     s->state = state;
153 
154     av_frame_free(&in);
155     return ret;
156 }
157 
158 static const AVFilterPad repeatfields_inputs[] = {
159     {
160         .name         = "default",
161         .type         = AVMEDIA_TYPE_VIDEO,
162         .filter_frame = filter_frame,
163         .config_props = config_input,
164     },
165 };
166 
167 static const AVFilterPad repeatfields_outputs[] = {
168     {
169         .name = "default",
170         .type = AVMEDIA_TYPE_VIDEO,
171     },
172 };
173 
174 const AVFilter ff_vf_repeatfields = {
175     .name          = "repeatfields",
176     .description   = NULL_IF_CONFIG_SMALL("Hard repeat fields based on MPEG repeat field flag."),
177     .priv_size     = sizeof(RepeatFieldsContext),
178     .uninit        = uninit,
179     FILTER_INPUTS(repeatfields_inputs),
180     FILTER_OUTPUTS(repeatfields_outputs),
181     FILTER_PIXFMTS_ARRAY(pixel_fmts_eq),
182 };
183