1 /*****************************************************************************
2 * vlc_timestamp_helper.h : timestamp handling helpers
3 *****************************************************************************
4 * Copyright (C) 2014 VLC authors and VideoLAN
7 * Authors: Felix Abecassis <felix.abecassis@gmail.com>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifndef VLC_TIMESTAMP_H
25 #define VLC_TIMESTAMP_H 1
27 /* Implementation of a circular buffer of timestamps with overwriting
28 * of older values. MediaCodec has only one type of timestamp, if a
29 * block has no PTS, we send the DTS instead. Some hardware decoders
30 * cannot cope with this situation and output the frames in the wrong
31 * order. As a workaround in this case, we use a FIFO of timestamps in
32 * order to remember which input packets had no PTS. Since an
33 * hardware decoder can silently drop frames, this might cause a
34 * growing desynchronization with the actual timestamp. Thus the
35 * circular buffer has a limited size and will overwrite older values.
45 static inline timestamp_fifo_t
*timestamp_FifoNew(uint32_t capacity
)
47 timestamp_fifo_t
*fifo
= calloc(1, sizeof(*fifo
));
50 fifo
->buffer
= malloc(capacity
* sizeof(*fifo
->buffer
));
55 fifo
->capacity
= capacity
;
59 static inline void timestamp_FifoRelease(timestamp_fifo_t
*fifo
)
65 static inline bool timestamp_FifoIsEmpty(timestamp_fifo_t
*fifo
)
67 return fifo
->size
== 0;
70 static inline bool timestamp_FifoIsFull(timestamp_fifo_t
*fifo
)
72 return fifo
->size
== fifo
->capacity
;
75 static inline void timestamp_FifoEmpty(timestamp_fifo_t
*fifo
)
80 static inline void timestamp_FifoPut(timestamp_fifo_t
*fifo
, int64_t ts
)
82 uint32_t end
= (fifo
->begin
+ fifo
->size
) % fifo
->capacity
;
83 fifo
->buffer
[end
] = ts
;
84 if (!timestamp_FifoIsFull(fifo
))
87 fifo
->begin
= (fifo
->begin
+ 1) % fifo
->capacity
;
90 static inline int64_t timestamp_FifoGet(timestamp_fifo_t
*fifo
)
92 if (timestamp_FifoIsEmpty(fifo
))
93 return VLC_TS_INVALID
;
95 int64_t result
= fifo
->buffer
[fifo
->begin
];
96 fifo
->begin
= (fifo
->begin
+ 1) % fifo
->capacity
;