]> git.sesse.net Git - mlt/blob - src/framework/mlt_frame.c
544bea3ee5888405d33a9fd1ab6f06ad5b95e01f
[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 "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                 }
57                 else
58                 {
59                         mlt_properties_set_int( properties, "width", 720 );
60                         mlt_properties_set_int( properties, "height", 480 );
61                         mlt_properties_set_int( properties, "normalised_width", 720 );
62                         mlt_properties_set_int( properties, "normalised_height", 480 );
63                 }
64
65                 mlt_properties_set_double( properties, "aspect_ratio", 4.0 / 3.0 );
66                 mlt_properties_set_data( properties, "audio", NULL, 0, NULL, NULL );
67                 mlt_properties_set_data( properties, "alpha", NULL, 0, NULL, NULL );
68
69                 // Construct stacks for frames and methods
70                 this->stack_get_image = mlt_deque_init( );
71                 this->stack_frame = mlt_deque_init( );
72                 this->stack_service = 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_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_get_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_get_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_frame, 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_frame );
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_service, 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_service );
180 }
181
182 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
183 {
184         mlt_properties properties = mlt_frame_properties( this );
185         mlt_get_image get_image = mlt_frame_pop_get_image( this );
186         mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
187         
188         if ( get_image != NULL )
189         {
190                 return get_image( this, buffer, format, width, height, writable );
191         }
192         else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
193         {
194                 *format = mlt_image_yuv422;
195                 *buffer = mlt_properties_get_data( properties, "image", NULL );
196                 *width = mlt_properties_get_int( properties, "width" );
197                 *height = mlt_properties_get_int( properties, "height" );
198         }
199         else if ( producer != NULL )
200         {
201                 mlt_frame test_frame = NULL;
202                 mlt_service_get_frame( mlt_producer_service( producer ), &test_frame, 0 );
203                 if ( test_frame != NULL )
204                 {
205                         mlt_properties test_properties = mlt_frame_properties( test_frame );
206                         mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
207                         mlt_properties_set_double( test_properties, "consumer_scale", mlt_properties_get_double( properties, "consumer_scale" ) );
208                         mlt_properties_set( test_properties, "rescale.interp", "nearest" );
209                         mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
210                         mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
211                         mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
212                         mlt_properties_set_int( properties, "width", *width );
213                         mlt_properties_set_int( properties, "height", *height );
214                 }
215                 else
216                 {
217                         mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
218                         mlt_frame_get_image( this, buffer, format, width, height, writable );
219                 }
220         }
221         else
222         {
223                 uint8_t *p;
224                 uint8_t *q;
225                 int size = 0;
226
227                 *width = *width == 0 ? 720 : *width;
228                 *height = *height == 0 ? 576 : *height;
229                 size = *width * *height;
230
231                 mlt_properties_set_int( properties, "width", *width );
232                 mlt_properties_set_int( properties, "height", *height );
233
234                 switch( *format )
235                 {
236                         case mlt_image_none:
237                                 size = 0;
238                                 *buffer = NULL;
239                                 break;
240                         case mlt_image_rgb24:
241                                 size *= 3;
242                                 size += *width * 3;
243                                 *buffer = mlt_pool_alloc( size );
244                                 if ( *buffer )
245                                         memset( *buffer, 255, size );
246                                 break;
247                         case mlt_image_rgb24a:
248                                 size *= 4;
249                                 size += *width * 4;
250                                 *buffer = mlt_pool_alloc( size );
251                                 if ( *buffer )
252                                         memset( *buffer, 255, size );
253                                 break;
254                         case mlt_image_yuv422:
255                                 size *= 2;
256                                 size += *width * 2;
257                                 *buffer = mlt_pool_alloc( size );
258                                 p = *buffer;
259                                 q = p + size;
260                                 while ( p != NULL && p != q )
261                                 {
262                                         *p ++ = 235;
263                                         *p ++ = 128;
264                                 }
265                                 break;
266                         case mlt_image_yuv420p:
267                                 size = size * 3 / 2;
268                                 *buffer = mlt_pool_alloc( size );
269                                 if ( *buffer )
270                                         memset( *buffer, 255, size );
271                                 break;
272                 }
273
274                 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
275                 mlt_properties_set_int( properties, "test_image", 1 );
276         }
277
278         return 0;
279 }
280
281 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
282 {
283         if ( this->get_alpha_mask != NULL )
284                 return this->get_alpha_mask( this );
285         return NULL;
286 }
287
288 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
289 {
290         mlt_properties properties = mlt_frame_properties( this );
291
292         if ( this->get_audio != NULL )
293         {
294                 return this->get_audio( this, buffer, format, frequency, channels, samples );
295         }
296         else
297         {
298                 int size = 0;
299                 *samples = *samples <= 0 ? 1920 : *samples;
300                 *channels = *channels <= 0 ? 2 : *channels;
301                 *frequency = *frequency <= 0 ? 48000 : *frequency;
302                 size = *samples * *channels * sizeof( int16_t );
303                 *buffer = mlt_pool_alloc( size );
304                 if ( *buffer != NULL )
305                         memset( *buffer, 0, size );
306                 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
307                 mlt_properties_set_int( properties, "test_audio", 1 );
308         }
309         return 0;
310 }
311
312 void mlt_frame_close( mlt_frame this )
313 {
314         if ( this != NULL )
315         {
316                 mlt_deque_close( this->stack_get_image );
317                 mlt_deque_close( this->stack_frame );
318                 mlt_deque_close( this->stack_service );
319                 mlt_properties_close( &this->parent );
320                 free( this );
321         }
322 }
323
324 /***** convenience functions *****/
325
326 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
327 {
328         int ret = 0;
329         register int y0, y1, u0, u1, v0, v1;
330         register int r, g, b;
331         register uint8_t *d = yuv;
332         register int i, j;
333
334         for ( i = 0; i < height; i++ )
335         {
336                 register uint8_t *s = rgba + ( stride * i );
337                 for ( j = 0; j < ( width / 2 ); j++ )
338                 {
339                         r = *s++;
340                         g = *s++;
341                         b = *s++;
342                         *alpha++ = *s++;
343                         RGB2YUV (r, g, b, y0, u0 , v0);
344                         r = *s++;
345                         g = *s++;
346                         b = *s++;
347                         *alpha++ = *s++;
348                         RGB2YUV (r, g, b, y1, u1 , v1);
349                         *d++ = y0;
350                         *d++ = (u0+u1) >> 1;
351                         *d++ = y1;
352                         *d++ = (v0+v1) >> 1;
353                 }
354                 if ( width % 2 )
355                 {
356                         r = *s++;
357                         g = *s++;
358                         b = *s++;
359                         *alpha++ = *s++;
360                         RGB2YUV (r, g, b, y0, u0 , v0);
361                         *d++ = y0;
362                         *d++ = u0;
363                 }
364         }
365         return ret;
366 }
367
368 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
369 {
370         int ret = 0;
371         register int y0, y1, u0, u1, v0, v1;
372         register int r, g, b;
373         register uint8_t *d = yuv;
374         register int i, j;
375
376         for ( i = 0; i < height; i++ )
377         {
378                 register uint8_t *s = rgb + ( stride * i );
379                 for ( j = 0; j < ( width / 2 ); j++ )
380                 {
381                         r = *s++;
382                         g = *s++;
383                         b = *s++;
384                         RGB2YUV (r, g, b, y0, u0 , v0);
385                         r = *s++;
386                         g = *s++;
387                         b = *s++;
388                         RGB2YUV (r, g, b, y1, u1 , v1);
389                         *d++ = y0;
390                         *d++ = (u0+u1) >> 1;
391                         *d++ = y1;
392                         *d++ = (v0+v1) >> 1;
393                 }
394                 if ( width % 2 )
395                 {
396                         r = *s++;
397                         g = *s++;
398                         b = *s++;
399                         RGB2YUV (r, g, b, y0, u0 , v0);
400                         *d++ = y0;
401                         *d++ = u0;
402                 }
403         }
404         return ret;
405 }
406
407 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
408 {
409         int ret = 0;
410         register int i, j;
411
412         int half = width >> 1;
413
414         uint8_t *Y = yuv420p;
415         uint8_t *U = Y + width * height;
416         uint8_t *V = U + width * height / 4;
417
418         register uint8_t *d = yuv;
419
420         for ( i = 0; i < height; i++ )
421         {
422                 register uint8_t *u = U + ( i / 2 ) * ( half );
423                 register uint8_t *v = V + ( i / 2 ) * ( half );
424
425                 for ( j = 0; j < half; j++ )
426                 {
427                         *d ++ = *Y ++;
428                         *d ++ = *u ++;
429                         *d ++ = *Y ++;
430                         *d ++ = *v ++;
431                 }
432         }
433         return ret;
434 }
435
436 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
437 {
438         // Calculate strides
439         int istride = iwidth * 2;
440         int ostride = owidth * 2;
441
442         iwidth = iwidth - ( iwidth % 4 );
443         owidth = owidth - ( owidth % 4 );
444         iheight = iheight - ( iheight % 2 );
445         oheight = oheight - ( oheight % 2 );
446
447         // Optimisation point
448         if ( iwidth == owidth && iheight == oheight )
449                 memcpy( output, input, iheight * istride );
450
451         // Coordinates (0,0 is middle of output)
452         int y;
453
454         // Calculate ranges
455         int out_x_range = owidth / 2;
456         int out_y_range = oheight / 2;
457         int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
458         int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
459
460         // Output pointers
461         uint8_t *out_line = output;
462         uint8_t *out_ptr = out_line;
463
464         // Calculate a middle and possibly invalid pointer in the input
465         uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
466         int in_line = - in_y_range * istride - in_x_range * 2;
467
468         int elements;
469
470         // Fill whole section with black
471         y = out_y_range - ( iheight / 2 );
472         int blank_elements = ostride * y / 2;
473         elements = blank_elements;
474         while ( elements -- )
475         {
476                 *out_line ++ = 16;
477                 *out_line ++ = 128;
478         }
479
480         int active_width = 2 * iwidth;
481         int inactive_width = out_x_range - in_x_range;
482
483         // Loop for the entirety of our output height.
484         while ( iheight -- )
485         {
486         // Start at the beginning of the line
487         out_ptr = out_line;
488
489                 // Fill the outer part with black
490                 elements = inactive_width;
491                 while ( elements -- )
492                 {
493                         *out_ptr ++ = 16;
494                         *out_ptr ++ = 128;
495                 }
496
497                 // We're in the input range for this row.
498                 memcpy( out_ptr, in_middle + in_line, active_width );
499                 out_ptr += active_width;
500
501                 // Fill the outer part with black
502                 elements = inactive_width;
503                 while ( elements -- )
504                 {
505                         *out_ptr ++ = 16;
506                         *out_ptr ++ = 128;
507                 }
508
509                 // Move to next input line
510                 in_line += istride;
511
512         // Move to next output line
513         out_line += ostride;
514         }
515
516         // Fill whole section with black
517         elements = blank_elements;
518         while ( elements -- )
519         {
520                 *out_line ++ = 16;
521                 *out_line ++ = 128;
522         }
523 }
524
525 /** A resizing function for yuv422 frames - this does not rescale, but simply
526         resizes. It assumes yuv422 images available on the frame so use with care.
527 */
528
529 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
530 {
531         // Get properties
532         mlt_properties properties = mlt_frame_properties( this );
533
534         // Get the input image, width and height
535         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
536         int iwidth = mlt_properties_get_int( properties, "width" );
537         int iheight = mlt_properties_get_int( properties, "height" );
538
539         // If width and height are correct, don't do anything
540         if ( iwidth != owidth || iheight != oheight )
541         {
542                 // Create the output image
543                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
544
545                 // Call the generic resize
546                 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
547
548                 // Now update the frame
549                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
550                 mlt_properties_set_int( properties, "width", owidth );
551                 mlt_properties_set_int( properties, "height", oheight );
552
553                 // Return the output
554                 return output;
555         }
556         // No change, return input
557         return input;
558 }
559
560 /** A rescaling function for yuv422 frames - low quality, and provided for testing
561         only. It assumes yuv422 images available on the frame so use with care.
562 */
563
564 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
565 {
566         // Get properties
567         mlt_properties properties = mlt_frame_properties( this );
568
569         // Get the input image, width and height
570         uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
571         int iwidth = mlt_properties_get_int( properties, "width" );
572         int iheight = mlt_properties_get_int( properties, "height" );
573
574         // If width and height are correct, don't do anything
575         if ( iwidth != owidth || iheight != oheight )
576         {
577                 // Create the output image
578                 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
579
580                 // Calculate strides
581                 int istride = iwidth * 2;
582                 int ostride = owidth * 2;
583
584                 iwidth = iwidth - ( iwidth % 4 );
585
586                 // Derived coordinates
587                 int dy, dx;
588
589         // Calculate ranges
590         int out_x_range = owidth / 2;
591         int out_y_range = oheight / 2;
592         int in_x_range = iwidth / 2;
593         int in_y_range = iheight / 2;
594
595         // Output pointers
596         register uint8_t *out_line = output;
597         register uint8_t *out_ptr;
598
599         // Calculate a middle pointer
600         uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
601         uint8_t *in_line;
602
603                 // Generate the affine transform scaling values
604                 register int scale_width = ( iwidth << 16 ) / owidth;
605                 register int scale_height = ( iheight << 16 ) / oheight;
606                 register int base = 0;
607
608                 int outer = out_x_range * scale_width;
609                 int bottom = out_y_range * scale_height;
610
611         // Loop for the entirety of our output height.
612         for ( dy = - bottom; dy < bottom; dy += scale_height )
613         {
614                 // Start at the beginning of the line
615                 out_ptr = out_line;
616         
617                 // Pointer to the middle of the input line
618                 in_line = in_middle + ( dy >> 16 ) * istride;
619
620                 // Loop for the entirety of our output row.
621                 for ( dx = - outer; dx < outer; dx += scale_width )
622                 {
623                                 base = dx >> 15;
624                                 base &= 0xfffffffe;
625                                 *out_ptr ++ = *( in_line + base );
626                                 base &= 0xfffffffc;
627                                 *out_ptr ++ = *( in_line + base + 1 );
628                                 dx += scale_width;
629                                 base = dx >> 15;
630                                 base &= 0xfffffffe;
631                                 *out_ptr ++ = *( in_line + base );
632                                 base &= 0xfffffffc;
633                                 *out_ptr ++ = *( in_line + base + 3 );
634                 }
635
636                 // Move to next output line
637                 out_line += ostride;
638         }
639
640                 // Now update the frame
641                 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
642                 mlt_properties_set_int( properties, "width", owidth );
643                 mlt_properties_set_int( properties, "height", oheight );
644
645                 // Return the output
646                 return output;
647         }
648
649         // No change, return input
650         return input;
651 }
652
653 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 )
654 {
655         int ret = 0;
656         int16_t *src, *dest;
657         int frequency_src = *frequency, frequency_dest = *frequency;
658         int channels_src = *channels, channels_dest = *channels;
659         int samples_src = *samples, samples_dest = *samples;
660         int i, j;
661         double d = 0, s = 0;
662
663         mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
664         //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" ) );
665         mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
666         //fprintf( stderr, "mix: frame src  samples %d channels %d\n", samples_src, channels_src );
667         
668         if ( channels_src > 6 )
669                 channels_src = 0;
670         if ( channels_dest > 6 )
671                 channels_dest = 0;
672         if ( samples_src > 4000 )
673                 samples_src = 0;
674         if ( samples_dest > 4000 )
675                 samples_dest = 0;
676
677         // determine number of samples to process
678         *samples = samples_src < samples_dest ? samples_src : samples_dest;
679         *channels = channels_src < channels_dest ? channels_src : channels_dest;
680         *buffer = dest;
681         *frequency = frequency_dest;
682
683         // Compute a smooth ramp over start to end
684         float weight = weight_start;
685         float weight_step = ( weight_end - weight_start ) / *samples;
686
687         // Mixdown
688         for ( i = 0; i < *samples; i++ )
689         {
690                 for ( j = 0; j < *channels; j++ )
691                 {
692                         if ( j < channels_dest )
693                                 d = (double) dest[ i * channels_dest + j ];
694                         if ( j < channels_src )
695                                 s = (double) src[ i * channels_src + j ];
696                         dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
697                 }
698                 weight += weight_step;
699         }
700
701         return ret;
702 }
703
704 int mlt_sample_calculator( float fps, int frequency, int64_t position )
705 {
706         int samples = 0;
707
708         if ( fps > 29 && fps <= 30 )
709         {
710                 samples = frequency / 30;
711
712                 switch ( frequency )
713                 {
714                         case 48000:
715                                 if ( position % 5 != 0 )
716                                         samples += 2;
717                                 break;
718                         case 44100:
719                                 if ( position % 300 == 0 )
720                                         samples = 1471;
721                                 else if ( position % 30 == 0 )
722                                         samples = 1470;
723                                 else if ( position % 2 == 0 )
724                                         samples = 1472;
725                                 else
726                                         samples = 1471;
727                                 break;
728                         case 32000:
729                                 if ( position % 30 == 0 )
730                                         samples = 1068;
731                                 else if ( position % 29 == 0 )
732                                         samples = 1067;
733                                 else if ( position % 4 == 2 )
734                                         samples = 1067;
735                                 else
736                                         samples = 1068;
737                                 break;
738                         default:
739                                 samples = 0;
740                 }
741         }
742         else if ( fps != 0 )
743         {
744                 samples = frequency / fps;
745         }
746
747         return samples;
748 }
749