d69fc82a75501870267bd6c4bb087ad9c466c58e
[melted] / src / modules / vorbis / producer_vorbis.c
1 /*
2 * producer_vorbis.c -- vorbis producer
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 // Local header files
22 #include "producer_vorbis.h"
23
24 // MLT Header files
25 #include <framework/mlt_frame.h>
26
27 // vorbis Header files
28 #include <vorbis/codec.h>
29 #include <vorbis/vorbisfile.h>
30
31 // System header files
32 #include <stdlib.h>
33 #include <string.h>
34
35 // Forward references.
36 static int producer_open( mlt_producer this, char *file );
37 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
38
39 /** Constructor for libvorbis.
40 */
41
42 mlt_producer producer_vorbis_init( char *file )
43 {
44 mlt_producer this = NULL;
45
46 // Check that we have a non-NULL argument
47 if ( file != NULL )
48 {
49 // Construct the producer
50 this = calloc( 1, sizeof( struct mlt_producer_s ) );
51
52 // Initialise it
53 if ( mlt_producer_init( this, NULL ) == 0 )
54 {
55 // Get the properties
56 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
57
58 // Set the resource property (required for all producers)
59 mlt_properties_set( properties, "resource", file );
60
61 // Register our get_frame implementation
62 this->get_frame = producer_get_frame;
63
64 // Open the file
65 if ( producer_open( this, file ) != 0 )
66 {
67 // Clean up
68 mlt_producer_close( this );
69 this = NULL;
70 }
71 }
72 }
73
74 return this;
75 }
76
77 /** Destuctor for ogg files.
78 */
79
80 static void producer_file_close( void *file )
81 {
82 if ( file != NULL )
83 {
84 // Close the ogg vorbis structure
85 ov_clear( file );
86
87 // Free the memory
88 free( file );
89 }
90 }
91
92 /** Open the file.
93 */
94
95 static int producer_open( mlt_producer this, char *file )
96 {
97 // FILE pointer for file
98 FILE *input = fopen( file, "r" );
99
100 // Error code to return
101 int error = input == NULL;
102
103 // Continue if file is open
104 if ( error == 0 )
105 {
106 // OggVorbis file structure
107 OggVorbis_File *ov = calloc( 1, sizeof( OggVorbis_File ) );
108
109 // Attempt to open the stream
110 error = ov == NULL || ov_open( input, ov, NULL, 0 ) != 0;
111
112 // Assign to producer properties if successful
113 if ( error == 0 )
114 {
115 // Get the properties
116 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
117
118 // Assign the ov structure
119 mlt_properties_set_data( properties, "ogg_vorbis_file", ov, 0, producer_file_close, NULL );
120
121 if ( ov_seekable( ov ) )
122 {
123 // Get the length of the file
124 double length = ov_time_total( ov, -1 );
125
126 // We will treat everything with the producer fps
127 double fps = mlt_properties_get_double( properties, "fps" );
128
129 // Set out and length of file
130 mlt_properties_set_position( properties, "out", ( length * fps ) - 1 );
131 mlt_properties_set_position( properties, "length", ( length * fps ) );
132
133 // Get the vorbis info
134 vorbis_info *vi = ov_info( ov, -1 );
135 mlt_properties_set_int( properties, "frequency", (int) vi->rate );
136 mlt_properties_set_int( properties, "channels", vi->channels );
137 }
138 }
139 else
140 {
141 // Clean up
142 free( ov );
143
144 // Must close input file when open fails
145 fclose( input );
146 }
147 }
148
149 return error;
150 }
151
152 /** Convert a frame position to a time code.
153 */
154
155 static double producer_time_of_frame( mlt_producer this, mlt_position position )
156 {
157 // Get the properties
158 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
159
160 // Obtain the fps
161 double fps = mlt_properties_get_double( properties, "fps" );
162
163 // Do the calc
164 return ( double )position / fps;
165 }
166
167 /** Get the audio from a frame.
168 */
169
170 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
171 {
172 // Get the properties from the frame
173 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
174
175 // Obtain the frame number of this frame
176 mlt_position position = mlt_properties_get_position( frame_properties, "vorbis_position" );
177
178 // Get the producer
179 mlt_producer this = mlt_frame_pop_audio( frame );
180
181 // Get the producer properties
182 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
183
184 // Get the ogg vorbis file
185 OggVorbis_File *ov = mlt_properties_get_data( properties, "ogg_vorbis_file", NULL );
186
187 // Obtain the expected frame numer
188 mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
189
190 // Get the fps for this producer
191 double fps = mlt_properties_get_double( properties, "fps" );
192
193 // Get the vorbis info
194 vorbis_info *vi = ov_info( ov, -1 );
195
196 // Obtain the audio buffer
197 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
198
199 // Get amount of audio used
200 int audio_used = mlt_properties_get_int( properties, "audio_used" );
201
202 // Number of frames to ignore (for ffwd)
203 int ignore = 0;
204
205 // Flag for paused (silence)
206 int paused = 0;
207
208 // Check for audio buffer and create if necessary
209 if ( audio_buffer == NULL )
210 {
211 // Allocate the audio buffer
212 audio_buffer = mlt_pool_alloc( 131072 * sizeof( int16_t ) );
213
214 // And store it on properties for reuse
215 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, mlt_pool_release, NULL );
216 }
217
218 // Seek if necessary
219 if ( position != expected )
220 {
221 if ( position + 1 == expected )
222 {
223 // We're paused - silence required
224 paused = 1;
225 }
226 else if ( position > expected && ( position - expected ) < 250 )
227 {
228 // Fast forward - seeking is inefficient for small distances - just ignore following frames
229 ignore = position - expected;
230 }
231 else
232 {
233 // Seek to the required position
234 ov_time_seek( ov, producer_time_of_frame( this, position ) );
235 expected = position;
236 audio_used = 0;
237 }
238 }
239
240 // Return info in frame
241 *frequency = vi->rate;
242 *channels = vi->channels;
243
244 // Get the audio if required
245 if ( !paused )
246 {
247 // Bitstream section
248 int current_section;
249
250 // Get the number of samples for the current frame
251 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
252
253 while( *samples > audio_used )
254 {
255 // Read the samples
256 int bytes = ov_read( ov, ( char * )( &audio_buffer[ audio_used * 2 ] ), 4096, 0, 2, 1, &current_section );
257
258 // Break if error or eof
259 if ( bytes <= 0 )
260 break;
261
262 // Increment number of samples used
263 audio_used += bytes / ( sizeof( int16_t ) * *channels );
264
265 // Handle ignore
266 while ( ignore && audio_used >= *samples )
267 {
268 ignore --;
269 audio_used -= *samples;
270 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
271 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
272 }
273 }
274
275 // Now handle the audio if we have enough
276 if ( audio_used >= *samples )
277 {
278 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
279 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
280 audio_used -= *samples;
281 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
282 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, mlt_pool_release, NULL );
283 }
284 else
285 {
286 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
287 audio_used = 0;
288 }
289
290 // Store the number of audio samples still available
291 mlt_properties_set_int( properties, "audio_used", audio_used );
292 }
293 else
294 {
295 // Get silence and don't touch the context
296 *samples = mlt_sample_calculator( fps, *frequency, position );
297 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
298 }
299
300 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
301 mlt_properties_set_position( properties, "audio_expected", position + 1 );
302
303 return 0;
304 }
305
306 /** Our get frame implementation.
307 */
308
309 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
310 {
311 // Create an empty frame
312 *frame = mlt_frame_init( );
313
314 // Update timecode on the frame we're creating
315 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
316
317 // Set the position of this producer
318 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( *frame );
319 mlt_properties_set_position( frame_properties, "vorbis_position", mlt_producer_frame( this ) );
320
321 // Set up the audio
322 mlt_frame_push_audio( *frame, this );
323 mlt_frame_push_audio( *frame, producer_get_audio );
324
325 // Pass audio properties to the frame
326 mlt_properties_pass_list( frame_properties, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
327
328 // Calculate the next timecode
329 mlt_producer_prepare_next( this );
330
331 return 0;
332 }