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
:
212 *buffer
= malloc( size
);
214 memset( *buffer
, 255, size
);
216 case mlt_image_rgb24a
:
218 *buffer
= malloc( size
);
220 memset( *buffer
, 255, size
);
222 case mlt_image_yuv422
:
224 *buffer
= malloc( size
);
227 while ( p
!= NULL
&& p
!= q
)
233 case mlt_image_yuv420p
:
235 *buffer
= malloc( size
);
237 memset( *buffer
, 255, size
);
241 mlt_properties_set_data( properties
, "image", *buffer
, size
, free
, NULL
);
242 mlt_properties_set_int( properties
, "test_image", 1 );
248 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
250 if ( this->get_alpha_mask
!= NULL
)
251 return this->get_alpha_mask( this );
255 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
257 mlt_properties properties
= mlt_frame_properties( this );
259 if ( this->get_audio
!= NULL
)
261 return this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
266 *samples
= *samples
<= 0 ?
1920 : *samples
;
267 *channels
= *channels
<= 0 ?
2 : *channels
;
268 *frequency
= *frequency
<= 0 ?
48000 : *frequency
;
269 size
= *samples
* *channels
* sizeof( int16_t );
270 *buffer
= malloc( size
);
271 if ( *buffer
!= NULL
)
272 memset( *buffer
, 0, size
);
273 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
274 mlt_properties_set_int( properties
, "test_audio", 1 );
279 void mlt_frame_close( mlt_frame
this )
281 mlt_properties_close( &this->parent
);
285 /***** convenience functions *****/
286 #define RGB2YUV(r, g, b, y, u, v)\
287 y = (306*r + 601*g + 117*b) >> 10;\
288 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
289 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
293 y = y > 255 ? 255 : y;\
294 u = u > 255 ? 255 : u;\
295 v = v > 255 ? 255 : v
297 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
300 register int y0
, y1
, u0
, u1
, v0
, v1
;
301 register int r
, g
, b
;
302 register uint8_t *d
= yuv
;
305 for ( i
= 0; i
< height
; i
++ )
307 register uint8_t *s
= rgba
+ ( stride
* i
);
308 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
314 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
319 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
331 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
339 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
342 register int y0
, y1
, u0
, u1
, v0
, v1
;
343 register int r
, g
, b
;
344 register uint8_t *d
= yuv
;
347 for ( i
= 0; i
< height
; i
++ )
349 register uint8_t *s
= rgb
+ ( stride
* i
);
350 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
355 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
359 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
370 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
378 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p
, int width
, int height
, int stride
, uint8_t *yuv
)
383 int half
= width
>> 1;
385 uint8_t *Y
= yuv420p
;
386 uint8_t *U
= Y
+ width
* height
;
387 uint8_t *V
= U
+ width
* height
/ 4;
389 register uint8_t *d
= yuv
;
391 for ( i
= 0; i
< height
; i
++ )
393 register uint8_t *u
= U
+ ( i
/ 2 ) * ( half
);
394 register uint8_t *v
= V
+ ( i
/ 2 ) * ( half
);
396 for ( j
= 0; j
< half
; j
++ )
407 void *memfill( void *dst
, void *src
, int l
, int elements
)
414 uint8_t *src2
= src
+ 1;
415 for ( i
= 0; i
< elements
; i
++ )
424 for ( i
= 0; i
< elements
; i
++ )
425 dst
= memcpy( dst
, src
, l
) + l
;
430 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
433 int istride
= iwidth
* 2;
434 int ostride
= owidth
* 2;
436 iwidth
= iwidth
- ( iwidth
% 4 );
437 owidth
= owidth
- ( owidth
% 4 );
438 iheight
= iheight
- ( iheight
% 2 );
439 oheight
= oheight
- ( oheight
% 2 );
441 // Optimisation point
442 if ( iwidth
== owidth
&& iheight
== oheight
)
443 memcpy( output
, input
, iheight
* istride
);
445 // Coordinates (0,0 is middle of output)
449 int out_x_range
= owidth
/ 2;
450 int out_y_range
= oheight
/ 2;
451 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
452 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
455 uint8_t *out_line
= output
;
456 uint8_t *out_ptr
= out_line
;
458 // Calculate a middle and possibly invalid pointer in the input
459 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
460 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
462 uint8_t black
[ 2 ] = { 16, 128 };
464 // Loop for the entirety of our output height.
465 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
467 // Start at the beginning of the line
470 if ( abs( y
) < iheight
/ 2 )
472 // Fill the outer part with black
473 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
475 // We're in the input range for this row.
476 memcpy( out_ptr
, in_middle
+ in_line
, 2 * iwidth
);
477 out_ptr
+= 2 * iwidth
;
479 // Fill the outer part with black
480 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
482 // Move to next input line
487 // Fill whole line with black
488 out_ptr
= memfill( out_ptr
, black
, 2, owidth
);
491 // Move to next output line
496 /** A resizing function for yuv422 frames - this does not rescale, but simply
497 resizes. It assumes yuv422 images available on the frame so use with care.
500 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
503 mlt_properties properties
= mlt_frame_properties( this );
505 // Get the input image, width and height
506 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
507 int iwidth
= mlt_properties_get_int( properties
, "width" );
508 int iheight
= mlt_properties_get_int( properties
, "height" );
510 // If width and height are correct, don't do anything
511 if ( iwidth
!= owidth
|| iheight
!= oheight
)
513 // Create the output image
514 uint8_t *output
= malloc( owidth
* oheight
* 2 );
516 // Call the generic resize
517 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
519 // Now update the frame
520 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
521 mlt_properties_set_int( properties
, "width", owidth
);
522 mlt_properties_set_int( properties
, "height", oheight
);
523 mlt_frame_set_aspect_ratio( this, 4.0/3.0/*( float )owidth / oheight*/ );
529 // No change, return input
533 /** A rescaling function for yuv422 frames - low quality, and provided for testing
534 only. It assumes yuv422 images available on the frame so use with care.
537 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
540 mlt_properties properties
= mlt_frame_properties( this );
542 // Get the input image, width and height
543 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
544 int iwidth
= mlt_properties_get_int( properties
, "width" );
545 int iheight
= mlt_properties_get_int( properties
, "height" );
547 // If width and height are correct, don't do anything
548 if ( iwidth
!= owidth
|| iheight
!= oheight
)
550 // Create the output image
551 uint8_t *output
= malloc( owidth
* oheight
* 2 );
554 int istride
= iwidth
* 2;
555 int ostride
= owidth
* 2;
557 iwidth
= iwidth
- ( iwidth
% 4 );
559 // Coordinates (0,0 is middle of output)
562 // Derived coordinates
566 int out_x_range
= owidth
/ 2;
567 int out_y_range
= oheight
/ 2;
568 int in_x_range
= iwidth
/ 2;
569 int in_y_range
= iheight
/ 2;
572 uint8_t *out_line
= output
;
575 // Calculate a middle pointer
576 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
580 // Generate the affine transform scaling values
581 int scale_width
= ( iwidth
<< 16 ) / owidth
;
582 int scale_height
= ( iheight
<< 16 ) / oheight
;
584 // Loop for the entirety of our output height.
585 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
587 // Calculate the derived y value
588 dy
= ( scale_height
* y
) >> 16;
590 // Start at the beginning of the line
593 // Pointer to the middle of the input line
594 in_line
= in_middle
+ dy
* istride
;
596 // Loop for the entirety of our output row.
597 for ( x
= - out_x_range
; x
< out_x_range
; x
+= 1 )
599 // Calculated the derived x
600 dx
= ( scale_width
* x
) >> 16;
602 // We're in the input range for this row.
603 in_ptr
= in_line
+ ( dx
<< 1 );
604 *out_ptr
++ = *in_ptr
++;
605 in_ptr
= in_line
+ ( ( dx
>> 1 ) << 2 ) + ( ( x
& 1 ) << 1 ) + 1;
606 *out_ptr
++ = *in_ptr
;
609 // Move to next output line
613 // Now update the frame
614 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
615 mlt_properties_set_int( properties
, "width", owidth
);
616 mlt_properties_set_int( properties
, "height", oheight
);
622 // No change, return input
626 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
)
629 int16_t *p_src
, *p_dest
;
631 //static int16_t *extra_src = NULL, *extra_dest = NULL;
632 static int extra_src_samples
= 0, extra_dest_samples
= 0;
633 int frequency_src
= *frequency
, frequency_dest
= *frequency
;
634 int channels_src
= *channels
, channels_dest
= *channels
;
635 int samples_src
= *samples
, samples_dest
= *samples
;
639 mlt_frame_get_audio( this, &p_dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
640 //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" ) );
641 mlt_frame_get_audio( that
, &p_src
, format
, &frequency_src
, &channels_src
, &samples_src
);
642 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
645 if ( channels_src
> 6 )
647 if ( channels_dest
> 6 )
649 if ( samples_src
> 4000 )
651 if ( samples_dest
> 4000 )
655 // Append new samples to leftovers
656 if ( extra_dest_samples
> 0 )
658 fprintf( stderr
, "prepending %d samples to dest\n", extra_dest_samples
);
659 dest
= realloc( extra_dest
, ( samples_dest
+ extra_dest_samples
) * 2 * channels_dest
);
660 memcpy( &extra_dest
[ extra_dest_samples
* channels_dest
], p_dest
, samples_dest
* 2 * channels_dest
);
664 if ( extra_src_samples
> 0 )
666 fprintf( stderr
, "prepending %d samples to src\n", extra_src_samples
);
667 src
= realloc( extra_src
, ( samples_src
+ extra_src_samples
) * 2 * channels_src
);
668 memcpy( &extra_src
[ extra_src_samples
* channels_src
], p_src
, samples_src
* 2 * channels_src
);
674 // determine number of samples to process
675 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
676 *samples
= samples_src
+ extra_src_samples
;
677 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
678 *samples
= samples_dest
+ extra_dest_samples
;
680 *channels
= channels_src
< channels_dest ? channels_src
: channels_dest
;
682 *frequency
= frequency_dest
;
684 // Compute a smooth ramp over start to end
685 float weight
= weight_start
;
686 float weight_step
= ( weight_end
- weight_start
) / *samples
;
689 for ( i
= 0; i
< *samples
; i
++ )
691 for ( j
= 0; j
< *channels
; j
++ )
693 if ( j
< channels_dest
)
694 d
= (double) dest
[ i
* channels_dest
+ j
];
695 if ( j
< channels_src
)
696 s
= (double) src
[ i
* channels_src
+ j
];
697 dest
[ i
* channels_dest
+ j
] = s
* weight
+ d
* ( 1.0 - weight
);
699 weight
+= weight_step
;
702 // We have to copy --sigh
703 if ( dest
!= p_dest
)
704 memcpy( p_dest
, dest
, *samples
* 2 * *channels
);
707 // Store the leftovers
708 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
710 extra_dest_samples
= ( samples_dest
+ extra_dest_samples
) - ( samples_src
+ extra_src_samples
);
711 size_t size
= extra_dest_samples
* 2 * channels_dest
;
712 fprintf( stderr
, "storing %d samples from dest\n", extra_dest_samples
);
715 extra_dest
= malloc( size
);
717 memcpy( extra_dest
, &p_dest
[ ( samples_dest
- extra_dest_samples
- 1 ) * channels_dest
], size
);
719 extra_dest_samples
= 0;
721 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
723 extra_src_samples
= ( samples_src
+ extra_src_samples
) - ( samples_dest
+ extra_dest_samples
);
724 size_t size
= extra_src_samples
* 2 * channels_src
;
725 fprintf( stderr
, "storing %d samples from src\n", extra_dest_samples
);
728 extra_src
= malloc( size
);
730 memcpy( extra_src
, &p_src
[ ( samples_src
- extra_src_samples
- 1 ) * channels_src
], size
);
732 extra_src_samples
= 0;
739 int mlt_sample_calculator( float fps
, int frequency
, int64_t position
)
743 if ( fps
> 29 && fps
<= 30 )
745 samples
= frequency
/ 30;
750 if ( position
% 5 != 0 )
754 if ( position
% 300 == 0 )
756 else if ( position
% 30 == 0 )
758 else if ( position
% 2 == 0 )
764 if ( position
% 30 == 0 )
766 else if ( position
% 29 == 0 )
768 else if ( position
% 4 == 2 )
779 samples
= frequency
/ fps
;