]> git.sesse.net Git - mlt/blob - src/framework/mlt_frame.c
64 bit fix and deque int holding
[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", 59.0/54.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", 10.0/11.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                 this->stack_service = mlt_deque_init( );
74         }
75
76         return this;
77 }
78
79 /** Fetch the frames properties.
80 */
81
82 mlt_properties mlt_frame_properties( mlt_frame this )
83 {
84         return this != NULL ? &this->parent : NULL;
85 }
86
87 /** Check if we have a way to derive something other than a test card.
88 */
89
90 int mlt_frame_is_test_card( mlt_frame this )
91 {
92         return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
93 }
94
95 /** Check if we have a way to derive something other than test audio.
96 */
97
98 int mlt_frame_is_test_audio( mlt_frame this )
99 {
100         return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
101 }
102
103 /** Get the aspect ratio of the frame.
104 */
105
106 double mlt_frame_get_aspect_ratio( mlt_frame this )
107 {
108         return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
109 }
110
111 /** Set the aspect ratio of the frame.
112 */
113
114 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
115 {
116         return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
117 }
118
119 /** Get the position of this frame.
120 */
121
122 mlt_position mlt_frame_get_position( mlt_frame this )
123 {
124         int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
125         return pos < 0 ? 0 : pos;
126 }
127
128 /** Set the position of this frame.
129 */
130
131 int mlt_frame_set_position( mlt_frame this, mlt_position value )
132 {
133         return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
134 }
135
136 /** Stack a get_image callback.
137 */
138
139 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
140 {
141         return mlt_deque_push_back( this->stack_image, get_image );
142 }
143
144 /** Pop a get_image callback.
145 */
146
147 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
148 {
149         return mlt_deque_pop_back( this->stack_image );
150 }
151
152 /** Push a frame.
153 */
154
155 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
156 {
157         return mlt_deque_push_back( this->stack_image, that );
158 }
159
160 /** Pop a frame.
161 */
162
163 mlt_frame mlt_frame_pop_frame( mlt_frame this )
164 {
165         return mlt_deque_pop_back( this->stack_image );
166 }
167
168 /** Push a service.
169 */
170
171 int mlt_frame_push_service( mlt_frame this, void *that )
172 {
173         return mlt_deque_push_back( this->stack_image, that );
174 }
175
176 /** Pop a service.
177 */
178
179 void *mlt_frame_pop_service( mlt_frame this )
180 {
181         return mlt_deque_pop_back( this->stack_image );
182 }
183
184 /** Push a service.
185 */
186
187 int mlt_frame_push_service_int( mlt_frame this, int that )
188 {
189         return mlt_deque_push_back_int( this->stack_image, that );
190 }
191
192 /** Pop a service.
193 */
194
195 int mlt_frame_pop_service_int( mlt_frame this )
196 {
197         return mlt_deque_pop_back_int( this->stack_image );
198 }
199
200 /** Push an audio item on the stack.
201 */
202
203 int mlt_frame_push_audio( mlt_frame this, void *that )
204 {
205         return mlt_deque_push_back( this->stack_audio, that );
206 }
207
208 /** Pop an audio item from the stack
209 */
210
211 void *mlt_frame_pop_audio( mlt_frame this )
212 {
213         return mlt_deque_pop_back( this->stack_audio );
214 }
215
216 /** Return the service stack
217 */
218
219 mlt_deque mlt_frame_service_stack( mlt_frame this )
220 {
221         return this->stack_service;
222 }
223
224 /** [EXPERIMENTAL] Replace image stack with the information provided.
225
226         This might prove to be unreliable and restrictive - the idea is that a transition
227         which normally uses two images may decide to only use the b frame (ie: in the case
228         of a composite where the b frame completely obscures the a frame).
229
230         The image must be writable and the destructor for the image itself must be taken
231         care of on another frame and that frame cannot have a replace applied to it... 
232         Further it assumes that no alpha mask is in use.
233
234         For these reasons, it can only be used in a specific situation - when you have 
235         multiple tracks each with their own transition and these transitions are applied
236         in a strictly reversed order (ie: highest numbered [lowest track] is processed 
237         first).
238
239         More reliable approach - the cases should be detected during the process phase
240         and the upper tracks should simply not be invited to stack...
241 */
242
243 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
244 {
245         // Herein lies the potential problem for this function - it makes a potentially 
246         // dangerous assumption that all content on the image stack can be removed without a destructor
247         while( mlt_deque_pop_back( this->stack_image ) ) ;
248
249         // Update the information 
250         mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
251         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
252         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
253         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
254         this->get_alpha_mask = NULL;
255 }
256
257 /** Get the image associated to the frame.
258 */
259
260 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
261 {
262         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
263         mlt_get_image get_image = mlt_frame_pop_get_image( this );
264         mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
265
266         *width = *width >> 1 << 1;
267         
268         if ( get_image != NULL )
269         {
270                 int error = 0;
271                 mlt_position position = mlt_frame_get_position( this );
272                 error = get_image( this, buffer, format, width, height, writable );
273                 mlt_frame_set_position( this, position );
274                 return error;
275         }
276         else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
277         {
278                 *format = mlt_image_yuv422;
279                 *buffer = mlt_properties_get_data( properties, "image", NULL );
280                 *width = mlt_properties_get_int( properties, "width" );
281                 *height = mlt_properties_get_int( properties, "height" );
282         }
283         else if ( producer != NULL )
284         {
285                 mlt_frame test_frame = NULL;
286                 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
287                 if ( test_frame != NULL )
288                 {
289                         mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
290                         mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
291                         mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
292                         mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
293                         mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
294                         mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
295                         mlt_properties_set_int( properties, "width", *width );
296                         mlt_properties_set_int( properties, "height", *height );
297                         mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
298                 }
299                 else
300                 {
301                         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
302                         mlt_frame_get_image( this, buffer, format, width, height, writable );
303                 }
304         }
305         else
306         {
307                 register uint8_t *p;
308                 register uint8_t *q;
309                 int size = 0;
310
311                 *width = *width == 0 ? 720 : *width;
312                 *height = *height == 0 ? 576 : *height;
313                 size = *width * *height;
314
315                 mlt_properties_set_int( properties, "width", *width );
316                 mlt_properties_set_int( properties, "height", *height );
317                 mlt_properties_set_int( properties, "aspect_ratio", 1 );
318
319                 switch( *format )
320                 {
321                         case mlt_image_none:
322                                 size = 0;
323                                 *buffer = NULL;
324                                 break;
325                         case mlt_image_rgb24:
326                                 size *= 3;
327                                 size += *width * 3;
328                                 *buffer = mlt_pool_alloc( size );
329                                 if ( *buffer )
330                                         memset( *buffer, 255, size );
331                                 break;
332                         case mlt_image_rgb24a:
333                                 size *= 4;
334                                 size += *width * 4;
335                                 *buffer = mlt_pool_alloc( size );
336                                 if ( *buffer )
337                                         memset( *buffer, 255, size );
338                                 break;
339                         case mlt_image_yuv422:
340                                 size *= 2;
341                                 size += *width * 2;
342                                 *buffer = mlt_pool_alloc( size );
343                                 p = *buffer;
344                                 q = p + size;
345                                 while ( p != NULL && p != q )
346                                 {
347                                         *p ++ = 235;
348                                         *p ++ = 128;
349                                 }
350                                 break;
351                         case mlt_image_yuv420p:
352                                 size = size * 3 / 2;
353                                 *buffer = mlt_pool_alloc( size );
354                                 if ( *buffer )
355                                         memset( *buffer, 255, size );
356                                 break;
357                 }
358
359                 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
360                 mlt_properties_set_int( properties, "test_image", 1 );
361         }
362
363         return 0;
364 }
365
366 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
367 {
368         if ( this != NULL && this->get_alpha_mask != NULL )
369                 return this->get_alpha_mask( this );
370         return this == NULL ? NULL : mlt_properties_get_data( &this->parent, "alpha", NULL );
371 }
372
373 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
374 {
375         mlt_get_audio get_audio = mlt_frame_pop_audio( this );
376         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
377         int hide = mlt_properties_get_int( properties, "test_audio" );
378
379         if ( hide == 0 && get_audio != NULL )
380         {
381                 mlt_position position = mlt_frame_get_position( this );
382                 get_audio( this, buffer, format, frequency, channels, samples );
383                 mlt_frame_set_position( this, position );
384         }
385         else if ( mlt_properties_get_data( properties, "audio", NULL ) )
386         {
387                 *buffer = mlt_properties_get_data( properties, "audio", NULL );
388                 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
389                 *channels = mlt_properties_get_int( properties, "audio_channels" );
390                 *samples = mlt_properties_get_int( properties, "audio_samples" );
391         }
392         else
393         {
394                 int size = 0;
395                 *samples = *samples <= 0 ? 1920 : *samples;
396                 *channels = *channels <= 0 ? 2 : *channels;
397                 *frequency = *frequency <= 0 ? 48000 : *frequency;
398                 size = *samples * *channels * sizeof( int16_t );
399                 *buffer = mlt_pool_alloc( size );
400                 if ( *buffer != NULL )
401                         memset( *buffer, 0, size );
402                 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
403                 mlt_properties_set_int( properties, "test_audio", 1 );
404         }
405
406         mlt_properties_set_int( properties, "audio_frequency", *frequency );
407         mlt_properties_set_int( properties, "audio_channels", *channels );
408         mlt_properties_set_int( properties, "audio_samples", *samples );
409
410         if ( mlt_properties_get( properties, "meta.volume" ) )
411         {
412                 double value = mlt_properties_get_double( properties, "meta.volume" );
413                 if ( value == 0.0 )
414                 {
415                         memset( *buffer, 0, *samples * *channels * 2 );
416                         mlt_properties_set_double( properties, "meta.volume", 1.0 );
417                 }
418                 else if ( value != 1.0 )
419                 {
420                         int total = *samples * *channels;
421                         int16_t *p = *buffer;
422                         while ( total -- )
423                         {
424                                 *p = *p * value;
425                                 p ++;
426                         }
427                         mlt_properties_set_double( properties, "meta.volume", 1.0 );
428                 }
429         }
430
431         return 0;
432 }
433
434 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
435 {
436         int16_t *pcm = NULL;
437         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
438         mlt_audio_format format = mlt_audio_pcm;
439         int frequency = 32000; // lower frequency available?
440         int channels = 2;
441         double fps = mlt_properties_get_double( properties, "fps" );
442         int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
443         
444         // Get the pcm data
445         mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
446         
447         // Make an 8-bit buffer large enough to hold rendering
448         int size = w * h;
449         unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
450         if ( bitmap != NULL )
451                 memset( bitmap, 0, size );
452         mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
453         
454         // Render vertical lines
455         int16_t *ubound = pcm + samples * channels;
456         int skip = samples / w - 1;
457         int i, j, k;
458         
459         // Iterate sample stream and along x coordinate
460         for ( i = 0; i < w && pcm < ubound; i++ )
461         {
462                 // pcm data has channels interleaved
463                 for ( j = 0; j < channels; j++ )
464                 {
465                         // Determine sample's magnitude from 2s complement;
466                         int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
467                         // The height of a line is the ratio of the magnitude multiplied by 
468                         // half the vertical resolution
469                         int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
470                         // Determine the starting y coordinate - left channel above center,
471                         // right channel below - currently assumes 2 channels
472                         int displacement = ( h / 2 ) - ( 1 - j ) * height;
473                         // Position buffer pointer using y coordinate, stride, and x coordinate
474                         unsigned char *p = &bitmap[ i + displacement * w ];
475                         
476                         // Draw vertical line
477                         for ( k = 0; k < height; k++ )
478                                 p[ w * k ] = 0xFF;
479                         
480                         pcm++;
481                 }
482                 pcm += skip * channels;
483         }
484
485         return bitmap;
486 }
487
488 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
489 {
490         if ( this != NULL )
491                 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
492         return NULL;
493 }
494
495 void mlt_frame_close( mlt_frame this )
496 {
497         if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
498         {
499                 mlt_deque_close( this->stack_image );
500                 mlt_deque_close( this->stack_audio );
501                 while( mlt_deque_peek_back( this->stack_service ) )
502                         mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
503                 mlt_deque_close( this->stack_service );
504                 mlt_properties_close( &this->parent );
505                 free( this );
506         }
507 }
508
509 /***** convenience functions *****/
510
511 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
512 {
513         int ret = 0;
514         register int y0, y1, u0, u1, v0, v1;
515         register int r, g, b;
516         register uint8_t *d = yuv;
517         register int i, j;
518
519         for ( i = 0; i < height; i++ )
520         {
521                 register uint8_t *s = rgba + ( stride * i );
522                 for ( j = 0; j < ( width / 2 ); j++ )
523                 {
524                         r = *s++;
525                         g = *s++;
526                         b = *s++;
527                         *alpha++ = *s++;
528                         RGB2YUV (r, g, b, y0, u0 , v0);
529                         r = *s++;
530                         g = *s++;
531                         b = *s++;
532                         *alpha++ = *s++;
533                         RGB2YUV (r, g, b, y1, u1 , v1);
534                         *d++ = y0;
535                         *d++ = (u0+u1) >> 1;
536                         *d++ = y1;
537                         *d++ = (v0+v1) >> 1;
538                 }
539                 if ( width % 2 )
540                 {
541                         r = *s++;
542                         g = *s++;
543                         b = *s++;
544                         *alpha++ = *s++;
545                         RGB2YUV (r, g, b, y0, u0 , v0);
546                         *d++ = y0;
547                         *d++ = u0;
548                 }
549         }
550         return ret;
551 }
552
553 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
554 {
555         int ret = 0;
556         register int y0, y1, u0, u1, v0, v1;
557         register int r, g, b;
558         register uint8_t *d = yuv;
559         register int i, j;
560
561         for ( i = 0; i < height; i++ )
562         {
563                 register uint8_t *s = rgb + ( stride * i );
564                 for ( j = 0; j < ( width / 2 ); j++ )
565                 {
566                         r = *s++;
567                         g = *s++;
568                         b = *s++;
569                         RGB2YUV (r, g, b, y0, u0 , v0);
570                         r = *s++;
571                         g = *s++;
572                         b = *s++;
573                         RGB2YUV (r, g, b, y1, u1 , v1);
574                         *d++ = y0;
575                         *d++ = (u0+u1) >> 1;
576                         *d++ = y1;
577                         *d++ = (v0+v1) >> 1;
578                 }
579                 if ( width % 2 )
580                 {
581                         r = *s++;
582                         g = *s++;
583                         b = *s++;
584                         RGB2YUV (r, g, b, y0, u0 , v0);
585                         *d++ = y0;
586                         *d++ = u0;
587                 }
588         }
589         return ret;
590 }
591
592 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
593 {
594         int ret = 0;
595         register int i, j;
596
597         int half = width >> 1;
598
599         uint8_t *Y = yuv420p;
600         uint8_t *U = Y + width * height;
601         uint8_t *V = U + width * height / 4;
602
603         register uint8_t *d = yuv;
604
605         for ( i = 0; i < height; i++ )
606         {
607                 register uint8_t *u = U + ( i / 2 ) * ( half );
608                 register uint8_t *v = V + ( i / 2 ) * ( half );
609
610                 for ( j = 0; j < half; j++ )
611                 {
612                         *d ++ = *Y ++;
613                         *d ++ = *u ++;
614                         *d ++ = *Y ++;
615                         *d ++ = *v ++;
616                 }
617         }
618         return ret;
619 }
620
621 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight )
622 {
623         uint8_t *output = NULL;
624
625         if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
626         {
627                 iwidth = iwidth - ( iwidth % 2 );
628                 owidth = owidth - ( owidth % 2 );
629
630                 output = mlt_pool_alloc( owidth * oheight );
631
632                 // Coordinates (0,0 is middle of output)
633                 int y;
634
635                 // Calculate ranges
636                 int out_x_range = owidth / 2;
637                 int out_y_range = oheight / 2;
638                 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
639                 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
640
641                 // Output pointers
642                 uint8_t *out_line = output;
643                 uint8_t *out_ptr = out_line;
644
645                 // Calculate a middle and possibly invalid pointer in the input
646                 uint8_t *in_middle = input + iwidth * ( iheight / 2 ) + ( iwidth / 2 );
647                 int in_line = - in_y_range * iwidth - in_x_range;
648
649                 int elements;
650
651                 // Fill whole section with black
652                 y = out_y_range - ( iheight / 2 );
653                 int blank_elements = owidth * y;
654                 elements = blank_elements;
655                 while ( elements -- )
656                         *out_line ++ = 0;
657
658                 int active_width = iwidth;
659                 int inactive_width = out_x_range - in_x_range;
660                 uint8_t *p = NULL;
661                 uint8_t *end = NULL;
662
663                 // Loop for the entirety of our output height.
664                 while ( iheight -- )
665                 {
666                 // Start at the beginning of the line
667                 out_ptr = out_line;
668
669                         // Fill the outer part with black
670                         elements = inactive_width;
671                         while ( elements -- )
672                                 *out_ptr ++ = 0;
673
674                         // We're in the input range for this row.
675                         p = in_middle + in_line;
676                         end = out_ptr + active_width;
677                         while ( out_ptr != end )
678                                 *out_ptr ++ = *p ++;
679
680                         // Fill the outer part with black
681                         elements = inactive_width;
682                         while ( elements -- )
683                                 *out_ptr ++ = 0;
684         
685                         // Move to next input line
686                         in_line += iwidth;
687
688                 // Move to next output line
689                 out_line += owidth;
690                 }
691
692                 // Fill whole section with black
693                 elements = blank_elements;
694                 while ( elements -- )
695                         *out_line ++ = 0;
696         }
697
698         return output;
699 }
700
701 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
702 {
703         // Calculate strides
704         int istride = iwidth * 2;
705         int ostride = owidth * 2;
706
707         iwidth = iwidth - ( iwidth % 2 );
708         owidth = owidth - ( owidth % 2 );
709         //iheight = iheight - ( iheight % 2 );
710         //oheight = oheight - ( oheight % 2 );
711         
712         // Optimisation point
713         if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
714         {
715                 return;
716         }
717         else if ( iwidth == owidth && iheight == oheight )
718         {
719                 memcpy( output, input, iheight * istride );
720                 return;
721         }
722
723         // Coordinates (0,0 is middle of output)
724         int y;
725
726         // Calculate ranges
727         int out_x_range = owidth / 2;
728         int out_y_range = oheight / 2;
729         int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
730         int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
731
732         // Output pointers
733         uint8_t *out_line = output;
734         uint8_t *out_ptr = out_line;
735
736         // Calculate a middle and possibly invalid pointer in the input
737         uint8_t *in_middle = input + istride * ( iheight / 2 ) + iwidth;
738         int in_line = - in_y_range * istride - in_x_range * 2;
739
740         int elements;
741
742         // Fill whole section with black
743         y = out_y_range - ( iheight / 2 );
744         int blank_elements = ostride * y / 2;
745         elements = blank_elements;
746         while ( elements -- )
747         {
748                 *out_line ++ = 16;
749                 *out_line ++ = 128;
750         }
751
752         int active_width = 2 * iwidth;
753         int left_inactive_width = out_x_range - in_x_range;
754         int right_inactive_width = left_inactive_width;
755         uint8_t *p = NULL;
756         uint8_t *end = NULL;
757
758         if ( in_line % 4 )
759         {
760                 active_width -= 2;
761                 in_middle += 2;
762                 right_inactive_width += 2;
763         }
764
765         // Loop for the entirety of our output height.
766         while ( iheight -- )
767         {
768         // Start at the beginning of the line
769         out_ptr = out_line;
770
771                 // Fill the outer part with black
772                 elements = left_inactive_width;
773                 while ( elements -- )
774                 {
775                         *out_ptr ++ = 16;
776                         *out_ptr ++ = 128;
777                 }
778
779                 // We're in the input range for this row.
780                 p = in_middle + in_line;
781                 end = out_ptr + active_width;
782                 while ( out_ptr != end )
783                 {
784                         *out_ptr ++ = *p ++;
785                         *out_ptr ++ = *p ++;
786                 }
787
788                 // Fill the outer part with black
789                 elements = right_inactive_width;
790                 while ( elements -- )
791                 {
792                         *out_ptr ++ = 16;
793                         *out_ptr ++ = 128;
794                 }
795
796                 // Move to next input line
797                 in_line += istride;
798
799         // Move to next output line
800         out_line += ostride;
801         }
802
803         // Fill whole section with black
804         elements = blank_elements;
805         while ( elements -- )
806         {
807                 *out_line ++ = 16;
808                 *out_line ++ = 128;
809         }
810 }
811
812 /** A resizing function for yuv422 frames - this does not rescale, but simply
813         resizes. It assumes yuv422 images available on the frame so use with care.
814 */
815
816 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
817 {
818         // Get properties
819         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
820
821         // Get the input image, width and height
822         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
823         uint8_t *alpha = mlt_frame_get_alpha_mask( this );
824
825         int iwidth = mlt_properties_get_int( properties, "width" );
826         int iheight = mlt_properties_get_int( properties, "height" );
827
828         // If width and height are correct, don't do anything
829         if ( iwidth != owidth || iheight != oheight )
830         {
831                 // Create the output image
832                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
833
834                 // Call the generic resize
835                 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
836
837                 // Now update the frame
838                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
839                 mlt_properties_set_int( properties, "width", owidth );
840                 mlt_properties_set_int( properties, "height", oheight );
841
842                 // We should resize the alpha too
843                 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight );
844                 if ( alpha != NULL )
845                 {
846                         mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
847                         this->get_alpha_mask = NULL;
848                 }
849
850                 // Return the output
851                 return output;
852         }
853         // No change, return input
854         return input;
855 }
856
857 /** A rescaling function for yuv422 frames - low quality, and provided for testing
858         only. It assumes yuv422 images available on the frame so use with care.
859 */
860
861 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
862 {
863         // Get properties
864         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
865
866         // Get the input image, width and height
867         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
868         int iwidth = mlt_properties_get_int( properties, "width" );
869         int iheight = mlt_properties_get_int( properties, "height" );
870
871         // If width and height are correct, don't do anything
872         if ( iwidth != owidth || iheight != oheight )
873         {
874                 // Create the output image
875                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
876
877                 // Calculate strides
878                 int istride = iwidth * 2;
879                 int ostride = owidth * 2;
880
881                 iwidth = iwidth - ( iwidth % 4 );
882
883                 // Derived coordinates
884                 int dy, dx;
885
886         // Calculate ranges
887         int out_x_range = owidth / 2;
888         int out_y_range = oheight / 2;
889         int in_x_range = iwidth / 2;
890         int in_y_range = iheight / 2;
891
892         // Output pointers
893         register uint8_t *out_line = output;
894         register uint8_t *out_ptr;
895
896         // Calculate a middle pointer
897         uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
898         uint8_t *in_line;
899
900                 // Generate the affine transform scaling values
901                 register int scale_width = ( iwidth << 16 ) / owidth;
902                 register int scale_height = ( iheight << 16 ) / oheight;
903                 register int base = 0;
904
905                 int outer = out_x_range * scale_width;
906                 int bottom = out_y_range * scale_height;
907
908         // Loop for the entirety of our output height.
909         for ( dy = - bottom; dy < bottom; dy += scale_height )
910         {
911                 // Start at the beginning of the line
912                 out_ptr = out_line;
913         
914                 // Pointer to the middle of the input line
915                 in_line = in_middle + ( dy >> 16 ) * istride;
916
917                 // Loop for the entirety of our output row.
918                 for ( dx = - outer; dx < outer; dx += scale_width )
919                 {
920                                 base = dx >> 15;
921                                 base &= 0xfffffffe;
922                                 *out_ptr ++ = *( in_line + base );
923                                 base &= 0xfffffffc;
924                                 *out_ptr ++ = *( in_line + base + 1 );
925                                 dx += scale_width;
926                                 base = dx >> 15;
927                                 base &= 0xfffffffe;
928                                 *out_ptr ++ = *( in_line + base );
929                                 base &= 0xfffffffc;
930                                 *out_ptr ++ = *( in_line + base + 3 );
931                 }
932
933                 // Move to next output line
934                 out_line += ostride;
935         }
936
937                 // Now update the frame
938                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
939                 mlt_properties_set_int( properties, "width", owidth );
940                 mlt_properties_set_int( properties, "height", oheight );
941
942                 // Return the output
943                 return output;
944         }
945
946         // No change, return input
947         return input;
948 }
949
950 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 )
951 {
952         int ret = 0;
953         int16_t *src, *dest;
954         int frequency_src = *frequency, frequency_dest = *frequency;
955         int channels_src = *channels, channels_dest = *channels;
956         int samples_src = *samples, samples_dest = *samples;
957         int i, j;
958         double d = 0, s = 0;
959
960         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
961         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
962
963         int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
964         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
965         if ( silent )
966                 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
967
968         silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
969         mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
970         if ( silent )
971                 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
972
973         if ( channels_src > 6 )
974                 channels_src = 0;
975         if ( channels_dest > 6 )
976                 channels_dest = 0;
977         if ( samples_src > 4000 )
978                 samples_src = 0;
979         if ( samples_dest > 4000 )
980                 samples_dest = 0;
981
982         // determine number of samples to process
983         *samples = samples_src < samples_dest ? samples_src : samples_dest;
984         *channels = channels_src < channels_dest ? channels_src : channels_dest;
985         *buffer = dest;
986         *frequency = frequency_dest;
987
988         // Compute a smooth ramp over start to end
989         float weight = weight_start;
990         float weight_step = ( weight_end - weight_start ) / *samples;
991
992         // Mixdown
993         for ( i = 0; i < *samples; i++ )
994         {
995                 for ( j = 0; j < *channels; j++ )
996                 {
997                         if ( j < channels_dest )
998                                 d = (double) dest[ i * channels_dest + j ];
999                         if ( j < channels_src )
1000                                 s = (double) src[ i * channels_src + j ];
1001                         dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1002                 }
1003                 weight += weight_step;
1004         }
1005
1006         return ret;
1007 }
1008
1009 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1010 {
1011         int samples = 0;
1012
1013         if ( ( int )( fps * 100 ) == 2997 )
1014         {
1015                 samples = frequency / 30;
1016
1017                 switch ( frequency )
1018                 {
1019                         case 48000:
1020                                 if ( position % 5 != 0 )
1021                                         samples += 2;
1022                                 break;
1023                         case 44100:
1024                                 if ( position % 300 == 0 )
1025                                         samples = 1471;
1026                                 else if ( position % 30 == 0 )
1027                                         samples = 1470;
1028                                 else if ( position % 2 == 0 )
1029                                         samples = 1472;
1030                                 else
1031                                         samples = 1471;
1032                                 break;
1033                         case 32000:
1034                                 if ( position % 30 == 0 )
1035                                         samples = 1068;
1036                                 else if ( position % 29 == 0 )
1037                                         samples = 1067;
1038                                 else if ( position % 4 == 2 )
1039                                         samples = 1067;
1040                                 else
1041                                         samples = 1068;
1042                                 break;
1043                         default:
1044                                 samples = 0;
1045                 }
1046         }
1047         else if ( fps != 0 )
1048         {
1049                 samples = frequency / fps;
1050         }
1051
1052         return samples;
1053 }