Ben De Rydt
February 17th, 2005, 06:33 AM
I've been wondering for a while why no vendor preserves timecode when capturing HDV. Turns out they are just lazy. The timecode is in the PES header where anyone familiar with MPEG 2 would expect it to be. I'm not familiar with MPEG 2 and I found it in a day. It's right there, in the m2t file.
The PES (Program Elementary Stream) header can be found before each frame (picture in MPEG 2 speak). It starts with 0x000001e0 (for video) and has a 10 or 15 byte payload. First 5 bytes are some lengths and flags and next is the PTS or a combination of the PTS and the DTS. The PTS (Presentation Time Stamp) says when a frame should be shown, the DTS (Decoding Time Stamp) says when a frame should be decoded. Both are necessary because MPEG 2 reorders frames: B frames come before P frames in an MPEG 2 stream.
The PTS and DTS are both 5 bytes long. They contain a 33 bit value which is a clock tick from a 90 kHz clock. The PTS and DTS are enocded as following: 4 bits flags, 3 most significant bits of PTS, a 1, the next 15 bits of the PTS, a 1, the last 15 bits of the PTS and a 1. The 1 bits are inserted so that there won't be two zero bytes in consecution.
Divide the PTS by 900 and you have the timecode value in hundreds of a second. From then on it's trivial to convert this value to HH:MM:SS:FF format, unless you live in a country where people drop frames from time to time :-)
The PTS values are stable. I captured the same piece twice from diffferent starting points and they stay the same. They accord to the timecode shown on screen. It is the timecode.
This means that a) you can extract timecode from any m2t stream produced by an FX1 and b) you can do it in real time since there is no MPEG 2 decoding necessary. You could also extract the timecode while converting the MPEG 2 stream to something else, provided the MPEG 2 decoder gives you a PTS value for each frame.
The PES (Program Elementary Stream) header can be found before each frame (picture in MPEG 2 speak). It starts with 0x000001e0 (for video) and has a 10 or 15 byte payload. First 5 bytes are some lengths and flags and next is the PTS or a combination of the PTS and the DTS. The PTS (Presentation Time Stamp) says when a frame should be shown, the DTS (Decoding Time Stamp) says when a frame should be decoded. Both are necessary because MPEG 2 reorders frames: B frames come before P frames in an MPEG 2 stream.
The PTS and DTS are both 5 bytes long. They contain a 33 bit value which is a clock tick from a 90 kHz clock. The PTS and DTS are enocded as following: 4 bits flags, 3 most significant bits of PTS, a 1, the next 15 bits of the PTS, a 1, the last 15 bits of the PTS and a 1. The 1 bits are inserted so that there won't be two zero bytes in consecution.
Divide the PTS by 900 and you have the timecode value in hundreds of a second. From then on it's trivial to convert this value to HH:MM:SS:FF format, unless you live in a country where people drop frames from time to time :-)
The PTS values are stable. I captured the same piece twice from diffferent starting points and they stay the same. They accord to the timecode shown on screen. It is the timecode.
This means that a) you can extract timecode from any m2t stream produced by an FX1 and b) you can do it in real time since there is no MPEG 2 decoding necessary. You could also extract the timecode while converting the MPEG 2 stream to something else, provided the MPEG 2 decoder gives you a PTS value for each frame.