00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <iostream>
00004 #include <math.h>
00005 #include <stdio.h>
00006 #include <qregexp.h>
00007 #include <sys/stat.h>
00008 using namespace std;
00009
00010 #include <mad.h>
00011
00012 #include "maddecoder.h"
00013 #include "metadata.h"
00014 #include "constants.h"
00015 #include <mythtv/audiooutput.h>
00016 #include "metaiotaglib.h"
00017
00018 #include <mythtv/mythconfig.h>
00019 #include <mythtv/mythcontext.h>
00020
00021 #define XING_MAGIC (('X' << 24) | ('i' << 16) | ('n' << 8) | 'g')
00022
00023 MadDecoder::MadDecoder(const QString &file, DecoderFactory *d, QIODevice *i,
00024 AudioOutput *o)
00025 : Decoder(d, i, o)
00026 {
00027 filename = file;
00028 inited = false;
00029 user_stop = false;
00030 done = false;
00031 finish = false;
00032 derror = false;
00033 eof = false;
00034 totalTime = 0.;
00035 seekTime = -1.;
00036 stat = 0;
00037 channels = 0;
00038 bks = 0;
00039 bitrate = 0;
00040 freq = 0;
00041 len = 0;
00042 input_buf = 0;
00043 input_bytes = 0;
00044 output_buf = 0;
00045 output_bytes = 0;
00046 output_at = 0;
00047 }
00048
00049 MadDecoder::~MadDecoder(void)
00050 {
00051 if (inited)
00052 deinit();
00053
00054 if (input_buf)
00055 delete [] input_buf;
00056 input_buf = 0;
00057
00058 if (output_buf)
00059 delete [] output_buf;
00060 output_buf = 0;
00061 }
00062
00063 bool MadDecoder::initialize()
00064 {
00065 bks = blockSize();
00066
00067 inited = false;
00068 user_stop = false;
00069 done = false;
00070 finish = false;
00071 derror = false;
00072 eof = false;
00073 totalTime = 0.;
00074 seekTime = -1.;
00075 stat = 0;
00076 channels = 0;
00077 bitrate = 0;
00078 freq = 0;
00079 len = 0;
00080 input_bytes = 0;
00081 output_bytes = 0;
00082 output_at = 0;
00083
00084 if (! input()) {
00085 error("DecoderMAD: cannot initialize. No input.");
00086 return FALSE;
00087 }
00088
00089 if (! input_buf)
00090 input_buf = new char[globalBufferSize];
00091
00092 if (! output_buf)
00093 output_buf = new char[globalBufferSize * 2];
00094
00095 if (! input()->isOpen()) {
00096 if (! input()->open(IO_ReadOnly)) {
00097 error("DecoderMAD: Failed to open input. Error " +
00098 QString::number(input()->status()) + ".");
00099 return FALSE;
00100 }
00101 }
00102
00103 mad_stream_init(&stream);
00104 mad_frame_init(&frame);
00105 mad_synth_init(&synth);
00106
00107 if (! findHeader()) {
00108 error("DecoderMAD: Cannot find a valid MPEG header.");
00109 return FALSE;
00110 }
00111
00112 if (output())
00113 {
00114 output()->Reconfigure(16, channels, freq, false );
00115 output()->SetSourceBitrate(bitrate);
00116 }
00117
00118 inited = TRUE;
00119 return TRUE;
00120 }
00121
00122 void MadDecoder::deinit()
00123 {
00124 if (input())
00125 input()->close();
00126
00127 mad_synth_finish(&synth);
00128 mad_frame_finish(&frame);
00129 mad_stream_finish(&stream);
00130
00131 inited = false;
00132 user_stop = false;
00133 done = false;
00134 finish = false;
00135 derror = false;
00136 eof = false;
00137 totalTime = 0.;
00138 seekTime = -1.;
00139 stat = 0;
00140 channels = 0;
00141 bks = 0;
00142 bitrate = 0;
00143 freq = 0;
00144 len = 0;
00145 input_bytes = 0;
00146 output_bytes = 0;
00147 output_at = 0;
00148 }
00149
00150 bool MadDecoder::findXingHeader(struct mad_bitptr ptr, unsigned int bitlen)
00151 {
00152 if (bitlen < 64 || mad_bit_read(&ptr, 32) != XING_MAGIC)
00153 goto fail;
00154
00155 xing.flags = mad_bit_read(&ptr, 32);
00156 bitlen -= 64;
00157
00158 if (xing.flags & XING_FRAMES) {
00159 if (bitlen < 32)
00160 goto fail;
00161
00162 xing.frames = mad_bit_read(&ptr, 32);
00163 bitlen -= 32;
00164 }
00165
00166 if (xing.flags & XING_BYTES) {
00167 if (bitlen < 32)
00168 goto fail;
00169
00170 xing.bytes = mad_bit_read(&ptr, 32);
00171 bitlen -= 32;
00172 }
00173
00174 if (xing.flags & XING_TOC) {
00175 int i;
00176
00177 if (bitlen < 800)
00178 goto fail;
00179
00180 for (i = 0; i < 100; ++i)
00181 xing.toc[i] = mad_bit_read(&ptr, 8);
00182
00183 bitlen -= 800;
00184 }
00185
00186 if (xing.flags & XING_SCALE) {
00187 if (bitlen < 32)
00188 goto fail;
00189
00190 xing.scale = mad_bit_read(&ptr, 32);
00191 bitlen -= 32;
00192 }
00193
00194 return true;
00195
00196 fail:
00197 xing.flags = 0;
00198 xing.frames = 0;
00199 xing.bytes = 0;
00200 xing.scale = 0;
00201 return false;
00202 }
00203
00204 bool MadDecoder::findHeader()
00205 {
00206 bool result = false;
00207 int count = 0;
00208
00209 while (1) {
00210 if (input_bytes < globalBufferSize) {
00211 int bytes = input()->readBlock(input_buf + input_bytes,
00212 globalBufferSize - input_bytes);
00213 if (bytes <= 0) {
00214 if (bytes == -1)
00215 result = false;;
00216 break;
00217 }
00218 input_bytes += bytes;
00219 }
00220
00221 mad_stream_buffer(&stream, (unsigned char *) input_buf, input_bytes);
00222
00223 bool done = false;
00224 while (! done) {
00225 if (mad_frame_decode(&frame, &stream) != -1)
00226 done = true;
00227 else if (!MAD_RECOVERABLE(stream.error))
00228 break;
00229 count++;
00230 }
00231
00232 findXingHeader(stream.anc_ptr, stream.anc_bitlen);
00233
00234 result = done;
00235
00236 if (stream.error == MAD_ERROR_BUFLEN)
00237 {
00238
00239 count = 0;
00240 input_bytes = 0;
00241 continue;
00242 }
00243
00244 if (count || stream.error != MAD_ERROR_BUFLEN)
00245 break;
00246
00247 input_bytes = &input_buf[input_bytes] - (char *) stream.next_frame;
00248 memmove(input_buf, stream.next_frame, input_bytes);
00249 }
00250
00251 if (result && count) {
00252 freq = frame.header.samplerate;
00253 channels = MAD_NCHANNELS(&frame.header);
00254 bitrate = frame.header.bitrate / 1000;
00255 calcLength(&frame.header);
00256 }
00257
00258 return result;
00259 }
00260
00261 void MadDecoder::calcLength(struct mad_header *header)
00262 {
00263 if (! input() || ! input()->isDirectAccess())
00264 return;
00265
00266 totalTime = 0.;
00267 if (xing.flags & XING_FRAMES) {
00268 mad_timer_t timer;
00269
00270 timer = header->duration;
00271 mad_timer_multiply(&timer, xing.frames);
00272
00273 totalTime = double(mad_timer_count(timer, MAD_UNITS_MILLISECONDS)) /
00274 1000.0;
00275 } else if (header->bitrate > 0)
00276 totalTime = input()->size() * 8 / header->bitrate;
00277 }
00278
00279 void MadDecoder::seek(double pos)
00280 {
00281 seekTime = pos;
00282 }
00283
00284 void MadDecoder::stop()
00285 {
00286 user_stop = TRUE;
00287 }
00288
00289 void MadDecoder::flush(bool final)
00290 {
00291 ulong min = final ? 0 : bks;
00292
00293 while ((! done && ! finish) && output_bytes > min) {
00294
00295 if (user_stop || finish) {
00296 inited = FALSE;
00297 done = TRUE;
00298 } else {
00299 ulong sz = output_bytes < bks ? output_bytes : bks;
00300
00301 int samples = (sz*8)/(channels*16);
00302 if (output()->AddSamples(output_buf, samples, -1))
00303 {
00304 output_bytes -= sz;
00305 memmove(output_buf, output_buf + sz, output_bytes);
00306 output_at = output_bytes;
00307 } else {
00308 unlock();
00309 usleep(500);
00310 lock();
00311 done = user_stop;
00312 }
00313 }
00314 }
00315 }
00316
00317 void MadDecoder::run()
00318 {
00319 lock();
00320
00321 if (! inited) {
00322 unlock();
00323 return;
00324 }
00325
00326 stat = DecoderEvent::Decoding;
00327
00328 unlock();
00329
00330 {
00331 DecoderEvent e((DecoderEvent::Type) stat);
00332 dispatch(e);
00333 }
00334
00335 while (! done && ! finish && ! derror) {
00336 lock();
00337
00338 if (seekTime >= 0.0) {
00339 long seek_pos = long(seekTime * input()->size() / totalTime);
00340 input()->at(seek_pos);
00341 input_bytes = 0;
00342 output_at = 0;
00343 output_bytes = 0;
00344 eof = false;
00345 }
00346
00347 finish = eof;
00348
00349 if (! eof) {
00350 if (stream.next_frame && seekTime == -1.) {
00351 input_bytes = &input_buf[input_bytes] -
00352 (char *) stream.next_frame;
00353 memmove(input_buf, stream.next_frame, input_bytes);
00354 }
00355
00356 if (input_bytes < globalBufferSize) {
00357 int len = input()->readBlock((char *) input_buf + input_bytes,
00358 globalBufferSize - input_bytes);
00359
00360 if (len == 0) {
00361 eof = true;
00362 } else if (len < 0) {
00363 derror = true;
00364 unlock();
00365 break;
00366 }
00367
00368 input_bytes += len;
00369 }
00370
00371 mad_stream_buffer(&stream, (unsigned char *) input_buf,
00372 input_bytes);
00373 }
00374
00375 seekTime = -1.;
00376
00377 unlock();
00378
00379 while (! done && ! finish && ! derror) {
00380 if (mad_frame_decode(&frame, &stream) == -1) {
00381 if (stream.error == MAD_ERROR_BUFLEN)
00382 break;
00383
00384 if (! MAD_RECOVERABLE(stream.error)) {
00385 derror = true;
00386 break;
00387 }
00388 continue;
00389 }
00390
00391 lock();
00392
00393 if (seekTime >= 0.) {
00394 unlock();
00395 break;
00396 }
00397
00398 mad_synth_frame(&synth, &frame);
00399 madOutput();
00400 unlock();
00401 }
00402 }
00403
00404 lock();
00405
00406 if (! user_stop && eof) {
00407 flush(TRUE);
00408
00409 if (output()) {
00410 output()->Drain();
00411 }
00412
00413 done = TRUE;
00414 if (! user_stop)
00415 finish = TRUE;
00416 }
00417
00418 if (finish)
00419 stat = DecoderEvent::Finished;
00420 else if (user_stop)
00421 stat = DecoderEvent::Stopped;
00422
00423 unlock();
00424
00425 {
00426 DecoderEvent e((DecoderEvent::Type) stat);
00427 dispatch(e);
00428 }
00429
00430 deinit();
00431 }
00432
00433 static inline signed int scale(mad_fixed_t sample)
00434 {
00435
00436 sample += (1L << (MAD_F_FRACBITS - 16));
00437
00438
00439 if (sample >= MAD_F_ONE)
00440 sample = MAD_F_ONE - 1;
00441 else if (sample < -MAD_F_ONE)
00442 sample = -MAD_F_ONE;
00443
00444
00445 return sample >> (MAD_F_FRACBITS + 1 - 16);
00446 }
00447
00448 static inline signed long fix_sample(unsigned int bits, mad_fixed_t sample)
00449 {
00450 mad_fixed_t quantized, check;
00451 quantized = sample;
00452 check = (sample >> MAD_F_FRACBITS) + 1;
00453 if (check & ~1) {
00454 if (sample >= MAD_F_ONE)
00455 quantized = MAD_F_ONE - 1;
00456 else if (sample < -MAD_F_ONE)
00457 quantized = -MAD_F_ONE;
00458 }
00459 quantized &= ~((1L << (MAD_F_FRACBITS + 1 - bits)) - 1);
00460 return quantized >> (MAD_F_FRACBITS + 1 - bits);
00461 }
00462
00463 enum mad_flow MadDecoder::madOutput()
00464 {
00465 unsigned int samples;
00466 mad_fixed_t const *left, *right;
00467
00468 samples = synth.pcm.length;
00469 left = synth.pcm.samples[0];
00470 right = synth.pcm.samples[1];
00471
00472 long newfreq = frame.header.samplerate;
00473 long newchannels = MAD_NCHANNELS(&frame.header);
00474 long newbitrate = frame.header.bitrate / 1000;
00475
00476 if (output())
00477 {
00478 if ((newfreq != freq) || (newchannels != channels))
00479 {
00480 freq = newfreq;
00481 channels = newchannels;
00482 output()->Reconfigure(16, channels, freq, false );
00483 }
00484 if (newbitrate != bitrate)
00485 {
00486 bitrate = newbitrate;
00487 output()->SetSourceBitrate(bitrate);
00488 }
00489 }
00490
00491 while (samples--)
00492 {
00493 signed int sample;
00494
00495 if (output_bytes + 4096 > globalBufferSize)
00496 {
00497 flush();
00498 }
00499 sample = fix_sample(16, *left++);
00500 #ifdef WORDS_BIGENDIAN
00501 *(output_buf + output_at++) = ((sample >> 8) & 0xff);
00502 *(output_buf + output_at++) = ((sample >> 0) & 0xff);
00503 #else
00504 *(output_buf + output_at++) = ((sample >> 0) & 0xff);
00505 *(output_buf + output_at++) = ((sample >> 8) & 0xff);
00506 #endif
00507 output_bytes += 2;
00508
00509 if (channels == 2)
00510 {
00511 sample = fix_sample(16, *right++);
00512 #ifdef WORDS_BIGENDIAN
00513 *(output_buf + output_at++) = ((sample >> 8) & 0xff);
00514 *(output_buf + output_at++) = ((sample >> 0) & 0xff);
00515 #else
00516 *(output_buf + output_at++) = ((sample >> 0) & 0xff);
00517 *(output_buf + output_at++) = ((sample >> 8) & 0xff);
00518 #endif
00519 output_bytes += 2;
00520 }
00521 }
00522
00523 if (done || finish) {
00524 return MAD_FLOW_STOP;
00525 }
00526
00527 return MAD_FLOW_CONTINUE;
00528 }
00529
00530 enum mad_flow MadDecoder::madError(struct mad_stream *stream,
00531 struct mad_frame *)
00532 {
00533 if (MAD_RECOVERABLE(stream->error))
00534 return MAD_FLOW_CONTINUE;
00535 fprintf(stderr, "MADERROR!\n");
00536 return MAD_FLOW_STOP;
00537 }
00538
00539 MetaIO *MadDecoder::doCreateTagger(void)
00540 {
00541 return new MetaIOTagLib();
00542 }
00543
00544 bool MadDecoderFactory::supports(const QString &source) const
00545 {
00546 bool res = false;
00547 QStringList list = QStringList::split("|", extension());
00548
00549 for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
00550 {
00551 if (*it == source.right((*it).length()).lower())
00552 {
00553 res = true;
00554 break;
00555 }
00556 }
00557 return res;
00558 }
00559
00560 const QString &MadDecoderFactory::extension() const
00561 {
00562 static QString ext(".mp3|.mp2");
00563 return ext;
00564 }
00565
00566 const QString &MadDecoderFactory::description() const
00567 {
00568 static QString desc(QObject::tr("MPEG Layer 1/2/3 Audio (MAD decoder)"));
00569 return desc;
00570 }
00571
00572 Decoder *MadDecoderFactory::create(const QString &file, QIODevice *input,
00573 AudioOutput *output, bool deletable)
00574 {
00575 if (deletable)
00576 return new MadDecoder(file, this, input, output);
00577
00578 static MadDecoder *decoder = 0;
00579
00580 if (! decoder) {
00581 decoder = new MadDecoder(file, this, input, output);
00582 } else {
00583 decoder->setInput(input);
00584 decoder->setFilename(file);
00585 decoder->setOutput(output);
00586 }
00587
00588 return decoder;
00589 }