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