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 )
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_pass( properties
, test_properties
, "" );
223 mlt_frame_get_image( test_frame
, buffer
, format
, width
, height
, writable
);
224 mlt_properties_set_data( properties
, "test_card_frame", test_frame
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
225 mlt_properties_set_data( properties
, "image", *buffer
, *width
* *height
* 2, NULL
, NULL
);
226 mlt_properties_set_int( properties
, "width", *width
);
227 mlt_properties_set_int( properties
, "height", *height
);
228 mlt_properties_set( properties
, "rescale.interps", "none" );
229 mlt_properties_set( properties
, "scale", "off" );
233 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
234 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
243 *width
= *width
== 0 ?
720 : *width
;
244 *height
= *height
== 0 ?
576 : *height
;
245 size
= *width
* *height
;
247 mlt_properties_set_int( properties
, "width", *width
);
248 mlt_properties_set_int( properties
, "height", *height
);
249 mlt_properties_set_int( properties
, "aspect_ratio", 1 );
257 case mlt_image_rgb24
:
260 *buffer
= mlt_pool_alloc( size
);
262 memset( *buffer
, 255, size
);
264 case mlt_image_rgb24a
:
267 *buffer
= mlt_pool_alloc( size
);
269 memset( *buffer
, 255, size
);
271 case mlt_image_yuv422
:
274 *buffer
= mlt_pool_alloc( size
);
277 while ( p
!= NULL
&& p
!= q
)
283 case mlt_image_yuv420p
:
285 *buffer
= mlt_pool_alloc( size
);
287 memset( *buffer
, 255, size
);
291 mlt_properties_set_data( properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
292 mlt_properties_set_int( properties
, "test_image", 1 );
298 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
300 if ( this->get_alpha_mask
!= NULL
)
301 return this->get_alpha_mask( this );
305 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
307 mlt_properties properties
= mlt_frame_properties( this );
309 if ( this->get_audio
!= NULL
)
311 return this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
316 *samples
= *samples
<= 0 ?
1920 : *samples
;
317 *channels
= *channels
<= 0 ?
2 : *channels
;
318 *frequency
= *frequency
<= 0 ?
48000 : *frequency
;
319 size
= *samples
* *channels
* sizeof( int16_t );
320 *buffer
= mlt_pool_alloc( size
);
321 if ( *buffer
!= NULL
)
322 memset( *buffer
, 0, size
);
323 mlt_properties_set_data( properties
, "audio", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
324 mlt_properties_set_int( properties
, "test_audio", 1 );
329 void mlt_frame_close( mlt_frame
this )
333 mlt_deque_close( this->stack_image
);
334 mlt_deque_close( this->stack_audio
);
335 mlt_properties_close( &this->parent
);
340 /***** convenience functions *****/
342 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
345 register int y0
, y1
, u0
, u1
, v0
, v1
;
346 register int r
, g
, b
;
347 register uint8_t *d
= yuv
;
350 for ( i
= 0; i
< height
; i
++ )
352 register uint8_t *s
= rgba
+ ( stride
* i
);
353 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
359 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
364 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
376 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
384 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
387 register int y0
, y1
, u0
, u1
, v0
, v1
;
388 register int r
, g
, b
;
389 register uint8_t *d
= yuv
;
392 for ( i
= 0; i
< height
; i
++ )
394 register uint8_t *s
= rgb
+ ( stride
* i
);
395 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
400 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
404 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
415 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
423 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p
, int width
, int height
, int stride
, uint8_t *yuv
)
428 int half
= width
>> 1;
430 uint8_t *Y
= yuv420p
;
431 uint8_t *U
= Y
+ width
* height
;
432 uint8_t *V
= U
+ width
* height
/ 4;
434 register uint8_t *d
= yuv
;
436 for ( i
= 0; i
< height
; i
++ )
438 register uint8_t *u
= U
+ ( i
/ 2 ) * ( half
);
439 register uint8_t *v
= V
+ ( i
/ 2 ) * ( half
);
441 for ( j
= 0; j
< half
; j
++ )
452 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
455 int istride
= iwidth
* 2;
456 int ostride
= owidth
* 2;
458 iwidth
= iwidth
- ( iwidth
% 4 );
459 owidth
= owidth
- ( owidth
% 4 );
460 iheight
= iheight
- ( iheight
% 2 );
461 oheight
= oheight
- ( oheight
% 2 );
463 // Optimisation point
464 if ( iwidth
== owidth
&& iheight
== oheight
)
465 memcpy( output
, input
, iheight
* istride
);
467 // Coordinates (0,0 is middle of output)
471 int out_x_range
= owidth
/ 2;
472 int out_y_range
= oheight
/ 2;
473 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
474 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
477 uint8_t *out_line
= output
;
478 uint8_t *out_ptr
= out_line
;
480 // Calculate a middle and possibly invalid pointer in the input
481 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
482 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
486 // Fill whole section with black
487 y
= out_y_range
- ( iheight
/ 2 );
488 int blank_elements
= ostride
* y
/ 2;
489 elements
= blank_elements
;
490 while ( elements
-- )
496 int active_width
= 2 * iwidth
;
497 int inactive_width
= out_x_range
- in_x_range
;
501 // Loop for the entirety of our output height.
504 // Start at the beginning of the line
507 // Fill the outer part with black
508 elements
= inactive_width
;
509 while ( elements
-- )
515 // We're in the input range for this row.
516 p
= in_middle
+ in_line
;
517 end
= out_ptr
+ active_width
;
518 while ( out_ptr
!= end
)
524 // Fill the outer part with black
525 elements
= inactive_width
;
526 while ( elements
-- )
532 // Move to next input line
535 // Move to next output line
539 // Fill whole section with black
540 elements
= blank_elements
;
541 while ( elements
-- )
548 /** A resizing function for yuv422 frames - this does not rescale, but simply
549 resizes. It assumes yuv422 images available on the frame so use with care.
552 uint8_t *mlt_frame_resize_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 );
568 // Call the generic resize
569 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
571 // Now update the frame
572 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
573 mlt_properties_set_int( properties
, "width", owidth
);
574 mlt_properties_set_int( properties
, "height", oheight
);
579 // No change, return input
583 /** A rescaling function for yuv422 frames - low quality, and provided for testing
584 only. It assumes yuv422 images available on the frame so use with care.
587 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
590 mlt_properties properties
= mlt_frame_properties( this );
592 // Get the input image, width and height
593 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
594 int iwidth
= mlt_properties_get_int( properties
, "width" );
595 int iheight
= mlt_properties_get_int( properties
, "height" );
597 // If width and height are correct, don't do anything
598 if ( iwidth
!= owidth
|| iheight
!= oheight
)
600 // Create the output image
601 uint8_t *output
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
604 int istride
= iwidth
* 2;
605 int ostride
= owidth
* 2;
607 iwidth
= iwidth
- ( iwidth
% 4 );
609 // Derived coordinates
613 int out_x_range
= owidth
/ 2;
614 int out_y_range
= oheight
/ 2;
615 int in_x_range
= iwidth
/ 2;
616 int in_y_range
= iheight
/ 2;
619 register uint8_t *out_line
= output
;
620 register uint8_t *out_ptr
;
622 // Calculate a middle pointer
623 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
626 // Generate the affine transform scaling values
627 register int scale_width
= ( iwidth
<< 16 ) / owidth
;
628 register int scale_height
= ( iheight
<< 16 ) / oheight
;
629 register int base
= 0;
631 int outer
= out_x_range
* scale_width
;
632 int bottom
= out_y_range
* scale_height
;
634 // Loop for the entirety of our output height.
635 for ( dy
= - bottom
; dy
< bottom
; dy
+= scale_height
)
637 // Start at the beginning of the line
640 // Pointer to the middle of the input line
641 in_line
= in_middle
+ ( dy
>> 16 ) * istride
;
643 // Loop for the entirety of our output row.
644 for ( dx
= - outer
; dx
< outer
; dx
+= scale_width
)
648 *out_ptr
++ = *( in_line
+ base
);
650 *out_ptr
++ = *( in_line
+ base
+ 1 );
654 *out_ptr
++ = *( in_line
+ base
);
656 *out_ptr
++ = *( in_line
+ base
+ 3 );
659 // Move to next output line
663 // Now update the frame
664 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
665 mlt_properties_set_int( properties
, "width", owidth
);
666 mlt_properties_set_int( properties
, "height", oheight
);
672 // No change, return input
676 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
)
680 int frequency_src
= *frequency
, frequency_dest
= *frequency
;
681 int channels_src
= *channels
, channels_dest
= *channels
;
682 int samples_src
= *samples
, samples_dest
= *samples
;
686 mlt_frame_get_audio( this, &dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
687 //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" ) );
688 mlt_frame_get_audio( that
, &src
, format
, &frequency_src
, &channels_src
, &samples_src
);
689 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
691 if ( channels_src
> 6 )
693 if ( channels_dest
> 6 )
695 if ( samples_src
> 4000 )
697 if ( samples_dest
> 4000 )
700 // determine number of samples to process
701 *samples
= samples_src
< samples_dest ? samples_src
: samples_dest
;
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
;
727 int mlt_sample_calculator( float fps
, int frequency
, int64_t position
)
731 if ( fps
> 29 && fps
<= 30 )
733 samples
= frequency
/ 30;
738 if ( position
% 5 != 0 )
742 if ( position
% 300 == 0 )
744 else if ( position
% 30 == 0 )
746 else if ( position
% 2 == 0 )
752 if ( position
% 30 == 0 )
754 else if ( position
% 29 == 0 )
756 else if ( position
% 4 == 2 )
767 samples
= frequency
/ fps
;