00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "common.h"
00034 #include "structs.h"
00035
00036 #include <stdlib.h>
00037 #include <string.h>
00038
00039 #include "specrec.h"
00040 #include "huffman.h"
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #ifdef ERROR_RESILIENCE
00056
00057
00058
00059 #define NUM_CB 6
00060 #define NUM_CB_ER 22
00061 #define MAX_CB 32
00062 #define VCB11_FIRST 16
00063 #define VCB11_LAST 31
00064
00065 static const uint8_t PreSortCB_STD[NUM_CB] =
00066 { 11, 9, 7, 5, 3, 1};
00067
00068 static const uint8_t PreSortCB_ER[NUM_CB_ER] =
00069 { 11, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 9, 7, 5, 3, 1};
00070
00071
00072
00073 static const uint8_t maxCwLen[MAX_CB] = {0, 11, 9, 20, 16, 13, 11, 14, 12, 17, 14, 49,
00074 0, 0, 0, 0, 14, 17, 21, 21, 25, 25, 29, 29, 29, 29, 33, 33, 33, 37, 37, 41};
00075
00076 #define segmentWidth(cb) min(maxCwLen[cb], ics->length_of_longest_codeword)
00077
00078
00079 static const uint8_t S[] = {1, 2, 4, 8, 16};
00080 static const uint32_t B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
00081
00082 typedef struct
00083 {
00084 uint8_t cb;
00085 uint8_t decoded;
00086 uint16_t sp_offset;
00087 bits_t bits;
00088 } codeword_t;
00089
00090
00091
00092 static uint32_t rewrev_word(uint32_t v, const uint8_t len)
00093 {
00094
00095 v = ((v >> S[0]) & B[0]) | ((v << S[0]) & ~B[0]);
00096 v = ((v >> S[1]) & B[1]) | ((v << S[1]) & ~B[1]);
00097 v = ((v >> S[2]) & B[2]) | ((v << S[2]) & ~B[2]);
00098 v = ((v >> S[3]) & B[3]) | ((v << S[3]) & ~B[3]);
00099 v = ((v >> S[4]) & B[4]) | ((v << S[4]) & ~B[4]);
00100
00101
00102 v >>= (32 - len);
00103
00104 return v;
00105 }
00106
00107
00108 static void rewrev_lword(uint32_t *hi, uint32_t *lo, const uint8_t len)
00109 {
00110 if (len <= 32) {
00111 *hi = 0;
00112 *lo = rewrev_word(*lo, len);
00113 } else
00114 {
00115 uint32_t t = *hi, v = *lo;
00116
00117
00118 v = ((v >> S[0]) & B[0]) | ((v << S[0]) & ~B[0]);
00119 t = ((t >> S[0]) & B[0]) | ((t << S[0]) & ~B[0]);
00120 v = ((v >> S[1]) & B[1]) | ((v << S[1]) & ~B[1]);
00121 t = ((t >> S[1]) & B[1]) | ((t << S[1]) & ~B[1]);
00122 v = ((v >> S[2]) & B[2]) | ((v << S[2]) & ~B[2]);
00123 t = ((t >> S[2]) & B[2]) | ((t << S[2]) & ~B[2]);
00124 v = ((v >> S[3]) & B[3]) | ((v << S[3]) & ~B[3]);
00125 t = ((t >> S[3]) & B[3]) | ((t << S[3]) & ~B[3]);
00126 v = ((v >> S[4]) & B[4]) | ((v << S[4]) & ~B[4]);
00127 t = ((t >> S[4]) & B[4]) | ((t << S[4]) & ~B[4]);
00128
00129
00130
00131
00132 *lo = (t >> (64 - len)) | (v << (len - 32));
00133 *hi = v >> (64 - len);
00134 }
00135 }
00136
00137
00138
00139 static void rewrev_bits(bits_t *bits)
00140 {
00141 if (bits->len == 0) return;
00142 rewrev_lword(&bits->bufb, &bits->bufa, bits->len);
00143 }
00144
00145
00146
00147 static void concat_bits(bits_t *b, bits_t *a)
00148 {
00149 uint32_t bl, bh, al, ah;
00150
00151 if (a->len == 0) return;
00152
00153 al = a->bufa;
00154 ah = a->bufb;
00155
00156 if (b->len > 32)
00157 {
00158
00159 bl = b->bufa;
00160 bh = b->bufb & ((1 << (b->len-32)) - 1);
00161
00162 ah = al << (b->len - 32);
00163 al = 0;
00164 } else {
00165 bl = b->bufa & ((1 << (b->len)) - 1);
00166 bh = 0;
00167 ah = (ah << (b->len)) | (al >> (32 - b->len));
00168 al = al << b->len;
00169 }
00170
00171
00172 b->bufa = bl | al;
00173 b->bufb = bh | ah;
00174
00175 b->len += a->len;
00176 }
00177
00178 uint8_t is_good_cb(uint8_t this_CB, uint8_t this_sec_CB)
00179 {
00180
00181 if ((this_sec_CB > ZERO_HCB && this_sec_CB <= ESC_HCB) || (this_sec_CB >= VCB11_FIRST && this_sec_CB <= VCB11_LAST))
00182 {
00183 if (this_CB < ESC_HCB)
00184 {
00185
00186 return ((this_sec_CB == this_CB) || (this_sec_CB == this_CB + 1));
00187 } else
00188 {
00189
00190 return (this_sec_CB == this_CB);
00191 }
00192 }
00193 return 0;
00194 }
00195
00196 void read_segment(bits_t *segment, uint8_t segwidth, bitfile *ld)
00197 {
00198 segment->len = segwidth;
00199
00200 if (segwidth > 32)
00201 {
00202 segment->bufb = faad_getbits(ld, segwidth - 32);
00203 segment->bufa = faad_getbits(ld, 32);
00204
00205 } else {
00206 segment->bufa = faad_getbits(ld, segwidth);
00207 segment->bufb = 0;
00208 }
00209 }
00210
00211 void fill_in_codeword(codeword_t *codeword, uint16_t index, uint16_t sp, uint8_t cb)
00212 {
00213 codeword[index].sp_offset = sp;
00214 codeword[index].cb = cb;
00215 codeword[index].decoded = 0;
00216 codeword[index].bits.len = 0;
00217 }
00218
00219 uint8_t reordered_spectral_data(NeAACDecHandle hDecoder, ic_stream *ics,
00220 bitfile *ld, int16_t *spectral_data)
00221 {
00222 uint16_t PCWs_done;
00223 uint16_t numberOfSegments, numberOfSets, numberOfCodewords;
00224
00225 codeword_t codeword[512];
00226 bits_t segment[512];
00227
00228 uint16_t sp_offset[8];
00229 uint16_t g, i, sortloop, set, bitsread;
00230 uint16_t bitsleft, codewordsleft;
00231 uint8_t w_idx, sfb, this_CB, last_CB, this_sec_CB;
00232
00233 const uint16_t nshort = hDecoder->frameLength/8;
00234 const uint16_t sp_data_len = ics->length_of_reordered_spectral_data;
00235
00236 const uint8_t *PreSortCb;
00237
00238
00239 if (sp_data_len == 0)
00240 return 0;
00241
00242
00243 if (ics->length_of_longest_codeword == 0)
00244 return 10;
00245
00246 if (sp_data_len < ics->length_of_longest_codeword)
00247 return 10;
00248
00249 sp_offset[0] = 0;
00250 for (g = 1; g < ics->num_window_groups; g++)
00251 {
00252 sp_offset[g] = sp_offset[g-1] + nshort*ics->window_group_length[g-1];
00253 }
00254
00255 PCWs_done = 0;
00256 numberOfSegments = 0;
00257 numberOfCodewords = 0;
00258 bitsread = 0;
00259
00260
00261 if (hDecoder->aacSectionDataResilienceFlag)
00262 {
00263 PreSortCb = PreSortCB_ER;
00264 last_CB = NUM_CB_ER;
00265 } else
00266 {
00267 PreSortCb = PreSortCB_STD;
00268 last_CB = NUM_CB;
00269 }
00270
00271
00272 for (sortloop = 0; sortloop < last_CB; sortloop++)
00273 {
00274
00275 this_CB = PreSortCb[sortloop];
00276
00277
00278 for (sfb = 0; sfb < ics->max_sfb; sfb++)
00279 {
00280
00281 for (w_idx = 0; 4*w_idx < (ics->swb_offset[sfb+1] - ics->swb_offset[sfb]); w_idx++)
00282 {
00283 for(g = 0; g < ics->num_window_groups; g++)
00284 {
00285 for (i = 0; i < ics->num_sec[g]; i++)
00286 {
00287
00288 if ((ics->sect_start[g][i] <= sfb) && (ics->sect_end[g][i] > sfb))
00289 {
00290
00291 this_sec_CB = ics->sect_cb[g][i];
00292
00293 if (is_good_cb(this_CB, this_sec_CB))
00294 {
00295
00296 uint16_t sect_sfb_size = ics->sect_sfb_offset[g][sfb+1] - ics->sect_sfb_offset[g][sfb];
00297 uint8_t inc = (this_sec_CB < FIRST_PAIR_HCB) ? QUAD_LEN : PAIR_LEN;
00298 uint16_t group_cws_count = (4*ics->window_group_length[g])/inc;
00299 uint8_t segwidth = segmentWidth(this_sec_CB);
00300 uint16_t cws;
00301
00302
00303 for (cws = 0; (cws < group_cws_count) && ((cws + w_idx*group_cws_count) < sect_sfb_size); cws++)
00304 {
00305 uint16_t sp = sp_offset[g] + ics->sect_sfb_offset[g][sfb] + inc * (cws + w_idx*group_cws_count);
00306
00307
00308 if (!PCWs_done)
00309 {
00310
00311 if (bitsread + segwidth <= sp_data_len)
00312 {
00313 read_segment(&segment[numberOfSegments], segwidth, ld);
00314 bitsread += segwidth;
00315
00316 huffman_spectral_data_2(this_sec_CB, &segment[numberOfSegments], &spectral_data[sp]);
00317
00318
00319 rewrev_bits(&segment[numberOfSegments]);
00320
00321 numberOfSegments++;
00322 } else {
00323
00324
00325
00326 if (bitsread < sp_data_len)
00327 {
00328 const uint8_t additional_bits = sp_data_len - bitsread;
00329
00330 read_segment(&segment[numberOfSegments], additional_bits, ld);
00331 segment[numberOfSegments].len += segment[numberOfSegments-1].len;
00332 rewrev_bits(&segment[numberOfSegments]);
00333
00334 if (segment[numberOfSegments-1].len > 32)
00335 {
00336 segment[numberOfSegments-1].bufb = segment[numberOfSegments].bufb +
00337 showbits_hcr(&segment[numberOfSegments-1], segment[numberOfSegments-1].len - 32);
00338 segment[numberOfSegments-1].bufa = segment[numberOfSegments].bufa +
00339 showbits_hcr(&segment[numberOfSegments-1], 32);
00340 } else {
00341 segment[numberOfSegments-1].bufa = segment[numberOfSegments].bufa +
00342 showbits_hcr(&segment[numberOfSegments-1], segment[numberOfSegments-1].len);
00343 segment[numberOfSegments-1].bufb = segment[numberOfSegments].bufb;
00344 }
00345 segment[numberOfSegments-1].len += additional_bits;
00346 }
00347 bitsread = sp_data_len;
00348 PCWs_done = 1;
00349
00350 fill_in_codeword(codeword, 0, sp, this_sec_CB);
00351 }
00352 } else {
00353 fill_in_codeword(codeword, numberOfCodewords - numberOfSegments, sp, this_sec_CB);
00354 }
00355 numberOfCodewords++;
00356 }
00357 }
00358 }
00359 }
00360 }
00361 }
00362 }
00363 }
00364
00365 if (numberOfSegments == 0)
00366 return 10;
00367
00368 numberOfSets = numberOfCodewords / numberOfSegments;
00369
00370
00371 for (set = 1; set <= numberOfSets; set++)
00372 {
00373 uint16_t trial;
00374
00375 for (trial = 0; trial < numberOfSegments; trial++)
00376 {
00377 uint16_t codewordBase;
00378
00379 for (codewordBase = 0; codewordBase < numberOfSegments; codewordBase++)
00380 {
00381 const uint16_t segment_idx = (trial + codewordBase) % numberOfSegments;
00382 const uint16_t codeword_idx = codewordBase + set*numberOfSegments - numberOfSegments;
00383
00384
00385 if (codeword_idx >= numberOfCodewords - numberOfSegments) break;
00386
00387 if (!codeword[codeword_idx].decoded && segment[segment_idx].len > 0)
00388 {
00389 uint8_t tmplen;
00390
00391 if (codeword[codeword_idx].bits.len != 0)
00392 concat_bits(&segment[segment_idx], &codeword[codeword_idx].bits);
00393
00394 tmplen = segment[segment_idx].len;
00395
00396 if (huffman_spectral_data_2(codeword[codeword_idx].cb, &segment[segment_idx],
00397 &spectral_data[codeword[codeword_idx].sp_offset]) >= 0)
00398 {
00399 codeword[codeword_idx].decoded = 1;
00400 } else
00401 {
00402 codeword[codeword_idx].bits = segment[segment_idx];
00403 codeword[codeword_idx].bits.len = tmplen;
00404 }
00405
00406 }
00407 }
00408 }
00409 for (i = 0; i < numberOfSegments; i++)
00410 rewrev_bits(&segment[i]);
00411 }
00412
00413 bitsleft = 0;
00414
00415 for (i = 0; i < numberOfSegments && !bitsleft; i++)
00416 bitsleft += segment[i].len;
00417
00418 if (bitsleft) return 10;
00419
00420 codewordsleft = 0;
00421
00422 for (i = 0; (i < numberOfCodewords - numberOfSegments) && (!codewordsleft); i++)
00423 if (!codeword[i].decoded)
00424 codewordsleft++;
00425
00426 if (codewordsleft) return 10;
00427
00428 return 0;
00429
00430 }
00431 #endif