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