19b7c81c758c88be6f883116ccd90b2d2641f821
[melted] / mlt / 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 // now do the compositing only to cropped extents
460 for ( i = 0; i < height_src; i++ )
461 {
462 uint8_t *p = p_src;
463 uint8_t *q = p_dest;
464 uint8_t *o = p_dest;
465 uint8_t *z = p_alpha;
466
467 for ( j = 0; j < width_src; j ++ )
468 {
469 uint8_t y = *p ++;
470 uint8_t uv = *p ++;
471 uint8_t a = ( z == NULL ) ? 255 : *z ++;
472 float value = ( weight * ( float ) a / 255.0 );
473 *o ++ = (uint8_t)( y * value + *q++ * ( 1 - value ) );
474 *o ++ = (uint8_t)( uv * value + *q++ * ( 1 - value ) );
475 }
476
477 p_src += stride_src;
478 p_dest += stride_dest;
479 if ( p_alpha )
480 p_alpha += stride_src / 2;
481 }
482
483 return ret;
484 }
485
486 void *memfill( void *dst, void *src, int l, int elements )
487 {
488 int i = 0;
489 if ( l == 2 )
490 {
491 uint8_t *p = dst;
492 uint8_t *src1 = src;
493 uint8_t *src2 = src + 1;
494 for ( i = 0; i < elements; i ++ )
495 {
496 *p ++ = *src1;
497 *p ++ = *src2;
498 }
499 dst = p;
500 }
501 else
502 {
503 for ( i = 0; i < elements; i ++ )
504 dst = memcpy( dst, src, l ) + l;
505 }
506 return dst;
507 }
508
509 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
510 {
511 // Calculate strides
512 int istride = iwidth * 2;
513 int ostride = owidth * 2;
514
515 iwidth = iwidth - ( iwidth % 4 );
516 owidth = owidth - ( owidth % 4 );
517 iheight = iheight - ( iheight % 2 );
518 oheight = oheight - ( oheight % 2 );
519
520 // Coordinates (0,0 is middle of output)
521 int y;
522
523 // Calculate ranges
524 int out_x_range = owidth / 2;
525 int out_y_range = oheight / 2;
526 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
527 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
528
529 // Output pointers
530 uint8_t *out_line = output;
531 uint8_t *out_ptr = out_line;
532
533 // Calculate a middle and possibly invalid pointer in the input
534 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
535 int in_line = - in_y_range * istride - in_x_range * 2;
536
537 uint8_t black[ 2 ] = { 16, 128 };
538
539 // Loop for the entirety of our output height.
540 for ( y = - out_y_range; y < out_y_range ; y ++ )
541 {
542 // Start at the beginning of the line
543 out_ptr = out_line;
544
545 if ( abs( y ) < iheight / 2 )
546 {
547 // Fill the outer part with black
548 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
549
550 // We're in the input range for this row.
551 memcpy( out_ptr, in_middle + in_line, 2 * iwidth );
552 out_ptr += 2 * iwidth;
553
554 // Fill the outer part with black
555 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
556
557 // Move to next input line
558 in_line += istride;
559 }
560 else
561 {
562 // Fill whole line with black
563 out_ptr = memfill( out_ptr, black, 2, owidth );
564 }
565
566 // Move to next output line
567 out_line += ostride;
568 }
569 }
570
571 /** A resizing function for yuv422 frames - this does not rescale, but simply
572 resizes. It assumes yuv422 images available on the frame so use with care.
573 */
574
575 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
576 {
577 // Get properties
578 mlt_properties properties = mlt_frame_properties( this );
579
580 // Get the input image, width and height
581 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
582 int iwidth = mlt_properties_get_int( properties, "width" );
583 int iheight = mlt_properties_get_int( properties, "height" );
584
585 // If width and height are correct, don't do anything
586 if ( iwidth != owidth || iheight != oheight )
587 {
588 // Create the output image
589 uint8_t *output = malloc( owidth * oheight * 2 );
590
591 // Call the generic resize
592 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
593
594 // Now update the frame
595 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
596 mlt_properties_set_int( properties, "width", owidth );
597 mlt_properties_set_int( properties, "height", oheight );
598
599 // Return the output
600 return output;
601 }
602
603 // No change, return input
604 return input;
605 }
606
607 /** A rescaling function for yuv422 frames - low quality, and provided for testing
608 only. It assumes yuv422 images available on the frame so use with care.
609 */
610
611 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
612 {
613 // Get properties
614 mlt_properties properties = mlt_frame_properties( this );
615
616 // Get the input image, width and height
617 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
618 int iwidth = mlt_properties_get_int( properties, "width" );
619 int iheight = mlt_properties_get_int( properties, "height" );
620
621 // If width and height are correct, don't do anything
622 if ( iwidth != owidth || iheight != oheight )
623 {
624 // Create the output image
625 uint8_t *output = malloc( owidth * oheight * 2 );
626
627 // Calculate strides
628 int istride = iwidth * 2;
629 int ostride = owidth * 2;
630
631 iwidth = iwidth - ( iwidth % 4 );
632
633 // Coordinates (0,0 is middle of output)
634 int y, x;
635
636 // Derived coordinates
637 int dy, dx;
638
639 // Calculate ranges
640 int out_x_range = owidth / 2;
641 int out_y_range = oheight / 2;
642 int in_x_range = iwidth / 2;
643 int in_y_range = iheight / 2;
644
645 // Output pointers
646 uint8_t *out_line = output;
647 uint8_t *out_ptr;
648
649 // Calculate a middle pointer
650 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
651 uint8_t *in_line;
652 uint8_t *in_ptr;
653
654 // Generate the affine transform scaling values
655 float scale_width = ( float )iwidth / ( float )owidth;
656 float scale_height = ( float )iheight / ( float )oheight;
657
658 // Loop for the entirety of our output height.
659 for ( y = - out_y_range; y < out_y_range ; y ++ )
660 {
661 // Calculate the derived y value
662 dy = scale_height * y;
663
664 // Start at the beginning of the line
665 out_ptr = out_line;
666
667 // Pointer to the middle of the input line
668 in_line = in_middle + dy * istride;
669
670 // Loop for the entirety of our output row.
671 for ( x = - out_x_range; x < out_x_range; x += 1 )
672 {
673 // Calculated the derived x
674 dx = scale_width * x;
675
676 // Check if x and y are in the valid input range.
677 if ( abs( dx ) < in_x_range && abs( dy ) < in_y_range )
678 {
679 // We're in the input range for this row.
680 in_ptr = in_line + ( dx >> 1 ) * 4 + 2 * ( x & 1 );
681 *out_ptr ++ = *in_ptr ++;
682 *out_ptr ++ = *in_ptr ++;
683 }
684 else
685 {
686 // We're not in the input range for this row.
687 *out_ptr ++ = 16;
688 *out_ptr ++ = 128;
689 }
690 }
691
692 // Move to next output line
693 out_line += ostride;
694 }
695
696 // Now update the frame
697 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
698 mlt_properties_set_int( properties, "width", owidth );
699 mlt_properties_set_int( properties, "height", oheight );
700
701 // Return the output
702 return output;
703 }
704
705 // No change, return input
706 return input;
707 }
708
709 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 )
710 {
711 int ret = 0;
712 int16_t *p_src, *p_dest;
713 int16_t *src, *dest;
714 //static int16_t *extra_src = NULL, *extra_dest = NULL;
715 static int extra_src_samples = 0, extra_dest_samples = 0;
716 int frequency_src = 0, frequency_dest = 0;
717 int channels_src = 0, channels_dest = 0;
718 int samples_src = 0, samples_dest = 0;
719 int i, j;
720
721 mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest );
722 //fprintf( stderr, "frame dest samples %d channels %d position %f\n", samples_dest, channels_dest, mlt_properties_get_position( mlt_frame_properties( this ), "position" ) );
723 mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src );
724 //fprintf( stderr, "frame src samples %d channels %d\n", samples_src, channels_src );
725 if ( channels_src > 6 )
726 channels_src = 0;
727 if ( channels_dest > 6 )
728 channels_dest = 0;
729 if ( samples_src > 4000 )
730 samples_src = 0;
731 if ( samples_dest > 4000 )
732 samples_dest = 0;
733
734 #if 0
735 // Append new samples to leftovers
736 if ( extra_dest_samples > 0 )
737 {
738 fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples );
739 dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest );
740 memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest );
741 }
742 else
743 dest = p_dest;
744 if ( extra_src_samples > 0 )
745 {
746 fprintf( stderr, "prepending %d samples to src\n", extra_src_samples );
747 src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src );
748 memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src );
749 }
750 else
751 src = p_src;
752 #else
753 src = p_src;
754 dest = p_dest;
755 #endif
756
757 // determine number of samples to process
758 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
759 *samples = samples_src + extra_src_samples;
760 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
761 *samples = samples_dest + extra_dest_samples;
762
763 *channels = channels_src < channels_dest ? channels_src : channels_dest;
764 *buffer = p_dest;
765
766 // Mixdown
767 for ( i = 0; i < *samples; i++ )
768 {
769 for ( j = 0; j < *channels; j++ )
770 {
771 double d = (double) dest[ i * channels_dest + j ];
772 double s = (double) src[ i * channels_src + j ];
773 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
774 }
775 }
776
777 // We have to copy --sigh
778 if ( dest != p_dest )
779 memcpy( p_dest, dest, *samples * 2 * *channels );
780
781 #if 0
782 // Store the leftovers
783 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
784 {
785 extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples );
786 size_t size = extra_dest_samples * 2 * channels_dest;
787 fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples );
788 if ( extra_dest )
789 free( extra_dest );
790 extra_dest = malloc( size );
791 if ( extra_dest )
792 memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size );
793 else
794 extra_dest_samples = 0;
795 }
796 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
797 {
798 extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples );
799 size_t size = extra_src_samples * 2 * channels_src;
800 fprintf( stderr, "storing %d samples from src\n", extra_dest_samples );
801 if ( extra_src )
802 free( extra_src );
803 extra_src = malloc( size );
804 if ( extra_src )
805 memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size );
806 else
807 extra_src_samples = 0;
808 }
809 #endif
810
811 return ret;
812 }
813
814 int mlt_sample_calculator( float fps, int frequency, int64_t position )
815 {
816 int samples = 0;
817
818 if ( fps > 29 && fps <= 30 )
819 {
820 samples = frequency / 30;
821
822 switch ( frequency )
823 {
824 case 48000:
825 if ( position % 5 != 0 )
826 samples += 2;
827 break;
828 case 44100:
829 if ( position % 300 == 0 )
830 samples = 1471;
831 else if ( position % 30 == 0 )
832 samples = 1470;
833 else if ( position % 2 == 0 )
834 samples = 1472;
835 else
836 samples = 1471;
837 break;
838 case 32000:
839 if ( position % 30 == 0 )
840 samples = 1068;
841 else if ( position % 29 == 0 )
842 samples = 1067;
843 else if ( position % 4 == 2 )
844 samples = 1067;
845 else
846 samples = 1068;
847 break;
848 default:
849 samples = 0;
850 }
851 }
852 else if ( fps != 0 )
853 {
854 samples = frequency / fps;
855 }
856
857 return samples;
858 }
859