Ticket #5900: audioencoding-trunk-jya15.patch

File audioencoding-trunk-jya15.patch, 78.0 KB (added by jyavenard@…, 15 years ago)

Patch for trunk (20651)

  • trunk/mythplugins/mythmusic/mythmusic/playbackbox.cpp

     
    367367            changeSpeed(true);
    368368        else if (action == "MUTE")
    369369            toggleMute();
     370        else if (action == "TOGGLEUPMIX")
     371            toggleUpmix();
    370372        else if (action == "MENU" && visualizer_status != 2)
    371373        {
    372374            menufilters = false;
     
    12021204    }
    12031205}
    12041206
     1207void PlaybackBoxMusic::toggleUpmix()
     1208{
     1209    if (gPlayer->getOutput())
     1210        gPlayer->getOutput()->ToggleUpmix();
     1211}
     1212   
     1213
    12051214void PlaybackBoxMusic::showProgressBar()
    12061215{
    12071216    if (progress_bar && visualizer_status != 2)
  • trunk/mythplugins/mythmusic/mythmusic/main.cpp

     
    367367    REG_KEY("Music", "VOLUMEDOWN", "Volume down",       "[,{,F10,Volume Down");
    368368    REG_KEY("Music", "VOLUMEUP",   "Volume up",         "],},F11,Volume Up");
    369369    REG_KEY("Music", "MUTE",       "Mute",              "|,\\,F9,Volume Mute");
     370    REG_KEY("Music", "TOGGLEUPMIX","Toggle upmixer",             "Ctrl+U");
    370371    REG_KEY("Music", "CYCLEVIS",   "Cycle visualizer mode",      "6");
    371372    REG_KEY("Music", "BLANKSCR",   "Blank screen",               "5");
    372373    REG_KEY("Music", "THMBUP",     "Increase rating",            "9");
  • trunk/mythplugins/mythmusic/mythmusic/musicplayer.cpp

     
    348348
    349349void MusicPlayer::openOutputDevice(void)
    350350{
    351     QString adevice;
     351    QString adevice, pdevice;
     352    bool isAC3upmix = gContext->GetNumSetting("MythAC3Upmix", 0);
    352353
    353354    if (gContext->GetSetting("MusicAudioDevice") == "default")
    354355        adevice = gContext->GetSetting("AudioOutputDevice");
    355356    else
    356357        adevice = gContext->GetSetting("MusicAudioDevice");
    357358
     359    if (!isAC3upmix)
     360        pdevice = "default";
     361    else
     362        pdevice = gContext->GetSetting("PassThruOutputDevice");
     363
    358364    // TODO: Error checking that device is opened correctly!
    359     m_output = AudioOutput::OpenAudio(adevice, "default", 16, 2, 44100,
    360                                     AUDIOOUTPUT_MUSIC, true, false);
     365    m_output = AudioOutput::OpenAudio(adevice, pdevice, 16, 2, 44100,
     366                                      AUDIOOUTPUT_MUSIC, true, false, isAC3upmix);
    361367    m_output->setBufferSize(256 * 1024);
    362368    m_output->SetBlocking(false);
    363369
  • trunk/mythplugins/mythmusic/mythmusic/playbackbox.h

     
    6969    void changeVolume(bool up_or_down);
    7070    void changeSpeed(bool up_or_down);
    7171    void toggleMute();
     72    void toggleUpmix();
    7273    void resetTimer();
    7374    void hideVolume(){showVolume(false);}
    7475    void showVolume(bool on_or_off);
  • trunk/mythtv/libs/libmythtv/NuppelVideoPlayer.cpp

     
    868868
    869869    no_audio_in = false;
    870870
     871    bool isAC3upmix = gContext->GetNumSetting("MythAC3Upmix",0);
     872    // If previously AC3 upmixer was different than current state, delete AudioOutput
     873    if (audioOutput && (audioOutput->isAC3upmix != isAC3upmix))
     874    {
     875        delete audioOutput;
     876        audioOutput = NULL;
     877    }
     878
    871879    if (!audioOutput && !using_null_videoout && player_ctx->IsAudioNeeded())
    872880    {
    873881        bool setVolume = gContext->GetNumSetting("MythControlsVolume", 1);
     
    876884                                             audio_bits, audio_channels,
    877885                                             audio_samplerate,
    878886                                             AUDIOOUTPUT_VIDEO,
    879                                              setVolume, audio_passthru);
     887                                             setVolume, audio_passthru, isAC3upmix);
    880888        if (!audioOutput)
    881889            errMsg = QObject::tr("Unable to create AudioOutput.");
    882890        else
     
    906914            audio_bits, audio_channels, audio_samplerate,
    907915            audio_passthru, audio_codec);
    908916        audioOutput->Reconfigure(settings);
     917        if (audioOutput->isAC3upmix && audio_passthru)
     918            audio_channels = 2;
    909919        errMsg = audioOutput->GetError();
    910920        if (!errMsg.isEmpty())
    911921            audioOutput->SetStretchFactor(audio_stretchfactor);
     
    52745284    }
    52755285}
    52765286
     5287bool NuppelVideoPlayer::GetisAC3upmix()
     5288{
     5289    if (audioOutput)
     5290        return audioOutput->isAC3upmix;
     5291    return false;
     5292}
     5293
     5294bool NuppelVideoPlayer::ToggleUpmix()
     5295{
     5296    if (audioOutput)
     5297        return audioOutput->ToggleUpmix();
     5298    return false;
     5299}
     5300
    52775301void NuppelVideoPlayer::Zoom(ZoomDirection direction)
    52785302{
    52795303    if (videoOutput)
  • trunk/mythtv/libs/libmythtv/avformatdecoder.cpp

     
    430430      audioSamples(NULL),
    431431      allow_ac3_passthru(false),    allow_dts_passthru(false),
    432432      disable_passthru(false),      max_channels(2),
    433       dummy_frame(NULL),
     433      last_ac3_channels(0),         dummy_frame(NULL),
    434434      // DVD
    435435      lastdvdtitle(-1),
    436436      decodeStillFrame(false),
     
    451451    allow_ac3_passthru = gContext->GetNumSetting("AC3PassThru", false);
    452452    allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false);
    453453    max_channels = (uint) gContext->GetNumSetting("MaxChannels", 2);
     454    isAC3upmix = gContext->GetNumSetting("MythAC3Upmix", 0);
    454455
    455456    audioIn.sample_size = -32; // force SetupAudioStream to run once
    456457    itv = GetNVP()->GetInteractiveTV();
     
    29752976    {
    29762977        int idx = atracks[i].av_stream_index;
    29772978        AVCodecContext *codec_ctx = ic->streams[idx]->codec;
    2978         bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
    2979                                 !disable_passthru &&
    2980                                 (codec_ctx->codec_id == CODEC_ID_AC3));
    2981         bool do_dts_passthru = (allow_dts_passthru && !transcoding &&
    2982                                 !disable_passthru &&
    2983                                 (codec_ctx->codec_id == CODEC_ID_DTS));
    29842979        AudioInfo item(codec_ctx->codec_id,
    29852980                       codec_ctx->sample_rate, codec_ctx->channels,
    2986                        do_ac3_passthru || do_dts_passthru);
     2981                       DoPassThrough(codec_ctx));
    29872982        VERBOSE(VB_AUDIO, LOC + " * " + item.toString());
    29882983    }
    29892984#endif
     
    31173112bool AvFormatDecoder::GetFrame(int onlyvideo)
    31183113{
    31193114    AVPacket *pkt = NULL;
     3115    AC3HeaderInfo hdr;
    31203116    int len;
    31213117    unsigned char *ptr;
    31223118    int data_size = 0;
     
    33173313        if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
    33183314            pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
    33193315
    3320         if (ringBuffer->isDVD() &&
     3316        if (ringBuffer->isDVD() && 
    33213317            curstream->codec->codec_type == CODEC_TYPE_VIDEO)
    33223318        {
    33233319            MpegPreProcessPkt(curstream, pkt);
     
    35463542                        reselectAudioTrack = true;
    35473543                    }
    35483544
    3549                     bool do_ac3_passthru =
    3550                         (allow_ac3_passthru && !transcoding &&
    3551                          (curstream->codec->codec_id == CODEC_ID_AC3));
    3552                     bool do_dts_passthru =
    3553                         (allow_dts_passthru && !transcoding &&
    3554                          (curstream->codec->codec_id == CODEC_ID_DTS));
    3555                     bool using_passthru = do_ac3_passthru || do_dts_passthru;
    3556 
    35573545                    // detect channels on streams that need
    35583546                    // to be decoded before we can know this
    35593547                    bool already_decoded = false;
     
    35643552                                QString("Setting channels to %1")
    35653553                                .arg(audioOut.channels));
    35663554
    3567                         if (using_passthru)
     3555                        if (DoPassThrough(curstream->codec))
    35683556                        {
    35693557                            // for passthru let it select the max number
    35703558                            // of channels
     
    35863574                        reselectAudioTrack |= curstream->codec->channels;
    35873575                    }
    35883576
     3577                    if (isAC3upmix && curstream->codec->codec_id == CODEC_ID_AC3)
     3578                    {
     3579                        GetBitContext gbc;
     3580                        init_get_bits(&gbc, ptr, len * 8);
     3581                        if (!ff_ac3_parse_header(&gbc, &hdr))
     3582                        {
     3583                            if (hdr.channels != last_ac3_channels)
     3584                            {
     3585                                last_ac3_channels = curstream->codec->channels = hdr.channels;
     3586                                SetupAudioStream();
     3587                            }
     3588                        }
     3589                    }
     3590
    35893591                    if (reselectAudioTrack)
    35903592                    {
    35913593                        QMutexLocker locker(&avcodeclock);
     
    36493651                            curstream->codec->request_channels =
    36503652                                audioOut.channels;
    36513653                            data_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
    3652                             ret = avcodec_decode_audio2(ctx, audioSamples,
     3654                            ret = avcodec_decode_audio2(ctx, audioSamples, 
    36533655                                                        &data_size, ptr, len);
    36543656                        }
    36553657
     
    41284130    }
    41294131}
    41304132
     4133bool AvFormatDecoder::DoPassThrough(const AVCodecContext *ctx)
     4134{
     4135    bool do_ac3_passthru =
     4136        (allow_ac3_passthru && !transcoding &&
     4137        (ctx->codec_id == CODEC_ID_AC3));
     4138    bool do_dts_passthru =
     4139        (allow_dts_passthru && !transcoding &&
     4140        (ctx->codec_id == CODEC_ID_DTS));
     4141    bool using_passthru = do_ac3_passthru || do_dts_passthru;
     4142
     4143    if (!isAC3upmix)
     4144        return using_passthru;
     4145
     4146    bool passthru = false;
     4147
     4148    if (ctx->codec_id == CODEC_ID_AC3)
     4149        passthru = allow_ac3_passthru &&
     4150                   ctx->channels >= (int)max_channels;
     4151    else if (ctx->codec_id == CODEC_ID_DTS)
     4152        passthru = allow_dts_passthru;
     4153   
     4154    passthru &= !transcoding && !disable_passthru;
     4155    // Don't know any cards that support spdif clocked at < 44100
     4156    // Some US cable transmissions have 2ch 32k AC-3 streams
     4157    passthru &= ctx->sample_rate >= 44100;
     4158
     4159    return passthru;
     4160}
     4161
    41314162/** \fn AvFormatDecoder::SetupAudioStream(void)
    41324163 *  \brief Reinitializes audio if it needs to be reinitialized.
    41334164 *
     
    41554186        codec_ctx = curstream->codec;
    41564187        if (codec_ctx)
    41574188        {
    4158             bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
    4159                                     (codec_ctx->codec_id == CODEC_ID_AC3));
    4160             bool do_dts_passthru = (allow_dts_passthru && !transcoding &&
    4161                                     (codec_ctx->codec_id == CODEC_ID_DTS));
    4162             using_passthru = do_ac3_passthru || do_dts_passthru;
    4163             info = AudioInfo(codec_ctx->codec_id,
    4164                              codec_ctx->sample_rate, codec_ctx->channels,
    4165                              using_passthru && !disable_passthru);
     4189            using_passthru = DoPassThrough(codec_ctx);
     4190            if (!isAC3upmix)
     4191                info = AudioInfo(codec_ctx->codec_id,
     4192                                 codec_ctx->sample_rate, codec_ctx->channels,
     4193                                 using_passthru && !disable_passthru);
     4194            else
     4195                info = AudioInfo(codec_ctx->codec_id, codec_ctx->sample_rate,
     4196                                codec_ctx->channels, using_passthru);
    41664197        }
    41674198    }
    41684199
     
    41794210            QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1));
    41804211
    41814212    audioOut = audioIn = info;
     4213
     4214    if (!isAC3upmix) {
    41824215    AudioInfo tmpAudioOut = audioOut;
    41834216
    41844217    // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card
     
    42144247
    42154248    // allow the audio stuff to reencode
    42164249    GetNVP()->SetAudioCodec((using_passthru) ? codec_ctx : NULL);
     4250    } else {
     4251    if (!using_passthru && audioOut.channels > (int)max_channels)
     4252    {
     4253        audioOut.channels = (int)max_channels;
     4254        audioOut.sample_size = audioOut.channels * 2;
     4255        codec_ctx->channels = audioOut.channels;
     4256    }
     4257
     4258    VERBOSE(VB_AUDIO, LOC + "Audio format changed " +
     4259            QString("\n\t\t\tfrom %1 to %2")
     4260            .arg(old_in.toString()).arg(audioOut.toString()));
     4261
     4262    if (audioOut.sample_rate > 0)
     4263        GetNVP()->SetEffDsp(audioOut.sample_rate * 100);
     4264
     4265    GetNVP()->SetAudioCodec(codec_ctx);
     4266    GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels,
     4267                             audioOut.sample_rate, audioOut.do_passthru);
     4268
     4269    }
    42174270    GetNVP()->ReinitAudio();
    42184271
    42194272    return true;
  • trunk/mythtv/libs/libmythtv/tv_play.h

     
    418418        ARBSEEK_FORWARD,
    419419        ARBSEEK_END
    420420    };
     421   
    421422    void DoArbSeek(PlayerContext*, ArbSeekWhence whence);
    422423    void NormalSpeed(PlayerContext*);
    423424    void ChangeSpeed(PlayerContext*, int direction);
     
    426427    bool TimeStretchHandleAction(PlayerContext*,
    427428                                 const QStringList &actions);
    428429
     430    void ToggleUpmix(PlayerContext*);
    429431    void ChangeAudioSync(PlayerContext*, int dir, bool allowEdit = true);
    430432    bool AudioSyncHandleAction(PlayerContext*, const QStringList &actions);
    431433
  • trunk/mythtv/libs/libmythtv/NuppelVideoPlayer.h

     
    182182    // Toggle Sets
    183183    void ToggleAspectOverride(AspectOverrideMode aspectMode = kAspect_Toggle);
    184184    void ToggleAdjustFill(AdjustFillMode adjustfillMode = kAdjustFill_Toggle);
     185    bool GetisAC3upmix(void);
     186    bool ToggleUpmix(void);
    185187
    186188    // Gets
    187189    QSize   GetVideoBufferSize(void) const    { return video_dim; }
  • trunk/mythtv/libs/libmythtv/tv_play.cpp

     
    491491    REG_KEY("TV Playback", "VOLUMEDOWN", "Volume down", "[,{,F10,Volume Down");
    492492    REG_KEY("TV Playback", "VOLUMEUP",   "Volume up",   "],},F11,Volume Up");
    493493    REG_KEY("TV Playback", "MUTE",       "Mute",        "|,\\,F9,Volume Mute");
     494    REG_KEY("TV Playback", "TOGGLEUPMIX", "Toggle upmixer", "Ctrl+U");
    494495    REG_KEY("TV Playback", "TOGGLEPIPMODE", "Toggle Picture-in-Picture view",
    495496            "V");
    496497    REG_KEY("TV Playback", "TOGGLEPBPMODE", "Toggle Picture-by-Picture view",
     
    630631  Teletext     F2,F3,F4,F5,F6,F7,F8
    631632  ITV          F2,F3,F4,F5,F6,F7,F12
    632633
    633   Playback: Ctrl-B,Ctrl-G,Ctrl-Y
     634  Playback: Ctrl-B,Ctrl-G,Ctrl-Y,Ctrl-U
    634635*/
    635636}
    636637
     
    43414342        DoTogglePictureAttribute(ctx, kAdjustingPicture_Playback);
    43424343    else if (has_action("TOGGLESTRETCH", actions))
    43434344        ToggleTimeStretch(ctx);
     4345    else if (has_action("TOGGLEUPMIX", actions))
     4346        ToggleUpmix(ctx);
    43444347    else if (has_action("TOGGLESLEEP", actions))
    43454348        ToggleSleepTimer(ctx);
    43464349    else if (has_action("TOGGLERECORD", actions) && islivetv)
     
    80038006    SetSpeedChangeTimer(0, __LINE__);
    80048007}
    80058008
     8009void TV::ToggleUpmix(PlayerContext *ctx)
     8010{
     8011    if (!ctx->nvp || !ctx->nvp->HasAudioOut())
     8012        return;
     8013    QString text;
     8014    if(!ctx->nvp->GetisAC3upmix()) {
     8015        text = tr("Upmixer disabled !");
     8016    } else {
     8017        if (ctx->nvp->ToggleUpmix())
     8018            text = tr("Upmixer On");
     8019        else
     8020            text = tr("Upmixer Off");
     8021    }
     8022
     8023    if (ctx->nvp->GetOSD() && !browsemode)
     8024        ctx->nvp->GetOSD()->SetSettingsText(text, 5);
     8025}
     8026   
    80068027// dir in 10ms jumps
    80078028void TV::ChangeAudioSync(PlayerContext *ctx, int dir, bool allowEdit)
    80088029{
     
    96379658        SetManualZoom(actx, true, tr("Zoom Mode ON"));
    96389659    else if (action == "TOGGLESTRETCH")
    96399660        ToggleTimeStretch(actx);
     9661    else if (action == "TOGGLEUPMIX")
     9662        ToggleUpmix(actx);
    96409663    else if (action.left(13) == "ADJUSTSTRETCH")
    96419664    {
    96429665        bool floatRead;
     
    999010013
    999110014    if (category == "AUDIOSYNC")
    999210015        new OSDGenericTree(treeMenu, tr("Adjust Audio Sync"), "TOGGLEAUDIOSYNC");
     10016    else if ((category == "TOGGLEUPMIX") && ctx->nvp && ctx->nvp->GetisAC3upmix())
     10017        new OSDGenericTree(treeMenu, tr("Toggle Upmixer"), "TOGGLEUPMIX");
    999310018    else if (category == "TIMESTRETCH")
    999410019        FillMenuTimeStretch(ctx, treeMenu);
    999510020    else if (category == "VIDEOSCAN")
  • trunk/mythtv/libs/libmythtv/tvosdmenuentry.cpp

     
    232232    curMenuEntries.append(
    233233        new TVOSDMenuEntry("AUDIOSYNC",           1, 1, 1, 1 , "Audio Sync"));
    234234    curMenuEntries.append(
     235        new TVOSDMenuEntry("TOGGLEUPMIX",        1, 1, 1, 1, "Toggle Upmixer"));
     236    curMenuEntries.append(
    235237        new TVOSDMenuEntry("TIMESTRETCH",        1, 1, 1, 1, "Time Stretch"));
    236238    curMenuEntries.append(
    237239        new TVOSDMenuEntry("VIDEOSCAN",            1, 1, 1, 1, "Video Scan"));
  • trunk/mythtv/libs/libmythtv/avformatdecoder.h

     
    200200
    201201    void SeekReset(long long, uint skipFrames, bool doFlush, bool discardFrames);
    202202
     203    bool DoPassThrough(const AVCodecContext *ctx);
    203204    bool SetupAudioStream(void);
    204205    void SetupAudioStreamSubIndexes(int streamIndex);
    205206    void RemoveAudioStreams();
     
    272273    bool              allow_dts_passthru;
    273274    bool              disable_passthru;
    274275    uint              max_channels;
     276    uint              last_ac3_channels;
     277    bool              isAC3upmix;
    275278
    276279    VideoFrame       *dummy_frame;
    277280
  • trunk/mythtv/libs/libmythsamplerate/samplerate.c

     
    452452        {       len -- ;
    453453
    454454                scaled_value = in [len] * (8.0 * 0x10000000) ;
    455                 if (CPU_CLIPS_POSITIVE == 0 && scaled_value >= (1.0 * 0x7FFFFFFF))
     455                if (scaled_value >= (1.0 * 0x7FFFFFFF))
    456456                {       out [len] = 32767 ;
    457457                        continue ;
    458458                        } ;
    459                 if (CPU_CLIPS_NEGATIVE == 0 && scaled_value <= (-8.0 * 0x10000000))
     459                if (scaled_value <= (-8.0 * 0x10000000))
    460460                {       out [len] = -32768 ;
    461461                        continue ;
    462462                        } ;
  • trunk/mythtv/libs/libmyth/audiooutputbase.h

     
    4343
    4444    virtual void SetStretchFactor(float factor);
    4545    virtual float GetStretchFactor(void) const;
     46    virtual bool ToggleUpmix(void);
    4647
    4748    virtual void Reset(void);
    4849
     
    132133    QString audio_passthru_device;
    133134
    134135    bool audio_passthru;
     136    bool audio_enc;
     137    bool audio_reenc;
    135138
    136139    float audio_stretchfactor;
    137140    AVCodecContext *audio_codec;
     
    144147    bool buffer_output_data_for_use; //  used by AudioOutputNULL
    145148
    146149    int configured_audio_channels;
     150    int orig_config_channels;
     151    int src_quality;
     152    int source_audio_channels;
    147153
    148154 private:
    149155    // resampler
     
    155161    AudioOutputDigitalEncoder *encoder;
    156162    FreeSurround              *upmixer;
    157163
    158     int source_audio_channels;
     164    int source_audio_samplerate;
    159165    int source_audio_bytes_per_sample;
    160166    bool needs_upmix;
    161167    int surround_mode;
     168    bool allow_ac3_passthru;
     169    float old_audio_stretchfactor;
    162170
    163171    bool blocking; // do AddSamples calls block?
    164172
  • trunk/mythtv/libs/libmyth/audiosettings.cpp

     
    1616    set_initial_vol(false),
    1717    use_passthru(false),
    1818    codec(NULL),
    19     source(AUDIOOUTPUT_UNKNOWN)
     19    source(AUDIOOUTPUT_UNKNOWN),
     20    isAC3upmix(false)
    2021{
    2122}
    2223
     
    2930    set_initial_vol(other.set_initial_vol),
    3031    use_passthru(other.use_passthru),
    3132    codec(other.codec),
    32     source(other.source)
     33    source(other.source),
     34    isAC3upmix(other.isAC3upmix)
    3335{
    3436}
    3537
     
    4244    AudioOutputSource audio_source,
    4345    bool audio_set_initial_vol,
    4446    bool audio_use_passthru,
    45     void *audio_codec) :
     47    void *audio_codec,
     48    bool AC3upmix) :
    4649    main_device(audio_main_device),
    4750    passthru_device(audio_passthru_device),
    4851    bits(audio_bits),
     
    5154    set_initial_vol(audio_set_initial_vol),
    5255    use_passthru(audio_use_passthru),
    5356    codec(audio_codec),
    54     source(audio_source)
     57    source(audio_source),
     58    isAC3upmix(AC3upmix)
    5559{
    5660}
    5761
     
    6064    int   audio_channels,
    6165    int   audio_samplerate,
    6266    bool  audio_use_passthru,
    63     void *audio_codec) :
     67    void *audio_codec,
     68    bool  AC3upmix) :
    6469    main_device(QString::null),
    6570    passthru_device(QString::null),
    6671    bits(audio_bits),
     
    6974    set_initial_vol(false),
    7075    use_passthru(audio_use_passthru),
    7176    codec(audio_codec),
    72     source(AUDIOOUTPUT_UNKNOWN)
     77    source(AUDIOOUTPUT_UNKNOWN),
     78    isAC3upmix(AC3upmix)
    7379{
    7480}
    7581
  • trunk/mythtv/libs/libmyth/audiooutputalsa.cpp

     
    88
    99#include "mythcontext.h"
    1010#include "audiooutputalsa.h"
     11#include "audiooutputdigitalencoder.h"
    1112   
    1213#define LOC QString("ALSA: ")
    1314#define LOC_WARN QString("ALSA, Warning: ")
     
    3233AudioOutputALSA::~AudioOutputALSA()
    3334{
    3435    KillAudio();
     36    if (isAC3upmix)
     37        SetIECStatus(true);
    3538}
    3639
     40void AudioOutputALSA::SetIECStatus(bool audio) {
     41   
     42    snd_ctl_t *ctl;
     43    const char *spdif_str = SND_CTL_NAME_IEC958("", PLAYBACK, DEFAULT);
     44    int spdif_index = -1;
     45    snd_ctl_elem_list_t *clist;
     46    snd_ctl_elem_id_t *cid;
     47    snd_ctl_elem_value_t *cval;
     48    snd_aes_iec958_t iec958;
     49    int cidx, controls;
     50
     51    VERBOSE(VB_AUDIO, QString("Setting IEC958 status: %1")
     52                      .arg(audio ? "audio" : "non-audio"));
     53   
     54    int err;
     55    if ((err = snd_ctl_open(&ctl, "default", 0)) < 0)
     56    {
     57        Error(QString("AudioOutputALSA::SetIECStatus: snd_ctl_open(default): %1")
     58              .arg(snd_strerror(err)));
     59        return;
     60    }
     61    snd_ctl_elem_list_alloca(&clist);
     62    snd_ctl_elem_list(ctl, clist);
     63    snd_ctl_elem_list_alloc_space(clist, snd_ctl_elem_list_get_count(clist));
     64    snd_ctl_elem_list(ctl, clist);
     65    controls = snd_ctl_elem_list_get_used(clist);
     66    for (cidx = 0; cidx < controls; cidx++) {
     67        if (!strcmp(snd_ctl_elem_list_get_name(clist, cidx), spdif_str))
     68            if (spdif_index < 0 ||
     69                snd_ctl_elem_list_get_index(clist, cidx) == (uint)spdif_index)
     70                    break;
     71    }
     72
     73    if (cidx >= controls)
     74        return;
     75
     76    snd_ctl_elem_id_alloca(&cid);
     77    snd_ctl_elem_list_get_id(clist, cidx, cid);
     78    snd_ctl_elem_value_alloca(&cval);
     79    snd_ctl_elem_value_set_id(cval, cid);
     80    snd_ctl_elem_read(ctl,cval);
     81    snd_ctl_elem_value_get_iec958(cval, &iec958);
     82   
     83    if (!audio)
     84        iec958.status[0] |= IEC958_AES0_NONAUDIO;
     85    else
     86        iec958.status[0] &= ~IEC958_AES0_NONAUDIO;
     87
     88    snd_ctl_elem_value_set_iec958(cval, &iec958);
     89    snd_ctl_elem_write(ctl, cval);
     90
     91}
     92
    3793bool AudioOutputALSA::OpenDevice()
    3894{
    3995    snd_pcm_format_t format;
    4096    unsigned int buffer_time, period_time;
    4197    int err;
     98    QString real_device;
    4299
    43100    if (pcm_handle != NULL)
    44101        CloseDevice();
    45102
    46103    pcm_handle = NULL;
    47104    numbadioctls = 0;
     105   
     106    if (!isAC3upmix) {
     107        real_device = (audio_passthru) ?
     108            audio_passthru_device : audio_main_device;
     109    }
     110    else
     111        if (audio_passthru || audio_enc)
     112        {
     113            real_device = audio_passthru_device;
     114            SetIECStatus(false);
     115        }
     116        else
     117        {
     118            real_device = audio_main_device;
     119            SetIECStatus(true);
     120        }
    48121
    49     QString real_device = (audio_passthru) ?
    50         audio_passthru_device : audio_main_device;
    51 
    52122    VERBOSE(VB_GENERAL, QString("Opening ALSA audio device '%1'.")
    53123            .arg(real_device));
    54124
     
    147217}
    148218
    149219
     220void AudioOutputALSA::reorder_6ch_ac3(void *buf, unsigned int len) {
     221    unsigned short *src = (unsigned short *)buf;
     222    unsigned short tmp;
     223    unsigned int samples = len >> 1;
     224
     225    for (uint i = 0; i < samples; i += 6) {
     226        tmp = src[i+1];
     227        src[i+1] = src[i+2];
     228        src[i+2] = src[i+3];
     229        src[i+3] = src[i+4];
     230        src[i+4] = tmp;
     231    }
     232}
     233
    150234void AudioOutputALSA::WriteAudio(unsigned char *aubuf, int size)
    151235{
    152236    unsigned char *tmpbuf;
     
    159243        return;
    160244    }
    161245   
     246    // Re-Order channels mapping if analog output is used and source is AC3 multi-channels
     247    if (isAC3upmix && !(audio_passthru || audio_enc)
     248        && audio_codec && (audio_codec->codec_id == CODEC_ID_AC3)
     249        && (source_audio_channels > 2)) {
     250        VERBOSE(VB_AUDIO+VB_TIMESTAMP,
     251                QString("WriteAudio: Re-ordering audio channels %1 bytes (%2 frames)")
     252                .arg(size).arg(frames));
     253        reorder_6ch_ac3(aubuf, size);
     254    }
    162255    tmpbuf = aubuf;
    163256
    164257    VERBOSE(VB_AUDIO+VB_TIMESTAMP,
  • trunk/mythtv/libs/libmyth/audiooutputbase.cpp

     
    2828
    2929    audio_main_device(settings.GetMainDevice()),
    3030    audio_passthru_device(settings.GetPassthruDevice()),
    31     audio_passthru(false),      audio_stretchfactor(1.0f),
     31    audio_passthru(false),      audio_enc(false),
     32    audio_reenc(false),         audio_stretchfactor(1.0f),
    3233
    3334    audio_codec(NULL),
    3435    source(settings.source),    killaudio(false),
     
    4748    pSoundStretch(NULL),
    4849    encoder(NULL),
    4950    upmixer(NULL),
     51
    5052    source_audio_channels(-1),
     53    source_audio_samplerate(0),
    5154    source_audio_bytes_per_sample(0),
    5255    needs_upmix(false),
    5356    surround_mode(FreeSurround::SurroundModePassive),
     57    old_audio_stretchfactor(1.0),
    5458
    5559    blocking(false),
    5660
     
    7983    memset(&audiotime_updated, 0, sizeof(audiotime_updated));
    8084    memset(audiobuffer,        0, sizeof(char)  * kAudioRingBufferSize);
    8185    configured_audio_channels = gContext->GetNumSetting("MaxChannels", 2);
     86    orig_config_channels = configured_audio_channels;
     87    allow_ac3_passthru = gContext->GetNumSetting("AC3PassThru", false);
     88    src_quality = gContext->GetNumSetting("SRCQuality", 3);
     89    isAC3upmix = settings.isAC3upmix;
    8290
    8391    // You need to call Reconfigure from your concrete class.
    8492    // Reconfigure(laudio_bits,       laudio_channels,
     
    110118void AudioOutputBase::SetStretchFactorLocked(float laudio_stretchfactor)
    111119{
    112120    effdspstretched = (int)((float)effdsp / laudio_stretchfactor);
    113     if ((audio_stretchfactor != laudio_stretchfactor) ||  !pSoundStretch)
     121    if ((audio_stretchfactor != laudio_stretchfactor) || !pSoundStretch)
    114122    {
    115123        audio_stretchfactor = laudio_stretchfactor;
    116124        if (pSoundStretch)
     
    124132            VERBOSE(VB_GENERAL, LOC + QString("Using time stretch %1")
    125133                                        .arg(audio_stretchfactor));
    126134            pSoundStretch = new soundtouch::SoundTouch();
    127             if (audio_codec)
    128             {
    129                 if (!encoder)
     135            if (!isAC3upmix) {
     136                if (audio_codec)
    130137                {
    131                     VERBOSE(VB_AUDIO, LOC +
    132                             QString("Creating Encoder for codec %1 origfs %2")
    133                             .arg(audio_codec->codec_id)
    134                             .arg(audio_codec->frame_size));
    135 
    136                     encoder = new AudioOutputDigitalEncoder();
    137                     if (!encoder->Init(audio_codec->codec_id,
    138                                 audio_codec->bit_rate,
    139                                 audio_codec->sample_rate,
    140                                 audio_codec->channels
    141                                 ))
     138                    if (!encoder)
    142139                    {
    143                         // eeks
    144                         delete encoder;
    145                         encoder = NULL;
    146140                        VERBOSE(VB_AUDIO, LOC +
    147                                 QString("Failed to Create Encoder"));
     141                                QString("Creating Encoder for codec %1 origfs %2")
     142                                .arg(audio_codec->codec_id)
     143                                .arg(audio_codec->frame_size));
     144
     145                        encoder = new AudioOutputDigitalEncoder(isAC3upmix);
     146                        if (!encoder->Init(audio_codec->codec_id,
     147                                    audio_codec->bit_rate,
     148                                    audio_codec->sample_rate,
     149                                    audio_codec->channels
     150                                    ))
     151                        {
     152                            // eeks
     153                            delete encoder;
     154                            encoder = NULL;
     155                            VERBOSE(VB_AUDIO, LOC +
     156                                    QString("Failed to Create Encoder"));
     157                        }
    148158                    }
    149159                }
    150             }
    151             if (audio_codec && encoder)
    152             {
    153                 pSoundStretch->setSampleRate(audio_codec->sample_rate);
    154                 pSoundStretch->setChannels(audio_codec->channels);
    155             }
    156             else
    157             {
     160                if (audio_codec && encoder)
     161                {
     162                    pSoundStretch->setSampleRate(audio_codec->sample_rate);
     163                    pSoundStretch->setChannels(audio_codec->channels);
     164                }
     165                else
     166                {
     167                    pSoundStretch->setSampleRate(audio_samplerate);
     168                    pSoundStretch->setChannels(audio_channels);
     169                }
     170            } else {
    158171                pSoundStretch->setSampleRate(audio_samplerate);
    159                 pSoundStretch->setChannels(audio_channels);
     172                pSoundStretch->setChannels(upmixer ?
     173                    configured_audio_channels : source_audio_channels);
    160174            }
    161 
    162175            pSoundStretch->setTempo(audio_stretchfactor);
    163176            pSoundStretch->setSetting(SETTING_SEQUENCE_MS, 35);
    164177
    165178            // dont need these with only tempo change
    166179            //pSoundStretch->setPitch(1.0);
    167180            //pSoundStretch->setRate(1.0);
    168 
    169181            //pSoundStretch->setSetting(SETTING_USE_QUICKSEEK, true);
    170182            //pSoundStretch->setSetting(SETTING_USE_AA_FILTER, false);
    171183        }
     
    183195    return audio_stretchfactor;
    184196}
    185197
     198bool AudioOutputBase::ToggleUpmix(void)
     199{
     200    if (orig_config_channels == 2 || audio_passthru)
     201        return false;
     202    if (configured_audio_channels == 6)
     203        configured_audio_channels = 2;
     204    else
     205        configured_audio_channels = 6;
     206
     207    const AudioSettings settings(audio_bits, source_audio_channels,
     208                            source_audio_samplerate, audio_passthru);
     209    Reconfigure(settings);
     210    return (configured_audio_channels == 6);
     211}
     212
     213
    186214void AudioOutputBase::Reconfigure(const AudioSettings &orig_settings)
    187215{
    188216    AudioSettings settings = orig_settings;
     
    193221    int cchannels = 0;
    194222    int lsource_audio_channels = settings.channels;
    195223    bool lneeds_upmix = false;
     224    bool laudio_reenc = false;
    196225
    197     if (settings.codec)
    198     {
    199         lcodec_id = ((AVCodecContext*)settings.codec)->codec_id;
    200         settings.bits = 16;
    201         settings.channels = 2;
    202         lsource_audio_channels = settings.channels;
    203         settings.samplerate = 48000;
    204         lcchannels = ((AVCodecContext*)settings.codec)->channels;
    205     }
     226    if (!isAC3upmix) {
     227        if (settings.codec)
     228        {
     229            lcodec_id = ((AVCodecContext*)settings.codec)->codec_id;
     230            settings.bits = 16;
     231            settings.channels = 2;
     232            lsource_audio_channels = settings.channels;
     233            settings.samplerate = 48000;
     234            lcchannels = ((AVCodecContext*)settings.codec)->channels;
     235        }
    206236
    207     if (audio_codec)
    208     {
    209         codec_id = audio_codec->codec_id;
    210         cchannels = ((AVCodecContext*)audio_codec)->channels;
    211     }
     237        if (audio_codec)
     238        {
     239            codec_id = audio_codec->codec_id;
     240            cchannels = ((AVCodecContext*)audio_codec)->channels;
     241        }
    212242
    213     if ((configured_audio_channels == 6) &&
    214         !(settings.codec || audio_codec))
    215     {
    216         settings.channels = configured_audio_channels;
    217         lneeds_upmix = true;
    218         VERBOSE(VB_AUDIO,LOC + "Needs upmix");
     243        if ((configured_audio_channels == 6) &&
     244            !(settings.codec || audio_codec))
     245        {
     246            settings.channels = configured_audio_channels;
     247            lneeds_upmix = true;
     248            VERBOSE(VB_AUDIO,LOC + "Needs upmix");
     249        }
     250    } else { //AC3 Upmix
     251        // Are we reencoding a (previously) timestretched bitstream?
     252        if (settings.channels > 2 && !settings.use_passthru)
     253            laudio_reenc = true;
     254
     255        // Enough channels? Upmix if not
     256        if (settings.channels < configured_audio_channels &&
     257            !settings.use_passthru)
     258        {
     259            settings.channels = configured_audio_channels;
     260            lneeds_upmix = true;
     261            VERBOSE(VB_AUDIO,LOC + "Needs upmix");
     262        }
     263        if (settings.codec)
     264            audio_codec = (AVCodecContext*)settings.codec;
    219265    }
    220266
    221267    ClearError();
     
    224270        settings.samplerate == audio_samplerate && !need_resampler &&
    225271        settings.use_passthru == audio_passthru &&
    226272        lneeds_upmix == needs_upmix &&
    227         lcodec_id == codec_id && lcchannels == cchannels);
     273        (isAC3upmix ? (laudio_reenc == audio_reenc) : (lcodec_id == codec_id && lcchannels == cchannels))
     274        );
    228275    bool upmix_deps =
    229276        (lsource_audio_channels == source_audio_channels);
    230277    if (general_deps && upmix_deps)
     
    255302    audio_channels = settings.channels;
    256303    source_audio_channels = lsource_audio_channels;
    257304    audio_bits = settings.bits;
    258     audio_samplerate = settings.samplerate;
    259     audio_codec = (AVCodecContext*)settings.codec;
     305    if (!isAC3upmix) {
     306        audio_samplerate = settings.samplerate;
     307        audio_codec = (AVCodecContext*)settings.codec;
     308    } else {
     309        source_audio_samplerate = audio_samplerate = settings.samplerate;
     310        audio_reenc = laudio_reenc;
     311    }
    260312    audio_passthru = settings.use_passthru;
    261313    needs_upmix = lneeds_upmix;
    262314
     
    265317        Error("AudioOutput only supports 8 or 16bit audio.");
    266318        return;
    267319    }
    268     audio_bytes_per_sample = audio_channels * audio_bits / 8;
    269     source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
    270320
    271321    need_resampler = false;
    272322    killaudio = false;
     
    275325    internal_vol = gContext->GetNumSetting("MythControlsVolume", 0);
    276326
    277327    numlowbuffer = 0;
     328   
     329    if (!isAC3upmix) {
     330    audio_bytes_per_sample = audio_channels * audio_bits / 8;
     331    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
     332    } else {
     333    // Encode to AC-3 if not passing thru, there's more than 2 channels
     334    // and we're allowed to passthru AC-3
     335    if (!audio_passthru && audio_channels > 2 && allow_ac3_passthru)
     336    {
     337        VERBOSE(VB_AUDIO, LOC + "Creating AC-3 Encoder");
     338        int srate = src_quality == 0 ? audio_samplerate : 48000;
     339        encoder = new AudioOutputDigitalEncoder(isAC3upmix);
     340        if (!encoder->Init(CODEC_ID_AC3, 448000, srate,
     341                           configured_audio_channels, audio_reenc))
     342        {
     343            VERBOSE(VB_AUDIO, LOC + "Can't create AC-3 encoder");
     344            delete encoder;
     345            encoder = NULL;
     346        }
     347       
     348        audio_enc = true;
     349    }       
     350   
     351    if(audio_passthru || audio_enc)
     352        // AC-3 output - soundcard expects a 2ch 48k stream
     353        audio_channels = 2;
     354   
     355    audio_bytes_per_sample = audio_channels * audio_bits / 8;
     356    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
    278357
     358    // Always resample to 48k - many cards can't do anything else
     359    // and ALSA will do it with linear interpolation (yuk) if we don't anyway
     360    if (src_quality != 0 && audio_samplerate != 48000)
     361    {
     362        int error;
     363        audio_samplerate = 48000;
     364        VERBOSE(VB_GENERAL, LOC + QString("Using resampler. From: %1 to %2")
     365            .arg(settings.samplerate).arg(audio_samplerate));
     366        src_ctx = src_new(3-src_quality, audio_channels, &error);
     367        if (error)
     368        {
     369            Error(QString("Error creating resampler, the error was: %1")
     370                  .arg(src_strerror(error)) );
     371            src_ctx = NULL;
     372            return;
     373        }
     374        src_data.src_ratio = (double) audio_samplerate / settings.samplerate;
     375        src_data.data_in = src_in;
     376        src_data.data_out = src_out;
     377        src_data.output_frames = 16384*6;
     378        need_resampler = true;
     379    }
     380    }
    279381    VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2(%3) sr %4")
    280382            .arg(audio_main_device).arg(audio_channels)
    281383            .arg(source_audio_channels).arg(audio_samplerate));
     
    309411    current_seconds = -1;
    310412    source_bitrate = -1;
    311413
     414    if (!isAC3upmix) {
    312415    // NOTE: this won't do anything as above samplerate vars are set equal
    313416    // Check if we need the resampler
    314417    if (audio_samplerate != settings.samplerate)
     
    329432        src_data.output_frames = 16384*6;
    330433        need_resampler = true;
    331434    }
    332 
     435    }
    333436    if (needs_upmix)
    334437    {
    335438        VERBOSE(VB_AUDIO, LOC + QString("create upmixer"));
     
    341444        upmixer = new FreeSurround(
    342445            audio_samplerate,
    343446            source == AUDIOOUTPUT_VIDEO,
    344             (FreeSurround::SurroundMode)surround_mode);
     447            (FreeSurround::SurroundMode)surround_mode,isAC3upmix);
    345448
    346449        VERBOSE(VB_AUDIO, LOC +
    347450                QString("create upmixer done with surround mode %1")
     
    350453
    351454    VERBOSE(VB_AUDIO, LOC + QString("Audio Stretch Factor: %1")
    352455            .arg(audio_stretchfactor));
    353     VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1")
    354             .arg((audio_codec) ?
     456    if (!isAC3upmix) {
     457        VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1")
     458                .arg((audio_codec) ?
    355459                 codec_id_string(audio_codec->codec_id) : "not set"));
    356460
    357     if (redo_stretch)
    358     {
    359         delete pSoundStretch;
    360         pSoundStretch = NULL;
    361         SetStretchFactorLocked(audio_stretchfactor);
    362     }
    363     else
    364     {
    365         SetStretchFactorLocked(audio_stretchfactor);
    366         if (pSoundStretch)
     461        if (redo_stretch)
    367462        {
    368             // if its passthru then we need to reencode
    369             if (audio_codec)
     463            delete pSoundStretch;
     464            pSoundStretch = NULL;
     465            SetStretchFactorLocked(audio_stretchfactor);
     466        }
     467        else
     468        {
     469            SetStretchFactorLocked(audio_stretchfactor);
     470            if (pSoundStretch)
    370471            {
    371                 if (!encoder)
     472                // if its passthru then we need to reencode
     473                if (audio_codec)
    372474                {
    373                     VERBOSE(VB_AUDIO, LOC +
    374                             QString("Creating Encoder for codec %1")
    375                             .arg(audio_codec->codec_id));
     475                    if (!encoder)
     476                    {
     477                        VERBOSE(VB_AUDIO, LOC +
     478                                QString("Creating Encoder for codec %1")
     479                                .arg(audio_codec->codec_id));
    376480
    377                     encoder = new AudioOutputDigitalEncoder();
    378                     if (!encoder->Init(audio_codec->codec_id,
    379                                 audio_codec->bit_rate,
    380                                 audio_codec->sample_rate,
    381                                 audio_codec->channels
    382                                 ))
    383                     {
    384                         // eeks
    385                         delete encoder;
    386                         encoder = NULL;
    387                         VERBOSE(VB_AUDIO, LOC + "Failed to Create Encoder");
     481                        encoder = new AudioOutputDigitalEncoder(isAC3upmix);
     482                        if (!encoder->Init(audio_codec->codec_id,
     483                                    audio_codec->bit_rate,
     484                                    audio_codec->sample_rate,
     485                                    audio_codec->channels
     486                                    ))
     487                        {
     488                            // eeks
     489                            delete encoder;
     490                            encoder = NULL;
     491                            VERBOSE(VB_AUDIO, LOC + "Failed to Create Encoder");
     492                        }
    388493                    }
    389494                }
     495                if (audio_codec && encoder)
     496                {
     497                    pSoundStretch->setSampleRate(audio_codec->sample_rate);
     498                    pSoundStretch->setChannels(audio_codec->channels);
     499                }
     500                else
     501                {
     502                    pSoundStretch->setSampleRate(audio_samplerate);
     503                    pSoundStretch->setChannels(audio_channels);
     504                }
    390505            }
    391             if (audio_codec && encoder)
    392             {
    393                 pSoundStretch->setSampleRate(audio_codec->sample_rate);
    394                 pSoundStretch->setChannels(audio_codec->channels);
    395             }
    396             else
    397             {
    398                 pSoundStretch->setSampleRate(audio_samplerate);
    399                 pSoundStretch->setChannels(audio_channels);
    400             }
    401506        }
     507    } else {
     508        SetStretchFactorLocked(old_audio_stretchfactor);
    402509    }
    403 
    404510    // Setup visualisations, zero the visualisations buffers
    405511    prepareVisuals();
    406512
     
    437543    killaudio = true;
    438544    StopOutputThread();
    439545
     546    if (isAC3upmix)
     547        QMutexLocker lock1(&audio_buflock);
     548
    440549    // Close resampler?
    441     if (src_ctx)
     550    if (src_ctx) {
    442551        src_delete(src_ctx);
     552        if (isAC3upmix)
     553            src_ctx = NULL;
     554    }
    443555    need_resampler = false;
    444556
    445557    // close sound stretcher
     
    447559    {
    448560        delete pSoundStretch;
    449561        pSoundStretch = NULL;
     562        if (isAC3upmix) {
     563            old_audio_stretchfactor = audio_stretchfactor;
     564            audio_stretchfactor = 1.0;
     565        }
    450566    }
    451567
    452568    if (encoder)
     
    461577        upmixer = NULL;
    462578    }
    463579    needs_upmix = false;
     580    if (isAC3upmix)
     581        audio_enc = false;
    464582
    465583    CloseDevice();
    466584
     
    612730    // include algorithmic latencies
    613731    if (pSoundStretch)
    614732    {
    615         // add the effect of any unused but processed samples,
    616         // AC3 reencode does this
    617         totalbuffer += (int)(pSoundStretch->numSamples() *
    618                              audio_bytes_per_sample);
     733        if (!isAC3upmix) {
     734            // add the effect of any unused but processed samples,
     735            // AC3 reencode does this
     736            totalbuffer += (int)(pSoundStretch->numSamples() *
     737                                 audio_bytes_per_sample);
     738        }
    619739        // add the effect of unprocessed samples in time stretch algo
    620740        totalbuffer += (int)((pSoundStretch->numUnprocessedSamples() *
    621741                              audio_bytes_per_sample) / audio_stretchfactor);
     
    626746        totalbuffer += upmixer->sampleLatency() * audio_bytes_per_sample;
    627747    }
    628748
     749    if (isAC3upmix && encoder)
     750        totalbuffer += encoder->Buffered();
     751
    629752    audiotime = audbuf_timecode - (int)(totalbuffer * 100000.0 /
    630753                                   (audio_bytes_per_sample * effdspstretched));
    631754
     
    681804        return false; // would overflow
    682805    }
    683806
     807    if (isAC3upmix)
     808        QMutexLocker lock1(&audio_buflock);
     809
    684810    // resample input if necessary
    685811    if (need_resampler && src_ctx)
    686812    {
     
    725851    int abps = (encoder) ?
    726852        encoder->audio_bytes_per_sample : audio_bytes_per_sample;
    727853    int len = samples * abps;
     854   
     855    if (isAC3upmix) {
     856        // Give original samples to mythmusic visualisation
     857        dispatchVisual((unsigned char *)buffer, len, timecode,
     858                       source_audio_channels, audio_bits);
     859    }
    728860
    729861    // Check we have enough space to write the data
    730862    if (need_resampler && src_ctx)
     
    749881        return false; // would overflow
    750882    }
    751883
     884    if (isAC3upmix)
     885        QMutexLocker lock1(&audio_buflock);
     886
    752887    // resample input if necessary
    753888    if (need_resampler && src_ctx)
    754889    {
     
    808943            if (src_ctx)
    809944            {
    810945                int error = src_reset(src_ctx);
    811                 if (error)
     946                if (error)
     947                {
    812948                    VERBOSE(VB_IMPORTANT, LOC_ERR + QString(
    813949                            "Error occured while resetting resampler: %1")
    814950                            .arg(src_strerror(error)));
     951                    if (isAC3upmix)
     952                        src_ctx = NULL;
     953                }
    815954            }
    816955        }
    817956    }
     
    821960void AudioOutputBase::_AddSamples(void *buffer, bool interleaved, int samples,
    822961                                  long long timecode)
    823962{
    824     audio_buflock.lock();
     963    if (!isAC3upmix)
     964        audio_buflock.lock();
    825965
    826966    int len; // = samples * audio_bytes_per_sample;
    827967    int audio_bytes = audio_bits / 8;
     
    839979            .arg(samples * abps)
    840980            .arg(kAudioRingBufferSize-afree).arg(afree).arg(timecode)
    841981            .arg(needs_upmix));
    842 
     982   
     983    if (isAC3upmix)
     984        len = WaitForFreeSpace(samples);
     985       
    843986    if (upmixer && needs_upmix)
    844987    {
    845988        int out_samples = 0;
    846989        int step = (interleaved)?source_audio_channels:1;
    847         len = WaitForFreeSpace(samples);    // test
     990       
     991        if (!isAC3upmix) {
     992            len = WaitForFreeSpace(samples);
     993        }
    848994        for (int itemp = 0; itemp < samples; )
    849995        {
    850996            // just in case it does a processing cycle, release the lock
    851997            // to allow the output loop to do output
    852             audio_buflock.unlock();
     998            if (!isAC3upmix)
     999                audio_buflock.unlock();
    8531000            if (audio_bytes == 2)
    8541001            {
    8551002                itemp += upmixer->putSamples(
     
    8661013                    source_audio_channels,
    8671014                    (interleaved) ? 0 : samples);
    8681015            }
    869             audio_buflock.lock();
     1016            if (!isAC3upmix)
     1017                audio_buflock.lock();
    8701018
    8711019            int copy_samples = upmixer->numSamples();
    8721020            if (copy_samples)
     
    9001048    }
    9011049    else
    9021050    {
    903         len = WaitForFreeSpace(samples);
    904 
     1051        if (!isAC3upmix)
     1052            len = WaitForFreeSpace(samples);
    9051053        if (interleaved)
    9061054        {
    9071055            char *mybuf = (char*)buffer;
     
    9361084        }
    9371085    }
    9381086
     1087    if (!isAC3upmix) {
    9391088    if (samples > 0)
    9401089    {
    9411090        if (pSoundStretch)
     
    9931142                        continue;
    9941143
    9951144                    //len = WaitForFreeSpace(amount);
    996                     char *ob = encoder->GetOutBuff();
     1145                    const char *ob = encoder->GetOutBuff();
    9971146                    if (amount >= bdiff)
    9981147                    {
    9991148                        memcpy(audiobuffer + org_waud, ob, bdiff);
     
    10681217    }
    10691218
    10701219    audio_buflock.unlock();
     1220    } else {
     1221    if (samples <= 0)
     1222        return;
     1223       
     1224    if (pSoundStretch)
     1225    {
     1226        // does not change the timecode, only the number of samples
     1227        // back to orig pos
     1228        org_waud = waud;
     1229        int bdiff = kAudioRingBufferSize - org_waud;
     1230        int nSamplesToEnd = bdiff/abps;
     1231        if (bdiff < len)
     1232        {
     1233            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)
     1234                                      (audiobuffer +
     1235                                       org_waud), nSamplesToEnd);
     1236            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)audiobuffer,
     1237                                      (len - bdiff) / abps);
     1238        }
     1239        else
     1240            pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)
     1241                                      (audiobuffer + org_waud),
     1242                                      len / abps);
     1243
     1244        int nSamples = pSoundStretch->numSamples();
     1245        len = WaitForFreeSpace(nSamples);
     1246       
     1247        while ((nSamples = pSoundStretch->numSamples()))
     1248        {
     1249            if (nSamples > nSamplesToEnd)
     1250                nSamples = nSamplesToEnd;
     1251           
     1252            nSamples = pSoundStretch->receiveSamples(
     1253                (soundtouch::SAMPLETYPE*)
     1254                (audiobuffer + org_waud), nSamples
     1255            );
     1256           
     1257            if (nSamples == nSamplesToEnd) {
     1258                org_waud = 0;
     1259                nSamplesToEnd = kAudioRingBufferSize/abps;
     1260            }
     1261            else {
     1262                org_waud += nSamples * abps;
     1263                nSamplesToEnd -= nSamples;
     1264            }
     1265           
     1266        }
     1267       
     1268    }
     1269
     1270    // Encode to AC-3?
     1271    if (encoder)
     1272    {
     1273       
     1274        org_waud = waud;
     1275        int bdiff = kAudioRingBufferSize - org_waud;
     1276        int to_get = 0;
     1277
     1278        if (bdiff < len)
     1279        {
     1280            encoder->EncodeUpmix(audiobuffer + org_waud, bdiff);
     1281            to_get = encoder->EncodeUpmix(audiobuffer, len - bdiff);
     1282        }
     1283        else
     1284            to_get = encoder->EncodeUpmix(audiobuffer + org_waud, len);
     1285       
     1286        if (to_get > 0)
     1287        {
     1288           
     1289            if (to_get >= bdiff)
     1290            {
     1291                encoder->GetFrames(audiobuffer + org_waud, bdiff);
     1292                to_get -= bdiff;
     1293                org_waud = 0;
     1294            }
     1295            if (to_get > 0)
     1296                encoder->GetFrames(audiobuffer + org_waud, to_get);
     1297
     1298            org_waud += to_get;
     1299
     1300        }
     1301
     1302    }
     1303
     1304    waud = org_waud;
     1305    lastaudiolen = audiolen(false);
     1306
     1307    if (timecode < 0)
     1308        // mythmusic doesn't give timestamps..
     1309        timecode = (int)((samples_buffered * 100000.0) / effdsp);
     1310
     1311    samples_buffered += samples;
     1312
     1313    /* we want the time at the end -- but the file format stores
     1314       time at the start of the chunk. */
     1315    // even with timestretch, timecode is still calculated from original
     1316    // sample count
     1317    audbuf_timecode = timecode + (int)((samples * 100000.0) / effdsp);
     1318    } // End AC3 upmiz
    10711319}
    10721320
    10731321void AudioOutputBase::Status()
  • trunk/mythtv/libs/libmyth/audiooutput.cpp

     
    3535    const QString &passthru_device,
    3636    int audio_bits, int audio_channels, int audio_samplerate,
    3737    AudioOutputSource source,
    38     bool set_initial_vol, bool audio_passthru)
     38    bool set_initial_vol, bool audio_passthru, bool AC3upmix)
    3939{
    4040    AudioSettings settings(
    4141        main_device, passthru_device, audio_bits,
    4242        audio_channels, audio_samplerate, source,
    43         set_initial_vol, audio_passthru);
     43        set_initial_vol, audio_passthru, NULL, AC3upmix);
    4444
    4545    settings.FixPassThrough();
    4646
  • trunk/mythtv/libs/libmyth/audiosettings.h

     
    3333        AudioOutputSource audio_source,
    3434        bool              audio_set_initial_vol,
    3535        bool              audio_use_passthru,
    36         void             *audio_codec = NULL);
     36        void             *audio_codec = NULL,
     37        bool              AC3upmix = false);
    3738
    3839    AudioSettings(int   audio_bits,
    3940                  int   audio_channels,
    4041                  int   audio_samplerate,
    4142                  bool  audio_use_passthru,
    42                   void *audio_codec = NULL);
     43                  void *audio_codec = NULL,
     44                  bool  AC3upmix = false);
    4345
    4446    void FixPassThrough(void);
    4547    void TrimDeviceType(void);
     
    5961    bool    use_passthru;
    6062    void   *codec;
    6163    AudioOutputSource source;
     64    bool    isAC3upmix;
    6265};
    6366
    6467#endif // _AUDIO_SETTINGS_H_
  • trunk/mythtv/libs/libmyth/audiooutputdigitalencoder.cpp

     
    2929AudioOutputDigitalEncoder::AudioOutputDigitalEncoder(void) :
    3030    audio_bytes_per_sample(0),
    3131    av_context(NULL),
    32     outbuf(NULL),
    33     outbuf_size(0),
    3432    frame_buffer(NULL),
    35     one_frame_bytes(0)
     33    one_frame_bytes(0),
     34    outbuflen(0),
     35    inbuflen(0),
     36    reorder(true),
     37    isAC3upmix(false)
    3638{
    3739}
    3840
     41AudioOutputDigitalEncoder::AudioOutputDigitalEncoder(bool AC3upmix) :
     42    audio_bytes_per_sample(0),
     43    av_context(NULL),
     44    frame_buffer(NULL),
     45    one_frame_bytes(0),
     46    outbuflen(0),
     47    inbuflen(0),
     48    reorder(true),
     49    isAC3upmix(AC3upmix)
     50{
     51}
     52
    3953AudioOutputDigitalEncoder::~AudioOutputDigitalEncoder()
    4054{
    4155    Dispose();
     
    5064        av_context = NULL;
    5165    }
    5266
    53     if (outbuf)
     67    if (!isAC3upmix && frame_buffer)
    5468    {
    55         delete [] outbuf;
    56         outbuf = NULL;
    57         outbuf_size = 0;
    58     }
    59 
    60     if (frame_buffer)
    61     {
    6269        delete [] frame_buffer;
    6370        frame_buffer = NULL;
    6471        one_frame_bytes = 0;
     
    6774
    6875//CODEC_ID_AC3
    6976bool AudioOutputDigitalEncoder::Init(
    70     CodecID codec_id, int bitrate, int samplerate, int channels)
     77    CodecID codec_id, int bitrate, int samplerate, int channels, bool reencoding)
    7178{
    7279    AVCodec *codec;
    7380    int ret;
    7481
    75     VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4")
     82    VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4 re=%5")
    7683            .arg(codec_id_string(codec_id))
    7784            .arg(bitrate)
    7885            .arg(samplerate)
    79             .arg(channels));
     86            .arg(channels)
     87            .arg(reencoding));
     88   
     89    reorder = !reencoding;
    8090
     91    // We need to do this when called from mythmusic
     92    if (isAC3upmix) {
     93        avcodec_init();
     94        avcodec_register_all();
     95    }
    8196    //codec = avcodec_find_encoder(codec_id);
    8297    // always AC3 as there is no DTS encoder at the moment 2005/1/9
    8398    codec = avcodec_find_encoder(CODEC_ID_AC3);
     
    107122    audio_bytes_per_sample = bytes_per_frame;
    108123    one_frame_bytes = bytes_per_frame * av_context->frame_size;
    109124
    110     outbuf_size = 16384;    // ok for AC3 but DTS?
    111     outbuf = new char [outbuf_size];
    112125    VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init fs=%1, bpf=%2 ofb=%3")
    113126            .arg(av_context->frame_size)
    114127            .arg(bytes_per_frame)
     
    253266
    254267} AESHeader;
    255268
     269void reorder_6ch_ac3(void *buf, unsigned int len) {
     270    unsigned short *src = (unsigned short *)buf;
     271    unsigned short tmp;
     272    unsigned int samples = len >> 1;
     273
     274    for (uint i = 0; i < samples; i += 6) {
     275        tmp = src[i+4];
     276        src[i+4] = src[i+3];
     277        src[i+3] = src[i+2];
     278        src[i+2] = src[i+1];
     279        src[i+1] = tmp;
     280    }
     281}
     282
    256283static int encode_frame(
    257284        bool dts,
    258285        unsigned char *data,
    259         size_t &len)
     286        size_t &len, bool AC3upmix)
    260287{
    261288    unsigned char *payload = data + 8;  // skip header, currently 52 or 54bits
    262     size_t         enc_len;
     289    size_t enc_len = len;
    263290    int            flags, sample_rate, bit_rate;
    264291
    265292    // we don't do any length/crc validation of the AC3 frame here; presumably
     
    270297    // ignore, and if so, may as well just assume that it will ignore
    271298    // anything with a bad CRC...
    272299
    273     uint nr_samples = 0, block_len;
     300    uint nr_samples = 0, block_len = 0;
     301   
    274302    if (dts)
    275303    {
    276304        enc_len = dts_syncinfo(payload, &flags, &sample_rate, &bit_rate);
     
    305333#endif
    306334    }
    307335
    308     if (enc_len == 0 || enc_len > len)
     336    if (!AC3upmix && (enc_len == 0 || enc_len > len))
    309337    {
    310338        int l = len;
    311339        len = 0;
     
    364392    data[6] = (enc_len << 3) & 0xFF;
    365393    data[7] = (enc_len >> 5) & 0xFF;
    366394    memset(payload + enc_len, 0, block_len - 8 - enc_len);
    367     len = block_len;
     395    if (!AC3upmix)
     396        len = block_len;
    368397
    369398    return enc_len;
    370399}
     
    377406 
    378407    // put data in the correct spot for encode frame
    379408    outsize = avcodec_encode_audio(
    380         av_context, ((uchar*)outbuf) + 8, outbuf_size - 8, buff);
     409        av_context, ((uchar*)outbuf) + 8, OUTBUFSIZE - 8, buff);
    381410
    382411    size_t tmpsize = outsize;
    383412
    384413    outsize = MAX_AC3_FRAME_SIZE;
    385414    encsize = encode_frame(
    386415        /*av_context->codec_id==CODEC_ID_DTS*/ false,
    387         (unsigned char*)outbuf, outsize);
     416        (unsigned char*)outbuf, outsize, false);
    388417
    389418    VERBOSE(VB_AUDIO+VB_TIMESTAMP,
    390419            QString("DigitalEncoder::Encode len1=%1 len2=%2 finallen=%3")
     
    392421
    393422    return outsize;
    394423}
     424
     425size_t AudioOutputDigitalEncoder::EncodeUpmix(void *buf, int len)
     426{
     427    size_t outsize = 0;
     428 
     429    int fs = FrameSize();
     430    memcpy(inbuf+inbuflen, buf, len);
     431    inbuflen += len;
     432    int frames = inbuflen / fs;
     433
     434    while (frames--)
     435    {
     436        if (reorder)
     437            reorder_6ch_ac3(inbuf, fs);
     438       
     439        // put data in the correct spot for encode frame
     440        outsize = avcodec_encode_audio(
     441            av_context, ((uchar*)outbuf) + outbuflen + 8, OUTBUFSIZE - 8, (short int *)inbuf);
     442       
     443        encode_frame(
     444            /*av_context->codec_id==CODEC_ID_DTS*/ false,
     445            (unsigned char*)outbuf + outbuflen, outsize, true
     446        );
     447
     448        outbuflen += MAX_AC3_FRAME_SIZE;
     449        inbuflen -= fs;
     450        memmove(inbuf, inbuf+fs, inbuflen);
     451    }
     452 
     453    return outbuflen;
     454}
     455
     456void AudioOutputDigitalEncoder::GetFrames(void *ptr, int maxlen)
     457{
     458    int len = (maxlen < outbuflen ? maxlen : outbuflen);
     459    memcpy(ptr, outbuf, len);
     460    outbuflen -= len;
     461    memmove(outbuf, outbuf+len, outbuflen);
     462}
  • trunk/mythtv/libs/libmyth/audiooutput.h

     
    1717        const QString &passthrudevice,
    1818        int audio_bits, int audio_channels, int audio_samplerate,
    1919        AudioOutputSource source,
    20         bool set_initial_vol, bool audio_passthru);
     20        bool set_initial_vol, bool audio_passthru, bool AC3upmix=false);
    2121
    2222    AudioOutput() :
    2323        VolumeBase(),             OutputListeners(),
     24        isAC3upmix(false),
    2425        lastError(QString::null), lastWarn(QString::null) {}
    2526
    2627    virtual ~AudioOutput() { };
     
    6970    virtual void bufferOutputData(bool y) = 0;
    7071    virtual int readOutputData(unsigned char *read_buffer, int max_length) = 0;
    7172
     73    // Audio AC3 Upmixer
     74    virtual bool ToggleUpmix(void) = 0;
     75    bool isAC3upmix;
     76
    7277  protected:
    7378    void Error(const QString &msg);
    7479    void Warn(const QString &msg);
  • trunk/mythtv/libs/libmyth/audiooutputdigitalencoder.h

     
    55#include "libavcodec/avcodec.h"
    66};
    77
     8#define INBUFSIZE 131072
     9#define OUTBUFSIZE 98304
     10
    811class AudioOutputDigitalEncoder
    912{
    1013  public:
    1114    AudioOutputDigitalEncoder(void);
     15    AudioOutputDigitalEncoder(bool AC3upmix);
    1216    ~AudioOutputDigitalEncoder();
    1317
    14     bool   Init(CodecID codec_id, int bitrate, int samplerate, int channels);
     18    bool   Init(CodecID codec_id, int bitrate, int samplerate,
     19                int channels, bool reencoding = false);
    1520    void   Dispose(void);
    1621    size_t Encode(short * buff);
    1722
    1823    inline char *GetFrameBuffer(void);
    1924    size_t FrameSize(void)  const { return one_frame_bytes; }
    20     char  *GetOutBuff(void) const { return outbuf;          }
     25    const char  *GetOutBuff(void) const { return outbuf; }
     26    //AC3 upmix
     27    size_t EncodeUpmix(void *buf, int len);
     28    void   GetFrames(void *ptr, int maxlen);
     29    int    Buffered(void) const { return inbuflen; }
    2130
    2231  public:
    2332    size_t audio_bytes_per_sample;
    2433
    2534  private:
    2635    AVCodecContext *av_context;
    27     char           *outbuf;
    28     int             outbuf_size;
     36    char            outbuf[OUTBUFSIZE];
    2937    char           *frame_buffer;
    3038    size_t          one_frame_bytes;
     39    // AC3 upmix
     40    char            inbuf[INBUFSIZE];
     41    int             outbuflen;
     42    int             inbuflen;
     43    bool            reorder;
     44    bool            isAC3upmix;
    3145};
    3246
    3347inline char *AudioOutputDigitalEncoder::GetFrameBuffer(void)
  • trunk/mythtv/libs/libmyth/audiooutputalsa.h

     
    6767    virtual int  GetBufferedOnSoundcard(void) const;
    6868
    6969  private:
     70    void SetIECStatus(bool audio);
    7071    inline int SetParameters(snd_pcm_t *handle,
    7172                             snd_pcm_format_t format, unsigned int channels,
    7273                             unsigned int rate, unsigned int buffer_time,
     
    7980    void CloseMixer(void);
    8081    void SetupMixer(void);
    8182    ALSAVolumeInfo GetVolumeRange(snd_mixer_elem_t *elem) const;
     83    void reorder_6ch_ac3(void *buf, unsigned int len);
    8284
    8385  private:
    8486    snd_pcm_t   *pcm_handle;
  • trunk/mythtv/libs/libmythfreesurround/el_processor.cpp

     
    4040
    4141const float PI = 3.141592654;
    4242const float epsilon = 0.000001;
     43const float center_level_upmix = 0.5*sqrt(0.5);
    4344//const float center_level = 0.5*sqrt(0.5);   // gain of the center channel
    4445//const float center_level = sqrt(0.5);   // gain of the center channel
    4546const float center_level = 1.0;   // gain of the center channel
     
    5758public:
    5859    // create an instance of the decoder
    5960    //  blocksize is fixed over the lifetime of this object for performance reasons
    60     decoder_impl(unsigned blocksize=8192): N(blocksize), halfN(blocksize/2) {
     61  decoder_impl(unsigned blocksize=8192, bool AC3upmix=false): N(blocksize), halfN(blocksize/2), isAC3upmix(AC3upmix) {
    6162#ifdef USE_FFTW3
    6263        // create FFTW buffers
    6364        lt = (float*)fftwf_malloc(sizeof(float)*N);
     
    99100            filter[c].resize(N);
    100101        }
    101102        // DC component of filters is always 0
    102         for (unsigned c=0;c<5;c++)
    103         {
    104             filter[c][0] = 0.0;
    105             filter[c][1] = 0.0;
    106             filter[c][halfN] = 0.0;
     103        if (!isAC3upmix) {
     104            for (unsigned c=0;c<5;c++)
     105            {
     106                filter[c][0] = 0.0;
     107                filter[c][1] = 0.0;
     108                filter[c][halfN] = 0.0;
     109            }
     110            sample_rate(48000);
     111            // generate the window function (square root of hann, b/c it is applied before and after the transform)
     112            wnd.resize(N);
     113            // dft normalization included in the window for zero cost scaling
     114            // also add a gain factor of *2 due to processing gain in algo (see center_level)
     115            surround_gain(1.0);
     116        } else {
     117            sample_rate(48000);
     118            // generate the window function (square root of hann, b/c it is applied before and after the transform)
     119            wnd.resize(N);
     120            for (unsigned k=0;k<N;k++)
     121                wnd[k] = sqrt(0.5*(1-cos(2*PI*k/N))/N);
    107122        }
    108         sample_rate(48000);
    109         // generate the window function (square root of hann, b/c it is applied before and after the transform)
    110         wnd.resize(N);
    111         // dft normalization included in the window for zero cost scaling
    112         // also add a gain factor of *2 due to processing gain in algo (see center_level)
    113         surround_gain(1.0);
    114123        current_buf = 0;
    115124        // set the default coefficients
    116125        surround_coefficients(0.8165,0.5774);
     
    192201    // set lfe filter params
    193202    void sample_rate(unsigned int srate) {
    194203        // lfe filter is just straight through band limited
    195         unsigned int cutoff = (250*N)/srate;
    196         for (unsigned f=0;f<=halfN;f++) {           
    197             if ((f>=2) && (f<cutoff))
    198                 filter[5][f] = 1.0;
    199             else
    200                 filter[5][f] = 0.0;
     204        unsigned int cutoff;
     205        if (!isAC3upmix) {
     206             cutoff = (250*N)/srate;
     207            for (unsigned f=0;f<=halfN;f++) {           
     208                if ((f>=2) && (f<cutoff))
     209                    filter[5][f] = 1.0;
     210                else
     211                    filter[5][f] = 0.0;
     212            }
     213        } else {
     214            cutoff = (30*N)/srate;
     215            for (unsigned f=0;f<=halfN;f++) {           
     216                if (f<cutoff)
     217                    filter[5][f] = 0.5*sqrt(0.5);
     218                else
     219                    filter[5][f] = 0.0;
     220            }
    201221        }
    202222    }
    203223
     
    237257    }
    238258
    239259private:
     260    bool isAC3upmix;
    240261    // polar <-> cartesian coodinates conversion
    241262    static inline float amplitude(const float cf[2]) { return sqrt(cf[0]*cf[0] + cf[1]*cf[1]); }
    242263    static inline float phase(const float cf[2]) { return atan2(cf[1],cf[0]); }
     
    290311
    291312        // 2. compare amplitude and phase of each DFT bin and produce the X/Y coordinates in the sound field
    292313        //    but dont do DC or N/2 component
    293         for (unsigned f=2;f<halfN;f++) {           
     314        unsigned start_f = isAC3upmix ? 0:2;
     315        for (unsigned f=start_f;f<halfN;f++) {           
    294316            // get left/right amplitudes/phases
    295317            float ampL = amplitude(dftL[f]), ampR = amplitude(dftR[f]);
    296318            float phaseL = phase(dftL[f]), phaseR = phase(dftR[f]);
     
    357379                float front = (1+yfs[f])/2, back = (1-yfs[f])/2;
    358380                float volume[5] = {
    359381                    front * (left * center_width + max(0,-xfs[f]) * (1-center_width)),  // left
    360                     front * center_level*((1-abs(xfs[f])) * (1-center_width)),          // center
     382                    front * (isAC3upmix ? center_level_upmix : center_level) *((1-abs(xfs[f])) * (1-center_width)),          // center
    361383                    front * (right * center_width + max(0, xfs[f]) * (1-center_width)), // right
    362384                    back * surround_level * left,                                       // left surround
    363385                    back * surround_level * right                                       // right surround
     
    402424                float front = (1+yfs[f])/2, back = (1-yfs[f])/2;
    403425                float volume[5] = {
    404426                    front * (left * center_width + max(0,-xfs[f]) * (1-center_width)),      // left
    405                     front * center_level*((1-abs(xfs[f])) * (1-center_width)),              // center
     427                    front * (isAC3upmix ? center_level_upmix : center_level) *((1-abs(xfs[f])) * (1-center_width)),          // center
    406428                    front * (right * center_width + max(0, xfs[f]) * (1-center_width)),     // right
    407429                    back * surround_level*max(0,min(1,((1-(xfs[f]/surround_balance))/2))),  // left surround
    408430                    back * surround_level*max(0,min(1,((1+(xfs[f]/surround_balance))/2)))   // right surround
     
    613635
    614636// implementation of the shell class
    615637
    616 fsurround_decoder::fsurround_decoder(unsigned blocksize): impl(new decoder_impl(blocksize)) { }
     638fsurround_decoder::fsurround_decoder(unsigned blocksize, bool AC3upmix): impl(new decoder_impl(blocksize,AC3upmix)) { }
    617639
    618640fsurround_decoder::~fsurround_decoder() { delete impl; }
    619641
  • trunk/mythtv/libs/libmythfreesurround/freesurround.cpp

     
    6363const unsigned default_block_size = 8192;
    6464// there will be a slider for this in the future
    6565//const float master_gain = 1.0;
    66 //#define MASTER_GAIN * master_gain
     66//#define MASTER_GAIN * master_gain 
    6767#define MASTER_GAIN
    6868//const float master_gain = 1.0/(1<<15);
    6969//const float inv_master_gain = (1<<15);
     
    163163
    164164// construction methods
    165165void *new_decoder() { return new fsurround_decoder(block_size); }
     166void *new_decoder_upmix() { return new fsurround_decoder(block_size,true); }
    166167void *new_buffers() { return new buffers(block_size/2); }
    167168void *new_int16buffers() { return new int16buffers(block_size/2); }
    168169
    169170object_pool dp(&new_decoder);
     171object_pool dp_upmix(&new_decoder_upmix);
    170172//object_pool bp(&new_buffers);
    171173object_pool bp16(&new_int16buffers);
    172174
     
    175177int channel_select = -1;
    176178#endif
    177179
    178 FreeSurround::FreeSurround(uint srate, bool moviemode, SurroundMode smode) :
     180FreeSurround::FreeSurround(uint srate, bool moviemode, SurroundMode smode, bool AC3upmix) :
    179181        srate(srate),
    180182        open_(false),
    181183        initialized_(false),
     
    186188        out_count(0),
    187189        processed(true),
    188190        processed_size(0),
    189         surround_mode(smode)
     191        surround_mode(smode),
     192        isAC3upmix(AC3upmix)
    190193{
    191194    VERBOSE(QString("FreeSurround::FreeSurround rate %1 moviemode %2").arg(srate).arg(moviemode));
    192195    if (moviemode)
    193196    {
    194197        params.phasemode = 1;
    195         params.center_width = 0;
    196         params.gain = 1.0;
     198        if (!isAC3upmix) {
     199            params.center_width = 0;
     200            params.gain = 1.0;
     201        } else {
     202            params.center_width = 25;
     203            params.dimension = 0.5;
     204        }
    197205    }
    198206    else
    199207    {
    200         params.center_width = 70;
    201         // for 50, gain should be about 1.9, c/lr about 2.7
    202         // for 70, gain should be about 3.1, c/lr about 1.5
    203         params.gain = 3.1;
     208        if (!isAC3upmix) {
     209            params.center_width = 70;
     210            // for 50, gain should be about 1.9, c/lr about 2.7
     211            // for 70, gain should be about 3.1, c/lr about 1.5
     212            params.gain = 3.1;
     213        } else {
     214            params.center_width = 65;
     215            params.dimension = 0.3;
     216        }
    204217    }
    205218    switch (surround_mode)
    206219    {
     
    236249        decoder->phase_mode(params.phasemode);
    237250        decoder->surround_coefficients(params.coeff_a, params.coeff_b);                         
    238251        decoder->separation(params.front_sep/100.0,params.rear_sep/100.0);
    239         decoder->gain(params.gain);
     252        if (!isAC3upmix)
     253            decoder->gain(params.gain);
    240254    }
    241255}
    242256
     
    655669    {
    656670        if (decoder)
    657671        {
    658             // actually these params need only be set when they change... but it doesn't hurt
    659 #if 0
    660             decoder->steering_mode(params.steering);
    661             decoder->phase_mode(params.phasemode);
    662             decoder->surround_coefficients(params.coeff_a, params.coeff_b);                             
    663             decoder->separation(params.front_sep/100.0,params.rear_sep/100.0);
    664 #endif
    665             // decode the bufs->block
    666             //decoder->decode(input,output,params.center_width/100.0,params.dimension/100.0);
    667             //decoder->decode(output,params.center_width/100.0,params.dimension/100.0);
    668672            decoder->decode(params.center_width/100.0,params.dimension/100.0);
    669673        }
    670674    }
     
    694698{               
    695699    if (!decoder)
    696700    {
    697         decoder = (fsurround_decoder*)dp.acquire((void*)1);
     701        if (!isAC3upmix)
     702            decoder = (fsurround_decoder*)dp.acquire((void*)1);
     703        else
     704            decoder = (fsurround_decoder*)dp_upmix.acquire((void*)1);
    698705        decoder->flush();
    699706        //if (bufs)
    700707        //    bufs->clear();
     
    709716{
    710717    if (decoder)
    711718    {
    712         dp.release(this);
     719        if (!isAC3upmix)
     720            dp.release(this);
     721        else
     722            dp_upmix.release(this);
    713723        decoder = 0;
    714724    }
    715725}
  • trunk/mythtv/libs/libmythfreesurround/el_processor.h

     
    2424public:
    2525        // create an instance of the decoder
    2626        //  blocksize is fixed over the lifetime of this object for performance reasons
    27         fsurround_decoder(unsigned blocksize=8192);
     27        fsurround_decoder(unsigned blocksize=8192, bool AC3upmix=false);
    2828        // destructor
    2929        ~fsurround_decoder();
    3030       
     
    5151        void gain(float gain);
    5252
    5353        // set the phase shifting mode for decoding
    54         // 0 = (+0°,+0°)   - music mode
    55         // 1 = (+0°,+180°) - PowerDVD compatibility
    56         // 2 = (+180°,+0°) - BeSweet compatibility
    57         // 3 = (-90°,+90°) - This seems to work. I just don't know why.
     54        // 0 = (+0,+0)   - music mode
     55        // 1 = (+0,+180) - PowerDVD compatibility
     56        // 2 = (+180,+0) - BeSweet compatibility
     57        // 3 = (-90,+90) - This seems to work. I just don't know why.
    5858        void phase_mode(unsigned mode);
    5959
    6060        // override the steering mode
     
    7171
    7272private:
    7373        class decoder_impl *impl; // private implementation (details hidden)
     74        bool isAC3upmix;
    7475};
    7576
    7677
  • trunk/mythtv/libs/libmythfreesurround/freesurround.h

     
    3131        SurroundModeActiveLinear
    3232    } SurroundMode;
    3333public:
    34     FreeSurround(uint srate, bool moviemode, SurroundMode mode);
     34    FreeSurround(uint srate, bool moviemode, SurroundMode mode, bool AC3upmix=false);
    3535    ~FreeSurround();
    3636
    3737    // put samples in buffer, returns number of samples used
     
    8888    bool processed;             // whether processing is enabled or not for latency calc
    8989    int processed_size;         // amount processed
    9090    SurroundMode surround_mode; // 1 of 3 surround modes supported
     91    bool isAC3upmix;
    9192
    9293};
    9394
  • trunk/mythtv/programs/mythfrontend/globalsettings.cpp

     
    119119    return gc;
    120120}
    121121
     122static HostCheckBox *AC3Upmix()
     123{
     124    HostCheckBox *gc = new HostCheckBox("MythAC3Upmix");
     125    gc->setLabel(QObject::tr("AC3 audio upmixer"));
     126    gc->setValue(false);
     127    gc->setHelpText(QObject::tr("Enable AC3 upmixer and digital software volume control."));
     128    return gc;
     129}
     130
     131static HostComboBox *SRCQuality()
     132{
     133    HostComboBox *gc = new HostComboBox("SRCQuality", false);
     134    gc->setLabel(QObject::tr("Sample Rate Conversion"));
     135    gc->addSelection(QObject::tr("Best"), "3", true); // default
     136    gc->addSelection(QObject::tr("Medium"), "2");
     137    gc->addSelection(QObject::tr("Fastest"), "1");
     138    gc->addSelection(QObject::tr("Disabled"), "0");
     139    gc->setHelpText(
     140            QObject::tr(
     141                "Set the quality of audio sample rate conversion. "
     142                "This only affects non 48000Hz PCM audio. "
     143                "All three options offer a worst-case SNR of 97dB. "
     144                "'Best' at a bandwidth of 97%. "
     145                "'Medium' at a bandwidth of 90%. "
     146                "'Fastest' at a bandwidth of 80%. "
     147                "Set 'Disabled' only if you know what you are doing."));
     148    return gc;
     149}
     150
    122151static HostComboBox *PassThroughOutputDevice()
    123152{
    124153    HostComboBox *gc = new HostComboBox("PassThruOutputDevice", true);
     
    34323461    return gs;
    34333462}
    34343463
    3435 class AudioSystemSettingsGroup : public VerticalConfigurationGroup
     3464class AudioSystemSettingsGroup : public TriggeredConfigurationGroup
    34363465{
    34373466  public:
    34383467    AudioSystemSettingsGroup() :
    3439         VerticalConfigurationGroup(false, true, false, false)
     3468        TriggeredConfigurationGroup(false, true, false, false)
    34403469    {
    34413470        setLabel(QObject::tr("Audio System"));
    34423471        setUseLabel(false);
     
    34513480        addChild(AC3PassThrough());
    34523481        addChild(DTSPassThrough());
    34533482        addChild(AggressiveBuffer());
     3483
     3484        Setting* ac3upmix = AC3Upmix();
     3485        addChild(ac3upmix);
     3486        setTrigger(ac3upmix);
     3487        addTarget("1", SRCQuality());
     3488        addTarget("0", new VerticalConfigurationGroup(false,false));
     3489
    34543490    }
    34553491};
    34563492
  • trunk/mythtv/programs/mythtranscode/transcode.cpp

     
    222222        // Do nothing
    223223        return kMuteOff;
    224224    }
     225    virtual bool ToggleUpmix(void)
     226    {
     227        // Do nothing
     228        return false;
     229    }
    225230
    226231    //  These are pure virtual in AudioOutput, but we don't need them here
    227232    virtual void bufferOutputData(bool){ return; }