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