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"
29 mlt_image_format vfmt
;
34 mlt_audio_format afmt
;
39 static frame_test test_card
= { mlt_image_none
, 0, 0, NULL
, NULL
, mlt_audio_none
, NULL
};
41 /** Constructor for a frame.
44 mlt_frame
mlt_frame_init( )
47 mlt_frame
this = calloc( sizeof( struct mlt_frame_s
), 1 );
51 // Initialise the properties
52 mlt_properties properties
= &this->parent
;
53 mlt_properties_init( properties
, this );
55 // Set default properties on the frame
56 mlt_properties_set_position( properties
, "position", 0.0 );
57 mlt_properties_set_data( properties
, "image", NULL
, 0, NULL
, NULL
);
58 mlt_properties_set_int( properties
, "width", 720 );
59 mlt_properties_set_int( properties
, "height", 576 );
60 mlt_properties_set_double( properties
, "aspect_ratio", 4.0 / 3.0 );
61 mlt_properties_set_data( properties
, "audio", NULL
, 0, NULL
, NULL
);
62 mlt_properties_set_data( properties
, "alpha", NULL
, 0, NULL
, NULL
);
67 /** Fetch the frames properties.
70 mlt_properties
mlt_frame_properties( mlt_frame
this )
75 /** Check if we have a way to derive something other than a test card.
78 int mlt_frame_is_test_card( mlt_frame
this )
80 return this->stack_get_image_size
== 0;
83 /** Get the aspect ratio of the frame.
86 double mlt_frame_get_aspect_ratio( mlt_frame
this )
88 mlt_properties properties
= mlt_frame_properties( this );
89 return mlt_properties_get_double( properties
, "aspect_ratio" );
92 /** Set the aspect ratio of the frame.
95 int mlt_frame_set_aspect_ratio( mlt_frame
this, double value
)
97 mlt_properties properties
= mlt_frame_properties( this );
98 return mlt_properties_set_double( properties
, "aspect_ratio", value
);
101 /** Get the position of this frame.
104 mlt_position
mlt_frame_get_position( mlt_frame
this )
106 mlt_properties properties
= mlt_frame_properties( this );
107 return mlt_properties_get_position( properties
, "position" );
110 /** Set the position of this frame.
113 int mlt_frame_set_position( mlt_frame
this, mlt_position value
)
115 mlt_properties properties
= mlt_frame_properties( this );
116 return mlt_properties_set_position( properties
, "position", value
);
119 /** Stack a get_image callback.
122 int mlt_frame_push_get_image( mlt_frame
this, mlt_get_image get_image
)
124 int ret
= this->stack_get_image_size
>= 10;
126 this->stack_get_image
[ this->stack_get_image_size
++ ] = get_image
;
130 /** Pop a get_image callback.
133 mlt_get_image
mlt_frame_pop_get_image( mlt_frame
this )
135 mlt_get_image result
= NULL
;
136 if ( this->stack_get_image_size
> 0 )
137 result
= this->stack_get_image
[ -- this->stack_get_image_size
];
144 int mlt_frame_push_frame( mlt_frame
this, mlt_frame that
)
146 int ret
= this->stack_frame_size
>= 10;
148 this->stack_frame
[ this->stack_frame_size
++ ] = that
;
155 mlt_frame
mlt_frame_pop_frame( mlt_frame
this )
157 mlt_frame result
= NULL
;
158 if ( this->stack_frame_size
> 0 )
159 result
= this->stack_frame
[ -- this->stack_frame_size
];
163 int mlt_frame_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
165 mlt_properties properties
= mlt_frame_properties( this );
166 mlt_get_image get_image
= mlt_frame_pop_get_image( this );
168 if ( get_image
!= NULL
)
170 return get_image( this, buffer
, format
, width
, height
, writable
);
172 else if ( mlt_properties_get_data( properties
, "image", NULL
) != NULL
)
174 *format
= mlt_image_yuv422
;
175 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
176 *width
= mlt_properties_get_int( properties
, "width" );
177 *height
= mlt_properties_get_int( properties
, "height" );
181 if ( test_card
.vfmt
!= *format
)
186 test_card
.vfmt
= *format
;
187 test_card
.width
= *width
== 0 ?
720 : *width
;
188 test_card
.height
= *height
== 0 ?
576 : *height
;
194 case mlt_image_rgb24
:
195 test_card
.image
= realloc( test_card
.image
, test_card
.width
* test_card
.height
* 3 );
196 memset( test_card
.image
, 255, test_card
.width
* test_card
.height
* 3 );
198 case mlt_image_rgb24a
:
199 test_card
.image
= realloc( test_card
.image
, test_card
.width
* test_card
.height
* 4 );
200 memset( test_card
.image
, 255, test_card
.width
* test_card
.height
* 4 );
202 case mlt_image_yuv422
:
203 test_card
.image
= realloc( test_card
.image
, test_card
.width
* test_card
.height
* 2 );
205 q
= test_card
.image
+ test_card
.width
* test_card
.height
* 2;
212 case mlt_image_yuv420p
:
213 test_card
.image
= realloc( test_card
.image
, test_card
.width
* test_card
.height
* 3 / 2 );
214 memset( test_card
.image
, 255, test_card
.width
* test_card
.height
* 3 / 2 );
219 *width
= test_card
.width
;
220 *height
= test_card
.height
;
221 *buffer
= test_card
.image
;
227 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
229 if ( this->get_alpha_mask
!= NULL
)
230 return this->get_alpha_mask( this );
231 return test_card
.alpha
;
234 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
236 if ( this->get_audio
!= NULL
)
238 return this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
244 if ( *channels
<= 0 )
246 if ( *frequency
<= 0 )
248 if ( test_card
.audio
== NULL
|| test_card
.afmt
!= *format
)
250 test_card
.afmt
= *format
;
251 test_card
.audio
= realloc( test_card
.audio
, *samples
* *channels
* sizeof( int16_t ) );
252 memset( test_card
.audio
, 0, *samples
* *channels
* sizeof( int16_t ) );
255 *buffer
= test_card
.audio
;
260 void mlt_frame_close( mlt_frame
this )
262 mlt_frame frame
= mlt_frame_pop_frame( this );
264 while ( frame
!= NULL
)
266 mlt_frame_close( frame
);
267 frame
= mlt_frame_pop_frame( this );
270 mlt_properties_close( &this->parent
);
275 /***** convenience functions *****/
276 #define RGB2YUV(r, g, b, y, u, v)\
277 y = (306*r + 601*g + 117*b) >> 10;\
278 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
279 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
283 y = y > 255 ? 255 : y;\
284 u = u > 255 ? 255 : u;\
285 v = v > 255 ? 255 : v
287 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
290 register int y0
, y1
, u0
, u1
, v0
, v1
;
291 register int r
, g
, b
;
292 register uint8_t *d
= yuv
;
295 for ( i
= 0; i
< height
; i
++ )
297 register uint8_t *s
= rgba
+ ( stride
* i
);
298 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
304 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
309 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
321 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
329 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
332 register int y0
, y1
, u0
, u1
, v0
, v1
;
333 register int r
, g
, b
;
334 register uint8_t *d
= yuv
;
337 for ( i
= 0; i
< height
; i
++ )
339 register uint8_t *s
= rgb
+ ( stride
* i
);
340 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
345 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
349 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
360 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
368 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p
, int width
, int height
, int stride
, uint8_t *yuv
)
373 int half
= width
>> 1;
375 uint8_t *Y
= yuv420p
;
376 uint8_t *U
= Y
+ width
* height
;
377 uint8_t *V
= U
+ width
* height
/ 4;
379 register uint8_t *d
= yuv
;
381 for ( i
= 0; i
< height
; i
++ )
383 register uint8_t *u
= U
+ ( i
/ 2 ) * ( half
);
384 register uint8_t *v
= V
+ ( i
/ 2 ) * ( half
);
386 for ( j
= 0; j
< half
; j
++ )
397 int mlt_frame_composite_yuv( mlt_frame
this, mlt_frame that
, int x
, int y
, float weight
)
400 int width_src
= 0, height_src
= 0;
401 int width_dest
= 0, height_dest
= 0;
402 mlt_image_format format_src
= mlt_image_yuv422
, format_dest
= mlt_image_yuv422
;
403 uint8_t *p_src
, *p_dest
;
407 int x_src
= 0, y_src
= 0;
409 // optimization point - no work to do
410 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
413 format_src
= mlt_image_yuv422
;
414 format_dest
= mlt_image_yuv422
;
416 //fprintf( stderr, "call get_image on frame a\n"), fflush( stderr );
417 mlt_frame_get_image( this, &p_dest
, &format_dest
, &width_dest
, &height_dest
, 1 /* writable */ );
418 //fprintf( stderr, "call get_image on frame b\n"), fflush( stderr );
419 mlt_frame_get_image( that
, &p_src
, &format_src
, &width_src
, &height_src
, 0 /* writable */ );
421 //fprintf( stderr, "mlt_frame_composite_yuv %dx%d -> %dx%d\n", width_src, height_src, width_dest, height_dest );
424 stride_src
= width_src
* 2;
425 stride_dest
= width_dest
* 2;
427 // crop overlay off the left edge of frame
435 // crop overlay beyond right edge of frame
436 else if ( x
+ width_src
> width_dest
)
437 width_src
= width_dest
- x
;
439 // crop overlay off the top edge of the frame
445 // crop overlay below bottom edge of frame
446 else if ( y
+ height_src
> height_dest
)
447 height_src
= height_dest
- y
;
449 // offset pointer into overlay buffer based on cropping
450 p_src
+= x_src
* 2 + y_src
* stride_src
;
452 // offset pointer into frame buffer based upon positive, even coordinates only!
453 // if ( interlaced && y % 2 )
455 p_dest
+= ( x
< 0 ?
0 : x
) * 2 + ( y
< 0 ?
0 : y
) * stride_dest
;
457 // Get the alpha channel of the overlay
458 uint8_t *p_alpha
= mlt_frame_get_alpha_mask( that
);
460 // offset pointer into alpha channel based upon cropping
462 p_alpha
+= x_src
+ y_src
* stride_src
/ 2;
464 // now do the compositing only to cropped extents
465 for ( i
= 0; i
< height_src
; i
++ )
470 uint8_t *z
= p_alpha
;
472 for ( j
= 0; j
< width_src
; j
++ )
476 uint8_t a
= ( z
== NULL
) ?
255 : *z
++;
477 float value
= ( weight
* ( float ) a
/ 255.0 );
478 *o
++ = (uint8_t)( y
* value
+ *q
++ * ( 1 - value
) );
479 *o
++ = (uint8_t)( uv
* value
+ *q
++ * ( 1 - value
) );
483 p_dest
+= stride_dest
;
485 p_alpha
+= stride_src
/ 2;
491 void *memfill( void *dst
, void *src
, int l
, int elements
)
498 uint8_t *src2
= src
+ 1;
499 for ( i
= 0; i
< elements
; i
++ )
508 for ( i
= 0; i
< elements
; i
++ )
509 dst
= memcpy( dst
, src
, l
) + l
;
514 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
517 int istride
= iwidth
* 2;
518 int ostride
= owidth
* 2;
520 iwidth
= iwidth
- ( iwidth
% 4 );
521 owidth
= owidth
- ( owidth
% 4 );
522 iheight
= iheight
- ( iheight
% 2 );
523 oheight
= oheight
- ( oheight
% 2 );
525 // Coordinates (0,0 is middle of output)
529 int out_x_range
= owidth
/ 2;
530 int out_y_range
= oheight
/ 2;
531 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
532 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
535 uint8_t *out_line
= output
;
536 uint8_t *out_ptr
= out_line
;
538 // Calculate a middle and possibly invalid pointer in the input
539 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
540 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
542 uint8_t black
[ 2 ] = { 16, 128 };
544 // Loop for the entirety of our output height.
545 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
547 // Start at the beginning of the line
550 if ( abs( y
) < iheight
/ 2 )
552 // Fill the outer part with black
553 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
555 // We're in the input range for this row.
556 memcpy( out_ptr
, in_middle
+ in_line
, 2 * iwidth
);
557 out_ptr
+= 2 * iwidth
;
559 // Fill the outer part with black
560 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
562 // Move to next input line
567 // Fill whole line with black
568 out_ptr
= memfill( out_ptr
, black
, 2, owidth
);
571 // Move to next output line
576 /** A resizing function for yuv422 frames - this does not rescale, but simply
577 resizes. It assumes yuv422 images available on the frame so use with care.
580 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
583 mlt_properties properties
= mlt_frame_properties( this );
585 // Get the input image, width and height
586 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
587 int iwidth
= mlt_properties_get_int( properties
, "width" );
588 int iheight
= mlt_properties_get_int( properties
, "height" );
590 // If width and height are correct, don't do anything
591 if ( iwidth
!= owidth
|| iheight
!= oheight
)
593 // Create the output image
594 uint8_t *output
= malloc( owidth
* oheight
* 2 );
596 // Call the generic resize
597 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
599 // Now update the frame
600 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
601 mlt_properties_set_int( properties
, "width", owidth
);
602 mlt_properties_set_int( properties
, "height", oheight
);
608 // No change, return input
612 /** A rescaling function for yuv422 frames - low quality, and provided for testing
613 only. It assumes yuv422 images available on the frame so use with care.
616 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
619 mlt_properties properties
= mlt_frame_properties( this );
621 // Get the input image, width and height
622 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
623 int iwidth
= mlt_properties_get_int( properties
, "width" );
624 int iheight
= mlt_properties_get_int( properties
, "height" );
626 // If width and height are correct, don't do anything
627 if ( iwidth
!= owidth
|| iheight
!= oheight
)
629 // Create the output image
630 uint8_t *output
= malloc( owidth
* oheight
* 2 );
633 int istride
= iwidth
* 2;
634 int ostride
= owidth
* 2;
636 iwidth
= iwidth
- ( iwidth
% 4 );
638 // Coordinates (0,0 is middle of output)
641 // Derived coordinates
645 int out_x_range
= owidth
/ 2;
646 int out_y_range
= oheight
/ 2;
647 int in_x_range
= iwidth
/ 2;
648 int in_y_range
= iheight
/ 2;
651 uint8_t *out_line
= output
;
654 // Calculate a middle pointer
655 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
659 // Generate the affine transform scaling values
660 float scale_width
= ( float )iwidth
/ ( float )owidth
;
661 float scale_height
= ( float )iheight
/ ( float )oheight
;
663 // Loop for the entirety of our output height.
664 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
666 // Calculate the derived y value
667 dy
= scale_height
* y
;
669 // Start at the beginning of the line
672 // Pointer to the middle of the input line
673 in_line
= in_middle
+ dy
* istride
;
675 // Loop for the entirety of our output row.
676 for ( x
= - out_x_range
; x
< out_x_range
; x
+= 1 )
678 // Calculated the derived x
679 dx
= scale_width
* x
;
681 // Check if x and y are in the valid input range.
682 if ( abs( dx
) < in_x_range
&& abs( dy
) < in_y_range
)
684 // We're in the input range for this row.
685 in_ptr
= in_line
+ ( dx
>> 1 ) * 4 + 2 * ( x
& 1 );
686 *out_ptr
++ = *in_ptr
++;
687 *out_ptr
++ = *in_ptr
++;
691 // We're not in the input range for this row.
697 // Move to next output line
701 // Now update the frame
702 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
703 mlt_properties_set_int( properties
, "width", owidth
);
704 mlt_properties_set_int( properties
, "height", oheight
);
710 // No change, return input
714 int mlt_frame_mix_audio( mlt_frame
this, mlt_frame that
, float weight
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
717 int16_t *p_src
, *p_dest
;
719 //static int16_t *extra_src = NULL, *extra_dest = NULL;
720 static int extra_src_samples
= 0, extra_dest_samples
= 0;
721 int frequency_src
= 0, frequency_dest
= 0;
722 int channels_src
= 0, channels_dest
= 0;
723 int samples_src
= 0, samples_dest
= 0;
726 mlt_frame_get_audio( this, &p_dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
727 //fprintf( stderr, "frame dest samples %d channels %d position %f\n", samples_dest, channels_dest, mlt_properties_get_position( mlt_frame_properties( this ), "position" ) );
728 mlt_frame_get_audio( that
, &p_src
, format
, &frequency_src
, &channels_src
, &samples_src
);
729 //fprintf( stderr, "frame src samples %d channels %d\n", samples_src, channels_src );
730 if ( channels_src
> 6 )
732 if ( channels_dest
> 6 )
734 if ( samples_src
> 4000 )
736 if ( samples_dest
> 4000 )
740 // Append new samples to leftovers
741 if ( extra_dest_samples
> 0 )
743 fprintf( stderr
, "prepending %d samples to dest\n", extra_dest_samples
);
744 dest
= realloc( extra_dest
, ( samples_dest
+ extra_dest_samples
) * 2 * channels_dest
);
745 memcpy( &extra_dest
[ extra_dest_samples
* channels_dest
], p_dest
, samples_dest
* 2 * channels_dest
);
749 if ( extra_src_samples
> 0 )
751 fprintf( stderr
, "prepending %d samples to src\n", extra_src_samples
);
752 src
= realloc( extra_src
, ( samples_src
+ extra_src_samples
) * 2 * channels_src
);
753 memcpy( &extra_src
[ extra_src_samples
* channels_src
], p_src
, samples_src
* 2 * channels_src
);
762 // determine number of samples to process
763 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
764 *samples
= samples_src
+ extra_src_samples
;
765 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
766 *samples
= samples_dest
+ extra_dest_samples
;
768 *channels
= channels_src
< channels_dest ? channels_src
: channels_dest
;
772 for ( i
= 0; i
< *samples
; i
++ )
774 for ( j
= 0; j
< *channels
; j
++ )
776 double d
= (double) dest
[ i
* channels_dest
+ j
];
777 double s
= (double) src
[ i
* channels_src
+ j
];
778 dest
[ i
* channels_dest
+ j
] = s
* weight
+ d
* ( 1.0 - weight
);
782 // We have to copy --sigh
783 if ( dest
!= p_dest
)
784 memcpy( p_dest
, dest
, *samples
* 2 * *channels
);
787 // Store the leftovers
788 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
790 extra_dest_samples
= ( samples_dest
+ extra_dest_samples
) - ( samples_src
+ extra_src_samples
);
791 size_t size
= extra_dest_samples
* 2 * channels_dest
;
792 fprintf( stderr
, "storing %d samples from dest\n", extra_dest_samples
);
795 extra_dest
= malloc( size
);
797 memcpy( extra_dest
, &p_dest
[ ( samples_dest
- extra_dest_samples
- 1 ) * channels_dest
], size
);
799 extra_dest_samples
= 0;
801 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
803 extra_src_samples
= ( samples_src
+ extra_src_samples
) - ( samples_dest
+ extra_dest_samples
);
804 size_t size
= extra_src_samples
* 2 * channels_src
;
805 fprintf( stderr
, "storing %d samples from src\n", extra_dest_samples
);
808 extra_src
= malloc( size
);
810 memcpy( extra_src
, &p_src
[ ( samples_src
- extra_src_samples
- 1 ) * channels_src
], size
);
812 extra_src_samples
= 0;
819 int mlt_sample_calculator( float fps
, int frequency
, int64_t position
)
823 if ( fps
> 29 && fps
<= 30 )
825 samples
= frequency
/ 30;
830 if ( position
% 5 != 0 )
834 if ( position
% 300 == 0 )
836 else if ( position
% 30 == 0 )
838 else if ( position
% 2 == 0 )
844 if ( position
% 30 == 0 )
846 else if ( position
% 29 == 0 )
848 else if ( position
% 4 == 2 )
859 samples
= frequency
/ fps
;