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 /** Constructor for a frame.
37 mlt_frame mlt_frame_init( mlt_service service )
40 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
44 mlt_profile profile = mlt_service_profile( service );
46 // Initialise the properties
47 mlt_properties properties = &this->parent;
48 mlt_properties_init( properties, this );
50 // Set default properties on the frame
51 mlt_properties_set_position( properties, "_position", 0.0 );
52 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
53 mlt_properties_set_int( properties, "width", profile? profile->width : 720 );
54 mlt_properties_set_int( properties, "height", profile? profile->height : 576 );
55 mlt_properties_set_int( properties, "normalised_width", profile? profile->width : 720 );
56 mlt_properties_set_int( properties, "normalised_height", profile? profile->height : 576 );
57 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
58 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
59 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
61 // Construct stacks for frames and methods
62 this->stack_image = mlt_deque_init( );
63 this->stack_audio = mlt_deque_init( );
64 this->stack_service = mlt_deque_init( );
70 /** Fetch the frames properties.
73 mlt_properties mlt_frame_properties( mlt_frame this )
75 return this != NULL ? &this->parent : NULL;
78 /** Check if we have a way to derive something other than a test card.
81 int mlt_frame_is_test_card( mlt_frame this )
83 return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
86 /** Check if we have a way to derive something other than test audio.
89 int mlt_frame_is_test_audio( mlt_frame this )
91 return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
94 /** Get the aspect ratio of the frame.
97 double mlt_frame_get_aspect_ratio( mlt_frame this )
99 return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
102 /** Set the aspect ratio of the frame.
105 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
107 return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
110 /** Get the position of this frame.
113 mlt_position mlt_frame_get_position( mlt_frame this )
115 int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
116 return pos < 0 ? 0 : pos;
119 /** Set the position of this frame.
122 int mlt_frame_set_position( mlt_frame this, mlt_position value )
124 return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
127 /** Stack a get_image callback.
130 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
132 return mlt_deque_push_back( this->stack_image, get_image );
135 /** Pop a get_image callback.
138 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
140 return mlt_deque_pop_back( this->stack_image );
146 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
148 return mlt_deque_push_back( this->stack_image, that );
154 mlt_frame mlt_frame_pop_frame( mlt_frame this )
156 return mlt_deque_pop_back( this->stack_image );
162 int mlt_frame_push_service( mlt_frame this, void *that )
164 return mlt_deque_push_back( this->stack_image, that );
170 void *mlt_frame_pop_service( mlt_frame this )
172 return mlt_deque_pop_back( this->stack_image );
178 int mlt_frame_push_service_int( mlt_frame this, int that )
180 return mlt_deque_push_back_int( this->stack_image, that );
186 int mlt_frame_pop_service_int( mlt_frame this )
188 return mlt_deque_pop_back_int( this->stack_image );
191 /** Push an audio item on the stack.
194 int mlt_frame_push_audio( mlt_frame this, void *that )
196 return mlt_deque_push_back( this->stack_audio, that );
199 /** Pop an audio item from the stack
202 void *mlt_frame_pop_audio( mlt_frame this )
204 return mlt_deque_pop_back( this->stack_audio );
207 /** Return the service stack
210 mlt_deque mlt_frame_service_stack( mlt_frame this )
212 return this->stack_service;
215 /** Replace image stack with the information provided.
217 This might prove to be unreliable and restrictive - the idea is that a transition
218 which normally uses two images may decide to only use the b frame (ie: in the case
219 of a composite where the b frame completely obscures the a frame).
221 The image must be writable and the destructor for the image itself must be taken
222 care of on another frame and that frame cannot have a replace applied to it...
223 Further it assumes that no alpha mask is in use.
225 For these reasons, it can only be used in a specific situation - when you have
226 multiple tracks each with their own transition and these transitions are applied
227 in a strictly reversed order (ie: highest numbered [lowest track] is processed
230 More reliable approach - the cases should be detected during the process phase
231 and the upper tracks should simply not be invited to stack...
234 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
236 // Remove all items from the stack
237 while( mlt_deque_pop_back( this->stack_image ) ) ;
239 // Update the information
240 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
241 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
242 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
243 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
244 this->get_alpha_mask = NULL;
247 /** Get the image associated to the frame.
250 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
252 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
253 mlt_get_image get_image = mlt_frame_pop_get_image( this );
254 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
257 if ( get_image != NULL )
259 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
260 mlt_position position = mlt_frame_get_position( this );
261 error = get_image( this, buffer, format, width, height, writable );
262 mlt_properties_set_int( properties, "width", *width );
263 mlt_properties_set_int( properties, "height", *height );
264 mlt_properties_set_int( properties, "format", *format );
265 mlt_frame_set_position( this, position );
267 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
269 *format = mlt_properties_get_int( properties, "format" );
270 *buffer = mlt_properties_get_data( properties, "image", NULL );
271 *width = mlt_properties_get_int( properties, "width" );
272 *height = mlt_properties_get_int( properties, "height" );
274 else if ( producer != NULL )
276 mlt_frame test_frame = NULL;
277 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
278 if ( test_frame != NULL )
280 mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
281 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
282 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
283 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
284 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
285 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
286 mlt_properties_set_int( properties, "width", *width );
287 mlt_properties_set_int( properties, "height", *height );
288 mlt_properties_set_int( properties, "format", *format );
289 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
293 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
294 mlt_frame_get_image( this, buffer, format, width, height, writable );
303 *width = *width == 0 ? 720 : *width;
304 *height = *height == 0 ? 576 : *height;
305 size = *width * *height;
307 mlt_properties_set_int( properties, "format", *format );
308 mlt_properties_set_int( properties, "width", *width );
309 mlt_properties_set_int( properties, "height", *height );
310 mlt_properties_set_int( properties, "aspect_ratio", 0 );
318 case mlt_image_rgb24:
321 *buffer = mlt_pool_alloc( size );
323 memset( *buffer, 255, size );
325 case mlt_image_rgb24a:
326 case mlt_image_opengl:
329 *buffer = mlt_pool_alloc( size );
331 memset( *buffer, 255, size );
333 case mlt_image_yuv422:
336 *buffer = mlt_pool_alloc( size );
339 while ( p != NULL && p != q )
345 case mlt_image_yuv420p:
347 *buffer = mlt_pool_alloc( size );
349 memset( *buffer, 255, size );
353 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
354 mlt_properties_set_int( properties, "test_image", 1 );
357 mlt_properties_set_int( properties, "scaled_width", *width );
358 mlt_properties_set_int( properties, "scaled_height", *height );
363 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
365 uint8_t *alpha = NULL;
368 if ( this->get_alpha_mask != NULL )
369 alpha = this->get_alpha_mask( this );
371 alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
374 int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
375 alpha = mlt_pool_alloc( size );
376 memset( alpha, 255, size );
377 mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
383 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
385 mlt_get_audio get_audio = mlt_frame_pop_audio( this );
386 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
387 int hide = mlt_properties_get_int( properties, "test_audio" );
389 if ( hide == 0 && get_audio != NULL )
391 mlt_position position = mlt_frame_get_position( this );
392 get_audio( this, buffer, format, frequency, channels, samples );
393 mlt_frame_set_position( this, position );
395 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
397 *buffer = mlt_properties_get_data( properties, "audio", NULL );
398 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
399 *channels = mlt_properties_get_int( properties, "audio_channels" );
400 *samples = mlt_properties_get_int( properties, "audio_samples" );
405 *samples = *samples <= 0 ? 1920 : *samples;
406 *channels = *channels <= 0 ? 2 : *channels;
407 *frequency = *frequency <= 0 ? 48000 : *frequency;
408 size = *samples * *channels * sizeof( int16_t );
409 *buffer = mlt_pool_alloc( size );
410 if ( *buffer != NULL )
411 memset( *buffer, 0, size );
412 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
413 mlt_properties_set_int( properties, "test_audio", 1 );
416 mlt_properties_set_int( properties, "audio_frequency", *frequency );
417 mlt_properties_set_int( properties, "audio_channels", *channels );
418 mlt_properties_set_int( properties, "audio_samples", *samples );
420 if ( mlt_properties_get( properties, "meta.volume" ) )
422 double value = mlt_properties_get_double( properties, "meta.volume" );
426 memset( *buffer, 0, *samples * *channels * 2 );
428 else if ( value != 1.0 )
430 int total = *samples * *channels;
431 int16_t *p = *buffer;
439 mlt_properties_set( properties, "meta.volume", NULL );
445 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
448 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
449 mlt_audio_format format = mlt_audio_pcm;
450 int frequency = 32000; // lower frequency available?
452 double fps = mlt_profile_fps( NULL );
453 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
456 mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
458 // Make an 8-bit buffer large enough to hold rendering
460 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
461 if ( bitmap != NULL )
462 memset( bitmap, 0, size );
463 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
465 // Render vertical lines
466 int16_t *ubound = pcm + samples * channels;
467 int skip = samples / w - 1;
470 // Iterate sample stream and along x coordinate
471 for ( i = 0; i < w && pcm < ubound; i++ )
473 // pcm data has channels interleaved
474 for ( j = 0; j < channels; j++ )
476 // Determine sample's magnitude from 2s complement;
477 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
478 // The height of a line is the ratio of the magnitude multiplied by
479 // half the vertical resolution
480 int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
481 // Determine the starting y coordinate - left channel above center,
482 // right channel below - currently assumes 2 channels
483 int displacement = ( h / 2 ) - ( 1 - j ) * height;
484 // Position buffer pointer using y coordinate, stride, and x coordinate
485 unsigned char *p = &bitmap[ i + displacement * w ];
487 // Draw vertical line
488 for ( k = 0; k < height; k++ )
493 pcm += skip * channels;
499 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
502 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
506 void mlt_frame_close( mlt_frame this )
508 if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
510 mlt_deque_close( this->stack_image );
511 mlt_deque_close( this->stack_audio );
512 while( mlt_deque_peek_back( this->stack_service ) )
513 mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
514 mlt_deque_close( this->stack_service );
515 mlt_properties_close( &this->parent );
520 /***** convenience functions *****/
522 int mlt_convert_yuv422_to_rgb24a( uint8_t *yuv, uint8_t *rgba, unsigned int total )
533 YUV2RGB(yy, uu, vv, r, g, b);
539 YUV2RGB(yy, uu, vv, r, g, b);
550 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
553 register int y0, y1, u0, u1, v0, v1;
554 register int r, g, b;
555 register uint8_t *d = yuv;
559 for ( i = 0; i < height; i++ )
561 register uint8_t *s = rgba + ( stride * i );
562 for ( j = 0; j < ( width / 2 ); j++ )
568 RGB2YUV (r, g, b, y0, u0 , v0);
573 RGB2YUV (r, g, b, y1, u1 , v1);
585 RGB2YUV (r, g, b, y0, u0 , v0);
591 for ( i = 0; i < height; i++ )
593 register uint8_t *s = rgba + ( stride * i );
594 for ( j = 0; j < ( width / 2 ); j++ )
600 RGB2YUV (r, g, b, y0, u0 , v0);
605 RGB2YUV (r, g, b, y1, u1 , v1);
617 RGB2YUV (r, g, b, y0, u0 , v0);
626 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
629 register int y0, y1, u0, u1, v0, v1;
630 register int r, g, b;
631 register uint8_t *d = yuv;
634 for ( i = 0; i < height; i++ )
636 register uint8_t *s = rgb + ( stride * i );
637 for ( j = 0; j < ( width / 2 ); j++ )
642 RGB2YUV (r, g, b, y0, u0 , v0);
646 RGB2YUV (r, g, b, y1, u1 , v1);
657 RGB2YUV (r, g, b, y0, u0 , v0);
665 int mlt_convert_bgr24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
668 register int y0, y1, u0, u1, v0, v1;
669 register int r, g, b;
670 register uint8_t *d = yuv;
674 for ( i = 0; i < height; i++ )
676 register uint8_t *s = rgba + ( stride * i );
677 for ( j = 0; j < ( width / 2 ); j++ )
683 RGB2YUV (r, g, b, y0, u0 , v0);
688 RGB2YUV (r, g, b, y1, u1 , v1);
700 RGB2YUV (r, g, b, y0, u0 , v0);
706 for ( i = 0; i < height; i++ )
708 register uint8_t *s = rgba + ( stride * i );
709 for ( j = 0; j < ( width / 2 ); j++ )
715 RGB2YUV (r, g, b, y0, u0 , v0);
720 RGB2YUV (r, g, b, y1, u1 , v1);
732 RGB2YUV (r, g, b, y0, u0 , v0);
740 int mlt_convert_bgr24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
743 register int y0, y1, u0, u1, v0, v1;
744 register int r, g, b;
745 register uint8_t *d = yuv;
748 for ( i = 0; i < height; i++ )
750 register uint8_t *s = rgb + ( stride * i );
751 for ( j = 0; j < ( width / 2 ); j++ )
756 RGB2YUV (r, g, b, y0, u0 , v0);
760 RGB2YUV (r, g, b, y1, u1 , v1);
771 RGB2YUV (r, g, b, y0, u0 , v0);
779 int mlt_convert_argb_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
782 register int y0, y1, u0, u1, v0, v1;
783 register int r, g, b;
784 register uint8_t *d = yuv;
788 for ( i = 0; i < height; i++ )
790 register uint8_t *s = rgba + ( stride * i );
791 for ( j = 0; j < ( width / 2 ); j++ )
797 RGB2YUV (r, g, b, y0, u0 , v0);
802 RGB2YUV (r, g, b, y1, u1 , v1);
814 RGB2YUV (r, g, b, y0, u0 , v0);
820 for ( i = 0; i < height; i++ )
822 register uint8_t *s = rgba + ( stride * i );
823 for ( j = 0; j < ( width / 2 ); j++ )
829 RGB2YUV (r, g, b, y0, u0 , v0);
834 RGB2YUV (r, g, b, y1, u1 , v1);
846 RGB2YUV (r, g, b, y0, u0 , v0);
854 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
859 int half = width >> 1;
861 uint8_t *Y = yuv420p;
862 uint8_t *U = Y + width * height;
863 uint8_t *V = U + width * height / 4;
865 register uint8_t *d = yuv;
867 for ( i = 0; i < height; i++ )
869 register uint8_t *u = U + ( i / 2 ) * ( half );
870 register uint8_t *v = V + ( i / 2 ) * ( half );
872 for ( j = 0; j < half; j++ )
883 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
885 uint8_t *output = NULL;
887 if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
890 int offset_x = ( owidth - iwidth ) / 2;
891 int offset_y = ( oheight - iheight ) / 2;
894 output = mlt_pool_alloc( owidth * oheight );
895 memset( output, alpha_value, owidth * oheight );
897 offset_x -= offset_x % 2;
899 out_line = output + offset_y * owidth;
900 out_line += offset_x;
902 // Loop for the entirety of our output height.
905 // We're in the input range for this row.
906 memcpy( out_line, input, iused );
908 // Move to next input line
911 // Move to next output line
919 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
922 int istride = iwidth * 2;
923 int ostride = owidth * 2;
924 int offset_x = ( owidth - iwidth );
925 int offset_y = ( oheight - iheight ) / 2;
926 uint8_t *in_line = input;
928 int size = owidth * oheight;
931 // Optimisation point
932 if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
936 else if ( iwidth == owidth && iheight == oheight )
938 memcpy( output, input, iheight * istride );
948 offset_x -= offset_x % 4;
950 out_line = output + offset_y * ostride;
951 out_line += offset_x;
953 // Loop for the entirety of our output height.
956 // We're in the input range for this row.
957 memcpy( out_line, in_line, iwidth * 2 );
959 // Move to next input line
962 // Move to next output line
967 /** A resizing function for yuv422 frames - this does not rescale, but simply
968 resizes. It assumes yuv422 images available on the frame so use with care.
971 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
974 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
976 // Get the input image, width and height
977 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
978 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
980 int iwidth = mlt_properties_get_int( properties, "width" );
981 int iheight = mlt_properties_get_int( properties, "height" );
983 // If width and height are correct, don't do anything
984 if ( iwidth != owidth || iheight != oheight )
986 uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
988 // Create the output image
989 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
991 // Call the generic resize
992 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
994 // Now update the frame
995 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
996 mlt_properties_set_int( properties, "width", owidth );
997 mlt_properties_set_int( properties, "height", oheight );
999 // We should resize the alpha too
1000 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
1001 if ( alpha != NULL )
1003 mlt_properties_set_data( properties, "alpha", alpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
1004 this->get_alpha_mask = NULL;
1007 // Return the output
1010 // No change, return input
1014 /** A rescaling function for yuv422 frames - low quality, and provided for testing
1015 only. It assumes yuv422 images available on the frame so use with care.
1018 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
1021 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
1023 // Get the input image, width and height
1024 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
1025 int iwidth = mlt_properties_get_int( properties, "width" );
1026 int iheight = mlt_properties_get_int( properties, "height" );
1028 // If width and height are correct, don't do anything
1029 if ( iwidth != owidth || iheight != oheight )
1031 // Create the output image
1032 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
1034 // Calculate strides
1035 int istride = iwidth * 2;
1036 int ostride = owidth * 2;
1038 iwidth = iwidth - ( iwidth % 4 );
1040 // Derived coordinates
1044 int out_x_range = owidth / 2;
1045 int out_y_range = oheight / 2;
1046 int in_x_range = iwidth / 2;
1047 int in_y_range = iheight / 2;
1050 register uint8_t *out_line = output;
1051 register uint8_t *out_ptr;
1053 // Calculate a middle pointer
1054 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
1057 // Generate the affine transform scaling values
1058 register int scale_width = ( iwidth << 16 ) / owidth;
1059 register int scale_height = ( iheight << 16 ) / oheight;
1060 register int base = 0;
1062 int outer = out_x_range * scale_width;
1063 int bottom = out_y_range * scale_height;
1065 // Loop for the entirety of our output height.
1066 for ( dy = - bottom; dy < bottom; dy += scale_height )
1068 // Start at the beginning of the line
1071 // Pointer to the middle of the input line
1072 in_line = in_middle + ( dy >> 16 ) * istride;
1074 // Loop for the entirety of our output row.
1075 for ( dx = - outer; dx < outer; dx += scale_width )
1079 *out_ptr ++ = *( in_line + base );
1081 *out_ptr ++ = *( in_line + base + 1 );
1085 *out_ptr ++ = *( in_line + base );
1087 *out_ptr ++ = *( in_line + base + 3 );
1090 // Move to next output line
1091 out_line += ostride;
1094 // Now update the frame
1095 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
1096 mlt_properties_set_int( properties, "width", owidth );
1097 mlt_properties_set_int( properties, "height", oheight );
1099 // Return the output
1103 // No change, return input
1107 int mlt_frame_mix_audio( mlt_frame this, mlt_frame that, float weight_start, float weight_end, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1110 int16_t *src, *dest;
1111 int frequency_src = *frequency, frequency_dest = *frequency;
1112 int channels_src = *channels, channels_dest = *channels;
1113 int samples_src = *samples, samples_dest = *samples;
1115 double d = 0, s = 0;
1117 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1118 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1120 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1121 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1123 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1125 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1126 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1128 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1130 if ( channels_src > 6 )
1132 if ( channels_dest > 6 )
1134 if ( samples_src > 4000 )
1136 if ( samples_dest > 4000 )
1139 // determine number of samples to process
1140 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1141 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1143 *frequency = frequency_dest;
1145 // Compute a smooth ramp over start to end
1146 float weight = weight_start;
1147 float weight_step = ( weight_end - weight_start ) / *samples;
1151 *samples = samples_src;
1152 *channels = channels_src;
1154 *frequency = frequency_src;
1159 for ( i = 0; i < *samples; i++ )
1161 for ( j = 0; j < *channels; j++ )
1163 if ( j < channels_dest )
1164 d = (double) dest[ i * channels_dest + j ];
1165 if ( j < channels_src )
1166 s = (double) src[ i * channels_src + j ];
1167 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1169 weight += weight_step;
1175 // Replacement for broken mlt_frame_audio_mix - this filter uses an inline low pass filter
1176 // to allow mixing without volume hacking
1177 int mlt_frame_combine_audio( mlt_frame this, mlt_frame that, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1180 int16_t *src, *dest;
1181 int frequency_src = *frequency, frequency_dest = *frequency;
1182 int channels_src = *channels, channels_dest = *channels;
1183 int samples_src = *samples, samples_dest = *samples;
1186 double b_weight = 1.0;
1188 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "meta.mixdown" ) )
1189 b_weight = 1.0 - mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "meta.volume" );
1191 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1192 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1194 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1195 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1197 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1199 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1200 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1202 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1206 *samples = samples_src;
1207 *channels = channels_src;
1209 *frequency = frequency_src;
1213 // determine number of samples to process
1214 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1215 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1217 *frequency = frequency_dest;
1219 for ( j = 0; j < *channels; j++ )
1220 vp[ j ] = ( double )dest[ j ];
1223 double B = exp(-2.0 * M_PI * Fc);
1227 for ( i = 0; i < *samples; i++ )
1229 for ( j = 0; j < *channels; j++ )
1231 v = ( double )( b_weight * dest[ i * channels_dest + j ] + src[ i * channels_src + j ] );
1232 v = v < -32767 ? -32767 : v > 32768 ? 32768 : v;
1233 vp[ j ] = dest[ i * channels_dest + j ] = ( int16_t )( v * A + vp[ j ] * B );
1240 /* Will this break when mlt_position is converted to double? -Zach */
1241 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1245 if ( ( int )( fps * 100 ) == 2997 )
1247 samples = frequency / 30;
1249 switch ( frequency )
1252 if ( position % 5 != 0 )
1256 if ( position % 300 == 0 )
1258 else if ( position % 30 == 0 )
1260 else if ( position % 2 == 0 )
1266 if ( position % 30 == 0 )
1268 else if ( position % 29 == 0 )
1270 else if ( position % 4 == 2 )
1279 else if ( fps != 0 )
1281 samples = frequency / fps;
1287 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
1289 int64_t samples = 0;
1291 // TODO: Correct rules for NTSC and drop the * 100 hack
1292 if ( ( int )( fps * 100 ) == 2997 )
1294 samples = ( ( double )( frame * frequency ) / 30 );
1298 samples += 2 * ( frame / 5 );
1301 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
1304 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
1308 else if ( fps != 0 )
1310 samples = ( ( frame * frequency ) / ( int )fps );