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 */
36typedef struct
38 uint32_t begin;
39 uint32_t size;
40 uint32_t capacity;
41 vlc_tick_t *buffer;
43
44static inline timestamp_fifo_t *timestamp_FifoNew(uint32_t capacity)
46 timestamp_fifo_t *fifo = (timestamp_fifo_t *)calloc(1, sizeof(*fifo));
47 if (!fifo)
48 return NULL;
49 fifo->buffer = (vlc_tick_t*)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
58static inline void timestamp_FifoRelease(timestamp_fifo_t *fifo)
60 free(fifo->buffer);
61 free(fifo);
62}
63
64static inline bool timestamp_FifoIsEmpty(timestamp_fifo_t *fifo)
66 return fifo->size == 0;
67}
68
69static inline bool timestamp_FifoIsFull(timestamp_fifo_t *fifo)
71 return fifo->size == fifo->capacity;
72}
73
74static inline void timestamp_FifoEmpty(timestamp_fifo_t *fifo)
76 fifo->size = 0;
77}
78
79static 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))
84 fifo->size += 1;
85 else
86 fifo->begin = (fifo->begin + 1) % fifo->capacity;
87}
88
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
Definition: vlc_timestamp_helper.h:38
vlc_tick_t * buffer
Definition: vlc_timestamp_helper.h:42
uint32_t capacity
Definition: vlc_timestamp_helper.h:41
uint32_t begin
Definition: vlc_timestamp_helper.h:39
uint32_t size
Definition: vlc_timestamp_helper.h:40
This file is a collection of common definitions and types.
static void * vlc_alloc(size_t count, size_t size)
Definition: vlc_common.h:1157
#define VLC_TICK_INVALID
Definition: vlc_config.h:44
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
static bool timestamp_FifoIsFull(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:70
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
static void timestamp_FifoRelease(timestamp_fifo_t *fifo)
Definition: vlc_timestamp_helper.h:59
static timestamp_fifo_t * timestamp_FifoNew(uint32_t capacity)
Definition: vlc_timestamp_helper.h:45
static void timestamp_FifoPut(timestamp_fifo_t *fifo, vlc_tick_t ts)
Definition: vlc_timestamp_helper.h:80