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