Changeset 183
- Timestamp:
- 12/21/06 13:37:08 (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libmpc/branches/r2d/mpcgain/mpcgain.c
r182 r183 33 33 */ 34 34 #include <stdio.h> 35 #include <math.h> 35 36 #include <mpcdec/mpcdec.h> 37 #include <mpc/minimax.h> 36 38 #include <replaygain/gain_analysis.h> 39 40 #include "../libmpcdec/internal.h" 41 42 // mpc_bits_reader.c 43 int mpc_bits_get_block(mpc_bits_reader * r, mpc_block * p_block); 44 unsigned int mpc_bits_get_size(mpc_bits_reader * r, mpc_uint64_t * p_size); 45 46 // crc32.c 47 unsigned long crc32(unsigned char *buf, int len); 37 48 38 49 static void usage(const char *exename) … … 48 59 mpc_status err; 49 60 MPC_SAMPLE_FORMAT sample_buffer[MPC_DECODER_BUFFER_LENGTH]; 61 MPC_SAMPLE_FORMAT album_max = 0; 62 mpc_uint16_t album_gain; 63 mpc_uint16_t album_peak; 64 mpc_uint16_t * title_gain; 65 mpc_uint16_t * title_peak; 66 mpc_uint32_t * header_pos; 50 67 Float_t left_samples[MPC_FRAME_LENGTH * sizeof(Float_t)]; 51 68 Float_t right_samples[MPC_FRAME_LENGTH * sizeof(Float_t)]; … … 57 74 } 58 75 76 title_gain = malloc((sizeof(mpc_uint16_t) * 2 + sizeof(mpc_uint32_t)) * (argc - 1)); 77 title_peak = title_gain + (argc - 1); 78 header_pos = (mpc_uint32_t *) (title_peak + (argc - 1)); 79 59 80 for( j = 1; j < argc; j++){ 81 MPC_SAMPLE_FORMAT title_max = 0; 82 60 83 err = mpc_reader_init_stdio(&reader, argv[j]); 61 84 if(err < 0) return !MPC_STATUS_OK; … … 78 101 left_samples[i] = sample_buffer[2 * i] * (1 << 15); 79 102 right_samples[i] = sample_buffer[2 * i + 1] * (1 << 15); 103 title_max = maxf(title_max, sample_buffer[2 * i]); 104 title_max = maxf(title_max, sample_buffer[2 * i + 1]); 80 105 } 81 106 … … 83 108 } 84 109 85 printf("%s : %f\n", argv[j], GetTitleGain()); 110 title_gain[j-1] = (mpc_uint16_t) (GetTitleGain() * 256); 111 title_peak[j-1] = (mpc_uint16_t) (log10(title_max * (1 << 15)) * 20 * 256); 112 header_pos[j-1] = si.header_position + 4; 113 114 album_max = maxf(album_max, title_max); 86 115 87 116 mpc_demux_exit(demux); … … 89 118 } 90 119 91 printf("album : %f\n", GetAlbumGain()); 120 album_gain = (mpc_uint16_t) (GetAlbumGain() * 256); 121 album_peak = (mpc_uint16_t) (log10(album_max * (1 << 15)) * 20 * 256); 122 123 for( j = 0; j < argc - 1; j++) { 124 unsigned char buffer[64]; 125 mpc_bits_reader r; 126 mpc_block b; 127 mpc_uint64_t size; 128 mpc_uint32_t crc_pos, crc; 129 FILE * file; 130 131 file = fopen( argv[j + 1], "r+"); 132 fseek(file, header_pos[j] - 4, SEEK_SET); 133 fread(buffer, 1, 16, file); 134 if (memcmp(buffer, "MPCK", 4) != 0) { 135 fprintf(stderr, "Unsupported file format, not a sv8 file : %s\n", argv[j + 1]); 136 fclose(file); 137 continue; 138 } 139 r.buff = buffer + 4; 140 r.count = 8; 141 142 size = mpc_bits_get_block(&r, &b); 143 144 while( memcmp(b.key, "SI", 2) != 0 ) { 145 header_pos[j] += b.size; 146 fseek(file, header_pos[j], SEEK_SET); 147 fread(buffer, 1, 16, file); 148 r.buff = buffer; 149 r.count = 8; 150 size = mpc_bits_get_block(&r, &b); 151 } 152 153 header_pos[j] += size; 154 155 fread(buffer + 16, 1, 48, file); 156 157 crc_pos = r.buff - buffer + (8 > r.count); // save position of CRC 158 r.buff += 4; // skip CRC 159 mpc_bits_get_size(&r, &size); 160 size += 4 + crc_pos; 161 162 buffer[size] = 1; // replaygain version 163 buffer[size + 1] = title_gain[j] >> 8; 164 buffer[size + 2] = title_gain[j] & 0xFF; 165 buffer[size + 3] = title_peak[j] >> 8; 166 buffer[size + 4] = title_peak[j] & 0xFF; 167 buffer[size + 5] = album_gain >> 8; 168 buffer[size + 6] = album_gain & 0xFF; 169 buffer[size + 7] = album_peak >> 8; 170 buffer[size + 8] = album_peak & 0xFF; 171 172 crc = crc32(buffer + crc_pos + 4, (int)b.size - 4); 173 buffer[crc_pos] = crc >> 24; 174 buffer[crc_pos + 1] = (crc >> 16) & 0xFF; 175 buffer[crc_pos + 2] = (crc >> 8) & 0xFF; 176 buffer[crc_pos + 3] = crc & 0xFF; 177 178 fseek(file, header_pos[j], SEEK_SET); 179 fwrite(buffer + crc_pos, 1, b.size, file); 180 fclose(file); 181 } 182 183 free(title_gain); 92 184 93 185 return 0;
Note: See TracChangeset
for help on using the changeset viewer.