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>
25 // vorbis Header files
26 #include <vorbis/codec.h>
27 #include <vorbis/vorbisfile.h>
29 // System header files
34 // Forward references.
35 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
);
36 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
);
38 /** Structure for metadata reading
41 typedef struct _sw_metadata sw_metadata
;
48 static sw_metadata
*vorbis_metadata_from_str (char * str
)
50 sw_metadata
* meta
= NULL
;
53 for (i
= 0; str
[i
]; i
++) {
54 str
[i
] = tolower(str
[i
]);
57 meta
= malloc (sizeof (sw_metadata
));
58 meta
->name
= malloc( strlen(str
) + 18 );
59 sprintf(meta
->name
, "meta.attr.%s.markup", str
);
60 meta
->content
= strdup (&str
[i
+1]);
67 /** Constructor for libvorbis.
70 mlt_producer
producer_vorbis_init( mlt_profile profile
, mlt_service_type type
, const char *id
, char *file
)
72 mlt_producer
this = NULL
;
74 // Check that we have a non-NULL argument
77 // Construct the producer
78 this = calloc( 1, sizeof( struct mlt_producer_s
) );
81 if ( mlt_producer_init( this, NULL
) == 0 )
84 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
86 // Set the resource property (required for all producers)
87 mlt_properties_set( properties
, "resource", file
);
89 // Register our get_frame implementation
90 this->get_frame
= producer_get_frame
;
93 if ( producer_open( this, profile
, file
) != 0 )
96 mlt_producer_close( this );
105 /** Destuctor for ogg files.
108 static void producer_file_close( void *file
)
112 // Close the ogg vorbis structure
123 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
)
125 // FILE pointer for file
126 FILE *input
= fopen( file
, "r" );
128 // Error code to return
129 int error
= input
== NULL
;
131 // Continue if file is open
134 // OggVorbis file structure
135 OggVorbis_File
*ov
= calloc( 1, sizeof( OggVorbis_File
) );
137 // Attempt to open the stream
138 error
= ov
== NULL
|| ov_open( input
, ov
, NULL
, 0 ) != 0;
140 // Assign to producer properties if successful
143 // Get the properties
144 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
146 // Assign the ov structure
147 mlt_properties_set_data( properties
, "ogg_vorbis_file", ov
, 0, producer_file_close
, NULL
);
150 sw_metadata
* metadata
= NULL
;
151 char **ptr
= ov_comment(ov
, -1)->user_comments
;
153 metadata
= vorbis_metadata_from_str (*ptr
);
154 if (metadata
!= NULL
)
155 mlt_properties_set(properties
, metadata
->name
, metadata
->content
);
159 if ( ov_seekable( ov
) )
161 // Get the length of the file
162 double length
= ov_time_total( ov
, -1 );
164 // We will treat everything with the producer fps
165 double fps
= mlt_profile_fps( profile
);
167 // Set out and length of file
168 mlt_properties_set_position( properties
, "out", ( length
* fps
) - 1 );
169 mlt_properties_set_position( properties
, "length", ( length
* fps
) );
171 // Get the vorbis info
172 vorbis_info
*vi
= ov_info( ov
, -1 );
173 mlt_properties_set_int( properties
, "frequency", (int) vi
->rate
);
174 mlt_properties_set_int( properties
, "channels", vi
->channels
);
176 // Set some media metadata
177 mlt_properties_set_int( properties
, "meta.media.nb_streams", 1 );
178 mlt_properties_set_int( properties
, "audio_index", 0 );
179 mlt_properties_set( properties
, "meta.media.0.stream.type", "audio" );
180 mlt_properties_set( properties
, "meta.media.0.codec.name", "vorbis" );
181 mlt_properties_set( properties
, "meta.media.0.codec.long_name", "Vorbis" );
189 // Must close input file when open fails
197 /** Convert a frame position to a time code.
200 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
202 return ( double )position
/ mlt_producer_get_fps( this );
205 /** Get the audio from a frame.
208 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
210 // Get the properties from the frame
211 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
213 // Obtain the frame number of this frame
214 mlt_position position
= mlt_properties_get_position( frame_properties
, "vorbis_position" );
217 mlt_producer
this = mlt_frame_pop_audio( frame
);
219 // Get the producer properties
220 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
222 // Get the ogg vorbis file
223 OggVorbis_File
*ov
= mlt_properties_get_data( properties
, "ogg_vorbis_file", NULL
);
225 // Obtain the expected frame numer
226 mlt_position expected
= mlt_properties_get_position( properties
, "audio_expected" );
228 // Get the fps for this producer
229 double fps
= mlt_producer_get_fps( this );
231 // Get the vorbis info
232 vorbis_info
*vi
= ov_info( ov
, -1 );
234 // Obtain the audio buffer
235 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
237 // Get amount of audio used
238 int audio_used
= mlt_properties_get_int( properties
, "audio_used" );
240 // Number of frames to ignore (for ffwd)
243 // Flag for paused (silence)
246 // Check for audio buffer and create if necessary
247 if ( audio_buffer
== NULL
)
249 // Allocate the audio buffer
250 audio_buffer
= mlt_pool_alloc( 131072 * sizeof( int16_t ) );
252 // And store it on properties for reuse
253 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, mlt_pool_release
, NULL
);
257 if ( position
!= expected
)
259 if ( position
+ 1 == expected
)
261 // We're paused - silence required
264 else if ( position
> expected
&& ( position
- expected
) < 250 )
266 // Fast forward - seeking is inefficient for small distances - just ignore following frames
267 ignore
= position
- expected
;
271 // Seek to the required position
272 ov_time_seek( ov
, producer_time_of_frame( this, position
) );
278 // Return info in frame
279 *frequency
= vi
->rate
;
280 *channels
= vi
->channels
;
282 // Get the audio if required
288 // Get the number of samples for the current frame
289 *samples
= mlt_sample_calculator( fps
, *frequency
, expected
++ );
291 while( *samples
> audio_used
)
294 int bytes
= ov_read( ov
, ( char * )( &audio_buffer
[ audio_used
* 2 ] ), 4096, 0, 2, 1, ¤t_section
);
296 // Break if error or eof
300 // Increment number of samples used
301 audio_used
+= bytes
/ ( sizeof( int16_t ) * *channels
);
304 while ( ignore
&& audio_used
>= *samples
)
307 audio_used
-= *samples
;
308 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
309 *samples
= mlt_sample_calculator( fps
, *frequency
, expected
++ );
313 // Now handle the audio if we have enough
314 if ( audio_used
>= *samples
)
316 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
317 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
318 audio_used
-= *samples
;
319 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
320 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, mlt_pool_release
, NULL
);
324 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
328 // Store the number of audio samples still available
329 mlt_properties_set_int( properties
, "audio_used", audio_used
);
333 // Get silence and don't touch the context
334 *samples
= mlt_sample_calculator( fps
, *frequency
, position
);
335 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
338 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
339 mlt_properties_set_position( properties
, "audio_expected", position
+ 1 );
344 /** Our get frame implementation.
347 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
349 // Create an empty frame
350 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
352 // Update timecode on the frame we're creating
353 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
355 // Set the position of this producer
356 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( *frame
);
357 mlt_properties_set_position( frame_properties
, "vorbis_position", mlt_producer_frame( this ) );
360 mlt_frame_push_audio( *frame
, this );
361 mlt_frame_push_audio( *frame
, producer_get_audio
);
363 // Pass audio properties to the frame
364 mlt_properties_pass_list( frame_properties
, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
366 // Calculate the next timecode
367 mlt_producer_prepare_next( this );