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"
24 #include "mlt_factory.h"
29 /** Constructor for a frame.
32 mlt_frame
mlt_frame_init( )
35 mlt_frame
this = calloc( sizeof( struct mlt_frame_s
), 1 );
39 // Get the normalisation
40 char *normalisation
= mlt_environment( "MLT_NORMALISATION" );
42 // Initialise the properties
43 mlt_properties properties
= &this->parent
;
44 mlt_properties_init( properties
, this );
46 // Set default properties on the frame
47 mlt_properties_set_position( properties
, "_position", 0.0 );
48 mlt_properties_set_data( properties
, "image", NULL
, 0, NULL
, NULL
);
50 if ( normalisation
== NULL
|| strcmp( normalisation
, "NTSC" ) )
52 mlt_properties_set_int( properties
, "width", 720 );
53 mlt_properties_set_int( properties
, "height", 576 );
54 mlt_properties_set_int( properties
, "normalised_width", 720 );
55 mlt_properties_set_int( properties
, "normalised_height", 576 );
56 mlt_properties_set_double( properties
, "aspect_ratio", 72.0/79.0 );
60 mlt_properties_set_int( properties
, "width", 720 );
61 mlt_properties_set_int( properties
, "height", 480 );
62 mlt_properties_set_int( properties
, "normalised_width", 720 );
63 mlt_properties_set_int( properties
, "normalised_height", 480 );
64 mlt_properties_set_double( properties
, "aspect_ratio", 128.0/117.0 );
67 mlt_properties_set_data( properties
, "audio", NULL
, 0, NULL
, NULL
);
68 mlt_properties_set_data( properties
, "alpha", NULL
, 0, NULL
, NULL
);
70 // Construct stacks for frames and methods
71 this->stack_image
= mlt_deque_init( );
72 this->stack_audio
= mlt_deque_init( );
78 /** Fetch the frames properties.
81 mlt_properties
mlt_frame_properties( mlt_frame
this )
83 return this != NULL ?
&this->parent
: NULL
;
86 /** Check if we have a way to derive something other than a test card.
89 int mlt_frame_is_test_card( mlt_frame
this )
91 return mlt_deque_count( this->stack_image
) == 0 || mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
94 /** Check if we have a way to derive something than test audio.
97 int mlt_frame_is_test_audio( mlt_frame
this )
99 return this->get_audio
== NULL
|| mlt_properties_get_int( mlt_frame_properties( this ), "test_audio" );
102 /** Get the aspect ratio of the frame.
105 double mlt_frame_get_aspect_ratio( mlt_frame
this )
107 return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
110 /** Set the aspect ratio of the frame.
113 int mlt_frame_set_aspect_ratio( mlt_frame
this, double value
)
115 return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value
);
118 /** Get the position of this frame.
121 mlt_position
mlt_frame_get_position( mlt_frame
this )
123 return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
126 /** Set the position of this frame.
129 int mlt_frame_set_position( mlt_frame
this, mlt_position value
)
131 return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value
);
134 /** Stack a get_image callback.
137 int mlt_frame_push_get_image( mlt_frame
this, mlt_get_image get_image
)
139 return mlt_deque_push_back( this->stack_image
, get_image
);
142 /** Pop a get_image callback.
145 mlt_get_image
mlt_frame_pop_get_image( mlt_frame
this )
147 return mlt_deque_pop_back( this->stack_image
);
153 int mlt_frame_push_frame( mlt_frame
this, mlt_frame that
)
155 return mlt_deque_push_back( this->stack_image
, that
);
161 mlt_frame
mlt_frame_pop_frame( mlt_frame
this )
163 return mlt_deque_pop_back( this->stack_image
);
169 int mlt_frame_push_service( mlt_frame
this, void *that
)
171 return mlt_deque_push_back( this->stack_image
, that
);
177 void *mlt_frame_pop_service( mlt_frame
this )
179 return mlt_deque_pop_back( this->stack_image
);
182 /** Push an audio item on the stack.
185 int mlt_frame_push_audio( mlt_frame
this, void *that
)
187 return mlt_deque_push_back( this->stack_audio
, that
);
190 /** Pop an audio item from the stack
193 void *mlt_frame_pop_audio( mlt_frame
this )
195 return mlt_deque_pop_back( this->stack_audio
);
198 int mlt_frame_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
200 mlt_properties properties
= mlt_frame_properties( this );
201 mlt_get_image get_image
= mlt_frame_pop_get_image( this );
202 mlt_producer producer
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
204 if ( get_image
!= NULL
)
206 return get_image( this, buffer
, format
, width
, height
, writable
);
208 else if ( mlt_properties_get_data( properties
, "image", NULL
) != NULL
)
210 *format
= mlt_image_yuv422
;
211 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
212 *width
= mlt_properties_get_int( properties
, "width" );
213 *height
= mlt_properties_get_int( properties
, "height" );
215 else if ( producer
!= NULL
)
217 mlt_frame test_frame
= NULL
;
218 mlt_service_get_frame( mlt_producer_service( producer
), &test_frame
, 0 );
219 if ( test_frame
!= NULL
)
221 mlt_properties test_properties
= mlt_frame_properties( test_frame
);
222 mlt_properties_set_double( test_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "consumer_aspect_ratio" ) );
223 mlt_properties_set( test_properties
, "rescale.interp", mlt_properties_get( properties
, "rescale.interp" ) );
224 mlt_frame_get_image( test_frame
, buffer
, format
, width
, height
, writable
);
225 mlt_properties_set_data( properties
, "test_card_frame", test_frame
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
226 mlt_properties_set_data( properties
, "image", *buffer
, *width
* *height
* 2, NULL
, NULL
);
227 mlt_properties_set_int( properties
, "width", *width
);
228 mlt_properties_set_int( properties
, "height", *height
);
229 mlt_properties_set( properties
, "rescale.interp", "none" );
230 mlt_properties_set( properties
, "scale", "off" );
234 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
235 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
244 *width
= *width
== 0 ?
720 : *width
;
245 *height
= *height
== 0 ?
576 : *height
;
246 size
= *width
* *height
;
248 mlt_properties_set_int( properties
, "width", *width
);
249 mlt_properties_set_int( properties
, "height", *height
);
250 mlt_properties_set_int( properties
, "aspect_ratio", 1 );
258 case mlt_image_rgb24
:
261 *buffer
= mlt_pool_alloc( size
);
263 memset( *buffer
, 255, size
);
265 case mlt_image_rgb24a
:
268 *buffer
= mlt_pool_alloc( size
);
270 memset( *buffer
, 255, size
);
272 case mlt_image_yuv422
:
275 *buffer
= mlt_pool_alloc( size
);
278 while ( p
!= NULL
&& p
!= q
)
284 case mlt_image_yuv420p
:
286 *buffer
= mlt_pool_alloc( size
);
288 memset( *buffer
, 255, size
);
292 mlt_properties_set_data( properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
293 mlt_properties_set_int( properties
, "test_image", 1 );
299 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
301 if ( this->get_alpha_mask
!= NULL
)
302 return this->get_alpha_mask( this );
306 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
308 mlt_properties properties
= mlt_frame_properties( this );
310 if ( this->get_audio
!= NULL
)
312 this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
314 else if ( mlt_properties_get_data( properties
, "audio", NULL
) )
316 *buffer
= mlt_properties_get_data( properties
, "audio", NULL
);
317 *frequency
= mlt_properties_get_int( properties
, "audio_frequency" );
318 *channels
= mlt_properties_get_int( properties
, "audio_channels" );
319 *samples
= mlt_properties_get_int( properties
, "audio_samples" );
324 *samples
= *samples
<= 0 ?
1920 : *samples
;
325 *channels
= *channels
<= 0 ?
2 : *channels
;
326 *frequency
= *frequency
<= 0 ?
48000 : *frequency
;
327 size
= *samples
* *channels
* sizeof( int16_t );
328 *buffer
= mlt_pool_alloc( size
);
329 if ( *buffer
!= NULL
)
330 memset( *buffer
, 0, size
);
331 mlt_properties_set_data( properties
, "audio", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
332 mlt_properties_set_int( properties
, "test_audio", 1 );
335 mlt_properties_set_int( properties
, "audio_frequency", *frequency
);
336 mlt_properties_set_int( properties
, "audio_channels", *channels
);
337 mlt_properties_set_int( properties
, "audio_samples", *samples
);
342 unsigned char *mlt_frame_get_waveform( mlt_frame
this, int w
, int h
)
345 mlt_properties properties
= mlt_frame_properties( this );
346 mlt_audio_format format
= mlt_audio_pcm
;
347 int frequency
= 32000; // lower frequency available?
349 double fps
= mlt_properties_get_double( properties
, "fps" );
350 int samples
= mlt_sample_calculator( fps
, frequency
, mlt_frame_get_position( this ) );
353 mlt_frame_get_audio( this, &pcm
, &format
, &frequency
, &channels
, &samples
);
355 // Make an 8-bit buffer large enough to hold rendering
357 unsigned char *bitmap
= ( unsigned char* )mlt_pool_alloc( size
);
358 if ( bitmap
!= NULL
)
359 memset( bitmap
, 0, size
);
360 mlt_properties_set_data( properties
, "waveform", bitmap
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
362 // Render vertical lines
363 int16_t *ubound
= pcm
+ samples
* channels
;
364 int skip
= samples
/ w
- 1;
367 // Iterate sample stream and along x coordinate
368 for ( i
= 0; i
< w
&& pcm
< ubound
; i
++ )
370 // pcm data has channels interleaved
371 for ( j
= 0; j
< channels
; j
++ )
373 // Determine sample's magnitude from 2s complement;
374 int pcm_magnitude
= *pcm
< 0 ?
~(*pcm
) + 1 : *pcm
;
375 // The height of a line is the ratio of the magnitude multiplied by
376 // half the vertical resolution
377 int height
= ( int )( ( double )( pcm_magnitude
) / 32768 * h
/ 2 );
378 // Determine the starting y coordinate - left channel above center,
379 // right channel below - currently assumes 2 channels
380 int displacement
= ( h
/ 2 ) - ( 1 - j
) * height
;
381 // Position buffer pointer using y coordinate, stride, and x coordinate
382 unsigned char *p
= &bitmap
[ i
+ displacement
* w
];
384 // Draw vertical line
385 for ( k
= 0; k
< height
; k
++ )
390 pcm
+= skip
* channels
;
397 void mlt_frame_close( mlt_frame
this )
399 if ( this != NULL
&& mlt_properties_dec_ref( mlt_frame_properties( this ) ) <= 0 )
401 mlt_deque_close( this->stack_image
);
402 mlt_deque_close( this->stack_audio
);
403 mlt_properties_close( &this->parent
);
408 /***** convenience functions *****/
410 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
413 register int y0
, y1
, u0
, u1
, v0
, v1
;
414 register int r
, g
, b
;
415 register uint8_t *d
= yuv
;
418 for ( i
= 0; i
< height
; i
++ )
420 register uint8_t *s
= rgba
+ ( stride
* i
);
421 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
427 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
432 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
444 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
452 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
455 register int y0
, y1
, u0
, u1
, v0
, v1
;
456 register int r
, g
, b
;
457 register uint8_t *d
= yuv
;
460 for ( i
= 0; i
< height
; i
++ )
462 register uint8_t *s
= rgb
+ ( stride
* i
);
463 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
468 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
472 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
483 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
491 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p
, int width
, int height
, int stride
, uint8_t *yuv
)
496 int half
= width
>> 1;
498 uint8_t *Y
= yuv420p
;
499 uint8_t *U
= Y
+ width
* height
;
500 uint8_t *V
= U
+ width
* height
/ 4;
502 register uint8_t *d
= yuv
;
504 for ( i
= 0; i
< height
; i
++ )
506 register uint8_t *u
= U
+ ( i
/ 2 ) * ( half
);
507 register uint8_t *v
= V
+ ( i
/ 2 ) * ( half
);
509 for ( j
= 0; j
< half
; j
++ )
520 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
523 int istride
= iwidth
* 2;
524 int ostride
= owidth
* 2;
526 iwidth
= iwidth
- ( iwidth
% 4 );
527 owidth
= owidth
- ( owidth
% 4 );
528 iheight
= iheight
- ( iheight
% 2 );
529 oheight
= oheight
- ( oheight
% 2 );
531 // Optimisation point
532 if ( iwidth
== owidth
&& iheight
== oheight
)
533 memcpy( output
, input
, iheight
* istride
);
535 // Coordinates (0,0 is middle of output)
539 int out_x_range
= owidth
/ 2;
540 int out_y_range
= oheight
/ 2;
541 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
542 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
545 uint8_t *out_line
= output
;
546 uint8_t *out_ptr
= out_line
;
548 // Calculate a middle and possibly invalid pointer in the input
549 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
550 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
554 // Fill whole section with black
555 y
= out_y_range
- ( iheight
/ 2 );
556 int blank_elements
= ostride
* y
/ 2;
557 elements
= blank_elements
;
558 while ( elements
-- )
564 int active_width
= 2 * iwidth
;
565 int inactive_width
= out_x_range
- in_x_range
;
569 // Loop for the entirety of our output height.
572 // Start at the beginning of the line
575 // Fill the outer part with black
576 elements
= inactive_width
;
577 while ( elements
-- )
583 // We're in the input range for this row.
584 p
= in_middle
+ in_line
;
585 end
= out_ptr
+ active_width
;
586 while ( out_ptr
!= end
)
592 // Fill the outer part with black
593 elements
= inactive_width
;
594 while ( elements
-- )
600 // Move to next input line
603 // Move to next output line
607 // Fill whole section with black
608 elements
= blank_elements
;
609 while ( elements
-- )
616 /** A resizing function for yuv422 frames - this does not rescale, but simply
617 resizes. It assumes yuv422 images available on the frame so use with care.
620 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
623 mlt_properties properties
= mlt_frame_properties( this );
625 // Get the input image, width and height
626 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
627 int iwidth
= mlt_properties_get_int( properties
, "width" );
628 int iheight
= mlt_properties_get_int( properties
, "height" );
630 // If width and height are correct, don't do anything
631 if ( iwidth
!= owidth
|| iheight
!= oheight
)
633 // Create the output image
634 uint8_t *output
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
636 // Call the generic resize
637 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
639 // Now update the frame
640 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
641 mlt_properties_set_int( properties
, "width", owidth
);
642 mlt_properties_set_int( properties
, "height", oheight
);
647 // No change, return input
651 /** A rescaling function for yuv422 frames - low quality, and provided for testing
652 only. It assumes yuv422 images available on the frame so use with care.
655 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
658 mlt_properties properties
= mlt_frame_properties( this );
660 // Get the input image, width and height
661 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
662 int iwidth
= mlt_properties_get_int( properties
, "width" );
663 int iheight
= mlt_properties_get_int( properties
, "height" );
665 // If width and height are correct, don't do anything
666 if ( iwidth
!= owidth
|| iheight
!= oheight
)
668 // Create the output image
669 uint8_t *output
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
672 int istride
= iwidth
* 2;
673 int ostride
= owidth
* 2;
675 iwidth
= iwidth
- ( iwidth
% 4 );
677 // Derived coordinates
681 int out_x_range
= owidth
/ 2;
682 int out_y_range
= oheight
/ 2;
683 int in_x_range
= iwidth
/ 2;
684 int in_y_range
= iheight
/ 2;
687 register uint8_t *out_line
= output
;
688 register uint8_t *out_ptr
;
690 // Calculate a middle pointer
691 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
694 // Generate the affine transform scaling values
695 register int scale_width
= ( iwidth
<< 16 ) / owidth
;
696 register int scale_height
= ( iheight
<< 16 ) / oheight
;
697 register int base
= 0;
699 int outer
= out_x_range
* scale_width
;
700 int bottom
= out_y_range
* scale_height
;
702 // Loop for the entirety of our output height.
703 for ( dy
= - bottom
; dy
< bottom
; dy
+= scale_height
)
705 // Start at the beginning of the line
708 // Pointer to the middle of the input line
709 in_line
= in_middle
+ ( dy
>> 16 ) * istride
;
711 // Loop for the entirety of our output row.
712 for ( dx
= - outer
; dx
< outer
; dx
+= scale_width
)
716 *out_ptr
++ = *( in_line
+ base
);
718 *out_ptr
++ = *( in_line
+ base
+ 1 );
722 *out_ptr
++ = *( in_line
+ base
);
724 *out_ptr
++ = *( in_line
+ base
+ 3 );
727 // Move to next output line
731 // Now update the frame
732 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
733 mlt_properties_set_int( properties
, "width", owidth
);
734 mlt_properties_set_int( properties
, "height", oheight
);
740 // No change, return input
744 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
)
748 int frequency_src
= *frequency
, frequency_dest
= *frequency
;
749 int channels_src
= *channels
, channels_dest
= *channels
;
750 int samples_src
= *samples
, samples_dest
= *samples
;
754 mlt_frame_get_audio( this, &dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
755 //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" ) );
756 mlt_frame_get_audio( that
, &src
, format
, &frequency_src
, &channels_src
, &samples_src
);
757 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
759 if ( channels_src
> 6 )
761 if ( channels_dest
> 6 )
763 if ( samples_src
> 4000 )
765 if ( samples_dest
> 4000 )
768 // determine number of samples to process
769 *samples
= samples_src
< samples_dest ? samples_src
: samples_dest
;
770 *channels
= channels_src
< channels_dest ? channels_src
: channels_dest
;
772 *frequency
= frequency_dest
;
774 // Compute a smooth ramp over start to end
775 float weight
= weight_start
;
776 float weight_step
= ( weight_end
- weight_start
) / *samples
;
779 for ( i
= 0; i
< *samples
; i
++ )
781 for ( j
= 0; j
< *channels
; j
++ )
783 if ( j
< channels_dest
)
784 d
= (double) dest
[ i
* channels_dest
+ j
];
785 if ( j
< channels_src
)
786 s
= (double) src
[ i
* channels_src
+ j
];
787 dest
[ i
* channels_dest
+ j
] = s
* weight
+ d
* ( 1.0 - weight
);
789 weight
+= weight_step
;
795 int mlt_sample_calculator( float fps
, int frequency
, int64_t position
)
799 if ( ( int )( fps
* 100 ) == 2997 )
801 samples
= frequency
/ 30;
806 if ( position
% 5 != 0 )
810 if ( position
% 300 == 0 )
812 else if ( position
% 30 == 0 )
814 else if ( position
% 2 == 0 )
820 if ( position
% 30 == 0 )
822 else if ( position
% 29 == 0 )
824 else if ( position
% 4 == 2 )
835 samples
= frequency
/ fps
;