| 1 |
/* Copyright (C) 2008 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
#define KATE_INTERNAL |
| 11 |
#include "kate_internal.h" |
| 12 |
|
| 13 |
#include "kate/kate.h" |
| 14 |
|
| 15 |
/** |
| 16 |
\ingroup granule |
| 17 |
Returns the granule shift for the given stream |
| 18 |
\param ki the kate_info structure describing the granule encoding setup |
| 19 |
\returns >=0 the granule shift |
| 20 |
\returns KATE_E_* error |
| 21 |
*/ |
| 22 |
int kate_granule_shift(const kate_info *ki) |
| 23 |
{ |
| 24 |
if (!ki) return KATE_E_INVALID_PARAMETER; |
| 25 |
return ki->granule_shift; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
\ingroup granule |
| 30 |
Converts a base/offset pair in seconds to a granule position |
| 31 |
\param ki the kate_info structure describing the granule encoding setup |
| 32 |
\param base the base time in seconds to convert |
| 33 |
\param offset the time offset in seconds to convert |
| 34 |
\returns >=0 the granule corresponding to the base/offset pair |
| 35 |
\returns KATE_E_* error |
| 36 |
*/ |
| 37 |
kate_int64_t kate_time_granule(const kate_info *ki,kate_float base,kate_float offset) |
| 38 |
{ |
| 39 |
kate_int64_t gbase,goffset; |
| 40 |
kate_float actual_base,actual_offset; |
| 41 |
|
| 42 |
if (!ki) return KATE_E_INVALID_PARAMETER; |
| 43 |
if (base<0 || offset<0) return KATE_E_INVALID_PARAMETER; |
| 44 |
|
| 45 |
gbase=(kate_int64_t)((base*ki->gps_numerator)/ki->gps_denominator); |
| 46 |
actual_base=gbase*(kate_float)ki->gps_denominator/ki->gps_numerator; |
| 47 |
gbase=(kate_int64_t)((actual_base*ki->gps_numerator)/ki->gps_denominator+(kate_float)0.5); |
| 48 |
actual_offset=offset+(base-actual_base); |
| 49 |
if (actual_offset<0) actual_offset=0; |
| 50 |
goffset=(kate_int64_t)((actual_offset*ki->gps_numerator)/ki->gps_denominator+(kate_float)0.5); |
| 51 |
/* check if the values fit with the granule encoding parameters */ |
| 52 |
if (gbase>=(((kate_int64_t)1)<<(63-ki->granule_shift))-1) return KATE_E_LIMIT; |
| 53 |
if (goffset>=(((kate_int64_t)1)<<ki->granule_shift)-1) return KATE_E_LIMIT; |
| 54 |
return (gbase<<ki->granule_shift)|goffset; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
\ingroup granule |
| 59 |
Converts a granule position to a base+offset time representation in seconds. |
| 60 |
\param ki the kate_info structure describing the granule encoding setup |
| 61 |
\param granulepos the granulepos to convert to a time representation |
| 62 |
\param base a pointer where to store the base part of the time corresponding to the granulepos |
| 63 |
\param offset a pointer where to store the offset part of the time corresponding to the granulepos |
| 64 |
\returns 0 success |
| 65 |
\returns KATE_E_* error |
| 66 |
*/ |
| 67 |
int kate_granule_split_time(const kate_info *ki,kate_int64_t granulepos,kate_float *base,kate_float *offset) |
| 68 |
{ |
| 69 |
kate_int64_t gbase,goffset; |
| 70 |
|
| 71 |
if (!ki || !base || !offset) return KATE_E_INVALID_PARAMETER; |
| 72 |
if (granulepos<0) return KATE_E_INVALID_PARAMETER; |
| 73 |
|
| 74 |
gbase=granulepos>>ki->granule_shift; |
| 75 |
goffset=granulepos-(gbase<<ki->granule_shift); |
| 76 |
*base=gbase*(kate_float)ki->gps_denominator/ki->gps_numerator; |
| 77 |
*offset=goffset*(kate_float)ki->gps_denominator/ki->gps_numerator; |
| 78 |
|
| 79 |
return 0; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
\ingroup granule |
| 84 |
Converts a granule position to a time in seconds. |
| 85 |
\param ki the kate_info structure describing the granule encoding setup |
| 86 |
\param granulepos the granulepos to convert to a time |
| 87 |
\returns >=0 the time corresponding to the granulepos |
| 88 |
\returns KATE_E_* error |
| 89 |
*/ |
| 90 |
kate_float kate_granule_time(const kate_info *ki,kate_int64_t granulepos) |
| 91 |
{ |
| 92 |
kate_float base,offset; |
| 93 |
int ret; |
| 94 |
|
| 95 |
ret=kate_granule_split_time(ki,granulepos,&base,&offset); |
| 96 |
if (ret<0) return (kate_float)ret; |
| 97 |
return base+offset; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
\ingroup granule |
| 102 |
Converts a duration to a granule duration |
| 103 |
\param ki the kate_info structure describing the granule encoding setup |
| 104 |
\param duration the duration in seconds to convert to a granulepos offset |
| 105 |
\returns >=0 the time corresponding to the granulepos |
| 106 |
\returns KATE_E_* error |
| 107 |
*/ |
| 108 |
kate_int64_t kate_duration_granule(const kate_info *ki,kate_float duration) |
| 109 |
{ |
| 110 |
kate_int64_t granule; |
| 111 |
|
| 112 |
if (!ki) return KATE_E_INVALID_PARAMETER; |
| 113 |
if (duration<0) return KATE_E_INVALID_PARAMETER; |
| 114 |
|
| 115 |
granule=(kate_int64_t)((duration*ki->gps_numerator)/ki->gps_denominator+(kate_float)0.5); |
| 116 |
if (granule<0) return KATE_E_BAD_GRANULE; |
| 117 |
return granule; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
\ingroup granule |
| 122 |
Converts a granule offset position to a time in seconds. |
| 123 |
\param ki the kate_info structure describing the granule encoding setup |
| 124 |
\param duration the granulepos offset to convert to a time |
| 125 |
\returns >=0 the time corresponding to the granulepos offset |
| 126 |
\returns KATE_E_* error |
| 127 |
*/ |
| 128 |
kate_float kate_granule_duration(const kate_info *ki,kate_int64_t duration) |
| 129 |
{ |
| 130 |
if (!ki) return KATE_E_INVALID_PARAMETER; |
| 131 |
if (duration<0) return KATE_E_INVALID_PARAMETER; |
| 132 |
|
| 133 |
return duration*(kate_float)ki->gps_denominator/ki->gps_numerator; |
| 134 |
} |
| 135 |
|