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"
27 /** Constructor for a frame.
30 mlt_frame
mlt_frame_init( )
33 mlt_frame
this = calloc( sizeof( struct mlt_frame_s
), 1 );
37 // Initialise the properties
38 mlt_properties properties
= &this->parent
;
39 mlt_properties_init( properties
, this );
41 // Set default properties on the frame
42 mlt_properties_set_position( properties
, "_position", 0.0 );
43 mlt_properties_set_data( properties
, "image", NULL
, 0, NULL
, NULL
);
44 mlt_properties_set_int( properties
, "width", 720 );
45 mlt_properties_set_int( properties
, "height", 576 );
46 mlt_properties_set_double( properties
, "aspect_ratio", 4.0 / 3.0 );
47 mlt_properties_set_data( properties
, "audio", NULL
, 0, NULL
, NULL
);
48 mlt_properties_set_data( properties
, "alpha", NULL
, 0, NULL
, NULL
);
53 /** Fetch the frames properties.
56 mlt_properties
mlt_frame_properties( mlt_frame
this )
61 /** Check if we have a way to derive something other than a test card.
64 int mlt_frame_is_test_card( mlt_frame
this )
66 return mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
69 /** Check if we have a way to derive something than test audio.
72 int mlt_frame_is_test_audio( mlt_frame
this )
74 return this->get_audio
== NULL
|| mlt_properties_get_int( mlt_frame_properties( this ), "test_audio" );
77 /** Get the aspect ratio of the frame.
80 double mlt_frame_get_aspect_ratio( mlt_frame
this )
82 return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
85 /** Set the aspect ratio of the frame.
88 int mlt_frame_set_aspect_ratio( mlt_frame
this, double value
)
90 return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value
);
93 /** Get the position of this frame.
96 mlt_position
mlt_frame_get_position( mlt_frame
this )
98 return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
101 /** Set the position of this frame.
104 int mlt_frame_set_position( mlt_frame
this, mlt_position value
)
106 return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value
);
109 /** Stack a get_image callback.
112 int mlt_frame_push_get_image( mlt_frame
this, mlt_get_image get_image
)
114 int ret
= this->stack_get_image_size
>= 10;
116 this->stack_get_image
[ this->stack_get_image_size
++ ] = get_image
;
120 /** Pop a get_image callback.
123 mlt_get_image
mlt_frame_pop_get_image( mlt_frame
this )
125 mlt_get_image result
= NULL
;
126 if ( this->stack_get_image_size
> 0 )
127 result
= this->stack_get_image
[ -- this->stack_get_image_size
];
134 int mlt_frame_push_frame( mlt_frame
this, mlt_frame that
)
136 int ret
= this->stack_frame_size
>= 10;
138 this->stack_frame
[ this->stack_frame_size
++ ] = that
;
145 mlt_frame
mlt_frame_pop_frame( mlt_frame
this )
147 mlt_frame result
= NULL
;
148 if ( this->stack_frame_size
> 0 )
149 result
= this->stack_frame
[ -- this->stack_frame_size
];
153 int mlt_frame_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
155 mlt_properties properties
= mlt_frame_properties( this );
156 mlt_get_image get_image
= mlt_frame_pop_get_image( this );
158 if ( get_image
!= NULL
)
160 return get_image( this, buffer
, format
, width
, height
, writable
);
162 else if ( mlt_properties_get_data( properties
, "image", NULL
) != NULL
)
164 *format
= mlt_image_yuv422
;
165 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
166 *width
= mlt_properties_get_int( properties
, "width" );
167 *height
= mlt_properties_get_int( properties
, "height" );
175 *width
= *width
== 0 ?
720 : *width
;
176 *height
= *height
== 0 ?
576 : *height
;
177 size
= *width
* *height
;
179 mlt_properties_set_int( properties
, "width", *width
);
180 mlt_properties_set_int( properties
, "height", *height
);
188 case mlt_image_rgb24
:
190 *buffer
= malloc( size
);
192 memset( *buffer
, 255, size
);
194 case mlt_image_rgb24a
:
196 *buffer
= malloc( size
);
198 memset( *buffer
, 255, size
);
200 case mlt_image_yuv422
:
202 *buffer
= malloc( size
);
205 while ( p
!= NULL
&& p
!= q
)
211 case mlt_image_yuv420p
:
213 *buffer
= malloc( size
);
215 memset( *buffer
, 255, size
);
219 mlt_properties_set_data( properties
, "image", *buffer
, size
, free
, NULL
);
220 mlt_properties_set_int( properties
, "test_image", 1 );
226 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
228 if ( this->get_alpha_mask
!= NULL
)
229 return this->get_alpha_mask( this );
233 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
235 mlt_properties properties
= mlt_frame_properties( this );
237 if ( this->get_audio
!= NULL
)
239 return this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
244 *samples
= *samples
<= 0 ?
1920 : *samples
;
245 *channels
= *channels
<= 0 ?
2 : *channels
;
246 *frequency
= *frequency
<= 0 ?
48000 : *frequency
;
247 size
= *samples
* *channels
* sizeof( int16_t );
248 *buffer
= malloc( size
);
249 if ( *buffer
!= NULL
)
250 memset( *buffer
, 0, size
);
251 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
252 mlt_properties_set_int( properties
, "test_audio", 1 );
257 void mlt_frame_close( mlt_frame
this )
259 mlt_properties_close( &this->parent
);
263 /***** convenience functions *****/
264 #define RGB2YUV(r, g, b, y, u, v)\
265 y = (306*r + 601*g + 117*b) >> 10;\
266 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
267 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
271 y = y > 255 ? 255 : y;\
272 u = u > 255 ? 255 : u;\
273 v = v > 255 ? 255 : v
275 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
278 register int y0
, y1
, u0
, u1
, v0
, v1
;
279 register int r
, g
, b
;
280 register uint8_t *d
= yuv
;
283 for ( i
= 0; i
< height
; i
++ )
285 register uint8_t *s
= rgba
+ ( stride
* i
);
286 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
292 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
297 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
309 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
317 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
320 register int y0
, y1
, u0
, u1
, v0
, v1
;
321 register int r
, g
, b
;
322 register uint8_t *d
= yuv
;
325 for ( i
= 0; i
< height
; i
++ )
327 register uint8_t *s
= rgb
+ ( stride
* i
);
328 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
333 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
337 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
348 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
356 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p
, int width
, int height
, int stride
, uint8_t *yuv
)
361 int half
= width
>> 1;
363 uint8_t *Y
= yuv420p
;
364 uint8_t *U
= Y
+ width
* height
;
365 uint8_t *V
= U
+ width
* height
/ 4;
367 register uint8_t *d
= yuv
;
369 for ( i
= 0; i
< height
; i
++ )
371 register uint8_t *u
= U
+ ( i
/ 2 ) * ( half
);
372 register uint8_t *v
= V
+ ( i
/ 2 ) * ( half
);
374 for ( j
= 0; j
< half
; j
++ )
385 void *memfill( void *dst
, void *src
, int l
, int elements
)
392 uint8_t *src2
= src
+ 1;
393 for ( i
= 0; i
< elements
; i
++ )
402 for ( i
= 0; i
< elements
; i
++ )
403 dst
= memcpy( dst
, src
, l
) + l
;
408 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
411 int istride
= iwidth
* 2;
412 int ostride
= owidth
* 2;
414 iwidth
= iwidth
- ( iwidth
% 4 );
415 owidth
= owidth
- ( owidth
% 4 );
416 iheight
= iheight
- ( iheight
% 2 );
417 oheight
= oheight
- ( oheight
% 2 );
419 // Coordinates (0,0 is middle of output)
423 int out_x_range
= owidth
/ 2;
424 int out_y_range
= oheight
/ 2;
425 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
426 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
429 uint8_t *out_line
= output
;
430 uint8_t *out_ptr
= out_line
;
432 // Calculate a middle and possibly invalid pointer in the input
433 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
434 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
436 uint8_t black
[ 2 ] = { 16, 128 };
438 // Loop for the entirety of our output height.
439 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
441 // Start at the beginning of the line
444 if ( abs( y
) < iheight
/ 2 )
446 // Fill the outer part with black
447 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
449 // We're in the input range for this row.
450 memcpy( out_ptr
, in_middle
+ in_line
, 2 * iwidth
);
451 out_ptr
+= 2 * iwidth
;
453 // Fill the outer part with black
454 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
456 // Move to next input line
461 // Fill whole line with black
462 out_ptr
= memfill( out_ptr
, black
, 2, owidth
);
465 // Move to next output line
470 /** A resizing function for yuv422 frames - this does not rescale, but simply
471 resizes. It assumes yuv422 images available on the frame so use with care.
474 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
477 mlt_properties properties
= mlt_frame_properties( this );
479 // Get the input image, width and height
480 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
481 int iwidth
= mlt_properties_get_int( properties
, "width" );
482 int iheight
= mlt_properties_get_int( properties
, "height" );
484 // If width and height are correct, don't do anything
485 if ( iwidth
!= owidth
|| iheight
!= oheight
)
487 // Create the output image
488 uint8_t *output
= malloc( owidth
* oheight
* 2 );
490 // Call the generic resize
491 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
493 // Now update the frame
494 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
495 mlt_properties_set_int( properties
, "width", owidth
);
496 mlt_properties_set_int( properties
, "height", oheight
);
497 mlt_frame_set_aspect_ratio( this, 4.0/3.0/*( float )owidth / oheight*/ );
503 // No change, return input
507 /** A rescaling function for yuv422 frames - low quality, and provided for testing
508 only. It assumes yuv422 images available on the frame so use with care.
511 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
514 mlt_properties properties
= mlt_frame_properties( this );
516 // Get the input image, width and height
517 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
518 int iwidth
= mlt_properties_get_int( properties
, "width" );
519 int iheight
= mlt_properties_get_int( properties
, "height" );
521 // If width and height are correct, don't do anything
522 if ( iwidth
!= owidth
|| iheight
!= oheight
)
524 // Create the output image
525 uint8_t *output
= malloc( owidth
* oheight
* 2 );
528 int istride
= iwidth
* 2;
529 int ostride
= owidth
* 2;
531 iwidth
= iwidth
- ( iwidth
% 4 );
533 // Coordinates (0,0 is middle of output)
536 // Derived coordinates
540 int out_x_range
= owidth
/ 2;
541 int out_y_range
= oheight
/ 2;
542 int in_x_range
= iwidth
/ 2;
543 int in_y_range
= iheight
/ 2;
546 uint8_t *out_line
= output
;
549 // Calculate a middle pointer
550 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
554 // Generate the affine transform scaling values
555 int scale_width
= ( iwidth
<< 16 ) / owidth
;
556 int scale_height
= ( iheight
<< 16 ) / oheight
;
558 // Loop for the entirety of our output height.
559 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
561 // Calculate the derived y value
562 dy
= ( scale_height
* y
) >> 16;
564 // Start at the beginning of the line
567 // Pointer to the middle of the input line
568 in_line
= in_middle
+ dy
* istride
;
570 // Loop for the entirety of our output row.
571 for ( x
= - out_x_range
; x
< out_x_range
; x
+= 1 )
573 // Calculated the derived x
574 dx
= ( scale_width
* x
) >> 16;
576 // We're in the input range for this row.
577 in_ptr
= in_line
+ ( dx
<< 1 );
578 *out_ptr
++ = *in_ptr
++;
579 in_ptr
= in_line
+ ( ( dx
>> 1 ) << 2 ) + ( ( x
& 1 ) << 1 ) + 1;
580 *out_ptr
++ = *in_ptr
;
583 // Move to next output line
587 // Now update the frame
588 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
589 mlt_properties_set_int( properties
, "width", owidth
);
590 mlt_properties_set_int( properties
, "height", oheight
);
596 // No change, return input
600 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
)
603 int16_t *p_src
, *p_dest
;
605 //static int16_t *extra_src = NULL, *extra_dest = NULL;
606 static int extra_src_samples
= 0, extra_dest_samples
= 0;
607 int frequency_src
= *frequency
, frequency_dest
= *frequency
;
608 int channels_src
= *channels
, channels_dest
= *channels
;
609 int samples_src
= *samples
, samples_dest
= *samples
;
613 mlt_frame_get_audio( this, &p_dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
614 //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" ) );
615 mlt_frame_get_audio( that
, &p_src
, format
, &frequency_src
, &channels_src
, &samples_src
);
616 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
619 if ( channels_src
> 6 )
621 if ( channels_dest
> 6 )
623 if ( samples_src
> 4000 )
625 if ( samples_dest
> 4000 )
629 // Append new samples to leftovers
630 if ( extra_dest_samples
> 0 )
632 fprintf( stderr
, "prepending %d samples to dest\n", extra_dest_samples
);
633 dest
= realloc( extra_dest
, ( samples_dest
+ extra_dest_samples
) * 2 * channels_dest
);
634 memcpy( &extra_dest
[ extra_dest_samples
* channels_dest
], p_dest
, samples_dest
* 2 * channels_dest
);
638 if ( extra_src_samples
> 0 )
640 fprintf( stderr
, "prepending %d samples to src\n", extra_src_samples
);
641 src
= realloc( extra_src
, ( samples_src
+ extra_src_samples
) * 2 * channels_src
);
642 memcpy( &extra_src
[ extra_src_samples
* channels_src
], p_src
, samples_src
* 2 * channels_src
);
648 // determine number of samples to process
649 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
650 *samples
= samples_src
+ extra_src_samples
;
651 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
652 *samples
= samples_dest
+ extra_dest_samples
;
654 *channels
= channels_src
< channels_dest ? channels_src
: channels_dest
;
656 *frequency
= frequency_dest
;
659 for ( i
= 0; i
< *samples
; i
++ )
661 for ( j
= 0; j
< *channels
; j
++ )
663 if ( j
< channels_dest
)
664 d
= (double) dest
[ i
* channels_dest
+ j
];
665 if ( j
< channels_src
)
666 s
= (double) src
[ i
* channels_src
+ j
];
667 dest
[ i
* channels_dest
+ j
] = s
* weight
+ d
* ( 1.0 - weight
);
671 // We have to copy --sigh
672 if ( dest
!= p_dest
)
673 memcpy( p_dest
, dest
, *samples
* 2 * *channels
);
676 // Store the leftovers
677 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
679 extra_dest_samples
= ( samples_dest
+ extra_dest_samples
) - ( samples_src
+ extra_src_samples
);
680 size_t size
= extra_dest_samples
* 2 * channels_dest
;
681 fprintf( stderr
, "storing %d samples from dest\n", extra_dest_samples
);
684 extra_dest
= malloc( size
);
686 memcpy( extra_dest
, &p_dest
[ ( samples_dest
- extra_dest_samples
- 1 ) * channels_dest
], size
);
688 extra_dest_samples
= 0;
690 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
692 extra_src_samples
= ( samples_src
+ extra_src_samples
) - ( samples_dest
+ extra_dest_samples
);
693 size_t size
= extra_src_samples
* 2 * channels_src
;
694 fprintf( stderr
, "storing %d samples from src\n", extra_dest_samples
);
697 extra_src
= malloc( size
);
699 memcpy( extra_src
, &p_src
[ ( samples_src
- extra_src_samples
- 1 ) * channels_src
], size
);
701 extra_src_samples
= 0;
708 int mlt_sample_calculator( float fps
, int frequency
, int64_t position
)
712 if ( fps
> 29 && fps
<= 30 )
714 samples
= frequency
/ 30;
719 if ( position
% 5 != 0 )
723 if ( position
% 300 == 0 )
725 else if ( position
% 30 == 0 )
727 else if ( position
% 2 == 0 )
733 if ( position
% 30 == 0 )
735 else if ( position
% 29 == 0 )
737 else if ( position
% 4 == 2 )
748 samples
= frequency
/ fps
;