<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;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.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"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="mso-ligatures:none">From:</span></b><span style="mso-ligatures:none"> Libav-user <libav-user-bounces@ffmpeg.org>
<b>On Behalf Of </b>Vaughn Treude<br>
<b>Sent:</b> Wednesday, September 27, 2023 3:56 PM<br>
<b>To:</b> libav-user@ffmpeg.org<br>
<b>Subject:</b> [Libav-user] Encountering exception while reading all frames of an MKV video file<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<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>
<div style="mso-element:para-border-div;border:none;border-bottom:double windowtext 2.25pt;padding:0in 0in 1.0pt 0in">
<p class="MsoNormal" style="border:none;padding:0in"><o:p> </o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">An update:<o:p></o:p></p>
<p class="MsoNormal">I believe the crash was due to my attempt to access the Ffmpeg-Autogen wrapper with multiple threads. That thing’s static! I put a lock on the decoder routine to prevent simultaneous access and the crash went away.<o:p></o:p></p>
<p class="MsoNormal">That said, it would still be helpful to have a better way to determine the number of frames in the video without needing to read it until I encounter an error code.<o:p></o:p></p>
<p class="MsoNormal">If anyone knows of such an approach, please let me know.<o:p></o:p></p>
<p class="MsoNormal">Thanks again,<o:p></o:p></p>
<p class="MsoNormal">Vaughn T.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="mso-element:para-border-div;border:solid #FFCACA 1.0pt;padding:12.0pt 12.0pt 12.0pt 12.0pt;background:#F3FF33">
<p style="background:#F3FF33;border:none;padding:0in"><b><span style="font-size:10.0pt;color:black">[EXTERNAL EMAIL]</span></b><span style="color:black">
</span><span style="font-size:8.0pt;color:black">DO NOT CLICK links or attachments unless you recognize the sender and know the content is safe.</span><o:p></o:p></p>
</div>
</div>
</body>
</html>