2 * producer_vorbis.c -- vorbis producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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
35 // Forward references.
36 static int producer_open( mlt_producer
this, char *file
);
37 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
);
39 /** Constructor for libvorbis.
42 mlt_producer
producer_vorbis_init( char *file
)
44 mlt_producer
this = NULL
;
46 // Check that we have a non-NULL argument
49 // Construct the producer
50 this = calloc( 1, sizeof( struct mlt_producer_s
) );
53 if ( mlt_producer_init( this, NULL
) == 0 )
56 mlt_properties properties
= mlt_producer_properties( this );
58 // Set the resource property (required for all producers)
59 mlt_properties_set( properties
, "resource", file
);
61 // Register our get_frame implementation
62 this->get_frame
= producer_get_frame
;
65 if ( producer_open( this, file
) != 0 )
68 mlt_producer_close( this );
77 /** Destuctor for ogg files.
80 static void producer_file_close( void *file
)
84 // Close the ogg vorbis structure
95 static int producer_open( mlt_producer
this, char *file
)
97 // FILE pointer for file
98 FILE *input
= fopen( file
, "r" );
100 // Error code to return
101 int error
= input
== NULL
;
103 // Continue if file is open
106 // OggVorbis file structure
107 OggVorbis_File
*ov
= calloc( 1, sizeof( OggVorbis_File
) );
109 // Attempt to open the stream
110 error
= ov
== NULL
|| ov_open( input
, ov
, NULL
, 0 ) != 0;
112 // Assign to producer properties if successful
115 // Get the properties
116 mlt_properties properties
= mlt_producer_properties( this );
118 // Assign the ov structure
119 mlt_properties_set_data( properties
, "ogg_vorbis_file", ov
, 0, producer_file_close
, NULL
);
121 if ( ov_seekable( ov
) )
123 // Get the length of the file
124 double length
= ov_time_total( ov
, -1 );
126 // We will treat everything with the producer fps
127 double fps
= mlt_properties_get_double( properties
, "fps" );
129 // Set out and length of file
130 mlt_properties_set_position( properties
, "out", ( length
* fps
) - 1 );
131 mlt_properties_set_position( properties
, "length", ( length
* fps
) );
139 // Must close input file when open fails
147 /** Convert a frame position to a time code.
150 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
152 // Get the properties
153 mlt_properties properties
= mlt_producer_properties( this );
156 double fps
= mlt_properties_get_double( properties
, "fps" );
159 return ( double )position
/ fps
;
162 /** Get the audio from a frame.
165 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
167 // Get the properties from the frame
168 mlt_properties frame_properties
= mlt_frame_properties( frame
);
170 // Obtain the frame number of this frame
171 mlt_position position
= mlt_properties_get_position( frame_properties
, "vorbis_position" );
174 mlt_producer
this = mlt_properties_get_data( frame_properties
, "vorbis_producer", NULL
);
176 // Get the producer properties
177 mlt_properties properties
= mlt_producer_properties( this );
179 // Get the ogg vorbis file
180 OggVorbis_File
*ov
= mlt_properties_get_data( properties
, "ogg_vorbis_file", NULL
);
182 // Obtain the expected frame numer
183 mlt_position expected
= mlt_properties_get_position( properties
, "audio_expected" );
185 // Get the fps for this producer
186 double fps
= mlt_properties_get_double( properties
, "fps" );
188 // Get the vorbis info
189 vorbis_info
*vi
= ov_info( ov
, -1 );
191 // Obtain the audio buffer
192 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
194 // Get amount of audio used
195 int audio_used
= mlt_properties_get_int( properties
, "audio_used" );
197 // Number of frames to ignore (for ffwd)
200 // Flag for paused (silence)
203 // Check for audio buffer and create if necessary
204 if ( audio_buffer
== NULL
)
207 void *release
= NULL
;
209 // Allocate the audio buffer
210 audio_buffer
= mlt_pool_allocate( 131072 * sizeof( int16_t ), &release
);
212 // And store it on properties for reuse
213 mlt_properties_set_data( properties
, "audio_buffer_release", release
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
214 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, NULL
, NULL
);
218 if ( position
!= expected
)
220 if ( position
+ 1 == expected
)
222 // We're paused - silence required
225 else if ( position
> expected
&& ( position
- expected
) < 250 )
227 // Fast forward - seeking is inefficient for small distances - just ignore following frames
228 ignore
= position
- expected
;
232 // Seek to the required position
233 ov_time_seek( ov
, producer_time_of_frame( this, position
) );
239 // Return info in frame
240 *frequency
= vi
->rate
;
241 *channels
= vi
->channels
;
243 // Get the audio if required
249 // Get the number of samples for the current frame
250 *samples
= mlt_sample_calculator( fps
, *frequency
, expected
++ );
252 while( *samples
> audio_used
)
255 int bytes
= ov_read( ov
, ( char * )( &audio_buffer
[ audio_used
* 2 ] ), 4096, 0, 2, 1, ¤t_section
);
257 // Break if error or eof
261 // Increment number of samples used
262 audio_used
+= bytes
/ ( sizeof( int16_t ) * *channels
);
265 while ( ignore
&& audio_used
>= *samples
)
268 audio_used
-= *samples
;
269 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
270 *samples
= mlt_sample_calculator( fps
, *frequency
, expected
++ );
274 // Now handle the audio if we have enough
275 if ( audio_used
>= *samples
)
277 void *release
= NULL
;
278 *buffer
= mlt_pool_allocate( *samples
* *channels
* sizeof( int16_t ), &release
);
279 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
280 audio_used
-= *samples
;
281 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
282 mlt_properties_set_data( frame_properties
, "audio_release", release
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
283 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, NULL
, NULL
);
287 frame
->get_audio
= NULL
;
288 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
292 // Store the number of audio samples still available
293 mlt_properties_set_int( properties
, "audio_used", audio_used
);
297 // Get silence and don't touch the context
298 frame
->get_audio
= NULL
;
299 *samples
= mlt_sample_calculator( fps
, *frequency
, position
);
300 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
303 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
304 mlt_properties_set_position( properties
, "audio_expected", position
+ 1 );
309 /** Set up audio handling.
312 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
314 // Get the properties
315 mlt_properties properties
= mlt_frame_properties( frame
);
317 // Set the audio method
318 frame
->get_audio
= producer_get_audio
;
320 // Set the producer on the frame
321 mlt_properties_set_data( properties
, "vorbis_producer", this, 0, NULL
, NULL
);
324 /** Our get frame implementation.
327 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
329 // Create an empty frame
330 *frame
= mlt_frame_init( );
332 // Update timecode on the frame we're creating
333 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
335 // Set the position of this producer
336 mlt_properties_set_position( mlt_frame_properties( *frame
), "vorbis_position", mlt_producer_get_in( this ) + mlt_producer_position( this ) );
339 producer_set_up_audio( this, *frame
);
341 // Calculate the next timecode
342 mlt_producer_prepare_next( this );