FFmpeg
vf_separatefields.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Paul B Mahol
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/pixdesc.h"
22 #include "avfilter.h"
23 #include "filters.h"
24 #include "internal.h"
25 
26 typedef struct SeparateFieldsContext {
27  int nb_planes;
30 
31 static int config_props_output(AVFilterLink *outlink)
32 {
33  AVFilterContext *ctx = outlink->src;
34  SeparateFieldsContext *s = ctx->priv;
35  AVFilterLink *inlink = ctx->inputs[0];
36 
37  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
38 
39  if (inlink->h & 1) {
40  av_log(ctx, AV_LOG_ERROR, "height must be even\n");
41  return AVERROR_INVALIDDATA;
42  }
43 
44  outlink->time_base.num = inlink->time_base.num;
45  outlink->time_base.den = inlink->time_base.den * 2;
46  outlink->frame_rate.num = inlink->frame_rate.num * 2;
47  outlink->frame_rate.den = inlink->frame_rate.den;
48  outlink->w = inlink->w;
49  outlink->h = inlink->h / 2;
50 
51  return 0;
52 }
53 
54 static void extract_field(AVFrame *frame, int nb_planes, int type)
55 {
56  int i;
57 
58  for (i = 0; i < nb_planes; i++) {
59  if (type)
60  frame->data[i] = frame->data[i] + frame->linesize[i];
61  frame->linesize[i] *= 2;
62  }
63 }
64 
65 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
66 {
67  AVFilterContext *ctx = inlink->dst;
68  SeparateFieldsContext *s = ctx->priv;
69  AVFilterLink *outlink = ctx->outputs[0];
70  int ret;
71 
72  inpicref->height = outlink->h;
73  inpicref->interlaced_frame = 0;
74 
75  if (!s->second) {
76  goto clone;
77  } else {
78  AVFrame *second = s->second;
79 
80  extract_field(second, s->nb_planes, second->top_field_first);
81 
82  if (second->pts != AV_NOPTS_VALUE &&
83  inpicref->pts != AV_NOPTS_VALUE)
84  second->pts += inpicref->pts;
85  else
86  second->pts = AV_NOPTS_VALUE;
87 
88  ret = ff_filter_frame(outlink, second);
89  if (ret < 0)
90  return ret;
91 clone:
92  s->second = av_frame_clone(inpicref);
93  if (!s->second)
94  return AVERROR(ENOMEM);
95  }
96 
97  extract_field(inpicref, s->nb_planes, !inpicref->top_field_first);
98 
99  if (inpicref->pts != AV_NOPTS_VALUE)
100  inpicref->pts *= 2;
101 
102  return ff_filter_frame(outlink, inpicref);
103 }
104 
105 static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
106 {
107  AVFilterContext *ctx = outlink->src;
108  SeparateFieldsContext *s = ctx->priv;
109  int ret = 0;
110 
111  if (s->second) {
112  *out_pts = s->second->pts += pts;
113  extract_field(s->second, s->nb_planes, s->second->top_field_first);
114  ret = ff_filter_frame(outlink, s->second);
115  s->second = NULL;
116  }
117 
118  return ret;
119 }
120 
122 {
123  AVFilterLink *inlink = ctx->inputs[0];
124  AVFilterLink *outlink = ctx->outputs[0];
125  AVFrame *in;
126  int64_t pts;
127  int ret, status;
128 
130 
132  if (ret < 0)
133  return ret;
134  if (ret > 0)
135  return filter_frame(inlink, in);
136 
138  if (status == AVERROR_EOF) {
139  int64_t out_pts = pts;
140 
141  ret = flush_frame(outlink, pts, &out_pts);
142  ff_outlink_set_status(outlink, status, out_pts);
143  return ret;
144  }
145  }
146 
148 
149  return FFERROR_NOT_READY;
150 }
151 
153 {
154  SeparateFieldsContext *s = ctx->priv;
155 
156  av_frame_free(&s->second);
157 }
158 
160  {
161  .name = "default",
162  .type = AVMEDIA_TYPE_VIDEO,
163  },
164  { NULL }
165 };
166 
168  {
169  .name = "default",
170  .type = AVMEDIA_TYPE_VIDEO,
171  .config_props = config_props_output,
172  },
173  { NULL }
174 };
175 
177  .name = "separatefields",
178  .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
179  .priv_size = sizeof(SeparateFieldsContext),
180  .activate = activate,
181  .uninit = uninit,
184 };
status
they must not be accessed directly The fifo field contains the frames that are queued in the input for processing by the filter The status_in and status_out fields contains the queued status(EOF or error) of the link
ff_vf_separatefields
AVFilter ff_vf_separatefields
Definition: vf_separatefields.c:176
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
SeparateFieldsContext::second
AVFrame * second
Definition: vf_separatefields.c:28
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
flush_frame
static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
Definition: vf_separatefields.c:105
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:202
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
pixdesc.h
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:388
AVFrame::top_field_first
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:447
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:148
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
ff_inlink_consume_frame
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition: avfilter.c:1481
av_pix_fmt_count_planes
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2562
SeparateFieldsContext::nb_planes
int nb_planes
Definition: vf_separatefields.c:27
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
Definition: vf_separatefields.c:65
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
pts
static int64_t pts
Definition: transcode_aac.c:647
AVRational::num
int num
Numerator.
Definition: rational.h:59
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:54
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
av_cold
#define av_cold
Definition: attributes.h:84
ff_outlink_set_status
static void ff_outlink_set_status(AVFilterLink *link, int status, int64_t pts)
Set the status field of a link from the source filter.
Definition: filters.h:189
s
#define s(width, name)
Definition: cbs_vp9.c:257
outputs
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
activate
static int activate(AVFilterContext *ctx)
Definition: vf_separatefields.c:121
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
separatefields_outputs
static const AVFilterPad separatefields_outputs[]
Definition: vf_separatefields.c:167
NULL
#define NULL
Definition: coverity.c:32
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
ff_inlink_acknowledge_status
int ff_inlink_acknowledge_status(AVFilterLink *link, int *rstatus, int64_t *rpts)
Test and acknowledge the change of status on the link.
Definition: avfilter.c:1436
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:188
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
SeparateFieldsContext
Definition: vf_separatefields.c:26
internal.h
AVFrame::interlaced_frame
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:442
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
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
separatefields_inputs
static const AVFilterPad separatefields_inputs[]
Definition: vf_separatefields.c:159
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:60
AVFilter
Filter definition.
Definition: avfilter.h:144
ret
ret
Definition: filter_design.txt:187
frame
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 the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AVFrame::height
int height
Definition: frame.h:353
uninit
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_separatefields.c:152
AVRational::den
int den
Denominator.
Definition: rational.h:60
avfilter.h
config_props_output
static int config_props_output(AVFilterLink *outlink)
Definition: vf_separatefields.c:31
AVFilterContext
An instance of a filter.
Definition: avfilter.h:338
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
extract_field
static void extract_field(AVFrame *frame, int nb_planes, int type)
Definition: vf_separatefields.c:54
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59