| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Bluetooth low-complexity, subband codec (SBC) | ||
| 3 | * | ||
| 4 | * Copyright (C) 2017 Aurelien Jacobs <aurel@gnuage.org> | ||
| 5 | * Copyright (C) 2012-2013 Intel Corporation | ||
| 6 | * Copyright (C) 2008-2010 Nokia Corporation | ||
| 7 | * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org> | ||
| 8 | * Copyright (C) 2004-2005 Henryk Ploetz <henryk@ploetzli.ch> | ||
| 9 | * Copyright (C) 2005-2008 Brad Midgley <bmidgley@xmission.com> | ||
| 10 | * | ||
| 11 | * This file is part of FFmpeg. | ||
| 12 | * | ||
| 13 | * FFmpeg is free software; you can redistribute it and/or | ||
| 14 | * modify it under the terms of the GNU Lesser General Public | ||
| 15 | * License as published by the Free Software Foundation; either | ||
| 16 | * version 2.1 of the License, or (at your option) any later version. | ||
| 17 | * | ||
| 18 | * FFmpeg is distributed in the hope that it will be useful, | ||
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 21 | * Lesser General Public License for more details. | ||
| 22 | * | ||
| 23 | * You should have received a copy of the GNU Lesser General Public | ||
| 24 | * License along with FFmpeg; if not, write to the Free Software | ||
| 25 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 26 | */ | ||
| 27 | |||
| 28 | /** | ||
| 29 | * @file | ||
| 30 | * SBC common functions for the encoder and decoder | ||
| 31 | */ | ||
| 32 | |||
| 33 | #include "sbc.h" | ||
| 34 | |||
| 35 | /* A2DP specification: Appendix B, page 69 */ | ||
| 36 | static const int sbc_offset4[4][4] = { | ||
| 37 | { -1, 0, 0, 0 }, | ||
| 38 | { -2, 0, 0, 1 }, | ||
| 39 | { -2, 0, 0, 1 }, | ||
| 40 | { -2, 0, 0, 1 } | ||
| 41 | }; | ||
| 42 | |||
| 43 | /* A2DP specification: Appendix B, page 69 */ | ||
| 44 | static const int sbc_offset8[4][8] = { | ||
| 45 | { -2, 0, 0, 0, 0, 0, 0, 1 }, | ||
| 46 | { -3, 0, 0, 0, 0, 0, 1, 2 }, | ||
| 47 | { -4, 0, 0, 0, 0, 0, 1, 2 }, | ||
| 48 | { -4, 0, 0, 0, 0, 0, 1, 2 } | ||
| 49 | }; | ||
| 50 | |||
| 51 | /* | ||
| 52 | * Calculates the CRC-8 of the first len bits in data | ||
| 53 | */ | ||
| 54 | 66445 | uint8_t ff_sbc_crc8(const AVCRC *ctx, const uint8_t *data, size_t len) | |
| 55 | { | ||
| 56 | 66445 | size_t byte_length = len >> 3; | |
| 57 | 66445 | int bit_length = len & 7; | |
| 58 | uint8_t crc; | ||
| 59 | |||
| 60 | 66445 | crc = av_crc(ctx, 0x0F, data, byte_length); | |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 8269 times.
✓ Branch 1 taken 58176 times.
|
66445 | if (bit_length) { |
| 63 | 8269 | uint8_t bits = data[byte_length]; | |
| 64 |
2/2✓ Branch 0 taken 33076 times.
✓ Branch 1 taken 8269 times.
|
41345 | while (bit_length--) { |
| 65 | 33076 | int8_t mask = bits ^ crc; | |
| 66 | 33076 | crc = (crc << 1) ^ ((mask >> 7) & 0x1D); | |
| 67 | 33076 | bits <<= 1; | |
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | 66445 | return crc; | |
| 72 | } | ||
| 73 | |||
| 74 | /* | ||
| 75 | * Code straight from the spec to calculate the bits array | ||
| 76 | * Takes a pointer to the frame in question and a pointer to the bits array | ||
| 77 | */ | ||
| 78 | 66445 | void ff_sbc_calculate_bits(const struct sbc_frame *frame, int (*bits)[8]) | |
| 79 | { | ||
| 80 | 66445 | int subbands = frame->subbands; | |
| 81 | 66445 | uint8_t sf = frame->frequency; | |
| 82 | |||
| 83 |
3/4✓ Branch 0 taken 24808 times.
✓ Branch 1 taken 41637 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24808 times.
|
108082 | if (frame->mode == MONO || frame->mode == DUAL_CHANNEL) { |
| 84 | int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice; | ||
| 85 | int ch, sb; | ||
| 86 | |||
| 87 |
2/2✓ Branch 0 taken 41637 times.
✓ Branch 1 taken 41637 times.
|
83274 | for (ch = 0; ch < frame->channels; ch++) { |
| 88 | 41637 | max_bitneed = 0; | |
| 89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41637 times.
|
41637 | if (frame->allocation == SNR) { |
| 90 | ✗ | for (sb = 0; sb < subbands; sb++) { | |
| 91 | ✗ | bitneed[ch][sb] = frame->scale_factor[ch][sb]; | |
| 92 | ✗ | if (bitneed[ch][sb] > max_bitneed) | |
| 93 | ✗ | max_bitneed = bitneed[ch][sb]; | |
| 94 | } | ||
| 95 | } else { | ||
| 96 |
2/2✓ Branch 0 taken 189092 times.
✓ Branch 1 taken 41637 times.
|
230729 | for (sb = 0; sb < subbands; sb++) { |
| 97 |
2/2✓ Branch 0 taken 102184 times.
✓ Branch 1 taken 86908 times.
|
189092 | if (frame->scale_factor[ch][sb] == 0) |
| 98 | 102184 | bitneed[ch][sb] = -5; | |
| 99 | else { | ||
| 100 |
2/2✓ Branch 0 taken 70132 times.
✓ Branch 1 taken 16776 times.
|
86908 | if (subbands == 4) |
| 101 | 70132 | loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb]; | |
| 102 | else | ||
| 103 | 16776 | loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb]; | |
| 104 |
2/2✓ Branch 0 taken 86544 times.
✓ Branch 1 taken 364 times.
|
86908 | if (loudness > 0) |
| 105 | 86544 | bitneed[ch][sb] = loudness / 2; | |
| 106 | else | ||
| 107 | 364 | bitneed[ch][sb] = loudness; | |
| 108 | } | ||
| 109 |
2/2✓ Branch 0 taken 43261 times.
✓ Branch 1 taken 145831 times.
|
189092 | if (bitneed[ch][sb] > max_bitneed) |
| 110 | 43261 | max_bitneed = bitneed[ch][sb]; | |
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | 41637 | bitcount = 0; | |
| 115 | 41637 | slicecount = 0; | |
| 116 | 41637 | bitslice = max_bitneed + 1; | |
| 117 | do { | ||
| 118 | 606645 | bitslice--; | |
| 119 | 606645 | bitcount += slicecount; | |
| 120 | 606645 | slicecount = 0; | |
| 121 |
2/2✓ Branch 0 taken 2753440 times.
✓ Branch 1 taken 606645 times.
|
3360085 | for (sb = 0; sb < subbands; sb++) { |
| 122 |
4/4✓ Branch 0 taken 1045944 times.
✓ Branch 1 taken 1707496 times.
✓ Branch 2 taken 1034870 times.
✓ Branch 3 taken 11074 times.
|
2753440 | if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16)) |
| 123 | 1034870 | slicecount++; | |
| 124 |
2/2✓ Branch 0 taken 188736 times.
✓ Branch 1 taken 1529834 times.
|
1718570 | else if (bitneed[ch][sb] == bitslice + 1) |
| 125 | 188736 | slicecount += 2; | |
| 126 | } | ||
| 127 |
2/2✓ Branch 0 taken 565008 times.
✓ Branch 1 taken 41637 times.
|
606645 | } while (bitcount + slicecount < frame->bitpool); |
| 128 | |||
| 129 |
2/2✓ Branch 0 taken 6418 times.
✓ Branch 1 taken 35219 times.
|
41637 | if (bitcount + slicecount == frame->bitpool) { |
| 130 | 6418 | bitcount += slicecount; | |
| 131 | 6418 | bitslice--; | |
| 132 | } | ||
| 133 | |||
| 134 |
2/2✓ Branch 0 taken 189092 times.
✓ Branch 1 taken 41637 times.
|
230729 | for (sb = 0; sb < subbands; sb++) { |
| 135 |
2/2✓ Branch 0 taken 4316 times.
✓ Branch 1 taken 184776 times.
|
189092 | if (bitneed[ch][sb] < bitslice + 2) |
| 136 | 4316 | bits[ch][sb] = 0; | |
| 137 | else { | ||
| 138 | 184776 | bits[ch][sb] = bitneed[ch][sb] - bitslice; | |
| 139 |
2/2✓ Branch 0 taken 310 times.
✓ Branch 1 taken 184466 times.
|
184776 | if (bits[ch][sb] > 16) |
| 140 | 310 | bits[ch][sb] = 16; | |
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 |
4/4✓ Branch 0 taken 92675 times.
✓ Branch 1 taken 40915 times.
✓ Branch 2 taken 91953 times.
✓ Branch 3 taken 722 times.
|
133590 | for (sb = 0; bitcount < frame->bitpool && |
| 145 | 91953 | sb < subbands; sb++) { | |
| 146 |
4/4✓ Branch 0 taken 89185 times.
✓ Branch 1 taken 2768 times.
✓ Branch 2 taken 78451 times.
✓ Branch 3 taken 10734 times.
|
91953 | if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) { |
| 147 | 78451 | bits[ch][sb]++; | |
| 148 | 78451 | bitcount++; | |
| 149 |
4/4✓ Branch 0 taken 2768 times.
✓ Branch 1 taken 10734 times.
✓ Branch 2 taken 1784 times.
✓ Branch 3 taken 984 times.
|
13502 | } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) { |
| 150 | 1784 | bits[ch][sb] = 2; | |
| 151 | 1784 | bitcount += 2; | |
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 |
3/4✓ Branch 0 taken 722 times.
✓ Branch 1 taken 41637 times.
✓ Branch 2 taken 722 times.
✗ Branch 3 not taken.
|
42359 | for (sb = 0; bitcount < frame->bitpool && |
| 156 | 722 | sb < subbands; sb++) { | |
| 157 |
1/2✓ Branch 0 taken 722 times.
✗ Branch 1 not taken.
|
722 | if (bits[ch][sb] < 16) { |
| 158 | 722 | bits[ch][sb]++; | |
| 159 | 722 | bitcount++; | |
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | } | ||
| 164 | |||
| 165 |
3/4✓ Branch 0 taken 12404 times.
✓ Branch 1 taken 12404 times.
✓ Branch 2 taken 12404 times.
✗ Branch 3 not taken.
|
24808 | } else if (frame->mode == STEREO || frame->mode == JOINT_STEREO) { |
| 166 | int bitneed[2][8], loudness, max_bitneed, bitcount, slicecount, bitslice; | ||
| 167 | int ch, sb; | ||
| 168 | |||
| 169 | 24808 | max_bitneed = 0; | |
| 170 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24808 times.
|
24808 | if (frame->allocation == SNR) { |
| 171 | ✗ | for (ch = 0; ch < 2; ch++) { | |
| 172 | ✗ | for (sb = 0; sb < subbands; sb++) { | |
| 173 | ✗ | bitneed[ch][sb] = frame->scale_factor[ch][sb]; | |
| 174 | ✗ | if (bitneed[ch][sb] > max_bitneed) | |
| 175 | ✗ | max_bitneed = bitneed[ch][sb]; | |
| 176 | } | ||
| 177 | } | ||
| 178 | } else { | ||
| 179 |
2/2✓ Branch 0 taken 49616 times.
✓ Branch 1 taken 24808 times.
|
74424 | for (ch = 0; ch < 2; ch++) { |
| 180 |
2/2✓ Branch 0 taken 264624 times.
✓ Branch 1 taken 49616 times.
|
314240 | for (sb = 0; sb < subbands; sb++) { |
| 181 |
2/2✓ Branch 0 taken 164686 times.
✓ Branch 1 taken 99938 times.
|
264624 | if (frame->scale_factor[ch][sb] == 0) |
| 182 | 164686 | bitneed[ch][sb] = -5; | |
| 183 | else { | ||
| 184 |
2/2✓ Branch 0 taken 60839 times.
✓ Branch 1 taken 39099 times.
|
99938 | if (subbands == 4) |
| 185 | 60839 | loudness = frame->scale_factor[ch][sb] - sbc_offset4[sf][sb]; | |
| 186 | else | ||
| 187 | 39099 | loudness = frame->scale_factor[ch][sb] - sbc_offset8[sf][sb]; | |
| 188 |
2/2✓ Branch 0 taken 98918 times.
✓ Branch 1 taken 1020 times.
|
99938 | if (loudness > 0) |
| 189 | 98918 | bitneed[ch][sb] = loudness / 2; | |
| 190 | else | ||
| 191 | 1020 | bitneed[ch][sb] = loudness; | |
| 192 | } | ||
| 193 |
2/2✓ Branch 0 taken 29720 times.
✓ Branch 1 taken 234904 times.
|
264624 | if (bitneed[ch][sb] > max_bitneed) |
| 194 | 29720 | max_bitneed = bitneed[ch][sb]; | |
| 195 | } | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | 24808 | bitcount = 0; | |
| 200 | 24808 | slicecount = 0; | |
| 201 | 24808 | bitslice = max_bitneed + 1; | |
| 202 | do { | ||
| 203 | 217498 | bitslice--; | |
| 204 | 217498 | bitcount += slicecount; | |
| 205 | 217498 | slicecount = 0; | |
| 206 |
2/2✓ Branch 0 taken 434996 times.
✓ Branch 1 taken 217498 times.
|
652494 | for (ch = 0; ch < 2; ch++) { |
| 207 |
2/2✓ Branch 0 taken 2182408 times.
✓ Branch 1 taken 434996 times.
|
2617404 | for (sb = 0; sb < subbands; sb++) { |
| 208 |
3/4✓ Branch 0 taken 374950 times.
✓ Branch 1 taken 1807458 times.
✓ Branch 2 taken 374950 times.
✗ Branch 3 not taken.
|
2182408 | if ((bitneed[ch][sb] > bitslice + 1) && (bitneed[ch][sb] < bitslice + 16)) |
| 209 | 374950 | slicecount++; | |
| 210 |
2/2✓ Branch 0 taken 123120 times.
✓ Branch 1 taken 1684338 times.
|
1807458 | else if (bitneed[ch][sb] == bitslice + 1) |
| 211 | 123120 | slicecount += 2; | |
| 212 | } | ||
| 213 | } | ||
| 214 |
2/2✓ Branch 0 taken 192690 times.
✓ Branch 1 taken 24808 times.
|
217498 | } while (bitcount + slicecount < frame->bitpool); |
| 215 | |||
| 216 |
2/2✓ Branch 0 taken 8947 times.
✓ Branch 1 taken 15861 times.
|
24808 | if (bitcount + slicecount == frame->bitpool) { |
| 217 | 8947 | bitcount += slicecount; | |
| 218 | 8947 | bitslice--; | |
| 219 | } | ||
| 220 | |||
| 221 |
2/2✓ Branch 0 taken 49616 times.
✓ Branch 1 taken 24808 times.
|
74424 | for (ch = 0; ch < 2; ch++) { |
| 222 |
2/2✓ Branch 0 taken 264624 times.
✓ Branch 1 taken 49616 times.
|
314240 | for (sb = 0; sb < subbands; sb++) { |
| 223 |
2/2✓ Branch 0 taken 183916 times.
✓ Branch 1 taken 80708 times.
|
264624 | if (bitneed[ch][sb] < bitslice + 2) { |
| 224 | 183916 | bits[ch][sb] = 0; | |
| 225 | } else { | ||
| 226 | 80708 | bits[ch][sb] = bitneed[ch][sb] - bitslice; | |
| 227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 80708 times.
|
80708 | if (bits[ch][sb] > 16) |
| 228 | ✗ | bits[ch][sb] = 16; | |
| 229 | } | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | 24808 | ch = 0; | |
| 234 | 24808 | sb = 0; | |
| 235 |
2/2✓ Branch 0 taken 77674 times.
✓ Branch 1 taken 22098 times.
|
99772 | while (bitcount < frame->bitpool) { |
| 236 |
3/4✓ Branch 0 taken 34835 times.
✓ Branch 1 taken 42839 times.
✓ Branch 2 taken 34835 times.
✗ Branch 3 not taken.
|
77674 | if ((bits[ch][sb] >= 2) && (bits[ch][sb] < 16)) { |
| 237 | 34835 | bits[ch][sb]++; | |
| 238 | 34835 | bitcount++; | |
| 239 |
4/4✓ Branch 0 taken 29001 times.
✓ Branch 1 taken 13838 times.
✓ Branch 2 taken 20129 times.
✓ Branch 3 taken 8872 times.
|
42839 | } else if ((bitneed[ch][sb] == bitslice + 1) && (frame->bitpool > bitcount + 1)) { |
| 240 | 20129 | bits[ch][sb] = 2; | |
| 241 | 20129 | bitcount += 2; | |
| 242 | } | ||
| 243 |
2/2✓ Branch 0 taken 35081 times.
✓ Branch 1 taken 42593 times.
|
77674 | if (ch == 1) { |
| 244 | 35081 | ch = 0; | |
| 245 | 35081 | sb++; | |
| 246 |
2/2✓ Branch 0 taken 2710 times.
✓ Branch 1 taken 32371 times.
|
35081 | if (sb >= subbands) |
| 247 | 2710 | break; | |
| 248 | } else | ||
| 249 | 42593 | ch = 1; | |
| 250 | } | ||
| 251 | |||
| 252 | 24808 | ch = 0; | |
| 253 | 24808 | sb = 0; | |
| 254 |
2/2✓ Branch 0 taken 2696 times.
✓ Branch 1 taken 24808 times.
|
27504 | while (bitcount < frame->bitpool) { |
| 255 |
1/2✓ Branch 0 taken 2696 times.
✗ Branch 1 not taken.
|
2696 | if (bits[ch][sb] < 16) { |
| 256 | 2696 | bits[ch][sb]++; | |
| 257 | 2696 | bitcount++; | |
| 258 | } | ||
| 259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2696 times.
|
2696 | if (ch == 1) { |
| 260 | ✗ | ch = 0; | |
| 261 | ✗ | sb++; | |
| 262 | ✗ | if (sb >= subbands) | |
| 263 | ✗ | break; | |
| 264 | } else | ||
| 265 | 2696 | ch = 1; | |
| 266 | } | ||
| 267 | |||
| 268 | } | ||
| 269 | |||
| 270 | 66445 | } | |
| 271 |