More sdl experimental mods, pixbuf writable work around and minor fixes
[melted] / src / framework / mlt_frame.c
1 /*
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>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "config.h"
22 #include "mlt_frame.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 typedef struct
28 {
29 mlt_image_format vfmt;
30 int width;
31 int height;
32 uint8_t *image;
33 uint8_t *alpha;
34 mlt_audio_format afmt;
35 int16_t *audio;
36 }
37 frame_test;
38
39 static frame_test test_card = { mlt_image_none, 0, 0, NULL, NULL, mlt_audio_none, NULL };
40
41 /** Constructor for a frame.
42 */
43
44 mlt_frame mlt_frame_init( )
45 {
46 // Allocate a frame
47 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
48
49 if ( this != NULL )
50 {
51 // Initialise the properties
52 mlt_properties properties = &this->parent;
53 mlt_properties_init( properties, this );
54
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 );
63 }
64 return this;
65 }
66
67 /** Fetch the frames properties.
68 */
69
70 mlt_properties mlt_frame_properties( mlt_frame this )
71 {
72 return &this->parent;
73 }
74
75 /** Check if we have a way to derive something other than a test card.
76 */
77
78 int mlt_frame_is_test_card( mlt_frame this )
79 {
80 return this->stack_get_image_size == 0;
81 }
82
83 /** Get the aspect ratio of the frame.
84 */
85
86 double mlt_frame_get_aspect_ratio( mlt_frame this )
87 {
88 mlt_properties properties = mlt_frame_properties( this );
89 return mlt_properties_get_double( properties, "aspect_ratio" );
90 }
91
92 /** Set the aspect ratio of the frame.
93 */
94
95 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
96 {
97 mlt_properties properties = mlt_frame_properties( this );
98 return mlt_properties_set_double( properties, "aspect_ratio", value );
99 }
100
101 /** Get the timecode of this frame.
102 */
103
104 mlt_timecode mlt_frame_get_timecode( mlt_frame this )
105 {
106 mlt_properties properties = mlt_frame_properties( this );
107 return mlt_properties_get_timecode( properties, "timecode" );
108 }
109
110 /** Set the timecode of this frame.
111 */
112
113 int mlt_frame_set_timecode( mlt_frame this, mlt_timecode value )
114 {
115 mlt_properties properties = mlt_frame_properties( this );
116 return mlt_properties_set_timecode( properties, "timecode", value );
117 }
118
119 /** Stack a get_image callback.
120 */
121
122 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
123 {
124 int ret = this->stack_get_image_size >= 10;
125 if ( ret == 0 )
126 this->stack_get_image[ this->stack_get_image_size ++ ] = get_image;
127 return ret;
128 }
129
130 /** Pop a get_image callback.
131 */
132
133 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
134 {
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 ];
138 return result;
139 }
140
141 /** Push a frame.
142 */
143
144 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
145 {
146 int ret = this->stack_frame_size >= 10;
147 if ( ret == 0 )
148 this->stack_frame[ this->stack_frame_size ++ ] = that;
149 return ret;
150 }
151
152 /** Pop a frame.
153 */
154
155 mlt_frame mlt_frame_pop_frame( mlt_frame this )
156 {
157 mlt_frame result = NULL;
158 if ( this->stack_frame_size > 0 )
159 result = this->stack_frame[ -- this->stack_frame_size ];
160 return result;
161 }
162
163 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
164 {
165 mlt_properties properties = mlt_frame_properties( this );
166 mlt_get_image get_image = mlt_frame_pop_get_image( this );
167
168 if ( get_image != NULL )
169 {
170 return get_image( this, buffer, format, width, height, writable );
171 }
172 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
173 {
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" );
178 }
179 else
180 {
181 if ( test_card.vfmt != *format )
182 {
183 uint8_t *p;
184 uint8_t *q;
185
186 test_card.vfmt = *format;
187 test_card.width = *width == 0 ? 720 : *width;
188 test_card.height = *height == 0 ? 576 : *height;
189
190 switch( *format )
191 {
192 case mlt_image_none:
193 break;
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 );
197 break;
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 );
201 break;
202 case mlt_image_yuv422:
203 test_card.image = realloc( test_card.image, test_card.width * test_card.height * 2 );
204 p = test_card.image;
205 q = test_card.image + test_card.width * test_card.height * 2;
206 while ( p != q )
207 {
208 *p ++ = 255;
209 *p ++ = 128;
210 }
211 break;
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 );
215 break;
216 }
217 }
218
219 *width = test_card.width;
220 *height = test_card.height;
221 *buffer = test_card.image;
222 }
223
224 return 0;
225 }
226
227 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
228 {
229 if ( this->get_alpha_mask != NULL )
230 return this->get_alpha_mask( this );
231 return test_card.alpha;
232 }
233
234 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
235 {
236 if ( this->get_audio != NULL )
237 {
238 return this->get_audio( this, buffer, format, frequency, channels, samples );
239 }
240 else
241 {
242 if ( test_card.afmt != *format )
243 {
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 ) );
247 }
248
249 *buffer = test_card.audio;
250 *frequency = 48000;
251 *channels = 2;
252 *samples = 1920;
253 }
254 return 0;
255 }
256
257 void mlt_frame_close( mlt_frame this )
258 {
259 mlt_frame frame = mlt_frame_pop_frame( this );
260
261 while ( frame != NULL )
262 {
263 mlt_frame_close( frame);
264 frame = mlt_frame_pop_frame( this );
265 }
266
267 mlt_properties_close( &this->parent );
268
269 free( this );
270 }
271
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;\
277 y = y < 0 ? 0 : y;\
278 u = u < 0 ? 0 : u;\
279 v = v < 0 ? 0 : v;\
280 y = y > 255 ? 255 : y;\
281 u = u > 255 ? 255 : u;\
282 v = v > 255 ? 255 : v
283
284 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
285 {
286 int ret = 0;
287 register int y0, y1, u0, u1, v0, v1;
288 register int r, g, b;
289 register uint8_t *d = yuv;
290 register int i, j;
291
292 for ( i = 0; i < height; i++ )
293 {
294 register uint8_t *s = rgba + ( stride * i );
295 for ( j = 0; j < ( width / 2 ); j++ )
296 {
297 r = *s++;
298 g = *s++;
299 b = *s++;
300 *alpha++ = *s++;
301 RGB2YUV (r, g, b, y0, u0 , v0);
302 r = *s++;
303 g = *s++;
304 b = *s++;
305 *alpha++ = *s++;
306 RGB2YUV (r, g, b, y1, u1 , v1);
307 *d++ = y0;
308 *d++ = (u0+u1) >> 1;
309 *d++ = y1;
310 *d++ = (v0+v1) >> 1;
311 }
312 if ( width % 2 )
313 {
314 r = *s++;
315 g = *s++;
316 b = *s++;
317 *alpha++ = *s++;
318 RGB2YUV (r, g, b, y0, u0 , v0);
319 *d++ = y0;
320 *d++ = u0;
321 }
322 }
323 return ret;
324 }
325
326 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
327 {
328 int ret = 0;
329 register int y0, y1, u0, u1, v0, v1;
330 register int r, g, b;
331 register uint8_t *d = yuv;
332 register int i, j;
333
334 for ( i = 0; i < height; i++ )
335 {
336 register uint8_t *s = rgb + ( stride * i );
337 for ( j = 0; j < ( width / 2 ); j++ )
338 {
339 r = *s++;
340 g = *s++;
341 b = *s++;
342 RGB2YUV (r, g, b, y0, u0 , v0);
343 r = *s++;
344 g = *s++;
345 b = *s++;
346 RGB2YUV (r, g, b, y1, u1 , v1);
347 *d++ = y0;
348 *d++ = (u0+u1) >> 1;
349 *d++ = y1;
350 *d++ = (v0+v1) >> 1;
351 }
352 if ( width % 2 )
353 {
354 r = *s++;
355 g = *s++;
356 b = *s++;
357 RGB2YUV (r, g, b, y0, u0 , v0);
358 *d++ = y0;
359 *d++ = u0;
360 }
361 }
362 return ret;
363 }
364
365 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
366 {
367 int ret = 0;
368 register int i, j;
369
370 int half = width >> 1;
371
372 uint8_t *Y = yuv420p;
373 uint8_t *U = Y + width * height;
374 uint8_t *V = U + width * height / 4;
375
376 register uint8_t *d = yuv;
377
378 for ( i = 0; i < height; i++ )
379 {
380 register uint8_t *u = U + ( i / 2 ) * ( half );
381 register uint8_t *v = V + ( i / 2 ) * ( half );
382
383 for ( j = 0; j < half; j++ )
384 {
385 *d ++ = *Y ++;
386 *d ++ = *u ++;
387 *d ++ = *Y ++;
388 *d ++ = *v ++;
389 }
390 }
391 return ret;
392 }
393
394 int mlt_frame_composite_yuv( mlt_frame this, mlt_frame that, int x, int y, float weight )
395 {
396 int ret = 0;
397 int x_start = 0;
398 int width_src, height_src;
399 int width_dest, height_dest;
400 mlt_image_format format_src, format_dest;
401 uint8_t *p_src, *p_dest;
402 int x_end;
403 int i, j;
404 int stride_src;
405 int stride_dest;
406
407 format_src = mlt_image_yuv422;
408 format_dest = mlt_image_yuv422;
409
410 mlt_frame_get_image( this, &p_dest, &format_dest, &width_dest, &height_dest, 1 /* writable */ );
411 mlt_frame_get_image( that, &p_src, &format_src, &width_src, &height_src, 0 /* writable */ );
412
413 x_end = width_src;
414
415 stride_src = width_src * 2;
416 stride_dest = width_dest * 2;
417 uint8_t *lower = p_dest;
418 uint8_t *upper = p_dest + height_dest * stride_dest;
419
420 p_dest += ( y * stride_dest ) + ( x * 2 );
421
422 if ( x < 0 )
423 {
424 x_start = -x;
425 x_end += x_start;
426 }
427
428 uint8_t *z = mlt_frame_get_alpha_mask( that );
429
430 for ( i = 0; i < height_src; i++ )
431 {
432 uint8_t *p = p_src;
433 uint8_t *q = p_dest;
434 uint8_t *o = p_dest;
435
436 for ( j = 0; j < width_src; j ++ )
437 {
438 if ( q >= lower && q < upper && j >= x_start && j < x_end )
439 {
440 uint8_t y = *p ++;
441 uint8_t uv = *p ++;
442 uint8_t a = ( z == NULL ) ? 255 : *z ++;
443 float value = ( weight * ( float ) a / 255.0 );
444 *o ++ = (uint8_t)( y * value + *q++ * ( 1 - value ) );
445 *o ++ = (uint8_t)( uv * value + *q++ * ( 1 - value ) );
446 }
447 else
448 {
449 p += 2;
450 o += 2;
451 q += 2;
452 if ( z != NULL )
453 z += 1;
454 }
455 }
456
457 p_src += stride_src;
458 p_dest += stride_dest;
459 }
460
461 return ret;
462 }
463
464 void *memfill( void *dst, void *src, int l, int elements )
465 {
466 int i = 0;
467 for ( i = 0; i < elements; i ++ )
468 dst = memcpy( dst, src, l ) + l;
469 return dst;
470 }
471
472 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
473 {
474 // Calculate strides
475 int istride = iwidth * 2;
476 int ostride = owidth * 2;
477
478 iwidth = iwidth - ( iwidth % 4 );
479 owidth = owidth - ( owidth % 4 );
480 iheight = iheight - ( iheight % 2 );
481 oheight = oheight - ( oheight % 2 );
482
483 // Coordinates (0,0 is middle of output)
484 int y;
485
486 // Calculate ranges
487 int out_x_range = owidth / 2;
488 int out_y_range = oheight / 2;
489 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
490 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
491
492 // Output pointers
493 uint8_t *out_line = output;
494 uint8_t *out_ptr = out_line;
495
496 // Calculate a middle and possibly invalid pointer in the input
497 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
498 int in_line = - in_y_range * istride - in_x_range * 2;
499
500 uint8_t black[ 2 ] = { 16, 128 };
501
502 // Loop for the entirety of our output height.
503 for ( y = - out_y_range; y < out_y_range ; y ++ )
504 {
505 // Start at the beginning of the line
506 out_ptr = out_line;
507
508 if ( abs( y ) < iheight / 2 )
509 {
510 // Fill the outer part with black
511 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
512
513 // We're in the input range for this row.
514 memcpy( out_ptr, in_middle + in_line, 2 * iwidth );
515 out_ptr += 2 * iwidth;
516
517 // Fill the outer part with black
518 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
519
520 // Move to next input line
521 in_line += istride;
522 }
523 else
524 {
525 // Fill whole line with black
526 out_ptr = memfill( out_ptr, black, 2, owidth );
527 }
528
529 // Move to next output line
530 out_line += ostride;
531 }
532 }
533
534 /** A resizing function for yuv422 frames - this does not rescale, but simply
535 resizes. It assumes yuv422 images available on the frame so use with care.
536 */
537
538 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
539 {
540 // Get properties
541 mlt_properties properties = mlt_frame_properties( this );
542
543 // Get the input image, width and height
544 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
545 int iwidth = mlt_properties_get_int( properties, "width" );
546 int iheight = mlt_properties_get_int( properties, "height" );
547
548 // If width and height are correct, don't do anything
549 if ( iwidth != owidth || iheight != oheight )
550 {
551 // Create the output image
552 uint8_t *output = malloc( owidth * oheight * 2 );
553
554 // Call the generic resize
555 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
556
557 // Now update the frame
558 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
559 mlt_properties_set_int( properties, "width", owidth );
560 mlt_properties_set_int( properties, "height", oheight );
561
562 // Return the output
563 return output;
564 }
565
566 // No change, return input
567 return input;
568 }
569
570 /** A rescaling function for yuv422 frames - low quality, and provided for testing
571 only. It assumes yuv422 images available on the frame so use with care.
572 */
573
574 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
575 {
576 // Get properties
577 mlt_properties properties = mlt_frame_properties( this );
578
579 // Get the input image, width and height
580 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
581 int iwidth = mlt_properties_get_int( properties, "width" );
582 int iheight = mlt_properties_get_int( properties, "height" );
583
584 // If width and height are correct, don't do anything
585 if ( iwidth != owidth || iheight != oheight )
586 {
587 // Create the output image
588 uint8_t *output = malloc( owidth * oheight * 2 );
589
590 // Calculate strides
591 int istride = iwidth * 2;
592 int ostride = owidth * 2;
593
594 iwidth = iwidth - ( iwidth % 4 );
595
596 // Coordinates (0,0 is middle of output)
597 int y, x;
598
599 // Derived coordinates
600 int dy, dx;
601
602 // Calculate ranges
603 int out_x_range = owidth / 2;
604 int out_y_range = oheight / 2;
605 int in_x_range = iwidth / 2;
606 int in_y_range = iheight / 2;
607
608 // Output pointers
609 uint8_t *out_line = output;
610 uint8_t *out_ptr;
611
612 // Calculate a middle pointer
613 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
614 uint8_t *in_line;
615 uint8_t *in_ptr;
616
617 // Generate the affine transform scaling values
618 float scale_width = ( float )iwidth / ( float )owidth;
619 float scale_height = ( float )iheight / ( float )oheight;
620
621 // Loop for the entirety of our output height.
622 for ( y = - out_y_range; y < out_y_range ; y ++ )
623 {
624 // Calculate the derived y value
625 dy = scale_height * y;
626
627 // Start at the beginning of the line
628 out_ptr = out_line;
629
630 // Pointer to the middle of the input line
631 in_line = in_middle + dy * istride;
632
633 // Loop for the entirety of our output row.
634 for ( x = - out_x_range; x < out_x_range; x += 1 )
635 {
636 // Calculated the derived x
637 dx = scale_width * x;
638
639 // Check if x and y are in the valid input range.
640 if ( abs( dx ) < in_x_range && abs( dy ) < in_y_range )
641 {
642 // We're in the input range for this row.
643 in_ptr = in_line + ( dx >> 1 ) * 4 - 2 * ( x & 1 );
644 *out_ptr ++ = *in_ptr ++;
645 *out_ptr ++ = *in_ptr ++;
646 }
647 else
648 {
649 // We're not in the input range for this row.
650 *out_ptr ++ = 16;
651 *out_ptr ++ = 128;
652 }
653 }
654
655 // Move to next output line
656 out_line += ostride;
657 }
658
659 // Now update the frame
660 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
661 mlt_properties_set_int( properties, "width", owidth );
662 mlt_properties_set_int( properties, "height", oheight );
663
664 // Return the output
665 return output;
666 }
667
668 // No change, return input
669 return input;
670 }
671