static void copy_bits(u_int8_t* src, /* Base pointer to source. */ size_t soffs, /* Bit offset for source relative to src. */ int sdir, /* Direction: 1 (forward) or -1 (backward). */ u_int8_t* dst, /* Base pointer to destination. */ size_t doffs, /* Bit offset for destination relative to dst. */ int ddir, /* Direction: 1 (forward) or -1 (backward). */ size_t n) /* Number of bits to copy. */ { u_int32_t lmask; u_int32_t rmask; u_int32_t count; u_int32_t deoffs; if (n == 0) { return; } src += sdir*BYTE_OFFSET(soffs); dst += ddir*BYTE_OFFSET(doffs); soffs = BIT_OFFSET(soffs); doffs = BIT_OFFSET(doffs); deoffs = BIT_OFFSET(doffs+n); lmask = (doffs) ? MAKE_MASK(8-doffs) : 0; rmask = (deoffs) ? (MAKE_MASK(deoffs)<<(8-deoffs)) : 0; /* * Take care of the case that all bits are in the same byte. */ if (doffs+n < 8) { /* All bits are in the same byte */ lmask = (lmask & rmask) ? (lmask & rmask) : (lmask | rmask); if (soffs == doffs) { *dst = MASK_BITS(*src,*dst,lmask); } else if (soffs > doffs) { u_int32_t bits = (*src << (soffs-doffs)); if (soffs+n > 8) { src += sdir; bits |= (*src >> (8-(soffs-doffs))); }
/* decode all macroblocks of the current picture */ static void i_picture(APEG_LAYER *layer) { const int MBAmax = layer->mb_cols*layer->mb_rows; int MBA, MBAinc; int dc_dct_pred[3]; int bx, by; unsigned int code; slice_start: dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0; code = apeg_start_code(layer); if(code < SLICE_START_CODE_MIN || code > SLICE_START_CODE_MAX) return; apeg_flush_bits32(layer); slice_header(layer); bx = get_mba_inc(layer); by = (code&255) - 1; MBA = by*layer->mb_cols + bx; bx <<= 4; by <<= 4; block_start: switch(show_bits(layer, 2)) { case 0: goto slice_start; case 1: layer->quantizer_scale = apeg_get_bits(layer, 7) & MASK_BITS(5); break; default: apeg_flush_bits1(layer); } apeg_decode_intra_blocks(layer, dc_dct_pred); apeg_fast_idct(apeg_block[0]); apeg_fast_idct(apeg_block[1]); apeg_fast_idct(apeg_block[2]); apeg_fast_idct(apeg_block[3]); apeg_fast_idct(apeg_block[4]); apeg_fast_idct(apeg_block[5]); Move_Blocks(layer, bx, by); if(++MBA >= MBAmax) return; if(show_bits(layer, 24) == 1) goto slice_start; MBAinc = get_mba_inc(layer); if(MBAinc == 0) { if((bx += 16) == layer->coded_width) { bx = 0; by += 16; } goto block_start; } dc_dct_pred[0] = dc_dct_pred[1] = dc_dct_pred[2] = 0; MBA += MBAinc; if(MBA >= MBAmax) return; bx = (MBA%layer->mb_cols) << 4; by = (MBA/layer->mb_cols) << 4; goto block_start; }
/* * Take care of the case that all bits are in the same byte. */ if (doffs+n < 8) { /* All bits are in the same byte */ lmask = (lmask & rmask) ? (lmask & rmask) : (lmask | rmask); if (soffs == doffs) { *dst = MASK_BITS(*src,*dst,lmask); } else if (soffs > doffs) { u_int32_t bits = (*src << (soffs-doffs)); if (soffs+n > 8) { src += sdir; bits |= (*src >> (8-(soffs-doffs))); } *dst = MASK_BITS(bits,*dst,lmask); } else { *dst = MASK_BITS((*src >> (doffs-soffs)),*dst,lmask); } return; /* We are done! */ } /* * At this point, we know that the bits are in 2 or more bytes. */ count = ((lmask) ? (n - (8 - doffs)) : n) >> 3; if (soffs == doffs) { /* * The bits are aligned in the same way. We can just copy the bytes