Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

DirectShow - Fine TV Tuning Using IKsPropertySet.

Rate me:
Please Sign up or sign in to vote.
4.88/5 (19 votes)
20 Aug 20043 min read 209.5K   69   45
How to fine tune a TV channel in DirectShow / How to set custom video frequencies of TV tuner devices?

Introduction

If you are developing a TV tuner application for your tuner device using Direct Show, then you will definitely look for some function which can perform fine TV tuning (i.e., you want to set video frequency manually to get the precise video signal on a specific TV channel), but unfortunately, there in no such function in standard DirectShow TV tuning interfaces IAMTuner and IAMTVTuner.

The DirectShow TV tuning architecture is based on pre-defined frequency tables, and the TV Tuner filter (kstvtune.ax) has an internal list of frequency tables. Each frequency table contains a list of frequencies, and corresponds to the broadcast or cable frequencies for a given country/region. So, when you use the IAMTuner interface, it gets the channel frequency values from the internal frequency tables according to the selected country/ region and tunes the TV channels.

Because the IAMTuner interface will not allow you to modify the frequency values for a specific channel manually, you can't fine tune a channel while previewing. However, there is an AutoTune function available in IAMTVTuner interface which can help you to fine tune a channel, but this will also give you no control for manual fine tuning on the fly or to set custom video frequencies.

But the following code will describe you the complete details of fine tuning on the fly using IKsPropertySet. I am writing this article only to help those who really want to get some clue on fine TV tuning because I know how difficult it is to get help on this topic. Even, you will not find a single function in the whole DirectShow documentation which can help you on this.

While writing this code, I assumed that the reader has good knowledge of DirectShow video capturing and know the details of DirectShow graph building techniques, filers, and TV tuner graphs. I hope the DirectShow guys will be happy after reading this article.

This is my first article on Code Project, so some of you may find it difficult to read ;).

// It is assumed that you have constructed the complete TV tuner Graph 
// of your Device.And will use this function to set custom frequencies 
// or to fine tune a tuned TV channel.Please also make sure to set the 
// appropriate Video standard and contry code before calling this function.

#define INSTANCEDATA_OF_PROPERTY_PTR(x) ((PKSPROPERTY((x))) + 1)
#define INSTANCEDATA_OF_PROPERTY_SIZE(x) (sizeof((x)) - sizeof(KSPROPERTY))

HRESULT SetFrequency(long Freq)
{ 
    HRESULT hr;
    DWORD dwSupported=0;  

    // Query the IKsPropertySet on your Device TV Tuner Filter.
    // m_pTvtuner is IBaseFilter Pointer of your TV Tuner Filter.   

    CComPtr m_pKSProp;
    hr = m_pTvtuner->QueryInterface(IID_IKsPropertySet, (void**)&m_pKSProp); 
    if (FAILED(hr))
        return E_FAIL;

    KSPROPERTY_TUNER_MODE_CAPS_S ModeCaps;
    KSPROPERTY_TUNER_FREQUENCY_S Frequency;
    memset(&ModeCaps,0,sizeof(KSPROPERTY_TUNER_MODE_CAPS_S));
    memset(&Frequency,0,sizeof(KSPROPERTY_TUNER_FREQUENCY_S));
    ModeCaps.Mode = AMTUNER_MODE_TV; 

    // Check either the Property is supported or not by the Tuner drivers 

    hr = m_pKSProp->QuerySupported(PROPSETID_TUNER, 
          KSPROPERTY_TUNER_MODE_CAPS,&dwSupported);
    if(SUCCEEDED(hr) && dwSupported&KSPROPERTY_SUPPORT_GET)
    {
        DWORD cbBytes=0;
        hr = m_pKSProp->Get(PROPSETID_TUNER,KSPROPERTY_TUNER_MODE_CAPS,
            INSTANCEDATA_OF_PROPERTY_PTR(&ModeCaps),
            INSTANCEDATA_OF_PROPERTY_SIZE(ModeCaps),
            &ModeCaps,
            sizeof(ModeCaps),
            &cbBytes);  
    }
    else
        return E_FAIL; 

    Frequency.Frequency=Freq;
    if(ModeCaps.Strategy==KS_TUNER_STRATEGY_DRIVER_TUNES)
        Frequency.TuningFlags=KS_TUNER_TUNING_FINE;
    else
        Frequency.TuningFlags=KS_TUNER_TUNING_EXACT;

    // Here the real magic starts

    if(Freq>=ModeCaps.MinFrequency && Freq<=ModeCaps.MaxFrequency)
    {
        hr = m_pKSProp->Set(PROPSETID_TUNER,
            KSPROPERTY_TUNER_FREQUENCY,
            INSTANCEDATA_OF_PROPERTY_PTR(&Frequency),
            INSTANCEDATA_OF_PROPERTY_SIZE(Frequency),
            &Frequency,
            sizeof(Frequency));
        if(FAILED(hr))
            return E_FAIL;  
    }
    else
        return E_FAIL;

    return S_OK;
}

Explanation of Code

