Skip to content

Commit

Permalink
Merging Pull Request BogdanovKirill#64, BogdanovKirill#87 and Bogdano…
Browse files Browse the repository at this point in the history
…vKirill#125 from BogdanovKirill/RtspClientSharp repository
  • Loading branch information
Davide Galli committed Nov 28, 2023
1 parent cd7170f commit 8674677
Show file tree
Hide file tree
Showing 28 changed files with 760 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ namespace SimpleRtspPlayer.RawFramesDecoding.FFmpeg
enum FFmpegVideoCodecId
{
MJPEG = 7,
H264 = 27
MPEG4 = 12,
H264 = 27,
MXPEG = 145,
HVEC = 173,
}

[Flags]
Expand All @@ -22,8 +25,37 @@ enum FFmpegScalingQuality
enum FFmpegPixelFormat
{
None = -1,
/// <summary>
/// planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
/// </summary>
YUV420P = 0,
/// <summary>
/// packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
/// </summary>
YUYV422 = 1,
/// <summary>
/// packed RGB 8:8:8, 24bpp, RGBRGB...
/// </summary>
RGB24 = 2,
/// <summary>
/// packed RGB 8:8:8, 24bpp, BGRBGR...
/// </summary>
BGR24 = 3,
/// <summary>
/// planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
/// </summary>
YUV422P = 4,
/// <summary>
/// planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
/// </summary>
YUV444P = 5,
/// <summary>
/// Y , 8bpp
/// </summary>
GRAY8 = 8,
/// <summary>
/// packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
/// </summary>
BGRA = 28
}

