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 );
59 mlt_properties_set_int( properties
, "width", 720 );
60 mlt_properties_set_int( properties
, "height", 480 );
61 mlt_properties_set_int( properties
, "normalised_width", 720 );
62 mlt_properties_set_int( properties
, "normalised_height", 480 );
65 mlt_properties_set_double( properties
, "aspect_ratio", 4.0 / 3.0 );
66 mlt_properties_set_data( properties
, "audio", NULL
, 0, NULL
, NULL
);
67 mlt_properties_set_data( properties
, "alpha", NULL
, 0, NULL
, NULL
);
69 // Construct stacks for frames and methods
70 this->stack_get_image
= mlt_deque_init( );
71 this->stack_frame
= mlt_deque_init( );
72 this->stack_service
= 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_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_get_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_get_image
);
153 int mlt_frame_push_frame( mlt_frame
this, mlt_frame that
)
155 return mlt_deque_push_back( this->stack_frame
, that
);
161 mlt_frame
mlt_frame_pop_frame( mlt_frame
this )
163 return mlt_deque_pop_back( this->stack_frame
);
169 int mlt_frame_push_service( mlt_frame
this, void *that
)
171 return mlt_deque_push_back( this->stack_service
, that
);
177 void *mlt_frame_pop_service( mlt_frame
this )
179 return mlt_deque_pop_back( this->stack_service
);
182 int mlt_frame_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
184 mlt_properties properties
= mlt_frame_properties( this );
185 mlt_get_image get_image
= mlt_frame_pop_get_image( this );
186 mlt_producer producer
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
188 if ( get_image
!= NULL
)
190 return get_image( this, buffer
, format
, width
, height
, writable
);
192 else if ( mlt_properties_get_data( properties
, "image", NULL
) != NULL
)
194 *format
= mlt_image_yuv422
;
195 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
196 *width
= mlt_properties_get_int( properties
, "width" );
197 *height
= mlt_properties_get_int( properties
, "height" );
199 else if ( producer
!= NULL
)
201 mlt_frame test_frame
= NULL
;
202 mlt_service_get_frame( mlt_producer_service( producer
), &test_frame
, 0 );
203 if ( test_frame
!= NULL
)
205 mlt_properties test_properties
= mlt_frame_properties( test_frame
);
206 mlt_properties_set_double( test_properties
, "consumer_aspect_ratio", mlt_properties_get_double( properties
, "consumer_aspect_ratio" ) );
207 mlt_properties_set_double( test_properties
, "consumer_scale", mlt_properties_get_double( properties
, "consumer_scale" ) );
208 mlt_properties_set( test_properties
, "rescale.interp", "nearest" );
209 mlt_frame_get_image( test_frame
, buffer
, format
, width
, height
, writable
);
210 mlt_properties_set_data( properties
, "test_card_frame", test_frame
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
211 mlt_properties_set_data( properties
, "image", *buffer
, *width
* *height
* 2, NULL
, NULL
);
212 mlt_properties_set_int( properties
, "width", *width
);
213 mlt_properties_set_int( properties
, "height", *height
);
217 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
218 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
227 *width
= *width
== 0 ?
720 : *width
;
228 *height
= *height
== 0 ?
576 : *height
;
229 size
= *width
* *height
;
231 mlt_properties_set_int( properties
, "width", *width
);
232 mlt_properties_set_int( properties
, "height", *height
);
240 case mlt_image_rgb24
:
243 *buffer
= mlt_pool_alloc( size
);
245 memset( *buffer
, 255, size
);
247 case mlt_image_rgb24a
:
250 *buffer
= mlt_pool_alloc( size
);
252 memset( *buffer
, 255, size
);
254 case mlt_image_yuv422
:
257 *buffer
= mlt_pool_alloc( size
);
260 while ( p
!= NULL
&& p
!= q
)
266 case mlt_image_yuv420p
:
268 *buffer
= mlt_pool_alloc( size
);
270 memset( *buffer
, 255, size
);
274 mlt_properties_set_data( properties
, "image", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
275 mlt_properties_set_int( properties
, "test_image", 1 );
281 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
283 if ( this->get_alpha_mask
!= NULL
)
284 return this->get_alpha_mask( this );
288 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
290 mlt_properties properties
= mlt_frame_properties( this );
292 if ( this->get_audio
!= NULL
)
294 return this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
299 *samples
= *samples
<= 0 ?
1920 : *samples
;
300 *channels
= *channels
<= 0 ?
2 : *channels
;
301 *frequency
= *frequency
<= 0 ?
48000 : *frequency
;
302 size
= *samples
* *channels
* sizeof( int16_t );
303 *buffer
= mlt_pool_alloc( size
);
304 if ( *buffer
!= NULL
)
305 memset( *buffer
, 0, size
);
306 mlt_properties_set_data( properties
, "audio", *buffer
, size
, ( mlt_destructor
)mlt_pool_release
, NULL
);
307 mlt_properties_set_int( properties
, "test_audio", 1 );
312 void mlt_frame_close( mlt_frame
this )
316 mlt_deque_close( this->stack_get_image
);
317 mlt_deque_close( this->stack_frame
);
318 mlt_deque_close( this->stack_service
);
319 mlt_properties_close( &this->parent
);
324 /***** convenience functions *****/
325 #define RGB2YUV(r, g, b, y, u, v)\
326 y = (306*r + 601*g + 117*b) >> 10;\
327 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
328 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
329 y = y < 16 ? 16 : y;\
330 u = u < 16 ? 16 : u;\
331 v = v < 16 ? 16 : v;\
332 y = y > 235 ? 235 : y;\
333 u = u > 240 ? 240 : u;\
334 v = v > 240 ? 240 : v
336 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
339 register int y0
, y1
, u0
, u1
, v0
, v1
;
340 register int r
, g
, b
;
341 register uint8_t *d
= yuv
;
344 for ( i
= 0; i
< height
; i
++ )
346 register uint8_t *s
= rgba
+ ( stride
* i
);
347 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
353 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
358 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
370 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
378 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
381 register int y0
, y1
, u0
, u1
, v0
, v1
;
382 register int r
, g
, b
;
383 register uint8_t *d
= yuv
;
386 for ( i
= 0; i
< height
; i
++ )
388 register uint8_t *s
= rgb
+ ( stride
* i
);
389 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
394 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
398 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
409 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
417 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p
, int width
, int height
, int stride
, uint8_t *yuv
)
422 int half
= width
>> 1;
424 uint8_t *Y
= yuv420p
;
425 uint8_t *U
= Y
+ width
* height
;
426 uint8_t *V
= U
+ width
* height
/ 4;
428 register uint8_t *d
= yuv
;
430 for ( i
= 0; i
< height
; i
++ )
432 register uint8_t *u
= U
+ ( i
/ 2 ) * ( half
);
433 register uint8_t *v
= V
+ ( i
/ 2 ) * ( half
);
435 for ( j
= 0; j
< half
; j
++ )
446 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
449 int istride
= iwidth
* 2;
450 int ostride
= owidth
* 2;
452 iwidth
= iwidth
- ( iwidth
% 4 );
453 owidth
= owidth
- ( owidth
% 4 );
454 iheight
= iheight
- ( iheight
% 2 );
455 oheight
= oheight
- ( oheight
% 2 );
457 // Optimisation point
458 if ( iwidth
== owidth
&& iheight
== oheight
)
459 memcpy( output
, input
, iheight
* istride
);
461 // Coordinates (0,0 is middle of output)
465 int out_x_range
= owidth
/ 2;
466 int out_y_range
= oheight
/ 2;
467 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
468 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
471 uint8_t *out_line
= output
;
472 uint8_t *out_ptr
= out_line
;
474 // Calculate a middle and possibly invalid pointer in the input
475 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
476 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
480 // Fill whole section with black
481 y
= out_y_range
- ( iheight
/ 2 );
482 int blank_elements
= ostride
* y
/ 2;
483 elements
= blank_elements
;
484 while ( elements
-- )
490 int active_width
= 2 * iwidth
;
491 int inactive_width
= out_x_range
- in_x_range
;
493 // Loop for the entirety of our output height.
496 // Start at the beginning of the line
499 // Fill the outer part with black
500 elements
= inactive_width
;
501 while ( elements
-- )
507 // We're in the input range for this row.
508 memcpy( out_ptr
, in_middle
+ in_line
, active_width
);
509 out_ptr
+= active_width
;
511 // Fill the outer part with black
512 elements
= inactive_width
;
513 while ( elements
-- )
519 // Move to next input line
522 // Move to next output line
526 // Fill whole section with black
527 elements
= blank_elements
;
528 while ( elements
-- )
535 /** A resizing function for yuv422 frames - this does not rescale, but simply
536 resizes. It assumes yuv422 images available on the frame so use with care.
539 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
542 mlt_properties properties
= mlt_frame_properties( this );
544 // Get the input image, width and height
545 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
546 int iwidth
= mlt_properties_get_int( properties
, "width" );
547 int iheight
= mlt_properties_get_int( properties
, "height" );
549 // If width and height are correct, don't do anything
550 if ( iwidth
!= owidth
|| iheight
!= oheight
)
552 // Create the output image
553 uint8_t *output
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
555 // Call the generic resize
556 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
558 // Now update the frame
559 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
560 mlt_properties_set_int( properties
, "width", owidth
);
561 mlt_properties_set_int( properties
, "height", oheight
);
566 // No change, return input
570 /** A rescaling function for yuv422 frames - low quality, and provided for testing
571 only. It assumes yuv422 images available on the frame so use with care.
574 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
577 mlt_properties properties
= mlt_frame_properties( this );
579 // Get the input image, width and height
580 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
581 int iwidth
= mlt_properties_get_int( properties
, "width" );
582 int iheight
= mlt_properties_get_int( properties
, "height" );
584 // If width and height are correct, don't do anything
585 if ( iwidth
!= owidth
|| iheight
!= oheight
)
587 // Create the output image
588 uint8_t *output
= mlt_pool_alloc( owidth
* ( oheight
+ 1 ) * 2 );
591 int istride
= iwidth
* 2;
592 int ostride
= owidth
* 2;
594 iwidth
= iwidth
- ( iwidth
% 4 );
596 // Derived coordinates
600 int out_x_range
= owidth
/ 2;
601 int out_y_range
= oheight
/ 2;
602 int in_x_range
= iwidth
/ 2;
603 int in_y_range
= iheight
/ 2;
606 register uint8_t *out_line
= output
;
607 register uint8_t *out_ptr
;
609 // Calculate a middle pointer
610 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
613 // Generate the affine transform scaling values
614 register int scale_width
= ( iwidth
<< 16 ) / owidth
;
615 register int scale_height
= ( iheight
<< 16 ) / oheight
;
616 register int base
= 0;
618 int outer
= out_x_range
* scale_width
;
619 int bottom
= out_y_range
* scale_height
;
621 // Loop for the entirety of our output height.
622 for ( dy
= - bottom
; dy
< bottom
; dy
+= scale_height
)
624 // Start at the beginning of the line
627 // Pointer to the middle of the input line
628 in_line
= in_middle
+ ( dy
>> 16 ) * istride
;
630 // Loop for the entirety of our output row.
631 for ( dx
= - outer
; dx
< outer
; dx
+= scale_width
)
635 *out_ptr
++ = *( in_line
+ base
);
637 *out_ptr
++ = *( in_line
+ base
+ 1 );
641 *out_ptr
++ = *( in_line
+ base
);
643 *out_ptr
++ = *( in_line
+ base
+ 3 );
646 // Move to next output line
650 // Now update the frame
651 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
652 mlt_properties_set_int( properties
, "width", owidth
);
653 mlt_properties_set_int( properties
, "height", oheight
);
659 // No change, return input
663 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
)
667 int frequency_src
= *frequency
, frequency_dest
= *frequency
;
668 int channels_src
= *channels
, channels_dest
= *channels
;
669 int samples_src
= *samples
, samples_dest
= *samples
;
673 mlt_frame_get_audio( this, &dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
674 //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" ) );
675 mlt_frame_get_audio( that
, &src
, format
, &frequency_src
, &channels_src
, &samples_src
);
676 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
678 if ( channels_src
> 6 )
680 if ( channels_dest
> 6 )
682 if ( samples_src
> 4000 )
684 if ( samples_dest
> 4000 )
687 // determine number of samples to process
688 *samples
= samples_src
< samples_dest ? samples_src
: samples_dest
;
689 *channels
= channels_src
< channels_dest ? channels_src
: channels_dest
;
691 *frequency
= frequency_dest
;
693 // Compute a smooth ramp over start to end
694 float weight
= weight_start
;
695 float weight_step
= ( weight_end
- weight_start
) / *samples
;
698 for ( i
= 0; i
< *samples
; i
++ )
700 for ( j
= 0; j
< *channels
; j
++ )
702 if ( j
< channels_dest
)
703 d
= (double) dest
[ i
* channels_dest
+ j
];
704 if ( j
< channels_src
)
705 s
= (double) src
[ i
* channels_src
+ j
];
706 dest
[ i
* channels_dest
+ j
] = s
* weight
+ d
* ( 1.0 - weight
);
708 weight
+= weight_step
;
714 int mlt_sample_calculator( float fps
, int frequency
, int64_t position
)
718 if ( fps
> 29 && fps
<= 30 )
720 samples
= frequency
/ 30;
725 if ( position
% 5 != 0 )
729 if ( position
% 300 == 0 )
731 else if ( position
% 30 == 0 )
733 else if ( position
% 2 == 0 )
739 if ( position
% 30 == 0 )
741 else if ( position
% 29 == 0 )
743 else if ( position
% 4 == 2 )
754 samples
= frequency
/ fps
;