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