LGPL deinterlace and resampler
[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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "filter_avresample.h"
22
23 #include <framework/mlt_frame.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 // ffmpeg Header files
30 #include <ffmpeg/avformat.h>
31
32 /** Get the audio.
33 */
34
35 static int resample_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
36 {
37 // Get the properties of the frame
38 mlt_properties properties = mlt_frame_properties( frame );
39
40 // Get the filter service
41 mlt_filter filter = mlt_frame_pop_audio( frame );
42
43 // Get the filter properties
44 mlt_properties filter_properties = mlt_filter_properties( filter );
45
46 // Get the resample information
47 int output_rate = mlt_properties_get_int( filter_properties, "frequency" );
48 int16_t *sample_buffer = mlt_properties_get_data( filter_properties, "buffer", NULL );
49
50 // Obtain the resample context if it exists
51 ReSampleContext *resample = mlt_properties_get_data( filter_properties, "audio_resample", NULL );
52
53 // Used to return number of channels in the source
54 int channels_avail = *channels;
55
56 // Loop variable
57 int i;
58
59 // If no resample frequency is specified, default to requested value
60 if ( output_rate == 0 )
61 output_rate = *frequency;
62
63 // Restore the original get_audio
64 frame->get_audio = mlt_frame_pop_audio( frame );
65
66 // Get the producer's audio
67 mlt_frame_get_audio( frame, buffer, format, frequency, &channels_avail, samples );
68
69 // Duplicate channels as necessary
70 if ( channels_avail < *channels )
71 {
72 int size = *channels * *samples * sizeof( int16_t );
73 int16_t *new_buffer = mlt_pool_alloc( size );
74 int j, k = 0;
75
76 // Duplicate the existing channels
77 for ( i = 0; i < *samples; i++ )
78 {
79 for ( j = 0; j < *channels; j++ )
80 {
81 new_buffer[ ( i * *channels ) + j ] = (*buffer)[ ( i * channels_avail ) + k ];
82 k = ( k + 1 ) % channels_avail;
83 }
84 }
85
86 // Update the audio buffer now - destroys the old
87 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
88
89 *buffer = new_buffer;
90 }
91 else if ( channels_avail == 6 && *channels == 2 )
92 {
93 // Nasty hack for ac3 5.1 audio - may be a cause of failure?
94 int size = *channels * *samples * sizeof( int16_t );
95 int16_t *new_buffer = mlt_pool_alloc( size );
96
97 // Drop all but the first *channels
98 for ( i = 0; i < *samples; i++ )
99 {
100 new_buffer[ ( i * *channels ) + 0 ] = (*buffer)[ ( i * channels_avail ) + 2 ];
101 new_buffer[ ( i * *channels ) + 1 ] = (*buffer)[ ( i * channels_avail ) + 3 ];
102 }
103
104 // Update the audio buffer now - destroys the old
105 mlt_properties_set_data( properties, "audio", new_buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
106
107 *buffer = new_buffer;
108 }
109
110 // Return now if no work to do
111 if ( output_rate != *frequency )
112 {
113 // Will store number of samples created
114 int used = 0;
115
116 // Create a resampler if nececessary
117 if ( resample == NULL || *frequency != mlt_properties_get_int( filter_properties, "last_frequency" ) )
118 {
119 // Create the resampler
120 resample = audio_resample_init( *channels, *channels, output_rate, *frequency );
121
122 // And store it on properties
123 mlt_properties_set_data( filter_properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
124
125 // And remember what it was created for
126 mlt_properties_set_int( filter_properties, "last_frequency", *frequency );
127 }
128
129 // Resample the audio
130 used = audio_resample( resample, sample_buffer, *buffer, *samples );
131
132 // Resize if necessary
133 if ( used > *samples )
134 {
135 *buffer = mlt_pool_realloc( *buffer, *samples * *channels * sizeof( int16_t ) );
136 mlt_properties_set_data( properties, "audio", *buffer, *channels * used * sizeof( int16_t ), mlt_pool_release, NULL );
137 }
138
139 // Copy samples
140 memcpy( *buffer, sample_buffer, *channels * used * sizeof( int16_t ) );
141
142 // Update output variables
143 *samples = used;
144 *frequency = output_rate;
145 }
146
147 return 0;
148 }
149
150 /** Filter processing.
151 */
152
153 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
154 {
155 // Only call this if we have a means to get audio
156 if ( frame->get_audio != NULL )
157 {
158 // Push the original method on to the stack
159 mlt_frame_push_audio( frame, frame->get_audio );
160
161 // Push the filter on to the stack
162 mlt_frame_push_audio( frame, this );
163
164 // Assign our get_audio method
165 frame->get_audio = resample_get_audio;
166 }
167
168 return frame;
169 }
170
171 /** Constructor for the filter.
172 */
173
174 mlt_filter filter_avresample_init( char *arg )
175 {
176 // Create a filter
177 mlt_filter this = mlt_filter_new( );
178
179 // Initialise if successful
180 if ( this != NULL )
181 {
182 // Calculate size of the buffer
183 int size = AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t );
184
185 // Allocate the buffer
186 int16_t *buffer = mlt_pool_alloc( size );
187
188 // Assign the process method
189 this->process = filter_process;
190
191 // Deal with argument
192 if ( arg != NULL )
193 mlt_properties_set( mlt_filter_properties( this ), "frequency", arg );
194
195 // Default to 2 channel output
196 mlt_properties_set_int( mlt_filter_properties( this ), "channels", 2 );
197
198 // Store the buffer
199 mlt_properties_set_data( mlt_filter_properties( this ), "buffer", buffer, size, mlt_pool_release, NULL );
200 }
201
202 return this;
203 }