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