VLC  3.0.21
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  * $Id$
6  *
7  * Authors: Felix Abecassis <felix.abecassis@gmail.com>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23 
24 #ifndef VLC_TIMESTAMP_H
25 #define VLC_TIMESTAMP_H 1
26 
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.
36  */
37 typedef struct
38 {
39  uint32_t begin;
40  uint32_t size;
41  uint32_t capacity;
42  int64_t *buffer;
44 
45 static inline timestamp_fifo_t *timestamp_FifoNew(uint32_t capacity)
46 {
47  timestamp_fifo_t *fifo = calloc(1, sizeof(*fifo));
48  if (!fifo)
49  return NULL;
50  fifo->buffer = vlc_alloc(capacity, sizeof(*fifo->buffer));
51  if (!fifo->buffer) {
52  free(fifo);
53  return NULL;
54  }
55  fifo->capacity = capacity;
56  return fifo;
57 }
58 
59 static inline void timestamp_FifoRelease(timestamp_fifo_t *fifo)
60 {
61  free(fifo->buffer);
62  free(fifo);
63 }
64 
65 static inline bool timestamp_FifoIsEmpty(timestamp_fifo_t *fifo)
66 {
67  return fifo->size == 0;
68 }
69 
70 static inline bool timestamp_FifoIsFull(timestamp_fifo_t *fifo)
71 {
72  return fifo->size == fifo->capacity;
73 }
74 
75 static inline void timestamp_FifoEmpty(timestamp_fifo_t *fifo)
76 {
77  fifo->size = 0;
78 }
79 
80 static inline void timestamp_FifoPut(timestamp_fifo_t *fifo, int64_t ts)
81 {
82  uint32_t end = (fifo->begin + fifo->size) % fifo->capacity;
83  fifo->buffer[end] = ts;
84  if (!timestamp_FifoIsFull(fifo))
85  fifo->size += 1;
86  else
87  fifo->begin = (fifo->begin + 1) % fifo->capacity;
88 }
89 
90 static inline int64_t timestamp_FifoGet(timestamp_fifo_t *fifo)
91 {
92  if (timestamp_FifoIsEmpty(fifo))
93  return VLC_TICK_INVALID;
94 
95  int64_t result = fifo->buffer[fifo->begin];
96  fifo->begin = (fifo->begin + 1) % fifo->capacity;
97  fifo->size -= 1;
98  return result;
99 }
100 
101 #endif
VLC_TICK_INVALID
#define VLC_TICK_INVALID
Definition: vlc_common.h:153
timestamp_FifoRelease
static void timestamp_FifoRelease(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:59
vlc_common.h
timestamp_fifo_t::capacity
uint32_t capacity
Definition: vlc_timestamp_helper.h:61
timestamp_FifoGet
static int64_t timestamp_FifoGet(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:90
timestamp_fifo_t
Definition: vlc_timestamp_helper.h:37
timestamp_FifoPut
static void timestamp_FifoPut(timestamp_fifo_t *fifo, int64_t ts)
Definition: vlc_timestamp_helper.h:80
timestamp_fifo_t::begin
uint32_t begin
Definition: vlc_timestamp_helper.h:59
timestamp_fifo_t::buffer
int64_t * buffer
Definition: vlc_timestamp_helper.h:62
timestamp_fifo_t::size
uint32_t size
Definition: vlc_timestamp_helper.h:60
timestamp_FifoNew
static timestamp_fifo_t * timestamp_FifoNew(uint32_t capacity)
Definition: vlc_timestamp_helper.h:45
timestamp_FifoEmpty
static void timestamp_FifoEmpty(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:75
vlc_alloc
static void * vlc_alloc(size_t count, size_t size)
Definition: vlc_common.h:957
timestamp_FifoIsEmpty
static bool timestamp_FifoIsEmpty(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:65
timestamp_FifoIsFull
static bool timestamp_FifoIsFull(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:70