#include "MP3Internals.hh"Include dependency graph for MP3InternalsHuffman.hh:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.
Data Structures | |
| class | MP3HuffmanEncodingInfo |
Defines | |
| #define | SSLIMIT 18 |
| #define | BYTES_PER_SAMPLE_VALUE 1 |
Functions | |
| void | updateSideInfoForHuffman (MP3SideInfo &sideInfo, Boolean isMPEG2, unsigned char const *mainDataPtr, unsigned p23L0, unsigned p23L1, unsigned &part23Length0a, unsigned &part23Length0aTruncation, unsigned &part23Length0b, unsigned &part23Length0bTruncation, unsigned &part23Length1a, unsigned &part23Length1aTruncation, unsigned &part23Length1b, unsigned &part23Length1bTruncation) |
| void | MP3HuffmanDecode (MP3SideInfo::gr_info_s_t *gr, int isMPEG2, unsigned char const *fromBasePtr, unsigned fromBitOffset, unsigned fromLength, unsigned &scaleFactorsLength, MP3HuffmanEncodingInfo &hei) |
Variables | |
| unsigned char | huffdec [] |
| #define BYTES_PER_SAMPLE_VALUE 1 |
Definition at line 71 of file MP3InternalsHuffman.hh.
| #define SSLIMIT 18 |
Definition at line 40 of file MP3InternalsHuffman.hh.
Referenced by MP3HuffmanEncodingInfo::MP3HuffmanEncodingInfo().
| void MP3HuffmanDecode | ( | MP3SideInfo::gr_info_s_t * | gr, | |
| int | isMPEG2, | |||
| unsigned char const * | fromBasePtr, | |||
| unsigned | fromBitOffset, | |||
| unsigned | fromLength, | |||
| unsigned & | scaleFactorsLength, | |||
| MP3HuffmanEncodingInfo & | hei | |||
| ) |
Definition at line 566 of file MP3InternalsHuffman.cpp.
References MP3HuffmanEncodingInfo::allBitOffsets, MP3SideInfo::gr_info_s::big_values, BitVector::curBitIndex(), MP3HuffmanEncodingInfo::decodedValues, getScaleFactorsLength(), if(), initialize_huffman(), NULL, MP3HuffmanEncodingInfo::numSamples, MP3HuffmanEncodingInfo::reg1Start, MP3HuffmanEncodingInfo::reg2Start, MP3SideInfo::gr_info_s::region1start, MP3SideInfo::gr_info_s::region2start, rsf_ht, rsf_huffman_decoder(), BitVector::skipBits(), and MP3SideInfo::gr_info_s::table_select.
Referenced by updateSideInfoForHuffman().
00570 { 00571 unsigned i; 00572 int x, y, v, w; 00573 struct huffcodetab *h; 00574 BitVector bv((unsigned char*)fromBasePtr, fromBitOffset, fromLength); 00575 00576 /* Compute the size of the scale factors (& also advance bv): */ 00577 scaleFactorsLength = getScaleFactorsLength(gr, isMPEG2); 00578 bv.skipBits(scaleFactorsLength); 00579 00580 initialize_huffman(); 00581 00582 hei.reg1Start = hei.reg2Start = hei.numSamples = 0; 00583 00584 /* Read bigvalues area. */ 00585 if (gr->big_values < gr->region1start + gr->region2start) { 00586 gr->big_values = gr->region1start + gr->region2start; /* sanity check */ 00587 } 00588 for (i = 0; i < gr->big_values; ++i) { 00589 if (i < gr->region1start) { 00590 /* in region 0 */ 00591 h = &rsf_ht[gr->table_select[0]]; 00592 } else if (i < gr->region2start) { 00593 /* in region 1 */ 00594 h = &rsf_ht[gr->table_select[1]]; 00595 if (hei.reg1Start == 0) { 00596 hei.reg1Start = bv.curBitIndex(); 00597 } 00598 } else { 00599 /* in region 2 */ 00600 h = &rsf_ht[gr->table_select[2]]; 00601 if (hei.reg2Start == 0) { 00602 hei.reg2Start = bv.curBitIndex(); 00603 } 00604 } 00605 00606 hei.allBitOffsets[i] = bv.curBitIndex(); 00607 rsf_huffman_decoder(bv, h, &x, &y, &v, &w); 00608 if (hei.decodedValues != NULL) { 00609 // Record the decoded values: 00610 unsigned* ptr = &hei.decodedValues[4*i]; 00611 ptr[0] = x; ptr[1] = y; ptr[2] = v; ptr[3] = w; 00612 } 00613 } 00614 hei.bigvalStart = bv.curBitIndex(); 00615 00616 /* Read count1 area. */ 00617 h = &rsf_ht[gr->count1table_select+32]; 00618 while (bv.curBitIndex() < bv.totNumBits() && i < SSLIMIT*SBLIMIT) { 00619 hei.allBitOffsets[i] = bv.curBitIndex(); 00620 rsf_huffman_decoder(bv, h, &x, &y, &v, &w); 00621 if (hei.decodedValues != NULL) { 00622 // Record the decoded values: 00623 unsigned* ptr = &hei.decodedValues[4*i]; 00624 ptr[0] = x; ptr[1] = y; ptr[2] = v; ptr[3] = w; 00625 } 00626 ++i; 00627 } 00628 00629 hei.allBitOffsets[i] = bv.curBitIndex(); 00630 hei.numSamples = i; 00631 }
| void updateSideInfoForHuffman | ( | MP3SideInfo & | sideInfo, | |
| Boolean | isMPEG2, | |||
| unsigned char const * | mainDataPtr, | |||
| unsigned | p23L0, | |||
| unsigned | p23L1, | |||
| unsigned & | part23Length0a, | |||
| unsigned & | part23Length0aTruncation, | |||
| unsigned & | part23Length0b, | |||
| unsigned & | part23Length0bTruncation, | |||
| unsigned & | part23Length1a, | |||
| unsigned & | part23Length1aTruncation, | |||
| unsigned & | part23Length1b, | |||
| unsigned & | part23Length1bTruncation | |||
| ) |
Definition at line 45 of file MP3InternalsHuffman.cpp.
References MP3HuffmanEncodingInfo::allBitOffsets, MP3SideInfo::gr_info_s::big_values, MP3HuffmanEncodingInfo::bigvalStart, MP3SideInfo::ch, debugCount, MP3SideInfo::gr, MP3HuffmanDecode(), MP3HuffmanEncodingInfo::numSamples, MP3SideInfo::gr_info_s::part2_3_length, MP3HuffmanEncodingInfo::reg1Start, and MP3HuffmanEncodingInfo::reg2Start.
Referenced by updateSideInfoSizes().
00055 { 00056 int i, j; 00057 unsigned sfLength, origTotABsize, adjustment; 00058 MP3SideInfo::gr_info_s_t* gr; 00059 00060 /* First, Huffman-decode each part of the segment's main data, 00061 to see at which bit-boundaries the samples appear: 00062 */ 00063 MP3HuffmanEncodingInfo hei; 00064 00065 ++debugCount; 00066 #ifdef DEBUG 00067 fprintf(stderr, "usifh-start: p23L0: %d, p23L1: %d\n", p23L0, p23L1); 00068 #endif 00069 00070 /* Process granule 0 */ 00071 { 00072 gr = &(sideInfo.ch[0].gr[0]); 00073 origTotABsize = gr->part2_3_length; 00074 00075 MP3HuffmanDecode(gr, isMPEG2, mainDataPtr, 0, origTotABsize, sfLength, hei); 00076 00077 /* Begin by computing new sizes for parts a & b (& their truncations) */ 00078 #ifdef DEBUG 00079 fprintf(stderr, "usifh-0: %d, %d:%d, %d:%d, %d:%d, %d:%d, %d:%d\n", 00080 hei.numSamples, 00081 sfLength/8, sfLength%8, 00082 hei.reg1Start/8, hei.reg1Start%8, 00083 hei.reg2Start/8, hei.reg2Start%8, 00084 hei.bigvalStart/8, hei.bigvalStart%8, 00085 origTotABsize/8, origTotABsize%8); 00086 #ifdef undef 00087 { 00088 unsigned k; 00089 for (k = 0; k < hei.numSamples; ++k) { 00090 fprintf(stderr, " %d:%d", 00091 hei.allBitOffsets[k]/8, hei.allBitOffsets[k]%8); 00092 } 00093 fprintf(stderr, "\n"); 00094 } 00095 #endif 00096 #endif 00097 if (p23L0 < sfLength) { 00098 /* We can't use this, so give it all to the next granule: */ 00099 p23L1 += p23L0; 00100 p23L0 = 0; 00101 } 00102 00103 part23Length0a = hei.bigvalStart; 00104 part23Length0b = origTotABsize - hei.bigvalStart; 00105 part23Length0aTruncation = part23Length0bTruncation = 0; 00106 if (origTotABsize > p23L0) { 00107 /* We need to shorten one or both of fields a & b */ 00108 unsigned truncation = origTotABsize - p23L0; 00109 #ifdef TRUNC_FAIRLY 00110 part23Length0aTruncation = (truncation*(part23Length0a-sfLength)) 00111 /(origTotABsize-sfLength); 00112 part23Length0bTruncation = truncation - part23Length0aTruncation; 00113 #endif 00114 #ifdef TRUNC_FAVORa 00115 part23Length0bTruncation 00116 = (truncation > part23Length0b) ? part23Length0b : truncation; 00117 part23Length0aTruncation = truncation - part23Length0bTruncation; 00118 #endif 00119 #ifdef TRUNC_FAVORb 00120 part23Length0aTruncation = (truncation > part23Length0a-sfLength) 00121 ? (part23Length0a-sfLength) : truncation; 00122 part23Length0bTruncation = truncation - part23Length0aTruncation; 00123 #endif 00124 } 00125 /* ASSERT: part23Length0xTruncation <= part23Length0x */ 00126 part23Length0a -= part23Length0aTruncation; 00127 part23Length0b -= part23Length0bTruncation; 00128 #ifdef DEBUG 00129 fprintf(stderr, "usifh-0: interim sizes: %d (%d), %d (%d)\n", 00130 part23Length0a, part23Length0aTruncation, 00131 part23Length0b, part23Length0bTruncation); 00132 #endif 00133 00134 /* Adjust these new lengths so they end on sample bit boundaries: */ 00135 for (i = 0; i < (int)hei.numSamples; ++i) { 00136 if (hei.allBitOffsets[i] == part23Length0a) break; 00137 else if (hei.allBitOffsets[i] > part23Length0a) {--i; break;} 00138 } 00139 if (i < 0) { /* should happen only if we couldn't fit sfLength */ 00140 i = 0; adjustment = 0; 00141 } else { 00142 adjustment = part23Length0a - hei.allBitOffsets[i]; 00143 } 00144 #ifdef DEBUG 00145 fprintf(stderr, "%d usifh-0: adjustment 1: %d\n", debugCount, adjustment); 00146 #endif 00147 part23Length0a -= adjustment; 00148 part23Length0aTruncation += adjustment; 00149 /* Assign the bits we just shaved to field b and granule 1: */ 00150 if (part23Length0bTruncation < adjustment) { 00151 p23L1 += (adjustment - part23Length0bTruncation); 00152 adjustment = part23Length0bTruncation; 00153 } 00154 part23Length0b += adjustment; 00155 part23Length0bTruncation -= adjustment; 00156 for (j = i; j < (int)hei.numSamples; ++j) { 00157 if (hei.allBitOffsets[j] 00158 == part23Length0a + part23Length0aTruncation + part23Length0b) 00159 break; 00160 else if (hei.allBitOffsets[j] 00161 > part23Length0a + part23Length0aTruncation + part23Length0b) 00162 {--j; break;} 00163 } 00164 if (j < 0) { /* should happen only if we couldn't fit sfLength */ 00165 j = 0; adjustment = 0; 00166 } else { 00167 adjustment = part23Length0a+part23Length0aTruncation+part23Length0b 00168 - hei.allBitOffsets[j]; 00169 } 00170 #ifdef DEBUG 00171 fprintf(stderr, "%d usifh-0: adjustment 2: %d\n", debugCount, adjustment); 00172 #endif 00173 if (adjustment > part23Length0b) adjustment = part23Length0b; /*sanity*/ 00174 part23Length0b -= adjustment; 00175 part23Length0bTruncation += adjustment; 00176 /* Assign the bits we just shaved to granule 1 */ 00177 p23L1 += adjustment; 00178 00179 if (part23Length0aTruncation > 0) { 00180 /* Change the granule's 'big_values' field to reflect the truncation */ 00181 gr->big_values = i; 00182 } 00183 } 00184 00185 /* Process granule 1 (MPEG-1 only) */ 00186 00187 if (isMPEG2) { 00188 part23Length1a = part23Length1b = 0; 00189 part23Length1aTruncation = part23Length1bTruncation = 0; 00190 } else { 00191 unsigned granule1Offset 00192 = origTotABsize + sideInfo.ch[1].gr[0].part2_3_length; 00193 00194 gr = &(sideInfo.ch[0].gr[1]); 00195 origTotABsize = gr->part2_3_length; 00196 00197 MP3HuffmanDecode(gr, isMPEG2, mainDataPtr, granule1Offset, 00198 origTotABsize, sfLength, hei); 00199 00200 /* Begin by computing new sizes for parts a & b (& their truncations) */ 00201 #ifdef DEBUG 00202 fprintf(stderr, "usifh-1: %d, %d:%d, %d:%d, %d:%d, %d:%d, %d:%d\n", 00203 hei.numSamples, 00204 sfLength/8, sfLength%8, 00205 hei.reg1Start/8, hei.reg1Start%8, 00206 hei.reg2Start/8, hei.reg2Start%8, 00207 hei.bigvalStart/8, hei.bigvalStart%8, 00208 origTotABsize/8, origTotABsize%8); 00209 #ifdef undef 00210 { 00211 unsigned k; 00212 for (k = 0; k < hei.numSamples; ++k) { 00213 fprintf(stderr, " %d:%d", 00214 hei.allBitOffsets[k]/8, hei.allBitOffsets[k]%8); 00215 } 00216 fprintf(stderr, "\n"); 00217 } 00218 #endif 00219 #endif 00220 if (p23L1 < sfLength) { 00221 /* We can't use this, so give up on this granule: */ 00222 p23L1 = 0; 00223 } 00224 00225 part23Length1a = hei.bigvalStart; 00226 part23Length1b = origTotABsize - hei.bigvalStart; 00227 part23Length1aTruncation = part23Length1bTruncation = 0; 00228 if (origTotABsize > p23L1) { 00229 /* We need to shorten one or both of fields a & b */ 00230 unsigned truncation = origTotABsize - p23L1; 00231 #ifdef TRUNC_FAIRLY 00232 part23Length1aTruncation = (truncation*(part23Length1a-sfLength)) 00233 /(origTotABsize-sfLength); 00234 part23Length1bTruncation = truncation - part23Length1aTruncation; 00235 #endif 00236 #ifdef TRUNC_FAVORa 00237 part23Length1bTruncation 00238 = (truncation > part23Length1b) ? part23Length1b : truncation; 00239 part23Length1aTruncation = truncation - part23Length1bTruncation; 00240 #endif 00241 #ifdef TRUNC_FAVORb 00242 part23Length1aTruncation = (truncation > part23Length1a-sfLength) 00243 ? (part23Length1a-sfLength) : truncation; 00244 part23Length1bTruncation = truncation - part23Length1aTruncation; 00245 #endif 00246 } 00247 /* ASSERT: part23Length1xTruncation <= part23Length1x */ 00248 part23Length1a -= part23Length1aTruncation; 00249 part23Length1b -= part23Length1bTruncation; 00250 #ifdef DEBUG 00251 fprintf(stderr, "usifh-1: interim sizes: %d (%d), %d (%d)\n", 00252 part23Length1a, part23Length1aTruncation, 00253 part23Length1b, part23Length1bTruncation); 00254 #endif 00255 00256 /* Adjust these new lengths so they end on sample bit boundaries: */ 00257 for (i = 0; i < (int)hei.numSamples; ++i) { 00258 if (hei.allBitOffsets[i] == part23Length1a) break; 00259 else if (hei.allBitOffsets[i] > part23Length1a) {--i; break;} 00260 } 00261 if (i < 0) { /* should happen only if we couldn't fit sfLength */ 00262 i = 0; adjustment = 0; 00263 } else { 00264 adjustment = part23Length1a - hei.allBitOffsets[i]; 00265 } 00266 #ifdef DEBUG 00267 fprintf(stderr, "%d usifh-1: adjustment 0: %d\n", debugCount, adjustment); 00268 #endif 00269 part23Length1a -= adjustment; 00270 part23Length1aTruncation += adjustment; 00271 /* Assign the bits we just shaved to field b: */ 00272 if (part23Length1bTruncation < adjustment) { 00273 adjustment = part23Length1bTruncation; 00274 } 00275 part23Length1b += adjustment; 00276 part23Length1bTruncation -= adjustment; 00277 for (j = i; j < (int)hei.numSamples; ++j) { 00278 if (hei.allBitOffsets[j] 00279 == part23Length1a + part23Length1aTruncation + part23Length1b) 00280 break; 00281 else if (hei.allBitOffsets[j] 00282 > part23Length1a + part23Length1aTruncation + part23Length1b) 00283 {--j; break;} 00284 } 00285 if (j < 0) { /* should happen only if we couldn't fit sfLength */ 00286 j = 0; adjustment = 0; 00287 } else { 00288 adjustment = part23Length1a+part23Length1aTruncation+part23Length1b 00289 - hei.allBitOffsets[j]; 00290 } 00291 #ifdef DEBUG 00292 fprintf(stderr, "%d usifh-1: adjustment 1: %d\n", debugCount, adjustment); 00293 #endif 00294 if (adjustment > part23Length1b) adjustment = part23Length1b; /*sanity*/ 00295 part23Length1b -= adjustment; 00296 part23Length1bTruncation += adjustment; 00297 00298 if (part23Length1aTruncation > 0) { 00299 /* Change the granule's 'big_values' field to reflect the truncation */ 00300 gr->big_values = i; 00301 } 00302 } 00303 #ifdef DEBUG 00304 fprintf(stderr, "usifh-end, new vals: %d (%d), %d (%d), %d (%d), %d (%d)\n", 00305 part23Length0a, part23Length0aTruncation, 00306 part23Length0b, part23Length0bTruncation, 00307 part23Length1a, part23Length1aTruncation, 00308 part23Length1b, part23Length1bTruncation); 00309 #endif 00310 }
| unsigned char huffdec[] |
1.5.2