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