2 * mlt_frame.c -- interface for all frame classes
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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
22 #include "mlt_frame.h"
23 #include "mlt_producer.h"
24 #include "mlt_factory.h"
25 #include "mlt_profile.h"
32 /** Constructor for a frame.
35 mlt_frame mlt_frame_init( )
38 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
42 // Initialise the properties
43 mlt_properties properties = &this->parent;
44 mlt_properties_init( properties, this );
46 // Set default properties on the frame
47 mlt_properties_set_position( properties, "_position", 0.0 );
48 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
49 mlt_properties_set_int( properties, "width", mlt_profile_get()->width );
50 mlt_properties_set_int( properties, "height", mlt_profile_get()->height );
51 mlt_properties_set_int( properties, "normalised_width", mlt_profile_get()->width );
52 mlt_properties_set_int( properties, "normalised_height", mlt_profile_get()->height );
53 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
54 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
55 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
57 // Construct stacks for frames and methods
58 this->stack_image = mlt_deque_init( );
59 this->stack_audio = mlt_deque_init( );
60 this->stack_service = mlt_deque_init( );
66 /** Fetch the frames properties.
69 mlt_properties mlt_frame_properties( mlt_frame this )
71 return this != NULL ? &this->parent : NULL;
74 /** Check if we have a way to derive something other than a test card.
77 int mlt_frame_is_test_card( mlt_frame this )
79 return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
82 /** Check if we have a way to derive something other than test audio.
85 int mlt_frame_is_test_audio( mlt_frame this )
87 return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
90 /** Get the aspect ratio of the frame.
93 double mlt_frame_get_aspect_ratio( mlt_frame this )
95 return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
98 /** Set the aspect ratio of the frame.
101 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
103 return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
106 /** Get the position of this frame.
109 mlt_position mlt_frame_get_position( mlt_frame this )
111 int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
112 return pos < 0 ? 0 : pos;
115 /** Set the position of this frame.
118 int mlt_frame_set_position( mlt_frame this, mlt_position value )
120 return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
123 /** Stack a get_image callback.
126 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
128 return mlt_deque_push_back( this->stack_image, get_image );
131 /** Pop a get_image callback.
134 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
136 return mlt_deque_pop_back( this->stack_image );
142 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
144 return mlt_deque_push_back( this->stack_image, that );
150 mlt_frame mlt_frame_pop_frame( mlt_frame this )
152 return mlt_deque_pop_back( this->stack_image );
158 int mlt_frame_push_service( mlt_frame this, void *that )
160 return mlt_deque_push_back( this->stack_image, that );
166 void *mlt_frame_pop_service( mlt_frame this )
168 return mlt_deque_pop_back( this->stack_image );
174 int mlt_frame_push_service_int( mlt_frame this, int that )
176 return mlt_deque_push_back_int( this->stack_image, that );
182 int mlt_frame_pop_service_int( mlt_frame this )
184 return mlt_deque_pop_back_int( this->stack_image );
187 /** Push an audio item on the stack.
190 int mlt_frame_push_audio( mlt_frame this, void *that )
192 return mlt_deque_push_back( this->stack_audio, that );
195 /** Pop an audio item from the stack
198 void *mlt_frame_pop_audio( mlt_frame this )
200 return mlt_deque_pop_back( this->stack_audio );
203 /** Return the service stack
206 mlt_deque mlt_frame_service_stack( mlt_frame this )
208 return this->stack_service;
211 /** Replace image stack with the information provided.
213 This might prove to be unreliable and restrictive - the idea is that a transition
214 which normally uses two images may decide to only use the b frame (ie: in the case
215 of a composite where the b frame completely obscures the a frame).
217 The image must be writable and the destructor for the image itself must be taken
218 care of on another frame and that frame cannot have a replace applied to it...
219 Further it assumes that no alpha mask is in use.
221 For these reasons, it can only be used in a specific situation - when you have
222 multiple tracks each with their own transition and these transitions are applied
223 in a strictly reversed order (ie: highest numbered [lowest track] is processed
226 More reliable approach - the cases should be detected during the process phase
227 and the upper tracks should simply not be invited to stack...
230 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
232 // Remove all items from the stack
233 while( mlt_deque_pop_back( this->stack_image ) ) ;
235 // Update the information
236 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
237 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
238 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
239 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
240 this->get_alpha_mask = NULL;
243 /** Get the image associated to the frame.
246 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
248 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
249 mlt_get_image get_image = mlt_frame_pop_get_image( this );
250 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
253 if ( get_image != NULL )
255 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
256 mlt_position position = mlt_frame_get_position( this );
257 error = get_image( this, buffer, format, width, height, writable );
258 mlt_properties_set_int( properties, "width", *width );
259 mlt_properties_set_int( properties, "height", *height );
260 mlt_properties_set_int( properties, "format", *format );
261 mlt_frame_set_position( this, position );
263 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
265 *format = mlt_properties_get_int( properties, "format" );
266 *buffer = mlt_properties_get_data( properties, "image", NULL );
267 *width = mlt_properties_get_int( properties, "width" );
268 *height = mlt_properties_get_int( properties, "height" );
270 else if ( producer != NULL )
272 mlt_frame test_frame = NULL;
273 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
274 if ( test_frame != NULL )
276 mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
277 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
278 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
279 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
280 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
281 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
282 mlt_properties_set_int( properties, "width", *width );
283 mlt_properties_set_int( properties, "height", *height );
284 mlt_properties_set_int( properties, "format", *format );
285 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
289 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
290 mlt_frame_get_image( this, buffer, format, width, height, writable );
299 *width = *width == 0 ? 720 : *width;
300 *height = *height == 0 ? 576 : *height;
301 size = *width * *height;
303 mlt_properties_set_int( properties, "format", *format );
304 mlt_properties_set_int( properties, "width", *width );
305 mlt_properties_set_int( properties, "height", *height );
306 mlt_properties_set_int( properties, "aspect_ratio", 0 );
314 case mlt_image_rgb24:
317 *buffer = mlt_pool_alloc( size );
319 memset( *buffer, 255, size );
321 case mlt_image_rgb24a:
322 case mlt_image_opengl:
325 *buffer = mlt_pool_alloc( size );
327 memset( *buffer, 255, size );
329 case mlt_image_yuv422:
332 *buffer = mlt_pool_alloc( size );
335 while ( p != NULL && p != q )
341 case mlt_image_yuv420p:
343 *buffer = mlt_pool_alloc( size );
345 memset( *buffer, 255, size );
349 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
350 mlt_properties_set_int( properties, "test_image", 1 );
353 mlt_properties_set_int( properties, "scaled_width", *width );
354 mlt_properties_set_int( properties, "scaled_height", *height );
359 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
361 uint8_t *alpha = NULL;
364 if ( this->get_alpha_mask != NULL )
365 alpha = this->get_alpha_mask( this );
367 alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
370 int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
371 alpha = mlt_pool_alloc( size );
372 memset( alpha, 255, size );
373 mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
379 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
381 mlt_get_audio get_audio = mlt_frame_pop_audio( this );
382 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
383 int hide = mlt_properties_get_int( properties, "test_audio" );
385 if ( hide == 0 && get_audio != NULL )
387 mlt_position position = mlt_frame_get_position( this );
388 get_audio( this, buffer, format, frequency, channels, samples );
389 mlt_frame_set_position( this, position );
391 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
393 *buffer = mlt_properties_get_data( properties, "audio", NULL );
394 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
395 *channels = mlt_properties_get_int( properties, "audio_channels" );
396 *samples = mlt_properties_get_int( properties, "audio_samples" );
401 *samples = *samples <= 0 ? 1920 : *samples;
402 *channels = *channels <= 0 ? 2 : *channels;
403 *frequency = *frequency <= 0 ? 48000 : *frequency;
404 size = *samples * *channels * sizeof( int16_t );
405 *buffer = mlt_pool_alloc( size );
406 if ( *buffer != NULL )
407 memset( *buffer, 0, size );
408 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
409 mlt_properties_set_int( properties, "test_audio", 1 );
412 mlt_properties_set_int( properties, "audio_frequency", *frequency );
413 mlt_properties_set_int( properties, "audio_channels", *channels );
414 mlt_properties_set_int( properties, "audio_samples", *samples );
416 if ( mlt_properties_get( properties, "meta.volume" ) )
418 double value = mlt_properties_get_double( properties, "meta.volume" );
422 memset( *buffer, 0, *samples * *channels * 2 );
424 else if ( value != 1.0 )
426 int total = *samples * *channels;
427 int16_t *p = *buffer;
435 mlt_properties_set( properties, "meta.volume", NULL );
441 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
444 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
445 mlt_audio_format format = mlt_audio_pcm;
446 int frequency = 32000; // lower frequency available?
448 double fps = mlt_profile_fps( NULL );
449 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
452 mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
454 // Make an 8-bit buffer large enough to hold rendering
456 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
457 if ( bitmap != NULL )
458 memset( bitmap, 0, size );
459 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
461 // Render vertical lines
462 int16_t *ubound = pcm + samples * channels;
463 int skip = samples / w - 1;
466 // Iterate sample stream and along x coordinate
467 for ( i = 0; i < w && pcm < ubound; i++ )
469 // pcm data has channels interleaved
470 for ( j = 0; j < channels; j++ )
472 // Determine sample's magnitude from 2s complement;
473 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
474 // The height of a line is the ratio of the magnitude multiplied by
475 // half the vertical resolution
476 int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
477 // Determine the starting y coordinate - left channel above center,
478 // right channel below - currently assumes 2 channels
479 int displacement = ( h / 2 ) - ( 1 - j ) * height;
480 // Position buffer pointer using y coordinate, stride, and x coordinate
481 unsigned char *p = &bitmap[ i + displacement * w ];
483 // Draw vertical line
484 for ( k = 0; k < height; k++ )
489 pcm += skip * channels;
495 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
498 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
502 void mlt_frame_close( mlt_frame this )
504 if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
506 mlt_deque_close( this->stack_image );
507 mlt_deque_close( this->stack_audio );
508 while( mlt_deque_peek_back( this->stack_service ) )
509 mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
510 mlt_deque_close( this->stack_service );
511 mlt_properties_close( &this->parent );
516 /***** convenience functions *****/
518 int mlt_convert_yuv422_to_rgb24a( uint8_t *yuv, uint8_t *rgba, unsigned int total )
529 YUV2RGB(yy, uu, vv, r, g, b);
535 YUV2RGB(yy, uu, vv, r, g, b);
546 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
549 register int y0, y1, u0, u1, v0, v1;
550 register int r, g, b;
551 register uint8_t *d = yuv;
555 for ( i = 0; i < height; i++ )
557 register uint8_t *s = rgba + ( stride * i );
558 for ( j = 0; j < ( width / 2 ); j++ )
564 RGB2YUV (r, g, b, y0, u0 , v0);
569 RGB2YUV (r, g, b, y1, u1 , v1);
581 RGB2YUV (r, g, b, y0, u0 , v0);
587 for ( i = 0; i < height; i++ )
589 register uint8_t *s = rgba + ( stride * i );
590 for ( j = 0; j < ( width / 2 ); j++ )
596 RGB2YUV (r, g, b, y0, u0 , v0);
601 RGB2YUV (r, g, b, y1, u1 , v1);
613 RGB2YUV (r, g, b, y0, u0 , v0);
622 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
625 register int y0, y1, u0, u1, v0, v1;
626 register int r, g, b;
627 register uint8_t *d = yuv;
630 for ( i = 0; i < height; i++ )
632 register uint8_t *s = rgb + ( stride * i );
633 for ( j = 0; j < ( width / 2 ); j++ )
638 RGB2YUV (r, g, b, y0, u0 , v0);
642 RGB2YUV (r, g, b, y1, u1 , v1);
653 RGB2YUV (r, g, b, y0, u0 , v0);
661 int mlt_convert_bgr24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
664 register int y0, y1, u0, u1, v0, v1;
665 register int r, g, b;
666 register uint8_t *d = yuv;
670 for ( i = 0; i < height; i++ )
672 register uint8_t *s = rgba + ( stride * i );
673 for ( j = 0; j < ( width / 2 ); j++ )
679 RGB2YUV (r, g, b, y0, u0 , v0);
684 RGB2YUV (r, g, b, y1, u1 , v1);
696 RGB2YUV (r, g, b, y0, u0 , v0);
702 for ( i = 0; i < height; i++ )
704 register uint8_t *s = rgba + ( stride * i );
705 for ( j = 0; j < ( width / 2 ); j++ )
711 RGB2YUV (r, g, b, y0, u0 , v0);
716 RGB2YUV (r, g, b, y1, u1 , v1);
728 RGB2YUV (r, g, b, y0, u0 , v0);
736 int mlt_convert_bgr24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
739 register int y0, y1, u0, u1, v0, v1;
740 register int r, g, b;
741 register uint8_t *d = yuv;
744 for ( i = 0; i < height; i++ )
746 register uint8_t *s = rgb + ( stride * i );
747 for ( j = 0; j < ( width / 2 ); j++ )
752 RGB2YUV (r, g, b, y0, u0 , v0);
756 RGB2YUV (r, g, b, y1, u1 , v1);
767 RGB2YUV (r, g, b, y0, u0 , v0);
775 int mlt_convert_argb_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
778 register int y0, y1, u0, u1, v0, v1;
779 register int r, g, b;
780 register uint8_t *d = yuv;
784 for ( i = 0; i < height; i++ )
786 register uint8_t *s = rgba + ( stride * i );
787 for ( j = 0; j < ( width / 2 ); j++ )
793 RGB2YUV (r, g, b, y0, u0 , v0);
798 RGB2YUV (r, g, b, y1, u1 , v1);
810 RGB2YUV (r, g, b, y0, u0 , v0);
816 for ( i = 0; i < height; i++ )
818 register uint8_t *s = rgba + ( stride * i );
819 for ( j = 0; j < ( width / 2 ); j++ )
825 RGB2YUV (r, g, b, y0, u0 , v0);
830 RGB2YUV (r, g, b, y1, u1 , v1);
842 RGB2YUV (r, g, b, y0, u0 , v0);
850 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
855 int half = width >> 1;
857 uint8_t *Y = yuv420p;
858 uint8_t *U = Y + width * height;
859 uint8_t *V = U + width * height / 4;
861 register uint8_t *d = yuv;
863 for ( i = 0; i < height; i++ )
865 register uint8_t *u = U + ( i / 2 ) * ( half );
866 register uint8_t *v = V + ( i / 2 ) * ( half );
868 for ( j = 0; j < half; j++ )
879 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
881 uint8_t *output = NULL;
883 if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
886 int offset_x = ( owidth - iwidth ) / 2;
887 int offset_y = ( oheight - iheight ) / 2;
890 output = mlt_pool_alloc( owidth * oheight );
891 memset( output, alpha_value, owidth * oheight );
893 offset_x -= offset_x % 2;
895 out_line = output + offset_y * owidth;
896 out_line += offset_x;
898 // Loop for the entirety of our output height.
901 // We're in the input range for this row.
902 memcpy( out_line, input, iused );
904 // Move to next input line
907 // Move to next output line
915 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
918 int istride = iwidth * 2;
919 int ostride = owidth * 2;
920 int offset_x = ( owidth - iwidth );
921 int offset_y = ( oheight - iheight ) / 2;
922 uint8_t *in_line = input;
924 int size = owidth * oheight;
927 // Optimisation point
928 if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
932 else if ( iwidth == owidth && iheight == oheight )
934 memcpy( output, input, iheight * istride );
944 offset_x -= offset_x % 4;
946 out_line = output + offset_y * ostride;
947 out_line += offset_x;
949 // Loop for the entirety of our output height.
952 // We're in the input range for this row.
953 memcpy( out_line, in_line, iwidth * 2 );
955 // Move to next input line
958 // Move to next output line
963 /** A resizing function for yuv422 frames - this does not rescale, but simply
964 resizes. It assumes yuv422 images available on the frame so use with care.
967 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
970 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
972 // Get the input image, width and height
973 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
974 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
976 int iwidth = mlt_properties_get_int( properties, "width" );
977 int iheight = mlt_properties_get_int( properties, "height" );
979 // If width and height are correct, don't do anything
980 if ( iwidth != owidth || iheight != oheight )
982 uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
984 // Create the output image
985 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
987 // Call the generic resize
988 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
990 // Now update the frame
991 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
992 mlt_properties_set_int( properties, "width", owidth );
993 mlt_properties_set_int( properties, "height", oheight );
995 // We should resize the alpha too
996 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
999 mlt_properties_set_data( properties, "alpha", alpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
1000 this->get_alpha_mask = NULL;
1003 // Return the output
1006 // No change, return input
1010 /** A rescaling function for yuv422 frames - low quality, and provided for testing
1011 only. It assumes yuv422 images available on the frame so use with care.
1014 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
1017 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
1019 // Get the input image, width and height
1020 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
1021 int iwidth = mlt_properties_get_int( properties, "width" );
1022 int iheight = mlt_properties_get_int( properties, "height" );
1024 // If width and height are correct, don't do anything
1025 if ( iwidth != owidth || iheight != oheight )
1027 // Create the output image
1028 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
1030 // Calculate strides
1031 int istride = iwidth * 2;
1032 int ostride = owidth * 2;
1034 iwidth = iwidth - ( iwidth % 4 );
1036 // Derived coordinates
1040 int out_x_range = owidth / 2;
1041 int out_y_range = oheight / 2;
1042 int in_x_range = iwidth / 2;
1043 int in_y_range = iheight / 2;
1046 register uint8_t *out_line = output;
1047 register uint8_t *out_ptr;
1049 // Calculate a middle pointer
1050 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
1053 // Generate the affine transform scaling values
1054 register int scale_width = ( iwidth << 16 ) / owidth;
1055 register int scale_height = ( iheight << 16 ) / oheight;
1056 register int base = 0;
1058 int outer = out_x_range * scale_width;
1059 int bottom = out_y_range * scale_height;
1061 // Loop for the entirety of our output height.
1062 for ( dy = - bottom; dy < bottom; dy += scale_height )
1064 // Start at the beginning of the line
1067 // Pointer to the middle of the input line
1068 in_line = in_middle + ( dy >> 16 ) * istride;
1070 // Loop for the entirety of our output row.
1071 for ( dx = - outer; dx < outer; dx += scale_width )
1075 *out_ptr ++ = *( in_line + base );
1077 *out_ptr ++ = *( in_line + base + 1 );
1081 *out_ptr ++ = *( in_line + base );
1083 *out_ptr ++ = *( in_line + base + 3 );
1086 // Move to next output line
1087 out_line += ostride;
1090 // Now update the frame
1091 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
1092 mlt_properties_set_int( properties, "width", owidth );
1093 mlt_properties_set_int( properties, "height", oheight );
1095 // Return the output
1099 // No change, return input
1103 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 )
1106 int16_t *src, *dest;
1107 int frequency_src = *frequency, frequency_dest = *frequency;
1108 int channels_src = *channels, channels_dest = *channels;
1109 int samples_src = *samples, samples_dest = *samples;
1111 double d = 0, s = 0;
1113 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1114 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1116 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1117 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1119 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1121 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1122 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1124 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1126 if ( channels_src > 6 )
1128 if ( channels_dest > 6 )
1130 if ( samples_src > 4000 )
1132 if ( samples_dest > 4000 )
1135 // determine number of samples to process
1136 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1137 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1139 *frequency = frequency_dest;
1141 // Compute a smooth ramp over start to end
1142 float weight = weight_start;
1143 float weight_step = ( weight_end - weight_start ) / *samples;
1147 *samples = samples_src;
1148 *channels = channels_src;
1150 *frequency = frequency_src;
1155 for ( i = 0; i < *samples; i++ )
1157 for ( j = 0; j < *channels; j++ )
1159 if ( j < channels_dest )
1160 d = (double) dest[ i * channels_dest + j ];
1161 if ( j < channels_src )
1162 s = (double) src[ i * channels_src + j ];
1163 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1165 weight += weight_step;
1171 // Replacement for broken mlt_frame_audio_mix - this filter uses an inline low pass filter
1172 // to allow mixing without volume hacking
1173 int mlt_frame_combine_audio( mlt_frame this, mlt_frame that, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1176 int16_t *src, *dest;
1177 int frequency_src = *frequency, frequency_dest = *frequency;
1178 int channels_src = *channels, channels_dest = *channels;
1179 int samples_src = *samples, samples_dest = *samples;
1182 double b_weight = 1.0;
1184 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "meta.mixdown" ) )
1185 b_weight = 1.0 - mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "meta.volume" );
1187 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1188 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1190 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1191 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1193 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1195 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1196 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1198 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1202 *samples = samples_src;
1203 *channels = channels_src;
1205 *frequency = frequency_src;
1209 // determine number of samples to process
1210 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1211 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1213 *frequency = frequency_dest;
1215 for ( j = 0; j < *channels; j++ )
1216 vp[ j ] = ( double )dest[ j ];
1219 double B = exp(-2.0 * M_PI * Fc);
1223 for ( i = 0; i < *samples; i++ )
1225 for ( j = 0; j < *channels; j++ )
1227 v = ( double )( b_weight * dest[ i * channels_dest + j ] + src[ i * channels_src + j ] );
1228 v = v < -32767 ? -32767 : v > 32768 ? 32768 : v;
1229 vp[ j ] = dest[ i * channels_dest + j ] = ( int16_t )( v * A + vp[ j ] * B );
1236 /* Will this break when mlt_position is converted to double? -Zach */
1237 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1241 if ( ( int )( fps * 100 ) == 2997 )
1243 samples = frequency / 30;
1245 switch ( frequency )
1248 if ( position % 5 != 0 )
1252 if ( position % 300 == 0 )
1254 else if ( position % 30 == 0 )
1256 else if ( position % 2 == 0 )
1262 if ( position % 30 == 0 )
1264 else if ( position % 29 == 0 )
1266 else if ( position % 4 == 2 )
1275 else if ( fps != 0 )
1277 samples = frequency / fps;
1283 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
1285 int64_t samples = 0;
1287 // TODO: Correct rules for NTSC and drop the * 100 hack
1288 if ( ( int )( fps * 100 ) == 2997 )
1290 samples = ( ( double )( frame * frequency ) / 30 );
1294 samples += 2 * ( frame / 5 );
1297 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
1300 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
1304 else if ( fps != 0 )
1306 samples = ( ( frame * frequency ) / ( int )fps );