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