2015-10-03 22:24:06 +00:00
/*
* Chromaprint fingerprinting muxer
2021-01-20 07:02:56 +00:00
* Copyright ( c ) 2015 rcombs
2015-10-03 22:24:06 +00:00
*
* This file is part of FFmpeg .
*
* FFmpeg is free software ; you can redistribute it and / or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation ; either
* version 2.1 of the License , or ( at your option ) any later version .
*
* FFmpeg is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
* Lesser General Public License for more details .
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
# include "avformat.h"
2023-01-27 14:06:00 +00:00
# include "mux.h"
2015-10-03 22:24:06 +00:00
# include "libavutil/opt.h"
2024-05-17 12:53:33 +00:00
# include "libavutil/thread.h"
2015-10-03 22:24:06 +00:00
# include <chromaprint.h>
# define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
CHROMAPRINT_VERSION_MINOR , \
CHROMAPRINT_VERSION_PATCH )
2024-05-17 12:53:33 +00:00
static AVMutex chromaprint_mutex = AV_MUTEX_INITIALIZER ;
2015-10-03 22:24:06 +00:00
typedef enum FingerprintFormat {
FINGERPRINT_RAW ,
FINGERPRINT_COMPRESSED ,
FINGERPRINT_BASE64 ,
} FingerprintFormat ;
typedef struct ChromaprintMuxContext {
const AVClass * class ;
int silence_threshold ;
int algorithm ;
FingerprintFormat fp_format ;
2016-12-06 20:07:59 +00:00
# if CPR_VERSION_INT >= AV_VERSION_INT(1, 4, 0)
ChromaprintContext * ctx ;
# else
2015-10-03 22:24:06 +00:00
ChromaprintContext ctx ;
2016-12-06 20:07:59 +00:00
# endif
2015-10-03 22:24:06 +00:00
} ChromaprintMuxContext ;
2021-09-21 23:52:23 +00:00
static void deinit ( AVFormatContext * s )
2015-10-03 22:24:06 +00:00
{
2021-09-21 23:52:23 +00:00
ChromaprintMuxContext * const cpr = s - > priv_data ;
2015-10-03 22:24:06 +00:00
if ( cpr - > ctx ) {
2024-05-17 12:53:33 +00:00
ff_mutex_lock ( & chromaprint_mutex ) ;
2015-10-03 22:24:06 +00:00
chromaprint_free ( cpr - > ctx ) ;
2024-05-17 12:53:33 +00:00
ff_mutex_unlock ( & chromaprint_mutex ) ;
2015-10-03 22:24:06 +00:00
}
}
2024-03-19 23:51:30 +00:00
static av_cold int init ( AVFormatContext * s )
2015-10-03 22:24:06 +00:00
{
ChromaprintMuxContext * cpr = s - > priv_data ;
AVStream * st ;
2024-05-17 12:53:33 +00:00
ff_mutex_lock ( & chromaprint_mutex ) ;
2015-10-03 22:24:06 +00:00
cpr - > ctx = chromaprint_new ( cpr - > algorithm ) ;
2024-05-17 12:53:33 +00:00
ff_mutex_unlock ( & chromaprint_mutex ) ;
2015-10-03 22:24:06 +00:00
if ( ! cpr - > ctx ) {
av_log ( s , AV_LOG_ERROR , " Failed to create chromaprint context. \n " ) ;
2021-09-22 04:57:44 +00:00
return AVERROR_EXTERNAL ;
2015-10-03 22:24:06 +00:00
}
if ( cpr - > silence_threshold ! = - 1 ) {
# if CPR_VERSION_INT >= AV_VERSION_INT(0, 7, 0)
if ( ! chromaprint_set_option ( cpr - > ctx , " silence_threshold " , cpr - > silence_threshold ) ) {
2019-10-06 05:49:48 +00:00
av_log ( s , AV_LOG_ERROR , " Failed to set silence threshold. Setting silence_threshold requires -algorithm 3 option. \n " ) ;
2021-09-22 04:57:44 +00:00
return AVERROR_EXTERNAL ;
2015-10-03 22:24:06 +00:00
}
# else
av_log ( s , AV_LOG_ERROR , " Setting the silence threshold requires Chromaprint "
" version 0.7.0 or later. \n " ) ;
2021-09-22 04:57:44 +00:00
return AVERROR ( ENOSYS ) ;
2015-10-03 22:24:06 +00:00
# endif
}
st = s - > streams [ 0 ] ;
2022-01-17 15:23:20 +00:00
if ( st - > codecpar - > ch_layout . nb_channels > 2 ) {
2015-10-03 22:24:06 +00:00
av_log ( s , AV_LOG_ERROR , " Only up to 2 channels are supported \n " ) ;
2021-09-22 04:57:44 +00:00
return AVERROR ( EINVAL ) ;
2015-10-03 22:24:06 +00:00
}
2016-04-10 19:58:15 +00:00
if ( st - > codecpar - > sample_rate < 1000 ) {
2015-10-03 22:24:06 +00:00
av_log ( s , AV_LOG_ERROR , " Sampling rate must be at least 1000 \n " ) ;
2021-09-22 04:57:44 +00:00
return AVERROR ( EINVAL ) ;
2015-10-03 22:24:06 +00:00
}
2022-01-17 15:23:20 +00:00
if ( ! chromaprint_start ( cpr - > ctx , st - > codecpar - > sample_rate , st - > codecpar - > ch_layout . nb_channels ) ) {
2015-10-03 22:24:06 +00:00
av_log ( s , AV_LOG_ERROR , " Failed to start chromaprint \n " ) ;
2021-09-22 04:57:44 +00:00
return AVERROR_EXTERNAL ;
2015-10-03 22:24:06 +00:00
}
return 0 ;
}
static int write_packet ( AVFormatContext * s , AVPacket * pkt )
{
ChromaprintMuxContext * cpr = s - > priv_data ;
2019-08-13 10:42:27 +00:00
return chromaprint_feed ( cpr - > ctx , ( const int16_t * ) pkt - > data , pkt - > size / 2 ) ? 0 : AVERROR ( EINVAL ) ;
2015-10-03 22:24:06 +00:00
}
static int write_trailer ( AVFormatContext * s )
{
ChromaprintMuxContext * cpr = s - > priv_data ;
AVIOContext * pb = s - > pb ;
2019-08-13 10:42:27 +00:00
void * fp = NULL ;
char * enc_fp = NULL ;
2021-09-22 04:57:44 +00:00
int size , enc_size , ret = AVERROR_EXTERNAL ;
2015-10-03 22:24:06 +00:00
if ( ! chromaprint_finish ( cpr - > ctx ) ) {
av_log ( s , AV_LOG_ERROR , " Failed to generate fingerprint \n " ) ;
goto fail ;
}
2019-08-13 10:42:27 +00:00
if ( ! chromaprint_get_raw_fingerprint ( cpr - > ctx , ( uint32_t * * ) & fp , & size ) ) {
2015-10-03 22:24:06 +00:00
av_log ( s , AV_LOG_ERROR , " Failed to retrieve fingerprint \n " ) ;
goto fail ;
}
switch ( cpr - > fp_format ) {
case FINGERPRINT_RAW :
2019-10-06 05:49:49 +00:00
avio_write ( pb , fp , size * 4 ) ; //fp points to array of uint32_t
2015-10-03 22:24:06 +00:00
break ;
case FINGERPRINT_COMPRESSED :
case FINGERPRINT_BASE64 :
if ( ! chromaprint_encode_fingerprint ( fp , size , cpr - > algorithm , & enc_fp , & enc_size ,
cpr - > fp_format = = FINGERPRINT_BASE64 ) ) {
av_log ( s , AV_LOG_ERROR , " Failed to encode fingerprint \n " ) ;
goto fail ;
}
avio_write ( pb , enc_fp , enc_size ) ;
break ;
}
ret = 0 ;
fail :
if ( fp )
chromaprint_dealloc ( fp ) ;
if ( enc_fp )
chromaprint_dealloc ( enc_fp ) ;
return ret ;
}
# define OFFSET(x) offsetof(ChromaprintMuxContext, x)
# define FLAGS AV_OPT_FLAG_ENCODING_PARAM
static const AVOption options [ ] = {
{ " silence_threshold " , " threshold for detecting silence " , OFFSET ( silence_threshold ) , AV_OPT_TYPE_INT , { . i64 = - 1 } , - 1 , 32767 , FLAGS } ,
{ " algorithm " , " version of the fingerprint algorithm " , OFFSET ( algorithm ) , AV_OPT_TYPE_INT , { . i64 = CHROMAPRINT_ALGORITHM_DEFAULT } , CHROMAPRINT_ALGORITHM_TEST1 , INT_MAX , FLAGS } ,
2024-02-11 14:41:05 +00:00
{ " fp_format " , " fingerprint format to write " , OFFSET ( fp_format ) , AV_OPT_TYPE_INT , { . i64 = FINGERPRINT_BASE64 } , FINGERPRINT_RAW , FINGERPRINT_BASE64 , FLAGS , . unit = " fp_format " } ,
{ " raw " , " binary raw fingerprint " , 0 , AV_OPT_TYPE_CONST , { . i64 = FINGERPRINT_RAW } , INT_MIN , INT_MAX , FLAGS , . unit = " fp_format " } ,
{ " compressed " , " binary compressed fingerprint " , 0 , AV_OPT_TYPE_CONST , { . i64 = FINGERPRINT_COMPRESSED } , INT_MIN , INT_MAX , FLAGS , . unit = " fp_format " } ,
{ " base64 " , " Base64 compressed fingerprint " , 0 , AV_OPT_TYPE_CONST , { . i64 = FINGERPRINT_BASE64 } , INT_MIN , INT_MAX , FLAGS , . unit = " fp_format " } ,
2015-10-03 22:24:06 +00:00
{ NULL } ,
} ;
static const AVClass chromaprint_class = {
. class_name = " chromaprint muxer " ,
2024-01-19 12:33:28 +00:00
. item_name = av_default_item_name ,
2015-10-03 22:24:06 +00:00
. option = options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2023-01-27 14:06:00 +00:00
const FFOutputFormat ff_chromaprint_muxer = {
. p . name = " chromaprint " ,
. p . long_name = NULL_IF_CONFIG_SMALL ( " Chromaprint " ) ,
2015-10-03 22:24:06 +00:00
. priv_data_size = sizeof ( ChromaprintMuxContext ) ,
2023-01-27 14:06:00 +00:00
. p . audio_codec = AV_NE ( AV_CODEC_ID_PCM_S16BE , AV_CODEC_ID_PCM_S16LE ) ,
2024-03-19 20:40:54 +00:00
. p . video_codec = AV_CODEC_ID_NONE ,
. p . subtitle_codec = AV_CODEC_ID_NONE ,
2024-03-19 22:44:16 +00:00
. flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS ,
2024-03-19 23:51:30 +00:00
. init = init ,
2015-10-03 22:24:06 +00:00
. write_packet = write_packet ,
. write_trailer = write_trailer ,
2021-09-21 23:52:23 +00:00
. deinit = deinit ,
2023-01-27 14:06:00 +00:00
. p . flags = AVFMT_NOTIMESTAMPS ,
. p . priv_class = & chromaprint_class ,
2015-10-03 22:24:06 +00:00
} ;