Question

In short: I have to encode a tiny amount of video frames and stitch them in front of a much bigger h.264 stream without reencoding said stream.

The details: I receive a multi GB transport stream containing a h.264 es and an audio es. Currently the h.264 streams are always generated using x264, and I can assume this will be the case in the future. Now I have to prepend some video frames to this stream, but am not allowed to decode/encode the whole stream; which leaves me with the only option to find out the exact parameters I need to pass x264_encoder_open so both streams match.

Currently what I'm doing is :

  1. Demux the original ts and extract h.264 NAL packets.
  2. When I find the first "user data unregistered" SEI packet, I parse it and find a bunch of x264 parameters.
  3. Use libavcodec to start decoding the video. That gives me the dimensions of the picture and the h264 profile and level in the AVCodecContext structure.
  4. Match all of that as best as I can in the x264_param_t structure.

I can do some encoding with that, and the encoded video plays correctly up to the join point. When VLC is reaching the stitch point it starts throwing out the following sequence of messages and soon after stops playing:

[h264 @ 0x7fe36cd75be0] decode_slice_header error
[h264 @ 0x7fe36cd75be0] no frame!
[h264 @ 0x7fe36ccc9080] Width/height changing with threads is not implemented. Update your Libav version to the newest one from Git. If the problem still occurs, it means that your file has a feature which has not been implemented.

which clearly shows that my encoded frames do not match the original ones. I've been browsing the source code and can't seem to find a way of doing this. What I have currently (besides not working), involves a lot of guesswork, so even if I could make it work with the handful of example files I have, I would be scared to deploy this in production servers.

So the obvious question is: Is there a safe, formal way of doing this?

Thanks in Advance

Was it helpful?

Solution

Finally I solved it myself. Turns out that you only need to match the image size and aspect ratio, so it can be as simple as:

x264_param_t xparam ;
x264_param_default( &xparam );

xparam.i_width          = width  ;
xparam.i_height         = height ;
xparam.vui.i_sar_height = 1      ;
xparam.vui.i_sar_width  = 1      ;

x264_t * x264 = x264_encoder_open( &xparam );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top