OSDN Git Service

Much better B-frame muxing frame-reordering. This will preserve the sps/pps info...
[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 /* Fifo */
66 struct hb_fifo_s
67 {
68     hb_lock_t    * lock;
69     int            capacity;
70     int            size;
71     hb_buffer_t  * first;
72     hb_buffer_t  * last;
73 };
74
75 hb_fifo_t * hb_fifo_init( int capacity )
76 {
77     hb_fifo_t * f;
78     f           = calloc( sizeof( hb_fifo_t ), 1 );
79     f->lock     = hb_lock_init();
80     f->capacity = capacity;
81     return f;
82 }
83
84 int hb_fifo_size( hb_fifo_t * f )
85 {
86     int ret;
87
88     hb_lock( f->lock );
89     ret = f->size;
90     hb_unlock( f->lock );
91
92     return ret;
93 }
94
95 int hb_fifo_is_full( hb_fifo_t * f )
96 {
97     int ret;
98
99     hb_lock( f->lock );
100     ret = ( f->size >= f->capacity );
101     hb_unlock( f->lock );
102
103     return ret;
104 }
105
106 float hb_fifo_percent_full( hb_fifo_t * f )
107 {
108     float ret;
109
110     hb_lock( f->lock );
111     ret = f->size / f->capacity;
112     hb_unlock( f->lock );
113
114     return ret;
115 }
116
117 hb_buffer_t * hb_fifo_get( hb_fifo_t * f )
118 {
119     hb_buffer_t * b;
120
121     hb_lock( f->lock );
122     if( f->size < 1 )
123     {
124         hb_unlock( f->lock );
125         return NULL;
126     }
127     b         = f->first;
128     f->first  = b->next;
129     b->next   = NULL;
130     f->size  -= 1;
131     hb_unlock( f->lock );
132
133     return b;
134 }
135
136 hb_buffer_t * hb_fifo_see( hb_fifo_t * f )
137 {
138     hb_buffer_t * b;
139
140     hb_lock( f->lock );
141     if( f->size < 1 )
142     {
143         hb_unlock( f->lock );
144         return NULL;
145     }
146     b = f->first;
147     hb_unlock( f->lock );
148
149     return b;
150 }
151
152 hb_buffer_t * hb_fifo_see2( hb_fifo_t * f )
153 {
154     hb_buffer_t * b;
155
156     hb_lock( f->lock );
157     if( f->size < 2 )
158     {
159         hb_unlock( f->lock );
160         return NULL;
161     }
162     b = f->first->next;
163     hb_unlock( f->lock );
164
165     return b;
166 }
167
168 void hb_fifo_push( hb_fifo_t * f, hb_buffer_t * b )
169 {
170     if( !b )
171     {
172         return;
173     }
174
175     hb_lock( f->lock );
176     if( f->size > 0 )
177     {
178         f->last->next = b;
179     }
180     else
181     {
182         f->first = b;
183     }
184     f->last  = b;
185     f->size += 1;
186     while( f->last->next )
187     {
188         f->size += 1;
189         f->last  = f->last->next;
190     }
191     hb_unlock( f->lock );
192 }
193
194 void hb_fifo_close( hb_fifo_t ** _f )
195 {
196     hb_fifo_t   * f = *_f;
197     hb_buffer_t * b;
198     
199     hb_log( "fifo_close: trashing %d buffer(s)", hb_fifo_size( f ) );
200     while( ( b = hb_fifo_get( f ) ) )
201     {
202         hb_buffer_close( &b );
203     }
204
205     hb_lock_close( &f->lock );
206     free( f );
207
208     *_f = NULL;
209 }