Apply FreeBSD fixes part of ldflags_order patch from Alberto Villa.
[melted] / src / modules / avformat / filter_avresample.c
1 /*
2 * filter_avresample.c -- adjust audio sample frequency
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 // ffmpeg Header files
29 #include <avformat.h>
30
31 /** Get the audio.
32 */
33
34 static int resample_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
35 {
36 // Get the properties of the frame
37 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
38
39 // Get the filter service
40 mlt_filter filter = mlt_frame_pop_audio( frame );
41
42 // Get the filter properties
43 mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
44
45 // Get the resample information
46 int output_rate = mlt_properties_get_int( filter_properties, "frequency" );
47 int16_t *sample_buffer = mlt_properties_get_data( filter_properties, "buffer", NULL );
48
49 // Obtain the resample context if it exists
50 ReSampleContext *resample = mlt_properties_get_data( filter_properties, "audio_resample", NULL );
51
52 // Used to return number of channels in the source
53 int channels_avail = *channels;
54
55 // Loop variable
56 int i;
57
58 // If no resample frequency is specified, default to requested value
59 if ( output_rate == 0 )
60 output_rate = *frequency;
61
62 // Get the producer's audio
63 mlt_frame_get_audio( frame, buffer, format, frequency, &channels_avail, samples );
64
65 // Duplicate channels as necessary
66 if ( channels_avail < *channels )
67 {
68 int size = *channels * *samples * sizeof( int16_t );
69 int16_t *new_buffer = mlt_pool_alloc( size );
70 int j, k = 0;
71
72 // Duplicate the existing channels
73 for ( i = 0; i < *samples; i++ )
74 {
75 for ( j = 0; j < *channels; j++ )
76 {
77 new_buffer[ ( i * *channels ) + j ] = (*buffer)[ ( i * channels_avail ) + k ];
78 k = ( k + 1 ) % channels_avail;
79 }
80 }
81
82 // Update the audio buffer now - destroys the old
83 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
84
85 *buffer = new_buffer;
86 }
87 else if ( channels_avail == 6 && *channels == 2 )
88 {
89 // Nasty hack for ac3 5.1 audio - may be a cause of failure?
90 int size = *channels * *samples * sizeof( int16_t );
91 int16_t *new_buffer = mlt_pool_alloc( size );
92
93 // Drop all but the first *channels
94 for ( i = 0; i < *samples; i++ )
95 {
96 new_buffer[ ( i * *channels ) + 0 ] = (*buffer)[ ( i * channels_avail ) + 2 ];
97 new_buffer[ ( i * *channels ) + 1 ] = (*buffer)[ ( i * channels_avail ) + 3 ];
98 }
99
100 // Update the audio buffer now - destroys the old
101 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
102
103 *buffer = new_buffer;
104 }
105
106 // Return now if no work to do
107 if ( output_rate != *frequency )
108 {
109 // Will store number of samples created
110 int used = 0;
111
112 // Create a resampler if nececessary
113 if ( resample == NULL || *frequency != mlt_properties_get_int( filter_properties, "last_frequency" ) )
114 {
115 // Create the resampler
116 resample = audio_resample_init( *channels, *channels, output_rate, *frequency );
117
118 // And store it on properties
119 mlt_properties_set_data( filter_properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
120
121 // And remember what it was created for
122 mlt_properties_set_int( filter_properties, "last_frequency", *frequency );
123 }
124
125 // Resample the audio
126 used = audio_resample( resample, sample_buffer, *buffer, *samples );
127
128 // Resize if necessary
129 if ( used > *samples )
130 {
131 *buffer = mlt_pool_realloc( *buffer, *samples * *channels * sizeof( int16_t ) );
132 mlt_properties_set_data( properties, "audio", *buffer, *channels * used * sizeof( int16_t ), mlt_pool_release, NULL );
133 }
134
135 // Copy samples
136 memcpy( *buffer, sample_buffer, *channels * used * sizeof( int16_t ) );
137
138 // Update output variables
139 *samples = used;
140 *frequency = output_rate;
141 }
142
143 return 0;
144 }
145
146 /** Filter processing.
147 */
148
149 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
150 {
151 // Only call this if we have a means to get audio
152 if ( mlt_frame_is_test_audio( frame ) == 0 )
153 {
154 // Push the filter on to the stack
155 mlt_frame_push_audio( frame, this );
156
157 // Assign our get_audio method
158 mlt_frame_push_audio( frame, resample_get_audio );
159 }
160
161 return frame;
162 }
163
164 /** Constructor for the filter.
165 */
166
167 mlt_filter filter_avresample_init( char *arg )
168 {
169 // Create a filter
170 mlt_filter this = mlt_filter_new( );
171
172 // Initialise if successful
173 if ( this != NULL )
174 {
175 // Calculate size of the buffer
176 int size = AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t );
177
178 // Allocate the buffer
179 int16_t *buffer = mlt_pool_alloc( size );
180
181 // Assign the process method
182 this->process = filter_process;
183
184 // Deal with argument
185 if ( arg != NULL )
186 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "frequency", arg );
187
188 // Default to 2 channel output
189 mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "channels", 2 );
190
191 // Store the buffer
192 mlt_properties_set_data( MLT_FILTER_PROPERTIES( this ), "buffer", buffer, size, mlt_pool_release, NULL );
193 }
194
195 return this;
196 }