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