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