vf_lenscorrection.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright (C) 2007 Richard Spindler (author of frei0r plugin from which this was derived)
  3. * Copyright (C) 2014 Daniel Oberhoff
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Lenscorrection filter, algorithm from the frei0r plugin with the same name
  24. */
  25. #include <stdlib.h>
  26. #include <math.h>
  27. #include "libavutil/opt.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/pixdesc.h"
  30. #include "avfilter.h"
  31. #include "internal.h"
  32. #include "video.h"
  33. typedef struct LenscorrectionCtx {
  34. const AVClass *av_class;
  35. unsigned int width;
  36. unsigned int height;
  37. int hsub, vsub;
  38. int nb_planes;
  39. double cx, cy, k1, k2;
  40. int32_t *correction[4];
  41. } LenscorrectionCtx;
  42. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  43. static const AVOption lenscorrection_options[] = {
  44. { "cx", "set relative center x", offsetof(LenscorrectionCtx, cx), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
  45. { "cy", "set relative center y", offsetof(LenscorrectionCtx, cy), AV_OPT_TYPE_DOUBLE, {.dbl=0.5}, 0, 1, .flags=FLAGS },
  46. { "k1", "set quadratic distortion factor", offsetof(LenscorrectionCtx, k1), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
  47. { "k2", "set double quadratic distortion factor", offsetof(LenscorrectionCtx, k2), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -1, 1, .flags=FLAGS },
  48. { NULL }
  49. };
  50. AVFILTER_DEFINE_CLASS(lenscorrection);
  51. typedef struct ThreadData {
  52. AVFrame *in, *out;
  53. int w, h;
  54. int plane;
  55. int xcenter, ycenter;
  56. int32_t *correction;
  57. } ThreadData;
  58. static int filter_slice(AVFilterContext *ctx, void *arg, int job, int nb_jobs)
  59. {
  60. ThreadData *td = (ThreadData*)arg;
  61. AVFrame *in = td->in;
  62. AVFrame *out = td->out;
  63. const int w = td->w, h = td->h;
  64. const int xcenter = td->xcenter;
  65. const int ycenter = td->ycenter;
  66. const int start = (h * job ) / nb_jobs;
  67. const int end = (h * (job+1)) / nb_jobs;
  68. const int plane = td->plane;
  69. const int inlinesize = in->linesize[plane];
  70. const int outlinesize = out->linesize[plane];
  71. const uint8_t *indata = in->data[plane];
  72. uint8_t *outrow = out->data[plane] + start * outlinesize;
  73. int i;
  74. for (i = start; i < end; i++, outrow += outlinesize) {
  75. const int off_y = i - ycenter;
  76. uint8_t *out = outrow;
  77. int j;
  78. for (j = 0; j < w; j++) {
  79. const int off_x = j - xcenter;
  80. const int64_t radius_mult = td->correction[j + i*w];
  81. const int x = xcenter + ((radius_mult * off_x + (1<<23))>>24);
  82. const int y = ycenter + ((radius_mult * off_y + (1<<23))>>24);
  83. const char isvalid = x > 0 && x < w - 1 && y > 0 && y < h - 1;
  84. *out++ = isvalid ? indata[y * inlinesize + x] : 0;
  85. }
  86. }
  87. return 0;
  88. }
  89. static int query_formats(AVFilterContext *ctx)
  90. {
  91. static const enum AVPixelFormat pix_fmts[] = {
  92. AV_PIX_FMT_YUV410P,
  93. AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
  94. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVJ420P,
  95. AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUVA420P,
  96. AV_PIX_FMT_YUV422P,
  97. AV_PIX_FMT_NONE
  98. };
  99. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  100. return 0;
  101. }
  102. static av_cold void uninit(AVFilterContext *ctx)
  103. {
  104. LenscorrectionCtx *rect = ctx->priv;
  105. int i;
  106. for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
  107. av_freep(&rect->correction[i]);
  108. }
  109. }
  110. static int config_props(AVFilterLink *outlink)
  111. {
  112. AVFilterContext *ctx = outlink->src;
  113. LenscorrectionCtx *rect = ctx->priv;
  114. AVFilterLink *inlink = ctx->inputs[0];
  115. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  116. rect->hsub = pixdesc->log2_chroma_w;
  117. rect->vsub = pixdesc->log2_chroma_h;
  118. outlink->w = rect->width = inlink->w;
  119. outlink->h = rect->height = inlink->h;
  120. rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
  121. return 0;
  122. }
  123. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  124. {
  125. AVFilterContext *ctx = inlink->dst;
  126. AVFilterLink *outlink = ctx->outputs[0];
  127. LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv;
  128. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  129. int plane;
  130. if (!out) {
  131. av_frame_free(&in);
  132. return AVERROR(ENOMEM);
  133. }
  134. av_frame_copy_props(out, in);
  135. for (plane = 0; plane < rect->nb_planes; ++plane) {
  136. int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
  137. int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
  138. int hdiv = 1 << hsub;
  139. int vdiv = 1 << vsub;
  140. int w = rect->width / hdiv;
  141. int h = rect->height / vdiv;
  142. int xcenter = rect->cx * w;
  143. int ycenter = rect->cy * h;
  144. int k1 = rect->k1 * (1<<24);
  145. int k2 = rect->k2 * (1<<24);
  146. ThreadData td = {
  147. .in = in,
  148. .out = out,
  149. .w = w,
  150. .h = h,
  151. .xcenter = xcenter,
  152. .ycenter = ycenter,
  153. .plane = plane};
  154. if (!rect->correction[plane]) {
  155. int i,j;
  156. const int64_t r2inv = (4LL<<60) / (w * w + h * h);
  157. rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
  158. if (!rect->correction[plane])
  159. return AVERROR(ENOMEM);
  160. for (j = 0; j < h; j++) {
  161. const int off_y = j - ycenter;
  162. const int off_y2 = off_y * off_y;
  163. for (i = 0; i < w; i++) {
  164. const int off_x = i - xcenter;
  165. const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
  166. const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
  167. const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
  168. rect->correction[plane][j * w + i] = radius_mult;
  169. }
  170. }
  171. }
  172. td.correction = rect->correction[plane];
  173. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
  174. }
  175. av_frame_free(&in);
  176. return ff_filter_frame(outlink, out);
  177. }
  178. static const AVFilterPad lenscorrection_inputs[] = {
  179. {
  180. .name = "default",
  181. .type = AVMEDIA_TYPE_VIDEO,
  182. .filter_frame = filter_frame,
  183. },
  184. { NULL }
  185. };
  186. static const AVFilterPad lenscorrection_outputs[] = {
  187. {
  188. .name = "default",
  189. .type = AVMEDIA_TYPE_VIDEO,
  190. .config_props = config_props,
  191. },
  192. { NULL }
  193. };
  194. AVFilter ff_vf_lenscorrection = {
  195. .name = "lenscorrection",
  196. .description = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
  197. .priv_size = sizeof(LenscorrectionCtx),
  198. .query_formats = query_formats,
  199. .inputs = lenscorrection_inputs,
  200. .outputs = lenscorrection_outputs,
  201. .priv_class = &lenscorrection_class,
  202. .uninit = uninit,
  203. .flags = AVFILTER_FLAG_SLICE_THREADS,
  204. };