]> git.sesse.net Git - mlt/blob - src/framework/mlt_frame.c
53472dbfb6537ecdc839033ac9aa3f6e49d5a25b
[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         int error = 0;
266
267         *width = *width >> 1 << 1;
268         
269         if ( get_image != NULL )
270         {
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         }
275         else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
276         {
277                 *format = mlt_image_yuv422;
278                 *buffer = mlt_properties_get_data( properties, "image", NULL );
279                 *width = mlt_properties_get_int( properties, "width" );
280                 *height = mlt_properties_get_int( properties, "height" );
281         }
282         else if ( producer != NULL )
283         {
284                 mlt_frame test_frame = NULL;
285                 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
286                 if ( test_frame != NULL )
287                 {
288                         mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
289                         mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
290                         mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
291                         mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
292                         mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
293                         mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
294                         mlt_properties_set_int( properties, "width", *width );
295                         mlt_properties_set_int( properties, "height", *height );
296                         mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
297                 }
298                 else
299                 {
300                         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
301                         mlt_frame_get_image( this, buffer, format, width, height, writable );
302                 }
303         }
304         else
305         {
306                 register uint8_t *p;
307                 register uint8_t *q;
308                 int size = 0;
309
310                 *width = *width == 0 ? 720 : *width;
311                 *height = *height == 0 ? 576 : *height;
312                 size = *width * *height;
313
314                 mlt_properties_set_int( properties, "width", *width );
315                 mlt_properties_set_int( properties, "height", *height );
316                 mlt_properties_set_int( properties, "aspect_ratio", 1 );
317
318                 switch( *format )
319                 {
320                         case mlt_image_none:
321                                 size = 0;
322                                 *buffer = NULL;
323                                 break;
324                         case mlt_image_rgb24:
325                                 size *= 3;
326                                 size += *width * 3;
327                                 *buffer = mlt_pool_alloc( size );
328                                 if ( *buffer )
329                                         memset( *buffer, 255, size );
330                                 break;
331                         case mlt_image_rgb24a:
332                                 size *= 4;
333                                 size += *width * 4;
334                                 *buffer = mlt_pool_alloc( size );
335                                 if ( *buffer )
336                                         memset( *buffer, 255, size );
337                                 break;
338                         case mlt_image_yuv422:
339                                 size *= 2;
340                                 size += *width * 2;
341                                 *buffer = mlt_pool_alloc( size );
342                                 p = *buffer;
343                                 q = p + size;
344                                 while ( p != NULL && p != q )
345                                 {
346                                         *p ++ = 235;
347                                         *p ++ = 128;
348                                 }
349                                 break;
350                         case mlt_image_yuv420p:
351                                 size = size * 3 / 2;
352                                 *buffer = mlt_pool_alloc( size );
353                                 if ( *buffer )
354                                         memset( *buffer, 255, size );
355                                 break;
356                 }
357
358                 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
359                 mlt_properties_set_int( properties, "test_image", 1 );
360         }
361
362         return error;
363 }
364
365 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
366 {
367         if ( this != NULL && this->get_alpha_mask != NULL )
368                 return this->get_alpha_mask( this );
369         return this == NULL ? NULL : mlt_properties_get_data( &this->parent, "alpha", NULL );
370 }
371
372 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
373 {
374         mlt_get_audio get_audio = mlt_frame_pop_audio( this );
375         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
376         int hide = mlt_properties_get_int( properties, "test_audio" );
377
378         if ( hide == 0 && get_audio != NULL )
379         {
380                 mlt_position position = mlt_frame_get_position( this );
381                 get_audio( this, buffer, format, frequency, channels, samples );
382                 mlt_frame_set_position( this, position );
383         }
384         else if ( mlt_properties_get_data( properties, "audio", NULL ) )
385         {
386                 *buffer = mlt_properties_get_data( properties, "audio", NULL );
387                 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
388                 *channels = mlt_properties_get_int( properties, "audio_channels" );
389                 *samples = mlt_properties_get_int( properties, "audio_samples" );
390         }
391         else
392         {
393                 int size = 0;
394                 *samples = *samples <= 0 ? 1920 : *samples;
395                 *channels = *channels <= 0 ? 2 : *channels;
396                 *frequency = *frequency <= 0 ? 48000 : *frequency;
397                 size = *samples * *channels * sizeof( int16_t );
398                 *buffer = mlt_pool_alloc( size );
399                 if ( *buffer != NULL )
400                         memset( *buffer, 0, size );
401                 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
402                 mlt_properties_set_int( properties, "test_audio", 1 );
403         }
404
405         mlt_properties_set_int( properties, "audio_frequency", *frequency );
406         mlt_properties_set_int( properties, "audio_channels", *channels );
407         mlt_properties_set_int( properties, "audio_samples", *samples );
408
409         if ( mlt_properties_get( properties, "meta.volume" ) )
410         {
411                 double value = mlt_properties_get_double( properties, "meta.volume" );
412                 if ( value == 0.0 )
413                 {
414                         memset( *buffer, 0, *samples * *channels * 2 );
415                         mlt_properties_set_double( properties, "meta.volume", 1.0 );
416                 }
417                 else if ( value != 1.0 )
418                 {
419                         int total = *samples * *channels;
420                         int16_t *p = *buffer;
421                         while ( total -- )
422                         {
423                                 *p = *p * value;
424                                 p ++;
425                         }
426                         mlt_properties_set_double( properties, "meta.volume", 1.0 );
427                 }
428         }
429
430         return 0;
431 }
432
433 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
434 {
435         int16_t *pcm = NULL;
436         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
437         mlt_audio_format format = mlt_audio_pcm;
438         int frequency = 32000; // lower frequency available?
439         int channels = 2;
440         double fps = mlt_properties_get_double( properties, "fps" );
441         int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
442         
443         // Get the pcm data
444         mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
445         
446         // Make an 8-bit buffer large enough to hold rendering
447         int size = w * h;
448         unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
449         if ( bitmap != NULL )
450                 memset( bitmap, 0, size );
451         mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
452         
453         // Render vertical lines
454         int16_t *ubound = pcm + samples * channels;
455         int skip = samples / w - 1;
456         int i, j, k;
457         
458         // Iterate sample stream and along x coordinate
459         for ( i = 0; i < w && pcm < ubound; i++ )
460         {
461                 // pcm data has channels interleaved
462                 for ( j = 0; j < channels; j++ )
463                 {
464                         // Determine sample's magnitude from 2s complement;
465                         int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
466                         // The height of a line is the ratio of the magnitude multiplied by 
467                         // half the vertical resolution
468                         int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
469                         // Determine the starting y coordinate - left channel above center,
470                         // right channel below - currently assumes 2 channels
471                         int displacement = ( h / 2 ) - ( 1 - j ) * height;
472                         // Position buffer pointer using y coordinate, stride, and x coordinate
473                         unsigned char *p = &bitmap[ i + displacement * w ];
474                         
475                         // Draw vertical line
476                         for ( k = 0; k < height; k++ )
477                                 p[ w * k ] = 0xFF;
478                         
479                         pcm++;
480                 }
481                 pcm += skip * channels;
482         }
483
484         return bitmap;
485 }
486
487 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
488 {
489         if ( this != NULL )
490                 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
491         return NULL;
492 }
493
494 void mlt_frame_close( mlt_frame this )
495 {
496         if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
497         {
498                 mlt_deque_close( this->stack_image );
499                 mlt_deque_close( this->stack_audio );
500                 while( mlt_deque_peek_back( this->stack_service ) )
501                         mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
502                 mlt_deque_close( this->stack_service );
503                 mlt_properties_close( &this->parent );
504                 free( this );
505         }
506 }
507
508 /***** convenience functions *****/
509
510 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
511 {
512         int ret = 0;
513         register int y0, y1, u0, u1, v0, v1;
514         register int r, g, b;
515         register uint8_t *d = yuv;
516         register int i, j;
517
518         for ( i = 0; i < height; i++ )
519         {
520                 register uint8_t *s = rgba + ( stride * i );
521                 for ( j = 0; j < ( width / 2 ); j++ )
522                 {
523                         r = *s++;
524                         g = *s++;
525                         b = *s++;
526                         *alpha++ = *s++;
527                         RGB2YUV (r, g, b, y0, u0 , v0);
528                         r = *s++;
529                         g = *s++;
530                         b = *s++;
531                         *alpha++ = *s++;
532                         RGB2YUV (r, g, b, y1, u1 , v1);
533                         *d++ = y0;
534                         *d++ = (u0+u1) >> 1;
535                         *d++ = y1;
536                         *d++ = (v0+v1) >> 1;
537                 }
538                 if ( width % 2 )
539                 {
540                         r = *s++;
541                         g = *s++;
542                         b = *s++;
543                         *alpha++ = *s++;
544                         RGB2YUV (r, g, b, y0, u0 , v0);
545                         *d++ = y0;
546                         *d++ = u0;
547                 }
548         }
549         return ret;
550 }
551
552 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
553 {
554         int ret = 0;
555         register int y0, y1, u0, u1, v0, v1;
556         register int r, g, b;
557         register uint8_t *d = yuv;
558         register int i, j;
559
560         for ( i = 0; i < height; i++ )
561         {
562                 register uint8_t *s = rgb + ( stride * i );
563                 for ( j = 0; j < ( width / 2 ); j++ )
564                 {
565                         r = *s++;
566                         g = *s++;
567                         b = *s++;
568                         RGB2YUV (r, g, b, y0, u0 , v0);
569                         r = *s++;
570                         g = *s++;
571                         b = *s++;
572                         RGB2YUV (r, g, b, y1, u1 , v1);
573                         *d++ = y0;
574                         *d++ = (u0+u1) >> 1;
575                         *d++ = y1;
576                         *d++ = (v0+v1) >> 1;
577                 }
578                 if ( width % 2 )
579                 {
580                         r = *s++;
581                         g = *s++;
582                         b = *s++;
583                         RGB2YUV (r, g, b, y0, u0 , v0);
584                         *d++ = y0;
585                         *d++ = u0;
586                 }
587         }
588         return ret;
589 }
590
591 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
592 {
593         int ret = 0;
594         register int i, j;
595
596         int half = width >> 1;
597
598         uint8_t *Y = yuv420p;
599         uint8_t *U = Y + width * height;
600         uint8_t *V = U + width * height / 4;
601
602         register uint8_t *d = yuv;
603
604         for ( i = 0; i < height; i++ )
605         {
606                 register uint8_t *u = U + ( i / 2 ) * ( half );
607                 register uint8_t *v = V + ( i / 2 ) * ( half );
608
609                 for ( j = 0; j < half; j++ )
610                 {
611                         *d ++ = *Y ++;
612                         *d ++ = *u ++;
613                         *d ++ = *Y ++;
614                         *d ++ = *v ++;
615                 }
616         }
617         return ret;
618 }
619
620 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight )
621 {
622         uint8_t *output = NULL;
623
624         if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
625         {
626                 iwidth = iwidth - ( iwidth % 2 );
627                 owidth = owidth - ( owidth % 2 );
628
629                 output = mlt_pool_alloc( owidth * oheight );
630
631                 // Coordinates (0,0 is middle of output)
632                 int y;
633
634                 // Calculate ranges
635                 int out_x_range = owidth / 2;
636                 int out_y_range = oheight / 2;
637                 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
638                 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
639
640                 // Output pointers
641                 uint8_t *out_line = output;
642                 uint8_t *out_ptr = out_line;
643
644                 // Calculate a middle and possibly invalid pointer in the input
645                 uint8_t *in_middle = input + iwidth * ( iheight / 2 ) + ( iwidth / 2 );
646                 int in_line = - in_y_range * iwidth - in_x_range;
647
648                 int elements;
649
650                 // Fill whole section with black
651                 y = out_y_range - ( iheight / 2 );
652                 int blank_elements = owidth * y;
653                 elements = blank_elements;
654                 while ( elements -- )
655                         *out_line ++ = 0;
656
657                 int active_width = iwidth;
658                 int inactive_width = out_x_range - in_x_range;
659                 uint8_t *p = NULL;
660                 uint8_t *end = NULL;
661
662                 // Loop for the entirety of our output height.
663                 while ( iheight -- )
664                 {
665                 // Start at the beginning of the line
666                 out_ptr = out_line;
667
668                         // Fill the outer part with black
669                         elements = inactive_width;
670                         while ( elements -- )
671                                 *out_ptr ++ = 0;
672
673                         // We're in the input range for this row.
674                         p = in_middle + in_line;
675                         end = out_ptr + active_width;
676                         while ( out_ptr != end )
677                                 *out_ptr ++ = *p ++;
678
679                         // Fill the outer part with black
680                         elements = inactive_width;
681                         while ( elements -- )
682                                 *out_ptr ++ = 0;
683         
684                         // Move to next input line
685                         in_line += iwidth;
686
687                 // Move to next output line
688                 out_line += owidth;
689                 }
690
691                 // Fill whole section with black
692                 elements = blank_elements;
693                 while ( elements -- )
694                         *out_line ++ = 0;
695         }
696
697         return output;
698 }
699
700 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
701 {
702         // Calculate strides
703         int istride = iwidth * 2;
704         int ostride = owidth * 2;
705
706         iwidth = iwidth - ( iwidth % 2 );
707         owidth = owidth - ( owidth % 2 );
708         //iheight = iheight - ( iheight % 2 );
709         //oheight = oheight - ( oheight % 2 );
710         
711         // Optimisation point
712         if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
713         {
714                 return;
715         }
716         else if ( iwidth == owidth && iheight == oheight )
717         {
718                 memcpy( output, input, iheight * istride );
719                 return;
720         }
721
722         // Coordinates (0,0 is middle of output)
723         int y;
724
725         // Calculate ranges
726         int out_x_range = owidth / 2;
727         int out_y_range = oheight / 2;
728         int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
729         int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
730
731         // Output pointers
732         uint8_t *out_line = output;
733         uint8_t *out_ptr = out_line;
734
735         // Calculate a middle and possibly invalid pointer in the input
736         uint8_t *in_middle = input + istride * ( iheight / 2 ) + iwidth;
737         int in_line = - in_y_range * istride - in_x_range * 2;
738
739         int elements;
740
741         // Fill whole section with black
742         y = out_y_range - ( iheight / 2 );
743         int blank_elements = ostride * y / 2;
744         elements = blank_elements;
745         while ( elements -- )
746         {
747                 *out_line ++ = 16;
748                 *out_line ++ = 128;
749         }
750
751         int active_width = 2 * iwidth;
752         int left_inactive_width = out_x_range - in_x_range;
753         int right_inactive_width = left_inactive_width;
754         uint8_t *p = NULL;
755         uint8_t *end = NULL;
756
757         if ( in_line % 4 )
758         {
759                 active_width -= 2;
760                 in_middle += 2;
761                 right_inactive_width += 2;
762         }
763
764         // Loop for the entirety of our output height.
765         while ( iheight -- )
766         {
767         // Start at the beginning of the line
768         out_ptr = out_line;
769
770                 // Fill the outer part with black
771                 elements = left_inactive_width;
772                 while ( elements -- )
773                 {
774                         *out_ptr ++ = 16;
775                         *out_ptr ++ = 128;
776                 }
777
778                 // We're in the input range for this row.
779                 p = in_middle + in_line;
780                 end = out_ptr + active_width;
781                 while ( out_ptr != end )
782                 {
783                         *out_ptr ++ = *p ++;
784                         *out_ptr ++ = *p ++;
785                 }
786
787                 // Fill the outer part with black
788                 elements = right_inactive_width;
789                 while ( elements -- )
790                 {
791                         *out_ptr ++ = 16;
792                         *out_ptr ++ = 128;
793                 }
794
795                 // Move to next input line
796                 in_line += istride;
797
798         // Move to next output line
799         out_line += ostride;
800         }
801
802         // Fill whole section with black
803         elements = blank_elements;
804         while ( elements -- )
805         {
806                 *out_line ++ = 16;
807                 *out_line ++ = 128;
808         }
809 }
810
811 /** A resizing function for yuv422 frames - this does not rescale, but simply
812         resizes. It assumes yuv422 images available on the frame so use with care.
813 */
814
815 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
816 {
817         // Get properties
818         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
819
820         // Get the input image, width and height
821         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
822         uint8_t *alpha = mlt_frame_get_alpha_mask( this );
823
824         int iwidth = mlt_properties_get_int( properties, "width" );
825         int iheight = mlt_properties_get_int( properties, "height" );
826
827         // If width and height are correct, don't do anything
828         if ( iwidth != owidth || iheight != oheight )
829         {
830                 // Create the output image
831                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
832
833                 // Call the generic resize
834                 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
835
836                 // Now update the frame
837                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
838                 mlt_properties_set_int( properties, "width", owidth );
839                 mlt_properties_set_int( properties, "height", oheight );
840
841                 // We should resize the alpha too
842                 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight );
843                 if ( alpha != NULL )
844                 {
845                         mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
846                         this->get_alpha_mask = NULL;
847                 }
848
849                 // Return the output
850                 return output;
851         }
852         // No change, return input
853         return input;
854 }
855
856 /** A rescaling function for yuv422 frames - low quality, and provided for testing
857         only. It assumes yuv422 images available on the frame so use with care.
858 */
859
860 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
861 {
862         // Get properties
863         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
864
865         // Get the input image, width and height
866         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
867         int iwidth = mlt_properties_get_int( properties, "width" );
868         int iheight = mlt_properties_get_int( properties, "height" );
869
870         // If width and height are correct, don't do anything
871         if ( iwidth != owidth || iheight != oheight )
872         {
873                 // Create the output image
874                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
875
876                 // Calculate strides
877                 int istride = iwidth * 2;
878                 int ostride = owidth * 2;
879
880                 iwidth = iwidth - ( iwidth % 4 );
881
882                 // Derived coordinates
883                 int dy, dx;
884
885         // Calculate ranges
886         int out_x_range = owidth / 2;
887         int out_y_range = oheight / 2;
888         int in_x_range = iwidth / 2;
889         int in_y_range = iheight / 2;
890
891         // Output pointers
892         register uint8_t *out_line = output;
893         register uint8_t *out_ptr;
894
895         // Calculate a middle pointer
896         uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
897         uint8_t *in_line;
898
899                 // Generate the affine transform scaling values
900                 register int scale_width = ( iwidth << 16 ) / owidth;
901                 register int scale_height = ( iheight << 16 ) / oheight;
902                 register int base = 0;
903
904                 int outer = out_x_range * scale_width;
905                 int bottom = out_y_range * scale_height;
906
907         // Loop for the entirety of our output height.
908         for ( dy = - bottom; dy < bottom; dy += scale_height )
909         {
910                 // Start at the beginning of the line
911                 out_ptr = out_line;
912         
913                 // Pointer to the middle of the input line
914                 in_line = in_middle + ( dy >> 16 ) * istride;
915
916                 // Loop for the entirety of our output row.
917                 for ( dx = - outer; dx < outer; dx += scale_width )
918                 {
919                                 base = dx >> 15;
920                                 base &= 0xfffffffe;
921                                 *out_ptr ++ = *( in_line + base );
922                                 base &= 0xfffffffc;
923                                 *out_ptr ++ = *( in_line + base + 1 );
924                                 dx += scale_width;
925                                 base = dx >> 15;
926                                 base &= 0xfffffffe;
927                                 *out_ptr ++ = *( in_line + base );
928                                 base &= 0xfffffffc;
929                                 *out_ptr ++ = *( in_line + base + 3 );
930                 }
931
932                 // Move to next output line
933                 out_line += ostride;
934         }
935
936                 // Now update the frame
937                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
938                 mlt_properties_set_int( properties, "width", owidth );
939                 mlt_properties_set_int( properties, "height", oheight );
940
941                 // Return the output
942                 return output;
943         }
944
945         // No change, return input
946         return input;
947 }
948
949 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 )
950 {
951         int ret = 0;
952         int16_t *src, *dest;
953         int frequency_src = *frequency, frequency_dest = *frequency;
954         int channels_src = *channels, channels_dest = *channels;
955         int samples_src = *samples, samples_dest = *samples;
956         int i, j;
957         double d = 0, s = 0;
958
959         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
960         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
961
962         int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
963         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
964         if ( silent )
965                 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
966
967         silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
968         mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
969         if ( silent )
970                 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
971
972         if ( channels_src > 6 )
973                 channels_src = 0;
974         if ( channels_dest > 6 )
975                 channels_dest = 0;
976         if ( samples_src > 4000 )
977                 samples_src = 0;
978         if ( samples_dest > 4000 )
979                 samples_dest = 0;
980
981         // determine number of samples to process
982         *samples = samples_src < samples_dest ? samples_src : samples_dest;
983         *channels = channels_src < channels_dest ? channels_src : channels_dest;
984         *buffer = dest;
985         *frequency = frequency_dest;
986
987         // Compute a smooth ramp over start to end
988         float weight = weight_start;
989         float weight_step = ( weight_end - weight_start ) / *samples;
990
991         // Mixdown
992         for ( i = 0; i < *samples; i++ )
993         {
994                 for ( j = 0; j < *channels; j++ )
995                 {
996                         if ( j < channels_dest )
997                                 d = (double) dest[ i * channels_dest + j ];
998                         if ( j < channels_src )
999                                 s = (double) src[ i * channels_src + j ];
1000                         dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1001                 }
1002                 weight += weight_step;
1003         }
1004
1005         return ret;
1006 }
1007
1008 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1009 {
1010         int samples = 0;
1011
1012         if ( ( int )( fps * 100 ) == 2997 )
1013         {
1014                 samples = frequency / 30;
1015
1016                 switch ( frequency )
1017                 {
1018                         case 48000:
1019                                 if ( position % 5 != 0 )
1020                                         samples += 2;
1021                                 break;
1022                         case 44100:
1023                                 if ( position % 300 == 0 )
1024                                         samples = 1471;
1025                                 else if ( position % 30 == 0 )
1026                                         samples = 1470;
1027                                 else if ( position % 2 == 0 )
1028                                         samples = 1472;
1029                                 else
1030                                         samples = 1471;
1031                                 break;
1032                         case 32000:
1033                                 if ( position % 30 == 0 )
1034                                         samples = 1068;
1035                                 else if ( position % 29 == 0 )
1036                                         samples = 1067;
1037                                 else if ( position % 4 == 2 )
1038                                         samples = 1067;
1039                                 else
1040                                         samples = 1068;
1041                                 break;
1042                         default:
1043                                 samples = 0;
1044                 }
1045         }
1046         else if ( fps != 0 )
1047         {
1048                 samples = frequency / fps;
1049         }
1050
1051         return samples;
1052 }