]> git.sesse.net Git - mlt/blob - src/framework/mlt_frame.c
Audio read ahead and fine tuning
[mlt] / src / framework / mlt_frame.c
1 /*
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>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "config.h"
22 #include "mlt_frame.h"
23 #include "mlt_producer.h"
24 #include "mlt_factory.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /** Constructor for a frame.
30 */
31
32 mlt_frame mlt_frame_init( )
33 {
34         // Allocate a frame
35         mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
36
37         if ( this != NULL )
38         {
39                 // Get the normalisation
40                 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
41
42                 // Initialise the properties
43                 mlt_properties properties = &this->parent;
44                 mlt_properties_init( properties, this );
45
46                 // Set default properties on the frame
47                 mlt_properties_set_position( properties, "_position", 0.0 );
48                 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
49
50                 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
51                 {
52                         mlt_properties_set_int( properties, "width", 720 );
53                         mlt_properties_set_int( properties, "height", 576 );
54                         mlt_properties_set_int( properties, "normalised_width", 720 );
55                         mlt_properties_set_int( properties, "normalised_height", 576 );
56                         mlt_properties_set_double( properties, "aspect_ratio", 72.0/79.0 );
57                 }
58                 else
59                 {
60                         mlt_properties_set_int( properties, "width", 720 );
61                         mlt_properties_set_int( properties, "height", 480 );
62                         mlt_properties_set_int( properties, "normalised_width", 720 );
63                         mlt_properties_set_int( properties, "normalised_height", 480 );
64                         mlt_properties_set_double( properties, "aspect_ratio", 128.0/117.0 );
65                 }
66
67                 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
68                 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
69
70                 // Construct stacks for frames and methods
71                 this->stack_image = mlt_deque_init( );
72                 this->stack_audio = mlt_deque_init( );
73         }
74
75         return this;
76 }
77
78 /** Fetch the frames properties.
79 */
80
81 mlt_properties mlt_frame_properties( mlt_frame this )
82 {
83         return &this->parent;
84 }
85
86 /** Check if we have a way to derive something other than a test card.
87 */
88
89 int mlt_frame_is_test_card( mlt_frame this )
90 {
91         return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
92 }
93
94 /** Check if we have a way to derive something than test audio.
95 */
96
97 int mlt_frame_is_test_audio( mlt_frame this )
98 {
99         return this->get_audio == NULL || mlt_properties_get_int( mlt_frame_properties( this ), "test_audio" );
100 }
101
102 /** Get the aspect ratio of the frame.
103 */
104
105 double mlt_frame_get_aspect_ratio( mlt_frame this )
106 {
107         return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
108 }
109
110 /** Set the aspect ratio of the frame.
111 */
112
113 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
114 {
115         return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value );
116 }
117
118 /** Get the position of this frame.
119 */
120
121 mlt_position mlt_frame_get_position( mlt_frame this )
122 {
123         return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
124 }
125
126 /** Set the position of this frame.
127 */
128
129 int mlt_frame_set_position( mlt_frame this, mlt_position value )
130 {
131         return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value );
132 }
133
134 /** Stack a get_image callback.
135 */
136
137 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
138 {
139         return mlt_deque_push_back( this->stack_image, get_image );
140 }
141
142 /** Pop a get_image callback.
143 */
144
145 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
146 {
147         return mlt_deque_pop_back( this->stack_image );
148 }
149
150 /** Push a frame.
151 */
152
153 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
154 {
155         return mlt_deque_push_back( this->stack_image, that );
156 }
157
158 /** Pop a frame.
159 */
160
161 mlt_frame mlt_frame_pop_frame( mlt_frame this )
162 {
163         return mlt_deque_pop_back( this->stack_image );
164 }
165
166 /** Push a service.
167 */
168
169 int mlt_frame_push_service( mlt_frame this, void *that )
170 {
171         return mlt_deque_push_back( this->stack_image, that );
172 }
173
174 /** Pop a service.
175 */
176
177 void *mlt_frame_pop_service( mlt_frame this )
178 {
179         return mlt_deque_pop_back( this->stack_image );
180 }
181
182 /** Push an audio item on the stack.
183 */
184
185 int mlt_frame_push_audio( mlt_frame this, void *that )
186 {
187         return mlt_deque_push_back( this->stack_audio, that );
188 }
189
190 /** Pop an audio item from the stack
191 */
192
193 void *mlt_frame_pop_audio( mlt_frame this )
194 {
195         return mlt_deque_pop_back( this->stack_audio );
196 }
197
198 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
199 {
200         mlt_properties properties = mlt_frame_properties( this );
201         mlt_get_image get_image = mlt_frame_pop_get_image( this );
202         mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
203         
204         if ( get_image != NULL )
205         {
206                 return get_image( this, buffer, format, width, height, writable );
207         }
208         else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
209         {
210                 *format = mlt_image_yuv422;
211                 *buffer = mlt_properties_get_data( properties, "image", NULL );
212                 *width = mlt_properties_get_int( properties, "width" );
213                 *height = mlt_properties_get_int( properties, "height" );
214         }
215         else if ( producer != NULL )
216         {
217                 mlt_frame test_frame = NULL;
218                 mlt_service_get_frame( mlt_producer_service( producer ), &test_frame, 0 );
219                 if ( test_frame != NULL )
220                 {
221                         mlt_properties test_properties = mlt_frame_properties( test_frame );
222                         mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
223                         mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
224                         mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
225                         mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
226                         mlt_properties_set_int( properties, "width", *width );
227                         mlt_properties_set_int( properties, "height", *height );
228                         mlt_properties_set( properties, "rescale.interp", "none" );
229                         mlt_properties_set( properties, "scale", "off" );
230                 }
231                 else
232                 {
233                         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
234                         mlt_frame_get_image( this, buffer, format, width, height, writable );
235                 }
236         }
237         else
238         {
239                 register uint8_t *p;
240                 register uint8_t *q;
241                 int size = 0;
242
243                 *width = *width == 0 ? 720 : *width;
244                 *height = *height == 0 ? 576 : *height;
245                 size = *width * *height;
246
247                 mlt_properties_set_int( properties, "width", *width );
248                 mlt_properties_set_int( properties, "height", *height );
249                 mlt_properties_set_int( properties, "aspect_ratio", 1 );
250
251                 switch( *format )
252                 {
253                         case mlt_image_none:
254                                 size = 0;
255                                 *buffer = NULL;
256                                 break;
257                         case mlt_image_rgb24:
258                                 size *= 3;
259                                 size += *width * 3;
260                                 *buffer = mlt_pool_alloc( size );
261                                 if ( *buffer )
262                                         memset( *buffer, 255, size );
263                                 break;
264                         case mlt_image_rgb24a:
265                                 size *= 4;
266                                 size += *width * 4;
267                                 *buffer = mlt_pool_alloc( size );
268                                 if ( *buffer )
269                                         memset( *buffer, 255, size );
270                                 break;
271                         case mlt_image_yuv422:
272                                 size *= 2;
273                                 size += *width * 2;
274                                 *buffer = mlt_pool_alloc( size );
275                                 p = *buffer;
276                                 q = p + size;
277                                 while ( p != NULL && p != q )
278                                 {
279                                         *p ++ = 235;
280                                         *p ++ = 128;
281                                 }
282                                 break;
283                         case mlt_image_yuv420p:
284                                 size = size * 3 / 2;
285                                 *buffer = mlt_pool_alloc( size );
286                                 if ( *buffer )
287                                         memset( *buffer, 255, size );
288                                 break;
289                 }
290
291                 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
292                 mlt_properties_set_int( properties, "test_image", 1 );
293         }
294
295         return 0;
296 }
297
298 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
299 {
300         if ( this->get_alpha_mask != NULL )
301                 return this->get_alpha_mask( this );
302         return NULL;
303 }
304
305 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
306 {
307         mlt_properties properties = mlt_frame_properties( this );
308
309         if ( this->get_audio != NULL )
310         {
311                 this->get_audio( this, buffer, format, frequency, channels, samples );
312         }
313         else if ( mlt_properties_get_data( properties, "audio", NULL ) )
314         {
315                 *buffer = mlt_properties_get_data( properties, "audio", NULL );
316                 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
317                 *channels = mlt_properties_get_int( properties, "audio_channels" );
318                 *samples = mlt_properties_get_int( properties, "audio_samples" );
319         }
320         else
321         {
322                 int size = 0;
323                 *samples = *samples <= 0 ? 1920 : *samples;
324                 *channels = *channels <= 0 ? 2 : *channels;
325                 *frequency = *frequency <= 0 ? 48000 : *frequency;
326                 size = *samples * *channels * sizeof( int16_t );
327                 *buffer = mlt_pool_alloc( size );
328                 if ( *buffer != NULL )
329                         memset( *buffer, 0, size );
330                 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
331                 mlt_properties_set_int( properties, "test_audio", 1 );
332         }
333
334         mlt_properties_set_int( properties, "audio_frequency", *frequency );
335         mlt_properties_set_int( properties, "audio_channels", *channels );
336         mlt_properties_set_int( properties, "audio_samples", *samples );
337
338         return 0;
339 }
340
341 void mlt_frame_close( mlt_frame this )
342 {
343         if ( this != NULL )
344         {
345                 mlt_deque_close( this->stack_image );
346                 mlt_deque_close( this->stack_audio );
347                 mlt_properties_close( &this->parent );
348                 free( this );
349         }
350 }
351
352 /***** convenience functions *****/
353
354 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
355 {
356         int ret = 0;
357         register int y0, y1, u0, u1, v0, v1;
358         register int r, g, b;
359         register uint8_t *d = yuv;
360         register int i, j;
361
362         for ( i = 0; i < height; i++ )
363         {
364                 register uint8_t *s = rgba + ( stride * i );
365                 for ( j = 0; j < ( width / 2 ); j++ )
366                 {
367                         r = *s++;
368                         g = *s++;
369                         b = *s++;
370                         *alpha++ = *s++;
371                         RGB2YUV (r, g, b, y0, u0 , v0);
372                         r = *s++;
373                         g = *s++;
374                         b = *s++;
375                         *alpha++ = *s++;
376                         RGB2YUV (r, g, b, y1, u1 , v1);
377                         *d++ = y0;
378                         *d++ = (u0+u1) >> 1;
379                         *d++ = y1;
380                         *d++ = (v0+v1) >> 1;
381                 }
382                 if ( width % 2 )
383                 {
384                         r = *s++;
385                         g = *s++;
386                         b = *s++;
387                         *alpha++ = *s++;
388                         RGB2YUV (r, g, b, y0, u0 , v0);
389                         *d++ = y0;
390                         *d++ = u0;
391                 }
392         }
393         return ret;
394 }
395
396 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
397 {
398         int ret = 0;
399         register int y0, y1, u0, u1, v0, v1;
400         register int r, g, b;
401         register uint8_t *d = yuv;
402         register int i, j;
403
404         for ( i = 0; i < height; i++ )
405         {
406                 register uint8_t *s = rgb + ( stride * i );
407                 for ( j = 0; j < ( width / 2 ); j++ )
408                 {
409                         r = *s++;
410                         g = *s++;
411                         b = *s++;
412                         RGB2YUV (r, g, b, y0, u0 , v0);
413                         r = *s++;
414                         g = *s++;
415                         b = *s++;
416                         RGB2YUV (r, g, b, y1, u1 , v1);
417                         *d++ = y0;
418                         *d++ = (u0+u1) >> 1;
419                         *d++ = y1;
420                         *d++ = (v0+v1) >> 1;
421                 }
422                 if ( width % 2 )
423                 {
424                         r = *s++;
425                         g = *s++;
426                         b = *s++;
427                         RGB2YUV (r, g, b, y0, u0 , v0);
428                         *d++ = y0;
429                         *d++ = u0;
430                 }
431         }
432         return ret;
433 }
434
435 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
436 {
437         int ret = 0;
438         register int i, j;
439
440         int half = width >> 1;
441
442         uint8_t *Y = yuv420p;
443         uint8_t *U = Y + width * height;
444         uint8_t *V = U + width * height / 4;
445
446         register uint8_t *d = yuv;
447
448         for ( i = 0; i < height; i++ )
449         {
450                 register uint8_t *u = U + ( i / 2 ) * ( half );
451                 register uint8_t *v = V + ( i / 2 ) * ( half );
452
453                 for ( j = 0; j < half; j++ )
454                 {
455                         *d ++ = *Y ++;
456                         *d ++ = *u ++;
457                         *d ++ = *Y ++;
458                         *d ++ = *v ++;
459                 }
460         }
461         return ret;
462 }
463
464 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
465 {
466         // Calculate strides
467         int istride = iwidth * 2;
468         int ostride = owidth * 2;
469
470         iwidth = iwidth - ( iwidth % 4 );
471         owidth = owidth - ( owidth % 4 );
472         iheight = iheight - ( iheight % 2 );
473         oheight = oheight - ( oheight % 2 );
474
475         // Optimisation point
476         if ( iwidth == owidth && iheight == oheight )
477                 memcpy( output, input, iheight * istride );
478
479         // Coordinates (0,0 is middle of output)
480         int y;
481
482         // Calculate ranges
483         int out_x_range = owidth / 2;
484         int out_y_range = oheight / 2;
485         int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
486         int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
487
488         // Output pointers
489         uint8_t *out_line = output;
490         uint8_t *out_ptr = out_line;
491
492         // Calculate a middle and possibly invalid pointer in the input
493         uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
494         int in_line = - in_y_range * istride - in_x_range * 2;
495
496         int elements;
497
498         // Fill whole section with black
499         y = out_y_range - ( iheight / 2 );
500         int blank_elements = ostride * y / 2;
501         elements = blank_elements;
502         while ( elements -- )
503         {
504                 *out_line ++ = 16;
505                 *out_line ++ = 128;
506         }
507
508         int active_width = 2 * iwidth;
509         int inactive_width = out_x_range - in_x_range;
510         uint8_t *p = NULL;
511         uint8_t *end = NULL;
512
513         // Loop for the entirety of our output height.
514         while ( iheight -- )
515         {
516         // Start at the beginning of the line
517         out_ptr = out_line;
518
519                 // Fill the outer part with black
520                 elements = inactive_width;
521                 while ( elements -- )
522                 {
523                         *out_ptr ++ = 16;
524                         *out_ptr ++ = 128;
525                 }
526
527                 // We're in the input range for this row.
528                 p = in_middle + in_line;
529                 end = out_ptr + active_width;
530                 while ( out_ptr != end )
531                 {
532                         *out_ptr ++ = *p ++;
533                         *out_ptr ++ = *p ++;
534                 }
535
536                 // Fill the outer part with black
537                 elements = inactive_width;
538                 while ( elements -- )
539                 {
540                         *out_ptr ++ = 16;
541                         *out_ptr ++ = 128;
542                 }
543
544                 // Move to next input line
545                 in_line += istride;
546
547         // Move to next output line
548         out_line += ostride;
549         }
550
551         // Fill whole section with black
552         elements = blank_elements;
553         while ( elements -- )
554         {
555                 *out_line ++ = 16;
556                 *out_line ++ = 128;
557         }
558 }
559
560 /** A resizing function for yuv422 frames - this does not rescale, but simply
561         resizes. It assumes yuv422 images available on the frame so use with care.
562 */
563
564 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
565 {
566         // Get properties
567         mlt_properties properties = mlt_frame_properties( this );
568
569         // Get the input image, width and height
570         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
571         int iwidth = mlt_properties_get_int( properties, "width" );
572         int iheight = mlt_properties_get_int( properties, "height" );
573
574         // If width and height are correct, don't do anything
575         if ( iwidth != owidth || iheight != oheight )
576         {
577                 // Create the output image
578                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
579
580                 // Call the generic resize
581                 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
582
583                 // Now update the frame
584                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
585                 mlt_properties_set_int( properties, "width", owidth );
586                 mlt_properties_set_int( properties, "height", oheight );
587
588                 // Return the output
589                 return output;
590         }
591         // No change, return input
592         return input;
593 }
594
595 /** A rescaling function for yuv422 frames - low quality, and provided for testing
596         only. It assumes yuv422 images available on the frame so use with care.
597 */
598
599 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
600 {
601         // Get properties
602         mlt_properties properties = mlt_frame_properties( this );
603
604         // Get the input image, width and height
605         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
606         int iwidth = mlt_properties_get_int( properties, "width" );
607         int iheight = mlt_properties_get_int( properties, "height" );
608
609         // If width and height are correct, don't do anything
610         if ( iwidth != owidth || iheight != oheight )
611         {
612                 // Create the output image
613                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
614
615                 // Calculate strides
616                 int istride = iwidth * 2;
617                 int ostride = owidth * 2;
618
619                 iwidth = iwidth - ( iwidth % 4 );
620
621                 // Derived coordinates
622                 int dy, dx;
623
624         // Calculate ranges
625         int out_x_range = owidth / 2;
626         int out_y_range = oheight / 2;
627         int in_x_range = iwidth / 2;
628         int in_y_range = iheight / 2;
629
630         // Output pointers
631         register uint8_t *out_line = output;
632         register uint8_t *out_ptr;
633
634         // Calculate a middle pointer
635         uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
636         uint8_t *in_line;
637
638                 // Generate the affine transform scaling values
639                 register int scale_width = ( iwidth << 16 ) / owidth;
640                 register int scale_height = ( iheight << 16 ) / oheight;
641                 register int base = 0;
642
643                 int outer = out_x_range * scale_width;
644                 int bottom = out_y_range * scale_height;
645
646         // Loop for the entirety of our output height.
647         for ( dy = - bottom; dy < bottom; dy += scale_height )
648         {
649                 // Start at the beginning of the line
650                 out_ptr = out_line;
651         
652                 // Pointer to the middle of the input line
653                 in_line = in_middle + ( dy >> 16 ) * istride;
654
655                 // Loop for the entirety of our output row.
656                 for ( dx = - outer; dx < outer; dx += scale_width )
657                 {
658                                 base = dx >> 15;
659                                 base &= 0xfffffffe;
660                                 *out_ptr ++ = *( in_line + base );
661                                 base &= 0xfffffffc;
662                                 *out_ptr ++ = *( in_line + base + 1 );
663                                 dx += scale_width;
664                                 base = dx >> 15;
665                                 base &= 0xfffffffe;
666                                 *out_ptr ++ = *( in_line + base );
667                                 base &= 0xfffffffc;
668                                 *out_ptr ++ = *( in_line + base + 3 );
669                 }
670
671                 // Move to next output line
672                 out_line += ostride;
673         }
674
675                 // Now update the frame
676                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
677                 mlt_properties_set_int( properties, "width", owidth );
678                 mlt_properties_set_int( properties, "height", oheight );
679
680                 // Return the output
681                 return output;
682         }
683
684         // No change, return input
685         return input;
686 }
687
688 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 )
689 {
690         int ret = 0;
691         int16_t *src, *dest;
692         int frequency_src = *frequency, frequency_dest = *frequency;
693         int channels_src = *channels, channels_dest = *channels;
694         int samples_src = *samples, samples_dest = *samples;
695         int i, j;
696         double d = 0, s = 0;
697
698         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
699         //fprintf( stderr, "mix: frame dest samples %d channels %d position %lld\n", samples_dest, channels_dest, mlt_properties_get_position( mlt_frame_properties( this ), "_position" ) );
700         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
701         //fprintf( stderr, "mix: frame src  samples %d channels %d\n", samples_src, channels_src );
702         
703         if ( channels_src > 6 )
704                 channels_src = 0;
705         if ( channels_dest > 6 )
706                 channels_dest = 0;
707         if ( samples_src > 4000 )
708                 samples_src = 0;
709         if ( samples_dest > 4000 )
710                 samples_dest = 0;
711
712         // determine number of samples to process
713         *samples = samples_src < samples_dest ? samples_src : samples_dest;
714         *channels = channels_src < channels_dest ? channels_src : channels_dest;
715         *buffer = dest;
716         *frequency = frequency_dest;
717
718         // Compute a smooth ramp over start to end
719         float weight = weight_start;
720         float weight_step = ( weight_end - weight_start ) / *samples;
721
722         // Mixdown
723         for ( i = 0; i < *samples; i++ )
724         {
725                 for ( j = 0; j < *channels; j++ )
726                 {
727                         if ( j < channels_dest )
728                                 d = (double) dest[ i * channels_dest + j ];
729                         if ( j < channels_src )
730                                 s = (double) src[ i * channels_src + j ];
731                         dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
732                 }
733                 weight += weight_step;
734         }
735
736         return ret;
737 }
738
739 int mlt_sample_calculator( float fps, int frequency, int64_t position )
740 {
741         int samples = 0;
742
743         if ( ( int )( fps * 100 ) == 2997 )
744         {
745                 samples = frequency / 30;
746
747                 switch ( frequency )
748                 {
749                         case 48000:
750                                 if ( position % 5 != 0 )
751                                         samples += 2;
752                                 break;
753                         case 44100:
754                                 if ( position % 300 == 0 )
755                                         samples = 1471;
756                                 else if ( position % 30 == 0 )
757                                         samples = 1470;
758                                 else if ( position % 2 == 0 )
759                                         samples = 1472;
760                                 else
761                                         samples = 1471;
762                                 break;
763                         case 32000:
764                                 if ( position % 30 == 0 )
765                                         samples = 1068;
766                                 else if ( position % 29 == 0 )
767                                         samples = 1067;
768                                 else if ( position % 4 == 2 )
769                                         samples = 1067;
770                                 else
771                                         samples = 1068;
772                                 break;
773                         default:
774                                 samples = 0;
775                 }
776         }
777         else if ( fps != 0 )
778         {
779                 samples = frequency / fps;
780         }
781
782         return samples;
783 }