22 #include "libavutil/eval.h"
23 #include "libavutil/imgutils.h"
24 #include "libavutil/pixdesc.h"
25 #include "libavutil/opt.h"
31 #define SUB_PIXEL_BITS 8
32 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
56 #define OFFSET(x) offsetof(PerspectiveContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
96 coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
98 coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
105 static const char *
const var_names[] = {
"W",
"H", NULL };
110 double x0, x1, x2, x3, x4, x5, x6, x7, q;
113 double (*
ref)[2] = s->
ref;
120 for (i = 0; i < 4; i++) {
121 for (j = 0; j < 2; j++) {
126 NULL, NULL, NULL, NULL,
149 (
ref[2][0] -
ref[3][0])) * h;
153 (
ref[1][1] -
ref[3][1])) * w;
154 q = (
ref[1][0] -
ref[3][0]) * (
ref[2][1] -
ref[3][1]) -
157 x0 = q * (
ref[1][0] -
ref[0][0]) * h + x6 *
ref[1][0];
158 x1 = q * (
ref[2][0] -
ref[0][0]) * w + x7 *
ref[2][0];
159 x2 = q *
ref[0][0] * w * h;
160 x3 = q * (
ref[1][1] -
ref[0][1]) * h + x6 *
ref[1][1];
161 x4 = q * (
ref[2][1] -
ref[0][1]) * w + x7 *
ref[2][1];
162 x5 = q *
ref[0][1] * w * h;
164 for (y = 0; y < h; y++){
165 for (x = 0; x < w; x++){
168 u = (int)floor(
SUB_PIXELS * (x0 * x + x1 * y + x2) /
169 (x6 * x + x7 * y + q * w * h) + 0.5);
170 v = (int)floor(
SUB_PIXELS * (x3 * x + x4 * y + x5) /
171 (x6 * x + x7 * y + q * w * h) + 0.5);
173 s->
pv[x + y * w][0] =
u;
174 s->
pv[x + y * w][1] =
v;
179 double d = i / (double)SUB_PIXELS;
183 for (j = 0; j < 4; j++)
186 for (j = 0; j < 4; j++)
189 for (j = 0; j < 4; j++)
197 uint8_t *dst,
int dst_linesize,
204 for (y = 0; y < h; y++) {
206 for (x = 0; x < w; x++) {
207 int u,
v, subU, subV, sum, sx;
217 if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
218 const int index = u + v*src_linesize;
219 const int a = s->
coeff[subU][0];
220 const int b = s->
coeff[subU][1];
221 const int c = s->
coeff[subU][2];
222 const int d = s->
coeff[subU][3];
224 sum = s->
coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] +
225 c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) +
226 s->
coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] +
227 c * src[index + 1 ] + d * src[index + 2 ]) +
228 s->
coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] +
229 c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) +
230 s->
coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] +
231 c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
237 for (dy = 0; dy < 4; dy++) {
244 for (dx = 0; dx < 4; dx++) {
252 sum += s->
coeff[subU][dx] * s->
coeff[subV][dy] * src[ ix + iy * src_linesize];
258 sum = av_clip(sum, 0, 255);
259 dst[x + y * dst_linesize] = sum;
265 uint8_t *dst,
int dst_linesize,
272 for (y = 0; y < h; y++){
274 for (x = 0; x < w; x++){
275 int u,
v, subU, subV, sum, sx,
index, subUI, subVI;
285 index = u + v * src_linesize;
289 if ((
unsigned)u < (unsigned)(w - 1)){
290 if((
unsigned)v < (unsigned)(h - 1)){
291 sum = subVI * (subUI * src[
index] + subU * src[index + 1]) +
292 subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
299 index = u + v * src_linesize;
300 sum = subUI * src[
index] + subU * src[index + 1];
308 if ((
unsigned)v < (unsigned)(h - 1)){
309 index = u + v * src_linesize;
310 sum = subVI * src[
index] + subV * src[index + src_linesize];
317 index = u + v * src_linesize;
322 sum = av_clip(sum, 0, 255);
323 dst[x + y * dst_linesize] = sum;
355 for (plane = 0; plane < s->
nb_planes; plane++) {
356 int hsub = plane == 1 || plane == 2 ? s->
hsub : 0;
357 int vsub = plane == 1 || plane == 2 ? s->
vsub : 0;
393 .
name =
"perspective",
399 .
inputs = perspective_inputs,
400 .
outputs = perspective_outputs,
401 .priv_class = &perspective_class,
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
const char * name
Filter name.
void * priv
private data for use by the filter
static const AVFilterPad outputs[]
static void resample_linear(PerspectiveContext *s, uint8_t *dst, int dst_linesize, uint8_t *src, int src_linesize, int w, int h, int hsub, int vsub)
int h
agreed upon image height
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
static int query_formats(AVFilterContext *ctx)
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
#define AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC
Some filters support a generic "enable" expression option that can be used to enable or disable a fil...
static int config_input(AVFilterLink *inlink)
const char * name
Pad name.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static av_cold void uninit(AVFilterContext *ctx)
int32_t coeff[SUB_PIXELS][4]
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
#define FF_CEIL_RSHIFT(a, b)
static const AVFilterPad perspective_inputs[]
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
void(* perspective)(struct PerspectiveContext *s, uint8_t *dst, int dst_linesize, uint8_t *src, int src_linesize, int w, int h, int hsub, int vsub)
A filter pad used for either input or output.
A link between two filters.
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
planar GBRA 4:4:4:4 32bpp
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV444P and setting color_...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of PIX_FMT_YUV440P and setting color_range ...
AVFilter avfilter_vf_perspective
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
AVPixelFormat
Pixel format.
int w
agreed upon image width
int av_expr_parse_and_eval(double *res, const char *s, const char *const *const_names, const double *const_values, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), void *opaque, int log_offset, void *log_ctx)
Parse and evaluate an expression.
int format
agreed upon media format
Main libavfilter public API header.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV420P and setting color_...
AVFilterLink ** outputs
array of pointers to output links
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
typedef void(RENAME(mix_any_func_type))
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
void * av_realloc_f(void *ptr, size_t nelem, size_t elsize)
Allocate or reallocate a block of memory.
Describe the class of an AVClass context structure.
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.
static const AVFilterPad inputs[]
static const char *const var_names[]
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
AVFilterContext * dst
dest filter
static av_cold int init(AVFilterContext *ctx)
static double get_coeff(double d)
static void resample_cubic(PerspectiveContext *s, uint8_t *dst, int dst_linesize, uint8_t *src, int src_linesize, int w, int h, int hsub, int vsub)
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){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));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=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);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);ff_audio_convert_init_arm(ac);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
#define AVFILTER_DEFINE_CLASS(fname)
static const double coeff[2][5]
static const AVFilterPad perspective_outputs[]
static const AVOption perspective_options[]
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of PIX_FMT_YUV422P and setting color_...
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)