FFmpeg
audiomatch.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23 #include <inttypes.h>
24 
25 #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
26 #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
27 #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
28 
29 static int64_t fsize(FILE *f) {
30  int64_t end, pos = ftell(f);
31  fseek(f, 0, SEEK_END);
32  end = ftell(f);
33  fseek(f, pos, SEEK_SET);
34  return end;
35 }
36 
37 int main(int argc, char **argv) {
38  FILE *f[2];
39  int i, pos;
40  int siglen, datlen;
41  int bestpos = 0;
42  const double eps = 0.161;
43  double bestc = 0;
44  double sigamp = 0;
45  int16_t *signal, *data;
46  int maxshift = 16384;
47 
48  if (argc < 3) {
49  printf("audiomatch <testfile> <reffile>\n");
50  printf("WAV headers are skipped automatically.\n");
51  return 1;
52  }
53 
54  f[0] = fopen(argv[1], "rb");
55  f[1] = fopen(argv[2], "rb");
56  if (!f[0] || !f[1]) {
57  fprintf(stderr, "Could not open input files.\n");
58  return 1;
59  }
60 
61  for (i = 0; i < 2; i++) {
62  uint8_t p[100];
63  if (fread(p, 1, 12, f[i]) != 12)
64  return 1;
65  if (!memcmp(p, "RIFF", 4) &&
66  !memcmp(p + 8, "WAVE", 4)) {
67  if (fread(p, 1, 8, f[i]) != 8)
68  return 1;
69  while (memcmp(p, "data", 4)) {
70  int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
71  fseek(f[i], s, SEEK_CUR);
72  if (fread(p, 1, 8, f[i]) != 8)
73  return 1;
74  }
75  } else {
76  fseek(f[i], -12, SEEK_CUR);
77  }
78  }
79 
80  datlen = fsize(f[0]) - ftell(f[0]);
81  siglen = fsize(f[1]) - ftell(f[1]);
82  data = malloc(datlen * sizeof(*data));
83  signal = malloc(siglen * sizeof(*signal));
84 
85  if (fread(data , 1, datlen, f[0]) != datlen)
86  goto read_fail;
87  if (fread(signal, 1, siglen, f[1]) != siglen)
88  goto read_fail;
89  datlen /= 2;
90  siglen /= 2;
91 
92  for (i = 0; i < siglen; i++) {
93  signal[i] = ((uint8_t*)(signal + i))[0] + 256*((uint8_t*)(signal + i))[1];
94  sigamp += signal[i] * signal[i];
95  }
96  for (i = 0; i < datlen; i++)
97  data[i] = ((uint8_t*)(data + i))[0] + 256*((uint8_t*)(data + i))[1];
98 
99  for (pos = 0; pos < maxshift; pos = pos < 0 ? -pos: -pos-1) {
100  int64_t c = 0;
101  int testlen = FFMIN(siglen, datlen-pos);
102  for (i = FFMAX(0, -pos); i < testlen; i++) {
103  int j = pos + i;
104  c += signal[i] * data[j];
105  }
106  if (FFABS(c) > sigamp * 0.94)
107  maxshift = FFMIN(maxshift, FFABS(pos)+32);
108  if (FFABS(c) > FFABS(bestc)) {
109  bestc = c;
110  bestpos = pos;
111  }
112  }
113  printf("presig: %d postsig:%d lenerr:%d\n", bestpos, datlen - siglen - bestpos, datlen - siglen);
114 
115  sigamp = bestc / sigamp;
116  if (fabs(1.0 - sigamp) > eps) {
117  printf("c: |1.0 - %.4f| > %.3f\n", sigamp, eps);
118  goto read_fail;
119  }
120  free(data);
121  free(signal);
122  return 0;
123 
124 read_fail:
125  free(data);
126  free(signal);
127  return 1;
128 }
FFMIN
#define FFMIN(a, b)
Definition: audiomatch.c:25
printf
__device__ int printf(const char *,...)
int64_t
long long int64_t
Definition: coverity.c:34
FFMAX
#define FFMAX(a, b)
Definition: audiomatch.c:26
data
const char data[16]
Definition: mxf.c:149
main
int main(int argc, char **argv)
Definition: audiomatch.c:37
s
#define s(width, name)
Definition: cbs_vp9.c:198
fsize
static int64_t fsize(FILE *f)
Definition: audiomatch.c:29
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
f
f
Definition: af_crystalizer.c:122
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
FFABS
#define FFABS(a)
Definition: audiomatch.c:27
pos
unsigned int pos
Definition: spdifenc.c:414
Windows::Graphics::DirectX::Direct3D11::p
IDirect3DDxgiInterfaceAccess _COM_Outptr_ void ** p
Definition: vsrc_gfxcapture_winrt.hpp:53