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 );
68 // Construct stacks for frames and methods
69 this->stack_get_image = mlt_deque_init( );
70 this->stack_frame = mlt_deque_init( );
76 /** Fetch the frames properties.
79 mlt_properties mlt_frame_properties( mlt_frame this )
84 /** Check if we have a way to derive something other than a test card.
87 int mlt_frame_is_test_card( mlt_frame this )
89 return mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
92 /** Check if we have a way to derive something than test audio.
95 int mlt_frame_is_test_audio( mlt_frame this )
97 return this->get_audio == NULL || mlt_properties_get_int( mlt_frame_properties( this ), "test_audio" );
100 /** Get the aspect ratio of the frame.
103 double mlt_frame_get_aspect_ratio( mlt_frame this )
105 return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
108 /** Set the aspect ratio of the frame.
111 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
113 return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value );
116 /** Get the position of this frame.
119 mlt_position mlt_frame_get_position( mlt_frame this )
121 return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
124 /** Set the position of this frame.
127 int mlt_frame_set_position( mlt_frame this, mlt_position value )
129 return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value );
132 /** Stack a get_image callback.
135 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
137 return mlt_deque_push_back( this->stack_get_image, get_image );
140 /** Pop a get_image callback.
143 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
145 return mlt_deque_pop_back( this->stack_get_image );
151 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
153 return mlt_deque_push_back( this->stack_frame, that );
159 mlt_frame mlt_frame_pop_frame( mlt_frame this )
161 return mlt_deque_pop_back( this->stack_frame );
164 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
166 mlt_properties properties = mlt_frame_properties( this );
167 mlt_get_image get_image = mlt_frame_pop_get_image( this );
168 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
170 if ( get_image != NULL )
172 return get_image( this, buffer, format, width, height, writable );
174 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
176 *format = mlt_image_yuv422;
177 *buffer = mlt_properties_get_data( properties, "image", NULL );
178 *width = mlt_properties_get_int( properties, "width" );
179 *height = mlt_properties_get_int( properties, "height" );
181 else if ( producer != NULL )
183 mlt_frame test_frame = NULL;
184 mlt_service_get_frame( mlt_producer_service( producer ), &test_frame, 0 );
185 if ( test_frame != NULL )
187 mlt_properties test_properties = mlt_frame_properties( test_frame );
188 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
189 mlt_properties_set_double( test_properties, "consumer_scale", mlt_properties_get_double( properties, "consumer_scale" ) );
190 mlt_properties_set( test_properties, "rescale.interp", "nearest" );
191 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
192 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
193 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
194 mlt_properties_set_int( properties, "width", *width );
195 mlt_properties_set_int( properties, "height", *height );
199 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
200 mlt_frame_get_image( this, buffer, format, width, height, writable );
209 *width = *width == 0 ? 720 : *width;
210 *height = *height == 0 ? 576 : *height;
211 size = *width * *height;
213 mlt_properties_set_int( properties, "width", *width );
214 mlt_properties_set_int( properties, "height", *height );
222 case mlt_image_rgb24:
225 *buffer = mlt_pool_alloc( size );
227 memset( *buffer, 255, size );
229 case mlt_image_rgb24a:
232 *buffer = mlt_pool_alloc( size );
234 memset( *buffer, 255, size );
236 case mlt_image_yuv422:
239 *buffer = mlt_pool_alloc( size );
242 while ( p != NULL && p != q )
248 case mlt_image_yuv420p:
250 *buffer = mlt_pool_alloc( size );
252 memset( *buffer, 255, size );
256 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
257 mlt_properties_set_int( properties, "test_image", 1 );
263 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
265 if ( this->get_alpha_mask != NULL )
266 return this->get_alpha_mask( this );
270 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
272 mlt_properties properties = mlt_frame_properties( this );
274 if ( this->get_audio != NULL )
276 return this->get_audio( this, buffer, format, frequency, channels, samples );
281 *samples = *samples <= 0 ? 1920 : *samples;
282 *channels = *channels <= 0 ? 2 : *channels;
283 *frequency = *frequency <= 0 ? 48000 : *frequency;
284 size = *samples * *channels * sizeof( int16_t );
285 *buffer = mlt_pool_alloc( size );
286 if ( *buffer != NULL )
287 memset( *buffer, 0, size );
288 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
289 mlt_properties_set_int( properties, "test_audio", 1 );
294 void mlt_frame_close( mlt_frame this )
296 mlt_deque_close( this->stack_get_image );
297 mlt_deque_close( this->stack_frame );
298 mlt_properties_close( &this->parent );
302 /***** convenience functions *****/
303 #define RGB2YUV(r, g, b, y, u, v)\
304 y = (306*r + 601*g + 117*b) >> 10;\
305 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
306 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
310 y = y > 255 ? 255 : y;\
311 u = u > 255 ? 255 : u;\
312 v = v > 255 ? 255 : v
314 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
317 register int y0, y1, u0, u1, v0, v1;
318 register int r, g, b;
319 register uint8_t *d = yuv;
322 for ( i = 0; i < height; i++ )
324 register uint8_t *s = rgba + ( stride * i );
325 for ( j = 0; j < ( width / 2 ); j++ )
331 RGB2YUV (r, g, b, y0, u0 , v0);
336 RGB2YUV (r, g, b, y1, u1 , v1);
348 RGB2YUV (r, g, b, y0, u0 , v0);
356 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
359 register int y0, y1, u0, u1, v0, v1;
360 register int r, g, b;
361 register uint8_t *d = yuv;
364 for ( i = 0; i < height; i++ )
366 register uint8_t *s = rgb + ( stride * i );
367 for ( j = 0; j < ( width / 2 ); j++ )
372 RGB2YUV (r, g, b, y0, u0 , v0);
376 RGB2YUV (r, g, b, y1, u1 , v1);
387 RGB2YUV (r, g, b, y0, u0 , v0);
395 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
400 int half = width >> 1;
402 uint8_t *Y = yuv420p;
403 uint8_t *U = Y + width * height;
404 uint8_t *V = U + width * height / 4;
406 register uint8_t *d = yuv;
408 for ( i = 0; i < height; i++ )
410 register uint8_t *u = U + ( i / 2 ) * ( half );
411 register uint8_t *v = V + ( i / 2 ) * ( half );
413 for ( j = 0; j < half; j++ )
424 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
427 int istride = iwidth * 2;
428 int ostride = owidth * 2;
430 iwidth = iwidth - ( iwidth % 4 );
431 owidth = owidth - ( owidth % 4 );
432 iheight = iheight - ( iheight % 2 );
433 oheight = oheight - ( oheight % 2 );
435 // Optimisation point
436 if ( iwidth == owidth && iheight == oheight )
437 memcpy( output, input, iheight * istride );
439 // Coordinates (0,0 is middle of output)
443 int out_x_range = owidth / 2;
444 int out_y_range = oheight / 2;
445 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
446 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
449 uint8_t *out_line = output;
450 uint8_t *out_ptr = out_line;
452 // Calculate a middle and possibly invalid pointer in the input
453 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
454 int in_line = - in_y_range * istride - in_x_range * 2;
458 // Fill whole section with black
459 y = out_y_range - ( iheight / 2 );
460 int blank_elements = ostride * y / 2;
461 elements = blank_elements;
462 while ( elements -- )
468 int active_width = 2 * iwidth;
469 int inactive_width = out_x_range - in_x_range;
471 // Loop for the entirety of our output height.
474 // Start at the beginning of the line
477 // Fill the outer part with black
478 elements = inactive_width;
479 while ( elements -- )
485 // We're in the input range for this row.
486 memcpy( out_ptr, in_middle + in_line, active_width );
487 out_ptr += active_width;
489 // Fill the outer part with black
490 elements = inactive_width;
491 while ( elements -- )
497 // Move to next input line
500 // Move to next output line
504 // Fill whole section with black
505 elements = blank_elements;
506 while ( elements -- )
513 /** A resizing function for yuv422 frames - this does not rescale, but simply
514 resizes. It assumes yuv422 images available on the frame so use with care.
517 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
520 mlt_properties properties = mlt_frame_properties( this );
522 // Get the input image, width and height
523 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
524 int iwidth = mlt_properties_get_int( properties, "width" );
525 int iheight = mlt_properties_get_int( properties, "height" );
527 // If width and height are correct, don't do anything
528 if ( iwidth != owidth || iheight != oheight )
530 // Create the output image
531 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
533 // Call the generic resize
534 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
536 // Now update the frame
537 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
538 mlt_properties_set_int( properties, "width", owidth );
539 mlt_properties_set_int( properties, "height", oheight );
544 // No change, return input
548 /** A rescaling function for yuv422 frames - low quality, and provided for testing
549 only. It assumes yuv422 images available on the frame so use with care.
552 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
555 mlt_properties properties = mlt_frame_properties( this );
557 // Get the input image, width and height
558 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
559 int iwidth = mlt_properties_get_int( properties, "width" );
560 int iheight = mlt_properties_get_int( properties, "height" );
562 // If width and height are correct, don't do anything
563 if ( iwidth != owidth || iheight != oheight )
565 // Create the output image
566 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
569 int istride = iwidth * 2;
570 int ostride = owidth * 2;
572 iwidth = iwidth - ( iwidth % 4 );
574 // Coordinates (0,0 is middle of output)
577 // Derived coordinates
581 int out_x_range = owidth / 2;
582 int out_y_range = oheight / 2;
583 int in_x_range = iwidth / 2;
584 int in_y_range = iheight / 2;
587 uint8_t *out_line = output;
590 // Calculate a middle pointer
591 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
595 // Generate the affine transform scaling values
596 int scale_width = ( iwidth << 16 ) / owidth;
597 int scale_height = ( iheight << 16 ) / oheight;
599 // Loop for the entirety of our output height.
600 for ( y = - out_y_range; y < out_y_range ; y ++ )
602 // Calculate the derived y value
603 dy = ( scale_height * y ) >> 16;
605 // Start at the beginning of the line
608 // Pointer to the middle of the input line
609 in_line = in_middle + dy * istride;
611 // Loop for the entirety of our output row.
612 for ( x = - out_x_range; x < out_x_range; x += 1 )
614 // Calculated the derived x
615 dx = ( scale_width * x ) >> 16;
617 // We're in the input range for this row.
618 in_ptr = in_line + ( dx << 1 );
619 *out_ptr ++ = *in_ptr ++;
620 in_ptr = in_line + ( ( dx >> 1 ) << 2 ) + ( ( x & 1 ) << 1 ) + 1;
621 *out_ptr ++ = *in_ptr;
624 // Move to next output line
628 // Now update the frame
629 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
630 mlt_properties_set_int( properties, "width", owidth );
631 mlt_properties_set_int( properties, "height", oheight );
637 // No change, return input
641 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 )
644 int16_t *p_src, *p_dest;
646 //static int16_t *extra_src = NULL, *extra_dest = NULL;
647 static int extra_src_samples = 0, extra_dest_samples = 0;
648 int frequency_src = *frequency, frequency_dest = *frequency;
649 int channels_src = *channels, channels_dest = *channels;
650 int samples_src = *samples, samples_dest = *samples;
654 mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest );
655 //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" ) );
656 mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src );
657 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
660 if ( channels_src > 6 )
662 if ( channels_dest > 6 )
664 if ( samples_src > 4000 )
666 if ( samples_dest > 4000 )
670 // Append new samples to leftovers
671 if ( extra_dest_samples > 0 )
673 fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples );
674 dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest );
675 memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest );
679 if ( extra_src_samples > 0 )
681 fprintf( stderr, "prepending %d samples to src\n", extra_src_samples );
682 src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src );
683 memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src );
689 // determine number of samples to process
690 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
691 *samples = samples_src + extra_src_samples;
692 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
693 *samples = samples_dest + extra_dest_samples;
695 *channels = channels_src < channels_dest ? channels_src : channels_dest;
697 *frequency = frequency_dest;
699 // Compute a smooth ramp over start to end
700 float weight = weight_start;
701 float weight_step = ( weight_end - weight_start ) / *samples;
704 for ( i = 0; i < *samples; i++ )
706 for ( j = 0; j < *channels; j++ )
708 if ( j < channels_dest )
709 d = (double) dest[ i * channels_dest + j ];
710 if ( j < channels_src )
711 s = (double) src[ i * channels_src + j ];
712 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
714 weight += weight_step;
717 // We have to copy --sigh
718 if ( dest != p_dest )
719 memcpy( p_dest, dest, *samples * 2 * *channels );
722 // Store the leftovers
723 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
725 extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples );
726 size_t size = extra_dest_samples * 2 * channels_dest;
727 fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples );
730 extra_dest = malloc( size );
732 memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size );
734 extra_dest_samples = 0;
736 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
738 extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples );
739 size_t size = extra_src_samples * 2 * channels_src;
740 fprintf( stderr, "storing %d samples from src\n", extra_dest_samples );
743 extra_src = malloc( size );
745 memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size );
747 extra_src_samples = 0;
754 int mlt_sample_calculator( float fps, int frequency, int64_t position )
758 if ( fps > 29 && fps <= 30 )
760 samples = frequency / 30;
765 if ( position % 5 != 0 )
769 if ( position % 300 == 0 )
771 else if ( position % 30 == 0 )
773 else if ( position % 2 == 0 )
779 if ( position % 30 == 0 )
781 else if ( position % 29 == 0 )
783 else if ( position % 4 == 2 )
794 samples = frequency / fps;