| 1 |
/* dvbcut |
| 2 |
Copyright (c) 2005-2007 Sven Over <[email protected]> |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program 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 |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* $Id$ */ |
| 20 |
|
| 21 |
#include <cstring> |
| 22 |
#include <stdint.h> |
| 23 |
#include <algorithm> |
| 24 |
#include <string> |
| 25 |
#include <sstream> |
| 26 |
|
| 27 |
#include "playaudio.h" |
| 28 |
#include "exception.h" |
| 29 |
|
| 30 |
#ifdef HAVE_LIB_AO |
| 31 |
#include <ao/ao.h> |
| 32 |
|
| 33 |
/// Simple class for audio output through libao |
| 34 |
class audioout |
| 35 |
{ |
| 36 |
protected: |
| 37 |
ao_device *m_device; |
| 38 |
int m_channels, m_samplerate; |
| 39 |
public: |
| 40 |
audioout() : m_device(NULL), m_channels(0), m_samplerate(0) {} |
| 41 |
~audioout() |
| 42 |
{ |
| 43 |
if (m_device) |
| 44 |
ao_close(m_device); |
| 45 |
} |
| 46 |
|
| 47 |
void play(int channels, int samplerate, int16_t *data, int bytes) |
| 48 |
{ |
| 49 |
if ((not m_device) or channels!=m_channels or samplerate!=m_samplerate) |
| 50 |
{ |
| 51 |
if (m_device) |
| 52 |
{ |
| 53 |
ao_close(m_device); |
| 54 |
m_device=NULL; |
| 55 |
} |
| 56 |
|
| 57 |
ao_sample_format format; |
| 58 |
// zero-initialize |
| 59 |
memset(&format, 0, sizeof(format)); |
| 60 |
|
| 61 |
format.bits = 16; |
| 62 |
format.channels = channels; |
| 63 |
format.rate = samplerate; |
| 64 |
format.byte_format = AO_FMT_LITTLE; |
| 65 |
|
| 66 |
m_channels=channels; |
| 67 |
m_samplerate=samplerate; |
| 68 |
|
| 69 |
m_device = ao_open_live(ao_default_driver_id(), &format, NULL /* no options */ ); |
| 70 |
if (!m_device) |
| 71 |
throw dvbcut_exception("Error setting up audio output"); |
| 72 |
} |
| 73 |
|
| 74 |
ao_play(m_device, (char*) data, bytes); |
| 75 |
} |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
#ifdef HAVE_LIB_A52 |
| 80 |
extern "C" |
| 81 |
{ |
| 82 |
#include <a52dec/a52.h> |
| 83 |
#include <a52dec/mm_accel.h> |
| 84 |
} |
| 85 |
|
| 86 |
/// Simple class for AC3 decoding |
| 87 |
class a52dec |
| 88 |
{ |
| 89 |
protected: |
| 90 |
a52_state_t *m_state; |
| 91 |
public: |
| 92 |
a52dec() : m_state(0) |
| 93 |
{ |
| 94 |
m_state=a52_init(MM_ACCEL_DJBFFT); |
| 95 |
|
| 96 |
if (not m_state) |
| 97 |
throw dvbcut_exception("Error setting up AC3 decoder"); |
| 98 |
} |
| 99 |
~a52dec() |
| 100 |
{ |
| 101 |
a52_free(m_state); |
| 102 |
} |
| 103 |
const sample_t *samples() |
| 104 |
{ |
| 105 |
return a52_samples(m_state); |
| 106 |
} |
| 107 |
int syncinfo(uint8_t *buffer, int &flags, int &sample_rate, int &bitrate) const |
| 108 |
{ |
| 109 |
return a52_syncinfo(buffer,&flags,&sample_rate,&bitrate); |
| 110 |
} |
| 111 |
int frame(uint8_t *buffer, int &flags, sample_t &level, sample_t bias) |
| 112 |
{ |
| 113 |
return a52_frame(m_state, buffer, &flags, &level, bias); |
| 114 |
} |
| 115 |
int block() |
| 116 |
{ |
| 117 |
return a52_block(m_state); |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
void playaudio_ac3(const void *data, uint32_t len) |
| 122 |
{ |
| 123 |
uint8_t *d=const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(data)); |
| 124 |
|
| 125 |
a52dec dec; |
| 126 |
audioout aout; |
| 127 |
int flags=0, sample_rate=0, bit_rate=0; |
| 128 |
sample_t level; |
| 129 |
|
| 130 |
while (len>=7) |
| 131 |
{ |
| 132 |
int bytes=dec.syncinfo(d,flags,sample_rate,bit_rate); |
| 133 |
|
| 134 |
if (bytes==0) |
| 135 |
{ |
| 136 |
++d; |
| 137 |
--len; |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
if (bytes>signed(len)) |
| 142 |
break; |
| 143 |
|
| 144 |
flags=A52_STEREO | A52_ADJUST_LEVEL; |
| 145 |
level=1; |
| 146 |
if (dec.frame(d,flags,level,0)!=0) |
| 147 |
throw dvbcut_exception("Error while decoding AC3 data"); |
| 148 |
if ((flags&A52_CHANNEL_MASK)!=A52_STEREO) |
| 149 |
throw dvbcut_exception("Error while decoding AC3 data (non-stereo output)"); |
| 150 |
for (int i=0;i<6;++i) |
| 151 |
{ |
| 152 |
if (dec.block()!=0) |
| 153 |
dvbcut_exception("Error while decoding AC3 data"); |
| 154 |
int16_t samples[256*2]; |
| 155 |
const sample_t *decoded=dec.samples(); |
| 156 |
for(int j=0;j<256;++j) |
| 157 |
{ |
| 158 |
samples[j*2+0]=int16_t(decoded[j]*sample_t(32767)); |
| 159 |
samples[j*2+1]=int16_t(decoded[j+256]*sample_t(32767)); |
| 160 |
} |
| 161 |
aout.play(2,sample_rate,samples,256*2*2); //dec.samples(); |
| 162 |
} |
| 163 |
|
| 164 |
len-=bytes; |
| 165 |
d+=bytes; |
| 166 |
} |
| 167 |
|
| 168 |
// throw dvbcut_exception("AC3 playback not yet implemented"); |
| 169 |
} |
| 170 |
#else // HAVE_LIB_A52 |
| 171 |
void playaudio_ac3(const void *, uint32_t) |
| 172 |
{ |
| 173 |
throw dvbcut_exception("DVBCUT was built without AC3 support"); |
| 174 |
} |
| 175 |
#endif // ifdef HAVE_LIB_A52 |
| 176 |
|
| 177 |
|
| 178 |
#ifdef HAVE_LIB_MAD |
| 179 |
#include <mad.h> |
| 180 |
|
| 181 |
/// Simple class for MP2 decoding and output throught the audioout class |
| 182 |
class mp2dec |
| 183 |
{ |
| 184 |
protected: |
| 185 |
mad_decoder m_decoder; |
| 186 |
const void *m_data; |
| 187 |
uint32_t m_len; |
| 188 |
audioout &m_aout; |
| 189 |
std::string m_error; |
| 190 |
|
| 191 |
static enum mad_flow input(void *data,struct mad_stream *stream) |
| 192 |
{ |
| 193 |
mp2dec *This=reinterpret_cast<mp2dec*>(data); |
| 194 |
|
| 195 |
if (This->m_len==0) |
| 196 |
return MAD_FLOW_STOP; |
| 197 |
|
| 198 |
mad_stream_buffer(stream, reinterpret_cast<const unsigned char*>(This->m_data), This->m_len); |
| 199 |
This->m_len=0; |
| 200 |
|
| 201 |
return MAD_FLOW_CONTINUE; |
| 202 |
} |
| 203 |
|
| 204 |
static enum mad_flow output(void *data, |
| 205 |
struct mad_header const */*header*/, |
| 206 |
struct mad_pcm *pcm) |
| 207 |
{ |
| 208 |
mp2dec *This=reinterpret_cast<mp2dec*>(data); |
| 209 |
|
| 210 |
unsigned int channels=pcm->channels; |
| 211 |
if (channels>2) |
| 212 |
channels=2; |
| 213 |
|
| 214 |
const unsigned int samples=pcm->length; |
| 215 |
|
| 216 |
mad_fixed_t *sample[channels]; |
| 217 |
for (unsigned int i=0;i<channels;++i) |
| 218 |
sample[i]=pcm->samples[i]; |
| 219 |
|
| 220 |
int16_t buffer[samples*channels]; |
| 221 |
|
| 222 |
// Transform the data delivered by libmad into samples accepted by libao. |
| 223 |
for (unsigned int s=0;s<samples;++s) |
| 224 |
for (unsigned int c=0;c<channels;++c) |
| 225 |
{ |
| 226 |
mad_fixed_t S=*(sample[c]++); |
| 227 |
|
| 228 |
S+=(1L << (MAD_F_FRACBITS - 16)); // round |
| 229 |
|
| 230 |
// clip |
| 231 |
S=std::max(std::min(S,MAD_F_ONE-1),-MAD_F_ONE); |
| 232 |
|
| 233 |
// ...and convert to 16-bit signed integer |
| 234 |
buffer[s*channels+c]=S >> (MAD_F_FRACBITS + 1 - 16); |
| 235 |
} |
| 236 |
|
| 237 |
This->m_aout.play(channels,pcm->samplerate,buffer,samples*channels*2); |
| 238 |
|
| 239 |
return MAD_FLOW_CONTINUE; |
| 240 |
} |
| 241 |
|
| 242 |
static enum mad_flow error(void *data, |
| 243 |
struct mad_stream *stream, |
| 244 |
struct mad_frame */*frame*/) |
| 245 |
{ |
| 246 |
mp2dec *This=reinterpret_cast<mp2dec*>(data); |
| 247 |
|
| 248 |
std::ostringstream out; |
| 249 |
|
| 250 |
out << "MP2 decoding error " << int(stream->error) << " (" << mad_stream_errorstr(stream) |
| 251 |
<< ") at byte offset " << int(stream->this_frame - reinterpret_cast<const unsigned char*>(This->m_data)); |
| 252 |
|
| 253 |
This->m_error=out.str(); |
| 254 |
return MAD_FLOW_CONTINUE; |
| 255 |
} |
| 256 |
|
| 257 |
public: |
| 258 |
mp2dec(const void *data, uint32_t len, audioout &aout) : m_data(data),m_len(len),m_aout(aout) |
| 259 |
{ |
| 260 |
mad_decoder_init(&m_decoder,this,input,0,0,output,error,0); |
| 261 |
} |
| 262 |
~mp2dec() |
| 263 |
{ |
| 264 |
mad_decoder_finish(&m_decoder); |
| 265 |
} |
| 266 |
void run() |
| 267 |
{ |
| 268 |
if (mad_decoder_run(&m_decoder,MAD_DECODER_MODE_SYNC)!=0) |
| 269 |
{ |
| 270 |
if (m_error.empty()) |
| 271 |
throw dvbcut_exception("Error decoding/playing MP2 audio"); |
| 272 |
else |
| 273 |
throw dvbcut_exception("Error decoding/playing MP2 audio: "+m_error); |
| 274 |
} |
| 275 |
} |
| 276 |
}; |
| 277 |
|
| 278 |
void playaudio_mp2(const void *data, uint32_t len) |
| 279 |
{ |
| 280 |
audioout aout; |
| 281 |
mp2dec(data,len,aout).run(); |
| 282 |
} |
| 283 |
#else // ifdef HAVE_LIB_MAD |
| 284 |
void playaudio_mp2(const void *data, uint32_t len) |
| 285 |
{ |
| 286 |
throw dvbcut_exception("DVBCUT was built without MP2 support"); |
| 287 |
} |
| 288 |
#endif // ifdef HAVE_LIB_MAD |
| 289 |
|
| 290 |
// If DVBCUT is compiled without audio support, these dummy functions |
| 291 |
// will just throw an exception. However, they should never get called, as |
| 292 |
// the Play Audio actions should be disabled anyhow. |
| 293 |
#else // ifdef HAVE_LIB_AO |
| 294 |
void playaudio_ac3(const void *, uint32_t) |
| 295 |
{ |
| 296 |
throw dvbcut_exception("DVBCUT was built without support for audio output"); |
| 297 |
} |
| 298 |
void playaudio_mp2(const void *, uint32_t) |
| 299 |
{ |
| 300 |
throw dvbcut_exception("DVBCUT was built without support for audio output"); |
| 301 |
} |
| 302 |
#endif // ifdef HAVE_LIB_AO |