3

First of all, sorry for my poor English. I am writting video streaming server in C++. I have multiple mpeg2-ts files (movies and advertisements) which I need to stream via HTTP as one single TS-FILE. The problem is that every mpeg-ts file has its own timestamps (PCR, PTS, DTS). And, as I understand, to make a continuous streaming flow, every new PCR (PTS, DTS) value should continue from the last PCR (PTS, DTS) value.

Here is a picture for better understanding of what I am saying about: https://i.stack.imgur.com/vL1m6.png (I can't include my picture directly in the message. Sorry)

I need to replace pcr`1, pcr`2, pcr`3 timestamps with the new ones. For example, I sent ts-packet containing the pcr3 timestamp and after a few more ts packets (not containing any value of PCR) I want to insert my advertisement. And my question is: how do I calculate the new values for pcr`1, pcr`2, pcr`3 and so on?

Is it correct to calculate the bitrate of the current video and then divide the amount of bits that the program have sent since the last PCR timestamp (in our case, it's pcr3) by this bitrate? I mean the following: (new timestamp) = (previous timestamp) + (the amount of bits) / (bitrate). Or is there a more efficient way to do it?

As for PTS and DTS timestamps, I read here that these timestamps can be non-linear. Is it will be correct to calculate it relative to the last original PCR that I received? I mean:

pts_new = (original_pts - last_original_pcr) + pcr_new. 
dts_new = (original_dts - last_original_pcr) + pcr_new. 

(original_pts - last_original_pcr) is the difference between pts and pcr values
pcr_new is the last modified pcr value

My program can read and edit these timestamps in mpeg-ts stream. Fortunately, there's a lot of literature on how to do it. But how do I calculate the new values for these timestamps?

I have just started to learn the specification of mpeg2-ts, and please correct my mistakes if I am wrong in something. Thanks in advance. Hope you understood me.

1
  • is it possible to use existing utilities or libraries (such as gstreamer)?
    – Juster
    Sep 18, 2014 at 5:28

1 Answer 1

5

Mpeg2 "Splicing" is an art-form, and is much more complicated than concatenating two streams. It requires manipulations which many companies have patented (http://www.google.com/patents/US6380991, http://www.google.com/patents/US6806909, http://www.google.com/patents/US6993081)

to answer some of your questions: your calculation of the next pcr looks ok, although you need take into account many compliancy issues (etr290 for example) for DTS/PTS you have much more work to do. the most basic splice will just restamp the ad's pts/dts in such a way that they continue from the last timestamp of the first TS.

ad first timestamp = last timestamp + frame interval

the trick lies within making sure that you have no "holes" in either the presentation time stamps or the decoding timestamps. This is the difficult part and requires deep understanding in MPEG2 buffers (tstd, eb, mb).

Good luck.

1
  • Thank you very much for your answer. I don't know why it is so, but my program works only on computer (I watch my movie using VLC media player). When I try to play the movie on the TV via DLNA, it doesn't seem to work. In most cases the TV just drops the connect with the computer when the ad starts. But when I try to insert ad without modifying any of the timestamps, the TV is able to play it from time to time. I don't know what can be the reason of it.
    – Dobby007
    Jul 8, 2013 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.