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( mlt_service service )
38 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
42 mlt_profile profile = mlt_service_profile( service );
44 // Initialise the properties
45 mlt_properties properties = &this->parent;
46 mlt_properties_init( properties, this );
48 // Set default properties on the frame
49 mlt_properties_set_position( properties, "_position", 0.0 );
50 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
51 mlt_properties_set_int( properties, "width", profile? profile->width : 720 );
52 mlt_properties_set_int( properties, "height", profile? profile->height : 576 );
53 mlt_properties_set_int( properties, "normalised_width", profile? profile->width : 720 );
54 mlt_properties_set_int( properties, "normalised_height", profile? profile->height : 576 );
55 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
56 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
57 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
59 // Construct stacks for frames and methods
60 this->stack_image = mlt_deque_init( );
61 this->stack_audio = mlt_deque_init( );
62 this->stack_service = mlt_deque_init( );
68 /** Fetch the frames properties.
71 mlt_properties mlt_frame_properties( mlt_frame this )
73 return this != NULL ? &this->parent : NULL;
76 /** Check if we have a way to derive something other than a test card.
79 int mlt_frame_is_test_card( mlt_frame this )
81 return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
84 /** Check if we have a way to derive something other than test audio.
87 int mlt_frame_is_test_audio( mlt_frame this )
89 return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
92 /** Get the aspect ratio of the frame.
95 double mlt_frame_get_aspect_ratio( mlt_frame this )
97 return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
100 /** Set the aspect ratio of the frame.
103 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
105 return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
108 /** Get the position of this frame.
111 mlt_position mlt_frame_get_position( mlt_frame this )
113 int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
114 return pos < 0 ? 0 : pos;
117 /** Set the position of this frame.
120 int mlt_frame_set_position( mlt_frame this, mlt_position value )
122 return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
125 /** Stack a get_image callback.
128 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
130 return mlt_deque_push_back( this->stack_image, get_image );
133 /** Pop a get_image callback.
136 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
138 return mlt_deque_pop_back( this->stack_image );
144 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
146 return mlt_deque_push_back( this->stack_image, that );
152 mlt_frame mlt_frame_pop_frame( mlt_frame this )
154 return mlt_deque_pop_back( this->stack_image );
160 int mlt_frame_push_service( mlt_frame this, void *that )
162 return mlt_deque_push_back( this->stack_image, that );
168 void *mlt_frame_pop_service( mlt_frame this )
170 return mlt_deque_pop_back( this->stack_image );
176 int mlt_frame_push_service_int( mlt_frame this, int that )
178 return mlt_deque_push_back_int( this->stack_image, that );
184 int mlt_frame_pop_service_int( mlt_frame this )
186 return mlt_deque_pop_back_int( this->stack_image );
189 /** Push an audio item on the stack.
192 int mlt_frame_push_audio( mlt_frame this, void *that )
194 return mlt_deque_push_back( this->stack_audio, that );
197 /** Pop an audio item from the stack
200 void *mlt_frame_pop_audio( mlt_frame this )
202 return mlt_deque_pop_back( this->stack_audio );
205 /** Return the service stack
208 mlt_deque mlt_frame_service_stack( mlt_frame this )
210 return this->stack_service;
213 /** Replace image stack with the information provided.
215 This might prove to be unreliable and restrictive - the idea is that a transition
216 which normally uses two images may decide to only use the b frame (ie: in the case
217 of a composite where the b frame completely obscures the a frame).
219 The image must be writable and the destructor for the image itself must be taken
220 care of on another frame and that frame cannot have a replace applied to it...
221 Further it assumes that no alpha mask is in use.
223 For these reasons, it can only be used in a specific situation - when you have
224 multiple tracks each with their own transition and these transitions are applied
225 in a strictly reversed order (ie: highest numbered [lowest track] is processed
228 More reliable approach - the cases should be detected during the process phase
229 and the upper tracks should simply not be invited to stack...
232 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
234 // Remove all items from the stack
235 while( mlt_deque_pop_back( this->stack_image ) ) ;
237 // Update the information
238 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
239 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
240 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
241 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
242 this->get_alpha_mask = NULL;
245 /** Get the image associated to the frame.
248 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
250 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
251 mlt_get_image get_image = mlt_frame_pop_get_image( this );
252 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
255 if ( get_image != NULL )
257 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
258 mlt_position position = mlt_frame_get_position( this );
259 error = get_image( this, buffer, format, width, height, writable );
260 mlt_properties_set_int( properties, "width", *width );
261 mlt_properties_set_int( properties, "height", *height );
262 mlt_properties_set_int( properties, "format", *format );
263 mlt_frame_set_position( this, position );
265 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
267 *format = mlt_properties_get_int( properties, "format" );
268 *buffer = mlt_properties_get_data( properties, "image", NULL );
269 *width = mlt_properties_get_int( properties, "width" );
270 *height = mlt_properties_get_int( properties, "height" );
272 else if ( producer != NULL )
274 mlt_frame test_frame = NULL;
275 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
276 if ( test_frame != NULL )
278 mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
279 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
280 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
281 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
282 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
283 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
284 mlt_properties_set_int( properties, "width", *width );
285 mlt_properties_set_int( properties, "height", *height );
286 mlt_properties_set_int( properties, "format", *format );
287 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
291 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
292 mlt_frame_get_image( this, buffer, format, width, height, writable );
301 *width = *width == 0 ? 720 : *width;
302 *height = *height == 0 ? 576 : *height;
303 size = *width * *height;
305 mlt_properties_set_int( properties, "format", *format );
306 mlt_properties_set_int( properties, "width", *width );
307 mlt_properties_set_int( properties, "height", *height );
308 mlt_properties_set_int( properties, "aspect_ratio", 0 );
316 case mlt_image_rgb24:
319 *buffer = mlt_pool_alloc( size );
321 memset( *buffer, 255, size );
323 case mlt_image_rgb24a:
324 case mlt_image_opengl:
327 *buffer = mlt_pool_alloc( size );
329 memset( *buffer, 255, size );
331 case mlt_image_yuv422:
334 *buffer = mlt_pool_alloc( size );
337 while ( p != NULL && p != q )
343 case mlt_image_yuv420p:
345 *buffer = mlt_pool_alloc( size );
347 memset( *buffer, 255, size );
351 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
352 mlt_properties_set_int( properties, "test_image", 1 );
355 mlt_properties_set_int( properties, "scaled_width", *width );
356 mlt_properties_set_int( properties, "scaled_height", *height );
361 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
363 uint8_t *alpha = NULL;
366 if ( this->get_alpha_mask != NULL )
367 alpha = this->get_alpha_mask( this );
369 alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
372 int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
373 alpha = mlt_pool_alloc( size );
374 memset( alpha, 255, size );
375 mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
381 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
383 mlt_get_audio get_audio = mlt_frame_pop_audio( this );
384 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
385 int hide = mlt_properties_get_int( properties, "test_audio" );
387 if ( hide == 0 && get_audio != NULL )
389 mlt_position position = mlt_frame_get_position( this );
390 get_audio( this, buffer, format, frequency, channels, samples );
391 mlt_frame_set_position( this, position );
393 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
395 *buffer = mlt_properties_get_data( properties, "audio", NULL );
396 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
397 *channels = mlt_properties_get_int( properties, "audio_channels" );
398 *samples = mlt_properties_get_int( properties, "audio_samples" );
403 *samples = *samples <= 0 ? 1920 : *samples;
404 *channels = *channels <= 0 ? 2 : *channels;
405 *frequency = *frequency <= 0 ? 48000 : *frequency;
406 size = *samples * *channels * sizeof( int16_t );
407 *buffer = mlt_pool_alloc( size );
408 if ( *buffer != NULL )
409 memset( *buffer, 0, size );
410 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
411 mlt_properties_set_int( properties, "test_audio", 1 );
414 mlt_properties_set_int( properties, "audio_frequency", *frequency );
415 mlt_properties_set_int( properties, "audio_channels", *channels );
416 mlt_properties_set_int( properties, "audio_samples", *samples );
418 if ( mlt_properties_get( properties, "meta.volume" ) )
420 double value = mlt_properties_get_double( properties, "meta.volume" );
424 memset( *buffer, 0, *samples * *channels * 2 );
426 else if ( value != 1.0 )
428 int total = *samples * *channels;
429 int16_t *p = *buffer;
437 mlt_properties_set( properties, "meta.volume", NULL );
443 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
446 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
447 mlt_audio_format format = mlt_audio_pcm;
448 int frequency = 32000; // lower frequency available?
450 double fps = mlt_profile_fps( NULL );
451 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
454 mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
456 // Make an 8-bit buffer large enough to hold rendering
458 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
459 if ( bitmap != NULL )
460 memset( bitmap, 0, size );
461 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
463 // Render vertical lines
464 int16_t *ubound = pcm + samples * channels;
465 int skip = samples / w - 1;
468 // Iterate sample stream and along x coordinate
469 for ( i = 0; i < w && pcm < ubound; i++ )
471 // pcm data has channels interleaved
472 for ( j = 0; j < channels; j++ )
474 // Determine sample's magnitude from 2s complement;
475 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
476 // The height of a line is the ratio of the magnitude multiplied by
477 // half the vertical resolution
478 int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
479 // Determine the starting y coordinate - left channel above center,
480 // right channel below - currently assumes 2 channels
481 int displacement = ( h / 2 ) - ( 1 - j ) * height;
482 // Position buffer pointer using y coordinate, stride, and x coordinate
483 unsigned char *p = &bitmap[ i + displacement * w ];
485 // Draw vertical line
486 for ( k = 0; k < height; k++ )
491 pcm += skip * channels;
497 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
500 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
504 void mlt_frame_close( mlt_frame this )
506 if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
508 mlt_deque_close( this->stack_image );
509 mlt_deque_close( this->stack_audio );
510 while( mlt_deque_peek_back( this->stack_service ) )
511 mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
512 mlt_deque_close( this->stack_service );
513 mlt_properties_close( &this->parent );
518 /***** convenience functions *****/
520 int mlt_convert_yuv422_to_rgb24a( uint8_t *yuv, uint8_t *rgba, unsigned int total )
531 YUV2RGB(yy, uu, vv, r, g, b);
537 YUV2RGB(yy, uu, vv, r, g, b);
548 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
551 register int y0, y1, u0, u1, v0, v1;
552 register int r, g, b;
553 register uint8_t *d = yuv;
557 for ( i = 0; i < height; i++ )
559 register uint8_t *s = rgba + ( stride * i );
560 for ( j = 0; j < ( width / 2 ); j++ )
566 RGB2YUV (r, g, b, y0, u0 , v0);
571 RGB2YUV (r, g, b, y1, u1 , v1);
583 RGB2YUV (r, g, b, y0, u0 , v0);
589 for ( i = 0; i < height; i++ )
591 register uint8_t *s = rgba + ( stride * i );
592 for ( j = 0; j < ( width / 2 ); j++ )
598 RGB2YUV (r, g, b, y0, u0 , v0);
603 RGB2YUV (r, g, b, y1, u1 , v1);
615 RGB2YUV (r, g, b, y0, u0 , v0);
624 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
627 register int y0, y1, u0, u1, v0, v1;
628 register int r, g, b;
629 register uint8_t *d = yuv;
632 for ( i = 0; i < height; i++ )
634 register uint8_t *s = rgb + ( stride * i );
635 for ( j = 0; j < ( width / 2 ); j++ )
640 RGB2YUV (r, g, b, y0, u0 , v0);
644 RGB2YUV (r, g, b, y1, u1 , v1);
655 RGB2YUV (r, g, b, y0, u0 , v0);
663 int mlt_convert_bgr24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
666 register int y0, y1, u0, u1, v0, v1;
667 register int r, g, b;
668 register uint8_t *d = yuv;
672 for ( i = 0; i < height; i++ )
674 register uint8_t *s = rgba + ( stride * i );
675 for ( j = 0; j < ( width / 2 ); j++ )
681 RGB2YUV (r, g, b, y0, u0 , v0);
686 RGB2YUV (r, g, b, y1, u1 , v1);
698 RGB2YUV (r, g, b, y0, u0 , v0);
704 for ( i = 0; i < height; i++ )
706 register uint8_t *s = rgba + ( stride * i );
707 for ( j = 0; j < ( width / 2 ); j++ )
713 RGB2YUV (r, g, b, y0, u0 , v0);
718 RGB2YUV (r, g, b, y1, u1 , v1);
730 RGB2YUV (r, g, b, y0, u0 , v0);
738 int mlt_convert_bgr24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
741 register int y0, y1, u0, u1, v0, v1;
742 register int r, g, b;
743 register uint8_t *d = yuv;
746 for ( i = 0; i < height; i++ )
748 register uint8_t *s = rgb + ( stride * i );
749 for ( j = 0; j < ( width / 2 ); j++ )
754 RGB2YUV (r, g, b, y0, u0 , v0);
758 RGB2YUV (r, g, b, y1, u1 , v1);
769 RGB2YUV (r, g, b, y0, u0 , v0);
777 int mlt_convert_argb_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
780 register int y0, y1, u0, u1, v0, v1;
781 register int r, g, b;
782 register uint8_t *d = yuv;
786 for ( i = 0; i < height; i++ )
788 register uint8_t *s = rgba + ( stride * i );
789 for ( j = 0; j < ( width / 2 ); j++ )
795 RGB2YUV (r, g, b, y0, u0 , v0);
800 RGB2YUV (r, g, b, y1, u1 , v1);
812 RGB2YUV (r, g, b, y0, u0 , v0);
818 for ( i = 0; i < height; i++ )
820 register uint8_t *s = rgba + ( stride * i );
821 for ( j = 0; j < ( width / 2 ); j++ )
827 RGB2YUV (r, g, b, y0, u0 , v0);
832 RGB2YUV (r, g, b, y1, u1 , v1);
844 RGB2YUV (r, g, b, y0, u0 , v0);
852 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
857 int half = width >> 1;
859 uint8_t *Y = yuv420p;
860 uint8_t *U = Y + width * height;
861 uint8_t *V = U + width * height / 4;
863 register uint8_t *d = yuv;
865 for ( i = 0; i < height; i++ )
867 register uint8_t *u = U + ( i / 2 ) * ( half );
868 register uint8_t *v = V + ( i / 2 ) * ( half );
870 for ( j = 0; j < half; j++ )
881 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
883 uint8_t *output = NULL;
885 if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
888 int offset_x = ( owidth - iwidth ) / 2;
889 int offset_y = ( oheight - iheight ) / 2;
892 output = mlt_pool_alloc( owidth * oheight );
893 memset( output, alpha_value, owidth * oheight );
895 offset_x -= offset_x % 2;
897 out_line = output + offset_y * owidth;
898 out_line += offset_x;
900 // Loop for the entirety of our output height.
903 // We're in the input range for this row.
904 memcpy( out_line, input, iused );
906 // Move to next input line
909 // Move to next output line
917 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
920 int istride = iwidth * 2;
921 int ostride = owidth * 2;
922 int offset_x = ( owidth - iwidth );
923 int offset_y = ( oheight - iheight ) / 2;
924 uint8_t *in_line = input;
926 int size = owidth * oheight;
929 // Optimisation point
930 if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
934 else if ( iwidth == owidth && iheight == oheight )
936 memcpy( output, input, iheight * istride );
946 offset_x -= offset_x % 4;
948 out_line = output + offset_y * ostride;
949 out_line += offset_x;
951 // Loop for the entirety of our output height.
954 // We're in the input range for this row.
955 memcpy( out_line, in_line, iwidth * 2 );
957 // Move to next input line
960 // Move to next output line
965 /** A resizing function for yuv422 frames - this does not rescale, but simply
966 resizes. It assumes yuv422 images available on the frame so use with care.
969 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
972 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
974 // Get the input image, width and height
975 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
976 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
978 int iwidth = mlt_properties_get_int( properties, "width" );
979 int iheight = mlt_properties_get_int( properties, "height" );
981 // If width and height are correct, don't do anything
982 if ( iwidth != owidth || iheight != oheight )
984 uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
986 // Create the output image
987 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
989 // Call the generic resize
990 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
992 // Now update the frame
993 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
994 mlt_properties_set_int( properties, "width", owidth );
995 mlt_properties_set_int( properties, "height", oheight );
997 // We should resize the alpha too
998 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
1001 mlt_properties_set_data( properties, "alpha", alpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
1002 this->get_alpha_mask = NULL;
1005 // Return the output
1008 // No change, return input
1012 /** A rescaling function for yuv422 frames - low quality, and provided for testing
1013 only. It assumes yuv422 images available on the frame so use with care.
1016 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
1019 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
1021 // Get the input image, width and height
1022 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
1023 int iwidth = mlt_properties_get_int( properties, "width" );
1024 int iheight = mlt_properties_get_int( properties, "height" );
1026 // If width and height are correct, don't do anything
1027 if ( iwidth != owidth || iheight != oheight )
1029 // Create the output image
1030 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
1032 // Calculate strides
1033 int istride = iwidth * 2;
1034 int ostride = owidth * 2;
1036 iwidth = iwidth - ( iwidth % 4 );
1038 // Derived coordinates
1042 int out_x_range = owidth / 2;
1043 int out_y_range = oheight / 2;
1044 int in_x_range = iwidth / 2;
1045 int in_y_range = iheight / 2;
1048 register uint8_t *out_line = output;
1049 register uint8_t *out_ptr;
1051 // Calculate a middle pointer
1052 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
1055 // Generate the affine transform scaling values
1056 register int scale_width = ( iwidth << 16 ) / owidth;
1057 register int scale_height = ( iheight << 16 ) / oheight;
1058 register int base = 0;
1060 int outer = out_x_range * scale_width;
1061 int bottom = out_y_range * scale_height;
1063 // Loop for the entirety of our output height.
1064 for ( dy = - bottom; dy < bottom; dy += scale_height )
1066 // Start at the beginning of the line
1069 // Pointer to the middle of the input line
1070 in_line = in_middle + ( dy >> 16 ) * istride;
1072 // Loop for the entirety of our output row.
1073 for ( dx = - outer; dx < outer; dx += scale_width )
1077 *out_ptr ++ = *( in_line + base );
1079 *out_ptr ++ = *( in_line + base + 1 );
1083 *out_ptr ++ = *( in_line + base );
1085 *out_ptr ++ = *( in_line + base + 3 );
1088 // Move to next output line
1089 out_line += ostride;
1092 // Now update the frame
1093 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
1094 mlt_properties_set_int( properties, "width", owidth );
1095 mlt_properties_set_int( properties, "height", oheight );
1097 // Return the output
1101 // No change, return input
1105 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 )
1108 int16_t *src, *dest;
1109 int frequency_src = *frequency, frequency_dest = *frequency;
1110 int channels_src = *channels, channels_dest = *channels;
1111 int samples_src = *samples, samples_dest = *samples;
1113 double d = 0, s = 0;
1115 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1116 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1118 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1119 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1121 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1123 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1124 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1126 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1128 if ( channels_src > 6 )
1130 if ( channels_dest > 6 )
1132 if ( samples_src > 4000 )
1134 if ( samples_dest > 4000 )
1137 // determine number of samples to process
1138 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1139 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1141 *frequency = frequency_dest;
1143 // Compute a smooth ramp over start to end
1144 float weight = weight_start;
1145 float weight_step = ( weight_end - weight_start ) / *samples;
1149 *samples = samples_src;
1150 *channels = channels_src;
1152 *frequency = frequency_src;
1157 for ( i = 0; i < *samples; i++ )
1159 for ( j = 0; j < *channels; j++ )
1161 if ( j < channels_dest )
1162 d = (double) dest[ i * channels_dest + j ];
1163 if ( j < channels_src )
1164 s = (double) src[ i * channels_src + j ];
1165 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1167 weight += weight_step;
1173 // Replacement for broken mlt_frame_audio_mix - this filter uses an inline low pass filter
1174 // to allow mixing without volume hacking
1175 int mlt_frame_combine_audio( mlt_frame this, mlt_frame that, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1178 int16_t *src, *dest;
1179 int frequency_src = *frequency, frequency_dest = *frequency;
1180 int channels_src = *channels, channels_dest = *channels;
1181 int samples_src = *samples, samples_dest = *samples;
1184 double b_weight = 1.0;
1186 if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "meta.mixdown" ) )
1187 b_weight = 1.0 - mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "meta.volume" );
1189 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1190 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1192 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1193 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1195 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1197 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1198 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1200 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1204 *samples = samples_src;
1205 *channels = channels_src;
1207 *frequency = frequency_src;
1211 // determine number of samples to process
1212 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1213 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1215 *frequency = frequency_dest;
1217 for ( j = 0; j < *channels; j++ )
1218 vp[ j ] = ( double )dest[ j ];
1221 double B = exp(-2.0 * M_PI * Fc);
1225 for ( i = 0; i < *samples; i++ )
1227 for ( j = 0; j < *channels; j++ )
1229 v = ( double )( b_weight * dest[ i * channels_dest + j ] + src[ i * channels_src + j ] );
1230 v = v < -32767 ? -32767 : v > 32768 ? 32768 : v;
1231 vp[ j ] = dest[ i * channels_dest + j ] = ( int16_t )( v * A + vp[ j ] * B );
1238 /* Will this break when mlt_position is converted to double? -Zach */
1239 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1243 if ( ( int )( fps * 100 ) == 2997 )
1245 samples = frequency / 30;
1247 switch ( frequency )
1250 if ( position % 5 != 0 )
1254 if ( position % 300 == 0 )
1256 else if ( position % 30 == 0 )
1258 else if ( position % 2 == 0 )
1264 if ( position % 30 == 0 )
1266 else if ( position % 29 == 0 )
1268 else if ( position % 4 == 2 )
1277 else if ( fps != 0 )
1279 samples = frequency / fps;
1285 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
1287 int64_t samples = 0;
1289 // TODO: Correct rules for NTSC and drop the * 100 hack
1290 if ( ( int )( fps * 100 ) == 2997 )
1292 samples = ( ( double )( frame * frequency ) / 30 );
1296 samples += 2 * ( frame / 5 );
1299 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
1302 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
1306 else if ( fps != 0 )
1308 samples = ( ( frame * frequency ) / ( int )fps );