Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

h.264 "fixer" tool #95

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

h.264 "fixer" tool #95

wants to merge 1 commit into from

Conversation

MikeIndiaAlpha
Copy link

There is long(ish) comment written in the commit itself, but a few extra things:

  • Most of my changes and all "interesting" code is in h264_fixer.*, so new files
  • Changes in the existing Mist code are limited to h264.* - basically extensions to current SPS and slice header parsing code allowing for some extra fields to be read, and one small change in bitstream.h - getter method giving access to bit offset of the bitstream reader.
  • I tried hard to follow Mist coding conventions, however the clang-format failed to do all the stuff automatically for me and so I had to do changes manually. Hopefully all is fine

IMPORTANT:
This code has no effect yet (apart from some extra fields parsed out of h.264 streams). Fixer needs to be called after segmentation, and this is not done yet. We agreed that somebody from the Mist team will take care of this.

EXAMPLE (this is using tools for parsing raw h.264 stream, actual Mist code will have NALs stripped out of 0,0,1 sequences already:

  const char *nal = nalu::scanAnnexB(stream, size);
  while (nal) {
    // find next NAL
    const char *next_nal = nalu::scanAnnexB(nal + 4, remaining - 4);
    // compute size of current NAL
    size_t nal_size = next_nal ? (next_nal - nal) : remaining;
    // found next NAL, put it in the buffer to emulate mist-style processing
    // we need 4 extra bytes before start code
    {
      std::vector<char> buffer;
      buffer.insert(buffer.end(), nal_size + 1, 0);
      memcpy(buffer.data() + 1, nal, nal_size);
      // offset points at actual start code, after 0, 0, 1 bytes
      size_t offset = 4;
      size_t size = nal_size - 3;
      fixer.process_nal(buffer.data(), offset, size);
      // write start code prefix
      char prefix[] = { 0, 0, 1 };
      fwrite(prefix, 3, 1, output);
      // write NAL
      fwrite(buffer.data() + offset, size, 1, output);
    }
    // move forward
    nal = next_nal;
    remaining -= nal_size;
  }

It turned out that some h.264 streams are hard to segment. Instead
of having periodic IDR frames, they only contain very first IDR
frame, and just I frames after that. Technically, some of these I
frames are equivalent of IDR ones (after them there are no inter
predictions to previous frames), but aren't coded as such.

Unfortunately, h.264 standard says that every compliant stream
should start with IDR frame. And thus, some software (including
ffmpeg used by LPMS, as well as ffplay) refuse to decode or play
back streams that our segmenter produces. This in turn leads to
dreaded "ZeroSegments" errors.

Code in this commit is an attempt to fix this problem. It works
as follows:
- Fresh h264Fixer object should be created for each new segment
- NALs from segment should be fed into said object via its
process_nal() method
- If the first Intra-coded frame of the segment is an I frame, it
will get converted to the IDR frame. Then all following reference
rames will have their frame_num fields changed accordingly
- On detecting IDR frame Fixer will go into bypass mode, not
changing anything on the stream. Therefore, it is safe to use
Fixer with any stream - most of the time it will detect that the
stream starts with the IDR frame and just do nothing

Notes:
- Please take care to call process_nal() properly, that is with
at least four extra bytes in front of NAL in the supplied buffer
This is because sometimes slice header will "grow" due to the
changes that Fixer makes, and we want to avoid copying huge
Intra-coded slices
- This is not done yet, but Fixer can be easily modified to
perform some additional tasks, such as making sure no extra
copies of SPS/PPS are issued. To that effect, code should not
write SPS/PPS immediately, but only after slice requesting
particular SPS/PPS pair is observed. Then that particular SPS/PPS
should be marked as "present" in the stream and not written
anymore
numRefFramesInPicOrderCntCycle = bs.getUExpGolomb();
for (size_t i = 0; i < numRefFramesInPicOrderCntCycle; i++){
bs.getExpGolomb(); // offset_for_ref_frame[i]
}
return;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am an idiot - this "return" should get removed. Obviously, it was there with the WARN_MSG. I accidentally removed this file during formatting and had to recreate changes from older backup. Forgot about this one :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant