57c53b80eebcc964a66b4c51748584658fd641bc
[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 this->stack_service = mlt_deque_init( );
74 }
75
76 return this;
77 }
78
79 /** Fetch the frames properties.
80 */
81
82 mlt_properties mlt_frame_properties( mlt_frame this )
83 {
84 return this != NULL ? &this->parent : NULL;
85 }
86
87 /** Check if we have a way to derive something other than a test card.
88 */
89
90 int mlt_frame_is_test_card( mlt_frame this )
91 {
92 return mlt_deque_count( this->stack_image ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_image" );
93 }
94
95 /** Check if we have a way to derive something other than test audio.
96 */
97
98 int mlt_frame_is_test_audio( mlt_frame this )
99 {
100 return mlt_deque_count( this->stack_audio ) == 0 || mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "test_audio" );
101 }
102
103 /** Get the aspect ratio of the frame.
104 */
105
106 double mlt_frame_get_aspect_ratio( mlt_frame this )
107 {
108 return mlt_properties_get_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio" );
109 }
110
111 /** Set the aspect ratio of the frame.
112 */
113
114 int mlt_frame_set_aspect_ratio( mlt_frame this, double value )
115 {
116 return mlt_properties_set_double( MLT_FRAME_PROPERTIES( this ), "aspect_ratio", value );
117 }
118
119 /** Get the position of this frame.
120 */
121
122 mlt_position mlt_frame_get_position( mlt_frame this )
123 {
124 int pos = mlt_properties_get_position( MLT_FRAME_PROPERTIES( this ), "_position" );
125 return pos < 0 ? 0 : pos;
126 }
127
128 /** Set the position of this frame.
129 */
130
131 int mlt_frame_set_position( mlt_frame this, mlt_position value )
132 {
133 return mlt_properties_set_position( MLT_FRAME_PROPERTIES( this ), "_position", value );
134 }
135
136 /** Stack a get_image callback.
137 */
138
139 int mlt_frame_push_get_image( mlt_frame this, mlt_get_image get_image )
140 {
141 return mlt_deque_push_back( this->stack_image, get_image );
142 }
143
144 /** Pop a get_image callback.
145 */
146
147 mlt_get_image mlt_frame_pop_get_image( mlt_frame this )
148 {
149 return mlt_deque_pop_back( this->stack_image );
150 }
151
152 /** Push a frame.
153 */
154
155 int mlt_frame_push_frame( mlt_frame this, mlt_frame that )
156 {
157 return mlt_deque_push_back( this->stack_image, that );
158 }
159
160 /** Pop a frame.
161 */
162
163 mlt_frame mlt_frame_pop_frame( mlt_frame this )
164 {
165 return mlt_deque_pop_back( this->stack_image );
166 }
167
168 /** Push a service.
169 */
170
171 int mlt_frame_push_service( mlt_frame this, void *that )
172 {
173 return mlt_deque_push_back( this->stack_image, that );
174 }
175
176 /** Pop a service.
177 */
178
179 void *mlt_frame_pop_service( mlt_frame this )
180 {
181 return mlt_deque_pop_back( this->stack_image );
182 }
183
184 /** Push a service.
185 */
186
187 int mlt_frame_push_service_int( mlt_frame this, int that )
188 {
189 return mlt_deque_push_back_int( this->stack_image, that );
190 }
191
192 /** Pop a service.
193 */
194
195 int mlt_frame_pop_service_int( mlt_frame this )
196 {
197 return mlt_deque_pop_back_int( this->stack_image );
198 }
199
200 /** Push an audio item on the stack.
201 */
202
203 int mlt_frame_push_audio( mlt_frame this, void *that )
204 {
205 return mlt_deque_push_back( this->stack_audio, that );
206 }
207
208 /** Pop an audio item from the stack
209 */
210
211 void *mlt_frame_pop_audio( mlt_frame this )
212 {
213 return mlt_deque_pop_back( this->stack_audio );
214 }
215
216 /** Return the service stack
217 */
218
219 mlt_deque mlt_frame_service_stack( mlt_frame this )
220 {
221 return this->stack_service;
222 }
223
224 /** Replace image stack with the information provided.
225
226 This might prove to be unreliable and restrictive - the idea is that a transition
227 which normally uses two images may decide to only use the b frame (ie: in the case
228 of a composite where the b frame completely obscures the a frame).
229
230 The image must be writable and the destructor for the image itself must be taken
231 care of on another frame and that frame cannot have a replace applied to it...
232 Further it assumes that no alpha mask is in use.
233
234 For these reasons, it can only be used in a specific situation - when you have
235 multiple tracks each with their own transition and these transitions are applied
236 in a strictly reversed order (ie: highest numbered [lowest track] is processed
237 first).
238
239 More reliable approach - the cases should be detected during the process phase
240 and the upper tracks should simply not be invited to stack...
241 */
242
243 void mlt_frame_replace_image( mlt_frame this, uint8_t *image, mlt_image_format format, int width, int height )
244 {
245 // Remove all items from the stack
246 while( mlt_deque_pop_back( this->stack_image ) ) ;
247
248 // Update the information
249 mlt_properties_set_data( MLT_FRAME_PROPERTIES( this ), "image", image, 0, NULL, NULL );
250 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "width", width );
251 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "height", height );
252 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "format", format );
253 this->get_alpha_mask = NULL;
254 }
255
256 /** Get the image associated to the frame.
257 */
258
259 int mlt_frame_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
260 {
261 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
262 mlt_get_image get_image = mlt_frame_pop_get_image( this );
263 mlt_producer producer = mlt_properties_get_data( properties, "test_card_producer", NULL );
264 int error = 0;
265
266 *width = *width >> 1 << 1;
267
268 if ( get_image != NULL )
269 {
270 mlt_properties_set_int( properties, "image_count", mlt_properties_get_int( properties, "image_count" ) - 1 );
271 mlt_position position = mlt_frame_get_position( this );
272 error = get_image( this, buffer, format, width, height, writable );
273 mlt_properties_set_int( properties, "width", *width );
274 mlt_properties_set_int( properties, "height", *height );
275 mlt_properties_set_int( properties, "format", *format );
276 mlt_frame_set_position( this, position );
277 }
278 else if ( mlt_properties_get_data( properties, "image", NULL ) != NULL )
279 {
280 *format = mlt_properties_get_int( properties, "format" );
281 *buffer = mlt_properties_get_data( properties, "image", NULL );
282 *width = mlt_properties_get_int( properties, "width" );
283 *height = mlt_properties_get_int( properties, "height" );
284 }
285 else if ( producer != NULL )
286 {
287 mlt_frame test_frame = NULL;
288 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &test_frame, 0 );
289 if ( test_frame != NULL )
290 {
291 mlt_properties test_properties = MLT_FRAME_PROPERTIES( test_frame );
292 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( properties, "consumer_aspect_ratio" ) );
293 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( properties, "rescale.interp" ) );
294 mlt_frame_get_image( test_frame, buffer, format, width, height, writable );
295 mlt_properties_set_data( properties, "test_card_frame", test_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
296 mlt_properties_set_data( properties, "image", *buffer, *width * *height * 2, NULL, NULL );
297 mlt_properties_set_int( properties, "width", *width );
298 mlt_properties_set_int( properties, "height", *height );
299 mlt_properties_set_int( properties, "format", *format );
300 mlt_properties_set_double( properties, "aspect_ratio", mlt_frame_get_aspect_ratio( test_frame ) );
301 }
302 else
303 {
304 mlt_properties_set_data( properties, "test_card_producer", NULL, 0, NULL, NULL );
305 mlt_frame_get_image( this, buffer, format, width, height, writable );
306 }
307 }
308 else
309 {
310 register uint8_t *p;
311 register uint8_t *q;
312 int size = 0;
313
314 *width = *width == 0 ? 720 : *width;
315 *height = *height == 0 ? 576 : *height;
316 size = *width * *height;
317
318 mlt_properties_set_int( properties, "format", *format );
319 mlt_properties_set_int( properties, "width", *width );
320 mlt_properties_set_int( properties, "height", *height );
321 mlt_properties_set_int( properties, "aspect_ratio", 0 );
322
323 switch( *format )
324 {
325 case mlt_image_none:
326 size = 0;
327 *buffer = NULL;
328 break;
329 case mlt_image_rgb24:
330 size *= 3;
331 size += *width * 3;
332 *buffer = mlt_pool_alloc( size );
333 if ( *buffer )
334 memset( *buffer, 255, size );
335 break;
336 case mlt_image_rgb24a:
337 case mlt_image_opengl:
338 size *= 4;
339 size += *width * 4;
340 *buffer = mlt_pool_alloc( size );
341 if ( *buffer )
342 memset( *buffer, 255, size );
343 break;
344 case mlt_image_yuv422:
345 size *= 2;
346 size += *width * 2;
347 *buffer = mlt_pool_alloc( size );
348 p = *buffer;
349 q = p + size;
350 while ( p != NULL && p != q )
351 {
352 *p ++ = 235;
353 *p ++ = 128;
354 }
355 break;
356 case mlt_image_yuv420p:
357 size = size * 3 / 2;
358 *buffer = mlt_pool_alloc( size );
359 if ( *buffer )
360 memset( *buffer, 255, size );
361 break;
362 }
363
364 mlt_properties_set_data( properties, "image", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
365 mlt_properties_set_int( properties, "test_image", 1 );
366 }
367
368 mlt_properties_set_int( properties, "scaled_width", *width );
369 mlt_properties_set_int( properties, "scaled_height", *height );
370
371 return error;
372 }
373
374 uint8_t *mlt_frame_get_alpha_mask( mlt_frame this )
375 {
376 uint8_t *alpha = NULL;
377 if ( this != NULL )
378 {
379 if ( this->get_alpha_mask != NULL )
380 alpha = this->get_alpha_mask( this );
381 if ( alpha == NULL )
382 alpha = mlt_properties_get_data( &this->parent, "alpha", NULL );
383 if ( alpha == NULL )
384 {
385 int size = mlt_properties_get_int( &this->parent, "scaled_width" ) * mlt_properties_get_int( &this->parent, "scaled_height" );
386 alpha = mlt_pool_alloc( size );
387 memset( alpha, 255, size );
388 mlt_properties_set_data( &this->parent, "alpha", alpha, size, mlt_pool_release, NULL );
389 }
390 }
391 return alpha;
392 }
393
394 int mlt_frame_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
395 {
396 mlt_get_audio get_audio = mlt_frame_pop_audio( this );
397 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
398 int hide = mlt_properties_get_int( properties, "test_audio" );
399
400 if ( hide == 0 && get_audio != NULL )
401 {
402 mlt_position position = mlt_frame_get_position( this );
403 get_audio( this, buffer, format, frequency, channels, samples );
404 mlt_frame_set_position( this, position );
405 }
406 else if ( mlt_properties_get_data( properties, "audio", NULL ) )
407 {
408 *buffer = mlt_properties_get_data( properties, "audio", NULL );
409 *frequency = mlt_properties_get_int( properties, "audio_frequency" );
410 *channels = mlt_properties_get_int( properties, "audio_channels" );
411 *samples = mlt_properties_get_int( properties, "audio_samples" );
412 }
413 else
414 {
415 int size = 0;
416 *samples = *samples <= 0 ? 1920 : *samples;
417 *channels = *channels <= 0 ? 2 : *channels;
418 *frequency = *frequency <= 0 ? 48000 : *frequency;
419 size = *samples * *channels * sizeof( int16_t );
420 *buffer = mlt_pool_alloc( size );
421 if ( *buffer != NULL )
422 memset( *buffer, 0, size );
423 mlt_properties_set_data( properties, "audio", *buffer, size, ( mlt_destructor )mlt_pool_release, NULL );
424 mlt_properties_set_int( properties, "test_audio", 1 );
425 }
426
427 mlt_properties_set_int( properties, "audio_frequency", *frequency );
428 mlt_properties_set_int( properties, "audio_channels", *channels );
429 mlt_properties_set_int( properties, "audio_samples", *samples );
430
431 if ( mlt_properties_get( properties, "meta.volume" ) )
432 {
433 double value = mlt_properties_get_double( properties, "meta.volume" );
434 if ( value == 0.0 )
435 {
436 memset( *buffer, 0, *samples * *channels * 2 );
437 mlt_properties_set_double( properties, "meta.volume", 1.0 );
438 }
439 else if ( value != 1.0 )
440 {
441 int total = *samples * *channels;
442 int16_t *p = *buffer;
443 while ( total -- )
444 {
445 *p = *p * value;
446 p ++;
447 }
448 mlt_properties_set_double( properties, "meta.volume", 1.0 );
449 }
450 }
451
452 return 0;
453 }
454
455 unsigned char *mlt_frame_get_waveform( mlt_frame this, int w, int h )
456 {
457 int16_t *pcm = NULL;
458 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
459 mlt_audio_format format = mlt_audio_pcm;
460 int frequency = 32000; // lower frequency available?
461 int channels = 2;
462 double fps = mlt_properties_get_double( properties, "fps" );
463 int samples = mlt_sample_calculator( fps, frequency, mlt_frame_get_position( this ) );
464
465 // Get the pcm data
466 mlt_frame_get_audio( this, &pcm, &format, &frequency, &channels, &samples );
467
468 // Make an 8-bit buffer large enough to hold rendering
469 int size = w * h;
470 unsigned char *bitmap = ( unsigned char* )mlt_pool_alloc( size );
471 if ( bitmap != NULL )
472 memset( bitmap, 0, size );
473 mlt_properties_set_data( properties, "waveform", bitmap, size, ( mlt_destructor )mlt_pool_release, NULL );
474
475 // Render vertical lines
476 int16_t *ubound = pcm + samples * channels;
477 int skip = samples / w - 1;
478 int i, j, k;
479
480 // Iterate sample stream and along x coordinate
481 for ( i = 0; i < w && pcm < ubound; i++ )
482 {
483 // pcm data has channels interleaved
484 for ( j = 0; j < channels; j++ )
485 {
486 // Determine sample's magnitude from 2s complement;
487 int pcm_magnitude = *pcm < 0 ? ~(*pcm) + 1 : *pcm;
488 // The height of a line is the ratio of the magnitude multiplied by
489 // half the vertical resolution
490 int height = ( int )( ( double )( pcm_magnitude ) / 32768 * h / 2 );
491 // Determine the starting y coordinate - left channel above center,
492 // right channel below - currently assumes 2 channels
493 int displacement = ( h / 2 ) - ( 1 - j ) * height;
494 // Position buffer pointer using y coordinate, stride, and x coordinate
495 unsigned char *p = &bitmap[ i + displacement * w ];
496
497 // Draw vertical line
498 for ( k = 0; k < height; k++ )
499 p[ w * k ] = 0xFF;
500
501 pcm++;
502 }
503 pcm += skip * channels;
504 }
505
506 return bitmap;
507 }
508
509 mlt_producer mlt_frame_get_original_producer( mlt_frame this )
510 {
511 if ( this != NULL )
512 return mlt_properties_get_data( MLT_FRAME_PROPERTIES( this ), "_producer", NULL );
513 return NULL;
514 }
515
516 void mlt_frame_close( mlt_frame this )
517 {
518 if ( this != NULL && mlt_properties_dec_ref( MLT_FRAME_PROPERTIES( this ) ) <= 0 )
519 {
520 mlt_deque_close( this->stack_image );
521 mlt_deque_close( this->stack_audio );
522 while( mlt_deque_peek_back( this->stack_service ) )
523 mlt_service_close( mlt_deque_pop_back( this->stack_service ) );
524 mlt_deque_close( this->stack_service );
525 mlt_properties_close( &this->parent );
526 free( this );
527 }
528 }
529
530 /***** convenience functions *****/
531
532 int mlt_convert_rgb24a_to_yuv422( uint8_t *rgba, int width, int height, int stride, uint8_t *yuv, uint8_t *alpha )
533 {
534 int ret = 0;
535 register int y0, y1, u0, u1, v0, v1;
536 register int r, g, b;
537 register uint8_t *d = yuv;
538 register int i, j;
539
540 for ( i = 0; i < height; i++ )
541 {
542 register uint8_t *s = rgba + ( stride * i );
543 for ( j = 0; j < ( width / 2 ); j++ )
544 {
545 r = *s++;
546 g = *s++;
547 b = *s++;
548 *alpha++ = *s++;
549 RGB2YUV (r, g, b, y0, u0 , v0);
550 r = *s++;
551 g = *s++;
552 b = *s++;
553 *alpha++ = *s++;
554 RGB2YUV (r, g, b, y1, u1 , v1);
555 *d++ = y0;
556 *d++ = (u0+u1) >> 1;
557 *d++ = y1;
558 *d++ = (v0+v1) >> 1;
559 }
560 if ( width % 2 )
561 {
562 r = *s++;
563 g = *s++;
564 b = *s++;
565 *alpha++ = *s++;
566 RGB2YUV (r, g, b, y0, u0 , v0);
567 *d++ = y0;
568 *d++ = u0;
569 }
570 }
571 return ret;
572 }
573
574 int mlt_convert_rgb24_to_yuv422( uint8_t *rgb, int width, int height, int stride, uint8_t *yuv )
575 {
576 int ret = 0;
577 register int y0, y1, u0, u1, v0, v1;
578 register int r, g, b;
579 register uint8_t *d = yuv;
580 register int i, j;
581
582 for ( i = 0; i < height; i++ )
583 {
584 register uint8_t *s = rgb + ( stride * i );
585 for ( j = 0; j < ( width / 2 ); j++ )
586 {
587 r = *s++;
588 g = *s++;
589 b = *s++;
590 RGB2YUV (r, g, b, y0, u0 , v0);
591 r = *s++;
592 g = *s++;
593 b = *s++;
594 RGB2YUV (r, g, b, y1, u1 , v1);
595 *d++ = y0;
596 *d++ = (u0+u1) >> 1;
597 *d++ = y1;
598 *d++ = (v0+v1) >> 1;
599 }
600 if ( width % 2 )
601 {
602 r = *s++;
603 g = *s++;
604 b = *s++;
605 RGB2YUV (r, g, b, y0, u0 , v0);
606 *d++ = y0;
607 *d++ = u0;
608 }
609 }
610 return ret;
611 }
612
613 int mlt_convert_yuv420p_to_yuv422( uint8_t *yuv420p, int width, int height, int stride, uint8_t *yuv )
614 {
615 int ret = 0;
616 register int i, j;
617
618 int half = width >> 1;
619
620 uint8_t *Y = yuv420p;
621 uint8_t *U = Y + width * height;
622 uint8_t *V = U + width * height / 4;
623
624 register uint8_t *d = yuv;
625
626 for ( i = 0; i < height; i++ )
627 {
628 register uint8_t *u = U + ( i / 2 ) * ( half );
629 register uint8_t *v = V + ( i / 2 ) * ( half );
630
631 for ( j = 0; j < half; j++ )
632 {
633 *d ++ = *Y ++;
634 *d ++ = *u ++;
635 *d ++ = *Y ++;
636 *d ++ = *v ++;
637 }
638 }
639 return ret;
640 }
641
642 uint8_t *mlt_resize_alpha( uint8_t *input, int owidth, int oheight, int iwidth, int iheight )
643 {
644 uint8_t *output = NULL;
645
646 if ( input != NULL && ( iwidth != owidth || iheight != oheight ) && ( owidth > 6 && oheight > 6 ) )
647 {
648 iwidth = iwidth - ( iwidth % 2 );
649 owidth = owidth - ( owidth % 2 );
650
651 output = mlt_pool_alloc( owidth * oheight );
652
653 // Coordinates (0,0 is middle of output)
654 int y;
655
656 // Calculate ranges
657 int out_x_range = owidth / 2;
658 int out_y_range = oheight / 2;
659 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
660 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
661
662 // Output pointers
663 uint8_t *out_line = output;
664 uint8_t *out_ptr = out_line;
665
666 // Calculate a middle and possibly invalid pointer in the input
667 uint8_t *in_middle = input + iwidth * ( iheight / 2 ) + ( iwidth / 2 );
668 int in_line = - in_y_range * iwidth - in_x_range;
669
670 int elements;
671
672 // Fill whole section with black
673 y = out_y_range - ( iheight / 2 );
674 int blank_elements = owidth * y;
675 elements = blank_elements;
676 while ( elements -- )
677 *out_line ++ = 0;
678
679 int active_width = iwidth;
680 int inactive_width = out_x_range - in_x_range;
681 uint8_t *p = NULL;
682 uint8_t *end = NULL;
683
684 // Loop for the entirety of our output height.
685 while ( iheight -- )
686 {
687 // Start at the beginning of the line
688 out_ptr = out_line;
689
690 // Fill the outer part with black
691 elements = inactive_width;
692 while ( elements -- )
693 *out_ptr ++ = 0;
694
695 // We're in the input range for this row.
696 p = in_middle + in_line;
697 end = out_ptr + active_width;
698 while ( out_ptr != end )
699 *out_ptr ++ = *p ++;
700
701 // Fill the outer part with black
702 elements = inactive_width;
703 while ( elements -- )
704 *out_ptr ++ = 0;
705
706 // Move to next input line
707 in_line += iwidth;
708
709 // Move to next output line
710 out_line += owidth;
711 }
712
713 // Fill whole section with black
714 elements = blank_elements;
715 while ( elements -- )
716 *out_line ++ = 0;
717 }
718
719 return output;
720 }
721
722 void mlt_resize_yuv422( uint8_t *output, int owidth, int oheight, uint8_t *input, int iwidth, int iheight )
723 {
724 // Calculate strides
725 int istride = iwidth * 2;
726 int ostride = owidth * 2;
727
728 iwidth = iwidth - ( iwidth % 2 );
729 owidth = owidth - ( owidth % 2 );
730 //iheight = iheight - ( iheight % 2 );
731 //oheight = oheight - ( oheight % 2 );
732
733 // Optimisation point
734 if ( output == NULL || input == NULL || ( owidth <= 6 || oheight <= 6 || iwidth <= 6 || oheight <= 6 ) )
735 {
736 return;
737 }
738 else if ( iwidth == owidth && iheight == oheight )
739 {
740 memcpy( output, input, iheight * istride );
741 return;
742 }
743
744 // Coordinates (0,0 is middle of output)
745 int y;
746
747 // Calculate ranges
748 int out_x_range = owidth / 2;
749 int out_y_range = oheight / 2;
750 int in_x_range = iwidth / 2 < out_x_range ? iwidth / 2 : out_x_range;
751 int in_y_range = iheight / 2 < out_y_range ? iheight / 2 : out_y_range;
752
753 // Output pointers
754 uint8_t *out_line = output;
755 uint8_t *out_ptr = out_line;
756
757 // Calculate a middle and possibly invalid pointer in the input
758 uint8_t *in_middle = input + istride * ( iheight / 2 ) + iwidth;
759 int in_line = - in_y_range * istride - in_x_range * 2;
760
761 int elements;
762
763 // Fill whole section with black
764 y = out_y_range - ( iheight / 2 );
765 int blank_elements = ostride * y / 2;
766 elements = blank_elements;
767 while ( elements -- )
768 {
769 *out_line ++ = 16;
770 *out_line ++ = 128;
771 }
772
773 int active_width = 2 * iwidth;
774 int left_inactive_width = out_x_range - in_x_range;
775 int right_inactive_width = left_inactive_width;
776 uint8_t *p = NULL;
777 uint8_t *end = NULL;
778
779 if ( in_line % 4 )
780 {
781 active_width -= 2;
782 in_middle += 2;
783 right_inactive_width += 2;
784 }
785
786 // Loop for the entirety of our output height.
787 while ( iheight -- )
788 {
789 // Start at the beginning of the line
790 out_ptr = out_line;
791
792 // Fill the outer part with black
793 elements = left_inactive_width;
794 while ( elements -- )
795 {
796 *out_ptr ++ = 16;
797 *out_ptr ++ = 128;
798 }
799
800 // We're in the input range for this row.
801 p = in_middle + in_line;
802 end = out_ptr + active_width;
803 while ( out_ptr != end )
804 {
805 *out_ptr ++ = *p ++;
806 *out_ptr ++ = *p ++;
807 }
808
809 // Fill the outer part with black
810 elements = right_inactive_width;
811 while ( elements -- )
812 {
813 *out_ptr ++ = 16;
814 *out_ptr ++ = 128;
815 }
816
817 // Move to next input line
818 in_line += istride;
819
820 // Move to next output line
821 out_line += ostride;
822 }
823
824 // Fill whole section with black
825 elements = blank_elements;
826 while ( elements -- )
827 {
828 *out_line ++ = 16;
829 *out_line ++ = 128;
830 }
831 }
832
833 /** A resizing function for yuv422 frames - this does not rescale, but simply
834 resizes. It assumes yuv422 images available on the frame so use with care.
835 */
836
837 uint8_t *mlt_frame_resize_yuv422( mlt_frame this, int owidth, int oheight )
838 {
839 // Get properties
840 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
841
842 // Get the input image, width and height
843 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
844 uint8_t *alpha = mlt_frame_get_alpha_mask( this );
845
846 int iwidth = mlt_properties_get_int( properties, "width" );
847 int iheight = mlt_properties_get_int( properties, "height" );
848
849 // If width and height are correct, don't do anything
850 if ( iwidth != owidth || iheight != oheight )
851 {
852 // Create the output image
853 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
854
855 // Call the generic resize
856 mlt_resize_yuv422( output, owidth, oheight, input, iwidth, iheight );
857
858 // Now update the frame
859 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
860 mlt_properties_set_int( properties, "width", owidth );
861 mlt_properties_set_int( properties, "height", oheight );
862
863 // We should resize the alpha too
864 alpha = mlt_resize_alpha( alpha, owidth, oheight, iwidth, iheight );
865 if ( alpha != NULL )
866 {
867 mlt_properties_set_data( properties, "alpha", alpha, owidth * ( oheight + 1 ), ( mlt_destructor )mlt_pool_release, NULL );
868 this->get_alpha_mask = NULL;
869 }
870
871 // Return the output
872 return output;
873 }
874 // No change, return input
875 return input;
876 }
877
878 /** A rescaling function for yuv422 frames - low quality, and provided for testing
879 only. It assumes yuv422 images available on the frame so use with care.
880 */
881
882 uint8_t *mlt_frame_rescale_yuv422( mlt_frame this, int owidth, int oheight )
883 {
884 // Get properties
885 mlt_properties properties = MLT_FRAME_PROPERTIES( this );
886
887 // Get the input image, width and height
888 uint8_t *input = mlt_properties_get_data( properties, "image", NULL );
889 int iwidth = mlt_properties_get_int( properties, "width" );
890 int iheight = mlt_properties_get_int( properties, "height" );
891
892 // If width and height are correct, don't do anything
893 if ( iwidth != owidth || iheight != oheight )
894 {
895 // Create the output image
896 uint8_t *output = mlt_pool_alloc( owidth * ( oheight + 1 ) * 2 );
897
898 // Calculate strides
899 int istride = iwidth * 2;
900 int ostride = owidth * 2;
901
902 iwidth = iwidth - ( iwidth % 4 );
903
904 // Derived coordinates
905 int dy, dx;
906
907 // Calculate ranges
908 int out_x_range = owidth / 2;
909 int out_y_range = oheight / 2;
910 int in_x_range = iwidth / 2;
911 int in_y_range = iheight / 2;
912
913 // Output pointers
914 register uint8_t *out_line = output;
915 register uint8_t *out_ptr;
916
917 // Calculate a middle pointer
918 uint8_t *in_middle = input + istride * in_y_range + in_x_range * 2;
919 uint8_t *in_line;
920
921 // Generate the affine transform scaling values
922 register int scale_width = ( iwidth << 16 ) / owidth;
923 register int scale_height = ( iheight << 16 ) / oheight;
924 register int base = 0;
925
926 int outer = out_x_range * scale_width;
927 int bottom = out_y_range * scale_height;
928
929 // Loop for the entirety of our output height.
930 for ( dy = - bottom; dy < bottom; dy += scale_height )
931 {
932 // Start at the beginning of the line
933 out_ptr = out_line;
934
935 // Pointer to the middle of the input line
936 in_line = in_middle + ( dy >> 16 ) * istride;
937
938 // Loop for the entirety of our output row.
939 for ( dx = - outer; dx < outer; dx += scale_width )
940 {
941 base = dx >> 15;
942 base &= 0xfffffffe;
943 *out_ptr ++ = *( in_line + base );
944 base &= 0xfffffffc;
945 *out_ptr ++ = *( in_line + base + 1 );
946 dx += scale_width;
947 base = dx >> 15;
948 base &= 0xfffffffe;
949 *out_ptr ++ = *( in_line + base );
950 base &= 0xfffffffc;
951 *out_ptr ++ = *( in_line + base + 3 );
952 }
953
954 // Move to next output line
955 out_line += ostride;
956 }
957
958 // Now update the frame
959 mlt_properties_set_data( properties, "image", output, owidth * ( oheight + 1 ) * 2, ( mlt_destructor )mlt_pool_release, NULL );
960 mlt_properties_set_int( properties, "width", owidth );
961 mlt_properties_set_int( properties, "height", oheight );
962
963 // Return the output
964 return output;
965 }
966
967 // No change, return input
968 return input;
969 }
970
971 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 )
972 {
973 int ret = 0;
974 int16_t *src, *dest;
975 int frequency_src = *frequency, frequency_dest = *frequency;
976 int channels_src = *channels, channels_dest = *channels;
977 int samples_src = *samples, samples_dest = *samples;
978 int i, j;
979 double d = 0, s = 0;
980
981 mlt_frame_get_audio( that, &src, format, &frequency_src, &channels_src, &samples_src );
982 mlt_frame_get_audio( this, &dest, format, &frequency_dest, &channels_dest, &samples_dest );
983
984 int silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( this ), "silent_audio" );
985 mlt_properties_set_int( MLT_FRAME_PROPERTIES( this ), "silent_audio", 0 );
986 if ( silent )
987 memset( dest, 0, samples_dest * channels_dest * sizeof( int16_t ) );
988
989 silent = mlt_properties_get_int( MLT_FRAME_PROPERTIES( that ), "silent_audio" );
990 mlt_properties_set_int( MLT_FRAME_PROPERTIES( that ), "silent_audio", 0 );
991 if ( silent )
992 memset( src, 0, samples_src * channels_src * sizeof( int16_t ) );
993
994 if ( channels_src > 6 )
995 channels_src = 0;
996 if ( channels_dest > 6 )
997 channels_dest = 0;
998 if ( samples_src > 4000 )
999 samples_src = 0;
1000 if ( samples_dest > 4000 )
1001 samples_dest = 0;
1002
1003 // determine number of samples to process
1004 *samples = samples_src < samples_dest ? samples_src : samples_dest;
1005 *channels = channels_src < channels_dest ? channels_src : channels_dest;
1006 *buffer = dest;
1007 *frequency = frequency_dest;
1008
1009 // Compute a smooth ramp over start to end
1010 float weight = weight_start;
1011 float weight_step = ( weight_end - weight_start ) / *samples;
1012
1013 // Mixdown
1014 for ( i = 0; i < *samples; i++ )
1015 {
1016 for ( j = 0; j < *channels; j++ )
1017 {
1018 if ( j < channels_dest )
1019 d = (double) dest[ i * channels_dest + j ];
1020 if ( j < channels_src )
1021 s = (double) src[ i * channels_src + j ];
1022 dest[ i * channels_dest + j ] = s * weight + d * ( 1.0 - weight );
1023 }
1024 weight += weight_step;
1025 }
1026
1027 return ret;
1028 }
1029
1030 /* Will this break when mlt_position is converted to double? -Zach */
1031 int mlt_sample_calculator( float fps, int frequency, int64_t position )
1032 {
1033 int samples = 0;
1034
1035 if ( ( int )( fps * 100 ) == 2997 )
1036 {
1037 samples = frequency / 30;
1038
1039 switch ( frequency )
1040 {
1041 case 48000:
1042 if ( position % 5 != 0 )
1043 samples += 2;
1044 break;
1045 case 44100:
1046 if ( position % 300 == 0 )
1047 samples = 1471;
1048 else if ( position % 30 == 0 )
1049 samples = 1470;
1050 else if ( position % 2 == 0 )
1051 samples = 1472;
1052 else
1053 samples = 1471;
1054 break;
1055 case 32000:
1056 if ( position % 30 == 0 )
1057 samples = 1068;
1058 else if ( position % 29 == 0 )
1059 samples = 1067;
1060 else if ( position % 4 == 2 )
1061 samples = 1067;
1062 else
1063 samples = 1068;
1064 break;
1065 default:
1066 samples = 0;
1067 }
1068 }
1069 else if ( fps != 0 )
1070 {
1071 samples = frequency / fps;
1072 }
1073
1074 return samples;
1075 }
1076
1077 int64_t mlt_sample_calculator_to_now( float fps, int frequency, int64_t frame )
1078 {
1079 int64_t samples = 0;
1080
1081 // TODO: Correct rules for NTSC and drop the * 100 hack
1082 if ( ( int )( fps * 100 ) == 2997 )
1083 {
1084 samples = ( ( double )( frame * frequency ) / 30 );
1085 switch( frequency )
1086 {
1087 case 48000:
1088 samples += 2 * ( frame / 5 );
1089 break;
1090 case 44100:
1091 samples += frame + ( frame / 2 ) - ( frame / 30 ) + ( frame / 300 );
1092 break;
1093 case 32000:
1094 samples += ( 2 * frame ) - ( frame / 4 ) - ( frame / 29 );
1095 break;
1096 }
1097 }
1098 else if ( fps != 0 )
1099 {
1100 samples = ( ( frame * frequency ) / ( int )fps );
1101 }
1102
1103 return samples;
1104 }