1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
/* $Id: Fifo.c,v 1.17 2004/04/27 19:30:00 titer Exp $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.m0k.org/>.
It may be used under the terms of the GNU General Public License. */
#include "Fifo.h"
#if defined( HB_BEOS ) || defined( HB_LINUX )
#include <malloc.h>
#endif
HBBuffer * HBBufferInit( int size )
{
HBBuffer * b;
if( !( b = calloc( sizeof( HBBuffer ), 1 ) ) )
{
HBLog( "HBBuffer: malloc() failed, gonna crash" );
return NULL;
}
b->alloc = size;
b->size = size;
#if defined( HB_BEOS ) || defined( HB_LINUX )
b->data = memalign( 16, size );
#elif defined( HB_MACOSX )
/* OS X's malloc returns 16-bytes aligned memory */
b->data = malloc( size );
#elif defined( HB_CYGWIN )
b->dataOrig = malloc( size + 15 );
b->data = b->dataOrig + 15;
b->data -= (long) b->data & 15;
#endif
b->dataf = (float*) b->data;
if( !b->data )
{
HBLog( "HBBuffer: malloc() failed, gonna crash" );
free( b );
return NULL;
}
return b;
}
void HBBufferReAlloc( HBBuffer * b, int size )
{
/* We don't care about alignment here, realloc is only used in the
AVI muxer anyway */
#if defined( HB_BEOS ) || defined( HB_LINUX ) || defined( HB_MACOSX )
b->data = realloc( b->data, size );
#elif defined( HB_CYGWIN )
int alignment = b->data - b->dataOrig;
b->dataOrig = realloc( b->dataOrig, size + alignment );
b->data = b->dataOrig + alignment;
#endif
b->alloc = size;
if( !b->data )
{
HBLog( "HBBuffer: realloc() failed, gonna crash soon" );
}
}
void HBBufferClose( HBBuffer ** _b )
{
HBBuffer * b = *_b;
#if defined( HB_BEOS ) || defined( HB_LINUX ) || defined( HB_MACOSX )
free( b->data );
#elif defined( HB_CYGWIN )
free( b->dataOrig );
#endif
free( b );
*_b = NULL;
}
HBFifo * HBFifoInit( int capacity )
{
HBFifo * f;
if( !( f = malloc( sizeof( HBFifo ) ) ) )
{
HBLog( "HBFifo: malloc() failed, gonna crash" );
return NULL;
}
f->die = 0;
f->capacity = capacity;
f->whereToPush = 0;
f->whereToPop = 0;
if( !( f->buffers = malloc( ( capacity + 1 ) * sizeof( void* ) ) ) )
{
HBLog( "HBFifo: malloc() failed, gonna crash" );
free( f );
return NULL;
}
f->lock = HBLockInit();
f->cond = HBCondInit();
return f;
}
void HBFifoDie( HBFifo * f )
{
HBLockLock( f->lock );
f->die = 1;
HBCondSignal( f->cond );
HBLockUnlock( f->lock );
}
void HBFifoClose( HBFifo ** _f )
{
HBFifo * f = (*_f);
HBLog( "HBFifo: trashing %d buffer%s",
HBFifoSize( f ), ( HBFifoSize( f ) > 1 ) ? "s" : "" );
while( f->whereToPush != f->whereToPop )
{
HBBufferClose( &(f->buffers[f->whereToPop]) );
f->whereToPop++;
f->whereToPop %= ( f->capacity + 1 );
}
HBLockClose( &f->lock );
HBCondClose( &f->cond );
free( f->buffers );
free( f );
*_f = NULL;
}
|