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