Question

How do I programmatically find out the width and height of the video in an mpeg-2 transport program stream file?

Edit: I am using C++, but am happy for examples in any language. Edit: Corrected question - it was probably program streams I was asking about

Was it helpful?

Solution

Check out the source code to libmpeg2, a F/OSS MPEG2 decoder. It appears that the width and height are set in the mpeg2_header_sequence() function in header.c. I'm not sure how control flows to that particular function, though. I'd suggest opening up an MPEG2 file in something using libmpeg2 (such as MPlayer) and attaching a debugger to see more closely exactly what it's doing.

OTHER TIPS

If you are using DirectX, there is a method in the VMRWindowlessControl interface:

piwc->GetNativeVideoSize(&w, &h, NULL, NULL);

Or the IBasicVideo interface:

pivb->GetVideoSize(&w, &h);

for MPEG2 Video the horizontal & vertical size can be found in the Video Sequence Header (from the video bit stream). The sequence header code is 0x000001B3. Some example code below. However it does not take into account the horizontal/vertical size extension if specified in sequence extension header.

#define VIDEO_SEQUENCE_HDR  0xB3
#define HOR_SIZE_MASK       0xFFF00000
#define HOR_SIZE_SHIFT      20
#define VER_SIZE_MASK       0x000FFF00
#define VER_SIZE_SHIFT      8

unsigned char *pTmp = tsPacket;
int len = 188;
int horizontal, vertical;

 while(len>0 && !horizontal && !vertical)
 {        
        if(*pTmp == 0 && *(pTmp+1) == 0
           && *(pTmp+2)== 0x01 && *(pTmp+3) == 0xB3 && (len-1) >0)
        {
            unsigned int *pHdr = (unsigned int *)pTmp;    
            pHdr++ ; 
            unsigned int secondByte = ntohl(*pHdr);
            horizontal = (secondByte & HOR_SIZE_MASK) >> HOR_SIZE_SHIFT;
            vertical = (secondByte & VER_SIZE_MASK) >> VER_SIZE_SHIFT;           
            break;
        }
        pTmp++;
        len--;
    }

You haven't said what language you want to use so I doubt you'll get any code samples.

Have a look at the following links. The first describes the MPEG-2 file format, the other is the RFC on transporting MPEG via RTP.

http://www.fh-friedberg.de/fachbereiche/e2/telekom-labor/zinke/mk/mpeg2beg/beginnzi.htm

http://www.ietf.org/rfc/rfc2250.txt

hamishmcn said that Adam Rosenfield's answer was what he needed. This makes me wonder about the accuracy of the question. An MPEG transport stream doesn't have a video sequence header. That header is found in an MPEG program stream.

I don't have an answer. I was just hoping against hope that someone's answer was correct, because I need one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top