Newer
Older
monitord / lame-3.97 / libmp3lame / .svn / text-base / mpglib_interface.c.svn-base
  1. /* -*- mode: C; mode: fold -*- */
  2. /*
  3. * LAME MP3 encoding engine
  4. *
  5. * Copyright (c) 1999-2000 Mark Taylor
  6. * Copyright (c) 2003 Olcios
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Library General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Library General Public
  19. * License along with this library; if not, write to the
  20. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. */
  23.  
  24. /* $Id: mpglib_interface.c,v 1.26.2.1 2005/11/20 14:08:25 bouvigne Exp $ */
  25.  
  26. #ifdef HAVE_CONFIG_H
  27. # include <config.h>
  28. #endif
  29.  
  30. #ifdef HAVE_MPGLIB
  31.  
  32. #include <limits.h>
  33. #include <stdlib.h>
  34. #include <assert.h>
  35.  
  36. #include "interface.h"
  37. #include "lame.h"
  38.  
  39. #ifdef WITH_DMALLOC
  40. #include <dmalloc.h>
  41. #endif
  42.  
  43.  
  44. MPSTR mp;
  45. plotting_data *mpg123_pinfo = NULL;
  46.  
  47. int
  48. lame_decode_exit(void)
  49. {
  50. ExitMP3(&mp);
  51. return 0;
  52. }
  53.  
  54.  
  55. int
  56. lame_decode_init(void)
  57. {
  58. InitMP3(&mp);
  59. return 0;
  60. }
  61.  
  62.  
  63.  
  64.  
  65. /* copy mono samples */
  66. #define COPY_MONO(DST_TYPE, SRC_TYPE) \
  67. DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw; \
  68. SRC_TYPE *p_samples = (SRC_TYPE *)p; \
  69. for (i = 0; i < processed_samples; i++) \
  70. *pcm_l++ = (DST_TYPE)*p_samples++;
  71.  
  72. /* copy stereo samples */
  73. #define COPY_STEREO(DST_TYPE, SRC_TYPE) \
  74. DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw, *pcm_r = (DST_TYPE *)pcm_r_raw; \
  75. SRC_TYPE *p_samples = (SRC_TYPE *)p; \
  76. for (i = 0; i < processed_samples; i++) { \
  77. *pcm_l++ = (DST_TYPE)*p_samples++; \
  78. *pcm_r++ = (DST_TYPE)*p_samples++; \
  79. }
  80.  
  81.  
  82.  
  83. /*
  84. * For lame_decode: return code
  85. * -1 error
  86. * 0 ok, but need more data before outputing any samples
  87. * n number of samples output. either 576 or 1152 depending on MP3 file.
  88. */
  89.  
  90. int
  91. lame_decode1_headersB_clipchoice(unsigned char *buffer, int len,
  92. char pcm_l_raw[], char pcm_r_raw[], mp3data_struct * mp3data,
  93. int *enc_delay, int *enc_padding,
  94. char *p, size_t psize, int decoded_sample_size,
  95. int (*decodeMP3_ptr)(PMPSTR,unsigned char *,int,char *,int,int*) )
  96. {
  97. static const int smpls[2][4] = {
  98. /* Layer I II III */
  99. {0, 384, 1152, 1152}, /* MPEG-1 */
  100. {0, 384, 1152, 576} /* MPEG-2(.5) */
  101. };
  102.  
  103. int processed_bytes;
  104. int processed_samples; /* processed samples per channel */
  105. int ret;
  106. int i;
  107.  
  108. mp3data->header_parsed = 0;
  109.  
  110. ret =
  111. (*decodeMP3_ptr)(&mp, buffer, len, p, psize, &processed_bytes);
  112. /* three cases:
  113. * 1. headers parsed, but data not complete
  114. * mp.header_parsed==1
  115. * mp.framesize=0
  116. * mp.fsizeold=size of last frame, or 0 if this is first frame
  117. *
  118. * 2. headers, data parsed, but ancillary data not complete
  119. * mp.header_parsed==1
  120. * mp.framesize=size of frame
  121. * mp.fsizeold=size of last frame, or 0 if this is first frame
  122. *
  123. * 3. frame fully decoded:
  124. * mp.header_parsed==0
  125. * mp.framesize=0
  126. * mp.fsizeold=size of frame (which is now the last frame)
  127. *
  128. */
  129. if (mp.header_parsed || mp.fsizeold > 0 || mp.framesize > 0) {
  130. mp3data->header_parsed = 1;
  131. mp3data->stereo = mp.fr.stereo;
  132. mp3data->samplerate = freqs[mp.fr.sampling_frequency];
  133. mp3data->mode = mp.fr.mode;
  134. mp3data->mode_ext = mp.fr.mode_ext;
  135. mp3data->framesize = smpls[mp.fr.lsf][mp.fr.lay];
  136.  
  137. /* free format, we need the entire frame before we can determine
  138. * the bitrate. If we haven't gotten the entire frame, bitrate=0 */
  139. if (mp.fsizeold > 0) /* works for free format and fixed, no overrun, temporal results are < 400.e6 */
  140. mp3data->bitrate = 8 * (4 + mp.fsizeold) * mp3data->samplerate /
  141. (1.e3 * mp3data->framesize) + 0.5;
  142. else if (mp.framesize > 0)
  143. mp3data->bitrate = 8 * (4 + mp.framesize) * mp3data->samplerate /
  144. (1.e3 * mp3data->framesize) + 0.5;
  145. else
  146. mp3data->bitrate =
  147. tabsel_123[mp.fr.lsf][mp.fr.lay - 1][mp.fr.bitrate_index];
  148.  
  149.  
  150.  
  151. if (mp.num_frames > 0) {
  152. /* Xing VBR header found and num_frames was set */
  153. mp3data->totalframes = mp.num_frames;
  154. mp3data->nsamp = mp3data->framesize * mp.num_frames;
  155. *enc_delay = mp.enc_delay;
  156. *enc_padding = mp.enc_padding;
  157. }
  158. }
  159.  
  160. switch (ret) {
  161. case MP3_OK:
  162. switch (mp.fr.stereo) {
  163. case 1:
  164. processed_samples = processed_bytes / decoded_sample_size;
  165. if (decoded_sample_size == sizeof(short)) {
  166. COPY_MONO(short,short)
  167. }
  168. else {
  169. COPY_MONO(sample_t,FLOAT)
  170. }
  171. break;
  172. case 2:
  173. processed_samples = (processed_bytes / decoded_sample_size) >> 1;
  174. if (decoded_sample_size == sizeof(short)) {
  175. COPY_STEREO(short,short)
  176. }
  177. else {
  178. COPY_STEREO(sample_t,FLOAT)
  179. }
  180. break;
  181. default:
  182. processed_samples = -1;
  183. assert(0);
  184. break;
  185. }
  186. break;
  187.  
  188. case MP3_NEED_MORE:
  189. processed_samples = 0;
  190. break;
  191.  
  192. default:
  193. assert(0);
  194. case MP3_ERR:
  195. processed_samples = -1;
  196. break;
  197.  
  198. }
  199.  
  200. /*fprintf(stderr,"ok, more, err: %i %i %i\n", MP3_OK, MP3_NEED_MORE, MP3_ERR );*/
  201. /*fprintf(stderr,"ret = %i out=%i\n", ret, processed_samples );*/
  202. return processed_samples;
  203. }
  204.  
  205.  
  206. #define OUTSIZE_CLIPPED 4096*sizeof(short)
  207.  
  208. int
  209. lame_decode1_headersB(unsigned char *buffer,
  210. int len,
  211. short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
  212. int *enc_delay, int *enc_padding)
  213. {
  214. static char out[OUTSIZE_CLIPPED];
  215.  
  216. return lame_decode1_headersB_clipchoice(buffer, len, (char *)pcm_l, (char *)pcm_r, mp3data, enc_delay, enc_padding, out, OUTSIZE_CLIPPED, sizeof(short), decodeMP3 );
  217. }
  218.  
  219.  
  220. /* we forbid input with more than 1152 samples per channel for output in the unclipped mode */
  221. #define OUTSIZE_UNCLIPPED 1152*2*sizeof(FLOAT)
  222.  
  223. int
  224. lame_decode1_unclipped(unsigned char *buffer, int len, sample_t pcm_l[], sample_t pcm_r[])
  225. {
  226. static char out[OUTSIZE_UNCLIPPED];
  227. mp3data_struct mp3data;
  228. int enc_delay,enc_padding;
  229.  
  230. return lame_decode1_headersB_clipchoice(buffer, len, (char *)pcm_l, (char *)pcm_r, &mp3data, &enc_delay, &enc_padding, out, OUTSIZE_UNCLIPPED, sizeof(FLOAT), decodeMP3_unclipped );
  231. }
  232.  
  233.  
  234.  
  235. /*
  236. * For lame_decode: return code
  237. * -1 error
  238. * 0 ok, but need more data before outputing any samples
  239. * n number of samples output. Will be at most one frame of
  240. * MPEG data.
  241. */
  242.  
  243. int
  244. lame_decode1_headers(unsigned char *buffer,
  245. int len,
  246. short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
  247. {
  248. int enc_delay,enc_padding;
  249. return lame_decode1_headersB(buffer,len,pcm_l,pcm_r,mp3data,&enc_delay,&enc_padding);
  250. }
  251.  
  252.  
  253. int
  254. lame_decode1(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
  255. {
  256. mp3data_struct mp3data;
  257.  
  258. return lame_decode1_headers(buffer, len, pcm_l, pcm_r, &mp3data);
  259. }
  260.  
  261.  
  262. /*
  263. * For lame_decode: return code
  264. * -1 error
  265. * 0 ok, but need more data before outputing any samples
  266. * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
  267. */
  268.  
  269. int
  270. lame_decode_headers(unsigned char *buffer,
  271. int len,
  272. short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
  273. {
  274. int ret;
  275. int totsize = 0; /* number of decoded samples per channel */
  276.  
  277. while (1) {
  278. switch (ret =
  279. lame_decode1_headers(buffer, len, pcm_l + totsize,
  280. pcm_r + totsize, mp3data)) {
  281. case -1:
  282. return ret;
  283. case 0:
  284. return totsize;
  285. default:
  286. totsize += ret;
  287. len = 0; /* future calls to decodeMP3 are just to flush buffers */
  288. break;
  289. }
  290. }
  291. }
  292.  
  293.  
  294. int
  295. lame_decode(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
  296. {
  297. mp3data_struct mp3data;
  298.  
  299. return lame_decode_headers(buffer, len, pcm_l, pcm_r, &mp3data);
  300. }
  301.  
  302.  
  303. #endif
  304.  
  305. /* end of mpglib_interface.c */
  306.