FFmpeg
Loading...
Searching...
No Matches
vf_morpho.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 ReneBrals
3 * Copyright (c) 2021 Paul B Mahol
4 *
5 * This file is part of FFmpeg.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26#include "libavutil/avassert.h"
27#include "libavutil/imgutils.h"
29#include "libavutil/mem.h"
30#include "libavutil/opt.h"
31#include "libavutil/pixdesc.h"
32#include "avfilter.h"
33#include "filters.h"
34#include "framesync.h"
35#include "video.h"
36
47
48typedef struct IPlane {
49 uint8_t **img;
50 int w, h;
51 int range;
52 int depth;
54
55 void (*max_out_place)(uint8_t *c, const uint8_t *a, const uint8_t *b, int x);
56 void (*min_out_place)(uint8_t *c, const uint8_t *a, const uint8_t *b, int x);
57 void (*diff_rin_place)(uint8_t *a, const uint8_t *b, int x);
58 void (*max_in_place)(uint8_t *a, const uint8_t *b, int x);
59 void (*min_in_place)(uint8_t *a, const uint8_t *b, int x);
60 void (*diff_in_place)(uint8_t *a, const uint8_t *b, int x);
61} IPlane;
62
63typedef struct LUT {
64 /* arr is shifted from base_arr by FFMAX(min_r, 0).
65 * arr != NULL means "lut completely allocated" */
66 uint8_t ***arr;
67 uint8_t ***base_arr;
68 int min_r;
69 int max_r;
70 int I;
71 int X;
74} LUT;
75
76typedef struct chord {
77 int x;
78 int y;
79 int l;
80 int i;
81} chord;
82
83typedef struct chord_set {
85 int size;
86 int cap;
87
88 int *R;
89 int Lnum;
90
91 int minX;
92 int maxX;
93 int minY;
94 int maxY;
95 unsigned nb_elements;
96} chord_set;
97
98#define MAX_THREADS 64
99
100typedef struct MorphoContext {
101 const AVClass *class;
103
106 IPlane g[4], f[4], h[4];
108
109 int mode;
112
117 int depth;
120
122
125
126#define OFFSET(x) offsetof(MorphoContext, x)
127#define FLAGS AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM | AV_OPT_FLAG_RUNTIME_PARAM
128
129static const AVOption morpho_options[] = {
130 { "mode", "set morphological transform", OFFSET(mode), AV_OPT_TYPE_INT, {.i64=0}, 0, NB_MODES-1, FLAGS, .unit = "mode" },
131 { "erode", NULL, 0, AV_OPT_TYPE_CONST, {.i64=ERODE}, 0, 0, FLAGS, .unit = "mode" },
132 { "dilate", NULL, 0, AV_OPT_TYPE_CONST, {.i64=DILATE}, 0, 0, FLAGS, .unit = "mode" },
133 { "open", NULL, 0, AV_OPT_TYPE_CONST, {.i64=OPEN}, 0, 0, FLAGS, .unit = "mode" },
134 { "close", NULL, 0, AV_OPT_TYPE_CONST, {.i64=CLOSE}, 0, 0, FLAGS, .unit = "mode" },
135 { "gradient",NULL, 0, AV_OPT_TYPE_CONST, {.i64=GRADIENT},0, 0, FLAGS, .unit = "mode" },
136 { "tophat",NULL, 0, AV_OPT_TYPE_CONST, {.i64=TOPHAT}, 0, 0, FLAGS, .unit = "mode" },
137 { "blackhat",NULL, 0, AV_OPT_TYPE_CONST, {.i64=BLACKHAT},0, 0, FLAGS, .unit = "mode" },
138 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=7}, 0, 15, FLAGS },
139 { "structure", "when to process structures", OFFSET(structures), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS, .unit = "str" },
140 { "first", "process only first structure, ignore rest", 0, AV_OPT_TYPE_CONST, {.i64=0}, 0, 0, FLAGS, .unit = "str" },
141 { "all", "process all structure", 0, AV_OPT_TYPE_CONST, {.i64=1}, 0, 0, FLAGS, .unit = "str" },
142 { NULL }
143};
144
146
168
169static void min_fun(uint8_t *c, const uint8_t *a, const uint8_t *b, int x)
170{
171 for (int i = 0; i < x; i++)
172 c[i] = FFMIN(b[i], a[i]);
173}
174
175static void mininplace_fun(uint8_t *a, const uint8_t *b, int x)
176{
177 for (int i = 0; i < x; i++)
178 a[i] = FFMIN(a[i], b[i]);
179}
180
181static void max_fun(uint8_t *c, const uint8_t *a, const uint8_t *b, int x)
182{
183 for (int i = 0; i < x; i++)
184 c[i] = FFMAX(a[i], b[i]);
185}
186
187static void maxinplace_fun(uint8_t *a, const uint8_t *b, int x)
188{
189 for (int i = 0; i < x; i++)
190 a[i] = FFMAX(a[i], b[i]);
191}
192
193static void diff_fun(uint8_t *a, const uint8_t *b, int x)
194{
195 for (int i = 0; i < x; i++)
196 a[i] = FFMAX(b[i] - a[i], 0);
197}
198
199static void diffinplace_fun(uint8_t *a, const uint8_t *b, int x)
200{
201 for (int i = 0; i < x; i++)
202 a[i] = FFMAX(a[i] - b[i], 0);
203}
204
205static void min16_fun(uint8_t *cc, const uint8_t *aa, const uint8_t *bb, int x)
206{
207 const uint16_t *a = (const uint16_t *)aa;
208 const uint16_t *b = (const uint16_t *)bb;
209 uint16_t *c = (uint16_t *)cc;
210
211 for (int i = 0; i < x; i++)
212 c[i] = FFMIN(b[i], a[i]);
213}
214
215static void mininplace16_fun(uint8_t *aa, const uint8_t *bb, int x)
216{
217 uint16_t *a = (uint16_t *)aa;
218 const uint16_t *b = (const uint16_t *)bb;
219
220 for (int i = 0; i < x; i++)
221 a[i] = FFMIN(a[i], b[i]);
222}
223
224static void diff16_fun(uint8_t *aa, const uint8_t *bb, int x)
225{
226 const uint16_t *b = (const uint16_t *)bb;
227 uint16_t *a = (uint16_t *)aa;
228
229 for (int i = 0; i < x; i++)
230 a[i] = FFMAX(b[i] - a[i], 0);
231}
232
233static void diffinplace16_fun(uint8_t *aa, const uint8_t *bb, int x)
234{
235 uint16_t *a = (uint16_t *)aa;
236 const uint16_t *b = (const uint16_t *)bb;
237
238 for (int i = 0; i < x; i++)
239 a[i] = FFMAX(a[i] - b[i], 0);
240}
241
242static void max16_fun(uint8_t *cc, const uint8_t *aa, const uint8_t *bb, int x)
243{
244 const uint16_t *a = (const uint16_t *)aa;
245 const uint16_t *b = (const uint16_t *)bb;
246 uint16_t *c = (uint16_t *)cc;
247
248 for (int i = 0; i < x; i++)
249 c[i] = FFMAX(a[i], b[i]);
250}
251
252static void maxinplace16_fun(uint8_t *aa, const uint8_t *bb, int x)
253{
254 uint16_t *a = (uint16_t *)aa;
255 const uint16_t *b = (const uint16_t *)bb;
256
257 for (int i = 0; i < x; i++)
258 a[i] = FFMAX(a[i], b[i]);
259}
260
261static int alloc_lut(LUT *Ty, chord_set *SE, int type_size, int mode)
262{
263 const int min = FFMAX(Ty->min_r, 0);
264 const int max = min + (Ty->max_r - Ty->min_r);
265 int pre_pad_x = 0;
266
267 if (SE->minX < 0)
268 pre_pad_x = 0 - SE->minX;
269 Ty->pre_pad_x = pre_pad_x;
270 Ty->type_size = type_size;
271
272 Ty->base_arr = av_calloc(max + 1, sizeof(*Ty->base_arr));
273 if (!Ty->base_arr)
274 return AVERROR(ENOMEM);
275 for (int r = min; r <= max; r++) {
276 uint8_t **arr = Ty->base_arr[r] = av_calloc(Ty->I, sizeof(uint8_t *));
277 if (!Ty->base_arr[r])
278 return AVERROR(ENOMEM);
279 for (int i = 0; i < Ty->I; i++) {
280 arr[i] = av_calloc(Ty->X + pre_pad_x, type_size);
281 if (!arr[i])
282 return AVERROR(ENOMEM);
283 if (mode == ERODE)
284 memset(arr[i], UINT8_MAX, pre_pad_x * type_size);
285 /* Shifting the X index such that negative indices correspond to
286 * the pre-padding.
287 */
288 arr[i] = &(arr[i][pre_pad_x * type_size]);
289 }
290 }
291
292 Ty->arr = &(Ty->base_arr[min - Ty->min_r]);
293
294 return 0;
295}
296
297static void free_lut(LUT *table)
298{
299 const int min = FFMAX(table->min_r, 0);
300 const int max = min + (table->max_r - table->min_r);
301
302 if (!table->base_arr)
303 return;
304
305 for (int r = min; r <= max; r++) {
306 if (!table->base_arr[r])
307 break;
308 for (int i = 0; i < table->I; i++) {
309 if (!table->base_arr[r][i])
310 break;
311 // The X index was also shifted, for padding purposes.
312 av_free(table->base_arr[r][i] - table->pre_pad_x * table->type_size);
313 }
314 av_freep(&table->base_arr[r]);
315 }
316 av_freep(&table->base_arr);
317 table->arr = NULL;
318}
319
321 int num, enum MorphModes mode)
322{
323 if (!Ty->arr || Ty->I != SE->Lnum ||
324 Ty->X != f->w ||
325 SE->minX < 0 && -SE->minX > Ty->pre_pad_x ||
326 Ty->min_r != SE->minY ||
327 Ty->max_r != SE->maxY + num - 1) {
328 int ret;
329
330 free_lut(Ty);
331
332 Ty->I = SE->Lnum;
333 Ty->X = f->w;
334 Ty->min_r = SE->minY;
335 Ty->max_r = SE->maxY + num - 1;
336 ret = alloc_lut(Ty, SE, f->type_size, mode);
337 if (ret < 0)
338 return ret;
339 }
340 return 0;
341}
342
343static void circular_swap(LUT *Ty)
344{
345 /*
346 * Swap the pointers to r-indices in a circle. This is useful because
347 * Ty(r,i,x) = Ty-1(r+1,i,x) for r < ymax.
348 */
349 if (Ty->max_r - Ty->min_r > 0) {
350 uint8_t **Ty0 = Ty->arr[Ty->min_r];
351
352 for (int r = Ty->min_r; r < Ty->max_r; r++)
353 Ty->arr[r] = Ty->arr[r + 1];
354
355 Ty->arr[Ty->max_r] = Ty0;
356 }
357}
358
359static void compute_min_row(IPlane *f, LUT *Ty, chord_set *SE, int r, int y)
360{
361 if (y + r >= 0 && y + r < f->h) {
362 memcpy(Ty->arr[r][0], f->img[y + r], Ty->X * Ty->type_size);
363 } else {
364 memset(Ty->arr[r][0], UINT8_MAX, Ty->X * Ty->type_size);
365 }
366
367 for (int i = 1; i < SE->Lnum; i++) {
368 int d = SE->R[i] - SE->R[i - 1];
369
370 f->min_out_place(Ty->arr[r][i] - Ty->pre_pad_x * f->type_size,
371 Ty->arr[r][i - 1] - Ty->pre_pad_x * f->type_size,
372 Ty->arr[r][i - 1] + (d - Ty->pre_pad_x) * f->type_size,
373 Ty->X + Ty->pre_pad_x - d);
374 memcpy(Ty->arr[r][i] + (Ty->X - d) * f->type_size,
375 Ty->arr[r][i - 1] + (Ty->X - d) * f->type_size,
376 d * f->type_size);
377 }
378}
379
380static void update_min_lut(IPlane *f, LUT *Ty, chord_set *SE, int y, int tid, int num)
381{
382 for (int i = 0; i < num; i++)
383 circular_swap(Ty);
384
385 compute_min_row(f, Ty, SE, Ty->max_r - tid, y);
386}
387
388static int compute_min_lut(LUT *Ty, IPlane *f, chord_set *SE, int y, int num)
389{
390 int ret = alloc_lut_if_necessary(Ty, f, SE, num, ERODE);
391 if (ret < 0)
392 return ret;
393
394 for (int r = Ty->min_r; r <= Ty->max_r; r++)
395 compute_min_row(f, Ty, SE, r, y);
396
397 return 0;
398}
399
400static void compute_max_row(IPlane *f, LUT *Ty, chord_set *SE, int r, int y)
401{
402 if (y + r >= 0 && y + r < f->h) {
403 memcpy(Ty->arr[r][0], f->img[y + r], Ty->X * Ty->type_size);
404 } else {
405 memset(Ty->arr[r][0], 0, Ty->X * Ty->type_size);
406 }
407
408 for (int i = 1; i < SE->Lnum; i++) {
409 int d = SE->R[i] - SE->R[i - 1];
410
411 f->max_out_place(Ty->arr[r][i] - Ty->pre_pad_x * f->type_size,
412 Ty->arr[r][i - 1] - Ty->pre_pad_x * f->type_size,
413 Ty->arr[r][i - 1] + (d - Ty->pre_pad_x) * f->type_size,
414 Ty->X + Ty->pre_pad_x - d);
415 memcpy(Ty->arr[r][i] + (Ty->X - d) * f->type_size,
416 Ty->arr[r][i - 1] + (Ty->X - d) * f->type_size,
417 d * f->type_size);
418 }
419}
420
421static void update_max_lut(IPlane *f, LUT *Ty, chord_set *SE, int y, int tid, int num)
422{
423 for (int i = 0; i < num; i++)
424 circular_swap(Ty);
425
426 compute_max_row(f, Ty, SE, Ty->max_r - tid, y);
427}
428
429static int compute_max_lut(LUT *Ty, IPlane *f, chord_set *SE, int y, int num)
430{
431 int ret = alloc_lut_if_necessary(Ty, f, SE, num, DILATE);
432 if (ret < 0)
433 return ret;
434
435 for (int r = Ty->min_r; r <= Ty->max_r; r++)
436 compute_max_row(f, Ty, SE, r, y);
437
438 return 0;
439}
440
441static void line_dilate(IPlane *g, LUT *Ty, chord_set *SE, int y, int tid)
442{
443 memset(g->img[y], 0, g->w * g->type_size);
444
445 for (int c = 0; c < SE->size; c++) {
446 g->max_in_place(g->img[y],
447 Ty->arr[SE->C[c].y + tid][SE->C[c].i] + SE->C[c].x * Ty->type_size,
448 av_clip(g->w - SE->C[c].x, 0, g->w));
449 }
450}
451
452static void line_erode(IPlane *g, LUT *Ty, chord_set *SE, int y, int tid)
453{
454 memset(g->img[y], UINT8_MAX, g->w * g->type_size);
455
456 for (int c = 0; c < SE->size; c++) {
457 g->min_in_place(g->img[y],
458 Ty->arr[SE->C[c].y + tid][SE->C[c].i] + SE->C[c].x * Ty->type_size,
459 av_clip(g->w - SE->C[c].x, 0, g->w));
460 }
461}
462
463static int dilate(IPlane *g, IPlane *f, chord_set *SE, LUT *Ty, int y0, int y1)
464{
465 int ret = compute_max_lut(Ty, f, SE, y0, 1);
466 if (ret < 0)
467 return ret;
468
469 line_dilate(g, Ty, SE, y0, 0);
470 for (int y = y0 + 1; y < y1; y++) {
471 update_max_lut(f, Ty, SE, y, 0, 1);
472 line_dilate(g, Ty, SE, y, 0);
473 }
474
475 return 0;
476}
477
478static int erode(IPlane *g, IPlane *f, chord_set *SE, LUT *Ty, int y0, int y1)
479{
480 int ret = compute_min_lut(Ty, f, SE, y0, 1);
481 if (ret < 0)
482 return ret;
483
484 line_erode(g, Ty, SE, y0, 0);
485 for (int y = y0 + 1; y < y1; y++) {
486 update_min_lut(f, Ty, SE, y, 0, 1);
487 line_erode(g, Ty, SE, y, 0);
488 }
489
490 return 0;
491}
492
493static void difference(IPlane *g, IPlane *f, int y0, int y1)
494{
495 for (int y = y0; y < y1; y++)
496 f->diff_in_place(g->img[y], f->img[y], f->w);
497}
498
499static void difference2(IPlane *g, IPlane *f, int y0, int y1)
500{
501 for (int y = y0; y < y1; y++)
502 f->diff_rin_place(g->img[y], f->img[y], f->w);
503}
504
505static int insert_chord_set(chord_set *chords, chord c)
506{
507 // Checking if chord fits in dynamic array, resize if not.
508 if (chords->size == chords->cap) {
509 chords->C = av_realloc_f(chords->C, chords->cap * 2, sizeof(chord));
510 if (!chords->C)
511 return AVERROR(ENOMEM);
512 chords->cap *= 2;
513 }
514
515 // Add the chord to the dynamic array.
516 chords->C[chords->size].x = c.x;
517 chords->C[chords->size].y = c.y;
518 chords->C[chords->size++].l = c.l;
519
520 // Update minimum/maximum x/y offsets of the chord set.
521 chords->minX = FFMIN(chords->minX, c.x);
522 chords->maxX = FFMAX(chords->maxX, c.x);
523
524 chords->minY = FFMIN(chords->minY, c.y);
525 chords->maxY = FFMAX(chords->maxY, c.y);
526
527 return 0;
528}
529
531{
532 av_freep(&SE->C);
533 SE->size = 0;
534 SE->cap = 0;
535
536 av_freep(&SE->R);
537 SE->Lnum = 0;
538}
539
540static int init_chordset(chord_set *chords)
541{
542 chords->nb_elements = 0;
543 chords->size = 0;
544 chords->C = av_calloc(1, sizeof(chord));
545 if (!chords->C)
546 return AVERROR(ENOMEM);
547
548 chords->cap = 1;
549 chords->minX = INT16_MAX;
550 chords->maxX = INT16_MIN;
551 chords->minY = INT16_MAX;
552 chords->maxY = INT16_MIN;
553
554 return 0;
555}
556
557static int comp_chord_length(const void *p, const void *q)
558{
559 chord a, b;
560 a = *((chord *)p);
561 b = *((chord *)q);
562
563 return (a.l > b.l) - (a.l < b.l);
564}
565
566static int comp_chord(const void *p, const void *q)
567{
568 chord a, b;
569 a = *((chord *)p);
570 b = *((chord *)q);
571
572 return (a.y > b.y) - (a.y < b.y);
573}
574
575static int build_chord_set(IPlane *SE, chord_set *chords)
576{
577 const int mid = 1 << (SE->depth - 1);
578 int chord_length_index;
579 int chord_start, val, ret;
580 int centerX, centerY;
581 int r_cap = 1;
582 chord c;
583
584 ret = init_chordset(chords);
585 if (ret < 0)
586 return ret;
587 /*
588 * In erosion/dilation, the center of the IPlane has S.E. offset (0,0).
589 * Otherwise, the resulting IPlane would be shifted to the top-left.
590 */
591 centerX = (SE->w - 1) / 2;
592 centerY = (SE->h - 1) / 2;
593
594 /*
595 * Computing the set of chords C.
596 */
597 for (int y = 0; y < SE->h; y++) {
598 int x;
599
600 chord_start = -1;
601 for (x = 0; x < SE->w; x++) {
602 if (SE->type_size == 1) {
603 chords->nb_elements += (SE->img[y][x] >= mid);
604 //A chord is a run of non-zero pixels.
605 if (SE->img[y][x] >= mid && chord_start == -1) {
606 // Chord starts.
607 chord_start = x;
608 } else if (SE->img[y][x] < mid && chord_start != -1) {
609 // Chord ends before end of line.
610 c.x = chord_start - centerX;
611 c.y = y - centerY;
612 c.l = x - chord_start;
613 ret = insert_chord_set(chords, c);
614 if (ret < 0)
615 return AVERROR(ENOMEM);
616 chord_start = -1;
617 }
618 } else {
619 chords->nb_elements += (AV_RN16(&SE->img[y][x * 2]) >= mid);
620 //A chord is a run of non-zero pixels.
621 if (AV_RN16(&SE->img[y][x * 2]) >= mid && chord_start == -1) {
622 // Chord starts.
623 chord_start = x;
624 } else if (AV_RN16(&SE->img[y][x * 2]) < mid && chord_start != -1) {
625 // Chord ends before end of line.
626 c.x = chord_start - centerX;
627 c.y = y - centerY;
628 c.l = x - chord_start;
629 ret = insert_chord_set(chords, c);
630 if (ret < 0)
631 return AVERROR(ENOMEM);
632 chord_start = -1;
633 }
634 }
635 }
636 if (chord_start != -1) {
637 // Chord ends at end of line.
638 c.x = chord_start - centerX;
639 c.y = y - centerY;
640 c.l = x - chord_start;
641 ret = insert_chord_set(chords, c);
642 if (ret < 0)
643 return AVERROR(ENOMEM);
644 }
645 }
646
647 /*
648 * Computing the array of chord lengths R(i).
649 * This is needed because the lookup table will contain a row for each
650 * length index i.
651 */
652 qsort(chords->C, chords->size, sizeof(chord), comp_chord_length);
653 chords->R = av_calloc(1, sizeof(*chords->R));
654 if (!chords->R)
655 return AVERROR(ENOMEM);
656 chords->Lnum = 0;
657 val = 0;
658 r_cap = 1;
659
660 if (chords->size > 0) {
661 val = 1;
662 if (chords->Lnum >= r_cap) {
663 chords->R = av_realloc_f(chords->R, r_cap * 2, sizeof(*chords->R));
664 if (!chords->R)
665 return AVERROR(ENOMEM);
666 r_cap *= 2;
667 }
668 chords->R[chords->Lnum++] = 1;
669 val = 1;
670 }
671
672 for (int i = 0; i < chords->size; i++) {
673 if (val != chords->C[i].l) {
674 while (2 * val < chords->C[i].l && val != 0) {
675 if (chords->Lnum >= r_cap) {
676 chords->R = av_realloc_f(chords->R, r_cap * 2, sizeof(*chords->R));
677 if (!chords->R)
678 return AVERROR(ENOMEM);
679 r_cap *= 2;
680 }
681
682 chords->R[chords->Lnum++] = 2 * val;
683 val *= 2;
684 }
685 val = chords->C[i].l;
686
687 if (chords->Lnum >= r_cap) {
688 chords->R = av_realloc_f(chords->R, r_cap * 2, sizeof(*chords->R));
689 if (!chords->R)
690 return AVERROR(ENOMEM);
691 r_cap *= 2;
692 }
693 chords->R[chords->Lnum++] = val;
694 }
695 }
696
697 /*
698 * Setting the length indices of chords.
699 * These are needed so that the algorithm can, for each chord,
700 * access the lookup table at the correct length in constant time.
701 */
702 chord_length_index = 0;
703 for (int i = 0; i < chords->size; i++) {
704 while (chords->R[chord_length_index] < chords->C[i].l)
705 chord_length_index++;
706 chords->C[i].i = chord_length_index;
707 }
708
709 /*
710 * Chords are sorted on Y. This way, when a row of the lookup table or IPlane
711 * is cached, the next chord offset has a better chance of being on the
712 * same cache line.
713 */
714 qsort(chords->C, chords->size, sizeof(chord), comp_chord);
715
716 return 0;
717}
718
719static void free_iplane(IPlane *imp)
720{
721 av_freep(&imp->img);
722}
723
724static int read_iplane(IPlane *imp, const uint8_t *dst, int dst_linesize,
725 int w, int h, int R, int type_size, int depth)
726{
727 if (!imp->img)
728 imp->img = av_calloc(h, sizeof(*imp->img));
729 if (!imp->img)
730 return AVERROR(ENOMEM);
731
732 imp->w = w;
733 imp->h = h;
734 imp->range = R;
735 imp->depth = depth;
736 imp->type_size = type_size;
737 imp->max_out_place = type_size == 1 ? max_fun : max16_fun;
738 imp->min_out_place = type_size == 1 ? min_fun : min16_fun;
739 imp->diff_rin_place = type_size == 1 ? diff_fun : diff16_fun;
740 imp->max_in_place = type_size == 1 ? maxinplace_fun : maxinplace16_fun;
741 imp->min_in_place = type_size == 1 ? mininplace_fun : mininplace16_fun;
742 imp->diff_in_place = type_size == 1 ? diffinplace_fun : diffinplace16_fun;
743
744 for (int y = 0; y < h; y++)
745 imp->img[y] = (uint8_t *)dst + y * dst_linesize;
746
747 return 0;
748}
749
750static int config_input(AVFilterLink *inlink)
751{
753 MorphoContext *s = inlink->dst->priv;
754
755 s->depth = desc->comp[0].depth;
756 s->type_size = (s->depth + 7) / 8;
757 s->nb_planes = desc->nb_components;
758 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
759 s->planewidth[0] = s->planewidth[3] = inlink->w;
760 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
761 s->planeheight[0] = s->planeheight[3] = inlink->h;
762
763 return 0;
764}
765
767{
769 AVFilterContext *ctx = inlink->dst;
770 MorphoContext *s = inlink->dst->priv;
771
772 av_assert0(ctx->inputs[0]->format == ctx->inputs[1]->format);
773
774 s->splanewidth[1] = s->splanewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
775 s->splanewidth[0] = s->splanewidth[3] = inlink->w;
776 s->splaneheight[1] = s->splaneheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
777 s->splaneheight[0] = s->splaneheight[3] = inlink->h;
778
779 return 0;
780}
781
783{
784 MorphoContext *s = ctx->priv;
785 return ff_framesync_activate(&s->fs);
786}
787
788typedef struct ThreadData {
789 AVFrame *in, *out;
790} ThreadData;
791
792static int morpho_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
793{
794 MorphoContext *s = ctx->priv;
795 ThreadData *td = arg;
796 AVFrame *out = td->out;
797 AVFrame *in = td->in;
798 int ret;
799
800 for (int p = 0; p < s->nb_planes; p++) {
801 const int width = s->planewidth[p];
802 const int height = s->planeheight[p];
803 const int y0 = ff_slice_pos(height, jobnr, nb_jobs);
804 const int y1 = ff_slice_pos(height, jobnr + 1, nb_jobs);
805 const int depth = s->depth;
806
807 if (ctx->is_disabled || !(s->planes & (1 << p))) {
808copy:
809 av_image_copy_plane(out->data[p] + y0 * out->linesize[p],
810 out->linesize[p],
811 in->data[p] + y0 * in->linesize[p],
812 in->linesize[p],
813 width * ((depth + 7) / 8),
814 y1 - y0);
815 continue;
816 }
817
818 if (s->SE[p].minX == INT16_MAX ||
819 s->SE[p].minY == INT16_MAX ||
820 s->SE[p].maxX == INT16_MIN ||
821 s->SE[p].maxY == INT16_MIN)
822 goto copy;
823
824 switch (s->mode) {
825 case ERODE:
826 ret = erode(&s->g[p], &s->f[p], &s->SE[p], &s->Ty[jobnr][0][p], y0, y1);
827 break;
828 case DILATE:
829 case GRADIENT:
830 ret = dilate(&s->g[p], &s->f[p], &s->SE[p], &s->Ty[jobnr][0][p], y0, y1);
831 break;
832 case OPEN:
833 case TOPHAT:
834 ret = erode(&s->h[p], &s->f[p], &s->SE[p], &s->Ty[jobnr][0][p], y0, y1);
835 break;
836 case CLOSE:
837 case BLACKHAT:
838 ret = dilate(&s->h[p], &s->f[p], &s->SE[p], &s->Ty[jobnr][0][p], y0, y1);
839 break;
840 default:
841 av_assert0(0);
842 }
843
844 if (ret < 0)
845 return ret;
846 }
847
848 return 0;
849}
850
851static int morpho_sliceX(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
852{
853 MorphoContext *s = ctx->priv;
854 int ret;
855
856 for (int p = 0; p < s->nb_planes; p++) {
857 const int height = s->planeheight[p];
858 const int y0 = ff_slice_pos(height, jobnr, nb_jobs);
859 const int y1 = ff_slice_pos(height, jobnr + 1, nb_jobs);
860
861 if (ctx->is_disabled || !(s->planes & (1 << p))) {
862copy:
863 continue;
864 }
865
866 if (s->SE[p].minX == INT16_MAX ||
867 s->SE[p].minY == INT16_MAX ||
868 s->SE[p].maxX == INT16_MIN ||
869 s->SE[p].maxY == INT16_MIN)
870 goto copy;
871
872 switch (s->mode) {
873 case OPEN:
874 ret = dilate(&s->g[p], &s->h[p], &s->SE[p], &s->Ty[jobnr][1][p], y0, y1);
875 break;
876 case CLOSE:
877 ret = erode(&s->g[p], &s->h[p], &s->SE[p], &s->Ty[jobnr][1][p], y0, y1);
878 break;
879 case GRADIENT:
880 ret = erode(&s->h[p], &s->f[p], &s->SE[p], &s->Ty[jobnr][1][p], y0, y1);
881 if (ret < 0)
882 break;
883 difference(&s->g[p], &s->h[p], y0, y1);
884 break;
885 case TOPHAT:
886 ret = dilate(&s->g[p], &s->h[p], &s->SE[p], &s->Ty[jobnr][1][p], y0, y1);
887 if (ret < 0)
888 break;
889 difference2(&s->g[p], &s->f[p], y0, y1);
890 break;
891 case BLACKHAT:
892 ret = erode(&s->g[p], &s->h[p], &s->SE[p], &s->Ty[jobnr][1][p], y0, y1);
893 if (ret < 0)
894 break;
895 difference(&s->g[p], &s->f[p], y0, y1);
896 break;
897 default:
898 av_assert0(0);
899 }
900
901 if (ret < 0)
902 return ret;
903 }
904
905 return 0;
906}
907
909{
910 AVFilterContext *ctx = fs->parent;
911 AVFilterLink *outlink = ctx->outputs[0];
912 MorphoContext *s = ctx->priv;
913 AVFrame *in = NULL, *structurepic = NULL;
914 ThreadData td;
915 AVFrame *out;
916 int ret;
917
918 ret = ff_framesync_dualinput_get(fs, &in, &structurepic);
919 if (ret < 0)
920 return ret;
921 if (!structurepic)
922 return ff_filter_frame(outlink, in);
923
924 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
925 if (!out) {
926 av_frame_free(&in);
927 return AVERROR(ENOMEM);
928 }
930
931 for (int p = 0; p < s->nb_planes; p++) {
932 const uint8_t *ssrc = structurepic->data[p];
933 const int ssrc_linesize = structurepic->linesize[p];
934 const int swidth = s->splanewidth[p];
935 const int sheight = s->splaneheight[p];
936 const uint8_t *src = in->data[p];
937 int src_linesize = in->linesize[p];
938 uint8_t *dst = out->data[p];
939 int dst_linesize = out->linesize[p];
940 const int width = s->planewidth[p];
941 const int height = s->planeheight[p];
942 const int depth = s->depth;
943 int type_size = s->type_size;
944
945 if (!s->got_structure[p] || s->structures) {
946 free_chord_set(&s->SE[p]);
947
948 ret = read_iplane(&s->SEimg[p], ssrc, ssrc_linesize, swidth, sheight, 1, type_size, depth);
949 if (ret < 0)
950 goto fail;
951 ret = build_chord_set(&s->SEimg[p], &s->SE[p]);
952 if (ret < 0)
953 goto fail;
954 s->got_structure[p] = 1;
955 }
956
957 ret = read_iplane(&s->f[p], src, src_linesize, width, height, 1, type_size, depth);
958 if (ret < 0)
959 goto fail;
960
961 ret = read_iplane(&s->g[p], dst, dst_linesize, s->f[p].w, s->f[p].h, s->f[p].range, type_size, depth);
962 if (ret < 0)
963 goto fail;
964
965 switch (s->mode) {
966 case OPEN:
967 case CLOSE:
968 case GRADIENT:
969 case TOPHAT:
970 case BLACKHAT:
971 ret = read_iplane(&s->h[p], s->temp->data[p], s->temp->linesize[p], width, height, 1, type_size, depth);
972 break;
973 }
974
975 if (ret < 0)
976 goto fail;
977 }
978
979 td.in = in; td.out = out;
981 FFMIN3(s->planeheight[1], s->planeheight[2],
983 if (ret == 0 && (s->mode != ERODE && s->mode != DILATE)) {
985 FFMIN3(s->planeheight[1], s->planeheight[2],
987 }
988
989 av_frame_free(&in);
990 out->pts = av_rescale_q(s->fs.pts, s->fs.time_base, outlink->time_base);
991 return ff_filter_frame(outlink, out);
992fail:
994 av_frame_free(&in);
995 return ret;
996}
997
998static int config_output(AVFilterLink *outlink)
999{
1000 AVFilterContext *ctx = outlink->src;
1001 MorphoContext *s = ctx->priv;
1002 AVFilterLink *mainlink = ctx->inputs[0];
1003 FilterLink *il = ff_filter_link(mainlink);
1004 FilterLink *ol = ff_filter_link(outlink);
1005 int ret;
1006
1007 s->fs.on_event = do_morpho;
1009 if (ret < 0)
1010 return ret;
1011 outlink->w = mainlink->w;
1012 outlink->h = mainlink->h;
1013 outlink->time_base = mainlink->time_base;
1014 outlink->sample_aspect_ratio = mainlink->sample_aspect_ratio;
1015 ol->frame_rate = il->frame_rate;
1016
1017 if ((ret = ff_framesync_configure(&s->fs)) < 0)
1018 return ret;
1019 outlink->time_base = s->fs.time_base;
1020
1021 s->temp = ff_get_video_buffer(outlink, outlink->w, outlink->h);
1022 if (!s->temp)
1023 return AVERROR(ENOMEM);
1024
1025 return 0;
1026}
1027
1029{
1030 MorphoContext *s = ctx->priv;
1031
1032 for (int p = 0; p < 4; p++) {
1033 free_iplane(&s->SEimg[p]);
1034 free_iplane(&s->f[p]);
1035 free_iplane(&s->g[p]);
1036 free_iplane(&s->h[p]);
1037 free_chord_set(&s->SE[p]);
1038 for (int n = 0; n < MAX_THREADS; n++) {
1039 free_lut(&s->Ty[n][0][p]);
1040 free_lut(&s->Ty[n][1][p]);
1041 }
1042 }
1043
1044 ff_framesync_uninit(&s->fs);
1045
1046 av_frame_free(&s->temp);
1047}
1048
1049static const AVFilterPad morpho_inputs[] = {
1050 {
1051 .name = "default",
1052 .type = AVMEDIA_TYPE_VIDEO,
1053 .config_props = config_input,
1054 },
1055 {
1056 .name = "structure",
1057 .type = AVMEDIA_TYPE_VIDEO,
1058 .config_props = config_input_structure,
1059 },
1060};
1061
1062static const AVFilterPad morpho_outputs[] = {
1063 {
1064 .name = "default",
1065 .type = AVMEDIA_TYPE_VIDEO,
1066 .config_props = config_output,
1067 },
1068};
1069
1071 .p.name = "morpho",
1072 .p.description = NULL_IF_CONFIG_SMALL("Apply Morphological filter."),
1073 .p.priv_class = &morpho_class,
1076 .preinit = morpho_framesync_preinit,
1077 .priv_size = sizeof(MorphoContext),
1078 .activate = activate,
1079 .uninit = uninit,
1083 .process_command = ff_filter_process_command,
1084};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:97
static double val(void *priv, double ch)
Definition aeval.c:77
static int config_input(AVFilterLink *inlink)
@ NB_MODES
Definition af_afftdn.c:48
const FFFilter ff_vf_morpho
Definition vf_morpho.c:1070
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1079
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:917
int ff_filter_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition avfilter.c:1721
int ff_filter_get_nb_threads(AVFilterContext *ctx)
Get number of threads for current filter instance.
Definition avfilter.c:857
Main libavfilter public API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
#define s(width, name)
Definition cbs_vp9.c:198
#define fs(width, name, subs,...)
Definition cbs_vp9.c:200
#define FLAGS
Definition cmdutils.c:596
#define AV_CEIL_RSHIFT(a, b)
Definition common.h:60
#define av_clip
Definition common.h:100
#define NULL
Definition coverity.c:32
#define min(a, b)
#define max(a, b)
#define SE
Definition dvbsubenc.c:569
int ret
Definition ffplay.c:391
#define MAX_THREADS
int ff_framesync_configure(FFFrameSync *fs)
Configure a frame sync structure.
Definition framesync.c:137
int ff_framesync_dualinput_get(FFFrameSync *fs, AVFrame **f0, AVFrame **f1)
Definition framesync.c:390
int ff_framesync_activate(FFFrameSync *fs)
Examine the frames in the filter's input and try to produce output.
Definition framesync.c:352
int ff_framesync_init_dualinput(FFFrameSync *fs, AVFilterContext *parent)
Initialize a frame sync structure for dualinput.
Definition framesync.c:372
void ff_framesync_uninit(FFFrameSync *fs)
Free all memory currently allocated.
Definition framesync.c:301
#define FRAMESYNC_DEFINE_CLASS(name, context, field)
Definition framesync.h:352
#define fail
Definition test.h:479
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
#define AVFILTER_FLAG_SLICE_THREADS
The filter supports multithreading by splitting frames into multiple parts and processing them concur...
Definition avfilter.h:172
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:210
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy_plane(uint8_t *dst, int dst_linesize, const uint8_t *src, int src_linesize, int bytewidth, int height)
Copy image plane from src to dst.
Definition imgutils.c:374
int a
#define R
Definition huffyuv.h:44
misc image utilities
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RN16(p)
static av_cold void uninit(AVBitStreamFilterContext *ctx)
static int activate(AVBitStreamFilterContext *ctx)
static int config_output(AVBitStreamFilterLink *outlink)
#define C
const char * arg
Definition jacosubdec.c:66
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
static int ff_slice_pos(int total, int jobnr, int nb_jobs)
Compute the boundary index for a slice when work of size total is split into nb_jobs slices.
Definition filters.h:789
#define FILTER_PIXFMTS_ARRAY(array)
Definition filters.h:259
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define av_cold
Definition attributes.h:117
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
static enum AVPixelFormat pix_fmts[]
Definition libkvazaar.c:296
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
uint8_t w
Definition llvidencdsp.c:39
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
#define FFMIN3(a, b, c)
Definition macros.h:50
@ GRADIENT
Definition magicyuv.c:47
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:370
Memory handling functions.
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3484
#define AV_PIX_FMT_GBRAP12
Definition pixfmt.h:572
#define AV_PIX_FMT_YUV420P16
Definition pixfmt.h:559
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:555
#define AV_PIX_FMT_YUV444P9
Definition pixfmt.h:547
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:548
#define AV_PIX_FMT_YUV440P12
Definition pixfmt.h:554
#define AV_PIX_FMT_GRAY9
Definition pixfmt.h:527
#define AV_PIX_FMT_GBRAP16
Definition pixfmt.h:574
#define AV_PIX_FMT_GBRP9
Definition pixfmt.h:566
#define AV_PIX_FMT_YUV422P9
Definition pixfmt.h:546
#define AV_PIX_FMT_YUVA444P10
Definition pixfmt.h:601
#define AV_PIX_FMT_YUVA420P16
Definition pixfmt.h:604
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUVA420P10
Definition pixfmt.h:599
#define AV_PIX_FMT_YUVA422P9
Definition pixfmt.h:597
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:553
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:567
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:549
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:529
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:568
#define AV_PIX_FMT_YUV420P9
Definition pixfmt.h:545
#define AV_PIX_FMT_YUVA420P9
Definition pixfmt.h:596
#define AV_PIX_FMT_YUVA422P10
Definition pixfmt.h:600
#define AV_PIX_FMT_YUV420P14
Definition pixfmt.h:556
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV440P
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
Definition pixfmt.h:106
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUVA420P
planar YUV 4:2:0, 20bpp, (1 Cr & Cb sample per 2x2 Y & A samples)
Definition pixfmt.h:108
@ AV_PIX_FMT_YUVJ440P
planar YUV 4:4:0 full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV440P and setting color_range
Definition pixfmt.h:107
@ AV_PIX_FMT_YUV410P
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Definition pixfmt.h:79
@ AV_PIX_FMT_YUV411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Definition pixfmt.h:80
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_YUVA444P
planar YUV 4:4:4 32bpp, (1 Cr & Cb sample per 1x1 Y & A samples)
Definition pixfmt.h:174
@ AV_PIX_FMT_YUVJ411P
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples) full scale (JPEG), deprecated in favor ...
Definition pixfmt.h:283
@ AV_PIX_FMT_GBRAP
planar GBRA 4:4:4:4 32bpp
Definition pixfmt.h:212
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition pixfmt.h:86
@ AV_PIX_FMT_YUVA422P
planar YUV 4:2:2 24bpp, (1 Cr & Cb sample per 2x1 Y & A samples)
Definition pixfmt.h:173
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition pixfmt.h:87
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition pixfmt.h:85
#define AV_PIX_FMT_YUVA422P12
Definition pixfmt.h:602
#define AV_PIX_FMT_YUV422P14
Definition pixfmt.h:557
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:528
#define AV_PIX_FMT_GRAY14
Definition pixfmt.h:530
#define AV_PIX_FMT_YUV422P16
Definition pixfmt.h:560
#define AV_PIX_FMT_GRAY16
Definition pixfmt.h:531
#define AV_PIX_FMT_GBRAP10
Definition pixfmt.h:571
#define AV_PIX_FMT_YUVA444P16
Definition pixfmt.h:606
#define AV_PIX_FMT_YUVA422P16
Definition pixfmt.h:605
#define AV_PIX_FMT_GBRP16
Definition pixfmt.h:570
#define AV_PIX_FMT_YUV444P14
Definition pixfmt.h:558
#define AV_PIX_FMT_YUVA444P9
Definition pixfmt.h:598
#define AV_PIX_FMT_GBRP14
Definition pixfmt.h:569
#define AV_PIX_FMT_YUVA444P12
Definition pixfmt.h:603
#define AV_PIX_FMT_YUV444P16
Definition pixfmt.h:561
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:551
static const uint16_t table[]
Definition prosumer.c:203
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:279
void * priv
private data for use by the filter
Definition avfilter.h:294
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:500
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:524
AVOption.
Definition opt.h:428
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
Frame sync structure.
Definition framesync.h:168
int type_size
Definition vf_morpho.c:53
void(* max_in_place)(uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:58
void(* max_out_place)(uint8_t *c, const uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:55
int depth
Definition vf_morpho.c:52
void(* min_in_place)(uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:59
int h
Definition vf_morpho.c:50
void(* diff_in_place)(uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:60
void(* diff_rin_place)(uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:57
int range
Definition vf_morpho.c:51
void(* min_out_place)(uint8_t *c, const uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:56
int w
Definition vf_morpho.c:50
uint8_t ** img
Definition vf_morpho.c:49
uint8_t *** arr
Definition vf_morpho.c:66
int I
Definition vf_morpho.c:70
int max_r
Definition vf_morpho.c:69
int type_size
Definition vf_morpho.c:73
int pre_pad_x
Definition vf_morpho.c:72
int X
Definition vf_morpho.c:71
uint8_t *** base_arr
Definition vf_morpho.c:67
int min_r
Definition vf_morpho.c:68
IPlane g[4]
Definition vf_morpho.c:106
LUT Ty[MAX_THREADS][2][4]
Definition vf_morpho.c:107
IPlane f[4]
Definition vf_morpho.c:106
int splanewidth[4]
Definition vf_morpho.c:115
IPlane SEimg[4]
Definition vf_morpho.c:105
int got_structure[4]
Definition vf_morpho.c:121
int splaneheight[4]
Definition vf_morpho.c:116
int planewidth[4]
Definition vf_morpho.c:113
FFFrameSync fs
Definition vf_morpho.c:102
IPlane h[4]
Definition vf_morpho.c:106
chord_set SE[4]
Definition vf_morpho.c:104
AVFrame * temp
Definition vf_morpho.c:123
int planeheight[4]
Definition vf_morpho.c:114
Used for passing data between threads.
Definition v210dec.c:35
AVFrame * out
int * R
Definition vf_morpho.c:88
unsigned nb_elements
Definition vf_morpho.c:95
chord * C
Definition vf_morpho.c:84
int x
Definition vf_morpho.c:77
int l
Definition vf_morpho.c:79
int y
Definition vf_morpho.c:78
int i
Definition vf_morpho.c:80
Definition swscale.c:85
#define av_free(p)
#define av_realloc_f(p, o, n)
#define av_freep(p)
#define src
Definition vp8dsp.c:248
int num
Definition error.c:23
#define height
Definition dsp.h:99
#define width
Definition dsp.h:99
const char * g
Definition vf_curves.c:128
static void update_min_lut(IPlane *f, LUT *Ty, chord_set *SE, int y, int tid, int num)
Definition vf_morpho.c:380
static int insert_chord_set(chord_set *chords, chord c)
Definition vf_morpho.c:505
static void min16_fun(uint8_t *cc, const uint8_t *aa, const uint8_t *bb, int x)
Definition vf_morpho.c:205
static int compute_max_lut(LUT *Ty, IPlane *f, chord_set *SE, int y, int num)
Definition vf_morpho.c:429
static void compute_max_row(IPlane *f, LUT *Ty, chord_set *SE, int r, int y)
Definition vf_morpho.c:400
static int alloc_lut_if_necessary(LUT *Ty, IPlane *f, chord_set *SE, int num, enum MorphModes mode)
Definition vf_morpho.c:320
static const AVFilterPad morpho_inputs[]
Definition vf_morpho.c:1049
MorphModes
Definition vf_morpho.c:37
@ OPEN
Definition vf_morpho.c:40
@ BLACKHAT
Definition vf_morpho.c:44
@ ERODE
Definition vf_morpho.c:38
@ CLOSE
Definition vf_morpho.c:41
@ DILATE
Definition vf_morpho.c:39
@ TOPHAT
Definition vf_morpho.c:43
static int erode(IPlane *g, IPlane *f, chord_set *SE, LUT *Ty, int y0, int y1)
Definition vf_morpho.c:478
static void free_chord_set(chord_set *SE)
Definition vf_morpho.c:530
static void line_dilate(IPlane *g, LUT *Ty, chord_set *SE, int y, int tid)
Definition vf_morpho.c:441
static int comp_chord(const void *p, const void *q)
Definition vf_morpho.c:566
static void update_max_lut(IPlane *f, LUT *Ty, chord_set *SE, int y, int tid, int num)
Definition vf_morpho.c:421
static void maxinplace_fun(uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:187
static int read_iplane(IPlane *imp, const uint8_t *dst, int dst_linesize, int w, int h, int R, int type_size, int depth)
Definition vf_morpho.c:724
static int init_chordset(chord_set *chords)
Definition vf_morpho.c:540
static void diff16_fun(uint8_t *aa, const uint8_t *bb, int x)
Definition vf_morpho.c:224
static int config_input(AVFilterLink *inlink)
Definition vf_morpho.c:750
static void difference(IPlane *g, IPlane *f, int y0, int y1)
Definition vf_morpho.c:493
static int morpho_sliceX(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_morpho.c:851
static void free_iplane(IPlane *imp)
Definition vf_morpho.c:719
static void diffinplace_fun(uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:199
static int do_morpho(FFFrameSync *fs)
Definition vf_morpho.c:908
static void diffinplace16_fun(uint8_t *aa, const uint8_t *bb, int x)
Definition vf_morpho.c:233
static int compute_min_lut(LUT *Ty, IPlane *f, chord_set *SE, int y, int num)
Definition vf_morpho.c:388
static void mininplace_fun(uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:175
#define MAX_THREADS
Definition vf_morpho.c:98
static int build_chord_set(IPlane *SE, chord_set *chords)
Definition vf_morpho.c:575
static void line_erode(IPlane *g, LUT *Ty, chord_set *SE, int y, int tid)
Definition vf_morpho.c:452
static void mininplace16_fun(uint8_t *aa, const uint8_t *bb, int x)
Definition vf_morpho.c:215
static const AVOption morpho_options[]
Definition vf_morpho.c:129
static const AVFilterPad morpho_outputs[]
Definition vf_morpho.c:1062
static int alloc_lut(LUT *Ty, chord_set *SE, int type_size, int mode)
Definition vf_morpho.c:261
static int dilate(IPlane *g, IPlane *f, chord_set *SE, LUT *Ty, int y0, int y1)
Definition vf_morpho.c:463
static int activate(AVFilterContext *ctx)
Definition vf_morpho.c:782
static av_cold void uninit(AVFilterContext *ctx)
Definition vf_morpho.c:1028
static void difference2(IPlane *g, IPlane *f, int y0, int y1)
Definition vf_morpho.c:499
#define OFFSET(x)
Definition vf_morpho.c:126
static void max_fun(uint8_t *c, const uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:181
static int config_output(AVFilterLink *outlink)
Definition vf_morpho.c:998
static int comp_chord_length(const void *p, const void *q)
Definition vf_morpho.c:557
static void diff_fun(uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:193
static void compute_min_row(IPlane *f, LUT *Ty, chord_set *SE, int r, int y)
Definition vf_morpho.c:359
static void maxinplace16_fun(uint8_t *aa, const uint8_t *bb, int x)
Definition vf_morpho.c:252
static int morpho_slice(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
Definition vf_morpho.c:792
static int config_input_structure(AVFilterLink *inlink)
Definition vf_morpho.c:766
static void circular_swap(LUT *Ty)
Definition vf_morpho.c:343
static void min_fun(uint8_t *c, const uint8_t *a, const uint8_t *b, int x)
Definition vf_morpho.c:169
static void max16_fun(uint8_t *cc, const uint8_t *aa, const uint8_t *bb, int x)
Definition vf_morpho.c:242
static void free_lut(LUT *table)
Definition vf_morpho.c:297
static void copy(const float *p1, float *p2, const int length)
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:120
static double c[64]