Ticket #1104: mythtv_ac3.19.patch
File mythtv_ac3.19.patch, 63.9 KB (added by , 18 years ago) |
---|
-
libs/libmyth/libmyth.pro
33 33 SOURCES += DisplayResScreen.cpp util-x11.cpp qmdcodec.cpp 34 34 SOURCES += virtualkeyboard.cpp mythobservable.cpp mythsocket.cpp 35 35 36 #INCLUDEPATH += ../libmythsamplerate ../libmythsoundtouch ../.. 37 #INCLUDEPATH += ../libavutil .. 38 #DEPENDPATH += ../libmythsamplerate ../libmythsoundtouch 36 39 INCLUDEPATH += ../libmythsamplerate ../libmythsoundtouch ../.. ../ 40 INCLUDEPATH += ../libavutil 37 41 DEPENDPATH += ../libmythsamplerate ../libmythsoundtouch ../ ../libmythui 42 DEPENDPATH += ../libavutil ../libavcodec 38 43 39 44 LIBS += -L../libmythsamplerate -lmythsamplerate-$${LIBVERSION} 40 45 LIBS += -L../libmythsoundtouch -lmythsoundtouch-$${LIBVERSION} 46 LIBS += -L../libavcodec -lmythavcodec-$${LIBVERSION} 41 47 42 48 isEmpty(QMAKE_EXTENSION_SHLIB) { 43 49 QMAKE_EXTENSION_SHLIB=so -
libs/libmyth/audiooutput.h
31 31 virtual ~AudioOutput() { }; 32 32 33 33 // reconfigure sound out for new params 34 virtual void Reconfigure(int audio_bits, int audio_channels, 35 int audio_samplerate, bool audio_passthru) = 0; 34 virtual void Reconfigure(int audio_bits, 35 int audio_channels, 36 int audio_samplerate, 37 bool audio_passthru, 38 void* audio_codec = NULL 39 ) = 0; 36 40 37 41 virtual void SetStretchFactor(float factor); 38 42 … … 74 78 lastError = msg; 75 79 VERBOSE(VB_IMPORTANT, "AudioOutput Error: " + lastError); 76 80 } 81 void ClearError() 82 { lastError = QString::null; }; 77 83 78 84 void Warn(QString msg) 79 85 { -
libs/libmyth/audiooutputdx.h
35 35 /// END HACK HACK HACK HACK 36 36 37 37 virtual void Reset(void); 38 virtual void Reconfigure(int audio_bits, int audio_channels, 39 int audio_samplerate, int audio_passthru); 38 virtual void Reconfigure(int audio_bits, 39 int audio_channels, 40 int audio_samplerate, 41 bool audio_passthru, 42 AudioCodecMode aom = AUDIOCODECMODE_NORMAL); 40 43 virtual void SetBlocking(bool blocking); 41 44 42 45 virtual bool AddSamples(char *buffer, int samples, long long timecode); -
libs/libmyth/audiooutputdx.cpp
130 130 // FIXME: kedl: not sure what else could be required here? 131 131 } 132 132 133 void AudioOutputDX::Reconfigure(int audio_bits, int audio_channels, 134 int audio_samplerate, int audio_passthru) 133 void AudioOutputDX::Reconfigure(int audio_bits, 134 int audio_channels, 135 int audio_samplerate, 136 int audio_passthru, 137 AudioCodecMode laom 138 ) 135 139 { 136 140 if (dsbuffer) 137 141 DestroyDSBuffer(); -
libs/libmyth/audiooutputbase.h
18 18 #include "samplerate.h" 19 19 #include "SoundTouch.h" 20 20 21 #define AUDBUFSIZE 768000 21 struct AVCodecContext; 22 class DigitalEncoder; 22 23 #define AUDIO_SRC_IN_SIZE 16384 23 24 #define AUDIO_SRC_OUT_SIZE (16384*6) 24 25 #define AUDIO_TMP_BUF_SIZE (16384*6) 25 26 27 //#define AUDBUFSIZE 768000 28 //divisible by 12,10,8,6,4,2 and around 1024000 29 //#define AUDBUFSIZE 1024080 30 #define AUDBUFSIZE 1536000 31 26 32 class AudioOutputBase : public AudioOutput 27 33 { 28 34 public: … … 35 41 virtual ~AudioOutputBase(); 36 42 37 43 // reconfigure sound out for new params 38 virtual void Reconfigure(int audio_bits, int audio_channels, 39 int audio_samplerate, bool audio_passthru); 44 virtual void Reconfigure(int audio_bits, 45 int audio_channels, 46 int audio_samplerate, 47 bool audio_passthru, 48 void* audio_codec = NULL); 40 49 41 50 // do AddSamples calls block? 42 51 virtual void SetBlocking(bool blocking); … … 120 129 bool audio_passthru; 121 130 122 131 float audio_stretchfactor; 132 AVCodecContext *audio_codec; 123 133 AudioOutputSource source; 124 134 125 135 bool killaudio; … … 128 138 bool set_initial_vol; 129 139 bool buffer_output_data_for_use; // used by AudioOutputNULL 130 140 141 int configured_audio_channels; 142 131 143 private: 132 144 // resampler 133 145 bool need_resampler; … … 139 151 140 152 // timestretch 141 153 soundtouch::SoundTouch * pSoundStretch; 154 DigitalEncoder * encoder; 142 155 143 156 bool blocking; // do AddSamples calls block? 144 157 … … 155 168 156 169 pthread_mutex_t avsync_lock; /* must hold avsync_lock to read or write 157 170 'audiotime' and 'audiotime_updated' */ 158 intaudiotime; // timecode of audio leaving the soundcard (same units as171 long long audiotime; // timecode of audio leaving the soundcard (same units as 159 172 // timecodes) ... 160 173 struct timeval audiotime_updated; // ... which was last updated at this time 161 174 162 175 /* Audio circular buffer */ 163 176 unsigned char audiobuffer[AUDBUFSIZE]; /* buffer */ 164 177 int raud, waud; /* read and write positions */ 165 intaudbuf_timecode; /* timecode of audio most recently placed into178 long long audbuf_timecode; /* timecode of audio most recently placed into 166 179 buffer */ 167 180 168 181 int numlowbuffer; -
libs/libmyth/audiooutputbase.cpp
16 16 // MythTV headers 17 17 #include "audiooutputbase.h" 18 18 19 extern "C" { 20 #include "libavcodec/avcodec.h" 21 #include "libavcodec/liba52/a52.h" 22 } 23 24 #if QT_VERSION < 0x030200 25 #define LONGLONGCONVERT (long) 26 #else 27 #define LONGLONGCONVERT 28 #endif 29 30 #define LOC QString("DEnc: "); 31 #define MAX_AC3_FRAME_SIZE 6144 32 class DigitalEncoder 33 { 34 public: 35 DigitalEncoder(); 36 ~DigitalEncoder(); 37 void Dispose(); 38 bool DigitalEncoder::Init(CodecID codec_id, int bitrate, int samplerate, int channels); 39 size_t Encode(short * buff); 40 41 // if needed 42 char * GetFrameBuffer() 43 { 44 if (!frame_buffer && av_context) 45 { 46 frame_buffer = new char [one_frame_bytes]; 47 } 48 return frame_buffer; 49 } 50 size_t FrameSize() const { return one_frame_bytes; } 51 char * GetOutBuff() const { return outbuf; } 52 53 size_t audio_bytes_per_sample; 54 private: 55 AVCodecContext *av_context; 56 char * outbuf; 57 char * frame_buffer; 58 int outbuf_size; 59 size_t one_frame_bytes; 60 }; 61 62 DigitalEncoder::DigitalEncoder() 63 { 64 av_context = NULL; 65 outbuf = NULL; 66 outbuf_size = 0; 67 one_frame_bytes = 0; 68 frame_buffer = NULL; 69 } 70 71 DigitalEncoder::~DigitalEncoder() 72 { 73 Dispose(); 74 } 75 76 void DigitalEncoder::Dispose() 77 { 78 if (av_context) 79 { 80 avcodec_close(av_context); 81 av_free(av_context); 82 av_context = NULL; 83 } 84 if (outbuf) 85 { 86 delete [] outbuf; 87 outbuf = NULL; 88 outbuf_size = 0; 89 } 90 if (frame_buffer) 91 { 92 delete [] frame_buffer; 93 frame_buffer = NULL; 94 one_frame_bytes = 0; 95 } 96 } 97 98 //CODEC_ID_AC3 99 bool DigitalEncoder::Init(CodecID codec_id, int bitrate, int samplerate, int channels) 100 { 101 AVCodec * codec; 102 int ret; 103 104 VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init codecid=%1, br=%2, sr=%3, ch=%4") 105 .arg(codec_id_string(codec_id)) 106 .arg(bitrate) 107 .arg(samplerate) 108 .arg(channels)); 109 //codec = avcodec_find_encoder(codec_id); 110 // always AC3 as there is no DTS encoder at the moment 2005/1/9 111 codec = avcodec_find_encoder(CODEC_ID_AC3); 112 if (!codec) 113 { 114 VERBOSE(VB_IMPORTANT,"Error: could not find codec"); 115 return false; 116 } 117 av_context = avcodec_alloc_context(); 118 av_context->bit_rate = bitrate; 119 av_context->sample_rate = samplerate; 120 av_context->channels = channels; 121 // open it */ 122 if ((ret = avcodec_open(av_context, codec)) < 0) 123 { 124 VERBOSE(VB_IMPORTANT,"Error: could not open codec, invalid bitrate or samplerate"); 125 Dispose(); 126 return false; 127 } 128 129 size_t bytes_per_frame = av_context->channels * sizeof(short); 130 audio_bytes_per_sample = bytes_per_frame; 131 one_frame_bytes = bytes_per_frame * av_context->frame_size; 132 133 outbuf_size = 16384; // ok for AC3 but DTS? 134 outbuf = new char [outbuf_size]; 135 VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init fs=%1, bpf=%2 ofb=%3") 136 .arg(av_context->frame_size) 137 .arg(bytes_per_frame) 138 .arg(one_frame_bytes) 139 ); 140 141 return true; 142 } 143 144 static int DTS_SAMPLEFREQS[16] = 145 { 146 0, 8000, 16000, 32000, 64000, 128000, 11025, 22050, 147 44100, 88200, 176400, 12000, 24000, 48000, 96000, 192000 148 }; 149 150 static int DTS_BITRATES[30] = 151 { 152 32000, 56000, 64000, 96000, 112000, 128000, 153 192000, 224000, 256000, 320000, 384000, 448000, 154 512000, 576000, 640000, 768000, 896000, 1024000, 155 1152000, 1280000, 1344000, 1408000, 1411200, 1472000, 156 1536000, 1920000, 2048000, 3072000, 3840000, 4096000 157 }; 158 159 static int dts_decode_header(uint8_t *indata_ptr, int *rate, 160 int *nblks, int *sfreq) 161 { 162 uint id = ((indata_ptr[0] << 24) | (indata_ptr[1] << 16) | 163 (indata_ptr[2] << 8) | (indata_ptr[3])); 164 165 if (id != 0x7ffe8001) 166 return -1; 167 168 int ftype = indata_ptr[4] >> 7; 169 170 int surp = (indata_ptr[4] >> 2) & 0x1f; 171 surp = (surp + 1) % 32; 172 173 *nblks = (indata_ptr[4] & 0x01) << 6 | (indata_ptr[5] >> 2); 174 ++*nblks; 175 176 int fsize = (indata_ptr[5] & 0x03) << 12 | 177 (indata_ptr[6] << 4) | (indata_ptr[7] >> 4); 178 ++fsize; 179 180 *sfreq = (indata_ptr[8] >> 2) & 0x0f; 181 *rate = (indata_ptr[8] & 0x03) << 3 | ((indata_ptr[9] >> 5) & 0x07); 182 183 if (ftype != 1) 184 { 185 VERBOSE(VB_IMPORTANT, LOC + 186 QString("DTS: Termination frames not handled (ftype %1)") 187 .arg(ftype)); 188 return -1; 189 } 190 191 if (*sfreq != 13) 192 { 193 VERBOSE(VB_IMPORTANT, LOC + 194 QString("DTS: Only 48kHz supported (sfreq %1)").arg(*sfreq)); 195 return -1; 196 } 197 198 if ((fsize > 8192) || (fsize < 96)) 199 { 200 VERBOSE(VB_IMPORTANT, LOC + 201 QString("DTS: fsize: %1 invalid").arg(fsize)); 202 return -1; 203 } 204 205 if (*nblks != 8 && *nblks != 16 && *nblks != 32 && 206 *nblks != 64 && *nblks != 128 && ftype == 1) 207 { 208 VERBOSE(VB_IMPORTANT, LOC + 209 QString("DTS: nblks %1 not valid for normal frame") 210 .arg(*nblks)); 211 return -1; 212 } 213 214 return fsize; 215 } 216 217 static int dts_syncinfo(uint8_t *indata_ptr, int * /*flags*/, 218 int *sample_rate, int *bit_rate) 219 { 220 int nblks; 221 int rate; 222 int sfreq; 223 224 int fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq); 225 if (fsize >= 0) 226 { 227 if (rate >= 0 && rate <= 29) 228 *bit_rate = DTS_BITRATES[rate]; 229 else 230 *bit_rate = 0; 231 if (sfreq >= 1 && sfreq <= 15) 232 *sample_rate = DTS_SAMPLEFREQS[sfreq]; 233 else 234 *sample_rate = 0; 235 } 236 return fsize; 237 } 238 239 static int encode_frame( 240 bool dts, 241 unsigned char *data, 242 size_t &len) 243 { 244 size_t enc_len; 245 int flags, sample_rate, bit_rate; 246 247 // we don't do any length/crc validation of the AC3 frame here; presumably 248 // the receiver will have enough sense to do that. if someone has a 249 // receiver that doesn't, here would be a good place to put in a call 250 // to a52_crc16_block(samples+2, data_size-2) - but what do we do if the 251 // packet is bad? we'd need to send something that the receiver would 252 // ignore, and if so, may as well just assume that it will ignore 253 // anything with a bad CRC... 254 255 uint nr_samples = 0, block_len; 256 if (dts) 257 { 258 enc_len = dts_syncinfo(data+8, &flags, &sample_rate, &bit_rate); 259 int rate, sfreq, nblks; 260 dts_decode_header(data+8, &rate, &nblks, &sfreq); 261 nr_samples = nblks * 32; 262 block_len = nr_samples * 2 * 2; 263 } 264 else 265 { 266 enc_len = a52_syncinfo(data+8, &flags, &sample_rate, &bit_rate); 267 block_len = MAX_AC3_FRAME_SIZE; 268 } 269 270 if (enc_len == 0 || enc_len > len) 271 { 272 int l = len; 273 len = 0; 274 return l; 275 } 276 277 enc_len = min((uint)enc_len, block_len - 8); 278 279 //uint32_t x = *(uint32_t*)(data+8); 280 // in place swab 281 swab(data+8, data+8, enc_len); 282 //VERBOSE(VB_AUDIO|VB_TIMESTAMP, 283 // QString("DigitalEncoder::Encode swab test %1 %2") 284 // .arg(x,0,16).arg(*(uint32_t*)(data+8),0,16)); 285 286 // the following values come from libmpcodecs/ad_hwac3.c in mplayer. 287 // they form a valid IEC958 AC3 header. 288 data[0] = 0x72; 289 data[1] = 0xF8; 290 data[2] = 0x1F; 291 data[3] = 0x4E; 292 data[4] = 0x01; 293 if (dts) 294 { 295 switch(nr_samples) 296 { 297 case 512: 298 data[4] = 0x0B; /* DTS-1 (512-sample bursts) */ 299 break; 300 301 case 1024: 302 data[4] = 0x0C; /* DTS-2 (1024-sample bursts) */ 303 break; 304 305 case 2048: 306 data[4] = 0x0D; /* DTS-3 (2048-sample bursts) */ 307 break; 308 309 default: 310 VERBOSE(VB_IMPORTANT, LOC + 311 QString("DTS: %1-sample bursts not supported") 312 .arg(nr_samples)); 313 data[4] = 0x00; 314 break; 315 } 316 } 317 data[5] = 0x00; 318 data[6] = (enc_len << 3) & 0xFF; 319 data[7] = (enc_len >> 5) & 0xFF; 320 memset(data + 8 + enc_len, 0, block_len - 8 - enc_len); 321 len = block_len; 322 323 return enc_len; 324 } 325 326 // must have exactly 1 frames worth of data 327 size_t DigitalEncoder::Encode(short * buff) 328 { 329 int encsize = 0; 330 size_t outsize = 0; 331 332 // put data in the correct spot for encode frame 333 outsize = avcodec_encode_audio( 334 av_context, 335 ((uchar*)outbuf)+8, 336 outbuf_size-8, 337 buff); 338 size_t tmpsize = outsize; 339 340 outsize = MAX_AC3_FRAME_SIZE; 341 encsize = encode_frame( 342 //av_context->codec_id==CODEC_ID_DTS, 343 false, 344 (unsigned char*)outbuf, outsize); 345 VERBOSE(VB_AUDIO|VB_TIMESTAMP, 346 QString("DigitalEncoder::Encode len1=%1 len2=%2 finallen=%3") 347 .arg(tmpsize) 348 .arg(encsize) 349 .arg(outsize) 350 ); 351 352 return outsize; 353 } 354 #undef LOC 19 355 #define LOC QString("AO: ") 20 356 #define LOC_ERR QString("AO, ERROR: ") 21 357 … … 24 360 int /*laudio_bits*/, int /*laudio_channels*/, 25 361 int /*laudio_samplerate*/, AudioOutputSource lsource, 26 362 bool lset_initial_vol, bool /*laudio_passthru*/) : 27 28 363 effdsp(0), effdspstretched(0), 29 364 audio_channels(-1), audio_bytes_per_sample(0), 30 365 audio_bits(-1), audio_samplerate(-1), … … 35 370 audio_passthru_device(QDeepCopy<QString>(laudio_passthru_device)), 36 371 audio_passthru(false), audio_stretchfactor(1.0f), 37 372 373 audio_codec(NULL), 38 374 source(lsource), killaudio(false), 39 375 40 376 pauseaudio(false), audio_actually_paused(false), … … 46 382 47 383 src_ctx(NULL), 48 384 49 pSoundStretch(NULL), blocking(false), 385 pSoundStretch(NULL), 386 encoder(NULL), 387 blocking(false), 50 388 51 389 lastaudiolen(0), samples_buffered(0), 52 390 audiotime(0), … … 61 399 pthread_cond_init(&audio_bufsig, NULL); 62 400 63 401 output_audio = 0; // TODO FIXME Not POSIX compatible! 402 configured_audio_channels = gContext->GetNumSetting("MaxChannels", 2); 64 403 65 404 bzero(&src_data, sizeof(SRC_DATA)); 66 405 bzero(src_in, sizeof(float) * AUDIO_SRC_IN_SIZE); … … 108 447 VERBOSE(VB_GENERAL, LOC + QString("Using time stretch %1") 109 448 .arg(audio_stretchfactor)); 110 449 pSoundStretch = new soundtouch::SoundTouch(); 111 pSoundStretch->setSampleRate(audio_samplerate); 112 pSoundStretch->setChannels(audio_channels); 450 if (audio_codec) 451 { 452 if (!encoder) 453 { 454 VERBOSE(VB_AUDIO, LOC + QString("Creating Encoder for codec %1 origfs %2").arg(audio_codec->codec_id).arg(audio_codec->frame_size)); 455 encoder = new DigitalEncoder(); 456 if (!encoder->Init(audio_codec->codec_id, 457 audio_codec->bit_rate, 458 audio_codec->sample_rate, 459 audio_codec->channels 460 )) 461 { 462 // eeks 463 delete encoder; 464 encoder = NULL; 465 VERBOSE(VB_AUDIO, LOC + QString("Failed to Create Encoder")); 466 } 467 } 468 } 469 if (encoder) 470 { 471 pSoundStretch->setSampleRate(audio_codec->sample_rate); 472 pSoundStretch->setChannels(audio_codec->channels); 473 } 474 else 475 { 476 pSoundStretch->setSampleRate(audio_samplerate); 477 pSoundStretch->setChannels(audio_channels); 478 } 113 479 114 480 pSoundStretch->setTempo(audio_stretchfactor); 115 481 pSoundStretch->setSetting(SETTING_SEQUENCE_MS, 35); … … 132 498 } 133 499 134 500 void AudioOutputBase::Reconfigure(int laudio_bits, int laudio_channels, 135 int laudio_samplerate, bool laudio_passthru) 501 int laudio_samplerate, bool laudio_passthru, 502 void* laudio_codec) 136 503 { 504 int codec_id = CODEC_ID_NONE; 505 int lcodec_id = CODEC_ID_NONE; 506 int lcchannels = 0; 507 int cchannels = 0; 508 if (laudio_codec) 509 { 510 lcodec_id = ((AVCodecContext*)laudio_codec)->codec_id; 511 laudio_bits = 16; 512 laudio_channels = 2; 513 laudio_samplerate = 48000; 514 lcchannels = ((AVCodecContext*)laudio_codec)->channels; 515 } 516 if (audio_codec) 517 { 518 codec_id = audio_codec->codec_id; 519 cchannels = ((AVCodecContext*)audio_codec)->channels; 520 } 521 ClearError(); 137 522 if (laudio_bits == audio_bits && laudio_channels == audio_channels && 138 laudio_samplerate == audio_samplerate && 139 laudio_passthru == audio_passthru && !need_resampler) 523 laudio_samplerate == audio_samplerate && !need_resampler && 524 laudio_passthru == audio_passthru && 525 lcodec_id == codec_id && lcchannels == cchannels) 140 526 return; 141 527 142 528 KillAudio(); … … 148 534 waud = raud = 0; 149 535 audio_actually_paused = false; 150 536 537 bool redo_stretch = (pSoundStretch && audio_channels != laudio_channels); 151 538 audio_channels = laudio_channels; 152 539 audio_bits = laudio_bits; 153 540 audio_samplerate = laudio_samplerate; 541 audio_codec = (AVCodecContext*)laudio_codec; 154 542 audio_passthru = laudio_passthru; 155 543 if (audio_bits != 8 && audio_bits != 16) 156 544 { … … 169 557 170 558 numlowbuffer = 0; 171 559 560 VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2 sr %3") 561 .arg(audio_main_device).arg(audio_channels).arg(audio_samplerate)); 562 172 563 // Actually do the device specific open call 173 564 if (!OpenDevice()) 174 565 { 175 566 VERBOSE(VB_AUDIO, LOC_ERR + "Aborting reconfigure"); 176 567 pthread_mutex_unlock(&avsync_lock); 177 568 pthread_mutex_unlock(&audio_buflock); 569 if (GetError().isEmpty()) 570 Error("Aborting reconfigure"); 571 VERBOSE(VB_AUDIO, "Aborting reconfigure"); 178 572 return; 179 573 } 180 574 … … 197 591 current_seconds = -1; 198 592 source_bitrate = -1; 199 593 594 // NOTE: this wont do anything as above samplerate vars are set equal 200 595 // Check if we need the resampler 201 596 if (audio_samplerate != laudio_samplerate) 202 597 { … … 221 616 222 617 VERBOSE(VB_AUDIO, LOC + QString("Audio Stretch Factor: %1") 223 618 .arg(audio_stretchfactor)); 619 VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1") 620 .arg(audio_codec?codec_id_string(audio_codec->codec_id):"not set")); 224 621 225 SetStretchFactorLocked(audio_stretchfactor); 226 if (pSoundStretch) 622 if (redo_stretch) 227 623 { 228 pSoundStretch->setSampleRate(audio_samplerate); 229 pSoundStretch->setChannels(audio_channels); 624 float laudio_stretchfactor = audio_stretchfactor; 625 delete pSoundStretch; 626 pSoundStretch = NULL; 627 audio_stretchfactor = 0.0; 628 SetStretchFactorLocked(laudio_stretchfactor); 230 629 } 630 else 631 { 632 SetStretchFactorLocked(audio_stretchfactor); 633 if (pSoundStretch) 634 { 635 // if its passthru then we need to reencode 636 if (audio_codec) 637 { 638 if (!encoder) 639 { 640 VERBOSE(VB_AUDIO, LOC + QString("Creating Encoder for codec %1").arg(audio_codec->codec_id)); 641 encoder = new DigitalEncoder(); 642 if (!encoder->Init(audio_codec->codec_id, 643 audio_codec->bit_rate, 644 audio_codec->sample_rate, 645 audio_codec->channels 646 )) 647 { 648 // eeks 649 delete encoder; 650 encoder = NULL; 651 VERBOSE(VB_AUDIO, LOC + QString("Failed to Create Encoder")); 652 } 653 } 654 } 655 if (encoder) 656 { 657 pSoundStretch->setSampleRate(audio_codec->sample_rate); 658 pSoundStretch->setChannels(audio_codec->channels); 659 } 660 else 661 { 662 pSoundStretch->setSampleRate(audio_samplerate); 663 pSoundStretch->setChannels(audio_channels); 664 } 665 } 666 } 231 667 232 668 // Setup visualisations, zero the visualisations buffers 233 669 prepareVisuals(); … … 273 709 pSoundStretch = NULL; 274 710 } 275 711 712 if (encoder) 713 { 714 delete encoder; 715 encoder = NULL; 716 } 717 276 718 CloseDevice(); 277 719 278 720 killAudioLock.unlock(); … … 286 728 287 729 void AudioOutputBase::Pause(bool paused) 288 730 { 731 VERBOSE(VB_AUDIO, LOC+ QString("Pause %0").arg(paused)); 289 732 pauseaudio = paused; 290 733 audio_actually_paused = false; 291 734 } … … 368 811 The reason is that computing 'audiotime' requires acquiring the audio 369 812 lock, which the video thread should not do. So, we call 'SetAudioTime()' 370 813 from the audio thread, and then call this from the video thread. */ 371 intret;814 long long ret; 372 815 struct timeval now; 373 816 374 817 if (audiotime == 0) … … 380 823 381 824 ret = (now.tv_sec - audiotime_updated.tv_sec) * 1000; 382 825 ret += (now.tv_usec - audiotime_updated.tv_usec) / 1000; 383 ret = ( int)(ret * audio_stretchfactor);826 ret = (long long)(ret * audio_stretchfactor); 384 827 828 #if 1 829 VERBOSE(VB_AUDIO|VB_TIMESTAMP, 830 QString("GetAudiotime now=%1.%2, set=%3.%4, ret=%5, audt=%6 sf=%7") 831 .arg(now.tv_sec).arg(now.tv_usec) 832 .arg(audiotime_updated.tv_sec).arg(audiotime_updated.tv_usec) 833 .arg(ret) 834 .arg(audiotime) 835 .arg(audio_stretchfactor) 836 ); 837 #endif 838 385 839 ret += audiotime; 386 840 387 841 pthread_mutex_unlock(&avsync_lock); 388 return ret;842 return (int)ret; 389 843 } 390 844 391 845 void AudioOutputBase::SetAudiotime(void) … … 422 876 // include algorithmic latencies 423 877 if (pSoundStretch) 424 878 { 879 // if encoder is active, then use its idea of audiobytes 880 //size_t abps = encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample; 881 882 // add the effect of any unused but processed samples, AC3 reencode does this 883 totalbuffer += (int)(pSoundStretch->numSamples() * audio_bytes_per_sample); 425 884 // add the effect of unprocessed samples in time stretch algo 426 885 totalbuffer += (int)((pSoundStretch->numUnprocessedSamples() * 427 886 audio_bytes_per_sample) / audio_stretchfactor); 428 887 } 429 888 430 889 audiotime = audbuf_timecode - (int)(totalbuffer * 100000.0 / 431 890 (audio_bytes_per_sample * effdspstretched)); 432 891 433 892 gettimeofday(&audiotime_updated, NULL); 893 #if 1 894 VERBOSE(VB_AUDIO|VB_TIMESTAMP, 895 QString("SetAudiotime set=%1.%2, audt=%3 atc=%4 tb=%5 sb=%6 eds=%7 abps=%8 sf=%9") 896 .arg(audiotime_updated.tv_sec).arg(audiotime_updated.tv_usec) 897 .arg(audiotime) 898 .arg(audbuf_timecode) 899 .arg(totalbuffer) 900 .arg(soundcard_buffer) 901 .arg(effdspstretched) 902 .arg(audio_bytes_per_sample) 903 .arg(audio_stretchfactor) 904 ); 905 #endif 434 906 435 907 pthread_mutex_unlock(&avsync_lock); 436 908 pthread_mutex_unlock(&audio_buflock); … … 498 970 // NOTE: This function is not threadsafe 499 971 500 972 int afree = audiofree(true); 501 int len = samples * audio_bytes_per_sample;973 int len = samples * (encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample); 502 974 503 975 // Check we have enough space to write the data 504 976 if (need_resampler && src_ctx) … … 509 981 VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + QString( 510 982 "AddSamples FAILED bytes=%1, used=%2, free=%3, timecode=%4") 511 983 .arg(len).arg(AUDBUFSIZE-afree).arg(afree) 512 .arg(timecode)); 513 984 .arg(LONGLONGCONVERT timecode)); 514 985 return false; // would overflow 515 986 } 516 987 … … 547 1018 548 1019 int AudioOutputBase::WaitForFreeSpace(int samples) 549 1020 { 550 int len = samples * audio_bytes_per_sample; 1021 int abps = encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample; 1022 int len = samples * abps; 551 1023 int afree = audiofree(false); 552 1024 553 1025 while (len > afree) 554 1026 { 555 1027 if (blocking) 556 1028 { 557 VERBOSE(VB_AUDIO , LOC + "Waiting for free space");1029 VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Waiting for free space"); 558 1030 // wait for more space 559 1031 pthread_cond_wait(&audio_bufsig, &audio_buflock); 560 1032 afree = audiofree(false); 561 1033 } 562 1034 else 563 1035 { 564 VERBOSE(VB_IMPORTANT, LOC_ERR + 565 "Audio buffer overflow, audio data lost!"); 566 samples = afree / audio_bytes_per_sample; 567 len = samples * audio_bytes_per_sample; 1036 VERBOSE(VB_IMPORTANT, LOC_ERR + 1037 QString("Audio buffer overflow, %1 audio samples lost!") 1038 .arg(samples-afree / abps)); 1039 samples = afree / abps; 1040 len = samples * abps; 568 1041 if (src_ctx) 569 1042 { 570 1043 int error = src_reset(src_ctx); … … 589 1062 590 1063 int afree = audiofree(false); 591 1064 592 VERBOSE(VB_AUDIO|VB_TIMESTAMP, 593 LOC + QString("_AddSamples bytes=%1, used=%2, free=%3, timecode=%4") 594 .arg(samples * audio_bytes_per_sample) 595 .arg(AUDBUFSIZE-afree).arg(afree).arg((long)timecode)); 1065 int abps = encoder?encoder->audio_bytes_per_sample:audio_bytes_per_sample; 1066 VERBOSE(VB_AUDIO|VB_TIMESTAMP, 1067 LOC + QString("_AddSamples samples=%1 bytes=%2, used=%3, free=%4, timecode=%5") 1068 .arg(samples) 1069 .arg(samples * abps) 1070 .arg(AUDBUFSIZE-afree).arg(afree).arg(LONGLONGCONVERT timecode)); 596 1071 597 1072 len = WaitForFreeSpace(samples); 598 1073 … … 629 1104 630 1105 if (pSoundStretch) 631 1106 { 1107 632 1108 // does not change the timecode, only the number of samples 633 1109 // back to orig pos 634 1110 org_waud = waud; 635 1111 int bdiff = AUDBUFSIZE - org_waud; 636 int nSamplesToEnd = bdiff/a udio_bytes_per_sample;1112 int nSamplesToEnd = bdiff/abps; 637 1113 if (bdiff < len) 638 1114 { 639 1115 pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)(audiobuffer + 640 1116 org_waud), nSamplesToEnd); 641 1117 pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)audiobuffer, 642 (len - bdiff) / a udio_bytes_per_sample);1118 (len - bdiff) / abps); 643 1119 } 644 1120 else 645 1121 { 646 1122 pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)(audiobuffer + 647 org_waud), len / a udio_bytes_per_sample);1123 org_waud), len / abps); 648 1124 } 649 1125 650 int newLen = 0; 651 int nSamples; 652 len = WaitForFreeSpace(pSoundStretch->numSamples() * 653 audio_bytes_per_sample); 654 do 1126 if (encoder) 655 1127 { 656 int samplesToGet = len/audio_bytes_per_sample; 657 if (samplesToGet > nSamplesToEnd) 1128 // pull out a packet's worth and reencode it until we dont have enough 1129 // for any more packets 1130 soundtouch::SAMPLETYPE* temp_buff = 1131 (soundtouch::SAMPLETYPE*)encoder->GetFrameBuffer(); 1132 size_t frameSize = encoder->FrameSize()/abps; 1133 VERBOSE(VB_AUDIO|VB_TIMESTAMP, 1134 QString("_AddSamples Enc sfs=%1 bfs=%2 sss=%3") 1135 .arg(frameSize) 1136 .arg(encoder->FrameSize()) 1137 .arg(pSoundStretch->numSamples()) 1138 ); 1139 // process the same number of samples as it creates a full encoded buffer 1140 // just like before 1141 while (pSoundStretch->numSamples() >= frameSize) 658 1142 { 659 samplesToGet = nSamplesToEnd; 1143 int got = pSoundStretch->receiveSamples(temp_buff, frameSize); 1144 int amount = encoder->Encode(temp_buff); 1145 VERBOSE(VB_AUDIO|VB_TIMESTAMP, 1146 QString("_AddSamples Enc bytes=%1 got=%2 left=%3") 1147 .arg(amount) 1148 .arg(got) 1149 .arg(pSoundStretch->numSamples()) 1150 ); 1151 if (amount == 0) 1152 continue; 1153 //len = WaitForFreeSpace(amount); 1154 char * ob = encoder->GetOutBuff(); 1155 if (amount >= bdiff) 1156 { 1157 memcpy(audiobuffer + org_waud, ob, bdiff); 1158 ob += bdiff; 1159 amount -= bdiff; 1160 org_waud = 0; 1161 } 1162 if (amount > 0) 1163 memcpy(audiobuffer + org_waud, ob, amount); 1164 bdiff = AUDBUFSIZE - amount; 1165 org_waud += amount; 660 1166 } 661 662 nSamples = pSoundStretch->receiveSamples((soundtouch::SAMPLETYPE*) 663 (audiobuffer + org_waud), samplesToGet); 664 if (nSamples == nSamplesToEnd) 1167 } 1168 else 1169 { 1170 int newLen = 0; 1171 int nSamples; 1172 len = WaitForFreeSpace(pSoundStretch->numSamples() * 1173 audio_bytes_per_sample); 1174 do 665 1175 { 666 org_waud = 0; 667 nSamplesToEnd = AUDBUFSIZE/audio_bytes_per_sample; 668 } 669 else 670 { 671 org_waud += nSamples * audio_bytes_per_sample; 672 nSamplesToEnd -= nSamples; 673 } 1176 int samplesToGet = len/audio_bytes_per_sample; 1177 if (samplesToGet > nSamplesToEnd) 1178 { 1179 samplesToGet = nSamplesToEnd; 1180 } 674 1181 675 newLen += nSamples * audio_bytes_per_sample; 676 len -= nSamples * audio_bytes_per_sample; 677 } while (nSamples > 0); 1182 nSamples = pSoundStretch->receiveSamples((soundtouch::SAMPLETYPE*) 1183 (audiobuffer + org_waud), samplesToGet); 1184 if (nSamples == nSamplesToEnd) 1185 { 1186 org_waud = 0; 1187 nSamplesToEnd = AUDBUFSIZE/audio_bytes_per_sample; 1188 } 1189 else 1190 { 1191 org_waud += nSamples * audio_bytes_per_sample; 1192 nSamplesToEnd -= nSamples; 1193 } 1194 1195 newLen += nSamples * audio_bytes_per_sample; 1196 len -= nSamples * audio_bytes_per_sample; 1197 } while (nSamples > 0); 1198 } 678 1199 } 679 1200 680 1201 waud = org_waud; … … 750 1271 space_on_soundcard = getSpaceOnSoundcard(); 751 1272 752 1273 if (space_on_soundcard != last_space_on_soundcard) { 753 VERBOSE(VB_AUDIO , LOC + QString("%1 bytes free on soundcard")1274 VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + QString("%1 bytes free on soundcard") 754 1275 .arg(space_on_soundcard)); 755 1276 last_space_on_soundcard = space_on_soundcard; 756 1277 } … … 763 1284 WriteAudio(zeros, fragment_size); 764 1285 } else { 765 1286 // this should never happen now -dag 766 VERBOSE(VB_AUDIO , LOC +1287 VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + 767 1288 QString("waiting for space on soundcard " 768 1289 "to write zeros: have %1 need %2") 769 1290 .arg(space_on_soundcard).arg(fragment_size)); … … 799 1320 if (fragment_size > audiolen(true)) 800 1321 { 801 1322 if (audiolen(true) > 0) // only log if we're sending some audio 802 VERBOSE(VB_AUDIO , LOC +1323 VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + 803 1324 QString("audio waiting for buffer to fill: " 804 1325 "have %1 want %2") 805 1326 .arg(audiolen(true)).arg(fragment_size)); 806 1327 807 VERBOSE(VB_AUDIO, LOC + "Broadcasting free space avail");1328 //VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Broadcasting free space avail"); 808 1329 pthread_mutex_lock(&audio_buflock); 809 1330 pthread_cond_broadcast(&audio_bufsig); 810 1331 pthread_mutex_unlock(&audio_buflock); … … 818 1339 if (fragment_size > space_on_soundcard) 819 1340 { 820 1341 if (space_on_soundcard != last_space_on_soundcard) { 821 VERBOSE(VB_AUDIO , LOC +1342 VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + 822 1343 QString("audio waiting for space on soundcard: " 823 1344 "have %1 need %2") 824 1345 .arg(space_on_soundcard).arg(fragment_size)); … … 880 1401 881 1402 /* update raud */ 882 1403 raud = (raud + fragment_size) % AUDBUFSIZE; 883 VERBOSE(VB_AUDIO, LOC + "Broadcasting free space avail");1404 //VERBOSE(VB_AUDIO|VB_TIMESTAMP, LOC + "Broadcasting free space avail"); 884 1405 pthread_cond_broadcast(&audio_bufsig); 885 1406 886 1407 written_size = fragment_size; -
libs/libmyth/audiooutputalsa.cpp
86 86 } 87 87 else 88 88 { 89 fragment_size = 6144; // nicely divisible by 2,4,6,8 channels @ 16-bits 90 buffer_time = 500000; // .5 seconds 89 //fragment_size = 6144; // nicely divisible by 2,4,6,8 channels @ 16-bits 90 //fragment_size = 3072*audio_channels; // nicely divisible by 2,4,6,8 channels @ 16-bits 91 fragment_size = (audio_bits * audio_channels * audio_samplerate) / (8*30); 92 buffer_time = 100000; // .5 seconds 91 93 period_time = buffer_time / 4; // 4 interrupts per buffer 92 94 } 93 95 … … 159 161 160 162 tmpbuf = aubuf; 161 163 162 VERBOSE(VB_AUDIO , QString("WriteAudio: Preparing %1 bytes (%2 frames)")164 VERBOSE(VB_AUDIO|VB_TIMESTAMP, QString("WriteAudio: Preparing %1 bytes (%2 frames)") 163 165 .arg(size).arg(frames)); 164 166 165 167 while (frames > 0) -
programs/mythfrontend/globalsettings.cpp
39 39 dev.setNameFilter("adsp*"); 40 40 gc->fillSelectionsFromDir(dev); 41 41 } 42 #ifdef USE_ALSA 43 gc->addSelection("ALSA:default", "ALSA:default"); 44 gc->addSelection("ALSA:analog", "ALSA:analog"); 45 gc->addSelection("ALSA:digital", "ALSA:digital"); 46 gc->addSelection("ALSA:mixed-analog", "ALSA:mixed-analog"); 47 gc->addSelection("ALSA:mixed-digital", "ALSA:mixed-digital"); 48 #endif 49 #ifdef USE_ARTS 50 gc->addSelection("ARTS:", "ARTS:"); 51 #endif 52 #ifdef USE_JACK 53 gc->addSelection("JACK:output", "JACK:output"); 54 #endif 55 gc->addSelection("NULL", "NULL"); 42 56 43 57 return gc; 44 58 } 45 59 60 static HostComboBox *MaxAudioChannels() 61 { 62 HostComboBox *gc = new HostComboBox("MaxChannels",false); 63 gc->setLabel(QObject::tr("Max Audio Channels")); 64 //gc->addSelection(QObject::tr("Mono"), "1"); 65 //gc->addSelection(QObject::tr("Stereo L+R"), "2", true); // default 66 //gc->addSelection(QObject::tr("3 Channel: L C R"), "3"); 67 //gc->addSelection(QObject::tr("4 Channel: L R LS RS"), "4"); 68 //gc->addSelection(QObject::tr("5 Channel: L C R LS RS"), "5"); 69 //gc->addSelection(QObject::tr("6 Channel: L C R LS RS LFE"), "6"); 70 gc->addSelection(QObject::tr("Stereo"), "2", true); // default 71 gc->addSelection(QObject::tr("6 Channel"), "6"); 72 gc->setHelpText( 73 QObject::tr("Set the maximum number of audio channels to be decoded. " 74 "This is for multi-channel/surround audio playback.")); 75 return gc; 76 } 77 46 78 static HostComboBox *PassThroughOutputDevice() 47 79 { 48 80 HostComboBox *gc = new HostComboBox("PassThruOutputDevice", true); … … 2372 2404 #ifdef CONFIG_DTS 2373 2405 vgrp0->addChild(DTSPassThrough()); 2374 2406 #endif 2407 addChild(MaxAudioChannels()); 2375 2408 2376 2409 VerticalConfigurationGroup *vgrp1 = 2377 2410 new VerticalConfigurationGroup(false, false, true, true); -
programs/mythtranscode/transcode.cpp
46 46 47 47 // reconfigure sound out for new params 48 48 virtual void Reconfigure(int audio_bits, int audio_channels, 49 int audio_samplerate, bool audio_passthru) 49 int audio_samplerate, bool audio_passthru, 50 void * = NULL) 50 51 { 52 ClearError(); 51 53 (void)audio_samplerate; 52 54 (void)audio_passthru; 53 55 bits = audio_bits; 54 56 channels = audio_channels; 55 57 bytes_per_sample = bits * channels / 8; 58 if (channels>2) 59 Error("Invalid channel count"); 56 60 } 57 61 58 62 // dsprate is in 100 * samples/second -
libs/libmythtv/avformatdecoder.h
241 241 bool allow_ac3_passthru; 242 242 bool allow_dts_passthru; 243 243 bool disable_passthru; 244 int max_channels; 244 245 245 246 AudioInfo audioIn; 246 247 AudioInfo audioOut; -
libs/libmythtv/avformatdecoder.cpp
44 44 #define MAX_AC3_FRAME_SIZE 6144 45 45 46 46 /** Set to zero to allow any number of AC3 channels. */ 47 #define MAXCHANNELSELECT 1 48 #if MAXCHANNELSELECT 49 #define MAX_OUTPUT_CHANNELS compiler error 50 #else 47 51 #define MAX_OUTPUT_CHANNELS 2 52 #endif 48 53 49 54 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, 50 55 int *sample_rate, int *bit_rate); … … 298 303 #ifdef CONFIG_DTS 299 304 allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false); 300 305 #endif 306 max_channels = gContext->GetNumSetting("MaxChannels", 2); 301 307 302 308 audioIn.sample_size = -32; // force SetupAudioStream to run once 303 309 itv = GetNVP()->GetInteractiveTV(); … … 431 437 framesPlayed = lastKey; 432 438 framesRead = lastKey; 433 439 440 VERBOSE(VB_PLAYBACK, QString("AvFormatDecoder::DoFastForward newframe %5 frame %1 fps %2 ts %3 disc %4 cur_dts %6 adj %7 newts %8 fsa %9") 441 .arg(desiredFrame) 442 .arg(fps) 443 .arg(ts) 444 .arg(discardFrames) 445 .arg(framesPlayed) 446 .arg(st->cur_dts) 447 .arg(adj_cur_dts) 448 .arg(newts) 449 .arg(frameseekadjust) 450 ); 451 434 452 int normalframes = desiredFrame - framesPlayed; 435 453 454 #if 0 455 if (!exactseeks) 456 normalframes = 0; 457 #endif 458 436 459 SeekReset(lastKey, normalframes, discardFrames, discardFrames); 437 460 438 461 if (discardFrames) … … 800 823 801 824 fmt->flags &= ~AVFMT_NOFILE; 802 825 826 #if 1 827 if ((m_playbackinfo) || livetv || watchingrecording) 828 { 829 const char *name = ic->av_class->item_name(ic); 830 VERBOSE(VB_GENERAL, QString("libavformat type %1").arg(name)); 831 } 832 #endif 833 834 //struct timeval one, two, res; 835 //gettimeofday(&one, NULL); 836 803 837 if (!ringBuffer->isDVD() && !livetv) 804 838 av_estimate_timings(ic); 805 839 … … 810 844 if (-1 == ret) 811 845 return ret; 812 846 847 // make sure its at the real start due to av_find_stream_info reading 848 ret = av_seek_frame(ic, -1, 0, AVSEEK_FLAG_BACKWARD); 849 if (ret < 0) 850 av_seek_frame(ic, -1, 0, AVSEEK_FLAG_BYTE); // reposition to start of stream 851 813 852 AutoSelectTracks(); // This is needed for transcoder 814 853 815 854 { … … 839 878 // If we don't have a position map, set up ffmpeg for seeking 840 879 if (!recordingHasPositionMap) 841 880 { 881 const char *name = ic->av_class->item_name(ic); 842 882 VERBOSE(VB_PLAYBACK, LOC + 843 "Recording has no position -- using libavformat seeking.");883 QString("Recording has no position -- using libavformat seeking. %1").arg(name)); 844 884 int64_t dur = ic->duration / (int64_t)AV_TIME_BASE; 845 885 846 886 if (dur > 0) … … 885 925 QString("Successfully opened decoder for file: " 886 926 "\"%1\". novideo(%2)").arg(filename).arg(novideo)); 887 927 928 // set initial position correctly 929 //DoFastForward(0, true); 930 888 931 // Return true if recording has position map 889 932 return recordingHasPositionMap; 890 933 } … … 1296 1339 <<") already open, leaving it alone."); 1297 1340 } 1298 1341 //assert(enc->codec_id); 1342 VERBOSE(VB_GENERAL, QString("AVFD: codec %1 has %2 channels").arg(codec_id_string(enc->codec_id)).arg(enc->channels)); 1343 #if 0 1344 if (enc->channels > 2) 1345 enc->channels = 2; 1346 #endif 1299 1347 1348 #if 0 1300 1349 // HACK BEGIN REALLY UGLY HACK FOR DTS PASSTHRU 1301 1350 if (enc->codec_id == CODEC_ID_DTS) 1302 1351 { … … 1305 1354 // enc->bit_rate = what??; 1306 1355 } 1307 1356 // HACK END REALLY UGLY HACK FOR DTS PASSTHRU 1357 #endif 1308 1358 1309 1359 bitrate += enc->bit_rate; 1310 1360 break; … … 2563 2613 2564 2614 AVStream *curstream = ic->streams[pkt->stream_index]; 2565 2615 2616 #if 0 2617 VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + QString("timecode pts:%1 dts:%2 codec:%3") 2618 .arg(pkt->pts) 2619 .arg(pkt->dts) 2620 .arg((curstream && curstream->codec)?curstream->codec->codec_type:-1) 2621 ); 2622 #endif 2623 2566 2624 if (pkt->dts != (int64_t)AV_NOPTS_VALUE) 2567 2625 pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000); 2568 2626 … … 2715 2773 if (!curstream->codec->channels) 2716 2774 { 2717 2775 QMutexLocker locker(&avcodeclock); 2776 #if MAXCHANNELSELECT 2777 VERBOSE(VB_IMPORTANT, LOC + QString("Setting channels to %1").arg(audioOut.channels)); 2778 curstream->codec->cqp = max_channels; 2779 curstream->codec->channels = audioOut.channels; 2780 #else 2718 2781 curstream->codec->channels = MAX_OUTPUT_CHANNELS; 2782 #endif 2719 2783 ret = avcodec_decode_audio( 2720 2784 curstream->codec, audioSamples, 2721 2785 &data_size, ptr, len); … … 2769 2833 { 2770 2834 AVCodecContext *ctx = curstream->codec; 2771 2835 2836 #if MAXCHANNELSELECT 2772 2837 if ((ctx->channels == 0) || 2838 (ctx->channels > audioOut.channels)) 2839 ctx->channels = audioOut.channels; 2840 #else 2841 if ((ctx->channels == 0) || 2773 2842 (ctx->channels > MAX_OUTPUT_CHANNELS)) 2774 2843 ctx->channels = MAX_OUTPUT_CHANNELS; 2844 #endif 2775 2845 2776 2846 ret = avcodec_decode_audio( 2777 2847 ctx, audioSamples, &data_size, ptr, len); … … 3080 3150 3081 3151 void AvFormatDecoder::SetDisablePassThrough(bool disable) 3082 3152 { 3153 #if MAXCHANNELSELECT 3154 // can only disable never reenable as once tiemstretch is on its on for the session 3155 if (disable_passthru) 3156 return; 3157 #endif 3083 3158 if (selectedTrack[kTrackTypeAudio].av_stream_index < 0) 3084 3159 { 3085 3160 disable_passthru = disable; 3086 3161 return; 3087 3162 } 3088 3163 3089 3164 if (disable != disable_passthru) 3090 3165 { 3091 3166 disable_passthru = disable; … … 3112 3187 AVCodecContext *codec_ctx = NULL; 3113 3188 AudioInfo old_in = audioIn; 3114 3189 AudioInfo old_out = audioOut; 3190 bool using_passthru = false; 3115 3191 3116 3192 if ((currentTrack[kTrackTypeAudio] >= 0) && 3117 3193 (selectedTrack[kTrackTypeAudio].av_stream_index <= ic->nb_streams) && … … 3120 3196 { 3121 3197 assert(curstream); 3122 3198 assert(curstream->codec); 3123 codec_ctx = curstream->codec; 3199 codec_ctx = curstream->codec; 3124 3200 bool do_ac3_passthru = (allow_ac3_passthru && !transcoding && 3125 !disable_passthru &&3126 3201 (codec_ctx->codec_id == CODEC_ID_AC3)); 3127 3202 bool do_dts_passthru = (allow_dts_passthru && !transcoding && 3128 !disable_passthru &&3129 3203 (codec_ctx->codec_id == CODEC_ID_DTS)); 3204 using_passthru = do_ac3_passthru || do_dts_passthru; 3130 3205 info = AudioInfo(codec_ctx->codec_id, 3131 3206 codec_ctx->sample_rate, codec_ctx->channels, 3132 do_ac3_passthru || do_dts_passthru);3207 using_passthru && !disable_passthru); 3133 3208 } 3134 3209 3135 3210 if (info == audioIn) 3136 3211 return false; // no change 3137 3212 3213 QString ptmsg = ""; 3214 if (using_passthru) 3215 { 3216 ptmsg = QString(" using passthru"); 3217 } 3138 3218 VERBOSE(VB_AUDIO, LOC + "Initializing audio parms from " + 3139 3219 QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1)); 3140 3220 3141 3221 audioOut = audioIn = info; 3222 #if MAXCHANNELSELECT 3223 if (using_passthru) 3224 #else 3142 3225 if (audioIn.do_passthru) 3226 #endif 3143 3227 { 3144 3228 // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card 3145 audioOut.channels = 2; 3146 audioOut.sample_rate = 48000; 3147 audioOut.sample_size = 4; 3229 AudioInfo digInfo = audioOut; 3230 if (!disable_passthru) 3231 { 3232 digInfo.channels = 2; 3233 digInfo.sample_rate = 48000; 3234 digInfo.sample_size = 4; 3235 } 3236 if (audioOut.channels > max_channels) 3237 { 3238 audioOut.channels = max_channels; 3239 audioOut.sample_size = audioOut.channels * 2; 3240 codec_ctx->channels = audioOut.channels; 3241 } 3242 #if MAXCHANNELSELECT 3243 VERBOSE(VB_AUDIO, LOC + "Audio format changed digital passthrough " + 3244 QString("%1\n\t\t\tfrom %2 ; %3\n\t\t\tto %4 ; %5") 3245 .arg(digInfo.toString()) 3246 .arg(old_in.toString()).arg(old_out.toString()) 3247 .arg(audioIn.toString()).arg(audioOut.toString())); 3248 3249 if (digInfo.sample_rate > 0) 3250 GetNVP()->SetEffDsp(digInfo.sample_rate * 100); 3251 3252 //GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels, 3253 // audioOut.sample_rate); 3254 GetNVP()->SetAudioParams(digInfo.bps(), digInfo.channels, 3255 digInfo.sample_rate, audioIn.do_passthru); 3256 // allow the audio stuff to reencode 3257 GetNVP()->SetAudioCodec(codec_ctx); 3258 GetNVP()->ReinitAudio(); 3259 return true; 3260 #endif 3148 3261 } 3262 #if MAXCHANNELSELECT 3149 3263 else 3150 3264 { 3265 if (audioOut.channels > max_channels) 3266 { 3267 audioOut.channels = max_channels; 3268 audioOut.sample_size = audioOut.channels * 2; 3269 codec_ctx->channels = audioOut.channels; 3270 } 3271 } 3272 bool audiook; 3273 #if 0 3274 do 3275 { 3276 #endif 3277 #else 3278 else 3279 { 3151 3280 if (audioOut.channels > MAX_OUTPUT_CHANNELS) 3152 3281 { 3153 3282 audioOut.channels = MAX_OUTPUT_CHANNELS; … … 3155 3284 codec_ctx->channels = MAX_OUTPUT_CHANNELS; 3156 3285 } 3157 3286 } 3287 #endif 3158 3288 3159 3289 VERBOSE(VB_AUDIO, LOC + "Audio format changed " + 3160 3290 QString("\n\t\t\tfrom %1 ; %2\n\t\t\tto %3 ; %4") … … 3167 3297 GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels, 3168 3298 audioOut.sample_rate, 3169 3299 audioIn.do_passthru); 3170 GetNVP()->ReinitAudio(); 3300 // allow the audio stuff to reencode 3301 GetNVP()->SetAudioCodec(using_passthru?codec_ctx:NULL); 3302 QString errMsg = GetNVP()->ReinitAudio(); 3303 #if MAXCHANNELSELECT 3304 audiook = errMsg.isEmpty(); 3305 #if 0 3306 if (!audiook) 3307 { 3308 switch (audioOut.channels) 3309 { 3310 #if 0 3311 case 8: 3312 audioOut.channels = 6; 3313 break; 3314 #endif 3315 case 6: 3316 #if 0 3317 audioOut.channels = 5; 3318 break; 3319 case 5: 3320 audioOut.channels = 4; 3321 break; 3322 case 4: 3323 audioOut.channels = 3; 3324 break; 3325 case 3: 3326 #endif 3327 audioOut.channels = 2; 3328 break; 3329 #if 0 3330 case 2: 3331 audioOut.channels = 1; 3332 break; 3333 #endif 3334 default: 3335 // failed to reconfigure under any circumstances 3336 audiook = true; 3337 audioOut.channels = 0; 3338 break; 3339 } 3340 audioOut.sample_size = audioOut.channels * 2; 3341 codec_ctx->channels = audioOut.channels; 3342 } 3343 } while (!audiook); 3344 #endif 3345 #endif 3171 3346 3172 3347 return true; 3173 3348 } -
libs/libmythtv/NuppelVideoPlayer.h
123 123 void SetAudioInfo(const QString &main, const QString &passthru, uint rate); 124 124 void SetAudioParams(int bits, int channels, int samplerate, bool passthru); 125 125 void SetEffDsp(int dsprate); 126 void SetAudioCodec(void *ac); 126 127 127 128 // Sets 128 129 void SetParentWidget(QWidget *widget) { parentWidget = widget; } … … 196 197 bool IsDecoderThreadAlive(void) const { return decoder_thread_alive; } 197 198 bool IsReallyNearEnd(void) const; 198 199 bool IsNearEnd(long long framesRemaining = -1) const; 200 float GetAudioStretchFactor() { return audio_stretchfactor; } 199 201 bool PlayingSlowForPrebuffer(void) const { return m_playing_slower; } 200 202 bool HasAudioIn(void) const { return !no_audio_in; } 201 203 bool HasAudioOut(void) const { return !no_audio_out; } … … 226 228 bool Play(float speed = 1.0, bool normal = true, 227 229 bool unpauseaudio = true); 228 230 bool GetPause(void) const; 231 float GetNextPlaySpeed() { return next_play_speed; } 229 232 230 233 // Seek stuff 231 234 bool FastForward(float seconds); … … 659 662 int audio_bits; 660 663 int audio_samplerate; 661 664 float audio_stretchfactor; 665 void *audio_codec; 662 666 bool audio_passthru; 663 667 664 668 // Picture-in-Picture -
libs/libmythtv/NuppelVideoPlayer.cpp
202 202 audio_passthru_device(QString::null), 203 203 audio_channels(2), audio_bits(-1), 204 204 audio_samplerate(44100), audio_stretchfactor(1.0f), 205 audio_codec(NULL), 205 206 // Picture-in-Picture 206 207 pipplayer(NULL), setpipplayer(NULL), needsetpipplayer(false), 207 208 // Preview window support … … 681 682 if (audioOutput) 682 683 { 683 684 audioOutput->Reconfigure(audio_bits, audio_channels, 684 audio_samplerate, audio_passthru); 685 audio_samplerate, audio_passthru, 686 audio_codec); 685 687 errMsg = audioOutput->GetError(); 686 688 if (!errMsg.isEmpty()) 687 689 audioOutput->SetStretchFactor(audio_stretchfactor); … … 2115 2117 // decoding; display the frame, but don't wait for A/V Sync. 2116 2118 videoOutput->PrepareFrame(buffer, kScan_Intr2ndField); 2117 2119 videoOutput->Show(kScan_Intr2ndField); 2118 VERBOSE(VB_PLAYBACK , LOC + dbg + "skipping A/V wait.");2120 VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + dbg + "skipping A/V wait."); 2119 2121 } 2120 2122 else 2121 2123 { 2122 2124 // If we are using software decoding, skip this frame altogether. 2125 //VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + dbg + "dropping frame."); 2123 2126 VERBOSE(VB_PLAYBACK, LOC + dbg + "dropping frame to catch up."); 2124 2127 } 2125 2128 } … … 2129 2132 if (buffer) 2130 2133 videoOutput->PrepareFrame(buffer, ps); 2131 2134 2135 VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, QString("AVSync waitforframe %1 %2").arg(avsync_adjustment).arg(m_double_framerate)); 2132 2136 videosync->WaitForFrame(avsync_adjustment); 2137 VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, "AVSync show"); 2133 2138 if (!resetvideo) 2134 2139 videoOutput->Show(ps); 2135 2140 … … 2151 2156 2152 2157 // Display the second field 2153 2158 videosync->AdvanceTrigger(); 2154 videosync->WaitForFrame( 0);2159 videosync->WaitForFrame(avsync_adjustment); 2155 2160 if (!resetvideo) 2156 2161 { 2157 2162 videoOutput->Show(ps); … … 2179 2184 // If audio is way behind of video, adjust for it... 2180 2185 // by cutting the frame rate in half for the length of this frame 2181 2186 2182 avsync_adjustment = frame_interval; 2187 //avsync_adjustment = frame_interval; 2188 avsync_adjustment = refreshrate; 2183 2189 lastsync = true; 2190 //VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC + 2191 // QString("Video is %1 frames ahead of audio,\n" 2192 // "\t\t\tdoubling video frame interval.").arg(diverge)); 2184 2193 VERBOSE(VB_PLAYBACK, LOC + 2185 2194 QString("Video is %1 frames ahead of audio,\n" 2186 2195 "\t\t\tdoubling video frame interval to slow down.").arg(diverge)); … … 2189 2198 if (audioOutput && normal_speed) 2190 2199 { 2191 2200 long long currentaudiotime = audioOutput->GetAudiotime(); 2192 #if 02201 #if 1 2193 2202 VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, QString( 2194 2203 "A/V timecodes audio %1 video %2 frameinterval %3 " 2195 2204 "avdel %4 avg %5 tcoffset %6") … … 2514 2523 2515 2524 usevideotimebase = gContext->GetNumSetting("UseVideoTimebase", 0); 2516 2525 2517 if ((print_verbose_messages & VB_PLAYBACK ) != 0)2526 if ((print_verbose_messages & VB_PLAYBACK|VB_TIMESTAMP) == (VB_PLAYBACK|VB_TIMESTAMP)) 2518 2527 output_jmeter = new Jitterometer("video_output", 100); 2519 2528 else 2520 2529 output_jmeter = NULL; … … 2560 2569 { 2561 2570 VERBOSE(VB_IMPORTANT, "Video sync method can't support double " 2562 2571 "framerate (refresh rate too low for bob deint)"); 2572 //m_scan = kScan_Ignore; 2573 //m_can_double = false; 2563 2574 FallbackDeint(); 2564 2575 } 2565 2576 } … … 3407 3418 audio_passthru = passthru; 3408 3419 } 3409 3420 3421 void NuppelVideoPlayer::SetAudioCodec(void* ac) 3422 { 3423 audio_codec = ac; 3424 } 3425 3410 3426 void NuppelVideoPlayer::SetEffDsp(int dsprate) 3411 3427 { 3412 3428 if (audioOutput) … … 3451 3467 tc_avcheck_framecounter++; 3452 3468 if (tc_avcheck_framecounter == 30) 3453 3469 { 3454 #define AUTO_RESYNC 13470 #define AUTO_RESYNC 0 3455 3471 #if AUTO_RESYNC 3456 3472 // something's terribly, terribly wrong. 3457 3473 if (tc_lastval[TC_AUDIO] < tc_lastval[TC_VIDEO] - 10000000 || -
libs/libavcodec/a52dec.c
142 142 } 143 143 } 144 144 145 static inline int16_t convert (int32_t i) 146 { 147 if (i > 0x43c07fff) 148 return 32767; 149 else if (i < 0x43bf8000) 150 return -32768; 151 else 152 return i - 0x43c00000; 153 } 154 155 void float2s16_2 (float * _f, int16_t * s16) 156 { 157 int i; 158 int32_t * f = (int32_t *) _f; 159 160 for (i = 0; i < 256; i++) { 161 s16[2*i] = convert (f[i]); 162 s16[2*i+1] = convert (f[i+256]); 163 } 164 } 165 166 void float2s16_4 (float * _f, int16_t * s16) 167 { 168 int i; 169 int32_t * f = (int32_t *) _f; 170 171 for (i = 0; i < 256; i++) { 172 s16[4*i] = convert (f[i]); 173 s16[4*i+1] = convert (f[i+256]); 174 s16[4*i+2] = convert (f[i+512]); 175 s16[4*i+3] = convert (f[i+768]); 176 } 177 } 178 179 void float2s16_5 (float * _f, int16_t * s16) 180 { 181 int i; 182 int32_t * f = (int32_t *) _f; 183 184 for (i = 0; i < 256; i++) { 185 s16[5*i] = convert (f[i]); 186 s16[5*i+1] = convert (f[i+256]); 187 s16[5*i+2] = convert (f[i+512]); 188 s16[5*i+3] = convert (f[i+768]); 189 s16[5*i+4] = convert (f[i+1024]); 190 } 191 } 192 193 int channels_multi (int flags) 194 { 195 if (flags & A52_LFE) 196 return 6; 197 else if (flags & 1) /* center channel */ 198 return 5; 199 else if ((flags & A52_CHANNEL_MASK) == A52_2F2R) 200 return 4; 201 else 202 return 2; 203 } 204 205 void float2s16_multi (float * _f, int16_t * s16, int flags) 206 { 207 int i; 208 int32_t * f = (int32_t *) _f; 209 210 switch (flags) { 211 case A52_MONO: 212 for (i = 0; i < 256; i++) { 213 s16[5*i] = s16[5*i+1] = s16[5*i+2] = s16[5*i+3] = 0; 214 s16[5*i+4] = convert (f[i]); 215 } 216 break; 217 case A52_CHANNEL: 218 case A52_STEREO: 219 case A52_DOLBY: 220 float2s16_2 (_f, s16); 221 break; 222 case A52_3F: 223 for (i = 0; i < 256; i++) { 224 s16[5*i] = convert (f[i]); 225 s16[5*i+1] = convert (f[i+512]); 226 s16[5*i+2] = s16[5*i+3] = 0; 227 s16[5*i+4] = convert (f[i+256]); 228 } 229 break; 230 case A52_2F2R: 231 float2s16_4 (_f, s16); 232 break; 233 case A52_3F2R: 234 float2s16_5 (_f, s16); 235 break; 236 case A52_MONO | A52_LFE: 237 for (i = 0; i < 256; i++) { 238 s16[6*i] = s16[6*i+1] = s16[6*i+2] = s16[6*i+3] = 0; 239 s16[6*i+4] = convert (f[i+256]); 240 s16[6*i+5] = convert (f[i]); 241 } 242 break; 243 case A52_CHANNEL | A52_LFE: 244 case A52_STEREO | A52_LFE: 245 case A52_DOLBY | A52_LFE: 246 for (i = 0; i < 256; i++) { 247 s16[6*i] = convert (f[i+256]); 248 s16[6*i+1] = convert (f[i+512]); 249 s16[6*i+2] = s16[6*i+3] = s16[6*i+4] = 0; 250 s16[6*i+5] = convert (f[i]); 251 } 252 break; 253 case A52_3F | A52_LFE: 254 for (i = 0; i < 256; i++) { 255 s16[6*i] = convert (f[i+256]); 256 s16[6*i+1] = convert (f[i+768]); 257 s16[6*i+2] = s16[6*i+3] = 0; 258 s16[6*i+4] = convert (f[i+512]); 259 s16[6*i+5] = convert (f[i]); 260 } 261 break; 262 case A52_2F2R | A52_LFE: 263 for (i = 0; i < 256; i++) { 264 s16[6*i] = convert (f[i+256]); 265 s16[6*i+1] = convert (f[i+512]); 266 s16[6*i+2] = convert (f[i+768]); 267 s16[6*i+3] = convert (f[i+1024]); 268 s16[6*i+4] = 0; 269 s16[6*i+5] = convert (f[i]); 270 } 271 break; 272 case A52_3F2R | A52_LFE: 273 for (i = 0; i < 256; i++) { 274 s16[6*i] = convert (f[i+256]); 275 s16[6*i+1] = convert (f[i+768]); 276 s16[6*i+2] = convert (f[i+1024]); 277 s16[6*i+3] = convert (f[i+1280]); 278 s16[6*i+4] = convert (f[i+512]); 279 s16[6*i+5] = convert (f[i]); 280 } 281 break; 282 } 283 } 284 285 145 286 /**** end */ 146 287 147 288 #define HEADER_SIZE 7 … … 183 324 /* update codec info */ 184 325 avctx->sample_rate = sample_rate; 185 326 s->channels = ac3_channels[s->flags & 7]; 327 if (avctx->cqp >= 0) 328 avctx->channels = avctx->cqp; 186 329 if (s->flags & A52_LFE) 187 330 s->channels++; 188 331 if (avctx->channels == 0) … … 205 348 s->inbuf_ptr += len; 206 349 buf_size -= len; 207 350 } else { 351 int chans; 208 352 flags = s->flags; 209 353 if (avctx->channels == 1) 210 354 flags = A52_MONO; 211 else if (avctx->channels == 2) 212 flags = A52_STEREO; 355 else if (avctx->channels == 2) { 356 if (s->channels>2) 357 flags = A52_DOLBY; 358 else 359 flags = A52_STEREO; 360 } 213 361 else 214 362 flags |= A52_ADJUST_LEVEL; 215 363 level = 1; 364 chans = channels_multi(flags); 216 365 if (s->a52_frame(s->state, s->inbuf, &flags, &level, 384)) { 217 366 fail: 218 367 s->inbuf_ptr = s->inbuf; … … 222 371 for (i = 0; i < 6; i++) { 223 372 if (s->a52_block(s->state)) 224 373 goto fail; 225 float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels); 374 //float_to_int(s->samples, out_samples + i * 256 * avctx->channels, avctx->channels); 375 float2s16_multi(s->samples, out_samples + i * 256 * chans, flags); 226 376 } 227 377 s->inbuf_ptr = s->inbuf; 228 378 s->frame_size = 0; 229 *data_size = 6 * avctx->channels * 256 * sizeof(int16_t); 379 //*data_size = 6 * avctx->channels * 256 * sizeof(int16_t); 380 *data_size = 6 * chans * 256 * sizeof(int16_t); 230 381 break; 231 382 } 232 383 } -
libs/libavcodec/ac3enc.c
1336 1336 { 1337 1337 AC3EncodeContext *s = avctx->priv_data; 1338 1338 int16_t *samples = data; 1339 // expects L C R LS RS LFE 1340 // audio format is L R LS RS C LFE 1341 static int channel_index[6] = { 0, 4, 1, 2, 3, 5 }; 1342 /* 1343 * A52->Analog->AC3Enc 1344 * 1->0->0 1345 * 3->1->2 1346 * 4->2->3 1347 * 5->3->4 1348 * 2->4->1 1349 * 0->5->5 1350 */ 1339 1351 int i, j, k, v, ch; 1340 1352 int16_t input_samples[N]; 1341 1353 int32_t mdct_coef[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; … … 1356 1368 /* compute input samples */ 1357 1369 memcpy(input_samples, s->last_samples[ch], N/2 * sizeof(int16_t)); 1358 1370 sinc = s->nb_all_channels; 1359 sptr = samples + (sinc * (N/2) * i) + ch ;1371 sptr = samples + (sinc * (N/2) * i) + channel_index[ch]; 1360 1372 for(j=0;j<N/2;j++) { 1361 1373 v = *sptr; 1362 1374 input_samples[j + N/2] = v;