ulaw.c
上传用户:luping1608
上传日期:2007-01-06
资源大小:38k
文件大小:8k
源码类别:

多媒体

开发平台:

Unix_Linux

  1. #include "quicktime.h"
  2. // ==================================== private for ulaw
  3. #define uBIAS 0x84
  4. #define uCLIP 32635
  5. int ulaw_init_ulawtoint16(quicktime_t *file, int track)
  6. {
  7. int i;
  8. quicktime_audio_map_t *atrack = &(file->atracks[track]);
  9. quicktime_ulaw_codec_t *codec = &(atrack->codecs.ulaw_codec);
  10. // We use the floating point table to get values for the 16 bit table
  11. ulaw_init_ulawtofloat(file, track);
  12. if(!codec->ulawtoint16_table)
  13. {
  14. codec->ulawtoint16_table = malloc(sizeof(QUICKTIME_INT16) * 256);
  15. codec->ulawtoint16_ptr = codec->ulawtoint16_table;
  16. for(i = 0; i < 256; i++)
  17. {
  18. codec->ulawtoint16_table[i] = (int)(32768 * codec->ulawtofloat_ptr[i]);
  19. }
  20. }
  21. return 0;
  22. }
  23. QUICKTIME_INT16 ulaw_bytetoint16(quicktime_ulaw_codec_t *codec, unsigned char input)
  24. {
  25. return codec->ulawtoint16_ptr[input];
  26. }
  27. int ulaw_init_ulawtofloat(quicktime_t *file, int track)
  28. {
  29. int i;
  30. float value;
  31. quicktime_ulaw_codec_t *codec = &(file->atracks[track].codecs.ulaw_codec);
  32. if(!codec->ulawtofloat_table)
  33. {
  34.      static int exp_lut[8] = { 0, 132, 396, 924, 1980, 4092, 8316, 16764 };
  35.      int sign, exponent, mantissa, sample;
  36. unsigned char ulawbyte;
  37. codec->ulawtofloat_table = malloc(sizeof(float) * 256);
  38. codec->ulawtofloat_ptr = codec->ulawtofloat_table;
  39. for(i = 0; i < 256; i++)
  40. {
  41. ulawbyte = (unsigned char)i;
  42.      ulawbyte = ~ulawbyte;
  43.      sign = (ulawbyte & 0x80);
  44.      exponent = (ulawbyte >> 4) & 0x07;
  45.      mantissa = ulawbyte & 0x0F;
  46.      sample = exp_lut[exponent] + (mantissa << (exponent + 3));
  47.      if(sign != 0) sample = -sample;
  48. codec->ulawtofloat_ptr[i] = (float)sample / 32768;
  49. }
  50. }
  51. return 0;
  52. }
  53. float ulaw_bytetofloat(quicktime_ulaw_codec_t *codec, unsigned char input)
  54. {
  55. return codec->ulawtofloat_ptr[input];
  56. }
  57. int ulaw_init_int16toulaw(quicktime_t *file, int track)
  58. {
  59. quicktime_audio_map_t *atrack = &(file->atracks[track]);
  60. quicktime_ulaw_codec_t *codec = &(atrack->codecs.ulaw_codec);
  61. if(!codec->int16toulaw_table)
  62. {
  63.      int sign, exponent, mantissa;
  64.      unsigned char ulawbyte;
  65. int sample;
  66. int i;
  67.      int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
  68.                                4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  69.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  70.                                5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  71.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  72.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  73.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  74.                                6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  75.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  76.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  77.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  78.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  79.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  80.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  81.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  82.                                7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
  83.   codec->int16toulaw_table = malloc(65536);
  84. codec->int16toulaw_ptr = codec->int16toulaw_table + 32768;
  85. for(i = -32768; i < 32768; i++)
  86. {
  87. sample = i;
  88. // Get the sample into sign-magnitude.
  89.      sign = (sample >> 8) & 0x80; // set aside the sign
  90.      if(sign != 0) sample = -sample; // get magnitude
  91.      if(sample > uCLIP) sample = uCLIP; // clip the magnitude
  92. // Convert from 16 bit linear to ulaw.
  93.      sample = sample + uBIAS;
  94.     exponent = exp_lut[(sample >> 7) & 0xFF];
  95.     mantissa = (sample >> (exponent + 3)) & 0x0F;
  96.     ulawbyte = ~(sign | (exponent << 4) | mantissa);
  97. #ifdef ZEROTRAP
  98.     if (ulawbyte == 0) ulawbyte = 0x02;     // optional CCITT trap
  99. #endif
  100.     codec->int16toulaw_ptr[i] = ulawbyte;
  101. }
  102. }
  103. return 0;
  104. }
  105. float ulaw_int16tobyte(quicktime_ulaw_codec_t *codec, QUICKTIME_INT16 input)
  106. {
  107. return codec->int16toulaw_ptr[input];
  108. }
  109. float ulaw_floattobyte(quicktime_ulaw_codec_t *codec, float input)
  110. {
  111. return codec->int16toulaw_ptr[(int)(input * 32768)];
  112. }
  113. int ulaw_get_read_buffer(quicktime_t *file, int track, long samples)
  114. {
  115. quicktime_ulaw_codec_t *codec = &(file->atracks[track].codecs.ulaw_codec);
  116. if(codec->read_buffer && codec->read_size != samples)
  117. {
  118. free(codec->read_buffer);
  119. codec->read_buffer = 0;
  120. }
  121. if(!codec->read_buffer) 
  122. {
  123. long bytes = samples * file->atracks[track].channels;
  124. codec->read_size = samples;
  125. if(!(codec->read_buffer = malloc(bytes))) return 1;
  126. }
  127. return 0;
  128. }
  129. int ulaw_delete_tables(quicktime_audio_map_t *atrack)
  130. {
  131. quicktime_ulaw_codec_t *codec = &(atrack->codecs.ulaw_codec);
  132. if(codec->ulawtofloat_table) 
  133. free(codec->ulawtofloat_table); 
  134. }
  135. if(codec->ulawtoint16_table) 
  136. free(codec->ulawtoint16_table); 
  137. }
  138. if(codec->int16toulaw_table) 
  139. free(codec->int16toulaw_table); 
  140. }
  141. if(codec->read_buffer) free(codec->read_buffer);
  142. codec->int16toulaw_table = 0;
  143. codec->ulawtoint16_table = 0;
  144. codec->ulawtofloat_table = 0;
  145. codec->read_buffer = 0;
  146. codec->read_size = 0;
  147. return 0;
  148. }
  149. // =================================== public for ulaw
  150. int quicktime_init_codec_ulaw(quicktime_audio_map_t *atrack)
  151. {
  152. quicktime_ulaw_codec_t *codec = &(atrack->codecs.ulaw_codec);
  153. codec->int16toulaw_table = 0;
  154. codec->ulawtoint16_table = 0;
  155. codec->ulawtofloat_table = 0;
  156. codec->read_buffer = 0;
  157. codec->read_size = 0;
  158. return 0;
  159. }
  160. int quicktime_delete_codec_ulaw(quicktime_audio_map_t *atrack)
  161. {
  162. quicktime_ulaw_codec_t *codec = &(atrack->codecs.ulaw_codec);
  163. ulaw_delete_tables(atrack);
  164. return 0;
  165. }
  166. int quicktime_decode_ulaw(quicktime_t *file, 
  167. QUICKTIME_INT16 *output_i, 
  168. float *output_f, 
  169. long samples, 
  170. int track, 
  171. int channel)
  172. {
  173. int result = 0;
  174. long i;
  175. quicktime_ulaw_codec_t *codec = &(file->atracks[track].codecs.ulaw_codec);
  176. result = ulaw_get_read_buffer(file, track, samples);
  177. if(output_f) result += ulaw_init_ulawtofloat(file, track);
  178. if(output_i) result += ulaw_init_ulawtoint16(file, track);
  179. if(!result)
  180. {
  181. result = quicktime_read_audio(file, codec->read_buffer, samples, track);
  182. if(result) result = 0; else result = 1;  // defeat fread's return
  183. //printf("quicktime_decode_ulaw %dn", result);
  184. if(!result)
  185. {
  186. if(output_f)
  187. {
  188. unsigned char *input = &(codec->read_buffer[channel]);
  189. float *output_ptr = output_f;
  190. float *output_end = output_f + samples;
  191. int step = file->atracks[track].channels;
  192. while(output_ptr < output_end)
  193. {
  194. *output_ptr++ = ulaw_bytetofloat(codec, *input);
  195. input += step;
  196. }
  197. }
  198. else
  199. if(output_i)
  200. {
  201. unsigned char *input = &(codec->read_buffer[channel]);
  202. QUICKTIME_INT16 *output_ptr = output_i;
  203. QUICKTIME_INT16 *output_end = output_i + samples;
  204. int step = file->atracks[track].channels;
  205. while(output_ptr < output_end)
  206. {
  207. *output_ptr++ = ulaw_bytetoint16(codec, *input);
  208. input += step;
  209. }
  210. }
  211. }
  212. }
  213. return result;
  214. }
  215. int quicktime_encode_ulaw(quicktime_t *file, 
  216. QUICKTIME_INT16 **input_i, 
  217. float **input_f, 
  218. int track, 
  219. long samples)
  220. {
  221. int result = 0;
  222. int channel, step;
  223. long offset;
  224. long i;
  225. quicktime_ulaw_codec_t *codec = &(file->atracks[track].codecs.ulaw_codec);
  226. result = ulaw_init_int16toulaw(file, track);
  227. result += ulaw_get_read_buffer(file, track, samples);
  228. if(!result)
  229. {
  230. step = file->atracks[track].channels;
  231. if(input_f)
  232. {
  233. for(channel = 0; channel < file->atracks[track].channels; channel++)
  234. {
  235. float *input_ptr = input_f[channel];
  236. float *input_end = input_f[channel] + samples;
  237. unsigned char *output = codec->read_buffer + channel;
  238. while(input_ptr < input_end)
  239. {
  240. *output = ulaw_floattobyte(codec, *input_ptr++);
  241. output += step;
  242. }
  243. }
  244. }
  245. else
  246. if(input_i)
  247. {
  248. for(channel = 0; channel < file->atracks[track].channels; channel++)
  249. {
  250. QUICKTIME_INT16 *input_ptr = input_i[channel];
  251. QUICKTIME_INT16 *input_end = input_i[channel] + samples;
  252. unsigned char *output = codec->read_buffer + channel;
  253. while(input_ptr < input_end)
  254. {
  255. *output = ulaw_floattobyte(codec, *input_ptr++);
  256. output += step;
  257. }
  258. }
  259. }
  260. offset = quicktime_position(file);
  261. result = quicktime_write_data(file, codec->read_buffer, samples * file->atracks[track].channels);
  262. if(result) result = 0; else result = 1; // defeat fwrite's return
  263. quicktime_update_tables(file->atracks[track].track, 
  264. offset, 
  265. file->atracks[track].current_chunk, 
  266. file->atracks[track].current_position, 
  267. samples, 
  268. 0);
  269. file->atracks[track].current_chunk++;
  270. }
  271. return result;
  272. }