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