Ticket #730: mythtv-dtspassthru.2.patch
File mythtv-dtspassthru.2.patch, 18.6 KB (added by , 18 years ago) |
---|
-
libs/libmythtv/avformatdecoder.cpp
35 35 36 36 #define MAX_AC3_FRAME_SIZE 6144 37 37 38 static int dts_syncinfo(uint8_t *indata_ptr, int *flags, int *sample_rate, int *bit_rate); 39 static int dts_decode_header(uint8_t *indata_ptr, int *rate, int *nblks, int *sfreq); 40 38 41 /** Set to zero to allow any number of AC3 channels. */ 39 42 #define MAX_OUTPUT_CHANNELS 2 40 43 … … 180 183 // Audio 181 184 audioSamples(new short int[AVCODEC_MAX_AUDIO_FRAME_SIZE]), 182 185 allow_ac3_passthru(false), 186 allow_dts_passthru(false), 183 187 // Audio selection 184 188 wantedAudioStream(), selectedAudioStream(), 185 189 // Subtitle selection … … 196 200 197 201 save_cctc[0] = save_cctc[1] = 0; 198 202 allow_ac3_passthru = gContext->GetNumSetting("AC3PassThru", false); 203 allow_dts_passthru = gContext->GetNumSetting("DTSPassThru", false); 199 204 200 205 audioIn.sample_size = -32; // force SetupAudioStream to run once 201 206 } … … 946 951 <<") already open, leaving it alone."); 947 952 } 948 953 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 } 949 960 bitrate += enc->bit_rate; 950 961 break; 951 962 } … … 1838 1849 return ret; 1839 1850 } 1840 1851 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 1862 1852 int filter_max_ch(const AVFormatContext *ic, 1863 1853 const sinfo_vec_t &audioStreams, 1864 const vector<int> &fs) 1854 const vector<int> &fs, 1855 enum CodecID codecId = CODEC_ID_NONE) 1865 1856 { 1866 1857 int selectedTrack = -1, max_seen = -1; 1867 1858 … … 1870 1861 { 1871 1862 const int stream_index = audioStreams[*it].av_stream_index; 1872 1863 const AVCodecContext *ctx = ic->streams[stream_index]->codec; 1873 if ( max_seen < ctx->channels)1864 if (codecId == CODEC_ID_NONE || codecId == ctx->codec_id) 1874 1865 { 1875 selectedTrack = *it; 1876 max_seen = ctx->channels; 1866 if (max_seen < ctx->channels) 1867 { 1868 selectedTrack = *it; 1869 max_seen = ctx->channels; 1870 } 1877 1871 } 1878 1872 } 1879 1873 … … 1883 1877 /** \fn AvFormatDecoder::autoSelectAudioTrack(void) 1884 1878 * \brief Selects the best audio track. 1885 1879 * 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: 1888 1883 * 1889 * First we try to select the stream last selected by the1890 * user, which is recalled as the Nth stream in the1891 * preferred language. If it can not be located we attempt1892 * to find a streamin 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. 1893 1888 * 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. 1899 1893 * 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. 1905 1917 * \return true if a track was selected, false otherwise 1906 1918 */ 1907 1919 bool AvFormatDecoder::autoSelectAudioTrack(void) … … 1918 1930 AVCodecContext *codec_ctx = ic->streams[idx]->codec; 1919 1931 AudioInfo item(codec_ctx->codec_id, 1920 1932 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)); 1923 1936 VERBOSE(VB_AUDIO, LOC + " * " + item.toString()); 1924 1937 } 1925 1938 #endif … … 1952 1965 for (; it != languagePreference.end(); ++it) 1953 1966 { 1954 1967 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) 1956 1970 break; 1971 if ((selectedTrack = filter_max_ch(ic, audioStreams, flang, CODEC_ID_AC3)) >= 0) 1972 break; 1957 1973 if ((selectedTrack = filter_max_ch(ic, audioStreams, flang)) >= 0) 1958 1974 break; 1959 1975 } … … 1962 1978 { 1963 1979 VERBOSE(VB_AUDIO, LOC + "Trying to select audio track (wo/lang)"); 1964 1980 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) 1966 1984 selectedTrack = filter_max_ch(ic, audioStreams, flang); 1967 1985 } 1968 1986 } … … 2365 2383 pthread_mutex_lock(&avcodeclock); 2366 2384 ret = len; 2367 2385 data_size = 0; 2368 if (audioOut.do_ ac3_passthru)2386 if (audioOut.do_passthru) 2369 2387 { 2370 2388 data_size = pkt->size; 2371 2389 ret = EncodeAC3Frame(ptr, len, audioSamples, … … 2397 2415 } 2398 2416 pthread_mutex_unlock(&avcodeclock); 2399 2417 2400 ptr += ret;2401 len -= ret;2402 2418 2403 2419 if (data_size <= 0) 2420 { 2421 ptr += ret; 2422 len -= ret; 2404 2423 continue; 2424 } 2405 2425 2406 2426 long long temppts = lastapts; 2407 2427 … … 2599 2619 { 2600 2620 assert(curstream); 2601 2621 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); 2605 2626 info = AudioInfo(codec_ctx->codec_id, 2606 2627 codec_ctx->sample_rate, codec_ctx->channels, 2607 do_ ac3_passthru);2628 do_passthru); 2608 2629 } 2609 2630 2610 2631 if (info == audioIn) … … 2614 2635 QString("audio track #%1").arg(currentAudioTrack+1)); 2615 2636 2616 2637 audioOut = audioIn = info; 2617 if (audioIn.do_ ac3_passthru)2638 if (audioIn.do_passthru) 2618 2639 { 2619 2640 // A passthru stream looks like a 48KHz 2ch (@ 16bit) to the sound card 2620 2641 audioOut.channels = 2; … … 2651 2672 { 2652 2673 int enc_len; 2653 2674 int flags, sample_rate, bit_rate; 2675 int nr_samples = 0, block_len; 2676 bool dts = false; 2654 2677 unsigned char* ucsamples = (unsigned char*) samples; 2655 2678 2656 2679 // we don't do any length/crc validation of the AC3 frame here; presumably … … 2661 2684 // ignore, and if so, may as well just assume that it will ignore 2662 2685 // anything with a bad CRC... 2663 2686 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 } 2665 2701 2666 2702 if (enc_len == 0 || enc_len > len) 2667 2703 { … … 2669 2705 return len; 2670 2706 } 2671 2707 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 } 2674 2712 2675 2713 swab(data, ucsamples + 8, enc_len); 2676 2714 2677 // the following values come from ao_hwac3.c in mplayer.2715 // the following values come from libmpcodecs/ad_hwac3.c in mplayer. 2678 2716 // they form a valid IEC958 AC3 header. 2679 2717 ucsamples[0] = 0x72; 2680 2718 ucsamples[1] = 0xF8; 2681 2719 ucsamples[2] = 0x1F; 2682 2720 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 } 2684 2749 ucsamples[5] = 0x00; 2685 2750 ucsamples[6] = (enc_len << 3) & 0xFF; 2686 2751 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; 2689 2754 2690 return len; // consume whole frame even if len > enc_len ?2755 return enc_len; 2691 2756 } 2692 2757 2693 2758 void AvFormatDecoder::AddTextData(unsigned char *buf, int len, … … 2695 2760 { 2696 2761 m_parent->AddTextData((char*)buf, len, timecode, type); 2697 2762 } 2763 2764 static 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 2784 static 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 2818 static 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 2839 static 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
38 38 public: 39 39 AudioInfo() : 40 40 codec_id(CODEC_ID_NONE), sample_size(-2), sample_rate(-1), 41 channels(-1), do_ ac3_passthru(false)41 channels(-1), do_passthru(false) 42 42 {;} 43 43 44 AudioInfo(CodecID id, int sr, int ch, bool ac3) :44 AudioInfo(CodecID id, int sr, int ch, bool passthru) : 45 45 codec_id(id), sample_size(ch*2), sample_rate(sr), 46 channels(ch), do_ ac3_passthru(ac3)46 channels(ch), do_passthru(passthru) 47 47 {;} 48 48 49 49 CodecID codec_id; 50 50 int sample_size, sample_rate, channels; 51 bool do_ ac3_passthru;51 bool do_passthru; 52 52 53 53 /// \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; } 55 55 bool operator==(const AudioInfo &o) const 56 56 { 57 57 return (codec_id==o.codec_id && channels==o.channels && 58 58 sample_size==o.sample_size && sample_rate==o.sample_rate && 59 do_ ac3_passthru==o.do_ac3_passthru);59 do_passthru==o.do_passthru); 60 60 } 61 61 QString toString() const 62 62 { 63 63 return QString("id(%1) %2Hz %3ch %4bps%5") 64 64 .arg(codec_id_string(codec_id),4).arg(sample_rate,5) 65 65 .arg(channels,2).arg(bps(),3) 66 .arg((do_ ac3_passthru) ? "pt":"",3);66 .arg((do_passthru) ? "pt":"",3); 67 67 } 68 68 }; 69 69 … … 139 139 140 140 protected: 141 141 /// 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 number143 /// 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. 144 144 bool autoSelectAudioTrack(); 145 145 146 146 bool autoSelectSubtitleTrack(); … … 237 237 // Audio 238 238 short int *audioSamples; 239 239 bool allow_ac3_passthru; 240 bool allow_dts_passthru; 240 241 241 242 AudioInfo audioIn; 242 243 AudioInfo audioOut; -
libs/libavformat/mpeg.c
88 88 #define AUDIO_ID 0xc0 89 89 #define VIDEO_ID 0xe0 90 90 #define AC3_ID 0x80 91 #define DTS_ID 0x8 a91 #define DTS_ID 0x88 92 92 #define LPCM_ID 0xa0 93 93 #define SUB_ID 0x20 94 94 -
programs/mythfrontend/globalsettings.cpp
133 133 return gc; 134 134 } 135 135 136 #ifdef CONFIG_DTS 137 static 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 136 150 static HostCheckBox *Deinterlace() 137 151 { 138 152 HostCheckBox *gc = new HostCheckBox("Deinterlace"); … … 2069 2083 2070 2084 addChild(AudioOutputDevice()); 2071 2085 addChild(AC3PassThrough()); 2086 #ifdef CONFIG_DTS 2087 addChild(DTSPassThrough()); 2088 #endif 2072 2089 addChild(AggressiveBuffer()); 2073 2090 2074 2091 Setting* volumeControl = MythControlsVolume();