Log in

View Full Version : Tell MPEG1 file from MPEG2 file by a header byte


Marcel
4th March 2004, 11:50
Hello out there in radio land,

I'm trying to tell MPEG1 files from MPEG2 files by their header.

Why I want to do this:
I'm building a tiny AVS creator for my Real Producer GUI RealHandy (http://forum.doom9.org/showthread.php?s=&threadid=71417).
If a .mpg file is given to it, I want to choose automatically between MPEG2Source via DVD2AVI and DirectShowSource, as DirectShowSource on MPEG2 files only works with Real Producer if Elecard MPEG2 Decoder is used.

What I've found:
I've had a look into several MPEG files, and every MPEG1 file (about 10) had $21 as fifth byte, and every MPEG2 file (about 10, too) had $44 as fifth byte (I asked MPEG Bitrate Viewer if they were MPEG1 or MPEG2; then, I used Total Commanders hex viewer to look at the raw header).

Final question:
Is it a reliable method to tell MPEG1 from MPEG2 by simply looking at the fifth byte?


Thanks in advance,
Marcel

Nic
4th March 2004, 14:18
That's not the most reliable way unfortuantely...as MPEG files are weird beasts....here's another way, which isn't brilliant but more reliable as it does not rely on the MPEG starting with correct start code (in C unfortunately)

buf = an array whith bufsize of mpeg data (just fill it with 1 meg or something ;) )

This code checks to see if the picture extension is there and if it is then assumes it's MPEG-2, I wrote this ages ago based on some GPL app....can't remember which...It's probably cr*p but it does seem to work well in one of my older apps...


bool bMPEG2 = false;
for ( i = 0; i != bufsize; i++ )
{
if ( buf[i] == 0 && buf[i+1] == 0 && buf[i+2] == 0x01 )
{
if ( buf[i+3] == 0xba )
{
bMPEG2 = ((buf[i+4] & 0xc0)>>6)==1; // Get first two bits
break;
}
}
}


-Nic

mpucoder
4th March 2004, 20:00
nic - I don't see how that code differentiates between mpeg-1 and mpeg-2. It sets bmpeg2 true if the marker bit of a PACK header is present. This bit is required (to prevent false start code detection during the following SCR data), and present in both mpeg-1 and mpeg-2. The only thing I can see being detected is the PACK header itself, which indicates a system stream vs an elementary stream (ie, your file is multiplexed already).

If the code is supposed to be looking for a picture_display_extension or a picture_coding_extension (which are mpeg-2 specific) then the start code (in buf[i+3]) should be 0xb5, not 0xba.

Nic
4th March 2004, 20:12
I thought about that afterwards, I copied and pasted bits in...my bad...ill quickly write something up better tomorrow. (shouldn't try and do things when im at work....thanks for the correction mpucoder)

-Nic

mpucoder
4th March 2004, 20:19
I screwed up too when looking at the specs, I accidentally looked at mpeg-2 twice. It seems the pack header for mpeg-1 is different, and that code will detect it. But is must be a system stream.

edit: a reworked page (http://www.mpucoder.com/doom9/packhdr.html) showing the two pack headers.

Nic
5th March 2004, 10:17
I remembered where I got the code from now....libmpeg2's demux code...

So it should really read:

if ( (buf[i+4] & 0xc0) == 0x40 )
// Then it's MPEG-2

if ( (buf[i+4] & 0xf0) == 0x20 )
// Then it's MPEG-1

if neither of these, it's a bad pack header....(according to libmpeg2)

Hopefully that should be enough for Marcel to be getting along with :)

Cheers,
-Nic

Marcel
5th March 2004, 13:04
Yeah, that's enough for me.

Many thanks to both of you! You really helped me a lot.

What I can see is that this solution isn't that far away from my own code. There are only two differences:

a) I assumed that an MPEG file is always starting with $000001BA (all files I inspected did; do all files neccessarily do so?).

b) I was looking for the whole byte instead of the first two or four bits.


Btw, you don't believe how glad I am that this piece of C code is so readable. I love Delphi and hate Java (influenced by a large project for university), but I never really dealed with C or C++, so I only know their basic syntax from Java and PHP, which are told to be similar in basic syntax. Before starting this thread, I had a look into DVD2AVI source. And that didn't give me facts, but only starting points for further guessing. Maybe I should learn C/C++ ...

