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