/src/vlc/include/vlc_timestamp_helper.h
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * vlc_timestamp_helper.h : timestamp handling helpers |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2014 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Felix Abecassis <felix.abecassis@gmail.com> |
7 | | * |
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. |
12 | | * |
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. |
17 | | * |
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 | | *****************************************************************************/ |
22 | | |
23 | | #ifndef VLC_TIMESTAMP_H |
24 | | #define VLC_TIMESTAMP_H 1 |
25 | | |
26 | | #include <vlc_tick.h> |
27 | | |
28 | | /* Implementation of a circular buffer of timestamps with overwriting |
29 | | * of older values. MediaCodec has only one type of timestamp, if a |
30 | | * block has no PTS, we send the DTS instead. Some hardware decoders |
31 | | * cannot cope with this situation and output the frames in the wrong |
32 | | * order. As a workaround in this case, we use a FIFO of timestamps in |
33 | | * order to remember which input packets had no PTS. Since an |
34 | | * hardware decoder can silently drop frames, this might cause a |
35 | | * growing desynchronization with the actual timestamp. Thus the |
36 | | * circular buffer has a limited size and will overwrite older values. |
37 | | */ |
38 | | typedef struct |
39 | | { |
40 | | uint32_t begin; |
41 | | uint32_t size; |
42 | | uint32_t capacity; |
43 | | vlc_tick_t *buffer; |
44 | | } timestamp_fifo_t; |
45 | | |
46 | | static inline timestamp_fifo_t *timestamp_FifoNew(uint32_t capacity) |
47 | 0 | { |
48 | 0 | timestamp_fifo_t *fifo = (timestamp_fifo_t *)calloc(1, sizeof(*fifo)); |
49 | 0 | if (!fifo) |
50 | 0 | return NULL; |
51 | 0 | fifo->buffer = (vlc_tick_t*)vlc_alloc(capacity, sizeof(*fifo->buffer)); |
52 | 0 | if (!fifo->buffer) { |
53 | 0 | free(fifo); |
54 | 0 | return NULL; |
55 | 0 | } |
56 | 0 | fifo->capacity = capacity; |
57 | 0 | return fifo; |
58 | 0 | } |
59 | | |
60 | | static inline void timestamp_FifoRelease(timestamp_fifo_t *fifo) |
61 | 0 | { |
62 | 0 | free(fifo->buffer); |
63 | 0 | free(fifo); |
64 | 0 | } |
65 | | |
66 | | static inline bool timestamp_FifoIsEmpty(timestamp_fifo_t *fifo) |
67 | 0 | { |
68 | 0 | return fifo->size == 0; |
69 | 0 | } |
70 | | |
71 | | static inline bool timestamp_FifoIsFull(timestamp_fifo_t *fifo) |
72 | 0 | { |
73 | 0 | return fifo->size == fifo->capacity; |
74 | 0 | } |
75 | | |
76 | | static inline void timestamp_FifoEmpty(timestamp_fifo_t *fifo) |
77 | 0 | { |
78 | 0 | fifo->size = 0; |
79 | 0 | } |
80 | | |
81 | | static inline void timestamp_FifoPut(timestamp_fifo_t *fifo, vlc_tick_t ts) |
82 | 0 | { |
83 | 0 | uint32_t end = (fifo->begin + fifo->size) % fifo->capacity; |
84 | 0 | fifo->buffer[end] = ts; |
85 | 0 | if (!timestamp_FifoIsFull(fifo)) |
86 | 0 | fifo->size += 1; |
87 | 0 | else |
88 | 0 | fifo->begin = (fifo->begin + 1) % fifo->capacity; |
89 | 0 | } |
90 | | |
91 | | static inline vlc_tick_t timestamp_FifoGet(timestamp_fifo_t *fifo) |
92 | 0 | { |
93 | 0 | if (timestamp_FifoIsEmpty(fifo)) |
94 | 0 | return VLC_TICK_INVALID; |
95 | 0 |
|
96 | 0 | vlc_tick_t result = fifo->buffer[fifo->begin]; |
97 | 0 | fifo->begin = (fifo->begin + 1) % fifo->capacity; |
98 | 0 | fifo->size -= 1; |
99 | 0 | return result; |
100 | 0 | } |
101 | | |
102 | | #endif |