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 // Initialise the properties
39 mlt_properties properties
= &this->parent
;
40 mlt_properties_init( properties
, this );
42 // Set default properties on the frame
43 mlt_properties_set_position( properties
, "_position", 0.0 );
44 mlt_properties_set_data( properties
, "image", NULL
, 0, NULL
, NULL
);
45 mlt_properties_set_int( properties
, "width", 720 );
46 mlt_properties_set_int( properties
, "height", 576 );
47 mlt_properties_set_double( properties
, "aspect_ratio", 4.0 / 3.0 );
48 mlt_properties_set_data( properties
, "audio", NULL
, 0, NULL
, NULL
);
49 mlt_properties_set_data( properties
, "alpha", NULL
, 0, NULL
, NULL
);
54 /** Fetch the frames properties.
57 mlt_properties
mlt_frame_properties( mlt_frame
this )
62 /** Check if we have a way to derive something other than a test card.
65 int mlt_frame_is_test_card( mlt_frame
this )
67 return mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
70 /** Check if we have a way to derive something than test audio.
73 int mlt_frame_is_test_audio( mlt_frame
this )
75 return this->get_audio
== NULL
|| mlt_properties_get_int( mlt_frame_properties( this ), "test_audio" );
78 /** Get the aspect ratio of the frame.
81 double mlt_frame_get_aspect_ratio( mlt_frame
this )
83 return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
86 /** Set the aspect ratio of the frame.
89 int mlt_frame_set_aspect_ratio( mlt_frame
this, double value
)
91 return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value
);
94 /** Get the position of this frame.
97 mlt_position
mlt_frame_get_position( mlt_frame
this )
99 return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
102 /** Set the position of this frame.
105 int mlt_frame_set_position( mlt_frame
this, mlt_position value
)
107 return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value
);
110 /** Stack a get_image callback.
113 int mlt_frame_push_get_image( mlt_frame
this, mlt_get_image get_image
)
115 int ret
= this->stack_get_image_size
>= 10;
117 this->stack_get_image
[ this->stack_get_image_size
++ ] = get_image
;
121 /** Pop a get_image callback.
124 mlt_get_image
mlt_frame_pop_get_image( mlt_frame
this )
126 mlt_get_image result
= NULL
;
127 if ( this->stack_get_image_size
> 0 )
128 result
= this->stack_get_image
[ -- this->stack_get_image_size
];
135 int mlt_frame_push_frame( mlt_frame
this, mlt_frame that
)
137 int ret
= this->stack_frame_size
>= 10;
139 this->stack_frame
[ this->stack_frame_size
++ ] = that
;
146 mlt_frame
mlt_frame_pop_frame( mlt_frame
this )
148 mlt_frame result
= NULL
;
149 if ( this->stack_frame_size
> 0 )
150 result
= this->stack_frame
[ -- this->stack_frame_size
];
154 int mlt_frame_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
156 mlt_properties properties
= mlt_frame_properties( this );
157 mlt_get_image get_image
= mlt_frame_pop_get_image( this );
158 mlt_producer producer
= mlt_properties_get_data( properties
, "test_card_producer", NULL
);
160 if ( get_image
!= NULL
)
162 return get_image( this, buffer
, format
, width
, height
, writable
);
164 else if ( mlt_properties_get_data( properties
, "image", NULL
) != NULL
)
166 *format
= mlt_image_yuv422
;
167 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
168 *width
= mlt_properties_get_int( properties
, "width" );
169 *height
= mlt_properties_get_int( properties
, "height" );
171 else if ( producer
!= NULL
)
173 mlt_frame test_frame
= NULL
;
174 mlt_service_get_frame( mlt_producer_service( producer
), &test_frame
, 0 );
175 if ( test_frame
!= NULL
)
177 mlt_frame_get_image( test_frame
, buffer
, format
, width
, height
, writable
);
178 mlt_properties_inherit( mlt_frame_properties( this ), mlt_frame_properties( test_frame
) );
179 mlt_properties_set_data( properties
, "test_card_frame", test_frame
, 0, ( mlt_destructor
)mlt_frame_close
, NULL
);
181 mlt_properties_set_data( properties
, "image", *buffer
, *width
* *height
* 2, NULL
, NULL
);
182 mlt_properties_set_int( properties
, "width", *width
);
183 mlt_properties_set_int( properties
, "height", *height
);
187 mlt_properties_set_data( properties
, "test_card_producer", NULL
, 0, NULL
, NULL
);
188 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
197 *width
= *width
== 0 ?
720 : *width
;
198 *height
= *height
== 0 ?
576 : *height
;
199 size
= *width
* *height
;
201 mlt_properties_set_int( properties
, "width", *width
);
202 mlt_properties_set_int( properties
, "height", *height
);
210 case mlt_image_rgb24
:
214 *buffer
= malloc( size
);
216 memset( *buffer
, 255, size
);
218 case mlt_image_rgb24a
:
222 *buffer
= malloc( size
);
224 memset( *buffer
, 255, size
);
226 case mlt_image_yuv422
:
230 *buffer
= malloc( size
);
233 while ( p
!= NULL
&& p
!= q
)
239 case mlt_image_yuv420p
:
241 *buffer
= malloc( size
);
243 memset( *buffer
, 255, size
);
247 mlt_properties_set_data( properties
, "image", *buffer
, size
, free
, NULL
);
248 mlt_properties_set_int( properties
, "test_image", 1 );
254 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
256 if ( this->get_alpha_mask
!= NULL
)
257 return this->get_alpha_mask( this );
261 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
263 mlt_properties properties
= mlt_frame_properties( this );
265 if ( this->get_audio
!= NULL
)
267 return this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
272 *samples
= *samples
<= 0 ?
1920 : *samples
;
273 *channels
= *channels
<= 0 ?
2 : *channels
;
274 *frequency
= *frequency
<= 0 ?
48000 : *frequency
;
275 size
= *samples
* *channels
* sizeof( int16_t );
276 *buffer
= malloc( size
);
277 if ( *buffer
!= NULL
)
278 memset( *buffer
, 0, size
);
279 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
280 mlt_properties_set_int( properties
, "test_audio", 1 );
285 void mlt_frame_close( mlt_frame
this )
287 mlt_properties_close( &this->parent
);
291 /***** convenience functions *****/
292 #define RGB2YUV(r, g, b, y, u, v)\
293 y = (306*r + 601*g + 117*b) >> 10;\
294 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
295 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
299 y = y > 255 ? 255 : y;\
300 u = u > 255 ? 255 : u;\
301 v = v > 255 ? 255 : v
303 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
306 register int y0
, y1
, u0
, u1
, v0
, v1
;
307 register int r
, g
, b
;
308 register uint8_t *d
= yuv
;
311 for ( i
= 0; i
< height
; i
++ )
313 register uint8_t *s
= rgba
+ ( stride
* i
);
314 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
320 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
325 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
337 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
345 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
348 register int y0
, y1
, u0
, u1
, v0
, v1
;
349 register int r
, g
, b
;
350 register uint8_t *d
= yuv
;
353 for ( i
= 0; i
< height
; i
++ )
355 register uint8_t *s
= rgb
+ ( stride
* i
);
356 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
361 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
365 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
376 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
384 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p
, int width
, int height
, int stride
, uint8_t *yuv
)
389 int half
= width
>> 1;
391 uint8_t *Y
= yuv420p
;
392 uint8_t *U
= Y
+ width
* height
;
393 uint8_t *V
= U
+ width
* height
/ 4;
395 register uint8_t *d
= yuv
;
397 for ( i
= 0; i
< height
; i
++ )
399 register uint8_t *u
= U
+ ( i
/ 2 ) * ( half
);
400 register uint8_t *v
= V
+ ( i
/ 2 ) * ( half
);
402 for ( j
= 0; j
< half
; j
++ )
413 void *memfill( void *dst
, void *src
, int l
, int elements
)
420 uint8_t *src2
= src
+ 1;
421 for ( i
= 0; i
< elements
; i
++ )
430 for ( i
= 0; i
< elements
; i
++ )
431 dst
= memcpy( dst
, src
, l
) + l
;
436 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
439 int istride
= iwidth
* 2;
440 int ostride
= owidth
* 2;
442 iwidth
= iwidth
- ( iwidth
% 4 );
443 owidth
= owidth
- ( owidth
% 4 );
444 iheight
= iheight
- ( iheight
% 2 );
445 oheight
= oheight
- ( oheight
% 2 );
447 // Optimisation point
448 if ( iwidth
== owidth
&& iheight
== oheight
)
449 memcpy( output
, input
, iheight
* istride
);
451 // Coordinates (0,0 is middle of output)
455 int out_x_range
= owidth
/ 2;
456 int out_y_range
= oheight
/ 2;
457 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
458 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
461 uint8_t *out_line
= output
;
462 uint8_t *out_ptr
= out_line
;
464 // Calculate a middle and possibly invalid pointer in the input
465 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
466 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
468 uint8_t black
[ 2 ] = { 16, 128 };
470 // Loop for the entirety of our output height.
471 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
473 // Start at the beginning of the line
476 if ( abs( y
) < iheight
/ 2 )
478 // Fill the outer part with black
479 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
481 // We're in the input range for this row.
482 memcpy( out_ptr
, in_middle
+ in_line
, 2 * iwidth
);
483 out_ptr
+= 2 * iwidth
;
485 // Fill the outer part with black
486 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
488 // Move to next input line
493 // Fill whole line with black
494 out_ptr
= memfill( out_ptr
, black
, 2, owidth
);
497 // Move to next output line
502 /** A resizing function for yuv422 frames - this does not rescale, but simply
503 resizes. It assumes yuv422 images available on the frame so use with care.
506 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
509 mlt_properties properties
= mlt_frame_properties( this );
511 // Get the input image, width and height
512 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
513 int iwidth
= mlt_properties_get_int( properties
, "width" );
514 int iheight
= mlt_properties_get_int( properties
, "height" );
516 // If width and height are correct, don't do anything
517 if ( iwidth
!= owidth
|| iheight
!= oheight
)
519 // Create the output image
521 uint8_t *output
= malloc( owidth
* ( oheight
+ 1 ) * 2 );
523 // Call the generic resize
524 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
526 // Now update the frame
527 mlt_properties_set_data( properties
, "image", output
, owidth
* ( oheight
+ 1 ) * 2, free
, NULL
);
528 mlt_properties_set_int( properties
, "width", owidth
);
529 mlt_properties_set_int( properties
, "height", oheight
);
534 // No change, return input
538 /** A rescaling function for yuv422 frames - low quality, and provided for testing
539 only. It assumes yuv422 images available on the frame so use with care.
542 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
545 mlt_properties properties
= mlt_frame_properties( this );
547 // Get the input image, width and height
548 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
549 int iwidth
= mlt_properties_get_int( properties
, "width" );
550 int iheight
= mlt_properties_get_int( properties
, "height" );
552 // If width and height are correct, don't do anything
553 if ( iwidth
!= owidth
|| iheight
!= oheight
)
555 // Create the output image
557 uint8_t *output
= malloc( owidth
* ( oheight
+ 1 ) * 2 );
560 int istride
= iwidth
* 2;
561 int ostride
= owidth
* 2;
563 iwidth
= iwidth
- ( iwidth
% 4 );
565 // Coordinates (0,0 is middle of output)
568 // Derived coordinates
572 int out_x_range
= owidth
/ 2;
573 int out_y_range
= oheight
/ 2;
574 int in_x_range
= iwidth
/ 2;
575 int in_y_range
= iheight
/ 2;
578 uint8_t *out_line
= output
;
581 // Calculate a middle pointer
582 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
586 // Generate the affine transform scaling values
587 int scale_width
= ( iwidth
<< 16 ) / owidth
;
588 int scale_height
= ( iheight
<< 16 ) / oheight
;
590 // Loop for the entirety of our output height.
591 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
593 // Calculate the derived y value
594 dy
= ( scale_height
* y
) >> 16;
596 // Start at the beginning of the line
599 // Pointer to the middle of the input line
600 in_line
= in_middle
+ dy
* istride
;
602 // Loop for the entirety of our output row.
603 for ( x
= - out_x_range
; x
< out_x_range
; x
+= 1 )
605 // Calculated the derived x
606 dx
= ( scale_width
* x
) >> 16;
608 // We're in the input range for this row.
609 in_ptr
= in_line
+ ( dx
<< 1 );
610 *out_ptr
++ = *in_ptr
++;
611 in_ptr
= in_line
+ ( ( dx
>> 1 ) << 2 ) + ( ( x
& 1 ) << 1 ) + 1;
612 *out_ptr
++ = *in_ptr
;
615 // Move to next output line
619 // Now update the frame
620 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
621 mlt_properties_set_int( properties
, "width", owidth
);
622 mlt_properties_set_int( properties
, "height", oheight
);
628 // No change, return input
632 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
)
635 int16_t *p_src
, *p_dest
;
637 //static int16_t *extra_src = NULL, *extra_dest = NULL;
638 static int extra_src_samples
= 0, extra_dest_samples
= 0;
639 int frequency_src
= *frequency
, frequency_dest
= *frequency
;
640 int channels_src
= *channels
, channels_dest
= *channels
;
641 int samples_src
= *samples
, samples_dest
= *samples
;
645 mlt_frame_get_audio( this, &p_dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
646 //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" ) );
647 mlt_frame_get_audio( that
, &p_src
, format
, &frequency_src
, &channels_src
, &samples_src
);
648 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
651 if ( channels_src
> 6 )
653 if ( channels_dest
> 6 )
655 if ( samples_src
> 4000 )
657 if ( samples_dest
> 4000 )
661 // Append new samples to leftovers
662 if ( extra_dest_samples
> 0 )
664 fprintf( stderr
, "prepending %d samples to dest\n", extra_dest_samples
);
665 dest
= realloc( extra_dest
, ( samples_dest
+ extra_dest_samples
) * 2 * channels_dest
);
666 memcpy( &extra_dest
[ extra_dest_samples
* channels_dest
], p_dest
, samples_dest
* 2 * channels_dest
);
670 if ( extra_src_samples
> 0 )
672 fprintf( stderr
, "prepending %d samples to src\n", extra_src_samples
);
673 src
= realloc( extra_src
, ( samples_src
+ extra_src_samples
) * 2 * channels_src
);
674 memcpy( &extra_src
[ extra_src_samples
* channels_src
], p_src
, samples_src
* 2 * channels_src
);
680 // determine number of samples to process
681 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
682 *samples
= samples_src
+ extra_src_samples
;
683 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
684 *samples
= samples_dest
+ extra_dest_samples
;
686 *channels
= channels_src
< channels_dest ? channels_src
: channels_dest
;
688 *frequency
= frequency_dest
;
690 // Compute a smooth ramp over start to end
691 float weight
= weight_start
;
692 float weight_step
= ( weight_end
- weight_start
) / *samples
;
695 for ( i
= 0; i
< *samples
; i
++ )
697 for ( j
= 0; j
< *channels
; j
++ )
699 if ( j
< channels_dest
)
700 d
= (double) dest
[ i
* channels_dest
+ j
];
701 if ( j
< channels_src
)
702 s
= (double) src
[ i
* channels_src
+ j
];
703 dest
[ i
* channels_dest
+ j
] = s
* weight
+ d
* ( 1.0 - weight
);
705 weight
+= weight_step
;
708 // We have to copy --sigh
709 if ( dest
!= p_dest
)
710 memcpy( p_dest
, dest
, *samples
* 2 * *channels
);
713 // Store the leftovers
714 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
716 extra_dest_samples
= ( samples_dest
+ extra_dest_samples
) - ( samples_src
+ extra_src_samples
);
717 size_t size
= extra_dest_samples
* 2 * channels_dest
;
718 fprintf( stderr
, "storing %d samples from dest\n", extra_dest_samples
);
721 extra_dest
= malloc( size
);
723 memcpy( extra_dest
, &p_dest
[ ( samples_dest
- extra_dest_samples
- 1 ) * channels_dest
], size
);
725 extra_dest_samples
= 0;
727 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
729 extra_src_samples
= ( samples_src
+ extra_src_samples
) - ( samples_dest
+ extra_dest_samples
);
730 size_t size
= extra_src_samples
* 2 * channels_src
;
731 fprintf( stderr
, "storing %d samples from src\n", extra_dest_samples
);
734 extra_src
= malloc( size
);
736 memcpy( extra_src
, &p_src
[ ( samples_src
- extra_src_samples
- 1 ) * channels_src
], size
);
738 extra_src_samples
= 0;
745 int mlt_sample_calculator( float fps
, int frequency
, int64_t position
)
749 if ( fps
> 29 && fps
<= 30 )
751 samples
= frequency
/ 30;
756 if ( position
% 5 != 0 )
760 if ( position
% 300 == 0 )
762 else if ( position
% 30 == 0 )
764 else if ( position
% 2 == 0 )
770 if ( position
% 30 == 0 )
772 else if ( position
% 29 == 0 )
774 else if ( position
% 4 == 2 )
785 samples
= frequency
/ fps
;