summaryrefslogtreecommitdiff
path: root/embdrv/sbc/decoder/srce/decoder-private.c
blob: 686dd6aff2f93e4658815d33a9fcba209f8258e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/******************************************************************************
 *
 *  Copyright (C) 2014 The Android Open Source Project
 *  Copyright 2003 - 2004 Open Interface North America, Inc. All rights
 *                        reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

/*******************************************************************************
  $Revision: #1 $
 ******************************************************************************/

/**
@file
This file drives SBC decoding.

@ingroup codec_internal
*/

/**
@addtogroup codec_internal
@{
*/

#include <stdio.h>
#include "oi_bitstream.h"
#include "oi_codec_sbc_private.h"

OI_CHAR* const OI_Codec_Copyright =
    "Copyright 2002-2007 Open Interface North America, Inc. All rights "
    "reserved";

INLINE OI_STATUS internal_DecoderReset(OI_CODEC_SBC_DECODER_CONTEXT* context,
                                       uint32_t* decoderData,
                                       uint32_t decoderDataBytes,
                                       OI_BYTE maxChannels, OI_BYTE pcmStride,
                                       OI_BOOL enhanced) {
  OI_UINT i;
  OI_STATUS status;

  for (i = 0; i < sizeof(*context); i++) {
    ((char*)context)[i] = 0;
  }

#ifdef SBC_ENHANCED
  context->enhancedEnabled = enhanced ? TRUE : FALSE;
#else
  context->enhancedEnabled = FALSE;
  if (enhanced) {
    return OI_STATUS_INVALID_PARAMETERS;
  }
#endif

  status = OI_CODEC_SBC_Alloc(&context->common, decoderData, decoderDataBytes,
                              maxChannels, pcmStride);

  if (!OI_SUCCESS(status)) {
    return status;
  }

  context->common.codecInfo = OI_Codec_Copyright;
  context->common.maxBitneed = 0;
  context->limitFrameFormat = FALSE;
  OI_SBC_ExpandFrameFields(&context->common.frameInfo);

  /*PLATFORM_DECODER_RESET(context);*/

  return OI_OK;
}

/**
 * Read the SBC header up to but not including the joint stereo mask. The
 * syncword has already been examined, and the enhanced mode flag set, by
 * FindSyncword.
 */
INLINE void OI_SBC_ReadHeader(OI_CODEC_SBC_COMMON_CONTEXT* common,
                              const OI_BYTE* data) {
  OI_CODEC_SBC_FRAME_INFO* frame = &common->frameInfo;
  uint8_t d1;

  OI_ASSERT(data[0] == OI_SBC_SYNCWORD || data[0] == OI_SBC_ENHANCED_SYNCWORD);

  /* Avoid filling out all these strucutures if we already remember the values
   * from last time. Just in case we get a stream corresponding to data[1] ==
   * 0, DecoderReset is responsible for ensuring the lookup table entries have
   * already been populated
   */
  d1 = data[1];
  if (d1 != frame->cachedInfo) {
    frame->freqIndex = (d1 & (BIT7 | BIT6)) >> 6;
    frame->frequency = freq_values[frame->freqIndex];

    frame->blocks = (d1 & (BIT5 | BIT4)) >> 4;
    frame->nrof_blocks = block_values[frame->blocks];

    frame->mode = (d1 & (BIT3 | BIT2)) >> 2;
    frame->nrof_channels = channel_values[frame->mode];

    frame->alloc = (d1 & BIT1) >> 1;

    frame->subbands = (d1 & BIT0);
    frame->nrof_subbands = band_values[frame->subbands];

    frame->cachedInfo = d1;
  }
  /*
   * For decode, the bit allocator needs to know the bitpool value
   */
  frame->bitpool = data[2];
  frame->crc = data[3];
}

#define LOW(x) ((x)&0xf)
#define HIGH(x) ((x) >> 4)

