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