mpglib_interface.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. * Copyright (c) 2008 Robert Hegemann
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Library General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public
  20. * License along with this library; if not, write to the
  21. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  22. * Boston, MA 02111-1307, USA.
  23. */
  24. /* $Id: mpglib_interface.c,v 1.44 2012/02/18 13:09:00 robert Exp $ */
  25. #ifdef HAVE_CONFIG_H
  26. # include <config.h>
  27. #endif
  28. #ifdef HAVE_MPGLIB
  29. #define hip_global_struct mpstr_tag
  30. #include "lame.h"
  31. #include "machine.h"
  32. #include "encoder.h"
  33. #include "interface.h"
  34. #include "util.h"
  35. #if DEPRECATED_OR_OBSOLETE_CODE_REMOVED
  36. /*
  37. * OBSOLETE:
  38. * - kept to let it link
  39. * - forward declaration to silence compiler
  40. */
  41. int CDECL lame_decode_init(void);
  42. int CDECL lame_decode(
  43. unsigned char * mp3buf,
  44. int len,
  45. short pcm_l[],
  46. short pcm_r[] );
  47. int CDECL lame_decode_headers(
  48. unsigned char* mp3buf,
  49. int len,
  50. short pcm_l[],
  51. short pcm_r[],
  52. mp3data_struct* mp3data );
  53. int CDECL lame_decode1(
  54. unsigned char* mp3buf,
  55. int len,
  56. short pcm_l[],
  57. short pcm_r[] );
  58. int CDECL lame_decode1_headers(
  59. unsigned char* mp3buf,
  60. int len,
  61. short pcm_l[],
  62. short pcm_r[],
  63. mp3data_struct* mp3data );
  64. int CDECL lame_decode1_headersB(
  65. unsigned char* mp3buf,
  66. int len,
  67. short pcm_l[],
  68. short pcm_r[],
  69. mp3data_struct* mp3data,
  70. int *enc_delay,
  71. int *enc_padding );
  72. int CDECL lame_decode_exit(void);
  73. #endif
  74. static MPSTR mp;
  75. int
  76. lame_decode_exit(void)
  77. {
  78. ExitMP3(&mp);
  79. return 0;
  80. }
  81. int
  82. lame_decode_init(void)
  83. {
  84. (void) InitMP3(&mp);
  85. return 0;
  86. }
  87. /* copy mono samples */
  88. #define COPY_MONO(DST_TYPE, SRC_TYPE) \
  89. DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw; \
  90. SRC_TYPE const *p_samples = (SRC_TYPE const *)p; \
  91. for (i = 0; i < processed_samples; i++) \
  92. *pcm_l++ = (DST_TYPE)(*p_samples++);
  93. /* copy stereo samples */
  94. #define COPY_STEREO(DST_TYPE, SRC_TYPE) \
  95. DST_TYPE *pcm_l = (DST_TYPE *)pcm_l_raw, *pcm_r = (DST_TYPE *)pcm_r_raw; \
  96. SRC_TYPE const *p_samples = (SRC_TYPE const *)p; \
  97. for (i = 0; i < processed_samples; i++) { \
  98. *pcm_l++ = (DST_TYPE)(*p_samples++); \
  99. *pcm_r++ = (DST_TYPE)(*p_samples++); \
  100. }
  101. /*
  102. * For lame_decode: return code
  103. * -1 error
  104. * 0 ok, but need more data before outputing any samples
  105. * n number of samples output. either 576 or 1152 depending on MP3 file.
  106. */
  107. static int
  108. decode1_headersB_clipchoice(PMPSTR pmp, unsigned char *buffer, size_t len,
  109. char pcm_l_raw[], char pcm_r_raw[], mp3data_struct * mp3data,
  110. int *enc_delay, int *enc_padding,
  111. char *p, size_t psize, int decoded_sample_size,
  112. int (*decodeMP3_ptr) (PMPSTR, unsigned char *, int, char *, int,
  113. int *))
  114. {
  115. static const int smpls[2][4] = {
  116. /* Layer I II III */
  117. {0, 384, 1152, 1152}, /* MPEG-1 */
  118. {0, 384, 1152, 576} /* MPEG-2(.5) */
  119. };
  120. int processed_bytes;
  121. int processed_samples; /* processed samples per channel */
  122. int ret;
  123. int i;
  124. int const len_l = len < INT_MAX ? (int) len : INT_MAX;
  125. int const psize_l = psize < INT_MAX ? (int) psize : INT_MAX;
  126. mp3data->header_parsed = 0;
  127. ret = (*decodeMP3_ptr) (pmp, buffer, len_l, p, psize_l, &processed_bytes);
  128. /* three cases:
  129. * 1. headers parsed, but data not complete
  130. * pmp->header_parsed==1
  131. * pmp->framesize=0
  132. * pmp->fsizeold=size of last frame, or 0 if this is first frame
  133. *
  134. * 2. headers, data parsed, but ancillary data not complete
  135. * pmp->header_parsed==1
  136. * pmp->framesize=size of frame
  137. * pmp->fsizeold=size of last frame, or 0 if this is first frame
  138. *
  139. * 3. frame fully decoded:
  140. * pmp->header_parsed==0
  141. * pmp->framesize=0
  142. * pmp->fsizeold=size of frame (which is now the last frame)
  143. *
  144. */
  145. if (pmp->header_parsed || pmp->fsizeold > 0 || pmp->framesize > 0) {
  146. mp3data->header_parsed = 1;
  147. mp3data->stereo = pmp->fr.stereo;
  148. mp3data->samplerate = freqs[pmp->fr.sampling_frequency];
  149. mp3data->mode = pmp->fr.mode;
  150. mp3data->mode_ext = pmp->fr.mode_ext;
  151. mp3data->framesize = smpls[pmp->fr.lsf][pmp->fr.lay];
  152. /* free format, we need the entire frame before we can determine
  153. * the bitrate. If we haven't gotten the entire frame, bitrate=0 */
  154. if (pmp->fsizeold > 0) /* works for free format and fixed, no overrun, temporal results are < 400.e6 */
  155. mp3data->bitrate = 8 * (4 + pmp->fsizeold) * mp3data->samplerate /
  156. (1.e3 * mp3data->framesize) + 0.5;
  157. else if (pmp->framesize > 0)
  158. mp3data->bitrate = 8 * (4 + pmp->framesize) * mp3data->samplerate /
  159. (1.e3 * mp3data->framesize) + 0.5;
  160. else
  161. mp3data->bitrate = tabsel_123[pmp->fr.lsf][pmp->fr.lay - 1][pmp->fr.bitrate_index];
  162. if (pmp->num_frames > 0) {
  163. /* Xing VBR header found and num_frames was set */
  164. mp3data->totalframes = pmp->num_frames;
  165. mp3data->nsamp = mp3data->framesize * pmp->num_frames;
  166. *enc_delay = pmp->enc_delay;
  167. *enc_padding = pmp->enc_padding;
  168. }
  169. }
  170. switch (ret) {
  171. case MP3_OK:
  172. switch (pmp->fr.stereo) {
  173. case 1:
  174. processed_samples = processed_bytes / decoded_sample_size;
  175. if (decoded_sample_size == sizeof(short)) {
  176. COPY_MONO(short, short)
  177. }
  178. else {
  179. COPY_MONO(sample_t, FLOAT)
  180. }
  181. break;
  182. case 2:
  183. processed_samples = (processed_bytes / decoded_sample_size) >> 1;
  184. if (decoded_sample_size == sizeof(short)) {
  185. COPY_STEREO(short, short)
  186. }
  187. else {
  188. COPY_STEREO(sample_t, FLOAT)
  189. }
  190. break;
  191. default:
  192. processed_samples = -1;
  193. assert(0);
  194. break;
  195. }
  196. break;
  197. case MP3_NEED_MORE:
  198. processed_samples = 0;
  199. break;
  200. case MP3_ERR:
  201. processed_samples = -1;
  202. break;
  203. default:
  204. processed_samples = -1;
  205. assert(0);
  206. break;
  207. }
  208. /*fprintf(stderr,"ok, more, err: %i %i %i\n", MP3_OK, MP3_NEED_MORE, MP3_ERR ); */
  209. /*fprintf(stderr,"ret = %i out=%i\n", ret, processed_samples ); */
  210. return processed_samples;
  211. }
  212. #define OUTSIZE_CLIPPED (4096*sizeof(short))
  213. int
  214. lame_decode1_headersB(unsigned char *buffer,
  215. int len,
  216. short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
  217. int *enc_delay, int *enc_padding)
  218. {
  219. static char out[OUTSIZE_CLIPPED];
  220. return decode1_headersB_clipchoice(&mp, buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
  221. enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
  222. sizeof(short), decodeMP3);
  223. }
  224. /*
  225. * For lame_decode: return code
  226. * -1 error
  227. * 0 ok, but need more data before outputing any samples
  228. * n number of samples output. Will be at most one frame of
  229. * MPEG data.
  230. */
  231. int
  232. lame_decode1_headers(unsigned char *buffer,
  233. int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
  234. {
  235. int enc_delay, enc_padding;
  236. return lame_decode1_headersB(buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
  237. }
  238. int
  239. lame_decode1(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
  240. {
  241. mp3data_struct mp3data;
  242. return lame_decode1_headers(buffer, len, pcm_l, pcm_r, &mp3data);
  243. }
  244. /*
  245. * For lame_decode: return code
  246. * -1 error
  247. * 0 ok, but need more data before outputing any samples
  248. * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
  249. */
  250. int
  251. lame_decode_headers(unsigned char *buffer,
  252. int len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
  253. {
  254. int ret;
  255. int totsize = 0; /* number of decoded samples per channel */
  256. for (;;) {
  257. switch (ret = lame_decode1_headers(buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
  258. case -1:
  259. return ret;
  260. case 0:
  261. return totsize;
  262. default:
  263. totsize += ret;
  264. len = 0; /* future calls to decodeMP3 are just to flush buffers */
  265. break;
  266. }
  267. }
  268. }
  269. int
  270. lame_decode(unsigned char *buffer, int len, short pcm_l[], short pcm_r[])
  271. {
  272. mp3data_struct mp3data;
  273. return lame_decode_headers(buffer, len, pcm_l, pcm_r, &mp3data);
  274. }
  275. hip_t hip_decode_init(void)
  276. {
  277. hip_t hip = lame_calloc(hip_global_flags, 1);
  278. InitMP3(hip);
  279. return hip;
  280. }
  281. int hip_decode_exit(hip_t hip)
  282. {
  283. if (hip) {
  284. ExitMP3(hip);
  285. free(hip);
  286. }
  287. return 0;
  288. }
  289. /* we forbid input with more than 1152 samples per channel for output in the unclipped mode */
  290. #define OUTSIZE_UNCLIPPED (1152*2*sizeof(FLOAT))
  291. int
  292. hip_decode1_unclipped(hip_t hip, unsigned char *buffer, size_t len, sample_t pcm_l[], sample_t pcm_r[])
  293. {
  294. static char out[OUTSIZE_UNCLIPPED];
  295. mp3data_struct mp3data;
  296. int enc_delay, enc_padding;
  297. if (hip) {
  298. return decode1_headersB_clipchoice(hip, buffer, len, (char *) pcm_l, (char *) pcm_r, &mp3data,
  299. &enc_delay, &enc_padding, out, OUTSIZE_UNCLIPPED,
  300. sizeof(FLOAT), decodeMP3_unclipped);
  301. }
  302. return 0;
  303. }
  304. /*
  305. * For hip_decode: return code
  306. * -1 error
  307. * 0 ok, but need more data before outputing any samples
  308. * n number of samples output. Will be at most one frame of
  309. * MPEG data.
  310. */
  311. int
  312. hip_decode1_headers(hip_t hip, unsigned char *buffer,
  313. size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
  314. {
  315. int enc_delay, enc_padding;
  316. return hip_decode1_headersB(hip, buffer, len, pcm_l, pcm_r, mp3data, &enc_delay, &enc_padding);
  317. }
  318. int
  319. hip_decode1(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
  320. {
  321. mp3data_struct mp3data;
  322. return hip_decode1_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
  323. }
  324. /*
  325. * For hip_decode: return code
  326. * -1 error
  327. * 0 ok, but need more data before outputing any samples
  328. * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
  329. */
  330. int
  331. hip_decode_headers(hip_t hip, unsigned char *buffer,
  332. size_t len, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
  333. {
  334. int ret;
  335. int totsize = 0; /* number of decoded samples per channel */
  336. for (;;) {
  337. switch (ret = hip_decode1_headers(hip, buffer, len, pcm_l + totsize, pcm_r + totsize, mp3data)) {
  338. case -1:
  339. return ret;
  340. case 0:
  341. return totsize;
  342. default:
  343. totsize += ret;
  344. len = 0; /* future calls to decodeMP3 are just to flush buffers */
  345. break;
  346. }
  347. }
  348. }
  349. int
  350. hip_decode(hip_t hip, unsigned char *buffer, size_t len, short pcm_l[], short pcm_r[])
  351. {
  352. mp3data_struct mp3data;
  353. return hip_decode_headers(hip, buffer, len, pcm_l, pcm_r, &mp3data);
  354. }
  355. int
  356. hip_decode1_headersB(hip_t hip, unsigned char *buffer,
  357. size_t len,
  358. short pcm_l[], short pcm_r[], mp3data_struct * mp3data,
  359. int *enc_delay, int *enc_padding)
  360. {
  361. static char out[OUTSIZE_CLIPPED];
  362. if (hip) {
  363. return decode1_headersB_clipchoice(hip, buffer, len, (char *) pcm_l, (char *) pcm_r, mp3data,
  364. enc_delay, enc_padding, out, OUTSIZE_CLIPPED,
  365. sizeof(short), decodeMP3);
  366. }
  367. return -1;
  368. }
  369. void hip_set_pinfo(hip_t hip, plotting_data* pinfo)
  370. {
  371. if (hip) {
  372. hip->pinfo = pinfo;
  373. }
  374. }
  375. void hip_set_errorf(hip_t hip, lame_report_function func)
  376. {
  377. if (hip) {
  378. hip->report_err = func;
  379. }
  380. }
  381. void hip_set_debugf(hip_t hip, lame_report_function func)
  382. {
  383. if (hip) {
  384. hip->report_dbg = func;
  385. }
  386. }
  387. void hip_set_msgf (hip_t hip, lame_report_function func)
  388. {
  389. if (hip) {
  390. hip->report_msg = func;
  391. }
  392. }
  393. #endif
  394. /* end of mpglib_interface.c */