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 "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];
33 
35 {
36  RepeatFieldsContext *s = ctx->priv;
37 
38  av_frame_free(&s->frame);
39 }
40 
42 {
43  static const enum AVPixelFormat pixel_fmts_eq[] = {
51  };
52 
53  AVFilterFormats *fmts_list = ff_make_format_list(pixel_fmts_eq);
54  if (!fmts_list)
55  return AVERROR(ENOMEM);
56  return ff_set_common_formats(ctx, fmts_list);
57 }
58 
60 {
61  RepeatFieldsContext *s = inlink->dst->priv;
63  int ret;
64 
65  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
66  return ret;
67 
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;
70 
71  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
72 
73  return 0;
74 }
75 
76 static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
77 {
78  if (av_cmp_q(link->frame_rate, (AVRational){30000, 1001}) == 0 &&
79  av_cmp_q(link->time_base, (AVRational){1001, 60000}) <= 0
80  ) {
81  f->pts = pts + av_rescale_q(fields, (AVRational){1001, 60000}, link->time_base);
82  } else
83  f->pts = AV_NOPTS_VALUE;
84 }
85 
87  AVFilterContext *ctx = inlink->dst;
88  AVFilterLink *outlink = inlink->dst->outputs[0];
89  RepeatFieldsContext *s = ctx->priv;
90  AVFrame *out;
91  int ret, i;
92  int state = s->state;
93 
94  if (!s->frame) {
95  s->frame = av_frame_clone(in);
96  if (!s->frame)
97  return AVERROR(ENOMEM);
98  s->frame->pts = AV_NOPTS_VALUE;
99  }
100 
101  out = s->frame;
102 
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);
108  state ^= 1;
109  }
110 
111  if (state == 0) {
112  AVFrame *new;
113 
114  new = av_frame_clone(in);
115  if (!new)
116  return AVERROR(ENOMEM);
117 
118  ret = ff_filter_frame(outlink, new);
119 
120  if (in->repeat_pict) {
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);
127  }
128  state = 1;
129  }
130  } else {
131  for (i = 0; i < s->nb_planes; i++) {
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);
136  }
137 
138  ret = ff_filter_frame(outlink, av_frame_clone(out));
139 
140  if (in->repeat_pict) {
141  AVFrame *new;
142 
143  new = av_frame_clone(in);
144  if (!new)
145  return AVERROR(ENOMEM);
146 
147  ret = ff_filter_frame(outlink, new);
148  state = 0;
149  } else {
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);
156  }
157  }
158  }
159 
160  s->state = state;
161 
162  av_frame_free(&in);
163  return ret;
164 }
165 
167  {
168  .name = "default",
169  .type = AVMEDIA_TYPE_VIDEO,
170  .filter_frame = filter_frame,
171  .config_props = config_input,
172  },
173  { NULL }
174 };
175 
177  {
178  .name = "default",
179  .type = AVMEDIA_TYPE_VIDEO,
180  },
181  { NULL }
182 };
183 
185  .name = "repeatfields",
186  .description = NULL_IF_CONFIG_SMALL("Hard repeat fields based on MPEG repeat field flag."),
187  .priv_size = sizeof(RepeatFieldsContext),
188  .uninit = uninit,
192 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
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_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:286
out
FILE * out
Definition: movenc.c:54
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1096
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
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
state
static struct @321 state
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
av_frame_make_writable
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:611
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:149
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:373
AVFilterFormats
A list of supported formats for one end of a filter link.
Definition: formats.h:65
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
pts
static int64_t pts
Definition: transcode_aac.c:652
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_repeatfields.c:34
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:587
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:257
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
ff_vf_repeatfields
AVFilter ff_vf_repeatfields
Definition: vf_repeatfields.c:184
RepeatFieldsContext::frame
AVFrame * frame
Definition: vf_repeatfields.c:31
RepeatFieldsContext
Definition: vf_repeatfields.c:25
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
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:540
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:66
f
#define f(width, name)
Definition: cbs_vp9.c:255
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
RepeatFieldsContext::nb_planes
int nb_planes
Definition: vf_repeatfields.c:28
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:67
NULL
#define NULL
Definition: coverity.c:32
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:243
AV_PIX_FMT_GRAY8
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition: pixfmt.h:74
RepeatFieldsContext::state
int state
Definition: vf_repeatfields.c:27
RepeatFieldsContext::planeheight
int planeheight[4]
Definition: vf_repeatfields.c:30
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:117
update_pts
static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
Definition: vf_repeatfields.c:76
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
RepeatFieldsContext::linesize
int linesize[4]
Definition: vf_repeatfields.c:29
internal.h
query_formats
static int query_formats(AVFilterContext *ctx)
Definition: vf_repeatfields.c:41
in
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) #define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac) { } void ff_audio_convert_free(AudioConvert **ac) { if(! *ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);} AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map) { AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method !=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2) { ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc) { av_free(ac);return NULL;} return ac;} in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar) { ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar ? ac->channels :1;} else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;} int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in) { int use_generic=1;int len=in->nb_samples;int p;if(ac->dc) { av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> in
Definition: audio_convert.c:326
i
int i
Definition: input.c:407
config_input
static int config_input(AVFilterLink *inlink)
Definition: vf_repeatfields.c:59
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
repeatfields_outputs
static const AVFilterPad repeatfields_outputs[]
Definition: vf_repeatfields.c:176
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:145
ret
ret
Definition: filter_design.txt:187
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
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:71
AVFilterContext
An instance of a filter.
Definition: avfilter.h:341
desc
const char * desc
Definition: libsvtav1.c:79
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:70
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
Definition: vf_repeatfields.c:86
repeatfields_inputs
static const AVFilterPad repeatfields_inputs[]
Definition: vf_repeatfields.c:166
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
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:73
imgutils.h
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:72
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28