uint32_t huffman_decode_one(struct huffman_decoder* decoder, struct bitstream* bitbuf) { /* peek ahead to get maxbits worth of data */ uint32_t bits = bitstream_peek(bitbuf, decoder->maxbits); /* look it up, then remove the actual number of bits for this code */ lookup_value lookup = decoder->lookup[bits]; bitstream_remove(bitbuf, lookup & 0x1f); /* return the value */ return lookup >> 5; }
int main(void) { int i, ret = 0; uint8_t *temp; PutBitContext pb; BitstreamContext bc; temp = av_malloc(SIZE); if (!temp) return 2; init_put_bits(&pb, temp, SIZE); for (i = 0; i < COUNT; i++) set_ue_golomb(&pb, i); flush_put_bits(&pb); bitstream_init8(&bc, temp, SIZE); for (i = 0; i < COUNT; i++) { int j, s = bitstream_peek(&bc, 25); j = get_ue_golomb(&bc); if (j != i) { fprintf(stderr, "get_ue_golomb: expected %d, got %d. bits: %7x\n", i, j, s); ret = 1; } } #define EXTEND(i) (i << 3 | i & 7) init_put_bits(&pb, temp, SIZE); for (i = 0; i < COUNT; i++) set_ue_golomb(&pb, EXTEND(i)); flush_put_bits(&pb); bitstream_init8(&bc, temp, SIZE); for (i = 0; i < COUNT; i++) { int j, s = bitstream_peek(&bc, 32); j = get_ue_golomb_long(&bc); if (j != EXTEND(i)) { fprintf(stderr, "get_ue_golomb_long: expected %d, got %d. " "bits: %8x\n", EXTEND(i), j, s); ret = 1; } } init_put_bits(&pb, temp, SIZE); for (i = 0; i < COUNT; i++) set_se_golomb(&pb, i - COUNT / 2); flush_put_bits(&pb); bitstream_init8(&bc, temp, SIZE); for (i = 0; i < COUNT; i++) { int j, s = bitstream_peek(&bc, 25); j = get_se_golomb(&bc); if (j != i - COUNT / 2) { fprintf(stderr, "get_se_golomb: expected %d, got %d. bits: %7x\n", i - COUNT / 2, j, s); ret = 1; } } av_free(temp); return ret; }