static void writeParameters(this EncodedBuffer buffer, byte[] source, string what) { var span = buffer.span; int pos = EmulationPrevention.writeStartCode4(span, 0); // pos = EmulationPrevention.writeBytes( span, source.AsSpan(), pos ); source.AsSpan().CopyTo(span.Slice(pos)); pos += source.Length; buffer.setLength(pos); Logger.logVerbose("Wrote {0} into buffer #{1}; took {2} bytes including start code", what, buffer.index, pos); }
/* protected override int writeFrame( EncodedBuffer destBuffer, int cb ) * { * // Read the complete sample * Span<byte> dest = destBuffer.span; * stream.Read( dest.Slice( 0, cb ) ); * * // Produce NALU start codes. * for( int offset = 0; offset < cb; ) * { * int naluLength = ( dest[ offset ] << 16 ) | ( dest[ offset + 1 ] << 8 ) | dest[ offset + 2 ]; * * dest[ offset ] = 0; * dest[ offset + 1 ] = 0; * dest[ offset + 2 ] = 1; * * offset += ( naluLength + 3 ); * } * return cb; * } */ protected override int writeNalu(EncodedBuffer destBuffer, out eNaluAction result, out eNaluType type) { Span <byte> naluLength = stackalloc byte[4]; naluLength[0] = 0; stream.read(naluLength.Slice(1)); int cbNalu = BitConverter.ToInt32(naluLength).endian(); Span <byte> dest = destBuffer.span; // Write NALU start code to mapped memory EmulationPrevention.writeStartCode4(dest, 0); // Read NALU payload from file into mapped memory Span <byte> naluPayload = dest.Slice(4, cbNalu); stream.read(naluPayload); destBuffer.setLength(cbNalu + 4); type = setBufferMetadata(destBuffer, naluPayload, out result); return(cbNalu + 3); }
eNaluAction iVideoTrackReader.writeNextNalu(EncodedBuffer dest) { if (EOF) { return(eNaluAction.EOF); } int naluLength; Span <byte> naluPayload; lock (clusters.syncRoot) { // Seek to the blob seek(ref readerState); // Read 4-bytes NALU length. Unlike mpeg4, MKV header never says it's 4 bytes, i.e. just guessing here. Span <byte> naluLengthSpan = stackalloc byte[4]; read(ref readerState, naluLengthSpan); naluLength = BinaryPrimitives.ReadInt32BigEndian(naluLengthSpan); Debug.Assert(readerState.bytesLeft >= naluLength); // Write NALU start code to mapped memory EmulationPrevention.writeStartCode4(dest.span, 0); // Write the payload naluPayload = dest.span.Slice(4, naluLength); read(ref readerState, naluPayload); } dest.setLength(naluLength + 4); // Parse the payload. The shared memory mapped by the driver is both readable and writeable. eNaluAction act = setBufferMetadata(dest, naluPayload); if (readerState.bytesLeft <= 0) { advance(); } return(act); }