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_timecode( properties
, "timecode", 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 timecode of this frame.
104 mlt_timecode
mlt_frame_get_timecode( mlt_frame
this )
106 mlt_properties properties
= mlt_frame_properties( this );
107 return mlt_properties_get_timecode( properties
, "timecode" );
110 /** Set the timecode of this frame.
113 int mlt_frame_set_timecode( mlt_frame
this, mlt_timecode value
)
115 mlt_properties properties
= mlt_frame_properties( this );
116 return mlt_properties_set_timecode( properties
, "timecode", 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
);
242 if ( test_card
.afmt
!= *format
)
244 test_card
.afmt
= *format
;
245 test_card
.audio
= realloc( test_card
.audio
, 1920 * 2 * sizeof( int16_t ) );
246 memset( test_card
.audio
, 0, 1920 * 2 * sizeof( int16_t ) );
249 *buffer
= test_card
.audio
;
257 void mlt_frame_close( mlt_frame
this )
259 mlt_frame frame
= mlt_frame_pop_frame( this );
261 while ( frame
!= NULL
)
263 mlt_frame_close( frame
);
264 frame
= mlt_frame_pop_frame( this );
267 mlt_properties_close( &this->parent
);
272 /***** convenience functions *****/
273 #define RGB2YUV(r, g, b, y, u, v)\
274 y = (306*r + 601*g + 117*b) >> 10;\
275 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
276 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
280 y = y > 255 ? 255 : y;\
281 u = u > 255 ? 255 : u;\
282 v = v > 255 ? 255 : v
284 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba
, int width
, int height
, int stride
, uint8_t *yuv
, uint8_t *alpha
)
287 register int y0
, y1
, u0
, u1
, v0
, v1
;
288 register int r
, g
, b
;
289 register uint8_t *d
= yuv
;
292 for ( i
= 0; i
< height
; i
++ )
294 register uint8_t *s
= rgba
+ ( stride
* i
);
295 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
301 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
306 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
318 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
326 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb
, int width
, int height
, int stride
, uint8_t *yuv
)
329 register int y0
, y1
, u0
, u1
, v0
, v1
;
330 register int r
, g
, b
;
331 register uint8_t *d
= yuv
;
334 for ( i
= 0; i
< height
; i
++ )
336 register uint8_t *s
= rgb
+ ( stride
* i
);
337 for ( j
= 0; j
< ( width
/ 2 ); j
++ )
342 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
346 RGB2YUV (r
, g
, b
, y1
, u1
, v1
);
357 RGB2YUV (r
, g
, b
, y0
, u0
, v0
);
365 int mlt_frame_composite_yuv( mlt_frame
this, mlt_frame that
, int x
, int y
, float weight
)
369 int width_src
, height_src
;
370 int width_dest
, height_dest
;
371 mlt_image_format format_src
, format_dest
;
372 uint8_t *p_src
, *p_dest
;
378 format_src
= mlt_image_yuv422
;
379 format_dest
= mlt_image_yuv422
;
381 mlt_frame_get_image( this, &p_dest
, &format_dest
, &width_dest
, &height_dest
, 1 /* writable */ );
382 mlt_frame_get_image( that
, &p_src
, &format_src
, &width_src
, &height_src
, 0 /* writable */ );
386 stride_src
= width_src
* 2;
387 stride_dest
= width_dest
* 2;
388 uint8_t *lower
= p_dest
;
389 uint8_t *upper
= p_dest
+ height_dest
* stride_dest
;
391 p_dest
+= ( y
* stride_dest
) + ( x
* 2 );
399 uint8_t *z
= mlt_frame_get_alpha_mask( that
);
401 for ( i
= 0; i
< height_src
; i
++ )
407 for ( j
= 0; j
< width_src
; j
++ )
409 if ( q
>= lower
&& q
< upper
&& j
>= x_start
&& j
< x_end
)
413 uint8_t a
= ( z
== NULL
) ?
255 : *z
++;
414 float value
= ( weight
* ( float ) a
/ 255.0 );
415 *o
++ = (uint8_t)( y
* value
+ *q
++ * ( 1 - value
) );
416 *o
++ = (uint8_t)( uv
* value
+ *q
++ * ( 1 - value
) );
429 p_dest
+= stride_dest
;
435 void mlt_resize_yuv422( uint8_t *output
, int owidth
, int oheight
, uint8_t *input
, int iwidth
, int iheight
)
438 int istride
= iwidth
* 2;
439 int ostride
= owidth
* 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;
448 int in_y_range
= iheight
/ 2;
451 uint8_t *out_line
= output
;
454 // Calculate a middle and possibly invalid pointer in the input
455 uint8_t *in_middle
= input
+ istride
* in_y_range
+ in_x_range
* 2;
456 int in_line
= - out_y_range
* istride
- out_x_range
* 2;
459 // Loop for the entirety of our output height.
460 for ( y
= - out_y_range
; y
< out_y_range
; y
++ )
462 // Start at the beginning of the line
465 // Point the start of the current input line (NB: can be out of range)
468 // Loop for the entirety of our output row.
469 for ( x
= - out_x_range
; x
< out_x_range
; x
++ )
471 // Check if x and y are in the valid input range.
472 if ( abs( x
) < in_x_range
&& abs( y
) < in_y_range
)
474 // We're in the input range for this row.
475 *out_ptr
++ = *( in_middle
+ in_ptr
++ );
476 *out_ptr
++ = *( in_middle
+ in_ptr
++ );
480 // We're not in the input range for this row.
487 // Move to next output line
490 // Move to next input line
495 /** A resizing function for yuv422 frames - this does not rescale, but simply
496 resizes. It assumes yuv422 images available on the frame so use with care.
499 uint8_t *mlt_frame_resize_yuv422( mlt_frame
this, int owidth
, int oheight
)
502 mlt_properties properties
= mlt_frame_properties( this );
504 // Get the input image, width and height
505 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
506 int iwidth
= mlt_properties_get_int( properties
, "width" );
507 int iheight
= mlt_properties_get_int( properties
, "height" );
509 // If width and height are correct, don't do anything
510 if ( iwidth
!= owidth
|| iheight
!= oheight
)
512 // Create the output image
513 uint8_t *output
= malloc( owidth
* oheight
* 2 );
515 // Call the generic resize
516 mlt_resize_yuv422( output
, owidth
, oheight
, input
, iwidth
, iheight
);
518 // Now update the frame
519 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
520 mlt_properties_set_int( properties
, "width", owidth
);
521 mlt_properties_set_int( properties
, "height", oheight
);
527 // No change, return input
531 /** A rescaling function for yuv422 frames - low quality, and provided for testing
532 only. It assumes yuv422 images available on the frame so use with care.
535 uint8_t *mlt_frame_rescale_yuv422( mlt_frame
this, int owidth
, int oheight
)
538 mlt_properties properties
= mlt_frame_properties( this );
540 // Get the input image, width and height
541 uint8_t *input
= mlt_properties_get_data( properties
, "image", NULL
);
542 int iwidth
= mlt_properties_get_int( properties
, "width" );
543 int iheight
= mlt_properties_get_int( properties
, "height" );
545 // If width and height are correct, don't do anything
546 if ( iwidth
!= owidth
|| iheight
!= oheight
)
548 // Create the output image
549 uint8_t *output
= malloc( owidth
* oheight
* 2 );
552 int istride
= iwidth
* 2;
553 int ostride
= owidth
* 2;
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 float scale_width
= ( float )iwidth
/ ( float )owidth
;
578 float scale_height
= ( float )iheight
/ ( float )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
;
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
+= 2 )
595 // Calculated the derived x
596 dx
= scale_width
* x
;
598 // Check if x and y are in the valid input range.
599 if ( abs( dx
) < in_x_range
&& abs( dy
) < in_y_range
)
601 // We're in the input range for this row.
602 in_ptr
= in_line
+ ( dx
>> 1 ) * 4;
603 *out_ptr
++ = *in_ptr
++;
604 *out_ptr
++ = *in_ptr
++;
605 *out_ptr
++ = *in_ptr
++;
606 *out_ptr
++ = *in_ptr
++;
610 // We're not in the input range for this row.
618 // Move to next output line
622 // Now update the frame
623 mlt_properties_set_data( properties
, "image", output
, owidth
* oheight
* 2, free
, NULL
);
624 mlt_properties_set_int( properties
, "width", owidth
);
625 mlt_properties_set_int( properties
, "height", oheight
);
631 // No change, return input