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