2 * producer_vorbis.c -- vorbis producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
22 #include <framework/mlt_producer.h>
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_profile.h>
26 // vorbis Header files
27 #include <vorbis/codec.h>
28 #include <vorbis/vorbisfile.h>
30 // System header files
35 // Forward references.
36 static int producer_open( mlt_producer this, mlt_profile profile, char *file );
37 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
39 /** Structure for metadata reading
42 typedef struct _sw_metadata sw_metadata;
49 static sw_metadata *vorbis_metadata_from_str (char * str)
51 sw_metadata * meta = NULL;
54 for (i = 0; str[i]; i++) {
55 str[i] = tolower(str[i]);
58 meta = malloc (sizeof (sw_metadata));
59 meta->name = malloc( strlen(str) + 18 );
60 sprintf(meta->name, "meta.attr.%s.markup", str);
61 meta->content = strdup (&str[i+1]);
68 /** Constructor for libvorbis.
71 mlt_producer producer_vorbis_init( mlt_profile profile, mlt_service_type type, const char *id, char *file )
73 mlt_producer this = NULL;
75 // Check that we have a non-NULL argument
78 // Construct the producer
79 this = calloc( 1, sizeof( struct mlt_producer_s ) );
82 if ( mlt_producer_init( this, NULL ) == 0 )
85 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
87 // Set the resource property (required for all producers)
88 mlt_properties_set( properties, "resource", file );
90 // Register our get_frame implementation
91 this->get_frame = producer_get_frame;
94 if ( producer_open( this, profile, file ) != 0 )
97 mlt_producer_close( this );
106 /** Destuctor for ogg files.
109 static void producer_file_close( void *file )
113 // Close the ogg vorbis structure
124 static int producer_open( mlt_producer this, mlt_profile profile, char *file )
126 // FILE pointer for file
127 FILE *input = fopen( file, "r" );
129 // Error code to return
130 int error = input == NULL;
132 // Continue if file is open
135 // OggVorbis file structure
136 OggVorbis_File *ov = calloc( 1, sizeof( OggVorbis_File ) );
138 // Attempt to open the stream
139 error = ov == NULL || ov_open( input, ov, NULL, 0 ) != 0;
141 // Assign to producer properties if successful
144 // Get the properties
145 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
147 // Assign the ov structure
148 mlt_properties_set_data( properties, "ogg_vorbis_file", ov, 0, producer_file_close, NULL );
151 sw_metadata * metadata = NULL;
152 char **ptr = ov_comment(ov, -1)->user_comments;
154 metadata = vorbis_metadata_from_str (*ptr);
155 if (metadata != NULL)
156 mlt_properties_set(properties, metadata->name, metadata->content);
160 if ( ov_seekable( ov ) )
162 // Get the length of the file
163 double length = ov_time_total( ov, -1 );
165 // We will treat everything with the producer fps
166 double fps = mlt_profile_fps( profile );
168 // Set out and length of file
169 mlt_properties_set_position( properties, "out", ( length * fps ) - 1 );
170 mlt_properties_set_position( properties, "length", ( length * fps ) );
172 // Get the vorbis info
173 vorbis_info *vi = ov_info( ov, -1 );
174 mlt_properties_set_int( properties, "frequency", (int) vi->rate );
175 mlt_properties_set_int( properties, "channels", vi->channels );
177 // Set some media metadata
178 mlt_properties_set_int( properties, "meta.media.nb_streams", 1 );
179 mlt_properties_set_int( properties, "audio_index", 0 );
180 mlt_properties_set( properties, "meta.media.0.stream.type", "audio" );
181 mlt_properties_set( properties, "meta.media.0.codec.name", "vorbis" );
182 mlt_properties_set( properties, "meta.media.0.codec.long_name", "Vorbis" );
190 // Must close input file when open fails
198 /** Convert a frame position to a time code.
201 static double producer_time_of_frame( mlt_producer this, mlt_position position )
203 return ( double )position / mlt_producer_get_fps( this );
206 /** Get the audio from a frame.
209 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
211 // Get the properties from the frame
212 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
214 // Obtain the frame number of this frame
215 mlt_position position = mlt_properties_get_position( frame_properties, "vorbis_position" );
218 mlt_producer this = mlt_frame_pop_audio( frame );
220 // Get the producer properties
221 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
223 // Get the ogg vorbis file
224 OggVorbis_File *ov = mlt_properties_get_data( properties, "ogg_vorbis_file", NULL );
226 // Obtain the expected frame numer
227 mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
229 // Get the fps for this producer
230 double fps = mlt_producer_get_fps( this );
232 // Get the vorbis info
233 vorbis_info *vi = ov_info( ov, -1 );
235 // Obtain the audio buffer
236 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
238 // Get amount of audio used
239 int audio_used = mlt_properties_get_int( properties, "audio_used" );
241 // Number of frames to ignore (for ffwd)
244 // Flag for paused (silence)
247 // Check for audio buffer and create if necessary
248 if ( audio_buffer == NULL )
250 // Allocate the audio buffer
251 audio_buffer = mlt_pool_alloc( 131072 * sizeof( int16_t ) );
253 // And store it on properties for reuse
254 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, mlt_pool_release, NULL );
258 if ( position != expected )
260 if ( position + 1 == expected )
262 // We're paused - silence required
265 else if ( position > expected && ( position - expected ) < 250 )
267 // Fast forward - seeking is inefficient for small distances - just ignore following frames
268 ignore = position - expected;
272 // Seek to the required position
273 ov_time_seek( ov, producer_time_of_frame( this, position ) );
279 // Return info in frame
280 *frequency = vi->rate;
281 *channels = vi->channels;
283 // Get the audio if required
289 // Get the number of samples for the current frame
290 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
292 while( *samples > audio_used )
295 int bytes = ov_read( ov, ( char * )( &audio_buffer[ audio_used * 2 ] ), 4096, 0, 2, 1, ¤t_section );
297 // Break if error or eof
301 // Increment number of samples used
302 audio_used += bytes / ( sizeof( int16_t ) * *channels );
305 while ( ignore && audio_used >= *samples )
308 audio_used -= *samples;
309 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
310 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
314 // Now handle the audio if we have enough
315 if ( audio_used >= *samples )
317 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
318 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
319 audio_used -= *samples;
320 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
321 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, mlt_pool_release, NULL );
325 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
329 // Store the number of audio samples still available
330 mlt_properties_set_int( properties, "audio_used", audio_used );
334 // Get silence and don't touch the context
335 *samples = mlt_sample_calculator( fps, *frequency, position );
336 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
339 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
340 mlt_properties_set_position( properties, "audio_expected", position + 1 );
345 /** Our get frame implementation.
348 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
350 // Create an empty frame
351 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
353 // Update timecode on the frame we're creating
354 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
356 // Set the position of this producer
357 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( *frame );
358 mlt_properties_set_position( frame_properties, "vorbis_position", mlt_producer_frame( this ) );
361 mlt_frame_push_audio( *frame, this );
362 mlt_frame_push_audio( *frame, producer_get_audio );
364 // Pass audio properties to the frame
365 mlt_properties_pass_list( frame_properties, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
367 // Calculate the next timecode
368 mlt_producer_prepare_next( this );