<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Hi Michael,</div><div class="gmail_quote"><br></div><div class="gmail_quote">mpeg2video wants the packets to be segmented in a way that logical things go together and that there is no extra "garbage" at the end.</div><div class="gmail_quote">One problem that you have is that your code will feed mpeg2video with last frame in a GOP and the sequence header of the next gop after it. That is not desired by mpeg2video.</div><div class="gmail_quote"><br></div><div class="gmail_quote">The other problem that you have is that you are triggering undefined behaviour with this line in your code:</div><div class="gmail_quote">p2 = memmem(p1 + 4, n, "\x00\x00\x01\x00", 4);<br></div><div class="gmail_quote"><br></div><div class="gmail_quote">from address at p1 there is at least n bytes in the buffer if you are doing the calculations correctly (which I think that you do).</div><div class="gmail_quote">but you are searching from p1+4 up n bytes which goes past the end of your buffer. If there are 00 after the end of your buffer memmem might find a "false" start code.</div><div class="gmail_quote"><br></div><div class="gmail_quote">I think that the line in question should be:</div><div class="gmail_quote">p2 = (char*)memmem(p1 + 4, n-4, "\x00\x00\x01\x00", 4);<br></div><div class="gmail_quote"><br></div><div class="gmail_quote">Probably you can get away with the first problem (headers of the next gop after end of previous gop) since you feed the sequence headers at the start of the stream to the decoder (the other sequence headers are just repeated values)</div><div class="gmail_quote"><br></div><div class="gmail_quote">All in all, you would be better off with already tested parsers and demuxers from libavformat</div><div class="gmail_quote"><br></div><div class="gmail_quote">Jaka</div><div class="gmail_quote"><br></div><div class="gmail_quote"><br></div><div class="gmail_quote">On 6 June 2017 at 22:56, Michael Goffioul <span dir="ltr"><<a href="mailto:michael.goffioul@gmail.com" target="_blank">michael.goffioul@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Jaka,<div><br></div><div>Thanks for your answer. Even though I'm segmenting the data on 0x00 0x00 0x01 0x00, I'm still feeding the entire data between those boundaries to the decoder. So I'm not skipping any data from the stream. Referencing my sample code: p1 points to the current picture start code, p2 points to the next picture start code, feed data between p1 and p2, p1 = p2, restart.</div><div><br></div><div>Is there a particular way to segment the data that is expected by the mpeg2video decoder?</div><span class="gmail-m_-8737402404596118084HOEnZb"><font color="#888888"><div><br></div><div>Michael.</div></font></span><div><div class="gmail-m_-8737402404596118084h5"><div><br></div><div><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 6, 2017 at 4:42 PM, Jaka Bac <span dir="ltr"><<a href="mailto:jakabac@gmail.com" target="_blank">jakabac@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Michael,<div><br></div><div>You are parsing the mpeg2 stream by yourself looking for the 0x00 0x00 0x01 0x00 startcode which means that you will only feed the mpeg2 encoded pictures to the decoder.</div><div>This way your program will skip over the sequence headers which may include a lot of important information for the mpeg2 decoder (for example custom intra and non intra quantiser matrices, picture dimensions, aspect ratio,...)</div><div><br></div><div>The ffmpeg mpeg2video codec will try its best to decode the pictures, but with the missing info it may not be fully successful.</div><div>Your PussInBoots file for example, includes custom quantiser matrices which are probably slightly different from the default ones (I did not look in detail).</div><div><br></div><div>My suggestion would be to use libavformat to read the source files. This way you would get correctly parsed AVPacket(s).</div><div><br></div><div>If you for some reason can't use libavformat, you could at least use av_parser_parse2 function to parse the stream for you. (See example here: <a href="https://www.ffmpeg.org/doxygen/trunk/decode_video_8c-example.html" target="_blank">https://www.ffmpeg.org/d<wbr>oxygen/trunk/decode_video_8c-e<wbr>xample.html</a>)</div><div><br></div><div>Hope that helps,</div><div>Jaka</div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="gmail-m_-8737402404596118084m_-8539639533101539108h5">On 4 June 2017 at 05:12, Michael Goffioul <span dir="ltr"><<a href="mailto:michael.goffioul@gmail.com" target="_blank">michael.goffioul@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="gmail-m_-8737402404596118084m_-8539639533101539108h5"><div dir="ltr">Hi,<div><br></div><div>I'm trying to use ffmpeg to decode a MPEG2 stream and I'm running into pixelation problem with some streams (not all of them). The fact is that the problematic streams play fine in ffplay (or vlc), so I figured the problem must be in the way I setup the decoder or feed data to it.</div><div><br></div><div>I've written a very simple code to illustrate the problem I'm having, it's available here:</div><div><a href="https://drive.google.com/open?id=0BwljeX6541LuTjlLRmN2bG52M3M" target="_blank">https://drive.google.com/open?<wbr>id=0BwljeX6541LuTjlLRmN2bG52M3<wbr>M</a><br></div><div><br></div><div>It doesn't do anything fancy: just initialize the mpeg2video codec, feed the data (segmented on the picture start code boundary), drain frames, convert to RGB and save as PNG. It accepts 2 arguments: the elementary MPEG2 stream filename and the number of frames to extract.</div><div><br></div><div>I'm using this sample clip as test case, it's the elementary video stream extracted from a MPEG/TS clip (next link):</div><div><a href="https://drive.google.com/open?id=0BwljeX6541LuUFFHSElwaUFGYkE" target="_blank">https://drive.google.com/open?<wbr>id=0BwljeX6541LuUFFHSElwaUFGYk<wbr>E</a><br></div><div><a href="https://drive.google.com/open?id=0BwljeX6541LuamdieXZzamNkNFE" target="_blank">https://drive.google.com/open?<wbr>id=0BwljeX6541LuamdieXZzamNkNF<wbr>E</a><br></div><div><br></div><div>The pixelation problem is illustrated in this extracted frame:</div><div><a href="https://drive.google.com/open?id=0BwljeX6541LubmhkS2VWekc5S00" target="_blank">https://drive.google.com/open?<wbr>id=0BwljeX6541LubmhkS2VWekc5S0<wbr>0</a><br></div><div><br></div><div>By comparison, playing the sample clip (elementary stream) with ffplay gives this:</div><div><a href="https://drive.google.com/open?id=0BwljeX6541LuQ0hHQmVZMlAxMmc" target="_blank">https://drive.google.com/open?<wbr>id=0BwljeX6541LuQ0hHQmVZMlAxMm<wbr>c</a><br></div><div><br></div><div>Clearly I must be doing something wrong. Does anybody have a hint or suggestion?</div><div><br></div><div>Thanks,</div><div>Michael.</div><div><br></div></div>
<br></div></div>______________________________<wbr>_________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/list<wbr>info/libav-user</a><br>
<br></blockquote></div><br></div>
<br>______________________________<wbr>_________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/list<wbr>info/libav-user</a><br>
<br></blockquote></div><br></div></div></div></div></div>
<br>______________________________<wbr>_________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" rel="noreferrer" target="_blank">http://ffmpeg.org/mailman/list<wbr>info/libav-user</a><br>
<br></blockquote></div><br></div></div>