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