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_producer_get_fps( this );
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 return ( double )position
/ mlt_producer_get_fps( this );
200 /** Get the audio from a frame.
203 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
205 // Get the properties from the frame
206 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
208 // Obtain the frame number of this frame
209 mlt_position position
= mlt_properties_get_position( frame_properties
, "vorbis_position" );
212 mlt_producer
this = mlt_frame_pop_audio( frame
);
214 // Get the producer properties
215 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
217 // Get the ogg vorbis file
218 OggVorbis_File
*ov
= mlt_properties_get_data( properties
, "ogg_vorbis_file", NULL
);
220 // Obtain the expected frame numer
221 mlt_position expected
= mlt_properties_get_position( properties
, "audio_expected" );
223 // Get the fps for this producer
224 double fps
= mlt_producer_get_fps( this );
226 // Get the vorbis info
227 vorbis_info
*vi
= ov_info( ov
, -1 );
229 // Obtain the audio buffer
230 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
232 // Get amount of audio used
233 int audio_used
= mlt_properties_get_int( properties
, "audio_used" );
235 // Number of frames to ignore (for ffwd)
238 // Flag for paused (silence)
241 // Check for audio buffer and create if necessary
242 if ( audio_buffer
== NULL
)
244 // Allocate the audio buffer
245 audio_buffer
= mlt_pool_alloc( 131072 * sizeof( int16_t ) );
247 // And store it on properties for reuse
248 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, mlt_pool_release
, NULL
);
252 if ( position
!= expected
)
254 if ( position
+ 1 == expected
)
256 // We're paused - silence required
259 else if ( position
> expected
&& ( position
- expected
) < 250 )
261 // Fast forward - seeking is inefficient for small distances - just ignore following frames
262 ignore
= position
- expected
;
266 // Seek to the required position
267 ov_time_seek( ov
, producer_time_of_frame( this, position
) );
273 // Return info in frame
274 *frequency
= vi
->rate
;
275 *channels
= vi
->channels
;
277 // Get the audio if required
283 // Get the number of samples for the current frame
284 *samples
= mlt_sample_calculator( fps
, *frequency
, expected
++ );
286 while( *samples
> audio_used
)
289 int bytes
= ov_read( ov
, ( char * )( &audio_buffer
[ audio_used
* 2 ] ), 4096, 0, 2, 1, ¤t_section
);
291 // Break if error or eof
295 // Increment number of samples used
296 audio_used
+= bytes
/ ( sizeof( int16_t ) * *channels
);
299 while ( ignore
&& audio_used
>= *samples
)
302 audio_used
-= *samples
;
303 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
304 *samples
= mlt_sample_calculator( fps
, *frequency
, expected
++ );
308 // Now handle the audio if we have enough
309 if ( audio_used
>= *samples
)
311 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
312 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
313 audio_used
-= *samples
;
314 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
315 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, mlt_pool_release
, NULL
);
319 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
323 // Store the number of audio samples still available
324 mlt_properties_set_int( properties
, "audio_used", audio_used
);
328 // Get silence and don't touch the context
329 *samples
= mlt_sample_calculator( fps
, *frequency
, position
);
330 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
333 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
334 mlt_properties_set_position( properties
, "audio_expected", position
+ 1 );
339 /** Our get frame implementation.
342 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
344 // Create an empty frame
345 *frame
= mlt_frame_init( );
347 // Update timecode on the frame we're creating
348 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
350 // Set the position of this producer
351 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( *frame
);
352 mlt_properties_set_position( frame_properties
, "vorbis_position", mlt_producer_frame( this ) );
355 mlt_frame_push_audio( *frame
, this );
356 mlt_frame_push_audio( *frame
, producer_get_audio
);
358 // Pass audio properties to the frame
359 mlt_properties_pass_list( frame_properties
, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
361 // Calculate the next timecode
362 mlt_producer_prepare_next( this );