2 * Copyright (C) 2006-2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
25 #include <liboil/liboil.h>
28 #include "swfdec_codec_audio.h"
29 #include "swfdec_debug.h"
30 #include "swfdec_internal.h"
33 SwfdecAudioDecoder decoder
;
35 struct mad_stream stream
;
36 struct mad_frame frame
;
37 struct mad_synth synth
;
38 guint8 data
[MAD_BUFFER_MDLEN
* 3];
40 SwfdecBufferQueue
* queue
;
44 convert_synth_to_buffer (MadData
*mdata
)
51 #define MAD_F_TO_S16(x) (CLAMP (x, -MAD_F_ONE, MAD_F_ONE) >> (MAD_F_FRACBITS - 14))
53 n_samples
= mdata
->synth
.pcm
.length
;
58 switch (mdata
->synth
.pcm
.samplerate
) {
68 SWFDEC_ERROR ("sample rate not handled (%d)",
69 mdata
->synth
.pcm
.samplerate
);
73 buffer
= swfdec_buffer_new_and_alloc (n_samples
* 2 * 2);
74 data
= (gint16
*) buffer
->data
;
76 if (mdata
->synth
.pcm
.samplerate
== 11025) {
77 if (mdata
->synth
.pcm
.channels
== 2) {
78 for (i
= 0; i
< mdata
->synth
.pcm
.length
; i
++) {
79 c0
= MAD_F_TO_S16 (mdata
->synth
.pcm
.samples
[0][i
]);
80 c1
= MAD_F_TO_S16 (mdata
->synth
.pcm
.samples
[1][i
]);
91 for (i
= 0; i
< mdata
->synth
.pcm
.length
; i
++) {
92 c0
= MAD_F_TO_S16( mdata
->synth
.pcm
.samples
[0][i
]);
103 } else if (mdata
->synth
.pcm
.samplerate
== 22050) {
104 if (mdata
->synth
.pcm
.channels
== 2) {
105 for (i
= 0; i
< mdata
->synth
.pcm
.length
; i
++) {
106 c0
= MAD_F_TO_S16 (mdata
->synth
.pcm
.samples
[0][i
]);
107 c1
= MAD_F_TO_S16 (mdata
->synth
.pcm
.samples
[1][i
]);
114 for (i
= 0; i
< mdata
->synth
.pcm
.length
; i
++) {
115 c0
= MAD_F_TO_S16 (mdata
->synth
.pcm
.samples
[0][i
]);
122 } else if (mdata
->synth
.pcm
.samplerate
== 44100) {
123 if (mdata
->synth
.pcm
.channels
== 2) {
124 for (i
= 0; i
< mdata
->synth
.pcm
.length
; i
++) {
125 c0
= MAD_F_TO_S16 (mdata
->synth
.pcm
.samples
[0][i
]);
126 c1
= MAD_F_TO_S16 (mdata
->synth
.pcm
.samples
[1][i
]);
131 for (i
= 0; i
< mdata
->synth
.pcm
.length
; i
++) {
132 c0
= MAD_F_TO_S16 (mdata
->synth
.pcm
.samples
[0][i
]);
138 SWFDEC_ERROR ("sample rate not handled (%d)",
139 mdata
->synth
.pcm
.samplerate
);
145 swfdec_audio_decoder_mad_push (SwfdecAudioDecoder
*dec
, SwfdecBuffer
*buffer
)
147 MadData
*data
= (MadData
*) dec
;
148 SwfdecBuffer
*out
, *empty
= NULL
;
149 guint amount
= 0, size
;
151 if (buffer
== NULL
) {
152 buffer
= empty
= swfdec_buffer_new ();
153 empty
->data
= g_malloc0 (MAD_BUFFER_GUARD
* 3);
154 empty
->length
= MAD_BUFFER_GUARD
* 3;
157 //write (1, buffer->data, buffer->length);
158 //g_print ("buffer %p gave us %u bytes\n", buffer, buffer->length);
159 while (amount
< buffer
->length
) {
160 size
= MIN (buffer
->length
- amount
, MAD_BUFFER_MDLEN
* 3 - data
->data_len
);
161 memcpy (&data
->data
[data
->data_len
], buffer
->data
+ amount
, size
);
162 //write (1, buffer->data + amount, size);
164 data
->data_len
+= size
;
165 mad_stream_buffer (&data
->stream
, data
->data
, data
->data_len
);
167 if (mad_frame_decode (&data
->frame
, &data
->stream
)) {
168 if (data
->stream
.error
== MAD_ERROR_BUFLEN
)
170 if (MAD_RECOVERABLE (data
->stream
.error
)) {
171 SWFDEC_LOG ("recoverable error 0x%04x", data
->stream
.error
);
174 SWFDEC_ERROR ("stream error 0x%04x", data
->stream
.error
);
178 mad_synth_frame (&data
->synth
, &data
->frame
);
179 out
= convert_synth_to_buffer (data
);
181 swfdec_buffer_queue_push (data
->queue
, out
);
183 if (data
->stream
.next_frame
== NULL
) {
186 data
->data_len
= data
->stream
.bufend
- data
->stream
.next_frame
;
187 memmove (data
->data
, data
->stream
.next_frame
, data
->data_len
);
190 //g_print ("%u bytes left\n", data->data_len);
193 swfdec_buffer_unref (empty
);
197 swfdec_audio_decoder_mad_free (SwfdecAudioDecoder
*dec
)
199 MadData
*data
= (MadData
*) dec
;
201 mad_synth_finish (&data
->synth
);
202 mad_frame_finish (&data
->frame
);
203 mad_stream_finish (&data
->stream
);
204 swfdec_buffer_queue_unref (data
->queue
);
205 g_slice_free (MadData
, data
);
208 static SwfdecBuffer
*
209 swfdec_audio_decoder_mad_pull (SwfdecAudioDecoder
*dec
)
211 return swfdec_buffer_queue_pull_buffer (((MadData
*) dec
)->queue
);
215 swfdec_audio_decoder_mad_new (guint type
, SwfdecAudioFormat format
)
219 if (type
!= SWFDEC_AUDIO_CODEC_MP3
)
222 data
= g_slice_new (MadData
);
223 data
->decoder
.format
= swfdec_audio_format_new (44100, 2, TRUE
);
224 data
->decoder
.push
= swfdec_audio_decoder_mad_push
;
225 data
->decoder
.pull
= swfdec_audio_decoder_mad_pull
;
226 data
->decoder
.free
= swfdec_audio_decoder_mad_free
;
227 mad_stream_init (&data
->stream
);
228 mad_frame_init (&data
->frame
);
229 mad_synth_init (&data
->synth
);
231 data
->queue
= swfdec_buffer_queue_new ();
233 return &data
->decoder
;