FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vf_repeatfields.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 0 82 0.0%
Functions: 0 4 0.0%
Branches: 0 40 0.0%

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