[mythtv] mediacodec errors

Peter Bennett pb.mythtv at gmail.com
Fri Jun 29 21:40:22 UTC 2018



On 06/28/2018 04:24 PM, Aman Gupta wrote:
>
>
> On Thu, Jun 28, 2018 at 3:59 PM Peter Bennett <pb.mythtv at gmail.com 
> <mailto:pb.mythtv at gmail.com>> wrote:
>
>     Hi Aman
>
>     I am making progress with mediacodec, but I have hit a new
>     problems that
>     I am not sure what to do about.
>
>     When the user Fast Forwards a video, the code  decodes a frame, then
>     clear buffers, skip forward some period of time in the file then
>     decodes
>     another frame, and repeats the process.
>
>     After sending packets to the system and getting back a frame, it
>     issues
>     avcodec_flush_buffers then starts with a new set of packets.
>
>     it loops on
>        avcodec_receive_frame
>        avcodec_send_packet
>     until it has a frame (taking account of EAGAIN return codes as
>     appropriate)
>     then it runs
>        avcodec_flush_buffers
>     Then starts from the beginning again with a frame from another
>     part of
>     the file.
>
>
> I use a similar technique for fast forwarding, but haven't run into 
> this issue.
>
>
>
>     This works for a while (maybe 10 or 20 frames) then I get these error
>     messages:
>
>     E  [amediacodec @ 0x202443e118] java.lang.IllegalStateException
>     occurred
>     E  [mpeg2_mediacodec @ 0x2024425378] Failed to dequeue output buffer
>     (status=-542398533)
>
>
> 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.
>
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.

I believe that this may only happen with 1080i interlaced video.

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.

Add this code before avcodec_flush_buffers:

                 int ret = 0;
                 while (ret == 0)
                 {
                     AVFrame *frame = av_frame_alloc();
                     ret = avcodec_receive_frame(enc, frame);
                     av_frame_free(&frame);
                 }

No More exceptions!!

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.

Please let me know if you make any change in ffmpeg to solve this issue 
so that I can remove this piece of code.

Peter


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mythtv.org/pipermail/mythtv-dev/attachments/20180629/7189e69e/attachment.html>


More information about the mythtv-dev mailing list