Expand Down
10 changes: 5 additions & 5 deletions Examples/libffmpeghelper/libffmpeghelper.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,32 @@
<ProjectGuid>{3C96BD24-3212-4CD8-86E3-63D1E3E38155}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>libffmpeghelper</RootNamespace>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
Expand Down
20 changes: 7 additions & 13 deletions RtspClientSharp/MediaParsers/H264Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace RtspClientSharp.MediaParsers
{
class H264Parser
class H264Parser(Func<DateTime> frameTimestampProvider)
{
private enum FrameType
{
Expand All @@ -18,27 +18,21 @@ private enum FrameType
PredictionFrame
}

public static readonly ArraySegment<byte> StartMarkerSegment = new ArraySegment<byte>(RawH264Frame.StartMarker);
public static readonly ArraySegment<byte> StartMarkerSegment = new(RawH264Frame.StartMarker);

private readonly Func<DateTime> _frameTimestampProvider;
private readonly BitStreamReader _bitStreamReader = new BitStreamReader();
private readonly Dictionary<int, byte[]> _spsMap = new Dictionary<int, byte[]>();
private readonly Dictionary<int, byte[]> _ppsMap = new Dictionary<int, byte[]>();
private readonly Func<DateTime> _frameTimestampProvider = frameTimestampProvider ?? throw new ArgumentNullException(nameof(frameTimestampProvider));
private readonly BitStreamReader _bitStreamReader = new();
private readonly Dictionary<int, byte[]> _spsMap = [];
private readonly Dictionary<int, byte[]> _ppsMap = [];
private bool _waitForIFrame = true;
private byte[] _spsPpsBytes = new byte[0];
private bool _updateSpsPpsBytes;
private int _sliceType = -1;

private readonly MemoryStream _frameStream;
private readonly MemoryStream _frameStream = new(8 * 1024);

public Action<RawFrame> FrameGenerated;

public H264Parser(Func<DateTime> frameTimestampProvider)
{
_frameTimestampProvider = frameTimestampProvider ?? throw new ArgumentNullException(nameof(frameTimestampProvider));
_frameStream = new MemoryStream(8 * 1024);
}

public void Parse(ArraySegment<byte> byteSegment, bool generateFrame)
{
Debug.Assert(byteSegment.Array != null, "byteSegment.Array != null");
Expand Down
185 changes: 185 additions & 0 deletions RtspClientSharp/MediaParsers/H265Parser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//using RtspClientSharp.RawFrames;
//using RtspClientSharp.RawFrames.Video;
//using RtspClientSharp.Utils;
//using System;
//using System.Collections.Generic;
//using System.Diagnostics;
//using System.IO;

//namespace RtspClientSharp.MediaParsers;
//class H265Parser(Func<DateTime> frameTimestampProvider)
//{
// private enum FrameType
// {
// Unknown,
// IntraFrame,
// PredictionFrame
// }


// public enum NalUnitType : byte
// {
// TRAIL_N = 0,
// TRAIL_R = 1,
// TSA_N = 2,
// TSA_R = 3,
// STSA_N = 4,
// STSA_R = 5,
// RADL_N = 6,
// RADL_R = 7,
// RASL_N = 8,
// RASL_R = 9,
// RSV_VCL_N10 = 10,
// RSV_VCL_R11 = 11,
// RSV_VCL_N12 = 12,
// RSV_VCL_R13 = 13,
// RSV_VCL_N14 = 14,
// RSV_VCL_R15 = 15,
// BLA_W_LP = 16,
// BLA_W_RADL = 17,
// BLA_N_LP = 18,
// IDR_W_RADL = 19,
// IDR_N_LP = 20,
// CRA_NUT = 21,
// RSV_IRAP_VCL22 = 22,
// RSV_IRAP_VCL23 = 23,
// RSV_VCL24 = 24,
// RSV_VCL25 = 25,
// RSV_VCL26 = 26,
// RSV_VCL27 = 27,
// RSV_VCL28 = 28,
// RSV_VCL29 = 29,
// RSV_VCL30 = 30,
// RSV_VCL31 = 31,
// VPS_NUT = 32,
// SPS_NUT = 33,
// PPS_NUT = 34,
// AUD_NUT = 35,
// EOS_NUT = 36,
// EOB_NUT = 37,
// FD_NUT = 38,
// PREFIX_SEI_NUT = 39,
// SUFFIX_SEI_NUT = 40,
// RSV_NVCL41 = 41,
// RSV_NVCL42 = 42,
// RSV_NVCL43 = 43,
// RSV_NVCL44 = 44,
// RSV_NVCL45 = 45,
// RSV_NVCL46 = 46,
// RSV_NVCL47 = 47,
// // 48-63: unspecified
// AP = 48,
// FU = 49,
// UNSPEC50 = 50,
// UNSPEC51 = 51,
// UNSPEC52 = 52,
// UNSPEC53 = 53,
// UNSPEC54 = 54,
// UNSPEC55 = 55,
// UNSPEC56 = 56,
// UNSPEC57 = 57,
// UNSPEC58 = 58,
// UNSPEC59 = 59,
// UNSPEC60 = 60,
// UNSPEC61 = 61,
// UNSPEC62 = 62,
// UNSPEC63 = 63,
// }

// public static readonly ArraySegment<byte> StartMarkerSegment = new(RawH265Frame.StartMarker);

// private readonly Func<DateTime> _frameTimestampProvider = frameTimestampProvider ?? throw new ArgumentNullException(nameof(frameTimestampProvider));
// private readonly BitStreamReader _bitStreamReader = new();
// private readonly Dictionary<int, byte[]> _spsMap = [];
// private readonly Dictionary<int, byte[]> _ppsMap = [];
// private bool _waitForIFrame = true;
// private byte[] _spsPpsBytes = new byte[0];
// private bool _updateSpsPpsBytes;
// private int _sliceType = -1;

// private readonly MemoryStream _frameStream = new(8 * 1024);

// public Action<RawFrame> FrameGenerated;


// public void Parse(ArraySegment<byte> byteSegment, bool generateFrame)
// {
// Debug.Assert(byteSegment.Array != null, "byteSegment.Array != null");

// if (ArrayUtils.StartsWith(byteSegment.Array, byteSegment.Offset, byteSegment.Count,
// RawH264Frame.StartMarker))
// H264Slicer.Slice(byteSegment, SlicerOnNalUnitFound);
// else
// ProcessNalUnit(byteSegment, false, ref generateFrame);

// if (generateFrame)
// TryGenerateFrame();
// }

// public void TryGenerateFrame()
// {
// if (_frameStream.Position == 0)
// return;

// var frameBytes = new ArraySegment<byte>(_frameStream.GetBuffer(), 0, (int)_frameStream.Position);
// _frameStream.Position = 0;
// TryGenerateFrame(frameBytes);
// }

// private void TryGenerateFrame(ArraySegment<byte> frameBytes)
// {
// if (_updateSpsPpsBytes)
// {
// UpdateSpsPpsBytes();
// _updateSpsPpsBytes = false;
// }

// if (_sliceType == -1 || _spsPpsBytes.Length == 0)
// return;

// FrameType frameType = GetFrameType(_sliceType);
// _sliceType = -1;
// DateTime frameTimestamp;

// if (frameType == FrameType.PredictionFrame && !_waitForIFrame)
// {
// frameTimestamp = _frameTimestampProvider();
// FrameGenerated?.Invoke(new RawH264PFrame(frameTimestamp, frameBytes));
// return;
// }

// if (frameType != FrameType.IntraFrame)
// return;

// _waitForIFrame = false;
// var byteSegment = new ArraySegment<byte>(_spsPpsBytes);

// frameTimestamp = _frameTimestampProvider();
// FrameGenerated?.Invoke(new RawH264IFrame(frameTimestamp, frameBytes, byteSegment));
// }

// public void ResetState()
// {
// _frameStream.Position = 0;
// _sliceType = -1;
// _waitForIFrame = true;
// }

// private void SlicerOnNalUnitFound(ArraySegment<byte> byteSegment)
// {
// bool generateFrame = false;
// ProcessNalUnit(byteSegment, true, ref generateFrame);
// }

// private void ProcessNalUnit(ArraySegment<byte> byteSegment, bool hasStartMarker, ref bool generateFrame)
// {
// Debug.Assert(byteSegment.Array != null, "byteSegment.Array != null");

// int offset = byteSegment.Offset;

// if (hasStartMarker)
// offset += RawH264Frame.StartMarker.Length;


// }
//}
23 changes: 23 additions & 0 deletions RtspClientSharp/MediaParsers/H265ParserException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Runtime.Serialization;

namespace RtspClientSharp.MediaParsers;
[Serializable]
public class H265ParserException : Exception
{
public H265ParserException()
{
}

public H265ParserException(string message) : base(message)
{
}

public H265ParserException(string message, Exception inner) : base(message, inner)
{
}

protected H265ParserException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
Loading

0 comments on commit 8674677

Please sign in to comment.