1 /*
2  * filter_audiomap.c -- remap audio channels
3  * Copyright (C) 2015 Meltytech, LLC
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19 
20 #include <framework/mlt_filter.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_log.h>
23 
24 #include <string.h>
25 #include <stdlib.h>
26 
27 #define MAX_CHANNELS 32
28 
filter_get_audio(mlt_frame frame,void ** buffer,mlt_audio_format * format,int * frequency,int * channels,int * samples)29 static int filter_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
30 {
31 	char prop_name[32], *prop_val;
32 	int i, j, l, m[MAX_CHANNELS];
33 
34 	mlt_filter filter = mlt_frame_pop_audio(frame);
35 
36 	// Get the producer's audio
37 	int error = mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
38 	if ( error ) return error;
39 
40 	mlt_properties properties = MLT_FILTER_PROPERTIES(filter);
41 
42 	/* find samples length */
43 	int len = mlt_audio_format_size( *format, 1, 1 );
44 
45 	/* pcm samples buffer */
46 	uint8_t *pcm = *buffer;
47 
48 	/* build matrix */
49 	for ( i = 0; i < MAX_CHANNELS; i++ )
50 	{
51 		m[i] = i;
52 
53 		snprintf( prop_name, sizeof(prop_name), "%d", i );
54 		if ( ( prop_val = mlt_properties_get( properties, prop_name ) ) )
55 		{
56 			j = atoi( prop_val );
57 			if( j >= 0 && j < MAX_CHANNELS )
58 				m[i] = j;
59 		}
60 	}
61 
62 	/* process samples */
63 	for ( i = 0; i < *samples; i++ )
64 	{
65 		uint8_t tmp[MAX_CHANNELS * 4];
66 
67 		for ( j = 0; j < MAX_CHANNELS && j < *channels; j++ )
68 			for(l = 0; l < len; l++)
69 				tmp[j * len + l] = pcm[m[j] * len + l];
70 
71 		for ( j = 0; j < MAX_CHANNELS && j < *channels; j++ )
72 			for ( l = 0; l < len; l++ )
73 				pcm[j * len + l] = tmp[j * len + l];
74 
75 		pcm += len * (*channels);
76 	}
77 
78 	return 0;
79 }
80 
81 /** Filter processing.
82 */
83 
filter_process(mlt_filter filter,mlt_frame frame)84 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
85 {
86 	mlt_frame_push_audio( frame, filter );
87 	mlt_frame_push_audio( frame, filter_get_audio );
88 	return frame;
89 }
90 
91 /** Constructor for the filter.
92 */
93 
filter_audiomap_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)94 mlt_filter filter_audiomap_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
95 {
96 	mlt_filter filter = mlt_filter_new();
97 	if ( filter )
98 		filter->process = filter_process;
99 	return filter;
100 }
101