avformat/factory.c, jackrack/jack_rack.c, jackrack/plugin_settings.c, vmfx/filter_chr...
[melted] / 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_properties_get_double( properties, "fps" );
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 // Get the properties
198 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
199
200 // Obtain the fps
201 double fps = mlt_properties_get_double( properties, "fps" );
202
203 // Do the calc
204 return ( double )position / fps;
205 }
206
207 /** Get the audio from a frame.
208 */
209
210 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
211 {
212 // Get the properties from the frame
213 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
214
215 // Obtain the frame number of this frame
216 mlt_position position = mlt_properties_get_position( frame_properties, "vorbis_position" );
217
218 // Get the producer
219 mlt_producer this = mlt_frame_pop_audio( frame );
220
221 // Get the producer properties
222 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
223
224 // Get the ogg vorbis file
225 OggVorbis_File *ov = mlt_properties_get_data( properties, "ogg_vorbis_file", NULL );
226
227 // Obtain the expected frame numer
228 mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
229
230 // Get the fps for this producer
231 double fps = mlt_properties_get_double( properties, "fps" );
232
233 // Get the vorbis info
234 vorbis_info *vi = ov_info( ov, -1 );
235
236 // Obtain the audio buffer
237 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
238
239 // Get amount of audio used
240 int audio_used = mlt_properties_get_int( properties, "audio_used" );
241
242 // Number of frames to ignore (for ffwd)
243 int ignore = 0;
244
245 // Flag for paused (silence)
246 int paused = 0;
247
248 // Check for audio buffer and create if necessary
249 if ( audio_buffer == NULL )
250 {
251 // Allocate the audio buffer
252 audio_buffer = mlt_pool_alloc( 131072 * sizeof( int16_t ) );
253
254 // And store it on properties for reuse
255 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, mlt_pool_release, NULL );
256 }
257
258 // Seek if necessary
259 if ( position != expected )
260 {
261 if ( position + 1 == expected )
262 {
263 // We're paused - silence required
264 paused = 1;
265 }
266 else if ( position > expected && ( position - expected ) < 250 )
267 {
268 // Fast forward - seeking is inefficient for small distances - just ignore following frames
269 ignore = position - expected;
270 }
271 else
272 {
273 // Seek to the required position
274 ov_time_seek( ov, producer_time_of_frame( this, position ) );
275 expected = position;
276 audio_used = 0;
277 }
278 }
279
280 // Return info in frame
281 *frequency = vi->rate;
282 *channels = vi->channels;
283
284 // Get the audio if required
285 if ( !paused )
286 {
287 // Bitstream section
288 int current_section;
289
290 // Get the number of samples for the current frame
291 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
292
293 while( *samples > audio_used )
294 {
295 // Read the samples
296 int bytes = ov_read( ov, ( char * )( &audio_buffer[ audio_used * 2 ] ), 4096, 0, 2, 1, &current_section );
297
298 // Break if error or eof
299 if ( bytes <= 0 )
300 break;
301
302 // Increment number of samples used
303 audio_used += bytes / ( sizeof( int16_t ) * *channels );
304
305 // Handle ignore
306 while ( ignore && audio_used >= *samples )
307 {
308 ignore --;
309 audio_used -= *samples;
310 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
311 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
312 }
313 }
314
315 // Now handle the audio if we have enough
316 if ( audio_used >= *samples )
317 {
318 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
319 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
320 audio_used -= *samples;
321 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
322 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, mlt_pool_release, NULL );
323 }
324 else
325 {
326 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
327 audio_used = 0;
328 }
329
330 // Store the number of audio samples still available
331 mlt_properties_set_int( properties, "audio_used", audio_used );
332 }
333 else
334 {
335 // Get silence and don't touch the context
336 *samples = mlt_sample_calculator( fps, *frequency, position );
337 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
338 }
339
340 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
341 mlt_properties_set_position( properties, "audio_expected", position + 1 );
342
343 return 0;
344 }
345
346 /** Our get frame implementation.
347 */
348
349 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
350 {
351 // Create an empty frame
352 *frame = mlt_frame_init( );
353
354 // Update timecode on the frame we're creating
355 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
356
357 // Set the position of this producer
358 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( *frame );
359 mlt_properties_set_position( frame_properties, "vorbis_position", mlt_producer_frame( this ) );
360
361 // Set up the audio
362 mlt_frame_push_audio( *frame, this );
363 mlt_frame_push_audio( *frame, producer_get_audio );
364
365 // Pass audio properties to the frame
366 mlt_properties_pass_list( frame_properties, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
367
368 // Calculate the next timecode
369 mlt_producer_prepare_next( this );
370
371 return 0;
372 }