vf_libopencv.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * copyright (c) 2010 Stefano Sabatini
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * libopencv wrapper functions
  23. */
  24. #include <opencv/cv.h>
  25. #include <opencv/cxtypes.h>
  26. #include "avfilter.h"
  27. static void fill_iplimage_from_picref(IplImage *img, const AVFilterBufferRef *picref, enum PixelFormat pixfmt)
  28. {
  29. IplImage *tmpimg;
  30. int depth, channels_nb;
  31. if (pixfmt == PIX_FMT_GRAY8) { depth = IPL_DEPTH_8U; channels_nb = 1; }
  32. else if (pixfmt == PIX_FMT_BGRA) { depth = IPL_DEPTH_8U; channels_nb = 4; }
  33. else if (pixfmt == PIX_FMT_BGR24) { depth = IPL_DEPTH_8U; channels_nb = 3; }
  34. else return;
  35. tmpimg = cvCreateImageHeader((CvSize){picref->video->w, picref->video->h}, depth, channels_nb);
  36. *img = *tmpimg;
  37. img->imageData = img->imageDataOrigin = picref->data[0];
  38. img->dataOrder = IPL_DATA_ORDER_PIXEL;
  39. img->origin = IPL_ORIGIN_TL;
  40. img->widthStep = picref->linesize[0];
  41. }
  42. static void fill_picref_from_iplimage(AVFilterBufferRef *picref, const IplImage *img, enum PixelFormat pixfmt)
  43. {
  44. picref->linesize[0] = img->widthStep;
  45. picref->data[0] = img->imageData;
  46. }
  47. static int query_formats(AVFilterContext *ctx)
  48. {
  49. static const enum PixelFormat pix_fmts[] = {
  50. PIX_FMT_BGR24, PIX_FMT_BGRA, PIX_FMT_GRAY8, PIX_FMT_NONE
  51. };
  52. avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
  53. return 0;
  54. }
  55. static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
  56. #if CONFIG_OCV_SMOOTH_FILTER
  57. typedef struct {
  58. int type;
  59. int param1, param2;
  60. double param3, param4;
  61. } SmoothContext;
  62. static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
  63. {
  64. SmoothContext *smooth = ctx->priv;
  65. char type_str[128] = "gaussian";
  66. smooth->param1 = 3;
  67. smooth->param2 = 0;
  68. smooth->param3 = 0.0;
  69. smooth->param4 = 0.0;
  70. if (args)
  71. sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
  72. if (!strcmp(type_str, "blur" )) smooth->type = CV_BLUR;
  73. else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
  74. else if (!strcmp(type_str, "median" )) smooth->type = CV_MEDIAN;
  75. else if (!strcmp(type_str, "gaussian" )) smooth->type = CV_GAUSSIAN;
  76. else if (!strcmp(type_str, "bilateral" )) smooth->type = CV_BILATERAL;
  77. else {
  78. av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str);
  79. return AVERROR(EINVAL);
  80. }
  81. if (smooth->param1 < 0 || !(smooth->param1%2)) {
  82. av_log(ctx, AV_LOG_ERROR,
  83. "Invalid value '%d' for param1, it has to be a positive odd number\n",
  84. smooth->param1);
  85. return AVERROR(EINVAL);
  86. }
  87. if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
  88. (smooth->param2 < 0 || (smooth->param2 && !(smooth->param2%2)))) {
  89. av_log(ctx, AV_LOG_ERROR,
  90. "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
  91. smooth->param2);
  92. return AVERROR(EINVAL);
  93. }
  94. av_log(ctx, AV_LOG_INFO, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
  95. type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
  96. return 0;
  97. }
  98. static void smooth_end_frame(AVFilterLink *inlink)
  99. {
  100. SmoothContext *smooth = inlink->dst->priv;
  101. AVFilterLink *outlink = inlink->dst->outputs[0];
  102. AVFilterBufferRef *inpicref = inlink ->cur_buf;
  103. AVFilterBufferRef *outpicref = outlink->out_buf;
  104. IplImage inimg, outimg;
  105. fill_iplimage_from_picref(&inimg , inpicref , inlink->format);
  106. fill_iplimage_from_picref(&outimg, outpicref, inlink->format);
  107. cvSmooth(&inimg, &outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
  108. fill_picref_from_iplimage(outpicref, &outimg, inlink->format);
  109. avfilter_unref_buffer(inpicref);
  110. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  111. avfilter_end_frame(outlink);
  112. avfilter_unref_buffer(outpicref);
  113. }
  114. AVFilter avfilter_vf_ocv_smooth = {
  115. .name = "ocv_smooth",
  116. .description = NULL_IF_CONFIG_SMALL("Apply smooth transform using libopencv."),
  117. .priv_size = sizeof(SmoothContext),
  118. .query_formats = query_formats,
  119. .init = smooth_init,
  120. .inputs = (AVFilterPad[]) {{ .name = "default",
  121. .type = AVMEDIA_TYPE_VIDEO,
  122. .draw_slice = null_draw_slice,
  123. .end_frame = smooth_end_frame,
  124. .min_perms = AV_PERM_READ },
  125. { .name = NULL}},
  126. .outputs = (AVFilterPad[]) {{ .name = "default",
  127. .type = AVMEDIA_TYPE_VIDEO, },
  128. { .name = NULL}},
  129. };
  130. #endif /* CONFIG_OCV_SMOOTH_FILTER */