frame.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef __FRAME_H__
  2. #define __FRAME_H__
  3. #include "basic/basic.h"
  4. class __BasicFrame {
  5. public:
  6. AVFrame* frame = nullptr;
  7. __BasicFrame() = default;
  8. __BasicFrame(__BasicFrame&& rhs) noexcept
  9. {
  10. frame = rhs.frame;
  11. rhs.frame = nullptr;
  12. }
  13. __BasicFrame& operator=(__BasicFrame&& rhs)
  14. {
  15. Free(frame, [this] { av_frame_free(&frame); });
  16. frame = rhs.frame;
  17. rhs.frame = nullptr;
  18. return *this;
  19. }
  20. __BasicFrame(const __BasicFrame& rhs) = delete;
  21. __BasicFrame& operator=(const __BasicFrame& rhs) = delete;
  22. ~__BasicFrame()
  23. {
  24. Free(frame, [this] { av_frame_free(&frame); });
  25. }
  26. };
  27. template <MediaType mediaType>
  28. class Frame;
  29. template <>
  30. class Frame<MediaType::AUDIO> : public __BasicFrame {
  31. public:
  32. static AVFrame* Alloc(AVSampleFormat sampleFmt,
  33. const AVChannelLayout* channel_layout,
  34. int sampleRate, int nbSamples);
  35. Frame(AVSampleFormat sampleFmt,
  36. const AVChannelLayout* channel_layout, int sampleRate,
  37. int nbSamples);
  38. Frame(AVFrame* frame);
  39. Frame() = default;
  40. };
  41. template <>
  42. class Frame<MediaType::VIDEO> : public __BasicFrame {
  43. public:
  44. static AVFrame* Alloc(AVPixelFormat pixFmt, int width, int height);
  45. Frame(AVPixelFormat pixFmt, int width, int height);
  46. Frame(AVFrame* frame);
  47. Frame() = default;
  48. };
  49. struct SwsContext;
  50. class FfmpegConverter {
  51. private:
  52. AVPixelFormat _from;
  53. AVPixelFormat _to;
  54. public:
  55. FfmpegConverter(AVPixelFormat from, AVPixelFormat to)
  56. : _from(from)
  57. , _to(to)
  58. {
  59. }
  60. bool SetSize(int width, int height);
  61. AVFrame* Trans(AVFrame* frameFrom);
  62. ~FfmpegConverter();
  63. private:
  64. AVFrame* _frameTo = nullptr;
  65. SwsContext* _swsCtx = nullptr;
  66. };
  67. #endif