h.264 "fixer" tool
Created by: MikeIndiaAlpha
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;
}