/* The order of speech bits is specified by 3GPP TS 26.101. */
static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf, int buf_size)
/* The order of speech bits is specified by 3GPP TS 26.101. */
static enum Mode unpack_bitstream(AMRContext *p, const uint8_t *buf, int buf_size)
{
    GetBitContext gb;
    enum Mode mode;
    init_get_bits(&gb, buf, buf_size * 8);
    skip_bits(&gb, 1);
    mode = get_bits(&gb, 4);
    p->bad_frame_indicator = !get_bits1(&gb);
    skip_bits(&gb, 2);
    if (mode < MODE_DTX) {
        uint16_t *data = (uint16_t *)&p->frame;
        const uint8_t *order = amr_unpacking_bitmaps_per_mode[mode];
        int field_size;
        memset(&p->frame, 0, sizeof(AMRNBFrame));
        buf++;
        while ((field_size = *order++)) {
            int field = 0;
            int field_offset = *order++;
            while (field_size--) {
               int bit = *order++;
               field <<= 1;
               field |= buf[bit >> 3] >> (bit & 7) & 1;
            }
            data[field_offset] = field;
        }
    }
    return mode;
}