Click here to Skip to main content
Click here to Skip to main content

DirectShow - Fine TV Tuning Using IKsPropertySet.

, 20 Aug 2004
Rate this:
Please Sign up or sign in to vote.
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 Wink | ;) .

// 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

Share

About the Author

Liaqat Fayyaz
Team Leader V One Multimedia
Singapore Singapore
No Biography provided

Comments and Discussions

 
SuggestionNo Frequency Change in tuner PinmemberMember 76604076-Jun-14 1:22 
GeneralSet TV Tuner Audio Frequency PinmemberDeath_Star_LAG28-Jan-11 8:55 
GeneralRe: Set TV Tuner Audio Frequency PinmemberMember 76604076-Jun-14 1:25 
GeneralKSPROPERTY_VIDEOPROCAMP_POWERLINE_FREQUENCY can not be set PinmemberQuyLV2-Mar-09 21:17 
GeneralPoor TV Quality Pinmemberammar7915-Aug-07 8:33 
Generala msg from ragavan PinstaffSean Ewington30-Apr-07 5:29 
Questionis there any other method to play TV using TV tuner card PinmemberLalit N Dubey21-Mar-07 20:35 
Generalpoor TV Graphic /picture quality problem PinmemberLalit N Dubey19-Mar-07 21:50 
GeneralRe: poor TV Graphic /picture quality problem PinmemberLiaqat Fayyaz20-Mar-07 0:06 
GeneralRe: poor TV Graphic /picture quality problem PinmemberLalit N Dubey21-Mar-07 15:15 
GeneralRe: poor TV Graphic /picture quality problem Pinmemberilvesh8-Sep-09 20:25 
QuestionHow can it have sound ? Pinmemberngochuy22-Feb-07 22:59 
AnswerRe: How can it have sound ? PinmemberLalit N Dubey19-Mar-07 21:54 
GeneralTv tuner vc++ 7.0 sample project (setfrequency included) Pinmembertakyonxxx8-Mar-06 4:12 
GeneralRe: Tv tuner vc++ 7.0 sample project (setfrequency included) Pinmemberkang gu joon1-Oct-06 20:20 
GeneralRe: Tv tuner vc++ 7.0 sample project (setfrequency included) Pinmemberngochuy22-Feb-07 22:38 
QuestionKSPROPERTY_TUNER_MODE_CAPS_S seems to be error? Pinmemberjarba7-Mar-06 23:24 
QuestionAccess with VB PinmemberLameHack8-Nov-05 14:52 
GeneralProblems with fine tuning and SignalPresent Pinsussrazvar27-Jun-05 1:56 
GeneralRe: Problems with fine tuning and SignalPresent PinmemberLiaqat Fayyaz27-Jun-05 2:34 
GeneralProject Files or C# Alternate Pinmemberyateee11-Mar-05 19:58 
GeneralRe: Project Files or C# Alternate Pinmemberhamdyali12-Jun-05 5:33 
GeneralRe: Project Files or C# Alternate Pinmemberalmere10922-Dec-05 1:53 
QuestionRe: Project Files or C# Alternate Pinmembertroedi9-Jan-07 3:45 
AnswerRe: Project Files or C# Alternate Pinmemberalmere1099-Jan-07 8:47 
GeneralRe: Project Files or C# Alternate Pinmembertroedi9-Jan-07 9:49 
GeneralRe: Project Files or C# Alternate Pinmemberalmere1099-Jan-07 11:57 
GeneralRe: Project Files or C# Alternate Pinmembertroedi12-Jan-07 3:56 
GeneralRe: Project Files or C# Alternate Pinmemberalmere10912-Jan-07 5:41 
GeneralRe: Project Files or C# Alternate Pinmemberalmere10915-Jan-07 11:42 
GeneralRe: Project Files or C# Alternate Pinmembertroedi15-Jan-07 11:51 
GeneralRe: Project Files or C# Alternate Pinmemberalmere10915-Jan-07 20:36 
GeneralRe: Project Files or C# Alternate Pinmemberalmere10923-Jan-07 10:56 
GeneralRe: Project Files or C# Alternate Pinmembertroedi25-Jan-07 23:02 
GeneralRe: Project Files or C# Alternate Pinmemberalmere10926-Jan-07 7:19 
GeneralRe: Project Files or C# Alternate Pinmembertroedi8-Feb-07 20:06 
GeneralRe: Project Files or C# Alternate Pinmemberalmere1099-Feb-07 7:58 
QuestionManual (Dirty Edit) of Tuner Filter? PinsussAnon_2004093029-Sep-04 16:00 
AnswerRe: Manual (Dirty Edit) of Tuner Filter? PinmemberRui Godinho Lopes4-Oct-04 9:55 
GeneralRe: Manual (Dirty Edit) of Tuner Filter? Pinmembersaruman2-May-05 1:20 
General[Message Deleted] PinmemberKyle Halse15-Sep-04 21:16 
GeneralRe: GetFrequency... PinmemberLiaqat Fayyaz15-Sep-04 21:44 
GeneralRe: GetFrequency... Pinmembertakyonxxx10-Mar-06 0:26 
QuestionMissing project files? PinsussAnonymous2-Sep-04 7:15 
AnswerRe: Missing project files? PinmemberLiaqat Fayyaz2-Sep-04 19:06 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 21 Aug 2004
Article Copyright 2004 by Liaqat Fayyaz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid