<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p><br>
</p>
<br>
<div class="moz-cite-prefix">On 06/28/2018 04:24 PM, Aman Gupta
wrote:<br>
</div>
<blockquote type="cite"
cite="mid:CAK=uwuzSRw0cR-vny2RLgEtzpLCC_NaDr2_p6eB4uxsJr2L-OA@mail.gmail.com">
<div><br>
</div>
<div><br>
<div class="gmail_quote">
<div dir="ltr">On Thu, Jun 28, 2018 at 3:59 PM Peter Bennett
<<a href="mailto:pb.mythtv@gmail.com"
moz-do-not-send="true">pb.mythtv@gmail.com</a>> wrote:<br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Aman<br>
<br>
I am making progress with mediacodec, but I have hit a new
problems that <br>
I am not sure what to do about.<br>
<br>
When the user Fast Forwards a video, the code decodes a
frame, then <br>
clear buffers, skip forward some period of time in the file
then decodes <br>
another frame, and repeats the process.<br>
<br>
After sending packets to the system and getting back a
frame, it issues <br>
avcodec_flush_buffers then starts with a new set of packets.<br>
<br>
it loops on<br>
avcodec_receive_frame<br>
avcodec_send_packet<br>
until it has a frame (taking account of EAGAIN return codes
as appropriate)<br>
then it runs<br>
avcodec_flush_buffers<br>
Then starts from the beginning again with a frame from
another part of <br>
the file.</blockquote>
<div dir="auto"><br>
</div>
<div dir="auto">I use a similar technique for fast forwarding,
but haven't run into this issue.</div>
<div dir="auto"><br>
</div>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
<br>
This works for a while (maybe 10 or 20 frames) then I get
these error <br>
messages:<br>
<br>
E [amediacodec @ 0x202443e118]
java.lang.IllegalStateException occurred<br>
E [mpeg2_mediacodec @ 0x2024425378] Failed to dequeue
output buffer <br>
(status=-542398533)</blockquote>
<div dir="auto"><br>
</div>
<div dir="auto">If you look at the official MediaCodec docs it
talks about how to deal with these state exceptions. There
are different types, and the decoder can either be reset or
recreated. I haven't had a reproducible way to make this
happen so I haven't added the code to deal with them.
Ideally this would be added to ffmpeg- if you create a patch
I can commit it to ffmpeg.</div>
<div dir="auto"><br>
</div>
</div>
</div>
</blockquote>
The method dequeueOutputBuffer can throw IllegalStateException.
According to the MediaCodec documentation that means it is not in
the Executing state, or codec is configured in asynchronous mode. I
don't have enough understanding of how it works to know why that
would be.<br>
<br>
I believe that this may only happen with 1080i interlaced video.<br>
<br>
The situation only occurs if you send several packets until there
are frames available, then receive only one frame from the codec
context and then flush, expecting to lose any remaining frames.
Doing this sequence repeatedly causes the error after about 10 or 15
times. Somehow those available frames are not being correctly
flushed. By adding my own code to discard those frames I avoid the
problem.<br>
<br>
Add this code before avcodec_flush_buffers:<br>
<br>
int ret = 0;<br>
while (ret == 0)<br>
{<br>
AVFrame *frame = av_frame_alloc();<br>
ret = avcodec_receive_frame(enc, frame);<br>
av_frame_free(&frame);<br>
}<br>
<br>
No More exceptions!!<br>
<br>
It should not be necessary to receive all available frames and
discard them like this before flushing. Perhaps something like this
code snippet should be added to avcodec_flush_buffers.<br>
<br>
Please let me know if you make any change in ffmpeg to solve this
issue so that I can remove this piece of code.<br>
<br>
Peter<br>
<br>
<br>
</body>
</html>