Ticket #5900: audioencoding-trunk.patch

File audioencoding-trunk.patch, 61.8 KB (added by foobum@…, 15 years ago)
  • libs/libmythtv/NuppelVideoPlayer.cpp

     
    212212      audio_passthru_device(QString::null),
    213213      audio_channels(2),            audio_bits(-1),
    214214      audio_samplerate(44100),      audio_stretchfactor(1.0f),
    215       audio_codec(NULL),
    216215      // Picture-in-Picture
    217216      pipplayer(NULL), setpipplayer(NULL), needsetpipplayer(false),
    218217      // Preview window support
     
    885884    if (audioOutput)
    886885    {
    887886        const AudioSettings settings(
    888             audio_bits, audio_channels, audio_samplerate,
    889             audio_passthru, audio_codec);
     887            audio_bits, audio_channels, audio_samplerate, audio_passthru);
    890888        audioOutput->Reconfigure(settings);
     889        if (audio_passthru)
     890            audio_channels = 2;
    891891        errMsg = audioOutput->GetError();
    892892        if (!errMsg.isEmpty())
    893893            audioOutput->SetStretchFactor(audio_stretchfactor);
     
    38223822    audio_passthru = passthru;
    38233823}
    38243824
    3825 void NuppelVideoPlayer::SetAudioCodec(void *ac)
    3826 {
    3827     audio_codec = ac;
    3828 }
    3829 
    38303825void NuppelVideoPlayer::SetEffDsp(int dsprate)
    38313826{
    38323827    if (audioOutput)
     
    50395034    }
    50405035}
    50415036
     5037bool NuppelVideoPlayer::ToggleUpmix()
     5038{
     5039    if (audioOutput)
     5040        return audioOutput->ToggleUpmix();
     5041    return false;
     5042}
     5043
    50425044void NuppelVideoPlayer::Zoom(ZoomDirection direction)
    50435045{
    50445046    if (videoOutput)
  • libs/libmythtv/avformatdecoder.cpp

     
    415415      audioSamples(NULL),
    416416      allow_ac3_passthru(false),    allow_dts_passthru(false),
    417417      disable_passthru(false),      max_channels(2),
    418       dummy_frame(NULL),
     418      last_ac3_channels(0),         dummy_frame(NULL),
    419419      // DVD
    420420      lastdvdtitle(-1),
    421421      decodeStillFrame(false),
     
    30003000bool AvFormatDecoder::GetFrame(int onlyvideo)
    30013001{
    30023002    AVPacket *pkt = NULL;
     3003    AC3HeaderInfo hdr;
    30033004    int len;
    30043005    unsigned char *ptr;
    30053006    int data_size = 0;
     
    31933194        pts = 0;
    31943195
    31953196        AVStream *curstream = ic->streams[pkt->stream_index];
     3197        AVCodecContext *ctx = curstream->codec;
    31963198
    31973199        if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
    31983200            pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
    31993201
    32003202        if (ringBuffer->isDVD() &&
    3201             curstream->codec->codec_type == CODEC_TYPE_VIDEO)
     3203            ctx->codec_type == CODEC_TYPE_VIDEO)
    32023204        {
    32033205            MpegPreProcessPkt(curstream, pkt);
    32043206           
     
    32203222           
    32213223            if (!d->HasMPEG2Dec())
    32223224            {
    3223                 int current_width = curstream->codec->width;
     3225                int current_width = ctx->width;
    32243226                int video_width = GetNVP()->GetVideoSize().width();
    32253227                if (dvd_xvmc_enabled && GetNVP() && GetNVP()->getVideoOutput())
    32263228                {
     
    32613263        }
    32623264
    32633265        if (storevideoframes &&
    3264             curstream->codec->codec_type == CODEC_TYPE_VIDEO)
     3266            ctx->codec_type == CODEC_TYPE_VIDEO)
    32653267        {
    32663268            av_dup_packet(pkt);
    32673269            storedPackets.append(pkt);
     
    32693271            continue;
    32703272        }
    32713273
    3272         if (len > 0 && curstream->codec->codec_type == CODEC_TYPE_VIDEO &&
     3274        if (len > 0 && ctx->codec_type == CODEC_TYPE_VIDEO &&
    32733275            pkt->stream_index == selectedVideoIndex)
    32743276        {
    3275             AVCodecContext *context = curstream->codec;
    32763277
    3277             if (context->codec_id == CODEC_ID_MPEG1VIDEO ||
    3278                 context->codec_id == CODEC_ID_MPEG2VIDEO ||
    3279                 context->codec_id == CODEC_ID_MPEG2VIDEO_XVMC ||
    3280                 context->codec_id == CODEC_ID_MPEG2VIDEO_XVMC_VLD)
     3278            if (ctx->codec_id == CODEC_ID_MPEG1VIDEO ||
     3279                ctx->codec_id == CODEC_ID_MPEG2VIDEO ||
     3280                ctx->codec_id == CODEC_ID_MPEG2VIDEO_XVMC ||
     3281                ctx->codec_id == CODEC_ID_MPEG2VIDEO_XVMC_VLD)
    32813282            {
    32823283                if (!ringBuffer->isDVD())
    32833284                    MpegPreProcessPkt(curstream, pkt);
    32843285            }
    3285             else if (context->codec_id == CODEC_ID_H264)
     3286            else if (ctx->codec_id == CODEC_ID_H264)
    32863287            {
    32873288                H264PreProcessPkt(curstream, pkt);
    32883289            }
     
    33273328        }
    33283329
    33293330        if (len > 0 &&
    3330             curstream->codec->codec_type == CODEC_TYPE_DATA &&
    3331             curstream->codec->codec_id   == CODEC_ID_MPEG2VBI)
     3331            ctx->codec_type == CODEC_TYPE_DATA &&
     3332            ctx->codec_id   == CODEC_ID_MPEG2VBI)
    33323333        {
    33333334            ProcessVBIDataPacket(curstream, pkt);
    33343335
     
    33373338        }
    33383339
    33393340        if (len > 0 &&
    3340             curstream->codec->codec_type == CODEC_TYPE_DATA &&
    3341             curstream->codec->codec_id   == CODEC_ID_DVB_VBI)
     3341            ctx->codec_type == CODEC_TYPE_DATA &&
     3342            ctx->codec_id   == CODEC_ID_DVB_VBI)
    33423343        {
    33433344            ProcessDVBDataPacket(curstream, pkt);
    33443345
     
    33483349
    33493350#ifdef USING_MHEG
    33503351        if (len > 0 &&
    3351             curstream->codec->codec_type == CODEC_TYPE_DATA &&
    3352             curstream->codec->codec_id   == CODEC_ID_DSMCC_B)
     3352            ctx->codec_type == CODEC_TYPE_DATA &&
     3353            ctx->codec_id   == CODEC_ID_DSMCC_B)
    33533354        {
    33543355            ProcessDSMCCPacket(curstream, pkt);
    33553356
     
    33703371#endif // USING_MHEG
    33713372
    33723373        // we don't care about other data streams
    3373         if (curstream->codec->codec_type == CODEC_TYPE_DATA)
     3374        if (ctx->codec_type == CODEC_TYPE_DATA)
    33743375        {
    33753376            av_free_packet(pkt);
    33763377            continue;
    33773378        }
    33783379
    3379         if (!curstream->codec->codec)
     3380        if (!ctx->codec)
    33803381        {
    33813382            VERBOSE(VB_PLAYBACK, LOC +
    33823383                    QString("No codec for stream index %1, type(%2) id(%3:%4)")
    33833384                    .arg(pkt->stream_index)
    3384                     .arg(codec_type_string(curstream->codec->codec_type))
    3385                     .arg(codec_id_string(curstream->codec->codec_id))
    3386                     .arg(curstream->codec->codec_id));
     3385                    .arg(codec_type_string(ctx->codec_type))
     3386                    .arg(codec_id_string(ctx->codec_id))
     3387                    .arg(ctx->codec_id));
    33873388            av_free_packet(pkt);
    33883389            continue;
    33893390        }
     
    33923393        have_err = false;
    33933394
    33943395        avcodeclock.lock();
    3395         int ctype  = curstream->codec->codec_type;
     3396        int ctype  = ctx->codec_type;
    33963397        int audIdx = selectedTrack[kTrackTypeAudio].av_stream_index;
    33973398        int audSubIdx = selectedTrack[kTrackTypeAudio].av_substream_index;
    33983399        int subIdx = selectedTrack[kTrackTypeSubtitle].av_stream_index;
     
    34173418
    34183419                    // detect switches between stereo and dual languages
    34193420                    bool wasDual = audSubIdx != -1;
    3420                     bool isDual = curstream->codec->avcodec_dual_language;
     3421                    bool isDual = ctx->avcodec_dual_language;
    34213422                    if ((wasDual && !isDual) || (!wasDual &&  isDual))
    34223423                    {
    34233424                        SetupAudioStreamSubIndexes(audIdx);
     
    34253426                    }                           
    34263427
    34273428                    bool do_ac3_passthru =
    3428                         (allow_ac3_passthru && !transcoding &&
    3429                          (curstream->codec->codec_id == CODEC_ID_AC3));
     3429                        (allow_ac3_passthru && !transcoding &&
     3430                         ctx->channels >= (int)max_channels &&
     3431                         (ctx->codec_id == CODEC_ID_AC3));
    34303432                    bool do_dts_passthru =
    34313433                        (allow_dts_passthru && !transcoding &&
    3432                          (curstream->codec->codec_id == CODEC_ID_DTS));
     3434                         (ctx->codec_id == CODEC_ID_DTS));
    34333435                    bool using_passthru = do_ac3_passthru || do_dts_passthru;
    34343436
    34353437                    // detect channels on streams that need
    34363438                    // to be decoded before we can know this
    34373439                    bool already_decoded = false;
    3438                     if (!curstream->codec->channels)
     3440                    if (!ctx->channels)
    34393441                    {
    34403442                        QMutexLocker locker(&avcodeclock);
    34413443                        VERBOSE(VB_IMPORTANT, LOC +
     
    34463448                        {
    34473449                            // for passthru let it select the max number
    34483450                            // of channels
    3449                             curstream->codec->channels = 0;
    3450                             curstream->codec->request_channels = 0;
     3451                            ctx->channels = 0;
     3452                            ctx->request_channels = 0;
    34513453                        }
    34523454                        else
    34533455                        {
    3454                             curstream->codec->channels = audioOut.channels;
    3455                             curstream->codec->request_channels =
     3456                            ctx->channels = audioOut.channels;
     3457                            ctx->request_channels =
    34563458                                audioOut.channels;
    34573459                        }
    34583460                        ret = avcodec_decode_audio(
    3459                             curstream->codec, audioSamples,
     3461                            ctx, audioSamples,
    34603462                            &data_size, ptr, len);
    34613463                        already_decoded = true;
    34623464
    3463                         reselectAudioTrack |= curstream->codec->channels;
     3465                        reselectAudioTrack |= ctx->channels;
    34643466                    }
    34653467
     3468                    if (ctx->codec_id == CODEC_ID_AC3)
     3469                    {
     3470                        GetBitContext gbc;
     3471                        init_get_bits(&gbc, ptr, len * 8);
     3472                        if (!ff_ac3_parse_header(&gbc, &hdr))
     3473                        {
     3474                            if (hdr.channels != last_ac3_channels)
     3475                            {
     3476                                last_ac3_channels = ctx->channels = hdr.channels;
     3477                                SetupAudioStream();
     3478                            }
     3479                        }
     3480                    }
     3481
    34663482                    if (reselectAudioTrack)
    34673483                    {
    34683484                        QMutexLocker locker(&avcodeclock);
     
    34763492                            .av_stream_index;
    34773493                        audSubIdx = selectedTrack[kTrackTypeAudio]
    34783494                            .av_substream_index;
     3495                        ctx = curstream->codec;
    34793496                    }
    34803497
    34813498                    if (firstloop && pkt->pts != (int64_t)AV_NOPTS_VALUE)
     
    35073524                    if (audioOut.do_passthru)
    35083525                    {
    35093526                        data_size = pkt->size;
    3510                         bool dts = CODEC_ID_DTS == curstream->codec->codec_id;
     3527                        bool dts = CODEC_ID_DTS == ctx->codec_id;
    35113528                        ret = encode_frame(dts, ptr, len,
    35123529                                           audioSamples, data_size);
    35133530                    }
    35143531                    else
    35153532                    {
    3516                         AVCodecContext *ctx = curstream->codec;
    3517 
    35183533                        if ((ctx->channels == 0) ||
    35193534                            (ctx->channels > audioOut.channels))
    35203535                        {
     
    35233538
    35243539                        if (!already_decoded)
    35253540                        {
    3526                             curstream->codec->request_channels =
    3527                                 audioOut.channels;
     3541                            ctx->request_channels = audioOut.channels;
    35283542                            ret = avcodec_decode_audio(
    35293543                                ctx, audioSamples, &data_size, ptr, len);
    35303544                        }
     
    35413555                            audIdx = -1;
    35423556                            AutoSelectAudioTrack();
    35433557                            data_size = 0;
     3558                            ctx = curstream->codec;
    35443559                        }
    35453560                    }
    35463561                    avcodeclock.unlock();
     
    35583573
    35593574                    // calc for next frame
    35603575                    lastapts += (long long)((double)(data_size * 1000) /
    3561                                 (curstream->codec->channels * 2) /
    3562                                 curstream->codec->sample_rate);
     3576                                (ctx->channels * 2) / ctx->sample_rate);
    35633577
    35643578                    VERBOSE(VB_PLAYBACK+VB_TIMESTAMP,
    35653579                            LOC + QString("audio timecode %1 %2 %3 %4")
     
    36273641                        continue;
    36283642                    }
    36293643
    3630                     AVCodecContext *context = curstream->codec;
    36313644                    AVFrame mpa_pic;
    36323645                    bzero(&mpa_pic, sizeof(AVFrame));
    36333646
     
    36423655                            // HACK
    36433656                            while (!gotpicture && count < 5)
    36443657                            {
    3645                                 ret = d->DecodeMPEG2Video(context, &mpa_pic,
     3658                                ret = d->DecodeMPEG2Video(ctx, &mpa_pic,
    36463659                                                  &gotpicture, ptr, len);
    36473660                                count++;
    36483661                            }
    36493662                        }
    36503663                        else
    36513664                        {
    3652                             ret = d->DecodeMPEG2Video(context, &mpa_pic,
     3665                            ret = d->DecodeMPEG2Video(ctx, &mpa_pic,
    36533666                                                &gotpicture, ptr, len);
    36543667                        }
    36553668                    }
    36563669                    else
    36573670                    {
    3658                         ret = avcodec_decode_video(context, &mpa_pic,
     3671                        ret = avcodec_decode_video(ctx, &mpa_pic,
    36593672                                                   &gotpicture, ptr, len);
    36603673                        // Reparse it to not drop the DVD still frame
    36613674                        if (decodeStillFrame)
    3662                             ret = avcodec_decode_video(context, &mpa_pic,
     3675                            ret = avcodec_decode_video(ctx, &mpa_pic,
    36633676                                                        &gotpicture, ptr, len);
    36643677                    }
    36653678                    avcodeclock.unlock();
     
    37263739
    37273740                        img_convert(&tmppicture, PIX_FMT_YUV420P,
    37283741                                    (AVPicture *)&mpa_pic,
    3729                                     context->pix_fmt,
    3730                                     context->width,
    3731                                     context->height);
     3742                                    ctx->pix_fmt,
     3743                                    ctx->width,
     3744                                    ctx->height);
    37323745
    37333746                        if (xf)
    37343747                        {
     
    37513764                        (temppts + 10000 > lastvpts || temppts < 0))
    37523765                    {
    37533766                        temppts = lastvpts;
    3754                         temppts += (long long)(1000 * av_q2d(context->time_base));
     3767                        temppts += (long long)(1000 * av_q2d(ctx->time_base));
    37553768                        // MPEG2 frames can be repeated, update pts accordingly
    37563769                        temppts += (long long)(mpa_pic.repeat_pict * 500
    3757                                       * av_q2d(curstream->codec->time_base));
     3770                                      * av_q2d(ctx->time_base));
    37583771                    }
    37593772
    37603773                    VERBOSE(VB_PLAYBACK+VB_TIMESTAMP, LOC +
     
    37903803                    picframe->frameNumber = framesPlayed;
    37913804                    GetNVP()->ReleaseNextVideoFrame(picframe, temppts);
    37923805                    if (d->HasMPEG2Dec() && mpa_pic.data[3])
    3793                         context->release_buffer(context, &mpa_pic);
     3806                        ctx->release_buffer(ctx, &mpa_pic);
    37943807
    37953808                    decoded_video_frame = picframe;
    37963809                    gotvideo = 1;
     
    38463859                }
    38473860                default:
    38483861                {
    3849                     AVCodecContext *enc = curstream->codec;
    38503862                    VERBOSE(VB_IMPORTANT, LOC_ERR +
    38513863                            QString("Decoding - id(%1) type(%2)")
    3852                             .arg(codec_id_string(enc->codec_id))
    3853                             .arg(codec_type_string(enc->codec_type)));
     3864                            .arg(codec_id_string(ctx->codec_id))
     3865                            .arg(codec_type_string(ctx->codec_type)));
    38543866                    have_err = true;
    38553867                    break;
    38563868                }
     
    40084020    AVStream *curstream = NULL;
    40094021    AVCodecContext *codec_ctx = NULL;
    40104022    AudioInfo old_in  = audioIn;
    4011     AudioInfo old_out = audioOut;
    40124023    bool using_passthru = false;
    40134024
    40144025    if ((currentTrack[kTrackTypeAudio] >= 0) &&
     
    40234034        if (codec_ctx)
    40244035        {
    40254036            bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
     4037                                    codec_ctx->channels >= (int)max_channels &&
    40264038                                    (codec_ctx->codec_id == CODEC_ID_AC3));
    40274039            bool do_dts_passthru = (allow_dts_passthru && !transcoding &&
    40284040                                    (codec_ctx->codec_id == CODEC_ID_DTS));
     
    40464058            QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1));
    40474059
    40484060    audioOut = audioIn = info;
    4049     AudioInfo tmpAudioOut = audioOut;
    40504061
    4051     // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card
    4052     if (using_passthru && !disable_passthru)
    4053     {
    4054         tmpAudioOut.channels    = 2;
    4055         tmpAudioOut.sample_rate = 48000;
    4056         tmpAudioOut.sample_size = 4;
    4057     }
    4058 
    4059     if (audioOut.channels > (int) max_channels)
    4060     {
    4061         audioOut.channels    = (int) max_channels;
    4062         audioOut.sample_size = audioOut.channels * 2;
    4063         codec_ctx->channels  = audioOut.channels;
    4064     }
    4065 
    4066     if (!using_passthru)
    4067         tmpAudioOut = audioOut;
    4068 
    40694062    VERBOSE(VB_AUDIO, LOC + "Audio format changed " +
    4070             QString("%1%2\n\t\t\tfrom %3 ; %4\n\t\t\tto   %5 ; %6")
    4071             .arg((using_passthru) ? "digital passthrough " : "")
    4072             .arg((using_passthru) ? tmpAudioOut.toString() : QString(""))
    4073             .arg(old_in.toString()).arg(old_out.toString())
    4074             .arg(audioIn.toString()).arg(audioOut.toString()));
     4063            QString("\n\t\t\tfrom %1 to %2")
     4064            .arg(old_in.toString()).arg(audioOut.toString()));
    40754065
    4076     if (tmpAudioOut.sample_rate > 0)
    4077         GetNVP()->SetEffDsp(tmpAudioOut.sample_rate * 100);
     4066    if (audioOut.sample_rate > 0)
     4067        GetNVP()->SetEffDsp(audioOut.sample_rate * 100);
    40784068
    4079     GetNVP()->SetAudioParams(tmpAudioOut.bps(), tmpAudioOut.channels,
    4080                              tmpAudioOut.sample_rate, audioIn.do_passthru);
     4069    GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels,
     4070                             audioOut.sample_rate, audioOut.do_passthru);
    40814071
    4082     // allow the audio stuff to reencode
    4083     GetNVP()->SetAudioCodec((using_passthru) ? codec_ctx : NULL);
    40844072    GetNVP()->ReinitAudio();
    40854073
    40864074    return true;
  • libs/libmythtv/tv_play.h

     
    333333    void ChangeSpeed(int direction);
    334334    void ToggleTimeStretch(void);
    335335    void ChangeTimeStretch(int dir, bool allowEdit = true);
     336    void ToggleUpmix(void);
    336337    void ChangeAudioSync(int dir, bool allowEdit = true);
    337338    float StopFFRew(void);
    338339    void ChangeFFRew(int direction);
  • libs/libmythtv/NuppelVideoPlayer.h

     
    178178    // Toggle Sets
    179179    void ToggleAspectOverride(AspectOverrideMode aspectMode = kAspect_Toggle);
    180180    void ToggleAdjustFill(AdjustFillMode adjustfillMode = kAdjustFill_Toggle);
     181    bool ToggleUpmix(void);
    181182
    182183    // Gets
    183184    QSize   GetVideoBufferSize(void) const    { return video_dim; }
     
    690691    int      audio_bits;
    691692    int      audio_samplerate;
    692693    float    audio_stretchfactor;
    693     void    *audio_codec;
    694694    bool     audio_passthru;
    695695
    696696    // Picture-in-Picture
  • libs/libmythtv/tv_play.cpp

     
    378378    REG_KEY("TV Playback", "VOLUMEDOWN", "Volume down", "[,{,F10,Volume Down");
    379379    REG_KEY("TV Playback", "VOLUMEUP",   "Volume up",   "],},F11,Volume Up");
    380380    REG_KEY("TV Playback", "MUTE",       "Mute",        "|,\\,F9,Volume Mute");
     381    REG_KEY("TV Playback", "TOGGLEUPMIX", "Toggle upmixer", "Ctrl+U");
    381382    REG_KEY("TV Playback", "TOGGLEPIPMODE", "Toggle Picture-in-Picture mode",
    382383            "V");
    383384    REG_KEY("TV Playback", "TOGGLEPIPWINDOW", "Toggle active PiP window", "B");
     
    510511  Teletext     F2,F3,F4,F5,F6,F7,F8
    511512  ITV          F2,F3,F4,F5,F6,F7,F12
    512513
    513   Playback: Ctrl-B,Ctrl-G,Ctrl-Y
     514  Playback: Ctrl-B,Ctrl-G,Ctrl-Y,Ctrl-U
    514515*/
    515516}
    516517
     
    27762777            else if (action == "VOLUMEDOWN" || action == "VOLUMEUP" ||
    27772778                     action == "STRETCHINC" || action == "STRETCHDEC" ||
    27782779                     action == "MUTE"       || action == "TOGGLEASPECT" ||
    2779                      action == "TOGGLEFILL" )
     2780                     action == "TOGGLEFILL" || action == "TOGGLEUPMIX")
    27802781            {
    27812782                passThru = 1;
    27822783                handled = false;
     
    28312832            else if (action == "VOLUMEDOWN" || action == "VOLUMEUP" ||
    28322833                     action == "STRETCHINC" || action == "STRETCHDEC" ||
    28332834                     action == "MUTE" || action == "PAUSE" ||
    2834                      action == "CLEAROSD")
     2835                     action == "CLEAROSD" || action == "TOGGLEUPMIX")
    28352836            {
    28362837                passThru = 1;
    28372838                handled = false;
     
    31593160            ChangeTimeStretch(0);   // just display
    31603161        else if (action == "TOGGLESTRETCH")
    31613162            ToggleTimeStretch();
     3163        else if (action == "TOGGLEUPMIX")
     3164            ToggleUpmix();
    31623165        else if (action == "CYCLECOMMSKIPMODE") {
    31633166            SetAutoCommercialSkip((enum commSkipMode)
    31643167                                  ((autoCommercialSkip + 1) % CommSkipModes));
     
    60626065    }
    60636066}
    60646067
     6068void TV::ToggleUpmix()
     6069{
     6070    if (!activenvp || !activenvp->HasAudioOut())
     6071        return;
     6072    QString text;
     6073    if (activenvp->ToggleUpmix())
     6074        text = tr("Upmixer On");
     6075    else
     6076        text = tr("Upmixer Off");
     6077   
     6078    if (GetOSD() && !browsemode)
     6079        GetOSD()->SetSettingsText(text, 5);
     6080}
     6081   
    60656082// dir in 10ms jumps
    60666083void TV::ChangeAudioSync(int dir, bool allowEdit)
    60676084{
     
    73757392
    73767393        ChangeTimeStretch(0, !floatRead);   // just display
    73777394    }
     7395    else if (action == "TOGGLEUPMIX")
     7396        ToggleUpmix();
    73787397    else if (action.left(11) == "SELECTSCAN_")
    73797398        activenvp->SetScanType((FrameScanType) action.right(1).toInt());
    73807399    else if (action.left(15) == "TOGGLEAUDIOSYNC")
     
    76297648    new OSDGenericTree(ats_item, tr("1.5X"), "ADJUSTSTRETCH1.5",
    76307649                       (speedX100 == 150) ? 1 : 0, NULL,
    76317650                       "STRETCHGROUP");
     7651   
     7652    new OSDGenericTree(treeMenu, tr("Toggle Upmixer"), "TOGGLEUPMIX");
    76327653
    76337654    // add scan mode override settings to menu
    76347655    FrameScanType scan_type = kScan_Ignore;
  • libs/libmythtv/avformatdecoder.h

     
    268268    bool              allow_dts_passthru;
    269269    bool              disable_passthru;
    270270    uint              max_channels;
     271    uint              last_ac3_channels;
    271272
    272273    VideoFrame       *dummy_frame;
    273274
  • 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                        } ;
  • 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;
    137     AVCodecContext *audio_codec;
    138140    AudioOutputSource source;
    139141
    140142    bool killaudio;
     
    156158    FreeSurround              *upmixer;
    157159
    158160    int source_audio_channels;
     161    int source_audio_samplerate;
    159162    int source_audio_bytes_per_sample;
    160163    bool needs_upmix;
    161164    int surround_mode;
     165    float old_audio_stretchfactor;
    162166
    163167    bool blocking; // do AddSamples calls block?
    164168
  • libs/libmyth/audiosettings.cpp

     
    1515    samplerate(-1),
    1616    set_initial_vol(false),
    1717    use_passthru(false),
    18     codec(NULL),
    1918    source(AUDIOOUTPUT_UNKNOWN)
    2019{
    2120}
     
    2827    samplerate(other.samplerate),
    2928    set_initial_vol(other.set_initial_vol),
    3029    use_passthru(other.use_passthru),
    31     codec(other.codec),
    3230    source(other.source)
    3331{
    3432}
     
    4139    int audio_samplerate,
    4240    AudioOutputSource audio_source,
    4341    bool audio_set_initial_vol,
    44     bool audio_use_passthru,
    45     void *audio_codec) :
     42    bool audio_use_passthru) :
    4643    main_device(audio_main_device),
    4744    passthru_device(audio_passthru_device),
    4845    bits(audio_bits),
     
    5047    samplerate(audio_samplerate),
    5148    set_initial_vol(audio_set_initial_vol),
    5249    use_passthru(audio_use_passthru),
    53     codec(audio_codec),
    5450    source(audio_source)
    5551{
    5652}
     
    5955    int   audio_bits,
    6056    int   audio_channels,
    6157    int   audio_samplerate,
    62     bool  audio_use_passthru,
    63     void *audio_codec) :
     58    bool  audio_use_passthru) :
    6459    main_device(QString::null),
    6560    passthru_device(QString::null),
    6661    bits(audio_bits),
     
    6863    samplerate(audio_samplerate),
    6964    set_initial_vol(false),
    7065    use_passthru(audio_use_passthru),
    71     codec(audio_codec),
    7266    source(AUDIOOUTPUT_UNKNOWN)
    7367{
    7468}
    7569
    7670void AudioSettings::FixPassThrough(void)
    7771{
    78     if (passthru_device.isEmpty() || passthru_device.toLower() == "default")
     72    if (passthru_device.toLower() == "default")
    7973        passthru_device = GetMainDevice();
    8074}
    8175
  • libs/libmyth/audiooutputalsa.cpp

     
    3434    KillAudio();
    3535}
    3636
     37void SetIECBits(bool audio) {
     38   
     39    snd_ctl_t *ctl;
     40    const char *spdif_str = SND_CTL_NAME_IEC958("", PLAYBACK, DEFAULT);
     41    int spdif_index = -1;
     42    snd_ctl_elem_list_t *clist;
     43    snd_ctl_elem_id_t *cid;
     44    snd_ctl_elem_value_t *cval;
     45    snd_aes_iec958_t iec958;
     46    int cidx, controls;
     47
     48    VERBOSE(VB_GENERAL, QString("Setting IEC958 status bits"));
     49   
     50    snd_ctl_open(&ctl, "default", 0);
     51    snd_ctl_elem_list_alloca(&clist);
     52    snd_ctl_elem_list(ctl, clist);
     53    snd_ctl_elem_list_alloc_space(clist, snd_ctl_elem_list_get_count(clist));
     54    snd_ctl_elem_list(ctl, clist);
     55    controls = snd_ctl_elem_list_get_used(clist);
     56    for (cidx = 0; cidx < controls; cidx++) {
     57        if (!strcmp(snd_ctl_elem_list_get_name(clist, cidx), spdif_str))
     58            if (spdif_index < 0 ||
     59                snd_ctl_elem_list_get_index(clist, cidx) == spdif_index)
     60                    break;
     61    }
     62    snd_ctl_elem_id_alloca(&cid);
     63    snd_ctl_elem_list_get_id(clist, cidx, cid);
     64    snd_ctl_elem_value_alloca(&cval);
     65    snd_ctl_elem_value_set_id(cval, cid);
     66    snd_ctl_elem_read(ctl,cval);
     67    snd_ctl_elem_value_get_iec958(cval, &iec958);
     68   
     69    if (!audio)
     70        iec958.status[0] |= IEC958_AES0_NONAUDIO;
     71    else
     72        iec958.status[0] &= ~IEC958_AES0_NONAUDIO;
     73
     74    snd_ctl_elem_value_set_iec958(cval, &iec958);
     75    snd_ctl_elem_write(ctl, cval);
     76
     77}
     78
    3779bool AudioOutputALSA::OpenDevice()
    3880{
    3981    snd_pcm_format_t format;
    4082    unsigned int buffer_time, period_time;
    4183    int err;
     84    QString real_device;
    4285
    4386    if (pcm_handle != NULL)
    4487        CloseDevice();
    4588
    4689    pcm_handle = NULL;
    4790    numbadioctls = 0;
     91   
     92    if (audio_passthru || audio_enc)
     93    {
     94        real_device = audio_passthru_device;
     95        SetIECBits(false);
     96    }
     97    else
     98    {
     99        real_device = audio_main_device;
     100        SetIECBits(true);
     101    }
    48102
    49     QString real_device = (audio_passthru) ?
    50         audio_passthru_device : audio_main_device;
    51 
    52103    VERBOSE(VB_GENERAL, QString("Opening ALSA audio device '%1'.")
    53104            .arg(real_device));
    54105
  • 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
    33     audio_codec(NULL),
    3434    source(settings.source),    killaudio(false),
    3535
    3636    pauseaudio(false),          audio_actually_paused(false),
     
    4848    encoder(NULL),
    4949    upmixer(NULL),
    5050    source_audio_channels(-1),
     51    source_audio_samplerate(0),
    5152    source_audio_bytes_per_sample(0),
    5253    needs_upmix(false),
    5354    surround_mode(FreeSurround::SurroundModePassive),
     55    old_audio_stretchfactor(1.0),
    5456
    5557    blocking(false),
    5658
     
    124126            VERBOSE(VB_GENERAL, LOC + QString("Using time stretch %1")
    125127                                        .arg(audio_stretchfactor));
    126128            pSoundStretch = new soundtouch::SoundTouch();
    127             if (audio_codec)
    128             {
    129                 if (!encoder)
    130                 {
    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));
     129            pSoundStretch->setSampleRate(audio_samplerate);
     130            pSoundStretch->setChannels(upmixer ?
     131                configured_audio_channels : source_audio_channels);
    135132
    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                                 ))
    142                     {
    143                         // eeks
    144                         delete encoder;
    145                         encoder = NULL;
    146                         VERBOSE(VB_AUDIO, LOC +
    147                                 QString("Failed to Create Encoder"));
    148                     }
    149                 }
    150             }
    151             if (audio_codec && encoder)
    152             {
    153                 pSoundStretch->setSampleRate(audio_codec->sample_rate);
    154                 pSoundStretch->setChannels(audio_codec->channels);
    155             }
    156             else
    157             {
    158                 pSoundStretch->setSampleRate(audio_samplerate);
    159                 pSoundStretch->setChannels(audio_channels);
    160             }
    161 
    162133            pSoundStretch->setTempo(audio_stretchfactor);
    163134            pSoundStretch->setSetting(SETTING_SEQUENCE_MS, 35);
    164135
    165136            // dont need these with only tempo change
    166137            //pSoundStretch->setPitch(1.0);
    167138            //pSoundStretch->setRate(1.0);
    168 
    169139            //pSoundStretch->setSetting(SETTING_USE_QUICKSEEK, true);
    170140            //pSoundStretch->setSetting(SETTING_USE_AA_FILTER, false);
    171141        }
     
    183153    return audio_stretchfactor;
    184154}
    185155
     156bool AudioOutputBase::ToggleUpmix(void)
     157{
     158    if (audio_passthru)
     159        return false;
     160    if (configured_audio_channels == 6)
     161        configured_audio_channels = 2;
     162    else
     163        configured_audio_channels = 6;
     164
     165    const AudioSettings settings(audio_bits, source_audio_channels,
     166                            source_audio_samplerate, audio_passthru);
     167    Reconfigure(settings);
     168    return (configured_audio_channels == 6);
     169}
     170
     171
    186172void AudioOutputBase::Reconfigure(const AudioSettings &orig_settings)
    187173{
    188174    AudioSettings settings = orig_settings;
    189175
    190     int codec_id = CODEC_ID_NONE;
    191     int lcodec_id = CODEC_ID_NONE;
    192     int lcchannels = 0;
    193     int cchannels = 0;
    194176    int lsource_audio_channels = settings.channels;
    195177    bool lneeds_upmix = false;
     178    bool laudio_reenc = false;
    196179
    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     }
     180    // Are we reencoding a (previously) timestretched bitstream?
     181    if (settings.channels > 2 && !settings.use_passthru)
     182        laudio_reenc = true;
    206183
    207     if (audio_codec)
     184    // Enough channels? Upmix if not
     185    if (settings.channels < configured_audio_channels &&
     186        !settings.use_passthru)
    208187    {
    209         codec_id = audio_codec->codec_id;
    210         cchannels = ((AVCodecContext*)audio_codec)->channels;
    211     }
    212 
    213     if ((configured_audio_channels == 6) &&
    214         !(settings.codec || audio_codec))
    215     {
    216188        settings.channels = configured_audio_channels;
    217189        lneeds_upmix = true;
    218190        VERBOSE(VB_AUDIO,LOC + "Needs upmix");
     
    224196        settings.samplerate == audio_samplerate && !need_resampler &&
    225197        settings.use_passthru == audio_passthru &&
    226198        lneeds_upmix == needs_upmix &&
    227         lcodec_id == codec_id && lcchannels == cchannels);
     199        laudio_reenc == audio_reenc);
    228200    bool upmix_deps =
    229201        (lsource_audio_channels == source_audio_channels);
    230202    if (general_deps && upmix_deps)
     
    251223    waud = raud = 0;
    252224    audio_actually_paused = false;
    253225
    254     bool redo_stretch = (pSoundStretch && audio_channels != settings.channels);
    255226    audio_channels = settings.channels;
    256227    source_audio_channels = lsource_audio_channels;
    257228    audio_bits = settings.bits;
    258     audio_samplerate = settings.samplerate;
    259     audio_codec = (AVCodecContext*)settings.codec;
     229    source_audio_samplerate = audio_samplerate = settings.samplerate;
     230    audio_reenc = laudio_reenc;
    260231    audio_passthru = settings.use_passthru;
    261232    needs_upmix = lneeds_upmix;
    262233
     
    265236        Error("AudioOutput only supports 8 or 16bit audio.");
    266237        return;
    267238    }
    268     audio_bytes_per_sample = audio_channels * audio_bits / 8;
    269     source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
    270239
    271240    need_resampler = false;
    272241    killaudio = false;
     
    275244    internal_vol = gContext->GetNumSetting("MythControlsVolume", 0);
    276245
    277246    numlowbuffer = 0;
     247   
     248    // Encode to AC-3 if not passing thru , there's > 2 channels
     249    // and a passthru device is defined
     250    if (!audio_passthru && audio_channels > 2 &&
     251        !audio_passthru_device.isEmpty())
     252    {
     253        VERBOSE(VB_AUDIO, LOC + "Creating AC-3 Encoder");
     254        encoder = new AudioOutputDigitalEncoder();
     255        if (!encoder->Init(CODEC_ID_AC3, 448000, 48000,
     256                           configured_audio_channels, audio_reenc))
     257        {
     258            VERBOSE(VB_AUDIO, LOC + "Can't create AC-3 encoder");
     259            delete encoder;
     260            encoder = NULL;
     261        }
     262        audio_channels = 2;
     263        audio_enc = true;
     264    }       
     265   
     266    if(audio_passthru)
     267        // Soundcard expects a 2ch 48k stream
     268        audio_channels = 2;
     269   
     270    audio_bytes_per_sample = audio_channels * audio_bits / 8;
     271    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
    278272
     273    // Always resample to 48k - many cards can't do anything else
     274    // and ALSA will do it with linear interpolation (yuk) if we don't anyway
     275    if (audio_samplerate != 48000)
     276    {
     277        int error;
     278        audio_samplerate = 48000;
     279        VERBOSE(VB_GENERAL, LOC + QString("Using resampler. From: %1 to %2")
     280            .arg(settings.samplerate).arg(audio_samplerate));
     281        src_ctx = src_new (SRC_SINC_BEST_QUALITY, audio_channels, &error);
     282        if (error)
     283        {
     284            Error(QString("Error creating resampler, the error was: %1")
     285                  .arg(src_strerror(error)) );
     286            src_ctx = NULL;
     287            return;
     288        }
     289        src_data.src_ratio = (double) audio_samplerate / settings.samplerate;
     290        src_data.data_in = src_in;
     291        src_data.data_out = src_out;
     292        src_data.output_frames = 16384*6;
     293        need_resampler = true;
     294    }
     295   
    279296    VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2(%3) sr %4")
    280297            .arg(audio_main_device).arg(audio_channels)
    281298            .arg(source_audio_channels).arg(audio_samplerate));
     
    309326    current_seconds = -1;
    310327    source_bitrate = -1;
    311328
    312     // NOTE: this won't do anything as above samplerate vars are set equal
    313     // Check if we need the resampler
    314     if (audio_samplerate != settings.samplerate)
    315     {
    316         int error;
    317         VERBOSE(VB_GENERAL, LOC + QString("Using resampler. From: %1 to %2")
    318                                .arg(settings.samplerate).arg(audio_samplerate));
    319         src_ctx = src_new (SRC_SINC_BEST_QUALITY, audio_channels, &error);
    320         if (error)
    321         {
    322             Error(QString("Error creating resampler, the error was: %1")
    323                   .arg(src_strerror(error)) );
    324             return;
    325         }
    326         src_data.src_ratio = (double) audio_samplerate / settings.samplerate;
    327         src_data.data_in = src_in;
    328         src_data.data_out = src_out;
    329         src_data.output_frames = 16384*6;
    330         need_resampler = true;
    331     }
    332 
    333329    if (needs_upmix)
    334330    {
    335331        VERBOSE(VB_AUDIO, LOC + QString("create upmixer"));
     
    344340            (FreeSurround::SurroundMode)surround_mode);
    345341
    346342        VERBOSE(VB_AUDIO, LOC +
    347                 QString("create upmixer done with surround mode %1")
     343                QString("Create upmixer done with surround mode %1")
    348344                .arg(surround_mode));
    349345    }
    350346
    351347    VERBOSE(VB_AUDIO, LOC + QString("Audio Stretch Factor: %1")
    352348            .arg(audio_stretchfactor));
    353     VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1")
    354             .arg((audio_codec) ?
    355                  codec_id_string(audio_codec->codec_id) : "not set"));
    356349
    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)
    367         {
    368             // if its passthru then we need to reencode
    369             if (audio_codec)
    370             {
    371                 if (!encoder)
    372                 {
    373                     VERBOSE(VB_AUDIO, LOC +
    374                             QString("Creating Encoder for codec %1")
    375                             .arg(audio_codec->codec_id));
    376 
    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");
    388                     }
    389                 }
    390             }
    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             }
    401         }
    402     }
    403 
     350    SetStretchFactorLocked(old_audio_stretchfactor);
     351   
    404352    // Setup visualisations, zero the visualisations buffers
    405353    prepareVisuals();
    406354
     
    436384    VERBOSE(VB_AUDIO, LOC + "Killing AudioOutputDSP");
    437385    killaudio = true;
    438386    StopOutputThread();
     387    QMutexLocker lock1(&audio_buflock);
    439388
    440389    // Close resampler?
    441390    if (src_ctx)
     391    {
    442392        src_delete(src_ctx);
     393        src_ctx = NULL;
     394    }
     395               
    443396    need_resampler = false;
    444397
    445398    // close sound stretcher
     
    447400    {
    448401        delete pSoundStretch;
    449402        pSoundStretch = NULL;
     403        old_audio_stretchfactor = audio_stretchfactor;
     404        audio_stretchfactor = 1.0;
    450405    }
    451406
    452407    if (encoder)
     
    461416        upmixer = NULL;
    462417    }
    463418    needs_upmix = false;
     419    audio_enc = false;
    464420
    465421    CloseDevice();
    466422
     
    562518    ret += (now.tv_usec - audiotime_updated.tv_usec) / 1000;
    563519    ret = (long long)(ret * audio_stretchfactor);
    564520
    565 #if 1
    566521    VERBOSE(VB_AUDIO+VB_TIMESTAMP,
    567522            QString("GetAudiotime now=%1.%2, set=%3.%4, ret=%5, audt=%6 sf=%7")
    568523            .arg(now.tv_sec).arg(now.tv_usec)
     
    571526            .arg(audiotime)
    572527            .arg(audio_stretchfactor)
    573528           );
    574 #endif
    575529
    576530    ret += audiotime;
    577531
     
    611565
    612566    // include algorithmic latencies
    613567    if (pSoundStretch)
    614     {
    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);
    619         // add the effect of unprocessed samples in time stretch algo
    620568        totalbuffer += (int)((pSoundStretch->numUnprocessedSamples() *
    621569                              audio_bytes_per_sample) / audio_stretchfactor);
    622     }
    623570
    624571    if (upmixer && needs_upmix)
    625     {
    626572        totalbuffer += upmixer->sampleLatency() * audio_bytes_per_sample;
    627     }
    628573
     574    if (encoder)
     575         totalbuffer += encoder->Buffered();
     576
    629577    audiotime = audbuf_timecode - (int)(totalbuffer * 100000.0 /
    630578                                   (audio_bytes_per_sample * effdspstretched));
    631579
    632580    gettimeofday(&audiotime_updated, NULL);
    633 #if 1
     581   
    634582    VERBOSE(VB_AUDIO+VB_TIMESTAMP,
    635583            QString("SetAudiotime set=%1.%2, audt=%3 atc=%4 "
    636584                    "tb=%5 sb=%6 eds=%7 abps=%8 sf=%9")
     
    642590            .arg(effdspstretched)
    643591            .arg(audio_bytes_per_sample)
    644592            .arg(audio_stretchfactor));
    645 #endif
    646593}
    647594
    648595int AudioOutputBase::GetAudioBufferedTime(void)
     
    681628        return false; // would overflow
    682629    }
    683630
     631    QMutexLocker lock1(&audio_buflock);
     632
    684633    // resample input if necessary
    685634    if (need_resampler && src_ctx)
    686635    {
     
    749698        return false; // would overflow
    750699    }
    751700
     701    QMutexLocker lock1(&audio_buflock);
     702
    752703    // resample input if necessary
    753704    if (need_resampler && src_ctx)
    754705    {
     
    808759            if (src_ctx)
    809760            {
    810761                int error = src_reset(src_ctx);
    811                 if (error)
     762                if (error)
     763                {
    812764                    VERBOSE(VB_IMPORTANT, LOC_ERR + QString(
    813765                            "Error occured while resetting resampler: %1")
    814766                            .arg(src_strerror(error)));
     767                    src_ctx = NULL;
     768                }
    815769            }
    816770        }
    817771    }
     
    821775void AudioOutputBase::_AddSamples(void *buffer, bool interleaved, int samples,
    822776                                  long long timecode)
    823777{
    824     audio_buflock.lock();
    825 
    826778    int len; // = samples * audio_bytes_per_sample;
    827779    int audio_bytes = audio_bits / 8;
    828780    int org_waud = waud;
     
    843795    if (upmixer && needs_upmix)
    844796    {
    845797        int out_samples = 0;
     798        org_waud = waud;
    846799        int step = (interleaved)?source_audio_channels:1;
    847800        len = WaitForFreeSpace(samples);    // test
     801       
    848802        for (int itemp = 0; itemp < samples; )
    849803        {
    850             // just in case it does a processing cycle, release the lock
    851             // to allow the output loop to do output
    852             audio_buflock.unlock();
    853804            if (audio_bytes == 2)
    854805            {
    855806                itemp += upmixer->putSamples(
     
    866817                    source_audio_channels,
    867818                    (interleaved) ? 0 : samples);
    868819            }
    869             audio_buflock.lock();
    870820
    871821            int copy_samples = upmixer->numSamples();
    872822            if (copy_samples)
     
    940890    {
    941891        if (pSoundStretch)
    942892        {
    943 
    944893            // does not change the timecode, only the number of samples
    945894            // back to orig pos
    946895            org_waud = waud;
     
    961910                                          len / abps);
    962911            }
    963912
    964             if (encoder)
     913            int nSamples = pSoundStretch->numSamples();
     914            len = WaitForFreeSpace(nSamples);
     915           
     916            while ((nSamples = pSoundStretch->numSamples()))
    965917            {
    966                 // pull out a packet's worth and reencode it until we
    967                 // don't have enough for any more packets
    968                 soundtouch::SAMPLETYPE *temp_buff =
    969                     (soundtouch::SAMPLETYPE*)encoder->GetFrameBuffer();
    970                 size_t frameSize = encoder->FrameSize()/abps;
    971 
    972                 VERBOSE(VB_AUDIO+VB_TIMESTAMP,
    973                         QString("_AddSamples Enc sfs=%1 bfs=%2 sss=%3")
    974                         .arg(frameSize)
    975                         .arg(encoder->FrameSize())
    976                         .arg(pSoundStretch->numSamples()));
    977 
    978                 // process the same number of samples as it creates
    979                 // a full encoded buffer just like before
    980                 while (pSoundStretch->numSamples() >= frameSize)
    981                 {
    982                     int got = pSoundStretch->receiveSamples(
    983                         temp_buff, frameSize);
    984                     int amount = encoder->Encode(temp_buff);
    985 
    986                     VERBOSE(VB_AUDIO+VB_TIMESTAMP,
    987                             QString("_AddSamples Enc bytes=%1 got=%2 left=%3")
    988                             .arg(amount)
    989                             .arg(got)
    990                             .arg(pSoundStretch->numSamples()));
    991 
    992                     if (!amount)
    993                         continue;
    994 
    995                     //len = WaitForFreeSpace(amount);
    996                     char *ob = encoder->GetOutBuff();
    997                     if (amount >= bdiff)
    998                     {
    999                         memcpy(audiobuffer + org_waud, ob, bdiff);
    1000                         ob += bdiff;
    1001                         amount -= bdiff;
    1002                         org_waud = 0;
    1003                     }
    1004                     if (amount > 0)
    1005                         memcpy(audiobuffer + org_waud, ob, amount);
    1006 
    1007                     bdiff = kAudioRingBufferSize - amount;
    1008                     org_waud = (org_waud + amount) % kAudioRingBufferSize;
     918                if (nSamples > nSamplesToEnd)
     919                    nSamples = nSamplesToEnd;
     920               
     921                nSamples = pSoundStretch->receiveSamples(
     922                    (soundtouch::SAMPLETYPE*)
     923                    (audiobuffer + org_waud), nSamples
     924                );
     925               
     926                if (nSamples == nSamplesToEnd) {
     927                    org_waud = 0;
     928                    nSamplesToEnd = kAudioRingBufferSize/abps;
    1009929                }
     930                else {
     931                    org_waud += nSamples * abps;
     932                    nSamplesToEnd -= nSamples;
     933                }
     934               
    1010935            }
    1011             else
     936           
     937        }
     938 
     939        // Encode to AC-3?
     940        if (encoder)
     941        {
     942           
     943            org_waud = waud;
     944            int bdiff = kAudioRingBufferSize - org_waud;
     945            int to_get = 0;
     946 
     947            if (bdiff < len)
    1012948            {
    1013                 int newLen = 0;
    1014                 int nSamples;
    1015                 len = WaitForFreeSpace(pSoundStretch->numSamples() *
    1016                                        audio_bytes_per_sample);
    1017                 do
     949                encoder->Encode(audiobuffer + org_waud, bdiff);
     950                to_get = encoder->Encode(audiobuffer, len - bdiff);
     951            }
     952            else
     953                to_get = encoder->Encode(audiobuffer + org_waud, len);
     954           
     955            if (to_get > 0)
     956            {
     957               
     958                if (to_get >= bdiff)
    1018959                {
    1019                     int samplesToGet = len/audio_bytes_per_sample;
    1020                     if (samplesToGet > nSamplesToEnd)
    1021                     {
    1022                         samplesToGet = nSamplesToEnd;
    1023                     }
    1024 
    1025                     nSamples = pSoundStretch->receiveSamples(
    1026                         (soundtouch::SAMPLETYPE*)
    1027                         (audiobuffer + org_waud), samplesToGet);
    1028                     if (nSamples == nSamplesToEnd)
    1029                     {
    1030                         org_waud = 0;
    1031                         nSamplesToEnd = kAudioRingBufferSize/audio_bytes_per_sample;
    1032                     }
    1033                     else
    1034                     {
    1035                         int bufsz = nSamples * audio_bytes_per_sample;
    1036                         org_waud = (org_waud + bufsz) % kAudioRingBufferSize;
    1037                         nSamplesToEnd -= nSamples;
    1038                     }
    1039 
    1040                     newLen += nSamples * audio_bytes_per_sample;
    1041                     len -= nSamples * audio_bytes_per_sample;
    1042                 } while (nSamples > 0);
     960                    encoder->GetFrames(audiobuffer + org_waud, bdiff);
     961                    to_get -= bdiff;
     962                    org_waud = 0;
     963                }
     964                if (to_get > 0)
     965                    encoder->GetFrames(audiobuffer + org_waud, to_get);
     966 
     967                org_waud += to_get;
     968 
    1043969            }
     970
    1044971        }
    1045972
    1046973        waud = org_waud;
    1047974        lastaudiolen = audiolen(false);
    1048975
    1049976        if (timecode < 0)
    1050         {
    1051977            // mythmusic doesn't give timestamps..
    1052978            timecode = (int)((samples_buffered * 100000.0) / effdsp);
    1053         }
    1054979 
    1055980        samples_buffered += samples;
    1056981 
     
    1061986        audbuf_timecode = timecode + (int)((samples * 100000.0) / effdsp);
    1062987
    1063988        if (interleaved)
    1064         {
     989            // Give original samples to mythmusic visualisation
    1065990            dispatchVisual((unsigned char *)buffer, len, timecode,
    1066991                           source_audio_channels, audio_bits);
    1067         }
    1068992    }
    1069993
    1070     audio_buflock.unlock();
    1071994}
    1072995
    1073996void AudioOutputBase::Status()
  • libs/libmyth/audiosettings.h

     
    3232        int               audio_samplerate,
    3333        AudioOutputSource audio_source,
    3434        bool              audio_set_initial_vol,
    35         bool              audio_use_passthru,
    36         void             *audio_codec = NULL);
     35        bool              audio_use_passthru);
    3736
    3837    AudioSettings(int   audio_bits,
    3938                  int   audio_channels,
    4039                  int   audio_samplerate,
    41                   bool  audio_use_passthru,
    42                   void *audio_codec = NULL);
     40                  bool  audio_use_passthru);
    4341
    4442    void FixPassThrough(void);
    4543    void TrimDeviceType(void);
     
    5755    int     samplerate;
    5856    bool    set_initial_vol;
    5957    bool    use_passthru;
    60     void   *codec;
    6158    AudioOutputSource source;
    6259};
    6360
  • libs/libmyth/audiooutputdigitalencoder.cpp

     
    2929AudioOutputDigitalEncoder::AudioOutputDigitalEncoder(void) :
    3030    audio_bytes_per_sample(0),
    3131    av_context(NULL),
    32     outbuf(NULL),
    33     outbuf_size(0),
    34     frame_buffer(NULL),
    35     one_frame_bytes(0)
     32    outbuflen(0),
     33    inbuflen(0),
     34    one_frame_bytes(0),
     35    reorder(true)
    3636{
    3737}
    3838
     
    4949        av_free(av_context);
    5050        av_context = NULL;
    5151    }
    52 
    53     if (outbuf)
    54     {
    55         delete [] outbuf;
    56         outbuf = NULL;
    57         outbuf_size = 0;
    58     }
    59 
    60     if (frame_buffer)
    61     {
    62         delete [] frame_buffer;
    63         frame_buffer = NULL;
    64         one_frame_bytes = 0;
    65     }
    6652}
    6753
    6854//CODEC_ID_AC3
    6955bool AudioOutputDigitalEncoder::Init(
    70     CodecID codec_id, int bitrate, int samplerate, int channels)
     56    CodecID codec_id, int bitrate, int samplerate, int channels, bool reencoding)
    7157{
    7258    AVCodec *codec;
    7359    int ret;
    7460
    75     VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4")
     61    VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4 re=%5")
    7662            .arg(codec_id_string(codec_id))
    7763            .arg(bitrate)
    7864            .arg(samplerate)
    79             .arg(channels));
     65            .arg(channels)
     66            .arg(reencoding));
     67   
     68    reorder = !reencoding;
    8069
    81     //codec = avcodec_find_encoder(codec_id);
     70    // We need to do this when called from mythmusic
     71    avcodec_init();
     72    avcodec_register_all();
    8273    // always AC3 as there is no DTS encoder at the moment 2005/1/9
    8374    codec = avcodec_find_encoder(CODEC_ID_AC3);
    8475    if (!codec)
     
    10798    audio_bytes_per_sample = bytes_per_frame;
    10899    one_frame_bytes = bytes_per_frame * av_context->frame_size;
    109100
    110     outbuf_size = 16384;    // ok for AC3 but DTS?
    111     outbuf = new char [outbuf_size];
    112101    VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init fs=%1, bpf=%2 ofb=%3")
    113102            .arg(av_context->frame_size)
    114103            .arg(bytes_per_frame)
     
    253242
    254243} AESHeader;
    255244
     245void reorder_6ch_ac3(void *buf, unsigned int len) {
     246    unsigned short *src = (unsigned short *)buf;
     247    unsigned short tmp;
     248    unsigned int samples = len >> 1;
     249
     250    for (uint i = 0; i < samples; i += 6) {
     251        tmp = src[i+4];
     252        src[i+4] = src[i+3];
     253        src[i+3] = src[i+2];
     254        src[i+2] = src[i+1];
     255        src[i+1] = tmp;
     256    }
     257}
     258
    256259static int encode_frame(
    257260        bool dts,
    258261        unsigned char *data,
    259         size_t &len)
     262        size_t enc_len)
    260263{
    261264    unsigned char *payload = data + 8;  // skip header, currently 52 or 54bits
    262     size_t         enc_len;
    263265    int            flags, sample_rate, bit_rate;
    264266
    265267    // we don't do any length/crc validation of the AC3 frame here; presumably
     
    270272    // ignore, and if so, may as well just assume that it will ignore
    271273    // anything with a bad CRC...
    272274
    273     uint nr_samples = 0, block_len;
     275    uint nr_samples = 0, block_len = 0;
     276   
    274277    if (dts)
    275278    {
    276279        enc_len = dts_syncinfo(payload, &flags, &sample_rate, &bit_rate);
     
    305308#endif
    306309    }
    307310
    308     if (enc_len == 0 || enc_len > len)
    309     {
    310         int l = len;
    311         len = 0;
    312         return l;
    313     }
    314 
    315311    enc_len = std::min((uint)enc_len, block_len - 8);
    316312
    317313    //uint32_t x = *(uint32_t*)payload;
     
    364360    data[6] = (enc_len << 3) & 0xFF;
    365361    data[7] = (enc_len >> 5) & 0xFF;
    366362    memset(payload + enc_len, 0, block_len - 8 - enc_len);
    367     len = block_len;
    368363
    369364    return enc_len;
    370365}
    371366
    372367// must have exactly 1 frames worth of data
    373 size_t AudioOutputDigitalEncoder::Encode(short *buff)
     368size_t AudioOutputDigitalEncoder::Encode(void *buf, int len)
    374369{
    375     int encsize = 0;
    376370    size_t outsize = 0;
    377371 
    378     // put data in the correct spot for encode frame
    379     outsize = avcodec_encode_audio(
    380         av_context, ((uchar*)outbuf) + 8, outbuf_size - 8, buff);
     372    int fs = FrameSize();
     373    memcpy(inbuf+inbuflen, buf, len);
     374    inbuflen += len;
     375    int frames = inbuflen / fs;
    381376
    382     size_t tmpsize = outsize;
     377    while (frames--)
     378    {
     379        if (reorder)
     380            reorder_6ch_ac3(inbuf, fs);
     381       
     382        // put data in the correct spot for encode frame
     383        outsize = avcodec_encode_audio(
     384            av_context, ((uchar*)outbuf) + outbuflen + 8, OUTBUFSIZE - 8, (short int *)inbuf);
     385       
     386        encode_frame(
     387            /*av_context->codec_id==CODEC_ID_DTS*/ false,
     388            (unsigned char*)outbuf + outbuflen, outsize
     389        );
    383390
    384     outsize = MAX_AC3_FRAME_SIZE;
    385     encsize = encode_frame(
    386         /*av_context->codec_id==CODEC_ID_DTS*/ false,
    387         (unsigned char*)outbuf, outsize);
     391        outbuflen += MAX_AC3_FRAME_SIZE;
     392        inbuflen -= fs;
     393        memmove(inbuf, inbuf+fs, inbuflen);
     394    }
     395 
     396    return outbuflen;
     397}
    388398
    389     VERBOSE(VB_AUDIO+VB_TIMESTAMP,
    390             QString("DigitalEncoder::Encode len1=%1 len2=%2 finallen=%3")
    391                 .arg(tmpsize).arg(encsize).arg(outsize));
    392 
    393     return outsize;
     399void AudioOutputDigitalEncoder::GetFrames(void *ptr, int maxlen)
     400{
     401    int len = (maxlen < outbuflen ? maxlen : outbuflen);
     402    memcpy(ptr, outbuf, len);
     403    outbuflen -= len;
     404    memmove(outbuf, outbuf+len, outbuflen);
    394405}
  • libs/libmyth/audiooutput.h

     
    6868   
    6969    virtual void bufferOutputData(bool y) = 0;
    7070    virtual int readOutputData(unsigned char *read_buffer, int max_length) = 0;
     71    virtual bool ToggleUpmix(void) = 0;
    7172
    7273  protected:
    7374    void Error(const QString &msg);
  • 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);
    1215    ~AudioOutputDigitalEncoder();
    1316
    14     bool   Init(CodecID codec_id, int bitrate, int samplerate, int channels);
     17    bool   Init(CodecID codec_id, int bitrate, int samplerate,
     18                int channels, bool reencoding);
    1519    void   Dispose(void);
    16     size_t Encode(short * buff);
    17 
    18     inline char *GetFrameBuffer(void);
     20    size_t Encode(void *buf, int len);
     21    void   GetFrames(void *ptr, int maxlen);
    1922    size_t FrameSize(void)  const { return one_frame_bytes; }
    20     char  *GetOutBuff(void) const { return outbuf;          }
     23    int    Buffered(void) const { return inbuflen; }
    2124
    2225  public:
    2326    size_t audio_bytes_per_sample;
    2427
    2528  private:
    2629    AVCodecContext *av_context;
    27     char           *outbuf;
    28     int             outbuf_size;
    29     char           *frame_buffer;
     30    char            outbuf[OUTBUFSIZE];
     31    char            inbuf[INBUFSIZE];
     32    int             outbuflen;
     33    int             inbuflen;
    3034    size_t          one_frame_bytes;
     35    bool            reorder;
    3136};
    3237
    33 inline char *AudioOutputDigitalEncoder::GetFrameBuffer(void)
    34 {
    35     if (!frame_buffer && av_context)
    36         frame_buffer = new char [one_frame_bytes];
    37 
    38     return frame_buffer;
    39 }
    40 
    4138#endif
  • libs/libmythfreesurround/el_processor.cpp

     
    192192    // set lfe filter params
    193193    void sample_rate(unsigned int srate) {
    194194        // lfe filter is just straight through band limited
    195         unsigned int cutoff = (250*N)/srate;
     195        unsigned int cutoff = (50*N)/srate;
    196196        for (unsigned f=0;f<=halfN;f++) {           
    197197            if ((f>=2) && (f<cutoff))
    198198                filter[5][f] = 1.0;
  • 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
    68 //const float master_gain = 1.0/(1<<15);
    69 //const float inv_master_gain = (1<<15);
     68//const float inv_master_gain = 1.0;
    7069//#define INV_MASTER_GAIN * inv_master_gain
    7170#define INV_MASTER_GAIN
    7271
     
    192191    if (moviemode)
    193192    {
    194193        params.phasemode = 1;
    195         params.center_width = 0;
    196         params.gain = 1.0;
     194        params.center_width = 40;
     195        params.gain = 3.0;
     196        params.dimension = 0.5;
    197197    }
    198198    else
    199199    {
    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
     200        params.center_width = 80;
    203201        params.gain = 3.1;
     202        params.dimension = 0.3;
    204203    }
    205204    switch (surround_mode)
    206205    {
  • programs/mythtranscode/transcode.cpp

     
    216216        // Do nothing
    217217        return kMuteOff;
    218218    }
     219    virtual bool ToggleUpmix(void)
     220    {
     221        // Do nothing
     222        return false;
     223    }
    219224
    220225    //  These are pure virtual in AudioOutput, but we don't need them here
    221226    virtual void bufferOutputData(bool){ return; }