3221f40b69c781da6f2edaeb0c44494dbd1da6da
[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 "mlt_producer.h"
24 #include "mlt_factory.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 /** Constructor for a frame.
30 */
31
32 mlt_frame mlt_frame_init( )
33 {
34 // Allocate a frame
35 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
36
37 if ( this != NULL )
38 {
39 // Get the normalisation
40 char *normalisation = mlt_environment( "MLT_NORMALISATION" );
41
42 // Initialise the properties
43 mlt_properties properties = &this->parent;
44 mlt_properties_init( properties, this );
45
46 // Set default properties on the frame
47 mlt_properties_set_position( properties, "_position", 0.0 );
48 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
49
50 if ( normalisation == NULL || strcmp( normalisation, "NTSC" ) )
51 {
52 mlt_properties_set_int( properties, "width", 720 );
53 mlt_properties_set_int( properties, "height", 576 );
54 mlt_properties_set_int( properties, "normalised_width", 720 );
55 mlt_properties_set_int( properties, "normalised_height", 576 );
56 }
57 else
58 {
59 mlt_properties_set_int( properties, "width", 720 );
60 mlt_properties_set_int( properties, "height", 480 );
61 mlt_properties_set_int( properties, "normalised_width", 720 );
62 mlt_properties_set_int( properties, "normalised_height", 480 );
63 }
64
65 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
66 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
67 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
68
69 // Construct stacks for frames and methods
70 this->stack_get_image = mlt_deque_init( );
71 this->stack_frame = mlt_deque_init( );
72 this->stack_service = mlt_deque_init( );
73 }
74
75 return this;
76 }
77
78 /** Fetch the frames properties.
79 */
80
81 mlt_properties mlt_frame_properties( mlt_frame this )
82 {
83 return &this->parent;
84 }
85
86 /** Check if we have a way to derive something other than a test card.
87 */
88
89 int mlt_frame_is_test_card( mlt_frame this )
90 {
91 return mlt_properties_get_int( mlt_frame_properties( this ), "test_image" );
92 }
93
94 /** Check if we have a way to derive something than test audio.
95 */
96
97 int mlt_frame_is_test_audio( mlt_frame this )
98 {
99 return this->get_audio == NULL || mlt_properties_get_int( mlt_frame_properties( this ), "test_audio" );
100 }
101
102 /** Get the aspect ratio of the frame.
103 */
104
105 double mlt_frame_get_aspect_ratio( mlt_frame this )
106 {
107 return mlt_properties_get_double( mlt_frame_properties( this ), "aspect_ratio" );
108 }
109
110 /** Set the aspect ratio of the frame.
111 */
112
113 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
114 {
115 return mlt_properties_set_double( mlt_frame_properties( this ), "aspect_ratio", value );
116 }
117
118 /** Get the position of this frame.
119 */
120
121 mlt_position mlt_frame_get_position( mlt_frame this )
122 {
123 return mlt_properties_get_position( mlt_frame_properties( this ), "_position" );
124 }
125
126 /** Set the position of this frame.
127 */
128
129 int mlt_frame_set_position( mlt_frame this, mlt_position value )
130 {
131 return mlt_properties_set_position( mlt_frame_properties( this ), "_position", value );
132 }
133
134 /** Stack a get_image callback.
135 */
136
137 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
138 {
139 return mlt_deque_push_back( this->stack_get_image, get_image );
140 }
141
142 /** Pop a get_image callback.
143 */
144
145 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
146 {
147 return mlt_deque_pop_back( this->stack_get_image );
148 }
149
150 /** Push a frame.
151 */
152
153 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
154 {
155 return mlt_deque_push_back( this->stack_frame, that );
156 }
157
158 /** Pop a frame.
159 */
160
161 mlt_frame mlt_frame_pop_frame( mlt_frame this )
162 {
163 return mlt_deque_pop_back( this->stack_frame );
164 }
165
166 /** Push a service.
167 */
168
169 int mlt_frame_push_service( mlt_frame this, void *that )
170 {
171 return mlt_deque_push_back( this->stack_service, that );
172 }
173
174 /** Pop a service.
175 */
176
177 void *mlt_frame_pop_service( mlt_frame this )
178 {
179 return mlt_deque_pop_back( this->stack_service );
180 }
181
182 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
183 {
184 mlt_properties properties = mlt_frame_properties( this );
185 mlt_get_image get_image = mlt_frame_pop_get_image( this );
186 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
187
188 if ( get_image != NULL )
189 {
190 return get_image( this, buffer, format, width, height, writable );
191 }
192 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
193 {
194 *format = mlt_image_yuv422;
195 *buffer = mlt_properties_get_data( properties, "image", NULL );
196 *width = mlt_properties_get_int( properties, "width" );
197 *height = mlt_properties_get_int( properties, "height" );
198 }
199 else if ( producer != NULL )
200 {
201 mlt_frame test_frame = NULL;
202 mlt_service_get_frame( mlt_producer_service( producer ), &test_frame, 0 );
203 if ( test_frame != NULL )
204 {
205 mlt_properties test_properties = mlt_frame_properties( test_frame );
206 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
207 mlt_properties_set_double( test_properties, "consumer_scale", mlt_properties_get_double( properties, "consumer_scale" ) );
208 mlt_properties_set( test_properties, "rescale.interp", "nearest" );
209 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
210 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
211 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
212 mlt_properties_set_int( properties, "width", *width );
213 mlt_properties_set_int( properties, "height", *height );
214 }
215 else
216 {
217 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
218 mlt_frame_get_image( this, buffer, format, width, height, writable );
219 }
220 }
221 else
222 {
223 uint8_t *p;
224 uint8_t *q;
225 int size = 0;
226
227 *width = *width == 0 ? 720 : *width;
228 *height = *height == 0 ? 576 : *height;
229 size = *width * *height;
230
231 mlt_properties_set_int( properties, "width", *width );
232 mlt_properties_set_int( properties, "height", *height );
233
234 switch( *format )
235 {
236 case mlt_image_none:
237 size = 0;
238 *buffer = NULL;
239 break;
240 case mlt_image_rgb24:
241 size *= 3;
242 size += *width * 3;
243 *buffer = mlt_pool_alloc( size );
244 if ( *buffer )
245 memset( *buffer, 255, size );
246 break;
247 case mlt_image_rgb24a:
248 size *= 4;
249 size += *width * 4;
250 *buffer = mlt_pool_alloc( size );
251 if ( *buffer )
252 memset( *buffer, 255, size );
253 break;
254 case mlt_image_yuv422:
255 size *= 2;
256 size += *width * 2;
257 *buffer = mlt_pool_alloc( size );
258 p = *buffer;
259 q = p + size;
260 while ( p != NULL && p != q )
261 {
262 *p ++ = 255;
263 *p ++ = 128;
264 }
265 break;
266 case mlt_image_yuv420p:
267 size = size * 3 / 2;
268 *buffer = mlt_pool_alloc( size );
269 if ( *buffer )
270 memset( *buffer, 255, size );
271 break;
272 }
273
274 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
275 mlt_properties_set_int( properties, "test_image", 1 );
276 }
277
278 return 0;
279 }
280
281 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
282 {
283 if ( this->get_alpha_mask != NULL )
284 return this->get_alpha_mask( this );
285 return NULL;
286 }
287
288 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
289 {
290 mlt_properties properties = mlt_frame_properties( this );
291
292 if ( this->get_audio != NULL )
293 {
294 return this->get_audio( this, buffer, format, frequency, channels, samples );
295 }
296 else
297 {
298 int size = 0;
299 *samples = *samples <= 0 ? 1920 : *samples;
300 *channels = *channels <= 0 ? 2 : *channels;
301 *frequency = *frequency <= 0 ? 48000 : *frequency;
302 size = *samples * *channels * sizeof( int16_t );
303 *buffer = mlt_pool_alloc( size );
304 if ( *buffer != NULL )
305 memset( *buffer, 0, size );
306 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
307 mlt_properties_set_int( properties, "test_audio", 1 );
308 }
309 return 0;
310 }
311
312 void mlt_frame_close( mlt_frame this )
313 {
314 if ( this != NULL )
315 {
316 mlt_deque_close( this->stack_get_image );
317 mlt_deque_close( this->stack_frame );
318 mlt_deque_close( this->stack_service );
319 mlt_properties_close( &this->parent );
320 free( this );
321 }
322 }
323
324 /***** convenience functions *****/
325 #define RGB2YUV(r, g, b, y, u, v)\
326 y = (306*r + 601*g + 117*b) >> 10;\
327 u = ((-172*r - 340*g + 512*b) >> 10) + 128;\
328 v = ((512*r - 429*g - 83*b) >> 10) + 128;\
329 y = y < 0 ? 0 : y;\
330 u = u < 0 ? 0 : u;\
331 v = v < 0 ? 0 : v;\
332 y = y > 255 ? 255 : y;\
333 u = u > 255 ? 255 : u;\
334 v = v > 255 ? 255 : v
335
336 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
337 {
338 int ret = 0;
339 register int y0, y1, u0, u1, v0, v1;
340 register int r, g, b;
341 register uint8_t *d = yuv;
342 register int i, j;
343
344 for ( i = 0; i < height; i++ )
345 {
346 register uint8_t *s = rgba + ( stride * i );
347 for ( j = 0; j < ( width / 2 ); j++ )
348 {
349 r = *s++;
350 g = *s++;
351 b = *s++;
352 *alpha++ = *s++;
353 RGB2YUV (r, g, b, y0, u0 , v0);
354 r = *s++;
355 g = *s++;
356 b = *s++;
357 *alpha++ = *s++;
358 RGB2YUV (r, g, b, y1, u1 , v1);
359 *d++ = y0;
360 *d++ = (u0+u1) >> 1;
361 *d++ = y1;
362 *d++ = (v0+v1) >> 1;
363 }
364 if ( width % 2 )
365 {
366 r = *s++;
367 g = *s++;
368 b = *s++;
369 *alpha++ = *s++;
370 RGB2YUV (r, g, b, y0, u0 , v0);
371 *d++ = y0;
372 *d++ = u0;
373 }
374 }
375 return ret;
376 }
377
378 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
379 {
380 int ret = 0;
381 register int y0, y1, u0, u1, v0, v1;
382 register int r, g, b;
383 register uint8_t *d = yuv;
384 register int i, j;
385
386 for ( i = 0; i < height; i++ )
387 {
388 register uint8_t *s = rgb + ( stride * i );
389 for ( j = 0; j < ( width / 2 ); j++ )
390 {
391 r = *s++;
392 g = *s++;
393 b = *s++;
394 RGB2YUV (r, g, b, y0, u0 , v0);
395 r = *s++;
396 g = *s++;
397 b = *s++;
398 RGB2YUV (r, g, b, y1, u1 , v1);
399 *d++ = y0;
400 *d++ = (u0+u1) >> 1;
401 *d++ = y1;
402 *d++ = (v0+v1) >> 1;
403 }
404 if ( width % 2 )
405 {
406 r = *s++;
407 g = *s++;
408 b = *s++;
409 RGB2YUV (r, g, b, y0, u0 , v0);
410 *d++ = y0;
411 *d++ = u0;
412 }
413 }
414 return ret;
415 }
416
417 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
418 {
419 int ret = 0;
420 register int i, j;
421
422 int half = width >> 1;
423
424 uint8_t *Y = yuv420p;
425 uint8_t *U = Y + width * height;
426 uint8_t *V = U + width * height / 4;
427
428 register uint8_t *d = yuv;
429
430 for ( i = 0; i < height; i++ )
431 {
432 register uint8_t *u = U + ( i / 2 ) * ( half );
433 register uint8_t *v = V + ( i / 2 ) * ( half );
434
435 for ( j = 0; j < half; j++ )
436 {
437 *d ++ = *Y ++;
438 *d ++ = *u ++;
439 *d ++ = *Y ++;
440 *d ++ = *v ++;
441 }
442 }
443 return ret;
444 }
445
446 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
447 {
448 // Calculate strides
449 int istride = iwidth * 2;
450 int ostride = owidth * 2;
451
452 iwidth = iwidth - ( iwidth % 4 );
453 owidth = owidth - ( owidth % 4 );
454 iheight = iheight - ( iheight % 2 );
455 oheight = oheight - ( oheight % 2 );
456
457 // Optimisation point
458 if ( iwidth == owidth && iheight == oheight )
459 memcpy( output, input, iheight * istride );
460
461 // Coordinates (0,0 is middle of output)
462 int y;
463
464 // Calculate ranges
465 int out_x_range = owidth / 2;
466 int out_y_range = oheight / 2;
467 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
468 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
469
470 // Output pointers
471 uint8_t *out_line = output;
472 uint8_t *out_ptr = out_line;
473
474 // Calculate a middle and possibly invalid pointer in the input
475 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
476 int in_line = - in_y_range * istride - in_x_range * 2;
477
478 int elements;
479
480 // Fill whole section with black
481 y = out_y_range - ( iheight / 2 );
482 int blank_elements = ostride * y / 2;
483 elements = blank_elements;
484 while ( elements -- )
485 {
486 *out_line ++ = 0;
487 *out_line ++ = 128;
488 }
489
490 int active_width = 2 * iwidth;
491 int inactive_width = out_x_range - in_x_range;
492
493 // Loop for the entirety of our output height.
494 while ( iheight -- )
495 {
496 // Start at the beginning of the line
497 out_ptr = out_line;
498
499 // Fill the outer part with black
500 elements = inactive_width;
501 while ( elements -- )
502 {
503 *out_ptr ++ = 0;
504 *out_ptr ++ = 128;
505 }
506
507 // We're in the input range for this row.
508 memcpy( out_ptr, in_middle + in_line, active_width );
509 out_ptr += active_width;
510
511 // Fill the outer part with black
512 elements = inactive_width;
513 while ( elements -- )
514 {
515 *out_ptr ++ = 0;
516 *out_ptr ++ = 128;
517 }
518
519 // Move to next input line
520 in_line += istride;
521
522 // Move to next output line
523 out_line += ostride;
524 }
525
526 // Fill whole section with black
527 elements = blank_elements;
528 while ( elements -- )
529 {
530 *out_line ++ = 0;
531 *out_line ++ = 128;
532 }
533 }
534
535 /** A resizing function for yuv422 frames - this does not rescale, but simply
536 resizes. It assumes yuv422 images available on the frame so use with care.
537 */
538
539 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
540 {
541 // Get properties
542 mlt_properties properties = mlt_frame_properties( this );
543
544 // Get the input image, width and height
545 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
546 int iwidth = mlt_properties_get_int( properties, "width" );
547 int iheight = mlt_properties_get_int( properties, "height" );
548
549 // If width and height are correct, don't do anything
550 if ( iwidth != owidth || iheight != oheight )
551 {
552 // Create the output image
553 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
554
555 // Call the generic resize
556 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
557
558 // Now update the frame
559 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
560 mlt_properties_set_int( properties, "width", owidth );
561 mlt_properties_set_int( properties, "height", oheight );
562
563 // Return the output
564 return output;
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 = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
589
590 // Calculate strides
591 int istride = iwidth * 2;
592 int ostride = owidth * 2;
593
594 iwidth = iwidth - ( iwidth % 4 );
595
596 // Derived coordinates
597 int dy, dx;
598
599 // Calculate ranges
600 int out_x_range = owidth / 2;
601 int out_y_range = oheight / 2;
602 int in_x_range = iwidth / 2;
603 int in_y_range = iheight / 2;
604
605 // Output pointers
606 register uint8_t *out_line = output;
607 register uint8_t *out_ptr;
608
609 // Calculate a middle pointer
610 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
611 uint8_t *in_line;
612
613 // Generate the affine transform scaling values
614 register int scale_width = ( iwidth << 16 ) / owidth;
615 register int scale_height = ( iheight << 16 ) / oheight;
616 register int base = 0;
617
618 int outer = out_x_range * scale_width;
619 int bottom = out_y_range * scale_height;
620
621 // Loop for the entirety of our output height.
622 for ( dy = - bottom; dy < bottom; dy += scale_height )
623 {
624 // Start at the beginning of the line
625 out_ptr = out_line;
626
627 // Pointer to the middle of the input line
628 in_line = in_middle + ( dy >> 16 ) * istride;
629
630 // Loop for the entirety of our output row.
631 for ( dx = - outer; dx < outer; dx += scale_width )
632 {
633 base = dx >> 15;
634 base &= 0xfffffffe;
635 *out_ptr ++ = *( in_line + base );
636 base &= 0xfffffffc;
637 *out_ptr ++ = *( in_line + base + 1 );
638 dx += scale_width;
639 base = dx >> 15;
640 base &= 0xfffffffe;
641 *out_ptr ++ = *( in_line + base );
642 base &= 0xfffffffc;
643 *out_ptr ++ = *( in_line + base + 3 );
644 }
645
646 // Move to next output line
647 out_line += ostride;
648 }
649
650 // Now update the frame
651 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
652 mlt_properties_set_int( properties, "width", owidth );
653 mlt_properties_set_int( properties, "height", oheight );
654
655 // Return the output
656 return output;
657 }
658
659 // No change, return input
660 return input;
661 }
662
663 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 )
664 {
665 int ret = 0;
666 int16_t *p_src, *p_dest;
667 int16_t *src, *dest;
668 //static int16_t *extra_src = NULL, *extra_dest = NULL;
669 static int extra_src_samples = 0, extra_dest_samples = 0;
670 int frequency_src = *frequency, frequency_dest = *frequency;
671 int channels_src = *channels, channels_dest = *channels;
672 int samples_src = *samples, samples_dest = *samples;
673 int i, j;
674 double d = 0, s = 0;
675
676 mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest );
677 //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" ) );
678 mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src );
679 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
680 src = p_src;
681 dest = p_dest;
682 if ( channels_src > 6 )
683 channels_src = 0;
684 if ( channels_dest > 6 )
685 channels_dest = 0;
686 if ( samples_src > 4000 )
687 samples_src = 0;
688 if ( samples_dest > 4000 )
689 samples_dest = 0;
690
691 #if 0
692 // Append new samples to leftovers
693 if ( extra_dest_samples > 0 )
694 {
695 fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples );
696 dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest );
697 memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest );
698 }
699 else
700 dest = p_dest;
701 if ( extra_src_samples > 0 )
702 {
703 fprintf( stderr, "prepending %d samples to src\n", extra_src_samples );
704 src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src );
705 memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src );
706 }
707 else
708 src = p_src;
709 #endif
710
711 // determine number of samples to process
712 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
713 *samples = samples_src + extra_src_samples;
714 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
715 *samples = samples_dest + extra_dest_samples;
716
717 *channels = channels_src < channels_dest ? channels_src : channels_dest;
718 *buffer = p_dest;
719 *frequency = frequency_dest;
720
721 // Compute a smooth ramp over start to end
722 float weight = weight_start;
723 float weight_step = ( weight_end - weight_start ) / *samples;
724
725 // Mixdown
726 for ( i = 0; i < *samples; i++ )
727 {
728 for ( j = 0; j < *channels; j++ )
729 {
730 if ( j < channels_dest )
731 d = (double) dest[ i * channels_dest + j ];
732 if ( j < channels_src )
733 s = (double) src[ i * channels_src + j ];
734 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
735 }
736 weight += weight_step;
737 }
738
739 // We have to copy --sigh
740 if ( dest != p_dest )
741 memcpy( p_dest, dest, *samples * 2 * *channels );
742
743 #if 0
744 // Store the leftovers
745 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
746 {
747 extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples );
748 size_t size = extra_dest_samples * 2 * channels_dest;
749 fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples );
750 if ( extra_dest )
751 free( extra_dest );
752 extra_dest = malloc( size );
753 if ( extra_dest )
754 memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size );
755 else
756 extra_dest_samples = 0;
757 }
758 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
759 {
760 extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples );
761 size_t size = extra_src_samples * 2 * channels_src;
762 fprintf( stderr, "storing %d samples from src\n", extra_dest_samples );
763 if ( extra_src )
764 free( extra_src );
765 extra_src = malloc( size );
766 if ( extra_src )
767 memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size );
768 else
769 extra_src_samples = 0;
770 }
771 #endif
772
773 return ret;
774 }
775
776 int mlt_sample_calculator( float fps, int frequency, int64_t position )
777 {
778 int samples = 0;
779
780 if ( fps > 29 && fps <= 30 )
781 {
782 samples = frequency / 30;
783
784 switch ( frequency )
785 {
786 case 48000:
787 if ( position % 5 != 0 )
788 samples += 2;
789 break;
790 case 44100:
791 if ( position % 300 == 0 )
792 samples = 1471;
793 else if ( position % 30 == 0 )
794 samples = 1470;
795 else if ( position % 2 == 0 )
796 samples = 1472;
797 else
798 samples = 1471;
799 break;
800 case 32000:
801 if ( position % 30 == 0 )
802 samples = 1068;
803 else if ( position % 29 == 0 )
804 samples = 1067;
805 else if ( position % 4 == 2 )
806 samples = 1067;
807 else
808 samples = 1068;
809 break;
810 default:
811 samples = 0;
812 }
813 }
814 else if ( fps != 0 )
815 {
816 samples = frequency / fps;
817 }
818
819 return samples;
820 }
821