[mythtv] MPEG2 fixup tool

Geoffrey Hausheer mythtv0368 at phracturedblue.com
Wed Oct 26 03:40:54 UTC 2005


Ok, well here is a very preliminary crack at an MPEG2 fixup tool. 
What it does is to insert spare P-frames into a video stream while
demuxing, such that a fixed frame-rate can occur throughout the entire
video, and the video can be safely remuxed without A/V desync.

As I've hooked up an MPEG2 encoder to generate the P-frames, this can
likely be used to reencode around commercial cutpoints, and possibly
to fix broken frames.

How it works is as follows:
It demuxes the audio and video, keeping track of the video PTS.  When
the PTS is larger than it should be by 1/2 the frame rate, a new frame
(copy of the current frame) is added and the frame numbers are updated
accordingly.  The demuxed files are written to disk as temp.m2v and
temp.mp2.

Once it is complete, I use dvb-mplex to remux the stream into a
'clean' MPEG2 stream as follows:
dvb-mplex -o out.mpg -i ES_STREAM -t MPEG2 -v delta temp.mp2 temp.m2v

delta is the number reported when the demuxer starts.

I have noticed that dvb-mplex doesn't produce 100% ideal PS streams
(some of the PTS values are missing..which is allowed by the MPEG2
spec, but not preferred), but it is the most compliant muxer I've seen
so far.

Notes:
This is VERY preliminary code.  It works on several of my TS streams,
but that doesn't mean much.
Assumptions:
The audio isn't missing any frames, and that the audio PTS is continuous.

One video and one audio stream in the original mpg, with stream
0=video, and 1=audio

The audio is in mp2 format

The PTS has no discontinuities either forward or backwards

The code can only handle cases where there are to few frames to meet
the rate requirement.  If there are too many frames, your SOL.

There are no rate-changes in the code (actually, this won't matter
during decode, but the multiplexer won't be happy, and the result may
not be compliant)

There is no multiplexer in the code, and I'm not sure if there are any
TS capable ones out there (haven't actually looked), so the output
will likely be PS

Only MPEG2 is handled currently, though in theory, it could be made to
work with any format that ffmpeg can avformat/avcodec can handle.
---
I am using this code to clean up TS streams that my Roku HD1000
doesn't like, and the output is about as good as I think I can get. 
Fixing most of the above issues shouldn't be too hard.

I will try to handle some of the above issues (though I don't know if
I have any streams that can be used to test many of them).  After
that, I'll try to rewerite the mpeg2trans code in mythtv to handle
commercial cutting.  In theory, we could create DVD compliant streams
sometime down the line, but that is a significnatly more complex task.

I'm not sure how much interest there is in this code as it stands, but
I thought i'd put it out there for y'all.

To compile, just drop it in your mythtv rool directory, and use the
command at the top of the file.  It uses libavcodec/libavformat to do
most of the grunt work.

.Geoff
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mpegfix-0.0.1.c
Type: text/x-csrc
Size: 16153 bytes
Desc: not available
Url : http://mythtv.org/pipermail/mythtv-dev/attachments/20051025/1cd561cd/mpegfix-0.0.1.c


More information about the mythtv-dev mailing list