<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"Cascadia Mono";
panose-1:2 11 6 9 2 0 0 2 0 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ligatures:standardcontextual;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;
mso-ligatures:none;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Hello all,<o:p></o:p></p>
<p class="MsoNormal">I'm using the FFMPEG libraries within an FFmpeg-Autogen C# wrapper in Windows Framework 4.8 to decode a video from an MKV (Matroska) container.<o:p></o:p></p>
<p class="MsoNormal">I open the file and initialize the stream using the following sequence:<o:p></o:p></p>
<p class="MsoNormal"> ffmpeg.avformat_open_input(&pFormatContext, url, null, null).ThrowExceptionIfError();<o:p></o:p></p>
<p class="MsoNormal"> ffmpeg.avformat_find_stream_info(_pFormatContext, null).ThrowExceptionIfError();<o:p></o:p></p>
<p class="MsoNormal"> AVCodec* codec = null;<o:p></o:p></p>
<p class="MsoNormal"> _streamIndex = ffmpeg<o:p></o:p></p>
<p class="MsoNormal"> .av_find_best_stream(_pFormatContext, AVMediaType.AVMEDIA_TYPE_VIDEO, -1, -1, &codec, 0)<o:p></o:p></p>
<p class="MsoNormal"> .ThrowExceptionIfError();<o:p></o:p></p>
<p class="MsoNormal"> _pCodecContext = ffmpeg.avcodec_alloc_context3(codec);<o:p></o:p></p>
<p class="MsoNormal"> ffmpeg.avcodec_parameters_to_context(_pCodecContext, _pFormatContext->streams[_streamIndex]->codecpar)<o:p></o:p></p>
<p class="MsoNormal"> .ThrowExceptionIfError();<o:p></o:p></p>
<p class="MsoNormal"> ffmpeg.avcodec_open2(_pCodecContext, codec, null).ThrowExceptionIfError();<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">After initializing, I can read each frame of the video stream using av_read_frame. I was assuming that at the end, av_read_frame would return an EOF error code. But it doesn't return any kind of error, so I continue with the process, calling
<span style="font-size:9.5pt;font-family:"Cascadia Mono";color:black">avcodec_send_packet with the packet of frame data I’ve supposedly just read, which</span> gives me an illegal memory access exception.<o:p></o:p></p>
<p class="MsoNormal">My normal approach would be to get the length of the video in frames and not read beyond this number. Unfortunately, I've been unable to find a way to determine the length of an MKV video in frames.<o:p></o:p></p>
<p class="MsoNormal">The nb_frames member of the stream object is never set to anything but zero. Alternately, I could multiply the duration in seconds by the frame rate, but the duration member is always set to a large-valued negative number.<o:p></o:p></p>
<p class="MsoNormal">How can I read all the frames in the file and exit gracefully at the end of the stream?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thank you kindly for any hints you can give me,<o:p></o:p></p>
<p class="MsoNormal">Vaughn T.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>