Index: trunk/mythplugins/mythmusic/mythmusic/playbackbox.cpp
===================================================================
--- trunk/mythplugins/mythmusic/mythmusic/playbackbox.cpp	(revision 20600)
+++ trunk/mythplugins/mythmusic/mythmusic/playbackbox.cpp	(working copy)
@@ -367,6 +367,8 @@
             changeSpeed(true);
         else if (action == "MUTE")
             toggleMute();
+        else if (action == "TOGGLEUPMIX")
+            toggleUpmix();
         else if (action == "MENU" && visualizer_status != 2)
         {
             menufilters = false;
@@ -1202,6 +1204,13 @@
     }
 }
 
+void PlaybackBoxMusic::toggleUpmix()
+{
+    if (gPlayer->getOutput())
+        gPlayer->getOutput()->ToggleUpmix();
+}
+    
+
 void PlaybackBoxMusic::showProgressBar()
 {
     if (progress_bar && visualizer_status != 2)
Index: trunk/mythplugins/mythmusic/mythmusic/main.cpp
===================================================================
--- trunk/mythplugins/mythmusic/mythmusic/main.cpp	(revision 20600)
+++ trunk/mythplugins/mythmusic/mythmusic/main.cpp	(working copy)
@@ -367,6 +367,7 @@
     REG_KEY("Music", "VOLUMEDOWN", "Volume down",       "[,{,F10,Volume Down");
     REG_KEY("Music", "VOLUMEUP",   "Volume up",         "],},F11,Volume Up");
     REG_KEY("Music", "MUTE",       "Mute",              "|,\\,F9,Volume Mute");
+    REG_KEY("Music", "TOGGLEUPMIX","Toggle upmixer",             "Ctrl+U");
     REG_KEY("Music", "CYCLEVIS",   "Cycle visualizer mode",      "6");
     REG_KEY("Music", "BLANKSCR",   "Blank screen",               "5");
     REG_KEY("Music", "THMBUP",     "Increase rating",            "9");
Index: trunk/mythplugins/mythmusic/mythmusic/musicplayer.cpp
===================================================================
--- trunk/mythplugins/mythmusic/mythmusic/musicplayer.cpp	(revision 20600)
+++ trunk/mythplugins/mythmusic/mythmusic/musicplayer.cpp	(working copy)
@@ -348,15 +348,20 @@
 
 void MusicPlayer::openOutputDevice(void)
 {
-    QString adevice;
+    QString adevice, pdevice;
 
     if (gContext->GetSetting("MusicAudioDevice") == "default")
         adevice = gContext->GetSetting("AudioOutputDevice");
     else
         adevice = gContext->GetSetting("MusicAudioDevice");
 
+    if (!gContext->GetNumSetting("MythAC3Upmix", 0))
+        pdevice = "default";
+    else
+        pdevice = gContext->GetSetting("PassThruOutputDevice");
+
     // TODO: Error checking that device is opened correctly!
-    m_output = AudioOutput::OpenAudio(adevice, "default", 16, 2, 44100,
+    m_output = AudioOutput::OpenAudio(adevice, pdevice, 16, 2, 44100,
                                     AUDIOOUTPUT_MUSIC, true, false);
     m_output->setBufferSize(256 * 1024);
     m_output->SetBlocking(false);
Index: trunk/mythplugins/mythmusic/mythmusic/playbackbox.h
===================================================================
--- trunk/mythplugins/mythmusic/mythmusic/playbackbox.h	(revision 20600)
+++ trunk/mythplugins/mythmusic/mythmusic/playbackbox.h	(working copy)
@@ -69,6 +69,7 @@
     void changeVolume(bool up_or_down);
     void changeSpeed(bool up_or_down);
     void toggleMute();
+    void toggleUpmix();
     void resetTimer();
     void hideVolume(){showVolume(false);}
     void showVolume(bool on_or_off);
Index: trunk/mythtv/libs/libmythtv/NuppelVideoPlayer.cpp
===================================================================
--- trunk/mythtv/libs/libmythtv/NuppelVideoPlayer.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmythtv/NuppelVideoPlayer.cpp	(working copy)
@@ -868,6 +868,14 @@
 
     no_audio_in = false;
 
+    bool isAC3upmix = gContext->GetNumSetting("MythAC3Upmix",0);
+    // If previously AC3 upmixer was different than current state, delete AudioOutput
+    if (audioOutput && (audioOutput->isAC3upmix != isAC3upmix))
+    {
+        delete audioOutput;
+        audioOutput = NULL;
+    }
+
     if (!audioOutput && !using_null_videoout && player_ctx->IsAudioNeeded())
     {
         bool setVolume = gContext->GetNumSetting("MythControlsVolume", 1);
@@ -876,7 +884,7 @@
                                              audio_bits, audio_channels,
                                              audio_samplerate,
                                              AUDIOOUTPUT_VIDEO,
-                                             setVolume, audio_passthru);
+                                             setVolume, audio_passthru, isAC3upmix);
         if (!audioOutput)
             errMsg = QObject::tr("Unable to create AudioOutput.");
         else
@@ -906,6 +914,8 @@
             audio_bits, audio_channels, audio_samplerate,
             audio_passthru, audio_codec);
         audioOutput->Reconfigure(settings);
+        if (audioOutput->isAC3upmix && audio_passthru)
+            audio_channels = 2;
         errMsg = audioOutput->GetError();
         if (!errMsg.isEmpty())
             audioOutput->SetStretchFactor(audio_stretchfactor);
@@ -5275,6 +5285,20 @@
     }
 }
 
+bool NuppelVideoPlayer::GetisAC3upmix()
+{
+    if (audioOutput)
+        return audioOutput->isAC3upmix;
+    return false;
+}
+
+bool NuppelVideoPlayer::ToggleUpmix()
+{
+    if (audioOutput)
+        return audioOutput->ToggleUpmix();
+    return false;
+}
+
 void NuppelVideoPlayer::Zoom(ZoomDirection direction)
 {
     if (videoOutput)
Index: trunk/mythtv/libs/libmythtv/avformatdecoder.cpp
===================================================================
--- trunk/mythtv/libs/libmythtv/avformatdecoder.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmythtv/avformatdecoder.cpp	(working copy)
@@ -430,7 +430,7 @@
       audioSamples(NULL),
       allow_ac3_passthru(false),    allow_dts_passthru(false),
       disable_passthru(false),      max_channels(2),
-      dummy_frame(NULL),
+      last_ac3_channels(0),	    dummy_frame(NULL),
       // DVD
       lastdvdtitle(-1),
       decodeStillFrame(false),
@@ -451,6 +451,7 @@
     allow_ac3_passthru = gContext->GetNumSetting("AC3PassThru", false);
     allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false);
     max_channels = (uint) gContext->GetNumSetting("MaxChannels", 2);
+    isAC3upmix = gContext->GetNumSetting("MythAC3Upmix", 0);
 
     audioIn.sample_size = -32; // force SetupAudioStream to run once
     itv = GetNVP()->GetInteractiveTV();
@@ -2975,15 +2976,9 @@
     {
         int idx = atracks[i].av_stream_index;
         AVCodecContext *codec_ctx = ic->streams[idx]->codec;
-        bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
-                                !disable_passthru &&
-                                (codec_ctx->codec_id == CODEC_ID_AC3));
-        bool do_dts_passthru = (allow_dts_passthru && !transcoding &&
-                                !disable_passthru &&
-                                (codec_ctx->codec_id == CODEC_ID_DTS));
         AudioInfo item(codec_ctx->codec_id,
                        codec_ctx->sample_rate, codec_ctx->channels,
-                       do_ac3_passthru || do_dts_passthru);
+                       DoPassThrough(codec_ctx));
         VERBOSE(VB_AUDIO, LOC + " * " + item.toString());
     }
 #endif
