1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2010-2014 L-SMASH project
6 * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
8 * Permission to use, copy, modify, and/or distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 *****************************************************************************/
21 /* This file is available under an ISC license. */
23 #include "internal.h" /* must be placed first */
29 #define WIN32_LEAN_AND_MEAN
34 double lsmash_fixed2double( uint64_t value
, int frac_width
)
36 return value
/ (double)(1ULL << frac_width
);
39 float lsmash_int2float32( uint32_t value
)
41 return (union {uint32_t i
; float f
;}){value
}.f
;
44 double lsmash_int2float64( uint64_t value
)
46 return (union {uint64_t i
; double d
;}){value
}.d
;
54 lsmash_log_level level
,
59 /* Dereference lsmash_class_t pointer if 'class' is non-NULL. */
60 lsmash_class_t
*cls
= class ? (lsmash_class_t
*)*(intptr_t *)class : NULL
;
61 if( cls
&& cls
->log_level_offset
)
63 lsmash_log_level log_level
= *(lsmash_log_level
*)((int8_t *)class + cls
->log_level_offset
);
64 if( level
> log_level
)
69 va_start( args
, message
);
72 case LSMASH_LOG_ERROR
:
75 case LSMASH_LOG_WARNING
:
86 fprintf( stderr
, "[%s: %s]: ", cls
->name
, prefix
);
88 fprintf( stderr
, "[%s]: ", prefix
);
89 vfprintf( stderr
, message
, args
);
93 void lsmash_log_refresh_line
95 const void *class /* unused, but for forward compatibility */
98 /* Assume 80 characters per line. */
99 fprintf( stderr
, "%80c", '\r' );
102 uint32_t lsmash_count_bits
107 bits
= (bits
& 0x55555555) + ((bits
>> 1) & 0x55555555);
108 bits
= (bits
& 0x33333333) + ((bits
>> 2) & 0x33333333);
109 bits
= (bits
& 0x0f0f0f0f) + ((bits
>> 4) & 0x0f0f0f0f);
110 bits
= (bits
& 0x00ff00ff) + ((bits
>> 8) & 0x00ff00ff);
111 return (bits
& 0x0000ffff) + ((bits
>> 16) & 0x0000ffff);
118 const char *format
, ...
122 va_start( args
, format
);
125 static const char *indent_string
[] =
139 fprintf( fp
, "%s", indent_string
[indent
] );
142 for( int i
= 0; i
< indent
; i
++ )
144 vfprintf( fp
, format
, args
);
154 while( value
> (1ULL << length
) )
159 /* for qsort function */
160 int lsmash_compare_dts
162 const lsmash_media_ts_t
*a
,
163 const lsmash_media_ts_t
*b
166 int64_t diff
= (int64_t)(a
->dts
- b
->dts
);
167 return diff
> 0 ? 1 : (diff
== 0 ? 0 : -1);
170 int lsmash_compare_cts
172 const lsmash_media_ts_t
*a
,
173 const lsmash_media_ts_t
*b
176 int64_t diff
= (int64_t)(a
->cts
- b
->cts
);
177 return diff
> 0 ? 1 : (diff
== 0 ? 0 : -1);
181 int lsmash_convert_ansi_to_utf8( const char *ansi
, char *utf8
, int length
)
183 int len0
= MultiByteToWideChar( CP_THREAD_ACP
, 0, ansi
, -1, 0, 0 );
184 wchar_t *buff
= lsmash_malloc( len0
* sizeof(wchar_t) );
187 int len1
= MultiByteToWideChar( CP_THREAD_ACP
, 0, ansi
, -1, buff
, len0
);
190 len0
= WideCharToMultiByte( CP_UTF8
, 0, buff
, -1, 0, 0, 0, 0 );
191 if( len0
> length
- 1 )
193 len1
= WideCharToMultiByte( CP_UTF8
, 0, buff
, -1, utf8
, length
, 0, 0 );