2
0

FfccVaribleGroup.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using FFmpeg.AutoGen;
  2. using System;
  3. namespace FF8
  4. {
  5. public class FfccVaribleGroup : IDisposable
  6. {
  7. #region Fields
  8. public unsafe AVCodec* _codec;
  9. public unsafe AVCodecContext* _codecContext;
  10. public unsafe AVFormatContext* _format;
  11. public unsafe AVFrame* _frame;
  12. public unsafe AVPacket* _packet;
  13. private bool disposedValue = false;
  14. #endregion Fields
  15. #region Constructors
  16. public unsafe FfccVaribleGroup()
  17. {
  18. Format = ffmpeg.avformat_alloc_context();
  19. Packet = ffmpeg.av_packet_alloc();
  20. Frame = ffmpeg.av_frame_alloc();
  21. CodecContext = null;
  22. StreamIndex = -1;
  23. }
  24. #endregion Constructors
  25. #region Destructors
  26. ~FfccVaribleGroup()
  27. {
  28. Dispose();
  29. }
  30. #endregion Destructors
  31. #region Properties
  32. /// <summary>
  33. /// Codec
  34. /// </summary>
  35. public unsafe AVCodec* Codec { get => _codec; set => _codec = value; }
  36. /// <summary>
  37. /// CodecContext
  38. /// </summary>
  39. public unsafe AVCodecContext* CodecContext { get => _codecContext; set => _codecContext = value; }
  40. /// <summary>
  41. /// Format holds alot of file info. File is opened and data about it is stored here.
  42. /// </summary>
  43. public unsafe AVFormatContext* Format { get => _format; set => _format = value; }
  44. /// <summary>
  45. /// Frame holds a chunk of data related to the current stream.
  46. /// </summary>
  47. public unsafe AVFrame* Frame { get => _frame; set => _frame = value; }
  48. /// <summary>
  49. /// Packet of data can contain 1 or more frames.
  50. /// </summary>
  51. public unsafe AVPacket* Packet { get => _packet; set => _packet = value; }
  52. /// <summary>
  53. /// Current Stream based on index
  54. /// </summary>
  55. public unsafe AVStream* Stream => StreamIndex >= 0 && Format != null ? Format->streams[StreamIndex] : null;
  56. /// <summary>
  57. /// Set Stream Index typically 0 is video 1 is audio, unless no video then 0 is audio. -1 for no stream of type.
  58. /// </summary>
  59. public int StreamIndex { get; set; }
  60. /// <summary>
  61. /// Type of current Stream.
  62. /// </summary>
  63. public unsafe AVMediaType Type => Stream != null ? Stream->codec->codec_type : AVMediaType.AVMEDIA_TYPE_UNKNOWN;
  64. #endregion Properties
  65. #region Methods
  66. // This code added to correctly implement the disposable pattern.
  67. public void Dispose()
  68. {
  69. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  70. Dispose(true);
  71. // TODO: uncomment the following line if the finalizer is overridden above.
  72. // GC.SuppressFinalize(this);
  73. }
  74. protected virtual unsafe void Dispose(bool disposing)
  75. {
  76. if (!disposedValue)
  77. {
  78. if (disposing)
  79. {
  80. // TODO: dispose managed state (managed objects).
  81. }
  82. if (_format != null)
  83. {
  84. fixed (AVFormatContext** tmp = &_format)
  85. {
  86. for (int i = 0; i < Format->nb_streams; i++)
  87. {
  88. ffmpeg.avcodec_close(Format->streams[i]->codec);
  89. }
  90. ffmpeg.avformat_close_input(tmp);
  91. }
  92. }
  93. if (_codecContext != null)
  94. {
  95. ffmpeg.avcodec_close(CodecContext);
  96. fixed (AVCodecContext** tmp = &_codecContext)
  97. {
  98. ffmpeg.avcodec_free_context(tmp); //ctd
  99. }
  100. }
  101. //ffmpeg.av_free(Codec); //CTD on linux
  102. ffmpeg.av_packet_unref(Packet);
  103. ffmpeg.av_free(Packet);
  104. ffmpeg.av_frame_unref(Frame);
  105. ffmpeg.av_free(Frame);
  106. ffmpeg.av_free(Stream);
  107. // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
  108. // TODO: set large fields to null.
  109. disposedValue = true;
  110. }
  111. }
  112. #endregion Methods
  113. }
  114. }