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"
27 /** Constructor for a frame.
30 mlt_frame mlt_frame_init( )
33 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
37 // Initialise the properties
38 mlt_properties properties = &this->parent;
39 mlt_properties_init( properties, this );
41 // Set default properties on the frame
42 mlt_properties_set_position( properties, "_position", 0.0 );
43 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
44 mlt_properties_set_int( properties, "width", 720 );
45 mlt_properties_set_int( properties, "height", 576 );
46 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
47 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
48 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
53 /** Fetch the frames properties.
56 mlt_properties mlt_frame_properties( mlt_frame this )
61 /** Check if we have a way to derive something other than a test card.
64 int mlt_frame_is_test_card( mlt_frame this )
66 return mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
69 /** Check if we have a way to derive something than test audio.
72 int mlt_frame_is_test_audio( mlt_frame this )
74 return this->get_audio == NULL || mlt_properties_get_int( mlt_frame_properties( this ), "test_audio" );
77 /** Get the aspect ratio of the frame.
80 double mlt_frame_get_aspect_ratio( mlt_frame this )
82 return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
85 /** Set the aspect ratio of the frame.
88 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
90 return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value );
93 /** Get the position of this frame.
96 mlt_position mlt_frame_get_position( mlt_frame this )
98 return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
101 /** Set the position of this frame.
104 int mlt_frame_set_position( mlt_frame this, mlt_position value )
106 return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value );
109 /** Stack a get_image callback.
112 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
114 int ret = this->stack_get_image_size >= 10;
116 this->stack_get_image[ this->stack_get_image_size ++ ] = get_image;
120 /** Pop a get_image callback.
123 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
125 mlt_get_image result = NULL;
126 if ( this->stack_get_image_size > 0 )
127 result = this->stack_get_image[ -- this->stack_get_image_size ];
134 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
136 int ret = this->stack_frame_size >= 10;
138 this->stack_frame[ this->stack_frame_size ++ ] = that;
145 mlt_frame mlt_frame_pop_frame( mlt_frame this )
147 mlt_frame result = NULL;
148 if ( this->stack_frame_size > 0 )
149 result = this->stack_frame[ -- this->stack_frame_size ];
153 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
155 mlt_properties properties = mlt_frame_properties( this );
156 mlt_get_image get_image = mlt_frame_pop_get_image( this );
158 if ( get_image != NULL )
160 return get_image( this, buffer, format, width, height, writable );
162 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
164 *format = mlt_image_yuv422;
165 *buffer = mlt_properties_get_data( properties, "image", NULL );
166 *width = mlt_properties_get_int( properties, "width" );
167 *height = mlt_properties_get_int( properties, "height" );
175 *width = *width == 0 ? 720 : *width;
176 *height = *height == 0 ? 576 : *height;
177 size = *width * *height;
179 mlt_properties_set_int( properties, "width", *width );
180 mlt_properties_set_int( properties, "height", *height );
188 case mlt_image_rgb24:
190 *buffer = malloc( size );
192 memset( *buffer, 255, size );
194 case mlt_image_rgb24a:
196 *buffer = malloc( size );
198 memset( *buffer, 255, size );
200 case mlt_image_yuv422:
202 *buffer = malloc( size );
205 while ( p != NULL && p != q )
211 case mlt_image_yuv420p:
213 *buffer = malloc( size );
215 memset( *buffer, 255, size );
219 mlt_properties_set_data( properties, "image", *buffer, size, free, NULL );
220 mlt_properties_set_int( properties, "test_image", 1 );
226 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
228 if ( this->get_alpha_mask != NULL )
229 return this->get_alpha_mask( this );
233 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
235 mlt_properties properties = mlt_frame_properties( this );
237 if ( this->get_audio != NULL )
239 return this->get_audio( this, buffer, format, frequency, channels, samples );
244 *samples = *samples <= 0 ? 1920 : *samples;
245 *channels = *channels <= 0 ? 2 : *channels;
246 *frequency = *frequency <= 0 ? 48000 : *frequency;
247 size = *samples * *channels * sizeof( int16_t );
248 *buffer = malloc( size );
249 if ( *buffer != NULL )
250 memset( *buffer, 0, size );
251 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
252 mlt_properties_set_int( properties, "test_audio", 1 );
257 void mlt_frame_close( mlt_frame this )
259 mlt_properties_close( &this->parent );
263 /***** convenience functions *****/
264 #define RGB2YUV(r, g, b, y, u, v)\
265 y = (306*r + 601*g + 117*b) >> 10;\
266 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
267 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
271 y = y > 255 ? 255 : y;\
272 u = u > 255 ? 255 : u;\
273 v = v > 255 ? 255 : v
275 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
278 register int y0, y1, u0, u1, v0, v1;
279 register int r, g, b;
280 register uint8_t *d = yuv;
283 for ( i = 0; i < height; i++ )
285 register uint8_t *s = rgba + ( stride * i );
286 for ( j = 0; j < ( width / 2 ); j++ )
292 RGB2YUV (r, g, b, y0, u0 , v0);
297 RGB2YUV (r, g, b, y1, u1 , v1);
309 RGB2YUV (r, g, b, y0, u0 , v0);
317 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
320 register int y0, y1, u0, u1, v0, v1;
321 register int r, g, b;
322 register uint8_t *d = yuv;
325 for ( i = 0; i < height; i++ )
327 register uint8_t *s = rgb + ( stride * i );
328 for ( j = 0; j < ( width / 2 ); j++ )
333 RGB2YUV (r, g, b, y0, u0 , v0);
337 RGB2YUV (r, g, b, y1, u1 , v1);
348 RGB2YUV (r, g, b, y0, u0 , v0);
356 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
361 int half = width >> 1;
363 uint8_t *Y = yuv420p;
364 uint8_t *U = Y + width * height;
365 uint8_t *V = U + width * height / 4;
367 register uint8_t *d = yuv;
369 for ( i = 0; i < height; i++ )
371 register uint8_t *u = U + ( i / 2 ) * ( half );
372 register uint8_t *v = V + ( i / 2 ) * ( half );
374 for ( j = 0; j < half; j++ )
385 void *memfill( void *dst, void *src, int l, int elements )
392 uint8_t *src2 = src + 1;
393 for ( i = 0; i < elements; i ++ )
402 for ( i = 0; i < elements; i ++ )
403 dst = memcpy( dst, src, l ) + l;
408 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
411 int istride = iwidth * 2;
412 int ostride = owidth * 2;
414 iwidth = iwidth - ( iwidth % 4 );
415 owidth = owidth - ( owidth % 4 );
416 iheight = iheight - ( iheight % 2 );
417 oheight = oheight - ( oheight % 2 );
419 // Optimisation point
420 if ( iwidth == owidth && iheight == oheight )
421 memcpy( output, input, iheight * istride );
423 // Coordinates (0,0 is middle of output)
427 int out_x_range = owidth / 2;
428 int out_y_range = oheight / 2;
429 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
430 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
433 uint8_t *out_line = output;
434 uint8_t *out_ptr = out_line;
436 // Calculate a middle and possibly invalid pointer in the input
437 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
438 int in_line = - in_y_range * istride - in_x_range * 2;
440 uint8_t black[ 2 ] = { 16, 128 };
442 // Loop for the entirety of our output height.
443 for ( y = - out_y_range; y < out_y_range ; y ++ )
445 // Start at the beginning of the line
448 if ( abs( y ) < iheight / 2 )
450 // Fill the outer part with black
451 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
453 // We're in the input range for this row.
454 memcpy( out_ptr, in_middle + in_line, 2 * iwidth );
455 out_ptr += 2 * iwidth;
457 // Fill the outer part with black
458 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
460 // Move to next input line
465 // Fill whole line with black
466 out_ptr = memfill( out_ptr, black, 2, owidth );
469 // Move to next output line
474 /** A resizing function for yuv422 frames - this does not rescale, but simply
475 resizes. It assumes yuv422 images available on the frame so use with care.
478 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
481 mlt_properties properties = mlt_frame_properties( this );
483 // Get the input image, width and height
484 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
485 int iwidth = mlt_properties_get_int( properties, "width" );
486 int iheight = mlt_properties_get_int( properties, "height" );
488 // If width and height are correct, don't do anything
489 if ( iwidth != owidth || iheight != oheight )
491 // Create the output image
492 uint8_t *output = malloc( owidth * oheight * 2 );
494 // Call the generic resize
495 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
497 // Now update the frame
498 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
499 mlt_properties_set_int( properties, "width", owidth );
500 mlt_properties_set_int( properties, "height", oheight );
501 mlt_frame_set_aspect_ratio( this, 4.0/3.0/*( float )owidth / oheight*/ );
507 // No change, return input
511 /** A rescaling function for yuv422 frames - low quality, and provided for testing
512 only. It assumes yuv422 images available on the frame so use with care.
515 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
518 mlt_properties properties = mlt_frame_properties( this );
520 // Get the input image, width and height
521 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
522 int iwidth = mlt_properties_get_int( properties, "width" );
523 int iheight = mlt_properties_get_int( properties, "height" );
525 // If width and height are correct, don't do anything
526 if ( iwidth != owidth || iheight != oheight )
528 // Create the output image
529 uint8_t *output = malloc( owidth * oheight * 2 );
532 int istride = iwidth * 2;
533 int ostride = owidth * 2;
535 iwidth = iwidth - ( iwidth % 4 );
537 // Coordinates (0,0 is middle of output)
540 // Derived coordinates
544 int out_x_range = owidth / 2;
545 int out_y_range = oheight / 2;
546 int in_x_range = iwidth / 2;
547 int in_y_range = iheight / 2;
550 uint8_t *out_line = output;
553 // Calculate a middle pointer
554 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
558 // Generate the affine transform scaling values
559 int scale_width = ( iwidth << 16 ) / owidth;
560 int scale_height = ( iheight << 16 ) / oheight;
562 // Loop for the entirety of our output height.
563 for ( y = - out_y_range; y < out_y_range ; y ++ )
565 // Calculate the derived y value
566 dy = ( scale_height * y ) >> 16;
568 // Start at the beginning of the line
571 // Pointer to the middle of the input line
572 in_line = in_middle + dy * istride;
574 // Loop for the entirety of our output row.
575 for ( x = - out_x_range; x < out_x_range; x += 1 )
577 // Calculated the derived x
578 dx = ( scale_width * x ) >> 16;
580 // We're in the input range for this row.
581 in_ptr = in_line + ( dx << 1 );
582 *out_ptr ++ = *in_ptr ++;
583 in_ptr = in_line + ( ( dx >> 1 ) << 2 ) + ( ( x & 1 ) << 1 ) + 1;
584 *out_ptr ++ = *in_ptr;
587 // Move to next output line
591 // Now update the frame
592 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
593 mlt_properties_set_int( properties, "width", owidth );
594 mlt_properties_set_int( properties, "height", oheight );
600 // No change, return input
604 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 )
607 int16_t *p_src, *p_dest;
609 //static int16_t *extra_src = NULL, *extra_dest = NULL;
610 static int extra_src_samples = 0, extra_dest_samples = 0;
611 int frequency_src = *frequency, frequency_dest = *frequency;
612 int channels_src = *channels, channels_dest = *channels;
613 int samples_src = *samples, samples_dest = *samples;
617 mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest );
618 //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" ) );
619 mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src );
620 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
623 if ( channels_src > 6 )
625 if ( channels_dest > 6 )
627 if ( samples_src > 4000 )
629 if ( samples_dest > 4000 )
633 // Append new samples to leftovers
634 if ( extra_dest_samples > 0 )
636 fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples );
637 dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest );
638 memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest );
642 if ( extra_src_samples > 0 )
644 fprintf( stderr, "prepending %d samples to src\n", extra_src_samples );
645 src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src );
646 memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src );
652 // determine number of samples to process
653 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
654 *samples = samples_src + extra_src_samples;
655 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
656 *samples = samples_dest + extra_dest_samples;
658 *channels = channels_src < channels_dest ? channels_src : channels_dest;
660 *frequency = frequency_dest;
663 for ( i = 0; i < *samples; i++ )
665 for ( j = 0; j < *channels; j++ )
667 if ( j < channels_dest )
668 d = (double) dest[ i * channels_dest + j ];
669 if ( j < channels_src )
670 s = (double) src[ i * channels_src + j ];
671 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
675 // We have to copy --sigh
676 if ( dest != p_dest )
677 memcpy( p_dest, dest, *samples * 2 * *channels );
680 // Store the leftovers
681 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
683 extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples );
684 size_t size = extra_dest_samples * 2 * channels_dest;
685 fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples );
688 extra_dest = malloc( size );
690 memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size );
692 extra_dest_samples = 0;
694 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
696 extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples );
697 size_t size = extra_src_samples * 2 * channels_src;
698 fprintf( stderr, "storing %d samples from src\n", extra_dest_samples );
701 extra_src = malloc( size );
703 memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size );
705 extra_src_samples = 0;
712 int mlt_sample_calculator( float fps, int frequency, int64_t position )
716 if ( fps > 29 && fps <= 30 )
718 samples = frequency / 30;
723 if ( position % 5 != 0 )
727 if ( position % 300 == 0 )
729 else if ( position % 30 == 0 )
731 else if ( position % 2 == 0 )
737 if ( position % 30 == 0 )
739 else if ( position % 29 == 0 )
741 else if ( position % 4 == 2 )
752 samples = frequency / fps;