The SetFrequency functions set the video frequency of a tuner device, the only parameter Freq is the desired video frequency in Hz. The code is using IKsPropertySet interface to perform the desired operations, IKsPropertySet interface is part of the WDM KS (Win32® Driver Model Kernel Streaming) architecture and allows drivers to provide extended capabilities that can be used without API extensions.

The basic 3 steps performed by this code are..

  1. Check the support of IKsPropertySet on Tuner Drivers.
  2. Check the PROPSETID_TUNER property on Tuner Drivers.
  3. Set the user defined video frequency on tuner in Hz.

Using the Code

The SetFrequency function can be used in several ways, i.e., fine TV tuning, manual TV tuning, new TV channels tuning, and etc.

  1. Fine tuning of a tuned TV channel.
    • Call IAMTuner::put_Channel to set the desired TV channel.
    • Call IAMTVTuner::get_VideoFrequency to get the current video frequency.
    • Call SetFrequency by adding the desired +/- frequency value in the original channel frequency and perform the operation until you find the best video quality on that channel.
  2. Manual TV tuning.
    • Call the SetFrequency with your desired frequency value in Hz.
      HRESULT hr= SetFrequency( 67250000 )

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Team Leader V One Multimedia
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionNo Frequency Change in tuner Pin
Member 76604076-Jun-14 1:22
Member 76604076-Jun-14 1:22 
GeneralSet TV Tuner Audio Frequency Pin
Death_Star_LAG28-Jan-11 8:55
Death_Star_LAG28-Jan-11 8:55 
GeneralRe: Set TV Tuner Audio Frequency Pin
Member 76604076-Jun-14 1:25
Member 76604076-Jun-14 1:25 
GeneralKSPROPERTY_VIDEOPROCAMP_POWERLINE_FREQUENCY can not be set Pin
QuyLV2-Mar-09 21:17
QuyLV2-Mar-09 21:17 
GeneralPoor TV Quality Pin
ammar7915-Aug-07 8:33
ammar7915-Aug-07 8:33 
Generala msg from ragavan Pin
Sean Ewington30-Apr-07 5:29
staffSean Ewington30-Apr-07 5:29 
Questionis there any other method to play TV using TV tuner card Pin
Lalit N Dubey21-Mar-07 20:35
Lalit N Dubey21-Mar-07 20:35 
Generalpoor TV Graphic /picture quality problem Pin
Lalit N Dubey19-Mar-07 21:50
Lalit N Dubey19-Mar-07 21:50 
GeneralRe: poor TV Graphic /picture quality problem Pin
Liaqat Fayyaz20-Mar-07 0:06
Liaqat Fayyaz20-Mar-07 0:06 
GeneralRe: poor TV Graphic /picture quality problem Pin
Lalit N Dubey21-Mar-07 15:15
Lalit N Dubey21-Mar-07 15:15 
GeneralRe: poor TV Graphic /picture quality problem Pin
sammyhg8-Sep-09 20:25
sammyhg8-Sep-09 20:25 
QuestionHow can it have sound ? Pin
ngochuy22-Feb-07 22:59
ngochuy22-Feb-07 22:59 
AnswerRe: How can it have sound ? Pin
Lalit N Dubey19-Mar-07 21:54
Lalit N Dubey19-Mar-07 21:54 
GeneralTv tuner vc++ 7.0 sample project (setfrequency included) Pin
takyonxxx8-Mar-06 4:12
takyonxxx8-Mar-06 4:12 
GeneralRe: Tv tuner vc++ 7.0 sample project (setfrequency included) Pin
vckang1-Oct-06 20:20
vckang1-Oct-06 20:20 
GeneralRe: Tv tuner vc++ 7.0 sample project (setfrequency included) Pin
ngochuy22-Feb-07 22:38
ngochuy22-Feb-07 22:38 
Thanks, but i couldn't get it (maybe it's died).

Can you send Turkay_TV.zip to my mail ? (my mail: hongochuy@gmail.com)

Thanks very much.
QuestionKSPROPERTY_TUNER_MODE_CAPS_S seems to be error? Pin
jarba7-Mar-06 23:24
jarba7-Mar-06 23:24 
QuestionAccess with VB Pin
LameHack8-Nov-05 14:52
LameHack8-Nov-05 14:52 
GeneralProblems with fine tuning and SignalPresent Pin
razvar27-Jun-05 1:56
razvar27-Jun-05 1:56 
GeneralRe: Problems with fine tuning and SignalPresent Pin
Liaqat Fayyaz27-Jun-05 2:34
Liaqat Fayyaz27-Jun-05 2:34 
GeneralProject Files or C# Alternate Pin
yateee11-Mar-05 19:58
yateee11-Mar-05 19:58 
GeneralRe: Project Files or C# Alternate Pin
hamdyali12-Jun-05 5:33
hamdyali12-Jun-05 5:33 
GeneralRe: Project Files or C# Alternate Pin
almere10922-Dec-05 1:53
almere10922-Dec-05 1:53 
QuestionRe: Project Files or C# Alternate Pin
troedi9-Jan-07 3:45
troedi9-Jan-07 3:45 
AnswerRe: Project Files or C# Alternate Pin
almere1099-Jan-07 8:47
almere1099-Jan-07 8:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.