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 && mlt_properties_get_data( mlt_frame_properties( this ), "image", NULL
) == NULL
);
83 /** Check if we have a way to derive something than test audio.
86 int mlt_frame_is_test_audio( mlt_frame
this )
88 return this->get_audio
== NULL
;
91 /** Get the aspect ratio of the frame.
94 double mlt_frame_get_aspect_ratio( mlt_frame
this )
96 return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
99 /** Set the aspect ratio of the frame.
102 int mlt_frame_set_aspect_ratio( mlt_frame
this, double value
)
104 return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value
);
107 /** Get the position of this frame.
110 mlt_position
mlt_frame_get_position( mlt_frame
this )
112 return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
115 /** Set the position of this frame.
118 int mlt_frame_set_position( mlt_frame
this, mlt_position value
)
120 return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value
);
123 /** Stack a get_image callback.
126 int mlt_frame_push_get_image( mlt_frame
this, mlt_get_image get_image
)
128 int ret
= this->stack_get_image_size
>= 10;
130 this->stack_get_image
[ this->stack_get_image_size
++ ] = get_image
;
134 /** Pop a get_image callback.
137 mlt_get_image
mlt_frame_pop_get_image( mlt_frame
this )
139 mlt_get_image result
= NULL
;
140 if ( this->stack_get_image_size
> 0 )
141 result
= this->stack_get_image
[ -- this->stack_get_image_size
];
148 int mlt_frame_push_frame( mlt_frame
this, mlt_frame that
)
150 int ret
= this->stack_frame_size
>= 10;
152 this->stack_frame
[ this->stack_frame_size
++ ] = that
;
159 mlt_frame
mlt_frame_pop_frame( mlt_frame
this )
161 mlt_frame result
= NULL
;
162 if ( this->stack_frame_size
> 0 )
163 result
= this->stack_frame
[ -- this->stack_frame_size
];
167 int mlt_frame_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
169 mlt_properties properties
= mlt_frame_properties( this );
170 mlt_get_image get_image
= mlt_frame_pop_get_image( this );
172 if ( get_image
!= NULL
)
174 return get_image( this, buffer
, format
, width
, height
, writable
);
176 else if ( mlt_properties_get_data( properties
, "image", NULL
) != NULL
)
178 *format
= mlt_image_yuv422
;
179 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
180 *width
= mlt_properties_get_int( properties
, "width" );
181 *height
= mlt_properties_get_int( properties
, "height" );
185 if ( test_card
.vfmt
!= *format
)
190 test_card
.vfmt
= *format
;
191 test_card
.width
= *width
== 0 ?
720 : *width
;
192 test_card
.height
= *height
== 0 ?
576 : *height
;
198 case mlt_image_rgb24
:
199 test_card
.image
= realloc( test_card
.image
, test_card
.width
* test_card
.height
* 3 );
200 memset( test_card
.image
, 255, test_card
.width
* test_card
.height
* 3 );
202 case mlt_image_rgb24a
:
203 test_card
.image
= realloc( test_card
.image
, test_card
.width
* test_card
.height
* 4 );
204 memset( test_card
.image
, 255, test_card
.width
* test_card
.height
* 4 );
206 case mlt_image_yuv422
:
207 test_card
.image
= realloc( test_card
.image
, test_card
.width
* test_card
.height
* 2 );
209 q
= test_card
.image
+ test_card
.width
* test_card
.height
* 2;
216 case mlt_image_yuv420p
:
217 test_card
.image
= realloc( test_card
.image
, test_card
.width
* test_card
.height
* 3 / 2 );
218 memset( test_card
.image
, 255, test_card
.width
* test_card
.height
* 3 / 2 );
223 *width
= test_card
.width
;
224 *height
= test_card
.height
;
225 *buffer
= test_card
.image
;
231 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
233 if ( this->get_alpha_mask
!= NULL
)
234 return this->get_alpha_mask( this );
235 return test_card
.alpha
;
238 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
240 if ( this->get_audio
!= NULL
)
242 return this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
248 if ( *channels
<= 0 )
250 if ( *frequency
<= 0 )
252 if ( test_card
.audio
== NULL
|| test_card
.afmt
!= *format
)
254 test_card
.afmt
= *format
;
255 test_card
.audio
= realloc( test_card
.audio
, *samples
* *channels
* sizeof( int16_t ) );
256 memset( test_card
.audio
, 0, *samples
* *channels
* sizeof( int16_t ) );
259 *buffer
= test_card
.audio
;
264 void mlt_frame_close( mlt_frame
this )
266 mlt_properties_close( &this->parent
);
270 /***** convenience functions *****/
271 #define RGB2YUV(r, g, b, y, u, v)\
272 y = (306*r + 601*g + 117*b) >> 10;\
273 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
274 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
278 y = y > 255 ? 255 : y;\
279 u = u > 255 ? 255 : u;\
280 v = v > 255 ? 255 : v
282 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
285 register int y0
, y1
, u0
, u1
, v0
, v1
;
286 register int r
, g
, b
;
287 register uint8_t *d
= yuv
;
290 for ( i
= 0; i
< height
; i
++ )
292 register uint8_t *s
= rgba
+ ( stride
* i
);
293 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
299 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
304 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
316 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
324 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
327 register int y0
, y1
, u0
, u1
, v0
, v1
;
328 register int r
, g
, b
;
329 register uint8_t *d
= yuv
;
332 for ( i
= 0; i
< height
; i
++ )
334 register uint8_t *s
= rgb
+ ( stride
* i
);
335 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
340 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
344 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
355 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
363 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p
, int width
, int height
, int stride
, uint8_t *yuv
)
368 int half
= width
>> 1;
370 uint8_t *Y
= yuv420p
;
371 uint8_t *U
= Y
+ width
* height
;
372 uint8_t *V
= U
+ width
* height
/ 4;
374 register uint8_t *d
= yuv
;
376 for ( i
= 0; i
< height
; i
++ )
378 register uint8_t *u
= U
+ ( i
/ 2 ) * ( half
);
379 register uint8_t *v
= V
+ ( i
/ 2 ) * ( half
);
381 for ( j
= 0; j
< half
; j
++ )
392 int mlt_frame_composite_yuv( mlt_frame
this, mlt_frame that
, int x
, int y
, float weight
)
395 int width_src
= 0, height_src
= 0;
396 int width_dest
= 0, height_dest
= 0;
397 mlt_image_format format_src
= mlt_image_yuv422
, format_dest
= mlt_image_yuv422
;
398 uint8_t *p_src
, *p_dest
;
402 int x_src
= 0, y_src
= 0;
404 // optimization point - no work to do
405 if ( ( x
< 0 && -x
>= width_src
) || ( y
< 0 && -y
>= height_src
) )
408 format_src
= mlt_image_yuv422
;
409 format_dest
= mlt_image_yuv422
;
411 //fprintf( stderr, "call get_image on frame a\n"), fflush( stderr );
412 mlt_frame_get_image( this, &p_dest
, &format_dest
, &width_dest
, &height_dest
, 1 /* writable */ );
413 //fprintf( stderr, "call get_image on frame b\n"), fflush( stderr );
414 mlt_frame_get_image( that
, &p_src
, &format_src
, &width_src
, &height_src
, 0 /* writable */ );
416 //fprintf( stderr, "mlt_frame_composite_yuv %dx%d -> %dx%d\n", width_src, height_src, width_dest, height_dest );
419 stride_src
= width_src
* 2;
420 stride_dest
= width_dest
* 2;
422 // crop overlay off the left edge of frame
430 // crop overlay beyond right edge of frame
431 else if ( x
+ width_src
> width_dest
)
432 width_src
= width_dest
- x
;
434 // crop overlay off the top edge of the frame
440 // crop overlay below bottom edge of frame
441 else if ( y
+ height_src
> height_dest
)
442 height_src
= height_dest
- y
;
444 // offset pointer into overlay buffer based on cropping
445 p_src
+= x_src
* 2 + y_src
* stride_src
;
447 // offset pointer into frame buffer based upon positive, even coordinates only!
448 // if ( interlaced && y % 2 )
450 p_dest
+= ( x
< 0 ?
0 : x
) * 2 + ( y
< 0 ?
0 : y
) * stride_dest
;
452 // Get the alpha channel of the overlay
453 uint8_t *p_alpha
= mlt_frame_get_alpha_mask( that
);
455 // offset pointer into alpha channel based upon cropping
457 p_alpha
+= x_src
+ y_src
* stride_src
/ 2;
462 uint8_t *z
= p_alpha
;
469 // now do the compositing only to cropped extents
470 for ( i
= 0; i
< height_src
; i
++ )
477 for ( j
= 0; j
< width_src
; j
++ )
481 a
= ( z
== NULL
) ?
255 : *z
++;
482 value
= ( weight
* ( float ) a
/ 255.0 );
483 *o
++ = (uint8_t)( Y
* value
+ *q
++ * ( 1 - value
) );
484 *o
++ = (uint8_t)( UV
* value
+ *q
++ * ( 1 - value
) );
488 p_dest
+= stride_dest
;
490 p_alpha
+= stride_src
/ 2;
496 void *memfill( void *dst
, void *src
, int l
, int elements
)
503 uint8_t *src2
= src
+ 1;
504 for ( i
= 0; i
< elements
; i
++ )
513 for ( i
= 0; i
< elements
; i
++ )
514 dst
= memcpy( dst
, src
, l
) + l
;
519 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
522 int istride
= iwidth
* 2;
523 int ostride
= owidth
* 2;
525 iwidth
= iwidth
- ( iwidth
% 4 );
526 owidth
= owidth
- ( owidth
% 4 );
527 iheight
= iheight
- ( iheight
% 2 );
528 oheight
= oheight
- ( oheight
% 2 );
530 // Coordinates (0,0 is middle of output)
534 int out_x_range
= owidth
/ 2;
535 int out_y_range
= oheight
/ 2;
536 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
537 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
540 uint8_t *out_line
= output
;
541 uint8_t *out_ptr
= out_line
;
543 // Calculate a middle and possibly invalid pointer in the input
544 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
545 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
547 uint8_t black
[ 2 ] = { 16, 128 };
549 // Loop for the entirety of our output height.
550 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
552 // Start at the beginning of the line
555 if ( abs( y
) < iheight
/ 2 )
557 // Fill the outer part with black
558 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
560 // We're in the input range for this row.
561 memcpy( out_ptr
, in_middle
+ in_line
, 2 * iwidth
);
562 out_ptr
+= 2 * iwidth
;
564 // Fill the outer part with black
565 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
567 // Move to next input line
572 // Fill whole line with black
573 out_ptr
= memfill( out_ptr
, black
, 2, owidth
);
576 // Move to next output line
581 /** A resizing function for yuv422 frames - this does not rescale, but simply
582 resizes. It assumes yuv422 images available on the frame so use with care.
585 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
588 mlt_properties properties
= mlt_frame_properties( this );
590 // Get the input image, width and height
591 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
592 int iwidth
= mlt_properties_get_int( properties
, "width" );
593 int iheight
= mlt_properties_get_int( properties
, "height" );
595 // If width and height are correct, don't do anything
596 if ( iwidth
!= owidth
|| iheight
!= oheight
)
598 // Create the output image
599 uint8_t *output
= malloc( owidth
* oheight
* 2 );
601 // Call the generic resize
602 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
604 // Now update the frame
605 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
606 mlt_properties_set_int( properties
, "width", owidth
);
607 mlt_properties_set_int( properties
, "height", oheight
);
608 mlt_frame_set_aspect_ratio( this, 4.0/3.0/*( float )owidth / oheight*/ );
614 // No change, return input
618 /** A rescaling function for yuv422 frames - low quality, and provided for testing
619 only. It assumes yuv422 images available on the frame so use with care.
622 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
625 mlt_properties properties
= mlt_frame_properties( this );
627 // Get the input image, width and height
628 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
629 int iwidth
= mlt_properties_get_int( properties
, "width" );
630 int iheight
= mlt_properties_get_int( properties
, "height" );
632 // If width and height are correct, don't do anything
633 if ( iwidth
!= owidth
|| iheight
!= oheight
)
635 // Create the output image
636 uint8_t *output
= malloc( owidth
* oheight
* 2 );
639 int istride
= iwidth
* 2;
640 int ostride
= owidth
* 2;
642 iwidth
= iwidth
- ( iwidth
% 4 );
644 // Coordinates (0,0 is middle of output)
647 // Derived coordinates
651 int out_x_range
= owidth
/ 2;
652 int out_y_range
= oheight
/ 2;
653 int in_x_range
= iwidth
/ 2;
654 int in_y_range
= iheight
/ 2;
657 uint8_t *out_line
= output
;
660 // Calculate a middle pointer
661 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
665 // Generate the affine transform scaling values
666 int scale_width
= ( iwidth
<< 16 ) / owidth
;
667 int scale_height
= ( iheight
<< 16 ) / oheight
;
669 // Loop for the entirety of our output height.
670 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
672 // Calculate the derived y value
673 dy
= ( scale_height
* y
) >> 16;
675 // Start at the beginning of the line
678 // Pointer to the middle of the input line
679 in_line
= in_middle
+ dy
* istride
;
681 // Loop for the entirety of our output row.
682 for ( x
= - out_x_range
; x
< out_x_range
; x
+= 1 )
684 // Calculated the derived x
685 dx
= ( scale_width
* x
) >> 16;
687 // We're in the input range for this row.
688 in_ptr
= in_line
+ ( dx
<< 1 );
689 *out_ptr
++ = *in_ptr
++;
690 in_ptr
= in_line
+ ( ( dx
>> 1 ) << 2 ) + ( ( x
& 1 ) << 1 ) + 1;
691 *out_ptr
++ = *in_ptr
;
694 // Move to next output line
698 // Now update the frame
699 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
700 mlt_properties_set_int( properties
, "width", owidth
);
701 mlt_properties_set_int( properties
, "height", oheight
);
707 // No change, return input
711 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
)
714 int16_t *p_src
, *p_dest
;
716 //static int16_t *extra_src = NULL, *extra_dest = NULL;
717 static int extra_src_samples
= 0, extra_dest_samples
= 0;
718 int frequency_src
= *frequency
, frequency_dest
= *frequency
;
719 int channels_src
= *channels
, channels_dest
= *channels
;
720 int samples_src
= *samples
, samples_dest
= *samples
;
724 mlt_frame_get_audio( this, &p_dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
725 //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" ) );
726 mlt_frame_get_audio( that
, &p_src
, format
, &frequency_src
, &channels_src
, &samples_src
);
727 //fprintf( stderr, "mix: 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
);
759 // determine number of samples to process
760 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
761 *samples
= samples_src
+ extra_src_samples
;
762 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
763 *samples
= samples_dest
+ extra_dest_samples
;
765 *channels
= channels_src
< channels_dest ? channels_src
: channels_dest
;
767 *frequency
= frequency_dest
;
770 for ( i
= 0; i
< *samples
; i
++ )
772 for ( j
= 0; j
< *channels
; j
++ )
774 if ( j
< channels_dest
)
775 d
= (double) dest
[ i
* channels_dest
+ j
];
776 if ( j
< channels_src
)
777 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
;