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