frame.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "basic/frame.h"
  2. extern "C" {
  3. #include <libswscale/swscale.h>
  4. }
  5. AVFrame* Frame<MediaType::AUDIO>::Alloc(AVSampleFormat sampleFmt,
  6. const AVChannelLayout* channel_layout,
  7. int sampleRate, int nbSamples)
  8. {
  9. AVFrame* frame = nullptr;
  10. __CheckNullptr(frame = av_frame_alloc());
  11. frame->format = sampleFmt;
  12. av_channel_layout_copy(&frame->ch_layout, channel_layout);
  13. frame->sample_rate = sampleRate;
  14. frame->nb_samples = nbSamples;
  15. /* allocate the buffers for the frame data */
  16. __CheckNullptr(av_frame_get_buffer(frame, 0) >= 0);
  17. return frame;
  18. }
  19. Frame<MediaType::AUDIO>::Frame(AVSampleFormat sampleFmt,
  20. const AVChannelLayout* channel_layout, int sampleRate,
  21. int nbSamples)
  22. {
  23. __CheckNo(frame = Alloc(sampleFmt, channel_layout, sampleRate, nbSamples));
  24. }
  25. Frame<MediaType::AUDIO>::Frame(AVFrame* frame)
  26. {
  27. if (frame == nullptr) {
  28. this->frame = nullptr;
  29. return;
  30. }
  31. __CheckNo(this->frame = Alloc(AVSampleFormat(frame->format), &frame->ch_layout, frame->sample_rate, frame->nb_samples));
  32. __CheckNo(av_frame_copy(this->frame, frame) >= 0);
  33. }
  34. Frame<MediaType::VIDEO>::Frame(AVPixelFormat pixFmt, int width, int height)
  35. {
  36. __CheckNo(frame = Alloc(pixFmt, width, height));
  37. }
  38. AVFrame* Frame<MediaType::VIDEO>::Alloc(AVPixelFormat pixFmt, int width, int height)
  39. {
  40. AVFrame* frame = nullptr;
  41. __CheckNullptr(frame = av_frame_alloc());
  42. frame->format = pixFmt;
  43. frame->width = width;
  44. frame->height = height;
  45. /* allocate the buffers for the frame data */
  46. __CheckNullptr(av_frame_get_buffer(frame, 0) >= 0);
  47. return frame;
  48. }
  49. Frame<MediaType::VIDEO>::Frame(AVFrame* frame)
  50. {
  51. if (frame == nullptr) {
  52. this->frame = nullptr;
  53. return;
  54. }
  55. __CheckNo(this->frame = Alloc(AVPixelFormat(frame->format), frame->width, frame->height));
  56. __CheckNo(av_frame_copy(this->frame, frame) >= 0);
  57. }
  58. bool FfmpegConverter::SetSize(int width, int height)
  59. {
  60. Free(_swsCtx, [this] { sws_freeContext(_swsCtx); });
  61. Free(_frameTo, [this] { av_frame_free(&_frameTo); });
  62. // 创建格式转换
  63. __CheckBool(_swsCtx = sws_getContext(
  64. width, height, _from,
  65. width, height, _to,
  66. 0, NULL, NULL, NULL));
  67. __CheckBool(_frameTo = Frame<MediaType::VIDEO>::Alloc(_to, width, height));
  68. return true;
  69. }
  70. AVFrame* FfmpegConverter::Trans(AVFrame* frameFrom)
  71. {
  72. // 如果是空指针,直接把缓存返回
  73. if (frameFrom == nullptr) {
  74. return _frameTo;
  75. }
  76. __CheckNullptr(
  77. sws_scale(_swsCtx, (const uint8_t* const*)frameFrom->data,
  78. frameFrom->linesize, 0, frameFrom->height, _frameTo->data,
  79. _frameTo->linesize)
  80. >= 0);
  81. return _frameTo;
  82. }
  83. FfmpegConverter::~FfmpegConverter()
  84. {
  85. Free(_swsCtx, [this] { sws_freeContext(_swsCtx); });
  86. Free(_frameTo, [this] { av_frame_free(&_frameTo); });
  87. }