]> git.sesse.net Git - vlc/blob - src/misc/picture_fifo.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / misc / picture_fifo.c
1 /*****************************************************************************
2  * picture_fifo.c : picture fifo functions
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * Copyright (C) 2009 Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
6  * $Id$
7  *
8  * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32 #include <assert.h>
33
34 #include <vlc_common.h>
35 #include <vlc_picture_fifo.h>
36
37 /*****************************************************************************
38  *
39  *****************************************************************************/
40 struct picture_fifo_t {
41     vlc_mutex_t lock;
42     picture_t   *first;
43     picture_t   **last_ptr;
44 };
45
46 static void PictureFifoReset(picture_fifo_t *fifo)
47 {
48     fifo->first    = NULL;
49     fifo->last_ptr = &fifo->first;
50 }
51 static void PictureFifoPush(picture_fifo_t *fifo, picture_t *picture)
52 {
53     assert(!picture->p_next);
54     *fifo->last_ptr = picture;
55     fifo->last_ptr  = &picture->p_next;
56 }
57 static picture_t *PictureFifoPop(picture_fifo_t *fifo)
58 {
59     picture_t *picture = fifo->first;
60
61     if (picture) {
62         fifo->first = picture->p_next;
63         if (!fifo->first)
64             fifo->last_ptr = &fifo->first;
65     }
66     return picture;
67 }
68
69 picture_fifo_t *picture_fifo_New(void)
70 {
71     picture_fifo_t *fifo = malloc(sizeof(*fifo));
72     if (!fifo)
73         return NULL;
74
75     vlc_mutex_init(&fifo->lock);
76     PictureFifoReset(fifo);
77     return fifo;
78 }
79
80 void picture_fifo_Push(picture_fifo_t *fifo, picture_t *picture)
81 {
82     vlc_mutex_lock(&fifo->lock);
83     PictureFifoPush(fifo, picture);
84     vlc_mutex_unlock(&fifo->lock);
85 }
86 picture_t *picture_fifo_Pop(picture_fifo_t *fifo)
87 {
88     vlc_mutex_lock(&fifo->lock);
89     picture_t *picture = PictureFifoPop(fifo);
90     vlc_mutex_unlock(&fifo->lock);
91
92     return picture;
93 }
94 picture_t *picture_fifo_Peek(picture_fifo_t *fifo)
95 {
96     vlc_mutex_lock(&fifo->lock);
97     picture_t *picture = fifo->first;
98     if (picture)
99         picture_Hold(picture);
100     vlc_mutex_unlock(&fifo->lock);
101
102     return picture;
103 }
104 void picture_fifo_Flush(picture_fifo_t *fifo, mtime_t date, bool flush_before)
105 {
106     picture_t *picture;
107
108     vlc_mutex_lock(&fifo->lock);
109
110     picture = fifo->first;
111     PictureFifoReset(fifo);
112
113     picture_fifo_t tmp;
114     PictureFifoReset(&tmp);
115
116     while (picture) {
117         picture_t *next = picture->p_next;
118
119         picture->p_next = NULL;
120         if (( flush_before && picture->date <= date) ||
121             (!flush_before && picture->date >= date))
122             PictureFifoPush(&tmp, picture);
123         else
124             PictureFifoPush(fifo, picture);
125         picture = next;
126     }
127     vlc_mutex_unlock(&fifo->lock);
128
129     for (;;) {
130         picture_t *picture = PictureFifoPop(&tmp);
131         if (!picture)
132             break;
133         picture_Release(picture);
134     }
135 }
136 void picture_fifo_OffsetDate(picture_fifo_t *fifo, mtime_t delta)
137 {
138     vlc_mutex_lock(&fifo->lock);
139     for (picture_t *picture = fifo->first; picture != NULL;) {
140         picture->date += delta;
141         picture = picture->p_next;
142     }
143     vlc_mutex_unlock(&fifo->lock);
144 }
145 void picture_fifo_Delete(picture_fifo_t *fifo)
146 {
147     picture_fifo_Flush(fifo, INT64_MAX, true);
148     vlc_mutex_destroy(&fifo->lock);
149     free(fifo);
150 }
151