Ticket #730: mythtv-dtspassthru.2.patch

File mythtv-dtspassthru.2.patch, 18.6 KB (added by martin@…, 18 years ago)

Updated DTS patch (not including Daniel's changes yet), against r8241

  • libs/libmythtv/avformatdecoder.cpp

     
    3535
    3636#define MAX_AC3_FRAME_SIZE 6144
    3737
     38static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate);
     39static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq);
     40
    3841/** Set to zero to allow any number of AC3 channels. */
    3942#define MAX_OUTPUT_CHANNELS 2
    4043
     
    180183      // Audio
    181184      audioSamples(new short int[AVCODEC_MAX_AUDIO_FRAME_SIZE]),
    182185      allow_ac3_passthru(false),
     186      allow_dts_passthru(false),
    183187      // Audio selection
    184188      wantedAudioStream(),    selectedAudioStream(),
    185189      // Subtitle selection
     
    196200
    197201    save_cctc[0] = save_cctc[1] = 0;
    198202    allow_ac3_passthru = gContext->GetNumSetting("AC3PassThru", false);
     203    allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false);
    199204
    200205    audioIn.sample_size = -32; // force SetupAudioStream to run once
    201206}
     
    946951                            <<") already open, leaving it alone.");
    947952                }
    948953                assert(enc->codec_id);
     954                if (enc->codec_id == CODEC_ID_DTS)
     955                {
     956                    enc->sample_rate = 48000;
     957                    enc->channels = 2;
     958                    // enc->bit_rate = what??;
     959                }
    949960                bitrate += enc->bit_rate;
    950961                break;
    951962            }
     
    18381849    return ret;
    18391850}
    18401851
    1841 int filter_ac3(const AVFormatContext *ic,
    1842                const sinfo_vec_t     &audioStreams,
    1843                const vector<int>     &fs)
    1844 {
    1845     int selectedTrack = -1;
    1846 
    1847     vector<int>::const_iterator it = fs.begin();
    1848     for (; it != fs.end(); ++it)
    1849     {
    1850         const int stream_index    = audioStreams[*it].av_stream_index;
    1851         const AVCodecContext *ctx = ic->streams[stream_index]->codec;
    1852         if (CODEC_ID_AC3 == ctx->codec_id)
    1853         {
    1854             selectedTrack = *it;
    1855             break;
    1856         }
    1857     }
    1858 
    1859     return selectedTrack;
    1860 }
    1861 
    18621852int filter_max_ch(const AVFormatContext *ic,
    18631853                  const sinfo_vec_t     &audioStreams,
    1864                   const vector<int>     &fs)
     1854                  const vector<int>     &fs,
     1855                  enum CodecID           codecId = CODEC_ID_NONE)
    18651856{
    18661857    int selectedTrack = -1, max_seen = -1;
    18671858
     
    18701861    {
    18711862        const int stream_index    = audioStreams[*it].av_stream_index;
    18721863        const AVCodecContext *ctx = ic->streams[stream_index]->codec;
    1873         if (max_seen < ctx->channels)
     1864        if (codecId == CODEC_ID_NONE || codecId == ctx->codec_id)
    18741865        {
    1875             selectedTrack = *it;
    1876             max_seen = ctx->channels;
     1866            if (max_seen < ctx->channels)
     1867            {
     1868                selectedTrack = *it;
     1869                max_seen = ctx->channels;
     1870            }
    18771871        }
    18781872    }
    18791873
     
    18831877/** \fn AvFormatDecoder::autoSelectAudioTrack(void)
    18841878 *  \brief Selects the best audio track.
    18851879 *
    1886  *   This function will select the best audio track
    1887  *   available using the following rules:
     1880 *   This function will select the best audio track available
     1881 *   using the following criteria, in order of decreasing
     1882 *   preference:
    18881883 *
    1889  *   First we try to select the stream last selected by the
    1890  *   user, which is recalled as the Nth stream in the
    1891  *   preferred language. If it can not be located we attempt
    1892  *   to find a stream in the same language.
     1884 *   1) The stream last selected by the user, which is
     1885 *      recalled as the Nth stream in the preferred language.
     1886 *      If it can not be located we attempt to find a stream
     1887 *     in the same language.
    18931888 *
    1894  *   If we can not reselect the last user selected stream,
    1895  *   then for each preferred language from most preferred to
    1896  *   least preferred, we try to use the first AC3 track found,
    1897  *   or if no AC3 audio is found then we try to select the
    1898  *   audio track with the greatest number of audio channels.
     1889 *   2) If we can not reselect the last user selected stream,
     1890 *      then for each preferred language from most preferred
     1891 *      to least preferred, we try to find a new stream based
     1892 *      on the algorithm below.
    18991893 *
    1900  *   If we can not select a stream in a preferred language
    1901  *   we try to use the first AC3 track found irrespective
    1902  *   of language, and if no AC3 audio is found then we try
    1903  *   to select the audio track with the greatest number of
    1904  *   audio channels.
     1894 *   3) If we can not select a stream in a preferred language
     1895 *      we try to select a stream irrespective of language
     1896 *      based on the algorithm below.
     1897 *
     1898 *   When searching for a new stream (ie. options 2 and 3
     1899 *   above), the following search is carried out in order:
     1900 *
     1901 *   i)   If DTS passthrough is enabled then the DTS track with
     1902 *        the greatest number of audio channels is selected
     1903 *        (the first will be chosen if there are several the
     1904 *        same). If DTS passthrough is not enabled this step
     1905 *        will be skipped because internal DTS decoding is not
     1906 *        currently supported.
     1907 *
     1908 *   ii)  If no DTS track is chosen, the AC3 track with the
     1909 *        greatest number of audio channels is selected (the
     1910 *        first will be chosen if there are several the same).
     1911 *        Internal decoding of AC3 is supported, so this will
     1912 *        be used irrespective of whether AC3 passthrough is
     1913 *        enabled.
     1914 *
     1915 *   iii) Lastly the track with the greatest number of audio
     1916 *        channels irrespective of type will be selected.
    19051917 *  \return true if a track was selected, false otherwise
    19061918 */
    19071919bool AvFormatDecoder::autoSelectAudioTrack(void)
     
    19181930        AVCodecContext *codec_ctx = ic->streams[idx]->codec;
    19191931        AudioInfo item(codec_ctx->codec_id,
    19201932                       codec_ctx->sample_rate, codec_ctx->channels,
    1921                        (allow_ac3_passthru && !transcoding &&
    1922                         (codec_ctx->codec_id == CODEC_ID_AC3)));
     1933                       !transcoding &&
     1934                       (allow_ac3_passthru && codec_ctx->codec_id == CODEC_ID_AC3 ||
     1935                        allow_dts_passthru && codec_ctx->codec_id == CODEC_ID_DTS));
    19231936        VERBOSE(VB_AUDIO, LOC + " * " + item.toString());
    19241937    }
    19251938#endif
     
    19521965        for (; it !=  languagePreference.end(); ++it)
    19531966        {
    19541967            vector<int> flang = filter_lang(audioStreams, *it);
    1955             if ((selectedTrack = filter_ac3(ic, audioStreams, flang)) >= 0)
     1968            if (allow_dts_passthru &&
     1969                (selectedTrack = filter_max_ch(ic, audioStreams, flang, CODEC_ID_DTS)) >= 0)
    19561970                break;
     1971            if ((selectedTrack = filter_max_ch(ic, audioStreams, flang, CODEC_ID_AC3)) >= 0)
     1972                break;
    19571973            if ((selectedTrack = filter_max_ch(ic, audioStreams, flang)) >= 0)
    19581974                break;
    19591975        }
     
    19621978        {
    19631979            VERBOSE(VB_AUDIO, LOC + "Trying to select audio track (wo/lang)");
    19641980            vector<int> flang = filter_lang(audioStreams, -1);
    1965             if ((selectedTrack = filter_ac3(ic, audioStreams, flang)) < 0)
     1981            if ((!allow_dts_passthru ||
     1982                 (selectedTrack = filter_max_ch(ic, audioStreams, flang, CODEC_ID_DTS)) < 0) &&
     1983                (selectedTrack = filter_max_ch(ic, audioStreams, flang, CODEC_ID_AC3)) < 0)
    19661984                selectedTrack  = filter_max_ch(ic, audioStreams, flang);
    19671985        }
    19681986    }
     
    23652383                    pthread_mutex_lock(&avcodeclock);
    23662384                    ret = len;
    23672385                    data_size = 0;
    2368                     if (audioOut.do_ac3_passthru)
     2386                    if (audioOut.do_passthru)
    23692387                    {
    23702388                        data_size = pkt->size;
    23712389                        ret = EncodeAC3Frame(ptr, len, audioSamples,
     
    23972415                    }
    23982416                    pthread_mutex_unlock(&avcodeclock);
    23992417
    2400                     ptr += ret;
    2401                     len -= ret;
    24022418
    24032419                    if (data_size <= 0)
     2420                    {
     2421                        ptr += ret;
     2422                        len -= ret;
    24042423                        continue;
     2424                    }
    24052425
    24062426                    long long temppts = lastapts;
    24072427
     
    25992619    {
    26002620        assert(curstream);
    26012621        assert(curstream->codec);
    2602         codec_ctx = curstream->codec;       
    2603         bool do_ac3_passthru = (allow_ac3_passthru && !transcoding &&
    2604                                 (codec_ctx->codec_id == CODEC_ID_AC3));
     2622        codec_ctx = curstream->codec;
     2623        bool do_passthru = !transcoding &&
     2624                           (allow_ac3_passthru && codec_ctx->codec_id == CODEC_ID_AC3 ||
     2625                            allow_dts_passthru && codec_ctx->codec_id == CODEC_ID_DTS);
    26052626        info = AudioInfo(codec_ctx->codec_id,
    26062627                         codec_ctx->sample_rate, codec_ctx->channels,
    2607                          do_ac3_passthru);
     2628                         do_passthru);
    26082629    }
    26092630
    26102631    if (info == audioIn)
     
    26142635            QString("audio track #%1").arg(currentAudioTrack+1));
    26152636
    26162637    audioOut = audioIn = info;
    2617     if (audioIn.do_ac3_passthru)
     2638    if (audioIn.do_passthru)
    26182639    {
    26192640        // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card
    26202641        audioOut.channels    = 2;
     
    26512672{
    26522673    int enc_len;
    26532674    int flags, sample_rate, bit_rate;
     2675    int nr_samples = 0, block_len;
     2676    bool dts = false;
    26542677    unsigned char* ucsamples = (unsigned char*) samples;
    26552678
    26562679    // we don't do any length/crc validation of the AC3 frame here; presumably
     
    26612684    // ignore, and if so, may as well just assume that it will ignore
    26622685    // anything with a bad CRC...
    26632686
    2664     enc_len = a52_syncinfo(data, &flags, &sample_rate, &bit_rate);
     2687    enc_len = dts_syncinfo(data, &flags, &sample_rate, &bit_rate);
     2688    if(enc_len >= 10)
     2689    {
     2690        dts = true;
     2691        int rate, sfreq, nblks;
     2692        dts_decode_header(data, &rate, &nblks, &sfreq);
     2693        nr_samples = nblks * 32;
     2694        block_len = nr_samples * 2 * 2;
     2695    }
     2696    else
     2697    {
     2698        enc_len = a52_syncinfo(data, &flags, &sample_rate, &bit_rate);
     2699        block_len = MAX_AC3_FRAME_SIZE;
     2700    }
    26652701
    26662702    if (enc_len == 0 || enc_len > len)
    26672703    {
     
    26692705        return len;
    26702706    }
    26712707
    2672     if (enc_len > MAX_AC3_FRAME_SIZE - 8)
    2673         enc_len = MAX_AC3_FRAME_SIZE - 8;
     2708    if (enc_len > block_len - 8)
     2709    {
     2710        enc_len = block_len - 8;
     2711    }
    26742712
    26752713    swab(data, ucsamples + 8, enc_len);
    26762714
    2677     // the following values come from ao_hwac3.c in mplayer.
     2715    // the following values come from libmpcodecs/ad_hwac3.c in mplayer.
    26782716    // they form a valid IEC958 AC3 header.
    26792717    ucsamples[0] = 0x72;
    26802718    ucsamples[1] = 0xF8;
    26812719    ucsamples[2] = 0x1F;
    26822720    ucsamples[3] = 0x4E;
    2683     ucsamples[4] = 0x01;
     2721    if(dts)
     2722    {
     2723        switch(nr_samples)
     2724        {
     2725            case 512:
     2726                ucsamples[4] = 0x0B;      /* DTS-1 (512-sample bursts) */
     2727                break;
     2728
     2729            case 1024:
     2730                ucsamples[4] = 0x0C;      /* DTS-2 (1024-sample bursts) */
     2731                break;
     2732
     2733            case 2048:
     2734                ucsamples[4] = 0x0D;      /* DTS-3 (2048-sample bursts) */
     2735                break;
     2736
     2737            default:
     2738                VERBOSE(VB_IMPORTANT, LOC +
     2739                        QString("DTS: %1-sample bursts not supported")
     2740                        .arg(nr_samples));
     2741                ucsamples[4] = 0x00;
     2742                break;
     2743        }
     2744    }
     2745    else
     2746    {
     2747        ucsamples[4] = 0x01;
     2748    }
    26842749    ucsamples[5] = 0x00;
    26852750    ucsamples[6] = (enc_len << 3) & 0xFF;
    26862751    ucsamples[7] = (enc_len >> 5) & 0xFF;
    2687     memset(ucsamples + 8 + enc_len, 0, MAX_AC3_FRAME_SIZE - 8 - enc_len);
    2688     samples_size = MAX_AC3_FRAME_SIZE;
     2752    memset(ucsamples + 8 + enc_len, 0, block_len - 8 - enc_len);
     2753    samples_size = block_len;
    26892754
    2690     return len;  // consume whole frame even if len > enc_len ?
     2755    return enc_len;
    26912756}
    26922757
    26932758void AvFormatDecoder::AddTextData(unsigned char *buf, int len,
     
    26952760{
    26962761    m_parent->AddTextData((char*)buf, len, timecode, type);
    26972762}
     2763 
     2764static int DTS_SAMPLEFREQS[16] =
     2765{
     2766    0,
     2767    8000,
     2768    16000,
     2769    32000,
     2770    64000,
     2771    128000,
     2772    11025,
     2773    22050,
     2774    44100,
     2775    88200,
     2776    176400,
     2777    12000,
     2778    24000,
     2779    48000,
     2780    96000,
     2781    192000
     2782};
     2783
     2784static int DTS_BITRATES[30] =
     2785{
     2786    32000,
     2787    56000,
     2788    64000,
     2789    96000,
     2790    112000,
     2791    128000,
     2792    192000,
     2793    224000,
     2794    256000,
     2795    320000,
     2796    384000,
     2797    448000,
     2798    512000,
     2799    576000,
     2800    640000,
     2801    768000,
     2802    896000,
     2803    1024000,
     2804    1152000,
     2805    1280000,
     2806    1344000,
     2807    1408000,
     2808    1411200,
     2809    1472000,
     2810    1536000,
     2811    1920000,
     2812    2048000,
     2813    3072000,
     2814    3840000,
     2815    4096000
     2816};
     2817
     2818static int dts_syncinfo(uint8_t *indata_ptr, int */*flags*/, int *sample_rate, int *bit_rate)
     2819{
     2820    int nblks;
     2821    int rate;
     2822    int sfreq;
     2823
     2824    int fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
     2825    if(fsize >= 0)
     2826    {
     2827        if(rate >= 0 && rate <= 29)
     2828            *bit_rate = DTS_BITRATES[rate];
     2829        else
     2830            *bit_rate = 0;
     2831        if(sfreq >= 1 && sfreq <= 15)
     2832            *sample_rate = DTS_SAMPLEFREQS[sfreq];
     2833        else
     2834            *sample_rate = 0;
     2835    }
     2836    return fsize;
     2837}
     2838
     2839static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq)
     2840{
     2841    if(((indata_ptr[0] << 24) | (indata_ptr[1] << 16) | (indata_ptr[2] << 8)
     2842        | (indata_ptr[3])) != 0x7ffe8001)
     2843        return -1;
     2844
     2845    int ftype = indata_ptr[4] >> 7;
     2846
     2847    int surp = (indata_ptr[4] >> 2) & 0x1f;
     2848    surp = (surp + 1) % 32;
     2849
     2850    *nblks = (indata_ptr[4] & 0x01) << 6 | (indata_ptr[5] >> 2);
     2851    *nblks = *nblks + 1;
     2852
     2853    int fsize = (indata_ptr[5] & 0x03) << 12 | (indata_ptr[6] << 4) | (indata_ptr[7] >> 4);
     2854    fsize = fsize + 1;
     2855
     2856    *sfreq = (indata_ptr[8] >> 2) & 0x0f;
     2857    *rate = (indata_ptr[8] & 0x03) << 3 | ((indata_ptr[9] >> 5) & 0x07);
     2858
     2859    if(ftype != 1)
     2860    {
     2861        VERBOSE(VB_IMPORTANT, LOC +
     2862                QString("DTS: Termination frames not handled (ftype %1)").arg(ftype));
     2863        return -1;
     2864    }
     2865
     2866    if(*sfreq != 13)
     2867    {
     2868        VERBOSE(VB_IMPORTANT, LOC +
     2869                QString("DTS: Only 48kHz supported (sfreq %1)").arg(*sfreq));
     2870        return -1;
     2871    }
     2872
     2873    if((fsize > 8192) || (fsize < 96))
     2874    {
     2875        VERBOSE(VB_IMPORTANT, LOC +
     2876                QString("DTS: fsize: %1 invalid").arg(fsize));
     2877        return -1;
     2878    }
     2879
     2880    if(*nblks != 8 &&
     2881       *nblks != 16 &&
     2882       *nblks != 32 &&
     2883       *nblks != 64 &&
     2884       *nblks != 128 &&
     2885       ftype == 1)
     2886    {
     2887        VERBOSE(VB_IMPORTANT, LOC +
     2888                QString("DTS: nblks %1 not valid for normal frame").arg(*nblks));
     2889        return -1;
     2890    }
     2891
     2892    return fsize;
     2893}
  • libs/libmythtv/avformatdecoder.h

     
    3838  public:
    3939    AudioInfo() :
    4040        codec_id(CODEC_ID_NONE), sample_size(-2),   sample_rate(-1),
    41         channels(-1), do_ac3_passthru(false)
     41        channels(-1), do_passthru(false)
    4242    {;}
    4343
    44     AudioInfo(CodecID id, int sr, int ch, bool ac3) :
     44    AudioInfo(CodecID id, int sr, int ch, bool passthru) :
    4545        codec_id(id), sample_size(ch*2),   sample_rate(sr),
    46         channels(ch), do_ac3_passthru(ac3)
     46        channels(ch), do_passthru(passthru)
    4747    {;}
    4848
    4949    CodecID codec_id;
    5050    int sample_size, sample_rate, channels;
    51     bool do_ac3_passthru;
     51    bool do_passthru;
    5252
    5353    /// \brief Bits per sample.
    54     int bps(void) const { return (8 * sample_size) / channels; }
     54    int bps(void) const { return channels ? (8 * sample_size) / channels : 0; }
    5555    bool operator==(const AudioInfo &o) const
    5656    {
    5757        return (codec_id==o.codec_id        && channels==o.channels       &&
    5858                sample_size==o.sample_size  && sample_rate==o.sample_rate &&
    59                 do_ac3_passthru==o.do_ac3_passthru);
     59                do_passthru==o.do_passthru);
    6060    }
    6161    QString toString() const
    6262    {
    6363        return QString("id(%1) %2Hz %3ch %4bps%5")
    6464            .arg(codec_id_string(codec_id),4).arg(sample_rate,5)
    6565            .arg(channels,2).arg(bps(),3)
    66             .arg((do_ac3_passthru) ? "pt":"",3);
     66            .arg((do_passthru) ? "pt":"",3);
    6767    }
    6868};
    6969
     
    139139
    140140  protected:
    141141    /// Attempt to find the optimal audio stream to use based on the number of channels,
    142     /// and if we're doing AC3 passthrough.  This will select the highest stream number
    143     /// that matches our criteria.
     142    /// and if we're doing AC3/DTS passthrough.  This will select the highest stream
     143    /// number that matches our criteria.
    144144    bool autoSelectAudioTrack();
    145145
    146146    bool autoSelectSubtitleTrack();
     
    237237    // Audio
    238238    short int        *audioSamples;
    239239    bool              allow_ac3_passthru;
     240    bool              allow_dts_passthru;
    240241
    241242    AudioInfo         audioIn;
    242243    AudioInfo         audioOut;
  • libs/libavformat/mpeg.c

     
    8888#define AUDIO_ID 0xc0
    8989#define VIDEO_ID 0xe0
    9090#define AC3_ID   0x80
    91 #define DTS_ID   0x8a
     91#define DTS_ID   0x88
    9292#define LPCM_ID  0xa0
    9393#define SUB_ID   0x20
    9494
  • programs/mythfrontend/globalsettings.cpp

     
    133133    return gc;
    134134}
    135135
     136#ifdef CONFIG_DTS
     137static HostCheckBox *DTSPassThrough()
     138{
     139    HostCheckBox *gc = new HostCheckBox("DTSPassThru");
     140    gc->setLabel(QObject::tr("Enable DTS to SPDIF passthrough"));
     141    gc->setValue(false);
     142    gc->setHelpText(QObject::tr("Enable sending DTS audio directly to your "
     143                    "sound card's SPDIF output, on sources which contain "
     144                    "DTS soundtracks (usually DVDs).  Requires that the "
     145                    "audio output device be set to something suitable."));
     146    return gc;
     147}
     148#endif
     149
    136150static HostCheckBox *Deinterlace()
    137151{
    138152    HostCheckBox *gc = new HostCheckBox("Deinterlace");
     
    20692083
    20702084         addChild(AudioOutputDevice());
    20712085         addChild(AC3PassThrough());
     2086#ifdef CONFIG_DTS
     2087         addChild(DTSPassThrough());
     2088#endif
    20722089         addChild(AggressiveBuffer());
    20732090
    20742091         Setting* volumeControl = MythControlsVolume();