<div>Not sure about libavformat, but I know some facts:</div><div>- sps and pps nal units are encoded by Golomb codes. This means there is no simple way for mapping.<br />-šyou can seešff_h264_decode_extradata() function from libavcodec/h264.c, it's decoding them as nal units, fillingšH264Context struct. Maybe there's a sense to use this code. Or you can just check, the fields ofšH264Context are filled by this func, and try to get them after using avcodec_decode_video() with sps/pps nal units as buffer of AVPacket.</div><div>š</div><div>--š</div><div>ó Õ×ÁÖÅÎÉÅÍ,</div><div>óÌÏÂÏÄÅÎÀË áÌÅËÓÁÎÄÒ éÇÏÒÅ×ÉÞ</div><div>š</div><div>ËÏÎÔÁËÔÎÙÊ ÔÅÌÅÆÏÎ: +7-925-050-64-62</div><div>š</div><div>š</div><div>š</div><div>30.05.2016, 10:49, "Sampsa Riikonen" <sampsa.riikonen@iki.fi>:</div><blockquote type="cite"><p>Hello,<br /><br />I have a code in which I am receiving H264 sps, pps and frame packets <br />and want to mux them into matroska, using libav*.<br /><br />As instructed in the sample codes, I am doing roughly the following:<br /><br />AVFormatContext *oc;<br />AVCodec *codec;<br />AVCodecContext *ctx;<br />AVStream *stream;<br />AVPacket *avpkt;<br /><br />avformat_alloc_output_context2(&oc, NULL, NULL, "output.mkv");<br /><br />codec = avcodec_find_decoder(AV_CODEC_ID_H264); // AVCodec = possible <br />parameters for a codec<br />ctx = avcodec_alloc_context3(codec); // reserve memory for <br />codec related thingies.. AVCodecContext = defines codec parameters (as <br />per AVCodec)<br />stream = avformat_new_stream(oc,ctx->codec); // add new stream to <br />AVFormatContext ctx->codec is "AVCodec"<br /><br />avcodec_copy_context(stream->codec,ctx); // stream.codec == <br />AVCodecContext (we populate the codec parameters for stream "stream")<br /><br />// THE QUESTION: How do we populate stream->codec, i.e. AVCodecContext <br />instance, correctly for the stream? (***)<br /><br />avio_open(&oc->pb, filename, AVIO_FLAG_WRITE); // open the matroska <br />file ..<br />ret = avformat_write_header(oc, NULL);<br /><br />...<br /><br />av_interleaved_write_frame(oc,avpkt); // pass packets to the muxer<br /><br /><br />The problem is, that prior to opening the matroska file, stream->codec <br />has no information about picture dimensions (matroska header requires <br />pic dimensions).<br />I can solve this by passing sps, pps and an i-frame to a decoder (with <br />avcodec_decode_video2), which populates an AVCodecContext with correct <br />picture information, and then use that AVCodecContext.<br />However, this seems to be an overkill.. I do not want to decode <br />anything, just to get the codec parameters from sps and pps. Is there a <br />simpler way to achieve this?<br /><br />In fact, for files (AVFormatContext), there is the routine <br />"avformat_find_stream_info" for such task. At the present case, <br />however, I only have raw sps, pps and frame packets.<br /><br />One observation more: the sps, pps and I-frame (nal unit types 7,8 & 5), <br />must the aggregated into a single packet in order for the matroska muxer <br />to work.<br /><br />Regards,<br /><br />Sampsa<br /><br />_______________________________________________<br />Libav-user mailing list<br /><a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br /><a href="http://ffmpeg.org/mailman/listinfo/libav-user">http://ffmpeg.org/mailman/listinfo/libav-user</a></p></blockquote>