How to parse an M2TS file?
How to parse an M2TS file?
Hi Rocky
A while ago you asked me if I need a tool which gives me all the PTS for the tracks in an m2ts file, I should ask you.
I think it is time for investigation in this topic.
On the other hand could you imagine to help me to learn how to parse the m2ts file.
I have the m2ts specs as a PDF but it is to much technical English for me.
It would be splendid to have my own parser.
Best regards
hubble
A while ago you asked me if I need a tool which gives me all the PTS for the tracks in an m2ts file, I should ask you.
I think it is time for investigation in this topic.
On the other hand could you imagine to help me to learn how to parse the m2ts file.
I have the m2ts specs as a PDF but it is to much technical English for me.
It would be splendid to have my own parser.
Best regards
hubble
How to parse an M2TS file?
Hello, Mr hubblec4!
Rocky asked me to help you with this. I'm the guy that thought up the "fast mode" for DGDemux, so I guess he trusts me to guide you right.
OK, sure it would be great if you had your own parser that you could tweak and enhance for your needs. So I thought I could guide you in creating it yourself. So can you tell me two things:
1. What do you need to do specifically with the M2TS? For example, what listing/properties are you looking for?
2. What language do you like to develop in? VS C/C++ would be great but anything else will be fine too, cuz you're going to do the coding following my guidance.
To get started we are going to make the top-level iterator that steps through the transport packets, collecting PIDs, etc. After that we will add the PES layer, where the timestamps are found. Then as much more as we need to meet your needs. I'll wait for your answers to the questions and then we'll really get started.
Rocky asked me to help you with this. I'm the guy that thought up the "fast mode" for DGDemux, so I guess he trusts me to guide you right.
OK, sure it would be great if you had your own parser that you could tweak and enhance for your needs. So I thought I could guide you in creating it yourself. So can you tell me two things:
1. What do you need to do specifically with the M2TS? For example, what listing/properties are you looking for?
2. What language do you like to develop in? VS C/C++ would be great but anything else will be fine too, cuz you're going to do the coding following my guidance.
To get started we are going to make the top-level iterator that steps through the transport packets, collecting PIDs, etc. After that we will add the PES layer, where the timestamps are found. Then as much more as we need to meet your needs. I'll wait for your answers to the questions and then we'll really get started.
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Hi Sherman
Thank you very much for taking care of me.
to 1.)
My only first main goal is to get the PTS for each track in an m2ts file. This would save me some WorkArounds and external tools.
(If all of this works, I can imagine, I'm sure I can think of something else that could be added.)
to 2.)
I use Lazarus - FreePascal, but I can read C/C++ code well(I think).
OK, lets start, I am very excited.
Thank you very much for taking care of me.
to 1.)
My only first main goal is to get the PTS for each track in an m2ts file. This would save me some WorkArounds and external tools.
(If all of this works, I can imagine, I'm sure I can think of something else that could be added.)
to 2.)
I use Lazarus - FreePascal, but I can read C/C++ code well(I think).
OK, lets start, I am very excited.
How to parse an M2TS file?
Can I get just a little more information before starting?
How will you specify the track? By PID that you know in advance?
Are you looking for all the PTSs for a given track? Or just the first? Need they be tied to an access unit number (such as frame number)?
How will you specify the track? By PID that you know in advance?
Are you looking for all the PTSs for a given track? Or just the first? Need they be tied to an access unit number (such as frame number)?
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Yes, all what you need.
Yes, I think this is a good way. The PIDs are known in advance by parsing the mpls file or the DGDemux output(scaning an mpls file).
All PTS's for all streams and all frames.
Yes the order of the PTS's should be in display order.
How to parse an M2TS file?
OK, thank you. One final question: Would you like me to provide VS C++ code for each step, or would you prefer just pseudocode from which you will code it in the language of your choice?
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Hi Sherman
I would leave it up to you to decide what is easier for you. I think both options are good.
I would leave it up to you to decide what is easier for you. I think both options are good.
How to parse an M2TS file?
Alrighty then. First we are going to parse the top-level packet layer. Please get these files:
https://rationalqm.us/misc/wedding.m2ts
https://rationalqm.us/misc/main.cpp
The first is the M2TS we will be using for now. The second is the VS C++ code. Run:
m2tsparse path\wedding.m2ts 1011
Note that video is on PID 0x1011. The output looks like this. I am showing only the fields we will need to get to the next (PES) layer.
1011: pusi = 1, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
...
1011: pusi = 1, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
...
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 3
pusi_count = 364
Note that the pusi_count is the number of pusi's set to 1. We have 364 and because this is the video PID that is also the number of video frames.
See here for a description of the transport packet fields:
https://en.wikipedia.org/wiki/MPEG_transport_stream
Note that M2TS has an additional 4 bytes ahead of the sync byte, thus accounting for the buffer offsets I use.
So please have a look at things and when you understand everything and have it working with your own code we will move on to the PES layer. If you have any questions, feel free to ask. BTW, this code assumes a valid, uncorrupted M2TS file. We can add error checking and recovery later for busted files, if needed.
https://rationalqm.us/misc/wedding.m2ts
https://rationalqm.us/misc/main.cpp
The first is the M2TS we will be using for now. The second is the VS C++ code. Run:
m2tsparse path\wedding.m2ts 1011
Note that video is on PID 0x1011. The output looks like this. I am showing only the fields we will need to get to the next (PES) layer.
1011: pusi = 1, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
...
1011: pusi = 1, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
...
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 1
1011: pusi = 0, afc = 3
pusi_count = 364
Note that the pusi_count is the number of pusi's set to 1. We have 364 and because this is the video PID that is also the number of video frames.
See here for a description of the transport packet fields:
https://en.wikipedia.org/wiki/MPEG_transport_stream
Note that M2TS has an additional 4 bytes ahead of the sync byte, thus accounting for the buffer offsets I use.
So please have a look at things and when you understand everything and have it working with your own code we will move on to the PES layer. If you have any questions, feel free to ask. BTW, this code assumes a valid, uncorrupted M2TS file. We can add error checking and recovery later for busted files, if needed.
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Not bad for a kid. Did Britney help you?
Curly Howard
Director of EAC3TO Development
Director of EAC3TO Development
How to parse an M2TS file?
Hi Sherman
Many many thanks for all this info and the c++ code.
Already, some things are much clearer.
And I also have a first question regarding the sync byte (x047) and the 4 bytes before it.
In the m2ts PDF that I have, the sync byte is the very first thing. But when I look at an m2ts with a hex editor, there are another 4 bytes before the sync byte.
So I thought that either the PDF is faulty or I have to search further to find the meaning of the first 4 bytes.
But when I look at the c++ code, the sync byte is always expected in the 5th position.
What do these 4 bytes mean?
Many many thanks for all this info and the c++ code.
Already, some things are much clearer.
And I also have a first question regarding the sync byte (x047) and the 4 bytes before it.
In the m2ts PDF that I have, the sync byte is the very first thing. But when I look at an m2ts with a hex editor, there are another 4 bytes before the sync byte.
So I thought that either the PDF is faulty or I have to search further to find the meaning of the first 4 bytes.
But when I look at the c++ code, the sync byte is always expected in the 5th position.
What do these 4 bytes mean?
How to parse an M2TS file?
That's the TP_extra_header, which consists of a 2-bit copy protection flag and a 30-bit arrival time. Do you have a copy of the bluray spec?
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Thanks for the info.
Yes, Mosu gave me a PDF for a long time. And another friendly user of cE shared with me the m2ts specs PDF.
Would you like to have it too?
Yes, Mosu gave me a PDF for a long time. And another friendly user of cE shared with me the m2ts specs PDF.
Would you like to have it too?
How to parse an M2TS file?
My simple m2ts parser in FreePascal is so far ready.
Do you want to see the FreePascal source code, or the final CLI tool?
Should I send you the output of my parser for comparison?
Or is it OK if I say everything works for me and we move on?
Do you want to see the FreePascal source code, or the final CLI tool?
Should I send you the output of my parser for comparison?
Or is it OK if I say everything works for me and we move on?
How to parse an M2TS file?
No need to see your code. If you say it works that's good enough for me.
It would be interesting to see your M2TS PDF, although if it doesn't mention the 4-byte extra header it seems possibly unreliable.
I'll get the next stage ready.
It would be interesting to see your M2TS PDF, although if it doesn't mention the 4-byte extra header it seems possibly unreliable.
I'll get the next stage ready.
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
OK.
Send you a PM.
How to parse an M2TS file?
Thank you. That is the spec for a generic MPEG transport packet. "Bluray M2TS" should not have been added to the filename! To that spec the bluray spec adds the TP_extra_header.
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Here is the next phase:
https://rationalqm.us/misc/main2.cpp
This builds upon what we did for the transport layer. Here is the output showing the coded frame number and PTS value:
1011[0]: 100800 [1120 ms]
1011[1]: 93600 [1040 ms]
1011[2]: 97200 [1080 ms]
1011[3]: 108000 [1200 ms]
1011[4]: 104400 [1160 ms]
1011[5]: 115200 [1280 ms]
1011[6]: 111600 [1240 ms]
1011[7]: 122400 [1360 ms]
1011[8]: 118800 [1320 ms]
1011[9]: 129600 [1440 ms]
1011[10]: 126000 [1400 ms]
1011[11]: 136800 [1520 ms]
1011[12]: 133200 [1480 ms]
...
The output is in coded order. If you need display order, these need to be sorted on the PTS value. You could gather them all and then sort everything, or you could assume a max number of B frames per GOP and then sort on the fly by reading ahead. Would you like me to give code for these possibilities?
Alternatively, it could be done on the fly using the frame types (I, P, B) but that would require parsing into the video ES layer, and things would be different for each video type. That will probably be overkill for what you need to do.
You can see that the difference between frame times is 40 ms, corresponding to the 25 fps frame rate.
Let me know where you'd like to go from here.
https://rationalqm.us/misc/main2.cpp
This builds upon what we did for the transport layer. Here is the output showing the coded frame number and PTS value:
1011[0]: 100800 [1120 ms]
1011[1]: 93600 [1040 ms]
1011[2]: 97200 [1080 ms]
1011[3]: 108000 [1200 ms]
1011[4]: 104400 [1160 ms]
1011[5]: 115200 [1280 ms]
1011[6]: 111600 [1240 ms]
1011[7]: 122400 [1360 ms]
1011[8]: 118800 [1320 ms]
1011[9]: 129600 [1440 ms]
1011[10]: 126000 [1400 ms]
1011[11]: 136800 [1520 ms]
1011[12]: 133200 [1480 ms]
...
The output is in coded order. If you need display order, these need to be sorted on the PTS value. You could gather them all and then sort everything, or you could assume a max number of B frames per GOP and then sort on the fly by reading ahead. Would you like me to give code for these possibilities?
Alternatively, it could be done on the fly using the frame types (I, P, B) but that would require parsing into the video ES layer, and things would be different for each video type. That will probably be overkill for what you need to do.
You can see that the difference between frame times is 40 ms, corresponding to the 25 fps frame rate.
Let me know where you'd like to go from here.
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Great work, Sherm!
Bless her heart.Yo mama helped me.
Curly Howard
Director of EAC3TO Development
Director of EAC3TO Development
How to parse an M2TS file?
OK, that means there are other generic transport stream packet specs?
I have send you the Blu-ray PDF too.
How to parse an M2TS file?
Many thanks. I will study this at night and will report back when all is working on my side.
How to parse an M2TS file?
DVB and others are derivatives of MPEG TS.
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Here is a version that outputs in sorted order (quick and dirty).
https://rationalqm.us/misc/main3.cpp
1011[1]: 93600 [1040 ms]
1011[2]: 97200 [1080 ms]
1011[0]: 100800 [1120 ms]
1011[4]: 104400 [1160 ms]
1011[3]: 108000 [1200 ms]
1011[6]: 111600 [1240 ms]
1011[5]: 115200 [1280 ms]
1011[8]: 118800 [1320 ms]
1011[7]: 122400 [1360 ms]
1011[10]: 126000 [1400 ms]
1011[9]: 129600 [1440 ms]
1011[12]: 133200 [1480 ms]
1011[11]: 136800 [1520 ms]
1011[14]: 140400 [1560 ms]
1011[15]: 144000 [1600 ms]
1011[13]: 147600 [1640 ms]
1011[17]: 151200 [1680 ms]
1011[16]: 154800 [1720 ms]
1011[19]: 158400 [1760 ms]
1011[18]: 162000 [1800 ms]
1011[21]: 165600 [1840 ms]
1011[20]: 169200 [1880 ms]
...
If you have any questions about correlating the code to the spec, just let me know. I should add comments but Alice wants me to pull her wagon.
https://rationalqm.us/misc/main3.cpp
1011[1]: 93600 [1040 ms]
1011[2]: 97200 [1080 ms]
1011[0]: 100800 [1120 ms]
1011[4]: 104400 [1160 ms]
1011[3]: 108000 [1200 ms]
1011[6]: 111600 [1240 ms]
1011[5]: 115200 [1280 ms]
1011[8]: 118800 [1320 ms]
1011[7]: 122400 [1360 ms]
1011[10]: 126000 [1400 ms]
1011[9]: 129600 [1440 ms]
1011[12]: 133200 [1480 ms]
1011[11]: 136800 [1520 ms]
1011[14]: 140400 [1560 ms]
1011[15]: 144000 [1600 ms]
1011[13]: 147600 [1640 ms]
1011[17]: 151200 [1680 ms]
1011[16]: 154800 [1720 ms]
1011[19]: 158400 [1760 ms]
1011[18]: 162000 [1800 ms]
1011[21]: 165600 [1840 ms]
1011[20]: 169200 [1880 ms]
...
If you have any questions about correlating the code to the spec, just let me know. I should add comments but Alice wants me to pull her wagon.
Sherman Peabody
Director of Linux Development
Director of Linux Development
How to parse an M2TS file?
Hi Sherman.
Thanks for the new updated code.
I have a question about the PTS.
When I look in to the PDF there are 3 Bytes for the PTS, but it seems that you use 5 Bytes for generating the PTS.
Could you please explain this a bit?
Thanks for the new updated code.
I have a question about the PTS.
When I look in to the PDF there are 3 Bytes for the PTS, but it seems that you use 5 Bytes for generating the PTS.
Could you please explain this a bit?