32 #define SUB_PIXEL_BITS 8
33 #define SUB_PIXELS (1 << SUB_PIXEL_BITS)
53 void *
arg,
int job,
int nb_jobs);
56 #define OFFSET(x) offsetof(PerspectiveContext, x)
57 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
77 {
"source",
"specify locations in source to send to corners in destination",
79 {
"destination",
"specify locations in destination to send corners of source",
109 coeff = (1.0 - (A + 3.0) * d * d + (A + 2.0) * d * d * d);
111 coeff = (-4.0 * A + 8.0 * A * d - 5.0 * A * d * d + A * d * d * d);
123 double x0, x1, x2, x3, x4, x5, x6, x7, x8, q;
127 double (*ref)[2] = s->
ref;
134 for (i = 0; i < 4; i++) {
135 for (j = 0; j < 2; j++) {
162 x6 = ((ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
163 (ref[2][1] - ref[3][1]) -
164 ( ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
165 (ref[2][0] - ref[3][0])) * h;
166 x7 = ((ref[0][1] - ref[1][1] - ref[2][1] + ref[3][1]) *
167 (ref[1][0] - ref[3][0]) -
168 ( ref[0][0] - ref[1][0] - ref[2][0] + ref[3][0]) *
169 (ref[1][1] - ref[3][1])) * w;
170 q = ( ref[1][0] - ref[3][0]) * (ref[2][1] - ref[3][1]) -
171 ( ref[2][0] - ref[3][0]) * (ref[1][1] - ref[3][1]);
173 x0 = q * (ref[1][0] - ref[0][0]) * h + x6 * ref[1][0];
174 x1 = q * (ref[2][0] - ref[0][0]) * w + x7 * ref[2][0];
175 x2 = q * ref[0][0] * w *
h;
176 x3 = q * (ref[1][1] - ref[0][1]) * h + x6 * ref[1][1];
177 x4 = q * (ref[2][1] - ref[0][1]) * w + x7 * ref[2][1];
178 x5 = q * ref[0][1] * w *
h;
182 t0 = ref[0][0] * (ref[3][1] - ref[1][1]) +
183 ref[1][0] * (ref[0][1] - ref[3][1]) +
184 ref[3][0] * (ref[1][1] - ref[0][1]);
185 t1 = ref[1][0] * (ref[2][1] - ref[3][1]) +
186 ref[2][0] * (ref[3][1] - ref[1][1]) +
187 ref[3][0] * (ref[1][1] - ref[2][1]);
188 t2 = ref[0][0] * (ref[3][1] - ref[2][1]) +
189 ref[2][0] * (ref[0][1] - ref[3][1]) +
190 ref[3][0] * (ref[2][1] - ref[0][1]);
191 t3 = ref[0][0] * (ref[1][1] - ref[2][1]) +
192 ref[1][0] * (ref[2][1] - ref[0][1]) +
193 ref[2][0] * (ref[0][1] - ref[1][1]);
195 x0 = t0 * t1 * w * (ref[2][1] - ref[0][1]);
196 x1 = t0 * t1 * w * (ref[0][0] - ref[2][0]);
197 x2 = t0 * t1 * w * (ref[0][1] * ref[2][0] - ref[0][0] * ref[2][1]);
198 x3 = t1 * t2 * h * (ref[1][1] - ref[0][1]);
199 x4 = t1 * t2 * h * (ref[0][0] - ref[1][0]);
200 x5 = t1 * t2 * h * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]);
201 x6 = t1 * t2 * (ref[1][1] - ref[0][1]) +
202 t0 * t3 * (ref[2][1] - ref[3][1]);
203 x7 = t1 * t2 * (ref[0][0] - ref[1][0]) +
204 t0 * t3 * (ref[3][0] - ref[2][0]);
205 x8 = t1 * t2 * (ref[0][1] * ref[1][0] - ref[0][0] * ref[1][1]) +
206 t0 * t3 * (ref[2][0] * ref[3][1] - ref[2][1] * ref[3][0]);
212 for (y = 0; y <
h; y++){
213 for (x = 0; x < w; x++){
217 (x6 * x + x7 * y + x8));
219 (x6 * x + x7 * y + x8));
221 s->
pv[x + y * w][0] = u;
222 s->
pv[x + y * w][1] = v;
227 double d = i / (double)SUB_PIXELS;
231 for (j = 0; j < 4; j++)
234 for (j = 0; j < 4; j++)
237 for (j = 0; j < 4; j++)
254 int job,
int nb_jobs)
266 int start = (h * job) / nb_jobs;
267 int end = (h * (job+1)) / nb_jobs;
268 const int linesize = s->
linesize[0];
271 for (y = start; y <
end; y++) {
273 for (x = 0; x < w; x++) {
274 int u, v, subU, subV, sum, sx;
277 u = s->
pv[sx + sy * linesize][0] >> hsub;
278 v = s->
pv[sx + sy * linesize][1] >> vsub;
284 if (u > 0 && v > 0 && u < w - 2 && v < h - 2){
285 const int index = u + v*src_linesize;
286 const int a = s->
coeff[subU][0];
287 const int b = s->
coeff[subU][1];
288 const int c = s->
coeff[subU][2];
289 const int d = s->
coeff[subU][3];
291 sum = s->
coeff[subV][0] * (a * src[index - 1 - src_linesize] + b * src[index - 0 - src_linesize] +
292 c * src[index + 1 - src_linesize] + d * src[index + 2 - src_linesize]) +
293 s->
coeff[subV][1] * (a * src[index - 1 ] + b * src[index - 0 ] +
294 c * src[index + 1 ] + d * src[index + 2 ]) +
295 s->
coeff[subV][2] * (a * src[index - 1 + src_linesize] + b * src[index - 0 + src_linesize] +
296 c * src[index + 1 + src_linesize] + d * src[index + 2 + src_linesize]) +
297 s->
coeff[subV][3] * (a * src[index - 1 + 2 * src_linesize] + b * src[index - 0 + 2 * src_linesize] +
298 c * src[index + 1 + 2 * src_linesize] + d * src[index + 2 + 2 * src_linesize]);
304 for (dy = 0; dy < 4; dy++) {
311 for (dx = 0; dx < 4; dx++) {
319 sum += s->
coeff[subU][dx] * s->
coeff[subV][dy] * src[ ix + iy * src_linesize];
325 sum = av_clip_uint8(sum);
326 dst[x + y * dst_linesize] = sum;
333 int job,
int nb_jobs)
345 int start = (h * job) / nb_jobs;
346 int end = (h * (job+1)) / nb_jobs;
347 const int linesize = s->
linesize[0];
350 for (y = start; y <
end; y++){
352 for (x = 0; x < w; x++){
353 int u, v, subU, subV, sum, sx,
index, subUI, subVI;
356 u = s->
pv[sx + sy * linesize][0] >> hsub;
357 v = s->
pv[sx + sy * linesize][1] >> vsub;
363 index = u + v * src_linesize;
367 if ((
unsigned)u < (unsigned)(w - 1)){
368 if((
unsigned)v < (unsigned)(h - 1)){
369 sum = subVI * (subUI * src[
index] + subU * src[index + 1]) +
370 subV * (subUI * src[index + src_linesize] + subU * src[index + src_linesize + 1]);
377 index = u + v * src_linesize;
378 sum = subUI * src[
index] + subU * src[index + 1];
386 if ((
unsigned)v < (unsigned)(h - 1)){
387 index = u + v * src_linesize;
388 sum = subVI * src[
index] + subV * src[index + src_linesize];
395 index = u + v * src_linesize;
400 sum = av_clip_uint8(sum);
401 dst[x + y * dst_linesize] = sum;
434 for (plane = 0; plane < s->
nb_planes; plane++) {
435 int hsub = plane == 1 || plane == 2 ? s->
hsub : 0;
436 int vsub = plane == 1 || plane == 2 ? s->
vsub : 0;
478 .
name =
"perspective",
484 .
inputs = perspective_inputs,
485 .
outputs = perspective_outputs,
486 .priv_class = &perspective_class,
static int resample_linear(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
This structure describes decoded (raw) audio or video data.
#define av_realloc_f(p, o, n)
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
int av_pix_fmt_count_planes(enum AVPixelFormat pix_fmt)
Main libavfilter public API header.
int h
agreed upon image height
AVFILTER_DEFINE_CLASS(perspective)
static int resample_cubic(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
coordinates give locations in destination of corners of source.
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)
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
struct AVFilterGraph * graph
filtergraph this filter belongs to
#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.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
static av_cold void uninit(AVFilterContext *ctx)
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
int32_t coeff[SUB_PIXELS][4]
static av_cold int end(AVCodecContext *avctx)
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range...
int nb_threads
Maximum number of threads used by filters in this graph.
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
static const AVFilterPad perspective_inputs[]
A filter pad used for either input or output.
A link between two filters.
int av_expr_parse_and_eval(double *d, 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.
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
simple assert() macros that are a bit more flexible than ISO C assert().
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int w
agreed upon image width
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
static const AVFilterPad outputs[]
int format
agreed upon media format
static const AVFilterPad inputs[]
coordinates give locations in source of corners of destination.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
int(* perspective)(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
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 char *const var_names[]
const char * name
Filter name.
AVFilterLink ** outputs
array of pointers to output links
static enum AVPixelFormat pix_fmts[]
AVFilterInternal * internal
An opaque struct for libavfilter internal use.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
static av_cold int init(AVFilterContext *ctx)
planar GBRA 4:4:4:4 32bpp
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
avfilter_execute_func * execute
static double get_coeff(double d)
AVFilter ff_vf_perspective
AVFilterContext * dst
dest filter
static const double coeff[2][5]
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
static const AVFilterPad perspective_outputs[]
static const AVOption perspective_options[]
AVPixelFormat
Pixel format.
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
simple arithmetic expression evaluator
#define AV_CEIL_RSHIFT(a, b)