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
|| test_card
.width
!= *width
|| test_card
.height
!= *height
|| test_card
.image
== NULL
)
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
;
227 mlt_properties_set_int( properties
, "width", *width
);
228 mlt_properties_set_int( properties
, "height", *height
);
232 uint8_t *copy
= malloc( *width
* *height
* 2 );
233 memcpy( copy
, *buffer
, *width
* *height
* 2 );
234 mlt_properties_set_data( properties
, "image", copy
, *width
* *height
* 2, free
, NULL
);
239 mlt_properties_set_data( properties
, "image", *buffer
, *width
* *height
* 2, NULL
, NULL
);
246 uint8_t *mlt_frame_get_alpha_mask( mlt_frame
this )
248 if ( this->get_alpha_mask
!= NULL
)
249 return this->get_alpha_mask( this );
250 return test_card
.alpha
;
253 int mlt_frame_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
255 if ( this->get_audio
!= NULL
)
257 return this->get_audio( this, buffer
, format
, frequency
, channels
, samples
);
263 if ( *channels
<= 0 )
265 if ( *frequency
<= 0 )
267 if ( test_card
.audio
== NULL
|| test_card
.afmt
!= *format
)
269 test_card
.afmt
= *format
;
270 test_card
.audio
= realloc( test_card
.audio
, *samples
* *channels
* sizeof( int16_t ) );
271 memset( test_card
.audio
, 0, *samples
* *channels
* sizeof( int16_t ) );
274 *buffer
= test_card
.audio
;
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 // Coordinates (0,0 is middle of output)
445 int out_x_range
= owidth
/ 2;
446 int out_y_range
= oheight
/ 2;
447 int in_x_range
= iwidth
/ 2 < out_x_range ? iwidth
/ 2 : out_x_range
;
448 int in_y_range
= iheight
/ 2 < out_y_range ? iheight
/ 2 : out_y_range
;
451 uint8_t *out_line
= output
;
452 uint8_t *out_ptr
= out_line
;
454 // Calculate a middle and possibly invalid pointer in the input
455 uint8_t *in_middle
= input
+ istride
* ( iheight
/ 2 ) + ( iwidth
/ 2 ) * 2;
456 int in_line
= - in_y_range
* istride
- in_x_range
* 2;
458 uint8_t black
[ 2 ] = { 16, 128 };
460 // Loop for the entirety of our output height.
461 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
463 // Start at the beginning of the line
466 if ( abs( y
) < iheight
/ 2 )
468 // Fill the outer part with black
469 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
471 // We're in the input range for this row.
472 memcpy( out_ptr
, in_middle
+ in_line
, 2 * iwidth
);
473 out_ptr
+= 2 * iwidth
;
475 // Fill the outer part with black
476 out_ptr
= memfill( out_ptr
, black
, 2, out_x_range
- in_x_range
);
478 // Move to next input line
483 // Fill whole line with black
484 out_ptr
= memfill( out_ptr
, black
, 2, owidth
);
487 // Move to next output line
492 /** A resizing function for yuv422 frames - this does not rescale, but simply
493 resizes. It assumes yuv422 images available on the frame so use with care.
496 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
499 mlt_properties properties
= mlt_frame_properties( this );
501 // Get the input image, width and height
502 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
503 int iwidth
= mlt_properties_get_int( properties
, "width" );
504 int iheight
= mlt_properties_get_int( properties
, "height" );
506 // If width and height are correct, don't do anything
507 if ( iwidth
!= owidth
|| iheight
!= oheight
)
509 // Create the output image
510 uint8_t *output
= malloc( owidth
* oheight
* 2 );
512 // Call the generic resize
513 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
515 // Now update the frame
516 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
517 mlt_properties_set_int( properties
, "width", owidth
);
518 mlt_properties_set_int( properties
, "height", oheight
);
519 mlt_frame_set_aspect_ratio( this, 4.0/3.0/*( float )owidth / oheight*/ );
525 // No change, return input
529 /** A rescaling function for yuv422 frames - low quality, and provided for testing
530 only. It assumes yuv422 images available on the frame so use with care.
533 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
536 mlt_properties properties
= mlt_frame_properties( this );
538 // Get the input image, width and height
539 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
540 int iwidth
= mlt_properties_get_int( properties
, "width" );
541 int iheight
= mlt_properties_get_int( properties
, "height" );
543 // If width and height are correct, don't do anything
544 if ( iwidth
!= owidth
|| iheight
!= oheight
)
546 // Create the output image
547 uint8_t *output
= malloc( owidth
* oheight
* 2 );
550 int istride
= iwidth
* 2;
551 int ostride
= owidth
* 2;
553 iwidth
= iwidth
- ( iwidth
% 4 );
555 // Coordinates (0,0 is middle of output)
558 // Derived coordinates
562 int out_x_range
= owidth
/ 2;
563 int out_y_range
= oheight
/ 2;
564 int in_x_range
= iwidth
/ 2;
565 int in_y_range
= iheight
/ 2;
568 uint8_t *out_line
= output
;
571 // Calculate a middle pointer
572 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
576 // Generate the affine transform scaling values
577 int scale_width
= ( iwidth
<< 16 ) / owidth
;
578 int scale_height
= ( iheight
<< 16 ) / oheight
;
580 // Loop for the entirety of our output height.
581 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
583 // Calculate the derived y value
584 dy
= ( scale_height
* y
) >> 16;
586 // Start at the beginning of the line
589 // Pointer to the middle of the input line
590 in_line
= in_middle
+ dy
* istride
;
592 // Loop for the entirety of our output row.
593 for ( x
= - out_x_range
; x
< out_x_range
; x
+= 1 )
595 // Calculated the derived x
596 dx
= ( scale_width
* x
) >> 16;
598 // We're in the input range for this row.
599 in_ptr
= in_line
+ ( dx
<< 1 );
600 *out_ptr
++ = *in_ptr
++;
601 in_ptr
= in_line
+ ( ( dx
>> 1 ) << 2 ) + ( ( x
& 1 ) << 1 ) + 1;
602 *out_ptr
++ = *in_ptr
;
605 // Move to next output line
609 // Now update the frame
610 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
611 mlt_properties_set_int( properties
, "width", owidth
);
612 mlt_properties_set_int( properties
, "height", oheight
);
618 // No change, return input
622 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
)
625 int16_t *p_src
, *p_dest
;
627 //static int16_t *extra_src = NULL, *extra_dest = NULL;
628 static int extra_src_samples
= 0, extra_dest_samples
= 0;
629 int frequency_src
= *frequency
, frequency_dest
= *frequency
;
630 int channels_src
= *channels
, channels_dest
= *channels
;
631 int samples_src
= *samples
, samples_dest
= *samples
;
635 mlt_frame_get_audio( this, &p_dest
, format
, &frequency_dest
, &channels_dest
, &samples_dest
);
636 //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" ) );
637 mlt_frame_get_audio( that
, &p_src
, format
, &frequency_src
, &channels_src
, &samples_src
);
638 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
641 if ( channels_src
> 6 )
643 if ( channels_dest
> 6 )
645 if ( samples_src
> 4000 )
647 if ( samples_dest
> 4000 )
651 // Append new samples to leftovers
652 if ( extra_dest_samples
> 0 )
654 fprintf( stderr
, "prepending %d samples to dest\n", extra_dest_samples
);
655 dest
= realloc( extra_dest
, ( samples_dest
+ extra_dest_samples
) * 2 * channels_dest
);
656 memcpy( &extra_dest
[ extra_dest_samples
* channels_dest
], p_dest
, samples_dest
* 2 * channels_dest
);
660 if ( extra_src_samples
> 0 )
662 fprintf( stderr
, "prepending %d samples to src\n", extra_src_samples
);
663 src
= realloc( extra_src
, ( samples_src
+ extra_src_samples
) * 2 * channels_src
);
664 memcpy( &extra_src
[ extra_src_samples
* channels_src
], p_src
, samples_src
* 2 * channels_src
);
670 // determine number of samples to process
671 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
672 *samples
= samples_src
+ extra_src_samples
;
673 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
674 *samples
= samples_dest
+ extra_dest_samples
;
676 *channels
= channels_src
< channels_dest ? channels_src
: channels_dest
;
678 *frequency
= frequency_dest
;
681 for ( i
= 0; i
< *samples
; i
++ )
683 for ( j
= 0; j
< *channels
; j
++ )
685 if ( j
< channels_dest
)
686 d
= (double) dest
[ i
* channels_dest
+ j
];
687 if ( j
< channels_src
)
688 s
= (double) src
[ i
* channels_src
+ j
];
689 dest
[ i
* channels_dest
+ j
] = s
* weight
+ d
* ( 1.0 - weight
);
693 // We have to copy --sigh
694 if ( dest
!= p_dest
)
695 memcpy( p_dest
, dest
, *samples
* 2 * *channels
);
698 // Store the leftovers
699 if ( samples_src
+ extra_src_samples
< samples_dest
+ extra_dest_samples
)
701 extra_dest_samples
= ( samples_dest
+ extra_dest_samples
) - ( samples_src
+ extra_src_samples
);
702 size_t size
= extra_dest_samples
* 2 * channels_dest
;
703 fprintf( stderr
, "storing %d samples from dest\n", extra_dest_samples
);
706 extra_dest
= malloc( size
);
708 memcpy( extra_dest
, &p_dest
[ ( samples_dest
- extra_dest_samples
- 1 ) * channels_dest
], size
);
710 extra_dest_samples
= 0;
712 else if ( samples_dest
+ extra_dest_samples
< samples_src
+ extra_src_samples
)
714 extra_src_samples
= ( samples_src
+ extra_src_samples
) - ( samples_dest
+ extra_dest_samples
);
715 size_t size
= extra_src_samples
* 2 * channels_src
;
716 fprintf( stderr
, "storing %d samples from src\n", extra_dest_samples
);
719 extra_src
= malloc( size
);
721 memcpy( extra_src
, &p_src
[ ( samples_src
- extra_src_samples
- 1 ) * channels_src
], size
);
723 extra_src_samples
= 0;
730 int mlt_sample_calculator( float fps
, int frequency
, int64_t position
)
734 if ( fps
> 29 && fps
<= 30 )
736 samples
= frequency
/ 30;
741 if ( position
% 5 != 0 )
745 if ( position
% 300 == 0 )
747 else if ( position
% 30 == 0 )
749 else if ( position
% 2 == 0 )
755 if ( position
% 30 == 0 )
757 else if ( position
% 29 == 0 )
759 else if ( position
% 4 == 2 )
770 samples
= frequency
/ fps
;