1 | /* |
2 | Copyright (c) 2012, Broadcom Europe Ltd |
3 | All rights reserved. |
4 | |
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the following conditions are met: |
7 | * Redistributions of source code must retain the above copyright |
8 | notice, this list of conditions and the following disclaimer. |
9 | * Redistributions in binary form must reproduce the above copyright |
10 | notice, this list of conditions and the following disclaimer in the |
11 | documentation and/or other materials provided with the distribution. |
12 | * Neither the name of the copyright holder nor the |
13 | names of its contributors may be used to endorse or promote products |
14 | derived from this software without specific prior written permission. |
15 | |
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | */ |
27 | #ifndef VC_CONTAINERS_TIME_H |
28 | #define VC_CONTAINERS_TIME_H |
29 | |
30 | /** \file |
31 | * Utility functions to help with timestamping of elementary stream frames |
32 | */ |
33 | |
34 | typedef struct VC_CONTAINER_TIME_T |
35 | { |
36 | uint32_t samplerate_num; |
37 | uint32_t samplerate_den; |
38 | uint32_t time_base; |
39 | |
40 | uint32_t remainder; |
41 | |
42 | int64_t time; |
43 | |
44 | } VC_CONTAINER_TIME_T; |
45 | |
46 | /*****************************************************************************/ |
47 | STATIC_INLINE void vc_container_time_init( VC_CONTAINER_TIME_T *time, uint32_t time_base ) |
48 | { |
49 | time->samplerate_num = 0; |
50 | time->samplerate_den = 0; |
51 | time->remainder = 0; |
52 | time->time_base = time_base; |
53 | time->time = VC_CONTAINER_TIME_UNKNOWN; |
54 | } |
55 | |
56 | /*****************************************************************************/ |
57 | STATIC_INLINE int64_t vc_container_time_get( VC_CONTAINER_TIME_T *time ) |
58 | { |
59 | if (time->time == VC_CONTAINER_TIME_UNKNOWN || !time->samplerate_num || !time->samplerate_den) |
60 | return VC_CONTAINER_TIME_UNKNOWN; |
61 | return time->time + time->remainder * (int64_t)time->time_base * time->samplerate_den / time->samplerate_num; |
62 | } |
63 | |
64 | /*****************************************************************************/ |
65 | STATIC_INLINE void vc_container_time_set_samplerate( VC_CONTAINER_TIME_T *time, uint32_t samplerate_num, uint32_t samplerate_den ) |
66 | { |
67 | if(time->samplerate_num == samplerate_num && |
68 | time->samplerate_den == samplerate_den) |
69 | return; |
70 | |
71 | /* We're changing samplerate, we need to reset our remainder */ |
72 | if(time->remainder) |
73 | time->time = vc_container_time_get( time ); |
74 | time->remainder = 0; |
75 | time->samplerate_num = samplerate_num; |
76 | time->samplerate_den = samplerate_den; |
77 | } |
78 | |
79 | /*****************************************************************************/ |
80 | STATIC_INLINE void vc_container_time_set( VC_CONTAINER_TIME_T *time, int64_t new_time ) |
81 | { |
82 | if (new_time == VC_CONTAINER_TIME_UNKNOWN) |
83 | return; |
84 | time->remainder = 0; |
85 | time->time = new_time; |
86 | } |
87 | |
88 | /*****************************************************************************/ |
89 | STATIC_INLINE int64_t vc_container_time_add( VC_CONTAINER_TIME_T *time, uint32_t samples ) |
90 | { |
91 | uint32_t increment; |
92 | |
93 | if (time->time == VC_CONTAINER_TIME_UNKNOWN || !time->samplerate_num || !time->samplerate_den) |
94 | return VC_CONTAINER_TIME_UNKNOWN; |
95 | |
96 | samples += time->remainder; |
97 | increment = samples * time->samplerate_den / time->samplerate_num; |
98 | time->time += increment * time->time_base; |
99 | time->remainder = samples - increment * time->samplerate_num / time->samplerate_den; |
100 | return vc_container_time_get(time); |
101 | } |
102 | |
103 | #endif /* VC_CONTAINERS_TIME_H */ |
104 | |