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