OSDN Git Service

This huge patch from huevos_rancheros ports a number of video filters from mencoder...
[handbrake-jp/handbrake-jp-git.git] / libhb / fifo.c
1 /* $Id: fifo.c,v 1.17 2005/10/15 18:05:03 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8
9 #ifndef SYS_DARWIN
10 #include <malloc.h>
11 #endif
12
13 hb_buffer_t * hb_buffer_init( int size )
14 {
15     hb_buffer_t * b;
16     
17     if( !( b = calloc( sizeof( hb_buffer_t ), 1 ) ) )
18     {
19         hb_log( "out of memory" );
20         return NULL;
21     }
22
23     b->alloc = size;
24     b->size  = size;
25     if (!size)
26         return b;
27 #if defined( SYS_DARWIN ) || defined( SYS_FREEBSD )
28     b->data  = malloc( size );
29 #elif defined( SYS_CYGWIN )
30     /* FIXME */
31     b->data  = malloc( size + 17 );
32 #else
33     b->data  = memalign( 16, size );
34 #endif
35
36     if( !b->data )
37     {
38         hb_log( "out of memory" );
39         free( b );
40         return NULL;
41     }
42     return b;
43 }
44
45 void hb_buffer_realloc( hb_buffer_t * b, int size )
46 {
47     /* No more alignment, but we don't care */
48     b->data  = realloc( b->data, size );
49     b->alloc = size;
50 }
51
52 void hb_buffer_close( hb_buffer_t ** _b )
53 {
54     hb_buffer_t * b = *_b;
55
56     if( b->data )
57     {
58         free( b->data );
59     }
60     free( b );
61
62     *_b = NULL;
63 }
64
65 void hb_buffer_copy_settings( hb_buffer_t * dst, const hb_buffer_t * src )
66 {
67     dst->start     = src->start;
68     dst->stop      = src->stop;
69     dst->new_chap  = src->new_chap;
70     dst->frametype = src->frametype;
71     dst->flags     = src->flags;
72 }
73
74 /* Fifo */
75 struct hb_fifo_s
76 {
77     hb_lock_t    * lock;
78     int            capacity;
79     int            size;
80     hb_buffer_t  * first;
81     hb_buffer_t  * last;
82 };
83
84 hb_fifo_t * hb_fifo_init( int capacity )
85 {
86     hb_fifo_t * f;
87     f           = calloc( sizeof( hb_fifo_t ), 1 );
88     f->lock     = hb_lock_init();
89     f->capacity = capacity;
90     return f;
91 }
92
93 int hb_fifo_size( hb_fifo_t * f )
94 {
95     int ret;
96
97     hb_lock( f->lock );
98     ret = f->size;
99     hb_unlock( f->lock );
100
101     return ret;
102 }
103
104 int hb_fifo_is_full( hb_fifo_t * f )
105 {
106     int ret;
107
108     hb_lock( f->lock );
109     ret = ( f->size >= f->capacity );
110     hb_unlock( f->lock );
111
112     return ret;
113 }
114
115 float hb_fifo_percent_full( hb_fifo_t * f )
116 {
117     float ret;
118
119     hb_lock( f->lock );
120     ret = f->size / f->capacity;
121     hb_unlock( f->lock );
122
123     return ret;
124 }
125
126 hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
127 {
128     hb_buffer_t * b;
129
130     hb_lock( f->lock );
131     if( f->size < 1 )
132     {
133         hb_unlock( f->lock );
134         return NULL;
135     }
136     b         = f->first;
137     f->first  = b->next;
138     b->next   = NULL;
139     f->size  -= 1;
140     hb_unlock( f->lock );
141     
142     return b;
143 }
144
145 hb_buffer_t * hb_fifo_see( hb_fifo_t * f )
146 {
147     hb_buffer_t * b;
148
149     hb_lock( f->lock );
150     if( f->size < 1 )
151     {
152         hb_unlock( f->lock );
153         return NULL;
154     }
155     b = f->first;
156     hb_unlock( f->lock );
157
158     return b;
159 }
160
161 hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
162 {
163     hb_buffer_t * b;
164
165     hb_lock( f->lock );
166     if( f->size < 2 )
167     {
168         hb_unlock( f->lock );
169         return NULL;
170     }
171     b = f->first->next;
172     hb_unlock( f->lock );
173
174     return b;
175 }
176
177 void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
178 {
179     if( !b )
180     {
181         return;
182     }
183
184     hb_lock( f->lock );
185     if( f->size > 0 )
186     {
187         f->last->next = b;
188     }
189     else
190     {
191         f->first = b;
192     }
193     f->last  = b;
194     f->size += 1;
195     while( f->last->next )
196     {
197         f->size += 1;
198         f->last  = f->last->next;
199     }
200     hb_unlock( f->lock );
201 }
202
203 void hb_fifo_close( hb_fifo_t ** _f )
204 {
205     hb_fifo_t   * f = *_f;
206     hb_buffer_t * b;
207     
208     hb_log( "fifo_close: trashing %d buffer(s)", hb_fifo_size( f ) );
209     while( ( b = hb_fifo_get( f ) ) )
210     {
211         hb_buffer_close( &b );
212     }
213
214     hb_lock_close( &f->lock );
215     free( f );
216
217     *_f = NULL;
218 }