FFmpeg
vf_repeatfields.c
Go to the documentation of this file.
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];
35 
37 {
38  RepeatFieldsContext *s = ctx->priv;
39 
40  av_frame_free(&s->frame);
41 }
42 
43 static const enum AVPixelFormat pixel_fmts_eq[] = {
51 };
52 
54 {
55  RepeatFieldsContext *s = inlink->dst->priv;
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 
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 
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",
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) {
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++) {
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 {
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 
178  {
179  .name = "default",
180  .type = AVMEDIA_TYPE_VIDEO,
181  .filter_frame = filter_frame,
182  .config_props = config_input,
183  },
184 };
185 
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,
194 };
ff_vf_repeatfields
const AVFilter ff_vf_repeatfields
Definition: vf_repeatfields.c:186
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1018
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2962
FILTER_PIXFMTS_ARRAY
#define FILTER_PIXFMTS_ARRAY(array)
Definition: internal.h:162
int64_t
long long int64_t
Definition: coverity.c:34
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:130
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:344
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:456
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:616
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
video.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:365
av_image_copy_plane
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition: imgutils.c:374
AV_FRAME_FLAG_TOP_FIELD_FIRST
#define AV_FRAME_FLAG_TOP_FIELD_FIRST
A flag to mark frames where the top field is displayed first if the content is interlaced.
Definition: frame.h:608
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3002
pts
static int64_t pts
Definition: transcode_aac.c:643
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:33
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_repeatfields.c:36
av_cold
#define av_cold
Definition: attributes.h:90
ff_video_default_filterpad
const AVFilterPad ff_video_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_VIDEO.
Definition: video.c:37
av_image_fill_linesizes
int av_image_fill_linesizes(int linesizes[4], enum AVPixelFormat pix_fmt, int width)
Fill plane linesizes for an image with pixel format pix_fmt and width width.
Definition: imgutils.c:89
s
#define s(width, name)
Definition: cbs_vp9.c:198
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
RepeatFieldsContext::frame
AVFrame * frame
Definition: vf_repeatfields.c:33
RepeatFieldsContext
Definition: vf_repeatfields.c:27
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
av_frame_clone
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:563
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:182
link
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a link
Definition: filter_design.txt:23
ff_inlink_make_frame_writable
int ff_inlink_make_frame_writable(AVFilterLink *link, AVFrame **rframe)
Make sure a frame is writable.
Definition: avfilter.c:1492
RepeatFieldsContext::nb_planes
int nb_planes
Definition: vf_repeatfields.c:30
fields
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the then the processing requires a frame on this link and the filter is expected to make efforts in that direction The status of input links is stored by the fifo and status_out fields
Definition: filter_design.txt:155
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
state
static struct @385 state
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:81
RepeatFieldsContext::state
int state
Definition: vf_repeatfields.c:29
RepeatFieldsContext::planeheight
int planeheight[4]
Definition: vf_repeatfields.c:32
f
f
Definition: af_crystalizer.c:121
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:106
update_pts
static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
Definition: vf_repeatfields.c:70
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AVFrame::time_base
AVRational time_base
Time base for the timestamps in this frame.
Definition: frame.h:471
RepeatFieldsContext::linesize
int linesize[4]
Definition: vf_repeatfields.c:31
internal.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_repeatfields.c:53
pixel_fmts_eq
static enum AVPixelFormat pixel_fmts_eq[]
Definition: vf_repeatfields.c:43
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:39
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
avfilter.h
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFilterContext
An instance of a filter.
Definition: avfilter.h:407
desc
const char * desc
Definition: libsvtav1.c:75
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_repeatfields.c:80
repeatfields_inputs
static const AVFilterPad repeatfields_inputs[]
Definition: vf_repeatfields.c:177
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:183
AV_PIX_FMT_YUV411P
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:80
imgutils.h
AVFrame::linesize
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition: frame.h:389
AV_PIX_FMT_YUV410P
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:79
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVFrame::repeat_pict
int repeat_pict
Number of fields in this frame which should be repeated, i.e.
Definition: frame.h:512