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