@@ -3117,6 +3112,7 @@
 bool AvFormatDecoder::GetFrame(int onlyvideo)
 {
     AVPacket *pkt = NULL;
+    AC3HeaderInfo hdr;
     int len;
     unsigned char *ptr;
     int data_size = 0;
@@ -3313,12 +3309,13 @@
         pts = 0;
 
         AVStream *curstream = ic->streams[pkt->stream_index];
+        AVCodecContext *ctx = curstream->codec;
 
         if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
             pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
 
-        if (ringBuffer->isDVD() &&
-            curstream->codec->codec_type == CODEC_TYPE_VIDEO)
+        if (ringBuffer->isDVD() && 
+            ctx->codec_type == CODEC_TYPE_VIDEO)
         {
             MpegPreProcessPkt(curstream, pkt);
 
@@ -3340,7 +3337,7 @@
 
             if (!d->HasMPEG2Dec())
             {
-                int current_width = curstream->codec->width;
+                int current_width = ctx->width;
                 int video_width = GetNVP()->GetVideoSize().width();
                 if (dvd_xvmc_enabled && GetNVP() && GetNVP()->getVideoOutput())
                 {
@@ -3381,7 +3378,7 @@
         }
 
         if (storevideoframes &&
-            curstream->codec->codec_type == CODEC_TYPE_VIDEO)
+            ctx->codec_type == CODEC_TYPE_VIDEO)
         {
             av_dup_packet(pkt);
             storedPackets.append(pkt);
@@ -3389,22 +3386,21 @@
             continue;
         }
 
-        if (len > 0 && curstream->codec->codec_type == CODEC_TYPE_VIDEO &&
+        if (len > 0 && ctx->codec_type == CODEC_TYPE_VIDEO &&
             pkt->stream_index == selectedVideoIndex)
         {
-            AVCodecContext *context = curstream->codec;
 
-            if (context->codec_id == CODEC_ID_MPEG1VIDEO ||
-                context->codec_id == CODEC_ID_MPEG2VIDEO ||
-                context->codec_id == CODEC_ID_MPEG2VIDEO_XVMC ||
-                context->codec_id == CODEC_ID_MPEG2VIDEO_XVMC_VLD ||
-                context->codec_id == CODEC_ID_MPEGVIDEO_VDPAU)
+            if (ctx->codec_id == CODEC_ID_MPEG1VIDEO ||
+                ctx->codec_id == CODEC_ID_MPEG2VIDEO ||
+                ctx->codec_id == CODEC_ID_MPEG2VIDEO_XVMC ||
+                ctx->codec_id == CODEC_ID_MPEG2VIDEO_XVMC_VLD ||
+                ctx->codec_id == CODEC_ID_MPEGVIDEO_VDPAU)
             {
                 if (!ringBuffer->isDVD())
                     MpegPreProcessPkt(curstream, pkt);
             }
-            else if (context->codec_id == CODEC_ID_H264 ||
-                     context->codec_id == CODEC_ID_H264_VDPAU)
+            else if (ctx->codec_id == CODEC_ID_H264 ||
+                     ctx->codec_id == CODEC_ID_H264_VDPAU)
             {
                 H264PreProcessPkt(curstream, pkt);
             }
@@ -3449,8 +3445,8 @@
         }
 
         if (len > 0 &&
-            curstream->codec->codec_type == CODEC_TYPE_DATA &&
-            curstream->codec->codec_id   == CODEC_ID_MPEG2VBI)
+            ctx->codec_type == CODEC_TYPE_DATA &&
+            ctx->codec_id   == CODEC_ID_MPEG2VBI)
         {
             ProcessVBIDataPacket(curstream, pkt);
 
@@ -3459,8 +3455,8 @@
         }
 
         if (len > 0 &&
-            curstream->codec->codec_type == CODEC_TYPE_DATA &&
-            curstream->codec->codec_id   == CODEC_ID_DVB_VBI)
+            ctx->codec_type == CODEC_TYPE_DATA &&
+            ctx->codec_id   == CODEC_ID_DVB_VBI)
         {
             ProcessDVBDataPacket(curstream, pkt);
 
@@ -3470,8 +3466,8 @@
 
 #ifdef USING_MHEG
         if (len > 0 &&
-            curstream->codec->codec_type == CODEC_TYPE_DATA &&
-            curstream->codec->codec_id   == CODEC_ID_DSMCC_B)
+            ctx->codec_type == CODEC_TYPE_DATA &&
+            ctx->codec_id   == CODEC_ID_DSMCC_B)
         {
             ProcessDSMCCPacket(curstream, pkt);
 
@@ -3492,20 +3488,20 @@
 #endif // USING_MHEG
 
         // we don't care about other data streams
-        if (curstream->codec->codec_type == CODEC_TYPE_DATA)
+        if (ctx->codec_type == CODEC_TYPE_DATA)
         {
             av_free_packet(pkt);
             continue;
         }
 
-        if (!curstream->codec->codec)
+        if (!ctx->codec)
         {
             VERBOSE(VB_PLAYBACK, LOC +
                     QString("No codec for stream index %1, type(%2) id(%3:%4)")
                     .arg(pkt->stream_index)
-                    .arg(codec_type_string(curstream->codec->codec_type))
-                    .arg(codec_id_string(curstream->codec->codec_id))
-                    .arg(curstream->codec->codec_id));
+                    .arg(codec_type_string(ctx->codec_type))
+                    .arg(codec_id_string(ctx->codec_id))
+                    .arg(ctx->codec_id));
             av_free_packet(pkt);
             continue;
         }
@@ -3514,7 +3510,7 @@
         have_err = false;
 
         avcodeclock.lock();
-        int ctype  = curstream->codec->codec_type;
+        int ctype  = ctx->codec_type;
         int audIdx = selectedTrack[kTrackTypeAudio].av_stream_index;
         int audSubIdx = selectedTrack[kTrackTypeAudio].av_substream_index;
         int subIdx = selectedTrack[kTrackTypeSubtitle].av_stream_index;
@@ -3539,53 +3535,59 @@
 
                     // detect switches between stereo and dual languages
                     bool wasDual = audSubIdx != -1;
-                    bool isDual = curstream->codec->avcodec_dual_language;
+                    bool isDual = ctx->avcodec_dual_language;
                     if ((wasDual && !isDual) || (!wasDual &&  isDual))
                     {
                         SetupAudioStreamSubIndexes(audIdx);
                         reselectAudioTrack = true;
                     }
 
-                    bool do_ac3_passthru =
-                        (allow_ac3_passthru && !transcoding &&
-                         (curstream->codec->codec_id == CODEC_ID_AC3));
-                    bool do_dts_passthru =
-                        (allow_dts_passthru && !transcoding &&
-                         (curstream->codec->codec_id == CODEC_ID_DTS));
-                    bool using_passthru = do_ac3_passthru || do_dts_passthru;
-
                     // detect channels on streams that need
                     // to be decoded before we can know this
                     bool already_decoded = false;
-                    if (!curstream->codec->channels)
+                    if (!ctx->channels)
                     {
                         QMutexLocker locker(&avcodeclock);
                         VERBOSE(VB_IMPORTANT, LOC +
                                 QString("Setting channels to %1")
                                 .arg(audioOut.channels));
 
-                        if (using_passthru)
+                        if (DoPassThrough(ctx))
                         {
                             // for passthru let it select the max number
                             // of channels
-                            curstream->codec->channels = 0;
-                            curstream->codec->request_channels = 0;
+                            ctx->channels = 0;
+                            ctx->request_channels = 0;
                         }
                         else
                         {
-                            curstream->codec->channels = audioOut.channels;
-                            curstream->codec->request_channels =
+                            ctx->channels = audioOut.channels;
+                            ctx->request_channels =
                                 audioOut.channels;
                         }
+                        
                         data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
-                        ret = avcodec_decode_audio2(curstream->codec,
-                                                    audioSamples, &data_size,
-                                                    ptr, len);
+                        ret = avcodec_decode_audio2(ctx, audioSamples,
+                                                   &data_size, ptr, len);
                         already_decoded = true;
 
-                        reselectAudioTrack |= curstream->codec->channels;
+                        reselectAudioTrack |= ctx->channels;
                     }
 
+                    if (isAC3upmix && ctx->codec_id == CODEC_ID_AC3)
+                    {
+                        GetBitContext gbc;
+                        init_get_bits(&gbc, ptr, len * 8);
+                        if (!ff_ac3_parse_header(&gbc, &hdr)) 
+                        {
+                            if (hdr.channels != last_ac3_channels) 
+                            {
+                                last_ac3_channels = ctx->channels = hdr.channels;
+                                SetupAudioStream();
+                            }
+                        }
+                    }
+
                     if (reselectAudioTrack)
                     {
                         QMutexLocker locker(&avcodeclock);
@@ -3599,6 +3601,7 @@
                             .av_stream_index;
                         audSubIdx = selectedTrack[kTrackTypeAudio]
                             .av_substream_index;
+                        ctx = curstream->codec;
                     }
 
                     if ((onlyvideo > 0) || (pkt->stream_index != audIdx))
@@ -3630,14 +3633,12 @@
                     if (audioOut.do_passthru)
                     {
                         data_size = pkt->size;
-                        bool dts = CODEC_ID_DTS == curstream->codec->codec_id;
+                        bool dts = CODEC_ID_DTS == ctx->codec_id;
                         ret = encode_frame(dts, ptr, len,
                                            audioSamples, data_size);
                     }
                     else
                     {
-                        AVCodecContext *ctx = curstream->codec;
-
                         if ((ctx->channels == 0) ||
                             (ctx->channels > audioOut.channels))
                         {
@@ -3646,10 +3647,9 @@
 
                         if (!already_decoded)
                         {
-                            curstream->codec->request_channels =
-                                audioOut.channels;
                             data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
-                            ret = avcodec_decode_audio2(ctx, audioSamples,
+                            ctx->request_channels = audioOut.channels;
+                            ret = avcodec_decode_audio2(ctx, audioSamples, 
                                                         &data_size, ptr, len);
                         }
 
@@ -3665,6 +3665,7 @@
                             audIdx = -1;
                             AutoSelectAudioTrack();
                             data_size = 0;
+                            ctx = curstream->codec;
                         }
                     }
                     avcodeclock.unlock();
@@ -3682,8 +3683,7 @@
 
                     // calc for next frame
                     lastapts += (long long)((double)(data_size * 1000) /
-                                (curstream->codec->channels * 2) /
-                                curstream->codec->sample_rate);
+                                (ctx->channels * 2) / ctx->sample_rate);
 
                     VERBOSE(VB_PLAYBACK+VB_TIMESTAMP,
                             LOC + QString("audio timecode %1 %2 %3 %4")
@@ -3751,7 +3751,6 @@
                         continue;
                     }
 
-                    AVCodecContext *context = curstream->codec;
                     AVFrame mpa_pic;
                     bzero(&mpa_pic, sizeof(AVFrame));
 
@@ -3766,24 +3765,24 @@
                             // HACK
                             while (!gotpicture && count < 5)
                             {
-                                ret = d->DecodeMPEG2Video(context, &mpa_pic,
+                                ret = d->DecodeMPEG2Video(ctx, &mpa_pic,
                                                   &gotpicture, ptr, len);
                                 count++;
                             }
                         }
                         else
                         {
-                            ret = d->DecodeMPEG2Video(context, &mpa_pic,
+                            ret = d->DecodeMPEG2Video(ctx, &mpa_pic,
                                                 &gotpicture, ptr, len);
                         }
                     }
                     else
                     {
-                        ret = avcodec_decode_video(context, &mpa_pic,
+                        ret = avcodec_decode_video(ctx, &mpa_pic,
                                                    &gotpicture, ptr, len);
                         // Reparse it to not drop the DVD still frame
                         if (decodeStillFrame)
-                            ret = avcodec_decode_video(context, &mpa_pic,
+                            ret = avcodec_decode_video(ctx, &mpa_pic,
                                                         &gotpicture, ptr, len);
                     }
                     avcodeclock.unlock();
@@ -3855,9 +3854,9 @@
 #endif
                             &tmppicture, PIX_FMT_YUV420P,
                                     (AVPicture *)&mpa_pic,
-                                    context->pix_fmt,
-                                    context->width,
-                                    context->height);
+                                    ctx->pix_fmt,
+                                    ctx->width,
+                                    ctx->height);
 
                         if (xf)
                         {
@@ -3880,10 +3879,10 @@
                         (temppts + 10000 > lastvpts || temppts < 0))
                     {
                         temppts = lastvpts;
-                        temppts += (long long)(1000 * av_q2d(context->time_base));
+                        temppts += (long long)(1000 * av_q2d(ctx->time_base));
                         // MPEG2 frames can be repeated, update pts accordingly
                         temppts += (long long)(mpa_pic.repeat_pict * 500
-                                      * av_q2d(curstream->codec->time_base));
+                                      * av_q2d(ctx->time_base));
                     }
 
                     VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC +
@@ -3919,7 +3918,7 @@
                     picframe->frameNumber = framesPlayed;
                     GetNVP()->ReleaseNextVideoFrame(picframe, temppts);
                     if (d->HasMPEG2Dec() && mpa_pic.data[3])
-                        context->release_buffer(context, &mpa_pic);
+                        ctx->release_buffer(ctx, &mpa_pic);
 
                     decoded_video_frame = picframe;
                     gotvideo = 1;
@@ -3975,11 +3974,10 @@
                 }
                 default:
                 {
-                    AVCodecContext *enc = curstream->codec;
                     VERBOSE(VB_IMPORTANT, LOC_ERR +
                             QString("Decoding - id(%1) type(%2)")
-                            .arg(codec_id_string(enc->codec_id))
-                            .arg(codec_type_string(enc->codec_type)));
+                            .arg(codec_id_string(ctx->codec_id))
+                            .arg(codec_type_string(ctx->codec_type)));
                     have_err = true;
                     break;
                 }
@@ -4128,6 +4126,35 @@
     }
 }
 
