Changeset 78 for mppenc/branches/r2d/libmpcenc/encode_sv7.c
- Timestamp:
- 10/27/06 18:42:05 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
mppenc/branches/r2d/libmpcenc/encode_sv7.c
r77 r78 23 23 #include "libmpcenc.h" 24 24 25 void WriteBits ( mpc_encoder_t*, const mpc_uint32_t input, const unsigned int bits ); 25 // bitstream.c 26 void writeBits (mpc_encoder_t * e, mpc_uint32_t input, unsigned int bits ); 27 unsigned int encodeSize(mpc_uint64_t, char *, mpc_bool_t); 28 void writeBlock ( mpc_encoder_t * e, const char * key, const mpc_bool_t addCRC); 29 26 30 void Init_Huffman_Encoder_SV7 ( void ); 27 31 … … 58 62 59 63 60 // initialize SV 764 // initialize SV8 61 65 void 62 Init_SV 7( mpc_encoder_t * e )66 Init_SV8 ( mpc_encoder_t * e ) 63 67 { 64 68 Init_Huffman_Encoder_SV7 (); … … 66 70 Klemm (); 67 71 68 e-> dword= 0;69 e-> filled = 32;70 e-> Zaehler= 0;71 e-> BufferedBits= 0;72 e->pos = 0; 73 e->bitsCount = 0; 74 e->outputBits = 0; 75 e->bitsBuff = 0; 72 76 e->Overflows = 0; 73 77 } 74 78 75 79 76 // writes SV 7-header80 // writes SV8-header 77 81 void 78 WriteHeader_SV 7( mpc_encoder_t*e,82 WriteHeader_SV8 ( mpc_encoder_t*e, 79 83 const unsigned int MaxBand, 80 const unsigned int Profile,81 84 const unsigned int MS_on, 82 const mpc_uint32_t TotalFrames, 83 const unsigned int SamplesRest, 85 const unsigned int SamplesCount, 84 86 const unsigned int StreamVersion, 85 const unsigned int SampleFreq ) 87 const unsigned int PNS_on, 88 const unsigned int SampleFreq, 89 const unsigned int ChannelCount) 86 90 { 87 WriteBits ( e, StreamVersion, 8 ); // StreamVersion 88 WriteBits ( e, 0x2B504D , 24 ); // Magic Number "MP+" 89 90 WriteBits ( e, TotalFrames , 32 ); // # of frames 91 92 WriteBits ( e, 0 , 1 ); // former IS-Flag (not supported anymore) 93 WriteBits ( e, MS_on , 1 ); // MS-Coding Flag 94 WriteBits ( e, MaxBand , 6 ); // Bandwidth 95 96 #if 0 97 if ( MPPENC_VERSION [3] & 1 ) 98 WriteBits ( e, 1 , 4 ); // 1: Experimental profile 99 else 100 #endif 101 102 WriteBits ( e, Profile , 4 ); // 5...15: below Telephone...above BrainDead 103 WriteBits ( e, 0 , 2 ); // for future use 104 switch ( SampleFreq ) { 105 case 44100: WriteBits ( e, 0, 2 ); break; 106 case 48000: WriteBits ( e, 1, 2 ); break; 107 case 37800: WriteBits ( e, 2, 2 ); break; 108 case 32000: WriteBits ( e, 3, 2 ); break; 91 unsigned char samplesCount[10]; 92 int samplesCountLen = encodeSize(SamplesCount, (char *)samplesCount, FALSE); 93 int i; 94 95 writeBits ( e, StreamVersion, 8 ); // StreamVersion 96 97 for( i = 0; i < samplesCountLen; i++) // nb of samples 98 writeBits ( e, samplesCount[i] , 8 ); 99 100 switch ( SampleFreq ) { 101 case 44100: writeBits ( e, 0, 4 ); break; 102 case 48000: writeBits ( e, 1, 4 ); break; 103 case 37800: writeBits ( e, 2, 4 ); break; 104 case 32000: writeBits ( e, 3, 4 ); break; 109 105 default : sprintf(stderr, "Internal error\n");// FIXME : stderr_printf ( "Internal error\n"); 110 exit (1); 111 } 112 WriteBits ( e, 0 , 16 ); // maximum input sample value, currently filled by replaygain 113 114 WriteBits ( e, 0 , 32 ); // title based gain controls, currently filled by replaygain 115 116 WriteBits ( e, 0 , 32 ); // album based gain controls, currently filled by replaygain 117 118 WriteBits ( e, 1 , 1 ); // true gapless: used? 119 WriteBits ( e, SamplesRest , 11 ); // true gapless: valid samples in last frame 120 WriteBits ( e, 1 , 1 ); // we now support fast seeking 121 WriteBits ( e, 0 , 19 ); 122 123 WriteBits ( e, (MPPENC_VERSION[0]&15)*100 + (MPPENC_VERSION[2]&15)*10 + (MPPENC_VERSION[3]&15), 124 8 ); // for future use 106 exit (1); 107 } 108 109 writeBits ( e, ChannelCount - 1 , 4 ); // Channels 110 writeBits ( e, MaxBand - 1 , 5 ); // Bandwidth 111 writeBits ( e, 0 , 1 ); // former IS-Flag (not supported anymore) 112 writeBits ( e, MS_on , 1 ); // MS-Coding Flag 113 writeBits ( e, PNS_on , 1 ); // PNS flag 114 writeBits ( e, FRAMES_PER_BLOCK_PWR, 4 ); // frames per block (log2 unit) 125 115 } 126 127 128 void129 FinishBitstream ( mpc_encoder_t* e )130 {131 e->Buffer [e->Zaehler++] = e->dword; // Assigning the "last" word132 }133 134 116 135 117 #define ENCODE_SCF1( new, old, rll ) \ 136 118 d = new - old + 7; \ 137 if ( d <= 14u && rll < 32) { \ 138 WriteBits ( e, Table[d].Code, Table[d].Length ); \ 139 } \ 140 else { \ 119 if ( d <= 14u && rll < 1) { \ 120 writeBits ( e, Table[d].Code, Table[d].Length ); \ 121 } else { \ 141 122 if ( new < 0 ) new = 0, e->Overflows++; \ 142 WriteBits ( e, Table[15].Code, Table[15].Length ); \143 WriteBits ( e, (unsigned int)new, 6 ); \123 writeBits ( e, Table[15].Code, Table[15].Length ); \ 124 writeBits ( e, (unsigned int)new, 6 ); \ 144 125 rll = 0; \ 145 126 } … … 148 129 d = new - old + 7; \ 149 130 if ( d <= 14u ) { \ 150 WriteBits ( e, Table[d].Code, Table[d].Length ); \ 151 } \ 152 else { \ 131 writeBits ( e, Table[d].Code, Table[d].Length ); \ 132 } else { \ 153 133 if ( new < 0 ) new = 0, e->Overflows++; \ 154 WriteBits ( e, Table[15].Code, Table[15].Length ); \155 WriteBits ( e, (unsigned int)new, 6 ); \134 writeBits ( e, Table[15].Code, Table[15].Length ); \ 135 writeBits ( e, (unsigned int)new, 6 ); \ 156 136 rll = 0; \ 157 137 } 158 159 160 static void161 test ( const int* const Res, const unsigned int* q )162 {163 #if 0164 int i;165 166 switch ( *Res ) {167 case 1:168 for ( i = 0; i < 36; i ++ )169 if ( q[i] != 1 )170 return;171 fprintf ( stderr, "Alles Nullsamples, aber Auflï¿œung = %u\n", *Res );172 *Res = 0;173 break;174 case 2:175 for ( i = 0; i < 36; i ++ )176 if ( q[i] != 2 )177 return;178 fprintf ( stderr, "Alles Nullsamples, aber Auflï¿œung = %u\n", *Res );179 *Res = 0;180 break;181 }182 #endif183 }184 138 185 139 … … 204 158 205 159 /************************************ Resolution *********************************/ 206 WriteBits ( e, (unsigned int)Res_L[0], 4 ); // subband 0207 WriteBits ( e, (unsigned int)Res_R[0], 4 );160 writeBits ( e, (unsigned int)Res_L[0], 4 ); // subband 0 161 writeBits ( e, (unsigned int)Res_R[0], 4 ); 208 162 if ( e->MS_Channelmode > 0 && !(Res_L[0]==0 && Res_R[0]==0) ) 209 WriteBits ( e, MS_Flag[0] , 1 );163 writeBits ( e, MS_Flag[0] , 1 ); 210 164 211 165 Table = HuffHdr; // subband 1...MaxBand 212 166 for ( n = 1; n <= MaxBand; n++ ) { 213 test ( Res_L+n, Q[n].L );214 215 167 d = Res_L[n] - Res_L[n-1] + 5; 216 168 if ( d <= 8u ) { 217 WriteBits ( e, Table[d].Code, Table[d].Length );169 writeBits ( e, Table[d].Code, Table[d].Length ); 218 170 } 219 171 else { 220 WriteBits ( e, Table[9].Code, Table[9].Length ); 221 WriteBits ( e, Res_L[n] , 4 ); 222 } 223 224 test ( Res_R+n, Q[n].R ); 172 writeBits ( e, Table[9].Code, Table[9].Length ); 173 writeBits ( e, Res_L[n] , 4 ); 174 } 175 225 176 d = Res_R[n] - Res_R[n-1] + 5; 226 177 if ( d <= 8u ) { 227 WriteBits ( e, Table[d].Code, Table[d].Length );178 writeBits ( e, Table[d].Code, Table[d].Length ); 228 179 } 229 180 else { 230 WriteBits ( e, Table[9].Code, Table[9].Length );231 WriteBits ( e, Res_R[n] , 4 );181 writeBits ( e, Table[9].Code, Table[9].Length ); 182 writeBits ( e, Res_R[n] , 4 ); 232 183 } 233 184 if ( e->MS_Channelmode > 0 && !(Res_L[n]==0 && Res_R[n]==0) ) 234 WriteBits ( e, MS_Flag[n], 1 );185 writeBits ( e, MS_Flag[n], 1 ); 235 186 } 236 187 … … 240 191 if ( Res_L[n] ) { 241 192 SCFI_L[n] = 2 * (SCF_Index_L[n][0] == SCF_Index_L[n][1]) + (SCF_Index_L[n][1] == SCF_Index_L[n][2]); 242 WriteBits ( e, Table[SCFI_L[n]].Code, Table[SCFI_L[n]].Length );193 writeBits ( e, Table[SCFI_L[n]].Code, Table[SCFI_L[n]].Length ); 243 194 } 244 195 if ( Res_R[n] ) { 245 196 SCFI_R[n] = 2 * (SCF_Index_R[n][0] == SCF_Index_R[n][1]) + (SCF_Index_R[n][1] == SCF_Index_R[n][2]); 246 WriteBits ( e, Table[SCFI_R[n]].Code, Table[SCFI_R[n]].Length );197 writeBits ( e, Table[SCFI_R[n]].Code, Table[SCFI_R[n]].Length ); 247 198 } 248 199 } … … 250 201 /************************************* SCF **********************************/ 251 202 Table = HuffDSCF; 252 for ( n = 0; n <= MaxBand; n++ ) { 203 for ( n = 0; n <= MaxBand; n++ ) { 204 if (e->framesInBlock == 0){ 205 DSCF_RLL_L[n] = DSCF_RLL_R[n] = 1; // new block -> force key frame 206 } 253 207 254 208 if ( Res_L[n] ) { … … 276 230 } 277 231 } 278 if (DSCF_RLL_L[n] <= 32)279 DSCF_RLL_L[n]++; // Increased counters for SCF that haven't been initialized again280 232 281 233 if ( Res_R[n] ) { … … 303 255 } 304 256 } 305 if (DSCF_RLL_R[n] <= 32)306 DSCF_RLL_R[n]++; // Increased counters for SCF that haven't been freshly initialized307 257 } 308 258 … … 326 276 } 327 277 book = sum >= 0; 328 WriteBits ( e, book, 1 );278 writeBits ( e, book, 1 ); 329 279 Table = HuffQ [book][1]; 330 280 for ( k = 0; k < 36; k += 3 ) { 331 281 idx = q[k+0] + 3*q[k+1] + 9*q[k+2]; 332 WriteBits ( e, Table[idx].Code, Table[idx].Length );282 writeBits ( e, Table[idx].Code, Table[idx].Length ); 333 283 } 334 284 break; … … 342 292 } 343 293 book = sum >= 0; 344 WriteBits ( e, book, 1 );294 writeBits ( e, book, 1 ); 345 295 Table = HuffQ [book][2]; 346 296 for ( k = 0; k < 36; k += 2 ) { 347 297 idx = q[k+0] + 5*q[k+1]; 348 WriteBits ( e, Table[idx].Code, Table[idx].Length );298 writeBits ( e, Table[idx].Code, Table[idx].Length ); 349 299 } 350 300 break; … … 361 311 } 362 312 book = sum >= 0; 363 WriteBits ( e, book, 1 );313 writeBits ( e, book, 1 ); 364 314 Table = HuffQ [book][Res_L[n]]; 365 315 for ( k = 0; k < 36; k++ ) { 366 316 idx = q[k]; 367 WriteBits ( e, Table[idx].Code, Table[idx].Length );317 writeBits ( e, Table[idx].Code, Table[idx].Length ); 368 318 } 369 319 break; 370 320 default: 371 321 for ( k = 0; k < 36; k++ ) 372 WriteBits ( e, q[k], Res_L[n]-1 );322 writeBits ( e, q[k], Res_L[n]-1 ); 373 323 break; 374 324 } … … 390 340 } 391 341 book = sum >= 0; 392 WriteBits ( e, book, 1 );342 writeBits ( e, book, 1 ); 393 343 Table = HuffQ [book][1]; 394 344 for ( k = 0; k < 36; k += 3 ) { 395 345 idx = q[k+0] + 3*q[k+1] + 9*q[k+2]; 396 WriteBits ( e, Table[idx].Code, Table[idx].Length );346 writeBits ( e, Table[idx].Code, Table[idx].Length ); 397 347 } 398 348 break; … … 406 356 } 407 357 book = sum >= 0; 408 WriteBits ( e, book, 1 );358 writeBits ( e, book, 1 ); 409 359 Table = HuffQ [book][2]; 410 360 for ( k = 0; k < 36; k += 2 ) { 411 361 idx = q[k+0] + 5*q[k+1]; 412 WriteBits ( e, Table[idx].Code, Table[idx].Length );362 writeBits ( e, Table[idx].Code, Table[idx].Length ); 413 363 } 414 364 break; … … 425 375 } 426 376 book = sum >= 0; 427 WriteBits ( e, book, 1 );377 writeBits ( e, book, 1 ); 428 378 Table = HuffQ [book][Res_R[n]]; 429 379 for ( k = 0; k < 36; k++ ) { 430 380 idx = q[k]; 431 WriteBits ( e, Table[idx].Code, Table[idx].Length );381 writeBits ( e, Table[idx].Code, Table[idx].Length ); 432 382 } 433 383 break; 434 384 default: 435 385 for ( k = 0; k < 36; k++ ) 436 WriteBits ( e, q[k], Res_R[n] - 1 );386 writeBits ( e, q[k], Res_R[n] - 1 ); 437 387 break; 438 388 } 439 389 440 390 } 441 return; 391 392 e->framesInBlock++; 393 if (e->framesInBlock == FRAMES_PER_BLOCK) 394 writeBlock(e, "AD", FALSE); 442 395 } 443 396
Note: See TracChangeset
for help on using the changeset viewer.