]> git.sesse.net Git - mlt/blob - src/framework/mlt_frame.c
ce820b9a0e74c3df928c52153240c0bfafcd2c54
[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_properties_set( test_properties, "rescale.interp", "nearest" );
224                         mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
225                         mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
226                         mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
227                         mlt_properties_set_int( properties, "width", *width );
228                         mlt_properties_set_int( properties, "height", *height );
229                 }
230                 else
231                 {
232                         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
233                         mlt_frame_get_image( this, buffer, format, width, height, writable );
234                 }
235         }
236         else
237         {
238                 uint8_t *p;
239                 uint8_t *q;
240                 int size = 0;
241
242                 *width = *width == 0 ? 720 : *width;
243                 *height = *height == 0 ? 576 : *height;
244                 size = *width * *height;
245
246                 mlt_properties_set_int( properties, "width", *width );
247                 mlt_properties_set_int( properties, "height", *height );
248                 mlt_properties_set_int( properties, "aspect_ratio", 1 );
249
250                 switch( *format )
251                 {
252                         case mlt_image_none:
253                                 size = 0;
254                                 *buffer = NULL;
255                                 break;
256                         case mlt_image_rgb24:
257                                 size *= 3;
258                                 size += *width * 3;
259                                 *buffer = mlt_pool_alloc( size );
260                                 if ( *buffer )
261                                         memset( *buffer, 255, size );
262                                 break;
263                         case mlt_image_rgb24a:
264                                 size *= 4;
265                                 size += *width * 4;
266                                 *buffer = mlt_pool_alloc( size );
267                                 if ( *buffer )
268                                         memset( *buffer, 255, size );
269                                 break;
270                         case mlt_image_yuv422:
271                                 size *= 2;
272                                 size += *width * 2;
273                                 *buffer = mlt_pool_alloc( size );
274                                 p = *buffer;
275                                 q = p + size;
276                                 while ( p != NULL && p != q )
277                                 {
278                                         *p ++ = 235;
279                                         *p ++ = 128;
280                                 }
281                                 break;
282                         case mlt_image_yuv420p:
283                                 size = size * 3 / 2;
284                                 *buffer = mlt_pool_alloc( size );
285                                 if ( *buffer )
286                                         memset( *buffer, 255, size );
287                                 break;
288                 }
289
290                 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
291                 mlt_properties_set_int( properties, "test_image", 1 );
292         }
293
294         return 0;
295 }
296
297 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
298 {
299         if ( this->get_alpha_mask != NULL )
300                 return this->get_alpha_mask( this );
301         return NULL;
302 }
303
304 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
305 {
306         mlt_properties properties = mlt_frame_properties( this );
307
308         if ( this->get_audio != NULL )
309         {
310                 return this->get_audio( this, buffer, format, frequency, channels, samples );
311         }
312         else
313         {
314                 int size = 0;
315                 *samples = *samples <= 0 ? 1920 : *samples;
316                 *channels = *channels <= 0 ? 2 : *channels;
317                 *frequency = *frequency <= 0 ? 48000 : *frequency;
318                 size = *samples * *channels * sizeof( int16_t );
319                 *buffer = mlt_pool_alloc( size );
320                 if ( *buffer != NULL )
321                         memset( *buffer, 0, size );
322                 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
323                 mlt_properties_set_int( properties, "test_audio", 1 );
324         }
325         return 0;
326 }
327
328 void mlt_frame_close( mlt_frame this )
329 {
330         if ( this != NULL )
331         {
332                 mlt_deque_close( this->stack_image );
333                 mlt_deque_close( this->stack_audio );
334                 mlt_properties_close( &this->parent );
335                 free( this );
336         }
337 }
338
339 /***** convenience functions *****/
340
341 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
342 {
343         int ret = 0;
344         register int y0, y1, u0, u1, v0, v1;
345         register int r, g, b;
346         register uint8_t *d = yuv;
347         register int i, j;
348
349         for ( i = 0; i < height; i++ )
350         {
351                 register uint8_t *s = rgba + ( stride * i );
352                 for ( j = 0; j < ( width / 2 ); j++ )
353                 {
354                         r = *s++;
355                         g = *s++;
356                         b = *s++;
357                         *alpha++ = *s++;
358                         RGB2YUV (r, g, b, y0, u0 , v0);
359                         r = *s++;
360                         g = *s++;
361                         b = *s++;
362                         *alpha++ = *s++;
363                         RGB2YUV (r, g, b, y1, u1 , v1);
364                         *d++ = y0;
365                         *d++ = (u0+u1) >> 1;
366                         *d++ = y1;
367                         *d++ = (v0+v1) >> 1;
368                 }
369                 if ( width % 2 )
370                 {
371                         r = *s++;
372                         g = *s++;
373                         b = *s++;
374                         *alpha++ = *s++;
375                         RGB2YUV (r, g, b, y0, u0 , v0);
376                         *d++ = y0;
377                         *d++ = u0;
378                 }
379         }
380         return ret;
381 }
382
383 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
384 {
385         int ret = 0;
386         register int y0, y1, u0, u1, v0, v1;
387         register int r, g, b;
388         register uint8_t *d = yuv;
389         register int i, j;
390
391         for ( i = 0; i < height; i++ )
392         {
393                 register uint8_t *s = rgb + ( stride * i );
394                 for ( j = 0; j < ( width / 2 ); j++ )
395                 {
396                         r = *s++;
397                         g = *s++;
398                         b = *s++;
399                         RGB2YUV (r, g, b, y0, u0 , v0);
400                         r = *s++;
401                         g = *s++;
402                         b = *s++;
403                         RGB2YUV (r, g, b, y1, u1 , v1);
404                         *d++ = y0;
405                         *d++ = (u0+u1) >> 1;
406                         *d++ = y1;
407                         *d++ = (v0+v1) >> 1;
408                 }
409                 if ( width % 2 )
410                 {
411                         r = *s++;
412                         g = *s++;
413                         b = *s++;
414                         RGB2YUV (r, g, b, y0, u0 , v0);
415                         *d++ = y0;
416                         *d++ = u0;
417                 }
418         }
419         return ret;
420 }
421
422 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
423 {
424         int ret = 0;
425         register int i, j;
426
427         int half = width >> 1;
428
429         uint8_t *Y = yuv420p;
430         uint8_t *U = Y + width * height;
431         uint8_t *V = U + width * height / 4;
432
433         register uint8_t *d = yuv;
434
435         for ( i = 0; i < height; i++ )
436         {
437                 register uint8_t *u = U + ( i / 2 ) * ( half );
438                 register uint8_t *v = V + ( i / 2 ) * ( half );
439
440                 for ( j = 0; j < half; j++ )
441                 {
442                         *d ++ = *Y ++;
443                         *d ++ = *u ++;
444                         *d ++ = *Y ++;
445                         *d ++ = *v ++;
446                 }
447         }
448         return ret;
449 }
450
451 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
452 {
453         // Calculate strides
454         int istride = iwidth * 2;
455         int ostride = owidth * 2;
456
457         iwidth = iwidth - ( iwidth % 4 );
458         owidth = owidth - ( owidth % 4 );
459         iheight = iheight - ( iheight % 2 );
460         oheight = oheight - ( oheight % 2 );
461
462         // Optimisation point
463         if ( iwidth == owidth && iheight == oheight )
464                 memcpy( output, input, iheight * istride );
465
466         // Coordinates (0,0 is middle of output)
467         int y;
468
469         // Calculate ranges
470         int out_x_range = owidth / 2;
471         int out_y_range = oheight / 2;
472         int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
473         int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
474
475         // Output pointers
476         uint8_t *out_line = output;
477         uint8_t *out_ptr = out_line;
478
479         // Calculate a middle and possibly invalid pointer in the input
480         uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
481         int in_line = - in_y_range * istride - in_x_range * 2;
482
483         int elements;
484
485         // Fill whole section with black
486         y = out_y_range - ( iheight / 2 );
487         int blank_elements = ostride * y / 2;
488         elements = blank_elements;
489         while ( elements -- )
490         {
491                 *out_line ++ = 16;
492                 *out_line ++ = 128;
493         }
494
495         int active_width = 2 * iwidth;
496         int inactive_width = out_x_range - in_x_range;
497
498         // Loop for the entirety of our output height.
499         while ( iheight -- )
500         {
501         // Start at the beginning of the line
502         out_ptr = out_line;
503
504                 // Fill the outer part with black
505                 elements = inactive_width;
506                 while ( elements -- )
507                 {
508                         *out_ptr ++ = 16;
509                         *out_ptr ++ = 128;
510                 }
511
512                 // We're in the input range for this row.
513                 memcpy( out_ptr, in_middle + in_line, active_width );
514                 out_ptr += active_width;
515
516                 // Fill the outer part with black
517                 elements = inactive_width;
518                 while ( elements -- )
519                 {
520                         *out_ptr ++ = 16;
521                         *out_ptr ++ = 128;
522                 }
523
524                 // Move to next input line
525                 in_line += istride;
526
527         // Move to next output line
528         out_line += ostride;
529         }
530
531         // Fill whole section with black
532         elements = blank_elements;
533         while ( elements -- )
534         {
535                 *out_line ++ = 16;
536                 *out_line ++ = 128;
537         }
538 }
539
540 /** A resizing function for yuv422 frames - this does not rescale, but simply
541         resizes. It assumes yuv422 images available on the frame so use with care.
542 */
543
544 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
545 {
546         // Get properties
547         mlt_properties properties = mlt_frame_properties( this );
548
549         // Get the input image, width and height
550         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
551         int iwidth = mlt_properties_get_int( properties, "width" );
552         int iheight = mlt_properties_get_int( properties, "height" );
553
554         // If width and height are correct, don't do anything
555         if ( iwidth != owidth || iheight != oheight )
556         {
557                 // Create the output image
558                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
559
560                 // Call the generic resize
561                 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
562
563                 // Now update the frame
564                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
565                 mlt_properties_set_int( properties, "width", owidth );
566                 mlt_properties_set_int( properties, "height", oheight );
567
568                 // Return the output
569                 return output;
570         }
571         // No change, return input
572         return input;
573 }
574
575 /** A rescaling function for yuv422 frames - low quality, and provided for testing
576         only. It assumes yuv422 images available on the frame so use with care.
577 */
578
579 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
580 {
581         // Get properties
582         mlt_properties properties = mlt_frame_properties( this );
583
584         // Get the input image, width and height
585         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
586         int iwidth = mlt_properties_get_int( properties, "width" );
587         int iheight = mlt_properties_get_int( properties, "height" );
588
589         // If width and height are correct, don't do anything
590         if ( iwidth != owidth || iheight != oheight )
591         {
592                 // Create the output image
593                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
594
595                 // Calculate strides
596                 int istride = iwidth * 2;
597                 int ostride = owidth * 2;
598
599                 iwidth = iwidth - ( iwidth % 4 );
600
601                 // Derived coordinates
602                 int dy, dx;
603
604         // Calculate ranges
605         int out_x_range = owidth / 2;
606         int out_y_range = oheight / 2;
607         int in_x_range = iwidth / 2;
608         int in_y_range = iheight / 2;
609
610         // Output pointers
611         register uint8_t *out_line = output;
612         register uint8_t *out_ptr;
613
614         // Calculate a middle pointer
615         uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
616         uint8_t *in_line;
617
618                 // Generate the affine transform scaling values
619                 register int scale_width = ( iwidth << 16 ) / owidth;
620                 register int scale_height = ( iheight << 16 ) / oheight;
621                 register int base = 0;
622
623                 int outer = out_x_range * scale_width;
624                 int bottom = out_y_range * scale_height;
625
626         // Loop for the entirety of our output height.
627         for ( dy = - bottom; dy < bottom; dy += scale_height )
628         {
629                 // Start at the beginning of the line
630                 out_ptr = out_line;
631         
632                 // Pointer to the middle of the input line
633                 in_line = in_middle + ( dy >> 16 ) * istride;
634
635                 // Loop for the entirety of our output row.
636                 for ( dx = - outer; dx < outer; dx += scale_width )
637                 {
638                                 base = dx >> 15;
639                                 base &= 0xfffffffe;
640                                 *out_ptr ++ = *( in_line + base );
641                                 base &= 0xfffffffc;
642                                 *out_ptr ++ = *( in_line + base + 1 );
643                                 dx += scale_width;
644                                 base = dx >> 15;
645                                 base &= 0xfffffffe;
646                                 *out_ptr ++ = *( in_line + base );
647                                 base &= 0xfffffffc;
648                                 *out_ptr ++ = *( in_line + base + 3 );
649                 }
650
651                 // Move to next output line
652                 out_line += ostride;
653         }
654
655                 // Now update the frame
656                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
657                 mlt_properties_set_int( properties, "width", owidth );
658                 mlt_properties_set_int( properties, "height", oheight );
659
660                 // Return the output
661                 return output;
662         }
663
664         // No change, return input
665         return input;
666 }
667
668 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 )
669 {
670         int ret = 0;
671         int16_t *src, *dest;
672         int frequency_src = *frequency, frequency_dest = *frequency;
673         int channels_src = *channels, channels_dest = *channels;
674         int samples_src = *samples, samples_dest = *samples;
675         int i, j;
676         double d = 0, s = 0;
677
678         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
679         //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" ) );
680         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
681         //fprintf( stderr, "mix: frame src  samples %d channels %d\n", samples_src, channels_src );
682         
683         if ( channels_src > 6 )
684                 channels_src = 0;
685         if ( channels_dest > 6 )
686                 channels_dest = 0;
687         if ( samples_src > 4000 )
688                 samples_src = 0;
689         if ( samples_dest > 4000 )
690                 samples_dest = 0;
691
692         // determine number of samples to process
693         *samples = samples_src < samples_dest ? samples_src : samples_dest;
694         *channels = channels_src < channels_dest ? channels_src : channels_dest;
695         *buffer = dest;
696         *frequency = frequency_dest;
697
698         // Compute a smooth ramp over start to end
699         float weight = weight_start;
700         float weight_step = ( weight_end - weight_start ) / *samples;
701
702         // Mixdown
703         for ( i = 0; i < *samples; i++ )
704         {
705                 for ( j = 0; j < *channels; j++ )
706                 {
707                         if ( j < channels_dest )
708                                 d = (double) dest[ i * channels_dest + j ];
709                         if ( j < channels_src )
710                                 s = (double) src[ i * channels_src + j ];
711                         dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
712                 }
713                 weight += weight_step;
714         }
715
716         return ret;
717 }
718
719 int mlt_sample_calculator( float fps, int frequency, int64_t position )
720 {
721         int samples = 0;
722
723         if ( fps > 29 && fps <= 30 )
724         {
725                 samples = frequency / 30;
726
727                 switch ( frequency )
728                 {
729                         case 48000:
730                                 if ( position % 5 != 0 )
731                                         samples += 2;
732                                 break;
733                         case 44100:
734                                 if ( position % 300 == 0 )
735                                         samples = 1471;
736                                 else if ( position % 30 == 0 )
737                                         samples = 1470;
738                                 else if ( position % 2 == 0 )
739                                         samples = 1472;
740                                 else
741                                         samples = 1471;
742                                 break;
743                         case 32000:
744                                 if ( position % 30 == 0 )
745                                         samples = 1068;
746                                 else if ( position % 29 == 0 )
747                                         samples = 1067;
748                                 else if ( position % 4 == 2 )
749                                         samples = 1067;
750                                 else
751                                         samples = 1068;
752                                 break;
753                         default:
754                                 samples = 0;
755                 }
756         }
757         else if ( fps != 0 )
758         {
759                 samples = frequency / fps;
760         }
761
762         return samples;
763 }