|
Packit |
015a35 |
/*
|
|
Packit |
015a35 |
* Copyright (C) 2000, 2001, 2002, 2003 HÃ¥kan Hjort <d95hjort@dtek.chalmers.se>
|
|
Packit |
015a35 |
*
|
|
Packit |
015a35 |
* This file is part of libdvdread.
|
|
Packit |
015a35 |
*
|
|
Packit |
015a35 |
* libdvdread is free software; you can redistribute it and/or modify
|
|
Packit |
015a35 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
015a35 |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit |
015a35 |
* (at your option) any later version.
|
|
Packit |
015a35 |
*
|
|
Packit |
015a35 |
* libdvdread is distributed in the hope that it will be useful,
|
|
Packit |
015a35 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
015a35 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
015a35 |
* GNU General Public License for more details.
|
|
Packit |
015a35 |
*
|
|
Packit |
015a35 |
* You should have received a copy of the GNU General Public License along
|
|
Packit |
015a35 |
* with libdvdread; if not, write to the Free Software Foundation, Inc.,
|
|
Packit |
015a35 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
Packit |
015a35 |
*/
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
#include "config.h"
|
|
Packit |
015a35 |
#include <stdio.h>
|
|
Packit |
015a35 |
#include <stdlib.h>
|
|
Packit |
015a35 |
#include <string.h>
|
|
Packit |
015a35 |
#include <inttypes.h>
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
#include "dvdread/bitreader.h"
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
int dvdread_getbits_init(getbits_state_t *state, uint8_t *start) {
|
|
Packit |
015a35 |
if ((state == NULL) || (start == NULL)) return 0;
|
|
Packit |
015a35 |
state->start = start;
|
|
Packit |
015a35 |
state->bit_position = 0;
|
|
Packit |
015a35 |
state->byte_position = 0;
|
|
Packit |
015a35 |
state->byte = start[0];
|
|
Packit |
015a35 |
return 1;
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
/* Non-optimized getbits. */
|
|
Packit |
015a35 |
/* This can easily be optimized for particular platforms. */
|
|
Packit |
015a35 |
uint32_t dvdread_getbits(getbits_state_t *state, uint32_t number_of_bits) {
|
|
Packit |
015a35 |
uint32_t result=0;
|
|
Packit |
015a35 |
uint8_t byte=0;
|
|
Packit |
015a35 |
if (number_of_bits > 32) {
|
|
Packit |
015a35 |
printf("Number of bits > 32 in getbits\n");
|
|
Packit |
015a35 |
abort();
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
if ((state->bit_position) > 0) { /* Last getbits left us in the middle of a byte. */
|
|
Packit |
015a35 |
if (number_of_bits > (8-state->bit_position)) { /* this getbits will span 2 or more bytes. */
|
|
Packit |
015a35 |
byte = state->byte;
|
|
Packit |
015a35 |
byte = byte >> (state->bit_position);
|
|
Packit |
015a35 |
result = byte;
|
|
Packit |
015a35 |
number_of_bits -= (8-state->bit_position);
|
|
Packit |
015a35 |
state->bit_position = 0;
|
|
Packit |
015a35 |
state->byte_position++;
|
|
Packit |
015a35 |
state->byte = state->start[state->byte_position];
|
|
Packit |
015a35 |
} else {
|
|
Packit |
015a35 |
byte=state->byte;
|
|
Packit |
015a35 |
state->byte = state->byte << number_of_bits;
|
|
Packit |
015a35 |
byte = byte >> (8 - number_of_bits);
|
|
Packit |
015a35 |
result = byte;
|
|
Packit |
015a35 |
state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 8 */
|
|
Packit |
015a35 |
if (state->bit_position == 8) {
|
|
Packit |
015a35 |
state->bit_position = 0;
|
|
Packit |
015a35 |
state->byte_position++;
|
|
Packit |
015a35 |
state->byte = state->start[state->byte_position];
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
number_of_bits = 0;
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
if ((state->bit_position) == 0) {
|
|
Packit |
015a35 |
while (number_of_bits > 7) {
|
|
Packit |
015a35 |
result = (result << 8) + state->byte;
|
|
Packit |
015a35 |
state->byte_position++;
|
|
Packit |
015a35 |
state->byte = state->start[state->byte_position];
|
|
Packit |
015a35 |
number_of_bits -= 8;
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
if (number_of_bits > 0) { /* number_of_bits < 8 */
|
|
Packit |
015a35 |
byte = state->byte;
|
|
Packit |
015a35 |
state->byte = state->byte << number_of_bits;
|
|
Packit |
015a35 |
state->bit_position += number_of_bits; /* Here it is impossible for bit_position > 7 */
|
|
Packit |
015a35 |
byte = byte >> (8 - number_of_bits);
|
|
Packit |
015a35 |
result = (result << number_of_bits) + byte;
|
|
Packit |
015a35 |
number_of_bits = 0;
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
return result;
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
#if 0 /* TODO: optimized versions not yet used */
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
/* WARNING: This function can only be used on a byte boundary.
|
|
Packit |
015a35 |
No checks are made that we are in fact on a byte boundary.
|
|
Packit |
015a35 |
*/
|
|
Packit |
015a35 |
uint16_t dvdread_get16bits(getbits_state_t *state) {
|
|
Packit |
015a35 |
uint16_t result;
|
|
Packit |
015a35 |
state->byte_position++;
|
|
Packit |
015a35 |
result = (state->byte << 8) + state->start[state->byte_position++];
|
|
Packit |
015a35 |
state->byte = state->start[state->byte_position];
|
|
Packit |
015a35 |
return result;
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
/* WARNING: This function can only be used on a byte boundary.
|
|
Packit |
015a35 |
No checks are made that we are in fact on a byte boundary.
|
|
Packit |
015a35 |
*/
|
|
Packit |
015a35 |
uint32_t dvdread_get32bits(getbits_state_t *state) {
|
|
Packit |
015a35 |
uint32_t result;
|
|
Packit |
015a35 |
state->byte_position++;
|
|
Packit |
015a35 |
result = (state->byte << 8) + state->start[state->byte_position++];
|
|
Packit |
015a35 |
result = (result << 8) + state->start[state->byte_position++];
|
|
Packit |
015a35 |
result = (result << 8) + state->start[state->byte_position++];
|
|
Packit |
015a35 |
state->byte = state->start[state->byte_position];
|
|
Packit |
015a35 |
return result;
|
|
Packit |
015a35 |
}
|
|
Packit |
015a35 |
|
|
Packit |
015a35 |
#endif
|