You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
6.1KB

  1. /*
  2. * Chromaprint fingerprinting muxer
  3. * Copyright (c) 2015 Rodger Combs
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. #include "libavutil/opt.h"
  23. #include "libavcodec/internal.h"
  24. #include <chromaprint.h>
  25. #define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
  26. CHROMAPRINT_VERSION_MINOR, \
  27. CHROMAPRINT_VERSION_PATCH)
  28. typedef enum FingerprintFormat {
  29. FINGERPRINT_RAW,
  30. FINGERPRINT_COMPRESSED,
  31. FINGERPRINT_BASE64,
  32. } FingerprintFormat;
  33. typedef struct ChromaprintMuxContext {
  34. const AVClass *class;
  35. int silence_threshold;
  36. int algorithm;
  37. FingerprintFormat fp_format;
  38. ChromaprintContext ctx;
  39. } ChromaprintMuxContext;
  40. static void cleanup(ChromaprintMuxContext *cpr)
  41. {
  42. if (cpr->ctx) {
  43. avpriv_lock_avformat();
  44. chromaprint_free(cpr->ctx);
  45. avpriv_unlock_avformat();
  46. }
  47. }
  48. static int write_header(AVFormatContext *s)
  49. {
  50. ChromaprintMuxContext *cpr = s->priv_data;
  51. AVStream *st;
  52. avpriv_lock_avformat();
  53. cpr->ctx = chromaprint_new(cpr->algorithm);
  54. avpriv_unlock_avformat();
  55. if (!cpr->ctx) {
  56. av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
  57. return AVERROR(ENOMEM);
  58. }
  59. if (cpr->silence_threshold != -1) {
  60. #if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
  61. if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
  62. av_log(s, AV_LOG_ERROR, "Failed to set silence threshold.\n");
  63. goto fail;
  64. }
  65. #else
  66. av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
  67. "version 0.7.0 or later.\n");
  68. goto fail;
  69. #endif
  70. }
  71. if (s->nb_streams != 1) {
  72. av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
  73. goto fail;
  74. }
  75. st = s->streams[0];
  76. if (st->codec->channels > 2) {
  77. av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
  78. goto fail;
  79. }
  80. if (st->codec->sample_rate < 1000) {
  81. av_log(s, AV_LOG_ERROR, "Sampling rate must be at least 1000\n");
  82. goto fail;
  83. }
  84. if (!chromaprint_start(cpr->ctx, st->codec->sample_rate, st->codec->channels)) {
  85. av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
  86. goto fail;
  87. }
  88. return 0;
  89. fail:
  90. cleanup(cpr);
  91. return AVERROR(EINVAL);
  92. }
  93. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  94. {
  95. ChromaprintMuxContext *cpr = s->priv_data;
  96. return chromaprint_feed(cpr->ctx, pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
  97. }
  98. static int write_trailer(AVFormatContext *s)
  99. {
  100. ChromaprintMuxContext *cpr = s->priv_data;
  101. AVIOContext *pb = s->pb;
  102. void *fp = NULL, *enc_fp = NULL;
  103. int size, enc_size, ret = AVERROR(EINVAL);
  104. if (!chromaprint_finish(cpr->ctx)) {
  105. av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
  106. goto fail;
  107. }
  108. if (!chromaprint_get_raw_fingerprint(cpr->ctx, &fp, &size)) {
  109. av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
  110. goto fail;
  111. }
  112. switch (cpr->fp_format) {
  113. case FINGERPRINT_RAW:
  114. avio_write(pb, fp, size);
  115. break;
  116. case FINGERPRINT_COMPRESSED:
  117. case FINGERPRINT_BASE64:
  118. if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
  119. cpr->fp_format == FINGERPRINT_BASE64)) {
  120. av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
  121. goto fail;
  122. }
  123. avio_write(pb, enc_fp, enc_size);
  124. break;
  125. }
  126. ret = 0;
  127. fail:
  128. if (fp)
  129. chromaprint_dealloc(fp);
  130. if (enc_fp)
  131. chromaprint_dealloc(enc_fp);
  132. cleanup(cpr);
  133. return ret;
  134. }
  135. #define OFFSET(x) offsetof(ChromaprintMuxContext, x)
  136. #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
  137. static const AVOption options[] = {
  138. { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
  139. { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
  140. { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS },
  141. { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
  142. { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
  143. { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
  144. { NULL },
  145. };
  146. static const AVClass chromaprint_class = {
  147. .class_name = "chromaprint muxer",
  148. .item_name = av_default_item_name,
  149. .option = options,
  150. .version = LIBAVUTIL_VERSION_INT,
  151. };
  152. AVOutputFormat ff_chromaprint_muxer = {
  153. .name = "chromaprint",
  154. .long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
  155. .priv_data_size = sizeof(ChromaprintMuxContext),
  156. .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  157. .write_header = write_header,
  158. .write_packet = write_packet,
  159. .write_trailer = write_trailer,
  160. .flags = AVFMT_NOTIMESTAMPS,
  161. .priv_class = &chromaprint_class,
  162. };