]> git.sesse.net Git - mlt/blob - src/framework/mlt_frame.c
mlt_frame.c: let image conversions accept NULL for the alpha parameter
[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 library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, 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 #include <math.h>
29
30 /** Constructor for a frame.
31 */
32
33 mlt_frame mlt_frame_init( )
34 {
35         // Allocate a frame
36         mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
37
38         if ( this != NULL )
39         {
40                 // Get the normalisation
41                 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
42
43                 // Initialise the properties
44                 mlt_properties properties = &this->parent;
45                 mlt_properties_init( properties, this );
46
47                 // Set default properties on the frame
48                 mlt_properties_set_position( properties, "_position", 0.0 );
49                 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
50
51                 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
52                 {
53                         mlt_properties_set_int( properties, "width", 720 );
54                         mlt_properties_set_int( properties, "height", 576 );
55                         mlt_properties_set_int( properties, "normalised_width", 720 );
56                         mlt_properties_set_int( properties, "normalised_height", 576 );
57                         mlt_properties_set_double( properties, "aspect_ratio", 59.0/54.0 );
58                 }
59                 else
60                 {
61                         mlt_properties_set_int( properties, "width", 720 );
62                         mlt_properties_set_int( properties, "height", 480 );
63                         mlt_properties_set_int( properties, "normalised_width", 720 );
64                         mlt_properties_set_int( properties, "normalised_height", 480 );
65                         mlt_properties_set_double( properties, "aspect_ratio", 10.0/11.0 );
66                 }
67
68                 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
69                 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
70
71                 // Construct stacks for frames and methods
72                 this->stack_image = mlt_deque_init( );
73                 this->stack_audio = mlt_deque_init( );
74                 this->stack_service = mlt_deque_init( );
75         }
76
77         return this;
78 }
79
80 /** Fetch the frames properties.
81 */
82
83 mlt_properties mlt_frame_properties( mlt_frame this )
84 {
85         return this != NULL ? &this->parent : NULL;
86 }
87
88 /** Check if we have a way to derive something other than a test card.
89 */
90
91 int mlt_frame_is_test_card( mlt_frame this )
92 {
93         return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
94 }
95
96 /** Check if we have a way to derive something other than test audio.
97 */
98
99 int mlt_frame_is_test_audio( mlt_frame this )
100 {
101         return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
102 }
103
104 /** Get the aspect ratio of the frame.
105 */
106
107 double mlt_frame_get_aspect_ratio( mlt_frame this )
108 {
109         return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
110 }
111
112 /** Set the aspect ratio of the frame.
113 */
114
115 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
116 {
117         return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
118 }
119
120 /** Get the position of this frame.
121 */
122
123 mlt_position mlt_frame_get_position( mlt_frame this )
124 {
125         int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
126         return pos < 0 ? 0 : pos;
127 }
128
129 /** Set the position of this frame.
130 */
131
132 int mlt_frame_set_position( mlt_frame this, mlt_position value )
133 {
134         return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
135 }
136
137 /** Stack a get_image callback.
138 */
139
140 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
141 {
142         return mlt_deque_push_back( this->stack_image, get_image );
143 }
144
145 /** Pop a get_image callback.
146 */
147
148 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
149 {
150         return mlt_deque_pop_back( this->stack_image );
151 }
152
153 /** Push a frame.
154 */
155
156 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
157 {
158         return mlt_deque_push_back( this->stack_image, that );
159 }
160
161 /** Pop a frame.
162 */
163
164 mlt_frame mlt_frame_pop_frame( mlt_frame this )
165 {
166         return mlt_deque_pop_back( this->stack_image );
167 }
168
169 /** Push a service.
170 */
171
172 int mlt_frame_push_service( mlt_frame this, void *that )
173 {
174         return mlt_deque_push_back( this->stack_image, that );
175 }
176
177 /** Pop a service.
178 */
179
180 void *mlt_frame_pop_service( mlt_frame this )
181 {
182         return mlt_deque_pop_back( this->stack_image );
183 }
184
185 /** Push a service.
186 */
187
188 int mlt_frame_push_service_int( mlt_frame this, int that )
189 {
190         return mlt_deque_push_back_int( this->stack_image, that );
191 }
192
193 /** Pop a service.
194 */
195
196 int mlt_frame_pop_service_int( mlt_frame this )
197 {
198         return mlt_deque_pop_back_int( this->stack_image );
199 }
200
201 /** Push an audio item on the stack.
202 */
203
204 int mlt_frame_push_audio( mlt_frame this, void *that )
205 {
206         return mlt_deque_push_back( this->stack_audio, that );
207 }
208
209 /** Pop an audio item from the stack
210 */
211
212 void *mlt_frame_pop_audio( mlt_frame this )
213 {
214         return mlt_deque_pop_back( this->stack_audio );
215 }
216
217 /** Return the service stack
218 */
219
220 mlt_deque mlt_frame_service_stack( mlt_frame this )
221 {
222         return this->stack_service;
223 }
224
225 /** Replace image stack with the information provided.
226
227         This might prove to be unreliable and restrictive - the idea is that a transition
228         which normally uses two images may decide to only use the b frame (ie: in the case
229         of a composite where the b frame completely obscures the a frame).
230
231         The image must be writable and the destructor for the image itself must be taken
232         care of on another frame and that frame cannot have a replace applied to it... 
233         Further it assumes that no alpha mask is in use.
234
235         For these reasons, it can only be used in a specific situation - when you have 
236         multiple tracks each with their own transition and these transitions are applied
237         in a strictly reversed order (ie: highest numbered [lowest track] is processed 
238         first).
239
240         More reliable approach - the cases should be detected during the process phase
241         and the upper tracks should simply not be invited to stack...
242 */
243
244 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
245 {
246         // Remove all items from the stack
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         if ( get_image != NULL )
268         {
269                 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
270                 mlt_position position = mlt_frame_get_position( this );
271                 error = get_image( this, buffer, format, width, height, writable );
272                 mlt_properties_set_int( properties, "width", *width );
273                 mlt_properties_set_int( properties, "height", *height );
274                 mlt_properties_set_int( properties, "format", *format );
275                 mlt_frame_set_position( this, position );
276         }
277         else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
278         {
279                 *format = mlt_properties_get_int( properties, "format" );
280                 *buffer = mlt_properties_get_data( properties, "image", NULL );
281                 *width = mlt_properties_get_int( properties, "width" );
282                 *height = mlt_properties_get_int( properties, "height" );
283         }
284         else if ( producer != NULL )
285         {
286                 mlt_frame test_frame = NULL;
287                 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
288                 if ( test_frame != NULL )
289                 {
290                         mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
291                         mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
292                         mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
293                         mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
294                         mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
295                         mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
296                         mlt_properties_set_int( properties, "width", *width );
297                         mlt_properties_set_int( properties, "height", *height );
298                         mlt_properties_set_int( properties, "format", *format );
299                         mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
300                 }
301                 else
302                 {
303                         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
304                         mlt_frame_get_image( this, buffer, format, width, height, writable );
305                 }
306         }
307         else
308         {
309                 register uint8_t *p;
310                 register uint8_t *q;
311                 int size = 0;
312
313                 *width = *width == 0 ? 720 : *width;
314                 *height = *height == 0 ? 576 : *height;
315                 size = *width * *height;
316
317                 mlt_properties_set_int( properties, "format", *format );
318                 mlt_properties_set_int( properties, "width", *width );
319                 mlt_properties_set_int( properties, "height", *height );
320                 mlt_properties_set_int( properties, "aspect_ratio", 0 );
321
322                 switch( *format )
323                 {
324                         case mlt_image_none:
325                                 size = 0;
326                                 *buffer = NULL;
327                                 break;
328                         case mlt_image_rgb24:
329                                 size *= 3;
330                                 size += *width * 3;
331                                 *buffer = mlt_pool_alloc( size );
332                                 if ( *buffer )
333                                         memset( *buffer, 255, size );
334                                 break;
335                         case mlt_image_rgb24a:
336                         case mlt_image_opengl:
337                                 size *= 4;
338                                 size += *width * 4;
339                                 *buffer = mlt_pool_alloc( size );
340                                 if ( *buffer )
341                                         memset( *buffer, 255, size );
342                                 break;
343                         case mlt_image_yuv422:
344                                 size *= 2;
345                                 size += *width * 2;
346                                 *buffer = mlt_pool_alloc( size );
347                                 p = *buffer;
348                                 q = p + size;
349                                 while ( p != NULL && p != q )
350                                 {
351                                         *p ++ = 235;
352                                         *p ++ = 128;
353                                 }
354                                 break;
355                         case mlt_image_yuv420p:
356                                 size = size * 3 / 2;
357                                 *buffer = mlt_pool_alloc( size );
358                                 if ( *buffer )
359                                         memset( *buffer, 255, size );
360                                 break;
361                 }
362
363                 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
364                 mlt_properties_set_int( properties, "test_image", 1 );
365         }
366
367         mlt_properties_set_int( properties, "scaled_width", *width );
368         mlt_properties_set_int( properties, "scaled_height", *height );
369
370         return error;
371 }
372
373 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
374 {
375         uint8_t *alpha = NULL;
376         if ( this != NULL )
377         {
378                 if ( this->get_alpha_mask != NULL )
379                         alpha = this->get_alpha_mask( this );
380                 if ( alpha == NULL )
381                         alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
382                 if ( alpha == NULL )
383                 {
384                         int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
385                         alpha = mlt_pool_alloc( size );
386                         memset( alpha, 255, size );
387                         mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
388                 }
389         }
390         return alpha;
391 }
392
393 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
394 {
395         mlt_get_audio get_audio = mlt_frame_pop_audio( this );
396         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
397         int hide = mlt_properties_get_int( properties, "test_audio" );
398
399         if ( hide == 0 && get_audio != NULL )
400         {
401                 mlt_position position = mlt_frame_get_position( this );
402                 get_audio( this, buffer, format, frequency, channels, samples );
403                 mlt_frame_set_position( this, position );
404         }
405         else if ( mlt_properties_get_data( properties, "audio", NULL ) )
406         {
407                 *buffer = mlt_properties_get_data( properties, "audio", NULL );
408                 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
409                 *channels = mlt_properties_get_int( properties, "audio_channels" );
410                 *samples = mlt_properties_get_int( properties, "audio_samples" );
411         }
412         else
413         {
414                 int size = 0;
415                 *samples = *samples <= 0 ? 1920 : *samples;
416                 *channels = *channels <= 0 ? 2 : *channels;
417                 *frequency = *frequency <= 0 ? 48000 : *frequency;
418                 size = *samples * *channels * sizeof( int16_t );
419                 *buffer = mlt_pool_alloc( size );
420                 if ( *buffer != NULL )
421                         memset( *buffer, 0, size );
422                 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
423                 mlt_properties_set_int( properties, "test_audio", 1 );
424         }
425
426         mlt_properties_set_int( properties, "audio_frequency", *frequency );
427         mlt_properties_set_int( properties, "audio_channels", *channels );
428         mlt_properties_set_int( properties, "audio_samples", *samples );
429
430         if ( mlt_properties_get( properties, "meta.volume" ) )
431         {
432                 double value = mlt_properties_get_double( properties, "meta.volume" );
433
434                 if ( value == 0.0 )
435                 {
436                         memset( *buffer, 0, *samples * *channels * 2 );
437                 }
438                 else if ( value != 1.0 )
439                 {
440                         int total = *samples * *channels;
441                         int16_t *p = *buffer;
442                         while ( total -- )
443                         {
444                                 *p = *p * value;
445                                 p ++;
446                         }
447                 }
448
449                 mlt_properties_set( properties, "meta.volume", NULL );
450         }
451
452         return 0;
453 }
454
455 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
456 {
457         int16_t *pcm = NULL;
458         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
459         mlt_audio_format format = mlt_audio_pcm;
460         int frequency = 32000; // lower frequency available?
461         int channels = 2;
462         double fps = mlt_properties_get_double( properties, "fps" );
463         int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
464         
465         // Get the pcm data
466         mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
467         
468         // Make an 8-bit buffer large enough to hold rendering
469         int size = w * h;
470         unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
471         if ( bitmap != NULL )
472                 memset( bitmap, 0, size );
473         mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
474         
475         // Render vertical lines
476         int16_t *ubound = pcm + samples * channels;
477         int skip = samples / w - 1;
478         int i, j, k;
479         
480         // Iterate sample stream and along x coordinate
481         for ( i = 0; i < w && pcm < ubound; i++ )
482         {
483                 // pcm data has channels interleaved
484                 for ( j = 0; j < channels; j++ )
485                 {
486                         // Determine sample's magnitude from 2s complement;
487                         int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
488                         // The height of a line is the ratio of the magnitude multiplied by 
489                         // half the vertical resolution
490                         int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
491                         // Determine the starting y coordinate - left channel above center,
492                         // right channel below - currently assumes 2 channels
493                         int displacement = ( h / 2 ) - ( 1 - j ) * height;
494                         // Position buffer pointer using y coordinate, stride, and x coordinate
495                         unsigned char *p = &bitmap[ i + displacement * w ];
496                         
497                         // Draw vertical line
498                         for ( k = 0; k < height; k++ )
499                                 p[ w * k ] = 0xFF;
500                         
501                         pcm++;
502                 }
503                 pcm += skip * channels;
504         }
505
506         return bitmap;
507 }
508
509 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
510 {
511         if ( this != NULL )
512                 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
513         return NULL;
514 }
515
516 void mlt_frame_close( mlt_frame this )
517 {
518         if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
519         {
520                 mlt_deque_close( this->stack_image );
521                 mlt_deque_close( this->stack_audio );
522                 while( mlt_deque_peek_back( this->stack_service ) )
523                         mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
524                 mlt_deque_close( this->stack_service );
525                 mlt_properties_close( &this->parent );
526                 free( this );
527         }
528 }
529
530 /***** convenience functions *****/
531
532 int mlt_convert_yuv422_to_rgb24a( uint8_t *yuv, uint8_t *rgba, unsigned int total )
533 {
534         int ret = 0;
535         int yy, uu, vv;
536         int r,g,b;
537         total /= 2;
538         while (total--) 
539         {
540                 yy = yuv[0];
541                 uu = yuv[1];
542                 vv = yuv[3];
543                 YUV2RGB(yy, uu, vv, r, g, b);
544                 rgba[0] = r;
545                 rgba[1] = g;
546                 rgba[2] = b;
547                 rgba[3] = 255;
548                 yy = yuv[2];
549                 YUV2RGB(yy, uu, vv, r, g, b);
550                 rgba[4] = r;
551                 rgba[5] = g;
552                 rgba[6] = b;
553                 rgba[7] = 255;
554                 yuv += 4;
555                 rgba += 8;
556         }
557         return ret;
558 }
559
560 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
561 {
562         int ret = 0;
563         register int y0, y1, u0, u1, v0, v1;
564         register int r, g, b;
565         register uint8_t *d = yuv;
566         register int i, j;
567
568         if ( alpha )
569         for ( i = 0; i < height; i++ )
570         {
571                 register uint8_t *s = rgba + ( stride * i );
572                 for ( j = 0; j < ( width / 2 ); j++ )
573                 {
574                         r = *s++;
575                         g = *s++;
576                         b = *s++;
577                         *alpha++ = *s++;
578                         RGB2YUV (r, g, b, y0, u0 , v0);
579                         r = *s++;
580                         g = *s++;
581                         b = *s++;
582                         *alpha++ = *s++;
583                         RGB2YUV (r, g, b, y1, u1 , v1);
584                         *d++ = y0;
585                         *d++ = (u0+u1) >> 1;
586                         *d++ = y1;
587                         *d++ = (v0+v1) >> 1;
588                 }
589                 if ( width % 2 )
590                 {
591                         r = *s++;
592                         g = *s++;
593                         b = *s++;
594                         *alpha++ = *s++;
595                         RGB2YUV (r, g, b, y0, u0 , v0);
596                         *d++ = y0;
597                         *d++ = u0;
598                 }
599         }
600         else
601         for ( i = 0; i < height; i++ )
602         {
603                 register uint8_t *s = rgba + ( stride * i );
604                 for ( j = 0; j < ( width / 2 ); j++ )
605                 {
606                         r = *s++;
607                         g = *s++;
608                         b = *s++;
609                         s++;
610                         RGB2YUV (r, g, b, y0, u0 , v0);
611                         r = *s++;
612                         g = *s++;
613                         b = *s++;
614                         s++;
615                         RGB2YUV (r, g, b, y1, u1 , v1);
616                         *d++ = y0;
617                         *d++ = (u0+u1) >> 1;
618                         *d++ = y1;
619                         *d++ = (v0+v1) >> 1;
620                 }
621                 if ( width % 2 )
622                 {
623                         r = *s++;
624                         g = *s++;
625                         b = *s++;
626                         s++;
627                         RGB2YUV (r, g, b, y0, u0 , v0);
628                         *d++ = y0;
629                         *d++ = u0;
630                 }
631         }
632
633         return ret;
634 }
635
636 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
637 {
638         int ret = 0;
639         register int y0, y1, u0, u1, v0, v1;
640         register int r, g, b;
641         register uint8_t *d = yuv;
642         register int i, j;
643
644         for ( i = 0; i < height; i++ )
645         {
646                 register uint8_t *s = rgb + ( stride * i );
647                 for ( j = 0; j < ( width / 2 ); j++ )
648                 {
649                         r = *s++;
650                         g = *s++;
651                         b = *s++;
652                         RGB2YUV (r, g, b, y0, u0 , v0);
653                         r = *s++;
654                         g = *s++;
655                         b = *s++;
656                         RGB2YUV (r, g, b, y1, u1 , v1);
657                         *d++ = y0;
658                         *d++ = (u0+u1) >> 1;
659                         *d++ = y1;
660                         *d++ = (v0+v1) >> 1;
661                 }
662                 if ( width % 2 )
663                 {
664                         r = *s++;
665                         g = *s++;
666                         b = *s++;
667                         RGB2YUV (r, g, b, y0, u0 , v0);
668                         *d++ = y0;
669                         *d++ = u0;
670                 }
671         }
672         return ret;
673 }
674
675 int mlt_convert_bgr24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
676 {
677         int ret = 0;
678         register int y0, y1, u0, u1, v0, v1;
679         register int r, g, b;
680         register uint8_t *d = yuv;
681         register int i, j;
682
683         if ( alpha )
684         for ( i = 0; i < height; i++ )
685         {
686                 register uint8_t *s = rgba + ( stride * i );
687                 for ( j = 0; j < ( width / 2 ); j++ )
688                 {
689                         b = *s++;
690                         g = *s++;
691                         r = *s++;
692                         *alpha++ = *s++;
693                         RGB2YUV (r, g, b, y0, u0 , v0);
694                         b = *s++;
695                         g = *s++;
696                         r = *s++;
697                         *alpha++ = *s++;
698                         RGB2YUV (r, g, b, y1, u1 , v1);
699                         *d++ = y0;
700                         *d++ = (u0+u1) >> 1;
701                         *d++ = y1;
702                         *d++ = (v0+v1) >> 1;
703                 }
704                 if ( width % 2 )
705                 {
706                         b = *s++;
707                         g = *s++;
708                         r = *s++;
709                         *alpha++ = *s++;
710                         RGB2YUV (r, g, b, y0, u0 , v0);
711                         *d++ = y0;
712                         *d++ = u0;
713                 }
714         }
715         else
716         for ( i = 0; i < height; i++ )
717         {
718                 register uint8_t *s = rgba + ( stride * i );
719                 for ( j = 0; j < ( width / 2 ); j++ )
720                 {
721                         b = *s++;
722                         g = *s++;
723                         r = *s++;
724                         s++;
725                         RGB2YUV (r, g, b, y0, u0 , v0);
726                         b = *s++;
727                         g = *s++;
728                         r = *s++;
729                         s++;
730                         RGB2YUV (r, g, b, y1, u1 , v1);
731                         *d++ = y0;
732                         *d++ = (u0+u1) >> 1;
733                         *d++ = y1;
734                         *d++ = (v0+v1) >> 1;
735                 }
736                 if ( width % 2 )
737                 {
738                         b = *s++;
739                         g = *s++;
740                         r = *s++;
741                         s++;
742                         RGB2YUV (r, g, b, y0, u0 , v0);
743                         *d++ = y0;
744                         *d++ = u0;
745                 }
746         }
747         return ret;
748 }
749
750 int mlt_convert_bgr24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
751 {
752         int ret = 0;
753         register int y0, y1, u0, u1, v0, v1;
754         register int r, g, b;
755         register uint8_t *d = yuv;
756         register int i, j;
757
758         for ( i = 0; i < height; i++ )
759         {
760                 register uint8_t *s = rgb + ( stride * i );
761                 for ( j = 0; j < ( width / 2 ); j++ )
762                 {
763                         b = *s++;
764                         g = *s++;
765                         r = *s++;
766                         RGB2YUV (r, g, b, y0, u0 , v0);
767                         b = *s++;
768                         g = *s++;
769                         r = *s++;
770                         RGB2YUV (r, g, b, y1, u1 , v1);
771                         *d++ = y0;
772                         *d++ = (u0+u1) >> 1;
773                         *d++ = y1;
774                         *d++ = (v0+v1) >> 1;
775                 }
776                 if ( width % 2 )
777                 {
778                         b = *s++;
779                         g = *s++;
780                         r = *s++;
781                         RGB2YUV (r, g, b, y0, u0 , v0);
782                         *d++ = y0;
783                         *d++ = u0;
784                 }
785         }
786         return ret;
787 }
788
789 int mlt_convert_argb_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
790 {
791         int ret = 0;
792         register int y0, y1, u0, u1, v0, v1;
793         register int r, g, b;
794         register uint8_t *d = yuv;
795         register int i, j;
796
797         if ( alpha )
798         for ( i = 0; i < height; i++ )
799         {
800                 register uint8_t *s = rgba + ( stride * i );
801                 for ( j = 0; j < ( width / 2 ); j++ )
802                 {
803                         *alpha++ = *s++;
804                         r = *s++;
805                         g = *s++;
806                         b = *s++;
807                         RGB2YUV (r, g, b, y0, u0 , v0);
808                         *alpha++ = *s++;
809                         r = *s++;
810                         g = *s++;
811                         b = *s++;
812                         RGB2YUV (r, g, b, y1, u1 , v1);
813                         *d++ = y0;
814                         *d++ = (u0+u1) >> 1;
815                         *d++ = y1;
816                         *d++ = (v0+v1) >> 1;
817                 }
818                 if ( width % 2 )
819                 {
820                         *alpha++ = *s++;
821                         r = *s++;
822                         g = *s++;
823                         b = *s++;
824                         RGB2YUV (r, g, b, y0, u0 , v0);
825                         *d++ = y0;
826                         *d++ = u0;
827                 }
828         }
829         else
830         for ( i = 0; i < height; i++ )
831         {
832                 register uint8_t *s = rgba + ( stride * i );
833                 for ( j = 0; j < ( width / 2 ); j++ )
834                 {
835                         s++;
836                         r = *s++;
837                         g = *s++;
838                         b = *s++;
839                         RGB2YUV (r, g, b, y0, u0 , v0);
840                         s++;
841                         r = *s++;
842                         g = *s++;
843                         b = *s++;
844                         RGB2YUV (r, g, b, y1, u1 , v1);
845                         *d++ = y0;
846                         *d++ = (u0+u1) >> 1;
847                         *d++ = y1;
848                         *d++ = (v0+v1) >> 1;
849                 }
850                 if ( width % 2 )
851                 {
852                         s++;
853                         r = *s++;
854                         g = *s++;
855                         b = *s++;
856                         RGB2YUV (r, g, b, y0, u0 , v0);
857                         *d++ = y0;
858                         *d++ = u0;
859                 }
860         }
861         return ret;
862 }
863
864 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
865 {
866         int ret = 0;
867         register int i, j;
868
869         int half = width >> 1;
870
871         uint8_t *Y = yuv420p;
872         uint8_t *U = Y + width * height;
873         uint8_t *V = U + width * height / 4;
874
875         register uint8_t *d = yuv;
876
877         for ( i = 0; i < height; i++ )
878         {
879                 register uint8_t *u = U + ( i / 2 ) * ( half );
880                 register uint8_t *v = V + ( i / 2 ) * ( half );
881
882                 for ( j = 0; j < half; j++ )
883                 {
884                         *d ++ = *Y ++;
885                         *d ++ = *u ++;
886                         *d ++ = *Y ++;
887                         *d ++ = *v ++;
888                 }
889         }
890         return ret;
891 }
892
893 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight, uint8_t alpha_value )
894 {
895         uint8_t *output = NULL;
896
897         if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
898         {
899                 uint8_t *out_line;
900                 int offset_x = ( owidth - iwidth ) / 2;
901                 int offset_y = ( oheight - iheight ) / 2;
902                 int iused = iwidth;
903
904                 output = mlt_pool_alloc( owidth * oheight );
905                 memset( output, alpha_value, owidth * oheight );
906
907                 offset_x -= offset_x % 2;
908
909                 out_line = output + offset_y * owidth;
910                 out_line += offset_x;
911
912                 // Loop for the entirety of our output height.
913                 while ( iheight -- )
914                 {
915                         // We're in the input range for this row.
916                         memcpy( out_line, input, iused );
917
918                         // Move to next input line
919                         input += iwidth;
920
921                 // Move to next output line
922                 out_line += owidth;
923                 }
924         }
925
926         return output;
927 }
928
929 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
930 {
931         // Calculate strides
932         int istride = iwidth * 2;
933         int ostride = owidth * 2;
934         int offset_x = ( owidth - iwidth );
935         int offset_y = ( oheight - iheight ) / 2;
936         uint8_t *in_line = input;
937         uint8_t *out_line;
938         int size = owidth * oheight;
939         uint8_t *p = output;
940
941         // Optimisation point
942         if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
943         {
944                 return;
945         }
946         else if ( iwidth == owidth && iheight == oheight )
947         {
948                 memcpy( output, input, iheight * istride );
949                 return;
950         }
951
952         while( size -- )
953         {
954                 *p ++ = 16;
955                 *p ++ = 128;
956         }
957
958         offset_x -= offset_x % 4;
959
960         out_line = output + offset_y * ostride;
961         out_line += offset_x;
962
963         // Loop for the entirety of our output height.
964         while ( iheight -- )
965         {
966                 // We're in the input range for this row.
967                 memcpy( out_line, in_line, iwidth * 2 );
968
969                 // Move to next input line
970                 in_line += istride;
971
972                 // Move to next output line
973                 out_line += ostride;
974         }
975 }
976
977 /** A resizing function for yuv422 frames - this does not rescale, but simply
978         resizes. It assumes yuv422 images available on the frame so use with care.
979 */
980
981 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
982 {
983         // Get properties
984         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
985
986         // Get the input image, width and height
987         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
988         uint8_t *alpha = mlt_frame_get_alpha_mask( this );
989
990         int iwidth = mlt_properties_get_int( properties, "width" );
991         int iheight = mlt_properties_get_int( properties, "height" );
992
993         // If width and height are correct, don't do anything
994         if ( iwidth != owidth || iheight != oheight )
995         {
996                 uint8_t alpha_value = mlt_properties_get_int( properties, "resize_alpha" );
997
998                 // Create the output image
999                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
1000
1001                 // Call the generic resize
1002                 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
1003
1004                 // Now update the frame
1005                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
1006                 mlt_properties_set_int( properties, "width", owidth );
1007                 mlt_properties_set_int( properties, "height", oheight );
1008
1009                 // We should resize the alpha too
1010                 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight, alpha_value );
1011                 if ( alpha != NULL )
1012                 {
1013                         mlt_properties_set_data( properties, "alpha", alpha, owidth * oheight, ( mlt_destructor )mlt_pool_release, NULL );
1014                         this->get_alpha_mask = NULL;
1015                 }
1016
1017                 // Return the output
1018                 return output;
1019         }
1020         // No change, return input
1021         return input;
1022 }
1023
1024 /** A rescaling function for yuv422 frames - low quality, and provided for testing
1025         only. It assumes yuv422 images available on the frame so use with care.
1026 */
1027
1028 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
1029 {
1030         // Get properties
1031         mlt_properties properties = MLT_FRAME_PROPERTIES( this );
1032
1033         // Get the input image, width and height
1034         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
1035         int iwidth = mlt_properties_get_int( properties, "width" );
1036         int iheight = mlt_properties_get_int( properties, "height" );
1037
1038         // If width and height are correct, don't do anything
1039         if ( iwidth != owidth || iheight != oheight )
1040         {
1041                 // Create the output image
1042                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
1043
1044                 // Calculate strides
1045                 int istride = iwidth * 2;
1046                 int ostride = owidth * 2;
1047
1048                 iwidth = iwidth - ( iwidth % 4 );
1049
1050                 // Derived coordinates
1051                 int dy, dx;
1052
1053         // Calculate ranges
1054         int out_x_range = owidth / 2;
1055         int out_y_range = oheight / 2;
1056         int in_x_range = iwidth / 2;
1057         int in_y_range = iheight / 2;
1058
1059         // Output pointers
1060         register uint8_t *out_line = output;
1061         register uint8_t *out_ptr;
1062
1063         // Calculate a middle pointer
1064         uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
1065         uint8_t *in_line;
1066
1067                 // Generate the affine transform scaling values
1068                 register int scale_width = ( iwidth << 16 ) / owidth;
1069                 register int scale_height = ( iheight << 16 ) / oheight;
1070                 register int base = 0;
1071
1072                 int outer = out_x_range * scale_width;
1073                 int bottom = out_y_range * scale_height;
1074
1075         // Loop for the entirety of our output height.
1076         for ( dy = - bottom; dy < bottom; dy += scale_height )
1077         {
1078                 // Start at the beginning of the line
1079                 out_ptr = out_line;
1080         
1081                 // Pointer to the middle of the input line
1082                 in_line = in_middle + ( dy >> 16 ) * istride;
1083
1084                 // Loop for the entirety of our output row.
1085                 for ( dx = - outer; dx < outer; dx += scale_width )
1086                 {
1087                                 base = dx >> 15;
1088                                 base &= 0xfffffffe;
1089                                 *out_ptr ++ = *( in_line + base );
1090                                 base &= 0xfffffffc;
1091                                 *out_ptr ++ = *( in_line + base + 1 );
1092                                 dx += scale_width;
1093                                 base = dx >> 15;
1094                                 base &= 0xfffffffe;
1095                                 *out_ptr ++ = *( in_line + base );
1096                                 base &= 0xfffffffc;
1097                                 *out_ptr ++ = *( in_line + base + 3 );
1098                 }
1099
1100                 // Move to next output line
1101                 out_line += ostride;
1102         }
1103
1104                 // Now update the frame
1105                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
1106                 mlt_properties_set_int( properties, "width", owidth );
1107                 mlt_properties_set_int( properties, "height", oheight );
1108
1109                 // Return the output
1110                 return output;
1111         }
1112
1113         // No change, return input
1114         return input;
1115 }
1116
1117 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 )
1118 {
1119         int ret = 0;
1120         int16_t *src, *dest;
1121         int frequency_src = *frequency, frequency_dest = *frequency;
1122         int channels_src = *channels, channels_dest = *channels;
1123         int samples_src = *samples, samples_dest = *samples;
1124         int i, j;
1125         double d = 0, s = 0;
1126
1127         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1128         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1129
1130         int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1131         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1132         if ( silent )
1133                 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1134
1135         silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1136         mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1137         if ( silent )
1138                 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1139
1140         if ( channels_src > 6 )
1141                 channels_src = 0;
1142         if ( channels_dest > 6 )
1143                 channels_dest = 0;
1144         if ( samples_src > 4000 )
1145                 samples_src = 0;
1146         if ( samples_dest > 4000 )
1147                 samples_dest = 0;
1148
1149         // determine number of samples to process
1150         *samples = samples_src < samples_dest ? samples_src : samples_dest;
1151         *channels = channels_src < channels_dest ? channels_src : channels_dest;
1152         *buffer = dest;
1153         *frequency = frequency_dest;
1154
1155         // Compute a smooth ramp over start to end
1156         float weight = weight_start;
1157         float weight_step = ( weight_end - weight_start ) / *samples;
1158
1159         if ( src == dest )
1160         {
1161                 *samples = samples_src;
1162                 *channels = channels_src;
1163                 *buffer = src;
1164                 *frequency = frequency_src;
1165                 return ret;
1166         }
1167
1168         // Mixdown
1169         for ( i = 0; i < *samples; i++ )
1170         {
1171                 for ( j = 0; j < *channels; j++ )
1172                 {
1173                         if ( j < channels_dest )
1174                                 d = (double) dest[ i * channels_dest + j ];
1175                         if ( j < channels_src )
1176                                 s = (double) src[ i * channels_src + j ];
1177                         dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1178                 }
1179                 weight += weight_step;
1180         }
1181
1182         return ret;
1183 }
1184
1185 // Replacement for broken mlt_frame_audio_mix - this filter uses an inline low pass filter
1186 // to allow mixing without volume hacking
1187 int mlt_frame_combine_audio( mlt_frame this, mlt_frame that, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1188 {
1189         int ret = 0;
1190         int16_t *src, *dest;
1191         int frequency_src = *frequency, frequency_dest = *frequency;
1192         int channels_src = *channels, channels_dest = *channels;
1193         int samples_src = *samples, samples_dest = *samples;
1194         int i, j;
1195         double vp[ 6 ];
1196         double b_weight = 1.0;
1197
1198         if ( mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "meta.mixdown" ) )
1199                 b_weight = 1.0 - mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "meta.volume" );
1200
1201         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
1202         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
1203
1204         int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
1205         mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
1206         if ( silent )
1207                 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
1208
1209         silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
1210         mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
1211         if ( silent )
1212                 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
1213
1214         if ( src == dest )
1215         {
1216                 *samples = samples_src;
1217                 *channels = channels_src;
1218                 *buffer = src;
1219                 *frequency = frequency_src;
1220                 return ret;
1221         }
1222
1223         // determine number of samples to process
1224         *samples = samples_src < samples_dest ? samples_src : samples_dest;
1225         *channels = channels_src < channels_dest ? channels_src : channels_dest;
1226         *buffer = dest;
1227         *frequency = frequency_dest;
1228
1229         for ( j = 0; j < *channels; j++ )
1230                 vp[ j ] = ( double )dest[ j ];
1231
1232         double Fc = 0.5;
1233         double B = exp(-2.0 * M_PI * Fc);
1234         double A = 1.0 - B;
1235         double v;
1236         
1237         for ( i = 0; i < *samples; i++ )
1238         {
1239                 for ( j = 0; j < *channels; j++ )
1240                 {
1241                         v = ( double )( b_weight * dest[ i * channels_dest + j ] + src[ i * channels_src + j ] );
1242                         v = v < -32767 ? -32767 : v > 32768 ? 32768 : v;
1243                         vp[ j ] = dest[ i * channels_dest + j ] = ( int16_t )( v * A + vp[ j ] * B );
1244                 }
1245         }
1246
1247         return ret;
1248 }
1249
1250 /* Will this break when mlt_position is converted to double? -Zach */
1251 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1252 {
1253         int samples = 0;
1254
1255         if ( ( int )( fps * 100 ) == 2997 )
1256         {
1257                 samples = frequency / 30;
1258
1259                 switch ( frequency )
1260                 {
1261                         case 48000:
1262                                 if ( position % 5 != 0 )
1263                                         samples += 2;
1264                                 break;
1265                         case 44100:
1266                                 if ( position % 300 == 0 )
1267                                         samples = 1471;
1268                                 else if ( position % 30 == 0 )
1269                                         samples = 1470;
1270                                 else if ( position % 2 == 0 )
1271                                         samples = 1472;
1272                                 else
1273                                         samples = 1471;
1274                                 break;
1275                         case 32000:
1276                                 if ( position % 30 == 0 )
1277                                         samples = 1068;
1278                                 else if ( position % 29 == 0 )
1279                                         samples = 1067;
1280                                 else if ( position % 4 == 2 )
1281                                         samples = 1067;
1282                                 else
1283                                         samples = 1068;
1284                                 break;
1285                         default:
1286                                 samples = 0;
1287                 }
1288         }
1289         else if ( fps != 0 )
1290         {
1291                 samples = frequency / fps;
1292         }
1293
1294         return samples;
1295 }
1296
1297 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
1298 {
1299         int64_t samples = 0;
1300
1301         // TODO: Correct rules for NTSC and drop the * 100 hack
1302         if ( ( int )( fps * 100 ) == 2997 )
1303         {
1304                 samples = ( ( double )( frame * frequency ) / 30 );
1305                 switch( frequency )
1306                 {
1307                         case 48000:
1308                                 samples += 2 * ( frame / 5 );
1309                                 break;
1310                         case 44100:
1311                                 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
1312                                 break;
1313                         case 32000:
1314                                 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
1315                                 break;
1316                 }
1317         }
1318         else if ( fps != 0 )
1319         {
1320                 samples = ( ( frame * frequency ) / ( int )fps );
1321         }
1322
1323         return samples;
1324 }