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