00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <faad.h>
00012
00013 #ifdef __STDC_LIMIT_MACROS
00014 #define FAAD_MODIFIED
00015 #endif
00016
00017 #include <iostream>
00018 #include <string>
00019 #include <qobject.h>
00020 #include <qiodevice.h>
00021 #include <qfile.h>
00022 #include <stdlib.h>
00023
00024 #include "aacdecoder.h"
00025 #include "constants.h"
00026 #include <mythtv/audiooutput.h>
00027 #include "metadata.h"
00028 #include "metaiomp4.h"
00029
00030 #include <mythtv/mythcontext.h>
00031
00032
00033
00034
00035
00036 extern "C" uint32_t read_callback(void *user_data, void *buffer, uint32_t length)
00037 {
00038 aacDecoder *the_decoder_object = (aacDecoder*) user_data;
00039 if(the_decoder_object)
00040 {
00041 return the_decoder_object->aacRead((char *)buffer, length);
00042 }
00043 cerr << "read_callback called with no aacDecoder object assigned" << endl;
00044 return 0;
00045
00046 }
00047
00048 uint32_t seek_callback(void *user_data, uint64_t position)
00049 {
00050 aacDecoder *the_decoder_object = (aacDecoder*) user_data;
00051 if (the_decoder_object)
00052 {
00053 return the_decoder_object->aacSeek(position);
00054 }
00055 cerr << "seek_callback called with no aacDecoder object assigned" << endl;
00056 return 0;
00057 }
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 aacDecoder::aacDecoder(const QString &file, DecoderFactory *d, QIODevice *i,
00068 AudioOutput *o)
00069 : Decoder(d, i, o)
00070 {
00071 filename = file;
00072 inited = FALSE;
00073 user_stop = FALSE;
00074 stat = 0;
00075 bks = 0;
00076 done = FALSE;
00077 finish = FALSE;
00078 len = 0;
00079 sample_rate = 0;
00080 bitrate = 0;
00081 seekTime = -1.0;
00082 totalTime = 0.0;
00083 channels = 0;
00084 output_buf = 0;
00085 output_bytes = 0;
00086 output_at = 0;
00087
00088 mp4_file_flag = false;
00089 mp4_callback = NULL;
00090 timescale = 0;
00091 framesize = 0;
00092 }
00093
00094 aacDecoder::~aacDecoder(void)
00095 {
00096 if (inited)
00097 {
00098 deinit();
00099 }
00100
00101 if (output_buf)
00102 delete [] output_buf;
00103 output_buf = 0;
00104 }
00105
00106 void aacDecoder::stop()
00107 {
00108 user_stop = TRUE;
00109 }
00110
00111 void aacDecoder::flush(bool final)
00112 {
00113 ulong min = final ? 0 : bks;
00114
00115 while ((!done && !finish && seekTime <= 0) && output_bytes > min)
00116 {
00117 if (user_stop || finish)
00118 {
00119 inited = FALSE;
00120 done = TRUE;
00121 }
00122 else
00123 {
00124 ulong sz = output_bytes < bks ? output_bytes : bks;
00125
00126 int samples = (sz * 8) / (channels * 16);
00127 if (output()->AddSamples(output_buf, samples, -1))
00128 {
00129 output_bytes -= sz;
00130 memmove(output_buf, output_buf + sz, output_bytes);
00131 output_at = output_bytes;
00132 } else {
00133 unlock();
00134 usleep(500);
00135 lock();
00136 done = user_stop;
00137 }
00138
00139 }
00140 }
00141 }
00142
00143 bool aacDecoder::initialize()
00144 {
00145 bks = blockSize();
00146
00147 inited = user_stop = done = finish = FALSE;
00148 len = sample_rate = bitrate = 0;
00149 stat = channels = 0;
00150 timescale = 0;
00151 framesize = 0;
00152 seekTime = -1.0;
00153 totalTime = 0.0;
00154
00155 mp4_file_flag = false;
00156
00157 if (! input())
00158 {
00159 error("aacDecoder: cannot initialize as it has no input");
00160 return false;
00161 }
00162
00163
00164 if (!output_buf) {
00165 output_buf = new char[globalBufferSize];
00166 }
00167 output_at = 0;
00168 output_bytes = 0;
00169
00170 if (! input()->isOpen())
00171 {
00172 if (! input()->open(IO_ReadOnly))
00173 {
00174 error("aacDecoder: failed to open input");
00175 return FALSE;
00176 }
00177 }
00178
00179
00180
00181
00182
00183
00184 if (!input()->at(0))
00185 {
00186 error("couldn't seek in input");
00187 return false;
00188 }
00189
00190
00191
00192
00193
00194
00195
00196 mp4_file_flag = false;
00197 char header_buffer[8];
00198 input()->readBlock(header_buffer, 8);
00199
00200
00201
00202
00203
00204 input()->at(0);
00205
00206 if (
00207 header_buffer[4] == 'f' &&
00208 header_buffer[5] == 't' &&
00209 header_buffer[6] == 'y' &&
00210 header_buffer[7] == 'p'
00211 )
00212 {
00213
00214
00215
00216 mp4_file_flag = true;
00217 return initializeMP4();
00218 }
00219 else
00220 {
00221 mp4_file_flag = false;
00222 error("aacDecoder: stream is not mp4 ... not yet supported");
00223 input()->close();
00224 inited = false;
00225 return false;
00226 }
00227
00228 input()->close();
00229 inited = false;
00230 return false;
00231 }
00232
00233
00234 bool aacDecoder::initializeMP4()
00235 {
00236
00237
00238
00239
00240 mp4_callback = (mp4ff_callback_t*) malloc(sizeof(mp4ff_callback_t));
00241 mp4_callback->read = read_callback;
00242 mp4_callback->seek = seek_callback;
00243 mp4_callback->user_data = this;
00244
00245
00246
00247
00248
00249 decoder_handle = faacDecOpen();
00250
00251
00252
00253
00254
00255 faacDecConfigurationPtr config = faacDecGetCurrentConfiguration(decoder_handle);
00256 config->outputFormat = FAAD_FMT_16BIT;
00257 config->downMatrix = 0;
00258 config->dontUpSampleImplicitSBR = 1;
00259 faacDecSetConfiguration(decoder_handle, config);
00260
00261
00262
00263
00264
00265 mp4_input_file = mp4ff_open_read(mp4_callback);
00266 if (!mp4_input_file)
00267 {
00268 error("could not open input as mp4 input file");
00269 faacDecClose(decoder_handle);
00270 free(mp4_callback);
00271 return false;
00272 }
00273
00274
00275
00276
00277
00278 if ( (aac_track_number = getAACTrack(mp4_input_file)) < 0)
00279 {
00280 error("could not find aac track inside mp4 input file");
00281 faacDecClose(decoder_handle);
00282 mp4ff_close(mp4_input_file);
00283 free(mp4_callback);
00284 return false;
00285 }
00286
00287
00288
00289
00290
00291
00292 unsigned char *buffer = NULL;
00293 uint buffer_size;
00294
00295 mp4ff_get_decoder_config(
00296 mp4_input_file,
00297 aac_track_number,
00298 &buffer,
00299 &buffer_size
00300 );
00301
00302
00303 #ifdef FAAD_MODIFIED
00304 uint32_t srate_tmp;
00305 int err = faacDecInit2(decoder_handle, buffer, buffer_size, &srate_tmp, &channels);
00306 sample_rate = srate_tmp;
00307 if (err < 0)
00308 #else
00309 if (faacDecInit2(decoder_handle, buffer, buffer_size,
00310 &sample_rate, &channels) < 0)
00311 #endif
00312 {
00313 error("aacDecoder: error in second stage initialization");
00314 faacDecClose(decoder_handle);
00315 mp4ff_close(mp4_input_file);
00316 free(mp4_callback);
00317 if (buffer)
00318 {
00319 free(buffer);
00320 }
00321 return 1;
00322 }
00323
00324 timescale = mp4ff_time_scale(mp4_input_file, aac_track_number);
00325 framesize = 1024;
00326
00327
00328
00329
00330
00331 mp4AudioSpecificConfig mp4ASC;
00332 if (buffer)
00333 {
00334 if (AudioSpecificConfig(buffer, buffer_size, &mp4ASC) >= 0)
00335 {
00336 if (mp4ASC.frameLengthFlag == 1) framesize = 960;
00337 if (mp4ASC.sbr_present_flag == 1) framesize *= 2;
00338 }
00339 free(buffer);
00340 }
00341
00342
00343
00344
00345
00346 long samples = mp4ff_num_samples(mp4_input_file, aac_track_number);
00347 float f = 1024.0;
00348 float seconds;
00349
00350 if (mp4ASC.sbr_present_flag == 1)
00351 {
00352 f = f * 2.0;
00353 }
00354
00355 seconds = (float)samples*(float)(f-1.0)/(float)mp4ASC.samplingFrequency;
00356
00357 totalTime = seconds;
00358
00359
00360 if (mp4ff_get_avg_bitrate(mp4_input_file, aac_track_number) ==
00361 mp4ff_get_max_bitrate(mp4_input_file, aac_track_number))
00362 {
00363 bitrate = mp4ff_get_avg_bitrate(mp4_input_file, aac_track_number) / 1000;
00364 }
00365
00366
00367
00368
00369
00370 if (channels != mp4ASC.channelsConfiguration)
00371 {
00372 error("accDecoder: possible confusion on number of channels");
00373 }
00374
00375 if (sample_rate != mp4ASC.samplingFrequency)
00376 {
00377 error("accDecoder: possible confusion on frequency");
00378 }
00379
00380
00381
00382
00383
00384
00385 if (output())
00386 {
00387 output()->Reconfigure(16, channels, sample_rate,
00388 false );
00389 output()->SetSourceBitrate(bitrate);
00390 }
00391
00392 inited = TRUE;
00393 return TRUE;
00394 }
00395
00396 int aacDecoder::getAACTrack(mp4ff_t *infile)
00397 {
00398
00399
00400
00401
00402 int i, rc;
00403 int numTracks = mp4ff_total_tracks(infile);
00404
00405 for (i = 0; i < numTracks; i++)
00406 {
00407 unsigned char *buff = NULL;
00408 uint buff_size = 0;
00409 mp4AudioSpecificConfig mp4ASC;
00410
00411 mp4ff_get_decoder_config(infile, i, &buff, &buff_size);
00412
00413 if (buff)
00414 {
00415 rc = AudioSpecificConfig(buff, buff_size, &mp4ASC);
00416 free(buff);
00417
00418 if (rc < 0)
00419 continue;
00420 return i;
00421 }
00422 }
00423
00424
00425
00426
00427 return -1;
00428 }
00429
00430 void aacDecoder::seek(double pos)
00431 {
00432 seekTime = pos;
00433 }
00434
00435 void aacDecoder::deinit()
00436 {
00437 faacDecClose(decoder_handle);
00438 mp4ff_close(mp4_input_file);
00439 if (mp4_callback)
00440 {
00441 free(mp4_callback);
00442 }
00443
00444 inited = user_stop = done = finish = FALSE;
00445 len = sample_rate = bitrate = 0;
00446 stat = channels = 0;
00447 timescale = 0;
00448 framesize = 0;
00449 setInput(0);
00450 setOutput(0);
00451
00452 }
00453
00454 void aacDecoder::run()
00455 {
00456
00457 lock();
00458
00459 if (!inited)
00460 {
00461 error("aacDecoder: run() called without being init'd");
00462 unlock();
00463 return;
00464 }
00465
00466
00467
00468 stat = DecoderEvent::Decoding;
00469
00470 unlock();
00471
00472 {
00473 DecoderEvent e((DecoderEvent::Type) stat);
00474 dispatch(e);
00475 }
00476
00477 long current_sample, total_numb_samples;
00478
00479
00480 total_numb_samples = mp4ff_num_samples(mp4_input_file, aac_track_number);
00481 current_sample = -1;
00482 uchar *buffer;
00483 uint buffer_size;
00484
00485 while (!done && !finish && !user_stop)
00486 {
00487 lock();
00488
00489 ++current_sample;
00490 if (seekTime >= 0.0)
00491 {
00492
00493
00494
00495
00496 current_sample = (long int) (((double)(seekTime / totalTime)) * total_numb_samples);
00497 seekTime = -1.0;
00498 }
00499
00500 if (current_sample >= total_numb_samples)
00501 {
00502
00503
00504
00505
00506 flush(TRUE);
00507
00508 if (output())
00509 {
00510 output()->Drain();
00511 }
00512
00513 done = TRUE;
00514 if (! user_stop)
00515 {
00516 finish = TRUE;
00517 }
00518 }
00519 else
00520 {
00521
00522 unsigned int sample_count;
00523
00524 buffer = NULL;
00525 buffer_size = 0;
00526
00527 int rc = mp4ff_read_sample(
00528 mp4_input_file,
00529 aac_track_number,
00530 current_sample,
00531 &buffer,
00532 &buffer_size
00533 );
00534 if (rc == 0)
00535 {
00536 error("decoder error reading sample");
00537 done = TRUE;
00538 }
00539 else
00540 {
00541
00542 faacDecFrameInfo frame_info;
00543 void *sample_buffer = faacDecDecode(
00544 decoder_handle,
00545 &frame_info,
00546 buffer,
00547 buffer_size
00548 );
00549
00550 sample_count = frame_info.samples;
00551
00552
00553
00554
00555
00556
00557
00558 if (((sample_count * 2) + output_at ) >= globalBufferSize)
00559 {
00560 error("aacDecoder: gloablBufferSize too small, "
00561 "truncating output (this is going to "
00562 "sound like crap)");
00563 sample_count = ((globalBufferSize - output_at) / 2) - 100;
00564 }
00565
00566 char *char_buffer = (char *)sample_buffer;
00567 short *sample_buffer16 = (short*)char_buffer;
00568 for(uint i = 0; i < sample_count; i++)
00569 {
00570 output_buf[output_at + (i*2)] = (char)(sample_buffer16[i] & 0xFF);
00571 output_buf[output_at + (i*2) + 1] = (char)((sample_buffer16[i] >> 8) & 0xFF);
00572 }
00573
00574 if (sample_count > 0)
00575 {
00576 output_at += sample_count * 2;
00577 output_bytes += sample_count * 2;
00578 if (output())
00579 {
00580
00581 if (bitrate)
00582 {
00583 output()->SetSourceBitrate(bitrate);
00584 }
00585 else
00586 {
00587 output()->SetSourceBitrate(
00588 (int) ((float) (frame_info.bytesconsumed * 8) /
00589 (frame_info.samples /
00590 frame_info.num_front_channels)
00591 * frame_info.samplerate) / 1000);
00592 }
00593
00594 flush();
00595 }
00596 }
00597
00598 if (buffer)
00599 {
00600 free(buffer);
00601 }
00602 }
00603 }
00604 unlock();
00605 }
00606
00607 flush(TRUE);
00608
00609 lock();
00610
00611
00612 if (finish)
00613 {
00614
00615 stat = DecoderEvent::Finished;
00616 }
00617 else if (user_stop) {
00618 stat = DecoderEvent::Stopped;
00619 }
00620
00621 unlock();
00622
00623 {
00624 DecoderEvent e((DecoderEvent::Type) stat);
00625 dispatch(e);
00626 }
00627
00628 deinit();
00629 }
00630
00631 MetaIO *aacDecoder::doCreateTagger(void)
00632 {
00633 return new MetaIOMP4;
00634 }
00635
00636 uint32_t aacDecoder::aacRead(char *buffer, uint32_t length)
00637 {
00638 if (input())
00639 {
00640 Q_LONG read_result = input()->readBlock(buffer, length);
00641 if (read_result < 1)
00642 {
00643 return 0;
00644 }
00645 return read_result;
00646 }
00647 error("aacDecoder: aacRead() was called, but there is no input");
00648 return 0;
00649 }
00650
00651 uint32_t aacDecoder::aacSeek(uint64_t position)
00652 {
00653 if (input())
00654 {
00655 return input()->at(position);
00656 }
00657 error("aacDecoder: aacSeek() was called, but there is no input");
00658 return 0;
00659 }
00660
00661
00662 bool aacDecoderFactory::supports(const QString &source) const
00663 {
00664 bool res = false;
00665 QStringList list = QStringList::split("|", extension());
00666
00667 for (QStringList::Iterator it = list.begin(); it != list.end(); ++it)
00668 {
00669 if (*it == source.right((*it).length()).lower())
00670 {
00671 res = true;
00672 break;
00673 }
00674 }
00675 return res;
00676 }
00677
00678 const QString &aacDecoderFactory::extension() const
00679 {
00680 static QString ext(".m4a");
00681 return ext;
00682 }
00683
00684
00685 const QString &aacDecoderFactory::description() const
00686 {
00687 static QString desc(QObject::tr("Windows Media Audio"));
00688 return desc;
00689 }
00690
00691 Decoder *aacDecoderFactory::create(const QString &file, QIODevice *input,
00692 AudioOutput *output, bool deletable)
00693 {
00694
00695 if (deletable)
00696 return new aacDecoder(file, this, input, output);
00697
00698 static aacDecoder *decoder = 0;
00699 if (!decoder)
00700 {
00701 decoder = new aacDecoder(file, this, input, output);
00702 }
00703 else
00704 {
00705 decoder->setInput(input);
00706 decoder->setOutput(output);
00707 }
00708
00709 return decoder;
00710 }