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
21 #include "mlt_frame.h"
22 #include "mlt_producer.h"
23 #include "mlt_factory.h"
24 #include "mlt_profile.h"
31 /** Constructor for a frame.
34 mlt_frame mlt_frame_init( mlt_service service )
37 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
41 mlt_profile profile = mlt_service_profile( service );
43 // Initialise the properties
44 mlt_properties properties = &this->parent;
45 mlt_properties_init( properties, this );
47 // Set default properties on the frame
48 mlt_properties_set_position( properties, "_position", 0.0 );
49 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
50 mlt_properties_set_int( properties, "width", profile? profile->width : 720 );
51 mlt_properties_set_int( properties, "height", profile? profile->height : 576 );
52 mlt_properties_set_int( properties, "normalised_width", profile? profile->width : 720 );
53 mlt_properties_set_int( properties, "normalised_height", profile? profile->height : 576 );
54 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
55 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
56 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
58 // Construct stacks for frames and methods
59 this->stack_image = mlt_deque_init( );
60 this->stack_audio = mlt_deque_init( );
61 this->stack_service = mlt_deque_init( );
67 /** Fetch the frames properties.
70 mlt_properties mlt_frame_properties( mlt_frame this )
72 return this != NULL ? &this->parent : NULL;
75 /** Check if we have a way to derive something other than a test card.
78 int mlt_frame_is_test_card( mlt_frame this )
80 return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
83 /** Check if we have a way to derive something other than test audio.
86 int mlt_frame_is_test_audio( mlt_frame this )
88 return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
91 /** Get the aspect ratio of the frame.
94 double mlt_frame_get_aspect_ratio( mlt_frame this )
96 return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
99 /** Set the aspect ratio of the frame.
102 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
104 return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
107 /** Get the position of this frame.
110 mlt_position mlt_frame_get_position( mlt_frame this )
112 int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
113 return pos < 0 ? 0 : pos;
116 /** Set the position of this frame.
119 int mlt_frame_set_position( mlt_frame this, mlt_position value )
121 return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
124 /** Stack a get_image callback.
127 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
129 return mlt_deque_push_back( this->stack_image, get_image );
132 /** Pop a get_image callback.
135 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
137 return mlt_deque_pop_back( this->stack_image );
143 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
145 return mlt_deque_push_back( this->stack_image, that );
151 mlt_frame mlt_frame_pop_frame( mlt_frame this )
153 return mlt_deque_pop_back( this->stack_image );
159 int mlt_frame_push_service( mlt_frame this, void *that )
161 return mlt_deque_push_back( this->stack_image, that );
167 void *mlt_frame_pop_service( mlt_frame this )
169 return mlt_deque_pop_back( this->stack_image );
175 int mlt_frame_push_service_int( mlt_frame this, int that )
177 return mlt_deque_push_back_int( this->stack_image, that );
183 int mlt_frame_pop_service_int( mlt_frame this )
185 return mlt_deque_pop_back_int( this->stack_image );
188 /** Push an audio item on the stack.
191 int mlt_frame_push_audio( mlt_frame this, void *that )
193 return mlt_deque_push_back( this->stack_audio, that );
196 /** Pop an audio item from the stack
199 void *mlt_frame_pop_audio( mlt_frame this )
201 return mlt_deque_pop_back( this->stack_audio );
204 /** Return the service stack
207 mlt_deque mlt_frame_service_stack( mlt_frame this )
209 return this->stack_service;
212 /** Replace image stack with the information provided.
214 This might prove to be unreliable and restrictive - the idea is that a transition
215 which normally uses two images may decide to only use the b frame (ie: in the case
216 of a composite where the b frame completely obscures the a frame).
218 The image must be writable and the destructor for the image itself must be taken
219 care of on another frame and that frame cannot have a replace applied to it...
220 Further it assumes that no alpha mask is in use.
222 For these reasons, it can only be used in a specific situation - when you have
223 multiple tracks each with their own transition and these transitions are applied
224 in a strictly reversed order (ie: highest numbered [lowest track] is processed
227 More reliable approach - the cases should be detected during the process phase
228 and the upper tracks should simply not be invited to stack...
231 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
233 // Remove all items from the stack
234 while( mlt_deque_pop_back( this->stack_image ) ) ;
236 // Update the information
237 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
238 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
239 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
240 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
241 this->get_alpha_mask = NULL;
244 /** Get the image associated to the frame.
247 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
249 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
250 mlt_get_image get_image = mlt_frame_pop_get_image( this );
251 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
254 if ( get_image != NULL )
256 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
257 mlt_position position = mlt_frame_get_position( this );
258 error = get_image( this, buffer, format, width, height, writable );
259 mlt_properties_set_int( properties, "width", *width );
260 mlt_properties_set_int( properties, "height", *height );
261 mlt_properties_set_int( properties, "format", *format );
262 mlt_frame_set_position( this, position );
264 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
266 *format = mlt_properties_get_int( properties, "format" );
267 *buffer = mlt_properties_get_data( properties, "image", NULL );
268 *width = mlt_properties_get_int( properties, "width" );
269 *height = mlt_properties_get_int( properties, "height" );
271 else if ( producer != NULL )
273 mlt_frame test_frame = NULL;
274 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
275 if ( test_frame != NULL )
277 mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
278 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
279 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
280 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
281 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
282 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
283 mlt_properties_set_int( properties, "width", *width );
284 mlt_properties_set_int( properties, "height", *height );
285 mlt_properties_set_int( properties, "format", *format );
286 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
290 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
291 mlt_frame_get_image( this, buffer, format, width, height, writable );
300 *width = *width == 0 ? 720 : *width;
301 *height = *height == 0 ? 576 : *height;
302 size = *width * *height;
304 mlt_properties_set_int( properties, "format", *format );
305 mlt_properties_set_int( properties, "width", *width );
306 mlt_properties_set_int( properties, "height", *height );
307 mlt_properties_set_int( properties, "aspect_ratio", 0 );
315 case mlt_image_rgb24:
318 *buffer = mlt_pool_alloc( size );
320 memset( *buffer, 255, size );
322 case mlt_image_rgb24a:
323 case mlt_image_opengl:
326 *buffer = mlt_pool_alloc( size );
328 memset( *buffer, 255, size );
330 case mlt_image_yuv422:
333 *buffer = mlt_pool_alloc( size );
336 while ( p != NULL && p != q )
342 case mlt_image_yuv420p:
344 *buffer = mlt_pool_alloc( size );
346 memset( *buffer, 255, size );
350 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
351 mlt_properties_set_int( properties, "test_image", 1 );
354 mlt_properties_set_int( properties, "scaled_width", *width );
355 mlt_properties_set_int( properties, "scaled_height", *height );
360 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
362 uint8_t *alpha = NULL;
365 if ( this->get_alpha_mask != NULL )
366 alpha = this->get_alpha_mask( this );
368 alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
371 int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
372 alpha = mlt_pool_alloc( size );
373 memset( alpha, 255, size );
374 mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
380 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
382 mlt_get_audio get_audio = mlt_frame_pop_audio( this );
383 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
384 int hide = mlt_properties_get_int( properties, "test_audio" );
386 if ( hide == 0 && get_audio != NULL )
388 mlt_position position = mlt_frame_get_position( this );
389 get_audio( this, buffer, format, frequency, channels, samples );
390 mlt_frame_set_position( this, position );
392 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
394 *buffer = mlt_properties_get_data( properties, "audio", NULL );
395 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
396 *channels = mlt_properties_get_int( properties, "audio_channels" );
397 *samples = mlt_properties_get_int( properties, "audio_samples" );
402 *samples = *samples <= 0 ? 1920 : *samples;
403 *channels = *channels <= 0 ? 2 : *channels;
404 *frequency = *frequency <= 0 ? 48000 : *frequency;
405 size = *samples * *channels * sizeof( int16_t );
406 *buffer = mlt_pool_alloc( size );
407 if ( *buffer != NULL )
408 memset( *buffer, 0, size );
409 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
410 mlt_properties_set_int( properties, "test_audio", 1 );
413 mlt_properties_set_int( properties, "audio_frequency", *frequency );
414 mlt_properties_set_int( properties, "audio_channels", *channels );
415 mlt_properties_set_int( properties, "audio_samples", *samples );
417 if ( mlt_properties_get( properties, "meta.volume" ) )
419 double value = mlt_properties_get_double( properties, "meta.volume" );
423 memset( *buffer, 0, *samples * *channels * 2 );
425 else if ( value != 1.0 )
427 int total = *samples * *channels;
428 int16_t *p = *buffer;
436 mlt_properties_set( properties, "meta.volume", NULL );
442 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
445 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
446 mlt_audio_format format = mlt_audio_pcm;
447 int frequency = 32000; // lower frequency available?
449 double fps = mlt_profile_fps( NULL );
450 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
453 mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
455 // Make an 8-bit buffer large enough to hold rendering
457 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
458 if ( bitmap != NULL )
459 memset( bitmap, 0, size );
460 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
462 // Render vertical lines
463 int16_t *ubound = pcm + samples * channels;
464 int skip = samples / w - 1;
467 // Iterate sample stream and along x coordinate
468 for ( i = 0; i < w && pcm < ubound; i++ )
470 // pcm data has channels interleaved
471 for ( j = 0; j < channels; j++ )
473 // Determine sample's magnitude from 2s complement;
474 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
475 // The height of a line is the ratio of the magnitude multiplied by
476 // half the vertical resolution
477 int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
478 // Determine the starting y coordinate - left channel above center,
479 // right channel below - currently assumes 2 channels
480 int displacement = ( h / 2 ) - ( 1 - j ) * height;
481 // Position buffer pointer using y coordinate, stride, and x coordinate
482 unsigned char *p = &bitmap[ i + displacement * w ];
484 // Draw vertical line
485 for ( k = 0; k < height; k++ )
490 pcm += skip * channels;
496 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
499 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
503 void mlt_frame_close( mlt_frame this )
505 if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
507 mlt_deque_close( this->stack_image );
508 mlt_deque_close( this->stack_audio );
509 while( mlt_deque_peek_back( this->stack_service ) )
510 mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
511 mlt_deque_close( this->stack_service );
512 mlt_properties_close( &this->parent );
517 /***** convenience functions *****/
519 int mlt_convert_yuv422_to_rgb24a( uint8_t *yuv, uint8_t *rgba, unsigned int total )
530 YUV2RGB(yy, uu, vv, r, g, b);
536 YUV2RGB(yy, uu, vv, r, g, b);
547 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
550 register int y0, y1, u0, u1, v0, v1;
551 register int r, g, b;
552 register uint8_t *d = yuv;
556 for ( i = 0; i < height; i++ )
558 register uint8_t *s = rgba + ( stride * i );
559 for ( j = 0; j < ( width / 2 ); j++ )
565 RGB2YUV (r, g, b, y0, u0 , v0);
570 RGB2YUV (r, g, b, y1, u1 , v1);
582 RGB2YUV (r, g, b, y0, u0 , v0);
588 for ( i = 0; i < height; i++ )
590 register uint8_t *s = rgba + ( stride * i );
591 for ( j = 0; j < ( width / 2 ); j++ )
597 RGB2YUV (r, g, b, y0, u0 , v0);
602 RGB2YUV (r, g, b, y1, u1 , v1);
614 RGB2YUV (r, g, b, y0, u0 , v0);
623 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
626 register int y0, y1, u0, u1, v0, v1;
627 register int r, g, b;
628 register uint8_t *d = yuv;
631 for ( i = 0; i < height; i++ )
633 register uint8_t *s = rgb + ( stride * i );
634 for ( j = 0; j < ( width / 2 ); j++ )
639 RGB2YUV (r, g, b, y0, u0 , v0);
643 RGB2YUV (r, g, b, y1, u1 , v1);
654 RGB2YUV (r, g, b, y0, u0 , v0);
662 int mlt_convert_bgr24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
665 register int y0, y1, u0, u1, v0, v1;
666 register int r, g, b;
667 register uint8_t *d = yuv;
671 for ( i = 0; i < height; i++ )
673 register uint8_t *s = rgba + ( stride * i );
674 for ( j = 0; j < ( width / 2 ); j++ )
680 RGB2YUV (r, g, b, y0, u0 , v0);
685 RGB2YUV (r, g, b, y1, u1 , v1);
697 RGB2YUV (r, g, b, y0, u0 , v0);
703 for ( i = 0; i < height; i++ )
705 register uint8_t *s = rgba + ( stride * i );
706 for ( j = 0; j < ( width / 2 ); j++ )
712 RGB2YUV (r, g, b, y0, u0 , v0);
717 RGB2YUV (r, g, b, y1, u1 , v1);
729 RGB2YUV (r, g, b, y0, u0 , v0);
737 int mlt_convert_bgr24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
740 register int y0, y1, u0, u1, v0, v1;
741 register int r, g, b;
742 register uint8_t *d = yuv;
745 for ( i = 0; i < height; i++ )
747 register uint8_t *s = rgb + ( stride * i );
748 for ( j = 0; j < ( width / 2 ); j++ )
753 RGB2YUV (r, g, b, y0, u0 , v0);
757 RGB2YUV (r, g, b, y1, u1 , v1);
768 RGB2YUV (r, g, b, y0, u0 , v0);
776 int mlt_convert_argb_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
779 register int y0, y1, u0, u1, v0, v1;
780 register int r, g, b;
781 register uint8_t *d = yuv;
785 for ( i = 0; i < height; i++ )
787 register uint8_t *s = rgba + ( stride * i );
788 for ( j = 0; j < ( width / 2 ); j++ )
794 RGB2YUV (r, g, b, y0, u0 , v0);
799 RGB2YUV (r, g, b, y1, u1 , v1);
811 RGB2YUV (r, g, b, y0, u0 , v0);
817 for ( i = 0; i < height; i++ )
819 register uint8_t *s = rgba + ( stride * i );
820 for ( j = 0; j < ( width / 2 ); j++ )
826 RGB2YUV (r, g, b, y0, u0 , v0);
831 RGB2YUV (r, g, b, y1, u1 , v1);
843 RGB2YUV (r, g, b, y0, u0 , v0);
851 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
856 int half = width >> 1;
858 uint8_t *Y = yuv420p;
859 uint8_t *U = Y + width * height;
860 uint8_t *V = U + width * height / 4;
862 register uint8_t *d = yuv;
864 for ( i = 0; i < height; i++ )
866 register uint8_t *u = U + ( i / 2 ) * ( half );
867 register uint8_t *v = V + ( i / 2 ) * ( half );
869 for ( j = 0; j < half; j++ )
880 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
882 uint8_t *output = NULL;
884 if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
887 int offset_x = ( owidth - iwidth ) / 2;
888 int offset_y = ( oheight - iheight ) / 2;
891 output = mlt_pool_alloc( owidth * oheight );
892 memset( output, alpha_value, owidth * oheight );
894 offset_x -= offset_x % 2;
896 out_line = output + offset_y * owidth;
897 out_line += offset_x;
899 // Loop for the entirety of our output height.
902 // We're in the input range for this row.
903 memcpy( out_line, input, iused );
905 // Move to next input line
908 // Move to next output line
916 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
919 int istride = iwidth * 2;
920 int ostride = owidth * 2;
921 int offset_x = ( owidth - iwidth );
922 int offset_y = ( oheight - iheight ) / 2;
923 uint8_t *in_line = input;
925 int size = owidth * oheight;
928 // Optimisation point
929 if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
933 else if ( iwidth == owidth && iheight == oheight )
935 memcpy( output, input, iheight * istride );
945 offset_x -= offset_x % 4;
947 out_line = output + offset_y * ostride;
948 out_line += offset_x;
950 // Loop for the entirety of our output height.
953 // We're in the input range for this row.
954 memcpy( out_line, in_line, iwidth * 2 );
956 // Move to next input line
959 // Move to next output line
964 /** A resizing function for yuv422 frames - this does not rescale, but simply
965 resizes. It assumes yuv422 images available on the frame so use with care.
968 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
971 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
973 // Get the input image, width and height
974 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
975 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
977 int iwidth = mlt_properties_get_int( properties, "width" );
978 int iheight = mlt_properties_get_int( properties, "height" );
980 // If width and height are correct, don't do anything
981 if ( iwidth != owidth || iheight != oheight )
983 uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
985 // Create the output image
986 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
988 // Call the generic resize
989 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
991 // Now update the frame
992 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
993 mlt_properties_set_int( properties, "width", owidth );
994 mlt_properties_set_int( properties, "height", oheight );
996 // We should resize the alpha too
997 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
1000 mlt_properties_set_data( properties, "alpha", alpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
1001 this->get_alpha_mask = NULL;
1004 // Return the output
1007 // No change, return input
1011 /** A rescaling function for yuv422 frames - low quality, and provided for testing
1012 only. It assumes yuv422 images available on the frame so use with care.
1015 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
1018 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
1020 // Get the input image, width and height
1021 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
1022 int iwidth = mlt_properties_get_int( properties, "width" );
1023 int iheight = mlt_properties_get_int( properties, "height" );
1025 // If width and height are correct, don't do anything
1026 if ( iwidth != owidth || iheight != oheight )
1028 // Create the output image
1029 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
1031 // Calculate strides
1032 int istride = iwidth * 2;
1033 int ostride = owidth * 2;
1035 iwidth = iwidth - ( iwidth % 4 );
1037 // Derived coordinates
1041 int out_x_range = owidth / 2;
1042 int out_y_range = oheight / 2;
1043 int in_x_range = iwidth / 2;
1044 int in_y_range = iheight / 2;
1047 register uint8_t *out_line = output;
1048 register uint8_t *out_ptr;
1050 // Calculate a middle pointer
1051 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
1054 // Generate the affine transform scaling values
1055 register int scale_width = ( iwidth << 16 ) / owidth;
1056 register int scale_height = ( iheight << 16 ) / oheight;
1057 register int base = 0;
1059 int outer = out_x_range * scale_width;
1060 int bottom = out_y_range * scale_height;
1062 // Loop for the entirety of our output height.
1063 for ( dy = - bottom; dy < bottom; dy += scale_height )
1065 // Start at the beginning of the line
1068 // Pointer to the middle of the input line
1069 in_line = in_middle + ( dy >> 16 ) * istride;
1071 // Loop for the entirety of our output row.
1072 for ( dx = - outer; dx < outer; dx += scale_width )
1076 *out_ptr ++ = *( in_line + base );
1078 *out_ptr ++ = *( in_line + base + 1 );
1082 *out_ptr ++ = *( in_line + base );
1084 *out_ptr ++ = *( in_line + base + 3 );
1087 // Move to next output line
1088 out_line += ostride;
1091 // Now update the frame
1092 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
1093 mlt_properties_set_int( properties, "width", owidth );
1094 mlt_properties_set_int( properties, "height", oheight );
1096 // Return the output
1100 // No change, return input
1104 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 )
1107 int16_t *src, *dest;
1108 int frequency_src = *frequency, frequency_dest = *frequency;
1109 int channels_src = *channels, channels_dest = *channels;
1110 int samples_src = *samples, samples_dest = *samples;
1112 double d = 0, s = 0;
1114 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1115 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1117 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1118 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1120 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1122 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1123 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1125 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1127 if ( channels_src > 6 )
1129 if ( channels_dest > 6 )
1131 if ( samples_src > 4000 )
1133 if ( samples_dest > 4000 )
1136 // determine number of samples to process
1137 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1138 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1140 *frequency = frequency_dest;
1142 // Compute a smooth ramp over start to end
1143 float weight = weight_start;
1144 float weight_step = ( weight_end - weight_start ) / *samples;
1148 *samples = samples_src;
1149 *channels = channels_src;
1151 *frequency = frequency_src;
1156 for ( i = 0; i < *samples; i++ )
1158 for ( j = 0; j < *channels; j++ )
1160 if ( j < channels_dest )
1161 d = (double) dest[ i * channels_dest + j ];
1162 if ( j < channels_src )
1163 s = (double) src[ i * channels_src + j ];
1164 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1166 weight += weight_step;
1172 // Replacement for broken mlt_frame_audio_mix - this filter uses an inline low pass filter
1173 // to allow mixing without volume hacking
1174 int mlt_frame_combine_audio( mlt_frame this, mlt_frame that, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1177 int16_t *src, *dest;
1178 int frequency_src = *frequency, frequency_dest = *frequency;
1179 int channels_src = *channels, channels_dest = *channels;
1180 int samples_src = *samples, samples_dest = *samples;
1183 double b_weight = 1.0;
1185 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "meta.mixdown" ) )
1186 b_weight = 1.0 - mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "meta.volume" );
1188 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1189 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1191 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1192 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1194 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1196 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1197 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1199 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1203 *samples = samples_src;
1204 *channels = channels_src;
1206 *frequency = frequency_src;
1210 // determine number of samples to process
1211 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1212 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1214 *frequency = frequency_dest;
1216 for ( j = 0; j < *channels; j++ )
1217 vp[ j ] = ( double )dest[ j ];
1220 double B = exp(-2.0 * M_PI * Fc);
1224 for ( i = 0; i < *samples; i++ )
1226 for ( j = 0; j < *channels; j++ )
1228 v = ( double )( b_weight * dest[ i * channels_dest + j ] + src[ i * channels_src + j ] );
1229 v = v < -32767 ? -32767 : v > 32768 ? 32768 : v;
1230 vp[ j ] = dest[ i * channels_dest + j ] = ( int16_t )( v * A + vp[ j ] * B );
1237 /* Will this break when mlt_position is converted to double? -Zach */
1238 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1242 if ( ( int )( fps * 100 ) == 2997 )
1244 samples = frequency / 30;
1246 switch ( frequency )
1249 if ( position % 5 != 0 )
1253 if ( position % 300 == 0 )
1255 else if ( position % 30 == 0 )
1257 else if ( position % 2 == 0 )
1263 if ( position % 30 == 0 )
1265 else if ( position % 29 == 0 )
1267 else if ( position % 4 == 2 )
1276 else if ( fps != 0 )
1278 samples = frequency / fps;
1284 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
1286 int64_t samples = 0;
1288 // TODO: Correct rules for NTSC and drop the * 100 hack
1289 if ( ( int )( fps * 100 ) == 2997 )
1291 samples = ( ( double )( frame * frequency ) / 30 );
1295 samples += 2 * ( frame / 5 );
1298 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
1301 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
1305 else if ( fps != 0 )
1307 samples = ( ( frame * frequency ) / ( int )fps );