vf_lenscorrection.c 7.7 KB

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