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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * 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"
30 /** Constructor for a frame.
33 mlt_frame mlt_frame_init( )
36 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
40 // Get the normalisation
41 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
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 );
51 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
53 mlt_properties_set_int( properties, "width", 720 );
54 mlt_properties_set_int( properties, "height", 576 );
55 mlt_properties_set_int( properties, "normalised_width", 720 );
56 mlt_properties_set_int( properties, "normalised_height", 576 );
57 mlt_properties_set_double( properties, "aspect_ratio", 59.0/54.0 );
61 mlt_properties_set_int( properties, "width", 720 );
62 mlt_properties_set_int( properties, "height", 480 );
63 mlt_properties_set_int( properties, "normalised_width", 720 );
64 mlt_properties_set_int( properties, "normalised_height", 480 );
65 mlt_properties_set_double( properties, "aspect_ratio", 10.0/11.0 );
68 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
69 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
71 // Construct stacks for frames and methods
72 this->stack_image = mlt_deque_init( );
73 this->stack_audio = mlt_deque_init( );
74 this->stack_service = mlt_deque_init( );
80 /** Fetch the frames properties.
83 mlt_properties mlt_frame_properties( mlt_frame this )
85 return this != NULL ? &this->parent : NULL;
88 /** Check if we have a way to derive something other than a test card.
91 int mlt_frame_is_test_card( mlt_frame this )
93 return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
96 /** Check if we have a way to derive something other than test audio.
99 int mlt_frame_is_test_audio( mlt_frame this )
101 return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
104 /** Get the aspect ratio of the frame.
107 double mlt_frame_get_aspect_ratio( mlt_frame this )
109 return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
112 /** Set the aspect ratio of the frame.
115 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
117 return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
120 /** Get the position of this frame.
123 mlt_position mlt_frame_get_position( mlt_frame this )
125 int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
126 return pos < 0 ? 0 : pos;
129 /** Set the position of this frame.
132 int mlt_frame_set_position( mlt_frame this, mlt_position value )
134 return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
137 /** Stack a get_image callback.
140 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
142 return mlt_deque_push_back( this->stack_image, get_image );
145 /** Pop a get_image callback.
148 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
150 return mlt_deque_pop_back( this->stack_image );
156 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
158 return mlt_deque_push_back( this->stack_image, that );
164 mlt_frame mlt_frame_pop_frame( mlt_frame this )
166 return mlt_deque_pop_back( this->stack_image );
172 int mlt_frame_push_service( mlt_frame this, void *that )
174 return mlt_deque_push_back( this->stack_image, that );
180 void *mlt_frame_pop_service( mlt_frame this )
182 return mlt_deque_pop_back( this->stack_image );
188 int mlt_frame_push_service_int( mlt_frame this, int that )
190 return mlt_deque_push_back_int( this->stack_image, that );
196 int mlt_frame_pop_service_int( mlt_frame this )
198 return mlt_deque_pop_back_int( this->stack_image );
201 /** Push an audio item on the stack.
204 int mlt_frame_push_audio( mlt_frame this, void *that )
206 return mlt_deque_push_back( this->stack_audio, that );
209 /** Pop an audio item from the stack
212 void *mlt_frame_pop_audio( mlt_frame this )
214 return mlt_deque_pop_back( this->stack_audio );
217 /** Return the service stack
220 mlt_deque mlt_frame_service_stack( mlt_frame this )
222 return this->stack_service;
225 /** Replace image stack with the information provided.
227 This might prove to be unreliable and restrictive - the idea is that a transition
228 which normally uses two images may decide to only use the b frame (ie: in the case
229 of a composite where the b frame completely obscures the a frame).
231 The image must be writable and the destructor for the image itself must be taken
232 care of on another frame and that frame cannot have a replace applied to it...
233 Further it assumes that no alpha mask is in use.
235 For these reasons, it can only be used in a specific situation - when you have
236 multiple tracks each with their own transition and these transitions are applied
237 in a strictly reversed order (ie: highest numbered [lowest track] is processed
240 More reliable approach - the cases should be detected during the process phase
241 and the upper tracks should simply not be invited to stack...
244 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
246 // Remove all items from the stack
247 while( mlt_deque_pop_back( this->stack_image ) ) ;
249 // Update the information
250 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
251 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
252 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
253 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
254 this->get_alpha_mask = NULL;
257 /** Get the image associated to the frame.
260 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
262 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
263 mlt_get_image get_image = mlt_frame_pop_get_image( this );
264 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
267 if ( get_image != NULL )
269 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
270 mlt_position position = mlt_frame_get_position( this );
271 error = get_image( this, buffer, format, width, height, writable );
272 mlt_properties_set_int( properties, "width", *width );
273 mlt_properties_set_int( properties, "height", *height );
274 mlt_properties_set_int( properties, "format", *format );
275 mlt_frame_set_position( this, position );
277 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
279 *format = mlt_properties_get_int( properties, "format" );
280 *buffer = mlt_properties_get_data( properties, "image", NULL );
281 *width = mlt_properties_get_int( properties, "width" );
282 *height = mlt_properties_get_int( properties, "height" );
284 else if ( producer != NULL )
286 mlt_frame test_frame = NULL;
287 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
288 if ( test_frame != NULL )
290 mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
291 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
292 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
293 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
294 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
295 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
296 mlt_properties_set_int( properties, "width", *width );
297 mlt_properties_set_int( properties, "height", *height );
298 mlt_properties_set_int( properties, "format", *format );
299 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
303 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
304 mlt_frame_get_image( this, buffer, format, width, height, writable );
313 *width = *width == 0 ? 720 : *width;
314 *height = *height == 0 ? 576 : *height;
315 size = *width * *height;
317 mlt_properties_set_int( properties, "format", *format );
318 mlt_properties_set_int( properties, "width", *width );
319 mlt_properties_set_int( properties, "height", *height );
320 mlt_properties_set_int( properties, "aspect_ratio", 0 );
328 case mlt_image_rgb24:
331 *buffer = mlt_pool_alloc( size );
333 memset( *buffer, 255, size );
335 case mlt_image_rgb24a:
336 case mlt_image_opengl:
339 *buffer = mlt_pool_alloc( size );
341 memset( *buffer, 255, size );
343 case mlt_image_yuv422:
346 *buffer = mlt_pool_alloc( size );
349 while ( p != NULL && p != q )
355 case mlt_image_yuv420p:
357 *buffer = mlt_pool_alloc( size );
359 memset( *buffer, 255, size );
363 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
364 mlt_properties_set_int( properties, "test_image", 1 );
367 mlt_properties_set_int( properties, "scaled_width", *width );
368 mlt_properties_set_int( properties, "scaled_height", *height );
373 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
375 uint8_t *alpha = NULL;
378 if ( this->get_alpha_mask != NULL )
379 alpha = this->get_alpha_mask( this );
381 alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
384 int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
385 alpha = mlt_pool_alloc( size );
386 memset( alpha, 255, size );
387 mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
393 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
395 mlt_get_audio get_audio = mlt_frame_pop_audio( this );
396 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
397 int hide = mlt_properties_get_int( properties, "test_audio" );
399 if ( hide == 0 && get_audio != NULL )
401 mlt_position position = mlt_frame_get_position( this );
402 get_audio( this, buffer, format, frequency, channels, samples );
403 mlt_frame_set_position( this, position );
405 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
407 *buffer = mlt_properties_get_data( properties, "audio", NULL );
408 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
409 *channels = mlt_properties_get_int( properties, "audio_channels" );
410 *samples = mlt_properties_get_int( properties, "audio_samples" );
415 *samples = *samples <= 0 ? 1920 : *samples;
416 *channels = *channels <= 0 ? 2 : *channels;
417 *frequency = *frequency <= 0 ? 48000 : *frequency;
418 size = *samples * *channels * sizeof( int16_t );
419 *buffer = mlt_pool_alloc( size );
420 if ( *buffer != NULL )
421 memset( *buffer, 0, size );
422 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
423 mlt_properties_set_int( properties, "test_audio", 1 );
426 mlt_properties_set_int( properties, "audio_frequency", *frequency );
427 mlt_properties_set_int( properties, "audio_channels", *channels );
428 mlt_properties_set_int( properties, "audio_samples", *samples );
430 if ( mlt_properties_get( properties, "meta.volume" ) )
432 double value = mlt_properties_get_double( properties, "meta.volume" );
436 memset( *buffer, 0, *samples * *channels * 2 );
438 else if ( value != 1.0 )
440 int total = *samples * *channels;
441 int16_t *p = *buffer;
449 mlt_properties_set( properties, "meta.volume", NULL );
455 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
458 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
459 mlt_audio_format format = mlt_audio_pcm;
460 int frequency = 32000; // lower frequency available?
462 double fps = mlt_properties_get_double( properties, "fps" );
463 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
466 mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
468 // Make an 8-bit buffer large enough to hold rendering
470 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
471 if ( bitmap != NULL )
472 memset( bitmap, 0, size );
473 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
475 // Render vertical lines
476 int16_t *ubound = pcm + samples * channels;
477 int skip = samples / w - 1;
480 // Iterate sample stream and along x coordinate
481 for ( i = 0; i < w && pcm < ubound; i++ )
483 // pcm data has channels interleaved
484 for ( j = 0; j < channels; j++ )
486 // Determine sample's magnitude from 2s complement;
487 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
488 // The height of a line is the ratio of the magnitude multiplied by
489 // half the vertical resolution
490 int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
491 // Determine the starting y coordinate - left channel above center,
492 // right channel below - currently assumes 2 channels
493 int displacement = ( h / 2 ) - ( 1 - j ) * height;
494 // Position buffer pointer using y coordinate, stride, and x coordinate
495 unsigned char *p = &bitmap[ i + displacement * w ];
497 // Draw vertical line
498 for ( k = 0; k < height; k++ )
503 pcm += skip * channels;
509 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
512 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
516 void mlt_frame_close( mlt_frame this )
518 if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
520 mlt_deque_close( this->stack_image );
521 mlt_deque_close( this->stack_audio );
522 while( mlt_deque_peek_back( this->stack_service ) )
523 mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
524 mlt_deque_close( this->stack_service );
525 mlt_properties_close( &this->parent );
530 /***** convenience functions *****/
532 int mlt_convert_yuv422_to_rgb24a( uint8_t *yuv, uint8_t *rgba, unsigned int total )
535 int yy, uu, vv, ug_plus_vg, ub, vr;
543 ug_plus_vg = uu * 88 + vv * 183;
547 g = (yy - ug_plus_vg) >> 8;
549 rgba[0] = r < 0 ? 0 : (r > 255 ? 255 : (unsigned char)r);
550 rgba[1] = g < 0 ? 0 : (g > 255 ? 255 : (unsigned char)g);
551 rgba[2] = b < 0 ? 0 : (b > 255 ? 255 : (unsigned char)b);
555 g = (yy - ug_plus_vg) >> 8;
557 rgba[4] = r < 0 ? 0 : (r > 255 ? 255 : (unsigned char)r);
558 rgba[5] = g < 0 ? 0 : (g > 255 ? 255 : (unsigned char)g);
559 rgba[6] = b < 0 ? 0 : (b > 255 ? 255 : (unsigned char)b);
567 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
570 register int y0, y1, u0, u1, v0, v1;
571 register int r, g, b;
572 register uint8_t *d = yuv;
575 for ( i = 0; i < height; i++ )
577 register uint8_t *s = rgba + ( stride * i );
578 for ( j = 0; j < ( width / 2 ); j++ )
584 RGB2YUV (r, g, b, y0, u0 , v0);
589 RGB2YUV (r, g, b, y1, u1 , v1);
601 RGB2YUV (r, g, b, y0, u0 , v0);
609 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
612 register int y0, y1, u0, u1, v0, v1;
613 register int r, g, b;
614 register uint8_t *d = yuv;
617 for ( i = 0; i < height; i++ )
619 register uint8_t *s = rgb + ( stride * i );
620 for ( j = 0; j < ( width / 2 ); j++ )
625 RGB2YUV (r, g, b, y0, u0 , v0);
629 RGB2YUV (r, g, b, y1, u1 , v1);
640 RGB2YUV (r, g, b, y0, u0 , v0);
648 int mlt_convert_bgr24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
651 register int y0, y1, u0, u1, v0, v1;
652 register int r, g, b;
653 register uint8_t *d = yuv;
656 for ( i = 0; i < height; i++ )
658 register uint8_t *s = rgba + ( stride * i );
659 for ( j = 0; j < ( width / 2 ); j++ )
665 RGB2YUV (r, g, b, y0, u0 , v0);
670 RGB2YUV (r, g, b, y1, u1 , v1);
682 RGB2YUV (r, g, b, y0, u0 , v0);
690 int mlt_convert_bgr24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
693 register int y0, y1, u0, u1, v0, v1;
694 register int r, g, b;
695 register uint8_t *d = yuv;
698 for ( i = 0; i < height; i++ )
700 register uint8_t *s = rgb + ( stride * i );
701 for ( j = 0; j < ( width / 2 ); j++ )
706 RGB2YUV (r, g, b, y0, u0 , v0);
710 RGB2YUV (r, g, b, y1, u1 , v1);
721 RGB2YUV (r, g, b, y0, u0 , v0);
729 int mlt_convert_argb_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
732 register int y0, y1, u0, u1, v0, v1;
733 register int r, g, b;
734 register uint8_t *d = yuv;
737 for ( i = 0; i < height; i++ )
739 register uint8_t *s = rgba + ( stride * i );
740 for ( j = 0; j < ( width / 2 ); j++ )
746 RGB2YUV (r, g, b, y0, u0 , v0);
751 RGB2YUV (r, g, b, y1, u1 , v1);
763 RGB2YUV (r, g, b, y0, u0 , v0);
771 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
776 int half = width >> 1;
778 uint8_t *Y = yuv420p;
779 uint8_t *U = Y + width * height;
780 uint8_t *V = U + width * height / 4;
782 register uint8_t *d = yuv;
784 for ( i = 0; i < height; i++ )
786 register uint8_t *u = U + ( i / 2 ) * ( half );
787 register uint8_t *v = V + ( i / 2 ) * ( half );
789 for ( j = 0; j < half; j++ )
800 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
802 uint8_t *output = NULL;
804 if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
807 int offset_x = ( owidth - iwidth ) / 2;
808 int offset_y = ( oheight - iheight ) / 2;
811 output = mlt_pool_alloc( owidth * oheight );
812 memset( output, alpha_value, owidth * oheight );
814 offset_x -= offset_x % 2;
816 out_line = output + offset_y * owidth;
817 out_line += offset_x;
819 // Loop for the entirety of our output height.
822 // We're in the input range for this row.
823 memcpy( out_line, input, iused );
825 // Move to next input line
828 // Move to next output line
836 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
839 int istride = iwidth * 2;
840 int ostride = owidth * 2;
841 int offset_x = ( owidth - iwidth );
842 int offset_y = ( oheight - iheight ) / 2;
843 uint8_t *in_line = input;
845 int size = owidth * oheight;
848 // Optimisation point
849 if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
853 else if ( iwidth == owidth && iheight == oheight )
855 memcpy( output, input, iheight * istride );
865 offset_x -= offset_x % 4;
867 out_line = output + offset_y * ostride;
868 out_line += offset_x;
870 // Loop for the entirety of our output height.
873 // We're in the input range for this row.
874 memcpy( out_line, in_line, iwidth * 2 );
876 // Move to next input line
879 // Move to next output line
884 /** A resizing function for yuv422 frames - this does not rescale, but simply
885 resizes. It assumes yuv422 images available on the frame so use with care.
888 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
891 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
893 // Get the input image, width and height
894 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
895 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
897 int iwidth = mlt_properties_get_int( properties, "width" );
898 int iheight = mlt_properties_get_int( properties, "height" );
900 // If width and height are correct, don't do anything
901 if ( iwidth != owidth || iheight != oheight )
903 uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
905 // Create the output image
906 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
908 // Call the generic resize
909 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
911 // Now update the frame
912 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
913 mlt_properties_set_int( properties, "width", owidth );
914 mlt_properties_set_int( properties, "height", oheight );
916 // We should resize the alpha too
917 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
920 mlt_properties_set_data( properties, "alpha", alpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
921 this->get_alpha_mask = NULL;
927 // No change, return input
931 /** A rescaling function for yuv422 frames - low quality, and provided for testing
932 only. It assumes yuv422 images available on the frame so use with care.
935 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
938 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
940 // Get the input image, width and height
941 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
942 int iwidth = mlt_properties_get_int( properties, "width" );
943 int iheight = mlt_properties_get_int( properties, "height" );
945 // If width and height are correct, don't do anything
946 if ( iwidth != owidth || iheight != oheight )
948 // Create the output image
949 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
952 int istride = iwidth * 2;
953 int ostride = owidth * 2;
955 iwidth = iwidth - ( iwidth % 4 );
957 // Derived coordinates
961 int out_x_range = owidth / 2;
962 int out_y_range = oheight / 2;
963 int in_x_range = iwidth / 2;
964 int in_y_range = iheight / 2;
967 register uint8_t *out_line = output;
968 register uint8_t *out_ptr;
970 // Calculate a middle pointer
971 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
974 // Generate the affine transform scaling values
975 register int scale_width = ( iwidth << 16 ) / owidth;
976 register int scale_height = ( iheight << 16 ) / oheight;
977 register int base = 0;
979 int outer = out_x_range * scale_width;
980 int bottom = out_y_range * scale_height;
982 // Loop for the entirety of our output height.
983 for ( dy = - bottom; dy < bottom; dy += scale_height )
985 // Start at the beginning of the line
988 // Pointer to the middle of the input line
989 in_line = in_middle + ( dy >> 16 ) * istride;
991 // Loop for the entirety of our output row.
992 for ( dx = - outer; dx < outer; dx += scale_width )
996 *out_ptr ++ = *( in_line + base );
998 *out_ptr ++ = *( in_line + base + 1 );
1002 *out_ptr ++ = *( in_line + base );
1004 *out_ptr ++ = *( in_line + base + 3 );
1007 // Move to next output line
1008 out_line += ostride;
1011 // Now update the frame
1012 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
1013 mlt_properties_set_int( properties, "width", owidth );
1014 mlt_properties_set_int( properties, "height", oheight );
1016 // Return the output
1020 // No change, return input
1024 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 )
1027 int16_t *src, *dest;
1028 int frequency_src = *frequency, frequency_dest = *frequency;
1029 int channels_src = *channels, channels_dest = *channels;
1030 int samples_src = *samples, samples_dest = *samples;
1032 double d = 0, s = 0;
1034 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1035 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1037 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1038 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1040 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1042 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1043 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1045 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1047 if ( channels_src > 6 )
1049 if ( channels_dest > 6 )
1051 if ( samples_src > 4000 )
1053 if ( samples_dest > 4000 )
1056 // determine number of samples to process
1057 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1058 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1060 *frequency = frequency_dest;
1062 // Compute a smooth ramp over start to end
1063 float weight = weight_start;
1064 float weight_step = ( weight_end - weight_start ) / *samples;
1068 *samples = samples_src;
1069 *channels = channels_src;
1071 *frequency = frequency_src;
1076 for ( i = 0; i < *samples; i++ )
1078 for ( j = 0; j < *channels; j++ )
1080 if ( j < channels_dest )
1081 d = (double) dest[ i * channels_dest + j ];
1082 if ( j < channels_src )
1083 s = (double) src[ i * channels_src + j ];
1084 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1086 weight += weight_step;
1092 // Replacement for broken mlt_frame_audio_mix - this filter uses an inline low pass filter
1093 // to allow mixing without volume hacking
1094 int mlt_frame_combine_audio( mlt_frame this, mlt_frame that, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1097 int16_t *src, *dest;
1098 int frequency_src = *frequency, frequency_dest = *frequency;
1099 int channels_src = *channels, channels_dest = *channels;
1100 int samples_src = *samples, samples_dest = *samples;
1103 double b_weight = 1.0;
1105 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "meta.mixdown" ) )
1106 b_weight = 1.0 - mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "meta.volume" );
1108 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1109 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1111 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1112 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1114 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1116 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1117 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1119 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1123 *samples = samples_src;
1124 *channels = channels_src;
1126 *frequency = frequency_src;
1130 // determine number of samples to process
1131 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1132 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1134 *frequency = frequency_dest;
1136 for ( j = 0; j < *channels; j++ )
1137 vp[ j ] = ( double )dest[ j ];
1140 double B = exp(-2.0 * M_PI * Fc);
1144 for ( i = 0; i < *samples; i++ )
1146 for ( j = 0; j < *channels; j++ )
1148 v = ( double )( b_weight * dest[ i * channels_dest + j ] + src[ i * channels_src + j ] );
1149 v = v < -32767 ? -32767 : v > 32768 ? 32768 : v;
1150 vp[ j ] = dest[ i * channels_dest + j ] = ( int16_t )( v * A + vp[ j ] * B );
1157 /* Will this break when mlt_position is converted to double? -Zach */
1158 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1162 if ( ( int )( fps * 100 ) == 2997 )
1164 samples = frequency / 30;
1166 switch ( frequency )
1169 if ( position % 5 != 0 )
1173 if ( position % 300 == 0 )
1175 else if ( position % 30 == 0 )
1177 else if ( position % 2 == 0 )
1183 if ( position % 30 == 0 )
1185 else if ( position % 29 == 0 )
1187 else if ( position % 4 == 2 )
1196 else if ( fps != 0 )
1198 samples = frequency / fps;
1204 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
1206 int64_t samples = 0;
1208 // TODO: Correct rules for NTSC and drop the * 100 hack
1209 if ( ( int )( fps * 100 ) == 2997 )
1211 samples = ( ( double )( frame * frequency ) / 30 );
1215 samples += 2 * ( frame / 5 );
1218 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
1221 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
1225 else if ( fps != 0 )
1227 samples = ( ( frame * frequency ) / ( int )fps );