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"
29 mlt_image_format vfmt;
34 mlt_audio_format afmt;
39 static frame_test test_card = { mlt_image_none, 0, 0, NULL, NULL, mlt_audio_none, NULL };
41 /** Constructor for a frame.
44 mlt_frame mlt_frame_init( )
47 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
51 // Initialise the properties
52 mlt_properties properties = &this->parent;
53 mlt_properties_init( properties, this );
55 // Set default properties on the frame
56 mlt_properties_set_position( properties, "position", 0.0 );
57 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
58 mlt_properties_set_int( properties, "width", 720 );
59 mlt_properties_set_int( properties, "height", 576 );
60 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
61 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
62 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
67 /** Fetch the frames properties.
70 mlt_properties mlt_frame_properties( mlt_frame this )
75 /** Check if we have a way to derive something other than a test card.
78 int mlt_frame_is_test_card( mlt_frame this )
80 return ( this->stack_get_image_size == 0 && mlt_properties_get_data( mlt_frame_properties( this ), "image", NULL ) == NULL );
83 /** Check if we have a way to derive something than test audio.
86 int mlt_frame_is_test_audio( mlt_frame this )
88 return this->get_audio == NULL;
91 /** Get the aspect ratio of the frame.
94 double mlt_frame_get_aspect_ratio( mlt_frame this )
96 mlt_properties properties = mlt_frame_properties( this );
97 return mlt_properties_get_double( properties, "aspect_ratio" );
100 /** Set the aspect ratio of the frame.
103 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
105 mlt_properties properties = mlt_frame_properties( this );
106 return mlt_properties_set_double( properties, "aspect_ratio", value );
109 /** Get the position of this frame.
112 mlt_position mlt_frame_get_position( mlt_frame this )
114 mlt_properties properties = mlt_frame_properties( this );
115 return mlt_properties_get_position( properties, "position" );
118 /** Set the position of this frame.
121 int mlt_frame_set_position( mlt_frame this, mlt_position value )
123 mlt_properties properties = mlt_frame_properties( this );
124 return mlt_properties_set_position( properties, "position", value );
127 /** Stack a get_image callback.
130 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
132 int ret = this->stack_get_image_size >= 10;
134 this->stack_get_image[ this->stack_get_image_size ++ ] = get_image;
138 /** Pop a get_image callback.
141 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
143 mlt_get_image result = NULL;
144 if ( this->stack_get_image_size > 0 )
145 result = this->stack_get_image[ -- this->stack_get_image_size ];
152 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
154 int ret = this->stack_frame_size >= 10;
156 this->stack_frame[ this->stack_frame_size ++ ] = that;
163 mlt_frame mlt_frame_pop_frame( mlt_frame this )
165 mlt_frame result = NULL;
166 if ( this->stack_frame_size > 0 )
167 result = this->stack_frame[ -- this->stack_frame_size ];
171 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
173 mlt_properties properties = mlt_frame_properties( this );
174 mlt_get_image get_image = mlt_frame_pop_get_image( this );
176 if ( get_image != NULL )
178 return get_image( this, buffer, format, width, height, writable );
180 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
182 *format = mlt_image_yuv422;
183 *buffer = mlt_properties_get_data( properties, "image", NULL );
184 *width = mlt_properties_get_int( properties, "width" );
185 *height = mlt_properties_get_int( properties, "height" );
189 if ( test_card.vfmt != *format )
194 test_card.vfmt = *format;
195 test_card.width = *width == 0 ? 720 : *width;
196 test_card.height = *height == 0 ? 576 : *height;
202 case mlt_image_rgb24:
203 test_card.image = realloc( test_card.image, test_card.width * test_card.height * 3 );
204 memset( test_card.image, 255, test_card.width * test_card.height * 3 );
206 case mlt_image_rgb24a:
207 test_card.image = realloc( test_card.image, test_card.width * test_card.height * 4 );
208 memset( test_card.image, 255, test_card.width * test_card.height * 4 );
210 case mlt_image_yuv422:
211 test_card.image = realloc( test_card.image, test_card.width * test_card.height * 2 );
213 q = test_card.image + test_card.width * test_card.height * 2;
220 case mlt_image_yuv420p:
221 test_card.image = realloc( test_card.image, test_card.width * test_card.height * 3 / 2 );
222 memset( test_card.image, 255, test_card.width * test_card.height * 3 / 2 );
227 *width = test_card.width;
228 *height = test_card.height;
229 *buffer = test_card.image;
235 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
237 if ( this->get_alpha_mask != NULL )
238 return this->get_alpha_mask( this );
239 return test_card.alpha;
242 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
244 if ( this->get_audio != NULL )
246 return this->get_audio( this, buffer, format, frequency, channels, samples );
252 if ( *channels <= 0 )
254 if ( *frequency <= 0 )
256 if ( test_card.audio == NULL || test_card.afmt != *format )
258 test_card.afmt = *format;
259 test_card.audio = realloc( test_card.audio, *samples * *channels * sizeof( int16_t ) );
260 memset( test_card.audio, 0, *samples * *channels * sizeof( int16_t ) );
263 *buffer = test_card.audio;
268 void mlt_frame_close( mlt_frame this )
270 mlt_frame frame = mlt_frame_pop_frame( this );
272 while ( frame != NULL )
274 mlt_frame_close( frame);
275 frame = mlt_frame_pop_frame( this );
278 mlt_properties_close( &this->parent );
283 /***** convenience functions *****/
284 #define RGB2YUV(r, g, b, y, u, v)\
285 y = (306*r + 601*g + 117*b) >> 10;\
286 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
287 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
291 y = y > 255 ? 255 : y;\
292 u = u > 255 ? 255 : u;\
293 v = v > 255 ? 255 : v
295 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
298 register int y0, y1, u0, u1, v0, v1;
299 register int r, g, b;
300 register uint8_t *d = yuv;
303 for ( i = 0; i < height; i++ )
305 register uint8_t *s = rgba + ( stride * i );
306 for ( j = 0; j < ( width / 2 ); j++ )
312 RGB2YUV (r, g, b, y0, u0 , v0);
317 RGB2YUV (r, g, b, y1, u1 , v1);
329 RGB2YUV (r, g, b, y0, u0 , v0);
337 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
340 register int y0, y1, u0, u1, v0, v1;
341 register int r, g, b;
342 register uint8_t *d = yuv;
345 for ( i = 0; i < height; i++ )
347 register uint8_t *s = rgb + ( stride * i );
348 for ( j = 0; j < ( width / 2 ); j++ )
353 RGB2YUV (r, g, b, y0, u0 , v0);
357 RGB2YUV (r, g, b, y1, u1 , v1);
368 RGB2YUV (r, g, b, y0, u0 , v0);
376 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
381 int half = width >> 1;
383 uint8_t *Y = yuv420p;
384 uint8_t *U = Y + width * height;
385 uint8_t *V = U + width * height / 4;
387 register uint8_t *d = yuv;
389 for ( i = 0; i < height; i++ )
391 register uint8_t *u = U + ( i / 2 ) * ( half );
392 register uint8_t *v = V + ( i / 2 ) * ( half );
394 for ( j = 0; j < half; j++ )
405 int mlt_frame_composite_yuv( mlt_frame this, mlt_frame that, int x, int y, float weight )
408 int width_src = 0, height_src = 0;
409 int width_dest = 0, height_dest = 0;
410 mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
411 uint8_t *p_src, *p_dest;
415 int x_src = 0, y_src = 0;
417 // optimization point - no work to do
418 if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
421 format_src = mlt_image_yuv422;
422 format_dest = mlt_image_yuv422;
424 //fprintf( stderr, "call get_image on frame a\n"), fflush( stderr );
425 mlt_frame_get_image( this, &p_dest, &format_dest, &width_dest, &height_dest, 1 /* writable */ );
426 //fprintf( stderr, "call get_image on frame b\n"), fflush( stderr );
427 mlt_frame_get_image( that, &p_src, &format_src, &width_src, &height_src, 0 /* writable */ );
429 //fprintf( stderr, "mlt_frame_composite_yuv %dx%d -> %dx%d\n", width_src, height_src, width_dest, height_dest );
432 stride_src = width_src * 2;
433 stride_dest = width_dest * 2;
435 // crop overlay off the left edge of frame
443 // crop overlay beyond right edge of frame
444 else if ( x + width_src > width_dest )
445 width_src = width_dest - x;
447 // crop overlay off the top edge of the frame
453 // crop overlay below bottom edge of frame
454 else if ( y + height_src > height_dest )
455 height_src = height_dest - y;
457 // offset pointer into overlay buffer based on cropping
458 p_src += x_src * 2 + y_src * stride_src;
460 // offset pointer into frame buffer based upon positive, even coordinates only!
461 // if ( interlaced && y % 2 )
463 p_dest += ( x < 0 ? 0 : x ) * 2 + ( y < 0 ? 0 : y ) * stride_dest;
465 // Get the alpha channel of the overlay
466 uint8_t *p_alpha = mlt_frame_get_alpha_mask( that );
468 // offset pointer into alpha channel based upon cropping
470 p_alpha += x_src + y_src * stride_src / 2;
472 // now do the compositing only to cropped extents
473 for ( i = 0; i < height_src; i++ )
478 uint8_t *z = p_alpha;
480 for ( j = 0; j < width_src; j ++ )
484 uint8_t a = ( z == NULL ) ? 255 : *z ++;
485 float value = ( weight * ( float ) a / 255.0 );
486 *o ++ = (uint8_t)( y * value + *q++ * ( 1 - value ) );
487 *o ++ = (uint8_t)( uv * value + *q++ * ( 1 - value ) );
491 p_dest += stride_dest;
493 p_alpha += stride_src / 2;
499 void *memfill( void *dst, void *src, int l, int elements )
506 uint8_t *src2 = src + 1;
507 for ( i = 0; i < elements; i ++ )
516 for ( i = 0; i < elements; i ++ )
517 dst = memcpy( dst, src, l ) + l;
522 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
525 int istride = iwidth * 2;
526 int ostride = owidth * 2;
528 iwidth = iwidth - ( iwidth % 4 );
529 owidth = owidth - ( owidth % 4 );
530 iheight = iheight - ( iheight % 2 );
531 oheight = oheight - ( oheight % 2 );
533 // Coordinates (0,0 is middle of output)
537 int out_x_range = owidth / 2;
538 int out_y_range = oheight / 2;
539 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
540 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
543 uint8_t *out_line = output;
544 uint8_t *out_ptr = out_line;
546 // Calculate a middle and possibly invalid pointer in the input
547 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
548 int in_line = - in_y_range * istride - in_x_range * 2;
550 uint8_t black[ 2 ] = { 16, 128 };
552 // Loop for the entirety of our output height.
553 for ( y = - out_y_range; y < out_y_range ; y ++ )
555 // Start at the beginning of the line
558 if ( abs( y ) < iheight / 2 )
560 // Fill the outer part with black
561 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
563 // We're in the input range for this row.
564 memcpy( out_ptr, in_middle + in_line, 2 * iwidth );
565 out_ptr += 2 * iwidth;
567 // Fill the outer part with black
568 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
570 // Move to next input line
575 // Fill whole line with black
576 out_ptr = memfill( out_ptr, black, 2, owidth );
579 // Move to next output line
584 /** A resizing function for yuv422 frames - this does not rescale, but simply
585 resizes. It assumes yuv422 images available on the frame so use with care.
588 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
591 mlt_properties properties = mlt_frame_properties( this );
593 // Get the input image, width and height
594 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
595 int iwidth = mlt_properties_get_int( properties, "width" );
596 int iheight = mlt_properties_get_int( properties, "height" );
598 // If width and height are correct, don't do anything
599 if ( iwidth != owidth || iheight != oheight )
601 // Create the output image
602 uint8_t *output = malloc( owidth * oheight * 2 );
604 // Call the generic resize
605 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
607 // Now update the frame
608 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
609 mlt_properties_set_int( properties, "width", owidth );
610 mlt_properties_set_int( properties, "height", oheight );
616 // No change, return input
620 /** A rescaling function for yuv422 frames - low quality, and provided for testing
621 only. It assumes yuv422 images available on the frame so use with care.
624 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
627 mlt_properties properties = mlt_frame_properties( this );
629 // Get the input image, width and height
630 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
631 int iwidth = mlt_properties_get_int( properties, "width" );
632 int iheight = mlt_properties_get_int( properties, "height" );
634 // If width and height are correct, don't do anything
635 if ( iwidth != owidth || iheight != oheight )
637 // Create the output image
638 uint8_t *output = malloc( owidth * oheight * 2 );
641 int istride = iwidth * 2;
642 int ostride = owidth * 2;
644 iwidth = iwidth - ( iwidth % 4 );
646 // Coordinates (0,0 is middle of output)
649 // Derived coordinates
653 int out_x_range = owidth / 2;
654 int out_y_range = oheight / 2;
655 int in_x_range = iwidth / 2;
656 int in_y_range = iheight / 2;
659 uint8_t *out_line = output;
662 // Calculate a middle pointer
663 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
667 // Generate the affine transform scaling values
668 float scale_width = ( float )iwidth / ( float )owidth;
669 float scale_height = ( float )iheight / ( float )oheight;
671 // Loop for the entirety of our output height.
672 for ( y = - out_y_range; y < out_y_range ; y ++ )
674 // Calculate the derived y value
675 dy = scale_height * y;
677 // Start at the beginning of the line
680 // Pointer to the middle of the input line
681 in_line = in_middle + dy * istride;
683 // Loop for the entirety of our output row.
684 for ( x = - out_x_range; x < out_x_range; x += 1 )
686 // Calculated the derived x
687 dx = scale_width * x;
689 // Check if x and y are in the valid input range.
690 if ( abs( dx ) < in_x_range && abs( dy ) < in_y_range )
692 // We're in the input range for this row.
693 in_ptr = in_line + ( dx >> 1 ) * 4 + 2 * ( x & 1 );
694 *out_ptr ++ = *in_ptr ++;
695 *out_ptr ++ = *in_ptr ++;
699 // We're not in the input range for this row.
705 // Move to next output line
709 // Now update the frame
710 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
711 mlt_properties_set_int( properties, "width", owidth );
712 mlt_properties_set_int( properties, "height", oheight );
718 // No change, return input
722 int mlt_frame_mix_audio( mlt_frame this, mlt_frame that, float weight, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
725 int16_t *p_src, *p_dest;
727 //static int16_t *extra_src = NULL, *extra_dest = NULL;
728 static int extra_src_samples = 0, extra_dest_samples = 0;
729 int frequency_src = 0, frequency_dest = 0;
730 int channels_src = 0, channels_dest = 0;
731 int samples_src = 0, samples_dest = 0;
734 mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest );
735 //fprintf( stderr, "frame dest samples %d channels %d position %f\n", samples_dest, channels_dest, mlt_properties_get_position( mlt_frame_properties( this ), "position" ) );
736 mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src );
737 //fprintf( stderr, "frame src samples %d channels %d\n", samples_src, channels_src );
738 if ( channels_src > 6 )
740 if ( channels_dest > 6 )
742 if ( samples_src > 4000 )
744 if ( samples_dest > 4000 )
748 // Append new samples to leftovers
749 if ( extra_dest_samples > 0 )
751 fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples );
752 dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest );
753 memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest );
757 if ( extra_src_samples > 0 )
759 fprintf( stderr, "prepending %d samples to src\n", extra_src_samples );
760 src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src );
761 memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src );
770 // determine number of samples to process
771 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
772 *samples = samples_src + extra_src_samples;
773 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
774 *samples = samples_dest + extra_dest_samples;
776 *channels = channels_src < channels_dest ? channels_src : channels_dest;
780 for ( i = 0; i < *samples; i++ )
782 for ( j = 0; j < *channels; j++ )
784 double d = (double) dest[ i * channels_dest + j ];
785 double s = (double) src[ i * channels_src + j ];
786 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
790 // We have to copy --sigh
791 if ( dest != p_dest )
792 memcpy( p_dest, dest, *samples * 2 * *channels );
795 // Store the leftovers
796 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
798 extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples );
799 size_t size = extra_dest_samples * 2 * channels_dest;
800 fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples );
803 extra_dest = malloc( size );
805 memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size );
807 extra_dest_samples = 0;
809 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
811 extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples );
812 size_t size = extra_src_samples * 2 * channels_src;
813 fprintf( stderr, "storing %d samples from src\n", extra_dest_samples );
816 extra_src = malloc( size );
818 memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size );
820 extra_src_samples = 0;
827 int mlt_sample_calculator( float fps, int frequency, int64_t position )
831 if ( fps > 29 && fps <= 30 )
833 samples = frequency / 30;
838 if ( position % 5 != 0 )
842 if ( position % 300 == 0 )
844 else if ( position % 30 == 0 )
846 else if ( position % 2 == 0 )
852 if ( position % 30 == 0 )
854 else if ( position % 29 == 0 )
856 else if ( position % 4 == 2 )
867 samples = frequency / fps;