Menion
Posts: 17
Joined: Mon Feb 22, 2016 6:52 pm

MMAL error 2 on control port

Wed Jan 11, 2017 8:53 am

Hi all
I'm working on improving the GPU accelerated transcoding for TV Headend. TV Headend use libavcodec for the transcoding job
I'm running into trouble when using mpeg2_mmal codec for the decoding of the input stream. If I use mpeg2video native (software) it works ok
The error I get is:

Code: Select all

MMAL error 2 on control port
According to the MMAL interface, error 2 is ENOSPC
Looking at the vcdb log I see:

Code: Select all

alloc_compact_internal(space=3291328, in_low_region=0, mode=7) failed
I'm a little bit lost here. I have noticed that if I transcode the same input stream, with ffmpeg tool (that use the veruy same libavcodec I link to tv headend) it works, but apparently mpeg2_mmal output a decoded stream of 768x576 even if the input, in my case is 320x240.
Does the mpeg2_mmal codec require some special pre initialization or?
Thanks for the support

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9835
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: MMAL error 2 on control port

Wed Jan 11, 2017 10:28 am

Have you bought and installed the licence for MPEG2 decode? http://www.raspberrypi.com/mpeg-2-license-key/

Code: Select all

vcgencmd codec_enabled MPG2
to check whether mpeg2 is enabled or disabled.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Menion
Posts: 17
Joined: Mon Feb 22, 2016 6:52 pm

Re: MMAL error 2 on control port

Wed Jan 11, 2017 10:41 am

Yes, I have the license and it is enabled
I have actually made one step forward. I have set the output resolution to 720x576 and now I don't get MMAL error 2 from the control port, but still no decoded frame. Unfortunately libavcodec is not so verbose in the mmal decoding. I'm pretty sure there is some missing/wrong codec initialization, required for MMAL and not for software decoders

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9835
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: MMAL error 2 on control port

Wed Jan 11, 2017 11:17 am

Simplify the test - can omxplayer play an MPEG2 file?

The memory allocation failure looks odd. What gpu_mem value are you using?
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Menion
Posts: 17
Joined: Mon Feb 22, 2016 6:52 pm

Re: MMAL error 2 on control port

Wed Jan 11, 2017 11:20 am

I'm using 64Mb of memory. I cannot test omxplayer right now, due to lack of connected display, however the ffmpeg tool is actually capable to decode via mmal, so I think there is some configuration issue, but I cannot tell which one for the moment

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9835
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: MMAL error 2 on control port

Wed Jan 11, 2017 11:36 am

Menion wrote:I'm using 64Mb of memory. I cannot test omxplayer right now, due to lack of connected display, however the ffmpeg tool is actually capable to decode via mmal, so I think there is some configuration issue, but I cannot tell which one for the moment
Configuration within ffmpeg, or on the GPU, or somewhere else? Narrow it down.

Even testing omxplayer headless would be worthwhile as I'd expect it to throw an error immediately if there is a memory issue.

If transcoding using hardware acceleration for both encode and decode then I'd look at increasing the gpu_mem, at least for a test.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Menion
Posts: 17
Joined: Mon Feb 22, 2016 6:52 pm

Re: MMAL error 2 on control port

Wed Jan 11, 2017 2:12 pm

Hi, sorry for being not clear, I try to explain better
I have compiled a ffmpeg 3.2.2 custom on my RPi3 with mmal and omx support
I can decode via mmal and encode via omx (hw->hw) correctly if I use the ffmpeg tool:

Code: Select all

pi@raspberrypi:/media/tvrec $ ffmpeg -vcodec mpeg2_mmal -i Prova.ts -vf yadif -c:a aac -c:v h264_omx -sn -b:v 128k prova2.mkv
ffmpeg version 3.2.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.9.2 (Raspbian 4.9.2-10)
  configuration: --prefix=/usr --enable-mmal --enable-omx --enable-omx-rpi --enable-decoder=h264_mmal --enable-encoder=h264_omx --enable-encoder=h264_omx --enable-libx264 --enable-libx265 --enable-nonfree --enable-gpl --enable-avresample
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.101 / 57. 64.101
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libavresample   3.  1.  0 /  3.  1.  0
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
  libpostproc    54.  1.100 / 54.  1.100
[mpegts @ 0x185a300] PES packet size mismatch
    Last message repeated 2 times
Input #0, mpegts, from 'Prova.ts':
  Duration: 09:56:08.63, start: 117669.938122, bitrate: 6 kb/s
  Program 4005 
    Metadata:
      service_name    : Canale5
      service_provider: Mediaset
    Stream #0:0[0x64a]: Video: mpeg2video ([2][0][0][0] / 0x0002), yuv420p(top first), 320x240, 25 fps, 25 tbr, 90k tbn, 50 tbc
    Stream #0:1[0x64b](ita): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, stereo, s16p, 192 kb/s
    Stream #0:2[0x64c](eng): Audio: mp2 ([4][0][0][0] / 0x0004), 48000 Hz, stereo, s16p, 128 kb/s
    Stream #0:3[0x653](ita,ita): Subtitle: dvb_teletext ([6][0][0][0] / 0x0006)
