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, 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, 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, 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_producer_get_fps( this );
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
);
182 // Must close input file when open fails
190 /** Convert a frame position to a time code.
193 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
195 return ( double )position
/ mlt_producer_get_fps( this );
198 /** Get the audio from a frame.
201 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
203 // Get the properties from the frame
204 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
206 // Obtain the frame number of this frame
207 mlt_position position
= mlt_properties_get_position( frame_properties
, "vorbis_position" );
210 mlt_producer
this = mlt_frame_pop_audio( frame
);
212 // Get the producer properties
213 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
215 // Get the ogg vorbis file
216 OggVorbis_File
*ov
= mlt_properties_get_data( properties
, "ogg_vorbis_file", NULL
);
218 // Obtain the expected frame numer
219 mlt_position expected
= mlt_properties_get_position( properties
, "audio_expected" );
221 // Get the fps for this producer
222 double fps
= mlt_producer_get_fps( this );
224 // Get the vorbis info
225 vorbis_info
*vi
= ov_info( ov
, -1 );
227 // Obtain the audio buffer
228 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
230 // Get amount of audio used
231 int audio_used
= mlt_properties_get_int( properties
, "audio_used" );
233 // Number of frames to ignore (for ffwd)
236 // Flag for paused (silence)
239 // Check for audio buffer and create if necessary
240 if ( audio_buffer
== NULL
)
242 // Allocate the audio buffer
243 audio_buffer
= mlt_pool_alloc( 131072 * sizeof( int16_t ) );
245 // And store it on properties for reuse
246 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, mlt_pool_release
, NULL
);
250 if ( position
!= expected
)
252 if ( position
+ 1 == expected
)
254 // We're paused - silence required
257 else if ( position
> expected
&& ( position
- expected
) < 250 )
259 // Fast forward - seeking is inefficient for small distances - just ignore following frames
260 ignore
= position
- expected
;
264 // Seek to the required position
265 ov_time_seek( ov
, producer_time_of_frame( this, position
) );
271 // Return info in frame
272 *frequency
= vi
->rate
;
273 *channels
= vi
->channels
;
275 // Get the audio if required
281 // Get the number of samples for the current frame
282 *samples
= mlt_sample_calculator( fps
, *frequency
, expected
++ );
284 while( *samples
> audio_used
)
287 int bytes
= ov_read( ov
, ( char * )( &audio_buffer
[ audio_used
* 2 ] ), 4096, 0, 2, 1, ¤t_section
);
289 // Break if error or eof
293 // Increment number of samples used
294 audio_used
+= bytes
/ ( sizeof( int16_t ) * *channels
);
297 while ( ignore
&& audio_used
>= *samples
)
300 audio_used
-= *samples
;
301 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
302 *samples
= mlt_sample_calculator( fps
, *frequency
, expected
++ );
306 // Now handle the audio if we have enough
307 if ( audio_used
>= *samples
)
309 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
310 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
311 audio_used
-= *samples
;
312 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
313 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, mlt_pool_release
, NULL
);
317 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
321 // Store the number of audio samples still available
322 mlt_properties_set_int( properties
, "audio_used", audio_used
);
326 // Get silence and don't touch the context
327 *samples
= mlt_sample_calculator( fps
, *frequency
, position
);
328 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
331 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
332 mlt_properties_set_position( properties
, "audio_expected", position
+ 1 );
337 /** Our get frame implementation.
340 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
342 // Create an empty frame
343 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
345 // Update timecode on the frame we're creating
346 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
348 // Set the position of this producer
349 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( *frame
);
350 mlt_properties_set_position( frame_properties
, "vorbis_position", mlt_producer_frame( this ) );
353 mlt_frame_push_audio( *frame
, this );
354 mlt_frame_push_audio( *frame
, producer_get_audio
);
356 // Pass audio properties to the frame
357 mlt_properties_pass_list( frame_properties
, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
359 // Calculate the next timecode
360 mlt_producer_prepare_next( this );