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_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
202 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
203 mlt_properties_set_int( properties, "width", *width );
204 mlt_properties_set_int( properties, "height", *height );
208 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
209 mlt_frame_get_image( this, buffer, format, width, height, writable );
218 *width = *width == 0 ? 720 : *width;
219 *height = *height == 0 ? 576 : *height;
220 size = *width * *height;
222 mlt_properties_set_int( properties, "width", *width );
223 mlt_properties_set_int( properties, "height", *height );
231 case mlt_image_rgb24:
234 *buffer = mlt_pool_alloc( size );
236 memset( *buffer, 255, size );
238 case mlt_image_rgb24a:
241 *buffer = mlt_pool_alloc( size );
243 memset( *buffer, 255, size );
245 case mlt_image_yuv422:
248 *buffer = mlt_pool_alloc( size );
251 while ( p != NULL && p != q )
257 case mlt_image_yuv420p:
259 *buffer = mlt_pool_alloc( size );
261 memset( *buffer, 255, size );
265 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
266 mlt_properties_set_int( properties, "test_image", 1 );
272 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
274 if ( this->get_alpha_mask != NULL )
275 return this->get_alpha_mask( this );
279 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
281 mlt_properties properties = mlt_frame_properties( this );
283 if ( this->get_audio != NULL )
285 return this->get_audio( this, buffer, format, frequency, channels, samples );
290 *samples = *samples <= 0 ? 1920 : *samples;
291 *channels = *channels <= 0 ? 2 : *channels;
292 *frequency = *frequency <= 0 ? 48000 : *frequency;
293 size = *samples * *channels * sizeof( int16_t );
294 *buffer = mlt_pool_alloc( size );
295 if ( *buffer != NULL )
296 memset( *buffer, 0, size );
297 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
298 mlt_properties_set_int( properties, "test_audio", 1 );
303 void mlt_frame_close( mlt_frame this )
305 mlt_properties_close( &this->parent );
309 /***** convenience functions *****/
310 #define RGB2YUV(r, g, b, y, u, v)\
311 y = (306*r + 601*g + 117*b) >> 10;\
312 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
313 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
317 y = y > 255 ? 255 : y;\
318 u = u > 255 ? 255 : u;\
319 v = v > 255 ? 255 : v
321 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
324 register int y0, y1, u0, u1, v0, v1;
325 register int r, g, b;
326 register uint8_t *d = yuv;
329 for ( i = 0; i < height; i++ )
331 register uint8_t *s = rgba + ( stride * i );
332 for ( j = 0; j < ( width / 2 ); j++ )
338 RGB2YUV (r, g, b, y0, u0 , v0);
343 RGB2YUV (r, g, b, y1, u1 , v1);
355 RGB2YUV (r, g, b, y0, u0 , v0);
363 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
366 register int y0, y1, u0, u1, v0, v1;
367 register int r, g, b;
368 register uint8_t *d = yuv;
371 for ( i = 0; i < height; i++ )
373 register uint8_t *s = rgb + ( stride * i );
374 for ( j = 0; j < ( width / 2 ); j++ )
379 RGB2YUV (r, g, b, y0, u0 , v0);
383 RGB2YUV (r, g, b, y1, u1 , v1);
394 RGB2YUV (r, g, b, y0, u0 , v0);
402 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
407 int half = width >> 1;
409 uint8_t *Y = yuv420p;
410 uint8_t *U = Y + width * height;
411 uint8_t *V = U + width * height / 4;
413 register uint8_t *d = yuv;
415 for ( i = 0; i < height; i++ )
417 register uint8_t *u = U + ( i / 2 ) * ( half );
418 register uint8_t *v = V + ( i / 2 ) * ( half );
420 for ( j = 0; j < half; j++ )
431 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
434 int istride = iwidth * 2;
435 int ostride = owidth * 2;
437 iwidth = iwidth - ( iwidth % 4 );
438 owidth = owidth - ( owidth % 4 );
439 iheight = iheight - ( iheight % 2 );
440 oheight = oheight - ( oheight % 2 );
442 // Optimisation point
443 if ( iwidth == owidth && iheight == oheight )
444 memcpy( output, input, iheight * istride );
446 // Coordinates (0,0 is middle of output)
450 int out_x_range = owidth / 2;
451 int out_y_range = oheight / 2;
452 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
453 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
456 uint8_t *out_line = output;
457 uint8_t *out_ptr = out_line;
459 // Calculate a middle and possibly invalid pointer in the input
460 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
461 int in_line = - in_y_range * istride - in_x_range * 2;
465 // Fill whole section with black
466 y = out_y_range - ( iheight / 2 );
467 int blank_elements = ostride * y / 2;
468 elements = blank_elements;
469 while ( elements -- )
475 int active_width = 2 * iwidth;
476 int inactive_width = out_x_range - in_x_range;
478 // Loop for the entirety of our output height.
481 // Start at the beginning of the line
484 // Fill the outer part with black
485 elements = inactive_width;
486 while ( elements -- )
492 // We're in the input range for this row.
493 memcpy( out_ptr, in_middle + in_line, active_width );
494 out_ptr += active_width;
496 // Fill the outer part with black
497 elements = inactive_width;
498 while ( elements -- )
504 // Move to next input line
507 // Move to next output line
511 // Fill whole section with black
512 elements = blank_elements;
513 while ( elements -- )
520 /** A resizing function for yuv422 frames - this does not rescale, but simply
521 resizes. It assumes yuv422 images available on the frame so use with care.
524 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
527 mlt_properties properties = mlt_frame_properties( this );
529 // Get the input image, width and height
530 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
531 int iwidth = mlt_properties_get_int( properties, "width" );
532 int iheight = mlt_properties_get_int( properties, "height" );
534 // If width and height are correct, don't do anything
535 if ( iwidth != owidth || iheight != oheight )
537 // Create the output image
538 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
540 // Call the generic resize
541 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
543 // Now update the frame
544 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
545 mlt_properties_set_int( properties, "width", owidth );
546 mlt_properties_set_int( properties, "height", oheight );
551 // No change, return input
555 /** A rescaling function for yuv422 frames - low quality, and provided for testing
556 only. It assumes yuv422 images available on the frame so use with care.
559 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
562 mlt_properties properties = mlt_frame_properties( this );
564 // Get the input image, width and height
565 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
566 int iwidth = mlt_properties_get_int( properties, "width" );
567 int iheight = mlt_properties_get_int( properties, "height" );
569 // If width and height are correct, don't do anything
570 if ( iwidth != owidth || iheight != oheight )
572 // Create the output image
573 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
576 int istride = iwidth * 2;
577 int ostride = owidth * 2;
579 iwidth = iwidth - ( iwidth % 4 );
581 // Coordinates (0,0 is middle of output)
584 // Derived coordinates
588 int out_x_range = owidth / 2;
589 int out_y_range = oheight / 2;
590 int in_x_range = iwidth / 2;
591 int in_y_range = iheight / 2;
594 uint8_t *out_line = output;
597 // Calculate a middle pointer
598 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
602 // Generate the affine transform scaling values
603 int scale_width = ( iwidth << 16 ) / owidth;
604 int scale_height = ( iheight << 16 ) / oheight;
606 // Loop for the entirety of our output height.
607 for ( y = - out_y_range; y < out_y_range ; y ++ )
609 // Calculate the derived y value
610 dy = ( scale_height * y ) >> 16;
612 // Start at the beginning of the line
615 // Pointer to the middle of the input line
616 in_line = in_middle + dy * istride;
618 // Loop for the entirety of our output row.
619 for ( x = - out_x_range; x < out_x_range; x += 1 )
621 // Calculated the derived x
622 dx = ( scale_width * x ) >> 16;
624 // We're in the input range for this row.
625 in_ptr = in_line + ( dx << 1 );
626 *out_ptr ++ = *in_ptr ++;
627 in_ptr = in_line + ( ( dx >> 1 ) << 2 ) + ( ( x & 1 ) << 1 ) + 1;
628 *out_ptr ++ = *in_ptr;
631 // Move to next output line
635 // Now update the frame
636 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
637 mlt_properties_set_int( properties, "width", owidth );
638 mlt_properties_set_int( properties, "height", oheight );
644 // No change, return input
648 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 )
651 int16_t *p_src, *p_dest;
653 //static int16_t *extra_src = NULL, *extra_dest = NULL;
654 static int extra_src_samples = 0, extra_dest_samples = 0;
655 int frequency_src = *frequency, frequency_dest = *frequency;
656 int channels_src = *channels, channels_dest = *channels;
657 int samples_src = *samples, samples_dest = *samples;
661 mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest );
662 //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" ) );
663 mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src );
664 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
667 if ( channels_src > 6 )
669 if ( channels_dest > 6 )
671 if ( samples_src > 4000 )
673 if ( samples_dest > 4000 )
677 // Append new samples to leftovers
678 if ( extra_dest_samples > 0 )
680 fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples );
681 dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest );
682 memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest );
686 if ( extra_src_samples > 0 )
688 fprintf( stderr, "prepending %d samples to src\n", extra_src_samples );
689 src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src );
690 memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src );
696 // determine number of samples to process
697 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
698 *samples = samples_src + extra_src_samples;
699 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
700 *samples = samples_dest + extra_dest_samples;
702 *channels = channels_src < channels_dest ? channels_src : channels_dest;
704 *frequency = frequency_dest;
706 // Compute a smooth ramp over start to end
707 float weight = weight_start;
708 float weight_step = ( weight_end - weight_start ) / *samples;
711 for ( i = 0; i < *samples; i++ )
713 for ( j = 0; j < *channels; j++ )
715 if ( j < channels_dest )
716 d = (double) dest[ i * channels_dest + j ];
717 if ( j < channels_src )
718 s = (double) src[ i * channels_src + j ];
719 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
721 weight += weight_step;
724 // We have to copy --sigh
725 if ( dest != p_dest )
726 memcpy( p_dest, dest, *samples * 2 * *channels );
729 // Store the leftovers
730 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
732 extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples );
733 size_t size = extra_dest_samples * 2 * channels_dest;
734 fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples );
737 extra_dest = malloc( size );
739 memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size );
741 extra_dest_samples = 0;
743 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
745 extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples );
746 size_t size = extra_src_samples * 2 * channels_src;
747 fprintf( stderr, "storing %d samples from src\n", extra_dest_samples );
750 extra_src = malloc( size );
752 memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size );
754 extra_src_samples = 0;
761 int mlt_sample_calculator( float fps, int frequency, int64_t position )
765 if ( fps > 29 && fps <= 30 )
767 samples = frequency / 30;
772 if ( position % 5 != 0 )
776 if ( position % 300 == 0 )
778 else if ( position % 30 == 0 )
780 else if ( position % 2 == 0 )
786 if ( position % 30 == 0 )
788 else if ( position % 29 == 0 )
790 else if ( position % 4 == 2 )
801 samples = frequency / fps;