+bool AvFormatDecoder::DoPassThrough(const AVCodecContext *ctx)
+{
+    bool do_ac3_passthru =
+        (allow_ac3_passthru && !transcoding &&
+        (ctx->codec_id == CODEC_ID_AC3));
+    bool do_dts_passthru =
+        (allow_dts_passthru && !transcoding &&
+        (ctx->codec_id == CODEC_ID_DTS));
+    bool using_passthru = do_ac3_passthru || do_dts_passthru;
+
+    if (!isAC3upmix)
+        return using_passthru;
+
+    bool passthru = false; 
+
+    if (ctx->codec_id == CODEC_ID_AC3)
+        passthru = allow_ac3_passthru && 
+                   ctx->channels >= (int)max_channels;
+    else if (ctx->codec_id == CODEC_ID_DTS)
+        passthru = allow_dts_passthru;
+    
+    passthru &= !transcoding && !disable_passthru;
+    // Don't know any cards that support spdif clocked at < 44100
+    // Some US cable transmissions have 2ch 32k AC-3 streams
+    passthru &= ctx->sample_rate >= 44100;
+
+    return passthru;
+}
+
 /** \fn AvFormatDecoder::SetupAudioStream(void)
  *  \brief Reinitializes audio if it needs to be reinitialized.
  *
@@ -4155,14 +4182,14 @@
         codec_ctx = curstream->codec;
         if (codec_ctx)
         {
-            bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
-                                    (codec_ctx->codec_id == CODEC_ID_AC3));
-            bool do_dts_passthru = (allow_dts_passthru && !transcoding &&
-                                    (codec_ctx->codec_id == CODEC_ID_DTS));
-            using_passthru = do_ac3_passthru || do_dts_passthru;
-            info = AudioInfo(codec_ctx->codec_id,
-                             codec_ctx->sample_rate, codec_ctx->channels,
-                             using_passthru && !disable_passthru);
+            using_passthru = DoPassThrough(codec_ctx);
+            if (!isAC3upmix)
+                info = AudioInfo(codec_ctx->codec_id,
+                                 codec_ctx->sample_rate, codec_ctx->channels,
+                                 using_passthru && !disable_passthru);
+            else
+                info = AudioInfo(codec_ctx->codec_id, codec_ctx->sample_rate, 
+                                codec_ctx->channels, using_passthru);
         }
     }
 
@@ -4179,6 +4206,8 @@
             QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1));
 
     audioOut = audioIn = info;
+
+    if (!isAC3upmix) {
     AudioInfo tmpAudioOut = audioOut;
 
     // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card
@@ -4214,6 +4243,25 @@
 
     // allow the audio stuff to reencode
     GetNVP()->SetAudioCodec((using_passthru) ? codec_ctx : NULL);
+    } else {
+    if (!using_passthru && audioOut.channels > (int)max_channels)
+    {
+        audioOut.channels = (int)max_channels;
+        audioOut.sample_size = audioOut.channels * 2;
+        codec_ctx->channels = audioOut.channels;
+    }
+
+    VERBOSE(VB_AUDIO, LOC + "Audio format changed " +
+            QString("\n\t\t\tfrom %1 to %2")
+            .arg(old_in.toString()).arg(audioOut.toString()));
+
+    if (audioOut.sample_rate > 0)
+        GetNVP()->SetEffDsp(audioOut.sample_rate * 100);
+
+    GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels,
+                             audioOut.sample_rate, audioOut.do_passthru);
+
+    }
     GetNVP()->ReinitAudio();
 
     return true;
Index: trunk/mythtv/libs/libmythtv/tv_play.h
===================================================================
--- trunk/mythtv/libs/libmythtv/tv_play.h	(revision 20600)
+++ trunk/mythtv/libs/libmythtv/tv_play.h	(working copy)
@@ -418,6 +418,7 @@
         ARBSEEK_FORWARD,
         ARBSEEK_END
     };
+    
     void DoArbSeek(PlayerContext*, ArbSeekWhence whence);
     void NormalSpeed(PlayerContext*);
     void ChangeSpeed(PlayerContext*, int direction);
@@ -426,6 +427,7 @@
     bool TimeStretchHandleAction(PlayerContext*,
                                  const QStringList &actions);
 
+    void ToggleUpmix(PlayerContext*);
     void ChangeAudioSync(PlayerContext*, int dir, bool allowEdit = true);
     bool AudioSyncHandleAction(PlayerContext*, const QStringList &actions);
 
Index: trunk/mythtv/libs/libmythtv/NuppelVideoPlayer.h
===================================================================
--- trunk/mythtv/libs/libmythtv/NuppelVideoPlayer.h	(revision 20600)
+++ trunk/mythtv/libs/libmythtv/NuppelVideoPlayer.h	(working copy)
@@ -182,6 +182,8 @@
     // Toggle Sets
     void ToggleAspectOverride(AspectOverrideMode aspectMode = kAspect_Toggle);
     void ToggleAdjustFill(AdjustFillMode adjustfillMode = kAdjustFill_Toggle);
+    bool GetisAC3upmix(void);
+    bool ToggleUpmix(void);
 
     // Gets
     QSize   GetVideoBufferSize(void) const    { return video_dim; }
Index: trunk/mythtv/libs/libmythtv/tv_play.cpp
===================================================================
--- trunk/mythtv/libs/libmythtv/tv_play.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmythtv/tv_play.cpp	(working copy)
@@ -491,6 +491,7 @@
     REG_KEY("TV Playback", "VOLUMEDOWN", "Volume down", "[,{,F10,Volume Down");
     REG_KEY("TV Playback", "VOLUMEUP",   "Volume up",   "],},F11,Volume Up");
     REG_KEY("TV Playback", "MUTE",       "Mute",        "|,\\,F9,Volume Mute");
+    REG_KEY("TV Playback", "TOGGLEUPMIX", "Toggle upmixer", "Ctrl+U");
     REG_KEY("TV Playback", "TOGGLEPIPMODE", "Toggle Picture-in-Picture view",
             "V");
     REG_KEY("TV Playback", "TOGGLEPBPMODE", "Toggle Picture-by-Picture view",
@@ -630,7 +631,7 @@
   Teletext     F2,F3,F4,F5,F6,F7,F8
   ITV          F2,F3,F4,F5,F6,F7,F12
 
-  Playback: Ctrl-B,Ctrl-G,Ctrl-Y
+  Playback: Ctrl-B,Ctrl-G,Ctrl-Y,Ctrl-U
 */
 }
 
@@ -4341,6 +4342,8 @@
         DoTogglePictureAttribute(ctx, kAdjustingPicture_Playback);
     else if (has_action("TOGGLESTRETCH", actions))
         ToggleTimeStretch(ctx);
+    else if (has_action("TOGGLEUPMIX", actions))
+        ToggleUpmix(ctx);
     else if (has_action("TOGGLESLEEP", actions))
         ToggleSleepTimer(ctx);
     else if (has_action("TOGGLERECORD", actions) && islivetv)
@@ -7998,6 +8001,24 @@
     SetSpeedChangeTimer(0, __LINE__);
 }
 
+void TV::ToggleUpmix(PlayerContext *ctx)
+{
+    if (!ctx->nvp || !ctx->nvp->HasAudioOut())
+        return;
+    QString text;
+    if(!ctx->nvp->GetisAC3upmix()) {
+        text = tr("Upmixer disabled !");
+    } else {
+        if (ctx->nvp->ToggleUpmix())
+            text = tr("Upmixer On");
+        else
+            text = tr("Upmixer Off");
+    }
+
+    if (ctx->nvp->GetOSD() && !browsemode)
+        ctx->nvp->GetOSD()->SetSettingsText(text, 5);
+}
+    
 // dir in 10ms jumps
 void TV::ChangeAudioSync(PlayerContext *ctx, int dir, bool allowEdit)
 {
@@ -9632,6 +9653,8 @@
         SetManualZoom(actx, true, tr("Zoom Mode ON"));
     else if (action == "TOGGLESTRETCH")
         ToggleTimeStretch(actx);
+    else if (action == "TOGGLEUPMIX")
+        ToggleUpmix(actx);
     else if (action.left(13) == "ADJUSTSTRETCH")
     {
         bool floatRead;
@@ -9985,6 +10008,8 @@
 
     if (category == "AUDIOSYNC")
         new OSDGenericTree(treeMenu, tr("Adjust Audio Sync"), "TOGGLEAUDIOSYNC");
+    else if ((category == "TOGGLEUPMIX") && ctx->nvp && ctx->nvp->GetisAC3upmix())
+        new OSDGenericTree(treeMenu, tr("Toggle Upmixer"), "TOGGLEUPMIX");
     else if (category == "TIMESTRETCH")
         FillMenuTimeStretch(ctx, treeMenu);
     else if (category == "VIDEOSCAN")
Index: trunk/mythtv/libs/libmythtv/tvosdmenuentry.cpp
===================================================================
--- trunk/mythtv/libs/libmythtv/tvosdmenuentry.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmythtv/tvosdmenuentry.cpp	(working copy)
@@ -232,6 +232,8 @@
     curMenuEntries.append(
         new TVOSDMenuEntry("AUDIOSYNC",           1, 1, 1, 1 , "Audio Sync"));
     curMenuEntries.append(
+        new TVOSDMenuEntry("TOGGLEUPMIX",        1, 1, 1, 1, "Toggle Upmixer"));
+    curMenuEntries.append(
         new TVOSDMenuEntry("TIMESTRETCH",        1, 1, 1, 1, "Time Stretch"));
     curMenuEntries.append(
         new TVOSDMenuEntry("VIDEOSCAN",            1, 1, 1, 1, "Video Scan"));
Index: trunk/mythtv/libs/libmythtv/avformatdecoder.h
===================================================================
--- trunk/mythtv/libs/libmythtv/avformatdecoder.h	(revision 20600)
+++ trunk/mythtv/libs/libmythtv/avformatdecoder.h	(working copy)
@@ -200,6 +200,7 @@
 
     void SeekReset(long long, uint skipFrames, bool doFlush, bool discardFrames);
 
+    bool DoPassThrough(const AVCodecContext *ctx);
     bool SetupAudioStream(void);
     void SetupAudioStreamSubIndexes(int streamIndex);
     void RemoveAudioStreams();
@@ -272,6 +273,8 @@
     bool              allow_dts_passthru;
     bool              disable_passthru;
     uint              max_channels;
+    uint              last_ac3_channels;
+    bool              isAC3upmix;
 
     VideoFrame       *dummy_frame;
 
Index: trunk/mythtv/libs/libmythsamplerate/samplerate.c
===================================================================
--- trunk/mythtv/libs/libmythsamplerate/samplerate.c	(revision 20600)
+++ trunk/mythtv/libs/libmythsamplerate/samplerate.c	(working copy)
@@ -452,11 +452,11 @@
 	{	len -- ;
 
 		scaled_value = in [len] * (8.0 * 0x10000000) ;
-		if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
+		if (scaled_value >= (1.0 * 0x7FFFFFFF))
 		{	out [len] = 32767 ;
 			continue ;
 			} ;
-		if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
+		if (scaled_value <= (-8.0 * 0x10000000))
 		{	out [len] = -32768 ;
 			continue ;
 			} ;
Index: trunk/mythtv/libs/libmyth/audiosettings.cpp
===================================================================
--- trunk/mythtv/libs/libmyth/audiosettings.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiosettings.cpp	(working copy)
@@ -16,7 +16,8 @@
     set_initial_vol(false),
     use_passthru(false),
     codec(NULL),
-    source(AUDIOOUTPUT_UNKNOWN)
+    source(AUDIOOUTPUT_UNKNOWN),
+    isAC3upmix(false)
 {
 }
 
@@ -29,7 +30,8 @@
     set_initial_vol(other.set_initial_vol),
     use_passthru(other.use_passthru),
     codec(other.codec),
-    source(other.source)
+    source(other.source),
+    isAC3upmix(other.isAC3upmix)
 {
 }
 
@@ -42,7 +44,8 @@
     AudioOutputSource audio_source,
     bool audio_set_initial_vol,
     bool audio_use_passthru,
-    void *audio_codec) :
+    void *audio_codec,
+    bool AC3upmix) :
     main_device(audio_main_device),
     passthru_device(audio_passthru_device),
     bits(audio_bits),
@@ -51,7 +54,8 @@
     set_initial_vol(audio_set_initial_vol),
     use_passthru(audio_use_passthru),
     codec(audio_codec),
-    source(audio_source)
+    source(audio_source),
+    isAC3upmix(AC3upmix)
 {
 }
 
@@ -60,7 +64,8 @@
     int   audio_channels, 
     int   audio_samplerate,
     bool  audio_use_passthru,
-    void *audio_codec) :
+    void *audio_codec,
+    bool  AC3upmix) :
     main_device(QString::null),
     passthru_device(QString::null),
     bits(audio_bits),
@@ -69,7 +74,8 @@
     set_initial_vol(false),
     use_passthru(audio_use_passthru),
     codec(audio_codec),
-    source(AUDIOOUTPUT_UNKNOWN)
+    source(AUDIOOUTPUT_UNKNOWN),
+    isAC3upmix(AC3upmix)
 {
 }
 
Index: trunk/mythtv/libs/libmyth/audiooutputbase.h
===================================================================
--- trunk/mythtv/libs/libmyth/audiooutputbase.h	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiooutputbase.h	(working copy)
@@ -43,6 +43,7 @@
 
     virtual void SetStretchFactor(float factor);
     virtual float GetStretchFactor(void) const;
+    virtual bool ToggleUpmix(void);
 
     virtual void Reset(void);
 
@@ -132,6 +133,8 @@
     QString audio_passthru_device;
 
     bool audio_passthru;
+    bool audio_enc;
+    bool audio_reenc;
 
     float audio_stretchfactor;
     AVCodecContext *audio_codec;
@@ -144,6 +147,9 @@
     bool buffer_output_data_for_use; //  used by AudioOutputNULL
 
     int configured_audio_channels;
