mpglib_interface.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* $Id: mpglib_interface.c,v 1.18 2001/01/06 01:00:33 markt Exp $ */
  2. #ifdef HAVE_CONFIG_H
  3. # include <config.h>
  4. #endif
  5. #ifdef HAVE_MPGLIB
  6. #include <limits.h>
  7. #include <stdlib.h>
  8. #include <assert.h>
  9. #include "interface.h"
  10. #include "lame.h"
  11. #ifdef WITH_DMALLOC
  12. #include <dmalloc.h>
  13. #endif
  14. MPSTR mp;
  15. plotting_data* mpg123_pinfo = NULL;
  16. int lame_decode_init( void )
  17. {
  18. InitMP3 ( &mp );
  19. return 0;
  20. }
  21. /*
  22. * For lame_decode: return code
  23. * -1 error
  24. * 0 ok, but need more data before outputing any samples
  25. * n number of samples output. either 576 or 1152 depending on MP3 file.
  26. */
  27. int lame_decode1_headers(
  28. unsigned char* buffer,
  29. int len,
  30. short pcm_l [],
  31. short pcm_r [],
  32. mp3data_struct* mp3data )
  33. {
  34. static const int smpls [2] [4] = {
  35. /* Layer I II III */
  36. { 0, 384, 1152, 1152 }, /* MPEG-1 */
  37. { 0, 384, 1152, 576 } /* MPEG-2(.5) */
  38. };
  39. static char out [8192];
  40. signed short int* p = (signed short int*) out;
  41. int processed_bytes;
  42. int processed_samples; // processed samples per channel
  43. int ret;
  44. int i;
  45. mp3data->header_parsed = 0;
  46. ret = decodeMP3 ( &mp, buffer, len, (char*)p, sizeof(out), &processed_bytes );
  47. if ( mp.header_parsed ) {
  48. mp3data->header_parsed = 1;
  49. mp3data->stereo = mp.fr.stereo;
  50. mp3data->samplerate = freqs [mp.fr.sampling_frequency];
  51. mp3data->mode = mp.fr.mode;
  52. mp3data->mode_ext = mp.fr.mode_ext;
  53. mp3data->framesize = smpls [mp.fr.lsf] [mp.fr.lay];
  54. if (mp.fsizeold > 0) /* works for free format and fixed, no overrun, temporal results are < 400.e6 */
  55. mp3data->bitrate = 8 * (4 + mp.fsizeold) * mp3data->samplerate /
  56. ( 1.e3 * mp3data->framesize ) + 0.5;
  57. else
  58. mp3data->bitrate = tabsel_123 [mp.fr.lsf] [mp.fr.lay-1] [mp.fr.bitrate_index];
  59. if (mp.num_frames>0) {
  60. /* Xing VBR header found and num_frames was set */
  61. mp3data->totalframes = mp.num_frames;
  62. mp3data->nsamp=mp3data->framesize * mp.num_frames;
  63. }
  64. }
  65. switch ( ret ) {
  66. case MP3_OK:
  67. switch ( mp.fr.stereo ) {
  68. case 1:
  69. processed_samples = processed_bytes >> 1;
  70. for ( i = 0; i < processed_samples; i++ )
  71. pcm_l [i] = *p++;
  72. break;
  73. case 2:
  74. processed_samples = processed_bytes >> 2;
  75. for ( i = 0; i < processed_samples; i++ ) {
  76. pcm_l [i] = *p++;
  77. pcm_r [i] = *p++;
  78. }
  79. break;
  80. default:
  81. processed_samples = -1;
  82. assert (0);
  83. break;
  84. }
  85. break;
  86. case MP3_NEED_MORE:
  87. processed_samples = 0;
  88. break;
  89. default:
  90. assert (0);
  91. case MP3_ERR:
  92. processed_samples = -1;
  93. break;
  94. }
  95. // printf ( "ok, more, err: %i %i %i\n", MP3_OK, MP3_NEED_MORE, MP3_ERR );
  96. // printf ( "ret = %i out=%i\n", ret, totsize );
  97. return processed_samples;
  98. }
  99. /*
  100. * For lame_decode: return code
  101. * -1 error
  102. * 0 ok, but need more data before outputing any samples
  103. * n number of samples output. Will be at most one frame of
  104. * MPEG data.
  105. */
  106. int lame_decode1(
  107. unsigned char* buffer,
  108. int len,
  109. short pcm_l [],
  110. short pcm_r [] )
  111. {
  112. mp3data_struct mp3data;
  113. return lame_decode1_headers ( buffer, len, pcm_l, pcm_r, &mp3data );
  114. }
  115. /*
  116. * For lame_decode: return code
  117. * -1 error
  118. * 0 ok, but need more data before outputing any samples
  119. * n number of samples output. a multiple of 576 or 1152 depending on MP3 file.
  120. */
  121. int lame_decode_headers(
  122. unsigned char* buffer,
  123. int len,
  124. short pcm_l [],
  125. short pcm_r [],
  126. mp3data_struct* mp3data )
  127. {
  128. int ret;
  129. int totsize = 0; // number of decoded samples per channel
  130. while (1) {
  131. switch ( ret = lame_decode1_headers ( buffer, len, pcm_l+totsize, pcm_r+totsize, mp3data ) ) {
  132. case -1: return ret;
  133. case 0: return totsize;
  134. default: totsize += ret;
  135. len = 0; /* future calls to decodeMP3 are just to flush buffers */
  136. break;
  137. }
  138. }
  139. }
  140. int lame_decode(
  141. unsigned char* buffer,
  142. int len,
  143. short pcm_l [],
  144. short pcm_r [] )
  145. {
  146. mp3data_struct mp3data;
  147. return lame_decode_headers ( buffer, len, pcm_l, pcm_r, &mp3data );
  148. }
  149. #endif
  150. /* end of mpglib_interface.c */