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 /**
22 * @file
23 * libopencv wrapper functions
24 */
25
26 #include <opencv/cv.h>
27 #include <opencv/cxcore.h>
36
38 {
39 IplImage *tmpimg;
40 int depth, channels_nb;
41
43 else if (pixfmt ==
AV_PIX_FMT_BGRA) { depth = IPL_DEPTH_8U; channels_nb = 4; }
44 else if (pixfmt ==
AV_PIX_FMT_BGR24) { depth = IPL_DEPTH_8U; channels_nb = 3; }
45 else return;
46
47 tmpimg = cvCreateImageHeader((CvSize){frame->
width, frame->
height},
depth, channels_nb);
48 *img = *tmpimg;
49 img->imageData = img->imageDataOrigin = frame->
data[0];
50 img->dataOrder = IPL_DATA_ORDER_PIXEL;
51 img->origin = IPL_ORIGIN_TL;
53 }
54
56 {
58 frame->
data[0] = img->imageData;
59 }
60
62 {
65 };
66
68 return 0;
69 }
70
80
86
88 {
91 char type_str[128] = "gaussian";
92
97
98 if (args)
99 sscanf(args,
"%127[^|]|%d|%d|%lf|%lf", type_str, &smooth->
param1, &smooth->
param2, &smooth->
param3, &smooth->
param4);
100
101 if (!strcmp(type_str,
"blur" )) smooth->
type = CV_BLUR;
102 else if (!strcmp(type_str,
"blur_no_scale")) smooth->
type = CV_BLUR_NO_SCALE;
103 else if (!strcmp(type_str,
"median" )) smooth->
type = CV_MEDIAN;
104 else if (!strcmp(type_str,
"gaussian" )) smooth->
type = CV_GAUSSIAN;
105 else if (!strcmp(type_str,
"bilateral" )) smooth->
type = CV_BILATERAL;
106 else {
109 }
110
113 "Invalid value '%d' for param1, it has to be a positive odd number\n",
116 }
117 if ((smooth->
type == CV_BLUR || smooth->
type == CV_BLUR_NO_SCALE || smooth->
type == CV_GAUSSIAN) &&
120 "Invalid value '%d' for param2, it has to be zero or a positive odd number\n",
123 }
124
127 return 0;
128 }
129
131 {
135 }
136
138 void *log_ctx)
139 {
143
144 if ((ret =
av_file_map(filename, &buf, &size, 0, log_ctx)) < 0)
146
147 /* prescan file to get the number of lines and the maximum width */
148 w = 0;
149 for (i = 0; i <
size; i++) {
150 if (buf[i] == '\n') {
151 if (*rows == INT_MAX) {
154 }
155 ++(*rows);
156 *cols =
FFMAX(*cols, w);
157 w = 0;
158 } else if (w == INT_MAX) {
161 }
162 w++;
163 }
164 if (*rows > (SIZE_MAX / sizeof(int) / *cols)) {
166 *rows, *cols);
168 }
171
172 /* fill *values */
174 pend = buf + size-1;
175 for (i = 0; i < *rows; i++) {
176 for (j = 0;; j++) {
177 if (p > pend || *p == '\n') {
178 p++;
179 break;
180 } else
181 (*values)[*cols*i + j] = !!
av_isgraph(*(p++));
182 }
183 }
185
186 #ifdef DEBUG
187 {
191 for (i = 0; i < *rows; i++) {
192 for (j = 0; j < *cols; j++)
193 line[j] = (*values)[i * *cols + j] ? '@' : ' ';
194 line[j] = 0;
196 }
198 }
199 #endif
200
201 return 0;
202 }
203
205 {
206 char shape_filename[128] = "", shape_str[32] = "rect";
207 int cols = 0, rows = 0, anchor_x = 0, anchor_y = 0, shape = CV_SHAPE_RECT;
209
210 sscanf(buf, "%dx%d+%dx%d/%32[^=]=%127s", &cols, &rows, &anchor_x, &anchor_y, shape_str, shape_filename);
211
212 if (!strcmp(shape_str, "rect" )) shape = CV_SHAPE_RECT;
213 else if (!strcmp(shape_str, "cross" )) shape = CV_SHAPE_CROSS;
214 else if (!strcmp(shape_str, "ellipse")) shape = CV_SHAPE_ELLIPSE;
215 else if (!strcmp(shape_str, "custom" )) {
216 shape = CV_SHAPE_CUSTOM;
219 } else {
221 "Shape unspecified or type '%s' unknown.\n", shape_str);
224 }
225
226 if (rows <= 0 || cols <= 0) {
228 "Invalid non-positive values for shape size %dx%d\n", cols, rows);
231 }
232
233 if (anchor_x < 0 || anchor_y < 0 || anchor_x >= cols || anchor_y >= rows) {
235 "Shape anchor %dx%d is not inside the rectangle with size %dx%d.\n",
236 anchor_x, anchor_y, cols, rows);
239 }
240
241 *kernel = cvCreateStructuringElementEx(cols, rows, anchor_x, anchor_y, shape, values);
242 if (!*kernel) {
245 }
246
248 rows, cols, anchor_x, anchor_y, shape_str);
252 }
253
258
260 {
263 char default_kernel_str[] = "3x3+0x0/rect";
264 char *kernel_str =
NULL;
267
268 if (args) {
270
271 if (!kernel_str)
273 }
274
276 (!kernel_str || !*kernel_str) ? default_kernel_str
277 : kernel_str,
278 ctx);
280 if (ret < 0)
282
290 }
291 return 0;
292 }
293
295 {
298
299 cvReleaseStructuringElement(&dilate->
kernel);
300 }
301
303 {
307 }
308
310 {
314 }
315
323
328 };
329
331 {
333 int i;
334
338 }
341 if (!strcmp(s->
name, entry->
name)) {
345
349 }
350 }
351
354 }
355
357 {
359
363 }
364
366 {
371 IplImage inimg, outimg;
372
374 if (!out) {
377 }
379
384
386
388 }
389
390 #define OFFSET(x) offsetof(OCVContext, x)
391 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM
396 };
397
399
401 {
405 },
407 };
408
410 {
413 },
415 };
416
421 .priv_class = &ocv_class,
425 .
inputs = avfilter_vf_ocv_inputs,
426 .
outputs = avfilter_vf_ocv_outputs,
427 };