]> git.sesse.net Git - mlt/blob - src/framework/mlt_frame.c
Massive refactoring of image conversion.
[mlt] / src / framework / mlt_frame.c
1 /**
2  * \file mlt_frame.c
3  * \brief interface for all frame classes
4  * \see mlt_frame_s
5  *
6  * Copyright (C) 2003-2009 Ushodaya Enterprises Limited
7  * \author Charles Yates <charles.yates@pandora.be>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "mlt_frame.h"
25 #include "mlt_producer.h"
26 #include "mlt_factory.h"
27 #include "mlt_profile.h"
28 #include "mlt_log.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34
35 /** Constructor for a frame.
36 */
37
38 mlt_frame mlt_frame_init( mlt_service service )
39 {
40         // Allocate a frame
41         mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
42
43         if ( this != NULL )
44         {
45                 mlt_profile profile = mlt_service_profile( service );
46
47                 // Initialise the properties
48                 mlt_properties properties = &this->parent;
49                 mlt_properties_init( properties, this );
50
51                 // Set default properties on the frame
52                 mlt_properties_set_position( properties, "_position", 0.0 );
53                 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
54                 mlt_properties_set_int( properties, "width", profile? profile->width : 720 );
55                 mlt_properties_set_int( properties, "height", profile? profile->height : 576 );
56                 mlt_properties_set_int( properties, "normalised_width", profile? profile->width : 720 );
57                 mlt_properties_set_int( properties, "normalised_height", profile? profile->height : 576 );
58                 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( NULL ) );
59                 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
60                 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
61
62                 // Construct stacks for frames and methods
63                 this->stack_image = mlt_deque_init( );
64                 this->stack_audio = mlt_deque_init( );
65                 this->stack_service = mlt_deque_init( );
66         }
67
68         return this;
69 }
70
71 /** Fetch the frames properties.
72 */
73
74 mlt_properties mlt_frame_properties( mlt_frame this )
75 {
76         return this != NULL ? &this->parent : NULL;
77 }
78
79 /** Check if we have a way to derive something other than a test card.
80 */
81
82 int mlt_frame_is_test_card( mlt_frame this )
83 {
84         return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
85 }
86
87 /** Check if we have a way to derive something other than test audio.
88 */
89
90 int mlt_frame_is_test_audio( mlt_frame this )
91 {
92         return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
93 }
94
95 /** Get the aspect ratio of the frame.
96 */
97
98 double mlt_frame_get_aspect_ratio( mlt_frame this )
99 {
100         return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
101 }
102
103 /** Set the aspect ratio of the frame.
104 */
105
106 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
107 {
108         return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
109 }
110
111 /** Get the position of this frame.
112 */
113
114 mlt_position mlt_frame_get_position( mlt_frame this )
115 {
116         int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
117         return pos < 0 ? 0 : pos;
118 }
119
120 /** Set the position of this frame.
121 */
122
123 int mlt_frame_set_position( mlt_frame this, mlt_position value )
124 {
125         return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
126 }
127
128 /** Stack a get_image callback.
129 */
130
131 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
132 {
133         return mlt_deque_push_back( this->stack_image, get_image );
134 }
135
136 /** Pop a get_image callback.
137 */
138
139 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
140 {
141         return mlt_deque_pop_back( this->stack_image );
142 }
143
144 /** Push a frame.
145 */
146
147 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
148 {
149         return mlt_deque_push_back( this->stack_image, that );
150 }
151
152 /** Pop a frame.
153 */
154
155 mlt_frame mlt_frame_pop_frame( mlt_frame this )
156 {
157         return mlt_deque_pop_back( this->stack_image );
158 }
159
160 /** Push a service.
161 */
162
163 int mlt_frame_push_service( mlt_frame this, void *that )
164 {
165         return mlt_deque_push_back( this->stack_image, that );
166 }
167
168 /** Pop a service.
169 */
170
171 void *mlt_frame_pop_service( mlt_frame this )
172 {
173         return mlt_deque_pop_back( this->stack_image );
174 }
175
176 /** Push a service.
177 */
178
179 int mlt_frame_push_service_int( mlt_frame this, int that )
180 {
181         return mlt_deque_push_back_int( this->stack_image, that );
182 }
183
184 /** Pop a service.
185 */
186
187 int mlt_frame_pop_service_int( mlt_frame this )
188 {
189         return mlt_deque_pop_back_int( this->stack_image );
190 }
191
192 /** Push an audio item on the stack.
193 */
194
195 int mlt_frame_push_audio( mlt_frame this, void *that )
196 {
197         return mlt_deque_push_back( this->stack_audio, that );
198 }
199
200 /** Pop an audio item from the stack
201 */
202
203 void *mlt_frame_pop_audio( mlt_frame this )
204 {
205         return mlt_deque_pop_back( this->stack_audio );
206 }
207
208 /** Return the service stack
209 */
210
211 mlt_deque mlt_frame_service_stack( mlt_frame this )
212 {
213         return this->stack_service;
214 }
215
216 /** Replace image stack with the information provided.
217
218         This might prove to be unreliable and restrictive - the idea is that a transition
219         which normally uses two images may decide to only use the b frame (ie: in the case
220         of a composite where the b frame completely obscures the a frame).
221
222         The image must be writable and the destructor for the image itself must be taken
223         care of on another frame and that frame cannot have a replace applied to it...
224         Further it assumes that no alpha mask is in use.
225
226         For these reasons, it can only be used in a specific situation - when you have
227         multiple tracks each with their own transition and these transitions are applied
228         in a strictly reversed order (ie: highest numbered [lowest track] is processed
229         first).
230
231         More reliable approach - the cases should be detected during the process phase
232         and the upper tracks should simply not be invited to stack...
233 */
234
235 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
236 {
237         // Remove all items from the stack
238         while( mlt_deque_pop_back( this->stack_image ) ) ;
239
240         // Update the information
241         mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
242         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
243         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
244         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
245         this->get_alpha_mask = NULL;
246 }
247
248 const char * mlt_image_format_name( mlt_image_format format )
249 {
250         switch ( format )
251         {
252                 case mlt_image_none:    return "none";
253                 case mlt_image_rgb24:   return "rgb24";
254                 case mlt_image_rgb24a:  return "rgb24a";
255                 case mlt_image_yuv422:  return "yuv422";
256                 case mlt_image_yuv420p: return "yuv420p";
257                 case mlt_image_opengl:  return "opengl";
258         }
259         return "invalid";
260 }
261
262 /** Get the image associated to the frame.
263 */
264
265 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
266 {
267         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
268         mlt_get_image get_image = mlt_frame_pop_get_image( this );
269         mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
270         mlt_image_format requested_format = *format;
271         int error = 0;
272
273         if ( get_image )
274         {
275                 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
276                 mlt_position position = mlt_frame_get_position( this );
277                 error = get_image( this, buffer, format, width, height, writable );
278                 mlt_properties_set_int( properties, "width", *width );
279                 mlt_properties_set_int( properties, "height", *height );
280                 mlt_properties_set_int( properties, "format", *format );
281                 mlt_frame_set_position( this, position );
282                 if ( this->convert_image )
283                         this->convert_image( this, buffer, format, requested_format );
284         }
285         else if ( mlt_properties_get_data( properties, "image", NULL ) )
286         {
287                 *format = mlt_properties_get_int( properties, "format" );
288                 *buffer = mlt_properties_get_data( properties, "image", NULL );
289                 *width = mlt_properties_get_int( properties, "width" );
290                 *height = mlt_properties_get_int( properties, "height" );
291                 if ( this->convert_image )
292                         this->convert_image( this, buffer, format, requested_format );
293         }
294         else if ( producer )
295         {
296                 mlt_frame test_frame = NULL;
297                 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
298                 if ( test_frame )
299                 {
300                         mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
301                         mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
302                         mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
303                         mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
304                         mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
305                         mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
306 //                      mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
307 //                      mlt_properties_set_int( properties, "width", *width );
308 //                      mlt_properties_set_int( properties, "height", *height );
309 //                      mlt_properties_set_int( properties, "format", *format );
310                 }
311                 else
312                 {
313                         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
314                         mlt_frame_get_image( this, buffer, format, width, height, writable );
315                 }
316         }
317         else
318         {
319                 register uint8_t *p;
320                 register uint8_t *q;
321                 int size = 0;
322
323                 *width = *width == 0 ? 720 : *width;
324                 *height = *height == 0 ? 576 : *height;
325                 size = *width * *height;
326
327                 mlt_properties_set_int( properties, "format", *format );
328                 mlt_properties_set_int( properties, "width", *width );
329                 mlt_properties_set_int( properties, "height", *height );
330                 mlt_properties_set_int( properties, "aspect_ratio", 0 );
331
332                 switch( *format )
333                 {
334                         case mlt_image_none:
335                                 size = 0;
336                                 *buffer = NULL;
337                                 break;
338                         case mlt_image_rgb24:
339                                 size *= 3;
340                                 size += *width * 3;
341                                 *buffer = mlt_pool_alloc( size );
342                                 if ( *buffer )
343                                         memset( *buffer, 255, size );
344                                 break;
345                         case mlt_image_rgb24a:
346                         case mlt_image_opengl:
347                                 size *= 4;
348                                 size += *width * 4;
349                                 *buffer = mlt_pool_alloc( size );
350                                 if ( *buffer )
351                                         memset( *buffer, 255, size );
352                                 break;
353                         case mlt_image_yuv422:
354                                 size *= 2;
355                                 size += *width * 2;
356                                 *buffer = mlt_pool_alloc( size );
357                                 p = *buffer;
358                                 q = p + size;
359                                 while ( p != NULL && p != q )
360                                 {
361                                         *p ++ = 235;
362                                         *p ++ = 128;
363                                 }
364                                 break;
365                         case mlt_image_yuv420p:
366                                 size = size * 3 / 2;
367                                 *buffer = mlt_pool_alloc( size );
368                                 if ( *buffer )
369                                         memset( *buffer, 255, size );
370                                 break;
371                 }
372
373                 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
374                 mlt_properties_set_int( properties, "test_image", 1 );
375         }
376
377         mlt_properties_set_int( properties, "scaled_width", *width );
378         mlt_properties_set_int( properties, "scaled_height", *height );
379
380         return error;
381 }
382
383 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
384 {
385         uint8_t *alpha = NULL;
386         if ( this != NULL )
387         {
388                 if ( this->get_alpha_mask != NULL )
389                         alpha = this->get_alpha_mask( this );
390                 if ( alpha == NULL )
391                         alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
392                 if ( alpha == NULL )
393                 {
394                         int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
395                         alpha = mlt_pool_alloc( size );
396                         memset( alpha, 255, size );
397                         mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
398                 }
399         }
400         return alpha;
401 }
402
403 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
404 {
405         mlt_get_audio get_audio = mlt_frame_pop_audio( this );
406         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
407         int hide = mlt_properties_get_int( properties, "test_audio" );
408
409         if ( hide == 0 && get_audio != NULL )
410         {
411                 mlt_position position = mlt_frame_get_position( this );
412                 get_audio( this, buffer, format, frequency, channels, samples );
413                 mlt_frame_set_position( this, position );
414         }
415         else if ( mlt_properties_get_data( properties, "audio", NULL ) )
416         {
417                 *buffer = mlt_properties_get_data( properties, "audio", NULL );
418                 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
419                 *channels = mlt_properties_get_int( properties, "audio_channels" );
420                 *samples = mlt_properties_get_int( properties, "audio_samples" );
421         }
422         else
423         {
424                 int size = 0;
425                 *samples = *samples <= 0 ? 1920 : *samples;
426                 *channels = *channels <= 0 ? 2 : *channels;
427                 *frequency = *frequency <= 0 ? 48000 : *frequency;
428                 size = *samples * *channels * sizeof( int16_t );
429                 *buffer = mlt_pool_alloc( size );
430                 if ( *buffer != NULL )
431                         memset( *buffer, 0, size );
432                 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
433                 mlt_properties_set_int( properties, "test_audio", 1 );
434         }
435
436         mlt_properties_set_int( properties, "audio_frequency", *frequency );
437         mlt_properties_set_int( properties, "audio_channels", *channels );
438         mlt_properties_set_int( properties, "audio_samples", *samples );
439
440         if ( mlt_properties_get( properties, "meta.volume" ) )
441         {
442                 double value = mlt_properties_get_double( properties, "meta.volume" );
443
444                 if ( value == 0.0 )
445                 {
446                         memset( *buffer, 0, *samples * *channels * 2 );
447                 }
448                 else if ( value != 1.0 )
449                 {
450                         int total = *samples * *channels;
451                         int16_t *p = *buffer;
452                         while ( total -- )
453                         {
454                                 *p = *p * value;
455                                 p ++;
456                         }
457                 }
458
459                 mlt_properties_set( properties, "meta.volume", NULL );
460         }
461
462         return 0;
463 }
464
465 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
466 {
467         int16_t *pcm = NULL;
468         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
469         mlt_audio_format format = mlt_audio_pcm;
470         int frequency = 32000; // lower frequency available?
471         int channels = 2;
472         double fps = mlt_profile_fps( NULL );
473         int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
474
475         // Get the pcm data
476         mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
477
478         // Make an 8-bit buffer large enough to hold rendering
479         int size = w * h;
480         unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
481         if ( bitmap != NULL )
482                 memset( bitmap, 0, size );
483         mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
484
485         // Render vertical lines
486         int16_t *ubound = pcm + samples * channels;
487         int skip = samples / w - 1;
488         int i, j, k;
489
490         // Iterate sample stream and along x coordinate
491         for ( i = 0; i < w && pcm < ubound; i++ )
492         {
493                 // pcm data has channels interleaved
494                 for ( j = 0; j < channels; j++ )
495                 {
496                         // Determine sample's magnitude from 2s complement;
497                         int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
498                         // The height of a line is the ratio of the magnitude multiplied by
499                         // half the vertical resolution
500                         int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
501                         // Determine the starting y coordinate - left channel above center,
502                         // right channel below - currently assumes 2 channels
503                         int displacement = ( h / 2 ) - ( 1 - j ) * height;
504                         // Position buffer pointer using y coordinate, stride, and x coordinate
505                         unsigned char *p = &bitmap[ i + displacement * w ];
506
507                         // Draw vertical line
508                         for ( k = 0; k < height; k++ )
509                                 p[ w * k ] = 0xFF;
510
511                         pcm++;
512                 }
513                 pcm += skip * channels;
514         }
515
516         return bitmap;
517 }
518
519 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
520 {
521         if ( this != NULL )
522                 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
523         return NULL;
524 }
525
526 void mlt_frame_close( mlt_frame this )
527 {
528         if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
529         {
530                 mlt_deque_close( this->stack_image );
531                 mlt_deque_close( this->stack_audio );
532                 while( mlt_deque_peek_back( this->stack_service ) )
533                         mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
534                 mlt_deque_close( this->stack_service );
535                 mlt_properties_close( &this->parent );
536                 free( this );
537         }
538 }
539
540 /***** convenience functions *****/
541
542 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 )
543 {
544         int ret = 0;
545         int16_t *src, *dest;
546         int frequency_src = *frequency, frequency_dest = *frequency;
547         int channels_src = *channels, channels_dest = *channels;
548         int samples_src = *samples, samples_dest = *samples;
549         int i, j;
550         double d = 0, s = 0;
551
552         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
553         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
554
555         int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
556         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
557         if ( silent )
558                 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
559
560         silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
561         mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
562         if ( silent )
563                 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
564
565         if ( channels_src > 6 )
566                 channels_src = 0;
567         if ( channels_dest > 6 )
568                 channels_dest = 0;
569         if ( samples_src > 4000 )
570                 samples_src = 0;
571         if ( samples_dest > 4000 )
572                 samples_dest = 0;
573
574         // determine number of samples to process
575         *samples = samples_src < samples_dest ? samples_src : samples_dest;
576         *channels = channels_src < channels_dest ? channels_src : channels_dest;
577         *buffer = dest;
578         *frequency = frequency_dest;
579
580         // Compute a smooth ramp over start to end
581         float weight = weight_start;
582         float weight_step = ( weight_end - weight_start ) / *samples;
583
584         if ( src == dest )
585         {
586                 *samples = samples_src;
587                 *channels = channels_src;
588                 *buffer = src;
589                 *frequency = frequency_src;
590                 return ret;
591         }
592
593         // Mixdown
594         for ( i = 0; i < *samples; i++ )
595         {
596                 for ( j = 0; j < *channels; j++ )
597                 {
598                         if ( j < channels_dest )
599                                 d = (double) dest[ i * channels_dest + j ];
600                         if ( j < channels_src )
601                                 s = (double) src[ i * channels_src + j ];
602                         dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
603                 }
604                 weight += weight_step;
605         }
606
607         return ret;
608 }
609
610 // Replacement for broken mlt_frame_audio_mix - this filter uses an inline low pass filter
611 // to allow mixing without volume hacking
612 int mlt_frame_combine_audio( mlt_frame this, mlt_frame that, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
613 {
614         int ret = 0;
615         int16_t *src, *dest;
616         int frequency_src = *frequency, frequency_dest = *frequency;
617         int channels_src = *channels, channels_dest = *channels;
618         int samples_src = *samples, samples_dest = *samples;
619         int i, j;
620         double vp[ 6 ];
621         double b_weight = 1.0;
622
623         if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "meta.mixdown" ) )
624                 b_weight = 1.0 - mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "meta.volume" );
625
626         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
627         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
628
629         int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
630         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
631         if ( silent )
632                 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
633
634         silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
635         mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
636         if ( silent )
637                 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
638
639         if ( src == dest )
640         {
641                 *samples = samples_src;
642                 *channels = channels_src;
643                 *buffer = src;
644                 *frequency = frequency_src;
645                 return ret;
646         }
647
648         // determine number of samples to process
649         *samples = samples_src < samples_dest ? samples_src : samples_dest;
650         *channels = channels_src < channels_dest ? channels_src : channels_dest;
651         *buffer = dest;
652         *frequency = frequency_dest;
653
654         for ( j = 0; j < *channels; j++ )
655                 vp[ j ] = ( double )dest[ j ];
656
657         double Fc = 0.5;
658         double B = exp(-2.0 * M_PI * Fc);
659         double A = 1.0 - B;
660         double v;
661
662         for ( i = 0; i < *samples; i++ )
663         {
664                 for ( j = 0; j < *channels; j++ )
665                 {
666                         v = ( double )( b_weight * dest[ i * channels_dest + j ] + src[ i * channels_src + j ] );
667                         v = v < -32767 ? -32767 : v > 32768 ? 32768 : v;
668                         vp[ j ] = dest[ i * channels_dest + j ] = ( int16_t )( v * A + vp[ j ] * B );
669                 }
670         }
671
672         return ret;
673 }
674
675 /* Will this break when mlt_position is converted to double? -Zach */
676 int mlt_sample_calculator( float fps, int frequency, int64_t position )
677 {
678         int samples = 0;
679
680         if ( ( int )( fps * 100 ) == 2997 )
681         {
682                 samples = frequency / 30;
683
684                 switch ( frequency )
685                 {
686                         case 48000:
687                                 if ( position % 5 != 0 )
688                                         samples += 2;
689                                 break;
690                         case 44100:
691                                 if ( position % 300 == 0 )
692                                         samples = 1471;
693                                 else if ( position % 30 == 0 )
694                                         samples = 1470;
695                                 else if ( position % 2 == 0 )
696                                         samples = 1472;
697                                 else
698                                         samples = 1471;
699                                 break;
700                         case 32000:
701                                 if ( position % 30 == 0 )
702                                         samples = 1068;
703                                 else if ( position % 29 == 0 )
704                                         samples = 1067;
705                                 else if ( position % 4 == 2 )
706                                         samples = 1067;
707                                 else
708                                         samples = 1068;
709                                 break;
710                         default:
711                                 samples = 0;
712                 }
713         }
714         else if ( fps != 0 )
715         {
716                 samples = frequency / fps;
717         }
718
719         return samples;
720 }
721
722 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
723 {
724         int64_t samples = 0;
725
726         // TODO: Correct rules for NTSC and drop the * 100 hack
727         if ( ( int )( fps * 100 ) == 2997 )
728         {
729                 samples = ( ( double )( frame * frequency ) / 30 );
730                 switch( frequency )
731                 {
732                         case 48000:
733                                 samples += 2 * ( frame / 5 );
734                                 break;
735                         case 44100:
736                                 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
737                                 break;
738                         case 32000:
739                                 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
740                                 break;
741                 }
742         }
743         else if ( fps != 0 )
744         {
745                 samples = ( ( frame * frequency ) / ( int )fps );
746         }
747
748         return samples;
749 }