+    int orig_config_channels;
+    int src_quality;
+    int source_audio_channels;
 
  private:
     // resampler
@@ -155,10 +161,12 @@
     AudioOutputDigitalEncoder *encoder;
     FreeSurround              *upmixer;
 
-    int source_audio_channels;
+    int source_audio_samplerate;
     int source_audio_bytes_per_sample;
     bool needs_upmix;
     int surround_mode;
+    bool allow_ac3_passthru;
+    float old_audio_stretchfactor;
 
     bool blocking; // do AddSamples calls block?
 
Index: trunk/mythtv/libs/libmyth/audiooutputalsa.cpp
===================================================================
--- trunk/mythtv/libs/libmyth/audiooutputalsa.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiooutputalsa.cpp	(working copy)
@@ -32,23 +32,92 @@
 AudioOutputALSA::~AudioOutputALSA()
 {
     KillAudio();
+    if (isAC3upmix)
+        SetIECStatus(true);
 }
 
+void AudioOutputALSA::SetIECStatus(bool audio) {
+    
+    snd_ctl_t *ctl;
+    const char *spdif_str = SND_CTL_NAME_IEC958("", PLAYBACK, DEFAULT);
+    int spdif_index = -1;
+    snd_ctl_elem_list_t *clist;
+    snd_ctl_elem_id_t *cid;
+    snd_ctl_elem_value_t *cval;
+    snd_aes_iec958_t iec958;
+    int cidx, controls;
+
+    VERBOSE(VB_AUDIO, QString("Setting IEC958 status: %1")
+                      .arg(audio ? "audio" : "non-audio"));
+    
+    int err;
+    if ((err = snd_ctl_open(&ctl, "default", 0)) < 0)
+    {
+        Error(QString("AudioOutputALSA::SetIECStatus: snd_ctl_open(default): %1")
+              .arg(snd_strerror(err)));
+        return;
+    }
+    snd_ctl_elem_list_alloca(&clist);
+    snd_ctl_elem_list(ctl, clist);
+    snd_ctl_elem_list_alloc_space(clist, snd_ctl_elem_list_get_count(clist));
+    snd_ctl_elem_list(ctl, clist);
+    controls = snd_ctl_elem_list_get_used(clist);
+    for (cidx = 0; cidx < controls; cidx++) {
+        if (!strcmp(snd_ctl_elem_list_get_name(clist, cidx), spdif_str))
+            if (spdif_index < 0 ||
+                snd_ctl_elem_list_get_index(clist, cidx) == (uint)spdif_index)
+                    break;
+    }
+
+    if (cidx >= controls)
+        return;
+
+    snd_ctl_elem_id_alloca(&cid);
+    snd_ctl_elem_list_get_id(clist, cidx, cid);
+    snd_ctl_elem_value_alloca(&cval);
+    snd_ctl_elem_value_set_id(cval, cid);
+    snd_ctl_elem_read(ctl,cval);
+    snd_ctl_elem_value_get_iec958(cval, &iec958);
+    
+    if (!audio) 
+        iec958.status[0] |= IEC958_AES0_NONAUDIO;
+    else
+        iec958.status[0] &= ~IEC958_AES0_NONAUDIO;
+
+    snd_ctl_elem_value_set_iec958(cval, &iec958);
+    snd_ctl_elem_write(ctl, cval);
+
+}
+
 bool AudioOutputALSA::OpenDevice()
 {
     snd_pcm_format_t format;
     unsigned int buffer_time, period_time;
     int err;
+    QString real_device;
 
     if (pcm_handle != NULL)
         CloseDevice();
 
     pcm_handle = NULL;
     numbadioctls = 0;
+    
+    if (!isAC3upmix) {
+        real_device = (audio_passthru) ?
+            audio_passthru_device : audio_main_device;
+    }
+    else 
+        if (audio_passthru || audio_enc)
+        {
+            real_device = audio_passthru_device;
+            SetIECStatus(false);
+        }
+        else 
+        {
+            real_device = audio_main_device;
+            SetIECStatus(true);
+        }
 
-    QString real_device = (audio_passthru) ?
-        audio_passthru_device : audio_main_device;
-
     VERBOSE(VB_GENERAL, QString("Opening ALSA audio device '%1'.")
             .arg(real_device));
 
@@ -147,6 +216,20 @@
 }
 
 
+void AudioOutputALSA::reorder_6ch_ac3(void *buf, unsigned int len) {
+    unsigned short *src = (unsigned short *)buf;
+    unsigned short tmp;
+    unsigned int samples = len >> 1;
+
+    for (uint i = 0; i < samples; i += 6) {
+        tmp = src[i+1];
+        src[i+1] = src[i+2];
+        src[i+2] = src[i+3];
+        src[i+3] = src[i+4];
+        src[i+4] = tmp;
+    }
+}
+
 void AudioOutputALSA::WriteAudio(unsigned char *aubuf, int size)
 {
     unsigned char *tmpbuf;
@@ -159,6 +242,10 @@
         return;
     }
     
+    // Re-Order channels mapping if analog output is used and source is stereo
+    if (isAC3upmix && !audio_passthru && (source_audio_channels > 2)) {
+        reorder_6ch_ac3(aubuf, size);
+    }
     tmpbuf = aubuf;
 
     VERBOSE(VB_AUDIO+VB_TIMESTAMP,
Index: trunk/mythtv/libs/libmyth/audiooutputbase.cpp
===================================================================
--- trunk/mythtv/libs/libmyth/audiooutputbase.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiooutputbase.cpp	(working copy)
@@ -28,7 +28,8 @@
 
     audio_main_device(settings.GetMainDevice()),
     audio_passthru_device(settings.GetPassthruDevice()),
-    audio_passthru(false),      audio_stretchfactor(1.0f),
+    audio_passthru(false),      audio_enc(false),
+    audio_reenc(false),         audio_stretchfactor(1.0f),
 
     audio_codec(NULL),
     source(settings.source),    killaudio(false),
@@ -47,10 +48,13 @@
     pSoundStretch(NULL),
     encoder(NULL),
     upmixer(NULL),
+
     source_audio_channels(-1),
+    source_audio_samplerate(0),
     source_audio_bytes_per_sample(0),
     needs_upmix(false),
     surround_mode(FreeSurround::SurroundModePassive),
+    old_audio_stretchfactor(1.0),
 
     blocking(false),
 
@@ -79,6 +83,10 @@
     memset(&audiotime_updated, 0, sizeof(audiotime_updated));
     memset(audiobuffer,        0, sizeof(char)  * kAudioRingBufferSize);
     configured_audio_channels = gContext->GetNumSetting("MaxChannels", 2);
+    orig_config_channels = configured_audio_channels;
+    allow_ac3_passthru = gContext->GetNumSetting("AC3PassThru", false);
+    src_quality = gContext->GetNumSetting("SRCQuality", 3);
+    isAC3upmix = settings.isAC3upmix;
 
     // You need to call Reconfigure from your concrete class.
     // Reconfigure(laudio_bits,       laudio_channels,
@@ -110,7 +118,7 @@
 void AudioOutputBase::SetStretchFactorLocked(float laudio_stretchfactor)
 {
     effdspstretched = (int)((float)effdsp / laudio_stretchfactor);
-    if ((audio_stretchfactor != laudio_stretchfactor) ||  !pSoundStretch)
+    if ((audio_stretchfactor != laudio_stretchfactor) || !pSoundStretch)
     {
         audio_stretchfactor = laudio_stretchfactor;
         if (pSoundStretch)
@@ -124,48 +132,52 @@
             VERBOSE(VB_GENERAL, LOC + QString("Using time stretch %1")
                                         .arg(audio_stretchfactor));
             pSoundStretch = new soundtouch::SoundTouch();
-            if (audio_codec)
-            {
-                if (!encoder)
+            if (!isAC3upmix) {
+                if (audio_codec)
                 {
-                    VERBOSE(VB_AUDIO, LOC +
-                            QString("Creating Encoder for codec %1 origfs %2")
-                            .arg(audio_codec->codec_id)
-                            .arg(audio_codec->frame_size));
-
-                    encoder = new AudioOutputDigitalEncoder();
-                    if (!encoder->Init(audio_codec->codec_id,
-                                audio_codec->bit_rate,
-                                audio_codec->sample_rate,
-                                audio_codec->channels
-                                ))
+                    if (!encoder)
                     {
-                        // eeks
-                        delete encoder;
-                        encoder = NULL;
                         VERBOSE(VB_AUDIO, LOC +
-                                QString("Failed to Create Encoder"));
+                                QString("Creating Encoder for codec %1 origfs %2")
+                                .arg(audio_codec->codec_id)
+                                .arg(audio_codec->frame_size));
+
+                        encoder = new AudioOutputDigitalEncoder(isAC3upmix);
+                        if (!encoder->Init(audio_codec->codec_id,
+                                    audio_codec->bit_rate,
+                                    audio_codec->sample_rate,
+                                    audio_codec->channels
+                                    ))
+                        {
+                            // eeks
+                            delete encoder;
+                            encoder = NULL;
+                            VERBOSE(VB_AUDIO, LOC +
+                                    QString("Failed to Create Encoder"));
+                        }
                     }
                 }
-            }
-            if (audio_codec && encoder)
-            {
-                pSoundStretch->setSampleRate(audio_codec->sample_rate);
-                pSoundStretch->setChannels(audio_codec->channels);
-            }
-            else
-            {
+                if (audio_codec && encoder)
+                {
+                    pSoundStretch->setSampleRate(audio_codec->sample_rate);
+                    pSoundStretch->setChannels(audio_codec->channels);
+                }
+                else
+                {
+                    pSoundStretch->setSampleRate(audio_samplerate);
+                    pSoundStretch->setChannels(audio_channels);
+                }
+            } else {
                 pSoundStretch->setSampleRate(audio_samplerate);
-                pSoundStretch->setChannels(audio_channels);
+                pSoundStretch->setChannels(upmixer ? 
+                    configured_audio_channels : source_audio_channels);
             }
-
             pSoundStretch->setTempo(audio_stretchfactor);
             pSoundStretch->setSetting(SETTING_SEQUENCE_MS, 35);
 
             // dont need these with only tempo change
             //pSoundStretch->setPitch(1.0);
             //pSoundStretch->setRate(1.0);
-
             //pSoundStretch->setSetting(SETTING_USE_QUICKSEEK, true);
             //pSoundStretch->setSetting(SETTING_USE_AA_FILTER, false);
         }
@@ -183,6 +195,22 @@
     return audio_stretchfactor;
 }
 
+bool AudioOutputBase::ToggleUpmix(void)
+{
+    if (orig_config_channels == 2 || audio_passthru)
+        return false;
+    if (configured_audio_channels == 6)
+        configured_audio_channels = 2;
+    else
+        configured_audio_channels = 6;
+
+    const AudioSettings settings(audio_bits, source_audio_channels,
+                            source_audio_samplerate, audio_passthru);
+    Reconfigure(settings); 
+    return (configured_audio_channels == 6);
+}
+
+
 void AudioOutputBase::Reconfigure(const AudioSettings &orig_settings)
 {
     AudioSettings settings = orig_settings;
@@ -193,29 +221,45 @@
     int cchannels = 0;
     int lsource_audio_channels = settings.channels;
     bool lneeds_upmix = false;
+    bool laudio_reenc = false;
 
-    if (settings.codec)
-    {
-        lcodec_id = ((AVCodecContext*)settings.codec)->codec_id;
-        settings.bits = 16;
-        settings.channels = 2;
-        lsource_audio_channels = settings.channels;
-        settings.samplerate = 48000;
-        lcchannels = ((AVCodecContext*)settings.codec)->channels;
-    }
+    if (!isAC3upmix) {
+        if (settings.codec)
+        {
+            lcodec_id = ((AVCodecContext*)settings.codec)->codec_id;
+            settings.bits = 16;
+            settings.channels = 2;
+            lsource_audio_channels = settings.channels;
+            settings.samplerate = 48000;
+            lcchannels = ((AVCodecContext*)settings.codec)->channels;
+        }
 
-    if (audio_codec)
-    {
-        codec_id = audio_codec->codec_id;
-        cchannels = ((AVCodecContext*)audio_codec)->channels;
-    }
+        if (audio_codec)
+        {
+            codec_id = audio_codec->codec_id;
+            cchannels = ((AVCodecContext*)audio_codec)->channels;
+        }
 
-    if ((configured_audio_channels == 6) &&
-        !(settings.codec || audio_codec))
-    {
-        settings.channels = configured_audio_channels;
-        lneeds_upmix = true;
-        VERBOSE(VB_AUDIO,LOC + "Needs upmix");
+        if ((configured_audio_channels == 6) &&
+            !(settings.codec || audio_codec))
+        {
+            settings.channels = configured_audio_channels;
+            lneeds_upmix = true;
+            VERBOSE(VB_AUDIO,LOC + "Needs upmix");
+        }
+    } else { //AC3 Upmix
+        // Are we reencoding a (previously) timestretched bitstream?
+        if (settings.channels > 2 && !settings.use_passthru) 
+            laudio_reenc = true;
+
+        // Enough channels? Upmix if not
+        if (settings.channels < configured_audio_channels && 
+            !settings.use_passthru)
+        {
+            settings.channels = configured_audio_channels;
+            lneeds_upmix = true;
+            VERBOSE(VB_AUDIO,LOC + "Needs upmix");
+        }
     }
 
     ClearError();
@@ -224,7 +268,8 @@
         settings.samplerate == audio_samplerate && !need_resampler &&
         settings.use_passthru == audio_passthru &&
         lneeds_upmix == needs_upmix &&
-        lcodec_id == codec_id && lcchannels == cchannels);
+        (isAC3upmix ? (laudio_reenc == audio_reenc) : (lcodec_id == codec_id && lcchannels == cchannels))
+        );
     bool upmix_deps =
         (lsource_audio_channels == source_audio_channels);
     if (general_deps && upmix_deps)
@@ -255,8 +300,13 @@
     audio_channels = settings.channels;
     source_audio_channels = lsource_audio_channels;
     audio_bits = settings.bits;
-    audio_samplerate = settings.samplerate;
-    audio_codec = (AVCodecContext*)settings.codec;
+    if (!isAC3upmix) {
+        audio_samplerate = settings.samplerate;
+        audio_codec = (AVCodecContext*)settings.codec;
+    } else {
+        source_audio_samplerate = audio_samplerate = settings.samplerate;
+        audio_reenc = laudio_reenc;
+    }
     audio_passthru = settings.use_passthru;
     needs_upmix = lneeds_upmix;
 
@@ -265,8 +315,6 @@
         Error("AudioOutput only supports 8 or 16bit audio.");
         return;
     }
