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