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 "producer_vorbis.h"
25 #include <framework/mlt_frame.h>
27 // vorbis Header files
28 #include <vorbis/codec.h>
29 #include <vorbis/vorbisfile.h>
31 // System header files
36 // Forward references.
37 static int producer_open( mlt_producer this, char *file );
38 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
40 /** Structure for metadata reading
43 typedef struct _sw_metadata sw_metadata;
50 static sw_metadata *vorbis_metadata_from_str (char * str)
52 sw_metadata * meta = NULL;
55 for (i = 0; str[i]; i++) {
56 str[i] = tolower(str[i]);
59 meta = malloc (sizeof (sw_metadata));
60 meta->name = malloc( strlen(str) + 18 );
61 sprintf(meta->name, "meta.attr.%s.markup", str);
62 meta->content = strdup (&str[i+1]);
69 /** Constructor for libvorbis.
72 mlt_producer producer_vorbis_init( char *file )
74 mlt_producer this = NULL;
76 // Check that we have a non-NULL argument
79 // Construct the producer
80 this = calloc( 1, sizeof( struct mlt_producer_s ) );
83 if ( mlt_producer_init( this, NULL ) == 0 )
86 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
88 // Set the resource property (required for all producers)
89 mlt_properties_set( properties, "resource", file );
91 // Register our get_frame implementation
92 this->get_frame = producer_get_frame;
95 if ( producer_open( this, file ) != 0 )
98 mlt_producer_close( this );
107 /** Destuctor for ogg files.
110 static void producer_file_close( void *file )
114 // Close the ogg vorbis structure
125 static int producer_open( mlt_producer this, char *file )
127 // FILE pointer for file
128 FILE *input = fopen( file, "r" );
130 // Error code to return
131 int error = input == NULL;
133 // Continue if file is open
136 // OggVorbis file structure
137 OggVorbis_File *ov = calloc( 1, sizeof( OggVorbis_File ) );
139 // Attempt to open the stream
140 error = ov == NULL || ov_open( input, ov, NULL, 0 ) != 0;
142 // Assign to producer properties if successful
145 // Get the properties
146 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
148 // Assign the ov structure
149 mlt_properties_set_data( properties, "ogg_vorbis_file", ov, 0, producer_file_close, NULL );
152 sw_metadata * metadata = NULL;
153 char **ptr = ov_comment(ov, -1)->user_comments;
155 metadata = vorbis_metadata_from_str (*ptr);
156 if (metadata != NULL)
157 mlt_properties_set(properties, metadata->name, metadata->content);
161 if ( ov_seekable( ov ) )
163 // Get the length of the file
164 double length = ov_time_total( ov, -1 );
166 // We will treat everything with the producer fps
167 double fps = mlt_properties_get_double( properties, "fps" );
169 // Set out and length of file
170 mlt_properties_set_position( properties, "out", ( length * fps ) - 1 );
171 mlt_properties_set_position( properties, "length", ( length * fps ) );
173 // Get the vorbis info
174 vorbis_info *vi = ov_info( ov, -1 );
175 mlt_properties_set_int( properties, "frequency", (int) vi->rate );
176 mlt_properties_set_int( properties, "channels", vi->channels );
184 // Must close input file when open fails
192 /** Convert a frame position to a time code.
195 static double producer_time_of_frame( mlt_producer this, mlt_position position )
197 // Get the properties
198 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
201 double fps = mlt_properties_get_double( properties, "fps" );
204 return ( double )position / fps;
207 /** Get the audio from a frame.
210 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
212 // Get the properties from the frame
213 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
215 // Obtain the frame number of this frame
216 mlt_position position = mlt_properties_get_position( frame_properties, "vorbis_position" );
219 mlt_producer this = mlt_frame_pop_audio( frame );
221 // Get the producer properties
222 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
224 // Get the ogg vorbis file
225 OggVorbis_File *ov = mlt_properties_get_data( properties, "ogg_vorbis_file", NULL );
227 // Obtain the expected frame numer
228 mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
230 // Get the fps for this producer
231 double fps = mlt_properties_get_double( properties, "fps" );
233 // Get the vorbis info
234 vorbis_info *vi = ov_info( ov, -1 );
236 // Obtain the audio buffer
237 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
239 // Get amount of audio used
240 int audio_used = mlt_properties_get_int( properties, "audio_used" );
242 // Number of frames to ignore (for ffwd)
245 // Flag for paused (silence)
248 // Check for audio buffer and create if necessary
249 if ( audio_buffer == NULL )
251 // Allocate the audio buffer
252 audio_buffer = mlt_pool_alloc( 131072 * sizeof( int16_t ) );
254 // And store it on properties for reuse
255 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, mlt_pool_release, NULL );
259 if ( position != expected )
261 if ( position + 1 == expected )
263 // We're paused - silence required
266 else if ( position > expected && ( position - expected ) < 250 )
268 // Fast forward - seeking is inefficient for small distances - just ignore following frames
269 ignore = position - expected;
273 // Seek to the required position
274 ov_time_seek( ov, producer_time_of_frame( this, position ) );
280 // Return info in frame
281 *frequency = vi->rate;
282 *channels = vi->channels;
284 // Get the audio if required
290 // Get the number of samples for the current frame
291 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
293 while( *samples > audio_used )
296 int bytes = ov_read( ov, ( char * )( &audio_buffer[ audio_used * 2 ] ), 4096, 0, 2, 1, ¤t_section );
298 // Break if error or eof
302 // Increment number of samples used
303 audio_used += bytes / ( sizeof( int16_t ) * *channels );
306 while ( ignore && audio_used >= *samples )
309 audio_used -= *samples;
310 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
311 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
315 // Now handle the audio if we have enough
316 if ( audio_used >= *samples )
318 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
319 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
320 audio_used -= *samples;
321 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
322 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, mlt_pool_release, NULL );
326 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
330 // Store the number of audio samples still available
331 mlt_properties_set_int( properties, "audio_used", audio_used );
335 // Get silence and don't touch the context
336 *samples = mlt_sample_calculator( fps, *frequency, position );
337 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
340 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
341 mlt_properties_set_position( properties, "audio_expected", position + 1 );
346 /** Our get frame implementation.
349 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
351 // Create an empty frame
352 *frame = mlt_frame_init( );
354 // Update timecode on the frame we're creating
355 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
357 // Set the position of this producer
358 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( *frame );
359 mlt_properties_set_position( frame_properties, "vorbis_position", mlt_producer_frame( this ) );
362 mlt_frame_push_audio( *frame, this );
363 mlt_frame_push_audio( *frame, producer_get_audio );
365 // Pass audio properties to the frame
366 mlt_properties_pass_list( frame_properties, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
368 // Calculate the next timecode
369 mlt_producer_prepare_next( this );