2 * filter_ebur128.c -- normalize audio according to EBU R128
3 * Copyright (C) 2014 Brian Matherly <pez4brian@yahoo.com>
4 * Author: Brian Matherly <pez4brian@yahoo.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt.h>
25 #include "ebur128/ebur128.h"
27 #define MAX_RESULT_SIZE 512
43 analyze_data* analyze;
45 mlt_position last_position;
48 static void destroy_analyze_data( mlt_filter filter )
50 private_data* private = (private_data*)filter->child;
51 ebur128_destroy( &private->analyze->state );
52 free( private->analyze );
53 private->analyze = NULL;
56 static void init_analyze_data( mlt_filter filter, int channels, int samplerate )
58 private_data* private = (private_data*)filter->child;
59 private->analyze = (analyze_data*)calloc( 1, sizeof(analyze_data) );
60 private->analyze->state = ebur128_init( (unsigned int)channels, (unsigned long)samplerate, EBUR128_MODE_I | EBUR128_MODE_LRA | EBUR128_MODE_SAMPLE_PEAK );
61 private->last_position = 0;
64 static void destroy_apply_data( mlt_filter filter )
66 private_data* private = (private_data*)filter->child;
67 free( private->apply );
68 private->apply = NULL;
71 static void init_apply_data( mlt_filter filter )
73 private_data* private = (private_data*)filter->child;
74 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
75 char* results = mlt_properties_get( properties, "results" );
78 private->apply = (apply_data*)calloc( 1, sizeof(apply_data) );
80 scan_return = sscanf( results,"L: %lf\tR: %lf\tP %lf", &private->apply->in_loudness, &private->apply->in_range, &private->apply->in_peak );
81 if( scan_return != 3 )
83 mlt_log_error( MLT_FILTER_SERVICE( filter ), "Unable to load results: %s\n", results );
84 destroy_apply_data( filter );
89 mlt_log_info( MLT_FILTER_SERVICE( filter ), "Loaded Results: L: %lf\tR: %lf\tP %lf\n", private->apply->in_loudness, private->apply->in_range, private->apply->in_peak );
93 static void analyze( mlt_filter filter, mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
95 private_data* private = (private_data*)filter->child;
96 mlt_position pos = mlt_filter_get_position( filter, frame );
98 // If any frames are skipped, analysis data will be incomplete.
99 if( private->analyze && pos != private->last_position + 1 )
101 mlt_log_error( MLT_FILTER_SERVICE(filter), "Analysis Failed: Bad frame sequence\n" );
102 destroy_analyze_data( filter );
106 if( !private->analyze && pos == 0 )
108 init_analyze_data( filter, *channels, *frequency );
111 if( private->analyze )
113 ebur128_add_frames_float( private->analyze->state, *buffer, *samples );
115 if ( pos + 1 == mlt_filter_get_length2( filter, frame ) )
117 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
118 double loudness = 0.0;
120 double tmpPeak = 0.0;
123 char result[MAX_RESULT_SIZE];
124 ebur128_loudness_global( private->analyze->state, &loudness );
125 ebur128_loudness_range( private->analyze->state, &range );
127 for ( i = 0; i < *channels; i++ )
129 ebur128_sample_peak( private->analyze->state, i, &tmpPeak );
136 snprintf( result, MAX_RESULT_SIZE, "L: %lf\tR: %lf\tP %lf", loudness, range, peak );
137 result[ MAX_RESULT_SIZE - 1 ] = '\0';
138 mlt_log_info( MLT_FILTER_SERVICE( filter ), "Stored results: %s", result );
139 mlt_properties_set( properties, "results", result );
140 destroy_analyze_data( filter );
143 private->last_position = pos;
147 static void apply( mlt_filter filter, mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
149 private_data* private = (private_data*)filter->child;
150 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
153 if( !private->apply )
155 init_apply_data( filter );
160 double target_db = mlt_properties_get_double( properties, "program" );
161 double delta_db = target_db - private->apply->in_loudness;
162 double coeff = delta_db > -90.0f ? powf(10.0f, delta_db * 0.05f) : 0.0f;
164 int count = *samples * *channels;
176 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
178 mlt_filter filter = mlt_frame_pop_audio( frame );
179 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
181 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
183 // Get the producer's audio
184 *format = mlt_audio_f32le;
185 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
187 char* results = mlt_properties_get( properties, "results" );
188 if( results && strcmp( results, "" ) )
190 apply( filter, frame, buffer, format, frequency, channels, samples );
194 analyze( filter, frame, buffer, format, frequency, channels, samples );
197 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
202 /** Filter processing.
205 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
207 mlt_frame_push_audio( frame, filter );
208 mlt_frame_push_audio( frame, filter_get_audio );
212 /** Destructor for the filter.
215 static void filter_close( mlt_filter filter )
217 private_data* private = (private_data*)filter->child;
221 if ( private->analyze )
223 destroy_analyze_data( filter );
227 filter->child = NULL;
228 filter->close = NULL;
229 filter->parent.close = NULL;
230 mlt_service_close( &filter->parent );
233 /** Constructor for the filter.
236 mlt_filter filter_loudness_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
238 mlt_filter filter = mlt_filter_new( );
239 private_data* data = (private_data*)calloc( 1, sizeof(private_data) );
241 if ( filter && data )
243 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
244 mlt_properties_set( properties, "program", "-23.0" );
246 data->analyze = NULL;
248 filter->close = filter_close;
249 filter->process = filter_process;
250 filter->child = data;
256 mlt_filter_close( filter );