-    audio_bytes_per_sample = audio_channels * audio_bits / 8;
-    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
 
     need_resampler = false;
     killaudio = false;
@@ -275,7 +323,59 @@
     internal_vol = gContext->GetNumSetting("MythControlsVolume", 0);
 
     numlowbuffer = 0;
+    
+    if (!isAC3upmix) {
+    audio_bytes_per_sample = audio_channels * audio_bits / 8;
+    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
+    } else {
+    // Encode to AC-3 if not passing thru, there's more than 2 channels
+    // and we're allowed to passthru AC-3
+    if (!audio_passthru && audio_channels > 2 && allow_ac3_passthru) 
+    {
+        VERBOSE(VB_AUDIO, LOC + "Creating AC-3 Encoder");
+        int srate = src_quality == 0 ? audio_samplerate : 48000;
+        encoder = new AudioOutputDigitalEncoder();
+        if (!encoder->Init(CODEC_ID_AC3, 448000, srate, 
+                           configured_audio_channels, audio_reenc)) 
+        {
+            VERBOSE(VB_AUDIO, LOC + "Can't create AC-3 encoder");
+            delete encoder;
+            encoder = NULL;
+        }
+        
+        audio_enc = true;
+    }        
+    
+    if(audio_passthru || audio_enc)
+        // AC-3 output - soundcard expects a 2ch 48k stream
+	audio_channels = 2;
+    
+    audio_bytes_per_sample = audio_channels * audio_bits / 8;
+    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
 
+    // Always resample to 48k - many cards can't do anything else
+    // and ALSA will do it with linear interpolation (yuk) if we don't anyway
+    if (src_quality != 0 && audio_samplerate != 48000)
+    {
+        int error;
+        audio_samplerate = 48000;
+        VERBOSE(VB_GENERAL, LOC + QString("Using resampler. From: %1 to %2")
+            .arg(settings.samplerate).arg(audio_samplerate));
+        src_ctx = src_new(3-src_quality, audio_channels, &error);
+        if (error)
+        {
+            Error(QString("Error creating resampler, the error was: %1")
+                  .arg(src_strerror(error)) );
+	    src_ctx = NULL;
+            return;
+        }
+        src_data.src_ratio = (double) audio_samplerate / settings.samplerate;
+        src_data.data_in = src_in;
+        src_data.data_out = src_out;
+        src_data.output_frames = 16384*6;
+        need_resampler = true;
+    }
+    }
     VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2(%3) sr %4")
             .arg(audio_main_device).arg(audio_channels)
             .arg(source_audio_channels).arg(audio_samplerate));
@@ -309,6 +409,7 @@
     current_seconds = -1;
     source_bitrate = -1;
 
+    if (!isAC3upmix) {
     // NOTE: this won't do anything as above samplerate vars are set equal
     // Check if we need the resampler
     if (audio_samplerate != settings.samplerate)
@@ -329,7 +430,7 @@
         src_data.output_frames = 16384*6;
         need_resampler = true;
     }
-
+    }
     if (needs_upmix)
     {
         VERBOSE(VB_AUDIO, LOC + QString("create upmixer"));
@@ -341,7 +442,7 @@
         upmixer = new FreeSurround(
             audio_samplerate,
             source == AUDIOOUTPUT_VIDEO,
-            (FreeSurround::SurroundMode)surround_mode);
+            (FreeSurround::SurroundMode)surround_mode,isAC3upmix);
 
         VERBOSE(VB_AUDIO, LOC +
                 QString("create upmixer done with surround mode %1")
@@ -350,57 +451,60 @@
 
     VERBOSE(VB_AUDIO, LOC + QString("Audio Stretch Factor: %1")
             .arg(audio_stretchfactor));
-    VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1")
-            .arg((audio_codec) ?
+    if (!isAC3upmix) {
+        VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1")
+                .arg((audio_codec) ?
                  codec_id_string(audio_codec->codec_id) : "not set"));
 
-    if (redo_stretch)
-    {
-        delete pSoundStretch;
-        pSoundStretch = NULL;
-        SetStretchFactorLocked(audio_stretchfactor);
-    }
-    else
-    {
-        SetStretchFactorLocked(audio_stretchfactor);
-        if (pSoundStretch)
+        if (redo_stretch)
         {
-            // if its passthru then we need to reencode
-            if (audio_codec)
+            delete pSoundStretch;
+            pSoundStretch = NULL;
+            SetStretchFactorLocked(audio_stretchfactor);
+        }
+        else
+        {
+            SetStretchFactorLocked(audio_stretchfactor);
+            if (pSoundStretch)
             {
-                if (!encoder)
+                // if its passthru then we need to reencode
+                if (audio_codec)
                 {
-                    VERBOSE(VB_AUDIO, LOC +
-                            QString("Creating Encoder for codec %1")
-                            .arg(audio_codec->codec_id));
+                    if (!encoder)
+                    {
+                        VERBOSE(VB_AUDIO, LOC +
+                                QString("Creating Encoder for codec %1")
+                                .arg(audio_codec->codec_id));
 
-                    encoder = new AudioOutputDigitalEncoder();
-                    if (!encoder->Init(audio_codec->codec_id,
-                                audio_codec->bit_rate,
-                                audio_codec->sample_rate,
-                                audio_codec->channels
-                                ))
-                    {
-                        // eeks
-                        delete encoder;
-                        encoder = NULL;
-                        VERBOSE(VB_AUDIO, LOC + "Failed to Create Encoder");
+                        encoder = new AudioOutputDigitalEncoder(isAC3upmix);
+                        if (!encoder->Init(audio_codec->codec_id,
+                                    audio_codec->bit_rate,
+                                    audio_codec->sample_rate,
+                                    audio_codec->channels
+                                    ))
+                        {
+                            // eeks
+                            delete encoder;
+                            encoder = NULL;
+                            VERBOSE(VB_AUDIO, LOC + "Failed to Create Encoder");
+                        }
                     }
                 }
+                if (audio_codec && encoder)
+                {
+                    pSoundStretch->setSampleRate(audio_codec->sample_rate);
+                    pSoundStretch->setChannels(audio_codec->channels);
+                }
+                else
+                {
+                    pSoundStretch->setSampleRate(audio_samplerate);
+                    pSoundStretch->setChannels(audio_channels);
+                }
             }
-            if (audio_codec && encoder)
-            {
-                pSoundStretch->setSampleRate(audio_codec->sample_rate);
-                pSoundStretch->setChannels(audio_codec->channels);
-            }
-            else
-            {
-                pSoundStretch->setSampleRate(audio_samplerate);
-                pSoundStretch->setChannels(audio_channels);
-            }
         }
+    } else {
+        SetStretchFactorLocked(old_audio_stretchfactor);
     }
-
     // Setup visualisations, zero the visualisations buffers
     prepareVisuals();
 
@@ -437,9 +541,15 @@
     killaudio = true;
     StopOutputThread();
 
+    if (isAC3upmix)
+        QMutexLocker lock1(&audio_buflock);
+
     // Close resampler?
-    if (src_ctx)
+    if (src_ctx) {
         src_delete(src_ctx);
+        if (isAC3upmix)
+            src_ctx = NULL;
+    }
     need_resampler = false;
 
     // close sound stretcher
@@ -447,6 +557,10 @@
     {
         delete pSoundStretch;
         pSoundStretch = NULL;
+        if (isAC3upmix) {
+            old_audio_stretchfactor = audio_stretchfactor;
+            audio_stretchfactor = 1.0;
+        }
     }
 
     if (encoder)
@@ -461,6 +575,8 @@
         upmixer = NULL;
     }
     needs_upmix = false;
+    if (isAC3upmix)
+        audio_enc = false;
 
     CloseDevice();
 
@@ -612,10 +728,12 @@
     // include algorithmic latencies
     if (pSoundStretch)
     {
-        // add the effect of any unused but processed samples,
-        // AC3 reencode does this
-        totalbuffer += (int)(pSoundStretch->numSamples() *
-                             audio_bytes_per_sample);
+        if (!isAC3upmix) {
+            // add the effect of any unused but processed samples,
+            // AC3 reencode does this
+            totalbuffer += (int)(pSoundStretch->numSamples() *
+                                 audio_bytes_per_sample);
+        }
         // add the effect of unprocessed samples in time stretch algo
         totalbuffer += (int)((pSoundStretch->numUnprocessedSamples() *
                               audio_bytes_per_sample) / audio_stretchfactor);
@@ -626,6 +744,9 @@
         totalbuffer += upmixer->sampleLatency() * audio_bytes_per_sample;
     }
 
+    if (isAC3upmix && encoder) 
+        totalbuffer += encoder->Buffered();
+
     audiotime = audbuf_timecode - (int)(totalbuffer * 100000.0 /
                                    (audio_bytes_per_sample * effdspstretched));
 
@@ -681,6 +802,9 @@
         return false; // would overflow
     }
 
+    if (isAC3upmix)
+        QMutexLocker lock1(&audio_buflock);
+
     // resample input if necessary
     if (need_resampler && src_ctx)
     {
@@ -725,6 +849,12 @@
     int abps = (encoder) ?
         encoder->audio_bytes_per_sample : audio_bytes_per_sample;
     int len = samples * abps;
+    
+    if (isAC3upmix) {
+        // Give original samples to mythmusic visualisation
+        dispatchVisual((unsigned char *)buffer, len, timecode,
+                       source_audio_channels, audio_bits);
+    }
 
     // Check we have enough space to write the data
     if (need_resampler && src_ctx)
@@ -749,6 +879,9 @@
         return false; // would overflow
     }
 
+    if (isAC3upmix)
+        QMutexLocker lock1(&audio_buflock);
+
     // resample input if necessary
     if (need_resampler && src_ctx)
     {
@@ -808,10 +941,14 @@
             if (src_ctx)
             {
                 int error = src_reset(src_ctx);
-                if (error)
+                if (error) 
+                {
                     VERBOSE(VB_IMPORTANT, LOC_ERR + QString(
                             "Error occured while resetting resampler: %1")
                             .arg(src_strerror(error)));
+                    if (isAC3upmix)
+                        src_ctx = NULL;
+                }
             }
         }
     }