Again, many, many thanks!

Marcel

diehardii
5th March 2004, 16:43
Hi Marcel,

To answer a)

Not necessarily, it denotes a program stream. You can also have interleaved PES streams(000001E0, or 000001C0), Transport streams(not sure), or just the video stream (000001B3)(and probably several others)

~Steve

Marcel
5th March 2004, 17:19
Hi diehardii,

thanks for that info, though it's not exactly what I hoped ...

Can the golden 4 bit only be found after the $000001BA, or after $000001E0, $000001C0 and too?
I've got two files containing a HDTV transport stream, both starting with:
47 50 22 19 00 00 01 E0 00 00 85 C0 0A 35 6D 35
So if these bits are present in a TS, they must be hiding elsewhere.

But I think I'll have no problem with telling the users to mux the file to a program stream MPEG file, with TMPGEnc, for example. This should work for me, right?

Marcel

mpucoder
5th March 2004, 17:42
I'd suggest you go to Ookami's Programmers Corner (http://ookami.videoxone.de/programmers.html) and get the mpeg standards (mpeg1.rar and mpeg2.rar, they're called "some mpeg documents" and "second part of the above") and study them.

What you underlined is the packet length of a video Packetized Elementary Stream. A length of 0000 indicates the packet has no specific length (ie it ends at another system header or the end of the file). This is only allowed in video PES within Transport Stream.
To determine if the video is mpeg2 you have to look for mpeg2 extensions (00000001b5).

Edit: This was so obvious, I overlooked it - MPEG-1 has no transport stream. So if it is a transport stream, it is MPEG-2.

Also, MPEG-2 transport or program streams can carry MPEG-1 video or audio. I don't know if you are trying to identify the elements or the system level. An example is DVD, which always uses MPEG-2 system streams, can carry MPEG-1 video/audio in it.

To summarize: If the file starts with a PACK header (000001BA) you can determine MPEG-1 or MPEG-2 system level by the 5th byte. If it starts with 0x47 it is an MPEG-2 Transport Stream.

mpucoder
5th March 2004, 18:28
Here are some other signatures:
00 00 01 B3 - video elementary stream (this is a SEQuence header)
0B 77 - AC3
7F FE 80 01 - DTS
52 49 46 46 xx xx xx xx 41 56 49 20 - avi (RIFFxxxxAVI )
52 49 46 46 xx xx xx xx 57 41 56 45 - wav (RIFFxxxxWAVE)

Marcel
6th March 2004, 15:38
Hi mpucoder,

the link to Ookami's Programmers Corner and the informations you gave me are very helpful for me. Thanks again!

About MPEG1 video in a MPEG2 file: I'll have a look if mpeg2dec.dll can handle them; if so, I'll just have to distinguish between MPEG1 and MPEG2 files, no matter what compression is used inside the MPEG2 files.

However, I plan to take all the informations and will code a small infos-about-mpeg-and-avi Delphi unit and publish it here.

Marcel

Marcel
16th March 2004, 16:13
And here is the result of my work:

VidInfo.pas (6k) (http://www.turbogenial.de/data/VidInfo.pas)
Delphi unit that retrieves resolution, FPS, aspect ratio, MPEG version, number of frames for AVI and MPEG/VOB/D2V files.

FPSreader (http://www.turbogenial.de/data/FPSreader.zip) (110k; executable, full source and FileDrop component included)
Demo application: Just start it and drag'n'drop files on its window. If you want to compile it on your own, the component FileDrop has to be installed into your Delphi. It's made with Delphi 3.

ATM, the number of frames is only determined for AVI files, as I haven't found this number yet in MPEG files.
And the aspect ratio detection is not tested yet with 16:9 NTSC VOB files. (if someone could tet it and tell me the value reported for AR - that would make me really happy!)

gurpreet1989
22nd February 2011, 13:08
IF both program stream1 and program stream2 is not present in mpeg video file. What we conclude about that file?

Which type of audio and video stream present in it?

and how to calculate number of streams in mpeg video file also tell me about how to find number of gop in one mpeg file. and number of pictures in one gop. also tell me about the number of pack header in mpeg file and number of audio and video streams in one mpeg file.

THANKS IN ADVANCE