[Libav-user] What to pass to avcodec_decode_video2 for H.264 Transport Stream?

Danny Wilson danny at klarity.tv
Fri Nov 25 03:46:38 EET 2016

I want to decode H.264 video from a collection of MPEG-2 Transport Stream packets but I am not clear what to pass to `avcodec_decode_video2`

The documentation [1] says to pass "the input AVPacket containing the input buffer."

But what should be in the input buffer?

A PES packet will be spread across the payload portion of several TS packets, with NALU(s) inside the PES. So pass a TS fragment? The entire PES? PES payload only?

Or do I strip the PES header and pass the ES starting at the start code?

This Sample Code [2] mentions:

BUT some other codecs (msmpeg4, mpeg4) are inherently frame based, so
you must call them with all the data for one frame exactly. You must
also initialize 'width' and 'height' before initializing them. 

But I can find no info on what "all the data" means...  I'm using AV_CODEC_ID_H264. How do I know if that codec can decode in chunks or do I need to reassemble?

Passing a fragment of a TS packet payload is not working:

	AVPacket avDecPkt;
	avDecPkt.data = inbuf_ptr;
	avDecPkt.size = esBufSize;

	len = avcodec_decode_video2(mpDecoderContext, mpFrameDec, &got_picture, &avDecPkt);
	if (len < 0)
		printf("  TS PKT #%.0f. Error decoding frame #%04d [rc=%d '%s']\n",
			tsPacket.pktNum, mDecodedFrameNum, len, av_make_error_string(errMsg, 128, len));


    [h264 @ 0x81cd2a0] no frame!
    TS PKT #2973. Error decoding frame #0001 [rc=-1094995529 'Invalid data found when processing input']

And I get lots and lots of these errors!

  [1]: https://www.ffmpeg.org/doxygen/2.7/group__lavc__decoding.html#ga99ee61b6dcffb7817a275d39da58cc74
  [2]: https://fossies.org/linux/ffmpeg/doc/examples/decoding_encoding.c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20161125/60ca7830/attachment.html>

More information about the Libav-user mailing list