amiga_mpega.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* MPGLIB replacement using mpega.library (AmigaOS)
  2. * Written by Thomas Wenzel and Sigbjrn (CISC) Skj�et.
  3. *
  4. * Big thanks to St�hane Tavernard for mpega.library.
  5. *
  6. */
  7. /* $Id$ */
  8. #ifdef HAVE_CONFIG_H
  9. #include <config.h>
  10. #endif
  11. #ifdef AMIGA_MPEGA
  12. #define __USE_SYSBASE
  13. #include "lame.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. /* We need a small workaround here so GCC doesn't fail upon redefinition. :P */
  17. #define FLOAT _FLOAT
  18. #include <proto/exec.h>
  19. #include <proto/mpega.h>
  20. #undef _FLOAT
  21. #ifndef __GNUC__
  22. #include <dos.h>
  23. #endif
  24. struct Library *MPEGABase = NULL;
  25. MPEGA_STREAM *mstream = NULL;
  26. MPEGA_CTRL mctrl;
  27. static const int smpls[2][4] = {
  28. /* Layer x I II III */
  29. {0, 384, 1152, 1152}, /* MPEG-1 */
  30. {0, 384, 1152, 576} /* MPEG-2(.5) */
  31. };
  32. #ifndef __GNUC__
  33. static int
  34. break_cleanup(void)
  35. {
  36. /* Dummy break function to make atexit() work. :P */
  37. return 1;
  38. }
  39. #endif
  40. static void
  41. exit_cleanup(void)
  42. {
  43. if (mstream) {
  44. MPEGA_close(mstream);
  45. mstream = NULL;
  46. }
  47. if (MPEGABase) {
  48. CloseLibrary(MPEGABase);
  49. MPEGABase = NULL;
  50. }
  51. }
  52. int
  53. lame_decode_initfile(const char *fullname, mp3data_struct * mp3data)
  54. {
  55. mctrl.bs_access = NULL;
  56. mctrl.layer_1_2.mono.quality = 2;
  57. mctrl.layer_1_2.stereo.quality = 2;
  58. mctrl.layer_1_2.mono.freq_div = 1;
  59. mctrl.layer_1_2.stereo.freq_div = 1;
  60. mctrl.layer_1_2.mono.freq_max = 48000;
  61. mctrl.layer_1_2.stereo.freq_max = 48000;
  62. mctrl.layer_3.mono.quality = 2;
  63. mctrl.layer_3.stereo.quality = 2;
  64. mctrl.layer_3.mono.freq_div = 1;
  65. mctrl.layer_3.stereo.freq_div = 1;
  66. mctrl.layer_3.mono.freq_max = 48000;
  67. mctrl.layer_3.stereo.freq_max = 48000;
  68. mctrl.layer_1_2.force_mono = 0;
  69. mctrl.layer_3.force_mono = 0;
  70. MPEGABase = OpenLibrary("mpega.library", 2);
  71. if (!MPEGABase) {
  72. error_printf("Unable to open mpega.library v2\n");
  73. exit(1);
  74. }
  75. #ifndef __GNUC__
  76. onbreak(break_cleanup);
  77. #endif
  78. atexit(exit_cleanup);
  79. mp3data->header_parsed = 0;
  80. mstream = MPEGA_open((char *) fullname, &mctrl);
  81. if (!mstream)
  82. return (-1);
  83. mp3data->header_parsed = 1;
  84. mp3data->stereo = mstream->dec_channels;
  85. mp3data->samplerate = mstream->dec_frequency;
  86. mp3data->bitrate = mstream->bitrate;
  87. mp3data->nsamp = (float) mstream->ms_duration / 1000 * mstream->dec_frequency;
  88. mp3data->mode = mstream->mode;
  89. mp3data->mode_ext = 0; /* mpega.library doesn't supply this info! :( */
  90. mp3data->framesize = smpls[mstream->norm - 1][mstream->layer];
  91. return 0;
  92. }
  93. int
  94. lame_decode_fromfile(FILE * fd, short pcm_l[], short pcm_r[], mp3data_struct * mp3data)
  95. {
  96. int outsize = 0;
  97. WORD *b[MPEGA_MAX_CHANNELS];
  98. b[0] = pcm_l;
  99. b[1] = pcm_r;
  100. mp3data->header_parsed = 0;
  101. while ((outsize == 0) || (outsize == MPEGA_ERR_BADFRAME)) /* Skip bad frames */
  102. outsize = MPEGA_decode_frame(mstream, b);
  103. if (outsize < 0)
  104. return (-1);
  105. mp3data->header_parsed = 1;
  106. mp3data->stereo = mstream->dec_channels;
  107. mp3data->samplerate = mstream->dec_frequency;
  108. mp3data->bitrate = mstream->bitrate;
  109. mp3data->mode = mstream->mode;
  110. mp3data->mode_ext = 0; /* mpega.library doesn't supply this info! :( */
  111. mp3data->framesize = smpls[mstream->norm - 1][mstream->layer];
  112. return outsize;
  113. }
  114. #endif /* AMIGA_MPEGA */