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