r134: Heroine Virtual's release 1.1.8
[cinelerra_cv/mob.git] / hvirtual / libmpeg3 / bitstream.c
blobbfebc132cb5c5583ead0eadc4180ad535ae14b00
1 #include "mpeg3private.h"
2 #include "mpeg3protos.h"
4 #include <stdlib.h>
6 mpeg3_bits_t* mpeg3bits_new_stream(mpeg3_t *file, mpeg3_demuxer_t *demuxer)
8 mpeg3_bits_t *stream = malloc(sizeof(mpeg3_bits_t));
9 stream->bfr = 0;
10 stream->bfr_size = 0;
11 stream->bit_number = 0;
12 stream->file = file;
13 stream->demuxer = demuxer;
14 stream->input_ptr = 0;
15 return stream;
18 int mpeg3bits_delete_stream(mpeg3_bits_t* stream)
20 free(stream);
21 return 0;
25 /* Fill a buffer. Only works if bit_number is on an 8 bit boundary */
26 int mpeg3bits_read_buffer(mpeg3_bits_t* stream, unsigned char *buffer, int bytes)
28 int result, i = 0;
29 while(stream->bit_number > 0)
31 stream->bit_number -= 8;
32 mpeg3demux_read_prev_char(stream->demuxer);
35 stream->bit_number = 0;
36 stream->bfr_size = 0;
37 stream->bfr = 0;
38 result = mpeg3demux_read_data(stream->demuxer, buffer, bytes);
39 return result;
42 /* For mp3 decompression use a pointer in a buffer for getbits. */
43 int mpeg3bits_use_ptr(mpeg3_bits_t* stream, unsigned char *buffer)
45 stream->bfr_size = stream->bit_number = 0;
46 stream->bfr = 0;
47 stream->input_ptr = buffer;
48 return 0;
51 /* Go back to using the demuxer for getbits in mp3. */
52 int mpeg3bits_use_demuxer(mpeg3_bits_t* stream)
54 if(stream->input_ptr)
56 stream->bfr_size = stream->bit_number = 0;
57 stream->input_ptr = 0;
58 stream->bfr = 0;
61 return 0;
64 /* Reconfigure for reverse operation */
65 /* Default is forward operation */
66 void mpeg3bits_start_reverse(mpeg3_bits_t* stream)
68 int i;
69 for(i = 0; i < stream->bfr_size; i += 8)
70 if(stream->input_ptr)
71 stream->input_ptr--;
72 else
73 mpeg3demux_read_prev_char(stream->demuxer);
76 /* Reconfigure for forward operation */
77 void mpeg3bits_start_forward(mpeg3_bits_t* stream)
79 int i;
80 for(i = 0; i < stream->bfr_size; i += 8)
81 if(stream->input_ptr)
82 stream->input_ptr++;
83 else
84 mpeg3demux_read_char(stream->demuxer);
87 /* Erase the buffer with the next 4 bytes in the file. */
88 int mpeg3bits_refill(mpeg3_bits_t* stream)
90 stream->bit_number = 32;
91 stream->bfr_size = 32;
93 if(stream->input_ptr)
95 stream->bfr = (unsigned int)(*stream->input_ptr++) << 24;
96 stream->bfr |= (unsigned int)(*stream->input_ptr++) << 16;
97 stream->bfr |= (unsigned int)(*stream->input_ptr++) << 8;
98 stream->bfr |= *stream->input_ptr++;
100 else
102 stream->bfr = mpeg3demux_read_char(stream->demuxer) << 24;
103 stream->bfr |= mpeg3demux_read_char(stream->demuxer) << 16;
104 stream->bfr |= mpeg3demux_read_char(stream->demuxer) << 8;
105 stream->bfr |= mpeg3demux_read_char(stream->demuxer);
107 return mpeg3demux_eof(stream->demuxer);
110 /* Erase the buffer with the previous 4 bytes in the file. */
111 int mpeg3bits_refill_backwards(mpeg3_bits_t* stream)
113 stream->bit_number = 0;
114 stream->bfr_size = 32;
115 stream->bfr = mpeg3demux_read_prev_char(stream->demuxer);
116 stream->bfr |= (unsigned int)mpeg3demux_read_prev_char(stream->demuxer) << 8;
117 stream->bfr |= (unsigned int)mpeg3demux_read_prev_char(stream->demuxer) << 16;
118 stream->bfr |= (unsigned int)mpeg3demux_read_prev_char(stream->demuxer) << 24;
119 return mpeg3demux_eof(stream->demuxer);
122 int mpeg3bits_byte_align(mpeg3_bits_t *stream)
124 stream->bit_number = (stream->bit_number + 7) & 0xf8;
125 return 0;
128 int mpeg3bits_seek_end(mpeg3_bits_t* stream)
130 stream->bfr_size = stream->bit_number = 0;
131 return mpeg3demux_seek_end(stream->demuxer);
134 int mpeg3bits_open_title(mpeg3_bits_t* stream, int title)
136 stream->bfr_size = stream->bit_number = 0;
137 return mpeg3demux_open_title(stream->demuxer, title);
140 int mpeg3bits_seek_start(mpeg3_bits_t* stream)
142 stream->bfr_size = stream->bit_number = 0;
143 return mpeg3demux_seek_start(stream->demuxer);
146 int mpeg3bits_seek_time(mpeg3_bits_t* stream, double time_position)
148 stream->bfr_size = stream->bit_number = 0;
149 return mpeg3demux_seek_time(stream->demuxer, time_position);
152 int mpeg3bits_seek_byte(mpeg3_bits_t* stream, int64_t position)
154 stream->bfr_size = stream->bit_number = 0;
155 return mpeg3demux_seek_byte(stream->demuxer, position);
158 int mpeg3bits_seek_percentage(mpeg3_bits_t* stream, double percentage)
160 stream->bfr_size = stream->bit_number = 0;
161 return mpeg3demux_seek_percentage(stream->demuxer, percentage);
164 int64_t mpeg3bits_tell(mpeg3_bits_t* stream)
166 return mpeg3demux_tell(stream->demuxer);
169 int mpeg3bits_getbitoffset(mpeg3_bits_t *stream)
171 return stream->bit_number & 7;