audiomatch.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <inttypes.h>
  23. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  24. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  25. static int64_t fsize(FILE *f) {
  26. int64_t end, pos = ftell(f);
  27. fseek(f, 0, SEEK_END);
  28. end = ftell(f);
  29. fseek(f, pos, SEEK_SET);
  30. return end;
  31. }
  32. int main(int argc, char **argv) {
  33. FILE *f[2];
  34. int i, pos;
  35. int siglen, datlen;
  36. int bestpos = 0;
  37. double bestc = 0;
  38. double sigamp = 0;
  39. int16_t *signal, *data;
  40. int maxshift = 16384;
  41. if (argc < 3) {
  42. printf("audiomatch <testfile> <reffile>\n");
  43. printf("WAV headers are skipped automatically.\n");
  44. return 1;
  45. }
  46. f[0] = fopen(argv[1], "rb");
  47. f[1] = fopen(argv[2], "rb");
  48. if (!f[0] || !f[1]) {
  49. fprintf(stderr, "Could not open input files.\n");
  50. return 1;
  51. }
  52. for (i = 0; i < 2; i++) {
  53. uint8_t p[100];
  54. if (fread(p, 1, 12, f[i]) != 12)
  55. return 1;
  56. if (!memcmp(p, "RIFF", 4) &&
  57. !memcmp(p + 8, "WAVE", 4)) {
  58. if (fread(p, 1, 8, f[i]) != 8)
  59. return 1;
  60. while (memcmp(p, "data", 4)) {
  61. int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
  62. fseek(f[i], s, SEEK_CUR);
  63. if (fread(p, 1, 8, f[i]) != 8)
  64. return 1;
  65. }
  66. } else {
  67. fseek(f[i], -12, SEEK_CUR);
  68. }
  69. }
  70. datlen = fsize(f[0]) - ftell(f[0]);
  71. siglen = fsize(f[1]) - ftell(f[1]);
  72. data = malloc(datlen * sizeof(*data));
  73. signal = malloc(siglen * sizeof(*signal));
  74. if (fread(data , 1, datlen, f[0]) != datlen)
  75. return 1;
  76. if (fread(signal, 1, siglen, f[1]) != siglen)
  77. return 1;
  78. datlen /= 2;
  79. siglen /= 2;
  80. for (i = 0; i < siglen; i++) {
  81. signal[i] = ((uint8_t*)(signal + i))[0] + 256*((uint8_t*)(signal + i))[1];
  82. sigamp += signal[i] * signal[i];
  83. }
  84. for (i = 0; i < datlen; i++)
  85. data[i] = ((uint8_t*)(data + i))[0] + 256*((uint8_t*)(data + i))[1];
  86. for (pos = 0; pos < maxshift; pos = pos < 0 ? -pos: -pos-1) {
  87. int64_t c = 0;
  88. int testlen = FFMIN(siglen, datlen-pos);
  89. for (i = FFMAX(0, -pos); i < testlen; i++) {
  90. int j = pos + i;
  91. c += signal[i] * data[j];
  92. }
  93. if (fabs(c) > sigamp * 0.94)
  94. maxshift = FFMIN(maxshift, fabs(pos)+32);
  95. if (fabs(c) > fabs(bestc)) {
  96. bestc = c;
  97. bestpos = pos;
  98. }
  99. }
  100. printf("presig: %d postsig:%d c:%7.4f lenerr:%d\n", bestpos, datlen - siglen - bestpos, bestc / sigamp, datlen - siglen);
  101. return 0;
  102. }