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);
158 free(metadata->name);
159 if (metadata->content)
160 free(metadata->content);
166 if ( ov_seekable( ov ) )
168 // Get the length of the file
169 double length = ov_time_total( ov, -1 );
171 // We will treat everything with the producer fps
172 double fps = mlt_profile_fps( profile );
174 // Set out and length of file
175 mlt_properties_set_position( properties, "out", ( length * fps ) - 1 );
176 mlt_properties_set_position( properties, "length", ( length * fps ) );
178 // Get the vorbis info
179 vorbis_info *vi = ov_info( ov, -1 );
180 mlt_properties_set_int( properties, "audio_frequency", (int) vi->rate );
181 mlt_properties_set_int( properties, "audio_channels", vi->channels );
183 // Set some media metadata
184 mlt_properties_set_int( properties, "meta.media.nb_streams", 1 );
185 mlt_properties_set_int( properties, "audio_index", 0 );
186 mlt_properties_set( properties, "meta.media.0.stream.type", "audio" );
187 mlt_properties_set( properties, "meta.media.0.codec.name", "vorbis" );
188 mlt_properties_set( properties, "meta.media.0.codec.long_name", "Vorbis" );
196 // Must close input file when open fails
204 /** Convert a frame position to a time code.
207 static double producer_time_of_frame( mlt_producer this, mlt_position position )
209 return ( double )position / mlt_producer_get_fps( this );
212 /** Get the audio from a frame.
215 static int producer_get_audio( mlt_frame frame, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
217 // Obtain the frame number of this frame
218 mlt_position position = mlt_frame_original_position( frame );
221 mlt_producer this = mlt_frame_pop_audio( frame );
223 // Get the producer properties
224 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
226 mlt_service_lock( MLT_PRODUCER_SERVICE( this ) );
228 // Get the ogg vorbis file
229 OggVorbis_File *ov = mlt_properties_get_data( properties, "ogg_vorbis_file", NULL );
231 // Obtain the expected frame numer
232 mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
234 // Get the fps for this producer
235 double fps = mlt_producer_get_fps( this );
237 // Get the vorbis info
238 vorbis_info *vi = ov_info( ov, -1 );
240 // Obtain the audio buffer
241 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
243 // Get amount of audio used
244 int audio_used = mlt_properties_get_int( properties, "audio_used" );
246 // Number of frames to ignore (for ffwd)
249 // Flag for paused (silence)
252 // Check for audio buffer and create if necessary
253 if ( audio_buffer == NULL )
255 // Allocate the audio buffer
256 audio_buffer = mlt_pool_alloc( 131072 * sizeof( int16_t ) );
258 // And store it on properties for reuse
259 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, mlt_pool_release, NULL );
263 if ( position != expected )
265 if ( position + 1 == expected )
267 // We're paused - silence required
270 else if ( position > expected && ( position - expected ) < 250 )
272 // Fast forward - seeking is inefficient for small distances - just ignore following frames
273 ignore = position - expected;
277 // Seek to the required position
278 ov_time_seek( ov, producer_time_of_frame( this, position ) );
284 // Return info in frame
285 *frequency = vi->rate;
286 *channels = vi->channels;
288 // Get the audio if required
294 // Get the number of samples for the current frame
295 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
297 while( *samples > audio_used )
300 int bytes = ov_read( ov, ( char * )( &audio_buffer[ audio_used * 2 ] ), 4096, 0, 2, 1, ¤t_section );
302 // Break if error or eof
306 // Increment number of samples used
307 audio_used += bytes / ( sizeof( int16_t ) * *channels );
310 while ( ignore && audio_used >= *samples )
313 audio_used -= *samples;
314 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) * *channels );
315 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
319 // Now handle the audio if we have enough
320 if ( audio_used >= *samples )
322 int size = *samples * *channels * sizeof( int16_t );
324 *format = mlt_audio_s16;
325 *buffer = mlt_pool_alloc( size );
326 memcpy( *buffer, audio_buffer, size );
327 audio_used -= *samples;
328 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
329 mlt_frame_set_audio( frame, *buffer, *format, size, mlt_pool_release );
333 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
337 // Store the number of audio samples still available
338 mlt_properties_set_int( properties, "audio_used", audio_used );
342 // Get silence and don't touch the context
343 *samples = mlt_sample_calculator( fps, *frequency, position );
344 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
347 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
348 mlt_properties_set_position( properties, "audio_expected", position + 1 );
350 mlt_service_unlock( MLT_PRODUCER_SERVICE( this ) );
355 /** Our get frame implementation.
358 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
360 // Create an empty frame
361 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
363 // Update timecode on the frame we're creating
364 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
367 mlt_frame_push_audio( *frame, this );
368 mlt_frame_push_audio( *frame, producer_get_audio );
370 // Pass audio properties to the frame
371 mlt_properties_pass_list( MLT_FRAME_PROPERTIES(*frame), MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
373 // Calculate the next timecode
374 mlt_producer_prepare_next( this );