]> git.sesse.net Git - mlt/blob - src/modules/avformat/filter_avresample.c
A little debugging.
[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 #include <framework/mlt_log.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 // ffmpeg Header files
30 #include <libavformat/avformat.h>
31 #include <libavutil/samplefmt.h>
32
33 #if defined(FFUDIV) || (LIBAVCODEC_VERSION_INT < ((54<<16)+(26<<8)+0))
34
35 #define MAX_AUDIO_FRAME_SIZE (192000) // 1 second of 48khz 32bit audio
36
37
38 /** Get the audio.
39 */
40
41 static int resample_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
42 {
43         // Get the filter service
44         mlt_filter filter = mlt_frame_pop_audio( frame );
45
46         // Get the filter properties
47         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
48
49         mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
50
51         // Get the resample information
52         int output_rate = mlt_properties_get_int( filter_properties, "frequency" );
53         int16_t *sample_buffer = mlt_properties_get_data( filter_properties, "buffer", NULL );
54
55         // Obtain the resample context if it exists
56         ReSampleContext *resample = mlt_properties_get_data( filter_properties, "audio_resample", NULL );
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         int error = mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
64         if ( error ) return error;
65
66         // Return now if no work to do
67         if ( output_rate != *frequency )
68         {
69                 // Will store number of samples created
70                 int used = 0;
71
72                 mlt_log_debug( MLT_FILTER_SERVICE(filter), "channels %d samples %d frequency %d -> %d\n",
73                         *channels, *samples, *frequency, output_rate );
74
75                 // Do not convert to s16 unless we need to change the rate
76                 if ( *format != mlt_audio_s16 )
77                 {
78                         *format = mlt_audio_s16;
79                         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
80                 }
81
82                 // Create a resampler if nececessary
83                 if ( resample == NULL || *frequency != mlt_properties_get_int( filter_properties, "last_frequency" ) )
84                 {
85                         // Create the resampler
86                         resample = av_audio_resample_init( *channels, *channels, output_rate, *frequency,
87                                 AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16, 16, 10, 0, 0.8 );
88
89                         // And store it on properties
90                         mlt_properties_set_data( filter_properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
91
92                         // And remember what it was created for
93                         mlt_properties_set_int( filter_properties, "last_frequency", *frequency );
94                 }
95
96                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
97
98                 // Resample the audio
99                 used = audio_resample( resample, sample_buffer, *buffer, *samples );
100                 int size = used * *channels * sizeof( int16_t );
101
102                 // Resize if necessary
103                 if ( used > *samples )
104                 {
105                         *buffer = mlt_pool_realloc( *buffer, size );
106                         mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
107                 }
108
109                 // Copy samples
110                 memcpy( *buffer, sample_buffer, size );
111
112                 // Update output variables
113                 *samples = used;
114                 *frequency = output_rate;
115         }
116         else
117         {
118                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
119         }
120
121         return error;
122 }
123
124 /** Filter processing.
125 */
126
127 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
128 {
129         // Only call this if we have a means to get audio
130         if ( mlt_frame_is_test_audio( frame ) == 0 )
131         {
132                 // Push the filter on to the stack
133                 mlt_frame_push_audio( frame, filter );
134
135                 // Assign our get_audio method
136                 mlt_frame_push_audio( frame, resample_get_audio );
137         }
138
139         return frame;
140 }
141
142 /** Constructor for the filter.
143 */
144
145 mlt_filter filter_avresample_init( char *arg )
146 {
147         // Create a filter
148         mlt_filter filter = mlt_filter_new( );
149
150         // Initialise if successful
151         if ( filter != NULL )
152         {
153                 // Calculate size of the buffer
154                 int size = MAX_AUDIO_FRAME_SIZE * sizeof( int16_t );
155
156                 // Allocate the buffer
157                 int16_t *buffer = mlt_pool_alloc( size );
158
159                 // Assign the process method
160                 filter->process = filter_process;
161
162                 // Deal with argument
163                 if ( arg != NULL )
164                         mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "frequency", arg );
165
166                 // Default to 2 channel output
167                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "channels", 2 );
168
169                 // Store the buffer
170                 mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "buffer", buffer, size, mlt_pool_release, NULL );
171         }
172
173         return filter;
174 }
175
176 #endif // defined(FFUDIV) || (LIBAVCODEC_VERSION_INT < ((54<<16)+(26<<8)+0))