FFmpeg  4.4.9
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];
37 
38  s->nb_planes = av_pix_fmt_count_planes(inlink->format);
39 
40  if (inlink->h & 1) {
41  av_log(ctx, AV_LOG_ERROR, "height must be even\n");
42  return AVERROR_INVALIDDATA;
43  }
44  if (AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h) & 1) {
45  av_log(ctx, AV_LOG_ERROR, "chroma height must be even\n");
46  return AVERROR_INVALIDDATA;
47  }
48 
49  outlink->time_base.num = inlink->time_base.num;
50  outlink->time_base.den = inlink->time_base.den * 2;
51  outlink->frame_rate.num = inlink->frame_rate.num * 2;
52  outlink->frame_rate.den = inlink->frame_rate.den;
53  outlink->w = inlink->w;
54  outlink->h = inlink->h / 2;
55 
56  return 0;
57 }
58 
59 static void extract_field(AVFrame *frame, int nb_planes, int type)
60 {
61  int i;
62 
63  for (i = 0; i < nb_planes; i++) {
64  if (type)
65  frame->data[i] = frame->data[i] + frame->linesize[i];
66  frame->linesize[i] *= 2;
67  }
68 }
69 
70 static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
71 {
72  AVFilterContext *ctx = inlink->dst;
73  SeparateFieldsContext *s = ctx->priv;
74  AVFilterLink *outlink = ctx->outputs[0];
75  int ret;
76 
77  inpicref->height = outlink->h;
78  inpicref->interlaced_frame = 0;
79 
80  if (!s->second) {
81  goto clone;
82  } else {
83  AVFrame *second = s->second;
84 
85  extract_field(second, s->nb_planes, second->top_field_first);
86 
87  if (second->pts != AV_NOPTS_VALUE &&
88  inpicref->pts != AV_NOPTS_VALUE)
89  second->pts += inpicref->pts;
90  else
91  second->pts = AV_NOPTS_VALUE;
92 
93  ret = ff_filter_frame(outlink, second);
94  if (ret < 0)
95  return ret;
96 clone:
97  s->second = av_frame_clone(inpicref);
98  if (!s->second)
99  return AVERROR(ENOMEM);
100  }
101 
102  extract_field(inpicref, s->nb_planes, !inpicref->top_field_first);
103 
104  if (inpicref->pts != AV_NOPTS_VALUE)
105  inpicref->pts *= 2;
106 
107  return ff_filter_frame(outlink, inpicref);
108 }
109 
110 static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
111 {
112  AVFilterContext *ctx = outlink->src;
113  SeparateFieldsContext *s = ctx->priv;
114  int ret = 0;
115 
116  if (s->second) {
117  *out_pts = s->second->pts += pts;
118  extract_field(s->second, s->nb_planes, s->second->top_field_first);
119  ret = ff_filter_frame(outlink, s->second);
120  s->second = NULL;
121  }
122 
123  return ret;
124 }
125 
127 {
128  AVFilterLink *inlink = ctx->inputs[0];
129  AVFilterLink *outlink = ctx->outputs[0];
130  AVFrame *in;
131  int64_t pts;
132  int ret, status;
133 
134  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
135 
136  ret = ff_inlink_consume_frame(inlink, &in);
137  if (ret < 0)
138  return ret;
139  if (ret > 0)
140  return filter_frame(inlink, in);
141 
142  if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
143  if (status == AVERROR_EOF) {
144  int64_t out_pts = pts;
145 
146  ret = flush_frame(outlink, pts, &out_pts);
147  ff_outlink_set_status(outlink, status, out_pts);
148  return ret;
149  }
150  }
151 
152  FF_FILTER_FORWARD_WANTED(outlink, inlink);
153 
154  return FFERROR_NOT_READY;
155 }
156 
158 {
159  SeparateFieldsContext *s = ctx->priv;
160 
161  av_frame_free(&s->second);
162 }
163 
165  {
166  .name = "default",
167  .type = AVMEDIA_TYPE_VIDEO,
168  },
169  { NULL }
170 };
171 
173  {
174  .name = "default",
175  .type = AVMEDIA_TYPE_VIDEO,
176  .config_props = config_props_output,
177  },
178  { NULL }
179 };
180 
182  .name = "separatefields",
183  .description = NULL_IF_CONFIG_SMALL("Split input video frames into fields."),
184  .priv_size = sizeof(SeparateFieldsContext),
185  .activate = activate,
186  .uninit = uninit,
189 };
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
#define av_cold
Definition: attributes.h:88
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
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:1449
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1099
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:1494
Main libavfilter public API header.
#define s(width, name)
Definition: cbs_vp9.c:257
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:58
#define NULL
Definition: coverity.c:32
long long int64_t
Definition: coverity.c:34
static AVFrame * frame
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition: filters.h:265
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:200
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:210
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:540
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
cl_device_type type
int i
Definition: input.c:407
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
const char * desc
Definition: libsvtav1.c:83
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2613
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
An instance of a filter.
Definition: avfilter.h:341
A filter pad used for either input or output.
Definition: internal.h:54
const char * name
Pad name.
Definition: internal.h:60
Filter definition.
Definition: avfilter.h:145
const char * name
Filter name.
Definition: avfilter.h:149
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int height
Definition: frame.h:376
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:470
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:465
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
int num
Numerator.
Definition: rational.h:59
int den
Denominator.
Definition: rational.h:60
#define av_log(a,...)
AVFormatContext * ctx
Definition: movenc.c:48
static int64_t pts
static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
static int filter_frame(AVFilterLink *inlink, AVFrame *inpicref)
static const AVFilterPad separatefields_outputs[]
static void extract_field(AVFrame *frame, int nb_planes, int type)
static int activate(AVFilterContext *ctx)
static av_cold void uninit(AVFilterContext *ctx)
static int config_props_output(AVFilterLink *outlink)
static const AVFilterPad separatefields_inputs[]
AVFilter ff_vf_separatefields