1 /**
2  * \file mlt_profile.c
3  * \brief video output definition
4  * \see mlt_profile_s
5  *
6  * Copyright (C) 2007-2018 Meltytech, LLC
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22 
23 #include "mlt.h"
24 
25 #include <stdlib.h>
26 #include <string.h>
27 #include <libgen.h>
28 #include <math.h>
29 
30 
31 /** the default subdirectory of the datadir for holding profiles */
32 #define PROFILES_DIR "/profiles/"
33 
34 /** Load a profile from the system folder.
35  *
36  * The environment variable MLT_PROFILES_PATH overrides the default \p PROFILES_DIR.
37  *
38  * \private \memberof mlt_profile_s
39  * \param name the name of a profile settings file located in the standard location or
40  * the full path name to a profile settings file
41  * \return a profile or NULL on error
42  */
43 
mlt_profile_select(const char * name)44 static mlt_profile mlt_profile_select( const char *name )
45 {
46 	char *filename = NULL;
47 	const char *prefix = getenv( "MLT_PROFILES_PATH" );
48 	mlt_properties properties = mlt_properties_load( name );
49 	mlt_profile profile = NULL;
50 
51 	// Try to load from file specification
52 	if ( properties && mlt_properties_get_int( properties, "width" ) )
53 	{
54 		filename = calloc( 1, strlen( name ) + 1 );
55 	}
56 	// Load from $datadir/mlt/profiles
57 	else if ( !prefix && mlt_environment( "MLT_DATA" ) )
58 	{
59 		prefix = mlt_environment( "MLT_DATA" );
60 		filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + strlen( name ) + 1 );
61 		strcpy( filename, prefix );
62 		strcat( filename, PROFILES_DIR );
63 	}
64 	// Use environment variable instead
65 	else if ( prefix )
66 	{
67 		filename = calloc( 1, strlen( prefix ) + strlen( name ) + 2 );
68 		strcpy( filename, prefix );
69 		if ( filename[ strlen( filename ) - 1 ] != '/' )
70 			filename[ strlen( filename ) ] = '/';
71 	}
72 	else
73 	{
74 		mlt_properties_close( properties );
75 		return profile;
76 	}
77 
78 	// Finish loading
79 	strcat( filename, name );
80 	profile = mlt_profile_load_file( filename );
81 
82 	// Cleanup
83 	mlt_properties_close( properties );
84 	free( filename );
85 
86 	return profile;
87 }
88 
89 /** Construct a profile.
90  *
91  * This will never return NULL as it uses the dv_pal settings as hard-coded fallback default.
92  *
93  * \public \memberof mlt_profile_s
94  * \param name the name of a profile settings file located in the standard location or
95  * the full path name to a profile settings file
96  * \return a profile
97  */
98 
mlt_profile_init(const char * name)99 mlt_profile mlt_profile_init( const char *name )
100 {
101 	mlt_profile profile = NULL;
102 
103 	// Explicit profile by name gets priority over environment variables
104 	if ( name )
105 		profile = mlt_profile_select( name );
106 
107 	// Try to load by environment variable
108 	if ( profile == NULL )
109 	{
110 		// MLT_PROFILE is preferred environment variable
111 		if ( getenv( "MLT_PROFILE" ) )
112 			profile = mlt_profile_select( getenv( "MLT_PROFILE" ) );
113 		// MLT_NORMALISATION backwards compatibility
114 		else if ( getenv( "MLT_NORMALISATION" ) && strcmp( getenv( "MLT_NORMALISATION" ), "PAL" ) )
115 			profile = mlt_profile_select( "dv_ntsc" );
116 		else
117 			profile = mlt_profile_select( "dv_pal" );
118 
119 		// If still not loaded (no profile files), default to PAL
120 		if ( profile == NULL )
121 		{
122 			profile = calloc( 1, sizeof( struct mlt_profile_s ) );
123 			if ( profile )
124 			{
125 				mlt_environment_set( "MLT_PROFILE", "dv_pal" );
126 				profile->description = strdup( "PAL 4:3 DV or DVD" );
127 				profile->frame_rate_num = 25;
128 				profile->frame_rate_den = 1;
129 				profile->width = 720;
130 				profile->height = 576;
131 				profile->progressive = 0;
132 				profile->sample_aspect_num = 16;
133 				profile->sample_aspect_den = 15;
134 				profile->display_aspect_num = 4;
135 				profile->display_aspect_den = 3;
136 				profile->colorspace = 601;
137 			}
138 		}
139 	}
140 	return profile;
141 }
142 
set_mlt_normalisation(const char * profile_name)143 static void set_mlt_normalisation( const char *profile_name )
144 {
145 	if ( profile_name )
146 	{
147 		if ( strstr( profile_name, "_ntsc" ) ||
148 		     strstr( profile_name, "_60" ) ||
149 		     strstr( profile_name, "_5994" ) ||
150 		     strstr( profile_name, "_2997" ) ||
151 		     strstr( profile_name, "_30" ) )
152 		{
153 			mlt_environment_set( "MLT_NORMALISATION", "NTSC" );
154 		}
155 		else if ( strstr( profile_name, "_pal" ) ||
156 		          strstr( profile_name, "_50" ) ||
157 		          strstr( profile_name, "_25" ) )
158 		{
159 			mlt_environment_set( "MLT_NORMALISATION", "PAL" );
160 		}
161 	}
162 }
163 
164 /** Load a profile from specific file.
165  *
166  * \public \memberof mlt_profile_s
167  * \param file the full path name to a properties file
168  * \return a profile or NULL on error
169  */
170 
mlt_profile_load_file(const char * file)171 mlt_profile mlt_profile_load_file( const char *file )
172 {
173 	mlt_profile profile = NULL;
174 
175 	// Load the profile as properties
176 	mlt_properties properties = mlt_properties_load( file );
177 	if ( properties )
178 	{
179 		// Simple check if the profile is valid
180 		if ( mlt_properties_get_int( properties, "width" ) )
181 		{
182 			profile = mlt_profile_load_properties( properties );
183 
184 			// Set MLT_PROFILE to basename
185 			char *filename = strdup( file );
186 			mlt_environment_set( "MLT_PROFILE", basename( filename ) );
187 			set_mlt_normalisation( basename( filename ) );
188 			free( filename );
189 		}
190 		mlt_properties_close( properties );
191 	}
192 
193 	// Set MLT_NORMALISATION to appease legacy modules
194 	char *profile_name = mlt_environment( "MLT_PROFILE" );
195 	set_mlt_normalisation( profile_name );
196 	return profile;
197 }
198 
199 /** Load a profile from a properties object.
200  *
201  * \public \memberof mlt_profile_s
202  * \param properties a properties list
203  * \return a profile or NULL if out of memory
204  */
205 
mlt_profile_load_properties(mlt_properties properties)206 mlt_profile mlt_profile_load_properties( mlt_properties properties )
207 {
208 	mlt_profile profile = calloc( 1, sizeof( struct mlt_profile_s ) );
209 	if ( profile )
210 	{
211 		if ( mlt_properties_get( properties, "name" ) )
212 			mlt_environment_set( "MLT_PROFILE", mlt_properties_get( properties, "name" ) );
213 		if ( mlt_properties_get( properties, "description" ) )
214 			profile->description = strdup( mlt_properties_get( properties, "description" ) );
215 		profile->frame_rate_num = mlt_properties_get_int( properties, "frame_rate_num" );
216 		profile->frame_rate_den = mlt_properties_get_int( properties, "frame_rate_den" );
217 		profile->width = mlt_properties_get_int( properties, "width" );
218 		profile->height = mlt_properties_get_int( properties, "height" );
219 		profile->progressive = mlt_properties_get_int( properties, "progressive" );
220 		profile->sample_aspect_num = mlt_properties_get_int( properties, "sample_aspect_num" );
221 		profile->sample_aspect_den = mlt_properties_get_int( properties, "sample_aspect_den" );
222 		profile->display_aspect_num = mlt_properties_get_int( properties, "display_aspect_num" );
223 		profile->display_aspect_den = mlt_properties_get_int( properties, "display_aspect_den" );
224 		profile->colorspace = mlt_properties_get_int( properties, "colorspace" );
225 	}
226 	return profile;
227 }
228 
229 /** Load an anonymous profile from string.
230  *
231  * \public \memberof mlt_profile_s
232  * \param string a newline-delimited list of properties as name=value pairs
233  * \return a profile or NULL if out of memory
234  */
235 
mlt_profile_load_string(const char * string)236 mlt_profile mlt_profile_load_string( const char *string )
237 {
238 	mlt_properties properties = mlt_properties_new();
239 	mlt_profile profile = NULL;
240 
241 	if ( properties )
242 	{
243 		const char *p = string;
244 		while ( p )
245 		{
246 			if ( strcmp( p, "" ) && p[ 0 ] != '#' )
247 				mlt_properties_parse( properties, p );
248 			p = strchr( p, '\n' );
249 			if ( p ) p++;
250 		}
251 		profile = mlt_profile_load_properties( properties );
252 		mlt_properties_close( properties );
253 	}
254 	return profile;
255 }
256 
257 /** Get the video frame rate as a floating point value.
258  *
259  * \public \memberof mlt_profile_s
260  * @param profile a profile
261  * @return the frame rate
262  */
263 
mlt_profile_fps(mlt_profile profile)264 double mlt_profile_fps( mlt_profile profile )
265 {
266 	if ( profile )
267 		return ( double ) profile->frame_rate_num / profile->frame_rate_den;
268 	else
269 		return 0;
270 }
271 
272 /** Get the sample aspect ratio as a floating point value.
273  *
274  * \public \memberof mlt_profile_s
275  * \param profile a profile
276  * \return the pixel aspect ratio
277  */
278 
mlt_profile_sar(mlt_profile profile)279 double mlt_profile_sar( mlt_profile profile )
280 {
281 	if ( profile )
282 		return ( double ) profile->sample_aspect_num / profile->sample_aspect_den;
283 	else
284 		return 0;
285 }
286 
287 /** Get the display aspect ratio as floating point value.
288  *
289  * \public \memberof mlt_profile_s
290  * \param profile a profile
291  * \return the image aspect ratio
292  */
293 
mlt_profile_dar(mlt_profile profile)294 double mlt_profile_dar( mlt_profile profile )
295 {
296 	if ( profile )
297 		return ( double ) profile->display_aspect_num / profile->display_aspect_den;
298 	else
299 		return 0;
300 }
301 
302 /** Free up the global profile resources.
303  *
304  * \public \memberof mlt_profile_s
305  * \param profile a profile
306  */
307 
mlt_profile_close(mlt_profile profile)308 void mlt_profile_close( mlt_profile profile )
309 {
310 	if ( profile )
311 	{
312 		free( profile->description );
313 		profile->description = NULL;
314 		free( profile );
315 		profile = NULL;
316 	}
317 }
318 
319 /** Make a copy of a profile.
320   *
321   * \public \memberof mlt_profile_s
322   * \param profile the profile to clone
323   * \return a copy of the profile
324   */
325 
mlt_profile_clone(mlt_profile profile)326 mlt_profile mlt_profile_clone( mlt_profile profile )
327 {
328 	mlt_profile clone = NULL;
329 
330 	if ( profile )
331 	{
332 		clone = calloc( 1, sizeof( *profile ) );
333 		if ( clone )
334 		{
335 			memcpy( clone, profile, sizeof( *profile ) );
336 			clone->description = strdup( profile->description );
337 		}
338 	}
339 	return clone;
340 }
341 
342 
343 /** Get the list of profiles.
344  *
345  * The caller MUST close the returned properties object!
346  * Each entry in the list is keyed on its name, and its value is another
347  * properties object that contains the attributes of the profile.
348  * \public \memberof mlt_profile_s
349  * \return a list of profiles
350  */
351 
mlt_profile_list()352 mlt_properties mlt_profile_list( )
353 {
354 	char *filename = NULL;
355 	const char *prefix = getenv( "MLT_PROFILES_PATH" );
356 	mlt_properties properties = mlt_properties_new();
357 	mlt_properties dir = mlt_properties_new();
358 	int sort = 1;
359 	const char *wildcard = NULL;
360 	int i;
361 
362 	// Load from $datadir/mlt/profiles if no env var
363 	if ( prefix == NULL )
364 	{
365 		prefix = mlt_environment( "MLT_DATA" );
366 		filename = calloc( 1, strlen( prefix ) + strlen( PROFILES_DIR ) + 1 );
367 		strcpy( filename, prefix );
368 		strcat( filename, PROFILES_DIR );
369 		prefix = filename;
370 	}
371 
372 	mlt_properties_dir_list( dir, prefix, wildcard, sort );
373 
374 	for ( i = 0; i < mlt_properties_count( dir ); i++ )
375 	{
376 		char *filename = mlt_properties_get_value( dir, i );
377 		char *profile_name = basename( filename );
378 		if ( profile_name[0] != '.' && strcmp( profile_name, "Makefile" ) &&
379 		     profile_name[ strlen( profile_name ) - 1 ] != '~' )
380 		{
381 			mlt_properties profile = mlt_properties_load( filename );
382 			if ( profile )
383 			{
384 				mlt_properties_set_data( properties, profile_name, profile, 0,
385 					(mlt_destructor) mlt_properties_close, NULL );
386 			}
387 		}
388 	}
389 	mlt_properties_close( dir );
390 	free( filename );
391 
392 	return properties;
393 }
394 
395 /** Update the profile using the attributes of a producer.
396  *
397  * Use this to make an "auto-profile." Typically, you need to re-open the producer
398  * after you use this because some producers (e.g. avformat) adjust their framerate
399  * to that of the profile used when you created it.
400  * \public \memberof mlt_profile_s
401  * \param profile the profile to update
402  * \param producer the producer to inspect
403  */
404 
mlt_profile_from_producer(mlt_profile profile,mlt_producer producer)405 void mlt_profile_from_producer( mlt_profile profile, mlt_producer producer )
406 {
407 	mlt_frame fr = NULL;
408 	uint8_t *buffer = NULL;
409 	mlt_image_format fmt = mlt_image_none;
410 	mlt_properties p;
411 	int w = profile->width;
412 	int h = profile->height;
413 
414 	if ( ! mlt_service_get_frame( MLT_PRODUCER_SERVICE(producer), &fr, 0 ) && fr )
415 	{
416 		// Skip scaling and padding since not needed and we request mlt_image_none.
417 		mlt_properties_set( MLT_FRAME_PROPERTIES(fr), "rescale.interp", "none" );
418 
419 		if ( ! mlt_frame_get_image( fr, &buffer, &fmt, &w, &h, 0 ) )
420 		{
421 			// Some source properties are not exposed until after the first get_image call.
422 			mlt_frame_close( fr );
423 			mlt_service_get_frame( MLT_PRODUCER_SERVICE(producer), &fr, 0 );
424 			p = MLT_FRAME_PROPERTIES( fr );
425 //			mlt_properties_dump(p, stderr);
426 			if ( mlt_properties_get_int( p, "meta.media.frame_rate_den" ) &&
427 				 mlt_properties_get_int( p, "meta.media.sample_aspect_den" ) )
428 			{
429 				profile->width = mlt_properties_get_int( p, "meta.media.width" );
430 				profile->height = mlt_properties_get_int( p, "meta.media.height" );
431 				profile->progressive = mlt_properties_get_int( p, "meta.media.progressive" );
432 				if ( 1000 > mlt_properties_get_double( p, "meta.media.frame_rate_num" )
433 				          / mlt_properties_get_double( p, "meta.media.frame_rate_den" ) )
434 				{
435 					profile->frame_rate_num = mlt_properties_get_int( p, "meta.media.frame_rate_num" );
436 					profile->frame_rate_den = mlt_properties_get_int( p, "meta.media.frame_rate_den" );
437 				} else {
438 					profile->frame_rate_num = 60;
439 					profile->frame_rate_den = 1;
440 				}
441 				// AVCHD is mis-reported as double frame rate.
442 				if ( profile->progressive == 0 && (
443 				     profile->frame_rate_num / profile->frame_rate_den == 50 ||
444 				     profile->frame_rate_num / profile->frame_rate_den == 59 ) )
445 					profile->frame_rate_num /= 2;
446 				profile->sample_aspect_num = mlt_properties_get_int( p, "meta.media.sample_aspect_num" );
447 				profile->sample_aspect_den = mlt_properties_get_int( p, "meta.media.sample_aspect_den" );
448 				profile->colorspace = mlt_properties_get_int( p, "meta.media.colorspace" );
449 				int n = profile->display_aspect_num = profile->sample_aspect_num * profile->width;
450 				int m = profile->display_aspect_den = profile->sample_aspect_den * profile->height;
451 				int gcd, remainder;
452 				while (n) {
453 					remainder = m % n;
454 					m = n;
455 					n = remainder;
456 				}
457 				gcd = m;
458 				profile->display_aspect_num /= gcd;
459 				profile->display_aspect_den /= gcd;
460 				free( profile->description );
461 				profile->description = strdup( "automatic" );
462 				profile->is_explicit = 0;
463 			}
464 		}
465 	}
466 	mlt_frame_close( fr );
467 	mlt_producer_seek( producer, 0 );
468 }
469 
470 /** Get the lumas subdirectory to use for the aspect ratio.
471  *
472  * \public \memberof mlt_profile_s
473  * \param profile the profile to update
474  * \return the name of a subdirectory generated by the lumas module
475  */
476 
mlt_profile_lumas_dir(mlt_profile profile)477 char *mlt_profile_lumas_dir( mlt_profile profile )
478 {
479 	if ( profile ) {
480 		if (profile->display_aspect_num == profile->display_aspect_den) {
481 			mlt_environment_set( "MLT_LUMAS_DIR", "square" );
482 		// [-inf, 0.8) => 9:16
483 		} else if ( mlt_profile_dar( profile ) < 0.8 ) {
484 			mlt_environment_set( "MLT_LUMAS_DIR", "9_16" ); // 0.5625
485 		// [0.8, 1.3) => 1:1
486 		} else if ( mlt_profile_dar( profile ) < 1.3 ) {
487 			mlt_environment_set( "MLT_LUMAS_DIR", "square" );
488 		// [1.3, 1.5) => 4:3
489 		} else if ( mlt_profile_dar( profile ) < 1.5 ) {
490 			if (profile->frame_rate_num == 30000 && profile->frame_rate_den == 1001)
491 				mlt_environment_set( "MLT_LUMAS_DIR", "NTSC" ); // 1.5
492 			else
493 				mlt_environment_set( "MLT_LUMAS_DIR", "PAL" );  // 1.25
494 		// [1.5, inf] => 16:9
495 		} else {
496 			mlt_environment_set( "MLT_LUMAS_DIR", "16_9" );
497 		}
498 	} else {
499 		mlt_environment_set("MLT_LUMAS_DIR", "16_9");
500 	}
501 	return mlt_environment( "MLT_LUMAS_DIR" );
502 }
503 
504 /** Get the width scale factor.
505  *
506  * \public \memberof mlt_profile_s
507  * \param profile the profile to reference
508  * \param width the number of pixels the consumer requested
509  * \return the scale factor for the width
510  */
511 
mlt_profile_scale_width(mlt_profile profile,int width)512 double mlt_profile_scale_width(mlt_profile profile, int width)
513 {
514 	return (profile && width && profile->width)? (double) width / profile->width : 1.0;
515 }
516 
517 /** Get the height scale factor.
518  *
519  * \public \memberof mlt_profile_s
520  * \param profile the profile to reference
521  * \param height the number of pixels the consumer requested
522  * \return the scale factor for the height
523  */
524 
mlt_profile_scale_height(mlt_profile profile,int height)525 double mlt_profile_scale_height(mlt_profile profile, int height)
526 {
527 	return (profile && profile->width)? (double) height / profile->height : 1.0;
528 }
529