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.

192 lines
6.2KB

  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 "internal.h"
  23. #include "libavutil/opt.h"
  24. #include "libavcodec/internal.h"
  25. #include <chromaprint.h>
  26. #define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
  27. CHROMAPRINT_VERSION_MINOR, \
  28. CHROMAPRINT_VERSION_PATCH)
  29. typedef enum FingerprintFormat {
  30. FINGERPRINT_RAW,
  31. FINGERPRINT_COMPRESSED,
  32. FINGERPRINT_BASE64,
  33. } FingerprintFormat;
  34. typedef struct ChromaprintMuxContext {
  35. const AVClass *class;
  36. int silence_threshold;
  37. int algorithm;
  38. FingerprintFormat fp_format;
  39. #if CPR_VERSION_INT >= AV_VERSION_INT(1, 4, 0)
  40. ChromaprintContext *ctx;
  41. #else
  42. ChromaprintContext ctx;
  43. #endif
  44. } ChromaprintMuxContext;
  45. static void cleanup(ChromaprintMuxContext *cpr)
  46. {
  47. if (cpr->ctx) {
  48. ff_lock_avformat();
  49. chromaprint_free(cpr->ctx);
  50. ff_unlock_avformat();
  51. }
  52. }
  53. static int write_header(AVFormatContext *s)
  54. {
  55. ChromaprintMuxContext *cpr = s->priv_data;
  56. AVStream *st;
  57. ff_lock_avformat();
  58. cpr->ctx = chromaprint_new(cpr->algorithm);
  59. ff_unlock_avformat();
  60. if (!cpr->ctx) {
  61. av_log(s, AV_LOG_ERROR, "Failed to create chromaprint context.\n");
  62. return AVERROR(ENOMEM);
  63. }
  64. if (cpr->silence_threshold != -1) {
  65. #if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
  66. if (!chromaprint_set_option(cpr->ctx, "silence_threshold", cpr->silence_threshold)) {
  67. av_log(s, AV_LOG_ERROR, "Failed to set silence threshold.\n");
  68. goto fail;
  69. }
  70. #else
  71. av_log(s, AV_LOG_ERROR, "Setting the silence threshold requires Chromaprint "
  72. "version 0.7.0 or later.\n");
  73. goto fail;
  74. #endif
  75. }
  76. if (s->nb_streams != 1) {
  77. av_log(s, AV_LOG_ERROR, "Only one stream is supported\n");
  78. goto fail;
  79. }
  80. st = s->streams[0];
  81. if (st->codecpar->channels > 2) {
  82. av_log(s, AV_LOG_ERROR, "Only up to 2 channels are supported\n");
  83. goto fail;
  84. }
  85. if (st->codecpar->sample_rate < 1000) {
  86. av_log(s, AV_LOG_ERROR, "Sampling rate must be at least 1000\n");
  87. goto fail;
  88. }
  89. if (!chromaprint_start(cpr->ctx, st->codecpar->sample_rate, st->codecpar->channels)) {
  90. av_log(s, AV_LOG_ERROR, "Failed to start chromaprint\n");
  91. goto fail;
  92. }
  93. return 0;
  94. fail:
  95. cleanup(cpr);
  96. return AVERROR(EINVAL);
  97. }
  98. static int write_packet(AVFormatContext *s, AVPacket *pkt)
  99. {
  100. ChromaprintMuxContext *cpr = s->priv_data;
  101. return chromaprint_feed(cpr->ctx, pkt->data, pkt->size / 2) ? 0 : AVERROR(EINVAL);
  102. }
  103. static int write_trailer(AVFormatContext *s)
  104. {
  105. ChromaprintMuxContext *cpr = s->priv_data;
  106. AVIOContext *pb = s->pb;
  107. void *fp = NULL, *enc_fp = NULL;
  108. int size, enc_size, ret = AVERROR(EINVAL);
  109. if (!chromaprint_finish(cpr->ctx)) {
  110. av_log(s, AV_LOG_ERROR, "Failed to generate fingerprint\n");
  111. goto fail;
  112. }
  113. if (!chromaprint_get_raw_fingerprint(cpr->ctx, &fp, &size)) {
  114. av_log(s, AV_LOG_ERROR, "Failed to retrieve fingerprint\n");
  115. goto fail;
  116. }
  117. switch (cpr->fp_format) {
  118. case FINGERPRINT_RAW:
  119. avio_write(pb, fp, size);
  120. break;
  121. case FINGERPRINT_COMPRESSED:
  122. case FINGERPRINT_BASE64:
  123. if (!chromaprint_encode_fingerprint(fp, size, cpr->algorithm, &enc_fp, &enc_size,
  124. cpr->fp_format == FINGERPRINT_BASE64)) {
  125. av_log(s, AV_LOG_ERROR, "Failed to encode fingerprint\n");
  126. goto fail;
  127. }
  128. avio_write(pb, enc_fp, enc_size);
  129. break;
  130. }
  131. ret = 0;
  132. fail:
  133. if (fp)
  134. chromaprint_dealloc(fp);
  135. if (enc_fp)
  136. chromaprint_dealloc(enc_fp);
  137. cleanup(cpr);
  138. return ret;
  139. }
  140. #define OFFSET(x) offsetof(ChromaprintMuxContext, x)
  141. #define FLAGS AV_OPT_FLAG_ENCODING_PARAM
  142. static const AVOption options[] = {
  143. { "silence_threshold", "threshold for detecting silence", OFFSET(silence_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 32767, FLAGS },
  144. { "algorithm", "version of the fingerprint algorithm", OFFSET(algorithm), AV_OPT_TYPE_INT, { .i64 = CHROMAPRINT_ALGORITHM_DEFAULT }, CHROMAPRINT_ALGORITHM_TEST1, INT_MAX, FLAGS },
  145. { "fp_format", "fingerprint format to write", OFFSET(fp_format), AV_OPT_TYPE_INT, { .i64 = FINGERPRINT_BASE64 }, FINGERPRINT_RAW, FINGERPRINT_BASE64, FLAGS },
  146. { "raw", "binary raw fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_RAW }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
  147. { "compressed", "binary compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_COMPRESSED }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
  148. { "base64", "Base64 compressed fingerprint", 0, AV_OPT_TYPE_CONST, {.i64 = FINGERPRINT_BASE64 }, INT_MIN, INT_MAX, FLAGS, "fp_format"},
  149. { NULL },
  150. };
  151. static const AVClass chromaprint_class = {
  152. .class_name = "chromaprint muxer",
  153. .item_name = av_default_item_name,
  154. .option = options,
  155. .version = LIBAVUTIL_VERSION_INT,
  156. };
  157. AVOutputFormat ff_chromaprint_muxer = {
  158. .name = "chromaprint",
  159. .long_name = NULL_IF_CONFIG_SMALL("Chromaprint"),
  160. .priv_data_size = sizeof(ChromaprintMuxContext),
  161. .audio_codec = AV_NE(AV_CODEC_ID_PCM_S16BE, AV_CODEC_ID_PCM_S16LE),
  162. .write_header = write_header,
  163. .write_packet = write_packet,
  164. .write_trailer = write_trailer,
  165. .flags = AVFMT_NOTIMESTAMPS,
  166. .priv_class = &chromaprint_class,
  167. };