Ticket #5900: audioencoding-fixes.patch

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

     
    209209      audio_passthru_device(QString::null),
    210210      audio_channels(2),            audio_bits(-1),
    211211      audio_samplerate(44100),      audio_stretchfactor(1.0f),
    212       audio_codec(NULL),
    213212      // Picture-in-Picture
    214213      pipplayer(NULL), setpipplayer(NULL), needsetpipplayer(false),
    215214      // Preview window support
     
    804803    if (audioOutput)
    805804    {
    806805        audioOutput->Reconfigure(audio_bits, audio_channels,
    807                                  audio_samplerate, audio_passthru,
    808                                  audio_codec);
     806                                 audio_samplerate, audio_passthru);
     807        if (audio_passthru)
     808            audio_channels = 2;
    809809        errMsg = audioOutput->GetError();
    810810        if (!errMsg.isEmpty())
    811811            audioOutput->SetStretchFactor(audio_stretchfactor);
     
    37183718    audio_passthru = passthru;
    37193719}
    37203720
    3721 void NuppelVideoPlayer::SetAudioCodec(void *ac)
    3722 {
    3723     audio_codec = ac;
    3724 }
    3725 
    37263721void NuppelVideoPlayer::SetEffDsp(int dsprate)
    37273722{
    37283723    if (audioOutput)
  • libs/libmythtv/avformatdecoder.cpp

     
    411411      audioSamples(new short int[AVCODEC_MAX_AUDIO_FRAME_SIZE]),
    412412      allow_ac3_passthru(false),    allow_dts_passthru(false),
    413413      disable_passthru(false),      max_channels(2),
    414       dummy_frame(NULL),
     414      last_ac3_channels(0),         dummy_frame(NULL),
    415415      // DVD
    416416      lastdvdtitle(-1),
    417417      decodeStillFrame(false),
     
    29712971bool AvFormatDecoder::GetFrame(int onlyvideo)
    29722972{
    29732973    AVPacket *pkt = NULL;
     2974    AC3HeaderInfo hdr;
    29742975    int len;
    29752976    unsigned char *ptr;
    29762977    int data_size = 0;
     
    31573158        pts = 0;
    31583159
    31593160        AVStream *curstream = ic->streams[pkt->stream_index];
     3161        AVCodecContext *ctx = curstream->codec;
    31603162
    31613163        if (pkt->dts != (int64_t)AV_NOPTS_VALUE)
    31623164            pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
    31633165
    31643166        if (ringBuffer->isDVD() &&
    3165             curstream->codec->codec_type == CODEC_TYPE_VIDEO)
     3167            ctx->codec_type == CODEC_TYPE_VIDEO)
    31663168        {
    31673169            MpegPreProcessPkt(curstream, pkt);
    31683170
     
    31903192
    31913193            if (!d->HasMPEG2Dec())
    31923194            {
    3193                 int current_width = curstream->codec->width;
     3195                int current_width = ctx->width;
    31943196                int video_width = GetNVP()->GetVideoSize().width();
    31953197                if (dvd_xvmc_enabled && GetNVP() && GetNVP()->getVideoOutput())
    31963198                {
     
    32313233        }
    32323234
    32333235        if (storevideoframes &&
    3234             curstream->codec->codec_type == CODEC_TYPE_VIDEO)
     3236            ctx->codec_type == CODEC_TYPE_VIDEO)
    32353237        {
    32363238            av_dup_packet(pkt);
    32373239            storedPackets.append(pkt);
     
    32393241            continue;
    32403242        }
    32413243
    3242         if (len > 0 && curstream->codec->codec_type == CODEC_TYPE_VIDEO &&
     3244        if (len > 0 && ctx->codec_type == CODEC_TYPE_VIDEO &&
    32433245            pkt->stream_index == selectedVideoIndex)
    32443246        {
    3245             AVCodecContext *context = curstream->codec;
    32463247
    3247             if (context->codec_id == CODEC_ID_MPEG1VIDEO ||
    3248                 context->codec_id == CODEC_ID_MPEG2VIDEO ||
    3249                 context->codec_id == CODEC_ID_MPEG2VIDEO_XVMC ||
    3250                 context->codec_id == CODEC_ID_MPEG2VIDEO_XVMC_VLD)
     3248            if (ctx->codec_id == CODEC_ID_MPEG1VIDEO ||
     3249                ctx->codec_id == CODEC_ID_MPEG2VIDEO ||
     3250                ctx->codec_id == CODEC_ID_MPEG2VIDEO_XVMC ||
     3251                ctx->codec_id == CODEC_ID_MPEG2VIDEO_XVMC_VLD)
    32513252            {
    32523253                if (!ringBuffer->isDVD())
    32533254                    MpegPreProcessPkt(curstream, pkt);
    32543255            }
    3255             else if (context->codec_id == CODEC_ID_H264)
     3256            else if (ctx->codec_id == CODEC_ID_H264)
    32563257            {
    32573258                H264PreProcessPkt(curstream, pkt);
    32583259            }
     
    32983299        }
    32993300
    33003301        if (len > 0 &&
    3301             curstream->codec->codec_type == CODEC_TYPE_DATA &&
    3302             curstream->codec->codec_id   == CODEC_ID_MPEG2VBI)
     3302            ctx->codec_type == CODEC_TYPE_DATA &&
     3303            ctx->codec_id   == CODEC_ID_MPEG2VBI)
    33033304        {
    33043305            ProcessVBIDataPacket(curstream, pkt);
    33053306
     
    33083309        }
    33093310
    33103311        if (len > 0 &&
    3311             curstream->codec->codec_type == CODEC_TYPE_DATA &&
    3312             curstream->codec->codec_id   == CODEC_ID_DVB_VBI)
     3312            ctx->codec_type == CODEC_TYPE_DATA &&
     3313            ctx->codec_id   == CODEC_ID_DVB_VBI)
    33133314        {
    33143315            ProcessDVBDataPacket(curstream, pkt);
    33153316
     
    33183319        }
    33193320
    33203321        if (len > 0 &&
    3321             curstream->codec->codec_type == CODEC_TYPE_DATA &&
    3322             curstream->codec->codec_id   == CODEC_ID_DSMCC_B)
     3322            ctx->codec_type == CODEC_TYPE_DATA &&
     3323            ctx->codec_id   == CODEC_ID_DSMCC_B)
    33233324        {
    33243325            ProcessDSMCCPacket(curstream, pkt);
    33253326
     
    33393340        }
    33403341
    33413342        // we don't care about other data streams
    3342         if (curstream->codec->codec_type == CODEC_TYPE_DATA)
     3343        if (ctx->codec_type == CODEC_TYPE_DATA)
    33433344        {
    33443345            av_free_packet(pkt);
    33453346            continue;
    33463347        }
    33473348
    3348         if (!curstream->codec->codec)
     3349        if (!ctx->codec)
    33493350        {
    33503351            VERBOSE(VB_PLAYBACK, LOC +
    33513352                    QString("No codec for stream index %1, type(%2) id(%3:%4)")
    33523353                    .arg(pkt->stream_index)
    3353                     .arg(codec_type_string(curstream->codec->codec_type))
    3354                     .arg(codec_id_string(curstream->codec->codec_id))
    3355                     .arg(curstream->codec->codec_id));
     3354                    .arg(codec_type_string(ctx->codec_type))
     3355                    .arg(codec_id_string(ctx->codec_id))
     3356                    .arg(ctx->codec_id));
    33563357            av_free_packet(pkt);
    33573358            continue;
    33583359        }
     
    33613362        have_err = false;
    33623363
    33633364        avcodeclock.lock();
    3364         int ctype  = curstream->codec->codec_type;
     3365        int ctype  = ctx->codec_type;
    33653366        int audIdx = selectedTrack[kTrackTypeAudio].av_stream_index;
    33663367        int audSubIdx = selectedTrack[kTrackTypeAudio].av_substream_index;
    33673368        int subIdx = selectedTrack[kTrackTypeSubtitle].av_stream_index;
     
    33863387
    33873388                    // detect switches between stereo and dual languages
    33883389                    bool wasDual = audSubIdx != -1;
    3389                     bool isDual = curstream->codec->avcodec_dual_language;
     3390                    bool isDual = ctx->avcodec_dual_language;
    33903391                    if ((wasDual && !isDual) || (!wasDual &&  isDual))
    33913392                    {
    33923393                        SetupAudioStreamSubIndexes(audIdx);
     
    33943395                    }                           
    33953396
    33963397                    bool do_ac3_passthru =
    3397                         (allow_ac3_passthru && !transcoding &&
    3398                          (curstream->codec->codec_id == CODEC_ID_AC3));
     3398                        (allow_ac3_passthru && !transcoding &&
     3399                         ctx->channels >= (int)max_channels &&
     3400                         (ctx->codec_id == CODEC_ID_AC3));
    33993401                    bool do_dts_passthru =
    34003402                        (allow_dts_passthru && !transcoding &&
    3401                          (curstream->codec->codec_id == CODEC_ID_DTS));
     3403                         (ctx->codec_id == CODEC_ID_DTS));
    34023404                    bool using_passthru = do_ac3_passthru || do_dts_passthru;
    34033405
    34043406                    // detect channels on streams that need
    34053407                    // to be decoded before we can know this
    34063408                    bool already_decoded = false;
    3407                     if (!curstream->codec->channels)
     3409                    if (!ctx->channels)
    34083410                    {
    34093411                        QMutexLocker locker(&avcodeclock);
    34103412                        VERBOSE(VB_IMPORTANT, LOC +
     
    34153417                        {
    34163418                            // for passthru let it select the max number
    34173419                            // of channels
    3418                             curstream->codec->channels = 0;
    3419                             curstream->codec->request_channels = 0;
     3420                            ctx->channels = 0;
     3421                            ctx->request_channels = 0;
    34203422                        }
    34213423                        else
    34223424                        {
    3423                             curstream->codec->channels = audioOut.channels;
    3424                             curstream->codec->request_channels =
     3425                            ctx->channels = audioOut.channels;
     3426                            ctx->request_channels =
    34253427                                audioOut.channels;
    34263428                        }
    34273429                        ret = avcodec_decode_audio(
    3428                             curstream->codec, audioSamples,
     3430                            ctx, audioSamples,
    34293431                            &data_size, ptr, len);
    34303432                        already_decoded = true;
    34313433
    3432                         reselectAudioTrack |= curstream->codec->channels;
     3434                        reselectAudioTrack |= ctx->channels;
    34333435                    }
    34343436
     3437                    if (ctx->codec_id == CODEC_ID_AC3 &&
     3438                        !ff_ac3_parse_header(ptr, &hdr))
     3439                    {
     3440                        if (hdr.channels != last_ac3_channels)
     3441                        {
     3442                            last_ac3_channels = ctx->channels = hdr.channels;
     3443                            SetupAudioStream();
     3444                        }
     3445                    }
     3446
    34353447                    if (reselectAudioTrack)
    34363448                    {
    34373449                        QMutexLocker locker(&avcodeclock);
     
    34453457                            .av_stream_index;
    34463458                        audSubIdx = selectedTrack[kTrackTypeAudio]
    34473459                            .av_substream_index;
     3460                        ctx = curstream->codec;
    34483461                    }
    34493462
    34503463                    if (firstloop && pkt->pts != (int64_t)AV_NOPTS_VALUE)
     
    34763489                    if (audioOut.do_passthru)
    34773490                    {
    34783491                        data_size = pkt->size;
    3479                         bool dts = CODEC_ID_DTS == curstream->codec->codec_id;
     3492                        bool dts = CODEC_ID_DTS == ctx->codec_id;
    34803493                        ret = encode_frame(dts, ptr, len,
    34813494                                           audioSamples, data_size);
    34823495                    }
    34833496                    else
    34843497                    {
    3485                         AVCodecContext *ctx = curstream->codec;
    3486 
    34873498                        if ((ctx->channels == 0) ||
    34883499                            (ctx->channels > audioOut.channels))
    34893500                        {
     
    34923503
    34933504                        if (!already_decoded)
    34943505                        {
    3495                             curstream->codec->request_channels =
    3496                                 audioOut.channels;
     3506                            ctx->request_channels = audioOut.channels;
    34973507                            ret = avcodec_decode_audio(
    34983508                                ctx, audioSamples, &data_size, ptr, len);
    34993509                        }
     
    35103520                            audIdx = -1;
    35113521                            AutoSelectAudioTrack();
    35123522                            data_size = 0;
     3523                            ctx = curstream->codec;
    35133524                        }
    35143525                    }
    35153526                    avcodeclock.unlock();
     
    35273538
    35283539                    // calc for next frame
    35293540                    lastapts += (long long)((double)(data_size * 1000) /
    3530                                 (curstream->codec->channels * 2) /
    3531                                 curstream->codec->sample_rate);
     3541                                (ctx->channels * 2) / ctx->sample_rate);
    35323542
    35333543                    VERBOSE(VB_PLAYBACK|VB_TIMESTAMP,
    35343544                            LOC + QString("audio timecode %1 %2 %3 %4")
     
    35883598                        continue;
    35893599                    }
    35903600
    3591                     AVCodecContext *context = curstream->codec;
    35923601                    AVFrame mpa_pic;
    35933602                    bzero(&mpa_pic, sizeof(AVFrame));
    35943603
     
    36033612                            // HACK
    36043613                            while (!gotpicture && count < 5)
    36053614                            {
    3606                                 ret = d->DecodeMPEG2Video(context, &mpa_pic,
     3615                                ret = d->DecodeMPEG2Video(ctx, &mpa_pic,
    36073616                                                  &gotpicture, ptr, len);
    36083617                                count++;
    36093618                            }
    36103619                        }
    36113620                        else
    36123621                        {
    3613                             ret = d->DecodeMPEG2Video(context, &mpa_pic,
     3622                            ret = d->DecodeMPEG2Video(ctx, &mpa_pic,
    36143623                                                &gotpicture, ptr, len);
    36153624                        }
    36163625                    }
    36173626                    else
    36183627                    {
    3619                         ret = avcodec_decode_video(context, &mpa_pic,
     3628                        ret = avcodec_decode_video(ctx, &mpa_pic,
    36203629                                                   &gotpicture, ptr, len);
    36213630                        // Reparse it to not drop the DVD still frame
    36223631                        if (decodeStillFrame)
    3623                             ret = avcodec_decode_video(context, &mpa_pic,
     3632                            ret = avcodec_decode_video(ctx, &mpa_pic,
    36243633                                                        &gotpicture, ptr, len);
    36253634                    }
    36263635                    avcodeclock.unlock();
     
    36873696
    36883697                        img_convert(&tmppicture, PIX_FMT_YUV420P,
    36893698                                    (AVPicture *)&mpa_pic,
    3690                                     context->pix_fmt,
    3691                                     context->width,
    3692                                     context->height);
     3699                                    ctx->pix_fmt,
     3700                                    ctx->width,
     3701                                    ctx->height);
    36933702
    36943703                        if (xf)
    36953704                        {
     
    37123721                        (temppts + 10000 > lastvpts || temppts < 0))
    37133722                    {
    37143723                        temppts = lastvpts;
    3715                         temppts += (long long)(1000 * av_q2d(context->time_base));
     3724                        temppts += (long long)(1000 * av_q2d(ctx->time_base));
    37163725                        // MPEG2 frames can be repeated, update pts accordingly
    37173726                        temppts += (long long)(mpa_pic.repeat_pict * 500
    3718                                       * av_q2d(curstream->codec->time_base));
     3727                                      * av_q2d(ctx->time_base));
    37193728                    }
    37203729
    37213730                    VERBOSE(VB_PLAYBACK|VB_TIMESTAMP, LOC +
     
    37513760                    picframe->frameNumber = framesPlayed;
    37523761                    GetNVP()->ReleaseNextVideoFrame(picframe, temppts);
    37533762                    if (d->HasMPEG2Dec() && mpa_pic.data[3])
    3754                         context->release_buffer(context, &mpa_pic);
     3763                        ctx->release_buffer(ctx, &mpa_pic);
    37553764
    37563765                    decoded_video_frame = picframe;
    37573766                    gotvideo = 1;
     
    38103819                }
    38113820                default:
    38123821                {
    3813                     AVCodecContext *enc = curstream->codec;
    38143822                    VERBOSE(VB_IMPORTANT, LOC_ERR +
    38153823                            QString("Decoding - id(%1) type(%2)")
    3816                             .arg(codec_id_string(enc->codec_id))
    3817                             .arg(codec_type_string(enc->codec_type)));
     3824                            .arg(codec_id_string(ctx->codec_id))
     3825                            .arg(codec_type_string(ctx->codec_type)));
    38183826                    have_err = true;
    38193827                    break;
    38203828                }
     
    39723980    AVStream *curstream = NULL;
    39733981    AVCodecContext *codec_ctx = NULL;
    39743982    AudioInfo old_in  = audioIn;
    3975     AudioInfo old_out = audioOut;
    39763983    bool using_passthru = false;
    39773984
    39783985    if ((currentTrack[kTrackTypeAudio] >= 0) &&
     
    39853992        assert(curstream->codec);
    39863993        codec_ctx = curstream->codec;       
    39873994        bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
     3995                                codec_ctx->channels >= (int)max_channels &&
    39883996                                (codec_ctx->codec_id == CODEC_ID_AC3));
    39893997        bool do_dts_passthru = (allow_dts_passthru && !transcoding &&
    39903998                                (codec_ctx->codec_id == CODEC_ID_DTS));
     
    40024010            QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1));
    40034011
    40044012    audioOut = audioIn = info;
    4005     if (using_passthru)
    4006     {
    4007         // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card
    4008         AudioInfo digInfo = audioOut;
    4009         if (!disable_passthru)
    4010         {
    4011             digInfo.channels    = 2;
    4012             digInfo.sample_rate = 48000;
    4013             digInfo.sample_size = 4;
    4014         }
    4015         if (audioOut.channels > (int) max_channels)
    4016         {
    4017             audioOut.channels = (int) max_channels;
    4018             audioOut.sample_size = audioOut.channels * 2;
    4019             codec_ctx->channels = audioOut.channels;
    4020         }
    4021         VERBOSE(VB_AUDIO, LOC + "Audio format changed digital passthrough " +
    4022                 QString("%1\n\t\t\tfrom %2 ; %3\n\t\t\tto   %4 ; %5")
    4023                 .arg(digInfo.toString())
    4024                 .arg(old_in.toString()).arg(old_out.toString())
    4025                 .arg(audioIn.toString()).arg(audioOut.toString()));
    4026 
    4027         if (digInfo.sample_rate > 0)
    4028             GetNVP()->SetEffDsp(digInfo.sample_rate * 100);
    4029 
    4030         GetNVP()->SetAudioParams(digInfo.bps(), digInfo.channels,
    4031                                  digInfo.sample_rate, audioIn.do_passthru);
    4032         // allow the audio stuff to reencode
    4033         GetNVP()->SetAudioCodec(codec_ctx);
    4034         GetNVP()->ReinitAudio();
    4035         return true;
    4036     }
    4037     else
    4038     {
    4039         if (audioOut.channels > (int) max_channels)
    4040         {
    4041             audioOut.channels = (int) max_channels;
    4042             audioOut.sample_size = audioOut.channels * 2;
    4043             codec_ctx->channels = audioOut.channels;
    4044         }
    4045     }
    4046 
     4013   
    40474014    VERBOSE(VB_AUDIO, LOC + "Audio format changed " +
    4048             QString("\n\t\t\tfrom %1 ; %2\n\t\t\tto   %3 ; %4")
    4049             .arg(old_in.toString()).arg(old_out.toString())
    4050             .arg(audioIn.toString()).arg(audioOut.toString()));
     4015            QString("\n\t\t\tfrom %1 to %2")
     4016            .arg(old_in.toString()).arg(audioOut.toString()));
    40514017
    40524018    if (audioOut.sample_rate > 0)
    40534019        GetNVP()->SetEffDsp(audioOut.sample_rate * 100);
    40544020
    4055     GetNVP()->SetAudioParams(audioOut.bps(), audioOut.channels,
     4021    GetNVP()->SetAudioParams(audioOut.bps(), audioIn.channels,
    40564022                             audioOut.sample_rate,
    4057                              audioIn.do_passthru);
     4023                             audioOut.do_passthru);
    40584024
    4059     // allow the audio stuff to reencode
    4060     GetNVP()->SetAudioCodec(using_passthru?codec_ctx:NULL);
    40614025    QString errMsg = GetNVP()->ReinitAudio();
    4062     bool audiook = errMsg.isEmpty();
    40634026
    40644027    return true;
    40654028}
  • libs/libmythtv/tv_play.h

     
    314314    void ChangeSpeed(int direction);
    315315    void ToggleTimeStretch(void);
    316316    void ChangeTimeStretch(int dir, bool allowEdit = true);
     317    void ToggleUpmix(void);
    317318    void ChangeAudioSync(int dir, bool allowEdit = true);
    318319    float StopFFRew(void);
    319320    void ChangeFFRew(int direction);
  • libs/libmythtv/NuppelVideoPlayer.h

     
    685685    int      audio_bits;
    686686    int      audio_samplerate;
    687687    float    audio_stretchfactor;
    688     void    *audio_codec;
    689688    bool     audio_passthru;
    690689
    691690    // Picture-in-Picture
  • libs/libmythtv/tv_play.cpp

     
    348348    REG_KEY("TV Playback", "VOLUMEDOWN", "Volume down", "[,{,F10,Volume Down");
    349349    REG_KEY("TV Playback", "VOLUMEUP",   "Volume up",   "],},F11,Volume Up");
    350350    REG_KEY("TV Playback", "MUTE",       "Mute",        "|,\\,F9,Volume Mute");
     351    REG_KEY("TV Playback", "TOGGLEUPMIX", "Toggle upmixer", "Ctrl+U");
    351352    REG_KEY("TV Playback", "TOGGLEPIPMODE", "Toggle Picture-in-Picture mode",
    352353            "V");
    353354    REG_KEY("TV Playback", "TOGGLEPIPWINDOW", "Toggle active PiP window", "B");
     
    480481  Teletext     F2,F3,F4,F5,F6,F7,F8
    481482  ITV          F2,F3,F4,F5,F6,F7,F12
    482483
    483   Playback: Ctrl-B,Ctrl-G,Ctrl-Y
     484  Playback: Ctrl-B,Ctrl-G,Ctrl-Y,Ctrl-U
    484485*/
    485486}
    486487
     
    27102711            else if (action == "VOLUMEDOWN" || action == "VOLUMEUP" ||
    27112712                     action == "STRETCHINC" || action == "STRETCHDEC" ||
    27122713                     action == "MUTE"       || action == "TOGGLEASPECT" ||
    2713                      action == "TOGGLEFILL" )
     2714                     action == "TOGGLEFILL" || action == "TOGGLEUPMIX")
    27142715            {
    27152716                passThru = 1;
    27162717                handled = false;
     
    27252726            else
    27262727                handled = false;
    27272728        }
    2728 
     2729       
    27292730        if (!passThru)
    27302731            return;
    27312732    }
    2732 
     2733 
    27332734    if (zoomMode)
    27342735    {
    27352736        int passThru = 0;
     
    27652766            else if (action == "VOLUMEDOWN" || action == "VOLUMEUP" ||
    27662767                     action == "STRETCHINC" || action == "STRETCHDEC" ||
    27672768                     action == "MUTE" || action == "PAUSE" ||
    2768                      action == "CLEAROSD")
     2769                     action == "CLEAROSD" || action == "TOGGLEUPMIX")
    27692770            {
    27702771                passThru = 1;
    27712772                handled = false;
     
    27772778        if (!passThru)
    27782779            return;
    27792780    }
    2780 
     2781   
    27812782    if (dialogname != "" && GetOSD() && GetOSD()->DialogShowing(dialogname))
    27822783    {
    27832784        for (unsigned int i = 0; i < actions.size() && !handled; i++)
     
    30363037                return;
    30373038       }
    30383039    }
    3039              
     3040   
    30403041    for (unsigned int i = 0; i < actions.size() && !handled; i++)
    30413042    {
    30423043        QString action = actions[i];
     
    30703071            ChangeTimeStretch(0);   // just display
    30713072        else if (action == "TOGGLESTRETCH")
    30723073            ToggleTimeStretch();
     3074        else if (action == "TOGGLEUPMIX")
     3075            ToggleUpmix();
    30733076        else if (action == "CYCLECOMMSKIPMODE") {
    30743077            SetAutoCommercialSkip((enum commSkipMode)
    30753078                                  ((autoCommercialSkip + 1) % CommSkipModes));
     
    60546057    }
    60556058}
    60566059
     6060void TV::ToggleUpmix()
     6061{
     6062    AudioOutput *aud = nvp->getAudioOutput();
     6063    if (!aud)
     6064        return;
     6065    QString text;
     6066    if (aud->ToggleUpmix())
     6067        text = tr("Upmixer On");
     6068    else
     6069        text = tr("Upmixer Off");
     6070   
     6071    if (GetOSD() && !browsemode)
     6072        GetOSD()->SetSettingsText(text, 5);
     6073}
     6074   
     6075
    60576076// dir in 10ms jumps
    60586077void TV::ChangeAudioSync(int dir, bool allowEdit)
    60596078{
     
    73007319
    73017320        ChangeTimeStretch(0, !floatRead);   // just display
    73027321    }
     7322    else if (action == "TOGGLEUPMIX")
     7323        ToggleUpmix();
    73037324    else if (action.left(11) == "SELECTSCAN_")
    73047325        activenvp->SetScanType((FrameScanType) action.right(1).toInt());
    73057326    else if (action.left(15) == "TOGGLEAUDIOSYNC")
     
    75477568    subitem = new OSDGenericTree(item, tr("1.5X"), "ADJUSTSTRETCH1.5",
    75487569                                 (speedX100 == 150) ? 1 : 0, NULL,
    75497570                                 "STRETCHGROUP");
     7571   
     7572    item = new OSDGenericTree(treeMenu, tr("Toggle Upmixer"), "TOGGLEUPMIX");
    75507573
    75517574    // add scan mode override settings to menu
    75527575    FrameScanType scan_type = kScan_Ignore;
  • libs/libmythtv/avformatdecoder.h

     
    264264    bool              allow_dts_passthru;
    265265    bool              disable_passthru;
    266266    uint              max_channels;
     267    uint              last_ac3_channels;
    267268
    268269    VideoFrame       *dummy_frame;
    269270
  • 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

     
    4848    virtual void Reconfigure(int   audio_bits,
    4949                             int   audio_channels,
    5050                             int   audio_samplerate,
    51                              bool  audio_passthru,
    52                              void *audio_codec = NULL);
     51                             bool  audio_passthru);
    5352   
    5453    // do AddSamples calls block?
    5554    virtual void SetBlocking(bool blocking);
     
    5958
    6059    virtual void SetStretchFactor(float factor);
    6160    virtual float GetStretchFactor(void);
     61    virtual bool ToggleUpmix(void);
    6262
    6363    virtual void Reset(void);
    6464
     
    139139    QString audio_passthru_device;
    140140
    141141    bool audio_passthru;
     142    bool audio_enc;
     143    bool audio_reenc;
    142144
    143145    float audio_stretchfactor;
    144     AVCodecContext *audio_codec;
    145146    AudioOutputSource source;
    146147
    147148    bool killaudio;
     
    167168    FreeSurround              *upmixer;
    168169
    169170    int source_audio_channels;
     171    int source_audio_samplerate;
    170172    int source_audio_bytes_per_sample;
    171173    bool needs_upmix;
    172174    int surround_mode;
     175    float old_audio_stretchfactor;
    173176
    174177    bool blocking; // do AddSamples calls block?
    175178
  • libs/libmyth/audiooutputalsa.cpp

     
    3737    KillAudio();
    3838}
    3939
     40void SetIECBits(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_GENERAL, QString("Setting IEC958 status bits"));
     52   
     53    snd_ctl_open(&ctl, "default", 0);
     54    snd_ctl_elem_list_alloca(&clist);
     55    snd_ctl_elem_list(ctl, clist);
     56    snd_ctl_elem_list_alloc_space(clist, snd_ctl_elem_list_get_count(clist));
     57    snd_ctl_elem_list(ctl, clist);
     58    controls = snd_ctl_elem_list_get_used(clist);
     59    for (cidx = 0; cidx < controls; cidx++) {
     60        if (!strcmp(snd_ctl_elem_list_get_name(clist, cidx), spdif_str))
     61            if (spdif_index < 0 ||
     62                snd_ctl_elem_list_get_index(clist, cidx) == spdif_index)
     63                    break;
     64    }
     65    snd_ctl_elem_id_alloca(&cid);
     66    snd_ctl_elem_list_get_id(clist, cidx, cid);
     67    snd_ctl_elem_value_alloca(&cval);
     68    snd_ctl_elem_value_set_id(cval, cid);
     69    snd_ctl_elem_read(ctl,cval);
     70    snd_ctl_elem_value_get_iec958(cval, &iec958);
     71   
     72    if (!audio)
     73        iec958.status[0] |= IEC958_AES0_NONAUDIO;
     74    else
     75        iec958.status[0] &= ~IEC958_AES0_NONAUDIO;
     76
     77    snd_ctl_elem_value_set_iec958(cval, &iec958);
     78    snd_ctl_elem_write(ctl, cval);
     79
     80}
     81
    4082bool AudioOutputALSA::OpenDevice()
    4183{
    4284    snd_pcm_format_t format;
    4385    unsigned int buffer_time, period_time;
    4486    int err;
     87    QString real_device;
    4588
    4689    if (pcm_handle != NULL)
    4790        CloseDevice();
    4891
    4992    pcm_handle = NULL;
    5093    numbadioctls = 0;
     94   
     95    if (audio_passthru || audio_enc)
     96    {
     97        real_device = audio_passthru_device;
     98        SetIECBits(false);
     99    }
     100    else
     101    {
     102        real_device = audio_main_device;
     103        SetIECBits(true);
     104    }
    51105
    52     QString real_device = (audio_passthru) ?
    53         audio_passthru_device : audio_main_device;
    54 
    55106    VERBOSE(VB_GENERAL, QString("Opening ALSA audio device '%1'.")
    56107            .arg(real_device));
    57108
  • 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/libmyth/audiooutputbase.cpp

     
    3737
    3838    audio_main_device(QDeepCopy<QString>(laudio_main_device)),
    3939    audio_passthru_device(QDeepCopy<QString>(laudio_passthru_device)),
    40     audio_passthru(false),      audio_stretchfactor(1.0f),
     40    audio_passthru(false),      audio_enc(false),
     41    audio_reenc(false),         audio_stretchfactor(1.0f),
    4142
    42     audio_codec(NULL),
    4343    source(lsource),            killaudio(false),
    4444
    4545    pauseaudio(false),          audio_actually_paused(false),
     
    5454    pSoundStretch(NULL),       
    5555    encoder(NULL),
    5656    upmixer(NULL),
     57
    5758    source_audio_channels(-1),
     59    source_audio_samplerate(0),
    5860    source_audio_bytes_per_sample(0),
    5961    needs_upmix(false),
    6062    surround_mode(FreeSurround::SurroundModePassive),
     63    old_audio_stretchfactor(1.0),
    6164
    6265    blocking(false),
    6366
     
    124127            VERBOSE(VB_GENERAL, LOC + QString("Using time stretch %1")
    125128                                        .arg(audio_stretchfactor));
    126129            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));
     130            pSoundStretch->setSampleRate(audio_samplerate);
     131            pSoundStretch->setChannels(upmixer ?
     132                configured_audio_channels : source_audio_channels);
    135133
    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 
    162134            pSoundStretch->setTempo(audio_stretchfactor);
    163135            pSoundStretch->setSetting(SETTING_SEQUENCE_MS, 35);
    164136
    165137            // dont need these with only tempo change
    166138            //pSoundStretch->setPitch(1.0);
    167139            //pSoundStretch->setRate(1.0);
    168 
    169140            //pSoundStretch->setSetting(SETTING_USE_QUICKSEEK, true);
    170141            //pSoundStretch->setSetting(SETTING_USE_AA_FILTER, false);
    171142        }
     
    184155    return audio_stretchfactor;
    185156}
    186157
     158bool AudioOutputBase::ToggleUpmix(void)
     159{
     160    if (audio_passthru)
     161        return false;
     162    if (configured_audio_channels == 6)
     163        configured_audio_channels = 2;
     164    else
     165        configured_audio_channels = 6;
     166   
     167    Reconfigure(audio_bits, source_audio_channels,
     168        source_audio_samplerate, audio_passthru);
     169    return (configured_audio_channels == 6);
     170}
     171
    187172void AudioOutputBase::Reconfigure(int laudio_bits, int laudio_channels,
    188                                   int laudio_samplerate, bool laudio_passthru,
    189                                   void *laudio_codec)
     173                                  int laudio_samplerate, bool laudio_passthru)
    190174{
    191     int codec_id = CODEC_ID_NONE;
    192     int lcodec_id = CODEC_ID_NONE;
    193     int lcchannels = 0;
    194     int cchannels = 0;
    195175    int lsource_audio_channels = laudio_channels;
    196176    bool lneeds_upmix = false;
    197 
    198     if (laudio_codec)
     177    bool laudio_reenc = false;
     178   
     179    // Are we reencoding a (previously) timestretched bitstream?
     180    if (laudio_channels > 2 && !laudio_passthru)
     181        laudio_reenc = true;
     182 
     183    // Enough channels? Upmix if not
     184    if (laudio_channels < configured_audio_channels && !laudio_passthru)
    199185    {
    200         lcodec_id = ((AVCodecContext*)laudio_codec)->codec_id;
    201         laudio_bits = 16;
    202         laudio_channels = 2;
    203         lsource_audio_channels = laudio_channels;
    204         laudio_samplerate = 48000;
    205         lcchannels = ((AVCodecContext*)laudio_codec)->channels;
    206     }
    207 
    208     if (audio_codec)
    209     {
    210         codec_id = audio_codec->codec_id;
    211         cchannels = ((AVCodecContext*)audio_codec)->channels;
    212     }
    213 
    214     if ((configured_audio_channels == 6) &&
    215         !(laudio_codec || audio_codec))
    216     {
    217186        laudio_channels = configured_audio_channels;
    218187        lneeds_upmix = true;
    219188        VERBOSE(VB_AUDIO,LOC + "Needs upmix");
     
    225194        laudio_samplerate == audio_samplerate && !need_resampler &&
    226195        laudio_passthru == audio_passthru &&
    227196        lneeds_upmix == needs_upmix &&
    228         lcodec_id == codec_id && lcchannels == cchannels);
     197        laudio_reenc == audio_reenc);
    229198    bool upmix_deps =
    230199        (lsource_audio_channels == source_audio_channels);
    231200    if (general_deps && upmix_deps)
     
    252221    waud = raud = 0;
    253222    audio_actually_paused = false;
    254223   
    255     bool redo_stretch = (pSoundStretch && audio_channels != laudio_channels);
    256224    audio_channels = laudio_channels;
    257225    source_audio_channels = lsource_audio_channels;
    258226    audio_bits = laudio_bits;
    259     audio_samplerate = laudio_samplerate;
    260     audio_codec = (AVCodecContext*)laudio_codec;
     227    source_audio_samplerate = audio_samplerate = laudio_samplerate;
     228    audio_reenc = laudio_reenc;
    261229    audio_passthru = laudio_passthru;
    262230    needs_upmix = lneeds_upmix;
    263231
     
    268236        Error("AudioOutput only supports 8 or 16bit audio.");
    269237        return;
    270238    }
    271     audio_bytes_per_sample = audio_channels * audio_bits / 8;
    272     source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
    273239   
    274240    need_resampler = false;
    275241    killaudio = false;
     
    278244    internal_vol = gContext->GetNumSetting("MythControlsVolume", 0);
    279245   
    280246    numlowbuffer = 0;
     247   
     248    // Encode to AC-3 if not passing thru, there's more than 2 channels
     249    // and a passhru device is defined
     250    // This won't reencode timestretched 2ch AC-3 but there's no point doing so
     251    if (!audio_passthru && audio_channels > 2 &&
     252        !audio_passthru_device.isEmpty())
     253    {
     254        VERBOSE(VB_AUDIO, LOC + "Creating AC-3 Encoder");
     255        encoder = new AudioOutputDigitalEncoder();
     256        if (!encoder->Init(CODEC_ID_AC3, 448000, 48000,
     257                           audio_channels, audio_reenc))
     258        {
     259            VERBOSE(VB_AUDIO, LOC + "Can't create AC-3 encoder");
     260            delete encoder;
     261            encoder = NULL;
     262        }
     263        audio_channels = 2;
     264        audio_enc = true;
     265    }       
     266   
     267    if(audio_passthru)
     268        // Soundcard expects a 2ch 48k stream
     269        audio_channels = 2;
     270   
     271    audio_bytes_per_sample = audio_channels * audio_bits / 8;
     272    source_audio_bytes_per_sample = source_audio_channels * audio_bits / 8;
    281273
     274    // Always resample to 48k - many cards can't do anything else
     275    // and ALSA will do it with linear interpolation (yuk) if we don't anyway
     276    if (audio_samplerate != 48000)
     277    {
     278        int error;
     279        audio_samplerate = 48000;
     280        VERBOSE(VB_GENERAL, LOC + QString("Using resampler. From: %1 to %2")
     281            .arg(laudio_samplerate).arg(audio_samplerate));
     282        src_ctx = src_new (SRC_SINC_BEST_QUALITY, audio_channels, &error);
     283        if (error)
     284        {
     285            Error(QString("Error creating resampler, the error was: %1")
     286                  .arg(src_strerror(error)) );
     287            pthread_mutex_unlock(&avsync_lock);
     288            pthread_mutex_unlock(&audio_buflock);
     289            src_ctx = NULL;
     290            return;
     291        }
     292        src_data.src_ratio = (double) audio_samplerate / laudio_samplerate;
     293        src_data.data_in = src_in;
     294        src_data.data_out = src_out;
     295        src_data.output_frames = 16384*6;
     296        need_resampler = true;
     297    }
     298   
    282299    VERBOSE(VB_GENERAL, QString("Opening audio device '%1'. ch %2(%3) sr %4")
    283300            .arg(audio_main_device).arg(audio_channels)
    284301            .arg(source_audio_channels).arg(audio_samplerate));
     
    314331    current_seconds = -1;
    315332    source_bitrate = -1;
    316333
    317     // NOTE: this won't do anything as above samplerate vars are set equal
    318     // Check if we need the resampler
    319     if (audio_samplerate != laudio_samplerate)
    320     {
    321         int error;
    322         VERBOSE(VB_GENERAL, LOC + QString("Using resampler. From: %1 to %2")
    323                                .arg(laudio_samplerate).arg(audio_samplerate));
    324         src_ctx = src_new (SRC_SINC_BEST_QUALITY, audio_channels, &error);
    325         if (error)
    326         {
    327             Error(QString("Error creating resampler, the error was: %1")
    328                   .arg(src_strerror(error)) );
    329             pthread_mutex_unlock(&avsync_lock);
    330             pthread_mutex_unlock(&audio_buflock);
    331             return;
    332         }
    333         src_data.src_ratio = (double) audio_samplerate / laudio_samplerate;
    334         src_data.data_in = src_in;
    335         src_data.data_out = src_out;
    336         src_data.output_frames = 16384*6;
    337         need_resampler = true;
    338     }
    339 
    340334    if (needs_upmix)
    341335    {
    342336        VERBOSE(VB_AUDIO, LOC + QString("create upmixer"));
     
    351345            (FreeSurround::SurroundMode)surround_mode);
    352346
    353347        VERBOSE(VB_AUDIO, LOC +
    354                 QString("create upmixer done with surround mode %1")
     348                QString("Create upmixer done with surround mode %1")
    355349                .arg(surround_mode));
    356350    }
    357351
    358352    VERBOSE(VB_AUDIO, LOC + QString("Audio Stretch Factor: %1")
    359353            .arg(audio_stretchfactor));
    360     VERBOSE(VB_AUDIO, QString("Audio Codec Used: %1")
    361             .arg((audio_codec) ?
    362                  codec_id_string(audio_codec->codec_id) : "not set"));
    363354
    364     if (redo_stretch)
    365     {
    366         float laudio_stretchfactor = audio_stretchfactor;
    367         delete pSoundStretch;
    368         pSoundStretch = NULL;
    369         audio_stretchfactor = 0.0f;
    370         SetStretchFactorLocked(laudio_stretchfactor);
    371     }
    372     else
    373     {
    374         SetStretchFactorLocked(audio_stretchfactor);
    375         if (pSoundStretch)
    376         {
    377             // if its passthru then we need to reencode
    378             if (audio_codec)
    379             {
    380                 if (!encoder)
    381                 {
    382                     VERBOSE(VB_AUDIO, LOC +
    383                             QString("Creating Encoder for codec %1")
    384                             .arg(audio_codec->codec_id));
     355    SetStretchFactorLocked(old_audio_stretchfactor);
    385356
    386                     encoder = new AudioOutputDigitalEncoder();
    387                     if (!encoder->Init(audio_codec->codec_id,
    388                                 audio_codec->bit_rate,
    389                                 audio_codec->sample_rate,
    390                                 audio_codec->channels
    391                                 ))
    392                     {
    393                         // eeks
    394                         delete encoder;
    395                         encoder = NULL;
    396                         VERBOSE(VB_AUDIO, LOC + "Failed to Create Encoder");
    397                     }
    398                 }
    399             }
    400             if (audio_codec && encoder)
    401             {
    402                 pSoundStretch->setSampleRate(audio_codec->sample_rate);
    403                 pSoundStretch->setChannels(audio_codec->channels);
    404             }
    405             else
    406             {
    407                 pSoundStretch->setSampleRate(audio_samplerate);
    408                 pSoundStretch->setChannels(audio_channels);
    409             }
    410         }
    411     }
    412 
    413357    // Setup visualisations, zero the visualisations buffers
    414358    prepareVisuals();
    415359
     
    455399    VERBOSE(VB_AUDIO, LOC + "Killing AudioOutputDSP");
    456400    killaudio = true;
    457401    StopOutputThread();
     402   
     403    pthread_mutex_lock(&audio_buflock);
    458404
    459405    // Close resampler?
    460406    if (src_ctx)
     407    {
    461408        src_delete(src_ctx);
     409        src_ctx = NULL;
     410    }
     411               
    462412    need_resampler = false;
    463413
    464414    // close sound stretcher
     
    466416    {
    467417        delete pSoundStretch;
    468418        pSoundStretch = NULL;
     419        old_audio_stretchfactor = audio_stretchfactor;
     420        audio_stretchfactor = 1.0;
    469421    }
    470422
    471423    if (encoder)
     
    480432        upmixer = NULL;
    481433    }
    482434    needs_upmix = false;
     435    audio_enc = false;
    483436
    484437    CloseDevice();
    485438
     439    pthread_mutex_unlock(&audio_buflock);
    486440    killAudioLock.unlock();
    487441}
    488442
     
    591545    ret += (now.tv_usec - audiotime_updated.tv_usec) / 1000;
    592546    ret = (long long)(ret * audio_stretchfactor);
    593547
    594 #if 1
    595548    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
    596549            QString("GetAudiotime now=%1.%2, set=%3.%4, ret=%5, audt=%6 sf=%7")
    597550            .arg(now.tv_sec).arg(now.tv_usec)
     
    600553            .arg(audiotime)
    601554            .arg(audio_stretchfactor)
    602555           );
    603 #endif
    604556
    605557    ret += audiotime;
    606558
     
    638590 
    639591    soundcard_buffer = getBufferedOnSoundcard(); // bytes
    640592    totalbuffer = audiolen(false) + soundcard_buffer;
    641  
     593
    642594    // include algorithmic latencies
    643595    if (pSoundStretch)
    644     {
    645         // add the effect of any unused but processed samples,
    646         // AC3 reencode does this
    647         totalbuffer += (int)(pSoundStretch->numSamples() *
    648                              audio_bytes_per_sample);
    649         // add the effect of unprocessed samples in time stretch algo
    650596        totalbuffer += (int)((pSoundStretch->numUnprocessedSamples() *
    651597                              audio_bytes_per_sample) / audio_stretchfactor);
    652     }
    653598
    654599    if (upmixer && needs_upmix)
    655     {
    656600        totalbuffer += upmixer->sampleLatency() * audio_bytes_per_sample;
    657     }
    658601
     602    if (encoder)
     603        totalbuffer += encoder->Buffered();
     604
    659605    audiotime = audbuf_timecode - (int)(totalbuffer * 100000.0 /
    660606                                   (audio_bytes_per_sample * effdspstretched));
    661607 
    662608    gettimeofday(&audiotime_updated, NULL);
    663 #if 1
     609   
    664610    VERBOSE(VB_AUDIO|VB_TIMESTAMP,
    665611            QString("SetAudiotime set=%1.%2, audt=%3 atc=%4 "
    666612                    "tb=%5 sb=%6 eds=%7 abps=%8 sf=%9")
     
    672618            .arg(effdspstretched)
    673619            .arg(audio_bytes_per_sample)
    674620            .arg(audio_stretchfactor));
    675 #endif
    676621
    677622    pthread_mutex_unlock(&avsync_lock);
    678623    pthread_mutex_unlock(&audio_buflock);
     
    708653
    709654        return false; // would overflow
    710655    }
     656   
     657    pthread_mutex_lock(&audio_buflock);
    711658
    712659    // resample input if necessary
    713660    if (need_resampler && src_ctx)
     
    741688        // Call our function to do the work
    742689        _AddSamples(buffers, false, samples, timecode);
    743690    }
     691   
     692    pthread_mutex_unlock(&audio_buflock);
    744693
    745694    return true;
    746695}
     
    776725                .arg(timecode));
    777726        return false; // would overflow
    778727    }
     728   
     729    pthread_mutex_lock(&audio_buflock);
    779730
    780731    // resample input if necessary
    781732    if (need_resampler && src_ctx)
     
    804755        // Call our function to do the work
    805756        _AddSamples(buffer, true, samples, timecode);
    806757    }
     758   
     759    pthread_mutex_unlock(&audio_buflock);
    807760
    808761    return true;
    809762}
     
    836789            if (src_ctx)
    837790            {
    838791                int error = src_reset(src_ctx);
    839                 if (error)
     792                if (error)
     793                {
    840794                    VERBOSE(VB_IMPORTANT, LOC_ERR + QString(
    841795                            "Error occured while resetting resampler: %1")
    842796                            .arg(src_strerror(error)));
     797                    src_ctx = NULL;
     798                }
    843799            }
    844800        }
    845801    }
     
    849805void AudioOutputBase::_AddSamples(void *buffer, bool interleaved, int samples,
    850806                                  long long timecode)
    851807{
    852     pthread_mutex_lock(&audio_buflock);
    853 
    854808    int len; // = samples * audio_bytes_per_sample;
    855809    int audio_bytes = audio_bits / 8;
    856810    int org_waud = waud;
     
    871825    if (upmixer && needs_upmix)
    872826    {
    873827        int out_samples = 0;
     828        org_waud = waud;
    874829        int step = (interleaved)?source_audio_channels:1;
    875830        len = WaitForFreeSpace(samples);    // test
     831       
    876832        for (int itemp = 0; itemp < samples; )
    877833        {
    878             // just in case it does a processing cycle, release the lock
    879             // to allow the output loop to do output
    880             pthread_mutex_unlock(&audio_buflock);
    881834            if (audio_bytes == 2)
    882835            {
    883836                itemp += upmixer->putSamples(
     
    894847                    source_audio_channels,
    895848                    (interleaved) ? 0 : samples);
    896849            }
    897             pthread_mutex_lock(&audio_buflock);
    898850
    899851            int copy_samples = upmixer->numSamples();
    900852            if (copy_samples)
     
    913865                        (short*)(audiobuffer), (copy_samples - bdiff_samples));
    914866                }
    915867                else
    916                 {
    917868                    upmixer->receiveSamples(
    918869                        (short*)(audiobuffer + org_waud), copy_samples);
    919                 }
     870               
    920871                org_waud = (org_waud + copy_len) % AUDBUFSIZE;
    921872            }
    922873        }
     
    969920    {
    970921        if (pSoundStretch)
    971922        {
    972 
    973923            // does not change the timecode, only the number of samples
    974924            // back to orig pos
    975925            org_waud = waud;
     
    984934                                          (len - bdiff) / abps);
    985935            }
    986936            else
    987             {
    988937                pSoundStretch->putSamples((soundtouch::SAMPLETYPE*)
    989938                                          (audiobuffer + org_waud),
    990939                                          len / abps);
    991             }
    992940
    993             if (encoder)
     941            int nSamples = pSoundStretch->numSamples();
     942            len = WaitForFreeSpace(nSamples);
     943           
     944            while ((nSamples = pSoundStretch->numSamples()))
    994945            {
    995                 // pull out a packet's worth and reencode it until we
    996                 // don't have enough for any more packets
    997                 soundtouch::SAMPLETYPE *temp_buff =
    998                     (soundtouch::SAMPLETYPE*)encoder->GetFrameBuffer();
    999                 size_t frameSize = encoder->FrameSize()/abps;
     946                if (nSamples > nSamplesToEnd)
     947                    nSamples = nSamplesToEnd;
     948               
     949                nSamples = pSoundStretch->receiveSamples(
     950                    (soundtouch::SAMPLETYPE*)
     951                    (audiobuffer + org_waud), nSamples
     952                );
     953               
     954                if (nSamples == nSamplesToEnd) {
     955                    org_waud = 0;
     956                    nSamplesToEnd = AUDBUFSIZE/abps;
     957                }
     958                else {
     959                    org_waud += nSamples * abps;
     960                    nSamplesToEnd -= nSamples;
     961                }
     962               
     963            }
     964           
     965        }
    1000966
    1001                 VERBOSE(VB_AUDIO|VB_TIMESTAMP,
    1002                         QString("_AddSamples Enc sfs=%1 bfs=%2 sss=%3")
    1003                         .arg(frameSize)
    1004                         .arg(encoder->FrameSize())
    1005                         .arg(pSoundStretch->numSamples()));
     967        // Encode to AC-3?
     968        if (encoder)
     969        {
     970           
     971            org_waud = waud;
     972            int bdiff = AUDBUFSIZE - org_waud;
     973            int to_get = 0;
    1006974
    1007                 // process the same number of samples as it creates
    1008                 // a full encoded buffer just like before
    1009                 while (pSoundStretch->numSamples() >= frameSize)
    1010                 {
    1011                     int got = pSoundStretch->receiveSamples(
    1012                         temp_buff, frameSize);
    1013                     int amount = encoder->Encode(temp_buff);
    1014 
    1015                     VERBOSE(VB_AUDIO|VB_TIMESTAMP,
    1016                             QString("_AddSamples Enc bytes=%1 got=%2 left=%3")
    1017                             .arg(amount)
    1018                             .arg(got)
    1019                             .arg(pSoundStretch->numSamples()));
    1020 
    1021                     if (!amount)
    1022                         continue;
    1023 
    1024                     //len = WaitForFreeSpace(amount);
    1025                     char *ob = encoder->GetOutBuff();
    1026                     if (amount >= bdiff)
    1027                     {
    1028                         memcpy(audiobuffer + org_waud, ob, bdiff);
    1029                         ob += bdiff;
    1030                         amount -= bdiff;
    1031                         org_waud = 0;
    1032                     }
    1033                     if (amount > 0)
    1034                         memcpy(audiobuffer + org_waud, ob, amount);
    1035 
    1036                     bdiff = AUDBUFSIZE - amount;
    1037                     org_waud += amount;
    1038                 }
     975            if (bdiff < len)
     976            {
     977                encoder->Encode(audiobuffer + org_waud, bdiff);
     978                to_get = encoder->Encode(audiobuffer, len - bdiff);
    1039979            }
    1040             else
     980            else
     981                to_get = encoder->Encode(audiobuffer + org_waud, len);
     982           
     983            if (to_get > 0)
    1041984            {
    1042                 int newLen = 0;
    1043                 int nSamples;
    1044                 len = WaitForFreeSpace(pSoundStretch->numSamples() *
    1045                                        audio_bytes_per_sample);
    1046                 do
     985               
     986                if (to_get >= bdiff)
    1047987                {
    1048                     int samplesToGet = len/audio_bytes_per_sample;
    1049                     if (samplesToGet > nSamplesToEnd)
    1050                     {
    1051                         samplesToGet = nSamplesToEnd;   
    1052                     }
     988                    encoder->GetFrames(audiobuffer + org_waud, bdiff);
     989                    to_get -= bdiff;
     990                    org_waud = 0;
     991                }
     992                if (to_get > 0)
     993                    encoder->GetFrames(audiobuffer + org_waud, to_get);
    1053994
    1054                     nSamples = pSoundStretch->receiveSamples(
    1055                         (soundtouch::SAMPLETYPE*)
    1056                         (audiobuffer + org_waud), samplesToGet);
    1057                     if (nSamples == nSamplesToEnd)
    1058                     {
    1059                         org_waud = 0;
    1060                         nSamplesToEnd = AUDBUFSIZE/audio_bytes_per_sample;
    1061                     }
    1062                     else
    1063                     {
    1064                         org_waud += nSamples * audio_bytes_per_sample;
    1065                         nSamplesToEnd -= nSamples;
    1066                     }
     995                org_waud += to_get;
    1067996
    1068                     newLen += nSamples * audio_bytes_per_sample;
    1069                     len -= nSamples * audio_bytes_per_sample;
    1070                 } while (nSamples > 0);
    1071997            }
     998
    1072999        }
    10731000
    10741001        waud = org_waud;
    10751002        lastaudiolen = audiolen(false);
    10761003
    10771004        if (timecode < 0)
    1078         {
    10791005            // mythmusic doesn't give timestamps..
    10801006            timecode = (int)((samples_buffered * 100000.0) / effdsp);
    1081         }
    10821007       
    10831008        samples_buffered += samples;
    10841009       
     
    10901015
    10911016        if (interleaved)
    10921017        {
     1018            // Give original samples to mythmusic visualisation
    10931019            dispatchVisual((unsigned char *)buffer, len, timecode,
    10941020                           source_audio_channels, audio_bits);
    10951021        }
    10961022    }
    10971023
    1098     pthread_mutex_unlock(&audio_buflock);
    10991024}
    11001025
    11011026void AudioOutputBase::Status()
  • libs/libmyth/audiooutputdigitalencoder.cpp

     
    2727
    2828AudioOutputDigitalEncoder::AudioOutputDigitalEncoder(void) :
    2929    av_context(NULL),
    30     outbuf(NULL),
    31     outbuf_size(0),
    32     frame_buffer(NULL),
    33     one_frame_bytes(0)
     30    outbuflen(0),
     31    inbuflen(0),
     32    one_frame_bytes(0),
     33    reorder(true)
    3434{
    3535}
    3636
     
    4747        av_free(av_context);
    4848        av_context = NULL;
    4949    }
    50 
    51     if (outbuf)
    52     {
    53         delete [] outbuf;
    54         outbuf = NULL;
    55         outbuf_size = 0;
    56     }
    57 
    58     if (frame_buffer)
    59     {
    60         delete [] frame_buffer;
    61         frame_buffer = NULL;
    62         one_frame_bytes = 0;
    63     }
    6450}
    6551
    6652//CODEC_ID_AC3
    6753bool AudioOutputDigitalEncoder::Init(
    68     CodecID codec_id, int bitrate, int samplerate, int channels)
     54    CodecID codec_id, int bitrate, int samplerate, int channels, bool reencoding)
    6955{
    7056    AVCodec *codec;
    7157    int ret;
    7258
    73     VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4")
     59    VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4 re=%5")
    7460            .arg(codec_id_string(codec_id))
    7561            .arg(bitrate)
    7662            .arg(samplerate)
    77             .arg(channels));
     63            .arg(channels)
     64            .arg(reencoding));
     65   
     66    reorder = !reencoding;
    7867
    79     //codec = avcodec_find_encoder(codec_id);
     68    // We need to do this when called from mythmusic
     69    avcodec_init();
     70    avcodec_register_all();
    8071    // always AC3 as there is no DTS encoder at the moment 2005/1/9
    8172    codec = avcodec_find_encoder(CODEC_ID_AC3);
    8273    if (!codec)
     
    10596    audio_bytes_per_sample = bytes_per_frame;
    10697    one_frame_bytes = bytes_per_frame * av_context->frame_size;
    10798
    108     outbuf_size = 16384;    // ok for AC3 but DTS?
    109     outbuf = new char [outbuf_size];
    11099    VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init fs=%1, bpf=%2 ofb=%3")
    111100            .arg(av_context->frame_size)
    112101            .arg(bytes_per_frame)
     
    256245
    257246} AESHeader;
    258247
     248
     249void reorder_6ch_ac3(void *buf, unsigned int len) {
     250    unsigned short *src = (unsigned short *)buf;
     251    unsigned short tmp;
     252    unsigned int samples = len >> 1;
     253
     254    for (uint i = 0; i < samples; i += 6) {
     255        tmp = src[i+4];
     256        src[i+4] = src[i+3];
     257        src[i+3] = src[i+2];
     258        src[i+2] = src[i+1];
     259        src[i+1] = tmp;
     260    }
     261}
     262
    259263static int encode_frame(
    260264        bool dts,
    261265        unsigned char *data,
    262         size_t &len)
     266        size_t enc_len)
    263267{
    264     size_t enc_len;
    265268    int flags, sample_rate, bit_rate;
    266269
    267270    // we don't do any length/crc validation of the AC3 frame here; presumably
     
    273276    // anything with a bad CRC...
    274277
    275278    uint nr_samples = 0, block_len;
     279   
    276280    if (dts)
    277281    {
    278282        enc_len = dts_syncinfo(data+8, &flags, &sample_rate, &bit_rate);
     
    293297#endif
    294298    }
    295299
    296     if (enc_len == 0 || enc_len > len)
    297     {
    298         int l = len;
    299         len = 0;
    300         return l;
    301     }
    302 
    303300    enc_len = min((uint)enc_len, block_len - 8);
    304 
     301   
    305302    //uint32_t x = *(uint32_t*)(data+8);
    306303    // in place swab
    307304    swab((const char *)data + 8, (char *)data + 8, enc_len);
     
    348345                break;
    349346        }
    350347    }
    351     data[5] = 0x00;
     348    data[5] = 0;
    352349    data[6] = (enc_len << 3) & 0xFF;
    353350    data[7] = (enc_len >> 5) & 0xFF;
    354351    memset(data + 8 + enc_len, 0, block_len - 8 - enc_len);
    355     len = block_len;
    356352
    357353    return enc_len;
    358354}
    359355
    360 // must have exactly 1 frames worth of data
    361 size_t AudioOutputDigitalEncoder::Encode(short *buff)
     356size_t AudioOutputDigitalEncoder::Encode(void *buf, int len)
    362357{
    363     int encsize = 0;
    364358    size_t outsize = 0;
    365359 
    366     // put data in the correct spot for encode frame
    367     outsize = avcodec_encode_audio(
    368         av_context, ((uchar*)outbuf) + 8, outbuf_size - 8, buff);
     360    int fs = FrameSize();
     361    memcpy(inbuf+inbuflen, buf, len);
     362    inbuflen += len;
     363    int frames = inbuflen / fs;
    369364
    370     size_t tmpsize = outsize;
     365    while (frames--)
     366    {
     367        if (reorder)
     368            reorder_6ch_ac3(inbuf, fs);
     369       
     370        // put data in the correct spot for encode frame
     371        outsize = avcodec_encode_audio(
     372            av_context, ((uchar*)outbuf) + outbuflen + 8, OUTBUFSIZE - 8, (short int *)inbuf);
    371373
    372     outsize = MAX_AC3_FRAME_SIZE;
    373     encsize = encode_frame(
    374         /*av_context->codec_id==CODEC_ID_DTS*/ false,
    375         (unsigned char*)outbuf, outsize);
     374        encode_frame(
     375            /*av_context->codec_id==CODEC_ID_DTS*/ false,
     376            (unsigned char*)outbuf + outbuflen, outsize
     377        );
    376378
    377     VERBOSE(VB_AUDIO|VB_TIMESTAMP,
    378             QString("DigitalEncoder::Encode len1=%1 len2=%2 finallen=%3")
    379                 .arg(tmpsize).arg(encsize).arg(outsize));
     379        outbuflen += MAX_AC3_FRAME_SIZE;
     380        inbuflen -= fs;
     381        memmove(inbuf, inbuf+fs, inbuflen);
     382    }
    380383
    381     return outsize;
     384    return outbuflen;
    382385}
     386
     387void AudioOutputDigitalEncoder::GetFrames(void *ptr, int maxlen)
     388{
     389    int len = (maxlen < outbuflen ? maxlen : outbuflen);
     390    memcpy(ptr, outbuf, len);
     391    outbuflen -= len;
     392    memmove(outbuf, outbuf+len, outbuflen);
     393}
  • libs/libmyth/audiooutput.h

     
    3434    virtual void Reconfigure(int audio_bits,
    3535                             int audio_channels,
    3636                             int audio_samplerate,
    37                              bool audio_passthru,
    38                              void* audio_codec = NULL) = 0;
     37                             bool audio_passthru) = 0;
    3938   
    4039    virtual void SetStretchFactor(float factor);
    4140    virtual float GetStretchFactor(void) { return 1.0f; }
     41    virtual bool ToggleUpmix(void) = 0;
    4242
    4343    // do AddSamples calls block?
    4444    virtual void SetBlocking(bool blocking) = 0;
  • 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
     
    191190    if (moviemode)
    192191    {
    193192        params.phasemode = 1;
    194         params.center_width = 0;
    195         params.gain = 1.0;
     193        params.center_width = 40;
     194        params.gain = 3.0;
     195        params.dimension = 0.5;
    196196    }
    197197    else
    198198    {
    199         params.center_width = 70;
    200         // for 50, gain should be about 1.9, c/lr about 2.7
    201         // for 70, gain should be about 3.1, c/lr about 1.5
     199        params.center_width = 80;
    202200        params.gain = 3.1;
     201        params.dimension = 0.3;
    203202    }
    204203    switch (surround_mode)
    205204    {
  • programs/mythtranscode/transcode.cpp

     
    5555
    5656    // reconfigure sound out for new params
    5757    virtual void Reconfigure(int audio_bits, int audio_channels,
    58                              int audio_samplerate, bool audio_passthru,
    59                              void *audio_codec = NULL)
     58                             int audio_samplerate, bool audio_passthru)
    6059    {
    6160        (void)audio_samplerate;
    6261        (void)audio_passthru;
    63         (void)audio_codec;
    6462
    6563        ClearError();
    6664        bits = audio_bits;
     
    218216        // Do nothing
    219217        return MUTE_OFF;
    220218    }
     219    virtual bool ToggleUpmix(void)
     220    {
     221        // Do nothing
     222        return false;
     223    }
    221224
    222225    //  These are pure virtual in AudioOutput, but we don't need them here
    223226    virtual void bufferOutputData(bool){ return; }