Fixed initialisation of tf in file_open(). Without setting the memory to 0,
[cinelerra_cv/mob.git] / libmpeg3 / bitstream.c
blob8521a822071457cedd6fd69805e374fd7f7b1c98
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)
71 if(stream->input_ptr)
72 stream->input_ptr--;
73 else
74 mpeg3demux_read_prev_char(stream->demuxer);
78 /* Reconfigure for forward operation */
79 void mpeg3bits_start_forward(mpeg3_bits_t* stream)
81 int i;
83 // If already at the bof, the buffer is already invalid.
84 if(stream->demuxer && mpeg3bits_bof(stream))
86 stream->bfr_size = 0;
87 stream->bit_number = 0;
88 stream->bfr = 0;
89 stream->input_ptr = 0;
90 mpeg3demux_read_char(stream->demuxer);
92 else
93 for(i = 0; i < stream->bfr_size; i += 8)
95 if(stream->input_ptr)
96 stream->input_ptr++;
97 else
98 mpeg3demux_read_char(stream->demuxer);
102 /* Erase the buffer with the next 4 bytes in the file. */
103 int mpeg3bits_refill(mpeg3_bits_t* stream)
105 stream->bit_number = 32;
106 stream->bfr_size = 32;
108 if(stream->input_ptr)
110 stream->bfr = (unsigned int)(*stream->input_ptr++) << 24;
111 stream->bfr |= (unsigned int)(*stream->input_ptr++) << 16;
112 stream->bfr |= (unsigned int)(*stream->input_ptr++) << 8;
113 stream->bfr |= *stream->input_ptr++;
115 else
117 stream->bfr = mpeg3demux_read_char(stream->demuxer) << 24;
118 stream->bfr |= mpeg3demux_read_char(stream->demuxer) << 16;
119 stream->bfr |= mpeg3demux_read_char(stream->demuxer) << 8;
120 stream->bfr |= mpeg3demux_read_char(stream->demuxer);
122 return mpeg3demux_eof(stream->demuxer);
125 /* Erase the buffer with the previous 4 bytes in the file. */
126 int mpeg3bits_refill_backwards(mpeg3_bits_t* stream)
128 stream->bit_number = 0;
129 stream->bfr_size = 32;
130 stream->bfr = mpeg3demux_read_prev_char(stream->demuxer);
131 stream->bfr |= (unsigned int)mpeg3demux_read_prev_char(stream->demuxer) << 8;
132 stream->bfr |= (unsigned int)mpeg3demux_read_prev_char(stream->demuxer) << 16;
133 stream->bfr |= (unsigned int)mpeg3demux_read_prev_char(stream->demuxer) << 24;
134 return mpeg3demux_eof(stream->demuxer);
137 int mpeg3bits_byte_align(mpeg3_bits_t *stream)
139 stream->bit_number = (stream->bit_number + 7) & 0xf8;
140 return 0;
143 int mpeg3bits_open_title(mpeg3_bits_t* stream, int title)
145 stream->bfr_size = stream->bit_number = 0;
146 return mpeg3demux_open_title(stream->demuxer, title);
150 * int mpeg3bits_seek_time(mpeg3_bits_t* stream, double time_position)
152 * stream->bfr_size = stream->bit_number = 0;
153 * return mpeg3demux_seek_time(stream->demuxer, time_position);
157 int mpeg3bits_seek_byte(mpeg3_bits_t* stream, int64_t position)
159 stream->bfr_size = stream->bit_number = 0;
160 return mpeg3demux_seek_byte(stream->demuxer, position);
163 void mpeg3bits_reset(mpeg3_bits_t *stream)
165 stream->bfr_size = stream->bit_number = 0;
168 int64_t mpeg3bits_tell(mpeg3_bits_t* stream)
170 return mpeg3demux_tell_byte(stream->demuxer);
173 int mpeg3bits_getbitoffset(mpeg3_bits_t *stream)
175 return stream->bit_number & 7;