2 * Copyright (c) 2017 Gerion Entrup
4 * This file is part of FFmpeg.
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.
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.
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.
23 * MPEG-7 video signature calculation and lookup filter
26 #include "signature.h"
28 #define HOUGH_MAX_OFFSET 90
29 #define MAX_FRAMERATE 60
33 #define DIR_PREV_END 2
34 #define DIR_NEXT_END 3
37 #define STATUS_END_REACHED 1
38 #define STATUS_BEGIN_REACHED 2
40 static void fill_l1distlut(uint8_t lut[])
42 int i, j, tmp_i, tmp_j,count;
45 for (i = 0, count = 0; i < 242; i++) {
46 for (j = i + 1; j < 243; j++, count++) {
47 /* ternary distance between i and j */
51 dist += FFABS((tmp_j % 3) - (tmp_i % 3));
54 } while (tmp_i > 0 || tmp_j > 0);
60 static unsigned int intersection_word(const uint8_t *first, const uint8_t *second)
63 for (i = 0; i < 28; i += 4) {
64 val += av_popcount( (first[i] & second[i] ) << 24 |
65 (first[i+1] & second[i+1]) << 16 |
66 (first[i+2] & second[i+2]) << 8 |
67 (first[i+3] & second[i+3]) );
69 val += av_popcount( (first[28] & second[28]) << 16 |
70 (first[29] & second[29]) << 8 |
71 (first[30] & second[30]) );
75 static unsigned int union_word(const uint8_t *first, const uint8_t *second)
78 for (i = 0; i < 28; i += 4) {
79 val += av_popcount( (first[i] | second[i] ) << 24 |
80 (first[i+1] | second[i+1]) << 16 |
81 (first[i+2] | second[i+2]) << 8 |
82 (first[i+3] | second[i+3]) );
84 val += av_popcount( (first[28] | second[28]) << 16 |
85 (first[29] | second[29]) << 8 |
86 (first[30] | second[30]) );
90 static unsigned int get_l1dist(AVFilterContext *ctx, SignatureContext *sc, const uint8_t *first, const uint8_t *second)
93 unsigned int dist = 0;
96 for (i = 0; i < SIGELEM_SIZE/5; i++) {
97 if (first[i] != second[i]) {
101 /* little variation of gauss sum formula */
102 dist += sc->l1distlut[243*242/2 - (243-s)*(242-s)/2 + f - s - 1];
104 dist += sc->l1distlut[243*242/2 - (243-f)*(242-f)/2 + s - f - 1];
112 * calculates the jaccard distance and evaluates a pair of coarse signatures as good
113 * @return 0 if pair is bad, 1 otherwise
115 static int get_jaccarddist(SignatureContext *sc, CoarseSignature *first, CoarseSignature *second)
117 int jaccarddist, i, composdist = 0, cwthcount = 0;
118 for (i = 0; i < 5; i++) {
119 if ((jaccarddist = intersection_word(first->data[i], second->data[i])) > 0) {
120 jaccarddist /= union_word(first->data[i], second->data[i]);
122 if (jaccarddist >= sc->thworddist) {
123 if (++cwthcount > 2) {
124 /* more than half (5/2) of distances are too wide */
128 composdist += jaccarddist;
129 if (composdist > sc->thcomposdist) {
137 * step through the coarsesignatures as long as a good candidate is found
138 * @return 0 if no candidate is found, 1 otherwise
140 static int find_next_coarsecandidate(SignatureContext *sc, CoarseSignature *secondstart, CoarseSignature **first, CoarseSignature **second, int start)
142 /* go one coarsesignature foreword */
144 if ((*second)->next) {
145 *second = (*second)->next;
146 } else if ((*first)->next) {
147 *second = secondstart;
148 *first = (*first)->next;
155 if (get_jaccarddist(sc, *first, *second))
159 if ((*second)->next) {
160 *second = (*second)->next;
161 } else if ((*first)->next) {
162 *second = secondstart;
163 *first = (*first)->next;
171 * compares framesignatures and sorts out signatures with a l1 distance above a given threshold.
172 * Then tries to find out offset and differences between framerates with a hough transformation
174 static MatchingInfo* get_matching_parameters(AVFilterContext *ctx, SignatureContext *sc, FineSignature *first, FineSignature *second)
176 FineSignature *f, *s;
177 size_t i, j, k, l, hmax = 0, score;
178 int framerate, offset, l1dist;
180 MatchingInfo *cands = NULL, *c = NULL;
186 uint8_t b_pos[COARSE_SIZE];
187 FineSignature *b[COARSE_SIZE];
188 } pairs[COARSE_SIZE];
190 typedef struct hspace_elem {
198 hspace_elem** hspace = av_malloc_array(MAX_FRAMERATE, sizeof(hspace_elem *));
200 /* initialize houghspace */
201 for (i = 0; i < MAX_FRAMERATE; i++) {
202 hspace[i] = av_malloc_array(2 * HOUGH_MAX_OFFSET + 1, sizeof(hspace_elem));
203 for (j = 0; j < HOUGH_MAX_OFFSET; j++) {
204 hspace[i][j].score = 0;
205 hspace[i][j].dist = 99999;
210 for (i = 0, f = first; i < COARSE_SIZE && f->next; i++, f = f->next) {
212 pairs[i].dist = 99999;
214 for (j = 0, s = second; j < COARSE_SIZE && s->next; j++, s = s->next) {
215 /* l1 distance of finesignature */
216 l1dist = get_l1dist(ctx, sc, f->framesig, s->framesig);
217 if (l1dist < sc->thl1) {
218 if (l1dist < pairs[i].dist) {
220 pairs[i].dist = l1dist;
221 pairs[i].b_pos[0] = j;
223 } else if (l1dist == pairs[i].dist) {
224 pairs[i].b[pairs[i].size] = s;
225 pairs[i].b_pos[pairs[i].size] = j;
231 /* last incomplete coarsesignature */
232 if (f->next == NULL) {
233 for (; i < COARSE_SIZE; i++) {
235 pairs[i].dist = 99999;
239 /* hough transformation */
240 for (i = 0; i < COARSE_SIZE; i++) {
241 for (j = 0; j < pairs[i].size; j++) {
242 for (k = i + 1; k < COARSE_SIZE; k++) {
243 for (l = 0; l < pairs[k].size; l++) {
244 if (pairs[i].b[j] != pairs[k].b[l]) {
245 /* linear regression */
246 m = (pairs[k].b_pos[l]-pairs[i].b_pos[j]) / (k-i); /* good value between 0.0 - 2.0 */
247 framerate = (int) m*30 + 0.5; /* round up to 0 - 60 */
248 if (framerate>0 && framerate <= MAX_FRAMERATE) {
249 offset = pairs[i].b_pos[j] - ((int) m*i + 0.5); /* only second part has to be rounded up */
250 if (offset > -HOUGH_MAX_OFFSET && offset < HOUGH_MAX_OFFSET) {
251 if (pairs[i].dist < pairs[k].dist) {
252 if (pairs[i].dist < hspace[framerate-1][offset+HOUGH_MAX_OFFSET].dist) {
253 hspace[framerate-1][offset+HOUGH_MAX_OFFSET].dist = pairs[i].dist;
254 hspace[framerate-1][offset+HOUGH_MAX_OFFSET].a = pairs[i].a;
255 hspace[framerate-1][offset+HOUGH_MAX_OFFSET].b = pairs[i].b[j];
258 if (pairs[k].dist < hspace[framerate-1][offset+HOUGH_MAX_OFFSET].dist) {
259 hspace[framerate-1][offset+HOUGH_MAX_OFFSET].dist = pairs[k].dist;
260 hspace[framerate-1][offset+HOUGH_MAX_OFFSET].a = pairs[k].a;
261 hspace[framerate-1][offset+HOUGH_MAX_OFFSET].b = pairs[k].b[l];
265 score = hspace[framerate-1][offset+HOUGH_MAX_OFFSET].score + 1;
268 hspace[framerate-1][offset+HOUGH_MAX_OFFSET].score = score;
278 hmax = (int) (0.7*hmax);
279 for (i = 0; i < MAX_FRAMERATE; i++) {
280 for (j = 0; j < HOUGH_MAX_OFFSET; j++) {
281 if (hmax < hspace[i][j].score) {
283 c = av_malloc(sizeof(MatchingInfo));
285 av_log(ctx, AV_LOG_FATAL, "Could not allocate memory");
288 c->next = av_malloc(sizeof(MatchingInfo));
290 av_log(ctx, AV_LOG_FATAL, "Could not allocate memory");
293 c->framerateratio = (i+1.0) / 30;
294 c->score = hspace[i][j].score;
296 c->first = hspace[i][j].a;
297 c->second = hspace[i][j].b;
308 for (i = 0; i < MAX_FRAMERATE; i++) {
309 av_freep(&hspace[i]);
315 static int iterate_frame(double frr, FineSignature **a, FineSignature **b, int fcount, int *bcount, int dir)
319 /* between 1 and 2, because frr is between 1 and 2 */
320 step = ((int) 0.5 + fcount * frr) /* current frame */
321 -((int) 0.5 + (fcount-1) * frr);/* last frame */
323 if (dir == DIR_NEXT) {
339 if ((*b)->next && (*b)->next->next) {
340 *b = (*b)->next->next;
361 if ((*a)->next && (*a)->next->next) {
362 *a = (*a)->next->next;
385 if ((*b)->prev && (*b)->prev->prev) {
386 *b = (*b)->prev->prev;
407 if ((*a)->prev && (*a)->prev->prev) {
408 *a = (*a)->prev->prev;
418 static MatchingInfo evaluate_parameters(AVFilterContext *ctx, SignatureContext *sc, MatchingInfo *infos, MatchingInfo bestmatch, int mode)
420 int dist, distsum = 0, bcount = 1, dir = DIR_NEXT;
421 int fcount = 0, goodfcount = 0, gooda = 0, goodb = 0;
422 double meandist, minmeandist = bestmatch.meandist;
423 int tolerancecount = 0;
424 FineSignature *a, *b, *aprev, *bprev;
425 int status = STATUS_NULL;
427 for (; infos != NULL; infos = infos->next) {
431 dist = get_l1dist(ctx, sc, a->framesig, b->framesig);
433 if (dist > sc->thl1) {
434 if (a->confidence >= 1 || b->confidence >= 1) {
435 /* bad frame (because high different information) */
439 if (tolerancecount > 2) {
442 if (dir == DIR_NEXT) {
460 if (a->confidence < 1) gooda++;
461 if (b->confidence < 1) goodb++;
466 dir = iterate_frame(infos->framerateratio, &a, &b, fcount, &bcount, dir);
467 if (dir == DIR_NEXT_END) {
468 status = STATUS_END_REACHED;
471 dir = iterate_frame(infos->framerateratio, &a, &b, fcount, &bcount, DIR_PREV);
474 if (dir == DIR_PREV_END) {
475 status |= STATUS_BEGIN_REACHED;
479 if (sc->thdi != 0 && bcount >= sc->thdi) {
480 break; /* enough frames found */
484 if (bcount < sc->thdi)
485 continue; /* matching sequence is too short */
486 if ((double) goodfcount / (double) fcount < sc->thit)
488 if ((double) goodfcount*0.5 < FFMAX(gooda, goodb))
491 meandist = (double) goodfcount / (double) distsum;
493 if (meandist < minmeandist ||
494 status == STATUS_END_REACHED | STATUS_BEGIN_REACHED ||
496 minmeandist = meandist;
497 /* bestcandidate in this iteration */
498 bestmatch.meandist = meandist;
499 bestmatch.matchframes = bcount;
500 bestmatch.framerateratio = infos->framerateratio;
501 bestmatch.score = infos->score;
502 bestmatch.offset = infos->offset;
503 bestmatch.first = infos->first;
504 bestmatch.second = infos->second;
505 bestmatch.whole = 0; /* will be set to true later */
506 bestmatch.next = NULL;
509 /* whole sequence is automatically best match */
510 if (status == (STATUS_END_REACHED | STATUS_BEGIN_REACHED)) {
515 /* first matching sequence is enough, finding the best one is not necessary */
516 if (mode == MODE_FAST) {
523 static void sll_free(MatchingInfo *sll)
533 static MatchingInfo lookup_signatures(AVFilterContext *ctx, SignatureContext *sc, StreamContext *first, StreamContext *second, int mode)
535 CoarseSignature *cs, *cs2;
537 MatchingInfo bestmatch;
540 cs = first->coarsesiglist;
541 cs2 = second->coarsesiglist;
543 /* score of bestmatch is 0, if no match is found */
545 bestmatch.meandist = 99999;
548 fill_l1distlut(sc->l1distlut);
550 /* stage 1: coarsesignature matching */
551 if (find_next_coarsecandidate(sc, second->coarsesiglist, &cs, &cs2, 1) == 0)
552 return bestmatch; /* no candidate found */
554 av_log(ctx, AV_LOG_DEBUG, "Stage 1: got coarsesignature pair. "
555 "indices of first frame: %"PRIu32" and %"PRIu32"\n",
556 cs->first->index, cs2->first->index);
557 /* stage 2: l1-distance and hough-transform */
558 av_log(ctx, AV_LOG_DEBUG, "Stage 2: calculate matching parameters\n");
559 infos = get_matching_parameters(ctx, sc, cs->first, cs2->first);
560 if (av_log_get_level() == AV_LOG_DEBUG) {
561 for (i = infos; i != NULL; i = i->next) {
562 av_log(ctx, AV_LOG_DEBUG, "Stage 2: matching pair at %"PRIu32" and %"PRIu32", "
563 "ratio %f, offset %d\n", i->first->index, i->second->index,
564 i->framerateratio, i->offset);
567 /* stage 3: evaluation */
568 av_log(ctx, AV_LOG_DEBUG, "Stage 3: evaluate\n");
570 bestmatch = evaluate_parameters(ctx, sc, infos, bestmatch, mode);
571 av_log(ctx, AV_LOG_DEBUG, "Stage 3: best matching pair at %"PRIu32" and %"PRIu32", "
572 "ratio %f, offset %d, score %d, %d frames matching\n",
573 bestmatch.first->index, bestmatch.second->index,
574 bestmatch.framerateratio, bestmatch.offset, bestmatch.score, bestmatch.matchframes);
577 } while (find_next_coarsecandidate(sc, second->coarsesiglist, &cs, &cs2, 0) && !bestmatch.whole);