]> git.sesse.net Git - mlt/blob - src/modules/vorbis/producer_vorbis.c
framework: remove global profile, rather share one mlt_profile across a service netwo...
[mlt] / src / modules / vorbis / producer_vorbis.c
1 /*
2  * producer_vorbis.c -- vorbis producer
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 // MLT Header files
22 #include <framework/mlt_producer.h>
23 #include <framework/mlt_frame.h>
24
25 // vorbis Header files
26 #include <vorbis/codec.h>
27 #include <vorbis/vorbisfile.h>
28
29 // System header files
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
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 );
37
38 /** Structure for metadata reading 
39 */
40
41 typedef struct _sw_metadata sw_metadata;
42
43 struct _sw_metadata {
44         char * name;
45         char * content;
46 };
47
48 static sw_metadata *vorbis_metadata_from_str (char * str)
49 {
50         sw_metadata * meta = NULL;
51         int i;
52
53         for (i = 0; str[i]; i++) {
54                 str[i] = tolower(str[i]);
55                 if (str[i] == '=') {
56                         str[i] = '\0';
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]);
61                         break;
62                 }
63         }
64         return meta;
65 }
66
67 /** Constructor for libvorbis.
68 */
69
70 mlt_producer producer_vorbis_init( mlt_profile profile, mlt_service_type type, const char *id, char *file )
71 {
72         mlt_producer this = NULL;
73
74         // Check that we have a non-NULL argument
75         if ( file != NULL )
76         {
77                 // Construct the producer
78                 this = calloc( 1, sizeof( struct mlt_producer_s ) );
79
80                 // Initialise it
81                 if ( mlt_producer_init( this, NULL ) == 0 )
82                 {
83                         // Get the properties
84                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
85
86                         // Set the resource property (required for all producers)
87                         mlt_properties_set( properties, "resource", file );
88
89                         // Register our get_frame implementation
90                         this->get_frame = producer_get_frame;
91
92                         // Open the file
93                         if ( producer_open( this, file ) != 0 )
94                         {
95                                 // Clean up
96                                 mlt_producer_close( this );
97                                 this = NULL;
98                         }
99                 }
100         }
101
102         return this;
103 }
104
105 /** Destuctor for ogg files.
106 */
107
108 static void producer_file_close( void *file )
109 {
110         if ( file != NULL )
111         {
112                 // Close the ogg vorbis structure
113                 ov_clear( file );
114
115                 // Free the memory
116                 free( file );
117         }
118 }
119
120 /** Open the file.
121 */
122
123 static int producer_open( mlt_producer this, char *file )
124 {
125         // FILE pointer for file
126         FILE *input = fopen( file, "r" );
127
128         // Error code to return
129         int error = input == NULL;
130
131         // Continue if file is open
132         if ( error == 0 )
133         {
134                 // OggVorbis file structure
135                 OggVorbis_File *ov = calloc( 1, sizeof( OggVorbis_File ) );
136
137                 // Attempt to open the stream
138                 error = ov == NULL || ov_open( input, ov, NULL, 0 ) != 0;
139
140                 // Assign to producer properties if successful
141                 if ( error == 0 )
142                 {
143                         // Get the properties
144                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
145
146                         // Assign the ov structure
147                         mlt_properties_set_data( properties, "ogg_vorbis_file", ov, 0, producer_file_close, NULL );
148
149                         // Read metadata
150                         sw_metadata * metadata = NULL;
151                         char **ptr = ov_comment(ov, -1)->user_comments;
152                         while(*ptr) {
153                                 metadata = vorbis_metadata_from_str (*ptr);
154                                 if (metadata != NULL)
155                                         mlt_properties_set(properties, metadata->name, metadata->content);
156                                 ++ptr;
157                         }
158
159                         if ( ov_seekable( ov ) )
160                         {
161                                 // Get the length of the file
162                         double length = ov_time_total( ov, -1 );
163
164                                 // We will treat everything with the producer fps
165                                 double fps = mlt_producer_get_fps( this );
166
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 ) );
170
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 );
175                         }
176                 }
177                 else
178                 {
179                         // Clean up
180                         free( ov );
181
182                         // Must close input file when open fails
183                         fclose( input );
184                 }
185         }
186
187         return error;
188 }
189
190 /** Convert a frame position to a time code.
191 */
192
193 static double producer_time_of_frame( mlt_producer this, mlt_position position )
194 {
195         return ( double )position / mlt_producer_get_fps( this );
196 }
197
198 /** Get the audio from a frame.
199 */
200
201 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
202 {
203         // Get the properties from the frame
204         mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
205
206         // Obtain the frame number of this frame
207         mlt_position position = mlt_properties_get_position( frame_properties, "vorbis_position" );
208
209         // Get the producer 
210         mlt_producer this = mlt_frame_pop_audio( frame );
211
212         // Get the producer properties
213         mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
214
215         // Get the ogg vorbis file
216         OggVorbis_File *ov = mlt_properties_get_data( properties, "ogg_vorbis_file", NULL );
217
218         // Obtain the expected frame numer
219         mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
220
221         // Get the fps for this producer
222         double fps = mlt_producer_get_fps( this );
223
224         // Get the vorbis info
225         vorbis_info *vi = ov_info( ov, -1 );
226
227         // Obtain the audio buffer
228         int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
229
230         // Get amount of audio used
231         int audio_used =  mlt_properties_get_int( properties, "audio_used" );
232
233         // Number of frames to ignore (for ffwd)
234         int ignore = 0;
235
236         // Flag for paused (silence) 
237         int paused = 0;
238
239         // Check for audio buffer and create if necessary
240         if ( audio_buffer == NULL )
241         {
242                 // Allocate the audio buffer
243                 audio_buffer = mlt_pool_alloc( 131072 * sizeof( int16_t ) );
244
245                 // And store it on properties for reuse
246                 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, mlt_pool_release, NULL );
247         }
248
249         // Seek if necessary
250         if ( position != expected )
251         {
252                 if ( position + 1 == expected )
253                 {
254                         // We're paused - silence required
255                         paused = 1;
256                 }
257                 else if ( position > expected && ( position - expected ) < 250 )
258                 {
259                         // Fast forward - seeking is inefficient for small distances - just ignore following frames
260                         ignore = position - expected;
261                 }
262                 else
263                 {
264                         // Seek to the required position
265                         ov_time_seek( ov, producer_time_of_frame( this, position ) );
266                         expected = position;
267                         audio_used = 0;
268                 }
269         }
270
271         // Return info in frame
272         *frequency = vi->rate;
273         *channels = vi->channels;
274
275         // Get the audio if required
276         if ( !paused )
277         {
278                 // Bitstream section
279                 int current_section;
280
281                 // Get the number of samples for the current frame
282                 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
283
284                 while( *samples > audio_used  )
285                 {
286                         // Read the samples
287                         int bytes = ov_read( ov, ( char * )( &audio_buffer[ audio_used * 2 ] ), 4096, 0, 2, 1, &current_section );
288
289                         // Break if error or eof
290                         if ( bytes <= 0 )
291                                 break;
292
293                         // Increment number of samples used
294                         audio_used += bytes / ( sizeof( int16_t ) * *channels );
295
296                         // Handle ignore
297                         while ( ignore && audio_used >= *samples )
298                         {
299                                 ignore --;
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 ++ );
303                         }
304                 }
305
306                 // Now handle the audio if we have enough
307                 if ( audio_used >= *samples )
308                 {
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 );
314                 }
315                 else
316                 {
317                         mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
318                         audio_used = 0;
319                 }
320                 
321                 // Store the number of audio samples still available
322                 mlt_properties_set_int( properties, "audio_used", audio_used );
323         }
324         else
325         {
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 );
329         }
330
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 );
333
334         return 0;
335 }
336
337 /** Our get frame implementation.
338 */
339
340 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
341 {
342         // Create an empty frame
343         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
344
345         // Update timecode on the frame we're creating
346         mlt_frame_set_position( *frame, mlt_producer_position( this ) );
347
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 ) );
351
352         // Set up the audio
353         mlt_frame_push_audio( *frame, this );
354         mlt_frame_push_audio( *frame, producer_get_audio );
355
356         // Pass audio properties to the frame
357         mlt_properties_pass_list( frame_properties, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
358
359         // Calculate the next timecode
360         mlt_producer_prepare_next( this );
361
362         return 0;
363 }