Cloning optimisations and introduction of the service parser
[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 != NULL ? &this->parent : NULL;
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 *width = *width >> 1 << 1;
205
206 if ( get_image != NULL )
207 {
208 int error = 0;
209 mlt_position position = mlt_frame_get_position( this );
210 error = get_image( this, buffer, format, width, height, writable );
211 mlt_frame_set_position( this, position );
212 return error;
213 }
214 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
215 {
216 *format = mlt_image_yuv422;
217 *buffer = mlt_properties_get_data( properties, "image", NULL );
218 *width = mlt_properties_get_int( properties, "width" );
219 *height = mlt_properties_get_int( properties, "height" );
220 }
221 else if ( producer != NULL )
222 {
223 mlt_frame test_frame = NULL;
224 mlt_service_get_frame( mlt_producer_service( producer ), &test_frame, 0 );
225 if ( test_frame != NULL )
226 {
227 mlt_properties test_properties = mlt_frame_properties( test_frame );
228 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
229 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
230 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
231 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
232 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
233 mlt_properties_set_int( properties, "width", *width );
234 mlt_properties_set_int( properties, "height", *height );
235 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
236 mlt_properties_set( properties, "rescale.interp", "none" );
237 mlt_properties_set( properties, "scale", "off" );
238 }
239 else
240 {
241 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
242 mlt_frame_get_image( this, buffer, format, width, height, writable );
243 }
244 }
245 else
246 {
247 register uint8_t *p;
248 register uint8_t *q;
249 int size = 0;
250
251 *width = *width == 0 ? 720 : *width;
252 *height = *height == 0 ? 576 : *height;
253 size = *width * *height;
254
255 mlt_properties_set_int( properties, "width", *width );
256 mlt_properties_set_int( properties, "height", *height );
257 mlt_properties_set_int( properties, "aspect_ratio", 1 );
258
259 switch( *format )
260 {
261 case mlt_image_none:
262 size = 0;
263 *buffer = NULL;
264 break;
265 case mlt_image_rgb24:
266 size *= 3;
267 size += *width * 3;
268 *buffer = mlt_pool_alloc( size );
269 if ( *buffer )
270 memset( *buffer, 255, size );
271 break;
272 case mlt_image_rgb24a:
273 size *= 4;
274 size += *width * 4;
275 *buffer = mlt_pool_alloc( size );
276 if ( *buffer )
277 memset( *buffer, 255, size );
278 break;
279 case mlt_image_yuv422:
280 size *= 2;
281 size += *width * 2;
282 *buffer = mlt_pool_alloc( size );
283 p = *buffer;
284 q = p + size;
285 while ( p != NULL && p != q )
286 {
287 *p ++ = 235;
288 *p ++ = 128;
289 }
290 break;
291 case mlt_image_yuv420p:
292 size = size * 3 / 2;
293 *buffer = mlt_pool_alloc( size );
294 if ( *buffer )
295 memset( *buffer, 255, size );
296 break;
297 }
298
299 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
300 mlt_properties_set_int( properties, "test_image", 1 );
301 }
302
303 return 0;
304 }
305
306 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
307 {
308 if ( this->get_alpha_mask != NULL )
309 return this->get_alpha_mask( this );
310 return NULL;
311 }
312
313 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
314 {
315 mlt_properties properties = mlt_frame_properties( this );
316
317 if ( this->get_audio != NULL )
318 {
319 this->get_audio( this, buffer, format, frequency, channels, samples );
320 }
321 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
322 {
323 *buffer = mlt_properties_get_data( properties, "audio", NULL );
324 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
325 *channels = mlt_properties_get_int( properties, "audio_channels" );
326 *samples = mlt_properties_get_int( properties, "audio_samples" );
327 }
328 else
329 {
330 int size = 0;
331 *samples = *samples <= 0 ? 1920 : *samples;
332 *channels = *channels <= 0 ? 2 : *channels;
333 *frequency = *frequency <= 0 ? 48000 : *frequency;
334 size = *samples * *channels * sizeof( int16_t );
335 *buffer = mlt_pool_alloc( size );
336 if ( *buffer != NULL )
337 memset( *buffer, 0, size );
338 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
339 mlt_properties_set_int( properties, "test_audio", 1 );
340 }
341
342 mlt_properties_set_int( properties, "audio_frequency", *frequency );
343 mlt_properties_set_int( properties, "audio_channels", *channels );
344 mlt_properties_set_int( properties, "audio_samples", *samples );
345
346 return 0;
347 }
348
349 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
350 {
351 int16_t *pcm = NULL;
352 mlt_properties properties = mlt_frame_properties( this );
353 mlt_audio_format format = mlt_audio_pcm;
354 int frequency = 32000; // lower frequency available?
355 int channels = 2;
356 double fps = mlt_properties_get_double( properties, "fps" );
357 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
358
359 // Get the pcm data
360 mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
361
362 // Make an 8-bit buffer large enough to hold rendering
363 int size = w * h;
364 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
365 if ( bitmap != NULL )
366 memset( bitmap, 0, size );
367 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
368
369 // Render vertical lines
370 int16_t *ubound = pcm + samples * channels;
371 int skip = samples / w - 1;
372 int i, j, k;
373
374 // Iterate sample stream and along x coordinate
375 for ( i = 0; i < w && pcm < ubound; i++ )
376 {
377 // pcm data has channels interleaved
378 for ( j = 0; j < channels; j++ )
379 {
380 // Determine sample's magnitude from 2s complement;
381 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
382 // The height of a line is the ratio of the magnitude multiplied by
383 // half the vertical resolution
384 int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
385 // Determine the starting y coordinate - left channel above center,
386 // right channel below - currently assumes 2 channels
387 int displacement = ( h / 2 ) - ( 1 - j ) * height;
388 // Position buffer pointer using y coordinate, stride, and x coordinate
389 unsigned char *p = &bitmap[ i + displacement * w ];
390
391 // Draw vertical line
392 for ( k = 0; k < height; k++ )
393 p[ w * k ] = 0xFF;
394
395 pcm++;
396 }
397 pcm += skip * channels;
398 }
399
400 return bitmap;
401 }
402
403 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
404 {
405 if ( this != NULL )
406 return mlt_properties_get_data( mlt_frame_properties( this ), "_producer", NULL );
407 return NULL;
408 }
409
410 void mlt_frame_close( mlt_frame this )
411 {
412 if ( this != NULL && mlt_properties_dec_ref( mlt_frame_properties( this ) ) <= 0 )
413 {
414 mlt_deque_close( this->stack_image );
415 mlt_deque_close( this->stack_audio );
416 mlt_properties_close( &this->parent );
417 free( this );
418 }
419 }
420
421 /***** convenience functions *****/
422
423 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
424 {
425 int ret = 0;
426 register int y0, y1, u0, u1, v0, v1;
427 register int r, g, b;
428 register uint8_t *d = yuv;
429 register int i, j;
430
431 for ( i = 0; i < height; i++ )
432 {
433 register uint8_t *s = rgba + ( stride * i );
434 for ( j = 0; j < ( width / 2 ); j++ )
435 {
436 r = *s++;
437 g = *s++;
438 b = *s++;
439 *alpha++ = *s++;
440 RGB2YUV (r, g, b, y0, u0 , v0);
441 r = *s++;
442 g = *s++;
443 b = *s++;
444 *alpha++ = *s++;
445 RGB2YUV (r, g, b, y1, u1 , v1);
446 *d++ = y0;
447 *d++ = (u0+u1) >> 1;
448 *d++ = y1;
449 *d++ = (v0+v1) >> 1;
450 }
451 if ( width % 2 )
452 {
453 r = *s++;
454 g = *s++;
455 b = *s++;
456 *alpha++ = *s++;
457 RGB2YUV (r, g, b, y0, u0 , v0);
458 *d++ = y0;
459 *d++ = u0;
460 }
461 }
462 return ret;
463 }
464
465 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
466 {
467 int ret = 0;
468 register int y0, y1, u0, u1, v0, v1;
469 register int r, g, b;
470 register uint8_t *d = yuv;
471 register int i, j;
472
473 for ( i = 0; i < height; i++ )
474 {
475 register uint8_t *s = rgb + ( stride * i );
476 for ( j = 0; j < ( width / 2 ); j++ )
477 {
478 r = *s++;
479 g = *s++;
480 b = *s++;
481 RGB2YUV (r, g, b, y0, u0 , v0);
482 r = *s++;
483 g = *s++;
484 b = *s++;
485 RGB2YUV (r, g, b, y1, u1 , v1);
486 *d++ = y0;
487 *d++ = (u0+u1) >> 1;
488 *d++ = y1;
489 *d++ = (v0+v1) >> 1;
490 }
491 if ( width % 2 )
492 {
493 r = *s++;
494 g = *s++;
495 b = *s++;
496 RGB2YUV (r, g, b, y0, u0 , v0);
497 *d++ = y0;
498 *d++ = u0;
499 }
500 }
501 return ret;
502 }
503
504 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
505 {
506 int ret = 0;
507 register int i, j;
508
509 int half = width >> 1;
510
511 uint8_t *Y = yuv420p;
512 uint8_t *U = Y + width * height;
513 uint8_t *V = U + width * height / 4;
514
515 register uint8_t *d = yuv;
516
517 for ( i = 0; i < height; i++ )
518 {
519 register uint8_t *u = U + ( i / 2 ) * ( half );
520 register uint8_t *v = V + ( i / 2 ) * ( half );
521
522 for ( j = 0; j < half; j++ )
523 {
524 *d ++ = *Y ++;
525 *d ++ = *u ++;
526 *d ++ = *Y ++;
527 *d ++ = *v ++;
528 }
529 }
530 return ret;
531 }
532
533 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
534 {
535 // Calculate strides
536 int istride = iwidth * 2;
537 int ostride = owidth * 2;
538
539 iwidth = iwidth - ( iwidth % 4 );
540 owidth = owidth - ( owidth % 4 );
541 iheight = iheight - ( iheight % 2 );
542 oheight = oheight - ( oheight % 2 );
543
544 // Optimisation point
545 if ( iwidth == owidth && iheight == oheight )
546 memcpy( output, input, iheight * istride );
547
548 // Coordinates (0,0 is middle of output)
549 int y;
550
551 // Calculate ranges
552 int out_x_range = owidth / 2;
553 int out_y_range = oheight / 2;
554 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
555 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
556
557 // Output pointers
558 uint8_t *out_line = output;
559 uint8_t *out_ptr = out_line;
560
561 // Calculate a middle and possibly invalid pointer in the input
562 uint8_t *in_middle = input + istride * ( iheight / 2 ) + ( iwidth / 2 ) * 2;
563 int in_line = - in_y_range * istride - in_x_range * 2;
564
565 int elements;
566
567 // Fill whole section with black
568 y = out_y_range - ( iheight / 2 );
569 int blank_elements = ostride * y / 2;
570 elements = blank_elements;
571 while ( elements -- )
572 {
573 *out_line ++ = 16;
574 *out_line ++ = 128;
575 }
576
577 int active_width = 2 * iwidth;
578 int inactive_width = out_x_range - in_x_range;
579 uint8_t *p = NULL;
580 uint8_t *end = NULL;
581
582 // Loop for the entirety of our output height.
583 while ( iheight -- )
584 {
585 // Start at the beginning of the line
586 out_ptr = out_line;
587
588 // Fill the outer part with black
589 elements = inactive_width;
590 while ( elements -- )
591 {
592 *out_ptr ++ = 16;
593 *out_ptr ++ = 128;
594 }
595
596 // We're in the input range for this row.
597 p = in_middle + in_line;
598 end = out_ptr + active_width;
599 while ( out_ptr != end )
600 {
601 *out_ptr ++ = *p ++;
602 *out_ptr ++ = *p ++;
603 }
604
605 // Fill the outer part with black
606 elements = inactive_width;
607 while ( elements -- )
608 {
609 *out_ptr ++ = 16;
610 *out_ptr ++ = 128;
611 }
612
613 // Move to next input line
614 in_line += istride;
615
616 // Move to next output line
617 out_line += ostride;
618 }
619
620 // Fill whole section with black
621 elements = blank_elements;
622 while ( elements -- )
623 {
624 *out_line ++ = 16;
625 *out_line ++ = 128;
626 }
627 }
628
629 /** A resizing function for yuv422 frames - this does not rescale, but simply
630 resizes. It assumes yuv422 images available on the frame so use with care.
631 */
632
633 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
634 {
635 // Get properties
636 mlt_properties properties = mlt_frame_properties( this );
637
638 // Get the input image, width and height
639 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
640 int iwidth = mlt_properties_get_int( properties, "width" );
641 int iheight = mlt_properties_get_int( properties, "height" );
642
643 // If width and height are correct, don't do anything
644 if ( iwidth != owidth || iheight != oheight )
645 {
646 // Create the output image
647 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
648
649 // Call the generic resize
650 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
651
652 // Now update the frame
653 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
654 mlt_properties_set_int( properties, "width", owidth );
655 mlt_properties_set_int( properties, "height", oheight );
656
657 // Return the output
658 return output;
659 }
660 // No change, return input
661 return input;
662 }
663
664 /** A rescaling function for yuv422 frames - low quality, and provided for testing
665 only. It assumes yuv422 images available on the frame so use with care.
666 */
667
668 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
669 {
670 // Get properties
671 mlt_properties properties = mlt_frame_properties( this );
672
673 // Get the input image, width and height
674 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
675 int iwidth = mlt_properties_get_int( properties, "width" );
676 int iheight = mlt_properties_get_int( properties, "height" );
677
678 // If width and height are correct, don't do anything
679 if ( iwidth != owidth || iheight != oheight )
680 {
681 // Create the output image
682 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
683
684 // Calculate strides
685 int istride = iwidth * 2;
686 int ostride = owidth * 2;
687
688 iwidth = iwidth - ( iwidth % 4 );
689
690 // Derived coordinates
691 int dy, dx;
692
693 // Calculate ranges
694 int out_x_range = owidth / 2;
695 int out_y_range = oheight / 2;
696 int in_x_range = iwidth / 2;
697 int in_y_range = iheight / 2;
698
699 // Output pointers
700 register uint8_t *out_line = output;
701 register uint8_t *out_ptr;
702
703 // Calculate a middle pointer
704 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
705 uint8_t *in_line;
706
707 // Generate the affine transform scaling values
708 register int scale_width = ( iwidth << 16 ) / owidth;
709 register int scale_height = ( iheight << 16 ) / oheight;
710 register int base = 0;
711
712 int outer = out_x_range * scale_width;
713 int bottom = out_y_range * scale_height;
714
715 // Loop for the entirety of our output height.
716 for ( dy = - bottom; dy < bottom; dy += scale_height )
717 {
718 // Start at the beginning of the line
719 out_ptr = out_line;
720
721 // Pointer to the middle of the input line
722 in_line = in_middle + ( dy >> 16 ) * istride;
723
724 // Loop for the entirety of our output row.
725 for ( dx = - outer; dx < outer; dx += scale_width )
726 {
727 base = dx >> 15;
728 base &= 0xfffffffe;
729 *out_ptr ++ = *( in_line + base );
730 base &= 0xfffffffc;
731 *out_ptr ++ = *( in_line + base + 1 );
732 dx += scale_width;
733 base = dx >> 15;
734 base &= 0xfffffffe;
735 *out_ptr ++ = *( in_line + base );
736 base &= 0xfffffffc;
737 *out_ptr ++ = *( in_line + base + 3 );
738 }
739
740 // Move to next output line
741 out_line += ostride;
742 }
743
744 // Now update the frame
745 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
746 mlt_properties_set_int( properties, "width", owidth );
747 mlt_properties_set_int( properties, "height", oheight );
748
749 // Return the output
750 return output;
751 }
752
753 // No change, return input
754 return input;
755 }
756
757 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 )
758 {
759 int ret = 0;
760 int16_t *src, *dest;
761 int frequency_src = *frequency, frequency_dest = *frequency;
762 int channels_src = *channels, channels_dest = *channels;
763 int samples_src = *samples, samples_dest = *samples;
764 int i, j;
765 double d = 0, s = 0;
766
767 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
768 //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" ) );
769 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
770 //fprintf( stderr, "mix: frame src samples %d channels %d\n", samples_src, channels_src );
771
772 if ( channels_src > 6 )
773 channels_src = 0;
774 if ( channels_dest > 6 )
775 channels_dest = 0;
776 if ( samples_src > 4000 )
777 samples_src = 0;
778 if ( samples_dest > 4000 )
779 samples_dest = 0;
780
781 // determine number of samples to process
782 *samples = samples_src < samples_dest ? samples_src : samples_dest;
783 *channels = channels_src < channels_dest ? channels_src : channels_dest;
784 *buffer = dest;
785 *frequency = frequency_dest;
786
787 // Compute a smooth ramp over start to end
788 float weight = weight_start;
789 float weight_step = ( weight_end - weight_start ) / *samples;
790
791 // Mixdown
792 for ( i = 0; i < *samples; i++ )
793 {
794 for ( j = 0; j < *channels; j++ )
795 {
796 if ( j < channels_dest )
797 d = (double) dest[ i * channels_dest + j ];
798 if ( j < channels_src )
799 s = (double) src[ i * channels_src + j ];
800 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
801 }
802 weight += weight_step;
803 }
804
805 return ret;
806 }
807
808 int mlt_sample_calculator( float fps, int frequency, int64_t position )
809 {
810 int samples = 0;
811
812 if ( ( int )( fps * 100 ) == 2997 )
813 {
814 samples = frequency / 30;
815
816 switch ( frequency )
817 {
818 case 48000:
819 if ( position % 5 != 0 )
820 samples += 2;
821 break;
822 case 44100:
823 if ( position % 300 == 0 )
824 samples = 1471;
825 else if ( position % 30 == 0 )
826 samples = 1470;
827 else if ( position % 2 == 0 )
828 samples = 1472;
829 else
830 samples = 1471;
831 break;
832 case 32000:
833 if ( position % 30 == 0 )
834 samples = 1068;
835 else if ( position % 29 == 0 )
836 samples = 1067;
837 else if ( position % 4 == 2 )
838 samples = 1067;
839 else
840 samples = 1068;
841 break;
842 default:
843 samples = 0;
844 }
845 }
846 else if ( fps != 0 )
847 {
848 samples = frequency / fps;
849 }
850
851 return samples;
852 }