1 #include "mpeg3private.h"
2 #include "mpeg3protos.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
));
11 stream
->bit_number
= 0;
13 stream
->demuxer
= demuxer
;
14 stream
->input_ptr
= 0;
18 int mpeg3bits_delete_stream(mpeg3_bits_t
* stream
)
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
)
29 while(stream
->bit_number
> 0)
31 stream
->bit_number
-= 8;
32 mpeg3demux_read_prev_char(stream
->demuxer
);
35 stream
->bit_number
= 0;
38 result
= mpeg3demux_read_data(stream
->demuxer
, buffer
, bytes
);
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;
47 stream
->input_ptr
= buffer
;
51 /* Go back to using the demuxer for getbits in mp3. */
52 int mpeg3bits_use_demuxer(mpeg3_bits_t
* stream
)
56 stream
->bfr_size
= stream
->bit_number
= 0;
57 stream
->input_ptr
= 0;
64 /* Reconfigure for reverse operation */
65 /* Default is forward operation */
66 void mpeg3bits_start_reverse(mpeg3_bits_t
* stream
)
69 for(i
= 0; i
< stream
->bfr_size
; i
+= 8)
74 mpeg3demux_read_prev_char(stream
->demuxer
);
78 /* Reconfigure for forward operation */
79 void mpeg3bits_start_forward(mpeg3_bits_t
* stream
)
83 // If already at the bof, the buffer is already invalid.
84 if(stream
->demuxer
&& mpeg3bits_bof(stream
))
87 stream
->bit_number
= 0;
89 stream
->input_ptr
= 0;
90 mpeg3demux_read_char(stream
->demuxer
);
93 for(i
= 0; i
< stream
->bfr_size
; i
+= 8)
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
++;
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;
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;