VLC  4.0.0-dev
vlc_timestamp_helper.h
Go to the documentation of this file.
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 /* 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.
35  */
36 typedef struct
37 {
38  uint32_t begin;
39  uint32_t size;
40  uint32_t capacity;
41  vlc_tick_t *buffer;
43 
44 static inline timestamp_fifo_t *timestamp_FifoNew(uint32_t capacity)
45 {
46  timestamp_fifo_t *fifo = calloc(1, sizeof(*fifo));
47  if (!fifo)
48  return NULL;
49  fifo->buffer = vlc_alloc(capacity, sizeof(*fifo->buffer));
50  if (!fifo->buffer) {
51  free(fifo);
52  return NULL;
53  }
54  fifo->capacity = capacity;
55  return fifo;
56 }
57 
58 static inline void timestamp_FifoRelease(timestamp_fifo_t *fifo)
59 {
60  free(fifo->buffer);
61  free(fifo);
62 }
63 
64 static inline bool timestamp_FifoIsEmpty(timestamp_fifo_t *fifo)
65 {
66  return fifo->size == 0;
67 }
68 
69 static inline bool timestamp_FifoIsFull(timestamp_fifo_t *fifo)
70 {
71  return fifo->size == fifo->capacity;
72 }
73 
74 static inline void timestamp_FifoEmpty(timestamp_fifo_t *fifo)
75 {
76  fifo->size = 0;
77 }
78 
79 static inline void timestamp_FifoPut(timestamp_fifo_t *fifo, vlc_tick_t ts)
80 {
81  uint32_t end = (fifo->begin + fifo->size) % fifo->capacity;
82  fifo->buffer[end] = ts;
83  if (!timestamp_FifoIsFull(fifo))
84  fifo->size += 1;
85  else
86  fifo->begin = (fifo->begin + 1) % fifo->capacity;
87 }
88 
90 {
91  if (timestamp_FifoIsEmpty(fifo))
92  return VLC_TICK_INVALID;
93 
94  vlc_tick_t result = fifo->buffer[fifo->begin];
95  fifo->begin = (fifo->begin + 1) % fifo->capacity;
96  fifo->size -= 1;
97  return result;
98 }
99 
100 #endif
uint32_t size
Definition: vlc_timestamp_helper.h:40
static vlc_tick_t timestamp_FifoGet(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:90
static bool timestamp_FifoIsEmpty(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:65
Definition: vlc_timestamp_helper.h:37
static timestamp_fifo_t * timestamp_FifoNew(uint32_t capacity)
Definition: vlc_timestamp_helper.h:45
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45
static void timestamp_FifoEmpty(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:75
vlc_tick_t * buffer
Definition: vlc_timestamp_helper.h:42
static void timestamp_FifoRelease(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:59
static bool timestamp_FifoIsFull(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:70
uint32_t begin
Definition: vlc_timestamp_helper.h:39
static void timestamp_FifoPut(timestamp_fifo_t *fifo, vlc_tick_t ts)
Definition: vlc_timestamp_helper.h:80
static void * vlc_alloc(size_t count, size_t size)
Definition: vlc_common.h:1141
uint32_t capacity
Definition: vlc_timestamp_helper.h:41
#define VLC_TICK_INVALID
Definition: vlc_config.h:44