vf_lenscorrection.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_GBRP, AV_PIX_FMT_GBRAP,
  98. AV_PIX_FMT_NONE
  99. };
  100. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  101. return 0;
  102. }
  103. static av_cold void uninit(AVFilterContext *ctx)
  104. {
  105. LenscorrectionCtx *rect = ctx->priv;
  106. int i;
  107. for (i = 0; i < FF_ARRAY_ELEMS(rect->correction); i++) {
  108. av_freep(&rect->correction[i]);
  109. }
  110. }
  111. static int config_props(AVFilterLink *outlink)
  112. {
  113. AVFilterContext *ctx = outlink->src;
  114. LenscorrectionCtx *rect = ctx->priv;
  115. AVFilterLink *inlink = ctx->inputs[0];
  116. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(inlink->format);
  117. rect->hsub = pixdesc->log2_chroma_w;
  118. rect->vsub = pixdesc->log2_chroma_h;
  119. outlink->w = rect->width = inlink->w;
  120. outlink->h = rect->height = inlink->h;
  121. rect->nb_planes = av_pix_fmt_count_planes(inlink->format);
  122. return 0;
  123. }
  124. static int filter_frame(AVFilterLink *inlink, AVFrame *in)
  125. {
  126. AVFilterContext *ctx = inlink->dst;
  127. AVFilterLink *outlink = ctx->outputs[0];
  128. LenscorrectionCtx *rect = (LenscorrectionCtx*)ctx->priv;
  129. AVFrame *out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
  130. int plane;
  131. if (!out) {
  132. av_frame_free(&in);
  133. return AVERROR(ENOMEM);
  134. }
  135. av_frame_copy_props(out, in);
  136. for (plane = 0; plane < rect->nb_planes; ++plane) {
  137. int hsub = plane == 1 || plane == 2 ? rect->hsub : 0;
  138. int vsub = plane == 1 || plane == 2 ? rect->vsub : 0;
  139. int hdiv = 1 << hsub;
  140. int vdiv = 1 << vsub;
  141. int w = rect->width / hdiv;
  142. int h = rect->height / vdiv;
  143. int xcenter = rect->cx * w;
  144. int ycenter = rect->cy * h;
  145. int k1 = rect->k1 * (1<<24);
  146. int k2 = rect->k2 * (1<<24);
  147. ThreadData td = {
  148. .in = in,
  149. .out = out,
  150. .w = w,
  151. .h = h,
  152. .xcenter = xcenter,
  153. .ycenter = ycenter,
  154. .plane = plane};
  155. if (!rect->correction[plane]) {
  156. int i,j;
  157. const int64_t r2inv = (4LL<<60) / (w * w + h * h);
  158. rect->correction[plane] = av_malloc_array(w, h * sizeof(**rect->correction));
  159. if (!rect->correction[plane])
  160. return AVERROR(ENOMEM);
  161. for (j = 0; j < h; j++) {
  162. const int off_y = j - ycenter;
  163. const int off_y2 = off_y * off_y;
  164. for (i = 0; i < w; i++) {
  165. const int off_x = i - xcenter;
  166. const int64_t r2 = ((off_x * off_x + off_y2) * r2inv + (1LL<<31)) >> 32;
  167. const int64_t r4 = (r2 * r2 + (1<<27)) >> 28;
  168. const int radius_mult = (r2 * k1 + r4 * k2 + (1LL<<27) + (1LL<<52))>>28;
  169. rect->correction[plane][j * w + i] = radius_mult;
  170. }
  171. }
  172. }
  173. td.correction = rect->correction[plane];
  174. ctx->internal->execute(ctx, filter_slice, &td, NULL, FFMIN(h, ctx->graph->nb_threads));
  175. }
  176. av_frame_free(&in);
  177. return ff_filter_frame(outlink, out);
  178. }
  179. static const AVFilterPad lenscorrection_inputs[] = {
  180. {
  181. .name = "default",
  182. .type = AVMEDIA_TYPE_VIDEO,
  183. .filter_frame = filter_frame,
  184. },
  185. { NULL }
  186. };
  187. static const AVFilterPad lenscorrection_outputs[] = {
  188. {
  189. .name = "default",
  190. .type = AVMEDIA_TYPE_VIDEO,
  191. .config_props = config_props,
  192. },
  193. { NULL }
  194. };
  195. AVFilter ff_vf_lenscorrection = {
  196. .name = "lenscorrection",
  197. .description = NULL_IF_CONFIG_SMALL("Rectify the image by correcting for lens distortion."),
  198. .priv_size = sizeof(LenscorrectionCtx),
  199. .query_formats = query_formats,
  200. .inputs = lenscorrection_inputs,
  201. .outputs = lenscorrection_outputs,
  202. .priv_class = &lenscorrection_class,
  203. .uninit = uninit,
  204. .flags = AVFILTER_FLAG_SLICE_THREADS,
  205. };