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