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