FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_fieldorder.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011 Mark Himsley
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 /**
22  * @file
23  * video field order filter, heavily influenced by vf_pad.c
24  */
25 
26 #include "libavutil/imgutils.h"
27 #include "libavutil/internal.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "formats.h"
32 #include "internal.h"
33 #include "video.h"
34 
35 typedef struct FieldOrderContext {
36  const AVClass *class;
37  int dst_tff; ///< output bff/tff
38  int line_size[4]; ///< bytes of pixel data per line for each plane
40 
42 {
45  int ret;
46 
47  /** accept any input pixel format that is not hardware accelerated, not
48  * a bitstream format, and does not have vertically sub-sampled chroma */
49  if (ctx->inputs[0]) {
50  const AVPixFmtDescriptor *desc = NULL;
51  formats = NULL;
52  while ((desc = av_pix_fmt_desc_next(desc))) {
53  pix_fmt = av_pix_fmt_desc_get_id(desc);
54  if (!(desc->flags & AV_PIX_FMT_FLAG_HWACCEL ||
55  desc->flags & AV_PIX_FMT_FLAG_PAL ||
57  desc->nb_components && !desc->log2_chroma_h &&
58  (ret = ff_add_format(&formats, pix_fmt)) < 0) {
59  ff_formats_unref(&formats);
60  return ret;
61  }
62  }
63  ff_formats_ref(formats, &ctx->inputs[0]->out_formats);
64  ff_formats_ref(formats, &ctx->outputs[0]->in_formats);
65  }
66 
67  return 0;
68 }
69 
70 static int config_input(AVFilterLink *inlink)
71 {
72  AVFilterContext *ctx = inlink->dst;
73  FieldOrderContext *s = ctx->priv;
74 
75  return av_image_fill_linesizes(s->line_size, inlink->format, inlink->w);
76 }
77 
78 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
79 {
80  AVFilterContext *ctx = inlink->dst;
81  FieldOrderContext *s = ctx->priv;
82  AVFilterLink *outlink = ctx->outputs[0];
83  int h, plane, src_line_step, dst_line_step, line_size, line;
84  uint8_t *dst, *src;
85  AVFrame *out;
86 
87  if (!frame->interlaced_frame ||
88  frame->top_field_first == s->dst_tff) {
90  "Skipping %s.\n",
91  frame->interlaced_frame ?
92  "frame with same field order" : "progressive frame");
93  return ff_filter_frame(outlink, frame);
94  }
95 
96  if (av_frame_is_writable(frame)) {
97  out = frame;
98  } else {
99  out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
100  if (!out) {
101  av_frame_free(&frame);
102  return AVERROR(ENOMEM);
103  }
104  av_frame_copy_props(out, frame);
105  }
106 
107  av_log(ctx, AV_LOG_TRACE,
108  "picture will move %s one line\n",
109  s->dst_tff ? "up" : "down");
110  h = frame->height;
111  for (plane = 0; plane < 4 && frame->data[plane] && frame->linesize[plane]; plane++) {
112  dst_line_step = out->linesize[plane];
113  src_line_step = frame->linesize[plane];
114  line_size = s->line_size[plane];
115  dst = out->data[plane];
116  src = frame->data[plane];
117  if (s->dst_tff) {
118  /** Move every line up one line, working from
119  * the top to the bottom of the frame.
120  * The original top line is lost.
121  * The new last line is created as a copy of the
122  * penultimate line from that field. */
123  for (line = 0; line < h; line++) {
124  if (1 + line < frame->height) {
125  memcpy(dst, src + src_line_step, line_size);
126  } else {
127  memcpy(dst, src - 2 * src_line_step, line_size);
128  }
129  dst += dst_line_step;
130  src += src_line_step;
131  }
132  } else {
133  /** Move every line down one line, working from
134  * the bottom to the top of the frame.
135  * The original bottom line is lost.
136  * The new first line is created as a copy of the
137  * second line from that field. */
138  dst += (h - 1) * dst_line_step;
139  src += (h - 1) * src_line_step;
140  for (line = h - 1; line >= 0 ; line--) {
141  if (line > 0) {
142  memcpy(dst, src - src_line_step, line_size);
143  } else {
144  memcpy(dst, src + 2 * src_line_step, line_size);
145  }
146  dst -= dst_line_step;
147  src -= src_line_step;
148  }
149  }
150  }
151  out->top_field_first = s->dst_tff;
152 
153  if (frame != out)
154  av_frame_free(&frame);
155  return ff_filter_frame(outlink, out);
156 }
157 
158 #define OFFSET(x) offsetof(FieldOrderContext, x)
159 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
160 
161 static const AVOption fieldorder_options[] = {
162  { "order", "output field order", OFFSET(dst_tff), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS, "order" },
163  { "bff", "bottom field first", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, .flags=FLAGS, .unit = "order" },
164  { "tff", "top field first", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, .flags=FLAGS, .unit = "order" },
165  { NULL }
166 };
167 
168 AVFILTER_DEFINE_CLASS(fieldorder);
169 
171  {
172  .name = "default",
173  .type = AVMEDIA_TYPE_VIDEO,
174  .config_props = config_input,
175  .filter_frame = filter_frame,
176  },
177  { NULL }
178 };
179 
181  {
182  .name = "default",
183  .type = AVMEDIA_TYPE_VIDEO,
184  },
185  { NULL }
186 };
187 
189  .name = "fieldorder",
190  .description = NULL_IF_CONFIG_SMALL("Set the field order."),
191  .priv_size = sizeof(FieldOrderContext),
192  .priv_class = &fieldorder_class,
194  .inputs = avfilter_vf_fieldorder_inputs,
195  .outputs = avfilter_vf_fieldorder_outputs,
197 };
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:115
int plane
Definition: avisynth_c.h:291
AVFilter ff_vf_fieldorder
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:631
static enum AVPixelFormat pix_fmt
This structure describes decoded (raw) audio or video data.
Definition: frame.h:171
static const AVOption fieldorder_options[]
AVOption.
Definition: opt.h:255
misc image utilities
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:248
Main libavfilter public API header.
static int query_formats(AVFilterContext *ctx)
Definition: vf_fieldorder.c:41
static enum AVSampleFormat formats[]
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition: video.c:109
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
Definition: avfilter.h:451
const char * name
Pad name.
Definition: internal.h:67
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:641
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
uint8_t
AVOptions.
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:204
int dst_tff
output bff/tff
Definition: vf_fieldorder.c:37
AVFILTER_DEFINE_CLASS(fieldorder)
static AVFrame * frame
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
int interlaced_frame
The content of the picture is interlaced.
Definition: frame.h:367
static const AVFilterPad avfilter_vf_fieldorder_inputs[]
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:61
int line_size[4]
bytes of pixel data per line for each plane
Definition: vf_fieldorder.c:38
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
Definition: pixdesc.h:89
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:148
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:175
void * priv
private data for use by the filter
Definition: avfilter.h:654
Definition: graph2dot.c:48
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:123
int ff_add_format(AVFilterFormats **avff, int64_t fmt)
Add fmt to the list of media formats contained in *avff.
Definition: formats.c:323
#define FLAGS
common internal API header
enum AVPixelFormat av_pix_fmt_desc_get_id(const AVPixFmtDescriptor *desc)
Definition: pixdesc.c:2109
uint8_t nb_components
The number of components each pixel has, (1-4)
Definition: pixdesc.h:71
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:422
ret
Definition: avfilter.c:974
#define OFFSET(x)
AVS_Value src
Definition: avisynth_c.h:482
int av_frame_is_writable(AVFrame *frame)
Check if the frame data is writable.
Definition: frame.c:488
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:199
uint8_t flags
Definition: pixdesc.h:90
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:470
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:88
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:239
void ff_formats_unref(AVFilterFormats **ref)
If *ref is non-NULL, remove *ref as a reference to the format list it currently points to...
Definition: formats.c:458
const char * name
Filter name.
Definition: avfilter.h:474
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:119
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
static const AVFilterPad avfilter_vf_fieldorder_outputs[]
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_fieldorder.c:78
static int flags
Definition: cpu.c:47
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:182
static int config_input(AVFilterLink *inlink)
Definition: vf_fieldorder.c:70
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:372
A list of supported formats for one end of a filter link.
Definition: formats.h:64
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;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);returnNULL;}returnac;}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;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->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);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
An instance of a filter.
Definition: avfilter.h:633
int height
Definition: frame.h:220
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:548
const AVPixFmtDescriptor * av_pix_fmt_desc_next(const AVPixFmtDescriptor *prev)
Iterate over all pixel format descriptors known to libavutil.
Definition: pixdesc.c:2097