FfccVariableGroup.cs 3.8 KB

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