3

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

hamishmcn
  • 7,333
  • 10
  • 38
  • 45

4 Answers4

3

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.

Adam Rosenfield
  • 360,316
  • 93
  • 484
  • 571
1

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--;
    }
Andrew
  • 297
  • 1
  • 5
  • 11
1

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);
0

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.

Dodger
  • 124
  • 2
  • 9
  • Hi @Dodger - I no longer remember, but suspect you are right - I was probably working with program streams (I have corrected the question) – hamishmcn Jul 21 '11 at 03:24
  • A video sequence header is part of an ES stream. Since a Transport Stream contains PES and thus ES, there is also a video sequence header. – DarkDust Jul 17 '13 at 07:56