NAL_UNIT_AU_DELIMITER (9) Problem
Hi,
I am trying to decode H264 bitmap streams encoded by a Microsoft H264 encoder. For some reason, the first frame always fails to decode properly, with the following error:
2 - parse_nal(), no exist Sequence Parameter Sets ahead of sequence when try to decode NAL(type:9).
The code which throws the error is found in au_parser.cpp:
if (! (IS_SEI_NAL (pNalUnitHeader->eNalUnitType) || IS_SPS_NAL (pNalUnitHeader->eNalUnitType) || pCtx->bSpsExistAheadFlag)) { if (pCtx->bPrintFrameErrorTraceFlag) { WelsLog (pLogCtx, WELS_LOG_WARNING, "parse_nal(), no exist Sequence Parameter Sets ahead of sequence when try to decode NAL(type:%d).\n", pNalUnitHeader->eNalUnitType); } pCtx->iErrorCode = dsNoParamSets; return NULL; }
I used a tool to analyze the first H264 frame that I am getting from the server. The first NAL is of type 9, or NAL_UNIT_AU_DELIMITER. I found that this type was defined in the headers, but that no code appears to be using it.
The second NAL in the bitmap stream is of type 7, which seems to be what the failing check was looking for:
!! Found NAL at offset 4 (0x0004), size 2 (0x0002) XX 00 00 00 01 09 10 ==================== NAL ==================== forbidden_zero_bit : 0 nal_ref_idc : 0 nal_unit_type : 9 ( Access unit delimiter ) ======= Access Unit Delimiter ======= primary_pic_type : 0 ( I ) !! Found NAL at offset 10 (0x000A), size 24 (0x0018) XX 00 00 00 01 67 42 C0 1F 95 A0 10 01 86 9B 20 00
==================== NAL ==================== forbidden_zero_bit : 0 nal_ref_idc : 3 nal_unit_type : 7 ( Sequence parameter set ) ======= SPS ======= ...
What would be the easiest way to make OpenH264 work with this? The H264 frames are received in the context of an RDP8.1 session, where the server is using H264 as a generic codec to encode remote desktop graphics. I do not know how important that NAL is, maybe I could manually strip it from the bitmap stream before passing it to OpenH264.
Yeah, there is something deficient with the decoder to some defined type in standard. one way is to ignore the reporting error and go on decoding. another way is as you mentioned, strip it directly.
@huili2 I am not an H.264 expert, but I still went ahead and read enough of the spec to figure out how to strip the NAL in question. However, it did not yield any interesting results. My first attempt at working around the issue was to call DecodeFrame2 with the exact same parameters twice when it fails, but that didn't work. However, one of our devs tried doing a second call to DecodeFrame2 with empty parameters, and for some odd reason it works. Here is the hack in question: https://github.com/awakecoding/FreeRDP/blob/8321d7ffadd23e0eaf469f4f593b3bd938fbd01d/libfreerdp/codec/h264.c#L372
I don't like hacks which I don't fully understand, but this seems to be putting the decoder back onto its feet. In RDP8.1 H.264, a set of clipping regions is passed alongside the H.264 bitstream which is fed into OpenH264. One thing I noticed is that with the failing frame, the clipping rects would be off by one frame. By this I mean that the clipping rects passed for the first frame would correctly fit the second frame when decoded. I assume this is the case, because when dumping the decoded frames, only specific regions of it contain valid image data that should be copied. The valid regions matched correctly the clipping rects passed for the previous frame. With the second DecodeFrame2 call hack, those clipping rects are now back in sync with the valid image data of the current decoded frame.
@awakecoding the "hacking" method is actually a correct method to immediately refresh the decoder for output. As I mentioned, decoder cannot deal with type = 9. So it needs to wait for the next packet come to judge if current picture is complete. This leads to delay decoding, and that's the case in our console code you can see. I guess what you wanted is to get the output right after current data is sent into decoder. So you can just use the "hacking" method. btw: only when sDstBufInfo.iBufferStatus == 1, there would be valid decoded output pointed by pData, and the output data information (width, height, stride) is described in sDstInfo. Otherwise, you should not output the data. This should solve the doubt in your last paragraph.
@huili2 great, so the "hack" is not too much of a hack after all. It works fine for RDP8.1 usage with it. How much work would it be to add support for the missing NAL? It's not necessary since it already works with the "hack", but it would be cleaner to get the NAL type 9 supported instead.
@awakecoding I agree, and it was in our to-do-list, but maybe it is not in high priority till now as you find that it can be done by some other method. We'll inform here when it is finished.
this support is in master now. Please let us know if it does not work with the bit streams you have.