@@ -821,7 +958,8 @@
 void AudioOutputBase::_AddSamples(void *buffer, bool interleaved, int samples,
                                   long long timecode)
 {
-    audio_buflock.lock();
+    if (!isAC3upmix)
+        audio_buflock.lock();
 
     int len; // = samples * audio_bytes_per_sample;
     int audio_bytes = audio_bits / 8;
@@ -839,17 +977,24 @@
             .arg(samples * abps)
             .arg(kAudioRingBufferSize-afree).arg(afree).arg(timecode)
             .arg(needs_upmix));
-
+    
+    if (isAC3upmix)
+        len = WaitForFreeSpace(samples);
+        
     if (upmixer && needs_upmix)
     {
         int out_samples = 0;
         int step = (interleaved)?source_audio_channels:1;
-        len = WaitForFreeSpace(samples);    // test
+	
+        if (!isAC3upmix) {
+            len = WaitForFreeSpace(samples);
+        }
         for (int itemp = 0; itemp < samples; )
         {
             // just in case it does a processing cycle, release the lock
             // to allow the output loop to do output
-            audio_buflock.unlock();
+            if (!isAC3upmix)
+                audio_buflock.unlock();
             if (audio_bytes == 2)
             {
                 itemp += upmixer->putSamples(
@@ -866,7 +1011,8 @@
                     source_audio_channels,
                     (interleaved) ? 0 : samples);
             }
-            audio_buflock.lock();
+            if (!isAC3upmix)
+                audio_buflock.lock();
 
             int copy_samples = upmixer->numSamples();
             if (copy_samples)
@@ -900,8 +1046,8 @@
     }
     else
     {
-        len = WaitForFreeSpace(samples);
-
+        if (!isAC3upmix)
+            len = WaitForFreeSpace(samples);
         if (interleaved)
         {
             char *mybuf = (char*)buffer;
@@ -936,6 +1082,7 @@
         }
     }
 
+    if (!isAC3upmix) {
     if (samples > 0)
     {
         if (pSoundStretch)
@@ -993,7 +1140,7 @@
                         continue;
 
                     //len = WaitForFreeSpace(amount);
-                    char *ob = encoder->GetOutBuff();
+                    const char *ob = encoder->GetOutBuff();
                     if (amount >= bdiff)
                     {
                         memcpy(audiobuffer + org_waud, ob, bdiff);
@@ -1068,6 +1215,105 @@
     }
 
     audio_buflock.unlock();
+    } else {
+    if (samples <= 0)
+        return;
+        
+    if (pSoundStretch)
+    {
+        // does not change the timecode, only the number of samples
+        // back to orig pos
+        org_waud = waud;
+        int bdiff = kAudioRingBufferSize - org_waud;
+        int nSamplesToEnd = bdiff/abps;
+        if (bdiff < len)
+        {
+            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)
+                                      (audiobuffer +
+                                       org_waud), nSamplesToEnd);
+            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)audiobuffer,
+                                      (len - bdiff) / abps);
+        }
+        else
+            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)
+                                      (audiobuffer + org_waud),
+                                      len / abps);
+
+        int nSamples = pSoundStretch->numSamples();
+        len = WaitForFreeSpace(nSamples); 
+        
+        while ((nSamples = pSoundStretch->numSamples())) 
+        {
+            if (nSamples > nSamplesToEnd) 
+                nSamples = nSamplesToEnd;
+            
+            nSamples = pSoundStretch->receiveSamples(
+                (soundtouch::SAMPLETYPE*)
+                (audiobuffer + org_waud), nSamples
+            );
+            
+            if (nSamples == nSamplesToEnd) {
+                org_waud = 0;
+                nSamplesToEnd = kAudioRingBufferSize/abps;
+            }
+            else {
+                org_waud += nSamples * abps;
+                nSamplesToEnd -= nSamples;
+            }
+            
+        }
+        
+    }
+
+    // Encode to AC-3? 
+    if (encoder) 
+    {
+        
+        org_waud = waud;
+        int bdiff = kAudioRingBufferSize - org_waud;
+        int to_get = 0;
+
+        if (bdiff < len) 
+        {
+            encoder->EncodeUpmix(audiobuffer + org_waud, bdiff);
+            to_get = encoder->EncodeUpmix(audiobuffer, len - bdiff);
+        }
+        else 
+            to_get = encoder->EncodeUpmix(audiobuffer + org_waud, len);
+        
+        if (to_get > 0) 
+        {
+            
+            if (to_get >= bdiff)
+            {
+                encoder->GetFrames(audiobuffer + org_waud, bdiff);
+                to_get -= bdiff;
+                org_waud = 0;
+            }
+            if (to_get > 0)
+                encoder->GetFrames(audiobuffer + org_waud, to_get);
+
+            org_waud += to_get;
+
+        }
+
+    }
+
+    waud = org_waud;
+    lastaudiolen = audiolen(false);
+
+    if (timecode < 0)
+        // mythmusic doesn't give timestamps..
+        timecode = (int)((samples_buffered * 100000.0) / effdsp);
+
+    samples_buffered += samples;
+
+    /* we want the time at the end -- but the file format stores
+       time at the start of the chunk. */
+    // even with timestretch, timecode is still calculated from original
+    // sample count
+    audbuf_timecode = timecode + (int)((samples * 100000.0) / effdsp);
+    } // End AC3 upmiz
 }
 
 void AudioOutputBase::Status()
Index: trunk/mythtv/libs/libmyth/audiooutput.cpp
===================================================================
--- trunk/mythtv/libs/libmyth/audiooutput.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiooutput.cpp	(working copy)
@@ -35,12 +35,12 @@
     const QString &passthru_device,
     int audio_bits, int audio_channels, int audio_samplerate,
     AudioOutputSource source,
-    bool set_initial_vol, bool audio_passthru)
+    bool set_initial_vol, bool audio_passthru, bool AC3upmix)
 {
     AudioSettings settings(
         main_device, passthru_device, audio_bits,
         audio_channels, audio_samplerate, source,
-        set_initial_vol, audio_passthru);
+        set_initial_vol, audio_passthru, NULL, AC3upmix);
 
     settings.FixPassThrough();
 
Index: trunk/mythtv/libs/libmyth/audiosettings.h
===================================================================
--- trunk/mythtv/libs/libmyth/audiosettings.h	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiosettings.h	(working copy)
@@ -33,13 +33,15 @@
         AudioOutputSource audio_source,
         bool              audio_set_initial_vol,
         bool              audio_use_passthru,
-        void             *audio_codec = NULL);
+        void             *audio_codec = NULL,
+	bool              AC3upmix = false);
 
     AudioSettings(int   audio_bits, 
                   int   audio_channels, 
                   int   audio_samplerate,
                   bool  audio_use_passthru,
-                  void *audio_codec = NULL);
+                  void *audio_codec = NULL,
+                  bool  AC3upmix = false);
 
     void FixPassThrough(void);
     void TrimDeviceType(void);
@@ -59,6 +61,7 @@
     bool    use_passthru;
     void   *codec;
     AudioOutputSource source;
+    bool    isAC3upmix;
 };
 
 #endif // _AUDIO_SETTINGS_H_
Index: trunk/mythtv/libs/libmyth/audiooutputdigitalencoder.cpp
===================================================================
--- trunk/mythtv/libs/libmyth/audiooutputdigitalencoder.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiooutputdigitalencoder.cpp	(working copy)
@@ -29,13 +29,27 @@
 AudioOutputDigitalEncoder::AudioOutputDigitalEncoder(void) :
     audio_bytes_per_sample(0),
     av_context(NULL),
-    outbuf(NULL),
-    outbuf_size(0),
     frame_buffer(NULL),
-    one_frame_bytes(0)
+    one_frame_bytes(0),
+    outbuflen(0),
+    inbuflen(0),
+    reorder(true),
+    isAC3upmix(false)
 {
 }
 
+AudioOutputDigitalEncoder::AudioOutputDigitalEncoder(bool AC3upmix) :
+    audio_bytes_per_sample(0),
+    av_context(NULL),
+    frame_buffer(NULL),
+    one_frame_bytes(0),
+    outbuflen(0),
+    inbuflen(0),
+    reorder(true),
+    isAC3upmix(AC3upmix)
+{
+}
+
 AudioOutputDigitalEncoder::~AudioOutputDigitalEncoder()
 {
     Dispose();
@@ -50,15 +64,8 @@
         av_context = NULL;
     }
 
-    if (outbuf)
+    if (!isAC3upmix && frame_buffer)
     {
-        delete [] outbuf;
-        outbuf = NULL;
-        outbuf_size = 0;
-    }
-
-    if (frame_buffer)
-    {
         delete [] frame_buffer;
         frame_buffer = NULL;
         one_frame_bytes = 0;
@@ -67,17 +74,25 @@
 
 //CODEC_ID_AC3
 bool AudioOutputDigitalEncoder::Init(
-    CodecID codec_id, int bitrate, int samplerate, int channels)
+    CodecID codec_id, int bitrate, int samplerate, int channels, bool reencoding)
 {
     AVCodec *codec;
     int ret;
 
-    VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4")
+    VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4 re=%5")
             .arg(codec_id_string(codec_id))
             .arg(bitrate)
             .arg(samplerate)
-            .arg(channels));
+            .arg(channels)
+            .arg(reencoding));
+    
+    reorder = !reencoding;
 
+    // We need to do this when called from mythmusic
+    if (isAC3upmix) {
+        avcodec_init();
+        avcodec_register_all();
+    }
     //codec = avcodec_find_encoder(codec_id);
     // always AC3 as there is no DTS encoder at the moment 2005/1/9
     codec = avcodec_find_encoder(CODEC_ID_AC3);
@@ -107,8 +122,6 @@
     audio_bytes_per_sample = bytes_per_frame;
     one_frame_bytes = bytes_per_frame * av_context->frame_size;
 
-    outbuf_size = 16384;    // ok for AC3 but DTS?
-    outbuf = new char [outbuf_size];
     VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init fs=%1, bpf=%2 ofb=%3")
             .arg(av_context->frame_size)
             .arg(bytes_per_frame)
@@ -253,13 +266,27 @@
 
 } AESHeader;
 
