3 * \brief interface for all frame classes
6 * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7 * \author Charles Yates <charles.yates@pandora.be>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "mlt_frame.h"
25 #include "mlt_producer.h"
26 #include "mlt_factory.h"
27 #include "mlt_profile.h"
34 /** Construct a frame object.
36 * \public \memberof mlt_frame_s
37 * \param service the pointer to any service that can provide access to the profile
38 * \return a frame object on success or NULL if there was an allocation error
41 mlt_frame mlt_frame_init( mlt_service service )
44 mlt_frame self = calloc( sizeof( struct mlt_frame_s ), 1 );
48 mlt_profile profile = mlt_service_profile( service );
50 // Initialise the properties
51 mlt_properties properties = &self->parent;
52 mlt_properties_init( properties, self );
54 // Set default properties on the frame
55 mlt_properties_set_position( properties, "_position", 0.0 );
56 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
57 mlt_properties_set_int( properties, "width", profile? profile->width : 720 );
58 mlt_properties_set_int( properties, "height", profile? profile->height : 576 );
59 mlt_properties_set_int( properties, "normalised_width", profile? profile->width : 720 );
60 mlt_properties_set_int( properties, "normalised_height", profile? profile->height : 576 );
61 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
62 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
63 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
65 // Construct stacks for frames and methods
66 self->stack_image = mlt_deque_init( );
67 self->stack_audio = mlt_deque_init( );
68 self->stack_service = mlt_deque_init( );
74 /** Get a frame's properties.
76 * \public \memberof mlt_frame_s
78 * \return the frame's properties or NULL if an invalid frame is supplied
81 mlt_properties mlt_frame_properties( mlt_frame self )
83 return self != NULL ? &self->parent : NULL;
86 /** Determine if the frame will produce a test card image.
88 * \public \memberof mlt_frame_s
90 * \return true (non-zero) if this will produce from a test card
93 int mlt_frame_is_test_card( mlt_frame self )
95 return mlt_deque_count( self->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( self ), "test_image" );
98 /** Determine if the frame will produce audio from a test card.
100 * \public \memberof mlt_frame_s
101 * \param self a frame
102 * \return true (non-zero) if this will produce from a test card
105 int mlt_frame_is_test_audio( mlt_frame self )
107 return mlt_deque_count( self->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( self ), "test_audio" );
110 /** Get the sample aspect ratio of the frame.
112 * \public \memberof mlt_frame_s
113 * \param self a frame
114 * \return the aspect ratio
117 double mlt_frame_get_aspect_ratio( mlt_frame self )
119 return mlt_properties_get_double( MLT_FRAME_PROPERTIES( self ), "aspect_ratio" );
122 /** Set the sample aspect ratio of the frame.
124 * \public \memberof mlt_frame_s
125 * \param self a frame
126 * \param value the new image sample aspect ratio
127 * \return true if error
130 int mlt_frame_set_aspect_ratio( mlt_frame self, double value )
132 return mlt_properties_set_double( MLT_FRAME_PROPERTIES( self ), "aspect_ratio", value );
135 /** Get the time position of this frame.
137 * \public \memberof mlt_frame_s
138 * \param self a frame
139 * \return the position
142 mlt_position mlt_frame_get_position( mlt_frame self )
144 int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( self ), "_position" );
145 return pos < 0 ? 0 : pos;
148 /** Set the time position of this frame.
150 * \public \memberof mlt_frame_s
151 * \param self a frame
152 * \param value the position
153 * \return true if error
156 int mlt_frame_set_position( mlt_frame self, mlt_position value )
158 return mlt_properties_set_position( MLT_FRAME_PROPERTIES( self ), "_position", value );
161 /** Stack a get_image callback.
163 * \public \memberof mlt_frame_s
164 * \param self a frame
165 * \param the get_image callback
166 * \return true if error
169 int mlt_frame_push_get_image( mlt_frame self, mlt_get_image get_image )
171 return mlt_deque_push_back( self->stack_image, get_image );
174 /** Pop a get_image callback.
176 * \public \memberof mlt_frame_s
177 * \param self a frame
178 * \return the get_image callback
181 mlt_get_image mlt_frame_pop_get_image( mlt_frame self )
183 return mlt_deque_pop_back( self->stack_image );
188 * \public \memberof mlt_frame_s
189 * \param self a frame
190 * \param that the frame to push onto \p self
191 * \return true if error
194 int mlt_frame_push_frame( mlt_frame self, mlt_frame that )
196 return mlt_deque_push_back( self->stack_image, that );
201 * \public \memberof mlt_frame_s
202 * \param self a frame
203 * \return a frame that was previously pushed
206 mlt_frame mlt_frame_pop_frame( mlt_frame self )
208 return mlt_deque_pop_back( self->stack_image );
213 * \public \memberof mlt_frame_s
214 * \param self a frame
215 * \param that an opaque pointer
216 * \return true if error
219 int mlt_frame_push_service( mlt_frame self, void *that )
221 return mlt_deque_push_back( self->stack_image, that );
226 * \public \memberof mlt_frame_s
227 * \param self a frame
228 * \return an opaque pointer to something previously pushed
231 void *mlt_frame_pop_service( mlt_frame self )
233 return mlt_deque_pop_back( self->stack_image );
238 * \public \memberof mlt_frame_s
239 * \param self a frame
240 * \param that an integer
241 * \return true if error
244 int mlt_frame_push_service_int( mlt_frame self, int that )
246 return mlt_deque_push_back_int( self->stack_image, that );
251 * \public \memberof mlt_frame_s
252 * \param self a frame
253 * \return an integer that was previously pushed
256 int mlt_frame_pop_service_int( mlt_frame self )
258 return mlt_deque_pop_back_int( self->stack_image );
261 /** Push an audio item on the stack.
263 * \public \memberof mlt_frame_s
264 * \param self a frame
265 * \param that an opaque pointer
266 * \return true if error
269 int mlt_frame_push_audio( mlt_frame self, void *that )
271 return mlt_deque_push_back( self->stack_audio, that );
274 /** Pop an audio item from the stack
276 * \public \memberof mlt_frame_s
277 * \param self a frame
278 * \return an opaque pointer to something that was pushed onto the frame's audio stack
281 void *mlt_frame_pop_audio( mlt_frame self )
283 return mlt_deque_pop_back( self->stack_audio );
286 /** Return the service stack
288 * \public \memberof mlt_frame_s
289 * \param self a frame
290 * \return the service stack
293 mlt_deque mlt_frame_service_stack( mlt_frame self )
295 return self->stack_service;
298 /** Set a new image on the frame.
300 * \public \memberof mlt_frame_s
301 * \param self a frame
302 * \param image a pointer to the raw image data
303 * \param size the size of the image data in bytes (optional)
304 * \param destroy a function to deallocate \p image when the frame is closed (optional)
305 * \return true if error
308 int mlt_frame_set_image( mlt_frame self, uint8_t *image, int size, mlt_destructor destroy )
310 return mlt_properties_set_data( MLT_FRAME_PROPERTIES( self ), "image", image, size, destroy, NULL );
313 /** Set a new alpha channel on the frame.
315 * \public \memberof mlt_frame_s
316 * \param self a frame
317 * \param alpha a pointer to the alpha channel
318 * \param size the size of the alpha channel in bytes (optional)
319 * \param destroy a function to deallocate \p alpha when the frame is closed (optional)
320 * \return true if error
323 int mlt_frame_set_alpha( mlt_frame self, uint8_t *alpha, int size, mlt_destructor destroy )
325 return mlt_properties_set_data( MLT_FRAME_PROPERTIES( self ), "alpha", alpha, size, destroy, NULL );
328 /** Replace image stack with the information provided.
330 * This might prove to be unreliable and restrictive - the idea is that a transition
331 * which normally uses two images may decide to only use the b frame (ie: in the case
332 * of a composite where the b frame completely obscures the a frame).
334 * The image must be writable and the destructor for the image itself must be taken
335 * care of on another frame and that frame cannot have a replace applied to it...
336 * Further it assumes that no alpha mask is in use.
338 * For these reasons, it can only be used in a specific situation - when you have
339 * multiple tracks each with their own transition and these transitions are applied
340 * in a strictly reversed order (ie: highest numbered [lowest track] is processed
343 * More reliable approach - the cases should be detected during the process phase
344 * and the upper tracks should simply not be invited to stack...
346 * \public \memberof mlt_frame_s
347 * \param self a frame
348 * \param image a new image
349 * \param format the image format
350 * \param width the width of the new image
351 * \param height the height of the new image
354 void mlt_frame_replace_image( mlt_frame self, uint8_t *image, mlt_image_format format, int width, int height )
356 // Remove all items from the stack
357 while( mlt_deque_pop_back( self->stack_image ) ) ;
359 // Update the information
360 mlt_properties_set_data( MLT_FRAME_PROPERTIES( self ), "image", image, 0, NULL, NULL );
361 mlt_properties_set_int( MLT_FRAME_PROPERTIES( self ), "width", width );
362 mlt_properties_set_int( MLT_FRAME_PROPERTIES( self ), "height", height );
363 mlt_properties_set_int( MLT_FRAME_PROPERTIES( self ), "format", format );
364 self->get_alpha_mask = NULL;
367 /** Get the short name for an image format.
369 * \public \memberof mlt_frame_s
370 * \param format the image format
374 const char * mlt_image_format_name( mlt_image_format format )
378 case mlt_image_none: return "none";
379 case mlt_image_rgb24: return "rgb24";
380 case mlt_image_rgb24a: return "rgb24a";
381 case mlt_image_yuv422: return "yuv422";
382 case mlt_image_yuv420p: return "yuv420p";
383 case mlt_image_opengl: return "opengl";
388 /** Get the image associated to the frame.
390 * You should express the desired format, width, and height as inputs. As long
391 * as the loader producer was used to generate this or the imageconvert filter
392 * was attached, then you will get the image back in the format you desire.
393 * However, you do not always get the width and height you request depending
394 * on properties and filters. You do not need to supply a pre-allocated
395 * buffer, but you should always supply the desired image format.
397 * \public \memberof mlt_frame_s
398 * \param self a frame
399 * \param[out] buffer an image buffer
400 * \param[in,out] format the image format
401 * \param[in,out] width the horizontal size in pixels
402 * \param[in,out] height the vertical size in pixels
403 * \param writable whether or not you will need to be able to write to the memory returned in \p buffer
404 * \return true if error
405 * \todo Better describe the width and height as inputs.
408 int mlt_frame_get_image( mlt_frame self, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
410 mlt_properties properties = MLT_FRAME_PROPERTIES( self );
411 mlt_get_image get_image = mlt_frame_pop_get_image( self );
412 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
413 mlt_image_format requested_format = *format;
418 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
419 error = get_image( self, buffer, format, width, height, writable );
420 if ( !error && *buffer )
422 mlt_properties_set_int( properties, "width", *width );
423 mlt_properties_set_int( properties, "height", *height );
424 if ( self->convert_image && *buffer )
425 self->convert_image( self, buffer, format, requested_format );
426 mlt_properties_set_int( properties, "format", *format );
430 // Cause the image to be loaded from test card or fallback (white) below.
431 mlt_frame_get_image( self, buffer, format, width, height, writable );
434 else if ( mlt_properties_get_data( properties, "image", NULL ) )
436 *format = mlt_properties_get_int( properties, "format" );
437 *buffer = mlt_properties_get_data( properties, "image", NULL );
438 *width = mlt_properties_get_int( properties, "width" );
439 *height = mlt_properties_get_int( properties, "height" );
440 if ( self->convert_image && *buffer )
442 self->convert_image( self, buffer, format, requested_format );
443 mlt_properties_set_int( properties, "format", *format );
448 mlt_frame test_frame = NULL;
449 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
452 mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
453 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
454 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
455 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
456 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
457 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
458 // mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
459 // mlt_properties_set_int( properties, "width", *width );
460 // mlt_properties_set_int( properties, "height", *height );
461 // mlt_properties_set_int( properties, "format", *format );
465 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
466 mlt_frame_get_image( self, buffer, format, width, height, writable );
475 *width = *width == 0 ? 720 : *width;
476 *height = *height == 0 ? 576 : *height;
477 size = *width * *height;
479 mlt_properties_set_int( properties, "format", *format );
480 mlt_properties_set_int( properties, "width", *width );
481 mlt_properties_set_int( properties, "height", *height );
482 mlt_properties_set_int( properties, "aspect_ratio", 0 );
490 case mlt_image_rgb24:
493 *buffer = mlt_pool_alloc( size );
495 memset( *buffer, 255, size );
497 case mlt_image_rgb24a:
498 case mlt_image_opengl:
501 *buffer = mlt_pool_alloc( size );
503 memset( *buffer, 255, size );
505 case mlt_image_yuv422:
508 *buffer = mlt_pool_alloc( size );
511 while ( p != NULL && p != q )
517 case mlt_image_yuv420p:
519 *buffer = mlt_pool_alloc( size );
521 memset( *buffer, 255, size );
525 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
526 mlt_properties_set_int( properties, "test_image", 1 );
532 /** Get the alpha channel associated to the frame.
534 * \public \memberof mlt_frame_s
535 * \param self a frame
536 * \return the alpha channel
539 uint8_t *mlt_frame_get_alpha_mask( mlt_frame self )
541 uint8_t *alpha = NULL;
544 if ( self->get_alpha_mask != NULL )
545 alpha = self->get_alpha_mask( self );
547 alpha = mlt_properties_get_data( &self->parent, "alpha", NULL );
550 int size = mlt_properties_get_int( &self->parent, "width" ) * mlt_properties_get_int( &self->parent, "height" );
551 alpha = mlt_pool_alloc( size );
552 memset( alpha, 255, size );
553 mlt_properties_set_data( &self->parent, "alpha", alpha, size, mlt_pool_release, NULL );
559 /** Get the short name for an audio format.
561 * You do not need to deallocate the returned string.
562 * \public \memberof mlt_frame_s
563 * \param self a frame
564 * \param format an image format enum
565 * \return a string for the name of the image format
568 const char * mlt_audio_format_name( mlt_audio_format format )
572 case mlt_audio_none: return "none";
573 case mlt_audio_s16: return "s16";
574 case mlt_audio_s32: return "s32";
575 case mlt_audio_float: return "float";
580 /** Get the audio associated to the frame.
582 * You should express the desired format, frequency, channels, and samples as inputs. As long
583 * as the loader producer was used to generate this or the audioconvert filter
584 * was attached, then you will get the audio back in the format you desire.
585 * However, you do not always get the channels and samples you request depending
586 * on properties and filters. You do not need to supply a pre-allocated
587 * buffer, but you should always supply the desired audio format.
588 * The audio is always in interleaved format.
589 * You should use the \p mlt_sample_calculator to determine the number of samples you want.
591 * \public \memberof mlt_frame_s
592 * \param self a frame
593 * \param[out] buffer an audio buffer
594 * \param[in,out] format the audio format
595 * \param[in,out] frequency the sample rate
596 * \param[in,out] channels
597 * \param[in,out] samples the number of samples per frame
598 * \return true if error
601 int mlt_frame_get_audio( mlt_frame self, void **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
603 mlt_get_audio get_audio = mlt_frame_pop_audio( self );
604 mlt_properties properties = MLT_FRAME_PROPERTIES( self );
605 int hide = mlt_properties_get_int( properties, "test_audio" );
606 mlt_audio_format requested_format = *format;
608 if ( hide == 0 && get_audio != NULL )
610 get_audio( self, buffer, format, frequency, channels, samples );
611 mlt_properties_set_int( properties, "audio_frequency", *frequency );
612 mlt_properties_set_int( properties, "audio_channels", *channels );
613 mlt_properties_set_int( properties, "audio_samples", *samples );
614 mlt_properties_set_int( properties, "audio_format", *format );
615 if ( self->convert_audio )
616 self->convert_audio( self, buffer, format, requested_format );
618 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
620 *buffer = mlt_properties_get_data( properties, "audio", NULL );
621 *format = mlt_properties_get_int( properties, "audio_format" );
622 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
623 *channels = mlt_properties_get_int( properties, "audio_channels" );
624 *samples = mlt_properties_get_int( properties, "audio_samples" );
625 if ( self->convert_audio )
626 self->convert_audio( self, buffer, format, requested_format );
631 *samples = *samples <= 0 ? 1920 : *samples;
632 *channels = *channels <= 0 ? 2 : *channels;
633 *frequency = *frequency <= 0 ? 48000 : *frequency;
634 mlt_properties_set_int( properties, "audio_frequency", *frequency );
635 mlt_properties_set_int( properties, "audio_channels", *channels );
636 mlt_properties_set_int( properties, "audio_samples", *samples );
637 mlt_properties_set_int( properties, "audio_format", *format );
646 size = *samples * *channels * sizeof( int16_t );
649 size = *samples * *channels * sizeof( int32_t );
651 case mlt_audio_float:
652 size = *samples * *channels * sizeof( float );
656 *buffer = mlt_pool_alloc( size );
658 memset( *buffer, 0, size );
659 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
660 mlt_properties_set_int( properties, "test_audio", 1 );
663 // TODO: This does not belong here
664 if ( *format == mlt_audio_s16 && mlt_properties_get( properties, "meta.volume" ) )
666 double value = mlt_properties_get_double( properties, "meta.volume" );
670 memset( *buffer, 0, *samples * *channels * 2 );
672 else if ( value != 1.0 )
674 int total = *samples * *channels;
675 int16_t *p = *buffer;
683 mlt_properties_set( properties, "meta.volume", NULL );
689 /** Set the audio on a frame.
691 * \public \memberof mlt_frame_s
692 * \param self a frame
693 * \param buffer an buffer containing audio samples
694 * \param format the format of the audio in the \p buffer
695 * \param size the total size of the buffer (optional)
696 * \param destructor a function that releases or deallocates the \p buffer
697 * \return true if error
700 int mlt_frame_set_audio( mlt_frame self, void *buffer, mlt_audio_format format, int size, mlt_destructor destructor )
702 mlt_properties_set_int( MLT_FRAME_PROPERTIES( self ), "audio_format", format );
703 return mlt_properties_set_data( MLT_FRAME_PROPERTIES( self ), "audio", buffer, size, destructor, NULL );
706 /** Get audio on a frame as a waveform image.
708 * This generates an 8-bit grayscale image representation of the audio in a
709 * frame. Currently, this only really works for 2 channels.
710 * This allocates the bitmap using mlt_pool so you should release the return
711 * value with \p mlt_pool_release.
713 * \public \memberof mlt_frame_s
714 * \param self a frame
715 * \param w the width of the image
716 * \param h the height of the image to create
717 * \return a pointer to a new bitmap
720 unsigned char *mlt_frame_get_waveform( mlt_frame self, int w, int h )
723 mlt_properties properties = MLT_FRAME_PROPERTIES( self );
724 mlt_audio_format format = mlt_audio_s16;
725 int frequency = 16000;
727 mlt_producer producer = mlt_frame_get_original_producer( self );
728 double fps = mlt_producer_get_fps( mlt_producer_cut_parent( producer ) );
729 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( self ) );
731 // Increase audio resolution proportional to requested image size
732 while ( samples < w )
735 samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( self ) );
739 mlt_frame_get_audio( self, (void**)&pcm, &format, &frequency, &channels, &samples );
741 // Make an 8-bit buffer large enough to hold rendering
743 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
744 if ( bitmap != NULL )
745 memset( bitmap, 0, size );
746 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
748 // Render vertical lines
749 int16_t *ubound = pcm + samples * channels;
750 int skip = samples / w;
751 skip = !skip ? 1 : skip;
752 unsigned char gray = 0xFF / skip;
755 // Iterate sample stream and along x coordinate
756 for ( i = 0; pcm < ubound; i++ )
758 // pcm data has channels interleaved
759 for ( j = 0; j < channels; j++, pcm++ )
761 // Determine sample's magnitude from 2s complement;
762 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
763 // The height of a line is the ratio of the magnitude multiplied by
764 // the vertical resolution of a single channel
765 int height = h * pcm_magnitude / channels / 2 / 32768;
766 // Determine the starting y coordinate - left top, right bottom
767 int displacement = h * (j * 2 + 1) / channels / 2 - ( *pcm < 0 ? 0 : height );
768 // Position buffer pointer using y coordinate, stride, and x coordinate
769 unsigned char *p = bitmap + i / skip + displacement * w;
771 // Draw vertical line
772 for ( k = 0; k < height + 1; k++ )
774 p[ w * k ] = ( k == 0 ) ? 0xFF : p[ w * k ] + gray;
776 p[ w * k ] = ( k == height ) ? 0xFF : p[ w * k ] + gray;
783 /** Get the end service that produced self frame.
785 * This fetches the first producer of the frame and not any producers that
788 * \public \memberof mlt_frame_s
789 * \param self a frame
793 mlt_producer mlt_frame_get_original_producer( mlt_frame self )
796 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( self ), "_producer", NULL );
800 /** Destroy the frame.
802 * \public \memberof mlt_frame_s
803 * \param self a frame
806 void mlt_frame_close( mlt_frame self )
808 if ( self != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( self ) ) <= 0 )
810 mlt_deque_close( self->stack_image );
811 mlt_deque_close( self->stack_audio );
812 while( mlt_deque_peek_back( self->stack_service ) )
813 mlt_service_close( mlt_deque_pop_back( self->stack_service ) );
814 mlt_deque_close( self->stack_service );
815 mlt_properties_close( &self->parent );
820 /***** convenience functions *****/
822 /** Determine the number of samples that belong in a frame at a time position.
824 * \public \memberof mlt_frame_s
825 * \param fps the frame rate
826 * \param frequency the sample rate
827 * \param position the time position
828 * \return the number of samples per channel
831 int mlt_sample_calculator( float fps, int frequency, int64_t position )
833 /* Compute the cumulative number of samples until the start of this frame and the
834 cumulative number of samples until the start of the next frame. Round each to the
835 nearest integer and take the difference to determine the number of samples in
838 This approach should prevent rounding errors that can accumulate over a large number
839 of frames causing A/V sync problems. */
840 return mlt_sample_calculator_to_now( fps, frequency, position + 1 )
841 - mlt_sample_calculator_to_now( fps, frequency, position );
844 /** Determine the number of samples that belong before a time position.
846 * \public \memberof mlt_frame_s
847 * \param fps the frame rate
848 * \param frequency the sample rate
849 * \param position the time position
850 * \return the number of samples per channel
851 * \bug Will this break when mlt_position is converted to double?
854 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t position )
860 samples = (int64_t)( (double) position * (double) frequency / (double) fps +
861 ( position < 0 ? -0.5 : 0.5 ) );
867 void mlt_frame_write_ppm( mlt_frame frame )
871 mlt_image_format format = mlt_image_rgb24;
874 if ( mlt_frame_get_image( frame, &image, &format, &width, &height, 0 ) == 0 )
879 sprintf( filename, "frame-%05d.ppm", mlt_frame_get_position( frame ) );
880 file = fopen( filename, "wb" );
883 fprintf( file, "P6\n%d %d\n255\n", width, height);
884 fwrite( image, width * height * 3, 1, file );
889 /** Get or create a properties object unique to this service instance.
891 * Use this function to hold a service's processing parameters for this
892 * particular frame. Set the parameters in the service's process function.
893 * Then, get the parameters in the function it pushes to the frame's audio
894 * or image stack. This makes the service more parallel by reducing race
895 * conditions and less sensitive to multiple instances (by not setting a
896 * non-unique property on the frame). Creation and destruction of the
897 * properties object is handled automatically.
899 * \public \memberof mlt_frame_s
900 * \param self a frame
901 * \param service a service
902 * \return a properties object
905 mlt_properties mlt_frame_unique_properties( mlt_frame self, mlt_service service )
907 mlt_properties frame_props = MLT_FRAME_PROPERTIES( self );
908 mlt_properties service_props = MLT_SERVICE_PROPERTIES( service );
909 char *unique = mlt_properties_get( service_props, "_unique_id" );
910 mlt_properties instance_props = mlt_properties_get_data( frame_props, unique, NULL );
912 if ( !instance_props )
914 instance_props = mlt_properties_new();
915 mlt_properties_set_data( frame_props, unique, instance_props, 0, (mlt_destructor) mlt_properties_close, NULL );
918 return instance_props;