1 /*****************************************************************************
2 * vlc_timestamp_helper.h : timestamp handling helpers
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
6 * Authors: Felix Abecassis <felix.abecassis@gmail.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program 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
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 #ifndef VLC_TIMESTAMP_H
24 #define VLC_TIMESTAMP_H 1
26 /* Implementation of a circular buffer of timestamps with overwriting
27 * of older values. MediaCodec has only one type of timestamp, if a
28 * block has no PTS, we send the DTS instead. Some hardware decoders
29 * cannot cope with this situation and output the frames in the wrong
30 * order. As a workaround in this case, we use a FIFO of timestamps in
31 * order to remember which input packets had no PTS. Since an
32 * hardware decoder can silently drop frames, this might cause a
33 * growing desynchronization with the actual timestamp. Thus the
34 * circular buffer has a limited size and will overwrite older values.
44 static inline timestamp_fifo_t
*timestamp_FifoNew(uint32_t capacity
)
46 timestamp_fifo_t
*fifo
= calloc(1, sizeof(*fifo
));
49 fifo
->buffer
= vlc_alloc(capacity
, sizeof(*fifo
->buffer
));
54 fifo
->capacity
= capacity
;
58 static inline void timestamp_FifoRelease(timestamp_fifo_t
*fifo
)
64 static inline bool timestamp_FifoIsEmpty(timestamp_fifo_t
*fifo
)
66 return fifo
->size
== 0;
69 static inline bool timestamp_FifoIsFull(timestamp_fifo_t
*fifo
)
71 return fifo
->size
== fifo
->capacity
;
74 static inline void timestamp_FifoEmpty(timestamp_fifo_t
*fifo
)
79 static inline void timestamp_FifoPut(timestamp_fifo_t
*fifo
, vlc_tick_t ts
)
81 uint32_t end
= (fifo
->begin
+ fifo
->size
) % fifo
->capacity
;
82 fifo
->buffer
[end
] = ts
;
83 if (!timestamp_FifoIsFull(fifo
))
86 fifo
->begin
= (fifo
->begin
+ 1) % fifo
->capacity
;
89 static inline vlc_tick_t
timestamp_FifoGet(timestamp_fifo_t
*fifo
)
91 if (timestamp_FifoIsEmpty(fifo
))
92 return VLC_TICK_INVALID
;
94 vlc_tick_t result
= fifo
->buffer
[fifo
->begin
];
95 fifo
->begin
= (fifo
->begin
+ 1) % fifo
->capacity
;