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