FFmpegContext.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using FFmpeg.AutoGen;
  2. using System;
  3. namespace Ryujinx.Graphics.Nvdec.H264
  4. {
  5. unsafe class FFmpegContext : IDisposable
  6. {
  7. private readonly AVCodec* _codec;
  8. private AVCodecContext* _context;
  9. public FFmpegContext()
  10. {
  11. _codec = ffmpeg.avcodec_find_decoder(AVCodecID.AV_CODEC_ID_H264);
  12. _context = ffmpeg.avcodec_alloc_context3(_codec);
  13. ffmpeg.avcodec_open2(_context, _codec, null);
  14. }
  15. public int DecodeFrame(Surface output, ReadOnlySpan<byte> bitstream)
  16. {
  17. AVPacket packet;
  18. ffmpeg.av_init_packet(&packet);
  19. fixed (byte* ptr = bitstream)
  20. {
  21. packet.data = ptr;
  22. packet.size = bitstream.Length;
  23. int rc = ffmpeg.avcodec_send_packet(_context, &packet);
  24. if (rc != 0)
  25. {
  26. return rc;
  27. }
  28. }
  29. return ffmpeg.avcodec_receive_frame(_context, output.Frame);
  30. }
  31. public void Dispose()
  32. {
  33. ffmpeg.avcodec_close(_context);
  34. fixed (AVCodecContext** ppContext = &_context)
  35. {
  36. ffmpeg.avcodec_free_context(ppContext);
  37. }
  38. }
  39. }
  40. }