]> git.sesse.net Git - mlt/blob - src/modules/avformat/filter_avresample.c
Rename 'this' in avformat module.
[mlt] / 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 <libavformat/avformat.h>
30
31 /** Get the audio.
32 */
33
34 static int resample_get_audio( mlt_frame frame, void **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         mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
46
47         // Get the resample information
48         int output_rate = mlt_properties_get_int( filter_properties, "frequency" );
49         int16_t *sample_buffer = mlt_properties_get_data( filter_properties, "buffer", NULL );
50
51         // Obtain the resample context if it exists
52         ReSampleContext *resample = mlt_properties_get_data( filter_properties, "audio_resample", NULL );
53
54         // Used to return number of channels in the source
55         int channels_avail = *channels;
56
57         // Loop variable
58         int i;
59
60         // If no resample frequency is specified, default to requested value
61         if ( output_rate == 0 )
62                 output_rate = *frequency;
63
64         // Get the producer's audio
65         *format = mlt_audio_s16;
66         mlt_frame_get_audio( frame, buffer, format, frequency, &channels_avail, samples );
67
68         // Duplicate channels as necessary
69         if ( channels_avail < *channels )
70         {
71                 int size = *channels * *samples * sizeof( int16_t );
72                 int16_t *new_buffer = mlt_pool_alloc( size );
73                 int j, k = 0;
74                 
75                 // Duplicate the existing channels
76                 for ( i = 0; i < *samples; i++ )
77                 {
78                         for ( j = 0; j < *channels; j++ )
79                         {
80                                 new_buffer[ ( i * *channels ) + j ] = ((int16_t*)(*buffer))[ ( i * channels_avail ) + k ];
81                                 k = ( k + 1 ) % channels_avail;
82                         }
83                 }
84                 
85                 // Update the audio buffer now - destroys the old
86                 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
87                 
88                 *buffer = new_buffer;
89         }
90         else if ( channels_avail == 6 && *channels == 2 )
91         {
92                 // Nasty hack for ac3 5.1 audio - may be a cause of failure?
93                 int size = *channels * *samples * sizeof( int16_t );
94                 int16_t *new_buffer = mlt_pool_alloc( size );
95                 
96                 // Drop all but the first *channels
97                 for ( i = 0; i < *samples; i++ )
98                 {
99                         new_buffer[ ( i * *channels ) + 0 ] = ((int16_t*)(*buffer))[ ( i * channels_avail ) + 2 ];
100                         new_buffer[ ( i * *channels ) + 1 ] = ((int16_t*)(*buffer))[ ( i * channels_avail ) + 3 ];
101                 }
102
103                 // Update the audio buffer now - destroys the old
104                 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
105                 
106                 *buffer = new_buffer;
107         }
108
109         // Return now if no work to do
110         if ( output_rate != *frequency )
111         {
112                 // Will store number of samples created
113                 int used = 0;
114
115                 // Create a resampler if nececessary
116                 if ( resample == NULL || *frequency != mlt_properties_get_int( filter_properties, "last_frequency" ) )
117                 {
118                         // Create the resampler
119 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
120                         resample = av_audio_resample_init( *channels, *channels, output_rate, *frequency,
121                                 SAMPLE_FMT_S16, SAMPLE_FMT_S16, 16, 10, 0, 0.8 );
122 #else
123                         resample = audio_resample_init( *channels, *channels, output_rate, *frequency );
124 #endif
125
126                         // And store it on properties
127                         mlt_properties_set_data( filter_properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
128
129                         // And remember what it was created for
130                         mlt_properties_set_int( filter_properties, "last_frequency", *frequency );
131                 }
132
133                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
134
135                 // Resample the audio
136                 used = audio_resample( resample, sample_buffer, *buffer, *samples );
137                 int size = used * *channels * sizeof( int16_t );
138
139                 // Resize if necessary
140                 if ( used > *samples )
141                 {
142                         *buffer = mlt_pool_realloc( *buffer, size );
143                         mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
144                 }
145
146                 // Copy samples
147                 memcpy( *buffer, sample_buffer, size );
148
149                 // Update output variables
150                 *samples = used;
151                 *frequency = output_rate;
152         }
153         else
154         {
155                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
156         }
157
158         return 0;
159 }
160
161 /** Filter processing.
162 */
163
164 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
165 {
166         // Only call this if we have a means to get audio
167         if ( mlt_frame_is_test_audio( frame ) == 0 )
168         {
169                 // Push the filter on to the stack
170                 mlt_frame_push_audio( frame, filter );
171
172                 // Assign our get_audio method
173                 mlt_frame_push_audio( frame, resample_get_audio );
174         }
175
176         return frame;
177 }
178
179 /** Constructor for the filter.
180 */
181
182 mlt_filter filter_avresample_init( char *arg )
183 {
184         // Create a filter
185         mlt_filter filter = mlt_filter_new( );
186
187         // Initialise if successful
188         if ( filter != NULL )
189         {
190                 // Calculate size of the buffer
191                 int size = AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t );
192
193                 // Allocate the buffer
194                 int16_t *buffer = mlt_pool_alloc( size );
195
196                 // Assign the process method
197                 filter->process = filter_process;
198
199                 // Deal with argument
200                 if ( arg != NULL )
201                         mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "frequency", arg );
202
203                 // Default to 2 channel output
204                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "channels", 2 );
205
206                 // Store the buffer
207                 mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "buffer", buffer, size, mlt_pool_release, NULL );
208         }
209
210         return filter;
211 }