/*
 * Read scalefactor values and prepare the bitstream for OI_SBC_ReadSamples
 */
PRIVATE void OI_SBC_ReadScalefactors(OI_CODEC_SBC_COMMON_CONTEXT* common,
                                     const OI_BYTE* b, OI_BITSTREAM* bs) {
  OI_UINT i = common->frameInfo.nrof_subbands * common->frameInfo.nrof_channels;
  int8_t* scale_factor = common->scale_factor;
  OI_UINT f;

  if (common->frameInfo.nrof_subbands == 8 ||
      common->frameInfo.mode != SBC_JOINT_STEREO) {
    if (common->frameInfo.mode == SBC_JOINT_STEREO) {
      common->frameInfo.join = *b++;
    } else {
      common->frameInfo.join = 0;
    }
    i /= 2;
    do {
      *scale_factor++ = HIGH(f = *b++);
      *scale_factor++ = LOW(f);
    } while (--i);
    /*
     * In this case we know that the scale factors end on a byte boundary so all
     * we need to do
     * is initialize the bitstream.
     */
    OI_BITSTREAM_ReadInit(bs, b);
  } else {
    OI_ASSERT(common->frameInfo.nrof_subbands == 4 &&
              common->frameInfo.mode == SBC_JOINT_STEREO);
    common->frameInfo.join = HIGH(f = *b++);
    i = (i - 1) / 2;
    do {
      *scale_factor++ = LOW(f);
      *scale_factor++ = HIGH(f = *b++);
    } while (--i);
    *scale_factor++ = LOW(f);
    /*
     * In 4-subband joint stereo mode, the joint stereo information ends on a
     * half-byte
     * boundary, so it's necessary to use the bitstream abstraction to read it,
     * since
     * OI_SBC_ReadSamples will need to pick up in mid-byte.
     */
    OI_BITSTREAM_ReadInit(bs, b);
    *scale_factor++ = OI_BITSTREAM_ReadUINT4Aligned(bs);
  }
}

/** Read quantized subband samples from the input bitstream and expand them. */
PRIVATE void OI_SBC_ReadSamples(OI_CODEC_SBC_DECODER_CONTEXT* context,
                                OI_BITSTREAM* global_bs) {
  OI_CODEC_SBC_COMMON_CONTEXT* common = &context->common;
  OI_UINT nrof_blocks = common->frameInfo.nrof_blocks;
  int32_t* RESTRICT s = common->subdata;
  uint8_t* ptr = global_bs->ptr.w;
  uint32_t value = global_bs->value;
  OI_UINT bitPtr = global_bs->bitPtr;

  const OI_UINT iter_count =
      common->frameInfo.nrof_channels * common->frameInfo.nrof_subbands / 4;
  do {
    OI_UINT i;
    for (i = 0; i < iter_count; ++i) {
      uint32_t sf_by4 = ((uint32_t*)common->scale_factor)[i];
      uint32_t bits_by4 = common->bits.uint32[i];
      OI_UINT n;
      for (n = 0; n < 4; ++n) {
        int32_t dequant;
        OI_UINT bits;
        OI_INT sf;

        if (OI_CPU_BYTE_ORDER == OI_LITTLE_ENDIAN_BYTE_ORDER) {
          bits = bits_by4 & 0xFF;
          bits_by4 >>= 8;
          sf = sf_by4 & 0xFF;
          sf_by4 >>= 8;
        } else {
          bits = (bits_by4 >> 24) & 0xFF;
          bits_by4 <<= 8;
          sf = (sf_by4 >> 24) & 0xFF;
          sf_by4 <<= 8;
        }
        if (bits) {
          uint32_t raw;
          OI_BITSTREAM_READUINT(raw, bits, ptr, value, bitPtr);
          dequant = OI_SBC_Dequant(raw, sf, bits);
        } else {
          dequant = 0;
        }
        *s++ = dequant;
      }
    }
  } while (--nrof_blocks);
}

/**
@}
*/