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