]> git.sesse.net Git - mlt/blob - src/modules/avformat/filter_avresample.c
Drop usage of av_demuxer_open() in avformat v53.
[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
32 /** Get the audio.
33 */
34
35 static int resample_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
36 {
37         // Get the filter service
38         mlt_filter filter = mlt_frame_pop_audio( frame );
39
40         // Get the filter properties
41         mlt_properties filter_properties = MLT_FILTER_PROPERTIES( filter );
42
43         mlt_service_lock( MLT_FILTER_SERVICE( 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         // If no resample frequency is specified, default to requested value
53         if ( output_rate == 0 )
54                 output_rate = *frequency;
55
56         // Get the producer's audio
57         int error = mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
58         if ( error ) return error;
59
60         // Return now if no work to do
61         if ( output_rate != *frequency )
62         {
63                 // Will store number of samples created
64                 int used = 0;
65
66                 mlt_log_debug( MLT_FILTER_SERVICE(filter), "channels %d samples %d frequency %d -> %d\n",
67                         *channels, *samples, *frequency, output_rate );
68
69                 // Do not convert to s16 unless we need to change the rate
70                 if ( *format != mlt_audio_s16 )
71                 {
72                         *format = mlt_audio_s16;
73                         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
74                 }
75
76                 // Create a resampler if nececessary
77                 if ( resample == NULL || *frequency != mlt_properties_get_int( filter_properties, "last_frequency" ) )
78                 {
79                         // Create the resampler
80 #if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
81                         resample = av_audio_resample_init( *channels, *channels, output_rate, *frequency,
82                                 SAMPLE_FMT_S16, SAMPLE_FMT_S16, 16, 10, 0, 0.8 );
83 #else
84                         resample = audio_resample_init( *channels, *channels, output_rate, *frequency );
85 #endif
86
87                         // And store it on properties
88                         mlt_properties_set_data( filter_properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
89
90                         // And remember what it was created for
91                         mlt_properties_set_int( filter_properties, "last_frequency", *frequency );
92                 }
93
94                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
95
96                 // Resample the audio
97                 used = audio_resample( resample, sample_buffer, *buffer, *samples );
98                 int size = used * *channels * sizeof( int16_t );
99
100                 // Resize if necessary
101                 if ( used > *samples )
102                 {
103                         *buffer = mlt_pool_realloc( *buffer, size );
104                         mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
105                 }
106
107                 // Copy samples
108                 memcpy( *buffer, sample_buffer, size );
109
110                 // Update output variables
111                 *samples = used;
112                 *frequency = output_rate;
113         }
114         else
115         {
116                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
117         }
118
119         return error;
120 }
121
122 /** Filter processing.
123 */
124
125 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
126 {
127         // Only call this if we have a means to get audio
128         if ( mlt_frame_is_test_audio( frame ) == 0 )
129         {
130                 // Push the filter on to the stack
131                 mlt_frame_push_audio( frame, filter );
132
133                 // Assign our get_audio method
134                 mlt_frame_push_audio( frame, resample_get_audio );
135         }
136
137         return frame;
138 }
139
140 /** Constructor for the filter.
141 */
142
143 mlt_filter filter_avresample_init( char *arg )
144 {
145         // Create a filter
146         mlt_filter filter = mlt_filter_new( );
147
148         // Initialise if successful
149         if ( filter != NULL )
150         {
151                 // Calculate size of the buffer
152                 int size = AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t );
153
154                 // Allocate the buffer
155                 int16_t *buffer = mlt_pool_alloc( size );
156
157                 // Assign the process method
158                 filter->process = filter_process;
159
160                 // Deal with argument
161                 if ( arg != NULL )
162                         mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "frequency", arg );
163
164                 // Default to 2 channel output
165                 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "channels", 2 );
166
167                 // Store the buffer
168                 mlt_properties_set_data( MLT_FILTER_PROPERTIES( filter ), "buffer", buffer, size, mlt_pool_release, NULL );
169         }
170
171         return filter;
172 }