forked from guardianproject/android-ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redact-plugins.patch
1102 lines (1102 loc) · 36.7 KB
/
redact-plugins.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
cd ~/code/eighthave/ffmpeg
git diff n0.11.1..redact-0.11.1 > ~/code/guardianproject/android-ffmpeg/redact-plugins.patch
diff --git a/ffmpeg/libavfilter/Makefile b/ffmpeg/libavfilter/Makefile
index 29345fc..9812a94 100644
--- a/ffmpeg/libavfilter/Makefile
+++ b/ffmpeg/libavfilter/Makefile
@@ -49,6 +49,7 @@ OBJS-$(CONFIG_AFORMAT_FILTER) += af_aformat.o
OBJS-$(CONFIG_AMERGE_FILTER) += af_amerge.o
OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o
OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o
+OBJS-$(CONFIG_AREDACT_FILTER) += af_aredact.o
OBJS-$(CONFIG_ARESAMPLE_FILTER) += af_aresample.o
OBJS-$(CONFIG_ASHOWINFO_FILTER) += af_ashowinfo.o
OBJS-$(CONFIG_ASPLIT_FILTER) += split.o
@@ -101,6 +102,7 @@ OBJS-$(CONFIG_OCV_FILTER) += vf_libopencv.o
OBJS-$(CONFIG_OVERLAY_FILTER) += vf_overlay.o
OBJS-$(CONFIG_PAD_FILTER) += vf_pad.o
OBJS-$(CONFIG_PIXDESCTEST_FILTER) += vf_pixdesctest.o
+OBJS-$(CONFIG_REDACT_FILTER) += vf_redact.o
OBJS-$(CONFIG_REMOVELOGO_FILTER) += bbox.o lswsutils.o lavfutils.o vf_removelogo.o
OBJS-$(CONFIG_SELECT_FILTER) += vf_select.o
OBJS-$(CONFIG_SETDAR_FILTER) += vf_aspect.o
diff --git a/ffmpeg/libavfilter/af_aredact.c b/ffmpeg/libavfilter/af_aredact.c
new file mode 100644
index 0000000..ae77b0f
--- /dev/null
+++ b/ffmpeg/libavfilter/af_aredact.c
@@ -0,0 +1,408 @@
+/*
+ * Copyright (c) 2011 Hans-Christoph Steiner
+ * Copyright (c) 2011 Stefano Sabatini
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+/**
+ * @file
+ * audio redaction filter
+ * based on af_volume.c and vf_redact.c code
+ */
+
+#include "libavutil/avstring.h"
+#include "libavutil/audioconvert.h"
+#include "libavutil/eval.h"
+#include "audio.h"
+#include "avfilter.h"
+#include "formats.h"
+
+/* for lazy char* allocation */
+#define BUFLEN 1000
+
+typedef enum {redact_none,
+ redact_mute,
+ redact_noise,
+ redact_resample,
+ redact_buzzer
+} redaction_method;
+
+typedef struct {
+ double start, end;
+ redaction_method method;
+} BoxTrack;
+
+typedef struct {
+ int numtracks;
+ double time_seconds;
+ BoxTrack **boxtracks;
+} RedactionContext;
+
+static BoxTrack *box_track_from_string(const char *track_def,
+ AVFilterContext *ctx) {
+ BoxTrack *boxtrack = NULL;
+ int rv = 0;
+ char methodbuf[BUFLEN];
+ double start, end;
+
+ // Allow comments, empty lines.
+ if (track_def[0] == '#' || track_def[0] == '\0')
+ return NULL;
+
+ rv = sscanf(track_def, "%lf,%lf,%s", &start,&end,methodbuf);
+ if (rv != 3) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to parse boxtrack '%s' .\n", track_def);
+ return NULL;
+ }
+ boxtrack = (BoxTrack *)av_malloc(sizeof(BoxTrack));
+ boxtrack->start = start;
+ boxtrack->end = end;
+
+ if (av_strncasecmp(methodbuf, "mute", 4) == 0)
+ boxtrack->method = redact_mute;
+ else if (av_strncasecmp(methodbuf, "noise", 5) == 0)
+ boxtrack->method = redact_noise;
+ else if (av_strncasecmp(methodbuf, "resample", 8) == 0)
+ boxtrack->method = redact_resample;
+ else if (av_strncasecmp(methodbuf, "buzzer", 6) == 0)
+ boxtrack->method = redact_buzzer;
+ else if (av_strncasecmp(methodbuf, "none", 4) == 0)
+ boxtrack->method = redact_none;
+ else {
+ boxtrack->method = redact_mute;
+ av_log(ctx, AV_LOG_ERROR, "Unknown audio redaction method '%s', using 'mute'.\n",
+ methodbuf);
+ }
+
+ return boxtrack;
+}
+
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ RedactionContext *redaction = ctx->priv;
+ FILE *file = NULL;
+ char buf[BUFLEN];
+
+ redaction->boxtracks = NULL;
+ redaction->time_seconds = 0.0;
+ if (!args) {
+ av_log(ctx, AV_LOG_ERROR, "No arguments given to redact.\n");
+ return 1;
+ }
+ file = fopen(args, "r");
+ if (!file) {
+ av_log(ctx, AV_LOG_ERROR, "Can't open redaction file: '%s'\n", args);
+ return 2;
+ }
+ redaction->numtracks = 0;
+ // Parse the config file.
+ while (!feof(file)) {
+ BoxTrack **boxtracks = NULL;
+ BoxTrack *new_track = NULL;
+
+ if (fgets(buf, BUFLEN, file) == NULL) break; // EOF
+ new_track = box_track_from_string(buf, ctx);
+ if (new_track == NULL)
+ continue;
+ // Resize the array and add the new track.
+ boxtracks = (BoxTrack **)av_malloc((redaction->numtracks + 1) *
+ sizeof(BoxTrack *));
+ for (int i = 0; i < redaction->numtracks; ++i)
+ boxtracks[i] = redaction->boxtracks[i];
+ boxtracks[redaction->numtracks++] = new_track;
+ av_free(redaction->boxtracks);
+ redaction->boxtracks = boxtracks;
+ }
+ fclose(file);
+ // Sort the tracks so the earliest-starting are at the end of the array.
+ for (int j = 0; j < redaction->numtracks -1; ++j)
+ for (int k = j + 1; k < redaction->numtracks; ++k)
+ if (redaction->boxtracks[j]->start < redaction->boxtracks[k]->start) {
+ BoxTrack *temp = redaction->boxtracks[j];
+ redaction->boxtracks[j] = redaction->boxtracks[k];
+ redaction->boxtracks[k] = temp;
+ }
+ return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ AVFilterFormats *formats = NULL;
+ AVFilterChannelLayouts *layouts;
+ enum AVSampleFormat sample_fmts[] = {
+ AV_SAMPLE_FMT_U8,
+ AV_SAMPLE_FMT_S16,
+ AV_SAMPLE_FMT_S32,
+ AV_SAMPLE_FMT_FLT,
+ AV_SAMPLE_FMT_DBL,
+ AV_SAMPLE_FMT_NONE
+ };
+
+ layouts = ff_all_channel_layouts();
+ if (!layouts)
+ return AVERROR(ENOMEM);
+ ff_set_common_channel_layouts(ctx, layouts);
+
+ formats = avfilter_make_format_list(sample_fmts);
+ if (!formats)
+ return AVERROR(ENOMEM);
+ avfilter_set_common_sample_formats(ctx, formats);
+
+ formats = ff_all_samplerates();
+ if (!formats)
+ return AVERROR(ENOMEM);
+ ff_set_common_samplerates(ctx, formats);
+
+ return 0;
+}
+
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples)
+{
+ RedactionContext *redaction = inlink->dst->priv;
+ AVFilterLink *outlink = inlink->dst->outputs[0];
+ const int nb_samples = insamples->audio->nb_samples *
+ av_get_channel_layout_nb_channels(insamples->audio->channel_layout);
+ int i, box;
+ BoxTrack *boxtrack;
+ redaction_method method = redact_none;
+
+ redaction->time_seconds = redaction->time_seconds +
+ (((double) nb_samples) / inlink->sample_rate);
+
+ for (box = redaction->numtracks -1; box >= 0; --box) {
+ boxtrack = redaction->boxtracks[box];
+ // Tracks are sorted by start time, so if this one starts in the future
+ // all remaining ones will.
+ if (boxtrack->start > redaction->time_seconds)
+ goto finished;
+ if (boxtrack->end < redaction->time_seconds) {
+ // Delete any tracks we've passed.
+ av_free(redaction->boxtracks[box]);
+ // Shuffle down any still-active tracks higher in the array.
+ // (We've already processed them this frame.)
+ for (int t = box + 1; t < redaction->numtracks; ++t)
+ redaction->boxtracks[t - 1] = redaction->boxtracks[t];
+ // Reduce the count.
+ redaction->boxtracks[--redaction->numtracks] = NULL;
+ } else {
+ method = boxtrack->method;
+ break; // use the first redaction time found for this timeslice
+ }
+ }
+
+ if (method != redact_none) {
+ switch (insamples->format) {
+ case AV_SAMPLE_FMT_U8:
+ {
+ uint8_t *p = (void *)insamples->data[0];
+ switch (method) {
+ case redact_mute:
+ {
+ for (i = 0; i < nb_samples; i++)
+ *p++ = 0;
+ break;
+ }
+ case redact_buzzer:
+ {
+ uint8_t state = 0;
+ for (i = 0; i < nb_samples; i++) {
+ *p++ = state / 2;
+ state = state*1664525+1013904223;
+ }
+ break;
+ }
+ case redact_resample:
+ {
+ uint8_t sample = *p;
+ int modval = inlink->sample_rate / 1500;
+ for (i = 0; i < nb_samples; i++) {
+ if (i % modval == 0)
+ sample = *p;
+ *p++ = sample;
+ }
+ break;
+ }
+ }
+ break;
+ }
+ case AV_SAMPLE_FMT_S16:
+ {
+ int16_t *p = (void *)insamples->data[0];
+ switch (method) {
+ case redact_mute:
+ {
+ for (i = 0; i < nb_samples; i++)
+ *p++ = 0;
+ break;
+ }
+ case redact_buzzer:
+ {
+ int16_t state = 0;
+ for (i = 0; i < nb_samples; i++) {
+ *p++ = state / 2;
+ state = state*1664525+1013904223;
+ }
+ break;
+ }
+ case redact_resample:
+ {
+ int16_t sample = *p;
+ int modval = inlink->sample_rate / 1500;
+ for (i = 0; i < nb_samples; i++) {
+ if (i % modval == 0)
+ sample = *p;
+ *p++ = sample;
+ }
+ break;
+ }
+ }
+ break;
+ }
+ case AV_SAMPLE_FMT_S32:
+ {
+ int32_t *p = (void *)insamples->data[0];
+ switch (method) {
+ case redact_mute:
+ {
+ for (i = 0; i < nb_samples; i++)
+ *p++ = 0;
+ break;
+ }
+ case redact_buzzer:
+ {
+ int32_t state = 0;
+ for (i = 0; i < nb_samples; i++) {
+ *p++ = state / 2;
+ state = state*1664525+1013904223;
+ }
+ break;
+ }
+ case redact_resample:
+ {
+ int32_t sample = *p;
+ int modval = inlink->sample_rate / 1500;
+ for (i = 0; i < nb_samples; i++) {
+ if (i % modval == 0)
+ sample = *p;
+ *p++ = sample;
+ }
+ break;
+ }
+ }
+ break;
+ }
+ case AV_SAMPLE_FMT_FLT:
+ {
+ float *p = (void *)insamples->data[0];
+ switch (method) {
+ case redact_mute:
+ {
+ for (i = 0; i < nb_samples; i++)
+ *p++ = 0.0;
+ break;
+ }
+ case redact_buzzer:
+ {
+ int32_t state = 0;
+ for (i = 0; i < nb_samples; i++) {
+ *p++ = (float)state / 4294967296;
+ state = state*1664525+1013904223;
+ }
+ break;
+ }
+ case redact_resample:
+ {
+ float sample = *p;
+ int modval = inlink->sample_rate / 1500;
+ for (i = 0; i < nb_samples; i++) {
+ if (i % modval == 0)
+ sample = *p;
+ *p++ = sample;
+ }
+ break;
+ }
+ }
+ break;
+ }
+ case AV_SAMPLE_FMT_DBL:
+ {
+ double *p = (void *)insamples->data[0];
+ switch (method) {
+ case redact_mute:
+ {
+ for (i = 0; i < nb_samples; i++)
+ *p++ = 0.0;
+ break;
+ }
+ case redact_buzzer:
+ {
+ int32_t state = 0;
+ for (i = 0; i < nb_samples; i++) {
+ *p++ = (double)state / 4294967296;
+ state = state*1664525+1013904223;
+ }
+ break;
+ }
+ case redact_resample:
+ {
+ double sample = *p;
+ int modval = inlink->sample_rate / 1500;
+ for (i = 0; i < nb_samples; i++) {
+ if (i % modval == 0)
+ sample = *p;
+ *p++ = sample;
+ }
+ break;
+ }
+ }
+ break;
+ }
+ }
+ }
+finished:
+ ff_filter_samples(outlink, insamples);
+}
+
+ static av_cold void uninit(AVFilterContext *ctx)
+ {
+ RedactionContext *redaction= ctx->priv;
+ for (int i = 0; i < redaction->numtracks; ++i) {
+ av_free(redaction->boxtracks[i]);
+ }
+ av_free(redaction->boxtracks);
+ }
+
+AVFilter avfilter_af_aredact = {
+ .name = "aredact",
+ .description = NULL_IF_CONFIG_SMALL("Redact the input audio according to a track file."),
+ .query_formats = query_formats,
+ .priv_size = sizeof(RedactionContext),
+ .init = init,
+ .uninit = uninit,
+
+ .inputs = (const AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO,
+ .filter_samples = filter_samples,
+ .min_perms = AV_PERM_READ|AV_PERM_WRITE},
+ { .name = NULL}},
+
+ .outputs = (const AVFilterPad[]) {{ .name = "default",
+ .type = AVMEDIA_TYPE_AUDIO, },
+ { .name = NULL}},
+};
diff --git a/ffmpeg/libavfilter/allfilters.c b/ffmpeg/libavfilter/allfilters.c
index b9d44f2..27c2346 100644
--- a/ffmpeg/libavfilter/allfilters.c
+++ b/ffmpeg/libavfilter/allfilters.c
@@ -39,6 +39,7 @@ void avfilter_register_all(void)
REGISTER_FILTER (AMERGE, amerge, af);
REGISTER_FILTER (AMIX, amix, af);
REGISTER_FILTER (ANULL, anull, af);
+ REGISTER_FILTER (AREDACT, aredact, af);
REGISTER_FILTER (ARESAMPLE, aresample, af);
REGISTER_FILTER (ASHOWINFO, ashowinfo, af);
REGISTER_FILTER (ASPLIT, asplit, af);
@@ -91,6 +92,7 @@ void avfilter_register_all(void)
REGISTER_FILTER (OVERLAY, overlay, vf);
REGISTER_FILTER (PAD, pad, vf);
REGISTER_FILTER (PIXDESCTEST, pixdesctest, vf);
+ REGISTER_FILTER (REDACT, redact, vf);
REGISTER_FILTER (REMOVELOGO, removelogo, vf);
REGISTER_FILTER (SELECT, select, vf);
REGISTER_FILTER (SETDAR, setdar, vf);
diff --git a/ffmpeg/libavfilter/vf_redact.c b/ffmpeg/libavfilter/vf_redact.c
new file mode 100644
index 0000000..e1e2ff5
--- /dev/null
+++ b/ffmpeg/libavfilter/vf_redact.c
@@ -0,0 +1,640 @@
+/*
+ * Copyright (c) 2011 Andrew Senior
+ *
+ * This file is for use with ffmpeg
+ * Version 0.04 - reverting to single output.
+ * No memory leak on linux. Checked with ffmpeg 0.10.4
+ */
+
+/**
+ * @file
+ * Redaction filter. Read a file describing redactions. Wipe boxes in frames
+ * accordingly.
+ */
+
+/* The redactions filename is passed as the sole parameter to the filter.
+ * The file consists of lines describing "boxtracks" which are defined
+ * by 7 comma-separated values: "%lf,%lf,%d,%d,%d,%d,%s"
+ * e.g. "0.5,1.5,50,100,0,1000,green"
+ * First: start and end times (floating point, in seconds, referenced to the
+ * presentation time stamp given by ffmpeg) and left,right, top, bottom
+ * spatial coordinates of the redaction region. (origin is top left,
+ * coordinates increase down and to right.)
+ * finally a redaction method string is given which is either "pixel" for
+ * pixellation, "inverse" for inverse pixellation (not yet implemented)
+ * "blur" for face blurring.
+ * or an ffmpeg color specifier for solid redaction.
+ * The file can contain comments, ie lines beginning with "#".
+ * Based on vf_transpose.
+ */
+
+/* Use:
+ * put this file in the libavfilter directory
+ * add the line
+ REGISTER_FILTER (REDACT, redact, vf);
+ * to avfilter_register_all in libavfilter/allfilters.c,
+ * add
+ OBJS-$(CONFIG_REDACT_FILTER) += vf_redact.o
+ * to libavfilter/Makefile, and
+ CONFIG_REDACT_FILTER=yes
+ * to config.mak
+ */
+
+
+/* todo:
+ * add noise to pixellation to make super-resolution attacks harder.
+ * a more stable-across-time pixellation appearance
+ * inverse pixellation
+ * allow the megapixel size to be specified.
+ * alternative ways to import the specification
+ * (e.g. another input port, or directly in the config string)
+ * doing face detection & tracking on the fly in the filter
+ * preserving redacted information in a separate stream
+ * allow motion specification for tracks (velocity, spline...)
+ * reference megapixels to the top left, rather than absolute grid?
+ */
+
+#include "libavutil/avstring.h"
+#include "libavutil/colorspace.h"
+#include "libavutil/lfg.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/parseutils.h"
+#include "avfilter.h"
+#include <strings.h>
+
+enum { Y, U, V, A };
+static int logging = 0;
+typedef enum {redact_solid,
+ redact_pixellate,
+ redact_inverse_pixellate,
+ redact_blur}
+ redaction_method;
+typedef struct {
+ int l, r, t, b;
+ double start, end;
+ redaction_method method;
+ unsigned char yuv_color[4]; // Used when method is redact_solid
+} BoxTrack;
+
+typedef struct {
+ int vsub, hsub; //< chroma subsampling
+ int numtracks;
+ double time_seconds;
+ BoxTrack **boxtracks;
+ AVLFG random;
+ AVFilterBufferRef *lastredacted; ///< Previous frame
+} RedactionContext;
+
+static void log_box_track(BoxTrack *bt,
+ AVFilterContext *ctx) {
+ av_log(ctx, AV_LOG_INFO, "Box track: %d: (%.1f-%.1fs) %d-%d x %d-%d\n",
+ bt->method, bt->start, bt->end,
+ bt->l, bt->r, bt->t, bt->b);
+}
+
+// memory status stuff from
+// stackoverflow.com/questions/1558402/memory-usage-of-current-process-in-c
+typedef struct {
+ unsigned long size,resident,share,text,lib,data,dt;
+} statm_t;
+
+static void read_off_memory_status(statm_t * result)
+{
+ const char* statm_path = "/proc/self/statm";
+
+ FILE *f = fopen(statm_path,"r");
+ if(!f){
+ abort();
+ }
+ if(7 != fscanf(f,"%ld %ld %ld %ld %ld %ld %ld",
+ &result->size,
+ &result->resident,
+ &result->share,
+ &result->text,
+ &result->lib,
+ &result->data,
+ &result->dt))
+ {
+ abort();
+ }
+ fclose(f);
+}
+
+static BoxTrack *box_track_from_string(const char *track_def,
+ AVFilterContext *ctx) {
+ BoxTrack *boxtrack = NULL;
+ int rv = 0;
+ int l, r, t, b;
+#define BUFLEN 1000
+ char method[BUFLEN];
+ double start, end;
+
+ // Allow comments, empty lines.
+ if (track_def[0] == '#' || track_def[0] == '\0')
+ return NULL;
+
+ rv = sscanf(track_def, "%lf,%lf,%d,%d,%d,%d,%s", &start,&end,
+ &l, &r, &t, &b, method);
+ if (rv != 7) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to parse boxtrack '%s' .\n", track_def);
+ return NULL;
+ }
+ boxtrack = (BoxTrack *)av_malloc(sizeof(BoxTrack));
+ boxtrack->l = l;
+ boxtrack->r = r;
+ boxtrack->t = t;
+ boxtrack->b = b;
+ boxtrack->start = start;
+ boxtrack->end = end;
+ boxtrack->method = redact_pixellate;
+
+ // todo: allow the granularity of the pixellation to be specified.
+ if (av_strncasecmp(method, "pixel", 5) == 0)
+ boxtrack->method = redact_pixellate;
+ else if (av_strncasecmp(method, "inv", 3) == 0)
+ boxtrack->method = redact_inverse_pixellate;
+ else if (av_strncasecmp(method, "blur", 4) == 0)
+ boxtrack->method = redact_blur;
+ else {
+ uint8_t rgba_color[4];
+
+ boxtrack->method = redact_solid;
+
+ if (av_parse_color(rgba_color, method, -1, ctx) < 0)
+ av_log(ctx, AV_LOG_ERROR, "Couldn't parse colour '%s' .\n", method);
+
+ boxtrack->yuv_color[Y] =
+ RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
+ boxtrack->yuv_color[U] =
+ RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
+ boxtrack->yuv_color[V] =
+ RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
+ boxtrack->yuv_color[A] = rgba_color[3];
+ }
+ return boxtrack;
+}
+
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
+{
+ RedactionContext *redaction= ctx->priv;
+ FILE *file = NULL;
+ char buf[BUFLEN];
+ unsigned int seed=298379;
+ redaction->boxtracks = NULL;
+ redaction->lastredacted = NULL;
+ redaction->time_seconds = NAN;
+ if (!args) {
+ av_log(ctx, AV_LOG_ERROR, "No arguments given to redact.\n");
+ return 1;
+ }
+ file = fopen(args, "r");
+ if (!file) {
+ av_log(ctx, AV_LOG_ERROR, "Can't open redaction file: '%s'\n", args);
+ return 2;
+ }
+ redaction->numtracks = 0;
+ // Parse the config file.
+ while (!feof(file)) {
+ BoxTrack **boxtracks = NULL;
+ BoxTrack *new_track = NULL;
+
+ if (fgets(buf, BUFLEN, file) == NULL) break; // EOF
+ if (strncmp(buf, "seed", 4) == 0) {
+ int rv = sscanf(buf, "seed %ud", &seed);
+ if (rv != 1)
+ av_log(ctx, AV_LOG_ERROR, "Didn't parse seed: %s.\n", buf);
+
+ continue;
+ }
+ new_track = box_track_from_string(buf, ctx);
+ if (new_track == NULL)
+ continue;
+ log_box_track(new_track, ctx);
+ // Resize the array and add the new track.
+ boxtracks = (BoxTrack **)av_malloc((redaction->numtracks + 1) *
+ sizeof(BoxTrack *));
+ for (int i = 0; i < redaction->numtracks; ++i)
+ boxtracks[i] = redaction->boxtracks[i];
+ boxtracks[redaction->numtracks++] = new_track;
+ av_free(redaction->boxtracks);
+ redaction->boxtracks = boxtracks;
+ }
+ av_log(ctx, AV_LOG_INFO, "Seed is : '%ud'\n", seed);
+ av_lfg_init(&redaction->random, seed);
+ fclose(file);
+ // Sort the tracks so the earliest-starting are at the end of the array.
+ for (int j = 0; j < redaction->numtracks -1; ++j)
+ for (int k = j + 1; k < redaction->numtracks; ++k)
+ if (redaction->boxtracks[j]->start < redaction->boxtracks[k]->start) {
+ BoxTrack *temp = redaction->boxtracks[j];
+ redaction->boxtracks[j] = redaction->boxtracks[k];
+ redaction->boxtracks[k] = temp;
+ }
+
+ return 0;
+}
+
+static int query_formats(AVFilterContext *ctx)
+{
+ enum PixelFormat pix_fmts[] = {
+ PIX_FMT_YUV444P, PIX_FMT_YUV422P, PIX_FMT_YUV420P,
+ PIX_FMT_YUV411P, PIX_FMT_YUV410P,
+ PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, PIX_FMT_YUVJ420P,
+ PIX_FMT_YUV440P, PIX_FMT_YUVJ440P,
+ PIX_FMT_NONE
+ };
+
+ avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
+ return 0;
+}
+
+static int config_input(AVFilterLink *inlink)
+{
+ RedactionContext *redaction = inlink->dst->priv;
+
+ redaction->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
+ redaction->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
+
+ av_log(inlink->dst, AV_LOG_INFO, "Redaction with %d tracks %d %d\n",
+ redaction->numtracks, redaction->hsub, redaction->vsub);
+
+ return 0;
+}
+
+// Decode the timestamp.
+static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *picref)
+{
+ AVFilterContext *ctx = inlink->dst;
+ RedactionContext *redaction = inlink->dst->priv;
+ AVFilterLink *outlink0 = inlink->dst->outputs[0];
+
+ redaction->time_seconds = picref->pts * av_q2d(inlink->time_base);
+ outlink0->out_buf = avfilter_get_video_buffer(outlink0, AV_PERM_WRITE,
+ outlink0->w, outlink0->h);
+ outlink0->out_buf->pts = picref->pts;
+ avfilter_start_frame(outlink0,
+ avfilter_ref_buffer(outlink0->out_buf, ~0));
+}
+
+static int noise = 10;
+static void convolve_ny(int t, int b, int maxy,
+ unsigned char *row, int blur, unsigned char *blurbuf,
+ int step,
+ AVLFG *random) {
+ int halfblur = blur/2;
+ int blursum = 0;
+ int i;
+ for (i = 0; i < blur; ++i) {
+ int pos = t + i - halfblur;
+ blurbuf[i] = (pos <= 0) ? row[0] : row[pos * step];
+ blursum += blurbuf[i];
+ }
+ for (int y = t; y < b; ++y, ++i) {
+ int newval = blursum / blur;
+ int newpos = y + (blur + 1)/2;
+ i %= blur;
+ if (noise > 0) {
+ newval += (av_lfg_get(random) % (2 * noise + 1)) - noise;
+ if (newval < 0 ) newval = 0;
+ else if (newval > 255) newval = 255;
+ }
+ row[y*step] = newval;
+ blursum -= blurbuf[i];
+ blurbuf[i] = row[step * ((newpos < maxy) ? newpos : (maxy - 1))];
+ blursum += blurbuf[i];
+ }
+}
+// Keep a rolling buffer of n values and their sum.
+// Store the average in the output vector and update the sum by dropping one
+// value and inserting another.
+static void convolve_nx(int l, int r, int maxx,
+ unsigned char *row, int blur, unsigned char *blurbuf,
+ AVLFG *random) {
+ int halfblur = blur/2;
+ int blursum = 0;
+ int i;
+ for (i = 0; i < blur; ++i) {
+ int pos = l + i - halfblur;
+ blurbuf[i] = (pos <= 0) ? row[0] : row[pos];
+ blursum += blurbuf[i];
+ }
+ for (int x = l; x < r; ++x, ++i) {
+ int newval = blursum / blur;
+ int newpos = x + (blur + 1)/2;
+ i %= blur;
+ if (noise > 0) {
+ newval += (av_lfg_get(random) % (2 * noise + 1)) - noise;
+ if (newval < 0 ) newval = 0;
+ else if (newval > 255) newval = 255;
+ }
+ row[x] = newval;
+ blursum -= blurbuf[i];
+ blurbuf[i] = row[(newpos < maxx) ? newpos : (maxx - 1)];
+ blursum += blurbuf[i];
+ }
+}
+
+static void blur_one_round(AVFilterBufferRef *picref, BoxTrack *boxtrack,
+ int y0, int h, int hsub, int vsub,
+ int blur, unsigned char *blurbuf,
+ AVLFG *random) {
+ int xb = boxtrack->l, yb = boxtrack->t;
+ int hb = boxtrack->b - boxtrack->t;
+ int wb = boxtrack->r - boxtrack->l;
+ int x, y;
+ int xmax, ymax;
+
+#define BLURX
+#define BLURY
+#ifdef BLURX
+ x = FFMAX(xb, 0);
+ blur = wb/2;
+ xmax = FFMIN( (xb + wb), picref->video->w);
+ for (y = FFMAX(yb, y0); y < (y0 + h) && y < (yb + hb); ++y) {
+ for (int plane = 0; plane < 3; plane++) {
+ int ds = (plane == 0) ? 0 : hsub;
+ unsigned char *row = picref->data[plane] +
+ picref->linesize[plane] * (y >> ((plane == 0) ? 0 : vsub));
+ convolve_nx(x >> ds, (xmax + ((1<<ds) -1))>> ds, picref->video->w >> ds,
+ row,
+ (blur + ((1<<ds) -1))>> ds, blurbuf, random);
+ }
+ }
+#endif
+#ifdef BLURY
+ y = FFMAX(yb, y0);
+ ymax = FFMIN( (yb + hb), (y0 + h));
+ blur = hb/2;
+ for (x = FFMAX(xb, 0); x < (xb + wb) && x < picref->video->w; x++) {
+ for (int plane = 0; plane < 3; plane++) {
+ int ds = (plane == 0) ? 0 : vsub;
+ unsigned char *col = picref->data[plane] + (x >> ((plane == 0) ? 0 : hsub));
+ convolve_ny(y >> ds, (ymax + ((1<<ds) -1)) >> ds,
+ (y0 + h) >> ds,
+ col, (blur + ((1<<ds) -1))>> ds, blurbuf,
+ picref->linesize[plane], random);
+ }
+ }
+#endif
+}
+
+static void copybox_mixold_alpha(AVFilterBufferRef *source,
+ AVFilterBufferRef *picref,
+ AVFilterBufferRef *lastref,
+ BoxTrack *boxtrack,
+ int hsub, int vsub,
+ AVLFG *random) {
+ int xb = boxtrack->l;
+ int yb = boxtrack->t;
+ int hb = boxtrack->b - boxtrack->t;
+ int wb = boxtrack->r - boxtrack->l;
+
+ float blur_boundary = 0.2;
+ for (int y = FFMAX(yb, 0); y < picref->video->h && y < boxtrack->b; y++) {
+ float ynormsq = (y * 2.0 - (boxtrack->b + boxtrack->t)) / hb;
+ ynormsq *= ynormsq;
+ for (int plane = 0; plane < 3; plane++) {
+ int ysub = (y >> ((plane == 0) ? 0 : vsub));
+ unsigned char *row = picref->data[plane] +
+ picref->linesize[plane] * ysub;
+ unsigned char *srcrow = source->data[plane] +
+ picref->linesize[plane] * ysub;
+ unsigned char *lastrow = lastref->data[plane] +
+ picref->linesize[plane] * ysub;
+
+ int thishsub = (plane == 0) ? 0 : hsub;
+ int xmin = FFMAX(xb, 0) >> thishsub;
+ int xmax = FFMIN(boxtrack->r, picref->video->w)
+ >> thishsub;
+ for (int x = xmin; x < xmax; x++) {
+ // TODO: do the alphablending in int.
+ // TODO: allow a flag for alpha blending or not.
+ float xnorm = ((x << thishsub) * 2.0 - (boxtrack->l + boxtrack->r))
+ / wb;
+ float mixlast = ((av_lfg_get(random) % 20) + 10) / 40.0;
+ float alphax = (1 - sqrt(xnorm * xnorm + ynormsq));
+ if (alphax < 0) {
+ row[x] = srcrow[x];
+ continue;
+ }
+ if (alphax > blur_boundary)
+ alphax = 1;
+ else
+ alphax /= blur_boundary;
+ row[x] = (1 - alphax) * srcrow[x] +
+ alphax * ((1 - mixlast) * row[x] + mixlast * lastrow[x]);
+ }
+ }
+ }
+}
+
+// In a picture carry out the obscuration of boxtrack.
+static void obscure_one_box(AVFilterBufferRef *source,
+ AVFilterBufferRef *picref,
+ AVFilterBufferRef *lastref,
+ BoxTrack *boxtrack,
+ int y0, int h, int hsub, int vsub,
+ AVLFG *random) {
+ unsigned char *row[4];
+ int xb = boxtrack->l, yb = boxtrack->t;
+ int hb = boxtrack->b - boxtrack->t;
+ int wb = boxtrack->r - boxtrack->l;
+ int megapixel_size = 64; // todo: get from file
+ int x, y;
+ if (boxtrack->method == redact_blur) {
+ const int blur = FFMAX(hb, wb);
+ unsigned char *blurbuf = (unsigned char *)av_malloc(blur);
+ blur_one_round(picref, boxtrack, y0, h, hsub, vsub, blur, blurbuf, random);
+ copybox_mixold_alpha(source, picref,
+ ((lastref==NULL)?source:lastref),
+ boxtrack, hsub, vsub, random);
+ av_free(blurbuf);
+ return;
+ }
+
+ for (y = FFMAX(yb, y0); y < (y0 + h) && y < (yb + hb); y++) {
+ row[0] = picref->data[0] + y * picref->linesize[0];
+
+ for (int plane = 1; plane < 3; plane++)
+ row[plane] = picref->data[plane] +
+ picref->linesize[plane] * (y >> vsub);
+
+ for (x = FFMAX(xb, 0); x < (xb + wb) && x < picref->video->w; x++) {
+ double alpha = (double)boxtrack->yuv_color[A] / 255;
+ if (boxtrack->method == redact_solid) {
+ row[0][x] = (1 - alpha) * row[0][x] +
+ alpha * boxtrack->yuv_color[Y];
+ // todo: if hsub is non-zero this will do the same pixel mutliple times.
+ // which is wasteful. Wrong if alpha is != 1
+ row[1][x >> hsub] = (1 - alpha) * row[1][x >> hsub] +
+ alpha * boxtrack->yuv_color[U];
+ row[2][x >> hsub] = (1 - alpha) * row[2][x >> hsub] +
+ alpha * boxtrack->yuv_color[V];
+ } else if (boxtrack->method == redact_pixellate) {
+ int x_quant = (x / megapixel_size) * megapixel_size;
+ int y_quant = (y / megapixel_size) * megapixel_size;
+ row[0][x] = (picref->data[0] + y_quant *
+ picref->linesize[0])[x_quant];
+ row[1][x >> hsub] = (picref->data[1] + picref->linesize[1] *
+ (y_quant >> vsub))[x_quant >> hsub];
+ row[2][x >> hsub] = (picref->data[2] + picref->linesize[2] *
+ (y_quant >> vsub))[x_quant >> hsub];
+ }
+ }
+ }
+}
+
+// Set all elements of planes 0,1,2 to val: 128=grey 0=green
+static void erase_output2(AVFilterBufferRef *outpic,
+ int y0, int h, int hsub, int vsub,
+ unsigned char val) {
+ unsigned char v[] = { 16, 128, 128};
+ for (int y = y0; y < (y0 + h); y++) {
+ for (int plane = 0; plane < 3; plane++) {
+ uint8_t *outrow = outpic->data[plane] +
+ outpic->linesize[plane] * (y >> ((plane == 0)?0:vsub));
+ const int xmax = outpic->video->w >> ((plane == 0)?0:hsub);
+ memset(outrow, v[plane], xmax);
+ } // plane
+ } // y
+}
+
+// Copy all the image data from picref to outpic.
+static void copy_all(AVFilterBufferRef *picref,
+ AVFilterBufferRef *outpic,
+ int hsub, int vsub) {
+ for (int y = 0; y < picref->video->h; y++) {
+ for (int plane = 0; plane < 3; plane++) {
+ uint8_t *row = picref->data[plane] +
+ picref->linesize[plane] * (y >> ((plane == 0)?0:vsub));
+ uint8_t *outrow = outpic->data[plane] +
+ outpic->linesize[plane] * (y >> ((plane == 0)?0:vsub));
+ int xwid = picref->video->w >> ((plane == 0)?0:hsub);
+ memcpy(outrow, row, xwid);
+ } // plane
+ } // y
+}
+
+// Copy one box from the input to the output.
+static void copy_one_box(AVFilterBufferRef *picref,
+ AVFilterBufferRef *outpic,
+ BoxTrack *boxtrack,
+ int y0, int h, int hsub, int vsub) {
+ int xb = boxtrack->l;
+ int yb = boxtrack->t;
+ int hb = boxtrack->b - boxtrack->t;
+ int wb = boxtrack->r - boxtrack->l;
+
+ for (int y = FFMAX(yb, y0); y < (y0 + h) && y < (yb + hb); y++) {
+ for (int plane = 0; plane < 3; plane++) {
+ uint8_t *row = picref->data[plane] +
+ picref->linesize[plane] * (y >> ((plane == 0)?0:vsub));
+ uint8_t *outrow = outpic->data[plane] +
+ outpic->linesize[plane] * (y >> ((plane == 0)?0:vsub));
+ int xmin = FFMAX(xb, 0) >> ((plane == 0)?0:hsub);
+ int xmax = FFMIN((xb + wb), picref->video->w)
+ >> ((plane == 0)?0:hsub);
+ memcpy(outrow + xmin, row + xmin, xmax - xmin);
+ } // plane