Log in

View Full Version : Cutting an h.264 stream in 1080i without an IDR Frame


svenali
18th August 2009, 09:22
Hello,

I would like to enhance my littlte cutting app with h.264-cutting support. My problem is, that most of my streams are recorded via dvb-s2 from channels like Sky Cinnema HD, History HD and so on. The problem is, that these streams do not use IDR-Frames. The broadcasting stations only sends I-, B- and P-Slices. If they send an I-slice you can find a SPS/PPS Header in front of the I-Slices.

OK. What I do now is: I cut only in front of an I-slice and copy Slice by Slice to a new file. The slices after that I-slice cut in have a smaller PTS. These slices I kick away. The disadvantage is, that the cutted frame has some errors at the beginning (pixel errors). It is logical because the following slices (after that I-slice) are references pictures before that I-slice.

I would only now, if somebody has experience with such kind of streams and if somebody has a better solution for that???

regards,
Sven

Guest
18th August 2009, 14:56
Cutting at an open GOP requires re-encoding of the retained GOP. You can't fix this in the compressed domain.

svenali
18th August 2009, 18:27
Thank you for your reply.

Yes I know and this is my problem. I would like encode this open GOP, but if the stream does not have any IDR-Frame, then it means that I have to reencode the whole stream??? What would you do?

regards,
Sven

Dark Shikari
18th August 2009, 18:37
Cutting at an open GOP requires re-encoding of the retained GOP. You can't fix this in the compressed domain.How about this?

Display order:

BBBIBBBP ... <some commercial break you want to cut> ... BBBIBBBP

Cut the first segment to:

BBBI <--That's right, keep the I-frame!

Cut the second segment to:

IBBBP

Result:

BBBIIBBBP

Downside: Two sequential I-frames.

Upside: I think it might work?

wyti
18th August 2009, 19:12
Well thinked.
But even if you'll not need to reencoding, in your example you will have 6 frames missing, so what happend if that during a motion, or what will happend if he has to drop about 30 frames because I frames aren't there ?

svenali
19th August 2009, 08:14
Thanks Dark Shikari, but I think wyti is right. My Streams are very complicated. Lots of P- and B- slices and not enough I-slices. Also Interlaced, that means that one frame consists of two slices. I don't have I-Frames. I have I slices. And a key frame is in my stream an I-slice containing the SPS/PPS Header in front of them. The I-slice is followed by a P-slice. And both together are a frame.

So I see, I need to reencode the whole stream or have to live with a dirty beginning and ending. If I record a stream via dvb-s2 with my receiver I see, that these transportstreams also have dirty beginnings. OK.

Shat do you think about reencoding. I've never tried it before, because MPEG2 is much easier to cut. Does the quality sinks increasly if I reencode it with x264?

regards,
Sven

Dark Shikari
19th August 2009, 10:32
Thanks Dark Shikari, but I think wyti is right. My Streams are very complicated. Lots of P- and B- slices and not enough I-slices. Also Interlaced, that means that one frame consists of two slices. I don't have I-Frames. I have I slices. And a key frame is in my stream an I-slice containing the SPS/PPS Header in front of them. The I-slice is followed by a P-slice. And both together are a frame. Ah, so it's PAFF, not MBAFF. Good luck then :p

Manao
19th August 2009, 12:00
Cutting at an open GOP requires re-encoding of the retained GOP. You can't fix this in the compressed domain. No. The decoder has all the necessary information to ignore the frames that makes the GOP open.
OK. What I do now is: I cut only in front of an I-slice and copy Slice by Slice to a new file. The slices after that I-slice cut in have a smaller PTS. These slices I kick away.Don't kick away those slices. Just keep them, the decoder will not decode them, but it won't break the first GOP either. It's removing them that breaks the first frames.

svenali
19th August 2009, 23:04
Thanks Dark Shikari. Yes that are PAFF streams. Sadly. Your Good Luck wish says to me, that is impossible to cut these streams without reencoding? Very Very Sad.

@Manao: Did you tried that? Have you experience with cutting PAFF streams?

At last I only want to know, if it is useful and good to cut PAFF streams without reencoding, or is it better to reencode the whole stream. It is a very important question to me, because I don't want to waste time in programming not useful stuff.

regards,
Sven

Manao
20th August 2009, 05:30
PAFF streams aren't more complicated to cut.

svenali
20th August 2009, 08:17
Then I want to summarize Manao.

Cutting PAFF Streams: I have to cut before an I-Slice. The following P and B slices, which have to presented before that I-Slice I let in the cutted stream. And the cut-out? Maybe before a P-Slice? Is that the way I should go?

regards,
Sven

Manao
20th August 2009, 08:22
You can cut out whenever you want, but if you cut before a I/P, you'll keep the end of the video coherent, so it would be better. And what you said applies to any stream, not just PAFF ones.

Just to be sure, you are just cutting, you aren't trying to merge things together afterward, aren't you ?

svenali
20th August 2009, 09:10
Sorry Manao, what do you mean with merge afterward? Do you mean, that I try to merge some h.264 snippets together?

regards,
Sven

Manao
20th August 2009, 09:16
Do you mean, that I try to merge some h.264 snippets together? Yes, I mean exactly that. Because if you want to do that, and if you want the resulting file to play seamlessly over the place the merging occur, you will need to recode.

svenali
20th August 2009, 10:08
Yes that's right. And if so (I mean merging) I need to reencode everything right (when using PAFF).

regards,
Sven

Shevach
20th August 2009, 10:31
I would like to add my two coins into the discussions.

Perhaps your stream contains pictures with MMCO=5. In such case you surely can cut the stream after a frame which contains MMCO=5.

svenali
20th August 2009, 10:46
May I ask you Shevach, in which header information I can retrieve the MMCO=5 value?

regards,
Sven

svenali
21st August 2009, 16:25
I'd like to thank everybody for this very interesting discussion! With your help I understand cutting an h.264 stream a lot better!

@Shevach: I take ffmpeg. I can retrive these value thru the H264Context Struct so I don't need to parse the bytes directly. Thank you very much.

Till the next discussion I say ...

regards,
Sven