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