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