File 'prova2.mkv' already exists. Overwrite ? [y/N] y
[h264_omx @ 0x19b4440] Using OMX.broadcom.video_encode
Output #0, matroska, to 'prova2.mkv':
  Metadata:
    encoder         : Lavf57.56.100
    Stream #0:0: Video: h264 (h264_omx) (H264 / 0x34363248), yuv420p, 320x240, q=2-31, 128 kb/s, 25 fps, 1k tbn, 25 tbc
    Metadata:
      encoder         : Lavc57.64.101 h264_omx
    Stream #0:1(ita): Audio: aac (LC) ([255][0][0][0] / 0x00FF), 48000 Hz, stereo, fltp, 128 kb/s
    Metadata:
      encoder         : Lavc57.64.101 aac
Stream mapping:
  Stream #0:0 -> #0:0 (mpeg2video (mpeg2_mmal) -> h264 (h264_omx))
  Stream #0:1 -> #0:1 (mp2 (native) -> aac (native))
Press [q] to stop, [?] for help
[mpeg2_mmal @ 0x19ca8f0] Changing output format.
Input stream #0:0 frame changed from size:320x240 fmt:yuv420p to size:720x576 fmt:yuv420p48x    
[mpegts @ 0x185a300] PES packet size mismatchme=00:00:38.55 bitrate= 256.8kbits/s speed=0.987x    
frame=  961 fps= 24 q=-0.0 Lsize=    1240kB time=00:00:39.24 bitrate= 259.0kbits/s speed=0.989x    
video:603kB audio:614kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.905568%
[aac @ 0x19f05a0] Qavg: 777.114
Then I moved to tvheadend. It's compiled to use the same share libraries that the ffmpeg tool use as well.
TV Headend works ok if I use mpeg2video native (sw) as decoder and h264_omx as encoder. But doing so the CPU is overkilled. This is why I wanted to use mpeg2_mmal as decoder. So I have made a simple modification, to pickup mpeg2_mmal as decoder and checked. Originally I got the MMAL error 2 and the memory allocation issue I have mentioned. Then looking at the ffmpeg output I have seen that mpeg2_mmal output a 720x576 decoded picture from the 320x240 input, so I tried to initialize the mpeg2_mmal codec with 720x576 as picture size and the MMAL error 2 disappeared. Now I'm actually getting ahead. The problem now is that avcodec_decode_video2 apparently return that a picture has been decoded, but with size = 0 which should mean that no frame has been decoded. But if I look at the decoded_frame info, they are valid. So I pass the decoded frame to the encoder (h264_omx) and I get a blank output

the sudo vcdbg log msg says:

Code: Select all

962433.862: venc: venc_rc: frame 86 aborted bits 57024 > 45581 (HRD), QP=23
962458.071: venc: venc_rc: frame 87 aborted bits 61376 > 45581 (HRD), QP=23
962482.601: venc: venc_rc: frame 88 aborted bits 63632 > 45581 (HRD), QP=23
962541.404: venc: venc_rc: frame 89 aborted bits 56656 > 45581 (HRD), QP=23
962565.607: venc: venc_rc: frame 90 aborted bits 55344 > 45581 (HRD), QP=23
962590.038: venc: venc_rc: frame 91 aborted bits 54000 > 45581 (HRD), QP=23
962675.000: venc: venc_rc: frame 92 aborted bits 60392 > 45581 (HRD), QP=23
962698.278: venc: venc_rc: frame 93 aborted bits 54592 > 45581 (HRD), QP=23
962723.066: venc: venc_rc: frame 94 aborted bits 55024 > 45581 (HRD), QP=23
962749.598: venc: venc_rc: frame 95 aborted bits 54080 > 45581 (HRD), QP=23
I think I'm close but I miss something. Probably AVCodex * needs some special parameters when using mpeg2_mmal codec, but I can't understand which one. the ffmpeg tool use some other, lower level APIs for the decoding, so I cannot understand which could be the critical parameter to set for having a valid decoded output from the codec. Can you help on this topic also?
Thanks for the support

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 9835
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: MMAL error 2 on control port

Wed Jan 11, 2017 2:42 pm

Code: Select all

venc: venc_rc: frame 91 aborted bits 54000 > 45581 (HRD), QP=23
rc = rate control. Quantisation Parameter (QP) has been set to 23 but the result has come out significantly over bit budget so is discarded.
That shouldn't happen on every frame as the bits it didn't send last frame are added to the budget for the next frame.
128kbit/s for a video stream is pretty low, even at 320x240.

There should be nothing extra needed by MMAL video_decode over and above the header bytes.

Is your source interlaced? The log denotes "top first" but you appear not to be deinterlacing. The encoder does not handle interlaced video and will just encode the whole frame.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Menion
Posts: 17
Joined: Mon Feb 22, 2016 6:52 pm

Re: MMAL error 2 on control port

Wed Jan 11, 2017 2:47 pm

Hi
The "sudo vcdbg log msg" log doesn't belong to the example of the ffmpeg encoding. The ffmpeg encoding works just fine, no error e no error in vcdbg logs. The vcdbg logs belongs to the encoding with TV Headend, when using mpeg2_mmal as decoder. If I just use software mpeg2 decoder, it works, so it must be something in the output of mpeg2_mmal confusing h264_omx encoder

Return to “Graphics programming”