2015-10-04 00:24:06 +02:00
/*
* Chromaprint fingerprinting muxer
2021-01-20 09:02:56 +02:00
* Copyright ( c ) 2015 rcombs
2015-10-04 00:24:06 +02: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"
2017-12-21 23:54:06 +02:00
# include "internal.h"
2023-01-27 16:06:00 +02:00
# include "mux.h"
2015-10-04 00:24:06 +02:00
# include "libavutil/opt.h"
# include <chromaprint.h>
# define CPR_VERSION_INT AV_VERSION_INT(CHROMAPRINT_VERSION_MAJOR, \
CHROMAPRINT_VERSION_MINOR , \
CHROMAPRINT_VERSION_PATCH )
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 22:07:59 +02:00
# if CPR_VERSION_INT >= AV_VERSION_INT(1, 4, 0)
ChromaprintContext * ctx ;
# else
2015-10-04 00:24:06 +02:00
ChromaprintContext ctx ;
2016-12-06 22:07:59 +02:00
# endif
2015-10-04 00:24:06 +02:00
} ChromaprintMuxContext ;
2021-09-22 01:52:23 +02:00
static void deinit ( AVFormatContext * s )
2015-10-04 00:24:06 +02:00
{
2021-09-22 01:52:23 +02:00
ChromaprintMuxContext * const cpr = s - > priv_data ;
2015-10-04 00:24:06 +02:00
if ( cpr - > ctx ) {
2017-12-21 23:54:06 +02:00
ff_lock_avformat ( ) ;
2015-10-04 00:24:06 +02:00
chromaprint_free ( cpr - > ctx ) ;
2017-12-21 23:54:06 +02:00
ff_unlock_avformat ( ) ;
2015-10-04 00:24:06 +02:00
}
}
static int write_header ( AVFormatContext * s )
{
ChromaprintMuxContext * cpr = s - > priv_data ;
AVStream * st ;
2017-12-21 23:54:06 +02:00
ff_lock_avformat ( ) ;
2015-10-04 00:24:06 +02:00
cpr - > ctx = chromaprint_new ( cpr - > algorithm ) ;
2017-12-21 23:54:06 +02:00
ff_unlock_avformat ( ) ;
2015-10-04 00:24:06 +02:00
if ( ! cpr - > ctx ) {
av_log ( s , AV_LOG_ERROR , " Failed to create chromaprint context. \n " ) ;
2021-09-22 06:57:44 +02:00
return AVERROR_EXTERNAL ;
2015-10-04 00:24:06 +02: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 07:49:48 +02:00
av_log ( s , AV_LOG_ERROR , " Failed to set silence threshold. Setting silence_threshold requires -algorithm 3 option. \n " ) ;
2021-09-22 06:57:44 +02:00
return AVERROR_EXTERNAL ;
2015-10-04 00:24:06 +02:00
}
# else
av_log ( s , AV_LOG_ERROR , " Setting the silence threshold requires Chromaprint "
" version 0.7.0 or later. \n " ) ;
2021-09-22 06:57:44 +02:00
return AVERROR ( ENOSYS ) ;
2015-10-04 00:24:06 +02:00
# endif
}
if ( s - > nb_streams ! = 1 ) {
av_log ( s , AV_LOG_ERROR , " Only one stream is supported \n " ) ;
2021-09-22 06:57:44 +02:00
return AVERROR ( EINVAL ) ;
2015-10-04 00:24:06 +02:00
}
st = s - > streams [ 0 ] ;
2022-01-17 17:23:20 +02:00
if ( st - > codecpar - > ch_layout . nb_channels > 2 ) {
2015-10-04 00:24:06 +02:00
av_log ( s , AV_LOG_ERROR , " Only up to 2 channels are supported \n " ) ;
2021-09-22 06:57:44 +02:00
return AVERROR ( EINVAL ) ;
2015-10-04 00:24:06 +02:00
}
2016-04-10 21:58:15 +02:00
if ( st - > codecpar - > sample_rate < 1000 ) {
2015-10-04 00:24:06 +02:00
av_log ( s , AV_LOG_ERROR , " Sampling rate must be at least 1000 \n " ) ;
2021-09-22 06:57:44 +02:00
return AVERROR ( EINVAL ) ;
2015-10-04 00:24:06 +02:00
}
2022-01-17 17:23:20 +02:00
if ( ! chromaprint_start ( cpr - > ctx , st - > codecpar - > sample_rate , st - > codecpar - > ch_layout . nb_channels ) ) {
2015-10-04 00:24:06 +02:00
av_log ( s , AV_LOG_ERROR , " Failed to start chromaprint \n " ) ;
2021-09-22 06:57:44 +02:00
return AVERROR_EXTERNAL ;
2015-10-04 00:24:06 +02:00
}
return 0 ;
}
static int write_packet ( AVFormatContext * s , AVPacket * pkt )
{
ChromaprintMuxContext * cpr = s - > priv_data ;
2019-08-13 12:42:27 +02:00
return chromaprint_feed ( cpr - > ctx , ( const int16_t * ) pkt - > data , pkt - > size / 2 ) ? 0 : AVERROR ( EINVAL ) ;
2015-10-04 00:24:06 +02:00
}
static int write_trailer ( AVFormatContext * s )
{
ChromaprintMuxContext * cpr = s - > priv_data ;
AVIOContext * pb = s - > pb ;
2019-08-13 12:42:27 +02:00
void * fp = NULL ;
char * enc_fp = NULL ;
2021-09-22 06:57:44 +02:00
int size , enc_size , ret = AVERROR_EXTERNAL ;
2015-10-04 00:24:06 +02:00
if ( ! chromaprint_finish ( cpr - > ctx ) ) {
av_log ( s , AV_LOG_ERROR , " Failed to generate fingerprint \n " ) ;
goto fail ;
}
2019-08-13 12:42:27 +02:00
if ( ! chromaprint_get_raw_fingerprint ( cpr - > ctx , ( uint32_t * * ) & fp , & size ) ) {
2015-10-04 00:24:06 +02:00
av_log ( s , AV_LOG_ERROR , " Failed to retrieve fingerprint \n " ) ;
goto fail ;
}
switch ( cpr - > fp_format ) {
case FINGERPRINT_RAW :
2019-10-06 07:49:49 +02:00
avio_write ( pb , fp , size * 4 ) ; //fp points to array of uint32_t
2015-10-04 00:24:06 +02: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 } ,
2019-10-06 07:49:46 +02:00
{ " fp_format " , " fingerprint format to write " , OFFSET ( fp_format ) , AV_OPT_TYPE_INT , { . i64 = FINGERPRINT_BASE64 } , FINGERPRINT_RAW , FINGERPRINT_BASE64 , FLAGS , " fp_format " } ,
2015-10-04 00:24:06 +02:00
{ " raw " , " binary raw fingerprint " , 0 , AV_OPT_TYPE_CONST , { . i64 = FINGERPRINT_RAW } , INT_MIN , INT_MAX , FLAGS , " fp_format " } ,
{ " compressed " , " binary compressed fingerprint " , 0 , AV_OPT_TYPE_CONST , { . i64 = FINGERPRINT_COMPRESSED } , INT_MIN , INT_MAX , FLAGS , " fp_format " } ,
{ " base64 " , " Base64 compressed fingerprint " , 0 , AV_OPT_TYPE_CONST , { . i64 = FINGERPRINT_BASE64 } , INT_MIN , INT_MAX , FLAGS , " fp_format " } ,
{ NULL } ,
} ;
static const AVClass chromaprint_class = {
. class_name = " chromaprint muxer " ,
. item_name = av_default_item_name ,
. option = options ,
. version = LIBAVUTIL_VERSION_INT ,
} ;
2023-01-27 16:06:00 +02:00
const FFOutputFormat ff_chromaprint_muxer = {
. p . name = " chromaprint " ,
. p . long_name = NULL_IF_CONFIG_SMALL ( " Chromaprint " ) ,
2015-10-04 00:24:06 +02:00
. priv_data_size = sizeof ( ChromaprintMuxContext ) ,
2023-01-27 16:06:00 +02:00
. p . audio_codec = AV_NE ( AV_CODEC_ID_PCM_S16BE , AV_CODEC_ID_PCM_S16LE ) ,
2015-10-04 00:24:06 +02:00
. write_header = write_header ,
. write_packet = write_packet ,
. write_trailer = write_trailer ,
2021-09-22 01:52:23 +02:00
. deinit = deinit ,
2023-01-27 16:06:00 +02:00
. p . flags = AVFMT_NOTIMESTAMPS ,
. p . priv_class = & chromaprint_class ,
2015-10-04 00:24:06 +02:00
} ;