-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitstream.c
executable file
·125 lines (103 loc) · 3.06 KB
/
bitstream.c
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
/*
* Bitstream writer function
*
* (C) Andree Buschmann 2000, Frank Klemm 2001,02. All rights reserved.
*
* Principles:
*
* History:
* ca. 1998 created
* 2002
*
* Global functions:
* -
*
* TODO:
* - Transition from 32 bit little endian word stream to 8 bit octet stream
*/
#include "mppenc.h"
static Uint32_t Buffer [4096+2]; // Puffer für Bitstromdatei
static Uint32_t dword; // 32-bit-Wort für Bitstrom-I/O
static int filled; // Position im aktuell zu füllenden 32-bit-Wortes
static unsigned int Zaehler; // Positionszeiger das bearbeitete Bitstromwort (32 bit)
static unsigned long WrittenBytes;
unsigned long
GetWrittenBytes ( void )
{
return WrittenBytes;
}
void
InitBitStream ( void )
{
dword = 0; // 32-bit-Wort für Bitstrom-I/O
filled = 32; // Position im aktuell zu füllenden 32-bit-Wortes
Zaehler = 0; // Positionszeiger das bearbeitete Bitstromwort (32 bit)
WrittenBytes = 0; // Zähler für Anzahl geschriebener Bits im Bitstrom
}
static void
FlushData ( FILE* fp,
const Uint8_t* buffer,
size_t bytes )
{
size_t WrittenBytes;
do {
WrittenBytes = fwrite ( buffer, 1, bytes, fp );
if ( WrittenBytes <= 0 ) {
stderr_printf ( "\b\n WARNING: Disk full?, retry after 10 sec ...\a" );
sleep (10);
continue;
}
buffer += WrittenBytes;
bytes -= WrittenBytes;
} while ( bytes != 0 );
}
void
FlushBitstream ( FILE* fp, int lenoffset )
{
int bytes;
Uint8_t* p = (Uint8_t*)Buffer;
ReadBE32 ( Buffer [Zaehler], & dword ); // Zuweisung des "letzten" Worts
bytes = 4 * Zaehler + 4 - (filled >> 3);
p[lenoffset+0] = ( bytes - 2 - lenoffset ) >> 8;
p[lenoffset+1] = ( bytes - 2 - lenoffset ) >> 0;
FlushData ( fp, (const Uint8_t*)Buffer, bytes );
dword = 0; // 32-bit-Wort für Bitstrom-I/O
filled = 32; // Position im aktuell zu füllenden 32-bit-Wortes
Zaehler = 0; // Positionszeiger das bearbeitete Bitstromwort (32 bit)
WrittenBytes += bytes;
}
void
WriteBits ( const Uint32_t input,
const unsigned int bits )
{
filled -= bits;
if ( filled > 0 ) {
dword |= input << filled;
}
else if ( filled < 0 ) {
dword |= input >> -filled ;
ReadBE32 ( Buffer [Zaehler], & dword );
Zaehler++;
filled += 32;
dword = input << filled;
}
else {
dword |= input;
ReadBE32 ( Buffer [Zaehler], & dword );
Zaehler++;
filled = 32;
dword = 0;
}
}
void
WriteBit ( const unsigned int input )
{
dword |= input << --filled;
if ( filled == 0 ) {
ReadBE32 ( Buffer [Zaehler], & dword );
Zaehler++;
filled = 32;
dword = 0;
}
}
/* end of bitstream.c */