filter fixes
[melted] / src / framework / mlt_frame.c
1 /*
2 * mlt_frame.c -- interface for all frame classes
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "config.h"
22 #include "mlt_frame.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 typedef struct
28 {
29 mlt_image_format vfmt;
30 int width;
31 int height;
32 uint8_t *image;
33 uint8_t *alpha;
34 mlt_audio_format afmt;
35 int16_t *audio;
36 }
37 frame_test;
38
39 static frame_test test_card = { mlt_image_none, 0, 0, NULL, NULL, mlt_audio_none, NULL };
40
41 /** Constructor for a frame.
42 */
43
44 mlt_frame mlt_frame_init( )
45 {
46 // Allocate a frame
47 mlt_frame this = calloc( sizeof( struct mlt_frame_s ), 1 );
48
49 if ( this != NULL )
50 {
51 // Initialise the properties
52 mlt_properties properties = &this->parent;
53 mlt_properties_init( properties, this );
54
55 // Set default properties on the frame
56 mlt_properties_set_position( properties, "_position", 0.0 );
57 mlt_properties_set_data( properties, "image", NULL, 0, NULL, NULL );
58 mlt_properties_set_int( properties, "width", 720 );
59 mlt_properties_set_int( properties, "height", 576 );
60 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
61 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
62 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
63 }
64 return this;
65 }
66
67 /** Fetch the frames properties.
68 */
69
70 mlt_properties mlt_frame_properties( mlt_frame this )
71 {
72 return &this->parent;
73 }
74
75 /** Check if we have a way to derive something other than a test card.
76 */
77
78 int mlt_frame_is_test_card( mlt_frame this )
79 {
80 return ( this->stack_get_image_size == 0 && mlt_properties_get_data( mlt_frame_properties( this ), "image", NULL ) == NULL );
81 }
82
83 /** Check if we have a way to derive something than test audio.
84 */
85
86 int mlt_frame_is_test_audio( mlt_frame this )
87 {
88 return this->get_audio == NULL;
89 }
90
91 /** Get the aspect ratio of the frame.
92 */
93
94 double mlt_frame_get_aspect_ratio( mlt_frame this )
95 {
96 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 || test_card.width != *width || test_card.height != *height || test_card.image == NULL )
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 mlt_properties_set_int( properties, "width", *width );
228 mlt_properties_set_int( properties, "height", *height );
229
230 if ( writable )
231 {
232 uint8_t *copy = malloc( *width * *height * 2 );
233 memcpy( copy, *buffer, *width * *height * 2 );
234 mlt_properties_set_data( properties, "image", copy, *width * *height * 2, free, NULL );
235 *buffer = copy;
236 }
237 else
238 {
239 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
240 }
241 }
242
243 return 0;
244 }
245
246 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
247 {
248 if ( this->get_alpha_mask != NULL )
249 return this->get_alpha_mask( this );
250 return test_card.alpha;
251 }
252
253 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
254 {
255 if ( this->get_audio != NULL )
256 {
257 return this->get_audio( this, buffer, format, frequency, channels, samples );
258 }
259 else
260 {
261 if ( *samples <= 0 )
262 *samples = 1920;
263 if ( *channels <= 0 )
264 *channels = 2;
265 if ( *frequency <= 0 )
266 *frequency = 48000;
267 if ( test_card.audio == NULL || test_card.afmt != *format )
268 {
269 test_card.afmt = *format;
270 test_card.audio = realloc( test_card.audio, *samples * *channels * sizeof( int16_t ) );
271 memset( test_card.audio, 0, *samples * *channels * sizeof( int16_t ) );
272 }
273
274 *buffer = test_card.audio;
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 // Coordinates (0,0 is middle of output)
442 int y;
443
444 // Calculate ranges
445 int out_x_range = owidth / 2;
446 int out_y_range = oheight / 2;
447 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
448 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
449
450 // Output pointers
451 uint8_t *out_line = output;
452 uint8_t *out_ptr = out_line;
453
454 // Calculate a middle and possibly invalid pointer in the input
455 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
456 int in_line = - in_y_range * istride - in_x_range * 2;
457
458 uint8_t black[ 2 ] = { 16, 128 };
459
460 // Loop for the entirety of our output height.
461 for ( y = - out_y_range; y < out_y_range ; y ++ )
462 {
463 // Start at the beginning of the line
464 out_ptr = out_line;
465
466 if ( abs( y ) < iheight / 2 )
467 {
468 // Fill the outer part with black
469 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
470
471 // We're in the input range for this row.
472 memcpy( out_ptr, in_middle + in_line, 2 * iwidth );
473 out_ptr += 2 * iwidth;
474
475 // Fill the outer part with black
476 out_ptr = memfill( out_ptr, black, 2, out_x_range - in_x_range );
477
478 // Move to next input line
479 in_line += istride;
480 }
481 else
482 {
483 // Fill whole line with black
484 out_ptr = memfill( out_ptr, black, 2, owidth );
485 }
486
487 // Move to next output line
488 out_line += ostride;
489 }
490 }
491
492 /** A resizing function for yuv422 frames - this does not rescale, but simply
493 resizes. It assumes yuv422 images available on the frame so use with care.
494 */
495
496 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
497 {
498 // Get properties
499 mlt_properties properties = mlt_frame_properties( this );
500
501 // Get the input image, width and height
502 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
503 int iwidth = mlt_properties_get_int( properties, "width" );
504 int iheight = mlt_properties_get_int( properties, "height" );
505
506 // If width and height are correct, don't do anything
507 if ( iwidth != owidth || iheight != oheight )
508 {
509 // Create the output image
510 uint8_t *output = malloc( owidth * oheight * 2 );
511
512 // Call the generic resize
513 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
514
515 // Now update the frame
516 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
517 mlt_properties_set_int( properties, "width", owidth );
518 mlt_properties_set_int( properties, "height", oheight );
519 mlt_frame_set_aspect_ratio( this, 4.0/3.0/*( float )owidth / oheight*/ );
520
521 // Return the output
522 return output;
523 }
524
525 // No change, return input
526 return input;
527 }
528
529 /** A rescaling function for yuv422 frames - low quality, and provided for testing
530 only. It assumes yuv422 images available on the frame so use with care.
531 */
532
533 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
534 {
535 // Get properties
536 mlt_properties properties = mlt_frame_properties( this );
537
538 // Get the input image, width and height
539 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
540 int iwidth = mlt_properties_get_int( properties, "width" );
541 int iheight = mlt_properties_get_int( properties, "height" );
542
543 // If width and height are correct, don't do anything
544 if ( iwidth != owidth || iheight != oheight )
545 {
546 // Create the output image
547 uint8_t *output = malloc( owidth * oheight * 2 );
548
549 // Calculate strides
550 int istride = iwidth * 2;
551 int ostride = owidth * 2;
552
553 iwidth = iwidth - ( iwidth % 4 );
554
555 // Coordinates (0,0 is middle of output)
556 int y, x;
557
558 // Derived coordinates
559 int dy, dx;
560
561 // Calculate ranges
562 int out_x_range = owidth / 2;
563 int out_y_range = oheight / 2;
564 int in_x_range = iwidth / 2;
565 int in_y_range = iheight / 2;
566
567 // Output pointers
568 uint8_t *out_line = output;
569 uint8_t *out_ptr;
570
571 // Calculate a middle pointer
572 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
573 uint8_t *in_line;
574 uint8_t *in_ptr;
575
576 // Generate the affine transform scaling values
577 int scale_width = ( iwidth << 16 ) / owidth;
578 int scale_height = ( iheight << 16 ) / oheight;
579
580 // Loop for the entirety of our output height.
581 for ( y = - out_y_range; y < out_y_range ; y ++ )
582 {
583 // Calculate the derived y value
584 dy = ( scale_height * y ) >> 16;
585
586 // Start at the beginning of the line
587 out_ptr = out_line;
588
589 // Pointer to the middle of the input line
590 in_line = in_middle + dy * istride;
591
592 // Loop for the entirety of our output row.
593 for ( x = - out_x_range; x < out_x_range; x += 1 )
594 {
595 // Calculated the derived x
596 dx = ( scale_width * x ) >> 16;
597
598 // We're in the input range for this row.
599 in_ptr = in_line + ( dx << 1 );
600 *out_ptr ++ = *in_ptr ++;
601 in_ptr = in_line + ( ( dx >> 1 ) << 2 ) + ( ( x & 1 ) << 1 ) + 1;
602 *out_ptr ++ = *in_ptr;
603 }
604
605 // Move to next output line
606 out_line += ostride;
607 }
608
609 // Now update the frame
610 mlt_properties_set_data( properties, "image", output, owidth * oheight * 2, free, NULL );
611 mlt_properties_set_int( properties, "width", owidth );
612 mlt_properties_set_int( properties, "height", oheight );
613
614 // Return the output
615 return output;
616 }
617
618 // No change, return input
619 return input;
620 }
621
622 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 )
623 {
624 int ret = 0;
625 int16_t *p_src, *p_dest;
626 int16_t *src, *dest;
627 //static int16_t *extra_src = NULL, *extra_dest = NULL;
628 static int extra_src_samples = 0, extra_dest_samples = 0;
629 int frequency_src = *frequency, frequency_dest = *frequency;
630 int channels_src = *channels, channels_dest = *channels;
631 int samples_src = *samples, samples_dest = *samples;
632 int i, j;
633 double d = 0, s = 0;
634
635 mlt_frame_get_audio( this, &p_dest, format, &frequency_dest, &channels_dest, &samples_dest );
636 //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" ) );
637 mlt_frame_get_audio( that, &p_src, format, &frequency_src, &channels_src, &samples_src );
638 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
639 src = p_src;
640 dest = p_dest;
641 if ( channels_src > 6 )
642 channels_src = 0;
643 if ( channels_dest > 6 )
644 channels_dest = 0;
645 if ( samples_src > 4000 )
646 samples_src = 0;
647 if ( samples_dest > 4000 )
648 samples_dest = 0;
649
650 #if 0
651 // Append new samples to leftovers
652 if ( extra_dest_samples > 0 )
653 {
654 fprintf( stderr, "prepending %d samples to dest\n", extra_dest_samples );
655 dest = realloc( extra_dest, ( samples_dest + extra_dest_samples ) * 2 * channels_dest );
656 memcpy( &extra_dest[ extra_dest_samples * channels_dest ], p_dest, samples_dest * 2 * channels_dest );
657 }
658 else
659 dest = p_dest;
660 if ( extra_src_samples > 0 )
661 {
662 fprintf( stderr, "prepending %d samples to src\n", extra_src_samples );
663 src = realloc( extra_src, ( samples_src + extra_src_samples ) * 2 * channels_src );
664 memcpy( &extra_src[ extra_src_samples * channels_src ], p_src, samples_src * 2 * channels_src );
665 }
666 else
667 src = p_src;
668 #endif
669
670 // determine number of samples to process
671 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
672 *samples = samples_src + extra_src_samples;
673 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
674 *samples = samples_dest + extra_dest_samples;
675
676 *channels = channels_src < channels_dest ? channels_src : channels_dest;
677 *buffer = p_dest;
678 *frequency = frequency_dest;
679
680 // Mixdown
681 for ( i = 0; i < *samples; i++ )
682 {
683 for ( j = 0; j < *channels; j++ )
684 {
685 if ( j < channels_dest )
686 d = (double) dest[ i * channels_dest + j ];
687 if ( j < channels_src )
688 s = (double) src[ i * channels_src + j ];
689 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
690 }
691 }
692
693 // We have to copy --sigh
694 if ( dest != p_dest )
695 memcpy( p_dest, dest, *samples * 2 * *channels );
696
697 #if 0
698 // Store the leftovers
699 if ( samples_src + extra_src_samples < samples_dest + extra_dest_samples )
700 {
701 extra_dest_samples = ( samples_dest + extra_dest_samples ) - ( samples_src + extra_src_samples );
702 size_t size = extra_dest_samples * 2 * channels_dest;
703 fprintf( stderr, "storing %d samples from dest\n", extra_dest_samples );
704 if ( extra_dest )
705 free( extra_dest );
706 extra_dest = malloc( size );
707 if ( extra_dest )
708 memcpy( extra_dest, &p_dest[ ( samples_dest - extra_dest_samples - 1 ) * channels_dest ], size );
709 else
710 extra_dest_samples = 0;
711 }
712 else if ( samples_dest + extra_dest_samples < samples_src + extra_src_samples )
713 {
714 extra_src_samples = ( samples_src + extra_src_samples ) - ( samples_dest + extra_dest_samples );
715 size_t size = extra_src_samples * 2 * channels_src;
716 fprintf( stderr, "storing %d samples from src\n", extra_dest_samples );
717 if ( extra_src )
718 free( extra_src );
719 extra_src = malloc( size );
720 if ( extra_src )
721 memcpy( extra_src, &p_src[ ( samples_src - extra_src_samples - 1 ) * channels_src ], size );
722 else
723 extra_src_samples = 0;
724 }
725 #endif
726
727 return ret;
728 }
729
730 int mlt_sample_calculator( float fps, int frequency, int64_t position )
731 {
732 int samples = 0;
733
734 if ( fps > 29 && fps <= 30 )
735 {
736 samples = frequency / 30;
737
738 switch ( frequency )
739 {
740 case 48000:
741 if ( position % 5 != 0 )
742 samples += 2;
743 break;
744 case 44100:
745 if ( position % 300 == 0 )
746 samples = 1471;
747 else if ( position % 30 == 0 )
748 samples = 1470;
749 else if ( position % 2 == 0 )
750 samples = 1472;
751 else
752 samples = 1471;
753 break;
754 case 32000:
755 if ( position % 30 == 0 )
756 samples = 1068;
757 else if ( position % 29 == 0 )
758 samples = 1067;
759 else if ( position % 4 == 2 )
760 samples = 1067;
761 else
762 samples = 1068;
763 break;
764 default:
765 samples = 0;
766 }
767 }
768 else if ( fps != 0 )
769 {
770 samples = frequency / fps;
771 }
772
773 return samples;
774 }
775