-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclipseekrutils.cpp
134 lines (113 loc) · 3.83 KB
/
clipseekrutils.cpp
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
#include <iostream>
#include <cstring>
#include <cmath>
#include "clipseekrutils.h"
#include <opencv2/core/core_c.h>
#include <opencv2/imgproc/imgproc_c.h>
using namespace std;
bool isblackframe(AVFrame *pframe, int pixel_threshold, int black_threshold){
int num_black = 0; // no. black pixels
int numpixels = pframe->width * pframe->height;
uint8_t *p = pframe->data[0];
for (int i=0;i<pframe->height;i++){
for (int j=0;j<pframe->width;j++){
num_black += p[j] < pixel_threshold;
}
p += pframe->linesize[0];
}
if (num_black * 100 /numpixels >= black_threshold){
return true;
}
return false;
}
int64_t process_timestamp(int64_t ts, AVRational tb){
return av_rescale(ts, tb.num, tb.den);
}
void process_timestamp(int64_t ts, AVRational tb, int &hrs, int &mins, int &secs){
int64_t seconds = av_rescale(ts, tb.num, tb.den);
hrs = seconds/3600;
int64_t s1 = seconds%3600;
mins = s1/60;
secs = s1%60;
}
void print_metadata(ph::MetaData &mdata){
cout << "--------info-------------" << endl;
cout << "title: " << mdata.title_str << endl;
cout << "artist: " << mdata.artist_str << endl;
cout << "album: " << mdata.album_str << endl;
cout << "genre: " << mdata.genre_str << endl;
cout << "composer: " << mdata.composer_str << endl;
cout << "performer: " << mdata.performer_str << endl;
cout << "album artist: " << mdata.album_artist_str << endl;
cout << "copyright: " << mdata.copyright_str << endl;
cout << "date: " << mdata.date_str << endl;
cout << "track: " << mdata.track_str << endl;
cout << "disc: " << mdata.disc_str << endl;
cout << "--------------------------" << endl;
}
void frame_hist(AVFrame *pframe, int hist[], int N){
int shiftby = 8 - log2(N);
memset((void*)hist, 0, N*sizeof(int));
uint8_t* p = pframe->data[0];
for (int i=0;i<pframe->height;i++){
for (int j=0;j<pframe->width;j++){
int pixel = p[j];
int hindex = pixel >> shiftby;
hist[hindex] += 1;
}
p += pframe->linesize[0];
}
}
double hist_diff(int hist[], int prevhist[], int N){
double acc = 0;
double sum = 0;
for (int i=0;i<N;i++){
sum += hist[i];
acc = acc + fabs(hist[i] - prevhist[i]);
}
return acc/sum;
}
static int _framehash(void *iplimg, float rho, uint64_t *hash, int blk_idx=1) {
if (!iplimg || !hash) return -1;
*hash = 0;
IplImage *img = (IplImage*)iplimg;
IplImage *imgplane = cvCreateImage(cvSize(img->width, img->height), img->depth, 1);
IplImage *imgblurred = cvCreateImage(cvSize(img->width, img->height), img->depth, 1);
IplImage *imgresized = cvCreateImage(cvSize(32,32), img->depth, 1);
IplImage *imgprime = cvCreateImage(cvSize(32,32), IPL_DEPTH_32F, 1);
IplImage *dctimg = cvCreateImage(cvSize(32,32), IPL_DEPTH_32F, 1);
cvSplit(img, imgplane, NULL, NULL, NULL);
cvSmooth(imgplane, imgblurred, CV_GAUSSIAN, 3, 0, rho, rho);
cvResize(imgblurred, imgresized, CV_INTER_AREA);
cvConvertScale(imgresized, imgprime, 1, 0);
cvDCT(imgprime, dctimg, 0);
double minval, maxval, medianval;
cvSetImageROI(dctimg, cvRect(blk_idx,blk_idx,blk_idx+8,blk_idx+8));
cvMinMaxLoc(dctimg, &minval, &maxval, NULL, NULL, NULL);
medianval = (maxval+minval)/2;
uint64_t hashval = 0;
for (int i=1;i<=8;i++){
const float *row = (const float*)(dctimg->imageData + i*dctimg->widthStep);
for (int j=1;j<=8;j++, hashval <<= 1){
if (row[j] > medianval){
hashval |= 1;
}
}
}
*hash = hashval;
cvReleaseImage(&imgplane);
cvReleaseImage(&imgblurred);
cvReleaseImage(&imgresized);
cvReleaseImage(&imgprime);
cvReleaseImage(&dctimg);
return 0;
}
int framehash(void *data, int width, int height, int widthstep, float rho, uint64_t *hash , int blk_idx){
if (data == NULL || hash == NULL) return -1;
IplImage *img = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 1);
if (img == NULL) return -1;
cvSetData(img, data, widthstep);
int rc = _framehash(img, rho, hash);
cvReleaseImageHeader(&img);
return rc;
}