vf_libopencv.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. typedef struct {
  57. const char *name;
  58. int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
  59. void (*uninit)(AVFilterContext *ctx);
  60. void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
  61. void *priv;
  62. } OCVContext;
  63. typedef struct {
  64. int type;
  65. int param1, param2;
  66. double param3, param4;
  67. } SmoothContext;
  68. static av_cold int smooth_init(AVFilterContext *ctx, const char *args, void *opaque)
  69. {
  70. OCVContext *ocv = ctx->priv;
  71. SmoothContext *smooth = ocv->priv;
  72. char type_str[128] = "gaussian";
  73. smooth->param1 = 3;
  74. smooth->param2 = 0;
  75. smooth->param3 = 0.0;
  76. smooth->param4 = 0.0;
  77. if (args)
  78. sscanf(args, "%127[^:]:%d:%d:%lf:%lf", type_str, &smooth->param1, &smooth->param2, &smooth->param3, &smooth->param4);
  79. if (!strcmp(type_str, "blur" )) smooth->type = CV_BLUR;
  80. else if (!strcmp(type_str, "blur_no_scale")) smooth->type = CV_BLUR_NO_SCALE;
  81. else if (!strcmp(type_str, "median" )) smooth->type = CV_MEDIAN;
  82. else if (!strcmp(type_str, "gaussian" )) smooth->type = CV_GAUSSIAN;
  83. else if (!strcmp(type_str, "bilateral" )) smooth->type = CV_BILATERAL;
  84. else {
  85. av_log(ctx, AV_LOG_ERROR, "Smoothing type '%s' unknown\n.", type_str);
  86. return AVERROR(EINVAL);
  87. }
  88. if (smooth->param1 < 0 || !(smooth->param1%2)) {
  89. av_log(ctx, AV_LOG_ERROR,
  90. "Invalid value '%d' for param1, it has to be a positive odd number\n",
  91. smooth->param1);
  92. return AVERROR(EINVAL);
  93. }
  94. if ((smooth->type == CV_BLUR || smooth->type == CV_BLUR_NO_SCALE || smooth->type == CV_GAUSSIAN) &&
  95. (smooth->param2 < 0 || (smooth->param2 && !(smooth->param2%2)))) {
  96. av_log(ctx, AV_LOG_ERROR,
  97. "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
  98. smooth->param2);
  99. return AVERROR(EINVAL);
  100. }
  101. av_log(ctx, AV_LOG_INFO, "type:%s param1:%d param2:%d param3:%f param4:%f\n",
  102. type_str, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
  103. return 0;
  104. }
  105. static void smooth_end_frame_filter(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg)
  106. {
  107. OCVContext *ocv = ctx->priv;
  108. SmoothContext *smooth = ocv->priv;
  109. cvSmooth(inimg, outimg, smooth->type, smooth->param1, smooth->param2, smooth->param3, smooth->param4);
  110. }
  111. typedef struct {
  112. const char *name;
  113. size_t priv_size;
  114. int (*init)(AVFilterContext *ctx, const char *args, void *opaque);
  115. void (*uninit)(AVFilterContext *ctx);
  116. void (*end_frame_filter)(AVFilterContext *ctx, IplImage *inimg, IplImage *outimg);
  117. } OCVFilterEntry;
  118. static OCVFilterEntry ocv_filter_entries[] = {
  119. { "smooth", sizeof(SmoothContext), smooth_init, NULL, smooth_end_frame_filter },
  120. };
  121. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  122. {
  123. OCVContext *ocv = ctx->priv;
  124. char name[128], priv_args[1024];
  125. int i;
  126. char c;
  127. sscanf(args, "%127[^=:]%c%1023s", name, &c, priv_args);
  128. for (i = 0; i < FF_ARRAY_ELEMS(ocv_filter_entries); i++) {
  129. OCVFilterEntry *entry = &ocv_filter_entries[i];
  130. if (!strcmp(name, entry->name)) {
  131. ocv->name = entry->name;
  132. ocv->init = entry->init;
  133. ocv->uninit = entry->uninit;
  134. ocv->end_frame_filter = entry->end_frame_filter;
  135. if (!(ocv->priv = av_mallocz(entry->priv_size)))
  136. return AVERROR(ENOMEM);
  137. return ocv->init(ctx, priv_args, opaque);
  138. }
  139. }
  140. av_log(ctx, AV_LOG_ERROR, "No libopencv filter named '%s'\n", name);
  141. return AVERROR(EINVAL);
  142. }
  143. static av_cold void uninit(AVFilterContext *ctx)
  144. {
  145. OCVContext *ocv = ctx->priv;
  146. if (ocv->uninit)
  147. ocv->uninit(ctx);
  148. av_free(ocv->priv);
  149. memset(ocv, 0, sizeof(*ocv));
  150. }
  151. static void end_frame(AVFilterLink *inlink)
  152. {
  153. AVFilterContext *ctx = inlink->dst;
  154. OCVContext *ocv = ctx->priv;
  155. AVFilterLink *outlink= inlink->dst->outputs[0];
  156. AVFilterBufferRef *inpicref = inlink ->cur_buf;
  157. AVFilterBufferRef *outpicref = outlink->out_buf;
  158. IplImage inimg, outimg;
  159. fill_iplimage_from_picref(&inimg , inpicref , inlink->format);
  160. fill_iplimage_from_picref(&outimg, outpicref, inlink->format);
  161. ocv->end_frame_filter(ctx, &inimg, &outimg);
  162. fill_picref_from_iplimage(outpicref, &outimg, inlink->format);
  163. avfilter_unref_buffer(inpicref);
  164. avfilter_draw_slice(outlink, 0, outlink->h, 1);
  165. avfilter_end_frame(outlink);
  166. avfilter_unref_buffer(outpicref);
  167. }
  168. AVFilter avfilter_vf_ocv = {
  169. .name = "ocv",
  170. .description = NULL_IF_CONFIG_SMALL("Apply transform using libopencv."),
  171. .priv_size = sizeof(OCVContext),
  172. .query_formats = query_formats,
  173. .init = init,
  174. .uninit = uninit,
  175. .inputs = (AVFilterPad[]) {{ .name = "default",
  176. .type = AVMEDIA_TYPE_VIDEO,
  177. .draw_slice = null_draw_slice,
  178. .end_frame = end_frame,
  179. .min_perms = AV_PERM_READ },
  180. { .name = NULL}},
  181. .outputs = (AVFilterPad[]) {{ .name = "default",
  182. .type = AVMEDIA_TYPE_VIDEO, },
  183. { .name = NULL}},
  184. };