Windows > Software Development for Windows Client Forums > DirectShow Development > How to setup CSourceStream::GetMediaType for MPEG2-PS?
Ask a questionAsk a question
 

QuestionHow to setup CSourceStream::GetMediaType for MPEG2-PS?

  • Thursday, November 05, 2009 4:43 PMmynameisnotcarlos Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi everyone,

    can someone tell me or link me to some information on how to check what is necessary to setup the media type for a MPEG2-PS?
    I thought about only setting the major- and subtype (MEDIATTYPE_Stream, MEDIASUBTYPE_MPEG2_PROGRAM) but leave everything else in their initialization status. So there wouldn't be any info about the format nor would I allocate any buffer for the format. Would that even work? Oh, of course I would have to set the sample size, buffer size in allocator. How big would that be in my case? Does it depend on the content of the program stream?

    What I'm basicly up to is to write an UDP source filter which receives a network stream as an mpeg2 ps. All I want to do is to forward my received data downstream to the demux / mpeg splitter.

    I hope you could follow me, otherwise I'll try to be more pricise if something's not clear.

    Thank you!

All Replies

  • Friday, November 06, 2009 10:27 AMmynameisnotcarlos Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I now gave the filter a try in graphedit. I can connect it to a MPEG2 Splitter (MS MPEG2 Splitter, Gabbest ect.) but the splitter won't create any outputs. So I guess it's necessary to set the format. The problem is, that I have no clue what kind of format I have to set, since I just want to pass the packets I get to the next filter downstream.
    Stream is MPEG2-PS with H.264 and MPEG2 Audio content. I've set the allocator buffer to 1400 (just an assumption, since that's the size I received with a simple UDP client).

    Any suggestions?

  • Wednesday, November 11, 2009 12:28 AMrep_movsd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi

    Here are the media types for MPEG2 PS as delivered by HD cams and DVDs


    /* Mpeg audio format block */
    BYTE g_MPEG1AudioFormat[] = 
    {
        0x50, 0x00, 0x02, 0x00, 0x80, 0xBB, 0x00, 0x00,
        0x00, 0x7D, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00,
        0x16, 0x00, 0x02, 0x00, 0x00, 0xE8, 0x03, 0x00,
        0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x1C, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    } ;

    /* Mpeg video format block ( is just used for connection, the decoder will get the actual media info from the mpeg data ) */
    BYTE g_Mpeg2ProgramVideo[] = 
    {
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.rcSource.left              = 0x00000000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.rcSource.top               = 0x00000000 */
        0xD0, 0x02, 0x00, 0x00,                         /*  .hdr.rcSource.right             = 0x000002d0 */
        0xE0, 0x01, 0x00, 0x00,                         /*  .hdr.rcSource.bottom            = 0x000001e0 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.rcTarget.left              = 0x00000000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.rcTarget.top               = 0x00000000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.rcTarget.right             = 0x00000000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.rcTarget.bottom            = 0x00000000 */
        0x00, 0x09, 0x3D, 0x00,                         /*  .hdr.dwBitRate                  = 0x003d0900 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.dwBitErrorRate             = 0x00000000 */
        0x63, 0x17, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, /*  .hdr.AvgTimePerFrame            = 0x0000000000051763 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.dwInterlaceFlags           = 0x00000000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.dwCopyProtectFlags         = 0x00000000 */
        0x04, 0x00, 0x00, 0x00,                         /*  .hdr.dwPictAspectRatioX         = 0x00000004 */
        0x03, 0x00, 0x00, 0x00,                         /*  .hdr.dwPictAspectRatioY         = 0x00000003 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.dwReserved1                = 0x00000000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.dwReserved2                = 0x00000000 */
        0x28, 0x00, 0x00, 0x00,                         /*  .hdr.bmiHeader.biSize           = 0x00000028 */
        0xA0, 0x05, 0x00, 0x00,                         /*  .hdr.bmiHeader.biWidth          = 0x000002d0 */
        0x40, 0x04, 0x00, 0x00,                         /*  .hdr.bmiHeader.biHeight         = 0x00000000 */
        0x00, 0x00,                                     /*  .hdr.bmiHeader.biPlanes         = 0x0000 */
        0x00, 0x00,                                     /*  .hdr.bmiHeader.biBitCount       = 0x0000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.bmiHeader.biCompression    = 0x00000000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.bmiHeader.biSizeImage      = 0x00000000 */
        0xD0, 0x07, 0x00, 0x00,                         /*  .hdr.bmiHeader.biXPelsPerMeter  = 0x000007d0 */
        0x27, 0xCF, 0x00, 0x00,                         /*  .hdr.bmiHeader.biYPelsPerMeter  = 0x0000cf27 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.bmiHeader.biClrUsed        = 0x00000000 */
        0x00, 0x00, 0x00, 0x00,                         /*  .hdr.bmiHeader.biClrImportant   = 0x00000000 */
        0x98, 0xF4, 0x06, 0x00,                         /*  .dwStartTimeCode                = 0x0006f498 */
        0x56, 0x00, 0x00, 0x00,                         /*  .cbSequenceHeader               = 0x00000056 */
        0x02, 0x00, 0x00, 0x00,                         /*  .dwProfile                      = 0x00000002 */
        0x02, 0x00, 0x00, 0x00,                         /*  .dwLevel                        = 0x00000002 */
        0x00, 0x00, 0x00, 0x00,                         /*  .Flags                          = 0x00000000 */
        /*  .dwSequenceHeader [1] */
        0x00, 0x00, 0x01, 0xB3, 0x2D, 0x01, 0xE0, 0x24,
        0x09, 0xC4, 0x23, 0x81, 0x10, 0x11, 0x11, 0x12, 
        0x12, 0x12, 0x13, 0x13, 0x13, 0x13, 0x14, 0x14, 
        0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, 0x15, 
        0x15, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 
        0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 0x17, 
        0x18, 0x18, 0x18, 0x19, 0x18, 0x18, 0x18, 0x19, 
        0x1A, 0x1A, 0x1A, 0x1A, 0x19, 0x1B, 0x1B, 0x1B, 
        0x1B, 0x1B, 0x1C, 0x1C, 0x1C, 0x1C, 0x1E, 0x1E, 
        0x1E, 0x1F, 0x1F, 0x21, 0x00, 0x00, 0x01, 0xB5, 
        0x14, 0x82, 0x00, 0x01, 0x00, 0x00
    } ;


    AM_MEDIA_TYPE amTypeVideo;
    memset(&amTypeVideo, 0, sizeof(AM_MEDIA_TYPE));
    amTypeVideo.majortype = MEDIATYPE_Video;
    amTypeVideo.subtype = MEDIASUBTYPE_MPEG2_VIDEO;
    amTypeVideo.bFixedSizeSamples = TRUE;
    amTypeVideo.bTemporalCompression = 0;
    amTypeVideo.formattype = FORMAT_MPEG2Video;
    amTypeVideo.pUnk = NULL;
    amTypeVideo.cbFormat = sizeof(g_Mpeg2ProgramVideo);
    amTypeVideo.pbFormat = g_Mpeg2ProgramVideo;

    AM_MEDIA_TYPE amTypeAudio;
    memset(&amTypeAudio, 0, sizeof(AM_MEDIA_TYPE));
    amTypeAudio.majortype = MEDIATYPE_Audio;
    amTypeAudio.bFixedSizeSamples = TRUE;
    amTypeAudio.bTemporalCompression = 0;
    amTypeAudio.formattype = FORMAT_WaveFormatEx;
    amTypeAudio.pUnk = NULL;
    amTypeAudio.cbFormat = sizeof g_MPEG1AudioFormat;
    amTypeAudio.pbFormat = g_MPEG1AudioFormat;

    If this isn't what you need, create a file with the format you need, render it from a graph, and dump the media type data from the MPEG splitters output pins.

    V

  • Thursday, November 12, 2009 3:41 PMmynameisnotcarlos Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi rep_movsd,

    thank you very much for that info.
    I'm still having problems, though. But I guess it's a problem with receiving the stream from the network.

    When I write a simple udp client which writes the buffer into a file, then I can play the file, I also get the single ESs. But it's pretty much a mess (lots of green blocks, missing packs).

    Are there specific rules when setting up a socket for an UDP stream?

    mySocket = socket(AF_INET, SOCK_DGRAM, 0);
  • Friday, November 13, 2009 7:08 AMrep_movsd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I hope you are aware that UDP packets can arrive in any order or not at all? For streaming purposes usually MPEG TS is used , it has a granularity of 192 bytes per TS packet and is more reslient to errors in transmission.

    You need to implement some sort of packet ordering scheme at the very least if you want to use UDP. Im sure MPEG decoders may forgive the odd missing packet, but will balk if the packet order itself is screwed up. For all I know the decoder itself may crash or worse.

    Some day the evil ones are going to make a doctored video file that can cause exploits on a system by crashing codecs - remember you read it here first...
  • Friday, November 13, 2009 3:50 PMmynameisnotcarlos Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    In fact, I didn't know that I get nonsequentially ordered packets via UDP.
    Isn't each transport packet 188 bytes of size? And would I have to add an extra amount of bytes for the UDP "container" when receiving them? How would I figure out the exact buffer size?

    Thanks alot for your answer!
  • Saturday, November 14, 2009 6:19 PMrep_movsd Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    There are a couple of TS formats, DVB (like from Digital Set Top Boxes) usually has 188 byte packets, HD cams add 4 bytes of data containing (apparently) a timecode.

    Sending 188 bytes per UDP packet is counter productive. If you study the TCP/IP stuff you will see that an IP packet is somewhere around 1500 bytes ( minus some amount ). So use the nearest multiple of 188 that will fit in one IP packet. 

    Probably you will end up reinventing some of the stuff that the TCP protocol does. Perhaps simply stick to TCP itself....