+void reorder_6ch_ac3(void *buf, unsigned int len) {
+    unsigned short *src = (unsigned short *)buf;
+    unsigned short tmp;
+    unsigned int samples = len >> 1;
+
+    for (uint i = 0; i < samples; i += 6) {
+        tmp = src[i+4];
+        src[i+4] = src[i+3];
+        src[i+3] = src[i+2];
+        src[i+2] = src[i+1];
+        src[i+1] = tmp;
+    }
+}
+
 static int encode_frame(
         bool dts, 
         unsigned char *data,
-        size_t &len)
+        size_t &len, bool AC3upmix)
 {
     unsigned char *payload = data + 8;  // skip header, currently 52 or 54bits
-    size_t         enc_len;
+    size_t enc_len = len;
     int            flags, sample_rate, bit_rate;
 
     // we don't do any length/crc validation of the AC3 frame here; presumably
@@ -270,7 +297,8 @@
     // ignore, and if so, may as well just assume that it will ignore
     // anything with a bad CRC...
 
-    uint nr_samples = 0, block_len;
+    uint nr_samples = 0, block_len = 0;
+    
     if (dts)
     {
         enc_len = dts_syncinfo(payload, &flags, &sample_rate, &bit_rate);
@@ -305,7 +333,7 @@
 #endif
     }
 
-    if (enc_len == 0 || enc_len > len)
+    if (!AC3upmix && (enc_len == 0 || enc_len > len))
     {
         int l = len;
         len = 0;
@@ -364,7 +392,8 @@
     data[6] = (enc_len << 3) & 0xFF;
     data[7] = (enc_len >> 5) & 0xFF;
     memset(payload + enc_len, 0, block_len - 8 - enc_len);
-    len = block_len;
+    if (!AC3upmix)
+        len = block_len;
 
     return enc_len;
 }
@@ -377,14 +406,14 @@
  
     // put data in the correct spot for encode frame
     outsize = avcodec_encode_audio(
-        av_context, ((uchar*)outbuf) + 8, outbuf_size - 8, buff);
+        av_context, ((uchar*)outbuf) + 8, OUTBUFSIZE - 8, buff);
 
     size_t tmpsize = outsize;
 
     outsize = MAX_AC3_FRAME_SIZE;
     encsize = encode_frame(
         /*av_context->codec_id==CODEC_ID_DTS*/ false,
-        (unsigned char*)outbuf, outsize);
+        (unsigned char*)outbuf, outsize, false);
 
     VERBOSE(VB_AUDIO+VB_TIMESTAMP, 
             QString("DigitalEncoder::Encode len1=%1 len2=%2 finallen=%3")
@@ -392,3 +421,42 @@
 
     return outsize;
 }
+
+size_t AudioOutputDigitalEncoder::EncodeUpmix(void *buf, int len)
+{
+    size_t outsize = 0;
+ 
+    int fs = FrameSize();
+    memcpy(inbuf+inbuflen, buf, len);
+    inbuflen += len;
+    int frames = inbuflen / fs;
+
+    while (frames--) 
+    {
+        if (reorder)
+            reorder_6ch_ac3(inbuf, fs);
+        
+	// put data in the correct spot for encode frame
+        outsize = avcodec_encode_audio(
+            av_context, ((uchar*)outbuf) + outbuflen + 8, OUTBUFSIZE - 8, (short int *)inbuf);
+        
+        encode_frame(
+            /*av_context->codec_id==CODEC_ID_DTS*/ false,
+            (unsigned char*)outbuf + outbuflen, outsize, true
+        );
+
+        outbuflen += MAX_AC3_FRAME_SIZE;
+        inbuflen -= fs;
+        memmove(inbuf, inbuf+fs, inbuflen);
+    }
+  
+    return outbuflen;
+}
+
+void AudioOutputDigitalEncoder::GetFrames(void *ptr, int maxlen)
+{
+    int len = (maxlen < outbuflen ? maxlen : outbuflen);
+    memcpy(ptr, outbuf, len);
+    outbuflen -= len;
+    memmove(outbuf, outbuf+len, outbuflen);
+}
Index: trunk/mythtv/libs/libmyth/audiooutput.h
===================================================================
--- trunk/mythtv/libs/libmyth/audiooutput.h	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiooutput.h	(working copy)
@@ -17,10 +17,11 @@
         const QString &passthrudevice,
         int audio_bits, int audio_channels, int audio_samplerate,
         AudioOutputSource source,
-        bool set_initial_vol, bool audio_passthru);
+        bool set_initial_vol, bool audio_passthru, bool AC3upmix=false);
 
     AudioOutput() :
         VolumeBase(),             OutputListeners(),
+        isAC3upmix(false),
         lastError(QString::null), lastWarn(QString::null) {}
 
     virtual ~AudioOutput() { };
@@ -69,6 +70,10 @@
     virtual void bufferOutputData(bool y) = 0;
     virtual int readOutputData(unsigned char *read_buffer, int max_length) = 0;
 
+    // Audio AC3 Upmixer
+    virtual bool ToggleUpmix(void) = 0;
+    bool isAC3upmix;
+
   protected:
     void Error(const QString &msg);
     void Warn(const QString &msg);
Index: trunk/mythtv/libs/libmyth/audiooutputdigitalencoder.h
===================================================================
--- trunk/mythtv/libs/libmyth/audiooutputdigitalencoder.h	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiooutputdigitalencoder.h	(working copy)
@@ -5,29 +5,43 @@
 #include "libavcodec/avcodec.h"
 };
 
+#define INBUFSIZE 131072
+#define OUTBUFSIZE 98304
+
 class AudioOutputDigitalEncoder
 {
   public:
     AudioOutputDigitalEncoder(void);
+    AudioOutputDigitalEncoder(bool AC3upmix);
     ~AudioOutputDigitalEncoder();
 
-    bool   Init(CodecID codec_id, int bitrate, int samplerate, int channels);
+    bool   Init(CodecID codec_id, int bitrate, int samplerate, 
+                int channels, bool reencoding = false);
     void   Dispose(void);
     size_t Encode(short * buff);
 
     inline char *GetFrameBuffer(void);
     size_t FrameSize(void)  const { return one_frame_bytes; }
-    char  *GetOutBuff(void) const { return outbuf;          }
+    const char  *GetOutBuff(void) const { return outbuf; }
+    //AC3 upmix
+    size_t EncodeUpmix(void *buf, int len);
+    void   GetFrames(void *ptr, int maxlen);
+    int    Buffered(void) const { return inbuflen; }
 
   public:
     size_t audio_bytes_per_sample;
 
   private:
     AVCodecContext *av_context;
-    char           *outbuf;
-    int             outbuf_size;
+    char            outbuf[OUTBUFSIZE];
     char           *frame_buffer;
     size_t          one_frame_bytes;
+    // AC3 upmix
+    char            inbuf[INBUFSIZE];
+    int             outbuflen;
+    int             inbuflen;
+    bool            reorder;
+    bool            isAC3upmix;
 };
 
 inline char *AudioOutputDigitalEncoder::GetFrameBuffer(void)
Index: trunk/mythtv/libs/libmyth/audiooutputalsa.h
===================================================================
--- trunk/mythtv/libs/libmyth/audiooutputalsa.h	(revision 20600)
+++ trunk/mythtv/libs/libmyth/audiooutputalsa.h	(working copy)
@@ -67,6 +67,7 @@
     virtual int  GetBufferedOnSoundcard(void) const;
 
   private:
+    void SetIECStatus(bool audio);
     inline int SetParameters(snd_pcm_t *handle,
                              snd_pcm_format_t format, unsigned int channels,
                              unsigned int rate, unsigned int buffer_time,
@@ -79,6 +80,7 @@
     void CloseMixer(void);
     void SetupMixer(void);
     ALSAVolumeInfo GetVolumeRange(snd_mixer_elem_t *elem) const;
+    void reorder_6ch_ac3(void *buf, unsigned int len);
 
   private:
     snd_pcm_t   *pcm_handle;
Index: trunk/mythtv/libs/libmythfreesurround/el_processor.cpp
===================================================================
--- trunk/mythtv/libs/libmythfreesurround/el_processor.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmythfreesurround/el_processor.cpp	(working copy)
@@ -40,6 +40,7 @@
 
 const float PI = 3.141592654;
 const float epsilon = 0.000001;
+const float center_level_upmix = 0.5*sqrt(0.5);
 //const float center_level = 0.5*sqrt(0.5);   // gain of the center channel
 //const float center_level = sqrt(0.5);   // gain of the center channel
 const float center_level = 1.0;   // gain of the center channel
@@ -57,7 +58,7 @@
 public:
     // create an instance of the decoder
     //  blocksize is fixed over the lifetime of this object for performance reasons
-    decoder_impl(unsigned blocksize=8192): N(blocksize), halfN(blocksize/2) {
+  decoder_impl(unsigned blocksize=8192, bool AC3upmix=false): N(blocksize), halfN(blocksize/2), isAC3upmix(AC3upmix) {
 #ifdef USE_FFTW3
         // create FFTW buffers
         lt = (float*)fftwf_malloc(sizeof(float)*N);
@@ -99,18 +100,26 @@
             filter[c].resize(N);
         }
         // DC component of filters is always 0
-        for (unsigned c=0;c<5;c++)
-        {
-            filter[c][0] = 0.0;
-            filter[c][1] = 0.0;
-            filter[c][halfN] = 0.0;
+        if (!isAC3upmix) {
+            for (unsigned c=0;c<5;c++)
+            {
+                filter[c][0] = 0.0;
+                filter[c][1] = 0.0;
+                filter[c][halfN] = 0.0;
+            }
+            sample_rate(48000);
+            // generate the window function (square root of hann, b/c it is applied before and after the transform)
+            wnd.resize(N);
+            // dft normalization included in the window for zero cost scaling
+            // also add a gain factor of *2 due to processing gain in algo (see center_level)
+            surround_gain(1.0);
+        } else {
+            sample_rate(48000);
+            // generate the window function (square root of hann, b/c it is applied before and after the transform)
+            wnd.resize(N);
+            for (unsigned k=0;k<N;k++)
+                wnd[k] = sqrt(0.5*(1-cos(2*PI*k/N))/N);
         }
-        sample_rate(48000);
-        // generate the window function (square root of hann, b/c it is applied before and after the transform)
-        wnd.resize(N);
-        // dft normalization included in the window for zero cost scaling
-        // also add a gain factor of *2 due to processing gain in algo (see center_level)
-        surround_gain(1.0);
         current_buf = 0;
         // set the default coefficients
         surround_coefficients(0.8165,0.5774);
@@ -192,12 +201,23 @@
     // set lfe filter params
     void sample_rate(unsigned int srate) {
         // lfe filter is just straight through band limited
-        unsigned int cutoff = (250*N)/srate;
-        for (unsigned f=0;f<=halfN;f++) {           
-            if ((f>=2) && (f<cutoff))
-                filter[5][f] = 1.0;
-            else
-                filter[5][f] = 0.0;
+        unsigned int cutoff;
+        if (!isAC3upmix) {
+             cutoff = (250*N)/srate;
+            for (unsigned f=0;f<=halfN;f++) {           
+                if ((f>=2) && (f<cutoff))
+                    filter[5][f] = 1.0;
+                else
+                    filter[5][f] = 0.0;
+            }
+        } else {
+            cutoff = (30*N)/srate;
+            for (unsigned f=0;f<=halfN;f++) {           
+                if (f<cutoff)
+                    filter[5][f] = 0.5*sqrt(0.5);
+                else
+                    filter[5][f] = 0.0;
+            }
         }
     }
 
@@ -237,6 +257,7 @@
     }
 
 private:
+    bool isAC3upmix;
     // polar <-> cartesian coodinates conversion
     static inline float amplitude(const float cf[2]) { return sqrt(cf[0]*cf[0] + cf[1]*cf[1]); }
     static inline float phase(const float cf[2]) { return atan2(cf[1],cf[0]); }
@@ -290,7 +311,8 @@
 
         // 2. compare amplitude and phase of each DFT bin and produce the X/Y coordinates in the sound field
         //    but dont do DC or N/2 component
-        for (unsigned f=2;f<halfN;f++) {           
+        unsigned start_f = isAC3upmix ? 0:2;
+        for (unsigned f=start_f;f<halfN;f++) {           
             // get left/right amplitudes/phases
             float ampL = amplitude(dftL[f]), ampR = amplitude(dftR[f]);
             float phaseL = phase(dftL[f]), phaseR = phase(dftR[f]);
@@ -357,7 +379,7 @@
                 float front = (1+yfs[f])/2, back = (1-yfs[f])/2;
                 float volume[5] = {
                     front * (left * center_width + max(0,-xfs[f]) * (1-center_width)),  // left
-                    front * center_level*((1-abs(xfs[f])) * (1-center_width)),          // center
+                    front * (isAC3upmix ? center_level_upmix : center_level) *((1-abs(xfs[f])) * (1-center_width)),          // center
                     front * (right * center_width + max(0, xfs[f]) * (1-center_width)), // right
                     back * surround_level * left,                                       // left surround
                     back * surround_level * right                                       // right surround
@@ -402,7 +424,7 @@
                 float front = (1+yfs[f])/2, back = (1-yfs[f])/2;
                 float volume[5] = {
                     front * (left * center_width + max(0,-xfs[f]) * (1-center_width)),      // left
-                    front * center_level*((1-abs(xfs[f])) * (1-center_width)),              // center
+                    front * (isAC3upmix ? center_level_upmix : center_level) *((1-abs(xfs[f])) * (1-center_width)),          // center
                     front * (right * center_width + max(0, xfs[f]) * (1-center_width)),     // right
                     back * surround_level*max(0,min(1,((1-(xfs[f]/surround_balance))/2))),  // left surround
                     back * surround_level*max(0,min(1,((1+(xfs[f]/surround_balance))/2)))   // right surround
@@ -613,7 +635,7 @@
 
 // implementation of the shell class
 
-fsurround_decoder::fsurround_decoder(unsigned blocksize): impl(new decoder_impl(blocksize)) { }
+fsurround_decoder::fsurround_decoder(unsigned blocksize, bool AC3upmix): impl(new decoder_impl(blocksize,AC3upmix)) { }
 
 fsurround_decoder::~fsurround_decoder() { delete impl; }
 
Index: trunk/mythtv/libs/libmythfreesurround/freesurround.cpp
===================================================================
--- trunk/mythtv/libs/libmythfreesurround/freesurround.cpp	(revision 20600)
+++ trunk/mythtv/libs/libmythfreesurround/freesurround.cpp	(working copy)
@@ -63,7 +63,7 @@
 const unsigned default_block_size = 8192;
 // there will be a slider for this in the future
 //const float master_gain = 1.0;
-//#define MASTER_GAIN * master_gain
+//#define MASTER_GAIN * master_gain 
 #define MASTER_GAIN
 //const float master_gain = 1.0/(1<<15);
 //const float inv_master_gain = (1<<15);
@@ -163,10 +163,12 @@
 
 // construction methods
 void *new_decoder() { return new fsurround_decoder(block_size); }
+void *new_decoder_upmix() { return new fsurround_decoder(block_size,true); }
 void *new_buffers() { return new buffers(block_size/2); }
 void *new_int16buffers() { return new int16buffers(block_size/2); }
 
 object_pool dp(&new_decoder);
+object_pool dp_upmix(&new_decoder_upmix);
 //object_pool bp(&new_buffers);
 object_pool bp16(&new_int16buffers);
 
@@ -175,7 +177,7 @@
 int channel_select = -1;
 #endif
 
-FreeSurround::FreeSurround(uint srate, bool moviemode, SurroundMode smode) :
+FreeSurround::FreeSurround(uint srate, bool moviemode, SurroundMode smode, bool AC3upmix) :
         srate(srate),
         open_(false),
         initialized_(false),
@@ -186,21 +188,32 @@
         out_count(0),
         processed(true),
         processed_size(0),
-        surround_mode(smode)
+        surround_mode(smode),
+        isAC3upmix(AC3upmix)
 {
     VERBOSE(QString("FreeSurround::FreeSurround rate %1 moviemode %2").arg(srate).arg(moviemode));
     if (moviemode)
     {
         params.phasemode = 1;
-        params.center_width = 0;
-        params.gain = 1.0;
+        if (!isAC3upmix) {
+            params.center_width = 0;
+            params.gain = 1.0;
+        } else {
+            params.center_width = 25;
+            params.dimension = 0.5;
+        }
     }
     else
     {
-        params.center_width = 70;
-        // for 50, gain should be about 1.9, c/lr about 2.7
-        // for 70, gain should be about 3.1, c/lr about 1.5
-        params.gain = 3.1;
+        if (!isAC3upmix) {
+            params.center_width = 70;
+            // for 50, gain should be about 1.9, c/lr about 2.7
+            // for 70, gain should be about 3.1, c/lr about 1.5
+            params.gain = 3.1;
+        } else {
+            params.center_width = 65;
+            params.dimension = 0.3;
+        }
     }
     switch (surround_mode)
     {
@@ -236,7 +249,8 @@
         decoder->phase_mode(params.phasemode);
         decoder->surround_coefficients(params.coeff_a, params.coeff_b);				
         decoder->separation(params.front_sep/100.0,params.rear_sep/100.0);
-        decoder->gain(params.gain);
+        if (!isAC3upmix)
+            decoder->gain(params.gain);
     }
 }
 
@@ -655,16 +669,6 @@
     {
         if (decoder) 
         {
-            // actually these params need only be set when they change... but it doesn't hurt
-#if 0
-            decoder->steering_mode(params.steering);
-            decoder->phase_mode(params.phasemode);
-            decoder->surround_coefficients(params.coeff_a, params.coeff_b);				
-            decoder->separation(params.front_sep/100.0,params.rear_sep/100.0);
-#endif
-            // decode the bufs->block
-            //decoder->decode(input,output,params.center_width/100.0,params.dimension/100.0);
-            //decoder->decode(output,params.center_width/100.0,params.dimension/100.0);
             decoder->decode(params.center_width/100.0,params.dimension/100.0);
         }
     }
@@ -694,7 +698,10 @@
 {		
     if (!decoder)
     {
-        decoder = (fsurround_decoder*)dp.acquire((void*)1);
+        if (!isAC3upmix)
+            decoder = (fsurround_decoder*)dp.acquire((void*)1);
+        else
+	    decoder = (fsurround_decoder*)dp_upmix.acquire((void*)1);
         decoder->flush();
         //if (bufs)
         //    bufs->clear();
@@ -709,7 +716,10 @@
 {
     if (decoder)
     {
-        dp.release(this);
+        if (!isAC3upmix)
+            dp.release(this);
+	else
+            dp_upmix.release(this);
         decoder = 0;
     }
 }
Index: trunk/mythtv/libs/libmythfreesurround/el_processor.h
===================================================================
--- trunk/mythtv/libs/libmythfreesurround/el_processor.h	(revision 20600)
+++ trunk/mythtv/libs/libmythfreesurround/el_processor.h	(working copy)
@@ -24,7 +24,7 @@
 public:
 	// create an instance of the decoder
 	//  blocksize is fixed over the lifetime of this object for performance reasons
-	fsurround_decoder(unsigned blocksize=8192);
+        fsurround_decoder(unsigned blocksize=8192, bool AC3upmix=false);
 	// destructor
 	~fsurround_decoder();
 	
@@ -51,10 +51,10 @@
 	void gain(float gain);
 
 	// set the phase shifting mode for decoding
-	// 0 = (+0°,+0°)   - music mode
-	// 1 = (+0°,+180°) - PowerDVD compatibility
-	// 2 = (+180°,+0°) - BeSweet compatibility
-	// 3 = (-90°,+90°) - This seems to work. I just don't know why.
+	// 0 = (+0οΎ°,+0οΎ°)   - music mode
+	// 1 = (+0οΎ°,+180οΎ°) - PowerDVD compatibility
+	// 2 = (+180οΎ°,+0οΎ°) - BeSweet compatibility
+	// 3 = (-90οΎ°,+90οΎ°) - This seems to work. I just don't know why.
 	void phase_mode(unsigned mode);
 
 	// override the steering mode
@@ -71,6 +71,7 @@
 
 private:
 	class decoder_impl *impl; // private implementation (details hidden)
+	bool isAC3upmix;
 };
 
 
Index: trunk/mythtv/libs/libmythfreesurround/freesurround.h
===================================================================
--- trunk/mythtv/libs/libmythfreesurround/freesurround.h	(revision 20600)
+++ trunk/mythtv/libs/libmythfreesurround/freesurround.h	(working copy)
@@ -31,7 +31,7 @@
         SurroundModeActiveLinear
     } SurroundMode;
 public:
-    FreeSurround(uint srate, bool moviemode, SurroundMode mode);
+    FreeSurround(uint srate, bool moviemode, SurroundMode mode, bool AC3upmix=false);
     ~FreeSurround();
 
     // put samples in buffer, returns number of samples used
@@ -88,6 +88,7 @@
     bool processed;             // whether processing is enabled or not for latency calc
     int processed_size;         // amount processed
     SurroundMode surround_mode; // 1 of 3 surround modes supported
+    bool isAC3upmix;
 
 };
 
Index: trunk/mythtv/programs/mythfrontend/globalsettings.cpp
===================================================================
--- trunk/mythtv/programs/mythfrontend/globalsettings.cpp	(revision 20600)
+++ trunk/mythtv/programs/mythfrontend/globalsettings.cpp	(working copy)
@@ -119,6 +119,36 @@
     return gc;
 }
 
+static HostCheckBox *AC3Upmix()
+{
+    HostCheckBox *gc = new HostCheckBox("MythAC3Upmix");
+    gc->setLabel(QObject::tr("AC3 audio upmixer"));
+    gc->setValue(false);
+    gc->setHelpText(QObject::tr("Enable AC3 upmixer and digital software volume control."));
+    return gc;
+}
+
+static HostComboBox *SRCQuality()
+{
+    HostComboBox *gc = new HostComboBox("SRCQuality", false);
+    gc->setLabel(QObject::tr("Sample Rate Conversion"));
+    gc->addSelection(QObject::tr("Best"), "3", true); // default
+    gc->addSelection(QObject::tr("Medium"), "2");
+    gc->addSelection(QObject::tr("Fastest"), "1");
+    gc->addSelection(QObject::tr("Disabled"), "0");
+    gc->setHelpText(
+            QObject::tr(
+                "Set the quality of audio sample rate conversion. "
+                "This only affects non 48000Hz PCM audio. "
+                "All three options offer a worst-case SNR of 97dB. "
+                "'Best' at a bandwidth of 97%. "
+                "'Medium' at a bandwidth of 90%. "
+                "'Fastest' at a bandwidth of 80%. "
+                "Set 'Disabled' only if you know what you are doing."));
+    return gc;
+}
+
+
 static HostComboBox *PassThroughOutputDevice()
 {
     HostComboBox *gc = new HostComboBox("PassThruOutputDevice", true);
@@ -3432,11 +3462,11 @@
     return gs;
 }
 
-class AudioSystemSettingsGroup : public VerticalConfigurationGroup
+class AudioSystemSettingsGroup : public TriggeredConfigurationGroup
 {
   public:
     AudioSystemSettingsGroup() :
-        VerticalConfigurationGroup(false, true, false, false)
+        TriggeredConfigurationGroup(false, true, false, false)
     {
         setLabel(QObject::tr("Audio System"));
         setUseLabel(false);
@@ -3451,6 +3481,13 @@
         addChild(AC3PassThrough());
         addChild(DTSPassThrough());
         addChild(AggressiveBuffer());
+
+        Setting* ac3upmix = AC3Upmix();
+        addChild(ac3upmix);
+        setTrigger(ac3upmix);
+        addTarget("1", SRCQuality());
+        addTarget("0", new VerticalConfigurationGroup(false,false));
+
     }
 };
 
Index: trunk/mythtv/programs/mythtranscode/transcode.cpp
===================================================================
--- trunk/mythtv/programs/mythtranscode/transcode.cpp	(revision 20600)
+++ trunk/mythtv/programs/mythtranscode/transcode.cpp	(working copy)
@@ -222,6 +222,11 @@
         // Do nothing
         return kMuteOff;
     }
+    virtual bool ToggleUpmix(void) 
+    {
+        // Do nothing
+        return false;
+    }
 
     //  These are pure virtual in AudioOutput, but we don't need them here
     virtual void bufferOutputData(bool){ return; }
