lieff / minimp4

Minimalistic MP4 mux/demux single header library
Creative Commons Zero v1.0 Universal
345 stars 59 forks source link

Access Unit Delimiter (AUD) handling #27

Closed leo-cydar closed 3 years ago

leo-cydar commented 3 years ago

I'm using this library to mux a H.264 bitstream that contains type 9 NAL units (Access unit delimiter) before each full frame.

According to Wikipedia, the Unit Access Delimiter is used as a basic prefix NAL unit to aid in interpreting frames:

A set of NAL units in a specified form is referred to as an access unit. The decoding of each access unit results in one decoded picture. Each access unit contains a set of VCL NAL units that together compose a primary coded picture. It may also be prefixed with an access unit delimiter to aid in locating the start of the access unit.

The code on master will return an error if it encounters an AUD, whereas it seems to work fine by skipping over the NAL unit. I think this is because it's an optional 'helper' unit that the library does not depend on to correctly interpret the stream.

The code I've experimented with essentially changes a return err statement to instead continue parsing other NAL units in the buffer:

2343| if (9 == payload_type)
2344|     return err; /* access unit delimiter */
2343| if (9 == payload_type)
2344|     continue; /* access unit delimiter */

It would be nice not to have to maintain a long lived fork, so I'm submitting this as a way of opening discussion to solutions for handling such streams (as are produced by the built-in Windows H.264 Encoder). More than happy to contribute code.

lieff commented 3 years ago

Hi) Sorry for late reply. This looks like correct change, this code was written before slices support, so 1 frame = 1 nalu was assumed. Mind make a PR?

leo-cydar commented 3 years ago

Thanks @lieff , that's submitted.

lieff commented 3 years ago

Thanks) Merged.