FFmpeg  4.4.9
signature_lookup.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Gerion Entrup
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 /**
22  * @file
23  * MPEG-7 video signature calculation and lookup filter
24  */
25 
26 #include "signature.h"
27 
28 #define HOUGH_MAX_OFFSET 90
29 #define MAX_FRAMERATE 60
30 
31 #define DIR_PREV 0
32 #define DIR_NEXT 1
33 #define DIR_PREV_END 2
34 #define DIR_NEXT_END 3
35 
36 #define STATUS_NULL 0
37 #define STATUS_END_REACHED 1
38 #define STATUS_BEGIN_REACHED 2
39 
40 static void sll_free(MatchingInfo **sll)
41 {
42  while (*sll) {
43  MatchingInfo *tmp = *sll;
44  *sll = tmp->next;
45  tmp->next = NULL;
46  av_free(tmp);
47  }
48 }
49 
50 static void fill_l1distlut(uint8_t lut[])
51 {
52  int i, j, tmp_i, tmp_j,count;
53  uint8_t dist;
54 
55  for (i = 0, count = 0; i < 242; i++) {
56  for (j = i + 1; j < 243; j++, count++) {
57  /* ternary distance between i and j */
58  dist = 0;
59  tmp_i = i; tmp_j = j;
60  do {
61  dist += FFABS((tmp_j % 3) - (tmp_i % 3));
62  tmp_j /= 3;
63  tmp_i /= 3;
64  } while (tmp_i > 0 || tmp_j > 0);
65  lut[count] = dist;
66  }
67  }
68 }
69 
70 static unsigned int intersection_word(const uint8_t *first, const uint8_t *second)
71 {
72  unsigned int val=0,i;
73  for (i = 0; i < 28; i += 4) {
74  val += av_popcount( (first[i] & second[i] ) << 24 |
75  (first[i+1] & second[i+1]) << 16 |
76  (first[i+2] & second[i+2]) << 8 |
77  (first[i+3] & second[i+3]) );
78  }
79  val += av_popcount( (first[28] & second[28]) << 16 |
80  (first[29] & second[29]) << 8 |
81  (first[30] & second[30]) );
82  return val;
83 }
84 
85 static unsigned int union_word(const uint8_t *first, const uint8_t *second)
86 {
87  unsigned int val=0,i;
88  for (i = 0; i < 28; i += 4) {
89  val += av_popcount( (first[i] | second[i] ) << 24 |
90  (first[i+1] | second[i+1]) << 16 |
91  (first[i+2] | second[i+2]) << 8 |
92  (first[i+3] | second[i+3]) );
93  }
94  val += av_popcount( (first[28] | second[28]) << 16 |
95  (first[29] | second[29]) << 8 |
96  (first[30] | second[30]) );
97  return val;
98 }
99 
100 static unsigned int get_l1dist(AVFilterContext *ctx, SignatureContext *sc, const uint8_t *first, const uint8_t *second)
101 {
102  unsigned int i;
103  unsigned int dist = 0;
104  uint8_t f, s;
105 
106  for (i = 0; i < SIGELEM_SIZE/5; i++) {
107  if (first[i] != second[i]) {
108  f = first[i];
109  s = second[i];
110  if (f > s) {
111  /* little variation of gauss sum formula */
112  dist += sc->l1distlut[243*242/2 - (243-s)*(242-s)/2 + f - s - 1];
113  } else {
114  dist += sc->l1distlut[243*242/2 - (243-f)*(242-f)/2 + s - f - 1];
115  }
116  }
117  }
118  return dist;
119 }
120 
121 /**
122  * calculates the jaccard distance and evaluates a pair of coarse signatures as good
123  * @return 0 if pair is bad, 1 otherwise
124  */
126 {
127  int jaccarddist, i, composdist = 0, cwthcount = 0;
128  for (i = 0; i < 5; i++) {
129  if ((jaccarddist = (1 << 16) * intersection_word(first->data[i], second->data[i])) > 0) {
130  jaccarddist /= union_word(first->data[i], second->data[i]);
131  }
132  jaccarddist = (1 << 16) - jaccarddist;
133  if (jaccarddist >= sc->thworddist) {
134  if (++cwthcount > 2) {
135  /* more than half (5/2) of distances are too wide */
136  return 0;
137  }
138  }
139  composdist += jaccarddist;
140  if (composdist > sc->thcomposdist) {
141  return 0;
142  }
143  }
144  return 1;
145 }
146 
147 /**
148  * step through the coarsesignatures as long as a good candidate is found
149  * @return 0 if no candidate is found, 1 otherwise
150  */
151 static int find_next_coarsecandidate(SignatureContext *sc, CoarseSignature *secondstart, CoarseSignature **first, CoarseSignature **second, int start)
152 {
153  /* go one coarsesignature foreword */
154  if (!start) {
155  if ((*second)->next) {
156  *second = (*second)->next;
157  } else if ((*first)->next) {
158  *second = secondstart;
159  *first = (*first)->next;
160  } else {
161  return 0;
162  }
163  }
164 
165  while (1) {
166  if (get_jaccarddist(sc, *first, *second))
167  return 1;
168 
169  /* next signature */
170  if ((*second)->next) {
171  *second = (*second)->next;
172  } else if ((*first)->next) {
173  *second = secondstart;
174  *first = (*first)->next;
175  } else {
176  return 0;
177  }
178  }
179 }
180 
181 /**
182  * compares framesignatures and sorts out signatures with a l1 distance above a given threshold.
183  * Then tries to find out offset and differences between framerates with a hough transformation
184  */
186 {
187  FineSignature *f, *s;
188  size_t i, j, k, l, hmax = 0, score;
189  int framerate, offset, l1dist;
190  double m;
191  MatchingInfo *cands = NULL, *c = NULL;
192 
193  struct {
194  uint8_t size;
195  unsigned int dist;
196  FineSignature *a;
197  uint8_t b_pos[COARSE_SIZE];
199  } pairs[COARSE_SIZE];
200 
201  typedef struct hspace_elem {
202  int dist;
203  size_t score;
204  FineSignature *a;
205  FineSignature *b;
206  } hspace_elem;
207 
208  /* houghspace */
209  hspace_elem **hspace = av_mallocz(MAX_FRAMERATE * sizeof(*hspace));
210 
211  if (!hspace)
212  return NULL;
213  /* initialize houghspace */
214  for (i = 0; i < MAX_FRAMERATE; i++) {
215  hspace[i] = av_malloc_array(2 * HOUGH_MAX_OFFSET + 1, sizeof(hspace_elem));
216  if (!hspace[i])
217  goto error;
218  for (j = 0; j < 2 * HOUGH_MAX_OFFSET + 1; j++) {
219  hspace[i][j].score = 0;
220  hspace[i][j].dist = 99999;
221  }
222  }
223 
224  /* l1 distances */
225  for (i = 0, f = first; i < COARSE_SIZE && f->next; i++, f = f->next) {
226  pairs[i].size = 0;
227  pairs[i].dist = 99999;
228  pairs[i].a = f;
229  for (j = 0, s = second; j < COARSE_SIZE && s->next; j++, s = s->next) {
230  /* l1 distance of finesignature */
231  l1dist = get_l1dist(ctx, sc, f->framesig, s->framesig);
232  if (l1dist < sc->thl1) {
233  if (l1dist < pairs[i].dist) {
234  pairs[i].size = 1;
235  pairs[i].dist = l1dist;
236  pairs[i].b_pos[0] = j;
237  pairs[i].b[0] = s;
238  } else if (l1dist == pairs[i].dist) {
239  pairs[i].b[pairs[i].size] = s;
240  pairs[i].b_pos[pairs[i].size] = j;
241  pairs[i].size++;
242  }
243  }
244  }
245  }
246  /* last incomplete coarsesignature */
247  if (f->next == NULL) {
248  for (; i < COARSE_SIZE; i++) {
249  pairs[i].size = 0;
250  pairs[i].dist = 99999;
251  }
252  }
253 
254  /* hough transformation */
255  for (i = 0; i < COARSE_SIZE; i++) {
256  for (j = 0; j < pairs[i].size; j++) {
257  for (k = i + 1; k < COARSE_SIZE; k++) {
258  for (l = 0; l < pairs[k].size; l++) {
259  if (pairs[i].b[j] != pairs[k].b[l]) {
260  /* linear regression */
261  m = (pairs[k].b_pos[l]-pairs[i].b_pos[j]) / (k-i); /* good value between 0.0 - 2.0 */
262  framerate = (int) (m*30 + 0.5); /* round up to 0 - 60 */
263  if (framerate>0 && framerate <= MAX_FRAMERATE) {
264  offset = pairs[i].b_pos[j] - ((int) (m*i + 0.5)); /* only second part has to be rounded up */
266  if (pairs[i].dist < pairs[k].dist) {
267  if (pairs[i].dist < hspace[framerate-1][offset+HOUGH_MAX_OFFSET].dist) {
268  hspace[framerate-1][offset+HOUGH_MAX_OFFSET].dist = pairs[i].dist;
269  hspace[framerate-1][offset+HOUGH_MAX_OFFSET].a = pairs[i].a;
270  hspace[framerate-1][offset+HOUGH_MAX_OFFSET].b = pairs[i].b[j];
271  }
272  } else {
273  if (pairs[k].dist < hspace[framerate-1][offset+HOUGH_MAX_OFFSET].dist) {
274  hspace[framerate-1][offset+HOUGH_MAX_OFFSET].dist = pairs[k].dist;
275  hspace[framerate-1][offset+HOUGH_MAX_OFFSET].a = pairs[k].a;
276  hspace[framerate-1][offset+HOUGH_MAX_OFFSET].b = pairs[k].b[l];
277  }
278  }
279 
280  score = hspace[framerate-1][offset+HOUGH_MAX_OFFSET].score + 1;
281  if (score > hmax )
282  hmax = score;
283  hspace[framerate-1][offset+HOUGH_MAX_OFFSET].score = score;
284  }
285  }
286  }
287  }
288  }
289  }
290  }
291 
292  if (hmax > 0) {
293  hmax = (int) (0.7*hmax);
294  for (i = 0; i < MAX_FRAMERATE; i++) {
295  for (j = 0; j < HOUGH_MAX_OFFSET; j++) {
296  if (hmax < hspace[i][j].score) {
297  if (c == NULL) {
298  c = av_malloc(sizeof(MatchingInfo));
299  if (!c)
300  av_log(ctx, AV_LOG_FATAL, "Could not allocate memory");
301  cands = c;
302  } else {
303  c->next = av_malloc(sizeof(MatchingInfo));
304  if (!c->next)
305  av_log(ctx, AV_LOG_FATAL, "Could not allocate memory");
306  c = c->next;
307 
308  }
309  if (!c) {
310  sll_free(&cands);
311  goto error;
312  }
313  c->framerateratio = (i+1.0) / 30;
314  c->score = hspace[i][j].score;
315  c->offset = j-90;
316  c->first = hspace[i][j].a;
317  c->second = hspace[i][j].b;
318  c->next = NULL;
319 
320  /* not used */
321  c->meandist = 0;
322  c->matchframes = 0;
323  c->whole = 0;
324  }
325  }
326  }
327  }
328  error:
329  for (i = 0; i < MAX_FRAMERATE; i++) {
330  av_freep(&hspace[i]);
331  }
332  av_freep(&hspace);
333  return cands;
334 }
335 
336 static int iterate_frame(double frr, FineSignature **a, FineSignature **b, int fcount, int *bcount, int dir)
337 {
338  int step;
339 
340  /* between 1 and 2, because frr is between 1 and 2 */
341  step = ((int) 0.5 + fcount * frr) /* current frame */
342  -((int) 0.5 + (fcount-1) * frr);/* last frame */
343 
344  if (dir == DIR_NEXT) {
345  if (frr >= 1.0) {
346  if ((*a)->next) {
347  *a = (*a)->next;
348  } else {
349  return DIR_NEXT_END;
350  }
351 
352  if (step == 1) {
353  if ((*b)->next) {
354  *b = (*b)->next;
355  (*bcount)++;
356  } else {
357  return DIR_NEXT_END;
358  }
359  } else {
360  if ((*b)->next && (*b)->next->next) {
361  *b = (*b)->next->next;
362  (*bcount)++;
363  } else {
364  return DIR_NEXT_END;
365  }
366  }
367  } else {
368  if ((*b)->next) {
369  *b = (*b)->next;
370  (*bcount)++;
371  } else {
372  return DIR_NEXT_END;
373  }
374 
375  if (step == 1) {
376  if ((*a)->next) {
377  *a = (*a)->next;
378  } else {
379  return DIR_NEXT_END;
380  }
381  } else {
382  if ((*a)->next && (*a)->next->next) {
383  *a = (*a)->next->next;
384  } else {
385  return DIR_NEXT_END;
386  }
387  }
388  }
389  return DIR_NEXT;
390  } else {
391  if (frr >= 1.0) {
392  if ((*a)->prev) {
393  *a = (*a)->prev;
394  } else {
395  return DIR_PREV_END;
396  }
397 
398  if (step == 1) {
399  if ((*b)->prev) {
400  *b = (*b)->prev;
401  (*bcount)++;
402  } else {
403  return DIR_PREV_END;
404  }
405  } else {
406  if ((*b)->prev && (*b)->prev->prev) {
407  *b = (*b)->prev->prev;
408  (*bcount)++;
409  } else {
410  return DIR_PREV_END;
411  }
412  }
413  } else {
414  if ((*b)->prev) {
415  *b = (*b)->prev;
416  (*bcount)++;
417  } else {
418  return DIR_PREV_END;
419  }
420 
421  if (step == 1) {
422  if ((*a)->prev) {
423  *a = (*a)->prev;
424  } else {
425  return DIR_PREV_END;
426  }
427  } else {
428  if ((*a)->prev && (*a)->prev->prev) {
429  *a = (*a)->prev->prev;
430  } else {
431  return DIR_PREV_END;
432  }
433  }
434  }
435  return DIR_PREV;
436  }
437 }
438 
440 {
441  int dist, distsum = 0, bcount = 1, dir = DIR_NEXT;
442  int fcount = 0, goodfcount = 0, gooda = 0, goodb = 0;
443  double meandist, minmeandist = bestmatch.meandist;
444  int tolerancecount = 0;
445  FineSignature *a, *b, *aprev, *bprev;
446  int status = STATUS_NULL;
447 
448  for (; infos != NULL; infos = infos->next) {
449  a = infos->first;
450  b = infos->second;
451  while (1) {
452  dist = get_l1dist(ctx, sc, a->framesig, b->framesig);
453 
454  if (dist > sc->thl1) {
455  if (a->confidence >= 1 || b->confidence >= 1) {
456  /* bad frame (because high different information) */
457  tolerancecount++;
458  }
459 
460  if (tolerancecount > 2) {
461  if (dir == DIR_NEXT) {
462  /* turn around */
463  a = infos->first;
464  b = infos->second;
465  dir = DIR_PREV;
466  } else {
467  a = aprev;
468  b = bprev;
469  break;
470  }
471  }
472  } else {
473  /* good frame */
474  distsum += dist;
475  goodfcount++;
476  tolerancecount=0;
477 
478  aprev = a;
479  bprev = b;
480 
481  if (a->confidence < 1) gooda++;
482  if (b->confidence < 1) goodb++;
483  }
484 
485  fcount++;
486 
487  dir = iterate_frame(infos->framerateratio, &a, &b, fcount, &bcount, dir);
488  if (dir == DIR_NEXT_END) {
489  status = STATUS_END_REACHED;
490  a = infos->first;
491  b = infos->second;
492  dir = iterate_frame(infos->framerateratio, &a, &b, fcount, &bcount, DIR_PREV);
493  }
494 
495  if (dir == DIR_PREV_END) {
496  status |= STATUS_BEGIN_REACHED;
497  break;
498  }
499 
500  if (sc->thdi != 0 && bcount >= sc->thdi) {
501  break; /* enough frames found */
502  }
503  }
504 
505  if (bcount < sc->thdi)
506  continue; /* matching sequence is too short */
507  if ((double) goodfcount / (double) fcount < sc->thit)
508  continue;
509  if ((double) goodfcount*0.5 <= FFMAX(gooda, goodb))
510  continue;
511 
512  meandist = (double) distsum / (double) goodfcount;
513 
514  if (meandist < minmeandist ||
516  mode == MODE_FAST){
517  minmeandist = meandist;
518  /* bestcandidate in this iteration */
519  bestmatch.meandist = meandist;
520  bestmatch.matchframes = bcount;
521  bestmatch.framerateratio = infos->framerateratio;
522  bestmatch.score = infos->score;
523  bestmatch.offset = infos->offset;
524  bestmatch.first = infos->first;
525  bestmatch.second = infos->second;
526  bestmatch.whole = 0; /* will be set to true later */
527  bestmatch.next = NULL;
528  }
529 
530  /* whole sequence is automatically best match */
531  if (status == (STATUS_END_REACHED | STATUS_BEGIN_REACHED)) {
532  bestmatch.whole = 1;
533  break;
534  }
535 
536  /* first matching sequence is enough, finding the best one is not necessary */
537  if (mode == MODE_FAST) {
538  break;
539  }
540  }
541  return bestmatch;
542 }
543 
545 {
546  CoarseSignature *cs, *cs2;
547  MatchingInfo *infos;
548  MatchingInfo bestmatch;
549  MatchingInfo *i;
550 
551  cs = first->coarsesiglist;
552  cs2 = second->coarsesiglist;
553 
554  /* score of bestmatch is 0, if no match is found */
555  bestmatch.score = 0;
556  bestmatch.meandist = 99999;
557  bestmatch.whole = 0;
558 
560 
561  /* stage 1: coarsesignature matching */
562  if (find_next_coarsecandidate(sc, second->coarsesiglist, &cs, &cs2, 1) == 0)
563  return bestmatch; /* no candidate found */
564  do {
565  av_log(ctx, AV_LOG_DEBUG, "Stage 1: got coarsesignature pair. "
566  "indices of first frame: %"PRIu32" and %"PRIu32"\n",
567  cs->first->index, cs2->first->index);
568  /* stage 2: l1-distance and hough-transform */
569  av_log(ctx, AV_LOG_DEBUG, "Stage 2: calculate matching parameters\n");
570  infos = get_matching_parameters(ctx, sc, cs->first, cs2->first);
571  if (av_log_get_level() == AV_LOG_DEBUG) {
572  for (i = infos; i != NULL; i = i->next) {
573  av_log(ctx, AV_LOG_DEBUG, "Stage 2: matching pair at %"PRIu32" and %"PRIu32", "
574  "ratio %f, offset %d\n", i->first->index, i->second->index,
575  i->framerateratio, i->offset);
576  }
577  }
578  /* stage 3: evaluation */
579  av_log(ctx, AV_LOG_DEBUG, "Stage 3: evaluate\n");
580  if (infos) {
581  bestmatch = evaluate_parameters(ctx, sc, infos, bestmatch, mode);
582  av_log(ctx, AV_LOG_DEBUG, "Stage 3: best matching pair at %"PRIu32" and %"PRIu32", "
583  "ratio %f, offset %d, score %d, %d frames matching\n",
584  bestmatch.first->index, bestmatch.second->index,
585  bestmatch.framerateratio, bestmatch.offset, bestmatch.score, bestmatch.matchframes);
586  sll_free(&infos);
587  }
588  } while (find_next_coarsecandidate(sc, second->coarsesiglist, &cs, &cs2, 0) && !bestmatch.whole);
589  return bestmatch;
590 
591 }
static double val(void *priv, double ch)
Definition: aeval.c:76
uint8_t
#define s(width, name)
Definition: cbs_vp9.c:257
#define f(width, name)
Definition: cbs_vp9.c:255
#define av_popcount
Definition: common.h:176
#define FFMAX(a, b)
Definition: common.h:103
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define NULL
Definition: coverity.c:32
mode
Use these values in ebur128_init (or'ed).
Definition: ebur128.h:83
int
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_FATAL
Something went wrong and recovery is not possible.
Definition: log.h:188
int av_log_get_level(void)
Get the current log level.
Definition: log.c:435
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
int i
Definition: input.c:407
MPEG-7 video signature calculation and lookup filter.
#define COARSE_SIZE
Definition: signature.h:39
#define SIGELEM_SIZE
Definition: signature.h:37
@ MODE_FAST
Definition: signature.h:44
#define DIR_NEXT
static int iterate_frame(double frr, FineSignature **a, FineSignature **b, int fcount, int *bcount, int dir)
#define DIR_PREV
#define DIR_PREV_END
static int find_next_coarsecandidate(SignatureContext *sc, CoarseSignature *secondstart, CoarseSignature **first, CoarseSignature **second, int start)
step through the coarsesignatures as long as a good candidate is found
static void sll_free(MatchingInfo **sll)
static MatchingInfo * get_matching_parameters(AVFilterContext *ctx, SignatureContext *sc, FineSignature *first, FineSignature *second)
compares framesignatures and sorts out signatures with a l1 distance above a given threshold.
static unsigned int union_word(const uint8_t *first, const uint8_t *second)
static MatchingInfo evaluate_parameters(AVFilterContext *ctx, SignatureContext *sc, MatchingInfo *infos, MatchingInfo bestmatch, int mode)
static unsigned int intersection_word(const uint8_t *first, const uint8_t *second)
#define STATUS_BEGIN_REACHED
#define HOUGH_MAX_OFFSET
#define MAX_FRAMERATE
#define STATUS_END_REACHED
static MatchingInfo lookup_signatures(AVFilterContext *ctx, SignatureContext *sc, StreamContext *first, StreamContext *second, int mode)
static void fill_l1distlut(uint8_t lut[])
#define STATUS_NULL
#define DIR_NEXT_END
static unsigned int get_l1dist(AVFilterContext *ctx, SignatureContext *sc, const uint8_t *first, const uint8_t *second)
static int get_jaccarddist(SignatureContext *sc, CoarseSignature *first, CoarseSignature *second)
calculates the jaccard distance and evaluates a pair of coarse signatures as good
An instance of a filter.
Definition: avfilter.h:341
struct FineSignature * first
Definition: signature.h:84
struct CoarseSignature * next
Definition: signature.h:86
uint8_t data[5][31]
Definition: signature.h:83
uint32_t index
Definition: signature.h:76
double meandist
Definition: signature.h:91
struct MatchingInfo * next
Definition: signature.h:99
double framerateratio
Definition: signature.h:92
struct FineSignature * second
Definition: signature.h:98
struct FineSignature * first
Definition: signature.h:97
int matchframes
Definition: signature.h:95
uint8_t l1distlut[243 *242/2]
Definition: signature.h:141
CoarseSignature * coarsesiglist
Definition: signature.h:114
#define av_free(p)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
int framerate
Definition: h264_levels.c:65
AVFormatContext * ctx
Definition: movenc.c:48
int size
const char * b
Definition: vf_curves.c:119
static const uint8_t offset[127][2]
Definition: vf_spp.c:107
static double c[64]