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