Page 1 of 1

Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Mon May 29, 2017 9:09 pm
by ObvB
Comskip should have the option to use mpeg2_mmal decoder on a Raspberry Pi.

For myself, I made the following change to the source code ...

Code: Select all

 mpeg2dec.c
Old
		codec = avcodec_find_decoder(codecCtx->codec_id);

New
        if (codecCtx->codec_id == AV_CODEC_ID_MPEG2VIDEO) 
        {
		    codec = avcodec_find_decoder_by_name("mpeg2_mmal");
        }
        else
        {
            codec = avcodec_find_decoder(codecCtx->codec_id);
        }
Obviously, if this were implemented it would need to be an option switched on/off by build configuration and/or command line switch and/or comskip.ini settings.

I have done some testing, and it seems to be working well for me so far.

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Tue May 30, 2017 8:12 am
by erik
Good suggestion. Will add

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Tue May 30, 2017 10:21 am
by IfThenERROR
May I suggest the addition as follows?

Code: Select all

    if (codecCtx->codec_id == AV_CODEC_ID_MPEG2VIDEO)
    {
    	codec = avcodec_find_decoder_by_name("mpeg2_mmal");
    }
    else if (codecCtx->codec_id == AV_CODEC_ID_H264)
    {
    	codec = avcodec_find_decoder_by_name("h264_mmal");
    }
    else
    {
		codec = avcodec_find_decoder(codecCtx->codec_id);
}
Unfortunately it seems ffmpeg doesn't recognize the RPi's VPU as hardware decoder, so you would need an additional switch.

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Wed May 31, 2017 9:02 am
by erik
if the mmal codec is unique for raspberry why not search for it and when it fails do the normal find decoder?

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Wed May 31, 2017 11:35 am
by IfThenERROR
That might be a possible way. I'll give it a try and send you a pull request if it works.

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Wed May 31, 2017 6:25 pm
by ObvB
Even if the mmal codecs are present, it may not always be correct to use them on all Raspberry Pis

It may be possible that there is no MPEG-2 licence. I've not tested, but I'm pretty sure mpeg2_mmal won't work in this case.

If there is not enough memory allocated to the GPU, using the mpeg2_mmal codec can cause failures. The (not very helpful) message "Empty input" is what I encountered. (The default of gpu_mem=64 is not enough).

My simple hard-coded hack is good enough for me where I know what my hardware/config is, but a generalized implementation needs to be more careful. Especially since it may not be obvious to the user what has caused the problem based on error the messages.

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Thu Jun 01, 2017 8:04 am
by erik
Clear
But why not use the already available switch and ini setting to enable/disable the HW decoding using the hardware_decode boolean?

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Fri Jun 02, 2017 12:32 am
by IfThenERROR
Generally agree.
It should be obvious that you can't hardware decode on a device that has a locked decoder.

The issue with not enough memory on the other hand should tested. I've read from people in the RPi forums who claim to run the hw decoder on just 64 MB video memory. That may or may not be true though.

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Fri Jun 02, 2017 7:11 am
by erik
If you can enable/disable the HW decoder everything should be covered?
Enable it, see if speed improves, if not, disable.
Or are there multiple HW decoders on the Pi?

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Fri Jun 02, 2017 9:07 am
by ObvB
IfThenERROR wrote:Generally agree.
It should be obvious that you can't hardware decode on a device that has a locked decoder.

The issue with not enough memory on the other hand should tested. I've read from people in the RPi forums who claim to run the hw decoder on just 64 MB video memory. That may or may not be true though.
It can depend on the video being decoded. SD MPEG2 (720x480) hw decodes in the default 64MB.
erik wrote:Or are there multiple HW decoders on the Pi?
mmal is the only one I think ... certainly the only one supported by ffmpeg ... https://trac.ffmpeg.org/wiki/HWAccelIntro

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Fri Jun 02, 2017 10:43 am
by IfThenERROR
ObvB wrote:mmal is the only one I think
Actually mmal is the VPU hardware on the RPi, but it supports 4 decoders: mpeg2, mpeg4, h.264 and vc1. All are supported in ffmpeg.

As I wrote in the OSMC forum they are all pretty easy to enable for the ffmpeg command line interface, but obviously not so easy to correctly call from a program like the mpeg2dec.

The bad thing about the decoders is, they may very well differ in the state of activation. h.264 is always unlocked, mpeg2 and vc1 each need an additional license to be purchased and are locked by default. Not sure about mpeg4. So there are at least 4 possible combinations of available decoders on the RPi.

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Posted: Fri Jun 02, 2017 11:02 am
by erik
In comskip you should allow someone to enable the HW decoder, test if it works and if its faster and if happy, leave enabled.
Don't try to solve all configuration problems