Merge ../mlt
[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 // 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 ++ptr;
158 }
159
160 if ( ov_seekable( ov ) )
161 {
162 // Get the length of the file
163 double length = ov_time_total( ov, -1 );
164
165 // We will treat everything with the producer fps
166 double fps = mlt_profile_fps( profile );
167
168 // Set out and length of file
169 mlt_properties_set_position( properties, "out", ( length * fps ) - 1 );
170 mlt_properties_set_position( properties, "length", ( length * fps ) );
171
172 // Get the vorbis info
173 vorbis_info *vi = ov_info( ov, -1 );
174 mlt_properties_set_int( properties, "frequency", (int) vi->rate );
175 mlt_properties_set_int( properties, "channels", vi->channels );
176
177 // Set some media metadata
178 mlt_properties_set_int( properties, "meta.media.nb_streams", 1 );
179 mlt_properties_set_int( properties, "audio_index", 0 );
180 mlt_properties_set( properties, "meta.media.0.stream.type", "audio" );
181 mlt_properties_set( properties, "meta.media.0.codec.name", "vorbis" );
182 mlt_properties_set( properties, "meta.media.0.codec.long_name", "Vorbis" );
183 }
184 }
185 else
186 {
187 // Clean up
188 free( ov );
189
190 // Must close input file when open fails
191 fclose( input );
192 }
193 }
194
195 return error;
196 }
197
198 /** Convert a frame position to a time code.
199 */
200
201 static double producer_time_of_frame( mlt_producer this, mlt_position position )
202 {
203 return ( double )position / mlt_producer_get_fps( this );
204 }
205
206 /** Get the audio from a frame.
207 */
208
209 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
210 {
211 // Get the properties from the frame
212 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
213
214 // Obtain the frame number of this frame
215 mlt_position position = mlt_properties_get_position( frame_properties, "vorbis_position" );
216
217 // Get the producer
218 mlt_producer this = mlt_frame_pop_audio( frame );
219
220 // Get the producer properties
221 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
222
223 // Get the ogg vorbis file
224 OggVorbis_File *ov = mlt_properties_get_data( properties, "ogg_vorbis_file", NULL );
225
226 // Obtain the expected frame numer
227 mlt_position expected = mlt_properties_get_position( properties, "audio_expected" );
228
229 // Get the fps for this producer
230 double fps = mlt_producer_get_fps( this );
231
232 // Get the vorbis info
233 vorbis_info *vi = ov_info( ov, -1 );
234
235 // Obtain the audio buffer
236 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
237
238 // Get amount of audio used
239 int audio_used = mlt_properties_get_int( properties, "audio_used" );
240
241 // Number of frames to ignore (for ffwd)
242 int ignore = 0;
243
244 // Flag for paused (silence)
245 int paused = 0;
246
247 // Check for audio buffer and create if necessary
248 if ( audio_buffer == NULL )
249 {
250 // Allocate the audio buffer
251 audio_buffer = mlt_pool_alloc( 131072 * sizeof( int16_t ) );
252
253 // And store it on properties for reuse
254 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, mlt_pool_release, NULL );
255 }
256
257 // Seek if necessary
258 if ( position != expected )
259 {
260 if ( position + 1 == expected )
261 {
262 // We're paused - silence required
263 paused = 1;
264 }
265 else if ( position > expected && ( position - expected ) < 250 )
266 {
267 // Fast forward - seeking is inefficient for small distances - just ignore following frames
268 ignore = position - expected;
269 }
270 else
271 {
272 // Seek to the required position
273 ov_time_seek( ov, producer_time_of_frame( this, position ) );
274 expected = position;
275 audio_used = 0;
276 }
277 }
278
279 // Return info in frame
280 *frequency = vi->rate;
281 *channels = vi->channels;
282
283 // Get the audio if required
284 if ( !paused )
285 {
286 // Bitstream section
287 int current_section;
288
289 // Get the number of samples for the current frame
290 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
291
292 while( *samples > audio_used )
293 {
294 // Read the samples
295 int bytes = ov_read( ov, ( char * )( &audio_buffer[ audio_used * 2 ] ), 4096, 0, 2, 1, &current_section );
296
297 // Break if error or eof
298 if ( bytes <= 0 )
299 break;
300
301 // Increment number of samples used
302 audio_used += bytes / ( sizeof( int16_t ) * *channels );
303
304 // Handle ignore
305 while ( ignore && audio_used >= *samples )
306 {
307 ignore --;
308 audio_used -= *samples;
309 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
310 *samples = mlt_sample_calculator( fps, *frequency, expected ++ );
311 }
312 }
313
314 // Now handle the audio if we have enough
315 if ( audio_used >= *samples )
316 {
317 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
318 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
319 audio_used -= *samples;
320 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
321 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, mlt_pool_release, NULL );
322 }
323 else
324 {
325 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
326 audio_used = 0;
327 }
328
329 // Store the number of audio samples still available
330 mlt_properties_set_int( properties, "audio_used", audio_used );
331 }
332 else
333 {
334 // Get silence and don't touch the context
335 *samples = mlt_sample_calculator( fps, *frequency, position );
336 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
337 }
338
339 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
340 mlt_properties_set_position( properties, "audio_expected", position + 1 );
341
342 return 0;
343 }
344
345 /** Our get frame implementation.
346 */
347
348 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
349 {
350 // Create an empty frame
351 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
352
353 // Update timecode on the frame we're creating
354 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
355
356 // Set the position of this producer
357 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( *frame );
358 mlt_properties_set_position( frame_properties, "vorbis_position", mlt_producer_frame( this ) );
359
360 // Set up the audio
361 mlt_frame_push_audio( *frame, this );
362 mlt_frame_push_audio( *frame, producer_get_audio );
363
364 // Pass audio properties to the frame
365 mlt_properties_pass_list( frame_properties, MLT_PRODUCER_PROPERTIES( this ), "frequency, channels" );
366
367 // Calculate the next timecode
368 mlt_producer_prepare_next( this );
369
370 return 0;
371 }