Raspberry Pi mpeg2_mmal (hardware) decode

Where all the user feedback and suggestions for improvements go
Post Reply
ObvB
Posts: 6
Joined: Mon May 29, 2017 8:45 pm

Raspberry Pi mpeg2_mmal (hardware) decode

Post 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.
erik
Site Admin
Posts: 3368
Joined: Sun Aug 21, 2005 3:49 pm

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post by erik »

Good suggestion. Will add
IfThenERROR
Posts: 4
Joined: Tue May 30, 2017 9:36 am

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post 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.
erik
Site Admin
Posts: 3368
Joined: Sun Aug 21, 2005 3:49 pm

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post by erik »

if the mmal codec is unique for raspberry why not search for it and when it fails do the normal find decoder?
IfThenERROR
Posts: 4
Joined: Tue May 30, 2017 9:36 am

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post by IfThenERROR »

That might be a possible way. I'll give it a try and send you a pull request if it works.
ObvB
Posts: 6
Joined: Mon May 29, 2017 8:45 pm

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post 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.
erik
Site Admin
Posts: 3368
Joined: Sun Aug 21, 2005 3:49 pm

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post 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?
IfThenERROR
Posts: 4
Joined: Tue May 30, 2017 9:36 am

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post 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.
erik
Site Admin
Posts: 3368
Joined: Sun Aug 21, 2005 3:49 pm

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post 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?
ObvB
Posts: 6
Joined: Mon May 29, 2017 8:45 pm

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post 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
IfThenERROR
Posts: 4
Joined: Tue May 30, 2017 9:36 am

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post 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.
erik
Site Admin
Posts: 3368
Joined: Sun Aug 21, 2005 3:49 pm

Re: Raspberry Pi mpeg2_mmal (hardware) decode

Post 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
Post Reply