FFmpeg  2.6.3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
34 static av_cold void uninit(AVFilterContext *ctx)
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  ff_set_common_formats(ctx, ff_make_format_list(pixel_fmts_eq));
54 
55  return 0;
56 }
57 
58 static int config_input(AVFilterLink *inlink)
59 {
60  RepeatFieldsContext *s = inlink->dst->priv;
61  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
62  int ret;
63 
64  if ((ret = av_image_fill_linesizes(s->linesize, inlink->format, inlink->w)) < 0)
65  return ret;
66 
67  s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
68  s->planeheight[0] = s->planeheight[3] = inlink->h;
69 
71 
72  return 0;
73 }
74 
75 static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
76 {
77  if (av_cmp_q(link->frame_rate, (AVRational){30000, 1001}) == 0 &&
78  av_cmp_q(link->time_base, (AVRational){1001, 60000}) <= 0
79  ) {
80  f->pts = pts + av_rescale_q(fields, (AVRational){1001, 60000}, link->time_base);
81  } else
82  f->pts = AV_NOPTS_VALUE;
83 }
84 
85 static int filter_frame(AVFilterLink *inlink, AVFrame *in) {
86  AVFilterContext *ctx = inlink->dst;
87  AVFilterLink *outlink = inlink->dst->outputs[0];
88  RepeatFieldsContext *s = ctx->priv;
89  AVFrame *out;
90  int ret, i;
91  int state = s->state;
92 
93  if (!s->frame) {
94  s->frame = av_frame_clone(in);
95  if (!s->frame)
96  return AVERROR(ENOMEM);
97  s->frame->pts = AV_NOPTS_VALUE;
98  }
99 
100  out = s->frame;
101 
102  if ((state == 0 && !in->top_field_first) ||
103  (state == 1 && in->top_field_first)) {
104  av_log(ctx, AV_LOG_WARNING, "Unexpected field flags: "
105  "state=%d top_field_first=%d repeat_first_field=%d\n",
106  state, in->top_field_first, in->repeat_pict);
107  state ^= 1;
108  }
109 
110  if (state == 0) {
111  AVFrame *new;
112 
113  new = av_frame_clone(in);
114  if (!new)
115  return AVERROR(ENOMEM);
116 
117  ret = ff_filter_frame(outlink, new);
118 
119  if (in->repeat_pict) {
121  update_pts(outlink, out, in->pts, 2);
122  for (i = 0; i < s->nb_planes; i++) {
123  av_image_copy_plane(out->data[i], out->linesize[i] * 2,
124  in->data[i], in->linesize[i] * 2,
125  s->linesize[i], s->planeheight[i] / 2);
126  }
127  state = 1;
128  }
129  } else {
130  for (i = 0; i < s->nb_planes; i++) {
132  av_image_copy_plane(out->data[i] + out->linesize[i], out->linesize[i] * 2,
133  in->data[i] + in->linesize[i], in->linesize[i] * 2,
134  s->linesize[i], s->planeheight[i] / 2);
135  }
136 
137  ret = ff_filter_frame(outlink, av_frame_clone(out));
138 
139  if (in->repeat_pict) {
140  AVFrame *new;
141 
142  new = av_frame_clone(in);
143  if (!new)
144  return AVERROR(ENOMEM);
145 
146  ret = ff_filter_frame(outlink, new);
147  state = 0;
148  } else {
150  update_pts(outlink, out, in->pts, 1);
151  for (i = 0; i < s->nb_planes; i++) {
152  av_image_copy_plane(out->data[i], out->linesize[i] * 2,
153  in->data[i], in->linesize[i] * 2,
154  s->linesize[i], s->planeheight[i] / 2);
155  }
156  }
157  }
158 
159  s->state = state;
160 
161  av_frame_free(&in);
162  return ret;
163 }
164 
166  {
167  .name = "default",
168  .type = AVMEDIA_TYPE_VIDEO,
169  .filter_frame = filter_frame,
170  .config_props = config_input,
171  },
172  { NULL }
173 };
174 
176  {
177  .name = "default",
178  .type = AVMEDIA_TYPE_VIDEO,
179  },
180  { NULL }
181 };
182 
184  .name = "repeatfields",
185  .description = NULL_IF_CONFIG_SMALL("Hard repeat fields based on MPEG repeat field flag."),
186  .priv_size = sizeof(RepeatFieldsContext),
187  .uninit = uninit,
188  .inputs = repeatfields_inputs,
189  .outputs = repeatfields_outputs,
191 };
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:669
static const AVFilterPad repeatfields_outputs[]
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2029
This structure describes decoded (raw) audio or video data.
Definition: frame.h:163
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:73
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:181
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:246
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2069
Main libavfilter public API header.
static int config_input(AVFilterLink *inlink)
int repeat_pict
When decoding, this signals how much the picture must be delayed.
Definition: frame.h:354
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_dlog(ac->avr,"%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-> in
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:66
static int query_formats(AVFilterContext *ctx)
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
const char * name
Pad name.
Definition: internal.h:67
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1145
#define av_cold
Definition: attributes.h:74
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:249
void 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:539
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:61
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:140
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:180
void * priv
private data for use by the filter
Definition: avfilter.h:654
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:72
static const AVFilterPad repeatfields_inputs[]
ret
Definition: avfilter.c:974
#define FF_CEIL_RSHIFT(a, b)
Definition: common.h:57
AVFrame * av_frame_clone(const AVFrame *src)
Create a new frame that references the same data as src.
Definition: frame.c:364
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:191
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition: pixfmt.h:74
Describe the class of an AVClass context structure.
Definition: log.h:66
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:237
rational number numerator/denominator
Definition: rational.h:43
const char * name
Filter name.
Definition: avfilter.h:474
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:648
static int64_t pts
Global timestamp for the audio frames.
int av_frame_make_writable(AVFrame *frame)
Ensure that the frame data is writable, avoiding data copy if possible.
Definition: frame.c:420
static uint32_t state
Definition: trasher.c:27
AVFilter ff_vf_repeatfields
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:174
static av_cold void uninit(AVFilterContext *ctx)
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_dlog(ac->avr,"%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
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:68
Y , 8bpp.
Definition: pixfmt.h:76
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition: pixfmt.h:75
static void update_pts(AVFilterLink *link, AVFrame *f, int64_t pts, int fields)
int top_field_first
If the content is interlaced, is top field displayed first.
Definition: frame.h:364
An instance of a filter.
Definition: avfilter.h:633
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:273
internal API functions
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:241