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>
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.
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.
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.
22 #include "mlt_frame.h"
23 #include "mlt_producer.h"
28 /** Constructor for a frame.
31 mlt_frame mlt_frame_init( )
34 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
38 // Get the normalisation
39 char *normalisation = getenv( "MLT_NORMALISATION" );
41 // Initialise the properties
42 mlt_properties properties = &this->parent;
43 mlt_properties_init( properties, this );
45 // Set default properties on the frame
46 mlt_properties_set_position( properties, "_position", 0.0 );
47 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
49 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
51 mlt_properties_set_int( properties, "width", 720 );
52 mlt_properties_set_int( properties, "height", 576 );
53 mlt_properties_set_int( properties, "normalised_width", 720 );
54 mlt_properties_set_int( properties, "normalised_height", 576 );
58 mlt_properties_set_int( properties, "width", 720 );
59 mlt_properties_set_int( properties, "height", 480 );
60 mlt_properties_set_int( properties, "normalised_width", 720 );
61 mlt_properties_set_int( properties, "normalised_height", 480 );
64 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
65 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
66 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
73 /** Fetch the frames properties.
76 mlt_properties mlt_frame_properties( mlt_frame this )
81 /** Check if we have a way to derive something other than a test card.
84 int mlt_frame_is_test_card( mlt_frame this )
86 return mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
89 /** Check if we have a way to derive something than test audio.
92 int mlt_frame_is_test_audio( mlt_frame this )
94 return this->get_audio == NULL || mlt_properties_get_int( mlt_frame_properties( this ), "test_audio" );
97 /** Get the aspect ratio of the frame.
100 double mlt_frame_get_aspect_ratio( mlt_frame this )
102 return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
105 /** Set the aspect ratio of the frame.
108 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
110 return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value );
113 /** Get the position of this frame.
116 mlt_position mlt_frame_get_position( mlt_frame this )
118 return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
121 /** Set the position of this frame.
124 int mlt_frame_set_position( mlt_frame this, mlt_position value )
126 return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value );
129 /** Stack a get_image callback.
132 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
134 int ret = this->stack_get_image_size >= 10;
136 this->stack_get_image[ this->stack_get_image_size ++ ] = get_image;
140 /** Pop a get_image callback.
143 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
145 mlt_get_image result = NULL;
146 if ( this->stack_get_image_size > 0 )
147 result = this->stack_get_image[ -- this->stack_get_image_size ];
154 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
156 int ret = this->stack_frame_size >= 10;
158 this->stack_frame[ this->stack_frame_size ++ ] = that;
165 mlt_frame mlt_frame_pop_frame( mlt_frame this )
167 mlt_frame result = NULL;
168 if ( this->stack_frame_size > 0 )
169 result = this->stack_frame[ -- this->stack_frame_size ];
173 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
175 mlt_properties properties = mlt_frame_properties( this );
176 mlt_get_image get_image = mlt_frame_pop_get_image( this );
177 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
179 if ( get_image != NULL )
181 return get_image( this, buffer, format, width, height, writable );
183 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
185 *format = mlt_image_yuv422;
186 *buffer = mlt_properties_get_data( properties, "image", NULL );
187 *width = mlt_properties_get_int( properties, "width" );
188 *height = mlt_properties_get_int( properties, "height" );
190 else if ( producer != NULL )
192 mlt_frame test_frame = NULL;
193 mlt_service_get_frame( mlt_producer_service( producer ), &test_frame, 0 );
194 if ( test_frame != NULL )
196 mlt_properties test_properties = mlt_frame_properties( test_frame );
197 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
198 mlt_properties_set_double( test_properties, "consumer_scale", mlt_properties_get_double( properties, "consumer_scale" ) );
199 mlt_properties_set( test_properties, "rescale.interp", "nearest" );
200 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
201 mlt_properties_inherit( properties, test_properties );
202 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
203 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
204 mlt_properties_set_int( properties, "width", *width );
205 mlt_properties_set_int( properties, "height", *height );
209 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
210 mlt_frame_get_image( this, buffer, format, width, height, writable );
215 void *release = NULL;
220 *width = *width == 0 ? 720 : *width;
221 *height = *height == 0 ? 576 : *height;
222 size = *width * *height;
224 mlt_properties_set_int( properties, "width", *width );
225 mlt_properties_set_int( properties, "height", *height );
233 case mlt_image_rgb24:
236 *buffer = mlt_pool_allocate( size, &release );
238 memset( *buffer, 255, size );
240 case mlt_image_rgb24a:
243 *buffer = mlt_pool_allocate( size, &release );
245 memset( *buffer, 255, size );
247 case mlt_image_yuv422:
250 *buffer = mlt_pool_allocate( size, &release );
253 while ( p != NULL && p != q )
259 case mlt_image_yuv420p:
261 *buffer = mlt_pool_allocate( size, &release );
263 memset( *buffer, 255, size );
267 mlt_properties_set_data( properties, "image_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
268 mlt_properties_set_data( properties, "image", *buffer, size, NULL, NULL );
269 mlt_properties_set_int( properties, "test_image", 1 );
275 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
277 if ( this->get_alpha_mask != NULL )
278 return this->get_alpha_mask( this );
282 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
284 mlt_properties properties = mlt_frame_properties( this );
286 if ( this->get_audio != NULL )
288 return this->get_audio( this, buffer, format, frequency, channels, samples );
293 void *release = NULL;
294 *samples = *samples <= 0 ? 1920 : *samples;
295 *channels = *channels <= 0 ? 2 : *channels;
296 *frequency = *frequency <= 0 ? 48000 : *frequency;
297 size = *samples * *channels * sizeof( int16_t );
298 *buffer = mlt_pool_allocate( size, &release );
299 if ( *buffer != NULL )
300 memset( *buffer, 0, size );
301 mlt_properties_set_data( properties, "audio_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
302 mlt_properties_set_data( properties, "audio", *buffer, size, NULL, NULL );
303 mlt_properties_set_int( properties, "test_audio", 1 );
308 void mlt_frame_close( mlt_frame this )
310 mlt_properties_close( &this->parent );
314 /***** convenience functions *****/
315 #define RGB2YUV(r, g, b, y, u, v)\
316 y = (306*r + 601*g + 117*b) >> 10;\
317 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
318 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
322 y = y > 255 ? 255 : y;\
323 u = u > 255 ? 255 : u;\
324 v = v > 255 ? 255 : v
326 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
329 register int y0, y1, u0, u1, v0, v1;
330 register int r, g, b;
331 register uint8_t *d = yuv;
334 for ( i = 0; i < height; i++ )
336 register uint8_t *s = rgba + ( stride * i );
337 for ( j = 0; j < ( width / 2 ); j++ )
343 RGB2YUV (r, g, b, y0, u0 , v0);
348 RGB2YUV (r, g, b, y1, u1 , v1);
360 RGB2YUV (r, g, b, y0, u0 , v0);
368 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
371 register int y0, y1, u0, u1, v0, v1;
372 register int r, g, b;
373 register uint8_t *d = yuv;
376 for ( i = 0; i < height; i++ )
378 register uint8_t *s = rgb + ( stride * i );
379 for ( j = 0; j < ( width / 2 ); j++ )
384 RGB2YUV (r, g, b, y0, u0 , v0);
388 RGB2YUV (r, g, b, y1, u1 , v1);
399 RGB2YUV (r, g, b, y0, u0 , v0);
407 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
412 int half = width >> 1;
414 uint8_t *Y = yuv420p;
415 uint8_t *U = Y + width * height;
416 uint8_t *V = U + width * height / 4;
418 register uint8_t *d = yuv;
420 for ( i = 0; i < height; i++ )
422 register uint8_t *u = U + ( i / 2 ) * ( half );
423 register uint8_t *v = V + ( i / 2 ) * ( half );
425 for ( j = 0; j < half; j++ )
436 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
439 int istride = iwidth * 2;
440 int ostride = owidth * 2;
442 iwidth = iwidth - ( iwidth % 4 );
443 owidth = owidth - ( owidth % 4 );
444 iheight = iheight - ( iheight % 2 );
445 oheight = oheight - ( oheight % 2 );
447 // Optimisation point
448 if ( iwidth == owidth && iheight == oheight )
449 memcpy( output, input, iheight * istride );
451 // Coordinates (0,0 is middle of output)
455 int out_x_range = owidth / 2;
456 int out_y_range = oheight / 2;
457 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
458 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
461 uint8_t *out_line = output;
462 uint8_t *out_ptr = out_line;
464 // Calculate a middle and possibly invalid pointer in the input
465 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
466 int in_line = - in_y_range * istride - in_x_range * 2;
468 uint8_t black[ 2 ] = { 0, 128 };
471 // Fill whole section with black
472 y = out_y_range - ( iheight / 2 );
473 int blank_elements = ostride * y / 2;
474 elements = blank_elements;
475 while ( elements -- )
477 *out_line ++ = black[ 0 ];
478 *out_line ++ = black[ 1 ];
481 int active_width = 2 * iwidth;
482 int inactive_width = out_x_range - in_x_range;
484 // Loop for the entirety of our output height.
487 // Start at the beginning of the line
490 // Fill the outer part with black
491 elements = inactive_width;
492 while ( elements -- )
494 *out_ptr ++ = black[ 0 ];
495 *out_ptr ++ = black[ 1 ];
498 // We're in the input range for this row.
499 memcpy( out_ptr, in_middle + in_line, active_width );
500 out_ptr += active_width;
502 // Fill the outer part with black
503 elements = inactive_width;
504 while ( elements -- )
506 *out_ptr ++ = black[ 0 ];
507 *out_ptr ++ = black[ 1 ];
510 // Move to next input line
513 // Move to next output line
517 // Fill whole section with black
518 elements = blank_elements;
519 while ( elements -- )
521 *out_line ++ = black[ 0 ];
522 *out_line ++ = black[ 1 ];
526 /** A resizing function for yuv422 frames - this does not rescale, but simply
527 resizes. It assumes yuv422 images available on the frame so use with care.
530 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
533 mlt_properties properties = mlt_frame_properties( this );
535 // Get the input image, width and height
536 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
537 int iwidth = mlt_properties_get_int( properties, "width" );
538 int iheight = mlt_properties_get_int( properties, "height" );
540 // If width and height are correct, don't do anything
541 if ( iwidth != owidth || iheight != oheight )
543 // Create the output image
544 void *release = NULL;
545 uint8_t *output = mlt_pool_allocate( owidth * ( oheight + 1 ) * 2, &release );
547 // Call the generic resize
548 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
550 // Now update the frame
551 mlt_properties_set_data( properties, "image_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
552 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, NULL, NULL );
553 mlt_properties_set_int( properties, "width", owidth );
554 mlt_properties_set_int( properties, "height", oheight );
559 // No change, return input
563 /** A rescaling function for yuv422 frames - low quality, and provided for testing
564 only. It assumes yuv422 images available on the frame so use with care.
567 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
570 mlt_properties properties = mlt_frame_properties( this );
572 // Get the input image, width and height
573 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
574 int iwidth = mlt_properties_get_int( properties, "width" );
575 int iheight = mlt_properties_get_int( properties, "height" );
577 // If width and height are correct, don't do anything
578 if ( iwidth != owidth || iheight != oheight )
580 // Create the output image
581 void *release = NULL;
582 uint8_t *output = mlt_pool_allocate( owidth * ( oheight + 1 ) * 2, &release );
585 int istride = iwidth * 2;
586 int ostride = owidth * 2;
588 iwidth = iwidth - ( iwidth % 4 );
590 // Coordinates (0,0 is middle of output)
593 // Derived coordinates
597 int out_x_range = owidth / 2;
598 int out_y_range = oheight / 2;
599 int in_x_range = iwidth / 2;
600 int in_y_range = iheight / 2;
603 uint8_t *out_line = output;
606 // Calculate a middle pointer
607 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
611 // Generate the affine transform scaling values
612 int scale_width = ( iwidth << 16 ) / owidth;
613 int scale_height = ( iheight << 16 ) / oheight;
615 // Loop for the entirety of our output height.
616 for ( y = - out_y_range; y < out_y_range ; y ++ )
618 // Calculate the derived y value
619 dy = ( scale_height * y ) >> 16;
621 // Start at the beginning of the line
624 // Pointer to the middle of the input line
625 in_line = in_middle + dy * istride;
627 // Loop for the entirety of our output row.
628 for ( x = - out_x_range; x < out_x_range; x += 1 )
630 // Calculated the derived x
631 dx = ( scale_width * x ) >> 16;
633 // We're in the input range for this row.
634 in_ptr = in_line + ( dx << 1 );
635 *out_ptr ++ = *in_ptr ++;
636 in_ptr = in_line + ( ( dx >> 1 ) << 2 ) + ( ( x & 1 ) << 1 ) + 1;
637 *out_ptr ++ = *in_ptr;
640 // Move to next output line
644 // Now update the frame
645 mlt_properties_set_data( properties, "image_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
646 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, NULL, NULL );
647 mlt_properties_set_int( properties, "width", owidth );
648 mlt_properties_set_int( properties, "height", oheight );
654 // No change, return input
658 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 )
661 int16_t *p_src, *p_dest;
663 //static int16_t *extra_src = NULL, *extra_dest = NULL;
664 static int extra_src_samples = 0, extra_dest_samples = 0;
665 int frequency_src = *frequency, frequency_dest = *frequency;
666 int channels_src = *channels, channels_dest = *channels;
667 int samples_src = *samples, samples_dest = *samples;
671 mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest );
672 //fprintf( stderr, "mix: frame dest samples %d channels %d position %lld\n", samples_dest, channels_dest, mlt_properties_get_position( mlt_frame_properties( this ), "_position" ) );
673 mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src );
674 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
677 if ( channels_src > 6 )
679 if ( channels_dest > 6 )
681 if ( samples_src > 4000 )
683 if ( samples_dest > 4000 )
687 // Append new samples to leftovers
688 if ( extra_dest_samples > 0 )
690 fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples );
691 dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest );
692 memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest );
696 if ( extra_src_samples > 0 )
698 fprintf( stderr, "prepending %d samples to src\n", extra_src_samples );
699 src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src );
700 memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src );
706 // determine number of samples to process
707 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
708 *samples = samples_src + extra_src_samples;
709 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
710 *samples = samples_dest + extra_dest_samples;
712 *channels = channels_src < channels_dest ? channels_src : channels_dest;
714 *frequency = frequency_dest;
716 // Compute a smooth ramp over start to end
717 float weight = weight_start;
718 float weight_step = ( weight_end - weight_start ) / *samples;
721 for ( i = 0; i < *samples; i++ )
723 for ( j = 0; j < *channels; j++ )
725 if ( j < channels_dest )
726 d = (double) dest[ i * channels_dest + j ];
727 if ( j < channels_src )
728 s = (double) src[ i * channels_src + j ];
729 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
731 weight += weight_step;
734 // We have to copy --sigh
735 if ( dest != p_dest )
736 memcpy( p_dest, dest, *samples * 2 * *channels );
739 // Store the leftovers
740 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
742 extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples );
743 size_t size = extra_dest_samples * 2 * channels_dest;
744 fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples );
747 extra_dest = malloc( size );
749 memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size );
751 extra_dest_samples = 0;
753 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
755 extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples );
756 size_t size = extra_src_samples * 2 * channels_src;
757 fprintf( stderr, "storing %d samples from src\n", extra_dest_samples );
760 extra_src = malloc( size );
762 memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size );
764 extra_src_samples = 0;
771 int mlt_sample_calculator( float fps, int frequency, int64_t position )
775 if ( fps > 29 && fps <= 30 )
777 samples = frequency / 30;
782 if ( position % 5 != 0 )
786 if ( position % 300 == 0 )
788 else if ( position % 30 == 0 )
790 else if ( position % 2 == 0 )
796 if ( position % 30 == 0 )
798 else if ( position % 29 == 0 )
800 else if ( position % 4 == 2 )
811 samples = frequency / fps;