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